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; |
1da177e4 LT |
75 | }; |
76 | ||
7fc33165 YH |
77 | #ifndef CONFIG_IPV6_SUBTREES |
78 | #define FIB6_SUBTREE(fn) NULL | |
79 | #else | |
80 | #define FIB6_SUBTREE(fn) ((fn)->subtree) | |
81 | #endif | |
1da177e4 | 82 | |
e715b6d3 FW |
83 | struct mx6_config { |
84 | const u32 *mx; | |
85 | DECLARE_BITMAP(mx_valid, RTAX_MAX); | |
86 | }; | |
87 | ||
1da177e4 LT |
88 | /* |
89 | * routing information | |
90 | * | |
91 | */ | |
92 | ||
fd2c3ef7 | 93 | struct rt6key { |
1da177e4 LT |
94 | struct in6_addr addr; |
95 | int plen; | |
96 | }; | |
97 | ||
c71099ac TG |
98 | struct fib6_table; |
99 | ||
fd2c3ef7 | 100 | struct rt6_info { |
d8d1f30b | 101 | struct dst_entry dst; |
1da177e4 | 102 | |
bd2c77a0 YH |
103 | /* |
104 | * Tail elements of dst_entry (__refcnt etc.) | |
105 | * and these elements (rarely used in hot path) are in | |
106 | * the same cache line. | |
107 | */ | |
108 | struct fib6_table *rt6i_table; | |
1da177e4 LT |
109 | struct fib6_node *rt6i_node; |
110 | ||
111 | struct in6_addr rt6i_gateway; | |
1da177e4 | 112 | |
51ebd318 ND |
113 | /* Multipath routes: |
114 | * siblings is a list of rt6_info that have the the same metric/weight, | |
115 | * destination, but not the same gateway. nsiblings is just a cache | |
116 | * to speed up lookup. | |
117 | */ | |
118 | struct list_head rt6i_siblings; | |
119 | unsigned int rt6i_nsiblings; | |
120 | ||
bd2c77a0 | 121 | atomic_t rt6i_ref; |
a47ed4cd | 122 | |
bd2c77a0 YH |
123 | /* These are in a separate cache line. */ |
124 | struct rt6key rt6i_dst ____cacheline_aligned_in_smp; | |
125 | u32 rt6i_flags; | |
126 | struct rt6key rt6i_src; | |
c3968a85 | 127 | struct rt6key rt6i_prefsrc; |
b4ce9277 | 128 | |
8d0b94af MKL |
129 | struct list_head rt6i_uncached; |
130 | struct uncached_list *rt6i_uncached_list; | |
131 | ||
bd2c77a0 | 132 | struct inet6_dev *rt6i_idev; |
d52d3997 | 133 | struct rt6_info * __percpu *rt6i_pcpu; |
b4ce9277 | 134 | |
705f1c86 | 135 | u32 rt6i_metric; |
4b32b5ad | 136 | u32 rt6i_pmtu; |
bd2c77a0 YH |
137 | /* more non-fragment space at head required */ |
138 | unsigned short rt6i_nfheader_len; | |
bd2c77a0 | 139 | u8 rt6i_protocol; |
1da177e4 LT |
140 | }; |
141 | ||
7a3025b1 YH |
142 | static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst) |
143 | { | |
144 | return ((struct rt6_info *)dst)->rt6i_idev; | |
145 | } | |
146 | ||
1716a961 G |
147 | static inline void rt6_clean_expires(struct rt6_info *rt) |
148 | { | |
1716a961 | 149 | rt->rt6i_flags &= ~RTF_EXPIRES; |
01ba16d6 | 150 | rt->dst.expires = 0; |
1716a961 G |
151 | } |
152 | ||
153 | static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires) | |
154 | { | |
1716a961 | 155 | rt->dst.expires = expires; |
ecd98837 | 156 | rt->rt6i_flags |= RTF_EXPIRES; |
1716a961 G |
157 | } |
158 | ||
ecd98837 | 159 | static inline void rt6_update_expires(struct rt6_info *rt0, int timeout) |
1716a961 | 160 | { |
ecd98837 YH |
161 | struct rt6_info *rt; |
162 | ||
163 | for (rt = rt0; rt && !(rt->rt6i_flags & RTF_EXPIRES); | |
164 | rt = (struct rt6_info *)rt->dst.from); | |
165 | if (rt && rt != rt0) | |
166 | rt0->dst.expires = rt->dst.expires; | |
167 | ||
168 | dst_set_expires(&rt0->dst, timeout); | |
169 | rt0->rt6i_flags |= RTF_EXPIRES; | |
1716a961 | 170 | } |
b197df4f MKL |
171 | |
172 | static inline u32 rt6_get_cookie(const struct rt6_info *rt) | |
173 | { | |
02bcf4e0 | 174 | if (rt->rt6i_flags & RTF_PCPU || |
a4c2fd7f | 175 | (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from)) |
3da59bd9 MKL |
176 | rt = (struct rt6_info *)(rt->dst.from); |
177 | ||
b197df4f MKL |
178 | return rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0; |
179 | } | |
1716a961 | 180 | |
94e187c0 AW |
181 | static inline void ip6_rt_put(struct rt6_info *rt) |
182 | { | |
183 | /* dst_release() accepts a NULL parameter. | |
184 | * We rely on dst being first structure in struct rt6_info | |
185 | */ | |
186 | BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0); | |
187 | dst_release(&rt->dst); | |
188 | } | |
189 | ||
a460aa83 IS |
190 | void rt6_free_pcpu(struct rt6_info *non_pcpu_rt); |
191 | ||
192 | static inline void rt6_hold(struct rt6_info *rt) | |
193 | { | |
194 | atomic_inc(&rt->rt6i_ref); | |
195 | } | |
196 | ||
197 | static inline void rt6_release(struct rt6_info *rt) | |
198 | { | |
199 | if (atomic_dec_and_test(&rt->rt6i_ref)) { | |
200 | rt6_free_pcpu(rt); | |
201 | dst_dev_put(&rt->dst); | |
202 | dst_release(&rt->dst); | |
203 | } | |
204 | } | |
205 | ||
94b2cfe0 HFS |
206 | enum fib6_walk_state { |
207 | #ifdef CONFIG_IPV6_SUBTREES | |
208 | FWS_S, | |
209 | #endif | |
210 | FWS_L, | |
211 | FWS_R, | |
212 | FWS_C, | |
213 | FWS_U | |
214 | }; | |
215 | ||
216 | struct fib6_walker { | |
bbef49da | 217 | struct list_head lh; |
1da177e4 LT |
218 | struct fib6_node *root, *node; |
219 | struct rt6_info *leaf; | |
94b2cfe0 HFS |
220 | enum fib6_walk_state state; |
221 | bool prune; | |
2bec5a36 PM |
222 | unsigned int skip; |
223 | unsigned int count; | |
94b2cfe0 | 224 | int (*func)(struct fib6_walker *); |
1da177e4 LT |
225 | void *args; |
226 | }; | |
227 | ||
1da177e4 LT |
228 | struct rt6_statistics { |
229 | __u32 fib_nodes; | |
230 | __u32 fib_route_nodes; | |
231 | __u32 fib_rt_alloc; /* permanent routes */ | |
232 | __u32 fib_rt_entries; /* rt entries in table */ | |
233 | __u32 fib_rt_cache; /* cache routes */ | |
234 | __u32 fib_discarded_routes; | |
235 | }; | |
236 | ||
237 | #define RTN_TL_ROOT 0x0001 | |
238 | #define RTN_ROOT 0x0002 /* tree root node */ | |
239 | #define RTN_RTINFO 0x0004 /* node with valid routing info */ | |
240 | ||
241 | /* | |
242 | * priority levels (or metrics) | |
243 | * | |
244 | */ | |
245 | ||
1da177e4 | 246 | |
c71099ac TG |
247 | struct fib6_table { |
248 | struct hlist_node tb6_hlist; | |
249 | u32 tb6_id; | |
250 | rwlock_t tb6_lock; | |
251 | struct fib6_node tb6_root; | |
8e773277 | 252 | struct inet_peer_base tb6_peers; |
830218c1 | 253 | unsigned int flags; |
e1ee0a5b | 254 | unsigned int fib_seq; |
830218c1 | 255 | #define RT6_TABLE_HAS_DFLT_ROUTER BIT(0) |
c71099ac TG |
256 | }; |
257 | ||
258 | #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC | |
259 | #define RT6_TABLE_MAIN RT_TABLE_MAIN | |
c71099ac TG |
260 | #define RT6_TABLE_DFLT RT6_TABLE_MAIN |
261 | #define RT6_TABLE_INFO RT6_TABLE_MAIN | |
262 | #define RT6_TABLE_PREFIX RT6_TABLE_MAIN | |
263 | ||
264 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
265 | #define FIB6_TABLE_MIN 1 | |
266 | #define FIB6_TABLE_MAX RT_TABLE_MAX | |
101367c2 | 267 | #define RT6_TABLE_LOCAL RT_TABLE_LOCAL |
c71099ac TG |
268 | #else |
269 | #define FIB6_TABLE_MIN RT_TABLE_MAIN | |
270 | #define FIB6_TABLE_MAX FIB6_TABLE_MIN | |
101367c2 | 271 | #define RT6_TABLE_LOCAL RT6_TABLE_MAIN |
c71099ac TG |
272 | #endif |
273 | ||
8ed67789 DL |
274 | typedef struct rt6_info *(*pol_lookup_t)(struct net *, |
275 | struct fib6_table *, | |
4c9483b2 | 276 | struct flowi6 *, int); |
1da177e4 | 277 | |
df77fe4d IS |
278 | struct fib6_entry_notifier_info { |
279 | struct fib_notifier_info info; /* must be first */ | |
280 | struct rt6_info *rt; | |
281 | }; | |
282 | ||
1da177e4 LT |
283 | /* |
284 | * exported functions | |
285 | */ | |
286 | ||
5c3a0fd7 JP |
287 | struct fib6_table *fib6_get_table(struct net *net, u32 id); |
288 | struct fib6_table *fib6_new_table(struct net *net, u32 id); | |
289 | struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6, | |
290 | int flags, pol_lookup_t lookup); | |
c71099ac | 291 | |
5c3a0fd7 JP |
292 | struct fib6_node *fib6_lookup(struct fib6_node *root, |
293 | const struct in6_addr *daddr, | |
294 | const struct in6_addr *saddr); | |
1da177e4 | 295 | |
5c3a0fd7 JP |
296 | struct fib6_node *fib6_locate(struct fib6_node *root, |
297 | const struct in6_addr *daddr, int dst_len, | |
298 | const struct in6_addr *saddr, int src_len); | |
1da177e4 | 299 | |
5c3a0fd7 | 300 | void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg), |
0c3584d5 | 301 | void *arg); |
c71099ac | 302 | |
e715b6d3 | 303 | int fib6_add(struct fib6_node *root, struct rt6_info *rt, |
333c4301 DA |
304 | struct nl_info *info, struct mx6_config *mxc, |
305 | struct netlink_ext_ack *extack); | |
5c3a0fd7 | 306 | int fib6_del(struct rt6_info *rt, struct nl_info *info); |
1da177e4 | 307 | |
37a1d361 RP |
308 | void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, |
309 | unsigned int flags); | |
1da177e4 | 310 | |
5c3a0fd7 | 311 | void fib6_run_gc(unsigned long expires, struct net *net, bool force); |
1da177e4 | 312 | |
5c3a0fd7 | 313 | void fib6_gc_cleanup(void); |
1da177e4 | 314 | |
5c3a0fd7 | 315 | int fib6_init(void); |
101367c2 | 316 | |
8d2ca1d7 HFS |
317 | int ipv6_route_open(struct inode *inode, struct file *file); |
318 | ||
16ab6d7d IS |
319 | int call_fib6_notifier(struct notifier_block *nb, struct net *net, |
320 | enum fib_event_type event_type, | |
321 | struct fib_notifier_info *info); | |
322 | int call_fib6_notifiers(struct net *net, enum fib_event_type event_type, | |
323 | struct fib_notifier_info *info); | |
324 | ||
325 | int __net_init fib6_notifier_init(struct net *net); | |
326 | void __net_exit fib6_notifier_exit(struct net *net); | |
327 | ||
e1ee0a5b IS |
328 | unsigned int fib6_tables_seq_read(struct net *net); |
329 | int fib6_tables_dump(struct net *net, struct notifier_block *nb); | |
330 | ||
7e5449c2 | 331 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
5c3a0fd7 JP |
332 | int fib6_rules_init(void); |
333 | void fib6_rules_cleanup(void); | |
e3ea9731 | 334 | bool fib6_rule_default(const struct fib_rule *rule); |
dcb18f76 IS |
335 | int fib6_rules_dump(struct net *net, struct notifier_block *nb); |
336 | unsigned int fib6_rules_seq_read(struct net *net); | |
7e5449c2 DL |
337 | #else |
338 | static inline int fib6_rules_init(void) | |
339 | { | |
340 | return 0; | |
341 | } | |
342 | static inline void fib6_rules_cleanup(void) | |
343 | { | |
344 | return ; | |
345 | } | |
e3ea9731 IS |
346 | static inline bool fib6_rule_default(const struct fib_rule *rule) |
347 | { | |
348 | return true; | |
349 | } | |
dcb18f76 IS |
350 | static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb) |
351 | { | |
352 | return 0; | |
353 | } | |
354 | static inline unsigned int fib6_rules_seq_read(struct net *net) | |
355 | { | |
356 | return 0; | |
357 | } | |
7e5449c2 | 358 | #endif |
1da177e4 | 359 | #endif |