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