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, <kuznet@ms2.inr.ac.ru> |
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 | ||
1da177e4 LT |
16 | #include <asm/uaccess.h> |
17 | #include <asm/system.h> | |
18 | #include <linux/bitops.h> | |
19 | #include <linux/types.h> | |
20 | #include <linux/kernel.h> | |
21 | #include <linux/jiffies.h> | |
22 | #include <linux/mm.h> | |
23 | #include <linux/string.h> | |
24 | #include <linux/socket.h> | |
25 | #include <linux/sockios.h> | |
26 | #include <linux/errno.h> | |
27 | #include <linux/in.h> | |
28 | #include <linux/inet.h> | |
14c85021 | 29 | #include <linux/inetdevice.h> |
1da177e4 LT |
30 | #include <linux/netdevice.h> |
31 | #include <linux/if_arp.h> | |
32 | #include <linux/proc_fs.h> | |
33 | #include <linux/skbuff.h> | |
1da177e4 | 34 | #include <linux/init.h> |
5a0e3ad6 | 35 | #include <linux/slab.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> |
1da177e4 LT |
46 | |
47 | #include "fib_lookup.h" | |
48 | ||
832b4c5e | 49 | static DEFINE_SPINLOCK(fib_info_lock); |
1da177e4 LT |
50 | static struct hlist_head *fib_info_hash; |
51 | static struct hlist_head *fib_info_laddrhash; | |
52 | static unsigned int fib_hash_size; | |
53 | static unsigned int fib_info_cnt; | |
54 | ||
55 | #define DEVINDEX_HASHBITS 8 | |
56 | #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS) | |
57 | static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE]; | |
58 | ||
59 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
60 | ||
61 | static DEFINE_SPINLOCK(fib_multipath_lock); | |
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 | ||
e905a9ed | 93 | static const struct |
1da177e4 LT |
94 | { |
95 | int error; | |
96 | u8 scope; | |
a0ee18b9 | 97 | } fib_props[RTN_MAX + 1] = { |
6a31d2a9 | 98 | [RTN_UNSPEC] = { |
1da177e4 LT |
99 | .error = 0, |
100 | .scope = RT_SCOPE_NOWHERE, | |
6a31d2a9 ED |
101 | }, |
102 | [RTN_UNICAST] = { | |
1da177e4 LT |
103 | .error = 0, |
104 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
105 | }, |
106 | [RTN_LOCAL] = { | |
1da177e4 LT |
107 | .error = 0, |
108 | .scope = RT_SCOPE_HOST, | |
6a31d2a9 ED |
109 | }, |
110 | [RTN_BROADCAST] = { | |
1da177e4 LT |
111 | .error = 0, |
112 | .scope = RT_SCOPE_LINK, | |
6a31d2a9 ED |
113 | }, |
114 | [RTN_ANYCAST] = { | |
1da177e4 LT |
115 | .error = 0, |
116 | .scope = RT_SCOPE_LINK, | |
6a31d2a9 ED |
117 | }, |
118 | [RTN_MULTICAST] = { | |
1da177e4 LT |
119 | .error = 0, |
120 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
121 | }, |
122 | [RTN_BLACKHOLE] = { | |
1da177e4 LT |
123 | .error = -EINVAL, |
124 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
125 | }, |
126 | [RTN_UNREACHABLE] = { | |
1da177e4 LT |
127 | .error = -EHOSTUNREACH, |
128 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
129 | }, |
130 | [RTN_PROHIBIT] = { | |
1da177e4 LT |
131 | .error = -EACCES, |
132 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
133 | }, |
134 | [RTN_THROW] = { | |
1da177e4 LT |
135 | .error = -EAGAIN, |
136 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
137 | }, |
138 | [RTN_NAT] = { | |
1da177e4 LT |
139 | .error = -EINVAL, |
140 | .scope = RT_SCOPE_NOWHERE, | |
6a31d2a9 ED |
141 | }, |
142 | [RTN_XRESOLVE] = { | |
1da177e4 LT |
143 | .error = -EINVAL, |
144 | .scope = RT_SCOPE_NOWHERE, | |
6a31d2a9 | 145 | }, |
1da177e4 LT |
146 | }; |
147 | ||
148 | ||
149 | /* Release a nexthop info record */ | |
150 | ||
ebc0ffae ED |
151 | static void free_fib_info_rcu(struct rcu_head *head) |
152 | { | |
153 | struct fib_info *fi = container_of(head, struct fib_info, rcu); | |
154 | ||
9c150e82 | 155 | kfree(fi->fib_metrics); |
ebc0ffae ED |
156 | kfree(fi); |
157 | } | |
158 | ||
1da177e4 LT |
159 | void free_fib_info(struct fib_info *fi) |
160 | { | |
161 | if (fi->fib_dead == 0) { | |
6a31d2a9 | 162 | pr_warning("Freeing alive fib_info %p\n", fi); |
1da177e4 LT |
163 | return; |
164 | } | |
165 | change_nexthops(fi) { | |
71fceff0 DM |
166 | if (nexthop_nh->nh_dev) |
167 | dev_put(nexthop_nh->nh_dev); | |
168 | nexthop_nh->nh_dev = NULL; | |
1da177e4 LT |
169 | } endfor_nexthops(fi); |
170 | fib_info_cnt--; | |
57d7a600 | 171 | release_net(fi->fib_net); |
ebc0ffae | 172 | call_rcu(&fi->rcu, free_fib_info_rcu); |
1da177e4 LT |
173 | } |
174 | ||
175 | void fib_release_info(struct fib_info *fi) | |
176 | { | |
832b4c5e | 177 | spin_lock_bh(&fib_info_lock); |
1da177e4 LT |
178 | if (fi && --fi->fib_treeref == 0) { |
179 | hlist_del(&fi->fib_hash); | |
180 | if (fi->fib_prefsrc) | |
181 | hlist_del(&fi->fib_lhash); | |
182 | change_nexthops(fi) { | |
71fceff0 | 183 | if (!nexthop_nh->nh_dev) |
1da177e4 | 184 | continue; |
71fceff0 | 185 | hlist_del(&nexthop_nh->nh_hash); |
1da177e4 LT |
186 | } endfor_nexthops(fi) |
187 | fi->fib_dead = 1; | |
188 | fib_info_put(fi); | |
189 | } | |
832b4c5e | 190 | spin_unlock_bh(&fib_info_lock); |
1da177e4 LT |
191 | } |
192 | ||
6a31d2a9 | 193 | static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi) |
1da177e4 LT |
194 | { |
195 | const struct fib_nh *onh = ofi->fib_nh; | |
196 | ||
197 | for_nexthops(fi) { | |
198 | if (nh->nh_oif != onh->nh_oif || | |
199 | nh->nh_gw != onh->nh_gw || | |
200 | nh->nh_scope != onh->nh_scope || | |
201 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
202 | nh->nh_weight != onh->nh_weight || | |
203 | #endif | |
c7066f70 | 204 | #ifdef CONFIG_IP_ROUTE_CLASSID |
1da177e4 LT |
205 | nh->nh_tclassid != onh->nh_tclassid || |
206 | #endif | |
6a31d2a9 | 207 | ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD)) |
1da177e4 LT |
208 | return -1; |
209 | onh++; | |
210 | } endfor_nexthops(fi); | |
211 | return 0; | |
212 | } | |
213 | ||
88ebc72f DM |
214 | static inline unsigned int fib_devindex_hashfn(unsigned int val) |
215 | { | |
216 | unsigned int mask = DEVINDEX_HASHSIZE - 1; | |
217 | ||
218 | return (val ^ | |
219 | (val >> DEVINDEX_HASHBITS) ^ | |
220 | (val >> (DEVINDEX_HASHBITS * 2))) & mask; | |
221 | } | |
222 | ||
1da177e4 LT |
223 | static inline unsigned int fib_info_hashfn(const struct fib_info *fi) |
224 | { | |
225 | unsigned int mask = (fib_hash_size - 1); | |
226 | unsigned int val = fi->fib_nhs; | |
227 | ||
228 | val ^= fi->fib_protocol; | |
81f7bf6c | 229 | val ^= (__force u32)fi->fib_prefsrc; |
1da177e4 | 230 | val ^= fi->fib_priority; |
88ebc72f DM |
231 | for_nexthops(fi) { |
232 | val ^= fib_devindex_hashfn(nh->nh_oif); | |
233 | } endfor_nexthops(fi) | |
1da177e4 LT |
234 | |
235 | return (val ^ (val >> 7) ^ (val >> 12)) & mask; | |
236 | } | |
237 | ||
238 | static struct fib_info *fib_find_info(const struct fib_info *nfi) | |
239 | { | |
240 | struct hlist_head *head; | |
241 | struct hlist_node *node; | |
242 | struct fib_info *fi; | |
243 | unsigned int hash; | |
244 | ||
245 | hash = fib_info_hashfn(nfi); | |
246 | head = &fib_info_hash[hash]; | |
247 | ||
248 | hlist_for_each_entry(fi, node, head, fib_hash) { | |
09ad9bc7 | 249 | if (!net_eq(fi->fib_net, nfi->fib_net)) |
4814bdbd | 250 | continue; |
1da177e4 LT |
251 | if (fi->fib_nhs != nfi->fib_nhs) |
252 | continue; | |
253 | if (nfi->fib_protocol == fi->fib_protocol && | |
254 | nfi->fib_prefsrc == fi->fib_prefsrc && | |
255 | nfi->fib_priority == fi->fib_priority && | |
256 | memcmp(nfi->fib_metrics, fi->fib_metrics, | |
257 | sizeof(fi->fib_metrics)) == 0 && | |
6a31d2a9 | 258 | ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 && |
1da177e4 LT |
259 | (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0)) |
260 | return fi; | |
261 | } | |
262 | ||
263 | return NULL; | |
264 | } | |
265 | ||
1da177e4 | 266 | /* Check, that the gateway is already configured. |
6a31d2a9 | 267 | * Used only by redirect accept routine. |
1da177e4 | 268 | */ |
d878e72e | 269 | int ip_fib_check_default(__be32 gw, struct net_device *dev) |
1da177e4 LT |
270 | { |
271 | struct hlist_head *head; | |
272 | struct hlist_node *node; | |
273 | struct fib_nh *nh; | |
274 | unsigned int hash; | |
275 | ||
832b4c5e | 276 | spin_lock(&fib_info_lock); |
1da177e4 LT |
277 | |
278 | hash = fib_devindex_hashfn(dev->ifindex); | |
279 | head = &fib_info_devhash[hash]; | |
280 | hlist_for_each_entry(nh, node, head, nh_hash) { | |
281 | if (nh->nh_dev == dev && | |
282 | nh->nh_gw == gw && | |
6a31d2a9 | 283 | !(nh->nh_flags & RTNH_F_DEAD)) { |
832b4c5e | 284 | spin_unlock(&fib_info_lock); |
1da177e4 LT |
285 | return 0; |
286 | } | |
287 | } | |
288 | ||
832b4c5e | 289 | spin_unlock(&fib_info_lock); |
1da177e4 LT |
290 | |
291 | return -1; | |
292 | } | |
293 | ||
339bf98f TG |
294 | static inline size_t fib_nlmsg_size(struct fib_info *fi) |
295 | { | |
296 | size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg)) | |
297 | + nla_total_size(4) /* RTA_TABLE */ | |
298 | + nla_total_size(4) /* RTA_DST */ | |
299 | + nla_total_size(4) /* RTA_PRIORITY */ | |
300 | + nla_total_size(4); /* RTA_PREFSRC */ | |
301 | ||
302 | /* space for nested metrics */ | |
303 | payload += nla_total_size((RTAX_MAX * nla_total_size(4))); | |
304 | ||
305 | if (fi->fib_nhs) { | |
306 | /* Also handles the special case fib_nhs == 1 */ | |
307 | ||
308 | /* each nexthop is packed in an attribute */ | |
309 | size_t nhsize = nla_total_size(sizeof(struct rtnexthop)); | |
310 | ||
311 | /* may contain flow and gateway attribute */ | |
312 | nhsize += 2 * nla_total_size(4); | |
313 | ||
314 | /* all nexthops are packed in a nested attribute */ | |
315 | payload += nla_total_size(fi->fib_nhs * nhsize); | |
316 | } | |
317 | ||
318 | return payload; | |
319 | } | |
320 | ||
81f7bf6c | 321 | void rtmsg_fib(int event, __be32 key, struct fib_alias *fa, |
b8f55831 MK |
322 | int dst_len, u32 tb_id, struct nl_info *info, |
323 | unsigned int nlm_flags) | |
1da177e4 LT |
324 | { |
325 | struct sk_buff *skb; | |
4e902c57 | 326 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
f21c7bc5 | 327 | int err = -ENOBUFS; |
1da177e4 | 328 | |
339bf98f | 329 | skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL); |
f21c7bc5 TG |
330 | if (skb == NULL) |
331 | goto errout; | |
1da177e4 | 332 | |
4e902c57 | 333 | err = fib_dump_info(skb, info->pid, seq, event, tb_id, |
be403ea1 | 334 | fa->fa_type, fa->fa_scope, key, dst_len, |
b8f55831 | 335 | fa->fa_tos, fa->fa_info, nlm_flags); |
26932566 PM |
336 | if (err < 0) { |
337 | /* -EMSGSIZE implies BUG in fib_nlmsg_size() */ | |
338 | WARN_ON(err == -EMSGSIZE); | |
339 | kfree_skb(skb); | |
340 | goto errout; | |
341 | } | |
1ce85fe4 PNA |
342 | rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE, |
343 | info->nlh, GFP_KERNEL); | |
344 | return; | |
f21c7bc5 TG |
345 | errout: |
346 | if (err < 0) | |
4d1169c1 | 347 | rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err); |
1da177e4 LT |
348 | } |
349 | ||
350 | /* Return the first fib alias matching TOS with | |
351 | * priority less than or equal to PRIO. | |
352 | */ | |
353 | struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio) | |
354 | { | |
355 | if (fah) { | |
356 | struct fib_alias *fa; | |
357 | list_for_each_entry(fa, fah, fa_list) { | |
358 | if (fa->fa_tos > tos) | |
359 | continue; | |
360 | if (fa->fa_info->fib_priority >= prio || | |
361 | fa->fa_tos < tos) | |
362 | return fa; | |
363 | } | |
364 | } | |
365 | return NULL; | |
366 | } | |
367 | ||
368 | int fib_detect_death(struct fib_info *fi, int order, | |
c17860a0 | 369 | struct fib_info **last_resort, int *last_idx, int dflt) |
1da177e4 LT |
370 | { |
371 | struct neighbour *n; | |
372 | int state = NUD_NONE; | |
373 | ||
374 | n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev); | |
375 | if (n) { | |
376 | state = n->nud_state; | |
377 | neigh_release(n); | |
378 | } | |
d9319100 | 379 | if (state == NUD_REACHABLE) |
1da177e4 | 380 | return 0; |
6a31d2a9 | 381 | if ((state & NUD_VALID) && order != dflt) |
1da177e4 | 382 | return 0; |
6a31d2a9 ED |
383 | if ((state & NUD_VALID) || |
384 | (*last_idx < 0 && order > dflt)) { | |
1da177e4 LT |
385 | *last_resort = fi; |
386 | *last_idx = order; | |
387 | } | |
388 | return 1; | |
389 | } | |
390 | ||
391 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
392 | ||
4e902c57 | 393 | static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining) |
1da177e4 LT |
394 | { |
395 | int nhs = 0; | |
1da177e4 | 396 | |
4e902c57 | 397 | while (rtnh_ok(rtnh, remaining)) { |
1da177e4 | 398 | nhs++; |
4e902c57 TG |
399 | rtnh = rtnh_next(rtnh, &remaining); |
400 | } | |
401 | ||
402 | /* leftover implies invalid nexthop configuration, discard it */ | |
403 | return remaining > 0 ? 0 : nhs; | |
1da177e4 LT |
404 | } |
405 | ||
4e902c57 TG |
406 | static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh, |
407 | int remaining, struct fib_config *cfg) | |
1da177e4 | 408 | { |
1da177e4 | 409 | change_nexthops(fi) { |
4e902c57 TG |
410 | int attrlen; |
411 | ||
412 | if (!rtnh_ok(rtnh, remaining)) | |
1da177e4 | 413 | return -EINVAL; |
4e902c57 | 414 | |
71fceff0 DM |
415 | nexthop_nh->nh_flags = |
416 | (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags; | |
417 | nexthop_nh->nh_oif = rtnh->rtnh_ifindex; | |
418 | nexthop_nh->nh_weight = rtnh->rtnh_hops + 1; | |
4e902c57 TG |
419 | |
420 | attrlen = rtnh_attrlen(rtnh); | |
421 | if (attrlen > 0) { | |
422 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); | |
423 | ||
424 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); | |
71fceff0 | 425 | nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0; |
c7066f70 | 426 | #ifdef CONFIG_IP_ROUTE_CLASSID |
4e902c57 | 427 | nla = nla_find(attrs, attrlen, RTA_FLOW); |
71fceff0 | 428 | nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0; |
1da177e4 LT |
429 | #endif |
430 | } | |
4e902c57 TG |
431 | |
432 | rtnh = rtnh_next(rtnh, &remaining); | |
1da177e4 | 433 | } endfor_nexthops(fi); |
4e902c57 | 434 | |
1da177e4 LT |
435 | return 0; |
436 | } | |
437 | ||
438 | #endif | |
439 | ||
4e902c57 | 440 | int fib_nh_match(struct fib_config *cfg, struct fib_info *fi) |
1da177e4 LT |
441 | { |
442 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
4e902c57 TG |
443 | struct rtnexthop *rtnh; |
444 | int remaining; | |
1da177e4 LT |
445 | #endif |
446 | ||
4e902c57 | 447 | if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority) |
1da177e4 LT |
448 | return 1; |
449 | ||
4e902c57 TG |
450 | if (cfg->fc_oif || cfg->fc_gw) { |
451 | if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) && | |
452 | (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw)) | |
1da177e4 LT |
453 | return 0; |
454 | return 1; | |
455 | } | |
456 | ||
457 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
4e902c57 | 458 | if (cfg->fc_mp == NULL) |
1da177e4 | 459 | return 0; |
4e902c57 TG |
460 | |
461 | rtnh = cfg->fc_mp; | |
462 | remaining = cfg->fc_mp_len; | |
e905a9ed | 463 | |
1da177e4 | 464 | for_nexthops(fi) { |
4e902c57 | 465 | int attrlen; |
1da177e4 | 466 | |
4e902c57 | 467 | if (!rtnh_ok(rtnh, remaining)) |
1da177e4 | 468 | return -EINVAL; |
4e902c57 TG |
469 | |
470 | if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif) | |
1da177e4 | 471 | return 1; |
4e902c57 TG |
472 | |
473 | attrlen = rtnh_attrlen(rtnh); | |
474 | if (attrlen < 0) { | |
475 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); | |
476 | ||
477 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); | |
17fb2c64 | 478 | if (nla && nla_get_be32(nla) != nh->nh_gw) |
1da177e4 | 479 | return 1; |
c7066f70 | 480 | #ifdef CONFIG_IP_ROUTE_CLASSID |
4e902c57 TG |
481 | nla = nla_find(attrs, attrlen, RTA_FLOW); |
482 | if (nla && nla_get_u32(nla) != nh->nh_tclassid) | |
1da177e4 LT |
483 | return 1; |
484 | #endif | |
485 | } | |
4e902c57 TG |
486 | |
487 | rtnh = rtnh_next(rtnh, &remaining); | |
1da177e4 LT |
488 | } endfor_nexthops(fi); |
489 | #endif | |
490 | return 0; | |
491 | } | |
492 | ||
493 | ||
494 | /* | |
6a31d2a9 ED |
495 | * Picture |
496 | * ------- | |
497 | * | |
498 | * Semantics of nexthop is very messy by historical reasons. | |
499 | * We have to take into account, that: | |
500 | * a) gateway can be actually local interface address, | |
501 | * so that gatewayed route is direct. | |
502 | * b) gateway must be on-link address, possibly | |
503 | * described not by an ifaddr, but also by a direct route. | |
504 | * c) If both gateway and interface are specified, they should not | |
505 | * contradict. | |
506 | * d) If we use tunnel routes, gateway could be not on-link. | |
507 | * | |
508 | * Attempt to reconcile all of these (alas, self-contradictory) conditions | |
509 | * results in pretty ugly and hairy code with obscure logic. | |
510 | * | |
511 | * I chose to generalized it instead, so that the size | |
512 | * of code does not increase practically, but it becomes | |
513 | * much more general. | |
514 | * Every prefix is assigned a "scope" value: "host" is local address, | |
515 | * "link" is direct route, | |
516 | * [ ... "site" ... "interior" ... ] | |
517 | * and "universe" is true gateway route with global meaning. | |
518 | * | |
519 | * Every prefix refers to a set of "nexthop"s (gw, oif), | |
520 | * where gw must have narrower scope. This recursion stops | |
521 | * when gw has LOCAL scope or if "nexthop" is declared ONLINK, | |
522 | * which means that gw is forced to be on link. | |
523 | * | |
524 | * Code is still hairy, but now it is apparently logically | |
525 | * consistent and very flexible. F.e. as by-product it allows | |
526 | * to co-exists in peace independent exterior and interior | |
527 | * routing processes. | |
528 | * | |
529 | * Normally it looks as following. | |
530 | * | |
531 | * {universe prefix} -> (gw, oif) [scope link] | |
532 | * | | |
533 | * |-> {link prefix} -> (gw, oif) [scope local] | |
534 | * | | |
535 | * |-> {local prefix} (terminal node) | |
1da177e4 | 536 | */ |
4e902c57 TG |
537 | static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi, |
538 | struct fib_nh *nh) | |
1da177e4 LT |
539 | { |
540 | int err; | |
86167a37 | 541 | struct net *net; |
6a31d2a9 | 542 | struct net_device *dev; |
1da177e4 | 543 | |
86167a37 | 544 | net = cfg->fc_nlinfo.nl_net; |
1da177e4 LT |
545 | if (nh->nh_gw) { |
546 | struct fib_result res; | |
547 | ||
6a31d2a9 | 548 | if (nh->nh_flags & RTNH_F_ONLINK) { |
1da177e4 | 549 | |
4e902c57 | 550 | if (cfg->fc_scope >= RT_SCOPE_LINK) |
1da177e4 | 551 | return -EINVAL; |
86167a37 | 552 | if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST) |
1da177e4 | 553 | return -EINVAL; |
6a31d2a9 ED |
554 | dev = __dev_get_by_index(net, nh->nh_oif); |
555 | if (!dev) | |
1da177e4 | 556 | return -ENODEV; |
6a31d2a9 | 557 | if (!(dev->flags & IFF_UP)) |
1da177e4 LT |
558 | return -ENETDOWN; |
559 | nh->nh_dev = dev; | |
560 | dev_hold(dev); | |
561 | nh->nh_scope = RT_SCOPE_LINK; | |
562 | return 0; | |
563 | } | |
ebc0ffae | 564 | rcu_read_lock(); |
1da177e4 | 565 | { |
4e902c57 | 566 | struct flowi fl = { |
5811662b CG |
567 | .fl4_dst = nh->nh_gw, |
568 | .fl4_scope = cfg->fc_scope + 1, | |
4e902c57 TG |
569 | .oif = nh->nh_oif, |
570 | }; | |
1da177e4 LT |
571 | |
572 | /* It is not necessary, but requires a bit of thinking */ | |
573 | if (fl.fl4_scope < RT_SCOPE_LINK) | |
574 | fl.fl4_scope = RT_SCOPE_LINK; | |
6a31d2a9 | 575 | err = fib_lookup(net, &fl, &res); |
ebc0ffae ED |
576 | if (err) { |
577 | rcu_read_unlock(); | |
1da177e4 | 578 | return err; |
ebc0ffae | 579 | } |
1da177e4 LT |
580 | } |
581 | err = -EINVAL; | |
582 | if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) | |
583 | goto out; | |
584 | nh->nh_scope = res.scope; | |
585 | nh->nh_oif = FIB_RES_OIF(res); | |
6a31d2a9 ED |
586 | nh->nh_dev = dev = FIB_RES_DEV(res); |
587 | if (!dev) | |
1da177e4 | 588 | goto out; |
6a31d2a9 | 589 | dev_hold(dev); |
8723e1b4 | 590 | err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN; |
1da177e4 LT |
591 | } else { |
592 | struct in_device *in_dev; | |
593 | ||
6a31d2a9 | 594 | if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) |
1da177e4 LT |
595 | return -EINVAL; |
596 | ||
8723e1b4 ED |
597 | rcu_read_lock(); |
598 | err = -ENODEV; | |
86167a37 | 599 | in_dev = inetdev_by_index(net, nh->nh_oif); |
1da177e4 | 600 | if (in_dev == NULL) |
8723e1b4 ED |
601 | goto out; |
602 | err = -ENETDOWN; | |
603 | if (!(in_dev->dev->flags & IFF_UP)) | |
604 | goto out; | |
1da177e4 LT |
605 | nh->nh_dev = in_dev->dev; |
606 | dev_hold(nh->nh_dev); | |
607 | nh->nh_scope = RT_SCOPE_HOST; | |
8723e1b4 | 608 | err = 0; |
1da177e4 | 609 | } |
8723e1b4 ED |
610 | out: |
611 | rcu_read_unlock(); | |
612 | return err; | |
1da177e4 LT |
613 | } |
614 | ||
81f7bf6c | 615 | static inline unsigned int fib_laddr_hashfn(__be32 val) |
1da177e4 LT |
616 | { |
617 | unsigned int mask = (fib_hash_size - 1); | |
618 | ||
6a31d2a9 ED |
619 | return ((__force u32)val ^ |
620 | ((__force u32)val >> 7) ^ | |
621 | ((__force u32)val >> 14)) & mask; | |
1da177e4 LT |
622 | } |
623 | ||
624 | static struct hlist_head *fib_hash_alloc(int bytes) | |
625 | { | |
626 | if (bytes <= PAGE_SIZE) | |
88f83491 | 627 | return kzalloc(bytes, GFP_KERNEL); |
1da177e4 LT |
628 | else |
629 | return (struct hlist_head *) | |
6a31d2a9 ED |
630 | __get_free_pages(GFP_KERNEL | __GFP_ZERO, |
631 | get_order(bytes)); | |
1da177e4 LT |
632 | } |
633 | ||
634 | static void fib_hash_free(struct hlist_head *hash, int bytes) | |
635 | { | |
636 | if (!hash) | |
637 | return; | |
638 | ||
639 | if (bytes <= PAGE_SIZE) | |
640 | kfree(hash); | |
641 | else | |
642 | free_pages((unsigned long) hash, get_order(bytes)); | |
643 | } | |
644 | ||
645 | static void fib_hash_move(struct hlist_head *new_info_hash, | |
646 | struct hlist_head *new_laddrhash, | |
647 | unsigned int new_size) | |
648 | { | |
b7656e7f | 649 | struct hlist_head *old_info_hash, *old_laddrhash; |
1da177e4 | 650 | unsigned int old_size = fib_hash_size; |
b7656e7f | 651 | unsigned int i, bytes; |
1da177e4 | 652 | |
832b4c5e | 653 | spin_lock_bh(&fib_info_lock); |
b7656e7f DM |
654 | old_info_hash = fib_info_hash; |
655 | old_laddrhash = fib_info_laddrhash; | |
1da177e4 LT |
656 | fib_hash_size = new_size; |
657 | ||
658 | for (i = 0; i < old_size; i++) { | |
659 | struct hlist_head *head = &fib_info_hash[i]; | |
660 | struct hlist_node *node, *n; | |
661 | struct fib_info *fi; | |
662 | ||
663 | hlist_for_each_entry_safe(fi, node, n, head, fib_hash) { | |
664 | struct hlist_head *dest; | |
665 | unsigned int new_hash; | |
666 | ||
667 | hlist_del(&fi->fib_hash); | |
668 | ||
669 | new_hash = fib_info_hashfn(fi); | |
670 | dest = &new_info_hash[new_hash]; | |
671 | hlist_add_head(&fi->fib_hash, dest); | |
672 | } | |
673 | } | |
674 | fib_info_hash = new_info_hash; | |
675 | ||
676 | for (i = 0; i < old_size; i++) { | |
677 | struct hlist_head *lhead = &fib_info_laddrhash[i]; | |
678 | struct hlist_node *node, *n; | |
679 | struct fib_info *fi; | |
680 | ||
681 | hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) { | |
682 | struct hlist_head *ldest; | |
683 | unsigned int new_hash; | |
684 | ||
685 | hlist_del(&fi->fib_lhash); | |
686 | ||
687 | new_hash = fib_laddr_hashfn(fi->fib_prefsrc); | |
688 | ldest = &new_laddrhash[new_hash]; | |
689 | hlist_add_head(&fi->fib_lhash, ldest); | |
690 | } | |
691 | } | |
692 | fib_info_laddrhash = new_laddrhash; | |
693 | ||
832b4c5e | 694 | spin_unlock_bh(&fib_info_lock); |
b7656e7f DM |
695 | |
696 | bytes = old_size * sizeof(struct hlist_head *); | |
697 | fib_hash_free(old_info_hash, bytes); | |
698 | fib_hash_free(old_laddrhash, bytes); | |
1da177e4 LT |
699 | } |
700 | ||
4e902c57 | 701 | struct fib_info *fib_create_info(struct fib_config *cfg) |
1da177e4 LT |
702 | { |
703 | int err; | |
704 | struct fib_info *fi = NULL; | |
705 | struct fib_info *ofi; | |
1da177e4 | 706 | int nhs = 1; |
7462bd74 | 707 | struct net *net = cfg->fc_nlinfo.nl_net; |
1da177e4 LT |
708 | |
709 | /* Fast check to catch the most weird cases */ | |
4e902c57 | 710 | if (fib_props[cfg->fc_type].scope > cfg->fc_scope) |
1da177e4 LT |
711 | goto err_inval; |
712 | ||
713 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
4e902c57 TG |
714 | if (cfg->fc_mp) { |
715 | nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len); | |
1da177e4 LT |
716 | if (nhs == 0) |
717 | goto err_inval; | |
718 | } | |
719 | #endif | |
1da177e4 LT |
720 | |
721 | err = -ENOBUFS; | |
722 | if (fib_info_cnt >= fib_hash_size) { | |
723 | unsigned int new_size = fib_hash_size << 1; | |
724 | struct hlist_head *new_info_hash; | |
725 | struct hlist_head *new_laddrhash; | |
726 | unsigned int bytes; | |
727 | ||
728 | if (!new_size) | |
729 | new_size = 1; | |
730 | bytes = new_size * sizeof(struct hlist_head *); | |
731 | new_info_hash = fib_hash_alloc(bytes); | |
732 | new_laddrhash = fib_hash_alloc(bytes); | |
733 | if (!new_info_hash || !new_laddrhash) { | |
734 | fib_hash_free(new_info_hash, bytes); | |
735 | fib_hash_free(new_laddrhash, bytes); | |
88f83491 | 736 | } else |
1da177e4 | 737 | fib_hash_move(new_info_hash, new_laddrhash, new_size); |
1da177e4 LT |
738 | |
739 | if (!fib_hash_size) | |
740 | goto failure; | |
741 | } | |
742 | ||
0da974f4 | 743 | fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL); |
1da177e4 LT |
744 | if (fi == NULL) |
745 | goto failure; | |
9c150e82 DM |
746 | fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); |
747 | if (!fi->fib_metrics) | |
748 | goto failure; | |
1da177e4 | 749 | fib_info_cnt++; |
1da177e4 | 750 | |
57d7a600 | 751 | fi->fib_net = hold_net(net); |
4e902c57 TG |
752 | fi->fib_protocol = cfg->fc_protocol; |
753 | fi->fib_flags = cfg->fc_flags; | |
754 | fi->fib_priority = cfg->fc_priority; | |
755 | fi->fib_prefsrc = cfg->fc_prefsrc; | |
1da177e4 LT |
756 | |
757 | fi->fib_nhs = nhs; | |
758 | change_nexthops(fi) { | |
71fceff0 | 759 | nexthop_nh->nh_parent = fi; |
1da177e4 LT |
760 | } endfor_nexthops(fi) |
761 | ||
4e902c57 TG |
762 | if (cfg->fc_mx) { |
763 | struct nlattr *nla; | |
764 | int remaining; | |
765 | ||
766 | nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { | |
8f4c1f9b | 767 | int type = nla_type(nla); |
4e902c57 TG |
768 | |
769 | if (type) { | |
770 | if (type > RTAX_MAX) | |
1da177e4 | 771 | goto err_inval; |
4e902c57 | 772 | fi->fib_metrics[type - 1] = nla_get_u32(nla); |
1da177e4 | 773 | } |
1da177e4 LT |
774 | } |
775 | } | |
1da177e4 | 776 | |
4e902c57 | 777 | if (cfg->fc_mp) { |
1da177e4 | 778 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
4e902c57 TG |
779 | err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg); |
780 | if (err != 0) | |
1da177e4 | 781 | goto failure; |
4e902c57 | 782 | if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif) |
1da177e4 | 783 | goto err_inval; |
4e902c57 | 784 | if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw) |
1da177e4 | 785 | goto err_inval; |
c7066f70 | 786 | #ifdef CONFIG_IP_ROUTE_CLASSID |
4e902c57 | 787 | if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) |
1da177e4 LT |
788 | goto err_inval; |
789 | #endif | |
790 | #else | |
791 | goto err_inval; | |
792 | #endif | |
793 | } else { | |
794 | struct fib_nh *nh = fi->fib_nh; | |
4e902c57 TG |
795 | |
796 | nh->nh_oif = cfg->fc_oif; | |
797 | nh->nh_gw = cfg->fc_gw; | |
798 | nh->nh_flags = cfg->fc_flags; | |
c7066f70 | 799 | #ifdef CONFIG_IP_ROUTE_CLASSID |
4e902c57 | 800 | nh->nh_tclassid = cfg->fc_flow; |
1da177e4 | 801 | #endif |
1da177e4 LT |
802 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
803 | nh->nh_weight = 1; | |
804 | #endif | |
805 | } | |
806 | ||
4e902c57 TG |
807 | if (fib_props[cfg->fc_type].error) { |
808 | if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) | |
1da177e4 LT |
809 | goto err_inval; |
810 | goto link_it; | |
811 | } | |
812 | ||
4e902c57 | 813 | if (cfg->fc_scope > RT_SCOPE_HOST) |
1da177e4 LT |
814 | goto err_inval; |
815 | ||
4e902c57 | 816 | if (cfg->fc_scope == RT_SCOPE_HOST) { |
1da177e4 LT |
817 | struct fib_nh *nh = fi->fib_nh; |
818 | ||
819 | /* Local address is added. */ | |
820 | if (nhs != 1 || nh->nh_gw) | |
821 | goto err_inval; | |
822 | nh->nh_scope = RT_SCOPE_NOWHERE; | |
7462bd74 | 823 | nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif); |
1da177e4 LT |
824 | err = -ENODEV; |
825 | if (nh->nh_dev == NULL) | |
826 | goto failure; | |
827 | } else { | |
828 | change_nexthops(fi) { | |
6a31d2a9 ED |
829 | err = fib_check_nh(cfg, fi, nexthop_nh); |
830 | if (err != 0) | |
1da177e4 LT |
831 | goto failure; |
832 | } endfor_nexthops(fi) | |
833 | } | |
834 | ||
835 | if (fi->fib_prefsrc) { | |
4e902c57 TG |
836 | if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst || |
837 | fi->fib_prefsrc != cfg->fc_dst) | |
7462bd74 | 838 | if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL) |
1da177e4 LT |
839 | goto err_inval; |
840 | } | |
841 | ||
842 | link_it: | |
6a31d2a9 ED |
843 | ofi = fib_find_info(fi); |
844 | if (ofi) { | |
1da177e4 LT |
845 | fi->fib_dead = 1; |
846 | free_fib_info(fi); | |
847 | ofi->fib_treeref++; | |
848 | return ofi; | |
849 | } | |
850 | ||
851 | fi->fib_treeref++; | |
852 | atomic_inc(&fi->fib_clntref); | |
832b4c5e | 853 | spin_lock_bh(&fib_info_lock); |
1da177e4 LT |
854 | hlist_add_head(&fi->fib_hash, |
855 | &fib_info_hash[fib_info_hashfn(fi)]); | |
856 | if (fi->fib_prefsrc) { | |
857 | struct hlist_head *head; | |
858 | ||
859 | head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)]; | |
860 | hlist_add_head(&fi->fib_lhash, head); | |
861 | } | |
862 | change_nexthops(fi) { | |
863 | struct hlist_head *head; | |
864 | unsigned int hash; | |
865 | ||
71fceff0 | 866 | if (!nexthop_nh->nh_dev) |
1da177e4 | 867 | continue; |
71fceff0 | 868 | hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex); |
1da177e4 | 869 | head = &fib_info_devhash[hash]; |
71fceff0 | 870 | hlist_add_head(&nexthop_nh->nh_hash, head); |
1da177e4 | 871 | } endfor_nexthops(fi) |
832b4c5e | 872 | spin_unlock_bh(&fib_info_lock); |
1da177e4 LT |
873 | return fi; |
874 | ||
875 | err_inval: | |
876 | err = -EINVAL; | |
877 | ||
878 | failure: | |
e905a9ed | 879 | if (fi) { |
1da177e4 LT |
880 | fi->fib_dead = 1; |
881 | free_fib_info(fi); | |
882 | } | |
4e902c57 TG |
883 | |
884 | return ERR_PTR(err); | |
1da177e4 LT |
885 | } |
886 | ||
e5b43760 | 887 | /* Note! fib_semantic_match intentionally uses RCU list functions. */ |
1da177e4 | 888 | int fib_semantic_match(struct list_head *head, const struct flowi *flp, |
ebc0ffae | 889 | struct fib_result *res, int prefixlen, int fib_flags) |
1da177e4 LT |
890 | { |
891 | struct fib_alias *fa; | |
892 | int nh_sel = 0; | |
893 | ||
e5b43760 | 894 | list_for_each_entry_rcu(fa, head, fa_list) { |
1da177e4 LT |
895 | int err; |
896 | ||
897 | if (fa->fa_tos && | |
898 | fa->fa_tos != flp->fl4_tos) | |
899 | continue; | |
900 | ||
901 | if (fa->fa_scope < flp->fl4_scope) | |
902 | continue; | |
903 | ||
9b0c290e | 904 | fib_alias_accessed(fa); |
1da177e4 LT |
905 | |
906 | err = fib_props[fa->fa_type].error; | |
907 | if (err == 0) { | |
908 | struct fib_info *fi = fa->fa_info; | |
909 | ||
910 | if (fi->fib_flags & RTNH_F_DEAD) | |
911 | continue; | |
912 | ||
913 | switch (fa->fa_type) { | |
914 | case RTN_UNICAST: | |
915 | case RTN_LOCAL: | |
916 | case RTN_BROADCAST: | |
917 | case RTN_ANYCAST: | |
918 | case RTN_MULTICAST: | |
919 | for_nexthops(fi) { | |
6a31d2a9 | 920 | if (nh->nh_flags & RTNH_F_DEAD) |
1da177e4 LT |
921 | continue; |
922 | if (!flp->oif || flp->oif == nh->nh_oif) | |
923 | break; | |
924 | } | |
925 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
926 | if (nhsel < fi->fib_nhs) { | |
927 | nh_sel = nhsel; | |
928 | goto out_fill_res; | |
929 | } | |
930 | #else | |
6a31d2a9 | 931 | if (nhsel < 1) |
1da177e4 | 932 | goto out_fill_res; |
1da177e4 LT |
933 | #endif |
934 | endfor_nexthops(fi); | |
935 | continue; | |
936 | ||
937 | default: | |
6a31d2a9 ED |
938 | pr_warning("fib_semantic_match bad type %#x\n", |
939 | fa->fa_type); | |
1da177e4 | 940 | return -EINVAL; |
3ff50b79 | 941 | } |
1da177e4 LT |
942 | } |
943 | return err; | |
944 | } | |
945 | return 1; | |
946 | ||
947 | out_fill_res: | |
948 | res->prefixlen = prefixlen; | |
949 | res->nh_sel = nh_sel; | |
950 | res->type = fa->fa_type; | |
951 | res->scope = fa->fa_scope; | |
952 | res->fi = fa->fa_info; | |
ebc0ffae ED |
953 | if (!(fib_flags & FIB_LOOKUP_NOREF)) |
954 | atomic_inc(&res->fi->fib_clntref); | |
1da177e4 LT |
955 | return 0; |
956 | } | |
957 | ||
958 | /* Find appropriate source address to this destination */ | |
959 | ||
b83738ae | 960 | __be32 __fib_res_prefsrc(struct fib_result *res) |
1da177e4 LT |
961 | { |
962 | return inet_select_addr(FIB_RES_DEV(*res), FIB_RES_GW(*res), res->scope); | |
963 | } | |
964 | ||
be403ea1 | 965 | int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event, |
81f7bf6c | 966 | u32 tb_id, u8 type, u8 scope, __be32 dst, int dst_len, u8 tos, |
be403ea1 | 967 | struct fib_info *fi, unsigned int flags) |
1da177e4 | 968 | { |
be403ea1 | 969 | struct nlmsghdr *nlh; |
1da177e4 | 970 | struct rtmsg *rtm; |
1da177e4 | 971 | |
be403ea1 TG |
972 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags); |
973 | if (nlh == NULL) | |
26932566 | 974 | return -EMSGSIZE; |
be403ea1 TG |
975 | |
976 | rtm = nlmsg_data(nlh); | |
1da177e4 LT |
977 | rtm->rtm_family = AF_INET; |
978 | rtm->rtm_dst_len = dst_len; | |
979 | rtm->rtm_src_len = 0; | |
980 | rtm->rtm_tos = tos; | |
709772e6 KPO |
981 | if (tb_id < 256) |
982 | rtm->rtm_table = tb_id; | |
983 | else | |
984 | rtm->rtm_table = RT_TABLE_COMPAT; | |
be403ea1 | 985 | NLA_PUT_U32(skb, RTA_TABLE, tb_id); |
1da177e4 LT |
986 | rtm->rtm_type = type; |
987 | rtm->rtm_flags = fi->fib_flags; | |
988 | rtm->rtm_scope = scope; | |
1da177e4 | 989 | rtm->rtm_protocol = fi->fib_protocol; |
be403ea1 TG |
990 | |
991 | if (rtm->rtm_dst_len) | |
17fb2c64 | 992 | NLA_PUT_BE32(skb, RTA_DST, dst); |
be403ea1 | 993 | |
1da177e4 | 994 | if (fi->fib_priority) |
be403ea1 TG |
995 | NLA_PUT_U32(skb, RTA_PRIORITY, fi->fib_priority); |
996 | ||
1da177e4 | 997 | if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0) |
be403ea1 TG |
998 | goto nla_put_failure; |
999 | ||
1da177e4 | 1000 | if (fi->fib_prefsrc) |
17fb2c64 | 1001 | NLA_PUT_BE32(skb, RTA_PREFSRC, fi->fib_prefsrc); |
be403ea1 | 1002 | |
1da177e4 LT |
1003 | if (fi->fib_nhs == 1) { |
1004 | if (fi->fib_nh->nh_gw) | |
17fb2c64 | 1005 | NLA_PUT_BE32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw); |
be403ea1 | 1006 | |
1da177e4 | 1007 | if (fi->fib_nh->nh_oif) |
be403ea1 | 1008 | NLA_PUT_U32(skb, RTA_OIF, fi->fib_nh->nh_oif); |
c7066f70 | 1009 | #ifdef CONFIG_IP_ROUTE_CLASSID |
8265abc0 | 1010 | if (fi->fib_nh[0].nh_tclassid) |
be403ea1 | 1011 | NLA_PUT_U32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid); |
8265abc0 | 1012 | #endif |
1da177e4 LT |
1013 | } |
1014 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
1015 | if (fi->fib_nhs > 1) { | |
be403ea1 TG |
1016 | struct rtnexthop *rtnh; |
1017 | struct nlattr *mp; | |
1018 | ||
1019 | mp = nla_nest_start(skb, RTA_MULTIPATH); | |
1020 | if (mp == NULL) | |
1021 | goto nla_put_failure; | |
1da177e4 LT |
1022 | |
1023 | for_nexthops(fi) { | |
be403ea1 TG |
1024 | rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); |
1025 | if (rtnh == NULL) | |
1026 | goto nla_put_failure; | |
1027 | ||
1028 | rtnh->rtnh_flags = nh->nh_flags & 0xFF; | |
1029 | rtnh->rtnh_hops = nh->nh_weight - 1; | |
1030 | rtnh->rtnh_ifindex = nh->nh_oif; | |
1031 | ||
1da177e4 | 1032 | if (nh->nh_gw) |
17fb2c64 | 1033 | NLA_PUT_BE32(skb, RTA_GATEWAY, nh->nh_gw); |
c7066f70 | 1034 | #ifdef CONFIG_IP_ROUTE_CLASSID |
8265abc0 | 1035 | if (nh->nh_tclassid) |
be403ea1 | 1036 | NLA_PUT_U32(skb, RTA_FLOW, nh->nh_tclassid); |
8265abc0 | 1037 | #endif |
be403ea1 TG |
1038 | /* length of rtnetlink header + attributes */ |
1039 | rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh; | |
1da177e4 | 1040 | } endfor_nexthops(fi); |
be403ea1 TG |
1041 | |
1042 | nla_nest_end(skb, mp); | |
1da177e4 LT |
1043 | } |
1044 | #endif | |
be403ea1 | 1045 | return nlmsg_end(skb, nlh); |
1da177e4 | 1046 | |
be403ea1 | 1047 | nla_put_failure: |
26932566 PM |
1048 | nlmsg_cancel(skb, nlh); |
1049 | return -EMSGSIZE; | |
1da177e4 LT |
1050 | } |
1051 | ||
1da177e4 | 1052 | /* |
6a31d2a9 ED |
1053 | * Update FIB if: |
1054 | * - local address disappeared -> we must delete all the entries | |
1055 | * referring to it. | |
1056 | * - device went down -> we must shutdown all nexthops going via it. | |
1da177e4 | 1057 | */ |
4814bdbd | 1058 | int fib_sync_down_addr(struct net *net, __be32 local) |
1da177e4 LT |
1059 | { |
1060 | int ret = 0; | |
85326fa5 DL |
1061 | unsigned int hash = fib_laddr_hashfn(local); |
1062 | struct hlist_head *head = &fib_info_laddrhash[hash]; | |
1063 | struct hlist_node *node; | |
1064 | struct fib_info *fi; | |
1da177e4 | 1065 | |
85326fa5 DL |
1066 | if (fib_info_laddrhash == NULL || local == 0) |
1067 | return 0; | |
1da177e4 | 1068 | |
85326fa5 | 1069 | hlist_for_each_entry(fi, node, head, fib_lhash) { |
09ad9bc7 | 1070 | if (!net_eq(fi->fib_net, net)) |
4814bdbd | 1071 | continue; |
85326fa5 DL |
1072 | if (fi->fib_prefsrc == local) { |
1073 | fi->fib_flags |= RTNH_F_DEAD; | |
1074 | ret++; | |
1da177e4 LT |
1075 | } |
1076 | } | |
85326fa5 DL |
1077 | return ret; |
1078 | } | |
1079 | ||
1080 | int fib_sync_down_dev(struct net_device *dev, int force) | |
1081 | { | |
1082 | int ret = 0; | |
1083 | int scope = RT_SCOPE_NOWHERE; | |
1084 | struct fib_info *prev_fi = NULL; | |
1085 | unsigned int hash = fib_devindex_hashfn(dev->ifindex); | |
1086 | struct hlist_head *head = &fib_info_devhash[hash]; | |
1087 | struct hlist_node *node; | |
1088 | struct fib_nh *nh; | |
1da177e4 | 1089 | |
85326fa5 DL |
1090 | if (force) |
1091 | scope = -1; | |
1da177e4 | 1092 | |
85326fa5 DL |
1093 | hlist_for_each_entry(nh, node, head, nh_hash) { |
1094 | struct fib_info *fi = nh->nh_parent; | |
1095 | int dead; | |
1da177e4 | 1096 | |
85326fa5 DL |
1097 | BUG_ON(!fi->fib_nhs); |
1098 | if (nh->nh_dev != dev || fi == prev_fi) | |
1099 | continue; | |
1100 | prev_fi = fi; | |
1101 | dead = 0; | |
1102 | change_nexthops(fi) { | |
6a31d2a9 | 1103 | if (nexthop_nh->nh_flags & RTNH_F_DEAD) |
85326fa5 | 1104 | dead++; |
71fceff0 DM |
1105 | else if (nexthop_nh->nh_dev == dev && |
1106 | nexthop_nh->nh_scope != scope) { | |
1107 | nexthop_nh->nh_flags |= RTNH_F_DEAD; | |
1da177e4 | 1108 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
85326fa5 | 1109 | spin_lock_bh(&fib_multipath_lock); |
71fceff0 DM |
1110 | fi->fib_power -= nexthop_nh->nh_power; |
1111 | nexthop_nh->nh_power = 0; | |
85326fa5 | 1112 | spin_unlock_bh(&fib_multipath_lock); |
1da177e4 | 1113 | #endif |
85326fa5 DL |
1114 | dead++; |
1115 | } | |
1da177e4 | 1116 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
71fceff0 | 1117 | if (force > 1 && nexthop_nh->nh_dev == dev) { |
85326fa5 DL |
1118 | dead = fi->fib_nhs; |
1119 | break; | |
1da177e4 | 1120 | } |
85326fa5 DL |
1121 | #endif |
1122 | } endfor_nexthops(fi) | |
1123 | if (dead == fi->fib_nhs) { | |
1124 | fi->fib_flags |= RTNH_F_DEAD; | |
1125 | ret++; | |
1da177e4 LT |
1126 | } |
1127 | } | |
1128 | ||
1129 | return ret; | |
1130 | } | |
1131 | ||
1132 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
1133 | ||
1134 | /* | |
6a31d2a9 ED |
1135 | * Dead device goes up. We wake up dead nexthops. |
1136 | * It takes sense only on multipath routes. | |
1da177e4 | 1137 | */ |
1da177e4 LT |
1138 | int fib_sync_up(struct net_device *dev) |
1139 | { | |
1140 | struct fib_info *prev_fi; | |
1141 | unsigned int hash; | |
1142 | struct hlist_head *head; | |
1143 | struct hlist_node *node; | |
1144 | struct fib_nh *nh; | |
1145 | int ret; | |
1146 | ||
6a31d2a9 | 1147 | if (!(dev->flags & IFF_UP)) |
1da177e4 LT |
1148 | return 0; |
1149 | ||
1150 | prev_fi = NULL; | |
1151 | hash = fib_devindex_hashfn(dev->ifindex); | |
1152 | head = &fib_info_devhash[hash]; | |
1153 | ret = 0; | |
1154 | ||
1155 | hlist_for_each_entry(nh, node, head, nh_hash) { | |
1156 | struct fib_info *fi = nh->nh_parent; | |
1157 | int alive; | |
1158 | ||
1159 | BUG_ON(!fi->fib_nhs); | |
1160 | if (nh->nh_dev != dev || fi == prev_fi) | |
1161 | continue; | |
1162 | ||
1163 | prev_fi = fi; | |
1164 | alive = 0; | |
1165 | change_nexthops(fi) { | |
6a31d2a9 | 1166 | if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) { |
1da177e4 LT |
1167 | alive++; |
1168 | continue; | |
1169 | } | |
71fceff0 | 1170 | if (nexthop_nh->nh_dev == NULL || |
6a31d2a9 | 1171 | !(nexthop_nh->nh_dev->flags & IFF_UP)) |
1da177e4 | 1172 | continue; |
71fceff0 DM |
1173 | if (nexthop_nh->nh_dev != dev || |
1174 | !__in_dev_get_rtnl(dev)) | |
1da177e4 LT |
1175 | continue; |
1176 | alive++; | |
1177 | spin_lock_bh(&fib_multipath_lock); | |
71fceff0 DM |
1178 | nexthop_nh->nh_power = 0; |
1179 | nexthop_nh->nh_flags &= ~RTNH_F_DEAD; | |
1da177e4 LT |
1180 | spin_unlock_bh(&fib_multipath_lock); |
1181 | } endfor_nexthops(fi) | |
1182 | ||
1183 | if (alive > 0) { | |
1184 | fi->fib_flags &= ~RTNH_F_DEAD; | |
1185 | ret++; | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | return ret; | |
1190 | } | |
1191 | ||
1192 | /* | |
6a31d2a9 ED |
1193 | * The algorithm is suboptimal, but it provides really |
1194 | * fair weighted route distribution. | |
1da177e4 | 1195 | */ |
1da177e4 LT |
1196 | void fib_select_multipath(const struct flowi *flp, struct fib_result *res) |
1197 | { | |
1198 | struct fib_info *fi = res->fi; | |
1199 | int w; | |
1200 | ||
1201 | spin_lock_bh(&fib_multipath_lock); | |
1202 | if (fi->fib_power <= 0) { | |
1203 | int power = 0; | |
1204 | change_nexthops(fi) { | |
6a31d2a9 | 1205 | if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) { |
71fceff0 DM |
1206 | power += nexthop_nh->nh_weight; |
1207 | nexthop_nh->nh_power = nexthop_nh->nh_weight; | |
1da177e4 LT |
1208 | } |
1209 | } endfor_nexthops(fi); | |
1210 | fi->fib_power = power; | |
1211 | if (power <= 0) { | |
1212 | spin_unlock_bh(&fib_multipath_lock); | |
1213 | /* Race condition: route has just become dead. */ | |
1214 | res->nh_sel = 0; | |
1215 | return; | |
1216 | } | |
1217 | } | |
1218 | ||
1219 | ||
1220 | /* w should be random number [0..fi->fib_power-1], | |
6a31d2a9 | 1221 | * it is pretty bad approximation. |
1da177e4 LT |
1222 | */ |
1223 | ||
1224 | w = jiffies % fi->fib_power; | |
1225 | ||
1226 | change_nexthops(fi) { | |
6a31d2a9 | 1227 | if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) && |
71fceff0 | 1228 | nexthop_nh->nh_power) { |
6a31d2a9 ED |
1229 | w -= nexthop_nh->nh_power; |
1230 | if (w <= 0) { | |
71fceff0 | 1231 | nexthop_nh->nh_power--; |
1da177e4 LT |
1232 | fi->fib_power--; |
1233 | res->nh_sel = nhsel; | |
1234 | spin_unlock_bh(&fib_multipath_lock); | |
1235 | return; | |
1236 | } | |
1237 | } | |
1238 | } endfor_nexthops(fi); | |
1239 | ||
1240 | /* Race condition: route has just become dead. */ | |
1241 | res->nh_sel = 0; | |
1242 | spin_unlock_bh(&fib_multipath_lock); | |
1243 | } | |
1244 | #endif |