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