blob: 4497a1eb4d4158bd2f4b94fc6ace7fb88a8de52b [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;
223 status = true;
224 }
225
226 rcu_read_unlock();
227 return status;
228}
229
Martin KaFai Laub197df42015-05-22 20:56:01 -0700230static inline u32 rt6_get_cookie(const struct rt6_info *rt)
231{
Wei Wangc5cff852017-08-21 09:47:10 -0700232 u32 cookie = 0;
233
Martin KaFai Lau02bcf4e2015-11-11 11:51:08 -0800234 if (rt->rt6i_flags & RTF_PCPU ||
Wei Wanga4c2fd72017-06-17 10:42:42 -0700235 (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -0700236 rt = (struct rt6_info *)(rt->dst.from);
237
Wei Wangc5cff852017-08-21 09:47:10 -0700238 rt6_get_cookie_safe(rt, &cookie);
239
240 return cookie;
Martin KaFai Laub197df42015-05-22 20:56:01 -0700241}
242
Amerigo Wang94e187c2012-10-29 00:13:19 +0000243static inline void ip6_rt_put(struct rt6_info *rt)
244{
245 /* dst_release() accepts a NULL parameter.
246 * We rely on dst being first structure in struct rt6_info
247 */
248 BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
249 dst_release(&rt->dst);
250}
251
Ido Schimmela460aa82017-08-03 13:28:25 +0200252void rt6_free_pcpu(struct rt6_info *non_pcpu_rt);
253
254static inline void rt6_hold(struct rt6_info *rt)
255{
256 atomic_inc(&rt->rt6i_ref);
257}
258
259static inline void rt6_release(struct rt6_info *rt)
260{
261 if (atomic_dec_and_test(&rt->rt6i_ref)) {
262 rt6_free_pcpu(rt);
263 dst_dev_put(&rt->dst);
264 dst_release(&rt->dst);
265 }
266}
267
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200268enum fib6_walk_state {
269#ifdef CONFIG_IPV6_SUBTREES
270 FWS_S,
271#endif
272 FWS_L,
273 FWS_R,
274 FWS_C,
275 FWS_U
276};
277
278struct fib6_walker {
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +0000279 struct list_head lh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct fib6_node *root, *node;
281 struct rt6_info *leaf;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200282 enum fib6_walk_state state;
283 bool prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000284 unsigned int skip;
285 unsigned int count;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200286 int (*func)(struct fib6_walker *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 void *args;
288};
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290struct rt6_statistics {
291 __u32 fib_nodes;
292 __u32 fib_route_nodes;
293 __u32 fib_rt_alloc; /* permanent routes */
294 __u32 fib_rt_entries; /* rt entries in table */
295 __u32 fib_rt_cache; /* cache routes */
296 __u32 fib_discarded_routes;
297};
298
299#define RTN_TL_ROOT 0x0001
300#define RTN_ROOT 0x0002 /* tree root node */
301#define RTN_RTINFO 0x0004 /* node with valid routing info */
302
303/*
304 * priority levels (or metrics)
305 *
306 */
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Thomas Grafc71099a2006-08-04 23:20:06 -0700309struct fib6_table {
310 struct hlist_node tb6_hlist;
311 u32 tb6_id;
312 rwlock_t tb6_lock;
313 struct fib6_node tb6_root;
David S. Miller8e773272012-06-11 00:01:52 -0700314 struct inet_peer_base tb6_peers;
David Ahern830218c2016-10-24 10:52:35 -0700315 unsigned int flags;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200316 unsigned int fib_seq;
David Ahern830218c2016-10-24 10:52:35 -0700317#define RT6_TABLE_HAS_DFLT_ROUTER BIT(0)
Thomas Grafc71099a2006-08-04 23:20:06 -0700318};
319
320#define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
321#define RT6_TABLE_MAIN RT_TABLE_MAIN
Thomas Grafc71099a2006-08-04 23:20:06 -0700322#define RT6_TABLE_DFLT RT6_TABLE_MAIN
323#define RT6_TABLE_INFO RT6_TABLE_MAIN
324#define RT6_TABLE_PREFIX RT6_TABLE_MAIN
325
326#ifdef CONFIG_IPV6_MULTIPLE_TABLES
327#define FIB6_TABLE_MIN 1
328#define FIB6_TABLE_MAX RT_TABLE_MAX
Thomas Graf101367c2006-08-04 03:39:02 -0700329#define RT6_TABLE_LOCAL RT_TABLE_LOCAL
Thomas Grafc71099a2006-08-04 23:20:06 -0700330#else
331#define FIB6_TABLE_MIN RT_TABLE_MAIN
332#define FIB6_TABLE_MAX FIB6_TABLE_MIN
Thomas Graf101367c2006-08-04 03:39:02 -0700333#define RT6_TABLE_LOCAL RT6_TABLE_MAIN
Thomas Grafc71099a2006-08-04 23:20:06 -0700334#endif
335
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800336typedef struct rt6_info *(*pol_lookup_t)(struct net *,
337 struct fib6_table *,
David S. Miller4c9483b2011-03-12 16:22:43 -0500338 struct flowi6 *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200340struct fib6_entry_notifier_info {
341 struct fib_notifier_info info; /* must be first */
342 struct rt6_info *rt;
343};
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345/*
346 * exported functions
347 */
348
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700349struct fib6_table *fib6_get_table(struct net *net, u32 id);
350struct fib6_table *fib6_new_table(struct net *net, u32 id);
351struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
352 int flags, pol_lookup_t lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -0700353
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700354struct fib6_node *fib6_lookup(struct fib6_node *root,
355 const struct in6_addr *daddr,
356 const struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700358struct fib6_node *fib6_locate(struct fib6_node *root,
359 const struct in6_addr *daddr, int dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -0700360 const struct in6_addr *saddr, int src_len,
361 bool exact_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700363void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
Li RongQing0c3584d2013-12-27 16:32:38 +0800364 void *arg);
Thomas Grafc71099a2006-08-04 23:20:06 -0700365
Florian Westphale715b6d2015-01-05 23:57:44 +0100366int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahern333c4302017-05-21 10:12:04 -0600367 struct nl_info *info, struct mx6_config *mxc,
368 struct netlink_ext_ack *extack);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700369int fib6_del(struct rt6_info *rt, struct nl_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Roopa Prabhu37a1d362015-09-13 10:18:33 -0700371void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
372 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700374void fib6_run_gc(unsigned long expires, struct net *net, bool force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700376void fib6_gc_cleanup(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700378int fib6_init(void);
Thomas Graf101367c2006-08-04 03:39:02 -0700379
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +0200380int ipv6_route_open(struct inode *inode, struct file *file);
381
Ido Schimmel16ab6d72017-08-03 13:28:16 +0200382int call_fib6_notifier(struct notifier_block *nb, struct net *net,
383 enum fib_event_type event_type,
384 struct fib_notifier_info *info);
385int call_fib6_notifiers(struct net *net, enum fib_event_type event_type,
386 struct fib_notifier_info *info);
387
388int __net_init fib6_notifier_init(struct net *net);
389void __net_exit fib6_notifier_exit(struct net *net);
390
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200391unsigned int fib6_tables_seq_read(struct net *net);
392int fib6_tables_dump(struct net *net, struct notifier_block *nb);
393
Wei Wang180ca442017-10-06 12:05:56 -0700394void fib6_update_sernum(struct rt6_info *rt);
395
Daniel Lezcano7e5449c2007-12-08 00:14:54 -0800396#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700397int fib6_rules_init(void);
398void fib6_rules_cleanup(void);
Ido Schimmele3ea9732017-08-03 13:28:15 +0200399bool fib6_rule_default(const struct fib_rule *rule);
Ido Schimmeldcb18f72017-08-03 13:28:18 +0200400int fib6_rules_dump(struct net *net, struct notifier_block *nb);
401unsigned int fib6_rules_seq_read(struct net *net);
Daniel Lezcano7e5449c2007-12-08 00:14:54 -0800402#else
403static inline int fib6_rules_init(void)
404{
405 return 0;
406}
407static inline void fib6_rules_cleanup(void)
408{
409 return ;
410}
Ido Schimmele3ea9732017-08-03 13:28:15 +0200411static inline bool fib6_rule_default(const struct fib_rule *rule)
412{
413 return true;
414}
Ido Schimmeldcb18f72017-08-03 13:28:18 +0200415static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb)
416{
417 return 0;
418}
419static inline unsigned int fib6_rules_seq_read(struct net *net)
420{
421 return 0;
422}
Daniel Lezcano7e5449c2007-12-08 00:14:54 -0800423#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424#endif