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.
6 * IPv4 Forwarding Information Base: semantics.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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.
16 #include <asm/uaccess.h>
17 #include <asm/system.h>
18 #include <linux/bitops.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/jiffies.h>
23 #include <linux/string.h>
24 #include <linux/socket.h>
25 #include <linux/sockios.h>
26 #include <linux/errno.h>
28 #include <linux/inet.h>
29 #include <linux/inetdevice.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/proc_fs.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
39 #include <net/protocol.h>
40 #include <net/route.h>
43 #include <net/ip_fib.h>
44 #include <net/netlink.h>
45 #include <net/nexthop.h>
47 #include "fib_lookup.h"
49 static DEFINE_SPINLOCK(fib_info_lock);
50 static struct hlist_head *fib_info_hash;
51 static struct hlist_head *fib_info_laddrhash;
52 static unsigned int fib_info_hash_size;
53 static unsigned int fib_info_cnt;
55 #define DEVINDEX_HASHBITS 8
56 #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
57 static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
59 #ifdef CONFIG_IP_ROUTE_MULTIPATH
61 static DEFINE_SPINLOCK(fib_multipath_lock);
63 #define for_nexthops(fi) { \
64 int nhsel; const struct fib_nh *nh; \
65 for (nhsel = 0, nh = (fi)->fib_nh; \
66 nhsel < (fi)->fib_nhs; \
69 #define change_nexthops(fi) { \
70 int nhsel; struct fib_nh *nexthop_nh; \
71 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
72 nhsel < (fi)->fib_nhs; \
73 nexthop_nh++, nhsel++)
75 #else /* CONFIG_IP_ROUTE_MULTIPATH */
77 /* Hope, that gcc will optimize it to get rid of dummy loop */
79 #define for_nexthops(fi) { \
80 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
81 for (nhsel = 0; nhsel < 1; nhsel++)
83 #define change_nexthops(fi) { \
85 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
86 for (nhsel = 0; nhsel < 1; nhsel++)
88 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
90 #define endfor_nexthops(fi) }
93 const struct fib_prop fib_props[RTN_MAX + 1] = {
96 .scope = RT_SCOPE_NOWHERE,
100 .scope = RT_SCOPE_UNIVERSE,
104 .scope = RT_SCOPE_HOST,
108 .scope = RT_SCOPE_LINK,
112 .scope = RT_SCOPE_LINK,
116 .scope = RT_SCOPE_UNIVERSE,
120 .scope = RT_SCOPE_UNIVERSE,
122 [RTN_UNREACHABLE] = {
123 .error = -EHOSTUNREACH,
124 .scope = RT_SCOPE_UNIVERSE,
128 .scope = RT_SCOPE_UNIVERSE,
132 .scope = RT_SCOPE_UNIVERSE,
136 .scope = RT_SCOPE_NOWHERE,
140 .scope = RT_SCOPE_NOWHERE,
144 /* Release a nexthop info record */
145 static void free_fib_info_rcu(struct rcu_head *head)
147 struct fib_info *fi = container_of(head, struct fib_info, rcu);
149 change_nexthops(fi) {
150 if (nexthop_nh->nh_dev)
151 dev_put(nexthop_nh->nh_dev);
152 } endfor_nexthops(fi);
154 release_net(fi->fib_net);
158 void free_fib_info(struct fib_info *fi)
160 if (fi->fib_dead == 0) {
161 pr_warning("Freeing alive fib_info %p\n", fi);
165 call_rcu(&fi->rcu, free_fib_info_rcu);
168 void fib_release_info(struct fib_info *fi)
170 spin_lock_bh(&fib_info_lock);
171 if (fi && --fi->fib_treeref == 0) {
172 hlist_del(&fi->fib_hash);
174 hlist_del(&fi->fib_lhash);
175 change_nexthops(fi) {
176 if (!nexthop_nh->nh_dev)
178 hlist_del(&nexthop_nh->nh_hash);
179 } endfor_nexthops(fi)
183 spin_unlock_bh(&fib_info_lock);
186 static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
188 const struct fib_nh *onh = ofi->fib_nh;
191 if (nh->nh_oif != onh->nh_oif ||
192 nh->nh_gw != onh->nh_gw ||
193 nh->nh_scope != onh->nh_scope ||
194 #ifdef CONFIG_IP_ROUTE_MULTIPATH
195 nh->nh_weight != onh->nh_weight ||
197 #ifdef CONFIG_IP_ROUTE_CLASSID
198 nh->nh_tclassid != onh->nh_tclassid ||
200 ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
203 } endfor_nexthops(fi);
207 static inline unsigned int fib_devindex_hashfn(unsigned int val)
209 unsigned int mask = DEVINDEX_HASHSIZE - 1;
212 (val >> DEVINDEX_HASHBITS) ^
213 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
216 static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
218 unsigned int mask = (fib_info_hash_size - 1);
219 unsigned int val = fi->fib_nhs;
221 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
222 val ^= (__force u32)fi->fib_prefsrc;
223 val ^= fi->fib_priority;
225 val ^= fib_devindex_hashfn(nh->nh_oif);
226 } endfor_nexthops(fi)
228 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
231 static struct fib_info *fib_find_info(const struct fib_info *nfi)
233 struct hlist_head *head;
234 struct hlist_node *node;
238 hash = fib_info_hashfn(nfi);
239 head = &fib_info_hash[hash];
241 hlist_for_each_entry(fi, node, head, fib_hash) {
242 if (!net_eq(fi->fib_net, nfi->fib_net))
244 if (fi->fib_nhs != nfi->fib_nhs)
246 if (nfi->fib_protocol == fi->fib_protocol &&
247 nfi->fib_scope == fi->fib_scope &&
248 nfi->fib_prefsrc == fi->fib_prefsrc &&
249 nfi->fib_priority == fi->fib_priority &&
250 memcmp(nfi->fib_metrics, fi->fib_metrics,
251 sizeof(u32) * RTAX_MAX) == 0 &&
252 ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
253 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
260 /* Check, that the gateway is already configured.
261 * Used only by redirect accept routine.
263 int ip_fib_check_default(__be32 gw, struct net_device *dev)
265 struct hlist_head *head;
266 struct hlist_node *node;
270 spin_lock(&fib_info_lock);
272 hash = fib_devindex_hashfn(dev->ifindex);
273 head = &fib_info_devhash[hash];
274 hlist_for_each_entry(nh, node, head, nh_hash) {
275 if (nh->nh_dev == dev &&
277 !(nh->nh_flags & RTNH_F_DEAD)) {
278 spin_unlock(&fib_info_lock);
283 spin_unlock(&fib_info_lock);
288 static inline size_t fib_nlmsg_size(struct fib_info *fi)
290 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
291 + nla_total_size(4) /* RTA_TABLE */
292 + nla_total_size(4) /* RTA_DST */
293 + nla_total_size(4) /* RTA_PRIORITY */
294 + nla_total_size(4); /* RTA_PREFSRC */
296 /* space for nested metrics */
297 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
300 /* Also handles the special case fib_nhs == 1 */
302 /* each nexthop is packed in an attribute */
303 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
305 /* may contain flow and gateway attribute */
306 nhsize += 2 * nla_total_size(4);
308 /* all nexthops are packed in a nested attribute */
309 payload += nla_total_size(fi->fib_nhs * nhsize);
315 void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
316 int dst_len, u32 tb_id, struct nl_info *info,
317 unsigned int nlm_flags)
320 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
323 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
327 err = fib_dump_info(skb, info->pid, seq, event, tb_id,
328 fa->fa_type, key, dst_len,
329 fa->fa_tos, fa->fa_info, nlm_flags);
331 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
332 WARN_ON(err == -EMSGSIZE);
336 rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
337 info->nlh, GFP_KERNEL);
341 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
344 /* Return the first fib alias matching TOS with
345 * priority less than or equal to PRIO.
347 struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
350 struct fib_alias *fa;
351 list_for_each_entry(fa, fah, fa_list) {
352 if (fa->fa_tos > tos)
354 if (fa->fa_info->fib_priority >= prio ||
362 int fib_detect_death(struct fib_info *fi, int order,
363 struct fib_info **last_resort, int *last_idx, int dflt)
366 int state = NUD_NONE;
368 n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
370 state = n->nud_state;
373 if (state == NUD_REACHABLE)
375 if ((state & NUD_VALID) && order != dflt)
377 if ((state & NUD_VALID) ||
378 (*last_idx < 0 && order > dflt)) {
385 #ifdef CONFIG_IP_ROUTE_MULTIPATH
387 static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
391 while (rtnh_ok(rtnh, remaining)) {
393 rtnh = rtnh_next(rtnh, &remaining);
396 /* leftover implies invalid nexthop configuration, discard it */
397 return remaining > 0 ? 0 : nhs;
400 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
401 int remaining, struct fib_config *cfg)
403 change_nexthops(fi) {
406 if (!rtnh_ok(rtnh, remaining))
409 nexthop_nh->nh_flags =
410 (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
411 nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
412 nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
414 attrlen = rtnh_attrlen(rtnh);
416 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
418 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
419 nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
420 #ifdef CONFIG_IP_ROUTE_CLASSID
421 nla = nla_find(attrs, attrlen, RTA_FLOW);
422 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
426 rtnh = rtnh_next(rtnh, &remaining);
427 } endfor_nexthops(fi);
434 int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
436 #ifdef CONFIG_IP_ROUTE_MULTIPATH
437 struct rtnexthop *rtnh;
441 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
444 if (cfg->fc_oif || cfg->fc_gw) {
445 if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
446 (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
451 #ifdef CONFIG_IP_ROUTE_MULTIPATH
452 if (cfg->fc_mp == NULL)
456 remaining = cfg->fc_mp_len;
461 if (!rtnh_ok(rtnh, remaining))
464 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
467 attrlen = rtnh_attrlen(rtnh);
469 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
471 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
472 if (nla && nla_get_be32(nla) != nh->nh_gw)
474 #ifdef CONFIG_IP_ROUTE_CLASSID
475 nla = nla_find(attrs, attrlen, RTA_FLOW);
476 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
481 rtnh = rtnh_next(rtnh, &remaining);
482 } endfor_nexthops(fi);
492 * Semantics of nexthop is very messy by historical reasons.
493 * We have to take into account, that:
494 * a) gateway can be actually local interface address,
495 * so that gatewayed route is direct.
496 * b) gateway must be on-link address, possibly
497 * described not by an ifaddr, but also by a direct route.
498 * c) If both gateway and interface are specified, they should not
500 * d) If we use tunnel routes, gateway could be not on-link.
502 * Attempt to reconcile all of these (alas, self-contradictory) conditions
503 * results in pretty ugly and hairy code with obscure logic.
505 * I chose to generalized it instead, so that the size
506 * of code does not increase practically, but it becomes
508 * Every prefix is assigned a "scope" value: "host" is local address,
509 * "link" is direct route,
510 * [ ... "site" ... "interior" ... ]
511 * and "universe" is true gateway route with global meaning.
513 * Every prefix refers to a set of "nexthop"s (gw, oif),
514 * where gw must have narrower scope. This recursion stops
515 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
516 * which means that gw is forced to be on link.
518 * Code is still hairy, but now it is apparently logically
519 * consistent and very flexible. F.e. as by-product it allows
520 * to co-exists in peace independent exterior and interior
523 * Normally it looks as following.
525 * {universe prefix} -> (gw, oif) [scope link]
527 * |-> {link prefix} -> (gw, oif) [scope local]
529 * |-> {local prefix} (terminal node)
531 static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
536 struct net_device *dev;
538 net = cfg->fc_nlinfo.nl_net;
540 struct fib_result res;
542 if (nh->nh_flags & RTNH_F_ONLINK) {
544 if (cfg->fc_scope >= RT_SCOPE_LINK)
546 if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
548 dev = __dev_get_by_index(net, nh->nh_oif);
551 if (!(dev->flags & IFF_UP))
555 nh->nh_scope = RT_SCOPE_LINK;
560 struct flowi4 fl4 = {
562 .flowi4_scope = cfg->fc_scope + 1,
563 .flowi4_oif = nh->nh_oif,
566 /* It is not necessary, but requires a bit of thinking */
567 if (fl4.flowi4_scope < RT_SCOPE_LINK)
568 fl4.flowi4_scope = RT_SCOPE_LINK;
569 err = fib_lookup(net, &fl4, &res);
576 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
578 nh->nh_scope = res.scope;
579 nh->nh_oif = FIB_RES_OIF(res);
580 nh->nh_dev = dev = FIB_RES_DEV(res);
584 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
586 struct in_device *in_dev;
588 if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
593 in_dev = inetdev_by_index(net, nh->nh_oif);
597 if (!(in_dev->dev->flags & IFF_UP))
599 nh->nh_dev = in_dev->dev;
600 dev_hold(nh->nh_dev);
601 nh->nh_scope = RT_SCOPE_HOST;
609 static inline unsigned int fib_laddr_hashfn(__be32 val)
611 unsigned int mask = (fib_info_hash_size - 1);
613 return ((__force u32)val ^
614 ((__force u32)val >> 7) ^
615 ((__force u32)val >> 14)) & mask;
618 static struct hlist_head *fib_info_hash_alloc(int bytes)
620 if (bytes <= PAGE_SIZE)
621 return kzalloc(bytes, GFP_KERNEL);
623 return (struct hlist_head *)
624 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
628 static void fib_info_hash_free(struct hlist_head *hash, int bytes)
633 if (bytes <= PAGE_SIZE)
636 free_pages((unsigned long) hash, get_order(bytes));
639 static void fib_info_hash_move(struct hlist_head *new_info_hash,
640 struct hlist_head *new_laddrhash,
641 unsigned int new_size)
643 struct hlist_head *old_info_hash, *old_laddrhash;
644 unsigned int old_size = fib_info_hash_size;
645 unsigned int i, bytes;
647 spin_lock_bh(&fib_info_lock);
648 old_info_hash = fib_info_hash;
649 old_laddrhash = fib_info_laddrhash;
650 fib_info_hash_size = new_size;
652 for (i = 0; i < old_size; i++) {
653 struct hlist_head *head = &fib_info_hash[i];
654 struct hlist_node *node, *n;
657 hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
658 struct hlist_head *dest;
659 unsigned int new_hash;
661 hlist_del(&fi->fib_hash);
663 new_hash = fib_info_hashfn(fi);
664 dest = &new_info_hash[new_hash];
665 hlist_add_head(&fi->fib_hash, dest);
668 fib_info_hash = new_info_hash;
670 for (i = 0; i < old_size; i++) {
671 struct hlist_head *lhead = &fib_info_laddrhash[i];
672 struct hlist_node *node, *n;
675 hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
676 struct hlist_head *ldest;
677 unsigned int new_hash;
679 hlist_del(&fi->fib_lhash);
681 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
682 ldest = &new_laddrhash[new_hash];
683 hlist_add_head(&fi->fib_lhash, ldest);
686 fib_info_laddrhash = new_laddrhash;
688 spin_unlock_bh(&fib_info_lock);
690 bytes = old_size * sizeof(struct hlist_head *);
691 fib_info_hash_free(old_info_hash, bytes);
692 fib_info_hash_free(old_laddrhash, bytes);
695 __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
697 nh->nh_saddr = inet_select_addr(nh->nh_dev,
699 nh->nh_parent->fib_scope);
700 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
705 struct fib_info *fib_create_info(struct fib_config *cfg)
708 struct fib_info *fi = NULL;
709 struct fib_info *ofi;
711 struct net *net = cfg->fc_nlinfo.nl_net;
713 if (cfg->fc_type > RTN_MAX)
716 /* Fast check to catch the most weird cases */
717 if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
720 #ifdef CONFIG_IP_ROUTE_MULTIPATH
722 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
729 if (fib_info_cnt >= fib_info_hash_size) {
730 unsigned int new_size = fib_info_hash_size << 1;
731 struct hlist_head *new_info_hash;
732 struct hlist_head *new_laddrhash;
737 bytes = new_size * sizeof(struct hlist_head *);
738 new_info_hash = fib_info_hash_alloc(bytes);
739 new_laddrhash = fib_info_hash_alloc(bytes);
740 if (!new_info_hash || !new_laddrhash) {
741 fib_info_hash_free(new_info_hash, bytes);
742 fib_info_hash_free(new_laddrhash, bytes);
744 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
746 if (!fib_info_hash_size)
750 fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
754 fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
755 if (!fi->fib_metrics)
758 fi->fib_metrics = (u32 *) dst_default_metrics;
761 fi->fib_net = hold_net(net);
762 fi->fib_protocol = cfg->fc_protocol;
763 fi->fib_scope = cfg->fc_scope;
764 fi->fib_flags = cfg->fc_flags;
765 fi->fib_priority = cfg->fc_priority;
766 fi->fib_prefsrc = cfg->fc_prefsrc;
769 change_nexthops(fi) {
770 nexthop_nh->nh_parent = fi;
771 } endfor_nexthops(fi)
777 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
778 int type = nla_type(nla);
783 fi->fib_metrics[type - 1] = nla_get_u32(nla);
789 #ifdef CONFIG_IP_ROUTE_MULTIPATH
790 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
793 if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
795 if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
797 #ifdef CONFIG_IP_ROUTE_CLASSID
798 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
805 struct fib_nh *nh = fi->fib_nh;
807 nh->nh_oif = cfg->fc_oif;
808 nh->nh_gw = cfg->fc_gw;
809 nh->nh_flags = cfg->fc_flags;
810 #ifdef CONFIG_IP_ROUTE_CLASSID
811 nh->nh_tclassid = cfg->fc_flow;
813 #ifdef CONFIG_IP_ROUTE_MULTIPATH
818 if (fib_props[cfg->fc_type].error) {
819 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
823 switch (cfg->fc_type) {
835 if (cfg->fc_scope > RT_SCOPE_HOST)
838 if (cfg->fc_scope == RT_SCOPE_HOST) {
839 struct fib_nh *nh = fi->fib_nh;
841 /* Local address is added. */
842 if (nhs != 1 || nh->nh_gw)
844 nh->nh_scope = RT_SCOPE_NOWHERE;
845 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
847 if (nh->nh_dev == NULL)
850 change_nexthops(fi) {
851 err = fib_check_nh(cfg, fi, nexthop_nh);
854 } endfor_nexthops(fi)
857 if (fi->fib_prefsrc) {
858 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
859 fi->fib_prefsrc != cfg->fc_dst)
860 if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
864 change_nexthops(fi) {
865 fib_info_update_nh_saddr(net, nexthop_nh);
866 } endfor_nexthops(fi)
869 ofi = fib_find_info(fi);
878 atomic_inc(&fi->fib_clntref);
879 spin_lock_bh(&fib_info_lock);
880 hlist_add_head(&fi->fib_hash,
881 &fib_info_hash[fib_info_hashfn(fi)]);
882 if (fi->fib_prefsrc) {
883 struct hlist_head *head;
885 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
886 hlist_add_head(&fi->fib_lhash, head);
888 change_nexthops(fi) {
889 struct hlist_head *head;
892 if (!nexthop_nh->nh_dev)
894 hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
895 head = &fib_info_devhash[hash];
896 hlist_add_head(&nexthop_nh->nh_hash, head);
897 } endfor_nexthops(fi)
898 spin_unlock_bh(&fib_info_lock);
913 int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
914 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
915 struct fib_info *fi, unsigned int flags)
917 struct nlmsghdr *nlh;
920 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
924 rtm = nlmsg_data(nlh);
925 rtm->rtm_family = AF_INET;
926 rtm->rtm_dst_len = dst_len;
927 rtm->rtm_src_len = 0;
930 rtm->rtm_table = tb_id;
932 rtm->rtm_table = RT_TABLE_COMPAT;
933 NLA_PUT_U32(skb, RTA_TABLE, tb_id);
934 rtm->rtm_type = type;
935 rtm->rtm_flags = fi->fib_flags;
936 rtm->rtm_scope = fi->fib_scope;
937 rtm->rtm_protocol = fi->fib_protocol;
939 if (rtm->rtm_dst_len)
940 NLA_PUT_BE32(skb, RTA_DST, dst);
942 if (fi->fib_priority)
943 NLA_PUT_U32(skb, RTA_PRIORITY, fi->fib_priority);
945 if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
946 goto nla_put_failure;
949 NLA_PUT_BE32(skb, RTA_PREFSRC, fi->fib_prefsrc);
951 if (fi->fib_nhs == 1) {
952 if (fi->fib_nh->nh_gw)
953 NLA_PUT_BE32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw);
955 if (fi->fib_nh->nh_oif)
956 NLA_PUT_U32(skb, RTA_OIF, fi->fib_nh->nh_oif);
957 #ifdef CONFIG_IP_ROUTE_CLASSID
958 if (fi->fib_nh[0].nh_tclassid)
959 NLA_PUT_U32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid);
962 #ifdef CONFIG_IP_ROUTE_MULTIPATH
963 if (fi->fib_nhs > 1) {
964 struct rtnexthop *rtnh;
967 mp = nla_nest_start(skb, RTA_MULTIPATH);
969 goto nla_put_failure;
972 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
974 goto nla_put_failure;
976 rtnh->rtnh_flags = nh->nh_flags & 0xFF;
977 rtnh->rtnh_hops = nh->nh_weight - 1;
978 rtnh->rtnh_ifindex = nh->nh_oif;
981 NLA_PUT_BE32(skb, RTA_GATEWAY, nh->nh_gw);
982 #ifdef CONFIG_IP_ROUTE_CLASSID
984 NLA_PUT_U32(skb, RTA_FLOW, nh->nh_tclassid);
986 /* length of rtnetlink header + attributes */
987 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
988 } endfor_nexthops(fi);
990 nla_nest_end(skb, mp);
993 return nlmsg_end(skb, nlh);
996 nlmsg_cancel(skb, nlh);
1002 * - local address disappeared -> we must delete all the entries
1004 * - device went down -> we must shutdown all nexthops going via it.
1006 int fib_sync_down_addr(struct net *net, __be32 local)
1009 unsigned int hash = fib_laddr_hashfn(local);
1010 struct hlist_head *head = &fib_info_laddrhash[hash];
1011 struct hlist_node *node;
1012 struct fib_info *fi;
1014 if (fib_info_laddrhash == NULL || local == 0)
1017 hlist_for_each_entry(fi, node, head, fib_lhash) {
1018 if (!net_eq(fi->fib_net, net))
1020 if (fi->fib_prefsrc == local) {
1021 fi->fib_flags |= RTNH_F_DEAD;
1028 int fib_sync_down_dev(struct net_device *dev, int force)
1031 int scope = RT_SCOPE_NOWHERE;
1032 struct fib_info *prev_fi = NULL;
1033 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1034 struct hlist_head *head = &fib_info_devhash[hash];
1035 struct hlist_node *node;
1041 hlist_for_each_entry(nh, node, head, nh_hash) {
1042 struct fib_info *fi = nh->nh_parent;
1045 BUG_ON(!fi->fib_nhs);
1046 if (nh->nh_dev != dev || fi == prev_fi)
1050 change_nexthops(fi) {
1051 if (nexthop_nh->nh_flags & RTNH_F_DEAD)
1053 else if (nexthop_nh->nh_dev == dev &&
1054 nexthop_nh->nh_scope != scope) {
1055 nexthop_nh->nh_flags |= RTNH_F_DEAD;
1056 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1057 spin_lock_bh(&fib_multipath_lock);
1058 fi->fib_power -= nexthop_nh->nh_power;
1059 nexthop_nh->nh_power = 0;
1060 spin_unlock_bh(&fib_multipath_lock);
1064 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1065 if (force > 1 && nexthop_nh->nh_dev == dev) {
1070 } endfor_nexthops(fi)
1071 if (dead == fi->fib_nhs) {
1072 fi->fib_flags |= RTNH_F_DEAD;
1080 /* Must be invoked inside of an RCU protected region. */
1081 void fib_select_default(struct fib_result *res)
1083 struct fib_info *fi = NULL, *last_resort = NULL;
1084 struct list_head *fa_head = res->fa_head;
1085 struct fib_table *tb = res->table;
1086 int order = -1, last_idx = -1;
1087 struct fib_alias *fa;
1089 list_for_each_entry_rcu(fa, fa_head, fa_list) {
1090 struct fib_info *next_fi = fa->fa_info;
1092 if (next_fi->fib_scope != res->scope ||
1093 fa->fa_type != RTN_UNICAST)
1096 if (next_fi->fib_priority > res->fi->fib_priority)
1098 if (!next_fi->fib_nh[0].nh_gw ||
1099 next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
1102 fib_alias_accessed(fa);
1105 if (next_fi != res->fi)
1107 } else if (!fib_detect_death(fi, order, &last_resort,
1108 &last_idx, tb->tb_default)) {
1109 fib_result_assign(res, fi);
1110 tb->tb_default = order;
1117 if (order <= 0 || fi == NULL) {
1118 tb->tb_default = -1;
1122 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
1124 fib_result_assign(res, fi);
1125 tb->tb_default = order;
1130 fib_result_assign(res, last_resort);
1131 tb->tb_default = last_idx;
1136 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1139 * Dead device goes up. We wake up dead nexthops.
1140 * It takes sense only on multipath routes.
1142 int fib_sync_up(struct net_device *dev)
1144 struct fib_info *prev_fi;
1146 struct hlist_head *head;
1147 struct hlist_node *node;
1151 if (!(dev->flags & IFF_UP))
1155 hash = fib_devindex_hashfn(dev->ifindex);
1156 head = &fib_info_devhash[hash];
1159 hlist_for_each_entry(nh, node, head, nh_hash) {
1160 struct fib_info *fi = nh->nh_parent;
1163 BUG_ON(!fi->fib_nhs);
1164 if (nh->nh_dev != dev || fi == prev_fi)
1169 change_nexthops(fi) {
1170 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
1174 if (nexthop_nh->nh_dev == NULL ||
1175 !(nexthop_nh->nh_dev->flags & IFF_UP))
1177 if (nexthop_nh->nh_dev != dev ||
1178 !__in_dev_get_rtnl(dev))
1181 spin_lock_bh(&fib_multipath_lock);
1182 nexthop_nh->nh_power = 0;
1183 nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
1184 spin_unlock_bh(&fib_multipath_lock);
1185 } endfor_nexthops(fi)
1188 fi->fib_flags &= ~RTNH_F_DEAD;
1197 * The algorithm is suboptimal, but it provides really
1198 * fair weighted route distribution.
1200 void fib_select_multipath(struct fib_result *res)
1202 struct fib_info *fi = res->fi;
1205 spin_lock_bh(&fib_multipath_lock);
1206 if (fi->fib_power <= 0) {
1208 change_nexthops(fi) {
1209 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
1210 power += nexthop_nh->nh_weight;
1211 nexthop_nh->nh_power = nexthop_nh->nh_weight;
1213 } endfor_nexthops(fi);
1214 fi->fib_power = power;
1216 spin_unlock_bh(&fib_multipath_lock);
1217 /* Race condition: route has just become dead. */
1224 /* w should be random number [0..fi->fib_power-1],
1225 * it is pretty bad approximation.
1228 w = jiffies % fi->fib_power;
1230 change_nexthops(fi) {
1231 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
1232 nexthop_nh->nh_power) {
1233 w -= nexthop_nh->nh_power;
1235 nexthop_nh->nh_power--;
1237 res->nh_sel = nhsel;
1238 spin_unlock_bh(&fib_multipath_lock);
1242 } endfor_nexthops(fi);
1244 /* Race condition: route has just become dead. */
1246 spin_unlock_bh(&fib_multipath_lock);