0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _IP6_FIB_H
0010 #define _IP6_FIB_H
0011
0012 #include <linux/ipv6_route.h>
0013 #include <linux/rtnetlink.h>
0014 #include <linux/spinlock.h>
0015 #include <linux/notifier.h>
0016 #include <net/dst.h>
0017 #include <net/flow.h>
0018 #include <net/ip_fib.h>
0019 #include <net/netlink.h>
0020 #include <net/inetpeer.h>
0021 #include <net/fib_notifier.h>
0022 #include <linux/indirect_call_wrapper.h>
0023 #include <uapi/linux/bpf.h>
0024
0025 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
0026 #define FIB6_TABLE_HASHSZ 256
0027 #else
0028 #define FIB6_TABLE_HASHSZ 1
0029 #endif
0030
0031 #define RT6_DEBUG 2
0032
0033 #if RT6_DEBUG >= 3
0034 #define RT6_TRACE(x...) pr_debug(x)
0035 #else
0036 #define RT6_TRACE(x...) do { ; } while (0)
0037 #endif
0038
0039 struct rt6_info;
0040 struct fib6_info;
0041
0042 struct fib6_config {
0043 u32 fc_table;
0044 u32 fc_metric;
0045 int fc_dst_len;
0046 int fc_src_len;
0047 int fc_ifindex;
0048 u32 fc_flags;
0049 u32 fc_protocol;
0050 u16 fc_type;
0051 u16 fc_delete_all_nh : 1,
0052 fc_ignore_dev_down:1,
0053 __unused : 14;
0054 u32 fc_nh_id;
0055
0056 struct in6_addr fc_dst;
0057 struct in6_addr fc_src;
0058 struct in6_addr fc_prefsrc;
0059 struct in6_addr fc_gateway;
0060
0061 unsigned long fc_expires;
0062 struct nlattr *fc_mx;
0063 int fc_mx_len;
0064 int fc_mp_len;
0065 struct nlattr *fc_mp;
0066
0067 struct nl_info fc_nlinfo;
0068 struct nlattr *fc_encap;
0069 u16 fc_encap_type;
0070 bool fc_is_fdb;
0071 };
0072
0073 struct fib6_node {
0074 struct fib6_node __rcu *parent;
0075 struct fib6_node __rcu *left;
0076 struct fib6_node __rcu *right;
0077 #ifdef CONFIG_IPV6_SUBTREES
0078 struct fib6_node __rcu *subtree;
0079 #endif
0080 struct fib6_info __rcu *leaf;
0081
0082 __u16 fn_bit;
0083 __u16 fn_flags;
0084 int fn_sernum;
0085 struct fib6_info __rcu *rr_ptr;
0086 struct rcu_head rcu;
0087 };
0088
0089 struct fib6_gc_args {
0090 int timeout;
0091 int more;
0092 };
0093
0094 #ifndef CONFIG_IPV6_SUBTREES
0095 #define FIB6_SUBTREE(fn) NULL
0096
0097 static inline bool fib6_routes_require_src(const struct net *net)
0098 {
0099 return false;
0100 }
0101
0102 static inline void fib6_routes_require_src_inc(struct net *net) {}
0103 static inline void fib6_routes_require_src_dec(struct net *net) {}
0104
0105 #else
0106
0107 static inline bool fib6_routes_require_src(const struct net *net)
0108 {
0109 return net->ipv6.fib6_routes_require_src > 0;
0110 }
0111
0112 static inline void fib6_routes_require_src_inc(struct net *net)
0113 {
0114 net->ipv6.fib6_routes_require_src++;
0115 }
0116
0117 static inline void fib6_routes_require_src_dec(struct net *net)
0118 {
0119 net->ipv6.fib6_routes_require_src--;
0120 }
0121
0122 #define FIB6_SUBTREE(fn) (rcu_dereference_protected((fn)->subtree, 1))
0123 #endif
0124
0125
0126
0127
0128
0129
0130 struct rt6key {
0131 struct in6_addr addr;
0132 int plen;
0133 };
0134
0135 struct fib6_table;
0136
0137 struct rt6_exception_bucket {
0138 struct hlist_head chain;
0139 int depth;
0140 };
0141
0142 struct rt6_exception {
0143 struct hlist_node hlist;
0144 struct rt6_info *rt6i;
0145 unsigned long stamp;
0146 struct rcu_head rcu;
0147 };
0148
0149 #define FIB6_EXCEPTION_BUCKET_SIZE_SHIFT 10
0150 #define FIB6_EXCEPTION_BUCKET_SIZE (1 << FIB6_EXCEPTION_BUCKET_SIZE_SHIFT)
0151 #define FIB6_MAX_DEPTH 5
0152
0153 struct fib6_nh {
0154 struct fib_nh_common nh_common;
0155
0156 #ifdef CONFIG_IPV6_ROUTER_PREF
0157 unsigned long last_probe;
0158 #endif
0159
0160 struct rt6_info * __percpu *rt6i_pcpu;
0161 struct rt6_exception_bucket __rcu *rt6i_exception_bucket;
0162 };
0163
0164 struct fib6_info {
0165 struct fib6_table *fib6_table;
0166 struct fib6_info __rcu *fib6_next;
0167 struct fib6_node __rcu *fib6_node;
0168
0169
0170
0171
0172
0173
0174 union {
0175 struct list_head fib6_siblings;
0176 struct list_head nh_list;
0177 };
0178 unsigned int fib6_nsiblings;
0179
0180 refcount_t fib6_ref;
0181 unsigned long expires;
0182 struct dst_metrics *fib6_metrics;
0183 #define fib6_pmtu fib6_metrics->metrics[RTAX_MTU-1]
0184
0185 struct rt6key fib6_dst;
0186 u32 fib6_flags;
0187 struct rt6key fib6_src;
0188 struct rt6key fib6_prefsrc;
0189
0190 u32 fib6_metric;
0191 u8 fib6_protocol;
0192 u8 fib6_type;
0193
0194 u8 offload;
0195 u8 trap;
0196 u8 offload_failed;
0197
0198 u8 should_flush:1,
0199 dst_nocount:1,
0200 dst_nopolicy:1,
0201 fib6_destroying:1,
0202 unused:4;
0203
0204 struct rcu_head rcu;
0205 struct nexthop *nh;
0206 struct fib6_nh fib6_nh[];
0207 };
0208
0209 struct rt6_info {
0210 struct dst_entry dst;
0211 struct fib6_info __rcu *from;
0212 int sernum;
0213
0214 struct rt6key rt6i_dst;
0215 struct rt6key rt6i_src;
0216 struct in6_addr rt6i_gateway;
0217 struct inet6_dev *rt6i_idev;
0218 u32 rt6i_flags;
0219
0220 struct list_head rt6i_uncached;
0221 struct uncached_list *rt6i_uncached_list;
0222
0223
0224 unsigned short rt6i_nfheader_len;
0225 };
0226
0227 struct fib6_result {
0228 struct fib6_nh *nh;
0229 struct fib6_info *f6i;
0230 u32 fib6_flags;
0231 u8 fib6_type;
0232 struct rt6_info *rt6;
0233 };
0234
0235 #define for_each_fib6_node_rt_rcu(fn) \
0236 for (rt = rcu_dereference((fn)->leaf); rt; \
0237 rt = rcu_dereference(rt->fib6_next))
0238
0239 #define for_each_fib6_walker_rt(w) \
0240 for (rt = (w)->leaf; rt; \
0241 rt = rcu_dereference_protected(rt->fib6_next, 1))
0242
0243 static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
0244 {
0245 return ((struct rt6_info *)dst)->rt6i_idev;
0246 }
0247
0248 static inline bool fib6_requires_src(const struct fib6_info *rt)
0249 {
0250 return rt->fib6_src.plen > 0;
0251 }
0252
0253 static inline void fib6_clean_expires(struct fib6_info *f6i)
0254 {
0255 f6i->fib6_flags &= ~RTF_EXPIRES;
0256 f6i->expires = 0;
0257 }
0258
0259 static inline void fib6_set_expires(struct fib6_info *f6i,
0260 unsigned long expires)
0261 {
0262 f6i->expires = expires;
0263 f6i->fib6_flags |= RTF_EXPIRES;
0264 }
0265
0266 static inline bool fib6_check_expired(const struct fib6_info *f6i)
0267 {
0268 if (f6i->fib6_flags & RTF_EXPIRES)
0269 return time_after(jiffies, f6i->expires);
0270 return false;
0271 }
0272
0273
0274
0275
0276
0277
0278 static inline bool fib6_get_cookie_safe(const struct fib6_info *f6i,
0279 u32 *cookie)
0280 {
0281 struct fib6_node *fn;
0282 bool status = false;
0283
0284 fn = rcu_dereference(f6i->fib6_node);
0285
0286 if (fn) {
0287 *cookie = READ_ONCE(fn->fn_sernum);
0288
0289 smp_rmb();
0290 status = true;
0291 }
0292
0293 return status;
0294 }
0295
0296 static inline u32 rt6_get_cookie(const struct rt6_info *rt)
0297 {
0298 struct fib6_info *from;
0299 u32 cookie = 0;
0300
0301 if (rt->sernum)
0302 return rt->sernum;
0303
0304 rcu_read_lock();
0305
0306 from = rcu_dereference(rt->from);
0307 if (from)
0308 fib6_get_cookie_safe(from, &cookie);
0309
0310 rcu_read_unlock();
0311
0312 return cookie;
0313 }
0314
0315 static inline void ip6_rt_put(struct rt6_info *rt)
0316 {
0317
0318
0319
0320 BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
0321 dst_release(&rt->dst);
0322 }
0323
0324 struct fib6_info *fib6_info_alloc(gfp_t gfp_flags, bool with_fib6_nh);
0325 void fib6_info_destroy_rcu(struct rcu_head *head);
0326
0327 static inline void fib6_info_hold(struct fib6_info *f6i)
0328 {
0329 refcount_inc(&f6i->fib6_ref);
0330 }
0331
0332 static inline bool fib6_info_hold_safe(struct fib6_info *f6i)
0333 {
0334 return refcount_inc_not_zero(&f6i->fib6_ref);
0335 }
0336
0337 static inline void fib6_info_release(struct fib6_info *f6i)
0338 {
0339 if (f6i && refcount_dec_and_test(&f6i->fib6_ref))
0340 call_rcu(&f6i->rcu, fib6_info_destroy_rcu);
0341 }
0342
0343 enum fib6_walk_state {
0344 #ifdef CONFIG_IPV6_SUBTREES
0345 FWS_S,
0346 #endif
0347 FWS_L,
0348 FWS_R,
0349 FWS_C,
0350 FWS_U
0351 };
0352
0353 struct fib6_walker {
0354 struct list_head lh;
0355 struct fib6_node *root, *node;
0356 struct fib6_info *leaf;
0357 enum fib6_walk_state state;
0358 unsigned int skip;
0359 unsigned int count;
0360 unsigned int skip_in_node;
0361 int (*func)(struct fib6_walker *);
0362 void *args;
0363 };
0364
0365 struct rt6_statistics {
0366 __u32 fib_nodes;
0367 __u32 fib_route_nodes;
0368 __u32 fib_rt_entries;
0369 __u32 fib_rt_cache;
0370 __u32 fib_discarded_routes;
0371
0372
0373 atomic_t fib_rt_alloc;
0374 };
0375
0376 #define RTN_TL_ROOT 0x0001
0377 #define RTN_ROOT 0x0002
0378 #define RTN_RTINFO 0x0004
0379
0380
0381
0382
0383
0384
0385
0386 struct fib6_table {
0387 struct hlist_node tb6_hlist;
0388 u32 tb6_id;
0389 spinlock_t tb6_lock;
0390 struct fib6_node tb6_root;
0391 struct inet_peer_base tb6_peers;
0392 unsigned int flags;
0393 unsigned int fib_seq;
0394 #define RT6_TABLE_HAS_DFLT_ROUTER BIT(0)
0395 };
0396
0397 #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
0398 #define RT6_TABLE_MAIN RT_TABLE_MAIN
0399 #define RT6_TABLE_DFLT RT6_TABLE_MAIN
0400 #define RT6_TABLE_INFO RT6_TABLE_MAIN
0401 #define RT6_TABLE_PREFIX RT6_TABLE_MAIN
0402
0403 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
0404 #define FIB6_TABLE_MIN 1
0405 #define FIB6_TABLE_MAX RT_TABLE_MAX
0406 #define RT6_TABLE_LOCAL RT_TABLE_LOCAL
0407 #else
0408 #define FIB6_TABLE_MIN RT_TABLE_MAIN
0409 #define FIB6_TABLE_MAX FIB6_TABLE_MIN
0410 #define RT6_TABLE_LOCAL RT6_TABLE_MAIN
0411 #endif
0412
0413 typedef struct rt6_info *(*pol_lookup_t)(struct net *,
0414 struct fib6_table *,
0415 struct flowi6 *,
0416 const struct sk_buff *, int);
0417
0418 struct fib6_entry_notifier_info {
0419 struct fib_notifier_info info;
0420 struct fib6_info *rt;
0421 unsigned int nsiblings;
0422 };
0423
0424
0425
0426
0427
0428 struct fib6_table *fib6_get_table(struct net *net, u32 id);
0429 struct fib6_table *fib6_new_table(struct net *net, u32 id);
0430 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
0431 const struct sk_buff *skb,
0432 int flags, pol_lookup_t lookup);
0433
0434
0435
0436
0437 int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
0438 struct fib6_result *res, int flags);
0439
0440
0441 int fib6_table_lookup(struct net *net, struct fib6_table *table,
0442 int oif, struct flowi6 *fl6, struct fib6_result *res,
0443 int strict);
0444
0445 void fib6_select_path(const struct net *net, struct fib6_result *res,
0446 struct flowi6 *fl6, int oif, bool have_oif_match,
0447 const struct sk_buff *skb, int strict);
0448 struct fib6_node *fib6_node_lookup(struct fib6_node *root,
0449 const struct in6_addr *daddr,
0450 const struct in6_addr *saddr);
0451
0452 struct fib6_node *fib6_locate(struct fib6_node *root,
0453 const struct in6_addr *daddr, int dst_len,
0454 const struct in6_addr *saddr, int src_len,
0455 bool exact_match);
0456
0457 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *arg),
0458 void *arg);
0459 void fib6_clean_all_skip_notify(struct net *net,
0460 int (*func)(struct fib6_info *, void *arg),
0461 void *arg);
0462
0463 int fib6_add(struct fib6_node *root, struct fib6_info *rt,
0464 struct nl_info *info, struct netlink_ext_ack *extack);
0465 int fib6_del(struct fib6_info *rt, struct nl_info *info);
0466
0467 static inline
0468 void rt6_get_prefsrc(const struct rt6_info *rt, struct in6_addr *addr)
0469 {
0470 const struct fib6_info *from;
0471
0472 rcu_read_lock();
0473
0474 from = rcu_dereference(rt->from);
0475 if (from) {
0476 *addr = from->fib6_prefsrc.addr;
0477 } else {
0478 struct in6_addr in6_zero = {};
0479
0480 *addr = in6_zero;
0481 }
0482
0483 rcu_read_unlock();
0484 }
0485
0486 int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
0487 struct fib6_config *cfg, gfp_t gfp_flags,
0488 struct netlink_ext_ack *extack);
0489 void fib6_nh_release(struct fib6_nh *fib6_nh);
0490 void fib6_nh_release_dsts(struct fib6_nh *fib6_nh);
0491
0492 int call_fib6_entry_notifiers(struct net *net,
0493 enum fib_event_type event_type,
0494 struct fib6_info *rt,
0495 struct netlink_ext_ack *extack);
0496 int call_fib6_multipath_entry_notifiers(struct net *net,
0497 enum fib_event_type event_type,
0498 struct fib6_info *rt,
0499 unsigned int nsiblings,
0500 struct netlink_ext_ack *extack);
0501 int call_fib6_entry_notifiers_replace(struct net *net, struct fib6_info *rt);
0502 void fib6_rt_update(struct net *net, struct fib6_info *rt,
0503 struct nl_info *info);
0504 void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
0505 unsigned int flags);
0506
0507 void fib6_run_gc(unsigned long expires, struct net *net, bool force);
0508
0509 void fib6_gc_cleanup(void);
0510
0511 int fib6_init(void);
0512
0513 struct ipv6_route_iter {
0514 struct seq_net_private p;
0515 struct fib6_walker w;
0516 loff_t skip;
0517 struct fib6_table *tbl;
0518 int sernum;
0519 };
0520
0521 extern const struct seq_operations ipv6_route_seq_ops;
0522
0523 int call_fib6_notifier(struct notifier_block *nb,
0524 enum fib_event_type event_type,
0525 struct fib_notifier_info *info);
0526 int call_fib6_notifiers(struct net *net, enum fib_event_type event_type,
0527 struct fib_notifier_info *info);
0528
0529 int __net_init fib6_notifier_init(struct net *net);
0530 void __net_exit fib6_notifier_exit(struct net *net);
0531
0532 unsigned int fib6_tables_seq_read(struct net *net);
0533 int fib6_tables_dump(struct net *net, struct notifier_block *nb,
0534 struct netlink_ext_ack *extack);
0535
0536 void fib6_update_sernum(struct net *net, struct fib6_info *rt);
0537 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt);
0538 void fib6_update_sernum_stub(struct net *net, struct fib6_info *f6i);
0539
0540 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val);
0541 static inline bool fib6_metric_locked(struct fib6_info *f6i, int metric)
0542 {
0543 return !!(f6i->fib6_metrics->metrics[RTAX_LOCK - 1] & (1 << metric));
0544 }
0545 void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
0546 bool offload, bool trap, bool offload_failed);
0547
0548 #if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL)
0549 struct bpf_iter__ipv6_route {
0550 __bpf_md_ptr(struct bpf_iter_meta *, meta);
0551 __bpf_md_ptr(struct fib6_info *, rt);
0552 };
0553 #endif
0554
0555 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_output(struct net *net,
0556 struct fib6_table *table,
0557 struct flowi6 *fl6,
0558 const struct sk_buff *skb,
0559 int flags));
0560 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_input(struct net *net,
0561 struct fib6_table *table,
0562 struct flowi6 *fl6,
0563 const struct sk_buff *skb,
0564 int flags));
0565 INDIRECT_CALLABLE_DECLARE(struct rt6_info *__ip6_route_redirect(struct net *net,
0566 struct fib6_table *table,
0567 struct flowi6 *fl6,
0568 const struct sk_buff *skb,
0569 int flags));
0570 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_lookup(struct net *net,
0571 struct fib6_table *table,
0572 struct flowi6 *fl6,
0573 const struct sk_buff *skb,
0574 int flags));
0575 static inline struct rt6_info *pol_lookup_func(pol_lookup_t lookup,
0576 struct net *net,
0577 struct fib6_table *table,
0578 struct flowi6 *fl6,
0579 const struct sk_buff *skb,
0580 int flags)
0581 {
0582 return INDIRECT_CALL_4(lookup,
0583 ip6_pol_route_output,
0584 ip6_pol_route_input,
0585 ip6_pol_route_lookup,
0586 __ip6_route_redirect,
0587 net, table, fl6, skb, flags);
0588 }
0589
0590 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
0591 static inline bool fib6_has_custom_rules(const struct net *net)
0592 {
0593 return net->ipv6.fib6_has_custom_rules;
0594 }
0595
0596 int fib6_rules_init(void);
0597 void fib6_rules_cleanup(void);
0598 bool fib6_rule_default(const struct fib_rule *rule);
0599 int fib6_rules_dump(struct net *net, struct notifier_block *nb,
0600 struct netlink_ext_ack *extack);
0601 unsigned int fib6_rules_seq_read(struct net *net);
0602
0603 static inline bool fib6_rules_early_flow_dissect(struct net *net,
0604 struct sk_buff *skb,
0605 struct flowi6 *fl6,
0606 struct flow_keys *flkeys)
0607 {
0608 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
0609
0610 if (!net->ipv6.fib6_rules_require_fldissect)
0611 return false;
0612
0613 skb_flow_dissect_flow_keys(skb, flkeys, flag);
0614 fl6->fl6_sport = flkeys->ports.src;
0615 fl6->fl6_dport = flkeys->ports.dst;
0616 fl6->flowi6_proto = flkeys->basic.ip_proto;
0617
0618 return true;
0619 }
0620 #else
0621 static inline bool fib6_has_custom_rules(const struct net *net)
0622 {
0623 return false;
0624 }
0625 static inline int fib6_rules_init(void)
0626 {
0627 return 0;
0628 }
0629 static inline void fib6_rules_cleanup(void)
0630 {
0631 return ;
0632 }
0633 static inline bool fib6_rule_default(const struct fib_rule *rule)
0634 {
0635 return true;
0636 }
0637 static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb,
0638 struct netlink_ext_ack *extack)
0639 {
0640 return 0;
0641 }
0642 static inline unsigned int fib6_rules_seq_read(struct net *net)
0643 {
0644 return 0;
0645 }
0646 static inline bool fib6_rules_early_flow_dissect(struct net *net,
0647 struct sk_buff *skb,
0648 struct flowi6 *fl6,
0649 struct flow_keys *flkeys)
0650 {
0651 return false;
0652 }
0653 #endif
0654 #endif