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