blob: 6bf929b509515569c5d9978f9b560350f758c664 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/ipv6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/rtnetlink.h>
18#include <linux/spinlock.h>
Ido Schimmel16ab6d72017-08-03 13:28:16 +020019#include <linux/notifier.h>
Thomas Graf86872cb2006-08-22 00:01:08 -070020#include <net/dst.h>
21#include <net/flow.h>
22#include <net/netlink.h>
David S. Millerb3419362010-11-30 12:27:11 -080023#include <net/inetpeer.h>
Ido Schimmel16ab6d72017-08-03 13:28:16 +020024#include <net/fib_notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Neil Hormana33bc5c2009-07-30 18:52:15 -070026#ifdef CONFIG_IPV6_MULTIPLE_TABLES
27#define FIB6_TABLE_HASHSZ 256
28#else
29#define FIB6_TABLE_HASHSZ 1
30#endif
31
Wei Wangc757faa2017-10-06 12:06:01 -070032#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070040struct rt6_info;
41
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000042struct fib6_config {
Thomas Graf86872cb2006-08-22 00:01:08 -070043 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;
David Ahern0ae81332017-02-02 12:37:08 -080050 u16 fc_type; /* only 8 bits are used */
51 u16 fc_delete_all_nh : 1,
52 __unused : 15;
Thomas Graf86872cb2006-08-22 00:01:08 -070053
54 struct in6_addr fc_dst;
55 struct in6_addr fc_src;
Daniel Walterc3968a82011-04-13 21:10:57 +000056 struct in6_addr fc_prefsrc;
Thomas Graf86872cb2006-08-22 00:01:08 -070057 struct in6_addr fc_gateway;
58
59 unsigned long fc_expires;
60 struct nlattr *fc_mx;
61 int fc_mx_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +000062 int fc_mp_len;
63 struct nlattr *fc_mp;
Thomas Graf86872cb2006-08-22 00:01:08 -070064
65 struct nl_info fc_nlinfo;
Roopa Prabhu19e42e42015-07-21 10:43:48 +020066 struct nlattr *fc_encap;
67 u16 fc_encap_type;
Thomas Graf86872cb2006-08-22 00:01:08 -070068};
69
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000070struct fib6_node {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 struct fib6_node *parent;
72 struct fib6_node *left;
73 struct fib6_node *right;
Kim Nordlund8bce65b2006-12-13 16:38:29 -080074#ifdef CONFIG_IPV6_SUBTREES
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct fib6_node *subtree;
Kim Nordlund8bce65b2006-12-13 16:38:29 -080076#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct rt6_info *leaf;
78
79 __u16 fn_bit; /* bit key */
80 __u16 fn_flags;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020081 int fn_sernum;
David S. Millerf11e6652007-03-24 20:36:25 -070082 struct rt6_info *rr_ptr;
Wei Wangc5cff852017-08-21 09:47:10 -070083 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
Wei Wangc757faa2017-10-06 12:06:01 -070086struct fib6_gc_args {
87 int timeout;
88 int more;
89};
90
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -070091#ifndef CONFIG_IPV6_SUBTREES
92#define FIB6_SUBTREE(fn) NULL
93#else
94#define FIB6_SUBTREE(fn) ((fn)->subtree)
95#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Florian Westphale715b6d2015-01-05 23:57:44 +010097struct mx6_config {
98 const u32 *mx;
99 DECLARE_BITMAP(mx_valid, RTAX_MAX);
100};
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
103 * routing information
104 *
105 */
106
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000107struct rt6key {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 struct in6_addr addr;
109 int plen;
110};
111
Thomas Grafc71099a2006-08-04 23:20:06 -0700112struct fib6_table;
113
Wei Wang35732d02017-10-06 12:05:57 -0700114struct 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
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000130struct rt6_info {
Changli Gaod8d1f302010-06-10 23:31:35 -0700131 struct dst_entry dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
YOSHIFUJI Hideaki / 吉藤英明bd2c77a2010-03-31 22:24:22 +0000133 /*
134 * Tail elements of dst_entry (__refcnt etc.)
135 * and these elements (rarely used in hot path) are in
136 * the same cache line.
137 */
138 struct fib6_table *rt6i_table;
Wei Wang4e587ea2017-08-25 15:03:10 -0700139 struct fib6_node __rcu *rt6i_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 struct in6_addr rt6i_gateway;
YOSHIFUJI Hideaki / 吉藤英明bd2c77a2010-03-31 22:24:22 +0000142
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000143 /* Multipath routes:
144 * siblings is a list of rt6_info that have the the same metric/weight,
145 * destination, but not the same gateway. nsiblings is just a cache
146 * to speed up lookup.
147 */
148 struct list_head rt6i_siblings;
149 unsigned int rt6i_nsiblings;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 atomic_t rt6i_ref;
Herbert Xub4ce9272007-11-13 21:33:32 -0800152
Ido Schimmelfe400792017-08-15 09:09:49 +0200153 unsigned int rt6i_nh_flags;
154
YOSHIFUJI Hideaki / 吉藤英明bd2c77a2010-03-31 22:24:22 +0000155 /* These are in a separate cache line. */
156 struct rt6key rt6i_dst ____cacheline_aligned_in_smp;
157 u32 rt6i_flags;
158 struct rt6key rt6i_src;
Daniel Walterc3968a82011-04-13 21:10:57 +0000159 struct rt6key rt6i_prefsrc;
Herbert Xub4ce9272007-11-13 21:33:32 -0800160
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700161 struct list_head rt6i_uncached;
162 struct uncached_list *rt6i_uncached_list;
163
YOSHIFUJI Hideaki / 吉藤英明bd2c77a2010-03-31 22:24:22 +0000164 struct inet6_dev *rt6i_idev;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700165 struct rt6_info * __percpu *rt6i_pcpu;
Wei Wang35732d02017-10-06 12:05:57 -0700166 struct rt6_exception_bucket __rcu *rt6i_exception_bucket;
Noriaki TAKAMIYAa47ed4c2007-09-06 03:31:25 -0700167
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +0200168 u32 rt6i_metric;
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700169 u32 rt6i_pmtu;
YOSHIFUJI Hideaki / 吉藤英明bd2c77a2010-03-31 22:24:22 +0000170 /* more non-fragment space at head required */
171 unsigned short rt6i_nfheader_len;
YOSHIFUJI Hideaki / 吉藤英明bd2c77a2010-03-31 22:24:22 +0000172 u8 rt6i_protocol;
Wei Wang35732d02017-10-06 12:05:57 -0700173 u8 exception_bucket_flushed:1,
174 unused:7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
YOSHIFUJI Hideaki7a3025b2006-10-13 16:17:25 +0900177static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
178{
179 return ((struct rt6_info *)dst)->rt6i_idev;
180}
181
Gao feng1716a962012-04-06 00:13:10 +0000182static inline void rt6_clean_expires(struct rt6_info *rt)
183{
Gao feng1716a962012-04-06 00:13:10 +0000184 rt->rt6i_flags &= ~RTF_EXPIRES;
Hannes Frederic Sowa01ba16d2013-10-24 10:14:27 +0200185 rt->dst.expires = 0;
Gao feng1716a962012-04-06 00:13:10 +0000186}
187
188static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires)
189{
Gao feng1716a962012-04-06 00:13:10 +0000190 rt->dst.expires = expires;
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000191 rt->rt6i_flags |= RTF_EXPIRES;
Gao feng1716a962012-04-06 00:13:10 +0000192}
193
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000194static inline void rt6_update_expires(struct rt6_info *rt0, int timeout)
Gao feng1716a962012-04-06 00:13:10 +0000195{
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000196 struct rt6_info *rt;
Gao feng1716a962012-04-06 00:13:10 +0000197
YOSHIFUJI Hideaki / 吉藤英明ecd98832013-02-20 00:29:08 +0000198 for (rt = rt0; rt && !(rt->rt6i_flags & RTF_EXPIRES);
199 rt = (struct rt6_info *)rt->dst.from);
200 if (rt && rt != rt0)
201 rt0->dst.expires = rt->dst.expires;
202
203 dst_set_expires(&rt0->dst, timeout);
204 rt0->rt6i_flags |= RTF_EXPIRES;
Gao feng1716a962012-04-06 00:13:10 +0000205}
206
Wei Wangc5cff852017-08-21 09:47:10 -0700207/* Function to safely get fn->sernum for passed in rt
208 * and store result in passed in cookie.
209 * Return true if we can get cookie safely
210 * Return false if not
211 */
212static inline bool rt6_get_cookie_safe(const struct rt6_info *rt,
213 u32 *cookie)
214{
215 struct fib6_node *fn;
216 bool status = false;
217
218 rcu_read_lock();
219 fn = rcu_dereference(rt->rt6i_node);
220
221 if (fn) {
222 *cookie = fn->fn_sernum;
Wei Wangbbd63f02017-10-06 12:06:07 -0700223 /* pairs with smp_wmb() in fib6_update_sernum_upto_root() */
224 smp_rmb();
Wei Wangc5cff852017-08-21 09:47:10 -0700225 status = true;
226 }
227
228 rcu_read_unlock();
229 return status;
230}
231
Martin KaFai Laub197df42015-05-22 20:56:01 -0700232static inline u32 rt6_get_cookie(const struct rt6_info *rt)
233{
Wei Wangc5cff852017-08-21 09:47:10 -0700234 u32 cookie = 0;
235
Martin KaFai Lau02bcf4e2015-11-11 11:51:08 -0800236 if (rt->rt6i_flags & RTF_PCPU ||
Wei Wanga4c2fd72017-06-17 10:42:42 -0700237 (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -0700238 rt = (struct rt6_info *)(rt->dst.from);
239
Wei Wangc5cff852017-08-21 09:47:10 -0700240 rt6_get_cookie_safe(rt, &cookie);
241
242 return cookie;
Martin KaFai Laub197df42015-05-22 20:56:01 -0700243}
244
Amerigo Wang94e187c2012-10-29 00:13:19 +0000245static inline void ip6_rt_put(struct rt6_info *rt)
246{
247 /* dst_release() accepts a NULL parameter.
248 * We rely on dst being first structure in struct rt6_info
249 */
250 BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
251 dst_release(&rt->dst);
252}
253
Ido Schimmela460aa82017-08-03 13:28:25 +0200254void rt6_free_pcpu(struct rt6_info *non_pcpu_rt);
255
256static inline void rt6_hold(struct rt6_info *rt)
257{
258 atomic_inc(&rt->rt6i_ref);
259}
260
261static inline void rt6_release(struct rt6_info *rt)
262{
263 if (atomic_dec_and_test(&rt->rt6i_ref)) {
264 rt6_free_pcpu(rt);
265 dst_dev_put(&rt->dst);
266 dst_release(&rt->dst);
267 }
268}
269
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200270enum fib6_walk_state {
271#ifdef CONFIG_IPV6_SUBTREES
272 FWS_S,
273#endif
274 FWS_L,
275 FWS_R,
276 FWS_C,
277 FWS_U
278};
279
280struct fib6_walker {
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +0000281 struct list_head lh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 struct fib6_node *root, *node;
283 struct rt6_info *leaf;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200284 enum fib6_walk_state state;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000285 unsigned int skip;
286 unsigned int count;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200287 int (*func)(struct fib6_walker *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 void *args;
289};
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291struct rt6_statistics {
292 __u32 fib_nodes;
293 __u32 fib_route_nodes;
294 __u32 fib_rt_alloc; /* permanent routes */
295 __u32 fib_rt_entries; /* rt entries in table */
296 __u32 fib_rt_cache; /* cache routes */
297 __u32 fib_discarded_routes;
298};
299
300#define RTN_TL_ROOT 0x0001
301#define RTN_ROOT 0x0002 /* tree root node */
302#define RTN_RTINFO 0x0004 /* node with valid routing info */
303
304/*
305 * priority levels (or metrics)
306 *
307 */
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Thomas Grafc71099a2006-08-04 23:20:06 -0700310struct fib6_table {
311 struct hlist_node tb6_hlist;
312 u32 tb6_id;
313 rwlock_t tb6_lock;
314 struct fib6_node tb6_root;
David S. Miller8e773272012-06-11 00:01:52 -0700315 struct inet_peer_base tb6_peers;
David Ahern830218c2016-10-24 10:52:35 -0700316 unsigned int flags;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200317 unsigned int fib_seq;
David Ahern830218c2016-10-24 10:52:35 -0700318#define RT6_TABLE_HAS_DFLT_ROUTER BIT(0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700319};
320
321#define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
322#define RT6_TABLE_MAIN RT_TABLE_MAIN
Thomas Grafc71099a2006-08-04 23:20:06 -0700323#define RT6_TABLE_DFLT RT6_TABLE_MAIN
324#define RT6_TABLE_INFO RT6_TABLE_MAIN
325#define RT6_TABLE_PREFIX RT6_TABLE_MAIN
326
327#ifdef CONFIG_IPV6_MULTIPLE_TABLES
328#define FIB6_TABLE_MIN 1
329#define FIB6_TABLE_MAX RT_TABLE_MAX
Thomas Graf101367c2006-08-04 03:39:02 -0700330#define RT6_TABLE_LOCAL RT_TABLE_LOCAL
Thomas Grafc71099a2006-08-04 23:20:06 -0700331#else
332#define FIB6_TABLE_MIN RT_TABLE_MAIN
333#define FIB6_TABLE_MAX FIB6_TABLE_MIN
Thomas Graf101367c2006-08-04 03:39:02 -0700334#define RT6_TABLE_LOCAL RT6_TABLE_MAIN
Thomas Grafc71099a2006-08-04 23:20:06 -0700335#endif
336
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800337typedef struct rt6_info *(*pol_lookup_t)(struct net *,
338 struct fib6_table *,
David S. Miller4c9483b2011-03-12 16:22:43 -0500339 struct flowi6 *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200341struct fib6_entry_notifier_info {
342 struct fib_notifier_info info; /* must be first */
343 struct rt6_info *rt;
344};
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/*
347 * exported functions
348 */
349
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700350struct fib6_table *fib6_get_table(struct net *net, u32 id);
351struct fib6_table *fib6_new_table(struct net *net, u32 id);
352struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
353 int flags, pol_lookup_t lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700354
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700355struct fib6_node *fib6_lookup(struct fib6_node *root,
356 const struct in6_addr *daddr,
357 const struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700359struct fib6_node *fib6_locate(struct fib6_node *root,
360 const struct in6_addr *daddr, int dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -0700361 const struct in6_addr *saddr, int src_len,
362 bool exact_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700364void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
Li RongQing0c3584d2013-12-27 16:32:38 +0800365 void *arg);
Thomas Grafc71099a2006-08-04 23:20:06 -0700366
Florian Westphale715b6d2015-01-05 23:57:44 +0100367int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahern333c4302017-05-21 10:12:04 -0600368 struct nl_info *info, struct mx6_config *mxc,
369 struct netlink_ext_ack *extack);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700370int fib6_del(struct rt6_info *rt, struct nl_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Roopa Prabhu37a1d362015-09-13 10:18:33 -0700372void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
373 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700375void fib6_run_gc(unsigned long expires, struct net *net, bool force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700377void fib6_gc_cleanup(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700379int fib6_init(void);
Thomas Graf101367c2006-08-04 03:39:02 -0700380
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +0200381int ipv6_route_open(struct inode *inode, struct file *file);
382
Ido Schimmel16ab6d72017-08-03 13:28:16 +0200383int call_fib6_notifier(struct notifier_block *nb, struct net *net,
384 enum fib_event_type event_type,
385 struct fib_notifier_info *info);
386int call_fib6_notifiers(struct net *net, enum fib_event_type event_type,
387 struct fib_notifier_info *info);
388
389int __net_init fib6_notifier_init(struct net *net);
390void __net_exit fib6_notifier_exit(struct net *net);
391
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200392unsigned int fib6_tables_seq_read(struct net *net);
393int fib6_tables_dump(struct net *net, struct notifier_block *nb);
394
Wei Wang180ca442017-10-06 12:05:56 -0700395void fib6_update_sernum(struct rt6_info *rt);
396
Daniel Lezcano7e5449c2007-12-08 00:14:54 -0800397#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700398int fib6_rules_init(void);
399void fib6_rules_cleanup(void);
Ido Schimmele3ea9732017-08-03 13:28:15 +0200400bool fib6_rule_default(const struct fib_rule *rule);
Ido Schimmeldcb18f72017-08-03 13:28:18 +0200401int fib6_rules_dump(struct net *net, struct notifier_block *nb);
402unsigned int fib6_rules_seq_read(struct net *net);
Daniel Lezcano7e5449c2007-12-08 00:14:54 -0800403#else
404static inline int fib6_rules_init(void)
405{
406 return 0;
407}
408static inline void fib6_rules_cleanup(void)
409{
410 return ;
411}
Ido Schimmele3ea9732017-08-03 13:28:15 +0200412static inline bool fib6_rule_default(const struct fib_rule *rule)
413{
414 return true;
415}
Ido Schimmeldcb18f72017-08-03 13:28:18 +0200416static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb)
417{
418 return 0;
419}
420static inline unsigned int fib6_rules_seq_read(struct net *net)
421{
422 return 0;
423}
Daniel Lezcano7e5449c2007-12-08 00:14:54 -0800424#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425#endif