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