0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef _NET_IP_FIB_H
0013 #define _NET_IP_FIB_H
0014
0015 #include <net/flow.h>
0016 #include <linux/seq_file.h>
0017 #include <linux/rcupdate.h>
0018 #include <net/fib_notifier.h>
0019 #include <net/fib_rules.h>
0020 #include <net/inet_dscp.h>
0021 #include <net/inetpeer.h>
0022 #include <linux/percpu.h>
0023 #include <linux/notifier.h>
0024 #include <linux/refcount.h>
0025
0026 struct fib_config {
0027 u8 fc_dst_len;
0028 dscp_t fc_dscp;
0029 u8 fc_protocol;
0030 u8 fc_scope;
0031 u8 fc_type;
0032 u8 fc_gw_family;
0033
0034 u32 fc_table;
0035 __be32 fc_dst;
0036 union {
0037 __be32 fc_gw4;
0038 struct in6_addr fc_gw6;
0039 };
0040 int fc_oif;
0041 u32 fc_flags;
0042 u32 fc_priority;
0043 __be32 fc_prefsrc;
0044 u32 fc_nh_id;
0045 struct nlattr *fc_mx;
0046 struct rtnexthop *fc_mp;
0047 int fc_mx_len;
0048 int fc_mp_len;
0049 u32 fc_flow;
0050 u32 fc_nlflags;
0051 struct nl_info fc_nlinfo;
0052 struct nlattr *fc_encap;
0053 u16 fc_encap_type;
0054 };
0055
0056 struct fib_info;
0057 struct rtable;
0058
0059 struct fib_nh_exception {
0060 struct fib_nh_exception __rcu *fnhe_next;
0061 int fnhe_genid;
0062 __be32 fnhe_daddr;
0063 u32 fnhe_pmtu;
0064 bool fnhe_mtu_locked;
0065 __be32 fnhe_gw;
0066 unsigned long fnhe_expires;
0067 struct rtable __rcu *fnhe_rth_input;
0068 struct rtable __rcu *fnhe_rth_output;
0069 unsigned long fnhe_stamp;
0070 struct rcu_head rcu;
0071 };
0072
0073 struct fnhe_hash_bucket {
0074 struct fib_nh_exception __rcu *chain;
0075 };
0076
0077 #define FNHE_HASH_SHIFT 11
0078 #define FNHE_HASH_SIZE (1 << FNHE_HASH_SHIFT)
0079 #define FNHE_RECLAIM_DEPTH 5
0080
0081 struct fib_nh_common {
0082 struct net_device *nhc_dev;
0083 netdevice_tracker nhc_dev_tracker;
0084 int nhc_oif;
0085 unsigned char nhc_scope;
0086 u8 nhc_family;
0087 u8 nhc_gw_family;
0088 unsigned char nhc_flags;
0089 struct lwtunnel_state *nhc_lwtstate;
0090
0091 union {
0092 __be32 ipv4;
0093 struct in6_addr ipv6;
0094 } nhc_gw;
0095
0096 int nhc_weight;
0097 atomic_t nhc_upper_bound;
0098
0099
0100 struct rtable __rcu * __percpu *nhc_pcpu_rth_output;
0101 struct rtable __rcu *nhc_rth_input;
0102 struct fnhe_hash_bucket __rcu *nhc_exceptions;
0103 };
0104
0105 struct fib_nh {
0106 struct fib_nh_common nh_common;
0107 struct hlist_node nh_hash;
0108 struct fib_info *nh_parent;
0109 #ifdef CONFIG_IP_ROUTE_CLASSID
0110 __u32 nh_tclassid;
0111 #endif
0112 __be32 nh_saddr;
0113 int nh_saddr_genid;
0114 #define fib_nh_family nh_common.nhc_family
0115 #define fib_nh_dev nh_common.nhc_dev
0116 #define fib_nh_dev_tracker nh_common.nhc_dev_tracker
0117 #define fib_nh_oif nh_common.nhc_oif
0118 #define fib_nh_flags nh_common.nhc_flags
0119 #define fib_nh_lws nh_common.nhc_lwtstate
0120 #define fib_nh_scope nh_common.nhc_scope
0121 #define fib_nh_gw_family nh_common.nhc_gw_family
0122 #define fib_nh_gw4 nh_common.nhc_gw.ipv4
0123 #define fib_nh_gw6 nh_common.nhc_gw.ipv6
0124 #define fib_nh_weight nh_common.nhc_weight
0125 #define fib_nh_upper_bound nh_common.nhc_upper_bound
0126 };
0127
0128
0129
0130
0131
0132 struct nexthop;
0133
0134 struct fib_info {
0135 struct hlist_node fib_hash;
0136 struct hlist_node fib_lhash;
0137 struct list_head nh_list;
0138 struct net *fib_net;
0139 refcount_t fib_treeref;
0140 refcount_t fib_clntref;
0141 unsigned int fib_flags;
0142 unsigned char fib_dead;
0143 unsigned char fib_protocol;
0144 unsigned char fib_scope;
0145 unsigned char fib_type;
0146 __be32 fib_prefsrc;
0147 u32 fib_tb_id;
0148 u32 fib_priority;
0149 struct dst_metrics *fib_metrics;
0150 #define fib_mtu fib_metrics->metrics[RTAX_MTU-1]
0151 #define fib_window fib_metrics->metrics[RTAX_WINDOW-1]
0152 #define fib_rtt fib_metrics->metrics[RTAX_RTT-1]
0153 #define fib_advmss fib_metrics->metrics[RTAX_ADVMSS-1]
0154 int fib_nhs;
0155 bool fib_nh_is_v6;
0156 bool nh_updated;
0157 struct nexthop *nh;
0158 struct rcu_head rcu;
0159 struct fib_nh fib_nh[];
0160 };
0161
0162
0163 #ifdef CONFIG_IP_MULTIPLE_TABLES
0164 struct fib_rule;
0165 #endif
0166
0167 struct fib_table;
0168 struct fib_result {
0169 __be32 prefix;
0170 unsigned char prefixlen;
0171 unsigned char nh_sel;
0172 unsigned char type;
0173 unsigned char scope;
0174 u32 tclassid;
0175 struct fib_nh_common *nhc;
0176 struct fib_info *fi;
0177 struct fib_table *table;
0178 struct hlist_head *fa_head;
0179 };
0180
0181 struct fib_result_nl {
0182 __be32 fl_addr;
0183 u32 fl_mark;
0184 unsigned char fl_tos;
0185 unsigned char fl_scope;
0186 unsigned char tb_id_in;
0187
0188 unsigned char tb_id;
0189 unsigned char prefixlen;
0190 unsigned char nh_sel;
0191 unsigned char type;
0192 unsigned char scope;
0193 int err;
0194 };
0195
0196 #ifdef CONFIG_IP_MULTIPLE_TABLES
0197 #define FIB_TABLE_HASHSZ 256
0198 #else
0199 #define FIB_TABLE_HASHSZ 2
0200 #endif
0201
0202 __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
0203 unsigned char scope);
0204 __be32 fib_result_prefsrc(struct net *net, struct fib_result *res);
0205
0206 #define FIB_RES_NHC(res) ((res).nhc)
0207 #define FIB_RES_DEV(res) (FIB_RES_NHC(res)->nhc_dev)
0208 #define FIB_RES_OIF(res) (FIB_RES_NHC(res)->nhc_oif)
0209
0210 struct fib_rt_info {
0211 struct fib_info *fi;
0212 u32 tb_id;
0213 __be32 dst;
0214 int dst_len;
0215 dscp_t dscp;
0216 u8 type;
0217 u8 offload:1,
0218 trap:1,
0219 offload_failed:1,
0220 unused:5;
0221 };
0222
0223 struct fib_entry_notifier_info {
0224 struct fib_notifier_info info;
0225 u32 dst;
0226 int dst_len;
0227 struct fib_info *fi;
0228 dscp_t dscp;
0229 u8 type;
0230 u32 tb_id;
0231 };
0232
0233 struct fib_nh_notifier_info {
0234 struct fib_notifier_info info;
0235 struct fib_nh *fib_nh;
0236 };
0237
0238 int call_fib4_notifier(struct notifier_block *nb,
0239 enum fib_event_type event_type,
0240 struct fib_notifier_info *info);
0241 int call_fib4_notifiers(struct net *net, enum fib_event_type event_type,
0242 struct fib_notifier_info *info);
0243
0244 int __net_init fib4_notifier_init(struct net *net);
0245 void __net_exit fib4_notifier_exit(struct net *net);
0246
0247 void fib_info_notify_update(struct net *net, struct nl_info *info);
0248 int fib_notify(struct net *net, struct notifier_block *nb,
0249 struct netlink_ext_ack *extack);
0250
0251 struct fib_table {
0252 struct hlist_node tb_hlist;
0253 u32 tb_id;
0254 int tb_num_default;
0255 struct rcu_head rcu;
0256 unsigned long *tb_data;
0257 unsigned long __data[];
0258 };
0259
0260 struct fib_dump_filter {
0261 u32 table_id;
0262
0263 bool filter_set;
0264 bool dump_routes;
0265 bool dump_exceptions;
0266 unsigned char protocol;
0267 unsigned char rt_type;
0268 unsigned int flags;
0269 struct net_device *dev;
0270 };
0271
0272 int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
0273 struct fib_result *res, int fib_flags);
0274 int fib_table_insert(struct net *, struct fib_table *, struct fib_config *,
0275 struct netlink_ext_ack *extack);
0276 int fib_table_delete(struct net *, struct fib_table *, struct fib_config *,
0277 struct netlink_ext_ack *extack);
0278 int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
0279 struct netlink_callback *cb, struct fib_dump_filter *filter);
0280 int fib_table_flush(struct net *net, struct fib_table *table, bool flush_all);
0281 struct fib_table *fib_trie_unmerge(struct fib_table *main_tb);
0282 void fib_table_flush_external(struct fib_table *table);
0283 void fib_free_table(struct fib_table *tb);
0284
0285 #ifndef CONFIG_IP_MULTIPLE_TABLES
0286
0287 #define TABLE_LOCAL_INDEX (RT_TABLE_LOCAL & (FIB_TABLE_HASHSZ - 1))
0288 #define TABLE_MAIN_INDEX (RT_TABLE_MAIN & (FIB_TABLE_HASHSZ - 1))
0289
0290 static inline struct fib_table *fib_get_table(struct net *net, u32 id)
0291 {
0292 struct hlist_node *tb_hlist;
0293 struct hlist_head *ptr;
0294
0295 ptr = id == RT_TABLE_LOCAL ?
0296 &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX] :
0297 &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX];
0298
0299 tb_hlist = rcu_dereference_rtnl(hlist_first_rcu(ptr));
0300
0301 return hlist_entry(tb_hlist, struct fib_table, tb_hlist);
0302 }
0303
0304 static inline struct fib_table *fib_new_table(struct net *net, u32 id)
0305 {
0306 return fib_get_table(net, id);
0307 }
0308
0309 static inline int fib_lookup(struct net *net, const struct flowi4 *flp,
0310 struct fib_result *res, unsigned int flags)
0311 {
0312 struct fib_table *tb;
0313 int err = -ENETUNREACH;
0314
0315 rcu_read_lock();
0316
0317 tb = fib_get_table(net, RT_TABLE_MAIN);
0318 if (tb)
0319 err = fib_table_lookup(tb, flp, res, flags | FIB_LOOKUP_NOREF);
0320
0321 if (err == -EAGAIN)
0322 err = -ENETUNREACH;
0323
0324 rcu_read_unlock();
0325
0326 return err;
0327 }
0328
0329 static inline bool fib4_has_custom_rules(const struct net *net)
0330 {
0331 return false;
0332 }
0333
0334 static inline bool fib4_rule_default(const struct fib_rule *rule)
0335 {
0336 return true;
0337 }
0338
0339 static inline int fib4_rules_dump(struct net *net, struct notifier_block *nb,
0340 struct netlink_ext_ack *extack)
0341 {
0342 return 0;
0343 }
0344
0345 static inline unsigned int fib4_rules_seq_read(struct net *net)
0346 {
0347 return 0;
0348 }
0349
0350 static inline bool fib4_rules_early_flow_dissect(struct net *net,
0351 struct sk_buff *skb,
0352 struct flowi4 *fl4,
0353 struct flow_keys *flkeys)
0354 {
0355 return false;
0356 }
0357 #else
0358 int __net_init fib4_rules_init(struct net *net);
0359 void __net_exit fib4_rules_exit(struct net *net);
0360
0361 struct fib_table *fib_new_table(struct net *net, u32 id);
0362 struct fib_table *fib_get_table(struct net *net, u32 id);
0363
0364 int __fib_lookup(struct net *net, struct flowi4 *flp,
0365 struct fib_result *res, unsigned int flags);
0366
0367 static inline int fib_lookup(struct net *net, struct flowi4 *flp,
0368 struct fib_result *res, unsigned int flags)
0369 {
0370 struct fib_table *tb;
0371 int err = -ENETUNREACH;
0372
0373 flags |= FIB_LOOKUP_NOREF;
0374 if (net->ipv4.fib_has_custom_rules)
0375 return __fib_lookup(net, flp, res, flags);
0376
0377 rcu_read_lock();
0378
0379 res->tclassid = 0;
0380
0381 tb = rcu_dereference_rtnl(net->ipv4.fib_main);
0382 if (tb)
0383 err = fib_table_lookup(tb, flp, res, flags);
0384
0385 if (!err)
0386 goto out;
0387
0388 tb = rcu_dereference_rtnl(net->ipv4.fib_default);
0389 if (tb)
0390 err = fib_table_lookup(tb, flp, res, flags);
0391
0392 out:
0393 if (err == -EAGAIN)
0394 err = -ENETUNREACH;
0395
0396 rcu_read_unlock();
0397
0398 return err;
0399 }
0400
0401 static inline bool fib4_has_custom_rules(const struct net *net)
0402 {
0403 return net->ipv4.fib_has_custom_rules;
0404 }
0405
0406 bool fib4_rule_default(const struct fib_rule *rule);
0407 int fib4_rules_dump(struct net *net, struct notifier_block *nb,
0408 struct netlink_ext_ack *extack);
0409 unsigned int fib4_rules_seq_read(struct net *net);
0410
0411 static inline bool fib4_rules_early_flow_dissect(struct net *net,
0412 struct sk_buff *skb,
0413 struct flowi4 *fl4,
0414 struct flow_keys *flkeys)
0415 {
0416 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
0417
0418 if (!net->ipv4.fib_rules_require_fldissect)
0419 return false;
0420
0421 skb_flow_dissect_flow_keys(skb, flkeys, flag);
0422 fl4->fl4_sport = flkeys->ports.src;
0423 fl4->fl4_dport = flkeys->ports.dst;
0424 fl4->flowi4_proto = flkeys->basic.ip_proto;
0425
0426 return true;
0427 }
0428
0429 #endif
0430
0431
0432 extern const struct nla_policy rtm_ipv4_policy[];
0433 void ip_fib_init(void);
0434 int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla,
0435 struct netlink_ext_ack *extack);
0436 __be32 fib_compute_spec_dst(struct sk_buff *skb);
0437 bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev);
0438 int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
0439 u8 tos, int oif, struct net_device *dev,
0440 struct in_device *idev, u32 *itag);
0441 #ifdef CONFIG_IP_ROUTE_CLASSID
0442 static inline int fib_num_tclassid_users(struct net *net)
0443 {
0444 return atomic_read(&net->ipv4.fib_num_tclassid_users);
0445 }
0446 #else
0447 static inline int fib_num_tclassid_users(struct net *net)
0448 {
0449 return 0;
0450 }
0451 #endif
0452 int fib_unmerge(struct net *net);
0453
0454 static inline bool nhc_l3mdev_matches_dev(const struct fib_nh_common *nhc,
0455 const struct net_device *dev)
0456 {
0457 if (nhc->nhc_dev == dev ||
0458 l3mdev_master_ifindex_rcu(nhc->nhc_dev) == dev->ifindex)
0459 return true;
0460
0461 return false;
0462 }
0463
0464
0465 int ip_fib_check_default(__be32 gw, struct net_device *dev);
0466 int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force);
0467 int fib_sync_down_addr(struct net_device *dev, __be32 local);
0468 int fib_sync_up(struct net_device *dev, unsigned char nh_flags);
0469 void fib_sync_mtu(struct net_device *dev, u32 orig_mtu);
0470 void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig);
0471
0472
0473
0474
0475
0476
0477 #define FIB_MULTIPATH_HASH_FIELD_SRC_IP BIT(0)
0478 #define FIB_MULTIPATH_HASH_FIELD_DST_IP BIT(1)
0479 #define FIB_MULTIPATH_HASH_FIELD_IP_PROTO BIT(2)
0480 #define FIB_MULTIPATH_HASH_FIELD_FLOWLABEL BIT(3)
0481 #define FIB_MULTIPATH_HASH_FIELD_SRC_PORT BIT(4)
0482 #define FIB_MULTIPATH_HASH_FIELD_DST_PORT BIT(5)
0483 #define FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP BIT(6)
0484 #define FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP BIT(7)
0485 #define FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO BIT(8)
0486 #define FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL BIT(9)
0487 #define FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT BIT(10)
0488 #define FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT BIT(11)
0489
0490 #define FIB_MULTIPATH_HASH_FIELD_OUTER_MASK \
0491 (FIB_MULTIPATH_HASH_FIELD_SRC_IP | \
0492 FIB_MULTIPATH_HASH_FIELD_DST_IP | \
0493 FIB_MULTIPATH_HASH_FIELD_IP_PROTO | \
0494 FIB_MULTIPATH_HASH_FIELD_FLOWLABEL | \
0495 FIB_MULTIPATH_HASH_FIELD_SRC_PORT | \
0496 FIB_MULTIPATH_HASH_FIELD_DST_PORT)
0497
0498 #define FIB_MULTIPATH_HASH_FIELD_INNER_MASK \
0499 (FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP | \
0500 FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP | \
0501 FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO | \
0502 FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL | \
0503 FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT | \
0504 FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
0505
0506 #define FIB_MULTIPATH_HASH_FIELD_ALL_MASK \
0507 (FIB_MULTIPATH_HASH_FIELD_OUTER_MASK | \
0508 FIB_MULTIPATH_HASH_FIELD_INNER_MASK)
0509
0510 #define FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK \
0511 (FIB_MULTIPATH_HASH_FIELD_SRC_IP | \
0512 FIB_MULTIPATH_HASH_FIELD_DST_IP | \
0513 FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
0514
0515 #ifdef CONFIG_IP_ROUTE_MULTIPATH
0516 int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
0517 const struct sk_buff *skb, struct flow_keys *flkeys);
0518 #endif
0519 int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
0520 struct netlink_ext_ack *extack);
0521 void fib_select_multipath(struct fib_result *res, int hash);
0522 void fib_select_path(struct net *net, struct fib_result *res,
0523 struct flowi4 *fl4, const struct sk_buff *skb);
0524
0525 int fib_nh_init(struct net *net, struct fib_nh *fib_nh,
0526 struct fib_config *cfg, int nh_weight,
0527 struct netlink_ext_ack *extack);
0528 void fib_nh_release(struct net *net, struct fib_nh *fib_nh);
0529 int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
0530 struct nlattr *fc_encap, u16 fc_encap_type,
0531 void *cfg, gfp_t gfp_flags,
0532 struct netlink_ext_ack *extack);
0533 void fib_nh_common_release(struct fib_nh_common *nhc);
0534
0535
0536 void fib_alias_hw_flags_set(struct net *net, const struct fib_rt_info *fri);
0537 void fib_trie_init(void);
0538 struct fib_table *fib_trie_table(u32 id, struct fib_table *alias);
0539 bool fib_lookup_good_nhc(const struct fib_nh_common *nhc, int fib_flags,
0540 const struct flowi4 *flp);
0541
0542 static inline void fib_combine_itag(u32 *itag, const struct fib_result *res)
0543 {
0544 #ifdef CONFIG_IP_ROUTE_CLASSID
0545 struct fib_nh_common *nhc = res->nhc;
0546 #ifdef CONFIG_IP_MULTIPLE_TABLES
0547 u32 rtag;
0548 #endif
0549 if (nhc->nhc_family == AF_INET) {
0550 struct fib_nh *nh;
0551
0552 nh = container_of(nhc, struct fib_nh, nh_common);
0553 *itag = nh->nh_tclassid << 16;
0554 } else {
0555 *itag = 0;
0556 }
0557
0558 #ifdef CONFIG_IP_MULTIPLE_TABLES
0559 rtag = res->tclassid;
0560 if (*itag == 0)
0561 *itag = (rtag<<16);
0562 *itag |= (rtag>>16);
0563 #endif
0564 #endif
0565 }
0566
0567 void fib_flush(struct net *net);
0568 void free_fib_info(struct fib_info *fi);
0569
0570 static inline void fib_info_hold(struct fib_info *fi)
0571 {
0572 refcount_inc(&fi->fib_clntref);
0573 }
0574
0575 static inline void fib_info_put(struct fib_info *fi)
0576 {
0577 if (refcount_dec_and_test(&fi->fib_clntref))
0578 free_fib_info(fi);
0579 }
0580
0581 #ifdef CONFIG_PROC_FS
0582 int __net_init fib_proc_init(struct net *net);
0583 void __net_exit fib_proc_exit(struct net *net);
0584 #else
0585 static inline int fib_proc_init(struct net *net)
0586 {
0587 return 0;
0588 }
0589 static inline void fib_proc_exit(struct net *net)
0590 {
0591 }
0592 #endif
0593
0594 u32 ip_mtu_from_fib_result(struct fib_result *res, __be32 daddr);
0595
0596 int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
0597 struct fib_dump_filter *filter,
0598 struct netlink_callback *cb);
0599
0600 int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nh,
0601 u8 rt_family, unsigned char *flags, bool skip_oif);
0602 int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nh,
0603 int nh_weight, u8 rt_family, u32 nh_tclassid);
0604 #endif