]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * INET An implementation of the TCP/IP protocol suite for the LINUX | |
3 | * operating system. INET is implemented using the BSD Socket | |
4 | * interface as the means of communication with the user level. | |
5 | * | |
6 | * IPv4 Forwarding Information Base: semantics. | |
7 | * | |
1da177e4 LT |
8 | * Authors: Alexey Kuznetsov, <[email protected]> |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public License | |
12 | * as published by the Free Software Foundation; either version | |
13 | * 2 of the License, or (at your option) any later version. | |
14 | */ | |
15 | ||
7c0f6ba6 | 16 | #include <linux/uaccess.h> |
1da177e4 LT |
17 | #include <linux/bitops.h> |
18 | #include <linux/types.h> | |
19 | #include <linux/kernel.h> | |
20 | #include <linux/jiffies.h> | |
21 | #include <linux/mm.h> | |
22 | #include <linux/string.h> | |
23 | #include <linux/socket.h> | |
24 | #include <linux/sockios.h> | |
25 | #include <linux/errno.h> | |
26 | #include <linux/in.h> | |
27 | #include <linux/inet.h> | |
14c85021 | 28 | #include <linux/inetdevice.h> |
1da177e4 LT |
29 | #include <linux/netdevice.h> |
30 | #include <linux/if_arp.h> | |
31 | #include <linux/proc_fs.h> | |
32 | #include <linux/skbuff.h> | |
1da177e4 | 33 | #include <linux/init.h> |
5a0e3ad6 | 34 | #include <linux/slab.h> |
c3ab2b4e | 35 | #include <linux/netlink.h> |
1da177e4 | 36 | |
14c85021 | 37 | #include <net/arp.h> |
1da177e4 LT |
38 | #include <net/ip.h> |
39 | #include <net/protocol.h> | |
40 | #include <net/route.h> | |
41 | #include <net/tcp.h> | |
42 | #include <net/sock.h> | |
43 | #include <net/ip_fib.h> | |
f21c7bc5 | 44 | #include <net/netlink.h> |
4e902c57 | 45 | #include <net/nexthop.h> |
571e7226 | 46 | #include <net/lwtunnel.h> |
04b1d4e5 | 47 | #include <net/fib_notifier.h> |
1da177e4 LT |
48 | |
49 | #include "fib_lookup.h" | |
50 | ||
832b4c5e | 51 | static DEFINE_SPINLOCK(fib_info_lock); |
1da177e4 LT |
52 | static struct hlist_head *fib_info_hash; |
53 | static struct hlist_head *fib_info_laddrhash; | |
123b9731 | 54 | static unsigned int fib_info_hash_size; |
1da177e4 LT |
55 | static unsigned int fib_info_cnt; |
56 | ||
57 | #define DEVINDEX_HASHBITS 8 | |
58 | #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS) | |
59 | static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE]; | |
60 | ||
61 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
1da177e4 | 62 | |
6a31d2a9 ED |
63 | #define for_nexthops(fi) { \ |
64 | int nhsel; const struct fib_nh *nh; \ | |
65 | for (nhsel = 0, nh = (fi)->fib_nh; \ | |
66 | nhsel < (fi)->fib_nhs; \ | |
67 | nh++, nhsel++) | |
68 | ||
69 | #define change_nexthops(fi) { \ | |
70 | int nhsel; struct fib_nh *nexthop_nh; \ | |
71 | for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \ | |
72 | nhsel < (fi)->fib_nhs; \ | |
73 | nexthop_nh++, nhsel++) | |
1da177e4 LT |
74 | |
75 | #else /* CONFIG_IP_ROUTE_MULTIPATH */ | |
76 | ||
77 | /* Hope, that gcc will optimize it to get rid of dummy loop */ | |
78 | ||
6a31d2a9 ED |
79 | #define for_nexthops(fi) { \ |
80 | int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \ | |
81 | for (nhsel = 0; nhsel < 1; nhsel++) | |
1da177e4 | 82 | |
6a31d2a9 ED |
83 | #define change_nexthops(fi) { \ |
84 | int nhsel; \ | |
85 | struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \ | |
86 | for (nhsel = 0; nhsel < 1; nhsel++) | |
1da177e4 LT |
87 | |
88 | #endif /* CONFIG_IP_ROUTE_MULTIPATH */ | |
89 | ||
90 | #define endfor_nexthops(fi) } | |
91 | ||
92 | ||
3be0686b | 93 | const struct fib_prop fib_props[RTN_MAX + 1] = { |
6a31d2a9 | 94 | [RTN_UNSPEC] = { |
1da177e4 LT |
95 | .error = 0, |
96 | .scope = RT_SCOPE_NOWHERE, | |
6a31d2a9 ED |
97 | }, |
98 | [RTN_UNICAST] = { | |
1da177e4 LT |
99 | .error = 0, |
100 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
101 | }, |
102 | [RTN_LOCAL] = { | |
1da177e4 LT |
103 | .error = 0, |
104 | .scope = RT_SCOPE_HOST, | |
6a31d2a9 ED |
105 | }, |
106 | [RTN_BROADCAST] = { | |
1da177e4 LT |
107 | .error = 0, |
108 | .scope = RT_SCOPE_LINK, | |
6a31d2a9 ED |
109 | }, |
110 | [RTN_ANYCAST] = { | |
1da177e4 LT |
111 | .error = 0, |
112 | .scope = RT_SCOPE_LINK, | |
6a31d2a9 ED |
113 | }, |
114 | [RTN_MULTICAST] = { | |
1da177e4 LT |
115 | .error = 0, |
116 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
117 | }, |
118 | [RTN_BLACKHOLE] = { | |
1da177e4 LT |
119 | .error = -EINVAL, |
120 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
121 | }, |
122 | [RTN_UNREACHABLE] = { | |
1da177e4 LT |
123 | .error = -EHOSTUNREACH, |
124 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
125 | }, |
126 | [RTN_PROHIBIT] = { | |
1da177e4 LT |
127 | .error = -EACCES, |
128 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
129 | }, |
130 | [RTN_THROW] = { | |
1da177e4 LT |
131 | .error = -EAGAIN, |
132 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
133 | }, |
134 | [RTN_NAT] = { | |
1da177e4 LT |
135 | .error = -EINVAL, |
136 | .scope = RT_SCOPE_NOWHERE, | |
6a31d2a9 ED |
137 | }, |
138 | [RTN_XRESOLVE] = { | |
1da177e4 LT |
139 | .error = -EINVAL, |
140 | .scope = RT_SCOPE_NOWHERE, | |
6a31d2a9 | 141 | }, |
1da177e4 LT |
142 | }; |
143 | ||
c5038a83 DM |
144 | static void rt_fibinfo_free(struct rtable __rcu **rtp) |
145 | { | |
146 | struct rtable *rt = rcu_dereference_protected(*rtp, 1); | |
147 | ||
148 | if (!rt) | |
149 | return; | |
150 | ||
151 | /* Not even needed : RCU_INIT_POINTER(*rtp, NULL); | |
152 | * because we waited an RCU grace period before calling | |
153 | * free_fib_info_rcu() | |
154 | */ | |
155 | ||
95c47f9c | 156 | dst_dev_put(&rt->dst); |
b838d5e1 | 157 | dst_release_immediate(&rt->dst); |
c5038a83 DM |
158 | } |
159 | ||
4895c771 DM |
160 | static void free_nh_exceptions(struct fib_nh *nh) |
161 | { | |
caa41527 | 162 | struct fnhe_hash_bucket *hash; |
4895c771 DM |
163 | int i; |
164 | ||
caa41527 ED |
165 | hash = rcu_dereference_protected(nh->nh_exceptions, 1); |
166 | if (!hash) | |
167 | return; | |
4895c771 DM |
168 | for (i = 0; i < FNHE_HASH_SIZE; i++) { |
169 | struct fib_nh_exception *fnhe; | |
170 | ||
5abf7f7e | 171 | fnhe = rcu_dereference_protected(hash[i].chain, 1); |
4895c771 DM |
172 | while (fnhe) { |
173 | struct fib_nh_exception *next; | |
82695b30 | 174 | |
5abf7f7e | 175 | next = rcu_dereference_protected(fnhe->fnhe_next, 1); |
c5038a83 | 176 | |
2ffae99d TT |
177 | rt_fibinfo_free(&fnhe->fnhe_rth_input); |
178 | rt_fibinfo_free(&fnhe->fnhe_rth_output); | |
c5038a83 | 179 | |
4895c771 DM |
180 | kfree(fnhe); |
181 | ||
182 | fnhe = next; | |
183 | } | |
184 | } | |
185 | kfree(hash); | |
186 | } | |
187 | ||
c5038a83 | 188 | static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp) |
d26b3a7c ED |
189 | { |
190 | int cpu; | |
191 | ||
192 | if (!rtp) | |
193 | return; | |
194 | ||
195 | for_each_possible_cpu(cpu) { | |
196 | struct rtable *rt; | |
197 | ||
198 | rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1); | |
0830106c | 199 | if (rt) { |
95c47f9c | 200 | dst_dev_put(&rt->dst); |
b838d5e1 | 201 | dst_release_immediate(&rt->dst); |
0830106c | 202 | } |
d26b3a7c ED |
203 | } |
204 | free_percpu(rtp); | |
205 | } | |
206 | ||
faa041a4 DA |
207 | void fib_nh_release(struct net *net, struct fib_nh *fib_nh) |
208 | { | |
209 | #ifdef CONFIG_IP_ROUTE_CLASSID | |
210 | if (fib_nh->nh_tclassid) | |
211 | net->ipv4.fib_num_tclassid_users--; | |
212 | #endif | |
213 | if (fib_nh->nh_dev) | |
214 | dev_put(fib_nh->nh_dev); | |
215 | ||
216 | lwtstate_put(fib_nh->nh_lwtstate); | |
217 | free_nh_exceptions(fib_nh); | |
218 | rt_fibinfo_free_cpus(fib_nh->nh_pcpu_rth_output); | |
219 | rt_fibinfo_free(&fib_nh->nh_rth_input); | |
220 | } | |
221 | ||
1da177e4 | 222 | /* Release a nexthop info record */ |
19c1ea14 YZ |
223 | static void free_fib_info_rcu(struct rcu_head *head) |
224 | { | |
225 | struct fib_info *fi = container_of(head, struct fib_info, rcu); | |
226 | ||
e49cc0da | 227 | change_nexthops(fi) { |
faa041a4 | 228 | fib_nh_release(fi->fib_net, nexthop_nh); |
e49cc0da YZ |
229 | } endfor_nexthops(fi); |
230 | ||
cc5f0eb2 DA |
231 | ip_fib_metrics_put(fi->fib_metrics); |
232 | ||
19c1ea14 YZ |
233 | kfree(fi); |
234 | } | |
1da177e4 LT |
235 | |
236 | void free_fib_info(struct fib_info *fi) | |
237 | { | |
238 | if (fi->fib_dead == 0) { | |
058bd4d2 | 239 | pr_warn("Freeing alive fib_info %p\n", fi); |
1da177e4 LT |
240 | return; |
241 | } | |
1da177e4 | 242 | fib_info_cnt--; |
faa041a4 | 243 | |
19c1ea14 | 244 | call_rcu(&fi->rcu, free_fib_info_rcu); |
1da177e4 | 245 | } |
b423cb10 | 246 | EXPORT_SYMBOL_GPL(free_fib_info); |
1da177e4 LT |
247 | |
248 | void fib_release_info(struct fib_info *fi) | |
249 | { | |
832b4c5e | 250 | spin_lock_bh(&fib_info_lock); |
1da177e4 LT |
251 | if (fi && --fi->fib_treeref == 0) { |
252 | hlist_del(&fi->fib_hash); | |
253 | if (fi->fib_prefsrc) | |
254 | hlist_del(&fi->fib_lhash); | |
255 | change_nexthops(fi) { | |
71fceff0 | 256 | if (!nexthop_nh->nh_dev) |
1da177e4 | 257 | continue; |
71fceff0 | 258 | hlist_del(&nexthop_nh->nh_hash); |
1da177e4 LT |
259 | } endfor_nexthops(fi) |
260 | fi->fib_dead = 1; | |
261 | fib_info_put(fi); | |
262 | } | |
832b4c5e | 263 | spin_unlock_bh(&fib_info_lock); |
1da177e4 LT |
264 | } |
265 | ||
6a31d2a9 | 266 | static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi) |
1da177e4 LT |
267 | { |
268 | const struct fib_nh *onh = ofi->fib_nh; | |
269 | ||
270 | for_nexthops(fi) { | |
271 | if (nh->nh_oif != onh->nh_oif || | |
272 | nh->nh_gw != onh->nh_gw || | |
273 | nh->nh_scope != onh->nh_scope || | |
274 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
275 | nh->nh_weight != onh->nh_weight || | |
276 | #endif | |
c7066f70 | 277 | #ifdef CONFIG_IP_ROUTE_CLASSID |
1da177e4 LT |
278 | nh->nh_tclassid != onh->nh_tclassid || |
279 | #endif | |
571e7226 | 280 | lwtunnel_cmp_encap(nh->nh_lwtstate, onh->nh_lwtstate) || |
8a3d0316 | 281 | ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_COMPARE_MASK)) |
1da177e4 LT |
282 | return -1; |
283 | onh++; | |
284 | } endfor_nexthops(fi); | |
285 | return 0; | |
286 | } | |
287 | ||
88ebc72f DM |
288 | static inline unsigned int fib_devindex_hashfn(unsigned int val) |
289 | { | |
290 | unsigned int mask = DEVINDEX_HASHSIZE - 1; | |
291 | ||
292 | return (val ^ | |
293 | (val >> DEVINDEX_HASHBITS) ^ | |
294 | (val >> (DEVINDEX_HASHBITS * 2))) & mask; | |
295 | } | |
296 | ||
1da177e4 LT |
297 | static inline unsigned int fib_info_hashfn(const struct fib_info *fi) |
298 | { | |
123b9731 | 299 | unsigned int mask = (fib_info_hash_size - 1); |
1da177e4 LT |
300 | unsigned int val = fi->fib_nhs; |
301 | ||
37e826c5 | 302 | val ^= (fi->fib_protocol << 8) | fi->fib_scope; |
81f7bf6c | 303 | val ^= (__force u32)fi->fib_prefsrc; |
1da177e4 | 304 | val ^= fi->fib_priority; |
88ebc72f DM |
305 | for_nexthops(fi) { |
306 | val ^= fib_devindex_hashfn(nh->nh_oif); | |
307 | } endfor_nexthops(fi) | |
1da177e4 LT |
308 | |
309 | return (val ^ (val >> 7) ^ (val >> 12)) & mask; | |
310 | } | |
311 | ||
312 | static struct fib_info *fib_find_info(const struct fib_info *nfi) | |
313 | { | |
314 | struct hlist_head *head; | |
1da177e4 LT |
315 | struct fib_info *fi; |
316 | unsigned int hash; | |
317 | ||
318 | hash = fib_info_hashfn(nfi); | |
319 | head = &fib_info_hash[hash]; | |
320 | ||
b67bfe0d | 321 | hlist_for_each_entry(fi, head, fib_hash) { |
09ad9bc7 | 322 | if (!net_eq(fi->fib_net, nfi->fib_net)) |
4814bdbd | 323 | continue; |
1da177e4 LT |
324 | if (fi->fib_nhs != nfi->fib_nhs) |
325 | continue; | |
326 | if (nfi->fib_protocol == fi->fib_protocol && | |
37e826c5 | 327 | nfi->fib_scope == fi->fib_scope && |
1da177e4 LT |
328 | nfi->fib_prefsrc == fi->fib_prefsrc && |
329 | nfi->fib_priority == fi->fib_priority && | |
f4ef85bb | 330 | nfi->fib_type == fi->fib_type && |
1da177e4 | 331 | memcmp(nfi->fib_metrics, fi->fib_metrics, |
fcd13f42 | 332 | sizeof(u32) * RTAX_MAX) == 0 && |
8a3d0316 | 333 | !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) && |
1da177e4 LT |
334 | (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0)) |
335 | return fi; | |
336 | } | |
337 | ||
338 | return NULL; | |
339 | } | |
340 | ||
1da177e4 | 341 | /* Check, that the gateway is already configured. |
6a31d2a9 | 342 | * Used only by redirect accept routine. |
1da177e4 | 343 | */ |
d878e72e | 344 | int ip_fib_check_default(__be32 gw, struct net_device *dev) |
1da177e4 LT |
345 | { |
346 | struct hlist_head *head; | |
1da177e4 LT |
347 | struct fib_nh *nh; |
348 | unsigned int hash; | |
349 | ||
832b4c5e | 350 | spin_lock(&fib_info_lock); |
1da177e4 LT |
351 | |
352 | hash = fib_devindex_hashfn(dev->ifindex); | |
353 | head = &fib_info_devhash[hash]; | |
b67bfe0d | 354 | hlist_for_each_entry(nh, head, nh_hash) { |
1da177e4 LT |
355 | if (nh->nh_dev == dev && |
356 | nh->nh_gw == gw && | |
6a31d2a9 | 357 | !(nh->nh_flags & RTNH_F_DEAD)) { |
832b4c5e | 358 | spin_unlock(&fib_info_lock); |
1da177e4 LT |
359 | return 0; |
360 | } | |
361 | } | |
362 | ||
832b4c5e | 363 | spin_unlock(&fib_info_lock); |
1da177e4 LT |
364 | |
365 | return -1; | |
366 | } | |
367 | ||
339bf98f TG |
368 | static inline size_t fib_nlmsg_size(struct fib_info *fi) |
369 | { | |
370 | size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg)) | |
371 | + nla_total_size(4) /* RTA_TABLE */ | |
372 | + nla_total_size(4) /* RTA_DST */ | |
373 | + nla_total_size(4) /* RTA_PRIORITY */ | |
ea697639 DB |
374 | + nla_total_size(4) /* RTA_PREFSRC */ |
375 | + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */ | |
339bf98f TG |
376 | |
377 | /* space for nested metrics */ | |
378 | payload += nla_total_size((RTAX_MAX * nla_total_size(4))); | |
379 | ||
380 | if (fi->fib_nhs) { | |
571e7226 | 381 | size_t nh_encapsize = 0; |
339bf98f TG |
382 | /* Also handles the special case fib_nhs == 1 */ |
383 | ||
384 | /* each nexthop is packed in an attribute */ | |
385 | size_t nhsize = nla_total_size(sizeof(struct rtnexthop)); | |
386 | ||
387 | /* may contain flow and gateway attribute */ | |
388 | nhsize += 2 * nla_total_size(4); | |
389 | ||
571e7226 RP |
390 | /* grab encap info */ |
391 | for_nexthops(fi) { | |
392 | if (nh->nh_lwtstate) { | |
393 | /* RTA_ENCAP_TYPE */ | |
394 | nh_encapsize += lwtunnel_get_encap_size( | |
395 | nh->nh_lwtstate); | |
396 | /* RTA_ENCAP */ | |
397 | nh_encapsize += nla_total_size(2); | |
398 | } | |
399 | } endfor_nexthops(fi); | |
400 | ||
339bf98f | 401 | /* all nexthops are packed in a nested attribute */ |
571e7226 RP |
402 | payload += nla_total_size((fi->fib_nhs * nhsize) + |
403 | nh_encapsize); | |
404 | ||
339bf98f TG |
405 | } |
406 | ||
407 | return payload; | |
408 | } | |
409 | ||
81f7bf6c | 410 | void rtmsg_fib(int event, __be32 key, struct fib_alias *fa, |
9877b253 | 411 | int dst_len, u32 tb_id, const struct nl_info *info, |
b8f55831 | 412 | unsigned int nlm_flags) |
1da177e4 LT |
413 | { |
414 | struct sk_buff *skb; | |
4e902c57 | 415 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
f21c7bc5 | 416 | int err = -ENOBUFS; |
1da177e4 | 417 | |
339bf98f | 418 | skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL); |
51456b29 | 419 | if (!skb) |
f21c7bc5 | 420 | goto errout; |
1da177e4 | 421 | |
15e47304 | 422 | err = fib_dump_info(skb, info->portid, seq, event, tb_id, |
37e826c5 | 423 | fa->fa_type, key, dst_len, |
b8f55831 | 424 | fa->fa_tos, fa->fa_info, nlm_flags); |
26932566 PM |
425 | if (err < 0) { |
426 | /* -EMSGSIZE implies BUG in fib_nlmsg_size() */ | |
427 | WARN_ON(err == -EMSGSIZE); | |
428 | kfree_skb(skb); | |
429 | goto errout; | |
430 | } | |
15e47304 | 431 | rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE, |
1ce85fe4 PNA |
432 | info->nlh, GFP_KERNEL); |
433 | return; | |
f21c7bc5 TG |
434 | errout: |
435 | if (err < 0) | |
4d1169c1 | 436 | rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err); |
1da177e4 LT |
437 | } |
438 | ||
c9cb6b6e SH |
439 | static int fib_detect_death(struct fib_info *fi, int order, |
440 | struct fib_info **last_resort, int *last_idx, | |
441 | int dflt) | |
1da177e4 LT |
442 | { |
443 | struct neighbour *n; | |
444 | int state = NUD_NONE; | |
445 | ||
446 | n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev); | |
447 | if (n) { | |
448 | state = n->nud_state; | |
449 | neigh_release(n); | |
88f64320 JA |
450 | } else { |
451 | return 0; | |
1da177e4 | 452 | } |
d9319100 | 453 | if (state == NUD_REACHABLE) |
1da177e4 | 454 | return 0; |
6a31d2a9 | 455 | if ((state & NUD_VALID) && order != dflt) |
1da177e4 | 456 | return 0; |
6a31d2a9 | 457 | if ((state & NUD_VALID) || |
88f64320 | 458 | (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) { |
1da177e4 LT |
459 | *last_resort = fi; |
460 | *last_idx = order; | |
461 | } | |
462 | return 1; | |
463 | } | |
464 | ||
e4516ef6 DA |
465 | int fib_nh_init(struct net *net, struct fib_nh *nh, |
466 | struct fib_config *cfg, int nh_weight, | |
467 | struct netlink_ext_ack *extack) | |
468 | { | |
469 | int err = -ENOMEM; | |
470 | ||
471 | nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *); | |
472 | if (!nh->nh_pcpu_rth_output) | |
473 | goto err_out; | |
474 | ||
475 | if (cfg->fc_encap) { | |
476 | struct lwtunnel_state *lwtstate; | |
477 | ||
478 | err = -EINVAL; | |
479 | if (cfg->fc_encap_type == LWTUNNEL_ENCAP_NONE) { | |
480 | NL_SET_ERR_MSG(extack, "LWT encap type not specified"); | |
481 | goto lwt_failure; | |
482 | } | |
483 | err = lwtunnel_build_state(cfg->fc_encap_type, | |
484 | cfg->fc_encap, AF_INET, cfg, | |
485 | &lwtstate, extack); | |
486 | if (err) | |
487 | goto lwt_failure; | |
488 | ||
489 | nh->nh_lwtstate = lwtstate_get(lwtstate); | |
490 | } | |
491 | ||
492 | nh->nh_oif = cfg->fc_oif; | |
493 | nh->nh_gw = cfg->fc_gw; | |
494 | nh->nh_flags = cfg->fc_flags; | |
495 | ||
496 | #ifdef CONFIG_IP_ROUTE_CLASSID | |
497 | nh->nh_tclassid = cfg->fc_flow; | |
498 | if (nh->nh_tclassid) | |
499 | net->ipv4.fib_num_tclassid_users++; | |
500 | #endif | |
501 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
502 | nh->nh_weight = nh_weight; | |
503 | #endif | |
504 | return 0; | |
505 | ||
506 | lwt_failure: | |
507 | rt_fibinfo_free_cpus(nh->nh_pcpu_rth_output); | |
508 | nh->nh_pcpu_rth_output = NULL; | |
509 | err_out: | |
510 | return err; | |
511 | } | |
512 | ||
1da177e4 LT |
513 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
514 | ||
6d8422a1 DA |
515 | static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining, |
516 | struct netlink_ext_ack *extack) | |
1da177e4 LT |
517 | { |
518 | int nhs = 0; | |
1da177e4 | 519 | |
4e902c57 | 520 | while (rtnh_ok(rtnh, remaining)) { |
1da177e4 | 521 | nhs++; |
4e902c57 TG |
522 | rtnh = rtnh_next(rtnh, &remaining); |
523 | } | |
524 | ||
525 | /* leftover implies invalid nexthop configuration, discard it */ | |
c3ab2b4e DA |
526 | if (remaining > 0) { |
527 | NL_SET_ERR_MSG(extack, | |
528 | "Invalid nexthop configuration - extra data after nexthops"); | |
529 | nhs = 0; | |
530 | } | |
531 | ||
532 | return nhs; | |
1da177e4 LT |
533 | } |
534 | ||
4e902c57 | 535 | static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh, |
6d8422a1 DA |
536 | int remaining, struct fib_config *cfg, |
537 | struct netlink_ext_ack *extack) | |
1da177e4 | 538 | { |
e4516ef6 DA |
539 | struct net *net = fi->fib_net; |
540 | struct fib_config fib_cfg; | |
571e7226 RP |
541 | int ret; |
542 | ||
1da177e4 | 543 | change_nexthops(fi) { |
4e902c57 TG |
544 | int attrlen; |
545 | ||
e4516ef6 DA |
546 | memset(&fib_cfg, 0, sizeof(fib_cfg)); |
547 | ||
c3ab2b4e DA |
548 | if (!rtnh_ok(rtnh, remaining)) { |
549 | NL_SET_ERR_MSG(extack, | |
550 | "Invalid nexthop configuration - extra data after nexthop"); | |
1da177e4 | 551 | return -EINVAL; |
c3ab2b4e | 552 | } |
4e902c57 | 553 | |
c3ab2b4e DA |
554 | if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) { |
555 | NL_SET_ERR_MSG(extack, | |
556 | "Invalid flags for nexthop - can not contain DEAD or LINKDOWN"); | |
80610229 | 557 | return -EINVAL; |
c3ab2b4e | 558 | } |
80610229 | 559 | |
e4516ef6 DA |
560 | fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags; |
561 | fib_cfg.fc_oif = rtnh->rtnh_ifindex; | |
4e902c57 TG |
562 | |
563 | attrlen = rtnh_attrlen(rtnh); | |
564 | if (attrlen > 0) { | |
565 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); | |
566 | ||
567 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); | |
e4516ef6 DA |
568 | if (nla) |
569 | fib_cfg.fc_gw = nla_get_in_addr(nla); | |
570 | ||
4e902c57 | 571 | nla = nla_find(attrs, attrlen, RTA_FLOW); |
e4516ef6 DA |
572 | if (nla) |
573 | fib_cfg.fc_flow = nla_get_u32(nla); | |
574 | ||
575 | fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); | |
576 | nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); | |
577 | if (nla) | |
578 | fib_cfg.fc_encap_type = nla_get_u16(nla); | |
1da177e4 | 579 | } |
4e902c57 | 580 | |
e4516ef6 DA |
581 | ret = fib_nh_init(net, nexthop_nh, &fib_cfg, |
582 | rtnh->rtnh_hops + 1, extack); | |
583 | if (ret) | |
584 | goto errout; | |
585 | ||
4e902c57 | 586 | rtnh = rtnh_next(rtnh, &remaining); |
1da177e4 | 587 | } endfor_nexthops(fi); |
4e902c57 | 588 | |
571e7226 | 589 | ret = -EINVAL; |
e4516ef6 DA |
590 | if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif) { |
591 | NL_SET_ERR_MSG(extack, | |
592 | "Nexthop device index does not match RTA_OIF"); | |
593 | goto errout; | |
594 | } | |
595 | if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw) { | |
596 | NL_SET_ERR_MSG(extack, | |
597 | "Nexthop gateway does not match RTA_GATEWAY"); | |
598 | goto errout; | |
599 | } | |
600 | #ifdef CONFIG_IP_ROUTE_CLASSID | |
601 | if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) { | |
602 | NL_SET_ERR_MSG(extack, | |
603 | "Nexthop class id does not match RTA_FLOW"); | |
604 | goto errout; | |
605 | } | |
606 | #endif | |
607 | ret = 0; | |
571e7226 RP |
608 | errout: |
609 | return ret; | |
1da177e4 LT |
610 | } |
611 | ||
0e884c78 PN |
612 | static void fib_rebalance(struct fib_info *fi) |
613 | { | |
614 | int total; | |
615 | int w; | |
0e884c78 PN |
616 | |
617 | if (fi->fib_nhs < 2) | |
618 | return; | |
619 | ||
620 | total = 0; | |
621 | for_nexthops(fi) { | |
622 | if (nh->nh_flags & RTNH_F_DEAD) | |
623 | continue; | |
624 | ||
331c7a40 | 625 | if (ip_ignore_linkdown(nh->nh_dev) && |
0e884c78 PN |
626 | nh->nh_flags & RTNH_F_LINKDOWN) |
627 | continue; | |
628 | ||
629 | total += nh->nh_weight; | |
630 | } endfor_nexthops(fi); | |
631 | ||
632 | w = 0; | |
633 | change_nexthops(fi) { | |
634 | int upper_bound; | |
635 | ||
0e884c78 PN |
636 | if (nexthop_nh->nh_flags & RTNH_F_DEAD) { |
637 | upper_bound = -1; | |
331c7a40 | 638 | } else if (ip_ignore_linkdown(nexthop_nh->nh_dev) && |
0e884c78 PN |
639 | nexthop_nh->nh_flags & RTNH_F_LINKDOWN) { |
640 | upper_bound = -1; | |
641 | } else { | |
642 | w += nexthop_nh->nh_weight; | |
0a837fe4 PN |
643 | upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, |
644 | total) - 1; | |
0e884c78 PN |
645 | } |
646 | ||
647 | atomic_set(&nexthop_nh->nh_upper_bound, upper_bound); | |
648 | } endfor_nexthops(fi); | |
0e884c78 | 649 | } |
0e884c78 PN |
650 | #else /* CONFIG_IP_ROUTE_MULTIPATH */ |
651 | ||
8373c6c8 DA |
652 | static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh, |
653 | int remaining, struct fib_config *cfg, | |
654 | struct netlink_ext_ack *extack) | |
655 | { | |
656 | NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel"); | |
657 | ||
658 | return -EINVAL; | |
659 | } | |
660 | ||
0e884c78 | 661 | #define fib_rebalance(fi) do { } while (0) |
0e884c78 PN |
662 | |
663 | #endif /* CONFIG_IP_ROUTE_MULTIPATH */ | |
1da177e4 | 664 | |
30357d7d | 665 | static int fib_encap_match(u16 encap_type, |
e01286ef | 666 | struct nlattr *encap, |
30357d7d | 667 | const struct fib_nh *nh, |
9ae28727 DA |
668 | const struct fib_config *cfg, |
669 | struct netlink_ext_ack *extack) | |
571e7226 RP |
670 | { |
671 | struct lwtunnel_state *lwtstate; | |
df383e62 | 672 | int ret, result = 0; |
571e7226 RP |
673 | |
674 | if (encap_type == LWTUNNEL_ENCAP_NONE) | |
675 | return 0; | |
676 | ||
9ae28727 DA |
677 | ret = lwtunnel_build_state(encap_type, encap, AF_INET, |
678 | cfg, &lwtstate, extack); | |
df383e62 JB |
679 | if (!ret) { |
680 | result = lwtunnel_cmp_encap(lwtstate, nh->nh_lwtstate); | |
681 | lwtstate_free(lwtstate); | |
682 | } | |
571e7226 | 683 | |
df383e62 | 684 | return result; |
571e7226 RP |
685 | } |
686 | ||
9ae28727 DA |
687 | int fib_nh_match(struct fib_config *cfg, struct fib_info *fi, |
688 | struct netlink_ext_ack *extack) | |
1da177e4 LT |
689 | { |
690 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
4e902c57 TG |
691 | struct rtnexthop *rtnh; |
692 | int remaining; | |
1da177e4 LT |
693 | #endif |
694 | ||
4e902c57 | 695 | if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority) |
1da177e4 LT |
696 | return 1; |
697 | ||
4e902c57 | 698 | if (cfg->fc_oif || cfg->fc_gw) { |
571e7226 | 699 | if (cfg->fc_encap) { |
9ae28727 DA |
700 | if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap, |
701 | fi->fib_nh, cfg, extack)) | |
702 | return 1; | |
571e7226 | 703 | } |
a8c6db1d SB |
704 | #ifdef CONFIG_IP_ROUTE_CLASSID |
705 | if (cfg->fc_flow && | |
706 | cfg->fc_flow != fi->fib_nh->nh_tclassid) | |
707 | return 1; | |
708 | #endif | |
4e902c57 TG |
709 | if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) && |
710 | (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw)) | |
1da177e4 LT |
711 | return 0; |
712 | return 1; | |
713 | } | |
714 | ||
715 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
51456b29 | 716 | if (!cfg->fc_mp) |
1da177e4 | 717 | return 0; |
4e902c57 TG |
718 | |
719 | rtnh = cfg->fc_mp; | |
720 | remaining = cfg->fc_mp_len; | |
e905a9ed | 721 | |
1da177e4 | 722 | for_nexthops(fi) { |
4e902c57 | 723 | int attrlen; |
1da177e4 | 724 | |
4e902c57 | 725 | if (!rtnh_ok(rtnh, remaining)) |
1da177e4 | 726 | return -EINVAL; |
4e902c57 TG |
727 | |
728 | if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif) | |
1da177e4 | 729 | return 1; |
4e902c57 TG |
730 | |
731 | attrlen = rtnh_attrlen(rtnh); | |
f76936d0 | 732 | if (attrlen > 0) { |
4e902c57 TG |
733 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
734 | ||
735 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); | |
67b61f6c | 736 | if (nla && nla_get_in_addr(nla) != nh->nh_gw) |
1da177e4 | 737 | return 1; |
c7066f70 | 738 | #ifdef CONFIG_IP_ROUTE_CLASSID |
4e902c57 TG |
739 | nla = nla_find(attrs, attrlen, RTA_FLOW); |
740 | if (nla && nla_get_u32(nla) != nh->nh_tclassid) | |
1da177e4 LT |
741 | return 1; |
742 | #endif | |
743 | } | |
4e902c57 TG |
744 | |
745 | rtnh = rtnh_next(rtnh, &remaining); | |
1da177e4 LT |
746 | } endfor_nexthops(fi); |
747 | #endif | |
748 | return 0; | |
749 | } | |
750 | ||
5f9ae3d9 XL |
751 | bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi) |
752 | { | |
753 | struct nlattr *nla; | |
754 | int remaining; | |
755 | ||
756 | if (!cfg->fc_mx) | |
757 | return true; | |
758 | ||
759 | nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { | |
760 | int type = nla_type(nla); | |
d03a4557 | 761 | u32 fi_val, val; |
5f9ae3d9 XL |
762 | |
763 | if (!type) | |
764 | continue; | |
765 | if (type > RTAX_MAX) | |
766 | return false; | |
767 | ||
768 | if (type == RTAX_CC_ALGO) { | |
769 | char tmp[TCP_CA_NAME_MAX]; | |
770 | bool ecn_ca = false; | |
771 | ||
772 | nla_strlcpy(tmp, nla, sizeof(tmp)); | |
6670e152 | 773 | val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca); |
5f9ae3d9 | 774 | } else { |
5b5e7a0d ED |
775 | if (nla_len(nla) != sizeof(u32)) |
776 | return false; | |
5f9ae3d9 XL |
777 | val = nla_get_u32(nla); |
778 | } | |
779 | ||
d03a4557 PS |
780 | fi_val = fi->fib_metrics->metrics[type - 1]; |
781 | if (type == RTAX_FEATURES) | |
782 | fi_val &= ~DST_FEATURE_ECN_CA; | |
783 | ||
784 | if (fi_val != val) | |
5f9ae3d9 XL |
785 | return false; |
786 | } | |
787 | ||
788 | return true; | |
789 | } | |
790 | ||
1da177e4 LT |
791 | |
792 | /* | |
6a31d2a9 ED |
793 | * Picture |
794 | * ------- | |
795 | * | |
796 | * Semantics of nexthop is very messy by historical reasons. | |
797 | * We have to take into account, that: | |
798 | * a) gateway can be actually local interface address, | |
799 | * so that gatewayed route is direct. | |
800 | * b) gateway must be on-link address, possibly | |
801 | * described not by an ifaddr, but also by a direct route. | |
802 | * c) If both gateway and interface are specified, they should not | |
803 | * contradict. | |
804 | * d) If we use tunnel routes, gateway could be not on-link. | |
805 | * | |
806 | * Attempt to reconcile all of these (alas, self-contradictory) conditions | |
807 | * results in pretty ugly and hairy code with obscure logic. | |
808 | * | |
809 | * I chose to generalized it instead, so that the size | |
810 | * of code does not increase practically, but it becomes | |
811 | * much more general. | |
812 | * Every prefix is assigned a "scope" value: "host" is local address, | |
813 | * "link" is direct route, | |
814 | * [ ... "site" ... "interior" ... ] | |
815 | * and "universe" is true gateway route with global meaning. | |
816 | * | |
817 | * Every prefix refers to a set of "nexthop"s (gw, oif), | |
818 | * where gw must have narrower scope. This recursion stops | |
819 | * when gw has LOCAL scope or if "nexthop" is declared ONLINK, | |
820 | * which means that gw is forced to be on link. | |
821 | * | |
822 | * Code is still hairy, but now it is apparently logically | |
823 | * consistent and very flexible. F.e. as by-product it allows | |
824 | * to co-exists in peace independent exterior and interior | |
825 | * routing processes. | |
826 | * | |
827 | * Normally it looks as following. | |
828 | * | |
829 | * {universe prefix} -> (gw, oif) [scope link] | |
830 | * | | |
831 | * |-> {link prefix} -> (gw, oif) [scope local] | |
832 | * | | |
833 | * |-> {local prefix} (terminal node) | |
1da177e4 | 834 | */ |
fa8fefaa DA |
835 | static int fib_check_nh(struct fib_config *cfg, struct fib_nh *nh, |
836 | struct netlink_ext_ack *extack) | |
1da177e4 | 837 | { |
127eb7cd | 838 | int err = 0; |
86167a37 | 839 | struct net *net; |
6a31d2a9 | 840 | struct net_device *dev; |
1da177e4 | 841 | |
86167a37 | 842 | net = cfg->fc_nlinfo.nl_net; |
1da177e4 LT |
843 | if (nh->nh_gw) { |
844 | struct fib_result res; | |
845 | ||
6a31d2a9 | 846 | if (nh->nh_flags & RTNH_F_ONLINK) { |
30bbaa19 | 847 | unsigned int addr_type; |
1da177e4 | 848 | |
c3ab2b4e DA |
849 | if (cfg->fc_scope >= RT_SCOPE_LINK) { |
850 | NL_SET_ERR_MSG(extack, | |
851 | "Nexthop has invalid scope"); | |
1da177e4 | 852 | return -EINVAL; |
c3ab2b4e | 853 | } |
6a31d2a9 | 854 | dev = __dev_get_by_index(net, nh->nh_oif); |
066b1030 DA |
855 | if (!dev) { |
856 | NL_SET_ERR_MSG(extack, "Nexthop device required for onlink"); | |
1da177e4 | 857 | return -ENODEV; |
066b1030 | 858 | } |
c3ab2b4e DA |
859 | if (!(dev->flags & IFF_UP)) { |
860 | NL_SET_ERR_MSG(extack, | |
861 | "Nexthop device is not up"); | |
1da177e4 | 862 | return -ENETDOWN; |
c3ab2b4e | 863 | } |
30bbaa19 | 864 | addr_type = inet_addr_type_dev_table(net, dev, nh->nh_gw); |
c3ab2b4e DA |
865 | if (addr_type != RTN_UNICAST) { |
866 | NL_SET_ERR_MSG(extack, | |
867 | "Nexthop has invalid gateway"); | |
30bbaa19 | 868 | return -EINVAL; |
c3ab2b4e | 869 | } |
8a3d0316 AG |
870 | if (!netif_carrier_ok(dev)) |
871 | nh->nh_flags |= RTNH_F_LINKDOWN; | |
1da177e4 LT |
872 | nh->nh_dev = dev; |
873 | dev_hold(dev); | |
874 | nh->nh_scope = RT_SCOPE_LINK; | |
875 | return 0; | |
876 | } | |
ebc0ffae | 877 | rcu_read_lock(); |
1da177e4 | 878 | { |
3bfd8472 | 879 | struct fib_table *tbl = NULL; |
9ade2286 DM |
880 | struct flowi4 fl4 = { |
881 | .daddr = nh->nh_gw, | |
882 | .flowi4_scope = cfg->fc_scope + 1, | |
883 | .flowi4_oif = nh->nh_oif, | |
6a662719 | 884 | .flowi4_iif = LOOPBACK_IFINDEX, |
4e902c57 | 885 | }; |
1da177e4 LT |
886 | |
887 | /* It is not necessary, but requires a bit of thinking */ | |
9ade2286 DM |
888 | if (fl4.flowi4_scope < RT_SCOPE_LINK) |
889 | fl4.flowi4_scope = RT_SCOPE_LINK; | |
3bfd8472 DA |
890 | |
891 | if (cfg->fc_table) | |
892 | tbl = fib_get_table(net, cfg->fc_table); | |
893 | ||
894 | if (tbl) | |
895 | err = fib_table_lookup(tbl, &fl4, &res, | |
1e313678 ED |
896 | FIB_LOOKUP_IGNORE_LINKSTATE | |
897 | FIB_LOOKUP_NOREF); | |
4c9bcd11 DA |
898 | |
899 | /* on error or if no table given do full lookup. This | |
900 | * is needed for example when nexthops are in the local | |
901 | * table rather than the given table | |
902 | */ | |
903 | if (!tbl || err) { | |
3bfd8472 DA |
904 | err = fib_lookup(net, &fl4, &res, |
905 | FIB_LOOKUP_IGNORE_LINKSTATE); | |
4c9bcd11 DA |
906 | } |
907 | ||
ebc0ffae | 908 | if (err) { |
c3ab2b4e DA |
909 | NL_SET_ERR_MSG(extack, |
910 | "Nexthop has invalid gateway"); | |
ebc0ffae | 911 | rcu_read_unlock(); |
1da177e4 | 912 | return err; |
ebc0ffae | 913 | } |
1da177e4 LT |
914 | } |
915 | err = -EINVAL; | |
c3ab2b4e DA |
916 | if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) { |
917 | NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway"); | |
1da177e4 | 918 | goto out; |
c3ab2b4e | 919 | } |
1da177e4 LT |
920 | nh->nh_scope = res.scope; |
921 | nh->nh_oif = FIB_RES_OIF(res); | |
6a31d2a9 | 922 | nh->nh_dev = dev = FIB_RES_DEV(res); |
c3ab2b4e DA |
923 | if (!dev) { |
924 | NL_SET_ERR_MSG(extack, | |
925 | "No egress device for nexthop gateway"); | |
1da177e4 | 926 | goto out; |
c3ab2b4e | 927 | } |
6a31d2a9 | 928 | dev_hold(dev); |
8a3d0316 AG |
929 | if (!netif_carrier_ok(dev)) |
930 | nh->nh_flags |= RTNH_F_LINKDOWN; | |
8723e1b4 | 931 | err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN; |
1da177e4 LT |
932 | } else { |
933 | struct in_device *in_dev; | |
934 | ||
c3ab2b4e DA |
935 | if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) { |
936 | NL_SET_ERR_MSG(extack, | |
937 | "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set"); | |
1da177e4 | 938 | return -EINVAL; |
c3ab2b4e | 939 | } |
8723e1b4 ED |
940 | rcu_read_lock(); |
941 | err = -ENODEV; | |
86167a37 | 942 | in_dev = inetdev_by_index(net, nh->nh_oif); |
51456b29 | 943 | if (!in_dev) |
8723e1b4 ED |
944 | goto out; |
945 | err = -ENETDOWN; | |
c3ab2b4e DA |
946 | if (!(in_dev->dev->flags & IFF_UP)) { |
947 | NL_SET_ERR_MSG(extack, "Device for nexthop is not up"); | |
8723e1b4 | 948 | goto out; |
c3ab2b4e | 949 | } |
1da177e4 LT |
950 | nh->nh_dev = in_dev->dev; |
951 | dev_hold(nh->nh_dev); | |
952 | nh->nh_scope = RT_SCOPE_HOST; | |
8a3d0316 AG |
953 | if (!netif_carrier_ok(nh->nh_dev)) |
954 | nh->nh_flags |= RTNH_F_LINKDOWN; | |
8723e1b4 | 955 | err = 0; |
1da177e4 | 956 | } |
8723e1b4 ED |
957 | out: |
958 | rcu_read_unlock(); | |
959 | return err; | |
1da177e4 LT |
960 | } |
961 | ||
81f7bf6c | 962 | static inline unsigned int fib_laddr_hashfn(__be32 val) |
1da177e4 | 963 | { |
123b9731 | 964 | unsigned int mask = (fib_info_hash_size - 1); |
1da177e4 | 965 | |
6a31d2a9 ED |
966 | return ((__force u32)val ^ |
967 | ((__force u32)val >> 7) ^ | |
968 | ((__force u32)val >> 14)) & mask; | |
1da177e4 LT |
969 | } |
970 | ||
123b9731 | 971 | static struct hlist_head *fib_info_hash_alloc(int bytes) |
1da177e4 LT |
972 | { |
973 | if (bytes <= PAGE_SIZE) | |
88f83491 | 974 | return kzalloc(bytes, GFP_KERNEL); |
1da177e4 LT |
975 | else |
976 | return (struct hlist_head *) | |
6a31d2a9 ED |
977 | __get_free_pages(GFP_KERNEL | __GFP_ZERO, |
978 | get_order(bytes)); | |
1da177e4 LT |
979 | } |
980 | ||
123b9731 | 981 | static void fib_info_hash_free(struct hlist_head *hash, int bytes) |
1da177e4 LT |
982 | { |
983 | if (!hash) | |
984 | return; | |
985 | ||
986 | if (bytes <= PAGE_SIZE) | |
987 | kfree(hash); | |
988 | else | |
989 | free_pages((unsigned long) hash, get_order(bytes)); | |
990 | } | |
991 | ||
123b9731 DM |
992 | static void fib_info_hash_move(struct hlist_head *new_info_hash, |
993 | struct hlist_head *new_laddrhash, | |
994 | unsigned int new_size) | |
1da177e4 | 995 | { |
b7656e7f | 996 | struct hlist_head *old_info_hash, *old_laddrhash; |
123b9731 | 997 | unsigned int old_size = fib_info_hash_size; |
b7656e7f | 998 | unsigned int i, bytes; |
1da177e4 | 999 | |
832b4c5e | 1000 | spin_lock_bh(&fib_info_lock); |
b7656e7f DM |
1001 | old_info_hash = fib_info_hash; |
1002 | old_laddrhash = fib_info_laddrhash; | |
123b9731 | 1003 | fib_info_hash_size = new_size; |
1da177e4 LT |
1004 | |
1005 | for (i = 0; i < old_size; i++) { | |
1006 | struct hlist_head *head = &fib_info_hash[i]; | |
b67bfe0d | 1007 | struct hlist_node *n; |
1da177e4 LT |
1008 | struct fib_info *fi; |
1009 | ||
b67bfe0d | 1010 | hlist_for_each_entry_safe(fi, n, head, fib_hash) { |
1da177e4 LT |
1011 | struct hlist_head *dest; |
1012 | unsigned int new_hash; | |
1013 | ||
1da177e4 LT |
1014 | new_hash = fib_info_hashfn(fi); |
1015 | dest = &new_info_hash[new_hash]; | |
1016 | hlist_add_head(&fi->fib_hash, dest); | |
1017 | } | |
1018 | } | |
1019 | fib_info_hash = new_info_hash; | |
1020 | ||
1021 | for (i = 0; i < old_size; i++) { | |
1022 | struct hlist_head *lhead = &fib_info_laddrhash[i]; | |
b67bfe0d | 1023 | struct hlist_node *n; |
1da177e4 LT |
1024 | struct fib_info *fi; |
1025 | ||
b67bfe0d | 1026 | hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) { |
1da177e4 LT |
1027 | struct hlist_head *ldest; |
1028 | unsigned int new_hash; | |
1029 | ||
1da177e4 LT |
1030 | new_hash = fib_laddr_hashfn(fi->fib_prefsrc); |
1031 | ldest = &new_laddrhash[new_hash]; | |
1032 | hlist_add_head(&fi->fib_lhash, ldest); | |
1033 | } | |
1034 | } | |
1035 | fib_info_laddrhash = new_laddrhash; | |
1036 | ||
832b4c5e | 1037 | spin_unlock_bh(&fib_info_lock); |
b7656e7f DM |
1038 | |
1039 | bytes = old_size * sizeof(struct hlist_head *); | |
123b9731 DM |
1040 | fib_info_hash_free(old_info_hash, bytes); |
1041 | fib_info_hash_free(old_laddrhash, bytes); | |
1da177e4 LT |
1042 | } |
1043 | ||
436c3b66 DM |
1044 | __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh) |
1045 | { | |
1046 | nh->nh_saddr = inet_select_addr(nh->nh_dev, | |
1047 | nh->nh_gw, | |
37e826c5 | 1048 | nh->nh_parent->fib_scope); |
436c3b66 DM |
1049 | nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid); |
1050 | ||
1051 | return nh->nh_saddr; | |
1052 | } | |
1053 | ||
021dd3b8 DA |
1054 | static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc) |
1055 | { | |
1056 | if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst || | |
1057 | fib_prefsrc != cfg->fc_dst) { | |
9b8ff518 | 1058 | u32 tb_id = cfg->fc_table; |
e1b8d903 | 1059 | int rc; |
021dd3b8 DA |
1060 | |
1061 | if (tb_id == RT_TABLE_MAIN) | |
1062 | tb_id = RT_TABLE_LOCAL; | |
1063 | ||
e1b8d903 DA |
1064 | rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net, |
1065 | fib_prefsrc, tb_id); | |
1066 | ||
1067 | if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) { | |
1068 | rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net, | |
1069 | fib_prefsrc, RT_TABLE_LOCAL); | |
021dd3b8 | 1070 | } |
e1b8d903 DA |
1071 | |
1072 | if (rc != RTN_LOCAL) | |
1073 | return false; | |
021dd3b8 DA |
1074 | } |
1075 | return true; | |
1076 | } | |
1077 | ||
6d8422a1 DA |
1078 | struct fib_info *fib_create_info(struct fib_config *cfg, |
1079 | struct netlink_ext_ack *extack) | |
1da177e4 LT |
1080 | { |
1081 | int err; | |
1082 | struct fib_info *fi = NULL; | |
1083 | struct fib_info *ofi; | |
1da177e4 | 1084 | int nhs = 1; |
7462bd74 | 1085 | struct net *net = cfg->fc_nlinfo.nl_net; |
1da177e4 | 1086 | |
4c8237cd DM |
1087 | if (cfg->fc_type > RTN_MAX) |
1088 | goto err_inval; | |
1089 | ||
1da177e4 | 1090 | /* Fast check to catch the most weird cases */ |
c3ab2b4e DA |
1091 | if (fib_props[cfg->fc_type].scope > cfg->fc_scope) { |
1092 | NL_SET_ERR_MSG(extack, "Invalid scope"); | |
1da177e4 | 1093 | goto err_inval; |
c3ab2b4e | 1094 | } |
1da177e4 | 1095 | |
c3ab2b4e DA |
1096 | if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) { |
1097 | NL_SET_ERR_MSG(extack, | |
1098 | "Invalid rtm_flags - can not contain DEAD or LINKDOWN"); | |
80610229 | 1099 | goto err_inval; |
c3ab2b4e | 1100 | } |
80610229 | 1101 | |
1da177e4 | 1102 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
4e902c57 | 1103 | if (cfg->fc_mp) { |
6d8422a1 | 1104 | nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack); |
1da177e4 LT |
1105 | if (nhs == 0) |
1106 | goto err_inval; | |
1107 | } | |
1108 | #endif | |
1da177e4 LT |
1109 | |
1110 | err = -ENOBUFS; | |
123b9731 DM |
1111 | if (fib_info_cnt >= fib_info_hash_size) { |
1112 | unsigned int new_size = fib_info_hash_size << 1; | |
1da177e4 LT |
1113 | struct hlist_head *new_info_hash; |
1114 | struct hlist_head *new_laddrhash; | |
1115 | unsigned int bytes; | |
1116 | ||
1117 | if (!new_size) | |
d94ce9b2 | 1118 | new_size = 16; |
1da177e4 | 1119 | bytes = new_size * sizeof(struct hlist_head *); |
123b9731 DM |
1120 | new_info_hash = fib_info_hash_alloc(bytes); |
1121 | new_laddrhash = fib_info_hash_alloc(bytes); | |
1da177e4 | 1122 | if (!new_info_hash || !new_laddrhash) { |
123b9731 DM |
1123 | fib_info_hash_free(new_info_hash, bytes); |
1124 | fib_info_hash_free(new_laddrhash, bytes); | |
88f83491 | 1125 | } else |
123b9731 | 1126 | fib_info_hash_move(new_info_hash, new_laddrhash, new_size); |
1da177e4 | 1127 | |
123b9731 | 1128 | if (!fib_info_hash_size) |
1da177e4 LT |
1129 | goto failure; |
1130 | } | |
1131 | ||
1f533ba6 | 1132 | fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL); |
51456b29 | 1133 | if (!fi) |
1da177e4 | 1134 | goto failure; |
767a2217 | 1135 | fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx, |
d7e774f3 | 1136 | cfg->fc_mx_len, extack); |
767a2217 DA |
1137 | if (unlikely(IS_ERR(fi->fib_metrics))) { |
1138 | err = PTR_ERR(fi->fib_metrics); | |
1139 | kfree(fi); | |
1140 | return ERR_PTR(err); | |
187e5b3a | 1141 | } |
767a2217 | 1142 | |
187e5b3a | 1143 | fib_info_cnt++; |
efd7ef1c | 1144 | fi->fib_net = net; |
4e902c57 | 1145 | fi->fib_protocol = cfg->fc_protocol; |
37e826c5 | 1146 | fi->fib_scope = cfg->fc_scope; |
4e902c57 TG |
1147 | fi->fib_flags = cfg->fc_flags; |
1148 | fi->fib_priority = cfg->fc_priority; | |
1149 | fi->fib_prefsrc = cfg->fc_prefsrc; | |
f4ef85bb | 1150 | fi->fib_type = cfg->fc_type; |
5a56a0b3 | 1151 | fi->fib_tb_id = cfg->fc_table; |
1da177e4 LT |
1152 | |
1153 | fi->fib_nhs = nhs; | |
1154 | change_nexthops(fi) { | |
71fceff0 | 1155 | nexthop_nh->nh_parent = fi; |
1da177e4 LT |
1156 | } endfor_nexthops(fi) |
1157 | ||
e4516ef6 | 1158 | if (cfg->fc_mp) |
6d8422a1 | 1159 | err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack); |
e4516ef6 DA |
1160 | else |
1161 | err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack); | |
571e7226 | 1162 | |
e4516ef6 DA |
1163 | if (err != 0) |
1164 | goto failure; | |
1da177e4 | 1165 | |
4e902c57 | 1166 | if (fib_props[cfg->fc_type].error) { |
c3ab2b4e DA |
1167 | if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) { |
1168 | NL_SET_ERR_MSG(extack, | |
1169 | "Gateway, device and multipath can not be specified for this route type"); | |
1da177e4 | 1170 | goto err_inval; |
c3ab2b4e | 1171 | } |
1da177e4 | 1172 | goto link_it; |
4c8237cd DM |
1173 | } else { |
1174 | switch (cfg->fc_type) { | |
1175 | case RTN_UNICAST: | |
1176 | case RTN_LOCAL: | |
1177 | case RTN_BROADCAST: | |
1178 | case RTN_ANYCAST: | |
1179 | case RTN_MULTICAST: | |
1180 | break; | |
1181 | default: | |
c3ab2b4e | 1182 | NL_SET_ERR_MSG(extack, "Invalid route type"); |
4c8237cd DM |
1183 | goto err_inval; |
1184 | } | |
1da177e4 LT |
1185 | } |
1186 | ||
c3ab2b4e DA |
1187 | if (cfg->fc_scope > RT_SCOPE_HOST) { |
1188 | NL_SET_ERR_MSG(extack, "Invalid scope"); | |
1da177e4 | 1189 | goto err_inval; |
c3ab2b4e | 1190 | } |
1da177e4 | 1191 | |
4e902c57 | 1192 | if (cfg->fc_scope == RT_SCOPE_HOST) { |
1da177e4 LT |
1193 | struct fib_nh *nh = fi->fib_nh; |
1194 | ||
1195 | /* Local address is added. */ | |
c3ab2b4e DA |
1196 | if (nhs != 1) { |
1197 | NL_SET_ERR_MSG(extack, | |
1198 | "Route with host scope can not have multiple nexthops"); | |
6d8422a1 | 1199 | goto err_inval; |
c3ab2b4e DA |
1200 | } |
1201 | if (nh->nh_gw) { | |
1202 | NL_SET_ERR_MSG(extack, | |
1203 | "Route with host scope can not have a gateway"); | |
1da177e4 | 1204 | goto err_inval; |
c3ab2b4e | 1205 | } |
1da177e4 | 1206 | nh->nh_scope = RT_SCOPE_NOWHERE; |
7462bd74 | 1207 | nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif); |
1da177e4 | 1208 | err = -ENODEV; |
51456b29 | 1209 | if (!nh->nh_dev) |
1da177e4 LT |
1210 | goto failure; |
1211 | } else { | |
8a3d0316 AG |
1212 | int linkdown = 0; |
1213 | ||
1da177e4 | 1214 | change_nexthops(fi) { |
fa8fefaa | 1215 | err = fib_check_nh(cfg, nexthop_nh, extack); |
6a31d2a9 | 1216 | if (err != 0) |
1da177e4 | 1217 | goto failure; |
8a3d0316 AG |
1218 | if (nexthop_nh->nh_flags & RTNH_F_LINKDOWN) |
1219 | linkdown++; | |
1da177e4 | 1220 | } endfor_nexthops(fi) |
8a3d0316 AG |
1221 | if (linkdown == fi->fib_nhs) |
1222 | fi->fib_flags |= RTNH_F_LINKDOWN; | |
1da177e4 LT |
1223 | } |
1224 | ||
c3ab2b4e DA |
1225 | if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) { |
1226 | NL_SET_ERR_MSG(extack, "Invalid prefsrc address"); | |
021dd3b8 | 1227 | goto err_inval; |
c3ab2b4e | 1228 | } |
1da177e4 | 1229 | |
1fc050a1 | 1230 | change_nexthops(fi) { |
436c3b66 | 1231 | fib_info_update_nh_saddr(net, nexthop_nh); |
1fc050a1 DM |
1232 | } endfor_nexthops(fi) |
1233 | ||
0e884c78 PN |
1234 | fib_rebalance(fi); |
1235 | ||
1da177e4 | 1236 | link_it: |
6a31d2a9 ED |
1237 | ofi = fib_find_info(fi); |
1238 | if (ofi) { | |
1da177e4 LT |
1239 | fi->fib_dead = 1; |
1240 | free_fib_info(fi); | |
1241 | ofi->fib_treeref++; | |
1242 | return ofi; | |
1243 | } | |
1244 | ||
1245 | fi->fib_treeref++; | |
0029c0de | 1246 | refcount_set(&fi->fib_clntref, 1); |
832b4c5e | 1247 | spin_lock_bh(&fib_info_lock); |
1da177e4 LT |
1248 | hlist_add_head(&fi->fib_hash, |
1249 | &fib_info_hash[fib_info_hashfn(fi)]); | |
1250 | if (fi->fib_prefsrc) { | |
1251 | struct hlist_head *head; | |
1252 | ||
1253 | head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)]; | |
1254 | hlist_add_head(&fi->fib_lhash, head); | |
1255 | } | |
1256 | change_nexthops(fi) { | |
1257 | struct hlist_head *head; | |
1258 | unsigned int hash; | |
1259 | ||
71fceff0 | 1260 | if (!nexthop_nh->nh_dev) |
1da177e4 | 1261 | continue; |
71fceff0 | 1262 | hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex); |
1da177e4 | 1263 | head = &fib_info_devhash[hash]; |
71fceff0 | 1264 | hlist_add_head(&nexthop_nh->nh_hash, head); |
1da177e4 | 1265 | } endfor_nexthops(fi) |
832b4c5e | 1266 | spin_unlock_bh(&fib_info_lock); |
1da177e4 LT |
1267 | return fi; |
1268 | ||
1269 | err_inval: | |
1270 | err = -EINVAL; | |
1271 | ||
1272 | failure: | |
e905a9ed | 1273 | if (fi) { |
1da177e4 LT |
1274 | fi->fib_dead = 1; |
1275 | free_fib_info(fi); | |
1276 | } | |
4e902c57 TG |
1277 | |
1278 | return ERR_PTR(err); | |
1da177e4 LT |
1279 | } |
1280 | ||
15e47304 | 1281 | int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event, |
37e826c5 | 1282 | u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos, |
be403ea1 | 1283 | struct fib_info *fi, unsigned int flags) |
1da177e4 | 1284 | { |
be403ea1 | 1285 | struct nlmsghdr *nlh; |
1da177e4 | 1286 | struct rtmsg *rtm; |
1da177e4 | 1287 | |
15e47304 | 1288 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags); |
51456b29 | 1289 | if (!nlh) |
26932566 | 1290 | return -EMSGSIZE; |
be403ea1 TG |
1291 | |
1292 | rtm = nlmsg_data(nlh); | |
1da177e4 LT |
1293 | rtm->rtm_family = AF_INET; |
1294 | rtm->rtm_dst_len = dst_len; | |
1295 | rtm->rtm_src_len = 0; | |
1296 | rtm->rtm_tos = tos; | |
709772e6 KPO |
1297 | if (tb_id < 256) |
1298 | rtm->rtm_table = tb_id; | |
1299 | else | |
1300 | rtm->rtm_table = RT_TABLE_COMPAT; | |
f3756b79 DM |
1301 | if (nla_put_u32(skb, RTA_TABLE, tb_id)) |
1302 | goto nla_put_failure; | |
1da177e4 LT |
1303 | rtm->rtm_type = type; |
1304 | rtm->rtm_flags = fi->fib_flags; | |
37e826c5 | 1305 | rtm->rtm_scope = fi->fib_scope; |
1da177e4 | 1306 | rtm->rtm_protocol = fi->fib_protocol; |
be403ea1 | 1307 | |
f3756b79 | 1308 | if (rtm->rtm_dst_len && |
930345ea | 1309 | nla_put_in_addr(skb, RTA_DST, dst)) |
f3756b79 DM |
1310 | goto nla_put_failure; |
1311 | if (fi->fib_priority && | |
1312 | nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority)) | |
1313 | goto nla_put_failure; | |
3fb07daf | 1314 | if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0) |
be403ea1 TG |
1315 | goto nla_put_failure; |
1316 | ||
f3756b79 | 1317 | if (fi->fib_prefsrc && |
930345ea | 1318 | nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc)) |
f3756b79 | 1319 | goto nla_put_failure; |
1da177e4 | 1320 | if (fi->fib_nhs == 1) { |
f3756b79 | 1321 | if (fi->fib_nh->nh_gw && |
930345ea | 1322 | nla_put_in_addr(skb, RTA_GATEWAY, fi->fib_nh->nh_gw)) |
f3756b79 DM |
1323 | goto nla_put_failure; |
1324 | if (fi->fib_nh->nh_oif && | |
1325 | nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif)) | |
1326 | goto nla_put_failure; | |
0eeb075f | 1327 | if (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN) { |
25dd169a | 1328 | rcu_read_lock(); |
331c7a40 | 1329 | if (ip_ignore_linkdown(fi->fib_nh->nh_dev)) |
0eeb075f | 1330 | rtm->rtm_flags |= RTNH_F_DEAD; |
25dd169a | 1331 | rcu_read_unlock(); |
0eeb075f | 1332 | } |
475abbf1 IS |
1333 | if (fi->fib_nh->nh_flags & RTNH_F_OFFLOAD) |
1334 | rtm->rtm_flags |= RTNH_F_OFFLOAD; | |
c7066f70 | 1335 | #ifdef CONFIG_IP_ROUTE_CLASSID |
f3756b79 DM |
1336 | if (fi->fib_nh[0].nh_tclassid && |
1337 | nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid)) | |
1338 | goto nla_put_failure; | |
8265abc0 | 1339 | #endif |
ea7a8085 DA |
1340 | if (fi->fib_nh->nh_lwtstate && |
1341 | lwtunnel_fill_encap(skb, fi->fib_nh->nh_lwtstate) < 0) | |
1342 | goto nla_put_failure; | |
1da177e4 LT |
1343 | } |
1344 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
1345 | if (fi->fib_nhs > 1) { | |
be403ea1 TG |
1346 | struct rtnexthop *rtnh; |
1347 | struct nlattr *mp; | |
1348 | ||
1349 | mp = nla_nest_start(skb, RTA_MULTIPATH); | |
51456b29 | 1350 | if (!mp) |
be403ea1 | 1351 | goto nla_put_failure; |
1da177e4 LT |
1352 | |
1353 | for_nexthops(fi) { | |
be403ea1 | 1354 | rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); |
51456b29 | 1355 | if (!rtnh) |
be403ea1 TG |
1356 | goto nla_put_failure; |
1357 | ||
1358 | rtnh->rtnh_flags = nh->nh_flags & 0xFF; | |
0eeb075f | 1359 | if (nh->nh_flags & RTNH_F_LINKDOWN) { |
25dd169a | 1360 | rcu_read_lock(); |
331c7a40 | 1361 | if (ip_ignore_linkdown(nh->nh_dev)) |
0eeb075f | 1362 | rtnh->rtnh_flags |= RTNH_F_DEAD; |
25dd169a | 1363 | rcu_read_unlock(); |
0eeb075f | 1364 | } |
be403ea1 TG |
1365 | rtnh->rtnh_hops = nh->nh_weight - 1; |
1366 | rtnh->rtnh_ifindex = nh->nh_oif; | |
1367 | ||
f3756b79 | 1368 | if (nh->nh_gw && |
930345ea | 1369 | nla_put_in_addr(skb, RTA_GATEWAY, nh->nh_gw)) |
f3756b79 | 1370 | goto nla_put_failure; |
c7066f70 | 1371 | #ifdef CONFIG_IP_ROUTE_CLASSID |
f3756b79 DM |
1372 | if (nh->nh_tclassid && |
1373 | nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid)) | |
1374 | goto nla_put_failure; | |
8265abc0 | 1375 | #endif |
ea7a8085 DA |
1376 | if (nh->nh_lwtstate && |
1377 | lwtunnel_fill_encap(skb, nh->nh_lwtstate) < 0) | |
1378 | goto nla_put_failure; | |
1379 | ||
be403ea1 TG |
1380 | /* length of rtnetlink header + attributes */ |
1381 | rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh; | |
1da177e4 | 1382 | } endfor_nexthops(fi); |
be403ea1 TG |
1383 | |
1384 | nla_nest_end(skb, mp); | |
1da177e4 LT |
1385 | } |
1386 | #endif | |
053c095a JB |
1387 | nlmsg_end(skb, nlh); |
1388 | return 0; | |
1da177e4 | 1389 | |
be403ea1 | 1390 | nla_put_failure: |
26932566 PM |
1391 | nlmsg_cancel(skb, nlh); |
1392 | return -EMSGSIZE; | |
1da177e4 LT |
1393 | } |
1394 | ||
1da177e4 | 1395 | /* |
6a31d2a9 ED |
1396 | * Update FIB if: |
1397 | * - local address disappeared -> we must delete all the entries | |
1398 | * referring to it. | |
1399 | * - device went down -> we must shutdown all nexthops going via it. | |
1da177e4 | 1400 | */ |
5a56a0b3 | 1401 | int fib_sync_down_addr(struct net_device *dev, __be32 local) |
1da177e4 LT |
1402 | { |
1403 | int ret = 0; | |
85326fa5 DL |
1404 | unsigned int hash = fib_laddr_hashfn(local); |
1405 | struct hlist_head *head = &fib_info_laddrhash[hash]; | |
5a56a0b3 MT |
1406 | struct net *net = dev_net(dev); |
1407 | int tb_id = l3mdev_fib_table(dev); | |
85326fa5 | 1408 | struct fib_info *fi; |
1da177e4 | 1409 | |
51456b29 | 1410 | if (!fib_info_laddrhash || local == 0) |
85326fa5 | 1411 | return 0; |
1da177e4 | 1412 | |
b67bfe0d | 1413 | hlist_for_each_entry(fi, head, fib_lhash) { |
5a56a0b3 MT |
1414 | if (!net_eq(fi->fib_net, net) || |
1415 | fi->fib_tb_id != tb_id) | |
4814bdbd | 1416 | continue; |
85326fa5 DL |
1417 | if (fi->fib_prefsrc == local) { |
1418 | fi->fib_flags |= RTNH_F_DEAD; | |
1419 | ret++; | |
1da177e4 LT |
1420 | } |
1421 | } | |
85326fa5 DL |
1422 | return ret; |
1423 | } | |
1424 | ||
982acb97 IS |
1425 | static int call_fib_nh_notifiers(struct fib_nh *fib_nh, |
1426 | enum fib_event_type event_type) | |
1427 | { | |
331c7a40 | 1428 | bool ignore_link_down = ip_ignore_linkdown(fib_nh->nh_dev); |
982acb97 IS |
1429 | struct fib_nh_notifier_info info = { |
1430 | .fib_nh = fib_nh, | |
1431 | }; | |
1432 | ||
1433 | switch (event_type) { | |
1434 | case FIB_EVENT_NH_ADD: | |
1435 | if (fib_nh->nh_flags & RTNH_F_DEAD) | |
1436 | break; | |
331c7a40 | 1437 | if (ignore_link_down && fib_nh->nh_flags & RTNH_F_LINKDOWN) |
982acb97 | 1438 | break; |
04b1d4e5 IS |
1439 | return call_fib4_notifiers(dev_net(fib_nh->nh_dev), event_type, |
1440 | &info.info); | |
982acb97 | 1441 | case FIB_EVENT_NH_DEL: |
331c7a40 | 1442 | if ((ignore_link_down && fib_nh->nh_flags & RTNH_F_LINKDOWN) || |
982acb97 | 1443 | (fib_nh->nh_flags & RTNH_F_DEAD)) |
04b1d4e5 IS |
1444 | return call_fib4_notifiers(dev_net(fib_nh->nh_dev), |
1445 | event_type, &info.info); | |
982acb97 IS |
1446 | default: |
1447 | break; | |
1448 | } | |
1449 | ||
1450 | return NOTIFY_DONE; | |
1451 | } | |
1452 | ||
af7d6cce SD |
1453 | /* Update the PMTU of exceptions when: |
1454 | * - the new MTU of the first hop becomes smaller than the PMTU | |
1455 | * - the old MTU was the same as the PMTU, and it limited discovery of | |
1456 | * larger MTUs on the path. With that limit raised, we can now | |
1457 | * discover larger MTUs | |
1458 | * A special case is locked exceptions, for which the PMTU is smaller | |
1459 | * than the minimal accepted PMTU: | |
1460 | * - if the new MTU is greater than the PMTU, don't make any change | |
1461 | * - otherwise, unlock and set PMTU | |
1462 | */ | |
1463 | static void nh_update_mtu(struct fib_nh *nh, u32 new, u32 orig) | |
1464 | { | |
1465 | struct fnhe_hash_bucket *bucket; | |
1466 | int i; | |
1467 | ||
1468 | bucket = rcu_dereference_protected(nh->nh_exceptions, 1); | |
1469 | if (!bucket) | |
1470 | return; | |
1471 | ||
1472 | for (i = 0; i < FNHE_HASH_SIZE; i++) { | |
1473 | struct fib_nh_exception *fnhe; | |
1474 | ||
1475 | for (fnhe = rcu_dereference_protected(bucket[i].chain, 1); | |
1476 | fnhe; | |
1477 | fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) { | |
1478 | if (fnhe->fnhe_mtu_locked) { | |
1479 | if (new <= fnhe->fnhe_pmtu) { | |
1480 | fnhe->fnhe_pmtu = new; | |
1481 | fnhe->fnhe_mtu_locked = false; | |
1482 | } | |
1483 | } else if (new < fnhe->fnhe_pmtu || | |
1484 | orig == fnhe->fnhe_pmtu) { | |
1485 | fnhe->fnhe_pmtu = new; | |
1486 | } | |
1487 | } | |
1488 | } | |
1489 | } | |
1490 | ||
1491 | void fib_sync_mtu(struct net_device *dev, u32 orig_mtu) | |
1492 | { | |
1493 | unsigned int hash = fib_devindex_hashfn(dev->ifindex); | |
1494 | struct hlist_head *head = &fib_info_devhash[hash]; | |
1495 | struct fib_nh *nh; | |
1496 | ||
1497 | hlist_for_each_entry(nh, head, nh_hash) { | |
1498 | if (nh->nh_dev == dev) | |
1499 | nh_update_mtu(nh, dev->mtu, orig_mtu); | |
1500 | } | |
1501 | } | |
1502 | ||
4f823def JA |
1503 | /* Event force Flags Description |
1504 | * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host | |
1505 | * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host | |
1506 | * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed | |
1507 | * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed | |
1508 | */ | |
1509 | int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force) | |
85326fa5 DL |
1510 | { |
1511 | int ret = 0; | |
1512 | int scope = RT_SCOPE_NOWHERE; | |
1513 | struct fib_info *prev_fi = NULL; | |
1514 | unsigned int hash = fib_devindex_hashfn(dev->ifindex); | |
1515 | struct hlist_head *head = &fib_info_devhash[hash]; | |
85326fa5 | 1516 | struct fib_nh *nh; |
1da177e4 | 1517 | |
4f823def | 1518 | if (force) |
85326fa5 | 1519 | scope = -1; |
1da177e4 | 1520 | |
b67bfe0d | 1521 | hlist_for_each_entry(nh, head, nh_hash) { |
85326fa5 DL |
1522 | struct fib_info *fi = nh->nh_parent; |
1523 | int dead; | |
1da177e4 | 1524 | |
85326fa5 DL |
1525 | BUG_ON(!fi->fib_nhs); |
1526 | if (nh->nh_dev != dev || fi == prev_fi) | |
1527 | continue; | |
1528 | prev_fi = fi; | |
1529 | dead = 0; | |
1530 | change_nexthops(fi) { | |
6a31d2a9 | 1531 | if (nexthop_nh->nh_flags & RTNH_F_DEAD) |
85326fa5 | 1532 | dead++; |
71fceff0 DM |
1533 | else if (nexthop_nh->nh_dev == dev && |
1534 | nexthop_nh->nh_scope != scope) { | |
8a3d0316 AG |
1535 | switch (event) { |
1536 | case NETDEV_DOWN: | |
1537 | case NETDEV_UNREGISTER: | |
1538 | nexthop_nh->nh_flags |= RTNH_F_DEAD; | |
1539 | /* fall through */ | |
1540 | case NETDEV_CHANGE: | |
1541 | nexthop_nh->nh_flags |= RTNH_F_LINKDOWN; | |
1542 | break; | |
1543 | } | |
982acb97 IS |
1544 | call_fib_nh_notifiers(nexthop_nh, |
1545 | FIB_EVENT_NH_DEL); | |
85326fa5 DL |
1546 | dead++; |
1547 | } | |
1da177e4 | 1548 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
8a3d0316 AG |
1549 | if (event == NETDEV_UNREGISTER && |
1550 | nexthop_nh->nh_dev == dev) { | |
85326fa5 DL |
1551 | dead = fi->fib_nhs; |
1552 | break; | |
1da177e4 | 1553 | } |
85326fa5 DL |
1554 | #endif |
1555 | } endfor_nexthops(fi) | |
1556 | if (dead == fi->fib_nhs) { | |
8a3d0316 AG |
1557 | switch (event) { |
1558 | case NETDEV_DOWN: | |
1559 | case NETDEV_UNREGISTER: | |
1560 | fi->fib_flags |= RTNH_F_DEAD; | |
1561 | /* fall through */ | |
1562 | case NETDEV_CHANGE: | |
1563 | fi->fib_flags |= RTNH_F_LINKDOWN; | |
1564 | break; | |
1565 | } | |
85326fa5 | 1566 | ret++; |
1da177e4 | 1567 | } |
0e884c78 PN |
1568 | |
1569 | fib_rebalance(fi); | |
1da177e4 LT |
1570 | } |
1571 | ||
1572 | return ret; | |
1573 | } | |
1574 | ||
0c838ff1 | 1575 | /* Must be invoked inside of an RCU protected region. */ |
c7b371e3 | 1576 | static void fib_select_default(const struct flowi4 *flp, struct fib_result *res) |
0c838ff1 DM |
1577 | { |
1578 | struct fib_info *fi = NULL, *last_resort = NULL; | |
56315f9e | 1579 | struct hlist_head *fa_head = res->fa_head; |
0c838ff1 | 1580 | struct fib_table *tb = res->table; |
18a912e9 | 1581 | u8 slen = 32 - res->prefixlen; |
0c838ff1 | 1582 | int order = -1, last_idx = -1; |
2392debc JA |
1583 | struct fib_alias *fa, *fa1 = NULL; |
1584 | u32 last_prio = res->fi->fib_priority; | |
1585 | u8 last_tos = 0; | |
0c838ff1 | 1586 | |
56315f9e | 1587 | hlist_for_each_entry_rcu(fa, fa_head, fa_list) { |
0c838ff1 DM |
1588 | struct fib_info *next_fi = fa->fa_info; |
1589 | ||
18a912e9 JA |
1590 | if (fa->fa_slen != slen) |
1591 | continue; | |
2392debc JA |
1592 | if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos) |
1593 | continue; | |
18a912e9 JA |
1594 | if (fa->tb_id != tb->tb_id) |
1595 | continue; | |
2392debc JA |
1596 | if (next_fi->fib_priority > last_prio && |
1597 | fa->fa_tos == last_tos) { | |
1598 | if (last_tos) | |
1599 | continue; | |
1600 | break; | |
1601 | } | |
1602 | if (next_fi->fib_flags & RTNH_F_DEAD) | |
1603 | continue; | |
1604 | last_tos = fa->fa_tos; | |
1605 | last_prio = next_fi->fib_priority; | |
1606 | ||
37e826c5 | 1607 | if (next_fi->fib_scope != res->scope || |
0c838ff1 DM |
1608 | fa->fa_type != RTN_UNICAST) |
1609 | continue; | |
0c838ff1 DM |
1610 | if (!next_fi->fib_nh[0].nh_gw || |
1611 | next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK) | |
1612 | continue; | |
1613 | ||
1614 | fib_alias_accessed(fa); | |
1615 | ||
51456b29 | 1616 | if (!fi) { |
0c838ff1 DM |
1617 | if (next_fi != res->fi) |
1618 | break; | |
2392debc | 1619 | fa1 = fa; |
0c838ff1 | 1620 | } else if (!fib_detect_death(fi, order, &last_resort, |
2392debc | 1621 | &last_idx, fa1->fa_default)) { |
0c838ff1 | 1622 | fib_result_assign(res, fi); |
2392debc | 1623 | fa1->fa_default = order; |
0c838ff1 DM |
1624 | goto out; |
1625 | } | |
1626 | fi = next_fi; | |
1627 | order++; | |
1628 | } | |
1629 | ||
51456b29 | 1630 | if (order <= 0 || !fi) { |
2392debc JA |
1631 | if (fa1) |
1632 | fa1->fa_default = -1; | |
0c838ff1 DM |
1633 | goto out; |
1634 | } | |
1635 | ||
1636 | if (!fib_detect_death(fi, order, &last_resort, &last_idx, | |
2392debc | 1637 | fa1->fa_default)) { |
0c838ff1 | 1638 | fib_result_assign(res, fi); |
2392debc | 1639 | fa1->fa_default = order; |
0c838ff1 DM |
1640 | goto out; |
1641 | } | |
1642 | ||
1643 | if (last_idx >= 0) | |
1644 | fib_result_assign(res, last_resort); | |
2392debc | 1645 | fa1->fa_default = last_idx; |
0c838ff1 | 1646 | out: |
31d40937 | 1647 | return; |
0c838ff1 DM |
1648 | } |
1649 | ||
1da177e4 | 1650 | /* |
6a31d2a9 ED |
1651 | * Dead device goes up. We wake up dead nexthops. |
1652 | * It takes sense only on multipath routes. | |
1da177e4 | 1653 | */ |
8a3d0316 | 1654 | int fib_sync_up(struct net_device *dev, unsigned int nh_flags) |
1da177e4 LT |
1655 | { |
1656 | struct fib_info *prev_fi; | |
1657 | unsigned int hash; | |
1658 | struct hlist_head *head; | |
1da177e4 LT |
1659 | struct fib_nh *nh; |
1660 | int ret; | |
1661 | ||
6a31d2a9 | 1662 | if (!(dev->flags & IFF_UP)) |
1da177e4 LT |
1663 | return 0; |
1664 | ||
c9b3292e JA |
1665 | if (nh_flags & RTNH_F_DEAD) { |
1666 | unsigned int flags = dev_get_flags(dev); | |
1667 | ||
1668 | if (flags & (IFF_RUNNING | IFF_LOWER_UP)) | |
1669 | nh_flags |= RTNH_F_LINKDOWN; | |
1670 | } | |
1671 | ||
1da177e4 LT |
1672 | prev_fi = NULL; |
1673 | hash = fib_devindex_hashfn(dev->ifindex); | |
1674 | head = &fib_info_devhash[hash]; | |
1675 | ret = 0; | |
1676 | ||
b67bfe0d | 1677 | hlist_for_each_entry(nh, head, nh_hash) { |
1da177e4 LT |
1678 | struct fib_info *fi = nh->nh_parent; |
1679 | int alive; | |
1680 | ||
1681 | BUG_ON(!fi->fib_nhs); | |
1682 | if (nh->nh_dev != dev || fi == prev_fi) | |
1683 | continue; | |
1684 | ||
1685 | prev_fi = fi; | |
1686 | alive = 0; | |
1687 | change_nexthops(fi) { | |
8a3d0316 | 1688 | if (!(nexthop_nh->nh_flags & nh_flags)) { |
1da177e4 LT |
1689 | alive++; |
1690 | continue; | |
1691 | } | |
51456b29 | 1692 | if (!nexthop_nh->nh_dev || |
6a31d2a9 | 1693 | !(nexthop_nh->nh_dev->flags & IFF_UP)) |
1da177e4 | 1694 | continue; |
71fceff0 DM |
1695 | if (nexthop_nh->nh_dev != dev || |
1696 | !__in_dev_get_rtnl(dev)) | |
1da177e4 LT |
1697 | continue; |
1698 | alive++; | |
8a3d0316 | 1699 | nexthop_nh->nh_flags &= ~nh_flags; |
982acb97 | 1700 | call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD); |
1da177e4 LT |
1701 | } endfor_nexthops(fi) |
1702 | ||
1703 | if (alive > 0) { | |
8a3d0316 | 1704 | fi->fib_flags &= ~nh_flags; |
1da177e4 LT |
1705 | ret++; |
1706 | } | |
0e884c78 PN |
1707 | |
1708 | fib_rebalance(fi); | |
1da177e4 LT |
1709 | } |
1710 | ||
1711 | return ret; | |
1712 | } | |
1713 | ||
8a3d0316 | 1714 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
a6db4494 DA |
1715 | static bool fib_good_nh(const struct fib_nh *nh) |
1716 | { | |
1717 | int state = NUD_REACHABLE; | |
1718 | ||
1719 | if (nh->nh_scope == RT_SCOPE_LINK) { | |
1720 | struct neighbour *n; | |
1721 | ||
1722 | rcu_read_lock_bh(); | |
1723 | ||
d985d151 ED |
1724 | n = __ipv4_neigh_lookup_noref(nh->nh_dev, |
1725 | (__force u32)nh->nh_gw); | |
a6db4494 DA |
1726 | if (n) |
1727 | state = n->nud_state; | |
1728 | ||
1729 | rcu_read_unlock_bh(); | |
1730 | } | |
1731 | ||
1732 | return !!(state & NUD_VALID); | |
1733 | } | |
8a3d0316 | 1734 | |
0e884c78 | 1735 | void fib_select_multipath(struct fib_result *res, int hash) |
1da177e4 LT |
1736 | { |
1737 | struct fib_info *fi = res->fi; | |
a6db4494 DA |
1738 | struct net *net = fi->fib_net; |
1739 | bool first = false; | |
1da177e4 | 1740 | |
0e884c78 | 1741 | for_nexthops(fi) { |
6174a30d XL |
1742 | if (net->ipv4.sysctl_fib_multipath_use_neigh) { |
1743 | if (!fib_good_nh(nh)) | |
1744 | continue; | |
1745 | if (!first) { | |
1746 | res->nh_sel = nhsel; | |
1747 | first = true; | |
1748 | } | |
1749 | } | |
1750 | ||
0e884c78 PN |
1751 | if (hash > atomic_read(&nh->nh_upper_bound)) |
1752 | continue; | |
1da177e4 | 1753 | |
6174a30d XL |
1754 | res->nh_sel = nhsel; |
1755 | return; | |
1da177e4 | 1756 | } endfor_nexthops(fi); |
1da177e4 LT |
1757 | } |
1758 | #endif | |
3ce58d84 DA |
1759 | |
1760 | void fib_select_path(struct net *net, struct fib_result *res, | |
bf4e0a3d | 1761 | struct flowi4 *fl4, const struct sk_buff *skb) |
3ce58d84 | 1762 | { |
0d876f2c DA |
1763 | if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF)) |
1764 | goto check_saddr; | |
7a18c5b9 | 1765 | |
3ce58d84 | 1766 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
0d876f2c | 1767 | if (res->fi->fib_nhs > 1) { |
7efc0b6b | 1768 | int h = fib_multipath_hash(net, fl4, skb, NULL); |
9920e48b | 1769 | |
bf4e0a3d | 1770 | fib_select_multipath(res, h); |
3ce58d84 DA |
1771 | } |
1772 | else | |
1773 | #endif | |
1774 | if (!res->prefixlen && | |
1775 | res->table->tb_num_default > 1 && | |
0d876f2c | 1776 | res->type == RTN_UNICAST) |
3ce58d84 DA |
1777 | fib_select_default(fl4, res); |
1778 | ||
0d876f2c | 1779 | check_saddr: |
3ce58d84 DA |
1780 | if (!fl4->saddr) |
1781 | fl4->saddr = FIB_RES_PREFSRC(net, *res); | |
1782 | } |