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