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
198 u8 offload;
199 u8 trap;
200 u8 offload_failed;
201
202 u8 should_flush:1,
203 dst_nocount:1,
204 dst_nopolicy:1,
205 fib6_destroying:1,
206 unused:4;
207
208 struct rcu_head rcu;
209 struct nexthop *nh;
210
211 ANDROID_KABI_RESERVE(1);
212
213 struct fib6_nh fib6_nh[];
214 };
215
216 struct rt6_info {
217 struct dst_entry dst;
218 struct fib6_info __rcu *from;
219 int sernum;
220
221 struct rt6key rt6i_dst;
222 struct rt6key rt6i_src;
223 struct in6_addr rt6i_gateway;
224 struct inet6_dev *rt6i_idev;
225 u32 rt6i_flags;
226
227 struct list_head rt6i_uncached;
228 struct uncached_list *rt6i_uncached_list;
229
230 /* more non-fragment space at head required */
231 unsigned short rt6i_nfheader_len;
232
233 ANDROID_KABI_RESERVE(1);
234 };
235
236 struct fib6_result {
237 struct fib6_nh *nh;
238 struct fib6_info *f6i;
239 u32 fib6_flags;
240 u8 fib6_type;
241 struct rt6_info *rt6;
242 };
243
244 #define for_each_fib6_node_rt_rcu(fn) \
245 for (rt = rcu_dereference((fn)->leaf); rt; \
246 rt = rcu_dereference(rt->fib6_next))
247
248 #define for_each_fib6_walker_rt(w) \
249 for (rt = (w)->leaf; rt; \
250 rt = rcu_dereference_protected(rt->fib6_next, 1))
251
ip6_dst_idev(struct dst_entry * dst)252 static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
253 {
254 return ((struct rt6_info *)dst)->rt6i_idev;
255 }
256
fib6_requires_src(const struct fib6_info * rt)257 static inline bool fib6_requires_src(const struct fib6_info *rt)
258 {
259 return rt->fib6_src.plen > 0;
260 }
261
fib6_clean_expires(struct fib6_info * f6i)262 static inline void fib6_clean_expires(struct fib6_info *f6i)
263 {
264 f6i->fib6_flags &= ~RTF_EXPIRES;
265 f6i->expires = 0;
266 }
267
fib6_set_expires(struct fib6_info * f6i,unsigned long expires)268 static inline void fib6_set_expires(struct fib6_info *f6i,
269 unsigned long expires)
270 {
271 f6i->expires = expires;
272 f6i->fib6_flags |= RTF_EXPIRES;
273 }
274
fib6_check_expired(const struct fib6_info * f6i)275 static inline bool fib6_check_expired(const struct fib6_info *f6i)
276 {
277 if (f6i->fib6_flags & RTF_EXPIRES)
278 return time_after(jiffies, f6i->expires);
279 return false;
280 }
281
282 /* Function to safely get fn->fn_sernum for passed in rt
283 * and store result in passed in cookie.
284 * Return true if we can get cookie safely
285 * Return false if not
286 */
fib6_get_cookie_safe(const struct fib6_info * f6i,u32 * cookie)287 static inline bool fib6_get_cookie_safe(const struct fib6_info *f6i,
288 u32 *cookie)
289 {
290 struct fib6_node *fn;
291 bool status = false;
292
293 fn = rcu_dereference(f6i->fib6_node);
294
295 if (fn) {
296 *cookie = READ_ONCE(fn->fn_sernum);
297 /* pairs with smp_wmb() in __fib6_update_sernum_upto_root() */
298 smp_rmb();
299 status = true;
300 }
301
302 return status;
303 }
304
rt6_get_cookie(const struct rt6_info * rt)305 static inline u32 rt6_get_cookie(const struct rt6_info *rt)
306 {
307 struct fib6_info *from;
308 u32 cookie = 0;
309
310 if (rt->sernum)
311 return rt->sernum;
312
313 rcu_read_lock();
314
315 from = rcu_dereference(rt->from);
316 if (from)
317 fib6_get_cookie_safe(from, &cookie);
318
319 rcu_read_unlock();
320
321 return cookie;
322 }
323
ip6_rt_put(struct rt6_info * rt)324 static inline void ip6_rt_put(struct rt6_info *rt)
325 {
326 /* dst_release() accepts a NULL parameter.
327 * We rely on dst being first structure in struct rt6_info
328 */
329 BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
330 dst_release(&rt->dst);
331 }
332
333 struct fib6_info *fib6_info_alloc(gfp_t gfp_flags, bool with_fib6_nh);
334 void fib6_info_destroy_rcu(struct rcu_head *head);
335
fib6_info_hold(struct fib6_info * f6i)336 static inline void fib6_info_hold(struct fib6_info *f6i)
337 {
338 refcount_inc(&f6i->fib6_ref);
339 }
340
fib6_info_hold_safe(struct fib6_info * f6i)341 static inline bool fib6_info_hold_safe(struct fib6_info *f6i)
342 {
343 return refcount_inc_not_zero(&f6i->fib6_ref);
344 }
345
fib6_info_release(struct fib6_info * f6i)346 static inline void fib6_info_release(struct fib6_info *f6i)
347 {
348 if (f6i && refcount_dec_and_test(&f6i->fib6_ref))
349 call_rcu(&f6i->rcu, fib6_info_destroy_rcu);
350 }
351
352 enum fib6_walk_state {
353 #ifdef CONFIG_IPV6_SUBTREES
354 FWS_S,
355 #endif
356 FWS_L,
357 FWS_R,
358 FWS_C,
359 FWS_U
360 };
361
362 struct fib6_walker {
363 struct list_head lh;
364 struct fib6_node *root, *node;
365 struct fib6_info *leaf;
366 enum fib6_walk_state state;
367 unsigned int skip;
368 unsigned int count;
369 unsigned int skip_in_node;
370 int (*func)(struct fib6_walker *);
371 void *args;
372 };
373
374 struct rt6_statistics {
375 __u32 fib_nodes; /* all fib6 nodes */
376 __u32 fib_route_nodes; /* intermediate nodes */
377 __u32 fib_rt_entries; /* rt entries in fib table */
378 __u32 fib_rt_cache; /* cached rt entries in exception table */
379 __u32 fib_discarded_routes; /* total number of routes delete */
380
381 /* The following stats are not protected by any lock */
382 atomic_t fib_rt_alloc; /* total number of routes alloced */
383 atomic_t fib_rt_uncache; /* rt entries in uncached list */
384 };
385
386 #define RTN_TL_ROOT 0x0001
387 #define RTN_ROOT 0x0002 /* tree root node */
388 #define RTN_RTINFO 0x0004 /* node with valid routing info */
389
390 /*
391 * priority levels (or metrics)
392 *
393 */
394
395
396 struct fib6_table {
397 struct hlist_node tb6_hlist;
398 u32 tb6_id;
399 spinlock_t tb6_lock;
400 struct fib6_node tb6_root;
401 struct inet_peer_base tb6_peers;
402 unsigned int flags;
403 unsigned int fib_seq;
404 #define RT6_TABLE_HAS_DFLT_ROUTER BIT(0)
405 };
406
407 #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
408 #define RT6_TABLE_MAIN RT_TABLE_MAIN
409 #define RT6_TABLE_DFLT RT6_TABLE_MAIN
410 #define RT6_TABLE_INFO RT6_TABLE_MAIN
411 #define RT6_TABLE_PREFIX RT6_TABLE_MAIN
412
413 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
414 #define FIB6_TABLE_MIN 1
415 #define FIB6_TABLE_MAX RT_TABLE_MAX
416 #define RT6_TABLE_LOCAL RT_TABLE_LOCAL
417 #else
418 #define FIB6_TABLE_MIN RT_TABLE_MAIN
419 #define FIB6_TABLE_MAX FIB6_TABLE_MIN
420 #define RT6_TABLE_LOCAL RT6_TABLE_MAIN
421 #endif
422
423 typedef struct rt6_info *(*pol_lookup_t)(struct net *,
424 struct fib6_table *,
425 struct flowi6 *,
426 const struct sk_buff *, int);
427
428 struct fib6_entry_notifier_info {
429 struct fib_notifier_info info; /* must be first */
430 struct fib6_info *rt;
431 unsigned int nsiblings;
432 };
433
434 /*
435 * exported functions
436 */
437
438 struct fib6_table *fib6_get_table(struct net *net, u32 id);
439 struct fib6_table *fib6_new_table(struct net *net, u32 id);
440 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
441 const struct sk_buff *skb,
442 int flags, pol_lookup_t lookup);
443
444 /* called with rcu lock held; can return error pointer
445 * caller needs to select path
446 */
447 int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
448 struct fib6_result *res, int flags);
449
450 /* called with rcu lock held; caller needs to select path */
451 int fib6_table_lookup(struct net *net, struct fib6_table *table,
452 int oif, struct flowi6 *fl6, struct fib6_result *res,
453 int strict);
454
455 void fib6_select_path(const struct net *net, struct fib6_result *res,
456 struct flowi6 *fl6, int oif, bool have_oif_match,
457 const struct sk_buff *skb, int strict);
458 struct fib6_node *fib6_node_lookup(struct fib6_node *root,
459 const struct in6_addr *daddr,
460 const struct in6_addr *saddr);
461
462 struct fib6_node *fib6_locate(struct fib6_node *root,
463 const struct in6_addr *daddr, int dst_len,
464 const struct in6_addr *saddr, int src_len,
465 bool exact_match);
466
467 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *arg),
468 void *arg);
469 void fib6_clean_all_skip_notify(struct net *net,
470 int (*func)(struct fib6_info *, void *arg),
471 void *arg);
472
473 int fib6_add(struct fib6_node *root, struct fib6_info *rt,
474 struct nl_info *info, struct netlink_ext_ack *extack);
475 int fib6_del(struct fib6_info *rt, struct nl_info *info);
476
477 static inline
rt6_get_prefsrc(const struct rt6_info * rt,struct in6_addr * addr)478 void rt6_get_prefsrc(const struct rt6_info *rt, struct in6_addr *addr)
479 {
480 const struct fib6_info *from;
481
482 rcu_read_lock();
483
484 from = rcu_dereference(rt->from);
485 if (from) {
486 *addr = from->fib6_prefsrc.addr;
487 } else {
488 struct in6_addr in6_zero = {};
489
490 *addr = in6_zero;
491 }
492
493 rcu_read_unlock();
494 }
495
496 int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
497 struct fib6_config *cfg, gfp_t gfp_flags,
498 struct netlink_ext_ack *extack);
499 void fib6_nh_release(struct fib6_nh *fib6_nh);
500 void fib6_nh_release_dsts(struct fib6_nh *fib6_nh);
501
502 int call_fib6_entry_notifiers(struct net *net,
503 enum fib_event_type event_type,
504 struct fib6_info *rt,
505 struct netlink_ext_ack *extack);
506 int call_fib6_multipath_entry_notifiers(struct net *net,
507 enum fib_event_type event_type,
508 struct fib6_info *rt,
509 unsigned int nsiblings,
510 struct netlink_ext_ack *extack);
511 int call_fib6_entry_notifiers_replace(struct net *net, struct fib6_info *rt);
512 void fib6_rt_update(struct net *net, struct fib6_info *rt,
513 struct nl_info *info);
514 void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
515 unsigned int flags);
516
517 void fib6_run_gc(unsigned long expires, struct net *net, bool force);
518
519 void fib6_gc_cleanup(void);
520
521 int fib6_init(void);
522
523 struct ipv6_route_iter {
524 struct seq_net_private p;
525 struct fib6_walker w;
526 loff_t skip;
527 struct fib6_table *tbl;
528 int sernum;
529 };
530
531 extern const struct seq_operations ipv6_route_seq_ops;
532
533 int call_fib6_notifier(struct notifier_block *nb,
534 enum fib_event_type event_type,
535 struct fib_notifier_info *info);
536 int call_fib6_notifiers(struct net *net, enum fib_event_type event_type,
537 struct fib_notifier_info *info);
538
539 int __net_init fib6_notifier_init(struct net *net);
540 void __net_exit fib6_notifier_exit(struct net *net);
541
542 unsigned int fib6_tables_seq_read(struct net *net);
543 int fib6_tables_dump(struct net *net, struct notifier_block *nb,
544 struct netlink_ext_ack *extack);
545
546 void fib6_update_sernum(struct net *net, struct fib6_info *rt);
547 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt);
548 void fib6_update_sernum_stub(struct net *net, struct fib6_info *f6i);
549
550 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val);
fib6_metric_locked(struct fib6_info * f6i,int metric)551 static inline bool fib6_metric_locked(struct fib6_info *f6i, int metric)
552 {
553 return !!(f6i->fib6_metrics->metrics[RTAX_LOCK - 1] & (1 << metric));
554 }
555 void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
556 bool offload, bool trap, bool offload_failed);
557
558 #if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL)
559 struct bpf_iter__ipv6_route {
560 __bpf_md_ptr(struct bpf_iter_meta *, meta);
561 __bpf_md_ptr(struct fib6_info *, rt);
562 };
563 #endif
564
565 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_output(struct net *net,
566 struct fib6_table *table,
567 struct flowi6 *fl6,
568 const struct sk_buff *skb,
569 int flags));
570 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_input(struct net *net,
571 struct fib6_table *table,
572 struct flowi6 *fl6,
573 const struct sk_buff *skb,
574 int flags));
575 INDIRECT_CALLABLE_DECLARE(struct rt6_info *__ip6_route_redirect(struct net *net,
576 struct fib6_table *table,
577 struct flowi6 *fl6,
578 const struct sk_buff *skb,
579 int flags));
580 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_lookup(struct net *net,
581 struct fib6_table *table,
582 struct flowi6 *fl6,
583 const struct sk_buff *skb,
584 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)585 static inline struct rt6_info *pol_lookup_func(pol_lookup_t lookup,
586 struct net *net,
587 struct fib6_table *table,
588 struct flowi6 *fl6,
589 const struct sk_buff *skb,
590 int flags)
591 {
592 return INDIRECT_CALL_4(lookup,
593 ip6_pol_route_output,
594 ip6_pol_route_input,
595 ip6_pol_route_lookup,
596 __ip6_route_redirect,
597 net, table, fl6, skb, flags);
598 }
599
600 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
fib6_has_custom_rules(const struct net * net)601 static inline bool fib6_has_custom_rules(const struct net *net)
602 {
603 return net->ipv6.fib6_has_custom_rules;
604 }
605
606 int fib6_rules_init(void);
607 void fib6_rules_cleanup(void);
608 bool fib6_rule_default(const struct fib_rule *rule);
609 int fib6_rules_dump(struct net *net, struct notifier_block *nb,
610 struct netlink_ext_ack *extack);
611 unsigned int fib6_rules_seq_read(struct net *net);
612
fib6_rules_early_flow_dissect(struct net * net,struct sk_buff * skb,struct flowi6 * fl6,struct flow_keys * flkeys)613 static inline bool fib6_rules_early_flow_dissect(struct net *net,
614 struct sk_buff *skb,
615 struct flowi6 *fl6,
616 struct flow_keys *flkeys)
617 {
618 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
619
620 if (!net->ipv6.fib6_rules_require_fldissect)
621 return false;
622
623 memset(flkeys, 0, sizeof(*flkeys));
624 __skb_flow_dissect(net, skb, &flow_keys_dissector,
625 flkeys, NULL, 0, 0, 0, flag);
626
627 fl6->fl6_sport = flkeys->ports.src;
628 fl6->fl6_dport = flkeys->ports.dst;
629 fl6->flowi6_proto = flkeys->basic.ip_proto;
630
631 return true;
632 }
633 #else
fib6_has_custom_rules(const struct net * net)634 static inline bool fib6_has_custom_rules(const struct net *net)
635 {
636 return false;
637 }
fib6_rules_init(void)638 static inline int fib6_rules_init(void)
639 {
640 return 0;
641 }
fib6_rules_cleanup(void)642 static inline void fib6_rules_cleanup(void)
643 {
644 return ;
645 }
fib6_rule_default(const struct fib_rule * rule)646 static inline bool fib6_rule_default(const struct fib_rule *rule)
647 {
648 return true;
649 }
fib6_rules_dump(struct net * net,struct notifier_block * nb,struct netlink_ext_ack * extack)650 static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb,
651 struct netlink_ext_ack *extack)
652 {
653 return 0;
654 }
fib6_rules_seq_read(struct net * net)655 static inline unsigned int fib6_rules_seq_read(struct net *net)
656 {
657 return 0;
658 }
fib6_rules_early_flow_dissect(struct net * net,struct sk_buff * skb,struct flowi6 * fl6,struct flow_keys * flkeys)659 static inline bool fib6_rules_early_flow_dissect(struct net *net,
660 struct sk_buff *skb,
661 struct flowi6 *fl6,
662 struct flow_keys *flkeys)
663 {
664 return false;
665 }
666 #endif
667 #endif
668