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 */
146 void free_fib_info(struct fib_info *fi)
148 if (fi->fib_dead == 0) {
149 pr_warning("Freeing alive fib_info %p\n", fi);
152 change_nexthops(fi) {
153 if (nexthop_nh->nh_dev)
154 dev_put(nexthop_nh->nh_dev);
155 nexthop_nh->nh_dev = NULL;
156 } endfor_nexthops(fi);
158 release_net(fi->fib_net);
162 void fib_release_info(struct fib_info *fi)
164 spin_lock_bh(&fib_info_lock);
165 if (fi && --fi->fib_treeref == 0) {
166 hlist_del(&fi->fib_hash);
168 hlist_del(&fi->fib_lhash);
169 change_nexthops(fi) {
170 if (!nexthop_nh->nh_dev)
172 hlist_del(&nexthop_nh->nh_hash);
173 } endfor_nexthops(fi)
177 spin_unlock_bh(&fib_info_lock);
180 static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
182 const struct fib_nh *onh = ofi->fib_nh;
185 if (nh->nh_oif != onh->nh_oif ||
186 nh->nh_gw != onh->nh_gw ||
187 nh->nh_scope != onh->nh_scope ||
188 #ifdef CONFIG_IP_ROUTE_MULTIPATH
189 nh->nh_weight != onh->nh_weight ||
191 #ifdef CONFIG_IP_ROUTE_CLASSID
192 nh->nh_tclassid != onh->nh_tclassid ||
194 ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
197 } endfor_nexthops(fi);
201 static inline unsigned int fib_devindex_hashfn(unsigned int val)
203 unsigned int mask = DEVINDEX_HASHSIZE - 1;
206 (val >> DEVINDEX_HASHBITS) ^
207 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
210 static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
212 unsigned int mask = (fib_info_hash_size - 1);
213 unsigned int val = fi->fib_nhs;
215 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
216 val ^= (__force u32)fi->fib_prefsrc;
217 val ^= fi->fib_priority;
219 val ^= fib_devindex_hashfn(nh->nh_oif);
220 } endfor_nexthops(fi)
222 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
225 static struct fib_info *fib_find_info(const struct fib_info *nfi)
227 struct hlist_head *head;
228 struct hlist_node *node;
232 hash = fib_info_hashfn(nfi);
233 head = &fib_info_hash[hash];
235 hlist_for_each_entry(fi, node, head, fib_hash) {
236 if (!net_eq(fi->fib_net, nfi->fib_net))
238 if (fi->fib_nhs != nfi->fib_nhs)
240 if (nfi->fib_protocol == fi->fib_protocol &&
241 nfi->fib_scope == fi->fib_scope &&
242 nfi->fib_prefsrc == fi->fib_prefsrc &&
243 nfi->fib_priority == fi->fib_priority &&
244 memcmp(nfi->fib_metrics, fi->fib_metrics,
245 sizeof(u32) * RTAX_MAX) == 0 &&
246 ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
247 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
254 /* Check, that the gateway is already configured.
255 * Used only by redirect accept routine.
257 int ip_fib_check_default(__be32 gw, struct net_device *dev)
259 struct hlist_head *head;
260 struct hlist_node *node;
264 spin_lock(&fib_info_lock);
266 hash = fib_devindex_hashfn(dev->ifindex);
267 head = &fib_info_devhash[hash];
268 hlist_for_each_entry(nh, node, head, nh_hash) {
269 if (nh->nh_dev == dev &&
271 !(nh->nh_flags & RTNH_F_DEAD)) {
272 spin_unlock(&fib_info_lock);
277 spin_unlock(&fib_info_lock);
282 static inline size_t fib_nlmsg_size(struct fib_info *fi)
284 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
285 + nla_total_size(4) /* RTA_TABLE */
286 + nla_total_size(4) /* RTA_DST */
287 + nla_total_size(4) /* RTA_PRIORITY */
288 + nla_total_size(4); /* RTA_PREFSRC */
290 /* space for nested metrics */
291 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
294 /* Also handles the special case fib_nhs == 1 */
296 /* each nexthop is packed in an attribute */
297 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
299 /* may contain flow and gateway attribute */
300 nhsize += 2 * nla_total_size(4);
302 /* all nexthops are packed in a nested attribute */
303 payload += nla_total_size(fi->fib_nhs * nhsize);
309 void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
310 int dst_len, u32 tb_id, struct nl_info *info,
311 unsigned int nlm_flags)
314 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
317 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
321 err = fib_dump_info(skb, info->pid, seq, event, tb_id,
322 fa->fa_type, key, dst_len,
323 fa->fa_tos, fa->fa_info, nlm_flags);
325 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
326 WARN_ON(err == -EMSGSIZE);
330 rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
331 info->nlh, GFP_KERNEL);
335 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
338 /* Return the first fib alias matching TOS with
339 * priority less than or equal to PRIO.
341 struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
344 struct fib_alias *fa;
345 list_for_each_entry(fa, fah, fa_list) {
346 if (fa->fa_tos > tos)
348 if (fa->fa_info->fib_priority >= prio ||
356 int fib_detect_death(struct fib_info *fi, int order,
357 struct fib_info **last_resort, int *last_idx, int dflt)
360 int state = NUD_NONE;
362 n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
364 state = n->nud_state;
367 if (state == NUD_REACHABLE)
369 if ((state & NUD_VALID) && order != dflt)
371 if ((state & NUD_VALID) ||
372 (*last_idx < 0 && order > dflt)) {
379 #ifdef CONFIG_IP_ROUTE_MULTIPATH
381 static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
385 while (rtnh_ok(rtnh, remaining)) {
387 rtnh = rtnh_next(rtnh, &remaining);
390 /* leftover implies invalid nexthop configuration, discard it */
391 return remaining > 0 ? 0 : nhs;
394 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
395 int remaining, struct fib_config *cfg)
397 change_nexthops(fi) {
400 if (!rtnh_ok(rtnh, remaining))
403 nexthop_nh->nh_flags =
404 (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
405 nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
406 nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
408 attrlen = rtnh_attrlen(rtnh);
410 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
412 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
413 nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
414 #ifdef CONFIG_IP_ROUTE_CLASSID
415 nla = nla_find(attrs, attrlen, RTA_FLOW);
416 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
420 rtnh = rtnh_next(rtnh, &remaining);
421 } endfor_nexthops(fi);
428 int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
430 #ifdef CONFIG_IP_ROUTE_MULTIPATH
431 struct rtnexthop *rtnh;
435 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
438 if (cfg->fc_oif || cfg->fc_gw) {
439 if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
440 (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
445 #ifdef CONFIG_IP_ROUTE_MULTIPATH
446 if (cfg->fc_mp == NULL)
450 remaining = cfg->fc_mp_len;
455 if (!rtnh_ok(rtnh, remaining))
458 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
461 attrlen = rtnh_attrlen(rtnh);
463 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
465 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
466 if (nla && nla_get_be32(nla) != nh->nh_gw)
468 #ifdef CONFIG_IP_ROUTE_CLASSID
469 nla = nla_find(attrs, attrlen, RTA_FLOW);
470 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
475 rtnh = rtnh_next(rtnh, &remaining);
476 } endfor_nexthops(fi);
486 * Semantics of nexthop is very messy by historical reasons.
487 * We have to take into account, that:
488 * a) gateway can be actually local interface address,
489 * so that gatewayed route is direct.
490 * b) gateway must be on-link address, possibly
491 * described not by an ifaddr, but also by a direct route.
492 * c) If both gateway and interface are specified, they should not
494 * d) If we use tunnel routes, gateway could be not on-link.
496 * Attempt to reconcile all of these (alas, self-contradictory) conditions
497 * results in pretty ugly and hairy code with obscure logic.
499 * I chose to generalized it instead, so that the size
500 * of code does not increase practically, but it becomes
502 * Every prefix is assigned a "scope" value: "host" is local address,
503 * "link" is direct route,
504 * [ ... "site" ... "interior" ... ]
505 * and "universe" is true gateway route with global meaning.
507 * Every prefix refers to a set of "nexthop"s (gw, oif),
508 * where gw must have narrower scope. This recursion stops
509 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
510 * which means that gw is forced to be on link.
512 * Code is still hairy, but now it is apparently logically
513 * consistent and very flexible. F.e. as by-product it allows
514 * to co-exists in peace independent exterior and interior
517 * Normally it looks as following.
519 * {universe prefix} -> (gw, oif) [scope link]
521 * |-> {link prefix} -> (gw, oif) [scope local]
523 * |-> {local prefix} (terminal node)
525 static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
530 struct net_device *dev;
532 net = cfg->fc_nlinfo.nl_net;
534 struct fib_result res;
536 if (nh->nh_flags & RTNH_F_ONLINK) {
538 if (cfg->fc_scope >= RT_SCOPE_LINK)
540 if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
542 dev = __dev_get_by_index(net, nh->nh_oif);
545 if (!(dev->flags & IFF_UP))
549 nh->nh_scope = RT_SCOPE_LINK;
554 struct flowi4 fl4 = {
556 .flowi4_scope = cfg->fc_scope + 1,
557 .flowi4_oif = nh->nh_oif,
560 /* It is not necessary, but requires a bit of thinking */
561 if (fl4.flowi4_scope < RT_SCOPE_LINK)
562 fl4.flowi4_scope = RT_SCOPE_LINK;
563 err = fib_lookup(net, &fl4, &res);
570 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
572 nh->nh_scope = res.scope;
573 nh->nh_oif = FIB_RES_OIF(res);
574 nh->nh_dev = dev = FIB_RES_DEV(res);
578 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
580 struct in_device *in_dev;
582 if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
587 in_dev = inetdev_by_index(net, nh->nh_oif);
591 if (!(in_dev->dev->flags & IFF_UP))
593 nh->nh_dev = in_dev->dev;
594 dev_hold(nh->nh_dev);
595 nh->nh_scope = RT_SCOPE_HOST;
603 static inline unsigned int fib_laddr_hashfn(__be32 val)
605 unsigned int mask = (fib_info_hash_size - 1);
607 return ((__force u32)val ^
608 ((__force u32)val >> 7) ^
609 ((__force u32)val >> 14)) & mask;
612 static struct hlist_head *fib_info_hash_alloc(int bytes)
614 if (bytes <= PAGE_SIZE)
615 return kzalloc(bytes, GFP_KERNEL);
617 return (struct hlist_head *)
618 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
622 static void fib_info_hash_free(struct hlist_head *hash, int bytes)
627 if (bytes <= PAGE_SIZE)
630 free_pages((unsigned long) hash, get_order(bytes));
633 static void fib_info_hash_move(struct hlist_head *new_info_hash,
634 struct hlist_head *new_laddrhash,
635 unsigned int new_size)
637 struct hlist_head *old_info_hash, *old_laddrhash;
638 unsigned int old_size = fib_info_hash_size;
639 unsigned int i, bytes;
641 spin_lock_bh(&fib_info_lock);
642 old_info_hash = fib_info_hash;
643 old_laddrhash = fib_info_laddrhash;
644 fib_info_hash_size = new_size;
646 for (i = 0; i < old_size; i++) {
647 struct hlist_head *head = &fib_info_hash[i];
648 struct hlist_node *node, *n;
651 hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
652 struct hlist_head *dest;
653 unsigned int new_hash;
655 hlist_del(&fi->fib_hash);
657 new_hash = fib_info_hashfn(fi);
658 dest = &new_info_hash[new_hash];
659 hlist_add_head(&fi->fib_hash, dest);
662 fib_info_hash = new_info_hash;
664 for (i = 0; i < old_size; i++) {
665 struct hlist_head *lhead = &fib_info_laddrhash[i];
666 struct hlist_node *node, *n;
669 hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
670 struct hlist_head *ldest;
671 unsigned int new_hash;
673 hlist_del(&fi->fib_lhash);
675 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
676 ldest = &new_laddrhash[new_hash];
677 hlist_add_head(&fi->fib_lhash, ldest);
680 fib_info_laddrhash = new_laddrhash;
682 spin_unlock_bh(&fib_info_lock);
684 bytes = old_size * sizeof(struct hlist_head *);
685 fib_info_hash_free(old_info_hash, bytes);
686 fib_info_hash_free(old_laddrhash, bytes);
689 __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
691 nh->nh_saddr = inet_select_addr(nh->nh_dev,
693 nh->nh_parent->fib_scope);
694 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
699 struct fib_info *fib_create_info(struct fib_config *cfg)
702 struct fib_info *fi = NULL;
703 struct fib_info *ofi;
705 struct net *net = cfg->fc_nlinfo.nl_net;
707 if (cfg->fc_type > RTN_MAX)
710 /* Fast check to catch the most weird cases */
711 if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
714 #ifdef CONFIG_IP_ROUTE_MULTIPATH
716 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
723 if (fib_info_cnt >= fib_info_hash_size) {
724 unsigned int new_size = fib_info_hash_size << 1;
725 struct hlist_head *new_info_hash;
726 struct hlist_head *new_laddrhash;
731 bytes = new_size * sizeof(struct hlist_head *);
732 new_info_hash = fib_info_hash_alloc(bytes);
733 new_laddrhash = fib_info_hash_alloc(bytes);
734 if (!new_info_hash || !new_laddrhash) {
735 fib_info_hash_free(new_info_hash, bytes);
736 fib_info_hash_free(new_laddrhash, bytes);
738 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
740 if (!fib_info_hash_size)
744 fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
748 fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
749 if (!fi->fib_metrics)
752 fi->fib_metrics = (u32 *) dst_default_metrics;
755 fi->fib_net = hold_net(net);
756 fi->fib_protocol = cfg->fc_protocol;
757 fi->fib_scope = cfg->fc_scope;
758 fi->fib_flags = cfg->fc_flags;
759 fi->fib_priority = cfg->fc_priority;
760 fi->fib_prefsrc = cfg->fc_prefsrc;
763 change_nexthops(fi) {
764 nexthop_nh->nh_parent = fi;
765 } endfor_nexthops(fi)
771 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
772 int type = nla_type(nla);
777 fi->fib_metrics[type - 1] = nla_get_u32(nla);
783 #ifdef CONFIG_IP_ROUTE_MULTIPATH
784 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
787 if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
789 if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
791 #ifdef CONFIG_IP_ROUTE_CLASSID
792 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
799 struct fib_nh *nh = fi->fib_nh;
801 nh->nh_oif = cfg->fc_oif;
802 nh->nh_gw = cfg->fc_gw;
803 nh->nh_flags = cfg->fc_flags;
804 #ifdef CONFIG_IP_ROUTE_CLASSID
805 nh->nh_tclassid = cfg->fc_flow;
807 #ifdef CONFIG_IP_ROUTE_MULTIPATH
812 if (fib_props[cfg->fc_type].error) {
813 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
817 switch (cfg->fc_type) {
829 if (cfg->fc_scope > RT_SCOPE_HOST)
832 if (cfg->fc_scope == RT_SCOPE_HOST) {
833 struct fib_nh *nh = fi->fib_nh;
835 /* Local address is added. */
836 if (nhs != 1 || nh->nh_gw)
838 nh->nh_scope = RT_SCOPE_NOWHERE;
839 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
841 if (nh->nh_dev == NULL)
844 change_nexthops(fi) {
845 err = fib_check_nh(cfg, fi, nexthop_nh);
848 } endfor_nexthops(fi)
851 if (fi->fib_prefsrc) {
852 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
853 fi->fib_prefsrc != cfg->fc_dst)
854 if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
858 change_nexthops(fi) {
859 fib_info_update_nh_saddr(net, nexthop_nh);
860 } endfor_nexthops(fi)
863 ofi = fib_find_info(fi);
872 atomic_inc(&fi->fib_clntref);
873 spin_lock_bh(&fib_info_lock);
874 hlist_add_head(&fi->fib_hash,
875 &fib_info_hash[fib_info_hashfn(fi)]);
876 if (fi->fib_prefsrc) {
877 struct hlist_head *head;
879 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
880 hlist_add_head(&fi->fib_lhash, head);
882 change_nexthops(fi) {
883 struct hlist_head *head;
886 if (!nexthop_nh->nh_dev)
888 hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
889 head = &fib_info_devhash[hash];
890 hlist_add_head(&nexthop_nh->nh_hash, head);
891 } endfor_nexthops(fi)
892 spin_unlock_bh(&fib_info_lock);
907 int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
908 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
909 struct fib_info *fi, unsigned int flags)
911 struct nlmsghdr *nlh;
914 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
918 rtm = nlmsg_data(nlh);
919 rtm->rtm_family = AF_INET;
920 rtm->rtm_dst_len = dst_len;
921 rtm->rtm_src_len = 0;
924 rtm->rtm_table = tb_id;
926 rtm->rtm_table = RT_TABLE_COMPAT;
927 NLA_PUT_U32(skb, RTA_TABLE, tb_id);
928 rtm->rtm_type = type;
929 rtm->rtm_flags = fi->fib_flags;
930 rtm->rtm_scope = fi->fib_scope;
931 rtm->rtm_protocol = fi->fib_protocol;
933 if (rtm->rtm_dst_len)
934 NLA_PUT_BE32(skb, RTA_DST, dst);
936 if (fi->fib_priority)
937 NLA_PUT_U32(skb, RTA_PRIORITY, fi->fib_priority);
939 if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
940 goto nla_put_failure;
943 NLA_PUT_BE32(skb, RTA_PREFSRC, fi->fib_prefsrc);
945 if (fi->fib_nhs == 1) {
946 if (fi->fib_nh->nh_gw)
947 NLA_PUT_BE32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw);
949 if (fi->fib_nh->nh_oif)
950 NLA_PUT_U32(skb, RTA_OIF, fi->fib_nh->nh_oif);
951 #ifdef CONFIG_IP_ROUTE_CLASSID
952 if (fi->fib_nh[0].nh_tclassid)
953 NLA_PUT_U32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid);
956 #ifdef CONFIG_IP_ROUTE_MULTIPATH
957 if (fi->fib_nhs > 1) {
958 struct rtnexthop *rtnh;
961 mp = nla_nest_start(skb, RTA_MULTIPATH);
963 goto nla_put_failure;
966 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
968 goto nla_put_failure;
970 rtnh->rtnh_flags = nh->nh_flags & 0xFF;
971 rtnh->rtnh_hops = nh->nh_weight - 1;
972 rtnh->rtnh_ifindex = nh->nh_oif;
975 NLA_PUT_BE32(skb, RTA_GATEWAY, nh->nh_gw);
976 #ifdef CONFIG_IP_ROUTE_CLASSID
978 NLA_PUT_U32(skb, RTA_FLOW, nh->nh_tclassid);
980 /* length of rtnetlink header + attributes */
981 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
982 } endfor_nexthops(fi);
984 nla_nest_end(skb, mp);
987 return nlmsg_end(skb, nlh);
990 nlmsg_cancel(skb, nlh);
996 * - local address disappeared -> we must delete all the entries
998 * - device went down -> we must shutdown all nexthops going via it.
1000 int fib_sync_down_addr(struct net *net, __be32 local)
1003 unsigned int hash = fib_laddr_hashfn(local);
1004 struct hlist_head *head = &fib_info_laddrhash[hash];
1005 struct hlist_node *node;
1006 struct fib_info *fi;
1008 if (fib_info_laddrhash == NULL || local == 0)
1011 hlist_for_each_entry(fi, node, head, fib_lhash) {
1012 if (!net_eq(fi->fib_net, net))
1014 if (fi->fib_prefsrc == local) {
1015 fi->fib_flags |= RTNH_F_DEAD;
1022 int fib_sync_down_dev(struct net_device *dev, int force)
1025 int scope = RT_SCOPE_NOWHERE;
1026 struct fib_info *prev_fi = NULL;
1027 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1028 struct hlist_head *head = &fib_info_devhash[hash];
1029 struct hlist_node *node;
1035 hlist_for_each_entry(nh, node, head, nh_hash) {
1036 struct fib_info *fi = nh->nh_parent;
1039 BUG_ON(!fi->fib_nhs);
1040 if (nh->nh_dev != dev || fi == prev_fi)
1044 change_nexthops(fi) {
1045 if (nexthop_nh->nh_flags & RTNH_F_DEAD)
1047 else if (nexthop_nh->nh_dev == dev &&
1048 nexthop_nh->nh_scope != scope) {
1049 nexthop_nh->nh_flags |= RTNH_F_DEAD;
1050 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1051 spin_lock_bh(&fib_multipath_lock);
1052 fi->fib_power -= nexthop_nh->nh_power;
1053 nexthop_nh->nh_power = 0;
1054 spin_unlock_bh(&fib_multipath_lock);
1058 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1059 if (force > 1 && nexthop_nh->nh_dev == dev) {
1064 } endfor_nexthops(fi)
1065 if (dead == fi->fib_nhs) {
1066 fi->fib_flags |= RTNH_F_DEAD;
1074 /* Must be invoked inside of an RCU protected region. */
1075 void fib_select_default(struct fib_result *res)
1077 struct fib_info *fi = NULL, *last_resort = NULL;
1078 struct list_head *fa_head = res->fa_head;
1079 struct fib_table *tb = res->table;
1080 int order = -1, last_idx = -1;
1081 struct fib_alias *fa;
1083 list_for_each_entry_rcu(fa, fa_head, fa_list) {
1084 struct fib_info *next_fi = fa->fa_info;
1086 if (next_fi->fib_scope != res->scope ||
1087 fa->fa_type != RTN_UNICAST)
1090 if (next_fi->fib_priority > res->fi->fib_priority)
1092 if (!next_fi->fib_nh[0].nh_gw ||
1093 next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
1096 fib_alias_accessed(fa);
1099 if (next_fi != res->fi)
1101 } else if (!fib_detect_death(fi, order, &last_resort,
1102 &last_idx, tb->tb_default)) {
1103 fib_result_assign(res, fi);
1104 tb->tb_default = order;
1111 if (order <= 0 || fi == NULL) {
1112 tb->tb_default = -1;
1116 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
1118 fib_result_assign(res, fi);
1119 tb->tb_default = order;
1124 fib_result_assign(res, last_resort);
1125 tb->tb_default = last_idx;
1130 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1133 * Dead device goes up. We wake up dead nexthops.
1134 * It takes sense only on multipath routes.
1136 int fib_sync_up(struct net_device *dev)
1138 struct fib_info *prev_fi;
1140 struct hlist_head *head;
1141 struct hlist_node *node;
1145 if (!(dev->flags & IFF_UP))
1149 hash = fib_devindex_hashfn(dev->ifindex);
1150 head = &fib_info_devhash[hash];
1153 hlist_for_each_entry(nh, node, head, nh_hash) {
1154 struct fib_info *fi = nh->nh_parent;
1157 BUG_ON(!fi->fib_nhs);
1158 if (nh->nh_dev != dev || fi == prev_fi)
1163 change_nexthops(fi) {
1164 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
1168 if (nexthop_nh->nh_dev == NULL ||
1169 !(nexthop_nh->nh_dev->flags & IFF_UP))
1171 if (nexthop_nh->nh_dev != dev ||
1172 !__in_dev_get_rtnl(dev))
1175 spin_lock_bh(&fib_multipath_lock);
1176 nexthop_nh->nh_power = 0;
1177 nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
1178 spin_unlock_bh(&fib_multipath_lock);
1179 } endfor_nexthops(fi)
1182 fi->fib_flags &= ~RTNH_F_DEAD;
1191 * The algorithm is suboptimal, but it provides really
1192 * fair weighted route distribution.
1194 void fib_select_multipath(struct fib_result *res)
1196 struct fib_info *fi = res->fi;
1199 spin_lock_bh(&fib_multipath_lock);
1200 if (fi->fib_power <= 0) {
1202 change_nexthops(fi) {
1203 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
1204 power += nexthop_nh->nh_weight;
1205 nexthop_nh->nh_power = nexthop_nh->nh_weight;
1207 } endfor_nexthops(fi);
1208 fi->fib_power = power;
1210 spin_unlock_bh(&fib_multipath_lock);
1211 /* Race condition: route has just become dead. */
1218 /* w should be random number [0..fi->fib_power-1],
1219 * it is pretty bad approximation.
1222 w = jiffies % fi->fib_power;
1224 change_nexthops(fi) {
1225 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
1226 nexthop_nh->nh_power) {
1227 w -= nexthop_nh->nh_power;
1229 nexthop_nh->nh_power--;
1231 res->nh_sel = nhsel;
1232 spin_unlock_bh(&fib_multipath_lock);
1236 } endfor_nexthops(fi);
1238 /* Race condition: route has just become dead. */
1240 spin_unlock_bh(&fib_multipath_lock);