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 <linux/bitops.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/jiffies.h>
22 #include <linux/string.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
25 #include <linux/errno.h>
27 #include <linux/inet.h>
28 #include <linux/inetdevice.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/proc_fs.h>
32 #include <linux/skbuff.h>
33 #include <linux/init.h>
34 #include <linux/slab.h>
38 #include <net/protocol.h>
39 #include <net/route.h>
42 #include <net/ip_fib.h>
43 #include <net/netlink.h>
44 #include <net/nexthop.h>
46 #include "fib_lookup.h"
48 static DEFINE_SPINLOCK(fib_info_lock);
49 static struct hlist_head *fib_info_hash;
50 static struct hlist_head *fib_info_laddrhash;
51 static unsigned int fib_info_hash_size;
52 static unsigned int fib_info_cnt;
54 #define DEVINDEX_HASHBITS 8
55 #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
56 static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
58 #ifdef CONFIG_IP_ROUTE_MULTIPATH
60 static DEFINE_SPINLOCK(fib_multipath_lock);
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; \
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++)
74 #else /* CONFIG_IP_ROUTE_MULTIPATH */
76 /* Hope, that gcc will optimize it to get rid of dummy loop */
78 #define for_nexthops(fi) { \
79 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
80 for (nhsel = 0; nhsel < 1; nhsel++)
82 #define change_nexthops(fi) { \
84 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
85 for (nhsel = 0; nhsel < 1; nhsel++)
87 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
89 #define endfor_nexthops(fi) }
92 const struct fib_prop fib_props[RTN_MAX + 1] = {
95 .scope = RT_SCOPE_NOWHERE,
99 .scope = RT_SCOPE_UNIVERSE,
103 .scope = RT_SCOPE_HOST,
107 .scope = RT_SCOPE_LINK,
111 .scope = RT_SCOPE_LINK,
115 .scope = RT_SCOPE_UNIVERSE,
119 .scope = RT_SCOPE_UNIVERSE,
121 [RTN_UNREACHABLE] = {
122 .error = -EHOSTUNREACH,
123 .scope = RT_SCOPE_UNIVERSE,
127 .scope = RT_SCOPE_UNIVERSE,
131 .scope = RT_SCOPE_UNIVERSE,
135 .scope = RT_SCOPE_NOWHERE,
139 .scope = RT_SCOPE_NOWHERE,
143 static void free_nh_exceptions(struct fib_nh *nh)
145 struct fnhe_hash_bucket *hash = nh->nh_exceptions;
148 for (i = 0; i < FNHE_HASH_SIZE; i++) {
149 struct fib_nh_exception *fnhe;
151 fnhe = rcu_dereference_protected(hash[i].chain, 1);
153 struct fib_nh_exception *next;
155 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
164 /* Release a nexthop info record */
165 static void free_fib_info_rcu(struct rcu_head *head)
167 struct fib_info *fi = container_of(head, struct fib_info, rcu);
169 change_nexthops(fi) {
170 if (nexthop_nh->nh_dev)
171 dev_put(nexthop_nh->nh_dev);
172 if (nexthop_nh->nh_exceptions)
173 free_nh_exceptions(nexthop_nh);
174 } endfor_nexthops(fi);
176 release_net(fi->fib_net);
177 if (fi->fib_metrics != (u32 *) dst_default_metrics)
178 kfree(fi->fib_metrics);
182 void free_fib_info(struct fib_info *fi)
184 if (fi->fib_dead == 0) {
185 pr_warn("Freeing alive fib_info %p\n", fi);
189 #ifdef CONFIG_IP_ROUTE_CLASSID
190 change_nexthops(fi) {
191 if (nexthop_nh->nh_tclassid)
192 fi->fib_net->ipv4.fib_num_tclassid_users--;
193 } endfor_nexthops(fi);
195 call_rcu(&fi->rcu, free_fib_info_rcu);
198 void fib_release_info(struct fib_info *fi)
200 spin_lock_bh(&fib_info_lock);
201 if (fi && --fi->fib_treeref == 0) {
202 hlist_del(&fi->fib_hash);
204 hlist_del(&fi->fib_lhash);
205 change_nexthops(fi) {
206 if (!nexthop_nh->nh_dev)
208 hlist_del(&nexthop_nh->nh_hash);
209 } endfor_nexthops(fi)
213 spin_unlock_bh(&fib_info_lock);
216 static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
218 const struct fib_nh *onh = ofi->fib_nh;
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 ||
227 #ifdef CONFIG_IP_ROUTE_CLASSID
228 nh->nh_tclassid != onh->nh_tclassid ||
230 ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
233 } endfor_nexthops(fi);
237 static inline unsigned int fib_devindex_hashfn(unsigned int val)
239 unsigned int mask = DEVINDEX_HASHSIZE - 1;
242 (val >> DEVINDEX_HASHBITS) ^
243 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
246 static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
248 unsigned int mask = (fib_info_hash_size - 1);
249 unsigned int val = fi->fib_nhs;
251 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
252 val ^= (__force u32)fi->fib_prefsrc;
253 val ^= fi->fib_priority;
255 val ^= fib_devindex_hashfn(nh->nh_oif);
256 } endfor_nexthops(fi)
258 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
261 static struct fib_info *fib_find_info(const struct fib_info *nfi)
263 struct hlist_head *head;
264 struct hlist_node *node;
268 hash = fib_info_hashfn(nfi);
269 head = &fib_info_hash[hash];
271 hlist_for_each_entry(fi, node, head, fib_hash) {
272 if (!net_eq(fi->fib_net, nfi->fib_net))
274 if (fi->fib_nhs != nfi->fib_nhs)
276 if (nfi->fib_protocol == fi->fib_protocol &&
277 nfi->fib_scope == fi->fib_scope &&
278 nfi->fib_prefsrc == fi->fib_prefsrc &&
279 nfi->fib_priority == fi->fib_priority &&
280 memcmp(nfi->fib_metrics, fi->fib_metrics,
281 sizeof(u32) * RTAX_MAX) == 0 &&
282 ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
283 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
290 /* Check, that the gateway is already configured.
291 * Used only by redirect accept routine.
293 int ip_fib_check_default(__be32 gw, struct net_device *dev)
295 struct hlist_head *head;
296 struct hlist_node *node;
300 spin_lock(&fib_info_lock);
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 &&
307 !(nh->nh_flags & RTNH_F_DEAD)) {
308 spin_unlock(&fib_info_lock);
313 spin_unlock(&fib_info_lock);
318 static inline size_t fib_nlmsg_size(struct fib_info *fi)
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 */
326 /* space for nested metrics */
327 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
330 /* Also handles the special case fib_nhs == 1 */
332 /* each nexthop is packed in an attribute */
333 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
335 /* may contain flow and gateway attribute */
336 nhsize += 2 * nla_total_size(4);
338 /* all nexthops are packed in a nested attribute */
339 payload += nla_total_size(fi->fib_nhs * nhsize);
345 void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
346 int dst_len, u32 tb_id, struct nl_info *info,
347 unsigned int nlm_flags)
350 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
353 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
357 err = fib_dump_info(skb, info->pid, seq, event, tb_id,
358 fa->fa_type, key, dst_len,
359 fa->fa_tos, fa->fa_info, nlm_flags);
361 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
362 WARN_ON(err == -EMSGSIZE);
366 rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
367 info->nlh, GFP_KERNEL);
371 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
374 /* Return the first fib alias matching TOS with
375 * priority less than or equal to PRIO.
377 struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
380 struct fib_alias *fa;
381 list_for_each_entry(fa, fah, fa_list) {
382 if (fa->fa_tos > tos)
384 if (fa->fa_info->fib_priority >= prio ||
392 int fib_detect_death(struct fib_info *fi, int order,
393 struct fib_info **last_resort, int *last_idx, int dflt)
396 int state = NUD_NONE;
398 n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
400 state = n->nud_state;
403 if (state == NUD_REACHABLE)
405 if ((state & NUD_VALID) && order != dflt)
407 if ((state & NUD_VALID) ||
408 (*last_idx < 0 && order > dflt)) {
415 #ifdef CONFIG_IP_ROUTE_MULTIPATH
417 static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
421 while (rtnh_ok(rtnh, remaining)) {
423 rtnh = rtnh_next(rtnh, &remaining);
426 /* leftover implies invalid nexthop configuration, discard it */
427 return remaining > 0 ? 0 : nhs;
430 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
431 int remaining, struct fib_config *cfg)
433 change_nexthops(fi) {
436 if (!rtnh_ok(rtnh, remaining))
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;
444 attrlen = rtnh_attrlen(rtnh);
446 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
448 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
449 nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
450 #ifdef CONFIG_IP_ROUTE_CLASSID
451 nla = nla_find(attrs, attrlen, RTA_FLOW);
452 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
453 if (nexthop_nh->nh_tclassid)
454 fi->fib_net->ipv4.fib_num_tclassid_users++;
458 rtnh = rtnh_next(rtnh, &remaining);
459 } endfor_nexthops(fi);
466 int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
468 #ifdef CONFIG_IP_ROUTE_MULTIPATH
469 struct rtnexthop *rtnh;
473 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
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))
483 #ifdef CONFIG_IP_ROUTE_MULTIPATH
484 if (cfg->fc_mp == NULL)
488 remaining = cfg->fc_mp_len;
493 if (!rtnh_ok(rtnh, remaining))
496 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
499 attrlen = rtnh_attrlen(rtnh);
501 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
503 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
504 if (nla && nla_get_be32(nla) != nh->nh_gw)
506 #ifdef CONFIG_IP_ROUTE_CLASSID
507 nla = nla_find(attrs, attrlen, RTA_FLOW);
508 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
513 rtnh = rtnh_next(rtnh, &remaining);
514 } endfor_nexthops(fi);
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
532 * d) If we use tunnel routes, gateway could be not on-link.
534 * Attempt to reconcile all of these (alas, self-contradictory) conditions
535 * results in pretty ugly and hairy code with obscure logic.
537 * I chose to generalized it instead, so that the size
538 * of code does not increase practically, but it becomes
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.
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.
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
555 * Normally it looks as following.
557 * {universe prefix} -> (gw, oif) [scope link]
559 * |-> {link prefix} -> (gw, oif) [scope local]
561 * |-> {local prefix} (terminal node)
563 static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
568 struct net_device *dev;
570 net = cfg->fc_nlinfo.nl_net;
572 struct fib_result res;
574 if (nh->nh_flags & RTNH_F_ONLINK) {
576 if (cfg->fc_scope >= RT_SCOPE_LINK)
578 if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
580 dev = __dev_get_by_index(net, nh->nh_oif);
583 if (!(dev->flags & IFF_UP))
587 nh->nh_scope = RT_SCOPE_LINK;
592 struct flowi4 fl4 = {
594 .flowi4_scope = cfg->fc_scope + 1,
595 .flowi4_oif = nh->nh_oif,
598 /* It is not necessary, but requires a bit of thinking */
599 if (fl4.flowi4_scope < RT_SCOPE_LINK)
600 fl4.flowi4_scope = RT_SCOPE_LINK;
601 err = fib_lookup(net, &fl4, &res);
608 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
610 nh->nh_scope = res.scope;
611 nh->nh_oif = FIB_RES_OIF(res);
612 nh->nh_dev = dev = FIB_RES_DEV(res);
616 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
618 struct in_device *in_dev;
620 if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
625 in_dev = inetdev_by_index(net, nh->nh_oif);
629 if (!(in_dev->dev->flags & IFF_UP))
631 nh->nh_dev = in_dev->dev;
632 dev_hold(nh->nh_dev);
633 nh->nh_scope = RT_SCOPE_HOST;
641 static inline unsigned int fib_laddr_hashfn(__be32 val)
643 unsigned int mask = (fib_info_hash_size - 1);
645 return ((__force u32)val ^
646 ((__force u32)val >> 7) ^
647 ((__force u32)val >> 14)) & mask;
650 static struct hlist_head *fib_info_hash_alloc(int bytes)
652 if (bytes <= PAGE_SIZE)
653 return kzalloc(bytes, GFP_KERNEL);
655 return (struct hlist_head *)
656 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
660 static void fib_info_hash_free(struct hlist_head *hash, int bytes)
665 if (bytes <= PAGE_SIZE)
668 free_pages((unsigned long) hash, get_order(bytes));
671 static void fib_info_hash_move(struct hlist_head *new_info_hash,
672 struct hlist_head *new_laddrhash,
673 unsigned int new_size)
675 struct hlist_head *old_info_hash, *old_laddrhash;
676 unsigned int old_size = fib_info_hash_size;
677 unsigned int i, bytes;
679 spin_lock_bh(&fib_info_lock);
680 old_info_hash = fib_info_hash;
681 old_laddrhash = fib_info_laddrhash;
682 fib_info_hash_size = new_size;
684 for (i = 0; i < old_size; i++) {
685 struct hlist_head *head = &fib_info_hash[i];
686 struct hlist_node *node, *n;
689 hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
690 struct hlist_head *dest;
691 unsigned int new_hash;
693 hlist_del(&fi->fib_hash);
695 new_hash = fib_info_hashfn(fi);
696 dest = &new_info_hash[new_hash];
697 hlist_add_head(&fi->fib_hash, dest);
700 fib_info_hash = new_info_hash;
702 for (i = 0; i < old_size; i++) {
703 struct hlist_head *lhead = &fib_info_laddrhash[i];
704 struct hlist_node *node, *n;
707 hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
708 struct hlist_head *ldest;
709 unsigned int new_hash;
711 hlist_del(&fi->fib_lhash);
713 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
714 ldest = &new_laddrhash[new_hash];
715 hlist_add_head(&fi->fib_lhash, ldest);
718 fib_info_laddrhash = new_laddrhash;
720 spin_unlock_bh(&fib_info_lock);
722 bytes = old_size * sizeof(struct hlist_head *);
723 fib_info_hash_free(old_info_hash, bytes);
724 fib_info_hash_free(old_laddrhash, bytes);
727 __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
729 nh->nh_saddr = inet_select_addr(nh->nh_dev,
731 nh->nh_parent->fib_scope);
732 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
737 struct fib_info *fib_create_info(struct fib_config *cfg)
740 struct fib_info *fi = NULL;
741 struct fib_info *ofi;
743 struct net *net = cfg->fc_nlinfo.nl_net;
745 if (cfg->fc_type > RTN_MAX)
748 /* Fast check to catch the most weird cases */
749 if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
752 #ifdef CONFIG_IP_ROUTE_MULTIPATH
754 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
761 if (fib_info_cnt >= fib_info_hash_size) {
762 unsigned int new_size = fib_info_hash_size << 1;
763 struct hlist_head *new_info_hash;
764 struct hlist_head *new_laddrhash;
769 bytes = new_size * sizeof(struct hlist_head *);
770 new_info_hash = fib_info_hash_alloc(bytes);
771 new_laddrhash = fib_info_hash_alloc(bytes);
772 if (!new_info_hash || !new_laddrhash) {
773 fib_info_hash_free(new_info_hash, bytes);
774 fib_info_hash_free(new_laddrhash, bytes);
776 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
778 if (!fib_info_hash_size)
782 fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
786 fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
787 if (!fi->fib_metrics)
790 fi->fib_metrics = (u32 *) dst_default_metrics;
793 fi->fib_net = hold_net(net);
794 fi->fib_protocol = cfg->fc_protocol;
795 fi->fib_scope = cfg->fc_scope;
796 fi->fib_flags = cfg->fc_flags;
797 fi->fib_priority = cfg->fc_priority;
798 fi->fib_prefsrc = cfg->fc_prefsrc;
801 change_nexthops(fi) {
802 nexthop_nh->nh_parent = fi;
803 } endfor_nexthops(fi)
809 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
810 int type = nla_type(nla);
817 val = nla_get_u32(nla);
818 if (type == RTAX_ADVMSS && val > 65535 - 40)
820 if (type == RTAX_MTU && val > 65535 - 15)
822 fi->fib_metrics[type - 1] = val;
828 #ifdef CONFIG_IP_ROUTE_MULTIPATH
829 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
832 if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
834 if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
836 #ifdef CONFIG_IP_ROUTE_CLASSID
837 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
844 struct fib_nh *nh = fi->fib_nh;
846 nh->nh_oif = cfg->fc_oif;
847 nh->nh_gw = cfg->fc_gw;
848 nh->nh_flags = cfg->fc_flags;
849 #ifdef CONFIG_IP_ROUTE_CLASSID
850 nh->nh_tclassid = cfg->fc_flow;
852 fi->fib_net->ipv4.fib_num_tclassid_users++;
854 #ifdef CONFIG_IP_ROUTE_MULTIPATH
859 if (fib_props[cfg->fc_type].error) {
860 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
864 switch (cfg->fc_type) {
876 if (cfg->fc_scope > RT_SCOPE_HOST)
879 if (cfg->fc_scope == RT_SCOPE_HOST) {
880 struct fib_nh *nh = fi->fib_nh;
882 /* Local address is added. */
883 if (nhs != 1 || nh->nh_gw)
885 nh->nh_scope = RT_SCOPE_NOWHERE;
886 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
888 if (nh->nh_dev == NULL)
891 change_nexthops(fi) {
892 err = fib_check_nh(cfg, fi, nexthop_nh);
895 } endfor_nexthops(fi)
898 if (fi->fib_prefsrc) {
899 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
900 fi->fib_prefsrc != cfg->fc_dst)
901 if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
905 change_nexthops(fi) {
906 fib_info_update_nh_saddr(net, nexthop_nh);
907 } endfor_nexthops(fi)
910 ofi = fib_find_info(fi);
919 atomic_inc(&fi->fib_clntref);
920 spin_lock_bh(&fib_info_lock);
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;
926 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
927 hlist_add_head(&fi->fib_lhash, head);
929 change_nexthops(fi) {
930 struct hlist_head *head;
933 if (!nexthop_nh->nh_dev)
935 hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
936 head = &fib_info_devhash[hash];
937 hlist_add_head(&nexthop_nh->nh_hash, head);
938 } endfor_nexthops(fi)
939 spin_unlock_bh(&fib_info_lock);
954 int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
955 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
956 struct fib_info *fi, unsigned int flags)
958 struct nlmsghdr *nlh;
961 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
965 rtm = nlmsg_data(nlh);
966 rtm->rtm_family = AF_INET;
967 rtm->rtm_dst_len = dst_len;
968 rtm->rtm_src_len = 0;
971 rtm->rtm_table = tb_id;
973 rtm->rtm_table = RT_TABLE_COMPAT;
974 if (nla_put_u32(skb, RTA_TABLE, tb_id))
975 goto nla_put_failure;
976 rtm->rtm_type = type;
977 rtm->rtm_flags = fi->fib_flags;
978 rtm->rtm_scope = fi->fib_scope;
979 rtm->rtm_protocol = fi->fib_protocol;
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;
987 if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
988 goto nla_put_failure;
990 if (fi->fib_prefsrc &&
991 nla_put_be32(skb, RTA_PREFSRC, fi->fib_prefsrc))
992 goto nla_put_failure;
993 if (fi->fib_nhs == 1) {
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;
1000 #ifdef CONFIG_IP_ROUTE_CLASSID
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;
1006 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1007 if (fi->fib_nhs > 1) {
1008 struct rtnexthop *rtnh;
1011 mp = nla_nest_start(skb, RTA_MULTIPATH);
1013 goto nla_put_failure;
1016 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1018 goto nla_put_failure;
1020 rtnh->rtnh_flags = nh->nh_flags & 0xFF;
1021 rtnh->rtnh_hops = nh->nh_weight - 1;
1022 rtnh->rtnh_ifindex = nh->nh_oif;
1025 nla_put_be32(skb, RTA_GATEWAY, nh->nh_gw))
1026 goto nla_put_failure;
1027 #ifdef CONFIG_IP_ROUTE_CLASSID
1028 if (nh->nh_tclassid &&
1029 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1030 goto nla_put_failure;
1032 /* length of rtnetlink header + attributes */
1033 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
1034 } endfor_nexthops(fi);
1036 nla_nest_end(skb, mp);
1039 return nlmsg_end(skb, nlh);
1042 nlmsg_cancel(skb, nlh);
1048 * - local address disappeared -> we must delete all the entries
1050 * - device went down -> we must shutdown all nexthops going via it.
1052 int fib_sync_down_addr(struct net *net, __be32 local)
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;
1060 if (fib_info_laddrhash == NULL || local == 0)
1063 hlist_for_each_entry(fi, node, head, fib_lhash) {
1064 if (!net_eq(fi->fib_net, net))
1066 if (fi->fib_prefsrc == local) {
1067 fi->fib_flags |= RTNH_F_DEAD;
1074 int fib_sync_down_dev(struct net_device *dev, int force)
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;
1087 hlist_for_each_entry(nh, node, head, nh_hash) {
1088 struct fib_info *fi = nh->nh_parent;
1091 BUG_ON(!fi->fib_nhs);
1092 if (nh->nh_dev != dev || fi == prev_fi)
1096 change_nexthops(fi) {
1097 if (nexthop_nh->nh_flags & RTNH_F_DEAD)
1099 else if (nexthop_nh->nh_dev == dev &&
1100 nexthop_nh->nh_scope != scope) {
1101 nexthop_nh->nh_flags |= RTNH_F_DEAD;
1102 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1103 spin_lock_bh(&fib_multipath_lock);
1104 fi->fib_power -= nexthop_nh->nh_power;
1105 nexthop_nh->nh_power = 0;
1106 spin_unlock_bh(&fib_multipath_lock);
1110 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1111 if (force > 1 && nexthop_nh->nh_dev == dev) {
1116 } endfor_nexthops(fi)
1117 if (dead == fi->fib_nhs) {
1118 fi->fib_flags |= RTNH_F_DEAD;
1126 /* Must be invoked inside of an RCU protected region. */
1127 void fib_select_default(struct fib_result *res)
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;
1135 list_for_each_entry_rcu(fa, fa_head, fa_list) {
1136 struct fib_info *next_fi = fa->fa_info;
1138 if (next_fi->fib_scope != res->scope ||
1139 fa->fa_type != RTN_UNICAST)
1142 if (next_fi->fib_priority > res->fi->fib_priority)
1144 if (!next_fi->fib_nh[0].nh_gw ||
1145 next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
1148 fib_alias_accessed(fa);
1151 if (next_fi != res->fi)
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;
1163 if (order <= 0 || fi == NULL) {
1164 tb->tb_default = -1;
1168 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
1170 fib_result_assign(res, fi);
1171 tb->tb_default = order;
1176 fib_result_assign(res, last_resort);
1177 tb->tb_default = last_idx;
1182 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1185 * Dead device goes up. We wake up dead nexthops.
1186 * It takes sense only on multipath routes.
1188 int fib_sync_up(struct net_device *dev)
1190 struct fib_info *prev_fi;
1192 struct hlist_head *head;
1193 struct hlist_node *node;
1197 if (!(dev->flags & IFF_UP))
1201 hash = fib_devindex_hashfn(dev->ifindex);
1202 head = &fib_info_devhash[hash];
1205 hlist_for_each_entry(nh, node, head, nh_hash) {
1206 struct fib_info *fi = nh->nh_parent;
1209 BUG_ON(!fi->fib_nhs);
1210 if (nh->nh_dev != dev || fi == prev_fi)
1215 change_nexthops(fi) {
1216 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
1220 if (nexthop_nh->nh_dev == NULL ||
1221 !(nexthop_nh->nh_dev->flags & IFF_UP))
1223 if (nexthop_nh->nh_dev != dev ||
1224 !__in_dev_get_rtnl(dev))
1227 spin_lock_bh(&fib_multipath_lock);
1228 nexthop_nh->nh_power = 0;
1229 nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
1230 spin_unlock_bh(&fib_multipath_lock);
1231 } endfor_nexthops(fi)
1234 fi->fib_flags &= ~RTNH_F_DEAD;
1243 * The algorithm is suboptimal, but it provides really
1244 * fair weighted route distribution.
1246 void fib_select_multipath(struct fib_result *res)
1248 struct fib_info *fi = res->fi;
1251 spin_lock_bh(&fib_multipath_lock);
1252 if (fi->fib_power <= 0) {
1254 change_nexthops(fi) {
1255 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
1256 power += nexthop_nh->nh_weight;
1257 nexthop_nh->nh_power = nexthop_nh->nh_weight;
1259 } endfor_nexthops(fi);
1260 fi->fib_power = power;
1262 spin_unlock_bh(&fib_multipath_lock);
1263 /* Race condition: route has just become dead. */
1270 /* w should be random number [0..fi->fib_power-1],
1271 * it is pretty bad approximation.
1274 w = jiffies % fi->fib_power;
1276 change_nexthops(fi) {
1277 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
1278 nexthop_nh->nh_power) {
1279 w -= nexthop_nh->nh_power;
1281 nexthop_nh->nh_power--;
1283 res->nh_sel = nhsel;
1284 spin_unlock_bh(&fib_multipath_lock);
1288 } endfor_nexthops(fi);
1290 /* Race condition: route has just become dead. */
1292 spin_unlock_bh(&fib_multipath_lock);