• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <linux/ipv6_route.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/spinlock.h>
19 #include <linux/notifier.h>
20 #include <net/dst.h>
21 #include <net/flow.h>
22 #include <net/netlink.h>
23 #include <net/inetpeer.h>
24 #include <net/fib_notifier.h>
25 
26 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
27 #define FIB6_TABLE_HASHSZ 256
28 #else
29 #define FIB6_TABLE_HASHSZ 1
30 #endif
31 
32 struct rt6_info;
33 
34 struct fib6_config {
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;
42 	u16		fc_type;        /* only 8 bits are used */
43 	u16		fc_delete_all_nh : 1,
44 			__unused : 15;
45 
46 	struct in6_addr	fc_dst;
47 	struct in6_addr	fc_src;
48 	struct in6_addr	fc_prefsrc;
49 	struct in6_addr	fc_gateway;
50 
51 	unsigned long	fc_expires;
52 	struct nlattr	*fc_mx;
53 	int		fc_mx_len;
54 	int		fc_mp_len;
55 	struct nlattr	*fc_mp;
56 
57 	struct nl_info	fc_nlinfo;
58 	struct nlattr	*fc_encap;
59 	u16		fc_encap_type;
60 };
61 
62 struct fib6_node {
63 	struct fib6_node	*parent;
64 	struct fib6_node	*left;
65 	struct fib6_node	*right;
66 #ifdef CONFIG_IPV6_SUBTREES
67 	struct fib6_node	*subtree;
68 #endif
69 	struct rt6_info		*leaf;
70 
71 	__u16			fn_bit;		/* bit key */
72 	__u16			fn_flags;
73 	int			fn_sernum;
74 	struct rt6_info		*rr_ptr;
75 	struct rcu_head		rcu;
76 };
77 
78 #ifndef CONFIG_IPV6_SUBTREES
79 #define FIB6_SUBTREE(fn)	NULL
80 #else
81 #define FIB6_SUBTREE(fn)	((fn)->subtree)
82 #endif
83 
84 struct mx6_config {
85 	const u32 *mx;
86 	DECLARE_BITMAP(mx_valid, RTAX_MAX);
87 };
88 
89 /*
90  *	routing information
91  *
92  */
93 
94 struct rt6key {
95 	struct in6_addr	addr;
96 	int		plen;
97 };
98 
99 struct fib6_table;
100 
101 struct rt6_info {
102 	struct dst_entry		dst;
103 
104 	/*
105 	 * Tail elements of dst_entry (__refcnt etc.)
106 	 * and these elements (rarely used in hot path) are in
107 	 * the same cache line.
108 	 */
109 	struct fib6_table		*rt6i_table;
110 	struct fib6_node __rcu		*rt6i_node;
111 
112 	struct in6_addr			rt6i_gateway;
113 
114 	/* Multipath routes:
115 	 * siblings is a list of rt6_info that have the the same metric/weight,
116 	 * destination, but not the same gateway. nsiblings is just a cache
117 	 * to speed up lookup.
118 	 */
119 	struct list_head		rt6i_siblings;
120 	unsigned int			rt6i_nsiblings;
121 
122 	atomic_t			rt6i_ref;
123 
124 	unsigned int			rt6i_nh_flags;
125 
126 	/* These are in a separate cache line. */
127 	struct rt6key			rt6i_dst ____cacheline_aligned_in_smp;
128 	u32				rt6i_flags;
129 	struct rt6key			rt6i_src;
130 	struct rt6key			rt6i_prefsrc;
131 
132 	struct list_head		rt6i_uncached;
133 	struct uncached_list		*rt6i_uncached_list;
134 
135 	struct inet6_dev		*rt6i_idev;
136 	struct rt6_info * __percpu	*rt6i_pcpu;
137 
138 	u32				rt6i_metric;
139 	u32				rt6i_pmtu;
140 	/* more non-fragment space at head required */
141 	unsigned short			rt6i_nfheader_len;
142 	u8				rt6i_protocol;
143 };
144 
ip6_dst_idev(struct dst_entry * dst)145 static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
146 {
147 	return ((struct rt6_info *)dst)->rt6i_idev;
148 }
149 
rt6_clean_expires(struct rt6_info * rt)150 static inline void rt6_clean_expires(struct rt6_info *rt)
151 {
152 	rt->rt6i_flags &= ~RTF_EXPIRES;
153 	rt->dst.expires = 0;
154 }
155 
rt6_set_expires(struct rt6_info * rt,unsigned long expires)156 static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires)
157 {
158 	rt->dst.expires = expires;
159 	rt->rt6i_flags |= RTF_EXPIRES;
160 }
161 
rt6_update_expires(struct rt6_info * rt0,int timeout)162 static inline void rt6_update_expires(struct rt6_info *rt0, int timeout)
163 {
164 	struct rt6_info *rt;
165 
166 	for (rt = rt0; rt && !(rt->rt6i_flags & RTF_EXPIRES);
167 	     rt = (struct rt6_info *)rt->dst.from);
168 	if (rt && rt != rt0)
169 		rt0->dst.expires = rt->dst.expires;
170 
171 	dst_set_expires(&rt0->dst, timeout);
172 	rt0->rt6i_flags |= RTF_EXPIRES;
173 }
174 
175 /* Function to safely get fn->sernum for passed in rt
176  * and store result in passed in cookie.
177  * Return true if we can get cookie safely
178  * Return false if not
179  */
rt6_get_cookie_safe(const struct rt6_info * rt,u32 * cookie)180 static inline bool rt6_get_cookie_safe(const struct rt6_info *rt,
181 				       u32 *cookie)
182 {
183 	struct fib6_node *fn;
184 	bool status = false;
185 
186 	rcu_read_lock();
187 	fn = rcu_dereference(rt->rt6i_node);
188 
189 	if (fn) {
190 		*cookie = fn->fn_sernum;
191 		status = true;
192 	}
193 
194 	rcu_read_unlock();
195 	return status;
196 }
197 
rt6_get_cookie(const struct rt6_info * rt)198 static inline u32 rt6_get_cookie(const struct rt6_info *rt)
199 {
200 	u32 cookie = 0;
201 
202 	if (rt->dst.from)
203 		rt = (struct rt6_info *)(rt->dst.from);
204 
205 	rt6_get_cookie_safe(rt, &cookie);
206 
207 	return cookie;
208 }
209 
ip6_rt_put(struct rt6_info * rt)210 static inline void ip6_rt_put(struct rt6_info *rt)
211 {
212 	/* dst_release() accepts a NULL parameter.
213 	 * We rely on dst being first structure in struct rt6_info
214 	 */
215 	BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
216 	dst_release(&rt->dst);
217 }
218 
219 void rt6_free_pcpu(struct rt6_info *non_pcpu_rt);
220 
rt6_hold(struct rt6_info * rt)221 static inline void rt6_hold(struct rt6_info *rt)
222 {
223 	atomic_inc(&rt->rt6i_ref);
224 }
225 
rt6_release(struct rt6_info * rt)226 static inline void rt6_release(struct rt6_info *rt)
227 {
228 	if (atomic_dec_and_test(&rt->rt6i_ref)) {
229 		rt6_free_pcpu(rt);
230 		dst_dev_put(&rt->dst);
231 		dst_release(&rt->dst);
232 	}
233 }
234 
235 enum fib6_walk_state {
236 #ifdef CONFIG_IPV6_SUBTREES
237 	FWS_S,
238 #endif
239 	FWS_L,
240 	FWS_R,
241 	FWS_C,
242 	FWS_U
243 };
244 
245 struct fib6_walker {
246 	struct list_head lh;
247 	struct fib6_node *root, *node;
248 	struct rt6_info *leaf;
249 	enum fib6_walk_state state;
250 	bool prune;
251 	unsigned int skip;
252 	unsigned int count;
253 	int (*func)(struct fib6_walker *);
254 	void *args;
255 };
256 
257 struct rt6_statistics {
258 	__u32		fib_nodes;
259 	__u32		fib_route_nodes;
260 	__u32		fib_rt_alloc;		/* permanent routes	*/
261 	__u32		fib_rt_entries;		/* rt entries in table	*/
262 	__u32		fib_rt_cache;		/* cache routes		*/
263 	__u32		fib_discarded_routes;
264 };
265 
266 #define RTN_TL_ROOT	0x0001
267 #define RTN_ROOT	0x0002		/* tree root node		*/
268 #define RTN_RTINFO	0x0004		/* node with valid routing info	*/
269 
270 /*
271  *	priority levels (or metrics)
272  *
273  */
274 
275 
276 struct fib6_table {
277 	struct hlist_node	tb6_hlist;
278 	u32			tb6_id;
279 	rwlock_t		tb6_lock;
280 	struct fib6_node	tb6_root;
281 	struct inet_peer_base	tb6_peers;
282 	unsigned int		flags;
283 	unsigned int		fib_seq;
284 #define RT6_TABLE_HAS_DFLT_ROUTER	BIT(0)
285 };
286 
287 #define RT6_TABLE_UNSPEC	RT_TABLE_UNSPEC
288 #define RT6_TABLE_MAIN		RT_TABLE_MAIN
289 #define RT6_TABLE_DFLT		RT6_TABLE_MAIN
290 #define RT6_TABLE_INFO		RT6_TABLE_MAIN
291 #define RT6_TABLE_PREFIX	RT6_TABLE_MAIN
292 
293 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
294 #define FIB6_TABLE_MIN		1
295 #define FIB6_TABLE_MAX		RT_TABLE_MAX
296 #define RT6_TABLE_LOCAL		RT_TABLE_LOCAL
297 #else
298 #define FIB6_TABLE_MIN		RT_TABLE_MAIN
299 #define FIB6_TABLE_MAX		FIB6_TABLE_MIN
300 #define RT6_TABLE_LOCAL		RT6_TABLE_MAIN
301 #endif
302 
303 typedef struct rt6_info *(*pol_lookup_t)(struct net *,
304 					 struct fib6_table *,
305 					 struct flowi6 *, int);
306 
307 struct fib6_entry_notifier_info {
308 	struct fib_notifier_info info; /* must be first */
309 	struct rt6_info *rt;
310 };
311 
312 /*
313  *	exported functions
314  */
315 
316 struct fib6_table *fib6_get_table(struct net *net, u32 id);
317 struct fib6_table *fib6_new_table(struct net *net, u32 id);
318 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
319 				   int flags, pol_lookup_t lookup);
320 
321 struct fib6_node *fib6_lookup(struct fib6_node *root,
322 			      const struct in6_addr *daddr,
323 			      const struct in6_addr *saddr);
324 
325 struct fib6_node *fib6_locate(struct fib6_node *root,
326 			      const struct in6_addr *daddr, int dst_len,
327 			      const struct in6_addr *saddr, int src_len);
328 
329 void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
330 		    void *arg);
331 
332 int fib6_add(struct fib6_node *root, struct rt6_info *rt,
333 	     struct nl_info *info, struct mx6_config *mxc,
334 	     struct netlink_ext_ack *extack);
335 int fib6_del(struct rt6_info *rt, struct nl_info *info);
336 
337 void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
338 		     unsigned int flags);
339 
340 void fib6_run_gc(unsigned long expires, struct net *net, bool force);
341 
342 void fib6_gc_cleanup(void);
343 
344 int fib6_init(void);
345 
346 int ipv6_route_open(struct inode *inode, struct file *file);
347 
348 int call_fib6_notifier(struct notifier_block *nb, struct net *net,
349 		       enum fib_event_type event_type,
350 		       struct fib_notifier_info *info);
351 int call_fib6_notifiers(struct net *net, enum fib_event_type event_type,
352 			struct fib_notifier_info *info);
353 
354 int __net_init fib6_notifier_init(struct net *net);
355 void __net_exit fib6_notifier_exit(struct net *net);
356 
357 unsigned int fib6_tables_seq_read(struct net *net);
358 int fib6_tables_dump(struct net *net, struct notifier_block *nb);
359 
360 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
361 int fib6_rules_init(void);
362 void fib6_rules_cleanup(void);
363 bool fib6_rule_default(const struct fib_rule *rule);
364 int fib6_rules_dump(struct net *net, struct notifier_block *nb);
365 unsigned int fib6_rules_seq_read(struct net *net);
366 #else
fib6_rules_init(void)367 static inline int               fib6_rules_init(void)
368 {
369 	return 0;
370 }
fib6_rules_cleanup(void)371 static inline void              fib6_rules_cleanup(void)
372 {
373 	return ;
374 }
fib6_rule_default(const struct fib_rule * rule)375 static inline bool fib6_rule_default(const struct fib_rule *rule)
376 {
377 	return true;
378 }
fib6_rules_dump(struct net * net,struct notifier_block * nb)379 static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb)
380 {
381 	return 0;
382 }
fib6_rules_seq_read(struct net * net)383 static inline unsigned int fib6_rules_seq_read(struct net *net)
384 {
385 	return 0;
386 }
387 #endif
388 #endif
389