]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - include/net/ip6_fib.h
ipv6: prepare rt6_clean_tohost() for exception table
[mirror_ubuntu-eoan-kernel.git] / include / net / ip6_fib.h
CommitLineData
1da177e4
LT
1/*
2 * Linux INET6 implementation
3 *
4 * Authors:
5 * Pedro Roque <roque@di.fc.ul.pt>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#ifndef _IP6_FIB_H
14#define _IP6_FIB_H
15
1da177e4 16#include <linux/ipv6_route.h>
1da177e4
LT
17#include <linux/rtnetlink.h>
18#include <linux/spinlock.h>
16ab6d7d 19#include <linux/notifier.h>
86872cb5
TG
20#include <net/dst.h>
21#include <net/flow.h>
22#include <net/netlink.h>
b3419363 23#include <net/inetpeer.h>
16ab6d7d 24#include <net/fib_notifier.h>
1da177e4 25
a33bc5c1
NH
26#ifdef CONFIG_IPV6_MULTIPLE_TABLES
27#define FIB6_TABLE_HASHSZ 256
28#else
29#define FIB6_TABLE_HASHSZ 1
30#endif
31
1da177e4
LT
32struct rt6_info;
33
fd2c3ef7 34struct fib6_config {
86872cb5
TG
35 u32 fc_table;
36 u32 fc_metric;
37 int fc_dst_len;
38 int fc_src_len;
39 int fc_ifindex;
40 u32 fc_flags;
41 u32 fc_protocol;
0ae81335
DA
42 u16 fc_type; /* only 8 bits are used */
43 u16 fc_delete_all_nh : 1,
44 __unused : 15;
86872cb5
TG
45
46 struct in6_addr fc_dst;
47 struct in6_addr fc_src;
c3968a85 48 struct in6_addr fc_prefsrc;
86872cb5
TG
49 struct in6_addr fc_gateway;
50
51 unsigned long fc_expires;
52 struct nlattr *fc_mx;
53 int fc_mx_len;
51ebd318
ND
54 int fc_mp_len;
55 struct nlattr *fc_mp;
86872cb5
TG
56
57 struct nl_info fc_nlinfo;
19e42e45
RP
58 struct nlattr *fc_encap;
59 u16 fc_encap_type;
86872cb5
TG
60};
61
fd2c3ef7 62struct fib6_node {
1da177e4
LT
63 struct fib6_node *parent;
64 struct fib6_node *left;
65 struct fib6_node *right;
8bce65b9 66#ifdef CONFIG_IPV6_SUBTREES
1da177e4 67 struct fib6_node *subtree;
8bce65b9 68#endif
1da177e4
LT
69 struct rt6_info *leaf;
70
71 __u16 fn_bit; /* bit key */
72 __u16 fn_flags;
42b18706 73 int fn_sernum;
f11e6659 74 struct rt6_info *rr_ptr;
c5cff856 75 struct rcu_head rcu;
1da177e4
LT
76};
77
7fc33165
YH
78#ifndef CONFIG_IPV6_SUBTREES
79#define FIB6_SUBTREE(fn) NULL
80#else
81#define FIB6_SUBTREE(fn) ((fn)->subtree)
82#endif
1da177e4 83
e715b6d3
FW
84struct mx6_config {
85 const u32 *mx;
86 DECLARE_BITMAP(mx_valid, RTAX_MAX);
87};
88
1da177e4
LT
89/*
90 * routing information
91 *
92 */
93
fd2c3ef7 94struct rt6key {
1da177e4
LT
95 struct in6_addr addr;
96 int plen;
97};
98
c71099ac
TG
99struct fib6_table;
100
35732d01
WW
101struct rt6_exception_bucket {
102 struct hlist_head chain;
103 int depth;
104};
105
106struct rt6_exception {
107 struct hlist_node hlist;
108 struct rt6_info *rt6i;
109 unsigned long stamp;
110 struct rcu_head rcu;
111};
112
113#define FIB6_EXCEPTION_BUCKET_SIZE_SHIFT 10
114#define FIB6_EXCEPTION_BUCKET_SIZE (1 << FIB6_EXCEPTION_BUCKET_SIZE_SHIFT)
115#define FIB6_MAX_DEPTH 5
116
fd2c3ef7 117struct rt6_info {
d8d1f30b 118 struct dst_entry dst;
1da177e4 119
bd2c77a0
YH
120 /*
121 * Tail elements of dst_entry (__refcnt etc.)
122 * and these elements (rarely used in hot path) are in
123 * the same cache line.
124 */
125 struct fib6_table *rt6i_table;
4e587ea7 126 struct fib6_node __rcu *rt6i_node;
1da177e4
LT
127
128 struct in6_addr rt6i_gateway;
1da177e4 129
51ebd318
ND
130 /* Multipath routes:
131 * siblings is a list of rt6_info that have the the same metric/weight,
132 * destination, but not the same gateway. nsiblings is just a cache
133 * to speed up lookup.
134 */
135 struct list_head rt6i_siblings;
136 unsigned int rt6i_nsiblings;
137
bd2c77a0 138 atomic_t rt6i_ref;
a47ed4cd 139
fe400799
IS
140 unsigned int rt6i_nh_flags;
141
bd2c77a0
YH
142 /* These are in a separate cache line. */
143 struct rt6key rt6i_dst ____cacheline_aligned_in_smp;
144 u32 rt6i_flags;
145 struct rt6key rt6i_src;
c3968a85 146 struct rt6key rt6i_prefsrc;
b4ce9277 147
8d0b94af
MKL
148 struct list_head rt6i_uncached;
149 struct uncached_list *rt6i_uncached_list;
150
bd2c77a0 151 struct inet6_dev *rt6i_idev;
d52d3997 152 struct rt6_info * __percpu *rt6i_pcpu;
35732d01 153 struct rt6_exception_bucket __rcu *rt6i_exception_bucket;
b4ce9277 154
705f1c86 155 u32 rt6i_metric;
4b32b5ad 156 u32 rt6i_pmtu;
bd2c77a0
YH
157 /* more non-fragment space at head required */
158 unsigned short rt6i_nfheader_len;
bd2c77a0 159 u8 rt6i_protocol;
35732d01
WW
160 u8 exception_bucket_flushed:1,
161 unused:7;
1da177e4
LT
162};
163
7a3025b1
YH
164static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
165{
166 return ((struct rt6_info *)dst)->rt6i_idev;
167}
168
1716a961
G
169static inline void rt6_clean_expires(struct rt6_info *rt)
170{
1716a961 171 rt->rt6i_flags &= ~RTF_EXPIRES;
01ba16d6 172 rt->dst.expires = 0;
1716a961
G
173}
174
175static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires)
176{
1716a961 177 rt->dst.expires = expires;
ecd98837 178 rt->rt6i_flags |= RTF_EXPIRES;
1716a961
G
179}
180
ecd98837 181static inline void rt6_update_expires(struct rt6_info *rt0, int timeout)
1716a961 182{
ecd98837
YH
183 struct rt6_info *rt;
184
185 for (rt = rt0; rt && !(rt->rt6i_flags & RTF_EXPIRES);
186 rt = (struct rt6_info *)rt->dst.from);
187 if (rt && rt != rt0)
188 rt0->dst.expires = rt->dst.expires;
189
190 dst_set_expires(&rt0->dst, timeout);
191 rt0->rt6i_flags |= RTF_EXPIRES;
1716a961 192}
b197df4f 193
c5cff856
WW
194/* Function to safely get fn->sernum for passed in rt
195 * and store result in passed in cookie.
196 * Return true if we can get cookie safely
197 * Return false if not
198 */
199static inline bool rt6_get_cookie_safe(const struct rt6_info *rt,
200 u32 *cookie)
201{
202 struct fib6_node *fn;
203 bool status = false;
204
205 rcu_read_lock();
206 fn = rcu_dereference(rt->rt6i_node);
207
208 if (fn) {
209 *cookie = fn->fn_sernum;
210 status = true;
211 }
212
213 rcu_read_unlock();
214 return status;
215}
216
b197df4f
MKL
217static inline u32 rt6_get_cookie(const struct rt6_info *rt)
218{
c5cff856
WW
219 u32 cookie = 0;
220
02bcf4e0 221 if (rt->rt6i_flags & RTF_PCPU ||
a4c2fd7f 222 (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from))
3da59bd9
MKL
223 rt = (struct rt6_info *)(rt->dst.from);
224
c5cff856
WW
225 rt6_get_cookie_safe(rt, &cookie);
226
227 return cookie;
b197df4f 228}
1716a961 229
94e187c0
AW
230static inline void ip6_rt_put(struct rt6_info *rt)
231{
232 /* dst_release() accepts a NULL parameter.
233 * We rely on dst being first structure in struct rt6_info
234 */
235 BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
236 dst_release(&rt->dst);
237}
238
a460aa83
IS
239void rt6_free_pcpu(struct rt6_info *non_pcpu_rt);
240
241static inline void rt6_hold(struct rt6_info *rt)
242{
243 atomic_inc(&rt->rt6i_ref);
244}
245
246static inline void rt6_release(struct rt6_info *rt)
247{
248 if (atomic_dec_and_test(&rt->rt6i_ref)) {
249 rt6_free_pcpu(rt);
250 dst_dev_put(&rt->dst);
251 dst_release(&rt->dst);
252 }
253}
254
94b2cfe0
HFS
255enum fib6_walk_state {
256#ifdef CONFIG_IPV6_SUBTREES
257 FWS_S,
258#endif
259 FWS_L,
260 FWS_R,
261 FWS_C,
262 FWS_U
263};
264
265struct fib6_walker {
bbef49da 266 struct list_head lh;
1da177e4
LT
267 struct fib6_node *root, *node;
268 struct rt6_info *leaf;
94b2cfe0
HFS
269 enum fib6_walk_state state;
270 bool prune;
2bec5a36
PM
271 unsigned int skip;
272 unsigned int count;
94b2cfe0 273 int (*func)(struct fib6_walker *);
1da177e4
LT
274 void *args;
275};
276
1da177e4
LT
277struct rt6_statistics {
278 __u32 fib_nodes;
279 __u32 fib_route_nodes;
280 __u32 fib_rt_alloc; /* permanent routes */
281 __u32 fib_rt_entries; /* rt entries in table */
282 __u32 fib_rt_cache; /* cache routes */
283 __u32 fib_discarded_routes;
284};
285
286#define RTN_TL_ROOT 0x0001
287#define RTN_ROOT 0x0002 /* tree root node */
288#define RTN_RTINFO 0x0004 /* node with valid routing info */
289
290/*
291 * priority levels (or metrics)
292 *
293 */
294
1da177e4 295
c71099ac
TG
296struct fib6_table {
297 struct hlist_node tb6_hlist;
298 u32 tb6_id;
299 rwlock_t tb6_lock;
300 struct fib6_node tb6_root;
8e773277 301 struct inet_peer_base tb6_peers;
830218c1 302 unsigned int flags;
e1ee0a5b 303 unsigned int fib_seq;
830218c1 304#define RT6_TABLE_HAS_DFLT_ROUTER BIT(0)
c71099ac
TG
305};
306
307#define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
308#define RT6_TABLE_MAIN RT_TABLE_MAIN
c71099ac
TG
309#define RT6_TABLE_DFLT RT6_TABLE_MAIN
310#define RT6_TABLE_INFO RT6_TABLE_MAIN
311#define RT6_TABLE_PREFIX RT6_TABLE_MAIN
312
313#ifdef CONFIG_IPV6_MULTIPLE_TABLES
314#define FIB6_TABLE_MIN 1
315#define FIB6_TABLE_MAX RT_TABLE_MAX
101367c2 316#define RT6_TABLE_LOCAL RT_TABLE_LOCAL
c71099ac
TG
317#else
318#define FIB6_TABLE_MIN RT_TABLE_MAIN
319#define FIB6_TABLE_MAX FIB6_TABLE_MIN
101367c2 320#define RT6_TABLE_LOCAL RT6_TABLE_MAIN
c71099ac
TG
321#endif
322
8ed67789
DL
323typedef struct rt6_info *(*pol_lookup_t)(struct net *,
324 struct fib6_table *,
4c9483b2 325 struct flowi6 *, int);
1da177e4 326
df77fe4d
IS
327struct fib6_entry_notifier_info {
328 struct fib_notifier_info info; /* must be first */
329 struct rt6_info *rt;
330};
331
1da177e4
LT
332/*
333 * exported functions
334 */
335
5c3a0fd7
JP
336struct fib6_table *fib6_get_table(struct net *net, u32 id);
337struct fib6_table *fib6_new_table(struct net *net, u32 id);
338struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
339 int flags, pol_lookup_t lookup);
c71099ac 340
5c3a0fd7
JP
341struct fib6_node *fib6_lookup(struct fib6_node *root,
342 const struct in6_addr *daddr,
343 const struct in6_addr *saddr);
1da177e4 344
5c3a0fd7
JP
345struct fib6_node *fib6_locate(struct fib6_node *root,
346 const struct in6_addr *daddr, int dst_len,
347 const struct in6_addr *saddr, int src_len);
1da177e4 348
5c3a0fd7 349void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
0c3584d5 350 void *arg);
c71099ac 351
e715b6d3 352int fib6_add(struct fib6_node *root, struct rt6_info *rt,
333c4301
DA
353 struct nl_info *info, struct mx6_config *mxc,
354 struct netlink_ext_ack *extack);
5c3a0fd7 355int fib6_del(struct rt6_info *rt, struct nl_info *info);
1da177e4 356
37a1d361
RP
357void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
358 unsigned int flags);
1da177e4 359
5c3a0fd7 360void fib6_run_gc(unsigned long expires, struct net *net, bool force);
1da177e4 361
5c3a0fd7 362void fib6_gc_cleanup(void);
1da177e4 363
5c3a0fd7 364int fib6_init(void);
101367c2 365
8d2ca1d7
HFS
366int ipv6_route_open(struct inode *inode, struct file *file);
367
16ab6d7d
IS
368int call_fib6_notifier(struct notifier_block *nb, struct net *net,
369 enum fib_event_type event_type,
370 struct fib_notifier_info *info);
371int call_fib6_notifiers(struct net *net, enum fib_event_type event_type,
372 struct fib_notifier_info *info);
373
374int __net_init fib6_notifier_init(struct net *net);
375void __net_exit fib6_notifier_exit(struct net *net);
376
e1ee0a5b
IS
377unsigned int fib6_tables_seq_read(struct net *net);
378int fib6_tables_dump(struct net *net, struct notifier_block *nb);
379
180ca444
WW
380void fib6_update_sernum(struct rt6_info *rt);
381
7e5449c2 382#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5c3a0fd7
JP
383int fib6_rules_init(void);
384void fib6_rules_cleanup(void);
e3ea9731 385bool fib6_rule_default(const struct fib_rule *rule);
dcb18f76
IS
386int fib6_rules_dump(struct net *net, struct notifier_block *nb);
387unsigned int fib6_rules_seq_read(struct net *net);
7e5449c2
DL
388#else
389static inline int fib6_rules_init(void)
390{
391 return 0;
392}
393static inline void fib6_rules_cleanup(void)
394{
395 return ;
396}
e3ea9731
IS
397static inline bool fib6_rule_default(const struct fib_rule *rule)
398{
399 return true;
400}
dcb18f76
IS
401static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb)
402{
403 return 0;
404}
405static inline unsigned int fib6_rules_seq_read(struct net *net)
406{
407 return 0;
408}
7e5449c2 409#endif
1da177e4 410#endif