net/ipv6: Cleanup exception and cache route handling
[linux-block.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
c757faa8
WW
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
1da177e4
LT
40struct rt6_info;
41
fd2c3ef7 42struct fib6_config {
86872cb5
TG
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;
0ae81335
DA
50 u16 fc_type; /* only 8 bits are used */
51 u16 fc_delete_all_nh : 1,
52 __unused : 15;
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
66f5d6ce 77 struct rt6_info __rcu *leaf;
1da177e4
LT
78
79 __u16 fn_bit; /* bit key */
80 __u16 fn_flags;
42b18706 81 int fn_sernum;
66f5d6ce 82 struct rt6_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
DA
125struct fib6_nh {
126 struct in6_addr nh_gw;
127 struct net_device *nh_dev;
128 struct lwtunnel_state *nh_lwtstate;
129
130 unsigned int nh_flags;
131 atomic_t nh_upper_bound;
132 int nh_weight;
133};
134
fd2c3ef7 135struct rt6_info {
d8d1f30b 136 struct dst_entry dst;
071fb37e 137 struct rt6_info __rcu *rt6_next;
3a2232e9 138 struct rt6_info *from;
1da177e4 139
bd2c77a0
YH
140 /*
141 * Tail elements of dst_entry (__refcnt etc.)
142 * and these elements (rarely used in hot path) are in
143 * the same cache line.
144 */
145 struct fib6_table *rt6i_table;
4e587ea7 146 struct fib6_node __rcu *rt6i_node;
1da177e4
LT
147
148 struct in6_addr rt6i_gateway;
1da177e4 149
51ebd318
ND
150 /* Multipath routes:
151 * siblings is a list of rt6_info that have the the same metric/weight,
152 * destination, but not the same gateway. nsiblings is just a cache
153 * to speed up lookup.
154 */
155 struct list_head rt6i_siblings;
156 unsigned int rt6i_nsiblings;
157
bd2c77a0 158 atomic_t rt6i_ref;
a47ed4cd 159
bd2c77a0
YH
160 /* These are in a separate cache line. */
161 struct rt6key rt6i_dst ____cacheline_aligned_in_smp;
162 u32 rt6i_flags;
163 struct rt6key rt6i_src;
c3968a85 164 struct rt6key rt6i_prefsrc;
b4ce9277 165
8d0b94af
MKL
166 struct list_head rt6i_uncached;
167 struct uncached_list *rt6i_uncached_list;
168
bd2c77a0 169 struct inet6_dev *rt6i_idev;
d52d3997 170 struct rt6_info * __percpu *rt6i_pcpu;
35732d01 171 struct rt6_exception_bucket __rcu *rt6i_exception_bucket;
b4ce9277 172
705f1c86 173 u32 rt6i_metric;
bd2c77a0
YH
174 /* more non-fragment space at head required */
175 unsigned short rt6i_nfheader_len;
bd2c77a0 176 u8 rt6i_protocol;
e8478e80 177 u8 fib6_type;
35732d01 178 u8 exception_bucket_flushed:1,
a2c554d3 179 should_flush:1,
3b6761d1
DA
180 dst_nocount:1,
181 dst_nopolicy:1,
182 dst_host:1,
183 unused:3;
5e670d84 184
14895687 185 unsigned long expires;
d4ead6b3
DA
186 struct dst_metrics *fib6_metrics;
187#define fib6_pmtu fib6_metrics->metrics[RTAX_MTU-1]
5e670d84 188 struct fib6_nh fib6_nh;
1da177e4
LT
189};
190
66f5d6ce
WW
191#define for_each_fib6_node_rt_rcu(fn) \
192 for (rt = rcu_dereference((fn)->leaf); rt; \
071fb37e 193 rt = rcu_dereference(rt->rt6_next))
66f5d6ce
WW
194
195#define for_each_fib6_walker_rt(w) \
196 for (rt = (w)->leaf; rt; \
071fb37e 197 rt = rcu_dereference_protected(rt->rt6_next, 1))
66f5d6ce 198
7a3025b1
YH
199static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
200{
201 return ((struct rt6_info *)dst)->rt6i_idev;
202}
203
14895687
DA
204static inline void fib6_clean_expires(struct rt6_info *f6i)
205{
206 f6i->rt6i_flags &= ~RTF_EXPIRES;
207 f6i->expires = 0;
208}
209
210static inline void fib6_set_expires(struct rt6_info *f6i,
211 unsigned long expires)
212{
213 f6i->expires = expires;
214 f6i->rt6i_flags |= RTF_EXPIRES;
215}
216
217static inline bool fib6_check_expired(const struct rt6_info *f6i)
218{
219 if (f6i->rt6i_flags & RTF_EXPIRES)
220 return time_after(jiffies, f6i->expires);
221 return false;
222}
223
1716a961
G
224static inline void rt6_clean_expires(struct rt6_info *rt)
225{
1716a961 226 rt->rt6i_flags &= ~RTF_EXPIRES;
01ba16d6 227 rt->dst.expires = 0;
1716a961
G
228}
229
230static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires)
231{
1716a961 232 rt->dst.expires = expires;
ecd98837 233 rt->rt6i_flags |= RTF_EXPIRES;
1716a961
G
234}
235
ecd98837 236static inline void rt6_update_expires(struct rt6_info *rt0, int timeout)
1716a961 237{
14895687
DA
238 if (!(rt0->rt6i_flags & RTF_EXPIRES) && rt0->from)
239 rt0->dst.expires = rt0->from->expires;
ecd98837 240
ecd98837
YH
241 dst_set_expires(&rt0->dst, timeout);
242 rt0->rt6i_flags |= RTF_EXPIRES;
1716a961 243}
b197df4f 244
c5cff856
WW
245/* Function to safely get fn->sernum for passed in rt
246 * and store result in passed in cookie.
247 * Return true if we can get cookie safely
248 * Return false if not
249 */
250static inline bool rt6_get_cookie_safe(const struct rt6_info *rt,
251 u32 *cookie)
252{
253 struct fib6_node *fn;
254 bool status = false;
255
256 rcu_read_lock();
257 fn = rcu_dereference(rt->rt6i_node);
258
259 if (fn) {
260 *cookie = fn->fn_sernum;
bbd63f06
WW
261 /* pairs with smp_wmb() in fib6_update_sernum_upto_root() */
262 smp_rmb();
c5cff856
WW
263 status = true;
264 }
265
266 rcu_read_unlock();
267 return status;
268}
269
b197df4f
MKL
270static inline u32 rt6_get_cookie(const struct rt6_info *rt)
271{
c5cff856
WW
272 u32 cookie = 0;
273
02bcf4e0 274 if (rt->rt6i_flags & RTF_PCPU ||
3a2232e9
DM
275 (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->from))
276 rt = rt->from;
3da59bd9 277
c5cff856
WW
278 rt6_get_cookie_safe(rt, &cookie);
279
280 return cookie;
b197df4f 281}
1716a961 282
94e187c0
AW
283static inline void ip6_rt_put(struct rt6_info *rt)
284{
285 /* dst_release() accepts a NULL parameter.
286 * We rely on dst being first structure in struct rt6_info
287 */
288 BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
289 dst_release(&rt->dst);
290}
291
a460aa83
IS
292void rt6_free_pcpu(struct rt6_info *non_pcpu_rt);
293
294static inline void rt6_hold(struct rt6_info *rt)
295{
296 atomic_inc(&rt->rt6i_ref);
297}
298
299static inline void rt6_release(struct rt6_info *rt)
300{
301 if (atomic_dec_and_test(&rt->rt6i_ref)) {
302 rt6_free_pcpu(rt);
303 dst_dev_put(&rt->dst);
304 dst_release(&rt->dst);
305 }
306}
307
94b2cfe0
HFS
308enum fib6_walk_state {
309#ifdef CONFIG_IPV6_SUBTREES
310 FWS_S,
311#endif
312 FWS_L,
313 FWS_R,
314 FWS_C,
315 FWS_U
316};
317
318struct fib6_walker {
bbef49da 319 struct list_head lh;
1da177e4
LT
320 struct fib6_node *root, *node;
321 struct rt6_info *leaf;
94b2cfe0 322 enum fib6_walk_state state;
2bec5a36
PM
323 unsigned int skip;
324 unsigned int count;
94b2cfe0 325 int (*func)(struct fib6_walker *);
1da177e4
LT
326 void *args;
327};
328
1da177e4 329struct rt6_statistics {
81eb8447
WW
330 __u32 fib_nodes; /* all fib6 nodes */
331 __u32 fib_route_nodes; /* intermediate nodes */
332 __u32 fib_rt_entries; /* rt entries in fib table */
333 __u32 fib_rt_cache; /* cached rt entries in exception table */
334 __u32 fib_discarded_routes; /* total number of routes delete */
335
336 /* The following stats are not protected by any lock */
337 atomic_t fib_rt_alloc; /* total number of routes alloced */
338 atomic_t fib_rt_uncache; /* rt entries in uncached list */
1da177e4
LT
339};
340
341#define RTN_TL_ROOT 0x0001
342#define RTN_ROOT 0x0002 /* tree root node */
343#define RTN_RTINFO 0x0004 /* node with valid routing info */
344
345/*
346 * priority levels (or metrics)
347 *
348 */
349
1da177e4 350
c71099ac
TG
351struct fib6_table {
352 struct hlist_node tb6_hlist;
353 u32 tb6_id;
66f5d6ce 354 spinlock_t tb6_lock;
c71099ac 355 struct fib6_node tb6_root;
8e773277 356 struct inet_peer_base tb6_peers;
830218c1 357 unsigned int flags;
e1ee0a5b 358 unsigned int fib_seq;
830218c1 359#define RT6_TABLE_HAS_DFLT_ROUTER BIT(0)
c71099ac
TG
360};
361
362#define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
363#define RT6_TABLE_MAIN RT_TABLE_MAIN
c71099ac
TG
364#define RT6_TABLE_DFLT RT6_TABLE_MAIN
365#define RT6_TABLE_INFO RT6_TABLE_MAIN
366#define RT6_TABLE_PREFIX RT6_TABLE_MAIN
367
368#ifdef CONFIG_IPV6_MULTIPLE_TABLES
369#define FIB6_TABLE_MIN 1
370#define FIB6_TABLE_MAX RT_TABLE_MAX
101367c2 371#define RT6_TABLE_LOCAL RT_TABLE_LOCAL
c71099ac
TG
372#else
373#define FIB6_TABLE_MIN RT_TABLE_MAIN
374#define FIB6_TABLE_MAX FIB6_TABLE_MIN
101367c2 375#define RT6_TABLE_LOCAL RT6_TABLE_MAIN
c71099ac
TG
376#endif
377
8ed67789
DL
378typedef struct rt6_info *(*pol_lookup_t)(struct net *,
379 struct fib6_table *,
b75cc8f9
DA
380 struct flowi6 *,
381 const struct sk_buff *, int);
1da177e4 382
df77fe4d
IS
383struct fib6_entry_notifier_info {
384 struct fib_notifier_info info; /* must be first */
385 struct rt6_info *rt;
386};
387
1da177e4
LT
388/*
389 * exported functions
390 */
391
5c3a0fd7
JP
392struct fib6_table *fib6_get_table(struct net *net, u32 id);
393struct fib6_table *fib6_new_table(struct net *net, u32 id);
394struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
b75cc8f9 395 const struct sk_buff *skb,
5c3a0fd7 396 int flags, pol_lookup_t lookup);
c71099ac 397
5c3a0fd7
JP
398struct fib6_node *fib6_lookup(struct fib6_node *root,
399 const struct in6_addr *daddr,
400 const struct in6_addr *saddr);
1da177e4 401
5c3a0fd7
JP
402struct fib6_node *fib6_locate(struct fib6_node *root,
403 const struct in6_addr *daddr, int dst_len,
38fbeeee
WW
404 const struct in6_addr *saddr, int src_len,
405 bool exact_match);
1da177e4 406
5c3a0fd7 407void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
0c3584d5 408 void *arg);
c71099ac 409
e715b6d3 410int fib6_add(struct fib6_node *root, struct rt6_info *rt,
d4ead6b3 411 struct nl_info *info, struct netlink_ext_ack *extack);
5c3a0fd7 412int fib6_del(struct rt6_info *rt, struct nl_info *info);
1da177e4 413
37a1d361
RP
414void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
415 unsigned int flags);
1da177e4 416
5c3a0fd7 417void fib6_run_gc(unsigned long expires, struct net *net, bool force);
1da177e4 418
5c3a0fd7 419void fib6_gc_cleanup(void);
1da177e4 420
5c3a0fd7 421int fib6_init(void);
101367c2 422
8d2ca1d7
HFS
423int ipv6_route_open(struct inode *inode, struct file *file);
424
16ab6d7d
IS
425int call_fib6_notifier(struct notifier_block *nb, struct net *net,
426 enum fib_event_type event_type,
427 struct fib_notifier_info *info);
428int call_fib6_notifiers(struct net *net, enum fib_event_type event_type,
429 struct fib_notifier_info *info);
430
431int __net_init fib6_notifier_init(struct net *net);
432void __net_exit fib6_notifier_exit(struct net *net);
433
e1ee0a5b
IS
434unsigned int fib6_tables_seq_read(struct net *net);
435int fib6_tables_dump(struct net *net, struct notifier_block *nb);
436
7aef6859 437void fib6_update_sernum(struct net *net, struct rt6_info *rt);
4a8e56ee 438void fib6_update_sernum_upto_root(struct net *net, struct rt6_info *rt);
180ca444 439
d4ead6b3
DA
440void fib6_metric_set(struct rt6_info *f6i, int metric, u32 val);
441static inline bool fib6_metric_locked(struct rt6_info *f6i, int metric)
442{
443 return !!(f6i->fib6_metrics->metrics[RTAX_LOCK - 1] & (1 << metric));
444}
445
7e5449c2 446#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5c3a0fd7
JP
447int fib6_rules_init(void);
448void fib6_rules_cleanup(void);
e3ea9731 449bool fib6_rule_default(const struct fib_rule *rule);
dcb18f76
IS
450int fib6_rules_dump(struct net *net, struct notifier_block *nb);
451unsigned int fib6_rules_seq_read(struct net *net);
5e5d6fed
RP
452
453static inline bool fib6_rules_early_flow_dissect(struct net *net,
454 struct sk_buff *skb,
455 struct flowi6 *fl6,
456 struct flow_keys *flkeys)
457{
458 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
459
460 if (!net->ipv6.fib6_rules_require_fldissect)
461 return false;
462
463 skb_flow_dissect_flow_keys(skb, flkeys, flag);
464 fl6->fl6_sport = flkeys->ports.src;
465 fl6->fl6_dport = flkeys->ports.dst;
466 fl6->flowi6_proto = flkeys->basic.ip_proto;
467
468 return true;
469}
7e5449c2
DL
470#else
471static inline int fib6_rules_init(void)
472{
473 return 0;
474}
475static inline void fib6_rules_cleanup(void)
476{
477 return ;
478}
e3ea9731
IS
479static inline bool fib6_rule_default(const struct fib_rule *rule)
480{
481 return true;
482}
dcb18f76
IS
483static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb)
484{
485 return 0;
486}
487static inline unsigned int fib6_rules_seq_read(struct net *net)
488{
489 return 0;
490}
5e5d6fed
RP
491static inline bool fib6_rules_early_flow_dissect(struct net *net,
492 struct sk_buff *skb,
493 struct flowi6 *fl6,
494 struct flow_keys *flkeys)
495{
496 return false;
497}
7e5449c2 498#endif
1da177e4 499#endif