11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * IPv4 Forwarding Information Base: semantics. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 111da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 121da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 131da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <asm/uaccess.h> 171da177e4SLinus Torvalds #include <linux/bitops.h> 181da177e4SLinus Torvalds #include <linux/types.h> 191da177e4SLinus Torvalds #include <linux/kernel.h> 201da177e4SLinus Torvalds #include <linux/jiffies.h> 211da177e4SLinus Torvalds #include <linux/mm.h> 221da177e4SLinus Torvalds #include <linux/string.h> 231da177e4SLinus Torvalds #include <linux/socket.h> 241da177e4SLinus Torvalds #include <linux/sockios.h> 251da177e4SLinus Torvalds #include <linux/errno.h> 261da177e4SLinus Torvalds #include <linux/in.h> 271da177e4SLinus Torvalds #include <linux/inet.h> 2814c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h> 291da177e4SLinus Torvalds #include <linux/netdevice.h> 301da177e4SLinus Torvalds #include <linux/if_arp.h> 311da177e4SLinus Torvalds #include <linux/proc_fs.h> 321da177e4SLinus Torvalds #include <linux/skbuff.h> 331da177e4SLinus Torvalds #include <linux/init.h> 345a0e3ad6STejun Heo #include <linux/slab.h> 351da177e4SLinus Torvalds 3614c85021SArnaldo Carvalho de Melo #include <net/arp.h> 371da177e4SLinus Torvalds #include <net/ip.h> 381da177e4SLinus Torvalds #include <net/protocol.h> 391da177e4SLinus Torvalds #include <net/route.h> 401da177e4SLinus Torvalds #include <net/tcp.h> 411da177e4SLinus Torvalds #include <net/sock.h> 421da177e4SLinus Torvalds #include <net/ip_fib.h> 43f21c7bc5SThomas Graf #include <net/netlink.h> 444e902c57SThomas Graf #include <net/nexthop.h> 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds #include "fib_lookup.h" 471da177e4SLinus Torvalds 48832b4c5eSStephen Hemminger static DEFINE_SPINLOCK(fib_info_lock); 491da177e4SLinus Torvalds static struct hlist_head *fib_info_hash; 501da177e4SLinus Torvalds static struct hlist_head *fib_info_laddrhash; 51123b9731SDavid S. Miller static unsigned int fib_info_hash_size; 521da177e4SLinus Torvalds static unsigned int fib_info_cnt; 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds #define DEVINDEX_HASHBITS 8 551da177e4SLinus Torvalds #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS) 561da177e4SLinus Torvalds static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE]; 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds static DEFINE_SPINLOCK(fib_multipath_lock); 611da177e4SLinus Torvalds 626a31d2a9SEric Dumazet #define for_nexthops(fi) { \ 636a31d2a9SEric Dumazet int nhsel; const struct fib_nh *nh; \ 646a31d2a9SEric Dumazet for (nhsel = 0, nh = (fi)->fib_nh; \ 656a31d2a9SEric Dumazet nhsel < (fi)->fib_nhs; \ 666a31d2a9SEric Dumazet nh++, nhsel++) 671da177e4SLinus Torvalds 686a31d2a9SEric Dumazet #define change_nexthops(fi) { \ 696a31d2a9SEric Dumazet int nhsel; struct fib_nh *nexthop_nh; \ 706a31d2a9SEric Dumazet for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \ 716a31d2a9SEric Dumazet nhsel < (fi)->fib_nhs; \ 726a31d2a9SEric Dumazet nexthop_nh++, nhsel++) 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds #else /* CONFIG_IP_ROUTE_MULTIPATH */ 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds /* Hope, that gcc will optimize it to get rid of dummy loop */ 771da177e4SLinus Torvalds 786a31d2a9SEric Dumazet #define for_nexthops(fi) { \ 796a31d2a9SEric Dumazet int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \ 801da177e4SLinus Torvalds for (nhsel = 0; nhsel < 1; nhsel++) 811da177e4SLinus Torvalds 826a31d2a9SEric Dumazet #define change_nexthops(fi) { \ 836a31d2a9SEric Dumazet int nhsel; \ 846a31d2a9SEric Dumazet struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \ 851da177e4SLinus Torvalds for (nhsel = 0; nhsel < 1; nhsel++) 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds #endif /* CONFIG_IP_ROUTE_MULTIPATH */ 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds #define endfor_nexthops(fi) } 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds 923be0686bSDavid S. Miller const struct fib_prop fib_props[RTN_MAX + 1] = { 936a31d2a9SEric Dumazet [RTN_UNSPEC] = { 941da177e4SLinus Torvalds .error = 0, 951da177e4SLinus Torvalds .scope = RT_SCOPE_NOWHERE, 966a31d2a9SEric Dumazet }, 976a31d2a9SEric Dumazet [RTN_UNICAST] = { 981da177e4SLinus Torvalds .error = 0, 991da177e4SLinus Torvalds .scope = RT_SCOPE_UNIVERSE, 1006a31d2a9SEric Dumazet }, 1016a31d2a9SEric Dumazet [RTN_LOCAL] = { 1021da177e4SLinus Torvalds .error = 0, 1031da177e4SLinus Torvalds .scope = RT_SCOPE_HOST, 1046a31d2a9SEric Dumazet }, 1056a31d2a9SEric Dumazet [RTN_BROADCAST] = { 1061da177e4SLinus Torvalds .error = 0, 1071da177e4SLinus Torvalds .scope = RT_SCOPE_LINK, 1086a31d2a9SEric Dumazet }, 1096a31d2a9SEric Dumazet [RTN_ANYCAST] = { 1101da177e4SLinus Torvalds .error = 0, 1111da177e4SLinus Torvalds .scope = RT_SCOPE_LINK, 1126a31d2a9SEric Dumazet }, 1136a31d2a9SEric Dumazet [RTN_MULTICAST] = { 1141da177e4SLinus Torvalds .error = 0, 1151da177e4SLinus Torvalds .scope = RT_SCOPE_UNIVERSE, 1166a31d2a9SEric Dumazet }, 1176a31d2a9SEric Dumazet [RTN_BLACKHOLE] = { 1181da177e4SLinus Torvalds .error = -EINVAL, 1191da177e4SLinus Torvalds .scope = RT_SCOPE_UNIVERSE, 1206a31d2a9SEric Dumazet }, 1216a31d2a9SEric Dumazet [RTN_UNREACHABLE] = { 1221da177e4SLinus Torvalds .error = -EHOSTUNREACH, 1231da177e4SLinus Torvalds .scope = RT_SCOPE_UNIVERSE, 1246a31d2a9SEric Dumazet }, 1256a31d2a9SEric Dumazet [RTN_PROHIBIT] = { 1261da177e4SLinus Torvalds .error = -EACCES, 1271da177e4SLinus Torvalds .scope = RT_SCOPE_UNIVERSE, 1286a31d2a9SEric Dumazet }, 1296a31d2a9SEric Dumazet [RTN_THROW] = { 1301da177e4SLinus Torvalds .error = -EAGAIN, 1311da177e4SLinus Torvalds .scope = RT_SCOPE_UNIVERSE, 1326a31d2a9SEric Dumazet }, 1336a31d2a9SEric Dumazet [RTN_NAT] = { 1341da177e4SLinus Torvalds .error = -EINVAL, 1351da177e4SLinus Torvalds .scope = RT_SCOPE_NOWHERE, 1366a31d2a9SEric Dumazet }, 1376a31d2a9SEric Dumazet [RTN_XRESOLVE] = { 1381da177e4SLinus Torvalds .error = -EINVAL, 1391da177e4SLinus Torvalds .scope = RT_SCOPE_NOWHERE, 1406a31d2a9SEric Dumazet }, 1411da177e4SLinus Torvalds }; 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds /* Release a nexthop info record */ 14419c1ea14SYan, Zheng static void free_fib_info_rcu(struct rcu_head *head) 14519c1ea14SYan, Zheng { 14619c1ea14SYan, Zheng struct fib_info *fi = container_of(head, struct fib_info, rcu); 14719c1ea14SYan, Zheng 148*e49cc0daSYanmin Zhang change_nexthops(fi) { 149*e49cc0daSYanmin Zhang if (nexthop_nh->nh_dev) 150*e49cc0daSYanmin Zhang dev_put(nexthop_nh->nh_dev); 151*e49cc0daSYanmin Zhang } endfor_nexthops(fi); 152*e49cc0daSYanmin Zhang 153*e49cc0daSYanmin Zhang release_net(fi->fib_net); 15419c1ea14SYan, Zheng if (fi->fib_metrics != (u32 *) dst_default_metrics) 15519c1ea14SYan, Zheng kfree(fi->fib_metrics); 15619c1ea14SYan, Zheng kfree(fi); 15719c1ea14SYan, Zheng } 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds void free_fib_info(struct fib_info *fi) 1601da177e4SLinus Torvalds { 1611da177e4SLinus Torvalds if (fi->fib_dead == 0) { 162058bd4d2SJoe Perches pr_warn("Freeing alive fib_info %p\n", fi); 1631da177e4SLinus Torvalds return; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds fib_info_cnt--; 16619c1ea14SYan, Zheng call_rcu(&fi->rcu, free_fib_info_rcu); 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds void fib_release_info(struct fib_info *fi) 1701da177e4SLinus Torvalds { 171832b4c5eSStephen Hemminger spin_lock_bh(&fib_info_lock); 1721da177e4SLinus Torvalds if (fi && --fi->fib_treeref == 0) { 1731da177e4SLinus Torvalds hlist_del(&fi->fib_hash); 1741da177e4SLinus Torvalds if (fi->fib_prefsrc) 1751da177e4SLinus Torvalds hlist_del(&fi->fib_lhash); 1761da177e4SLinus Torvalds change_nexthops(fi) { 17771fceff0SDavid S. Miller if (!nexthop_nh->nh_dev) 1781da177e4SLinus Torvalds continue; 17971fceff0SDavid S. Miller hlist_del(&nexthop_nh->nh_hash); 1801da177e4SLinus Torvalds } endfor_nexthops(fi) 1811da177e4SLinus Torvalds fi->fib_dead = 1; 1821da177e4SLinus Torvalds fib_info_put(fi); 1831da177e4SLinus Torvalds } 184832b4c5eSStephen Hemminger spin_unlock_bh(&fib_info_lock); 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1876a31d2a9SEric Dumazet static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi) 1881da177e4SLinus Torvalds { 1891da177e4SLinus Torvalds const struct fib_nh *onh = ofi->fib_nh; 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds for_nexthops(fi) { 1921da177e4SLinus Torvalds if (nh->nh_oif != onh->nh_oif || 1931da177e4SLinus Torvalds nh->nh_gw != onh->nh_gw || 1941da177e4SLinus Torvalds nh->nh_scope != onh->nh_scope || 1951da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 1961da177e4SLinus Torvalds nh->nh_weight != onh->nh_weight || 1971da177e4SLinus Torvalds #endif 198c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 1991da177e4SLinus Torvalds nh->nh_tclassid != onh->nh_tclassid || 2001da177e4SLinus Torvalds #endif 2011da177e4SLinus Torvalds ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD)) 2021da177e4SLinus Torvalds return -1; 2031da177e4SLinus Torvalds onh++; 2041da177e4SLinus Torvalds } endfor_nexthops(fi); 2051da177e4SLinus Torvalds return 0; 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 20888ebc72fSDavid S. Miller static inline unsigned int fib_devindex_hashfn(unsigned int val) 20988ebc72fSDavid S. Miller { 21088ebc72fSDavid S. Miller unsigned int mask = DEVINDEX_HASHSIZE - 1; 21188ebc72fSDavid S. Miller 21288ebc72fSDavid S. Miller return (val ^ 21388ebc72fSDavid S. Miller (val >> DEVINDEX_HASHBITS) ^ 21488ebc72fSDavid S. Miller (val >> (DEVINDEX_HASHBITS * 2))) & mask; 21588ebc72fSDavid S. Miller } 21688ebc72fSDavid S. Miller 2171da177e4SLinus Torvalds static inline unsigned int fib_info_hashfn(const struct fib_info *fi) 2181da177e4SLinus Torvalds { 219123b9731SDavid S. Miller unsigned int mask = (fib_info_hash_size - 1); 2201da177e4SLinus Torvalds unsigned int val = fi->fib_nhs; 2211da177e4SLinus Torvalds 22237e826c5SDavid S. Miller val ^= (fi->fib_protocol << 8) | fi->fib_scope; 22381f7bf6cSAl Viro val ^= (__force u32)fi->fib_prefsrc; 2241da177e4SLinus Torvalds val ^= fi->fib_priority; 22588ebc72fSDavid S. Miller for_nexthops(fi) { 22688ebc72fSDavid S. Miller val ^= fib_devindex_hashfn(nh->nh_oif); 22788ebc72fSDavid S. Miller } endfor_nexthops(fi) 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds return (val ^ (val >> 7) ^ (val >> 12)) & mask; 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds static struct fib_info *fib_find_info(const struct fib_info *nfi) 2331da177e4SLinus Torvalds { 2341da177e4SLinus Torvalds struct hlist_head *head; 2351da177e4SLinus Torvalds struct hlist_node *node; 2361da177e4SLinus Torvalds struct fib_info *fi; 2371da177e4SLinus Torvalds unsigned int hash; 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds hash = fib_info_hashfn(nfi); 2401da177e4SLinus Torvalds head = &fib_info_hash[hash]; 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds hlist_for_each_entry(fi, node, head, fib_hash) { 24309ad9bc7SOctavian Purdila if (!net_eq(fi->fib_net, nfi->fib_net)) 2444814bdbdSDenis V. Lunev continue; 2451da177e4SLinus Torvalds if (fi->fib_nhs != nfi->fib_nhs) 2461da177e4SLinus Torvalds continue; 2471da177e4SLinus Torvalds if (nfi->fib_protocol == fi->fib_protocol && 24837e826c5SDavid S. Miller nfi->fib_scope == fi->fib_scope && 2491da177e4SLinus Torvalds nfi->fib_prefsrc == fi->fib_prefsrc && 2501da177e4SLinus Torvalds nfi->fib_priority == fi->fib_priority && 2511da177e4SLinus Torvalds memcmp(nfi->fib_metrics, fi->fib_metrics, 252fcd13f42SEric Dumazet sizeof(u32) * RTAX_MAX) == 0 && 2531da177e4SLinus Torvalds ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 && 2541da177e4SLinus Torvalds (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0)) 2551da177e4SLinus Torvalds return fi; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds return NULL; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds /* Check, that the gateway is already configured. 2626a31d2a9SEric Dumazet * Used only by redirect accept routine. 2631da177e4SLinus Torvalds */ 264d878e72eSAl Viro int ip_fib_check_default(__be32 gw, struct net_device *dev) 2651da177e4SLinus Torvalds { 2661da177e4SLinus Torvalds struct hlist_head *head; 2671da177e4SLinus Torvalds struct hlist_node *node; 2681da177e4SLinus Torvalds struct fib_nh *nh; 2691da177e4SLinus Torvalds unsigned int hash; 2701da177e4SLinus Torvalds 271832b4c5eSStephen Hemminger spin_lock(&fib_info_lock); 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds hash = fib_devindex_hashfn(dev->ifindex); 2741da177e4SLinus Torvalds head = &fib_info_devhash[hash]; 2751da177e4SLinus Torvalds hlist_for_each_entry(nh, node, head, nh_hash) { 2761da177e4SLinus Torvalds if (nh->nh_dev == dev && 2771da177e4SLinus Torvalds nh->nh_gw == gw && 2781da177e4SLinus Torvalds !(nh->nh_flags & RTNH_F_DEAD)) { 279832b4c5eSStephen Hemminger spin_unlock(&fib_info_lock); 2801da177e4SLinus Torvalds return 0; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 284832b4c5eSStephen Hemminger spin_unlock(&fib_info_lock); 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds return -1; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 289339bf98fSThomas Graf static inline size_t fib_nlmsg_size(struct fib_info *fi) 290339bf98fSThomas Graf { 291339bf98fSThomas Graf size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg)) 292339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 293339bf98fSThomas Graf + nla_total_size(4) /* RTA_DST */ 294339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 295339bf98fSThomas Graf + nla_total_size(4); /* RTA_PREFSRC */ 296339bf98fSThomas Graf 297339bf98fSThomas Graf /* space for nested metrics */ 298339bf98fSThomas Graf payload += nla_total_size((RTAX_MAX * nla_total_size(4))); 299339bf98fSThomas Graf 300339bf98fSThomas Graf if (fi->fib_nhs) { 301339bf98fSThomas Graf /* Also handles the special case fib_nhs == 1 */ 302339bf98fSThomas Graf 303339bf98fSThomas Graf /* each nexthop is packed in an attribute */ 304339bf98fSThomas Graf size_t nhsize = nla_total_size(sizeof(struct rtnexthop)); 305339bf98fSThomas Graf 306339bf98fSThomas Graf /* may contain flow and gateway attribute */ 307339bf98fSThomas Graf nhsize += 2 * nla_total_size(4); 308339bf98fSThomas Graf 309339bf98fSThomas Graf /* all nexthops are packed in a nested attribute */ 310339bf98fSThomas Graf payload += nla_total_size(fi->fib_nhs * nhsize); 311339bf98fSThomas Graf } 312339bf98fSThomas Graf 313339bf98fSThomas Graf return payload; 314339bf98fSThomas Graf } 315339bf98fSThomas Graf 31681f7bf6cSAl Viro void rtmsg_fib(int event, __be32 key, struct fib_alias *fa, 317b8f55831SMilan Kocian int dst_len, u32 tb_id, struct nl_info *info, 318b8f55831SMilan Kocian unsigned int nlm_flags) 3191da177e4SLinus Torvalds { 3201da177e4SLinus Torvalds struct sk_buff *skb; 3214e902c57SThomas Graf u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 322f21c7bc5SThomas Graf int err = -ENOBUFS; 3231da177e4SLinus Torvalds 324339bf98fSThomas Graf skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL); 325f21c7bc5SThomas Graf if (skb == NULL) 326f21c7bc5SThomas Graf goto errout; 3271da177e4SLinus Torvalds 3284e902c57SThomas Graf err = fib_dump_info(skb, info->pid, seq, event, tb_id, 32937e826c5SDavid S. Miller fa->fa_type, key, dst_len, 330b8f55831SMilan Kocian fa->fa_tos, fa->fa_info, nlm_flags); 33126932566SPatrick McHardy if (err < 0) { 33226932566SPatrick McHardy /* -EMSGSIZE implies BUG in fib_nlmsg_size() */ 33326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 33426932566SPatrick McHardy kfree_skb(skb); 33526932566SPatrick McHardy goto errout; 33626932566SPatrick McHardy } 3371ce85fe4SPablo Neira Ayuso rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE, 3384e902c57SThomas Graf info->nlh, GFP_KERNEL); 3391ce85fe4SPablo Neira Ayuso return; 340f21c7bc5SThomas Graf errout: 341f21c7bc5SThomas Graf if (err < 0) 3424d1169c1SDenis V. Lunev rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds /* Return the first fib alias matching TOS with 3461da177e4SLinus Torvalds * priority less than or equal to PRIO. 3471da177e4SLinus Torvalds */ 3481da177e4SLinus Torvalds struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio) 3491da177e4SLinus Torvalds { 3501da177e4SLinus Torvalds if (fah) { 3511da177e4SLinus Torvalds struct fib_alias *fa; 3521da177e4SLinus Torvalds list_for_each_entry(fa, fah, fa_list) { 3531da177e4SLinus Torvalds if (fa->fa_tos > tos) 3541da177e4SLinus Torvalds continue; 3551da177e4SLinus Torvalds if (fa->fa_info->fib_priority >= prio || 3561da177e4SLinus Torvalds fa->fa_tos < tos) 3571da177e4SLinus Torvalds return fa; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds return NULL; 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds int fib_detect_death(struct fib_info *fi, int order, 364c17860a0SDenis V. Lunev struct fib_info **last_resort, int *last_idx, int dflt) 3651da177e4SLinus Torvalds { 3661da177e4SLinus Torvalds struct neighbour *n; 3671da177e4SLinus Torvalds int state = NUD_NONE; 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev); 3701da177e4SLinus Torvalds if (n) { 3711da177e4SLinus Torvalds state = n->nud_state; 3721da177e4SLinus Torvalds neigh_release(n); 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds if (state == NUD_REACHABLE) 3751da177e4SLinus Torvalds return 0; 376c17860a0SDenis V. Lunev if ((state & NUD_VALID) && order != dflt) 3771da177e4SLinus Torvalds return 0; 3781da177e4SLinus Torvalds if ((state & NUD_VALID) || 379c17860a0SDenis V. Lunev (*last_idx < 0 && order > dflt)) { 3801da177e4SLinus Torvalds *last_resort = fi; 3811da177e4SLinus Torvalds *last_idx = order; 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds return 1; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 3871da177e4SLinus Torvalds 3884e902c57SThomas Graf static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining) 3891da177e4SLinus Torvalds { 3901da177e4SLinus Torvalds int nhs = 0; 3911da177e4SLinus Torvalds 3924e902c57SThomas Graf while (rtnh_ok(rtnh, remaining)) { 3931da177e4SLinus Torvalds nhs++; 3944e902c57SThomas Graf rtnh = rtnh_next(rtnh, &remaining); 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds 3974e902c57SThomas Graf /* leftover implies invalid nexthop configuration, discard it */ 3984e902c57SThomas Graf return remaining > 0 ? 0 : nhs; 3994e902c57SThomas Graf } 4001da177e4SLinus Torvalds 4014e902c57SThomas Graf static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh, 4024e902c57SThomas Graf int remaining, struct fib_config *cfg) 4034e902c57SThomas Graf { 4041da177e4SLinus Torvalds change_nexthops(fi) { 4054e902c57SThomas Graf int attrlen; 4064e902c57SThomas Graf 4074e902c57SThomas Graf if (!rtnh_ok(rtnh, remaining)) 4081da177e4SLinus Torvalds return -EINVAL; 4094e902c57SThomas Graf 41071fceff0SDavid S. Miller nexthop_nh->nh_flags = 41171fceff0SDavid S. Miller (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags; 41271fceff0SDavid S. Miller nexthop_nh->nh_oif = rtnh->rtnh_ifindex; 41371fceff0SDavid S. Miller nexthop_nh->nh_weight = rtnh->rtnh_hops + 1; 4144e902c57SThomas Graf 4154e902c57SThomas Graf attrlen = rtnh_attrlen(rtnh); 4164e902c57SThomas Graf if (attrlen > 0) { 4174e902c57SThomas Graf struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 4184e902c57SThomas Graf 4194e902c57SThomas Graf nla = nla_find(attrs, attrlen, RTA_GATEWAY); 42071fceff0SDavid S. Miller nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0; 421c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 4224e902c57SThomas Graf nla = nla_find(attrs, attrlen, RTA_FLOW); 42371fceff0SDavid S. Miller nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0; 4241da177e4SLinus Torvalds #endif 4251da177e4SLinus Torvalds } 4264e902c57SThomas Graf 4274e902c57SThomas Graf rtnh = rtnh_next(rtnh, &remaining); 4281da177e4SLinus Torvalds } endfor_nexthops(fi); 4294e902c57SThomas Graf 4301da177e4SLinus Torvalds return 0; 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds #endif 4341da177e4SLinus Torvalds 4354e902c57SThomas Graf int fib_nh_match(struct fib_config *cfg, struct fib_info *fi) 4361da177e4SLinus Torvalds { 4371da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 4384e902c57SThomas Graf struct rtnexthop *rtnh; 4394e902c57SThomas Graf int remaining; 4401da177e4SLinus Torvalds #endif 4411da177e4SLinus Torvalds 4424e902c57SThomas Graf if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority) 4431da177e4SLinus Torvalds return 1; 4441da177e4SLinus Torvalds 4454e902c57SThomas Graf if (cfg->fc_oif || cfg->fc_gw) { 4464e902c57SThomas Graf if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) && 4474e902c57SThomas Graf (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw)) 4481da177e4SLinus Torvalds return 0; 4491da177e4SLinus Torvalds return 1; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 4534e902c57SThomas Graf if (cfg->fc_mp == NULL) 4541da177e4SLinus Torvalds return 0; 4554e902c57SThomas Graf 4564e902c57SThomas Graf rtnh = cfg->fc_mp; 4574e902c57SThomas Graf remaining = cfg->fc_mp_len; 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds for_nexthops(fi) { 4604e902c57SThomas Graf int attrlen; 4611da177e4SLinus Torvalds 4624e902c57SThomas Graf if (!rtnh_ok(rtnh, remaining)) 4631da177e4SLinus Torvalds return -EINVAL; 4644e902c57SThomas Graf 4654e902c57SThomas Graf if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif) 4661da177e4SLinus Torvalds return 1; 4674e902c57SThomas Graf 4684e902c57SThomas Graf attrlen = rtnh_attrlen(rtnh); 4694e902c57SThomas Graf if (attrlen < 0) { 4704e902c57SThomas Graf struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 4714e902c57SThomas Graf 4724e902c57SThomas Graf nla = nla_find(attrs, attrlen, RTA_GATEWAY); 47317fb2c64SAl Viro if (nla && nla_get_be32(nla) != nh->nh_gw) 4741da177e4SLinus Torvalds return 1; 475c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 4764e902c57SThomas Graf nla = nla_find(attrs, attrlen, RTA_FLOW); 4774e902c57SThomas Graf if (nla && nla_get_u32(nla) != nh->nh_tclassid) 4781da177e4SLinus Torvalds return 1; 4791da177e4SLinus Torvalds #endif 4801da177e4SLinus Torvalds } 4814e902c57SThomas Graf 4824e902c57SThomas Graf rtnh = rtnh_next(rtnh, &remaining); 4831da177e4SLinus Torvalds } endfor_nexthops(fi); 4841da177e4SLinus Torvalds #endif 4851da177e4SLinus Torvalds return 0; 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds /* 4906a31d2a9SEric Dumazet * Picture 4916a31d2a9SEric Dumazet * ------- 4926a31d2a9SEric Dumazet * 4936a31d2a9SEric Dumazet * Semantics of nexthop is very messy by historical reasons. 4946a31d2a9SEric Dumazet * We have to take into account, that: 4956a31d2a9SEric Dumazet * a) gateway can be actually local interface address, 4966a31d2a9SEric Dumazet * so that gatewayed route is direct. 4976a31d2a9SEric Dumazet * b) gateway must be on-link address, possibly 4986a31d2a9SEric Dumazet * described not by an ifaddr, but also by a direct route. 4996a31d2a9SEric Dumazet * c) If both gateway and interface are specified, they should not 5006a31d2a9SEric Dumazet * contradict. 5016a31d2a9SEric Dumazet * d) If we use tunnel routes, gateway could be not on-link. 5026a31d2a9SEric Dumazet * 5036a31d2a9SEric Dumazet * Attempt to reconcile all of these (alas, self-contradictory) conditions 5046a31d2a9SEric Dumazet * results in pretty ugly and hairy code with obscure logic. 5056a31d2a9SEric Dumazet * 5066a31d2a9SEric Dumazet * I chose to generalized it instead, so that the size 5076a31d2a9SEric Dumazet * of code does not increase practically, but it becomes 5086a31d2a9SEric Dumazet * much more general. 5096a31d2a9SEric Dumazet * Every prefix is assigned a "scope" value: "host" is local address, 5106a31d2a9SEric Dumazet * "link" is direct route, 5116a31d2a9SEric Dumazet * [ ... "site" ... "interior" ... ] 5126a31d2a9SEric Dumazet * and "universe" is true gateway route with global meaning. 5136a31d2a9SEric Dumazet * 5146a31d2a9SEric Dumazet * Every prefix refers to a set of "nexthop"s (gw, oif), 5156a31d2a9SEric Dumazet * where gw must have narrower scope. This recursion stops 5166a31d2a9SEric Dumazet * when gw has LOCAL scope or if "nexthop" is declared ONLINK, 5176a31d2a9SEric Dumazet * which means that gw is forced to be on link. 5186a31d2a9SEric Dumazet * 5196a31d2a9SEric Dumazet * Code is still hairy, but now it is apparently logically 5206a31d2a9SEric Dumazet * consistent and very flexible. F.e. as by-product it allows 5216a31d2a9SEric Dumazet * to co-exists in peace independent exterior and interior 5226a31d2a9SEric Dumazet * routing processes. 5236a31d2a9SEric Dumazet * 5246a31d2a9SEric Dumazet * Normally it looks as following. 5256a31d2a9SEric Dumazet * 5266a31d2a9SEric Dumazet * {universe prefix} -> (gw, oif) [scope link] 5276a31d2a9SEric Dumazet * | 5286a31d2a9SEric Dumazet * |-> {link prefix} -> (gw, oif) [scope local] 5296a31d2a9SEric Dumazet * | 5306a31d2a9SEric Dumazet * |-> {local prefix} (terminal node) 5311da177e4SLinus Torvalds */ 5324e902c57SThomas Graf static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi, 5334e902c57SThomas Graf struct fib_nh *nh) 5341da177e4SLinus Torvalds { 5351da177e4SLinus Torvalds int err; 53686167a37SDenis V. Lunev struct net *net; 5376a31d2a9SEric Dumazet struct net_device *dev; 5381da177e4SLinus Torvalds 53986167a37SDenis V. Lunev net = cfg->fc_nlinfo.nl_net; 5401da177e4SLinus Torvalds if (nh->nh_gw) { 5411da177e4SLinus Torvalds struct fib_result res; 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds if (nh->nh_flags & RTNH_F_ONLINK) { 5441da177e4SLinus Torvalds 5454e902c57SThomas Graf if (cfg->fc_scope >= RT_SCOPE_LINK) 5461da177e4SLinus Torvalds return -EINVAL; 54786167a37SDenis V. Lunev if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST) 5481da177e4SLinus Torvalds return -EINVAL; 5496a31d2a9SEric Dumazet dev = __dev_get_by_index(net, nh->nh_oif); 5506a31d2a9SEric Dumazet if (!dev) 5511da177e4SLinus Torvalds return -ENODEV; 5521da177e4SLinus Torvalds if (!(dev->flags & IFF_UP)) 5531da177e4SLinus Torvalds return -ENETDOWN; 5541da177e4SLinus Torvalds nh->nh_dev = dev; 5551da177e4SLinus Torvalds dev_hold(dev); 5561da177e4SLinus Torvalds nh->nh_scope = RT_SCOPE_LINK; 5571da177e4SLinus Torvalds return 0; 5581da177e4SLinus Torvalds } 559ebc0ffaeSEric Dumazet rcu_read_lock(); 5601da177e4SLinus Torvalds { 5619ade2286SDavid S. Miller struct flowi4 fl4 = { 5629ade2286SDavid S. Miller .daddr = nh->nh_gw, 5639ade2286SDavid S. Miller .flowi4_scope = cfg->fc_scope + 1, 5649ade2286SDavid S. Miller .flowi4_oif = nh->nh_oif, 5654e902c57SThomas Graf }; 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds /* It is not necessary, but requires a bit of thinking */ 5689ade2286SDavid S. Miller if (fl4.flowi4_scope < RT_SCOPE_LINK) 5699ade2286SDavid S. Miller fl4.flowi4_scope = RT_SCOPE_LINK; 5709ade2286SDavid S. Miller err = fib_lookup(net, &fl4, &res); 571ebc0ffaeSEric Dumazet if (err) { 572ebc0ffaeSEric Dumazet rcu_read_unlock(); 5731da177e4SLinus Torvalds return err; 5741da177e4SLinus Torvalds } 575ebc0ffaeSEric Dumazet } 5761da177e4SLinus Torvalds err = -EINVAL; 5771da177e4SLinus Torvalds if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) 5781da177e4SLinus Torvalds goto out; 5791da177e4SLinus Torvalds nh->nh_scope = res.scope; 5801da177e4SLinus Torvalds nh->nh_oif = FIB_RES_OIF(res); 5816a31d2a9SEric Dumazet nh->nh_dev = dev = FIB_RES_DEV(res); 5826a31d2a9SEric Dumazet if (!dev) 5831da177e4SLinus Torvalds goto out; 5846a31d2a9SEric Dumazet dev_hold(dev); 5858723e1b4SEric Dumazet err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN; 5861da177e4SLinus Torvalds } else { 5871da177e4SLinus Torvalds struct in_device *in_dev; 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) 5901da177e4SLinus Torvalds return -EINVAL; 5911da177e4SLinus Torvalds 5928723e1b4SEric Dumazet rcu_read_lock(); 5938723e1b4SEric Dumazet err = -ENODEV; 59486167a37SDenis V. Lunev in_dev = inetdev_by_index(net, nh->nh_oif); 5951da177e4SLinus Torvalds if (in_dev == NULL) 5968723e1b4SEric Dumazet goto out; 5978723e1b4SEric Dumazet err = -ENETDOWN; 5988723e1b4SEric Dumazet if (!(in_dev->dev->flags & IFF_UP)) 5998723e1b4SEric Dumazet goto out; 6001da177e4SLinus Torvalds nh->nh_dev = in_dev->dev; 6011da177e4SLinus Torvalds dev_hold(nh->nh_dev); 6021da177e4SLinus Torvalds nh->nh_scope = RT_SCOPE_HOST; 6038723e1b4SEric Dumazet err = 0; 6041da177e4SLinus Torvalds } 6058723e1b4SEric Dumazet out: 6068723e1b4SEric Dumazet rcu_read_unlock(); 6078723e1b4SEric Dumazet return err; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 61081f7bf6cSAl Viro static inline unsigned int fib_laddr_hashfn(__be32 val) 6111da177e4SLinus Torvalds { 612123b9731SDavid S. Miller unsigned int mask = (fib_info_hash_size - 1); 6131da177e4SLinus Torvalds 6146a31d2a9SEric Dumazet return ((__force u32)val ^ 6156a31d2a9SEric Dumazet ((__force u32)val >> 7) ^ 6166a31d2a9SEric Dumazet ((__force u32)val >> 14)) & mask; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 619123b9731SDavid S. Miller static struct hlist_head *fib_info_hash_alloc(int bytes) 6201da177e4SLinus Torvalds { 6211da177e4SLinus Torvalds if (bytes <= PAGE_SIZE) 62288f83491SJoonwoo Park return kzalloc(bytes, GFP_KERNEL); 6231da177e4SLinus Torvalds else 6241da177e4SLinus Torvalds return (struct hlist_head *) 6256a31d2a9SEric Dumazet __get_free_pages(GFP_KERNEL | __GFP_ZERO, 6266a31d2a9SEric Dumazet get_order(bytes)); 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds 629123b9731SDavid S. Miller static void fib_info_hash_free(struct hlist_head *hash, int bytes) 6301da177e4SLinus Torvalds { 6311da177e4SLinus Torvalds if (!hash) 6321da177e4SLinus Torvalds return; 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds if (bytes <= PAGE_SIZE) 6351da177e4SLinus Torvalds kfree(hash); 6361da177e4SLinus Torvalds else 6371da177e4SLinus Torvalds free_pages((unsigned long) hash, get_order(bytes)); 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds 640123b9731SDavid S. Miller static void fib_info_hash_move(struct hlist_head *new_info_hash, 6411da177e4SLinus Torvalds struct hlist_head *new_laddrhash, 6421da177e4SLinus Torvalds unsigned int new_size) 6431da177e4SLinus Torvalds { 644b7656e7fSDavid S. Miller struct hlist_head *old_info_hash, *old_laddrhash; 645123b9731SDavid S. Miller unsigned int old_size = fib_info_hash_size; 646b7656e7fSDavid S. Miller unsigned int i, bytes; 6471da177e4SLinus Torvalds 648832b4c5eSStephen Hemminger spin_lock_bh(&fib_info_lock); 649b7656e7fSDavid S. Miller old_info_hash = fib_info_hash; 650b7656e7fSDavid S. Miller old_laddrhash = fib_info_laddrhash; 651123b9731SDavid S. Miller fib_info_hash_size = new_size; 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds for (i = 0; i < old_size; i++) { 6541da177e4SLinus Torvalds struct hlist_head *head = &fib_info_hash[i]; 6551da177e4SLinus Torvalds struct hlist_node *node, *n; 6561da177e4SLinus Torvalds struct fib_info *fi; 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds hlist_for_each_entry_safe(fi, node, n, head, fib_hash) { 6591da177e4SLinus Torvalds struct hlist_head *dest; 6601da177e4SLinus Torvalds unsigned int new_hash; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds hlist_del(&fi->fib_hash); 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds new_hash = fib_info_hashfn(fi); 6651da177e4SLinus Torvalds dest = &new_info_hash[new_hash]; 6661da177e4SLinus Torvalds hlist_add_head(&fi->fib_hash, dest); 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds fib_info_hash = new_info_hash; 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds for (i = 0; i < old_size; i++) { 6721da177e4SLinus Torvalds struct hlist_head *lhead = &fib_info_laddrhash[i]; 6731da177e4SLinus Torvalds struct hlist_node *node, *n; 6741da177e4SLinus Torvalds struct fib_info *fi; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) { 6771da177e4SLinus Torvalds struct hlist_head *ldest; 6781da177e4SLinus Torvalds unsigned int new_hash; 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds hlist_del(&fi->fib_lhash); 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds new_hash = fib_laddr_hashfn(fi->fib_prefsrc); 6831da177e4SLinus Torvalds ldest = &new_laddrhash[new_hash]; 6841da177e4SLinus Torvalds hlist_add_head(&fi->fib_lhash, ldest); 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds fib_info_laddrhash = new_laddrhash; 6881da177e4SLinus Torvalds 689832b4c5eSStephen Hemminger spin_unlock_bh(&fib_info_lock); 690b7656e7fSDavid S. Miller 691b7656e7fSDavid S. Miller bytes = old_size * sizeof(struct hlist_head *); 692123b9731SDavid S. Miller fib_info_hash_free(old_info_hash, bytes); 693123b9731SDavid S. Miller fib_info_hash_free(old_laddrhash, bytes); 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds 696436c3b66SDavid S. Miller __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh) 697436c3b66SDavid S. Miller { 698436c3b66SDavid S. Miller nh->nh_saddr = inet_select_addr(nh->nh_dev, 699436c3b66SDavid S. Miller nh->nh_gw, 70037e826c5SDavid S. Miller nh->nh_parent->fib_scope); 701436c3b66SDavid S. Miller nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid); 702436c3b66SDavid S. Miller 703436c3b66SDavid S. Miller return nh->nh_saddr; 704436c3b66SDavid S. Miller } 705436c3b66SDavid S. Miller 7064e902c57SThomas Graf struct fib_info *fib_create_info(struct fib_config *cfg) 7071da177e4SLinus Torvalds { 7081da177e4SLinus Torvalds int err; 7091da177e4SLinus Torvalds struct fib_info *fi = NULL; 7101da177e4SLinus Torvalds struct fib_info *ofi; 7111da177e4SLinus Torvalds int nhs = 1; 7127462bd74SDenis V. Lunev struct net *net = cfg->fc_nlinfo.nl_net; 7131da177e4SLinus Torvalds 7144c8237cdSDavid S. Miller if (cfg->fc_type > RTN_MAX) 7154c8237cdSDavid S. Miller goto err_inval; 7164c8237cdSDavid S. Miller 7171da177e4SLinus Torvalds /* Fast check to catch the most weird cases */ 7184e902c57SThomas Graf if (fib_props[cfg->fc_type].scope > cfg->fc_scope) 7191da177e4SLinus Torvalds goto err_inval; 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 7224e902c57SThomas Graf if (cfg->fc_mp) { 7234e902c57SThomas Graf nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len); 7241da177e4SLinus Torvalds if (nhs == 0) 7251da177e4SLinus Torvalds goto err_inval; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds #endif 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds err = -ENOBUFS; 730123b9731SDavid S. Miller if (fib_info_cnt >= fib_info_hash_size) { 731123b9731SDavid S. Miller unsigned int new_size = fib_info_hash_size << 1; 7321da177e4SLinus Torvalds struct hlist_head *new_info_hash; 7331da177e4SLinus Torvalds struct hlist_head *new_laddrhash; 7341da177e4SLinus Torvalds unsigned int bytes; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds if (!new_size) 7371da177e4SLinus Torvalds new_size = 1; 7381da177e4SLinus Torvalds bytes = new_size * sizeof(struct hlist_head *); 739123b9731SDavid S. Miller new_info_hash = fib_info_hash_alloc(bytes); 740123b9731SDavid S. Miller new_laddrhash = fib_info_hash_alloc(bytes); 7411da177e4SLinus Torvalds if (!new_info_hash || !new_laddrhash) { 742123b9731SDavid S. Miller fib_info_hash_free(new_info_hash, bytes); 743123b9731SDavid S. Miller fib_info_hash_free(new_laddrhash, bytes); 74488f83491SJoonwoo Park } else 745123b9731SDavid S. Miller fib_info_hash_move(new_info_hash, new_laddrhash, new_size); 7461da177e4SLinus Torvalds 747123b9731SDavid S. Miller if (!fib_info_hash_size) 7481da177e4SLinus Torvalds goto failure; 7491da177e4SLinus Torvalds } 7501da177e4SLinus Torvalds 7510da974f4SPanagiotis Issaris fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL); 7521da177e4SLinus Torvalds if (fi == NULL) 7531da177e4SLinus Torvalds goto failure; 754725d1e1bSDavid S. Miller if (cfg->fc_mx) { 7559c150e82SDavid S. Miller fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 7569c150e82SDavid S. Miller if (!fi->fib_metrics) 7579c150e82SDavid S. Miller goto failure; 758725d1e1bSDavid S. Miller } else 759725d1e1bSDavid S. Miller fi->fib_metrics = (u32 *) dst_default_metrics; 7601da177e4SLinus Torvalds fib_info_cnt++; 7611da177e4SLinus Torvalds 76257d7a600SDenis V. Lunev fi->fib_net = hold_net(net); 7634e902c57SThomas Graf fi->fib_protocol = cfg->fc_protocol; 76437e826c5SDavid S. Miller fi->fib_scope = cfg->fc_scope; 7654e902c57SThomas Graf fi->fib_flags = cfg->fc_flags; 7664e902c57SThomas Graf fi->fib_priority = cfg->fc_priority; 7674e902c57SThomas Graf fi->fib_prefsrc = cfg->fc_prefsrc; 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds fi->fib_nhs = nhs; 7701da177e4SLinus Torvalds change_nexthops(fi) { 77171fceff0SDavid S. Miller nexthop_nh->nh_parent = fi; 7721da177e4SLinus Torvalds } endfor_nexthops(fi) 7731da177e4SLinus Torvalds 7744e902c57SThomas Graf if (cfg->fc_mx) { 7754e902c57SThomas Graf struct nlattr *nla; 7764e902c57SThomas Graf int remaining; 7771da177e4SLinus Torvalds 7784e902c57SThomas Graf nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 7798f4c1f9bSThomas Graf int type = nla_type(nla); 7804e902c57SThomas Graf 7814e902c57SThomas Graf if (type) { 7824e902c57SThomas Graf if (type > RTAX_MAX) 7831da177e4SLinus Torvalds goto err_inval; 7844e902c57SThomas Graf fi->fib_metrics[type - 1] = nla_get_u32(nla); 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds } 7874e902c57SThomas Graf } 7881da177e4SLinus Torvalds 7894e902c57SThomas Graf if (cfg->fc_mp) { 7901da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 7914e902c57SThomas Graf err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg); 7924e902c57SThomas Graf if (err != 0) 7931da177e4SLinus Torvalds goto failure; 7944e902c57SThomas Graf if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif) 7951da177e4SLinus Torvalds goto err_inval; 7964e902c57SThomas Graf if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw) 7971da177e4SLinus Torvalds goto err_inval; 798c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 7994e902c57SThomas Graf if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) 8001da177e4SLinus Torvalds goto err_inval; 8011da177e4SLinus Torvalds #endif 8021da177e4SLinus Torvalds #else 8031da177e4SLinus Torvalds goto err_inval; 8041da177e4SLinus Torvalds #endif 8051da177e4SLinus Torvalds } else { 8061da177e4SLinus Torvalds struct fib_nh *nh = fi->fib_nh; 8074e902c57SThomas Graf 8084e902c57SThomas Graf nh->nh_oif = cfg->fc_oif; 8094e902c57SThomas Graf nh->nh_gw = cfg->fc_gw; 8104e902c57SThomas Graf nh->nh_flags = cfg->fc_flags; 811c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 8124e902c57SThomas Graf nh->nh_tclassid = cfg->fc_flow; 8131da177e4SLinus Torvalds #endif 8141da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 8151da177e4SLinus Torvalds nh->nh_weight = 1; 8161da177e4SLinus Torvalds #endif 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds 8194e902c57SThomas Graf if (fib_props[cfg->fc_type].error) { 8204e902c57SThomas Graf if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) 8211da177e4SLinus Torvalds goto err_inval; 8221da177e4SLinus Torvalds goto link_it; 8234c8237cdSDavid S. Miller } else { 8244c8237cdSDavid S. Miller switch (cfg->fc_type) { 8254c8237cdSDavid S. Miller case RTN_UNICAST: 8264c8237cdSDavid S. Miller case RTN_LOCAL: 8274c8237cdSDavid S. Miller case RTN_BROADCAST: 8284c8237cdSDavid S. Miller case RTN_ANYCAST: 8294c8237cdSDavid S. Miller case RTN_MULTICAST: 8304c8237cdSDavid S. Miller break; 8314c8237cdSDavid S. Miller default: 8324c8237cdSDavid S. Miller goto err_inval; 8334c8237cdSDavid S. Miller } 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 8364e902c57SThomas Graf if (cfg->fc_scope > RT_SCOPE_HOST) 8371da177e4SLinus Torvalds goto err_inval; 8381da177e4SLinus Torvalds 8394e902c57SThomas Graf if (cfg->fc_scope == RT_SCOPE_HOST) { 8401da177e4SLinus Torvalds struct fib_nh *nh = fi->fib_nh; 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds /* Local address is added. */ 8431da177e4SLinus Torvalds if (nhs != 1 || nh->nh_gw) 8441da177e4SLinus Torvalds goto err_inval; 8451da177e4SLinus Torvalds nh->nh_scope = RT_SCOPE_NOWHERE; 8467462bd74SDenis V. Lunev nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif); 8471da177e4SLinus Torvalds err = -ENODEV; 8481da177e4SLinus Torvalds if (nh->nh_dev == NULL) 8491da177e4SLinus Torvalds goto failure; 8501da177e4SLinus Torvalds } else { 8511da177e4SLinus Torvalds change_nexthops(fi) { 8526a31d2a9SEric Dumazet err = fib_check_nh(cfg, fi, nexthop_nh); 8536a31d2a9SEric Dumazet if (err != 0) 8541da177e4SLinus Torvalds goto failure; 8551da177e4SLinus Torvalds } endfor_nexthops(fi) 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds if (fi->fib_prefsrc) { 8594e902c57SThomas Graf if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst || 8604e902c57SThomas Graf fi->fib_prefsrc != cfg->fc_dst) 8617462bd74SDenis V. Lunev if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL) 8621da177e4SLinus Torvalds goto err_inval; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 8651fc050a1SDavid S. Miller change_nexthops(fi) { 866436c3b66SDavid S. Miller fib_info_update_nh_saddr(net, nexthop_nh); 8671fc050a1SDavid S. Miller } endfor_nexthops(fi) 8681fc050a1SDavid S. Miller 8691da177e4SLinus Torvalds link_it: 8706a31d2a9SEric Dumazet ofi = fib_find_info(fi); 8716a31d2a9SEric Dumazet if (ofi) { 8721da177e4SLinus Torvalds fi->fib_dead = 1; 8731da177e4SLinus Torvalds free_fib_info(fi); 8741da177e4SLinus Torvalds ofi->fib_treeref++; 8751da177e4SLinus Torvalds return ofi; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds fi->fib_treeref++; 8791da177e4SLinus Torvalds atomic_inc(&fi->fib_clntref); 880832b4c5eSStephen Hemminger spin_lock_bh(&fib_info_lock); 8811da177e4SLinus Torvalds hlist_add_head(&fi->fib_hash, 8821da177e4SLinus Torvalds &fib_info_hash[fib_info_hashfn(fi)]); 8831da177e4SLinus Torvalds if (fi->fib_prefsrc) { 8841da177e4SLinus Torvalds struct hlist_head *head; 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)]; 8871da177e4SLinus Torvalds hlist_add_head(&fi->fib_lhash, head); 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds change_nexthops(fi) { 8901da177e4SLinus Torvalds struct hlist_head *head; 8911da177e4SLinus Torvalds unsigned int hash; 8921da177e4SLinus Torvalds 89371fceff0SDavid S. Miller if (!nexthop_nh->nh_dev) 8941da177e4SLinus Torvalds continue; 89571fceff0SDavid S. Miller hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex); 8961da177e4SLinus Torvalds head = &fib_info_devhash[hash]; 89771fceff0SDavid S. Miller hlist_add_head(&nexthop_nh->nh_hash, head); 8981da177e4SLinus Torvalds } endfor_nexthops(fi) 899832b4c5eSStephen Hemminger spin_unlock_bh(&fib_info_lock); 9001da177e4SLinus Torvalds return fi; 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds err_inval: 9031da177e4SLinus Torvalds err = -EINVAL; 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds failure: 9061da177e4SLinus Torvalds if (fi) { 9071da177e4SLinus Torvalds fi->fib_dead = 1; 9081da177e4SLinus Torvalds free_fib_info(fi); 9091da177e4SLinus Torvalds } 9104e902c57SThomas Graf 9114e902c57SThomas Graf return ERR_PTR(err); 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds 914be403ea1SThomas Graf int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event, 91537e826c5SDavid S. Miller u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos, 916b6544c0bSJamal Hadi Salim struct fib_info *fi, unsigned int flags) 9171da177e4SLinus Torvalds { 9181da177e4SLinus Torvalds struct nlmsghdr *nlh; 919be403ea1SThomas Graf struct rtmsg *rtm; 9201da177e4SLinus Torvalds 921be403ea1SThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags); 922be403ea1SThomas Graf if (nlh == NULL) 92326932566SPatrick McHardy return -EMSGSIZE; 924be403ea1SThomas Graf 925be403ea1SThomas Graf rtm = nlmsg_data(nlh); 9261da177e4SLinus Torvalds rtm->rtm_family = AF_INET; 9271da177e4SLinus Torvalds rtm->rtm_dst_len = dst_len; 9281da177e4SLinus Torvalds rtm->rtm_src_len = 0; 9291da177e4SLinus Torvalds rtm->rtm_tos = tos; 930709772e6SKrzysztof Piotr Oledzki if (tb_id < 256) 9311da177e4SLinus Torvalds rtm->rtm_table = tb_id; 932709772e6SKrzysztof Piotr Oledzki else 933709772e6SKrzysztof Piotr Oledzki rtm->rtm_table = RT_TABLE_COMPAT; 934f3756b79SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, tb_id)) 935f3756b79SDavid S. Miller goto nla_put_failure; 9361da177e4SLinus Torvalds rtm->rtm_type = type; 9371da177e4SLinus Torvalds rtm->rtm_flags = fi->fib_flags; 93837e826c5SDavid S. Miller rtm->rtm_scope = fi->fib_scope; 9391da177e4SLinus Torvalds rtm->rtm_protocol = fi->fib_protocol; 940be403ea1SThomas Graf 941f3756b79SDavid S. Miller if (rtm->rtm_dst_len && 942f3756b79SDavid S. Miller nla_put_be32(skb, RTA_DST, dst)) 943f3756b79SDavid S. Miller goto nla_put_failure; 944f3756b79SDavid S. Miller if (fi->fib_priority && 945f3756b79SDavid S. Miller nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority)) 946f3756b79SDavid S. Miller goto nla_put_failure; 9471da177e4SLinus Torvalds if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0) 948be403ea1SThomas Graf goto nla_put_failure; 949be403ea1SThomas Graf 950f3756b79SDavid S. Miller if (fi->fib_prefsrc && 951f3756b79SDavid S. Miller nla_put_be32(skb, RTA_PREFSRC, fi->fib_prefsrc)) 952f3756b79SDavid S. Miller goto nla_put_failure; 9531da177e4SLinus Torvalds if (fi->fib_nhs == 1) { 954f3756b79SDavid S. Miller if (fi->fib_nh->nh_gw && 955f3756b79SDavid S. Miller nla_put_be32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw)) 956f3756b79SDavid S. Miller goto nla_put_failure; 957f3756b79SDavid S. Miller if (fi->fib_nh->nh_oif && 958f3756b79SDavid S. Miller nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif)) 959f3756b79SDavid S. Miller goto nla_put_failure; 960c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 961f3756b79SDavid S. Miller if (fi->fib_nh[0].nh_tclassid && 962f3756b79SDavid S. Miller nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid)) 963f3756b79SDavid S. Miller goto nla_put_failure; 9648265abc0SPatrick McHardy #endif 9651da177e4SLinus Torvalds } 9661da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 9671da177e4SLinus Torvalds if (fi->fib_nhs > 1) { 968be403ea1SThomas Graf struct rtnexthop *rtnh; 969be403ea1SThomas Graf struct nlattr *mp; 970be403ea1SThomas Graf 971be403ea1SThomas Graf mp = nla_nest_start(skb, RTA_MULTIPATH); 972be403ea1SThomas Graf if (mp == NULL) 973be403ea1SThomas Graf goto nla_put_failure; 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds for_nexthops(fi) { 976be403ea1SThomas Graf rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 977be403ea1SThomas Graf if (rtnh == NULL) 978be403ea1SThomas Graf goto nla_put_failure; 979be403ea1SThomas Graf 980be403ea1SThomas Graf rtnh->rtnh_flags = nh->nh_flags & 0xFF; 981be403ea1SThomas Graf rtnh->rtnh_hops = nh->nh_weight - 1; 982be403ea1SThomas Graf rtnh->rtnh_ifindex = nh->nh_oif; 983be403ea1SThomas Graf 984f3756b79SDavid S. Miller if (nh->nh_gw && 985f3756b79SDavid S. Miller nla_put_be32(skb, RTA_GATEWAY, nh->nh_gw)) 986f3756b79SDavid S. Miller goto nla_put_failure; 987c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 988f3756b79SDavid S. Miller if (nh->nh_tclassid && 989f3756b79SDavid S. Miller nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid)) 990f3756b79SDavid S. Miller goto nla_put_failure; 9918265abc0SPatrick McHardy #endif 992be403ea1SThomas Graf /* length of rtnetlink header + attributes */ 993be403ea1SThomas Graf rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh; 9941da177e4SLinus Torvalds } endfor_nexthops(fi); 995be403ea1SThomas Graf 996be403ea1SThomas Graf nla_nest_end(skb, mp); 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds #endif 999be403ea1SThomas Graf return nlmsg_end(skb, nlh); 10001da177e4SLinus Torvalds 1001be403ea1SThomas Graf nla_put_failure: 100226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 100326932566SPatrick McHardy return -EMSGSIZE; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds /* 10076a31d2a9SEric Dumazet * Update FIB if: 10086a31d2a9SEric Dumazet * - local address disappeared -> we must delete all the entries 10096a31d2a9SEric Dumazet * referring to it. 10106a31d2a9SEric Dumazet * - device went down -> we must shutdown all nexthops going via it. 10111da177e4SLinus Torvalds */ 10124814bdbdSDenis V. Lunev int fib_sync_down_addr(struct net *net, __be32 local) 10131da177e4SLinus Torvalds { 10141da177e4SLinus Torvalds int ret = 0; 10151da177e4SLinus Torvalds unsigned int hash = fib_laddr_hashfn(local); 10161da177e4SLinus Torvalds struct hlist_head *head = &fib_info_laddrhash[hash]; 10171da177e4SLinus Torvalds struct hlist_node *node; 10181da177e4SLinus Torvalds struct fib_info *fi; 10191da177e4SLinus Torvalds 102085326fa5SDenis V. Lunev if (fib_info_laddrhash == NULL || local == 0) 102185326fa5SDenis V. Lunev return 0; 102285326fa5SDenis V. Lunev 10231da177e4SLinus Torvalds hlist_for_each_entry(fi, node, head, fib_lhash) { 102409ad9bc7SOctavian Purdila if (!net_eq(fi->fib_net, net)) 10254814bdbdSDenis V. Lunev continue; 10261da177e4SLinus Torvalds if (fi->fib_prefsrc == local) { 10271da177e4SLinus Torvalds fi->fib_flags |= RTNH_F_DEAD; 10281da177e4SLinus Torvalds ret++; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds } 103185326fa5SDenis V. Lunev return ret; 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds 103485326fa5SDenis V. Lunev int fib_sync_down_dev(struct net_device *dev, int force) 103585326fa5SDenis V. Lunev { 103685326fa5SDenis V. Lunev int ret = 0; 103785326fa5SDenis V. Lunev int scope = RT_SCOPE_NOWHERE; 10381da177e4SLinus Torvalds struct fib_info *prev_fi = NULL; 10391da177e4SLinus Torvalds unsigned int hash = fib_devindex_hashfn(dev->ifindex); 10401da177e4SLinus Torvalds struct hlist_head *head = &fib_info_devhash[hash]; 10411da177e4SLinus Torvalds struct hlist_node *node; 10421da177e4SLinus Torvalds struct fib_nh *nh; 10431da177e4SLinus Torvalds 104485326fa5SDenis V. Lunev if (force) 104585326fa5SDenis V. Lunev scope = -1; 104685326fa5SDenis V. Lunev 10471da177e4SLinus Torvalds hlist_for_each_entry(nh, node, head, nh_hash) { 10481da177e4SLinus Torvalds struct fib_info *fi = nh->nh_parent; 10491da177e4SLinus Torvalds int dead; 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds BUG_ON(!fi->fib_nhs); 10521da177e4SLinus Torvalds if (nh->nh_dev != dev || fi == prev_fi) 10531da177e4SLinus Torvalds continue; 10541da177e4SLinus Torvalds prev_fi = fi; 10551da177e4SLinus Torvalds dead = 0; 10561da177e4SLinus Torvalds change_nexthops(fi) { 105771fceff0SDavid S. Miller if (nexthop_nh->nh_flags & RTNH_F_DEAD) 10581da177e4SLinus Torvalds dead++; 105971fceff0SDavid S. Miller else if (nexthop_nh->nh_dev == dev && 106071fceff0SDavid S. Miller nexthop_nh->nh_scope != scope) { 106171fceff0SDavid S. Miller nexthop_nh->nh_flags |= RTNH_F_DEAD; 10621da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 10631da177e4SLinus Torvalds spin_lock_bh(&fib_multipath_lock); 106471fceff0SDavid S. Miller fi->fib_power -= nexthop_nh->nh_power; 106571fceff0SDavid S. Miller nexthop_nh->nh_power = 0; 10661da177e4SLinus Torvalds spin_unlock_bh(&fib_multipath_lock); 10671da177e4SLinus Torvalds #endif 10681da177e4SLinus Torvalds dead++; 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 107171fceff0SDavid S. Miller if (force > 1 && nexthop_nh->nh_dev == dev) { 10721da177e4SLinus Torvalds dead = fi->fib_nhs; 10731da177e4SLinus Torvalds break; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds #endif 10761da177e4SLinus Torvalds } endfor_nexthops(fi) 10771da177e4SLinus Torvalds if (dead == fi->fib_nhs) { 10781da177e4SLinus Torvalds fi->fib_flags |= RTNH_F_DEAD; 10791da177e4SLinus Torvalds ret++; 10801da177e4SLinus Torvalds } 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds return ret; 10841da177e4SLinus Torvalds } 10851da177e4SLinus Torvalds 10860c838ff1SDavid S. Miller /* Must be invoked inside of an RCU protected region. */ 10870c838ff1SDavid S. Miller void fib_select_default(struct fib_result *res) 10880c838ff1SDavid S. Miller { 10890c838ff1SDavid S. Miller struct fib_info *fi = NULL, *last_resort = NULL; 10900c838ff1SDavid S. Miller struct list_head *fa_head = res->fa_head; 10910c838ff1SDavid S. Miller struct fib_table *tb = res->table; 10920c838ff1SDavid S. Miller int order = -1, last_idx = -1; 10930c838ff1SDavid S. Miller struct fib_alias *fa; 10940c838ff1SDavid S. Miller 10950c838ff1SDavid S. Miller list_for_each_entry_rcu(fa, fa_head, fa_list) { 10960c838ff1SDavid S. Miller struct fib_info *next_fi = fa->fa_info; 10970c838ff1SDavid S. Miller 109837e826c5SDavid S. Miller if (next_fi->fib_scope != res->scope || 10990c838ff1SDavid S. Miller fa->fa_type != RTN_UNICAST) 11000c838ff1SDavid S. Miller continue; 11010c838ff1SDavid S. Miller 11020c838ff1SDavid S. Miller if (next_fi->fib_priority > res->fi->fib_priority) 11030c838ff1SDavid S. Miller break; 11040c838ff1SDavid S. Miller if (!next_fi->fib_nh[0].nh_gw || 11050c838ff1SDavid S. Miller next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK) 11060c838ff1SDavid S. Miller continue; 11070c838ff1SDavid S. Miller 11080c838ff1SDavid S. Miller fib_alias_accessed(fa); 11090c838ff1SDavid S. Miller 11100c838ff1SDavid S. Miller if (fi == NULL) { 11110c838ff1SDavid S. Miller if (next_fi != res->fi) 11120c838ff1SDavid S. Miller break; 11130c838ff1SDavid S. Miller } else if (!fib_detect_death(fi, order, &last_resort, 11140c838ff1SDavid S. Miller &last_idx, tb->tb_default)) { 11150c838ff1SDavid S. Miller fib_result_assign(res, fi); 11160c838ff1SDavid S. Miller tb->tb_default = order; 11170c838ff1SDavid S. Miller goto out; 11180c838ff1SDavid S. Miller } 11190c838ff1SDavid S. Miller fi = next_fi; 11200c838ff1SDavid S. Miller order++; 11210c838ff1SDavid S. Miller } 11220c838ff1SDavid S. Miller 11230c838ff1SDavid S. Miller if (order <= 0 || fi == NULL) { 11240c838ff1SDavid S. Miller tb->tb_default = -1; 11250c838ff1SDavid S. Miller goto out; 11260c838ff1SDavid S. Miller } 11270c838ff1SDavid S. Miller 11280c838ff1SDavid S. Miller if (!fib_detect_death(fi, order, &last_resort, &last_idx, 11290c838ff1SDavid S. Miller tb->tb_default)) { 11300c838ff1SDavid S. Miller fib_result_assign(res, fi); 11310c838ff1SDavid S. Miller tb->tb_default = order; 11320c838ff1SDavid S. Miller goto out; 11330c838ff1SDavid S. Miller } 11340c838ff1SDavid S. Miller 11350c838ff1SDavid S. Miller if (last_idx >= 0) 11360c838ff1SDavid S. Miller fib_result_assign(res, last_resort); 11370c838ff1SDavid S. Miller tb->tb_default = last_idx; 11380c838ff1SDavid S. Miller out: 113931d40937SEric Dumazet return; 11400c838ff1SDavid S. Miller } 11410c838ff1SDavid S. Miller 11421da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds /* 11456a31d2a9SEric Dumazet * Dead device goes up. We wake up dead nexthops. 11466a31d2a9SEric Dumazet * It takes sense only on multipath routes. 11471da177e4SLinus Torvalds */ 11481da177e4SLinus Torvalds int fib_sync_up(struct net_device *dev) 11491da177e4SLinus Torvalds { 11501da177e4SLinus Torvalds struct fib_info *prev_fi; 11511da177e4SLinus Torvalds unsigned int hash; 11521da177e4SLinus Torvalds struct hlist_head *head; 11531da177e4SLinus Torvalds struct hlist_node *node; 11541da177e4SLinus Torvalds struct fib_nh *nh; 11551da177e4SLinus Torvalds int ret; 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds if (!(dev->flags & IFF_UP)) 11581da177e4SLinus Torvalds return 0; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds prev_fi = NULL; 11611da177e4SLinus Torvalds hash = fib_devindex_hashfn(dev->ifindex); 11621da177e4SLinus Torvalds head = &fib_info_devhash[hash]; 11631da177e4SLinus Torvalds ret = 0; 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds hlist_for_each_entry(nh, node, head, nh_hash) { 11661da177e4SLinus Torvalds struct fib_info *fi = nh->nh_parent; 11671da177e4SLinus Torvalds int alive; 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds BUG_ON(!fi->fib_nhs); 11701da177e4SLinus Torvalds if (nh->nh_dev != dev || fi == prev_fi) 11711da177e4SLinus Torvalds continue; 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds prev_fi = fi; 11741da177e4SLinus Torvalds alive = 0; 11751da177e4SLinus Torvalds change_nexthops(fi) { 117671fceff0SDavid S. Miller if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) { 11771da177e4SLinus Torvalds alive++; 11781da177e4SLinus Torvalds continue; 11791da177e4SLinus Torvalds } 118071fceff0SDavid S. Miller if (nexthop_nh->nh_dev == NULL || 118171fceff0SDavid S. Miller !(nexthop_nh->nh_dev->flags & IFF_UP)) 11821da177e4SLinus Torvalds continue; 118371fceff0SDavid S. Miller if (nexthop_nh->nh_dev != dev || 118471fceff0SDavid S. Miller !__in_dev_get_rtnl(dev)) 11851da177e4SLinus Torvalds continue; 11861da177e4SLinus Torvalds alive++; 11871da177e4SLinus Torvalds spin_lock_bh(&fib_multipath_lock); 118871fceff0SDavid S. Miller nexthop_nh->nh_power = 0; 118971fceff0SDavid S. Miller nexthop_nh->nh_flags &= ~RTNH_F_DEAD; 11901da177e4SLinus Torvalds spin_unlock_bh(&fib_multipath_lock); 11911da177e4SLinus Torvalds } endfor_nexthops(fi) 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds if (alive > 0) { 11941da177e4SLinus Torvalds fi->fib_flags &= ~RTNH_F_DEAD; 11951da177e4SLinus Torvalds ret++; 11961da177e4SLinus Torvalds } 11971da177e4SLinus Torvalds } 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds return ret; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds /* 12036a31d2a9SEric Dumazet * The algorithm is suboptimal, but it provides really 12046a31d2a9SEric Dumazet * fair weighted route distribution. 12051da177e4SLinus Torvalds */ 12061b7fe593SDavid S. Miller void fib_select_multipath(struct fib_result *res) 12071da177e4SLinus Torvalds { 12081da177e4SLinus Torvalds struct fib_info *fi = res->fi; 12091da177e4SLinus Torvalds int w; 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds spin_lock_bh(&fib_multipath_lock); 12121da177e4SLinus Torvalds if (fi->fib_power <= 0) { 12131da177e4SLinus Torvalds int power = 0; 12141da177e4SLinus Torvalds change_nexthops(fi) { 121571fceff0SDavid S. Miller if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) { 121671fceff0SDavid S. Miller power += nexthop_nh->nh_weight; 121771fceff0SDavid S. Miller nexthop_nh->nh_power = nexthop_nh->nh_weight; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds } endfor_nexthops(fi); 12201da177e4SLinus Torvalds fi->fib_power = power; 12211da177e4SLinus Torvalds if (power <= 0) { 12221da177e4SLinus Torvalds spin_unlock_bh(&fib_multipath_lock); 12231da177e4SLinus Torvalds /* Race condition: route has just become dead. */ 12241da177e4SLinus Torvalds res->nh_sel = 0; 12251da177e4SLinus Torvalds return; 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds /* w should be random number [0..fi->fib_power-1], 12316a31d2a9SEric Dumazet * it is pretty bad approximation. 12321da177e4SLinus Torvalds */ 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds w = jiffies % fi->fib_power; 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds change_nexthops(fi) { 123771fceff0SDavid S. Miller if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) && 123871fceff0SDavid S. Miller nexthop_nh->nh_power) { 12396a31d2a9SEric Dumazet w -= nexthop_nh->nh_power; 12406a31d2a9SEric Dumazet if (w <= 0) { 124171fceff0SDavid S. Miller nexthop_nh->nh_power--; 12421da177e4SLinus Torvalds fi->fib_power--; 12431da177e4SLinus Torvalds res->nh_sel = nhsel; 12441da177e4SLinus Torvalds spin_unlock_bh(&fib_multipath_lock); 12451da177e4SLinus Torvalds return; 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds } endfor_nexthops(fi); 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds /* Race condition: route has just become dead. */ 12511da177e4SLinus Torvalds res->nh_sel = 0; 12521da177e4SLinus Torvalds spin_unlock_bh(&fib_multipath_lock); 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds #endif 1255