]>
Commit | Line | Data |
---|---|---|
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 |
32 | struct rt6_info; |
33 | ||
fd2c3ef7 | 34 | struct 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 | 62 | struct 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 |
84 | struct 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 | 94 | struct rt6key { |
1da177e4 LT |
95 | struct in6_addr addr; |
96 | int plen; | |
97 | }; | |
98 | ||
c71099ac TG |
99 | struct fib6_table; |
100 | ||
fd2c3ef7 | 101 | struct rt6_info { |
d8d1f30b | 102 | struct dst_entry dst; |
1da177e4 | 103 | |
bd2c77a0 YH |
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; | |
4e587ea7 | 110 | struct fib6_node __rcu *rt6i_node; |
1da177e4 LT |
111 | |
112 | struct in6_addr rt6i_gateway; | |
1da177e4 | 113 | |
51ebd318 ND |
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 | ||
bd2c77a0 | 122 | atomic_t rt6i_ref; |
a47ed4cd | 123 | |
fe400799 IS |
124 | unsigned int rt6i_nh_flags; |
125 | ||
bd2c77a0 YH |
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; | |
c3968a85 | 130 | struct rt6key rt6i_prefsrc; |
b4ce9277 | 131 | |
8d0b94af MKL |
132 | struct list_head rt6i_uncached; |
133 | struct uncached_list *rt6i_uncached_list; | |
134 | ||
bd2c77a0 | 135 | struct inet6_dev *rt6i_idev; |
d52d3997 | 136 | struct rt6_info * __percpu *rt6i_pcpu; |
b4ce9277 | 137 | |
705f1c86 | 138 | u32 rt6i_metric; |
4b32b5ad | 139 | u32 rt6i_pmtu; |
bd2c77a0 YH |
140 | /* more non-fragment space at head required */ |
141 | unsigned short rt6i_nfheader_len; | |
bd2c77a0 | 142 | u8 rt6i_protocol; |
1da177e4 LT |
143 | }; |
144 | ||
7a3025b1 YH |
145 | static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst) |
146 | { | |
147 | return ((struct rt6_info *)dst)->rt6i_idev; | |
148 | } | |
149 | ||
1716a961 G |
150 | static inline void rt6_clean_expires(struct rt6_info *rt) |
151 | { | |
1716a961 | 152 | rt->rt6i_flags &= ~RTF_EXPIRES; |
01ba16d6 | 153 | rt->dst.expires = 0; |
1716a961 G |
154 | } |
155 | ||
156 | static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires) | |
157 | { | |
1716a961 | 158 | rt->dst.expires = expires; |
ecd98837 | 159 | rt->rt6i_flags |= RTF_EXPIRES; |
1716a961 G |
160 | } |
161 | ||
ecd98837 | 162 | static inline void rt6_update_expires(struct rt6_info *rt0, int timeout) |
1716a961 | 163 | { |
ecd98837 YH |
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; | |
1716a961 | 173 | } |
b197df4f | 174 | |
c5cff856 WW |
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 | */ | |
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 | ||
b197df4f MKL |
198 | static inline u32 rt6_get_cookie(const struct rt6_info *rt) |
199 | { | |
c5cff856 WW |
200 | u32 cookie = 0; |
201 | ||
02bcf4e0 | 202 | if (rt->rt6i_flags & RTF_PCPU || |
a4c2fd7f | 203 | (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from)) |
3da59bd9 MKL |
204 | rt = (struct rt6_info *)(rt->dst.from); |
205 | ||
c5cff856 WW |
206 | rt6_get_cookie_safe(rt, &cookie); |
207 | ||
208 | return cookie; | |
b197df4f | 209 | } |
1716a961 | 210 | |
94e187c0 AW |
211 | static inline void ip6_rt_put(struct rt6_info *rt) |
212 | { | |
213 | /* dst_release() accepts a NULL parameter. | |
214 | * We rely on dst being first structure in struct rt6_info | |
215 | */ | |
216 | BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0); | |
217 | dst_release(&rt->dst); | |
218 | } | |
219 | ||
a460aa83 IS |
220 | void rt6_free_pcpu(struct rt6_info *non_pcpu_rt); |
221 | ||
222 | static inline void rt6_hold(struct rt6_info *rt) | |
223 | { | |
224 | atomic_inc(&rt->rt6i_ref); | |
225 | } | |
226 | ||
227 | static inline void rt6_release(struct rt6_info *rt) | |
228 | { | |
229 | if (atomic_dec_and_test(&rt->rt6i_ref)) { | |
230 | rt6_free_pcpu(rt); | |
231 | dst_dev_put(&rt->dst); | |
232 | dst_release(&rt->dst); | |
233 | } | |
234 | } | |
235 | ||
94b2cfe0 HFS |
236 | enum fib6_walk_state { |
237 | #ifdef CONFIG_IPV6_SUBTREES | |
238 | FWS_S, | |
239 | #endif | |
240 | FWS_L, | |
241 | FWS_R, | |
242 | FWS_C, | |
243 | FWS_U | |
244 | }; | |
245 | ||
246 | struct fib6_walker { | |
bbef49da | 247 | struct list_head lh; |
1da177e4 LT |
248 | struct fib6_node *root, *node; |
249 | struct rt6_info *leaf; | |
94b2cfe0 HFS |
250 | enum fib6_walk_state state; |
251 | bool prune; | |
2bec5a36 PM |
252 | unsigned int skip; |
253 | unsigned int count; | |
94b2cfe0 | 254 | int (*func)(struct fib6_walker *); |
1da177e4 LT |
255 | void *args; |
256 | }; | |
257 | ||
1da177e4 LT |
258 | struct rt6_statistics { |
259 | __u32 fib_nodes; | |
260 | __u32 fib_route_nodes; | |
261 | __u32 fib_rt_alloc; /* permanent routes */ | |
262 | __u32 fib_rt_entries; /* rt entries in table */ | |
263 | __u32 fib_rt_cache; /* cache routes */ | |
264 | __u32 fib_discarded_routes; | |
265 | }; | |
266 | ||
267 | #define RTN_TL_ROOT 0x0001 | |
268 | #define RTN_ROOT 0x0002 /* tree root node */ | |
269 | #define RTN_RTINFO 0x0004 /* node with valid routing info */ | |
270 | ||
271 | /* | |
272 | * priority levels (or metrics) | |
273 | * | |
274 | */ | |
275 | ||
1da177e4 | 276 | |
c71099ac TG |
277 | struct fib6_table { |
278 | struct hlist_node tb6_hlist; | |
279 | u32 tb6_id; | |
280 | rwlock_t tb6_lock; | |
281 | struct fib6_node tb6_root; | |
8e773277 | 282 | struct inet_peer_base tb6_peers; |
830218c1 | 283 | unsigned int flags; |
e1ee0a5b | 284 | unsigned int fib_seq; |
830218c1 | 285 | #define RT6_TABLE_HAS_DFLT_ROUTER BIT(0) |
c71099ac TG |
286 | }; |
287 | ||
288 | #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC | |
289 | #define RT6_TABLE_MAIN RT_TABLE_MAIN | |
c71099ac TG |
290 | #define RT6_TABLE_DFLT RT6_TABLE_MAIN |
291 | #define RT6_TABLE_INFO RT6_TABLE_MAIN | |
292 | #define RT6_TABLE_PREFIX RT6_TABLE_MAIN | |
293 | ||
294 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
295 | #define FIB6_TABLE_MIN 1 | |
296 | #define FIB6_TABLE_MAX RT_TABLE_MAX | |
101367c2 | 297 | #define RT6_TABLE_LOCAL RT_TABLE_LOCAL |
c71099ac TG |
298 | #else |
299 | #define FIB6_TABLE_MIN RT_TABLE_MAIN | |
300 | #define FIB6_TABLE_MAX FIB6_TABLE_MIN | |
101367c2 | 301 | #define RT6_TABLE_LOCAL RT6_TABLE_MAIN |
c71099ac TG |
302 | #endif |
303 | ||
8ed67789 DL |
304 | typedef struct rt6_info *(*pol_lookup_t)(struct net *, |
305 | struct fib6_table *, | |
4c9483b2 | 306 | struct flowi6 *, int); |
1da177e4 | 307 | |
df77fe4d IS |
308 | struct fib6_entry_notifier_info { |
309 | struct fib_notifier_info info; /* must be first */ | |
310 | struct rt6_info *rt; | |
311 | }; | |
312 | ||
1da177e4 LT |
313 | /* |
314 | * exported functions | |
315 | */ | |
316 | ||
5c3a0fd7 JP |
317 | struct fib6_table *fib6_get_table(struct net *net, u32 id); |
318 | struct fib6_table *fib6_new_table(struct net *net, u32 id); | |
319 | struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6, | |
320 | int flags, pol_lookup_t lookup); | |
c71099ac | 321 | |
5c3a0fd7 JP |
322 | struct fib6_node *fib6_lookup(struct fib6_node *root, |
323 | const struct in6_addr *daddr, | |
324 | const struct in6_addr *saddr); | |
1da177e4 | 325 | |
5c3a0fd7 JP |
326 | struct fib6_node *fib6_locate(struct fib6_node *root, |
327 | const struct in6_addr *daddr, int dst_len, | |
328 | const struct in6_addr *saddr, int src_len); | |
1da177e4 | 329 | |
5c3a0fd7 | 330 | void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg), |
0c3584d5 | 331 | void *arg); |
c71099ac | 332 | |
e715b6d3 | 333 | int fib6_add(struct fib6_node *root, struct rt6_info *rt, |
333c4301 DA |
334 | struct nl_info *info, struct mx6_config *mxc, |
335 | struct netlink_ext_ack *extack); | |
5c3a0fd7 | 336 | int fib6_del(struct rt6_info *rt, struct nl_info *info); |
1da177e4 | 337 | |
37a1d361 RP |
338 | void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, |
339 | unsigned int flags); | |
1da177e4 | 340 | |
5c3a0fd7 | 341 | void fib6_run_gc(unsigned long expires, struct net *net, bool force); |
1da177e4 | 342 | |
5c3a0fd7 | 343 | void fib6_gc_cleanup(void); |
1da177e4 | 344 | |
5c3a0fd7 | 345 | int fib6_init(void); |
101367c2 | 346 | |
8d2ca1d7 HFS |
347 | int ipv6_route_open(struct inode *inode, struct file *file); |
348 | ||
16ab6d7d IS |
349 | int call_fib6_notifier(struct notifier_block *nb, struct net *net, |
350 | enum fib_event_type event_type, | |
351 | struct fib_notifier_info *info); | |
352 | int call_fib6_notifiers(struct net *net, enum fib_event_type event_type, | |
353 | struct fib_notifier_info *info); | |
354 | ||
355 | int __net_init fib6_notifier_init(struct net *net); | |
356 | void __net_exit fib6_notifier_exit(struct net *net); | |
357 | ||
e1ee0a5b IS |
358 | unsigned int fib6_tables_seq_read(struct net *net); |
359 | int fib6_tables_dump(struct net *net, struct notifier_block *nb); | |
360 | ||
7e5449c2 | 361 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
5c3a0fd7 JP |
362 | int fib6_rules_init(void); |
363 | void fib6_rules_cleanup(void); | |
e3ea9731 | 364 | bool fib6_rule_default(const struct fib_rule *rule); |
dcb18f76 IS |
365 | int fib6_rules_dump(struct net *net, struct notifier_block *nb); |
366 | unsigned int fib6_rules_seq_read(struct net *net); | |
7e5449c2 DL |
367 | #else |
368 | static inline int fib6_rules_init(void) | |
369 | { | |
370 | return 0; | |
371 | } | |
372 | static inline void fib6_rules_cleanup(void) | |
373 | { | |
374 | return ; | |
375 | } | |
e3ea9731 IS |
376 | static inline bool fib6_rule_default(const struct fib_rule *rule) |
377 | { | |
378 | return true; | |
379 | } | |
dcb18f76 IS |
380 | static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb) |
381 | { | |
382 | return 0; | |
383 | } | |
384 | static inline unsigned int fib6_rules_seq_read(struct net *net) | |
385 | { | |
386 | return 0; | |
387 | } | |
7e5449c2 | 388 | #endif |
1da177e4 | 389 | #endif |