1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 */
8
9 #ifndef _IP6_FIB_H
10 #define _IP6_FIB_H
11
12 #include <linux/ipv6_route.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/spinlock.h>
15 #include <linux/notifier.h>
16 #include <linux/android_kabi.h>
17 #include <net/dst.h>
18 #include <net/flow.h>
19 #include <net/ip_fib.h>
20 #include <net/netlink.h>
21 #include <net/inetpeer.h>
22 #include <net/fib_notifier.h>
23 #include <linux/indirect_call_wrapper.h>
24
25 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
26 #define FIB6_TABLE_HASHSZ 256
27 #else
28 #define FIB6_TABLE_HASHSZ 1
29 #endif
30
31 #define RT6_DEBUG 2
32
33 #if RT6_DEBUG >= 3
34 #define RT6_TRACE(x...) pr_debug(x)
35 #else
36 #define RT6_TRACE(x...) do { ; } while (0)
37 #endif
38
39 struct rt6_info;
40 struct fib6_info;
41
42 struct fib6_config {
43 u32 fc_table;
44 u32 fc_metric;
45 int fc_dst_len;
46 int fc_src_len;
47 int fc_ifindex;
48 u32 fc_flags;
49 u32 fc_protocol;
50 u16 fc_type; /* only 8 bits are used */
51 u16 fc_delete_all_nh : 1,
52 fc_ignore_dev_down:1,
53 __unused : 14;
54 u32 fc_nh_id;
55
56 struct in6_addr fc_dst;
57 struct in6_addr fc_src;
58 struct in6_addr fc_prefsrc;
59 struct in6_addr fc_gateway;
60
61 unsigned long fc_expires;
62 struct nlattr *fc_mx;
63 int fc_mx_len;
64 int fc_mp_len;
65 struct nlattr *fc_mp;
66
67 struct nl_info fc_nlinfo;
68 struct nlattr *fc_encap;
69 u16 fc_encap_type;
70 bool fc_is_fdb;
71
72 ANDROID_KABI_RESERVE(1);
73 };
74
75 struct fib6_node {
76 struct fib6_node __rcu *parent;
77 struct fib6_node __rcu *left;
78 struct fib6_node __rcu *right;
79 #ifdef CONFIG_IPV6_SUBTREES
80 struct fib6_node __rcu *subtree;
81 #endif
82 struct fib6_info __rcu *leaf;
83
84 __u16 fn_bit; /* bit key */
85 __u16 fn_flags;
86 int fn_sernum;
87 struct fib6_info __rcu *rr_ptr;
88 struct rcu_head rcu;
89
90 ANDROID_KABI_RESERVE(1);
91 };
92
93 struct fib6_gc_args {
94 int timeout;
95 int more;
96 };
97
98 #ifndef CONFIG_IPV6_SUBTREES
99 #define FIB6_SUBTREE(fn) NULL
100
fib6_routes_require_src(const struct net * net)101 static inline bool fib6_routes_require_src(const struct net *net)
102 {
103 return false;
104 }
105
fib6_routes_require_src_inc(struct net * net)106 static inline void fib6_routes_require_src_inc(struct net *net) {}
fib6_routes_require_src_dec(struct net * net)107 static inline void fib6_routes_require_src_dec(struct net *net) {}
108
109 #else
110
fib6_routes_require_src(const struct net * net)111 static inline bool fib6_routes_require_src(const struct net *net)
112 {
113 return net->ipv6.fib6_routes_require_src > 0;
114 }
115
fib6_routes_require_src_inc(struct net * net)116 static inline void fib6_routes_require_src_inc(struct net *net)
117 {
118 net->ipv6.fib6_routes_require_src++;
119 }
120
fib6_routes_require_src_dec(struct net * net)121 static inline void fib6_routes_require_src_dec(struct net *net)
122 {
123 net->ipv6.fib6_routes_require_src--;
124 }
125
126 #define FIB6_SUBTREE(fn) (rcu_dereference_protected((fn)->subtree, 1))
127 #endif
128
129 /*
130 * routing information
131 *
132 */
133
134 struct rt6key {
135 struct in6_addr addr;
136 int plen;
137 };
138
139 struct fib6_table;
140
141 struct rt6_exception_bucket {
142 struct hlist_head chain;
143 int depth;
144 };
145
146 struct rt6_exception {
147 struct hlist_node hlist;
148 struct rt6_info *rt6i;
149 unsigned long stamp;
150 struct rcu_head rcu;
151 };
152
153 #define FIB6_EXCEPTION_BUCKET_SIZE_SHIFT 10
154 #define FIB6_EXCEPTION_BUCKET_SIZE (1 << FIB6_EXCEPTION_BUCKET_SIZE_SHIFT)
155 #define FIB6_MAX_DEPTH 5
156
157 struct fib6_nh {
158 struct fib_nh_common nh_common;
159
160 #ifdef CONFIG_IPV6_ROUTER_PREF
161 unsigned long last_probe;
162 #endif
163
164 struct rt6_info * __percpu *rt6i_pcpu;
165 struct rt6_exception_bucket __rcu *rt6i_exception_bucket;
166 };
167
168 struct fib6_info {
169 struct fib6_table *fib6_table;
170 struct fib6_info __rcu *fib6_next;
171 struct fib6_node __rcu *fib6_node;
172
173 /* Multipath routes:
174 * siblings is a list of fib6_info that have the same metric/weight,
175 * destination, but not the same gateway. nsiblings is just a cache
176 * to speed up lookup.
177 */
178 union {
179 struct list_head fib6_siblings;
180 struct list_head nh_list;
181 };
182 unsigned int fib6_nsiblings;
183
184 refcount_t fib6_ref;
185 unsigned long expires;
186 struct dst_metrics *fib6_metrics;
187 #define fib6_pmtu fib6_metrics->metrics[RTAX_MTU-1]
188
189 struct rt6key fib6_dst;
190 u32 fib6_flags;
191 struct rt6key fib6_src;
192 struct rt6key fib6_prefsrc;
193
194 u32 fib6_metric;
195 u8 fib6_protocol;
196 u8 fib6_type;
197 u8 should_flush:1,
198 dst_nocount:1,
199 dst_nopolicy:1,
200 fib6_destroying:1,
201 offload:1,
202 trap:1,
203 unused:2;
204
205 struct rcu_head rcu;
206 struct nexthop *nh;
207
208 ANDROID_KABI_RESERVE(1);
209
210 struct fib6_nh fib6_nh[];
211 };
212
213 struct rt6_info {
214 struct dst_entry dst;
215 struct fib6_info __rcu *from;
216 int sernum;
217
218 struct rt6key rt6i_dst;
219 struct rt6key rt6i_src;
220 struct in6_addr rt6i_gateway;
221 struct inet6_dev *rt6i_idev;
222 u32 rt6i_flags;
223
224 struct list_head rt6i_uncached;
225 struct uncached_list *rt6i_uncached_list;
226
227 /* more non-fragment space at head required */
228 unsigned short rt6i_nfheader_len;
229
230 ANDROID_KABI_RESERVE(1);
231 };
232
233 struct fib6_result {
234 struct fib6_nh *nh;
235 struct fib6_info *f6i;
236 u32 fib6_flags;
237 u8 fib6_type;
238 struct rt6_info *rt6;
239 };
240
241 #define for_each_fib6_node_rt_rcu(fn) \
242 for (rt = rcu_dereference((fn)->leaf); rt; \
243 rt = rcu_dereference(rt->fib6_next))
244
245 #define for_each_fib6_walker_rt(w) \
246 for (rt = (w)->leaf; rt; \
247 rt = rcu_dereference_protected(rt->fib6_next, 1))
248
ip6_dst_idev(struct dst_entry * dst)249 static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
250 {
251 return ((struct rt6_info *)dst)->rt6i_idev;
252 }
253
fib6_requires_src(const struct fib6_info * rt)254 static inline bool fib6_requires_src(const struct fib6_info *rt)
255 {
256 return rt->fib6_src.plen > 0;
257 }
258
fib6_clean_expires(struct fib6_info * f6i)259 static inline void fib6_clean_expires(struct fib6_info *f6i)
260 {
261 f6i->fib6_flags &= ~RTF_EXPIRES;
262 f6i->expires = 0;
263 }
264
fib6_set_expires(struct fib6_info * f6i,unsigned long expires)265 static inline void fib6_set_expires(struct fib6_info *f6i,
266 unsigned long expires)
267 {
268 f6i->expires = expires;
269 f6i->fib6_flags |= RTF_EXPIRES;
270 }
271
fib6_check_expired(const struct fib6_info * f6i)272 static inline bool fib6_check_expired(const struct fib6_info *f6i)
273 {
274 if (f6i->fib6_flags & RTF_EXPIRES)
275 return time_after(jiffies, f6i->expires);
276 return false;
277 }
278
279 /* Function to safely get fn->sernum for passed in rt
280 * and store result in passed in cookie.
281 * Return true if we can get cookie safely
282 * Return false if not
283 */
fib6_get_cookie_safe(const struct fib6_info * f6i,u32 * cookie)284 static inline bool fib6_get_cookie_safe(const struct fib6_info *f6i,
285 u32 *cookie)
286 {
287 struct fib6_node *fn;
288 bool status = false;
289
290 fn = rcu_dereference(f6i->fib6_node);
291
292 if (fn) {
293 *cookie = READ_ONCE(fn->fn_sernum);
294 /* pairs with smp_wmb() in fib6_update_sernum_upto_root() */
295 smp_rmb();
296 status = true;
297 }
298
299 return status;
300 }
301
rt6_get_cookie(const struct rt6_info * rt)302 static inline u32 rt6_get_cookie(const struct rt6_info *rt)
303 {
304 struct fib6_info *from;
305 u32 cookie = 0;
306
307 if (rt->sernum)
308 return rt->sernum;
309
310 rcu_read_lock();
311
312 from = rcu_dereference(rt->from);
313 if (from)
314 fib6_get_cookie_safe(from, &cookie);
315
316 rcu_read_unlock();
317
318 return cookie;
319 }
320
ip6_rt_put(struct rt6_info * rt)321 static inline void ip6_rt_put(struct rt6_info *rt)
322 {
323 /* dst_release() accepts a NULL parameter.
324 * We rely on dst being first structure in struct rt6_info
325 */
326 BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
327 dst_release(&rt->dst);
328 }
329
330 struct fib6_info *fib6_info_alloc(gfp_t gfp_flags, bool with_fib6_nh);
331 void fib6_info_destroy_rcu(struct rcu_head *head);
332
fib6_info_hold(struct fib6_info * f6i)333 static inline void fib6_info_hold(struct fib6_info *f6i)
334 {
335 refcount_inc(&f6i->fib6_ref);
336 }
337
fib6_info_hold_safe(struct fib6_info * f6i)338 static inline bool fib6_info_hold_safe(struct fib6_info *f6i)
339 {
340 return refcount_inc_not_zero(&f6i->fib6_ref);
341 }
342
fib6_info_release(struct fib6_info * f6i)343 static inline void fib6_info_release(struct fib6_info *f6i)
344 {
345 if (f6i && refcount_dec_and_test(&f6i->fib6_ref))
346 call_rcu(&f6i->rcu, fib6_info_destroy_rcu);
347 }
348
fib6_info_hw_flags_set(struct fib6_info * f6i,bool offload,bool trap)349 static inline void fib6_info_hw_flags_set(struct fib6_info *f6i, bool offload,
350 bool trap)
351 {
352 f6i->offload = offload;
353 f6i->trap = trap;
354 }
355
356 enum fib6_walk_state {
357 #ifdef CONFIG_IPV6_SUBTREES
358 FWS_S,
359 #endif
360 FWS_L,
361 FWS_R,
362 FWS_C,
363 FWS_U
364 };
365
366 struct fib6_walker {
367 struct list_head lh;
368 struct fib6_node *root, *node;
369 struct fib6_info *leaf;
370 enum fib6_walk_state state;
371 unsigned int skip;
372 unsigned int count;
373 unsigned int skip_in_node;
374 int (*func)(struct fib6_walker *);
375 void *args;
376 };
377
378 struct rt6_statistics {
379 __u32 fib_nodes; /* all fib6 nodes */
380 __u32 fib_route_nodes; /* intermediate nodes */
381 __u32 fib_rt_entries; /* rt entries in fib table */
382 __u32 fib_rt_cache; /* cached rt entries in exception table */
383 __u32 fib_discarded_routes; /* total number of routes delete */
384
385 /* The following stats are not protected by any lock */
386 atomic_t fib_rt_alloc; /* total number of routes alloced */
387 atomic_t fib_rt_uncache; /* rt entries in uncached list */
388 };
389
390 #define RTN_TL_ROOT 0x0001
391 #define RTN_ROOT 0x0002 /* tree root node */
392 #define RTN_RTINFO 0x0004 /* node with valid routing info */
393
394 /*
395 * priority levels (or metrics)
396 *
397 */
398
399
400 struct fib6_table {
401 struct hlist_node tb6_hlist;
402 u32 tb6_id;
403 spinlock_t tb6_lock;
404 struct fib6_node tb6_root;
405 struct inet_peer_base tb6_peers;
406 unsigned int flags;
407 unsigned int fib_seq;
408 #define RT6_TABLE_HAS_DFLT_ROUTER BIT(0)
409 };
410
411 #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
412 #define RT6_TABLE_MAIN RT_TABLE_MAIN
413 #define RT6_TABLE_DFLT RT6_TABLE_MAIN
414 #define RT6_TABLE_INFO RT6_TABLE_MAIN
415 #define RT6_TABLE_PREFIX RT6_TABLE_MAIN
416
417 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
418 #define FIB6_TABLE_MIN 1
419 #define FIB6_TABLE_MAX RT_TABLE_MAX
420 #define RT6_TABLE_LOCAL RT_TABLE_LOCAL
421 #else
422 #define FIB6_TABLE_MIN RT_TABLE_MAIN
423 #define FIB6_TABLE_MAX FIB6_TABLE_MIN
424 #define RT6_TABLE_LOCAL RT6_TABLE_MAIN
425 #endif
426
427 typedef struct rt6_info *(*pol_lookup_t)(struct net *,
428 struct fib6_table *,
429 struct flowi6 *,
430 const struct sk_buff *, int);
431
432 struct fib6_entry_notifier_info {
433 struct fib_notifier_info info; /* must be first */
434 struct fib6_info *rt;
435 unsigned int nsiblings;
436 };
437
438 /*
439 * exported functions
440 */
441
442 struct fib6_table *fib6_get_table(struct net *net, u32 id);
443 struct fib6_table *fib6_new_table(struct net *net, u32 id);
444 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
445 const struct sk_buff *skb,
446 int flags, pol_lookup_t lookup);
447
448 /* called with rcu lock held; can return error pointer
449 * caller needs to select path
450 */
451 int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
452 struct fib6_result *res, int flags);
453
454 /* called with rcu lock held; caller needs to select path */
455 int fib6_table_lookup(struct net *net, struct fib6_table *table,
456 int oif, struct flowi6 *fl6, struct fib6_result *res,
457 int strict);
458
459 void fib6_select_path(const struct net *net, struct fib6_result *res,
460 struct flowi6 *fl6, int oif, bool have_oif_match,
461 const struct sk_buff *skb, int strict);
462 struct fib6_node *fib6_node_lookup(struct fib6_node *root,
463 const struct in6_addr *daddr,
464 const struct in6_addr *saddr);
465
466 struct fib6_node *fib6_locate(struct fib6_node *root,
467 const struct in6_addr *daddr, int dst_len,
468 const struct in6_addr *saddr, int src_len,
469 bool exact_match);
470
471 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *arg),
472 void *arg);
473 void fib6_clean_all_skip_notify(struct net *net,
474 int (*func)(struct fib6_info *, void *arg),
475 void *arg);
476
477 int fib6_add(struct fib6_node *root, struct fib6_info *rt,
478 struct nl_info *info, struct netlink_ext_ack *extack);
479 int fib6_del(struct fib6_info *rt, struct nl_info *info);
480
481 static inline
rt6_get_prefsrc(const struct rt6_info * rt,struct in6_addr * addr)482 void rt6_get_prefsrc(const struct rt6_info *rt, struct in6_addr *addr)
483 {
484 const struct fib6_info *from;
485
486 rcu_read_lock();
487
488 from = rcu_dereference(rt->from);
489 if (from) {
490 *addr = from->fib6_prefsrc.addr;
491 } else {
492 struct in6_addr in6_zero = {};
493
494 *addr = in6_zero;
495 }
496
497 rcu_read_unlock();
498 }
499
500 int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
501 struct fib6_config *cfg, gfp_t gfp_flags,
502 struct netlink_ext_ack *extack);
503 void fib6_nh_release(struct fib6_nh *fib6_nh);
504
505 int call_fib6_entry_notifiers(struct net *net,
506 enum fib_event_type event_type,
507 struct fib6_info *rt,
508 struct netlink_ext_ack *extack);
509 int call_fib6_multipath_entry_notifiers(struct net *net,
510 enum fib_event_type event_type,
511 struct fib6_info *rt,
512 unsigned int nsiblings,
513 struct netlink_ext_ack *extack);
514 int call_fib6_entry_notifiers_replace(struct net *net, struct fib6_info *rt);
515 void fib6_rt_update(struct net *net, struct fib6_info *rt,
516 struct nl_info *info);
517 void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
518 unsigned int flags);
519
520 void fib6_run_gc(unsigned long expires, struct net *net, bool force);
521
522 void fib6_gc_cleanup(void);
523
524 int fib6_init(void);
525
526 struct ipv6_route_iter {
527 struct seq_net_private p;
528 struct fib6_walker w;
529 loff_t skip;
530 struct fib6_table *tbl;
531 int sernum;
532 };
533
534 extern const struct seq_operations ipv6_route_seq_ops;
535
536 int call_fib6_notifier(struct notifier_block *nb,
537 enum fib_event_type event_type,
538 struct fib_notifier_info *info);
539 int call_fib6_notifiers(struct net *net, enum fib_event_type event_type,
540 struct fib_notifier_info *info);
541
542 int __net_init fib6_notifier_init(struct net *net);
543 void __net_exit fib6_notifier_exit(struct net *net);
544
545 unsigned int fib6_tables_seq_read(struct net *net);
546 int fib6_tables_dump(struct net *net, struct notifier_block *nb,
547 struct netlink_ext_ack *extack);
548
549 void fib6_update_sernum(struct net *net, struct fib6_info *rt);
550 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt);
551 void fib6_update_sernum_stub(struct net *net, struct fib6_info *f6i);
552
553 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val);
fib6_metric_locked(struct fib6_info * f6i,int metric)554 static inline bool fib6_metric_locked(struct fib6_info *f6i, int metric)
555 {
556 return !!(f6i->fib6_metrics->metrics[RTAX_LOCK - 1] & (1 << metric));
557 }
558
559 #if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL)
560 struct bpf_iter__ipv6_route {
561 __bpf_md_ptr(struct bpf_iter_meta *, meta);
562 __bpf_md_ptr(struct fib6_info *, rt);
563 };
564 #endif
565
566 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_output(struct net *net,
567 struct fib6_table *table,
568 struct flowi6 *fl6,
569 const struct sk_buff *skb,
570 int flags));
571 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_input(struct net *net,
572 struct fib6_table *table,
573 struct flowi6 *fl6,
574 const struct sk_buff *skb,
575 int flags));
576 INDIRECT_CALLABLE_DECLARE(struct rt6_info *__ip6_route_redirect(struct net *net,
577 struct fib6_table *table,
578 struct flowi6 *fl6,
579 const struct sk_buff *skb,
580 int flags));
581 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_lookup(struct net *net,
582 struct fib6_table *table,
583 struct flowi6 *fl6,
584 const struct sk_buff *skb,
585 int flags));
pol_lookup_func(pol_lookup_t lookup,struct net * net,struct fib6_table * table,struct flowi6 * fl6,const struct sk_buff * skb,int flags)586 static inline struct rt6_info *pol_lookup_func(pol_lookup_t lookup,
587 struct net *net,
588 struct fib6_table *table,
589 struct flowi6 *fl6,
590 const struct sk_buff *skb,
591 int flags)
592 {
593 return INDIRECT_CALL_4(lookup,
594 ip6_pol_route_output,
595 ip6_pol_route_input,
596 ip6_pol_route_lookup,
597 __ip6_route_redirect,
598 net, table, fl6, skb, flags);
599 }
600
601 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
fib6_has_custom_rules(const struct net * net)602 static inline bool fib6_has_custom_rules(const struct net *net)
603 {
604 return net->ipv6.fib6_has_custom_rules;
605 }
606
607 int fib6_rules_init(void);
608 void fib6_rules_cleanup(void);
609 bool fib6_rule_default(const struct fib_rule *rule);
610 int fib6_rules_dump(struct net *net, struct notifier_block *nb,
611 struct netlink_ext_ack *extack);
612 unsigned int fib6_rules_seq_read(struct net *net);
613
fib6_rules_early_flow_dissect(struct net * net,struct sk_buff * skb,struct flowi6 * fl6,struct flow_keys * flkeys)614 static inline bool fib6_rules_early_flow_dissect(struct net *net,
615 struct sk_buff *skb,
616 struct flowi6 *fl6,
617 struct flow_keys *flkeys)
618 {
619 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
620
621 if (!net->ipv6.fib6_rules_require_fldissect)
622 return false;
623
624 skb_flow_dissect_flow_keys(skb, flkeys, flag);
625 fl6->fl6_sport = flkeys->ports.src;
626 fl6->fl6_dport = flkeys->ports.dst;
627 fl6->flowi6_proto = flkeys->basic.ip_proto;
628
629 return true;
630 }
631 #else
fib6_has_custom_rules(const struct net * net)632 static inline bool fib6_has_custom_rules(const struct net *net)
633 {
634 return false;
635 }
fib6_rules_init(void)636 static inline int fib6_rules_init(void)
637 {
638 return 0;
639 }
fib6_rules_cleanup(void)640 static inline void fib6_rules_cleanup(void)
641 {
642 return ;
643 }
fib6_rule_default(const struct fib_rule * rule)644 static inline bool fib6_rule_default(const struct fib_rule *rule)
645 {
646 return true;
647 }
fib6_rules_dump(struct net * net,struct notifier_block * nb,struct netlink_ext_ack * extack)648 static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb,
649 struct netlink_ext_ack *extack)
650 {
651 return 0;
652 }
fib6_rules_seq_read(struct net * net)653 static inline unsigned int fib6_rules_seq_read(struct net *net)
654 {
655 return 0;
656 }
fib6_rules_early_flow_dissect(struct net * net,struct sk_buff * skb,struct flowi6 * fl6,struct flow_keys * flkeys)657 static inline bool fib6_rules_early_flow_dissect(struct net *net,
658 struct sk_buff *skb,
659 struct flowi6 *fl6,
660 struct flow_keys *flkeys)
661 {
662 return false;
663 }
664 #endif
665 #endif
666