1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
12 #include <linux/ipv6_route.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/spinlock.h>
15 #include <linux/notifier.h>
18 #include <net/ip_fib.h>
19 #include <net/netlink.h>
20 #include <net/inetpeer.h>
21 #include <net/fib_notifier.h>
22 #include <linux/indirect_call_wrapper.h>
23 #include <uapi/linux/bpf.h>
25 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
26 #define FIB6_TABLE_HASHSZ 256
28 #define FIB6_TABLE_HASHSZ 1
34 #define RT6_TRACE(x...) pr_debug(x)
36 #define RT6_TRACE(x...) do { ; } while (0)
50 u16 fc_type; /* only 8 bits are used */
51 u16 fc_delete_all_nh : 1,
56 struct in6_addr fc_dst;
57 struct in6_addr fc_src;
58 struct in6_addr fc_prefsrc;
59 struct in6_addr fc_gateway;
61 unsigned long fc_expires;
67 struct nl_info fc_nlinfo;
68 struct nlattr *fc_encap;
74 struct fib6_node __rcu *parent;
75 struct fib6_node __rcu *left;
76 struct fib6_node __rcu *right;
77 #ifdef CONFIG_IPV6_SUBTREES
78 struct fib6_node __rcu *subtree;
80 struct fib6_info __rcu *leaf;
82 __u16 fn_bit; /* bit key */
85 struct fib6_info __rcu *rr_ptr;
94 #ifndef CONFIG_IPV6_SUBTREES
95 #define FIB6_SUBTREE(fn) NULL
97 static inline bool fib6_routes_require_src(const struct net *net)
102 static inline void fib6_routes_require_src_inc(struct net *net) {}
103 static inline void fib6_routes_require_src_dec(struct net *net) {}
107 static inline bool fib6_routes_require_src(const struct net *net)
109 return net->ipv6.fib6_routes_require_src > 0;
112 static inline void fib6_routes_require_src_inc(struct net *net)
114 net->ipv6.fib6_routes_require_src++;
117 static inline void fib6_routes_require_src_dec(struct net *net)
119 net->ipv6.fib6_routes_require_src--;
122 #define FIB6_SUBTREE(fn) (rcu_dereference_protected((fn)->subtree, 1))
126 * routing information
131 struct in6_addr addr;
137 struct rt6_exception_bucket {
138 struct hlist_head chain;
142 struct rt6_exception {
143 struct hlist_node hlist;
144 struct rt6_info *rt6i;
149 #define FIB6_EXCEPTION_BUCKET_SIZE_SHIFT 10
150 #define FIB6_EXCEPTION_BUCKET_SIZE (1 << FIB6_EXCEPTION_BUCKET_SIZE_SHIFT)
151 #define FIB6_MAX_DEPTH 5
154 struct fib_nh_common nh_common;
156 #ifdef CONFIG_IPV6_ROUTER_PREF
157 unsigned long last_probe;
160 struct rt6_info * __percpu *rt6i_pcpu;
161 struct rt6_exception_bucket __rcu *rt6i_exception_bucket;
165 struct fib6_table *fib6_table;
166 struct fib6_info __rcu *fib6_next;
167 struct fib6_node __rcu *fib6_node;
170 * siblings is a list of fib6_info that have the same metric/weight,
171 * destination, but not the same gateway. nsiblings is just a cache
172 * to speed up lookup.
175 struct list_head fib6_siblings;
176 struct list_head nh_list;
178 unsigned int fib6_nsiblings;
181 unsigned long expires;
183 struct hlist_node gc_link;
185 struct dst_metrics *fib6_metrics;
186 #define fib6_pmtu fib6_metrics->metrics[RTAX_MTU-1]
188 struct rt6key fib6_dst;
190 struct rt6key fib6_src;
191 struct rt6key fib6_prefsrc;
209 struct fib6_nh fib6_nh[];
213 struct dst_entry dst;
214 struct fib6_info __rcu *from;
217 struct rt6key rt6i_dst;
218 struct rt6key rt6i_src;
219 struct in6_addr rt6i_gateway;
220 struct inet6_dev *rt6i_idev;
223 /* more non-fragment space at head required */
224 unsigned short rt6i_nfheader_len;
229 struct fib6_info *f6i;
232 struct rt6_info *rt6;
235 #define for_each_fib6_node_rt_rcu(fn) \
236 for (rt = rcu_dereference((fn)->leaf); rt; \
237 rt = rcu_dereference(rt->fib6_next))
239 #define for_each_fib6_walker_rt(w) \
240 for (rt = (w)->leaf; rt; \
241 rt = rcu_dereference_protected(rt->fib6_next, 1))
243 static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
245 return ((struct rt6_info *)dst)->rt6i_idev;
248 static inline bool fib6_requires_src(const struct fib6_info *rt)
250 return rt->fib6_src.plen > 0;
253 static inline bool fib6_check_expired(const struct fib6_info *f6i)
255 if (f6i->fib6_flags & RTF_EXPIRES)
256 return time_after(jiffies, f6i->expires);
260 static inline bool fib6_has_expires(const struct fib6_info *f6i)
262 return f6i->fib6_flags & RTF_EXPIRES;
265 /* Function to safely get fn->fn_sernum for passed in rt
266 * and store result in passed in cookie.
267 * Return true if we can get cookie safely
268 * Return false if not
270 static inline bool fib6_get_cookie_safe(const struct fib6_info *f6i,
273 struct fib6_node *fn;
276 fn = rcu_dereference(f6i->fib6_node);
279 *cookie = READ_ONCE(fn->fn_sernum);
280 /* pairs with smp_wmb() in __fib6_update_sernum_upto_root() */
288 static inline u32 rt6_get_cookie(const struct rt6_info *rt)
290 struct fib6_info *from;
298 from = rcu_dereference(rt->from);
300 fib6_get_cookie_safe(from, &cookie);
307 static inline void ip6_rt_put(struct rt6_info *rt)
309 /* dst_release() accepts a NULL parameter.
310 * We rely on dst being first structure in struct rt6_info
312 BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
313 dst_release(&rt->dst);
316 struct fib6_info *fib6_info_alloc(gfp_t gfp_flags, bool with_fib6_nh);
317 void fib6_info_destroy_rcu(struct rcu_head *head);
319 static inline void fib6_info_hold(struct fib6_info *f6i)
321 refcount_inc(&f6i->fib6_ref);
324 static inline bool fib6_info_hold_safe(struct fib6_info *f6i)
326 return refcount_inc_not_zero(&f6i->fib6_ref);
329 static inline void fib6_info_release(struct fib6_info *f6i)
331 if (f6i && refcount_dec_and_test(&f6i->fib6_ref))
332 call_rcu(&f6i->rcu, fib6_info_destroy_rcu);
335 enum fib6_walk_state {
336 #ifdef CONFIG_IPV6_SUBTREES
347 struct fib6_node *root, *node;
348 struct fib6_info *leaf;
349 enum fib6_walk_state state;
352 unsigned int skip_in_node;
353 int (*func)(struct fib6_walker *);
357 struct rt6_statistics {
358 __u32 fib_nodes; /* all fib6 nodes */
359 __u32 fib_route_nodes; /* intermediate nodes */
360 __u32 fib_rt_entries; /* rt entries in fib table */
361 __u32 fib_rt_cache; /* cached rt entries in exception table */
362 __u32 fib_discarded_routes; /* total number of routes delete */
364 /* The following stat is not protected by any lock */
365 atomic_t fib_rt_alloc; /* total number of routes alloced */
368 #define RTN_TL_ROOT 0x0001
369 #define RTN_ROOT 0x0002 /* tree root node */
370 #define RTN_RTINFO 0x0004 /* node with valid routing info */
373 * priority levels (or metrics)
379 struct hlist_node tb6_hlist;
382 struct fib6_node tb6_root;
383 struct inet_peer_base tb6_peers;
385 unsigned int fib_seq;
386 struct hlist_head tb6_gc_hlist; /* GC candidates */
387 #define RT6_TABLE_HAS_DFLT_ROUTER BIT(0)
390 #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
391 #define RT6_TABLE_MAIN RT_TABLE_MAIN
392 #define RT6_TABLE_DFLT RT6_TABLE_MAIN
393 #define RT6_TABLE_INFO RT6_TABLE_MAIN
394 #define RT6_TABLE_PREFIX RT6_TABLE_MAIN
396 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
397 #define FIB6_TABLE_MIN 1
398 #define FIB6_TABLE_MAX RT_TABLE_MAX
399 #define RT6_TABLE_LOCAL RT_TABLE_LOCAL
401 #define FIB6_TABLE_MIN RT_TABLE_MAIN
402 #define FIB6_TABLE_MAX FIB6_TABLE_MIN
403 #define RT6_TABLE_LOCAL RT6_TABLE_MAIN
406 typedef struct rt6_info *(*pol_lookup_t)(struct net *,
409 const struct sk_buff *, int);
411 struct fib6_entry_notifier_info {
412 struct fib_notifier_info info; /* must be first */
413 struct fib6_info *rt;
414 unsigned int nsiblings;
421 struct fib6_table *fib6_get_table(struct net *net, u32 id);
422 struct fib6_table *fib6_new_table(struct net *net, u32 id);
423 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
424 const struct sk_buff *skb,
425 int flags, pol_lookup_t lookup);
427 /* called with rcu lock held; can return error pointer
428 * caller needs to select path
430 int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
431 struct fib6_result *res, int flags);
433 /* called with rcu lock held; caller needs to select path */
434 int fib6_table_lookup(struct net *net, struct fib6_table *table,
435 int oif, struct flowi6 *fl6, struct fib6_result *res,
438 void fib6_select_path(const struct net *net, struct fib6_result *res,
439 struct flowi6 *fl6, int oif, bool have_oif_match,
440 const struct sk_buff *skb, int strict);
441 struct fib6_node *fib6_node_lookup(struct fib6_node *root,
442 const struct in6_addr *daddr,
443 const struct in6_addr *saddr);
445 struct fib6_node *fib6_locate(struct fib6_node *root,
446 const struct in6_addr *daddr, int dst_len,
447 const struct in6_addr *saddr, int src_len,
450 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *arg),
452 void fib6_clean_all_skip_notify(struct net *net,
453 int (*func)(struct fib6_info *, void *arg),
456 int fib6_add(struct fib6_node *root, struct fib6_info *rt,
457 struct nl_info *info, struct netlink_ext_ack *extack);
458 int fib6_del(struct fib6_info *rt, struct nl_info *info);
461 void rt6_get_prefsrc(const struct rt6_info *rt, struct in6_addr *addr)
463 const struct fib6_info *from;
467 from = rcu_dereference(rt->from);
469 *addr = from->fib6_prefsrc.addr;
476 int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
477 struct fib6_config *cfg, gfp_t gfp_flags,
478 struct netlink_ext_ack *extack);
479 void fib6_nh_release(struct fib6_nh *fib6_nh);
480 void fib6_nh_release_dsts(struct fib6_nh *fib6_nh);
482 int call_fib6_entry_notifiers(struct net *net,
483 enum fib_event_type event_type,
484 struct fib6_info *rt,
485 struct netlink_ext_ack *extack);
486 int call_fib6_multipath_entry_notifiers(struct net *net,
487 enum fib_event_type event_type,
488 struct fib6_info *rt,
489 unsigned int nsiblings,
490 struct netlink_ext_ack *extack);
491 int call_fib6_entry_notifiers_replace(struct net *net, struct fib6_info *rt);
492 void fib6_rt_update(struct net *net, struct fib6_info *rt,
493 struct nl_info *info);
494 void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
497 void fib6_run_gc(unsigned long expires, struct net *net, bool force);
499 void fib6_gc_cleanup(void);
503 /* fib6_info must be locked by the caller, and fib6_info->fib6_table can be
506 static inline void fib6_set_expires_locked(struct fib6_info *f6i,
507 unsigned long expires)
509 struct fib6_table *tb6;
511 tb6 = f6i->fib6_table;
512 f6i->expires = expires;
513 if (tb6 && !fib6_has_expires(f6i))
514 hlist_add_head(&f6i->gc_link, &tb6->tb6_gc_hlist);
515 f6i->fib6_flags |= RTF_EXPIRES;
518 /* fib6_info must be locked by the caller, and fib6_info->fib6_table can be
519 * NULL. If fib6_table is NULL, the fib6_info will no be inserted into the
520 * list of GC candidates until it is inserted into a table.
522 static inline void fib6_set_expires(struct fib6_info *f6i,
523 unsigned long expires)
525 spin_lock_bh(&f6i->fib6_table->tb6_lock);
526 fib6_set_expires_locked(f6i, expires);
527 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
530 static inline void fib6_clean_expires_locked(struct fib6_info *f6i)
532 if (fib6_has_expires(f6i))
533 hlist_del_init(&f6i->gc_link);
534 f6i->fib6_flags &= ~RTF_EXPIRES;
538 static inline void fib6_clean_expires(struct fib6_info *f6i)
540 spin_lock_bh(&f6i->fib6_table->tb6_lock);
541 fib6_clean_expires_locked(f6i);
542 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
545 struct ipv6_route_iter {
546 struct seq_net_private p;
547 struct fib6_walker w;
549 struct fib6_table *tbl;
553 extern const struct seq_operations ipv6_route_seq_ops;
555 int call_fib6_notifier(struct notifier_block *nb,
556 enum fib_event_type event_type,
557 struct fib_notifier_info *info);
558 int call_fib6_notifiers(struct net *net, enum fib_event_type event_type,
559 struct fib_notifier_info *info);
561 int __net_init fib6_notifier_init(struct net *net);
562 void __net_exit fib6_notifier_exit(struct net *net);
564 unsigned int fib6_tables_seq_read(struct net *net);
565 int fib6_tables_dump(struct net *net, struct notifier_block *nb,
566 struct netlink_ext_ack *extack);
568 void fib6_update_sernum(struct net *net, struct fib6_info *rt);
569 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt);
570 void fib6_update_sernum_stub(struct net *net, struct fib6_info *f6i);
572 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val);
573 static inline bool fib6_metric_locked(struct fib6_info *f6i, int metric)
575 return !!(f6i->fib6_metrics->metrics[RTAX_LOCK - 1] & (1 << metric));
577 void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
578 bool offload, bool trap, bool offload_failed);
580 #if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL)
581 struct bpf_iter__ipv6_route {
582 __bpf_md_ptr(struct bpf_iter_meta *, meta);
583 __bpf_md_ptr(struct fib6_info *, rt);
587 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_output(struct net *net,
588 struct fib6_table *table,
590 const struct sk_buff *skb,
592 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_input(struct net *net,
593 struct fib6_table *table,
595 const struct sk_buff *skb,
597 INDIRECT_CALLABLE_DECLARE(struct rt6_info *__ip6_route_redirect(struct net *net,
598 struct fib6_table *table,
600 const struct sk_buff *skb,
602 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_lookup(struct net *net,
603 struct fib6_table *table,
605 const struct sk_buff *skb,
607 static inline struct rt6_info *pol_lookup_func(pol_lookup_t lookup,
609 struct fib6_table *table,
611 const struct sk_buff *skb,
614 return INDIRECT_CALL_4(lookup,
615 ip6_pol_route_output,
617 ip6_pol_route_lookup,
618 __ip6_route_redirect,
619 net, table, fl6, skb, flags);
622 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
623 static inline bool fib6_has_custom_rules(const struct net *net)
625 return net->ipv6.fib6_has_custom_rules;
628 int fib6_rules_init(void);
629 void fib6_rules_cleanup(void);
630 bool fib6_rule_default(const struct fib_rule *rule);
631 int fib6_rules_dump(struct net *net, struct notifier_block *nb,
632 struct netlink_ext_ack *extack);
633 unsigned int fib6_rules_seq_read(struct net *net);
635 static inline bool fib6_rules_early_flow_dissect(struct net *net,
638 struct flow_keys *flkeys)
640 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
642 if (!net->ipv6.fib6_rules_require_fldissect)
645 memset(flkeys, 0, sizeof(*flkeys));
646 __skb_flow_dissect(net, skb, &flow_keys_dissector,
647 flkeys, NULL, 0, 0, 0, flag);
649 fl6->fl6_sport = flkeys->ports.src;
650 fl6->fl6_dport = flkeys->ports.dst;
651 fl6->flowi6_proto = flkeys->basic.ip_proto;
656 static inline bool fib6_has_custom_rules(const struct net *net)
660 static inline int fib6_rules_init(void)
664 static inline void fib6_rules_cleanup(void)
668 static inline bool fib6_rule_default(const struct fib_rule *rule)
672 static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb,
673 struct netlink_ext_ack *extack)
677 static inline unsigned int fib6_rules_seq_read(struct net *net)
681 static inline bool fib6_rules_early_flow_dissect(struct net *net,
684 struct flow_keys *flkeys)