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 #include <uapi/linux/bpf.h>
25
26 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
27 #define FIB6_TABLE_HASHSZ 256
28 #else
29 #define FIB6_TABLE_HASHSZ 1
30 #endif
31
32 #define RT6_DEBUG 2
33
34 #if RT6_DEBUG >= 3
35 #define RT6_TRACE(x...) pr_debug(x)
36 #else
37 #define RT6_TRACE(x...) do { ; } while (0)
38 #endif
39
40 struct rt6_info;
41 struct fib6_info;
42
43 struct fib6_config {
44 u32 fc_table;
45 u32 fc_metric;
46 int fc_dst_len;
47 int fc_src_len;
48 int fc_ifindex;
49 u32 fc_flags;
50 u32 fc_protocol;
51 u16 fc_type; /* only 8 bits are used */
52 u16 fc_delete_all_nh : 1,
53 fc_ignore_dev_down:1,
54 __unused : 14;
55 u32 fc_nh_id;
56
57 struct in6_addr fc_dst;
58 struct in6_addr fc_src;
59 struct in6_addr fc_prefsrc;
60 struct in6_addr fc_gateway;
61
62 unsigned long fc_expires;
63 struct nlattr *fc_mx;
64 int fc_mx_len;
65 int fc_mp_len;
66 struct nlattr *fc_mp;
67
68 struct nl_info fc_nlinfo;
69 struct nlattr *fc_encap;
70 u16 fc_encap_type;
71 bool fc_is_fdb;
72
73 ANDROID_KABI_RESERVE(1);
74 };
75
76 struct fib6_node {
77 struct fib6_node __rcu *parent;
78 struct fib6_node __rcu *left;
79 struct fib6_node __rcu *right;
80 #ifdef CONFIG_IPV6_SUBTREES
81 struct fib6_node __rcu *subtree;
82 #endif
83 struct fib6_info __rcu *leaf;
84
85 __u16 fn_bit; /* bit key */
86 __u16 fn_flags;
87 int fn_sernum;
88 struct fib6_info __rcu *rr_ptr;
89 struct rcu_head rcu;
90
91 ANDROID_KABI_RESERVE(1);
92 };
93
94 struct fib6_gc_args {
95 int timeout;
96 int more;
97 };
98
99 #ifndef CONFIG_IPV6_SUBTREES
100 #define FIB6_SUBTREE(fn) NULL
101
fib6_routes_require_src(const struct net * net)102 static inline bool fib6_routes_require_src(const struct net *net)
103 {
104 return false;
105 }
106
fib6_routes_require_src_inc(struct net * net)107 static inline void fib6_routes_require_src_inc(struct net *net) {}
fib6_routes_require_src_dec(struct net * net)108 static inline void fib6_routes_require_src_dec(struct net *net) {}
109
110 #else
111
fib6_routes_require_src(const struct net * net)112 static inline bool fib6_routes_require_src(const struct net *net)
113 {
114 return net->ipv6.fib6_routes_require_src > 0;
115 }
116
fib6_routes_require_src_inc(struct net * net)117 static inline void fib6_routes_require_src_inc(struct net *net)
118 {
119 net->ipv6.fib6_routes_require_src++;
120 }
121
fib6_routes_require_src_dec(struct net * net)122 static inline void fib6_routes_require_src_dec(struct net *net)
123 {
124 net->ipv6.fib6_routes_require_src--;
125 }
126
127 #define FIB6_SUBTREE(fn) (rcu_dereference_protected((fn)->subtree, 1))
128 #endif
129
130 /*
131 * routing information
132 *
133 */
134
135 struct rt6key {
136 struct in6_addr addr;
137 int plen;
138 };
139
140 struct fib6_table;
141
142 struct rt6_exception_bucket {
143 struct hlist_head chain;
144 int depth;
145 };
146
147 struct rt6_exception {
148 struct hlist_node hlist;
149 struct rt6_info *rt6i;
150 unsigned long stamp;
151 struct rcu_head rcu;
152 };
153
154 #define FIB6_EXCEPTION_BUCKET_SIZE_SHIFT 10
155 #define FIB6_EXCEPTION_BUCKET_SIZE (1 << FIB6_EXCEPTION_BUCKET_SIZE_SHIFT)
156 #define FIB6_MAX_DEPTH 5
157
158 struct fib6_nh {
159 struct fib_nh_common nh_common;
160
161 #ifdef CONFIG_IPV6_ROUTER_PREF
162 unsigned long last_probe;
163 #endif
164
165 struct rt6_info * __percpu *rt6i_pcpu;
166 struct rt6_exception_bucket __rcu *rt6i_exception_bucket;
167 };
168
169 struct fib6_info {
170 struct fib6_table *fib6_table;
171 struct fib6_info __rcu *fib6_next;
172 struct fib6_node __rcu *fib6_node;
173
174 /* Multipath routes:
175 * siblings is a list of fib6_info that have the same metric/weight,
176 * destination, but not the same gateway. nsiblings is just a cache
177 * to speed up lookup.
178 */
179 union {
180 struct list_head fib6_siblings;
181 struct list_head nh_list;
182 };
183 unsigned int fib6_nsiblings;
184
185 refcount_t fib6_ref;
186 unsigned long expires;
187 struct dst_metrics *fib6_metrics;
188 #define fib6_pmtu fib6_metrics->metrics[RTAX_MTU-1]
189
190 struct rt6key fib6_dst;
191 u32 fib6_flags;
192 struct rt6key fib6_src;
193 struct rt6key fib6_prefsrc;
194
195 u32 fib6_metric;
196 u8 fib6_protocol;
197 u8 fib6_type;
198
199 u8 offload;
200 u8 trap;
201 u8 offload_failed;
202
203 u8 should_flush:1,
204 dst_nocount:1,
205 dst_nopolicy:1,
206 fib6_destroying:1,
207 unused:4;
208
209 struct rcu_head rcu;
210 struct nexthop *nh;
211
212 ANDROID_KABI_RESERVE(1);
213
214 struct fib6_nh fib6_nh[];
215 };
216
217 struct rt6_info {
218 struct dst_entry dst;
219 struct fib6_info __rcu *from;
220 int sernum;
221
222 struct rt6key rt6i_dst;
223 struct rt6key rt6i_src;
224 struct in6_addr rt6i_gateway;
225 struct inet6_dev *rt6i_idev;
226 u32 rt6i_flags;
227
228 struct list_head rt6i_uncached;
229 struct uncached_list *rt6i_uncached_list;
230
231 /* more non-fragment space at head required */
232 unsigned short rt6i_nfheader_len;
233
234 ANDROID_KABI_RESERVE(1);
235 };
236
237 struct fib6_result {
238 struct fib6_nh *nh;
239 struct fib6_info *f6i;
240 u32 fib6_flags;
241 u8 fib6_type;
242 struct rt6_info *rt6;
243 };
244
245 #define for_each_fib6_node_rt_rcu(fn) \
246 for (rt = rcu_dereference((fn)->leaf); rt; \
247 rt = rcu_dereference(rt->fib6_next))
248
249 #define for_each_fib6_walker_rt(w) \
250 for (rt = (w)->leaf; rt; \
251 rt = rcu_dereference_protected(rt->fib6_next, 1))
252
ip6_dst_idev(struct dst_entry * dst)253 static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
254 {
255 return ((struct rt6_info *)dst)->rt6i_idev;
256 }
257
fib6_requires_src(const struct fib6_info * rt)258 static inline bool fib6_requires_src(const struct fib6_info *rt)
259 {
260 return rt->fib6_src.plen > 0;
261 }
262
fib6_clean_expires(struct fib6_info * f6i)263 static inline void fib6_clean_expires(struct fib6_info *f6i)
264 {
265 f6i->fib6_flags &= ~RTF_EXPIRES;
266 f6i->expires = 0;
267 }
268
fib6_set_expires(struct fib6_info * f6i,unsigned long expires)269 static inline void fib6_set_expires(struct fib6_info *f6i,
270 unsigned long expires)
271 {
272 f6i->expires = expires;
273 f6i->fib6_flags |= RTF_EXPIRES;
274 }
275
fib6_check_expired(const struct fib6_info * f6i)276 static inline bool fib6_check_expired(const struct fib6_info *f6i)
277 {
278 if (f6i->fib6_flags & RTF_EXPIRES)
279 return time_after(jiffies, f6i->expires);
280 return false;
281 }
282
283 /* Function to safely get fn->fn_sernum for passed in rt
284 * and store result in passed in cookie.
285 * Return true if we can get cookie safely
286 * Return false if not
287 */
fib6_get_cookie_safe(const struct fib6_info * f6i,u32 * cookie)288 static inline bool fib6_get_cookie_safe(const struct fib6_info *f6i,
289 u32 *cookie)
290 {
291 struct fib6_node *fn;
292 bool status = false;
293
294 fn = rcu_dereference(f6i->fib6_node);
295
296 if (fn) {
297 *cookie = READ_ONCE(fn->fn_sernum);
298 /* pairs with smp_wmb() in __fib6_update_sernum_upto_root() */
299 smp_rmb();
300 status = true;
301 }
302
303 return status;
304 }
305
rt6_get_cookie(const struct rt6_info * rt)306 static inline u32 rt6_get_cookie(const struct rt6_info *rt)
307 {
308 struct fib6_info *from;
309 u32 cookie = 0;
310
311 if (rt->sernum)
312 return rt->sernum;
313
314 rcu_read_lock();
315
316 from = rcu_dereference(rt->from);
317 if (from)
318 fib6_get_cookie_safe(from, &cookie);
319
320 rcu_read_unlock();
321
322 return cookie;
323 }
324
ip6_rt_put(struct rt6_info * rt)325 static inline void ip6_rt_put(struct rt6_info *rt)
326 {
327 /* dst_release() accepts a NULL parameter.
328 * We rely on dst being first structure in struct rt6_info
329 */
330 BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
331 dst_release(&rt->dst);
332 }
333
334 struct fib6_info *fib6_info_alloc(gfp_t gfp_flags, bool with_fib6_nh);
335 void fib6_info_destroy_rcu(struct rcu_head *head);
336
fib6_info_hold(struct fib6_info * f6i)337 static inline void fib6_info_hold(struct fib6_info *f6i)
338 {
339 refcount_inc(&f6i->fib6_ref);
340 }
341
fib6_info_hold_safe(struct fib6_info * f6i)342 static inline bool fib6_info_hold_safe(struct fib6_info *f6i)
343 {
344 return refcount_inc_not_zero(&f6i->fib6_ref);
345 }
346
fib6_info_release(struct fib6_info * f6i)347 static inline void fib6_info_release(struct fib6_info *f6i)
348 {
349 if (f6i && refcount_dec_and_test(&f6i->fib6_ref))
350 call_rcu(&f6i->rcu, fib6_info_destroy_rcu);
351 }
352
353 enum fib6_walk_state {
354 #ifdef CONFIG_IPV6_SUBTREES
355 FWS_S,
356 #endif
357 FWS_L,
358 FWS_R,
359 FWS_C,
360 FWS_U
361 };
362
363 struct fib6_walker {
364 struct list_head lh;
365 struct fib6_node *root, *node;
366 struct fib6_info *leaf;
367 enum fib6_walk_state state;
368 unsigned int skip;
369 unsigned int count;
370 unsigned int skip_in_node;
371 int (*func)(struct fib6_walker *);
372 void *args;
373 };
374
375 struct rt6_statistics {
376 __u32 fib_nodes; /* all fib6 nodes */
377 __u32 fib_route_nodes; /* intermediate nodes */
378 __u32 fib_rt_entries; /* rt entries in fib table */
379 __u32 fib_rt_cache; /* cached rt entries in exception table */
380 __u32 fib_discarded_routes; /* total number of routes delete */
381
382 /* The following stat is not protected by any lock */
383 atomic_t fib_rt_alloc; /* total number of routes alloced */
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 *addr = in6addr_any;
489
490 rcu_read_unlock();
491 }
492
493 int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
494 struct fib6_config *cfg, gfp_t gfp_flags,
495 struct netlink_ext_ack *extack);
496 void fib6_nh_release(struct fib6_nh *fib6_nh);
497 void fib6_nh_release_dsts(struct fib6_nh *fib6_nh);
498
499 int call_fib6_entry_notifiers(struct net *net,
500 enum fib_event_type event_type,
501 struct fib6_info *rt,
502 struct netlink_ext_ack *extack);
503 int call_fib6_multipath_entry_notifiers(struct net *net,
504 enum fib_event_type event_type,
505 struct fib6_info *rt,
506 unsigned int nsiblings,
507 struct netlink_ext_ack *extack);
508 int call_fib6_entry_notifiers_replace(struct net *net, struct fib6_info *rt);
509 void fib6_rt_update(struct net *net, struct fib6_info *rt,
510 struct nl_info *info);
511 void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
512 unsigned int flags);
513
514 void fib6_run_gc(unsigned long expires, struct net *net, bool force);
515
516 void fib6_gc_cleanup(void);
517
518 int fib6_init(void);
519
520 struct ipv6_route_iter {
521 struct seq_net_private p;
522 struct fib6_walker w;
523 loff_t skip;
524 struct fib6_table *tbl;
525 int sernum;
526 };
527
528 extern const struct seq_operations ipv6_route_seq_ops;
529
530 int call_fib6_notifier(struct notifier_block *nb,
531 enum fib_event_type event_type,
532 struct fib_notifier_info *info);
533 int call_fib6_notifiers(struct net *net, enum fib_event_type event_type,
534 struct fib_notifier_info *info);
535
536 int __net_init fib6_notifier_init(struct net *net);
537 void __net_exit fib6_notifier_exit(struct net *net);
538
539 unsigned int fib6_tables_seq_read(struct net *net);
540 int fib6_tables_dump(struct net *net, struct notifier_block *nb,
541 struct netlink_ext_ack *extack);
542
543 void fib6_update_sernum(struct net *net, struct fib6_info *rt);
544 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt);
545 void fib6_update_sernum_stub(struct net *net, struct fib6_info *f6i);
546
547 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val);
fib6_metric_locked(struct fib6_info * f6i,int metric)548 static inline bool fib6_metric_locked(struct fib6_info *f6i, int metric)
549 {
550 return !!(f6i->fib6_metrics->metrics[RTAX_LOCK - 1] & (1 << metric));
551 }
552 void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
553 bool offload, bool trap, bool offload_failed);
554
555 #if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL)
556 struct bpf_iter__ipv6_route {
557 __bpf_md_ptr(struct bpf_iter_meta *, meta);
558 __bpf_md_ptr(struct fib6_info *, rt);
559 };
560 #endif
561
562 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_output(struct net *net,
563 struct fib6_table *table,
564 struct flowi6 *fl6,
565 const struct sk_buff *skb,
566 int flags));
567 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_input(struct net *net,
568 struct fib6_table *table,
569 struct flowi6 *fl6,
570 const struct sk_buff *skb,
571 int flags));
572 INDIRECT_CALLABLE_DECLARE(struct rt6_info *__ip6_route_redirect(struct net *net,
573 struct fib6_table *table,
574 struct flowi6 *fl6,
575 const struct sk_buff *skb,
576 int flags));
577 INDIRECT_CALLABLE_DECLARE(struct rt6_info *ip6_pol_route_lookup(struct net *net,
578 struct fib6_table *table,
579 struct flowi6 *fl6,
580 const struct sk_buff *skb,
581 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)582 static inline struct rt6_info *pol_lookup_func(pol_lookup_t lookup,
583 struct net *net,
584 struct fib6_table *table,
585 struct flowi6 *fl6,
586 const struct sk_buff *skb,
587 int flags)
588 {
589 return INDIRECT_CALL_4(lookup,
590 ip6_pol_route_output,
591 ip6_pol_route_input,
592 ip6_pol_route_lookup,
593 __ip6_route_redirect,
594 net, table, fl6, skb, flags);
595 }
596
597 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
fib6_has_custom_rules(const struct net * net)598 static inline bool fib6_has_custom_rules(const struct net *net)
599 {
600 return net->ipv6.fib6_has_custom_rules;
601 }
602
603 int fib6_rules_init(void);
604 void fib6_rules_cleanup(void);
605 bool fib6_rule_default(const struct fib_rule *rule);
606 int fib6_rules_dump(struct net *net, struct notifier_block *nb,
607 struct netlink_ext_ack *extack);
608 unsigned int fib6_rules_seq_read(struct net *net);
609
fib6_rules_early_flow_dissect(struct net * net,struct sk_buff * skb,struct flowi6 * fl6,struct flow_keys * flkeys)610 static inline bool fib6_rules_early_flow_dissect(struct net *net,
611 struct sk_buff *skb,
612 struct flowi6 *fl6,
613 struct flow_keys *flkeys)
614 {
615 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
616
617 if (!net->ipv6.fib6_rules_require_fldissect)
618 return false;
619
620 memset(flkeys, 0, sizeof(*flkeys));
621 __skb_flow_dissect(net, skb, &flow_keys_dissector,
622 flkeys, NULL, 0, 0, 0, flag);
623
624 fl6->fl6_sport = flkeys->ports.src;
625 fl6->fl6_dport = flkeys->ports.dst;
626 fl6->flowi6_proto = flkeys->basic.ip_proto;
627
628 return true;
629 }
630 #else
fib6_has_custom_rules(const struct net * net)631 static inline bool fib6_has_custom_rules(const struct net *net)
632 {
633 return false;
634 }
fib6_rules_init(void)635 static inline int fib6_rules_init(void)
636 {
637 return 0;
638 }
fib6_rules_cleanup(void)639 static inline void fib6_rules_cleanup(void)
640 {
641 return ;
642 }
fib6_rule_default(const struct fib_rule * rule)643 static inline bool fib6_rule_default(const struct fib_rule *rule)
644 {
645 return true;
646 }
fib6_rules_dump(struct net * net,struct notifier_block * nb,struct netlink_ext_ack * extack)647 static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb,
648 struct netlink_ext_ack *extack)
649 {
650 return 0;
651 }
fib6_rules_seq_read(struct net * net)652 static inline unsigned int fib6_rules_seq_read(struct net *net)
653 {
654 return 0;
655 }
fib6_rules_early_flow_dissect(struct net * net,struct sk_buff * skb,struct flowi6 * fl6,struct flow_keys * flkeys)656 static inline bool fib6_rules_early_flow_dissect(struct net *net,
657 struct sk_buff *skb,
658 struct flowi6 *fl6,
659 struct flow_keys *flkeys)
660 {
661 return false;
662 }
663 #endif
664 #endif
665