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> 45571e7226SRoopa Prabhu #include <net/lwtunnel.h> 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds #include "fib_lookup.h" 481da177e4SLinus Torvalds 49832b4c5eSStephen Hemminger static DEFINE_SPINLOCK(fib_info_lock); 501da177e4SLinus Torvalds static struct hlist_head *fib_info_hash; 511da177e4SLinus Torvalds static struct hlist_head *fib_info_laddrhash; 52123b9731SDavid S. Miller static unsigned int fib_info_hash_size; 531da177e4SLinus Torvalds static unsigned int fib_info_cnt; 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #define DEVINDEX_HASHBITS 8 561da177e4SLinus Torvalds #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS) 571da177e4SLinus Torvalds static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE]; 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds static DEFINE_SPINLOCK(fib_multipath_lock); 621da177e4SLinus Torvalds 636a31d2a9SEric Dumazet #define for_nexthops(fi) { \ 646a31d2a9SEric Dumazet int nhsel; const struct fib_nh *nh; \ 656a31d2a9SEric Dumazet for (nhsel = 0, nh = (fi)->fib_nh; \ 666a31d2a9SEric Dumazet nhsel < (fi)->fib_nhs; \ 676a31d2a9SEric Dumazet nh++, nhsel++) 681da177e4SLinus Torvalds 696a31d2a9SEric Dumazet #define change_nexthops(fi) { \ 706a31d2a9SEric Dumazet int nhsel; struct fib_nh *nexthop_nh; \ 716a31d2a9SEric Dumazet for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \ 726a31d2a9SEric Dumazet nhsel < (fi)->fib_nhs; \ 736a31d2a9SEric Dumazet nexthop_nh++, nhsel++) 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds #else /* CONFIG_IP_ROUTE_MULTIPATH */ 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds /* Hope, that gcc will optimize it to get rid of dummy loop */ 781da177e4SLinus Torvalds 796a31d2a9SEric Dumazet #define for_nexthops(fi) { \ 806a31d2a9SEric Dumazet int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \ 811da177e4SLinus Torvalds for (nhsel = 0; nhsel < 1; nhsel++) 821da177e4SLinus Torvalds 836a31d2a9SEric Dumazet #define change_nexthops(fi) { \ 846a31d2a9SEric Dumazet int nhsel; \ 856a31d2a9SEric Dumazet struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \ 861da177e4SLinus Torvalds for (nhsel = 0; nhsel < 1; nhsel++) 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds #endif /* CONFIG_IP_ROUTE_MULTIPATH */ 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds #define endfor_nexthops(fi) } 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds 933be0686bSDavid S. Miller const struct fib_prop fib_props[RTN_MAX + 1] = { 946a31d2a9SEric Dumazet [RTN_UNSPEC] = { 951da177e4SLinus Torvalds .error = 0, 961da177e4SLinus Torvalds .scope = RT_SCOPE_NOWHERE, 976a31d2a9SEric Dumazet }, 986a31d2a9SEric Dumazet [RTN_UNICAST] = { 991da177e4SLinus Torvalds .error = 0, 1001da177e4SLinus Torvalds .scope = RT_SCOPE_UNIVERSE, 1016a31d2a9SEric Dumazet }, 1026a31d2a9SEric Dumazet [RTN_LOCAL] = { 1031da177e4SLinus Torvalds .error = 0, 1041da177e4SLinus Torvalds .scope = RT_SCOPE_HOST, 1056a31d2a9SEric Dumazet }, 1066a31d2a9SEric Dumazet [RTN_BROADCAST] = { 1071da177e4SLinus Torvalds .error = 0, 1081da177e4SLinus Torvalds .scope = RT_SCOPE_LINK, 1096a31d2a9SEric Dumazet }, 1106a31d2a9SEric Dumazet [RTN_ANYCAST] = { 1111da177e4SLinus Torvalds .error = 0, 1121da177e4SLinus Torvalds .scope = RT_SCOPE_LINK, 1136a31d2a9SEric Dumazet }, 1146a31d2a9SEric Dumazet [RTN_MULTICAST] = { 1151da177e4SLinus Torvalds .error = 0, 1161da177e4SLinus Torvalds .scope = RT_SCOPE_UNIVERSE, 1176a31d2a9SEric Dumazet }, 1186a31d2a9SEric Dumazet [RTN_BLACKHOLE] = { 1191da177e4SLinus Torvalds .error = -EINVAL, 1201da177e4SLinus Torvalds .scope = RT_SCOPE_UNIVERSE, 1216a31d2a9SEric Dumazet }, 1226a31d2a9SEric Dumazet [RTN_UNREACHABLE] = { 1231da177e4SLinus Torvalds .error = -EHOSTUNREACH, 1241da177e4SLinus Torvalds .scope = RT_SCOPE_UNIVERSE, 1256a31d2a9SEric Dumazet }, 1266a31d2a9SEric Dumazet [RTN_PROHIBIT] = { 1271da177e4SLinus Torvalds .error = -EACCES, 1281da177e4SLinus Torvalds .scope = RT_SCOPE_UNIVERSE, 1296a31d2a9SEric Dumazet }, 1306a31d2a9SEric Dumazet [RTN_THROW] = { 1311da177e4SLinus Torvalds .error = -EAGAIN, 1321da177e4SLinus Torvalds .scope = RT_SCOPE_UNIVERSE, 1336a31d2a9SEric Dumazet }, 1346a31d2a9SEric Dumazet [RTN_NAT] = { 1351da177e4SLinus Torvalds .error = -EINVAL, 1361da177e4SLinus Torvalds .scope = RT_SCOPE_NOWHERE, 1376a31d2a9SEric Dumazet }, 1386a31d2a9SEric Dumazet [RTN_XRESOLVE] = { 1391da177e4SLinus Torvalds .error = -EINVAL, 1401da177e4SLinus Torvalds .scope = RT_SCOPE_NOWHERE, 1416a31d2a9SEric Dumazet }, 1421da177e4SLinus Torvalds }; 1431da177e4SLinus Torvalds 144c5038a83SDavid S. Miller static void rt_fibinfo_free(struct rtable __rcu **rtp) 14554764bb6SEric Dumazet { 14654764bb6SEric Dumazet struct rtable *rt = rcu_dereference_protected(*rtp, 1); 14754764bb6SEric Dumazet 14854764bb6SEric Dumazet if (!rt) 14954764bb6SEric Dumazet return; 15054764bb6SEric Dumazet 15154764bb6SEric Dumazet /* Not even needed : RCU_INIT_POINTER(*rtp, NULL); 15254764bb6SEric Dumazet * because we waited an RCU grace period before calling 15354764bb6SEric Dumazet * free_fib_info_rcu() 15454764bb6SEric Dumazet */ 15554764bb6SEric Dumazet 15654764bb6SEric Dumazet dst_free(&rt->dst); 15754764bb6SEric Dumazet } 15854764bb6SEric Dumazet 159c5038a83SDavid S. Miller static void free_nh_exceptions(struct fib_nh *nh) 160c5038a83SDavid S. Miller { 161caa41527SEric Dumazet struct fnhe_hash_bucket *hash; 162c5038a83SDavid S. Miller int i; 163c5038a83SDavid S. Miller 164caa41527SEric Dumazet hash = rcu_dereference_protected(nh->nh_exceptions, 1); 165caa41527SEric Dumazet if (!hash) 166caa41527SEric Dumazet return; 167c5038a83SDavid S. Miller for (i = 0; i < FNHE_HASH_SIZE; i++) { 168c5038a83SDavid S. Miller struct fib_nh_exception *fnhe; 169c5038a83SDavid S. Miller 170c5038a83SDavid S. Miller fnhe = rcu_dereference_protected(hash[i].chain, 1); 171c5038a83SDavid S. Miller while (fnhe) { 172c5038a83SDavid S. Miller struct fib_nh_exception *next; 173c5038a83SDavid S. Miller 174c5038a83SDavid S. Miller next = rcu_dereference_protected(fnhe->fnhe_next, 1); 175c5038a83SDavid S. Miller 1762ffae99dSTimo Teräs rt_fibinfo_free(&fnhe->fnhe_rth_input); 1772ffae99dSTimo Teräs rt_fibinfo_free(&fnhe->fnhe_rth_output); 178c5038a83SDavid S. Miller 179c5038a83SDavid S. Miller kfree(fnhe); 180c5038a83SDavid S. Miller 181c5038a83SDavid S. Miller fnhe = next; 182c5038a83SDavid S. Miller } 183c5038a83SDavid S. Miller } 184c5038a83SDavid S. Miller kfree(hash); 185c5038a83SDavid S. Miller } 186c5038a83SDavid S. Miller 187c5038a83SDavid S. Miller static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp) 188d26b3a7cSEric Dumazet { 189d26b3a7cSEric Dumazet int cpu; 190d26b3a7cSEric Dumazet 191d26b3a7cSEric Dumazet if (!rtp) 192d26b3a7cSEric Dumazet return; 193d26b3a7cSEric Dumazet 194d26b3a7cSEric Dumazet for_each_possible_cpu(cpu) { 195d26b3a7cSEric Dumazet struct rtable *rt; 196d26b3a7cSEric Dumazet 197d26b3a7cSEric Dumazet rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1); 198d26b3a7cSEric Dumazet if (rt) 199d26b3a7cSEric Dumazet dst_free(&rt->dst); 200d26b3a7cSEric Dumazet } 201d26b3a7cSEric Dumazet free_percpu(rtp); 202d26b3a7cSEric Dumazet } 203d26b3a7cSEric Dumazet 2041da177e4SLinus Torvalds /* Release a nexthop info record */ 20519c1ea14SYan, Zheng static void free_fib_info_rcu(struct rcu_head *head) 20619c1ea14SYan, Zheng { 20719c1ea14SYan, Zheng struct fib_info *fi = container_of(head, struct fib_info, rcu); 20819c1ea14SYan, Zheng 209e49cc0daSYanmin Zhang change_nexthops(fi) { 210e49cc0daSYanmin Zhang if (nexthop_nh->nh_dev) 211e49cc0daSYanmin Zhang dev_put(nexthop_nh->nh_dev); 2125a6228a0SNicolas Dichtel lwtstate_put(nexthop_nh->nh_lwtstate); 2134895c771SDavid S. Miller free_nh_exceptions(nexthop_nh); 214c5038a83SDavid S. Miller rt_fibinfo_free_cpus(nexthop_nh->nh_pcpu_rth_output); 215c5038a83SDavid S. Miller rt_fibinfo_free(&nexthop_nh->nh_rth_input); 216e49cc0daSYanmin Zhang } endfor_nexthops(fi); 217e49cc0daSYanmin Zhang 21819c1ea14SYan, Zheng if (fi->fib_metrics != (u32 *) dst_default_metrics) 21919c1ea14SYan, Zheng kfree(fi->fib_metrics); 22019c1ea14SYan, Zheng kfree(fi); 22119c1ea14SYan, Zheng } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds void free_fib_info(struct fib_info *fi) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds if (fi->fib_dead == 0) { 226058bd4d2SJoe Perches pr_warn("Freeing alive fib_info %p\n", fi); 2271da177e4SLinus Torvalds return; 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds fib_info_cnt--; 2307a9bc9b8SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID 2317a9bc9b8SDavid S. Miller change_nexthops(fi) { 2327a9bc9b8SDavid S. Miller if (nexthop_nh->nh_tclassid) 233f4530fa5SDavid S. Miller fi->fib_net->ipv4.fib_num_tclassid_users--; 2347a9bc9b8SDavid S. Miller } endfor_nexthops(fi); 2357a9bc9b8SDavid S. Miller #endif 23619c1ea14SYan, Zheng call_rcu(&fi->rcu, free_fib_info_rcu); 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds void fib_release_info(struct fib_info *fi) 2401da177e4SLinus Torvalds { 241832b4c5eSStephen Hemminger spin_lock_bh(&fib_info_lock); 2421da177e4SLinus Torvalds if (fi && --fi->fib_treeref == 0) { 2431da177e4SLinus Torvalds hlist_del(&fi->fib_hash); 2441da177e4SLinus Torvalds if (fi->fib_prefsrc) 2451da177e4SLinus Torvalds hlist_del(&fi->fib_lhash); 2461da177e4SLinus Torvalds change_nexthops(fi) { 24771fceff0SDavid S. Miller if (!nexthop_nh->nh_dev) 2481da177e4SLinus Torvalds continue; 24971fceff0SDavid S. Miller hlist_del(&nexthop_nh->nh_hash); 2501da177e4SLinus Torvalds } endfor_nexthops(fi) 2511da177e4SLinus Torvalds fi->fib_dead = 1; 2521da177e4SLinus Torvalds fib_info_put(fi); 2531da177e4SLinus Torvalds } 254832b4c5eSStephen Hemminger spin_unlock_bh(&fib_info_lock); 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2576a31d2a9SEric Dumazet static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi) 2581da177e4SLinus Torvalds { 2591da177e4SLinus Torvalds const struct fib_nh *onh = ofi->fib_nh; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds for_nexthops(fi) { 2621da177e4SLinus Torvalds if (nh->nh_oif != onh->nh_oif || 2631da177e4SLinus Torvalds nh->nh_gw != onh->nh_gw || 2641da177e4SLinus Torvalds nh->nh_scope != onh->nh_scope || 2651da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 2661da177e4SLinus Torvalds nh->nh_weight != onh->nh_weight || 2671da177e4SLinus Torvalds #endif 268c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 2691da177e4SLinus Torvalds nh->nh_tclassid != onh->nh_tclassid || 2701da177e4SLinus Torvalds #endif 271571e7226SRoopa Prabhu lwtunnel_cmp_encap(nh->nh_lwtstate, onh->nh_lwtstate) || 2728a3d0316SAndy Gospodarek ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_COMPARE_MASK)) 2731da177e4SLinus Torvalds return -1; 2741da177e4SLinus Torvalds onh++; 2751da177e4SLinus Torvalds } endfor_nexthops(fi); 2761da177e4SLinus Torvalds return 0; 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 27988ebc72fSDavid S. Miller static inline unsigned int fib_devindex_hashfn(unsigned int val) 28088ebc72fSDavid S. Miller { 28188ebc72fSDavid S. Miller unsigned int mask = DEVINDEX_HASHSIZE - 1; 28288ebc72fSDavid S. Miller 28388ebc72fSDavid S. Miller return (val ^ 28488ebc72fSDavid S. Miller (val >> DEVINDEX_HASHBITS) ^ 28588ebc72fSDavid S. Miller (val >> (DEVINDEX_HASHBITS * 2))) & mask; 28688ebc72fSDavid S. Miller } 28788ebc72fSDavid S. Miller 2881da177e4SLinus Torvalds static inline unsigned int fib_info_hashfn(const struct fib_info *fi) 2891da177e4SLinus Torvalds { 290123b9731SDavid S. Miller unsigned int mask = (fib_info_hash_size - 1); 2911da177e4SLinus Torvalds unsigned int val = fi->fib_nhs; 2921da177e4SLinus Torvalds 29337e826c5SDavid S. Miller val ^= (fi->fib_protocol << 8) | fi->fib_scope; 29481f7bf6cSAl Viro val ^= (__force u32)fi->fib_prefsrc; 2951da177e4SLinus Torvalds val ^= fi->fib_priority; 29688ebc72fSDavid S. Miller for_nexthops(fi) { 29788ebc72fSDavid S. Miller val ^= fib_devindex_hashfn(nh->nh_oif); 29888ebc72fSDavid S. Miller } endfor_nexthops(fi) 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds return (val ^ (val >> 7) ^ (val >> 12)) & mask; 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds static struct fib_info *fib_find_info(const struct fib_info *nfi) 3041da177e4SLinus Torvalds { 3051da177e4SLinus Torvalds struct hlist_head *head; 3061da177e4SLinus Torvalds struct fib_info *fi; 3071da177e4SLinus Torvalds unsigned int hash; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds hash = fib_info_hashfn(nfi); 3101da177e4SLinus Torvalds head = &fib_info_hash[hash]; 3111da177e4SLinus Torvalds 312b67bfe0dSSasha Levin hlist_for_each_entry(fi, head, fib_hash) { 31309ad9bc7SOctavian Purdila if (!net_eq(fi->fib_net, nfi->fib_net)) 3144814bdbdSDenis V. Lunev continue; 3151da177e4SLinus Torvalds if (fi->fib_nhs != nfi->fib_nhs) 3161da177e4SLinus Torvalds continue; 3171da177e4SLinus Torvalds if (nfi->fib_protocol == fi->fib_protocol && 31837e826c5SDavid S. Miller nfi->fib_scope == fi->fib_scope && 3191da177e4SLinus Torvalds nfi->fib_prefsrc == fi->fib_prefsrc && 3201da177e4SLinus Torvalds nfi->fib_priority == fi->fib_priority && 321f4ef85bbSEric Dumazet nfi->fib_type == fi->fib_type && 3221da177e4SLinus Torvalds memcmp(nfi->fib_metrics, fi->fib_metrics, 323fcd13f42SEric Dumazet sizeof(u32) * RTAX_MAX) == 0 && 3248a3d0316SAndy Gospodarek !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) && 3251da177e4SLinus Torvalds (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0)) 3261da177e4SLinus Torvalds return fi; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds return NULL; 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds /* Check, that the gateway is already configured. 3336a31d2a9SEric Dumazet * Used only by redirect accept routine. 3341da177e4SLinus Torvalds */ 335d878e72eSAl Viro int ip_fib_check_default(__be32 gw, struct net_device *dev) 3361da177e4SLinus Torvalds { 3371da177e4SLinus Torvalds struct hlist_head *head; 3381da177e4SLinus Torvalds struct fib_nh *nh; 3391da177e4SLinus Torvalds unsigned int hash; 3401da177e4SLinus Torvalds 341832b4c5eSStephen Hemminger spin_lock(&fib_info_lock); 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds hash = fib_devindex_hashfn(dev->ifindex); 3441da177e4SLinus Torvalds head = &fib_info_devhash[hash]; 345b67bfe0dSSasha Levin hlist_for_each_entry(nh, head, nh_hash) { 3461da177e4SLinus Torvalds if (nh->nh_dev == dev && 3471da177e4SLinus Torvalds nh->nh_gw == gw && 3481da177e4SLinus Torvalds !(nh->nh_flags & RTNH_F_DEAD)) { 349832b4c5eSStephen Hemminger spin_unlock(&fib_info_lock); 3501da177e4SLinus Torvalds return 0; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 354832b4c5eSStephen Hemminger spin_unlock(&fib_info_lock); 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds return -1; 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 359339bf98fSThomas Graf static inline size_t fib_nlmsg_size(struct fib_info *fi) 360339bf98fSThomas Graf { 361339bf98fSThomas Graf size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg)) 362339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 363339bf98fSThomas Graf + nla_total_size(4) /* RTA_DST */ 364339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 365ea697639SDaniel Borkmann + nla_total_size(4) /* RTA_PREFSRC */ 366ea697639SDaniel Borkmann + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */ 367339bf98fSThomas Graf 368339bf98fSThomas Graf /* space for nested metrics */ 369339bf98fSThomas Graf payload += nla_total_size((RTAX_MAX * nla_total_size(4))); 370339bf98fSThomas Graf 371339bf98fSThomas Graf if (fi->fib_nhs) { 372571e7226SRoopa Prabhu size_t nh_encapsize = 0; 373339bf98fSThomas Graf /* Also handles the special case fib_nhs == 1 */ 374339bf98fSThomas Graf 375339bf98fSThomas Graf /* each nexthop is packed in an attribute */ 376339bf98fSThomas Graf size_t nhsize = nla_total_size(sizeof(struct rtnexthop)); 377339bf98fSThomas Graf 378339bf98fSThomas Graf /* may contain flow and gateway attribute */ 379339bf98fSThomas Graf nhsize += 2 * nla_total_size(4); 380339bf98fSThomas Graf 381571e7226SRoopa Prabhu /* grab encap info */ 382571e7226SRoopa Prabhu for_nexthops(fi) { 383571e7226SRoopa Prabhu if (nh->nh_lwtstate) { 384571e7226SRoopa Prabhu /* RTA_ENCAP_TYPE */ 385571e7226SRoopa Prabhu nh_encapsize += lwtunnel_get_encap_size( 386571e7226SRoopa Prabhu nh->nh_lwtstate); 387571e7226SRoopa Prabhu /* RTA_ENCAP */ 388571e7226SRoopa Prabhu nh_encapsize += nla_total_size(2); 389571e7226SRoopa Prabhu } 390571e7226SRoopa Prabhu } endfor_nexthops(fi); 391571e7226SRoopa Prabhu 392339bf98fSThomas Graf /* all nexthops are packed in a nested attribute */ 393571e7226SRoopa Prabhu payload += nla_total_size((fi->fib_nhs * nhsize) + 394571e7226SRoopa Prabhu nh_encapsize); 395571e7226SRoopa Prabhu 396339bf98fSThomas Graf } 397339bf98fSThomas Graf 398339bf98fSThomas Graf return payload; 399339bf98fSThomas Graf } 400339bf98fSThomas Graf 40181f7bf6cSAl Viro void rtmsg_fib(int event, __be32 key, struct fib_alias *fa, 4029877b253SJoe Perches int dst_len, u32 tb_id, const struct nl_info *info, 403b8f55831SMilan Kocian unsigned int nlm_flags) 4041da177e4SLinus Torvalds { 4051da177e4SLinus Torvalds struct sk_buff *skb; 4064e902c57SThomas Graf u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 407f21c7bc5SThomas Graf int err = -ENOBUFS; 4081da177e4SLinus Torvalds 409339bf98fSThomas Graf skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL); 41051456b29SIan Morris if (!skb) 411f21c7bc5SThomas Graf goto errout; 4121da177e4SLinus Torvalds 41315e47304SEric W. Biederman err = fib_dump_info(skb, info->portid, seq, event, tb_id, 41437e826c5SDavid S. Miller fa->fa_type, key, dst_len, 415b8f55831SMilan Kocian fa->fa_tos, fa->fa_info, nlm_flags); 41626932566SPatrick McHardy if (err < 0) { 41726932566SPatrick McHardy /* -EMSGSIZE implies BUG in fib_nlmsg_size() */ 41826932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 41926932566SPatrick McHardy kfree_skb(skb); 42026932566SPatrick McHardy goto errout; 42126932566SPatrick McHardy } 42215e47304SEric W. Biederman rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE, 4234e902c57SThomas Graf info->nlh, GFP_KERNEL); 4241ce85fe4SPablo Neira Ayuso return; 425f21c7bc5SThomas Graf errout: 426f21c7bc5SThomas Graf if (err < 0) 4274d1169c1SDenis V. Lunev rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err); 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds 430c9cb6b6eSStephen Hemminger static int fib_detect_death(struct fib_info *fi, int order, 431c9cb6b6eSStephen Hemminger struct fib_info **last_resort, int *last_idx, 432c9cb6b6eSStephen Hemminger int dflt) 4331da177e4SLinus Torvalds { 4341da177e4SLinus Torvalds struct neighbour *n; 4351da177e4SLinus Torvalds int state = NUD_NONE; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev); 4381da177e4SLinus Torvalds if (n) { 4391da177e4SLinus Torvalds state = n->nud_state; 4401da177e4SLinus Torvalds neigh_release(n); 44188f64320SJulian Anastasov } else { 44288f64320SJulian Anastasov return 0; 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds if (state == NUD_REACHABLE) 4451da177e4SLinus Torvalds return 0; 446c17860a0SDenis V. Lunev if ((state & NUD_VALID) && order != dflt) 4471da177e4SLinus Torvalds return 0; 4481da177e4SLinus Torvalds if ((state & NUD_VALID) || 44988f64320SJulian Anastasov (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) { 4501da177e4SLinus Torvalds *last_resort = fi; 4511da177e4SLinus Torvalds *last_idx = order; 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds return 1; 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 4571da177e4SLinus Torvalds 4584e902c57SThomas Graf static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining) 4591da177e4SLinus Torvalds { 4601da177e4SLinus Torvalds int nhs = 0; 4611da177e4SLinus Torvalds 4624e902c57SThomas Graf while (rtnh_ok(rtnh, remaining)) { 4631da177e4SLinus Torvalds nhs++; 4644e902c57SThomas Graf rtnh = rtnh_next(rtnh, &remaining); 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 4674e902c57SThomas Graf /* leftover implies invalid nexthop configuration, discard it */ 4684e902c57SThomas Graf return remaining > 0 ? 0 : nhs; 4694e902c57SThomas Graf } 4701da177e4SLinus Torvalds 4714e902c57SThomas Graf static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh, 4724e902c57SThomas Graf int remaining, struct fib_config *cfg) 4734e902c57SThomas Graf { 474571e7226SRoopa Prabhu struct net *net = cfg->fc_nlinfo.nl_net; 475571e7226SRoopa Prabhu int ret; 476571e7226SRoopa Prabhu 4771da177e4SLinus Torvalds change_nexthops(fi) { 4784e902c57SThomas Graf int attrlen; 4794e902c57SThomas Graf 4804e902c57SThomas Graf if (!rtnh_ok(rtnh, remaining)) 4811da177e4SLinus Torvalds return -EINVAL; 4824e902c57SThomas Graf 48371fceff0SDavid S. Miller nexthop_nh->nh_flags = 48471fceff0SDavid S. Miller (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags; 48571fceff0SDavid S. Miller nexthop_nh->nh_oif = rtnh->rtnh_ifindex; 48671fceff0SDavid S. Miller nexthop_nh->nh_weight = rtnh->rtnh_hops + 1; 4874e902c57SThomas Graf 4884e902c57SThomas Graf attrlen = rtnh_attrlen(rtnh); 4894e902c57SThomas Graf if (attrlen > 0) { 4904e902c57SThomas Graf struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 4914e902c57SThomas Graf 4924e902c57SThomas Graf nla = nla_find(attrs, attrlen, RTA_GATEWAY); 49367b61f6cSJiri Benc nexthop_nh->nh_gw = nla ? nla_get_in_addr(nla) : 0; 494c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 4954e902c57SThomas Graf nla = nla_find(attrs, attrlen, RTA_FLOW); 49671fceff0SDavid S. Miller nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0; 4977a9bc9b8SDavid S. Miller if (nexthop_nh->nh_tclassid) 498f4530fa5SDavid S. Miller fi->fib_net->ipv4.fib_num_tclassid_users++; 4991da177e4SLinus Torvalds #endif 500571e7226SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP); 501571e7226SRoopa Prabhu if (nla) { 502571e7226SRoopa Prabhu struct lwtunnel_state *lwtstate; 503571e7226SRoopa Prabhu struct net_device *dev = NULL; 504571e7226SRoopa Prabhu struct nlattr *nla_entype; 505571e7226SRoopa Prabhu 506571e7226SRoopa Prabhu nla_entype = nla_find(attrs, attrlen, 507571e7226SRoopa Prabhu RTA_ENCAP_TYPE); 508571e7226SRoopa Prabhu if (!nla_entype) 509571e7226SRoopa Prabhu goto err_inval; 510571e7226SRoopa Prabhu if (cfg->fc_oif) 511571e7226SRoopa Prabhu dev = __dev_get_by_index(net, cfg->fc_oif); 512571e7226SRoopa Prabhu ret = lwtunnel_build_state(dev, nla_get_u16( 513571e7226SRoopa Prabhu nla_entype), 514571e7226SRoopa Prabhu nla, &lwtstate); 515571e7226SRoopa Prabhu if (ret) 516571e7226SRoopa Prabhu goto errout; 5175a6228a0SNicolas Dichtel nexthop_nh->nh_lwtstate = 5185a6228a0SNicolas Dichtel lwtstate_get(lwtstate); 519571e7226SRoopa Prabhu } 5201da177e4SLinus Torvalds } 5214e902c57SThomas Graf 5224e902c57SThomas Graf rtnh = rtnh_next(rtnh, &remaining); 5231da177e4SLinus Torvalds } endfor_nexthops(fi); 5244e902c57SThomas Graf 5251da177e4SLinus Torvalds return 0; 526571e7226SRoopa Prabhu 527571e7226SRoopa Prabhu err_inval: 528571e7226SRoopa Prabhu ret = -EINVAL; 529571e7226SRoopa Prabhu 530571e7226SRoopa Prabhu errout: 531571e7226SRoopa Prabhu return ret; 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds #endif 5351da177e4SLinus Torvalds 536571e7226SRoopa Prabhu int fib_encap_match(struct net *net, u16 encap_type, 537571e7226SRoopa Prabhu struct nlattr *encap, 538571e7226SRoopa Prabhu int oif, const struct fib_nh *nh) 539571e7226SRoopa Prabhu { 540571e7226SRoopa Prabhu struct lwtunnel_state *lwtstate; 541571e7226SRoopa Prabhu struct net_device *dev = NULL; 542571e7226SRoopa Prabhu int ret; 543571e7226SRoopa Prabhu 544571e7226SRoopa Prabhu if (encap_type == LWTUNNEL_ENCAP_NONE) 545571e7226SRoopa Prabhu return 0; 546571e7226SRoopa Prabhu 547571e7226SRoopa Prabhu if (oif) 548571e7226SRoopa Prabhu dev = __dev_get_by_index(net, oif); 549571e7226SRoopa Prabhu ret = lwtunnel_build_state(dev, encap_type, 550571e7226SRoopa Prabhu encap, &lwtstate); 551571e7226SRoopa Prabhu if (!ret) 552571e7226SRoopa Prabhu return lwtunnel_cmp_encap(lwtstate, nh->nh_lwtstate); 553571e7226SRoopa Prabhu 554571e7226SRoopa Prabhu return 0; 555571e7226SRoopa Prabhu } 556571e7226SRoopa Prabhu 5574e902c57SThomas Graf int fib_nh_match(struct fib_config *cfg, struct fib_info *fi) 5581da177e4SLinus Torvalds { 559571e7226SRoopa Prabhu struct net *net = cfg->fc_nlinfo.nl_net; 5601da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 5614e902c57SThomas Graf struct rtnexthop *rtnh; 5624e902c57SThomas Graf int remaining; 5631da177e4SLinus Torvalds #endif 5641da177e4SLinus Torvalds 5654e902c57SThomas Graf if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority) 5661da177e4SLinus Torvalds return 1; 5671da177e4SLinus Torvalds 5684e902c57SThomas Graf if (cfg->fc_oif || cfg->fc_gw) { 569571e7226SRoopa Prabhu if (cfg->fc_encap) { 570571e7226SRoopa Prabhu if (fib_encap_match(net, cfg->fc_encap_type, 571571e7226SRoopa Prabhu cfg->fc_encap, cfg->fc_oif, 572571e7226SRoopa Prabhu fi->fib_nh)) 573571e7226SRoopa Prabhu return 1; 574571e7226SRoopa Prabhu } 5754e902c57SThomas Graf if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) && 5764e902c57SThomas Graf (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw)) 5771da177e4SLinus Torvalds return 0; 5781da177e4SLinus Torvalds return 1; 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 58251456b29SIan Morris if (!cfg->fc_mp) 5831da177e4SLinus Torvalds return 0; 5844e902c57SThomas Graf 5854e902c57SThomas Graf rtnh = cfg->fc_mp; 5864e902c57SThomas Graf remaining = cfg->fc_mp_len; 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds for_nexthops(fi) { 5894e902c57SThomas Graf int attrlen; 5901da177e4SLinus Torvalds 5914e902c57SThomas Graf if (!rtnh_ok(rtnh, remaining)) 5921da177e4SLinus Torvalds return -EINVAL; 5934e902c57SThomas Graf 5944e902c57SThomas Graf if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif) 5951da177e4SLinus Torvalds return 1; 5964e902c57SThomas Graf 5974e902c57SThomas Graf attrlen = rtnh_attrlen(rtnh); 598f76936d0SJiri Pirko if (attrlen > 0) { 5994e902c57SThomas Graf struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 6004e902c57SThomas Graf 6014e902c57SThomas Graf nla = nla_find(attrs, attrlen, RTA_GATEWAY); 60267b61f6cSJiri Benc if (nla && nla_get_in_addr(nla) != nh->nh_gw) 6031da177e4SLinus Torvalds return 1; 604c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 6054e902c57SThomas Graf nla = nla_find(attrs, attrlen, RTA_FLOW); 6064e902c57SThomas Graf if (nla && nla_get_u32(nla) != nh->nh_tclassid) 6071da177e4SLinus Torvalds return 1; 6081da177e4SLinus Torvalds #endif 6091da177e4SLinus Torvalds } 6104e902c57SThomas Graf 6114e902c57SThomas Graf rtnh = rtnh_next(rtnh, &remaining); 6121da177e4SLinus Torvalds } endfor_nexthops(fi); 6131da177e4SLinus Torvalds #endif 6141da177e4SLinus Torvalds return 0; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds /* 6196a31d2a9SEric Dumazet * Picture 6206a31d2a9SEric Dumazet * ------- 6216a31d2a9SEric Dumazet * 6226a31d2a9SEric Dumazet * Semantics of nexthop is very messy by historical reasons. 6236a31d2a9SEric Dumazet * We have to take into account, that: 6246a31d2a9SEric Dumazet * a) gateway can be actually local interface address, 6256a31d2a9SEric Dumazet * so that gatewayed route is direct. 6266a31d2a9SEric Dumazet * b) gateway must be on-link address, possibly 6276a31d2a9SEric Dumazet * described not by an ifaddr, but also by a direct route. 6286a31d2a9SEric Dumazet * c) If both gateway and interface are specified, they should not 6296a31d2a9SEric Dumazet * contradict. 6306a31d2a9SEric Dumazet * d) If we use tunnel routes, gateway could be not on-link. 6316a31d2a9SEric Dumazet * 6326a31d2a9SEric Dumazet * Attempt to reconcile all of these (alas, self-contradictory) conditions 6336a31d2a9SEric Dumazet * results in pretty ugly and hairy code with obscure logic. 6346a31d2a9SEric Dumazet * 6356a31d2a9SEric Dumazet * I chose to generalized it instead, so that the size 6366a31d2a9SEric Dumazet * of code does not increase practically, but it becomes 6376a31d2a9SEric Dumazet * much more general. 6386a31d2a9SEric Dumazet * Every prefix is assigned a "scope" value: "host" is local address, 6396a31d2a9SEric Dumazet * "link" is direct route, 6406a31d2a9SEric Dumazet * [ ... "site" ... "interior" ... ] 6416a31d2a9SEric Dumazet * and "universe" is true gateway route with global meaning. 6426a31d2a9SEric Dumazet * 6436a31d2a9SEric Dumazet * Every prefix refers to a set of "nexthop"s (gw, oif), 6446a31d2a9SEric Dumazet * where gw must have narrower scope. This recursion stops 6456a31d2a9SEric Dumazet * when gw has LOCAL scope or if "nexthop" is declared ONLINK, 6466a31d2a9SEric Dumazet * which means that gw is forced to be on link. 6476a31d2a9SEric Dumazet * 6486a31d2a9SEric Dumazet * Code is still hairy, but now it is apparently logically 6496a31d2a9SEric Dumazet * consistent and very flexible. F.e. as by-product it allows 6506a31d2a9SEric Dumazet * to co-exists in peace independent exterior and interior 6516a31d2a9SEric Dumazet * routing processes. 6526a31d2a9SEric Dumazet * 6536a31d2a9SEric Dumazet * Normally it looks as following. 6546a31d2a9SEric Dumazet * 6556a31d2a9SEric Dumazet * {universe prefix} -> (gw, oif) [scope link] 6566a31d2a9SEric Dumazet * | 6576a31d2a9SEric Dumazet * |-> {link prefix} -> (gw, oif) [scope local] 6586a31d2a9SEric Dumazet * | 6596a31d2a9SEric Dumazet * |-> {local prefix} (terminal node) 6601da177e4SLinus Torvalds */ 6614e902c57SThomas Graf static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi, 6624e902c57SThomas Graf struct fib_nh *nh) 6631da177e4SLinus Torvalds { 6641da177e4SLinus Torvalds int err; 66586167a37SDenis V. Lunev struct net *net; 6666a31d2a9SEric Dumazet struct net_device *dev; 6671da177e4SLinus Torvalds 66886167a37SDenis V. Lunev net = cfg->fc_nlinfo.nl_net; 6691da177e4SLinus Torvalds if (nh->nh_gw) { 6701da177e4SLinus Torvalds struct fib_result res; 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds if (nh->nh_flags & RTNH_F_ONLINK) { 67330bbaa19SDavid Ahern unsigned int addr_type; 6741da177e4SLinus Torvalds 6754e902c57SThomas Graf if (cfg->fc_scope >= RT_SCOPE_LINK) 6761da177e4SLinus Torvalds return -EINVAL; 6776a31d2a9SEric Dumazet dev = __dev_get_by_index(net, nh->nh_oif); 6786a31d2a9SEric Dumazet if (!dev) 6791da177e4SLinus Torvalds return -ENODEV; 6801da177e4SLinus Torvalds if (!(dev->flags & IFF_UP)) 6811da177e4SLinus Torvalds return -ENETDOWN; 68230bbaa19SDavid Ahern addr_type = inet_addr_type_dev_table(net, dev, nh->nh_gw); 68330bbaa19SDavid Ahern if (addr_type != RTN_UNICAST) 68430bbaa19SDavid Ahern return -EINVAL; 6858a3d0316SAndy Gospodarek if (!netif_carrier_ok(dev)) 6868a3d0316SAndy Gospodarek nh->nh_flags |= RTNH_F_LINKDOWN; 6871da177e4SLinus Torvalds nh->nh_dev = dev; 6881da177e4SLinus Torvalds dev_hold(dev); 6891da177e4SLinus Torvalds nh->nh_scope = RT_SCOPE_LINK; 6901da177e4SLinus Torvalds return 0; 6911da177e4SLinus Torvalds } 692ebc0ffaeSEric Dumazet rcu_read_lock(); 6931da177e4SLinus Torvalds { 6949ade2286SDavid S. Miller struct flowi4 fl4 = { 6959ade2286SDavid S. Miller .daddr = nh->nh_gw, 6969ade2286SDavid S. Miller .flowi4_scope = cfg->fc_scope + 1, 6979ade2286SDavid S. Miller .flowi4_oif = nh->nh_oif, 6986a662719SCong Wang .flowi4_iif = LOOPBACK_IFINDEX, 6994e902c57SThomas Graf }; 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds /* It is not necessary, but requires a bit of thinking */ 7029ade2286SDavid S. Miller if (fl4.flowi4_scope < RT_SCOPE_LINK) 7039ade2286SDavid S. Miller fl4.flowi4_scope = RT_SCOPE_LINK; 7040eeb075fSAndy Gospodarek err = fib_lookup(net, &fl4, &res, 7050eeb075fSAndy Gospodarek FIB_LOOKUP_IGNORE_LINKSTATE); 706ebc0ffaeSEric Dumazet if (err) { 707ebc0ffaeSEric Dumazet rcu_read_unlock(); 7081da177e4SLinus Torvalds return err; 7091da177e4SLinus Torvalds } 710ebc0ffaeSEric Dumazet } 7111da177e4SLinus Torvalds err = -EINVAL; 7121da177e4SLinus Torvalds if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) 7131da177e4SLinus Torvalds goto out; 7141da177e4SLinus Torvalds nh->nh_scope = res.scope; 7151da177e4SLinus Torvalds nh->nh_oif = FIB_RES_OIF(res); 7166a31d2a9SEric Dumazet nh->nh_dev = dev = FIB_RES_DEV(res); 7176a31d2a9SEric Dumazet if (!dev) 7181da177e4SLinus Torvalds goto out; 7196a31d2a9SEric Dumazet dev_hold(dev); 7208a3d0316SAndy Gospodarek if (!netif_carrier_ok(dev)) 7218a3d0316SAndy Gospodarek nh->nh_flags |= RTNH_F_LINKDOWN; 7228723e1b4SEric Dumazet err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN; 7231da177e4SLinus Torvalds } else { 7241da177e4SLinus Torvalds struct in_device *in_dev; 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) 7271da177e4SLinus Torvalds return -EINVAL; 7281da177e4SLinus Torvalds 7298723e1b4SEric Dumazet rcu_read_lock(); 7308723e1b4SEric Dumazet err = -ENODEV; 73186167a37SDenis V. Lunev in_dev = inetdev_by_index(net, nh->nh_oif); 73251456b29SIan Morris if (!in_dev) 7338723e1b4SEric Dumazet goto out; 7348723e1b4SEric Dumazet err = -ENETDOWN; 7358723e1b4SEric Dumazet if (!(in_dev->dev->flags & IFF_UP)) 7368723e1b4SEric Dumazet goto out; 7371da177e4SLinus Torvalds nh->nh_dev = in_dev->dev; 7381da177e4SLinus Torvalds dev_hold(nh->nh_dev); 7391da177e4SLinus Torvalds nh->nh_scope = RT_SCOPE_HOST; 7408a3d0316SAndy Gospodarek if (!netif_carrier_ok(nh->nh_dev)) 7418a3d0316SAndy Gospodarek nh->nh_flags |= RTNH_F_LINKDOWN; 7428723e1b4SEric Dumazet err = 0; 7431da177e4SLinus Torvalds } 7448723e1b4SEric Dumazet out: 7458723e1b4SEric Dumazet rcu_read_unlock(); 7468723e1b4SEric Dumazet return err; 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds 74981f7bf6cSAl Viro static inline unsigned int fib_laddr_hashfn(__be32 val) 7501da177e4SLinus Torvalds { 751123b9731SDavid S. Miller unsigned int mask = (fib_info_hash_size - 1); 7521da177e4SLinus Torvalds 7536a31d2a9SEric Dumazet return ((__force u32)val ^ 7546a31d2a9SEric Dumazet ((__force u32)val >> 7) ^ 7556a31d2a9SEric Dumazet ((__force u32)val >> 14)) & mask; 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 758123b9731SDavid S. Miller static struct hlist_head *fib_info_hash_alloc(int bytes) 7591da177e4SLinus Torvalds { 7601da177e4SLinus Torvalds if (bytes <= PAGE_SIZE) 76188f83491SJoonwoo Park return kzalloc(bytes, GFP_KERNEL); 7621da177e4SLinus Torvalds else 7631da177e4SLinus Torvalds return (struct hlist_head *) 7646a31d2a9SEric Dumazet __get_free_pages(GFP_KERNEL | __GFP_ZERO, 7656a31d2a9SEric Dumazet get_order(bytes)); 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds 768123b9731SDavid S. Miller static void fib_info_hash_free(struct hlist_head *hash, int bytes) 7691da177e4SLinus Torvalds { 7701da177e4SLinus Torvalds if (!hash) 7711da177e4SLinus Torvalds return; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds if (bytes <= PAGE_SIZE) 7741da177e4SLinus Torvalds kfree(hash); 7751da177e4SLinus Torvalds else 7761da177e4SLinus Torvalds free_pages((unsigned long) hash, get_order(bytes)); 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds 779123b9731SDavid S. Miller static void fib_info_hash_move(struct hlist_head *new_info_hash, 7801da177e4SLinus Torvalds struct hlist_head *new_laddrhash, 7811da177e4SLinus Torvalds unsigned int new_size) 7821da177e4SLinus Torvalds { 783b7656e7fSDavid S. Miller struct hlist_head *old_info_hash, *old_laddrhash; 784123b9731SDavid S. Miller unsigned int old_size = fib_info_hash_size; 785b7656e7fSDavid S. Miller unsigned int i, bytes; 7861da177e4SLinus Torvalds 787832b4c5eSStephen Hemminger spin_lock_bh(&fib_info_lock); 788b7656e7fSDavid S. Miller old_info_hash = fib_info_hash; 789b7656e7fSDavid S. Miller old_laddrhash = fib_info_laddrhash; 790123b9731SDavid S. Miller fib_info_hash_size = new_size; 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds for (i = 0; i < old_size; i++) { 7931da177e4SLinus Torvalds struct hlist_head *head = &fib_info_hash[i]; 794b67bfe0dSSasha Levin struct hlist_node *n; 7951da177e4SLinus Torvalds struct fib_info *fi; 7961da177e4SLinus Torvalds 797b67bfe0dSSasha Levin hlist_for_each_entry_safe(fi, n, head, fib_hash) { 7981da177e4SLinus Torvalds struct hlist_head *dest; 7991da177e4SLinus Torvalds unsigned int new_hash; 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds new_hash = fib_info_hashfn(fi); 8021da177e4SLinus Torvalds dest = &new_info_hash[new_hash]; 8031da177e4SLinus Torvalds hlist_add_head(&fi->fib_hash, dest); 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds } 8061da177e4SLinus Torvalds fib_info_hash = new_info_hash; 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds for (i = 0; i < old_size; i++) { 8091da177e4SLinus Torvalds struct hlist_head *lhead = &fib_info_laddrhash[i]; 810b67bfe0dSSasha Levin struct hlist_node *n; 8111da177e4SLinus Torvalds struct fib_info *fi; 8121da177e4SLinus Torvalds 813b67bfe0dSSasha Levin hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) { 8141da177e4SLinus Torvalds struct hlist_head *ldest; 8151da177e4SLinus Torvalds unsigned int new_hash; 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds new_hash = fib_laddr_hashfn(fi->fib_prefsrc); 8181da177e4SLinus Torvalds ldest = &new_laddrhash[new_hash]; 8191da177e4SLinus Torvalds hlist_add_head(&fi->fib_lhash, ldest); 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds fib_info_laddrhash = new_laddrhash; 8231da177e4SLinus Torvalds 824832b4c5eSStephen Hemminger spin_unlock_bh(&fib_info_lock); 825b7656e7fSDavid S. Miller 826b7656e7fSDavid S. Miller bytes = old_size * sizeof(struct hlist_head *); 827123b9731SDavid S. Miller fib_info_hash_free(old_info_hash, bytes); 828123b9731SDavid S. Miller fib_info_hash_free(old_laddrhash, bytes); 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds 831436c3b66SDavid S. Miller __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh) 832436c3b66SDavid S. Miller { 833436c3b66SDavid S. Miller nh->nh_saddr = inet_select_addr(nh->nh_dev, 834436c3b66SDavid S. Miller nh->nh_gw, 83537e826c5SDavid S. Miller nh->nh_parent->fib_scope); 836436c3b66SDavid S. Miller nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid); 837436c3b66SDavid S. Miller 838436c3b66SDavid S. Miller return nh->nh_saddr; 839436c3b66SDavid S. Miller } 840436c3b66SDavid S. Miller 841*021dd3b8SDavid Ahern static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc) 842*021dd3b8SDavid Ahern { 843*021dd3b8SDavid Ahern if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst || 844*021dd3b8SDavid Ahern fib_prefsrc != cfg->fc_dst) { 845*021dd3b8SDavid Ahern int tb_id = cfg->fc_table; 846*021dd3b8SDavid Ahern 847*021dd3b8SDavid Ahern if (tb_id == RT_TABLE_MAIN) 848*021dd3b8SDavid Ahern tb_id = RT_TABLE_LOCAL; 849*021dd3b8SDavid Ahern 850*021dd3b8SDavid Ahern if (inet_addr_type_table(cfg->fc_nlinfo.nl_net, 851*021dd3b8SDavid Ahern fib_prefsrc, tb_id) != RTN_LOCAL) { 852*021dd3b8SDavid Ahern return false; 853*021dd3b8SDavid Ahern } 854*021dd3b8SDavid Ahern } 855*021dd3b8SDavid Ahern return true; 856*021dd3b8SDavid Ahern } 857*021dd3b8SDavid Ahern 8584e902c57SThomas Graf struct fib_info *fib_create_info(struct fib_config *cfg) 8591da177e4SLinus Torvalds { 8601da177e4SLinus Torvalds int err; 8611da177e4SLinus Torvalds struct fib_info *fi = NULL; 8621da177e4SLinus Torvalds struct fib_info *ofi; 8631da177e4SLinus Torvalds int nhs = 1; 8647462bd74SDenis V. Lunev struct net *net = cfg->fc_nlinfo.nl_net; 8651da177e4SLinus Torvalds 8664c8237cdSDavid S. Miller if (cfg->fc_type > RTN_MAX) 8674c8237cdSDavid S. Miller goto err_inval; 8684c8237cdSDavid S. Miller 8691da177e4SLinus Torvalds /* Fast check to catch the most weird cases */ 8704e902c57SThomas Graf if (fib_props[cfg->fc_type].scope > cfg->fc_scope) 8711da177e4SLinus Torvalds goto err_inval; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 8744e902c57SThomas Graf if (cfg->fc_mp) { 8754e902c57SThomas Graf nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len); 8761da177e4SLinus Torvalds if (nhs == 0) 8771da177e4SLinus Torvalds goto err_inval; 8781da177e4SLinus Torvalds } 8791da177e4SLinus Torvalds #endif 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds err = -ENOBUFS; 882123b9731SDavid S. Miller if (fib_info_cnt >= fib_info_hash_size) { 883123b9731SDavid S. Miller unsigned int new_size = fib_info_hash_size << 1; 8841da177e4SLinus Torvalds struct hlist_head *new_info_hash; 8851da177e4SLinus Torvalds struct hlist_head *new_laddrhash; 8861da177e4SLinus Torvalds unsigned int bytes; 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds if (!new_size) 889d94ce9b2SEric Dumazet new_size = 16; 8901da177e4SLinus Torvalds bytes = new_size * sizeof(struct hlist_head *); 891123b9731SDavid S. Miller new_info_hash = fib_info_hash_alloc(bytes); 892123b9731SDavid S. Miller new_laddrhash = fib_info_hash_alloc(bytes); 8931da177e4SLinus Torvalds if (!new_info_hash || !new_laddrhash) { 894123b9731SDavid S. Miller fib_info_hash_free(new_info_hash, bytes); 895123b9731SDavid S. Miller fib_info_hash_free(new_laddrhash, bytes); 89688f83491SJoonwoo Park } else 897123b9731SDavid S. Miller fib_info_hash_move(new_info_hash, new_laddrhash, new_size); 8981da177e4SLinus Torvalds 899123b9731SDavid S. Miller if (!fib_info_hash_size) 9001da177e4SLinus Torvalds goto failure; 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds 9030da974f4SPanagiotis Issaris fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL); 90451456b29SIan Morris if (!fi) 9051da177e4SLinus Torvalds goto failure; 906aeefa1ecSSergey Popovich fib_info_cnt++; 907725d1e1bSDavid S. Miller if (cfg->fc_mx) { 9089c150e82SDavid S. Miller fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 9099c150e82SDavid S. Miller if (!fi->fib_metrics) 9109c150e82SDavid S. Miller goto failure; 911725d1e1bSDavid S. Miller } else 912725d1e1bSDavid S. Miller fi->fib_metrics = (u32 *) dst_default_metrics; 9131da177e4SLinus Torvalds 914efd7ef1cSEric W. Biederman fi->fib_net = net; 9154e902c57SThomas Graf fi->fib_protocol = cfg->fc_protocol; 91637e826c5SDavid S. Miller fi->fib_scope = cfg->fc_scope; 9174e902c57SThomas Graf fi->fib_flags = cfg->fc_flags; 9184e902c57SThomas Graf fi->fib_priority = cfg->fc_priority; 9194e902c57SThomas Graf fi->fib_prefsrc = cfg->fc_prefsrc; 920f4ef85bbSEric Dumazet fi->fib_type = cfg->fc_type; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds fi->fib_nhs = nhs; 9231da177e4SLinus Torvalds change_nexthops(fi) { 92471fceff0SDavid S. Miller nexthop_nh->nh_parent = fi; 925d26b3a7cSEric Dumazet nexthop_nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *); 926f8a17175SJulian Anastasov if (!nexthop_nh->nh_pcpu_rth_output) 927f8a17175SJulian Anastasov goto failure; 9281da177e4SLinus Torvalds } endfor_nexthops(fi) 9291da177e4SLinus Torvalds 9304e902c57SThomas Graf if (cfg->fc_mx) { 9314e902c57SThomas Graf struct nlattr *nla; 9324e902c57SThomas Graf int remaining; 9331da177e4SLinus Torvalds 9344e902c57SThomas Graf nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 9358f4c1f9bSThomas Graf int type = nla_type(nla); 9364e902c57SThomas Graf 9374e902c57SThomas Graf if (type) { 9386fac2625SDavid S. Miller u32 val; 9396fac2625SDavid S. Miller 9404e902c57SThomas Graf if (type > RTAX_MAX) 9411da177e4SLinus Torvalds goto err_inval; 942ea697639SDaniel Borkmann if (type == RTAX_CC_ALGO) { 943ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX]; 944ea697639SDaniel Borkmann 945ea697639SDaniel Borkmann nla_strlcpy(tmp, nla, sizeof(tmp)); 946ea697639SDaniel Borkmann val = tcp_ca_get_key_by_name(tmp); 947ea697639SDaniel Borkmann if (val == TCP_CA_UNSPEC) 948ea697639SDaniel Borkmann goto err_inval; 949ea697639SDaniel Borkmann } else { 9506fac2625SDavid S. Miller val = nla_get_u32(nla); 951ea697639SDaniel Borkmann } 9526fac2625SDavid S. Miller if (type == RTAX_ADVMSS && val > 65535 - 40) 9536fac2625SDavid S. Miller val = 65535 - 40; 954710ab6c0SDavid S. Miller if (type == RTAX_MTU && val > 65535 - 15) 955710ab6c0SDavid S. Miller val = 65535 - 15; 9566fac2625SDavid S. Miller fi->fib_metrics[type - 1] = val; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds } 9594e902c57SThomas Graf } 9601da177e4SLinus Torvalds 9614e902c57SThomas Graf if (cfg->fc_mp) { 9621da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 9634e902c57SThomas Graf err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg); 9644e902c57SThomas Graf if (err != 0) 9651da177e4SLinus Torvalds goto failure; 9664e902c57SThomas Graf if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif) 9671da177e4SLinus Torvalds goto err_inval; 9684e902c57SThomas Graf if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw) 9691da177e4SLinus Torvalds goto err_inval; 970c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 9714e902c57SThomas Graf if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) 9721da177e4SLinus Torvalds goto err_inval; 9731da177e4SLinus Torvalds #endif 9741da177e4SLinus Torvalds #else 9751da177e4SLinus Torvalds goto err_inval; 9761da177e4SLinus Torvalds #endif 9771da177e4SLinus Torvalds } else { 9781da177e4SLinus Torvalds struct fib_nh *nh = fi->fib_nh; 9794e902c57SThomas Graf 980571e7226SRoopa Prabhu if (cfg->fc_encap) { 981571e7226SRoopa Prabhu struct lwtunnel_state *lwtstate; 982571e7226SRoopa Prabhu struct net_device *dev = NULL; 983571e7226SRoopa Prabhu 984571e7226SRoopa Prabhu if (cfg->fc_encap_type == LWTUNNEL_ENCAP_NONE) 985571e7226SRoopa Prabhu goto err_inval; 986571e7226SRoopa Prabhu if (cfg->fc_oif) 987571e7226SRoopa Prabhu dev = __dev_get_by_index(net, cfg->fc_oif); 988571e7226SRoopa Prabhu err = lwtunnel_build_state(dev, cfg->fc_encap_type, 989571e7226SRoopa Prabhu cfg->fc_encap, &lwtstate); 990571e7226SRoopa Prabhu if (err) 991571e7226SRoopa Prabhu goto failure; 992571e7226SRoopa Prabhu 9935a6228a0SNicolas Dichtel nh->nh_lwtstate = lwtstate_get(lwtstate); 994571e7226SRoopa Prabhu } 9954e902c57SThomas Graf nh->nh_oif = cfg->fc_oif; 9964e902c57SThomas Graf nh->nh_gw = cfg->fc_gw; 9974e902c57SThomas Graf nh->nh_flags = cfg->fc_flags; 998c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 9994e902c57SThomas Graf nh->nh_tclassid = cfg->fc_flow; 10007a9bc9b8SDavid S. Miller if (nh->nh_tclassid) 1001f4530fa5SDavid S. Miller fi->fib_net->ipv4.fib_num_tclassid_users++; 10021da177e4SLinus Torvalds #endif 10031da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 10041da177e4SLinus Torvalds nh->nh_weight = 1; 10051da177e4SLinus Torvalds #endif 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds 10084e902c57SThomas Graf if (fib_props[cfg->fc_type].error) { 10094e902c57SThomas Graf if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) 10101da177e4SLinus Torvalds goto err_inval; 10111da177e4SLinus Torvalds goto link_it; 10124c8237cdSDavid S. Miller } else { 10134c8237cdSDavid S. Miller switch (cfg->fc_type) { 10144c8237cdSDavid S. Miller case RTN_UNICAST: 10154c8237cdSDavid S. Miller case RTN_LOCAL: 10164c8237cdSDavid S. Miller case RTN_BROADCAST: 10174c8237cdSDavid S. Miller case RTN_ANYCAST: 10184c8237cdSDavid S. Miller case RTN_MULTICAST: 10194c8237cdSDavid S. Miller break; 10204c8237cdSDavid S. Miller default: 10214c8237cdSDavid S. Miller goto err_inval; 10224c8237cdSDavid S. Miller } 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds 10254e902c57SThomas Graf if (cfg->fc_scope > RT_SCOPE_HOST) 10261da177e4SLinus Torvalds goto err_inval; 10271da177e4SLinus Torvalds 10284e902c57SThomas Graf if (cfg->fc_scope == RT_SCOPE_HOST) { 10291da177e4SLinus Torvalds struct fib_nh *nh = fi->fib_nh; 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds /* Local address is added. */ 10321da177e4SLinus Torvalds if (nhs != 1 || nh->nh_gw) 10331da177e4SLinus Torvalds goto err_inval; 10341da177e4SLinus Torvalds nh->nh_scope = RT_SCOPE_NOWHERE; 10357462bd74SDenis V. Lunev nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif); 10361da177e4SLinus Torvalds err = -ENODEV; 103751456b29SIan Morris if (!nh->nh_dev) 10381da177e4SLinus Torvalds goto failure; 10391da177e4SLinus Torvalds } else { 10408a3d0316SAndy Gospodarek int linkdown = 0; 10418a3d0316SAndy Gospodarek 10421da177e4SLinus Torvalds change_nexthops(fi) { 10436a31d2a9SEric Dumazet err = fib_check_nh(cfg, fi, nexthop_nh); 10446a31d2a9SEric Dumazet if (err != 0) 10451da177e4SLinus Torvalds goto failure; 10468a3d0316SAndy Gospodarek if (nexthop_nh->nh_flags & RTNH_F_LINKDOWN) 10478a3d0316SAndy Gospodarek linkdown++; 10481da177e4SLinus Torvalds } endfor_nexthops(fi) 10498a3d0316SAndy Gospodarek if (linkdown == fi->fib_nhs) 10508a3d0316SAndy Gospodarek fi->fib_flags |= RTNH_F_LINKDOWN; 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds 1053*021dd3b8SDavid Ahern if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) 10541da177e4SLinus Torvalds goto err_inval; 10551da177e4SLinus Torvalds 10561fc050a1SDavid S. Miller change_nexthops(fi) { 1057436c3b66SDavid S. Miller fib_info_update_nh_saddr(net, nexthop_nh); 10581fc050a1SDavid S. Miller } endfor_nexthops(fi) 10591fc050a1SDavid S. Miller 10601da177e4SLinus Torvalds link_it: 10616a31d2a9SEric Dumazet ofi = fib_find_info(fi); 10626a31d2a9SEric Dumazet if (ofi) { 10631da177e4SLinus Torvalds fi->fib_dead = 1; 10641da177e4SLinus Torvalds free_fib_info(fi); 10651da177e4SLinus Torvalds ofi->fib_treeref++; 10661da177e4SLinus Torvalds return ofi; 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds fi->fib_treeref++; 10701da177e4SLinus Torvalds atomic_inc(&fi->fib_clntref); 1071832b4c5eSStephen Hemminger spin_lock_bh(&fib_info_lock); 10721da177e4SLinus Torvalds hlist_add_head(&fi->fib_hash, 10731da177e4SLinus Torvalds &fib_info_hash[fib_info_hashfn(fi)]); 10741da177e4SLinus Torvalds if (fi->fib_prefsrc) { 10751da177e4SLinus Torvalds struct hlist_head *head; 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)]; 10781da177e4SLinus Torvalds hlist_add_head(&fi->fib_lhash, head); 10791da177e4SLinus Torvalds } 10801da177e4SLinus Torvalds change_nexthops(fi) { 10811da177e4SLinus Torvalds struct hlist_head *head; 10821da177e4SLinus Torvalds unsigned int hash; 10831da177e4SLinus Torvalds 108471fceff0SDavid S. Miller if (!nexthop_nh->nh_dev) 10851da177e4SLinus Torvalds continue; 108671fceff0SDavid S. Miller hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex); 10871da177e4SLinus Torvalds head = &fib_info_devhash[hash]; 108871fceff0SDavid S. Miller hlist_add_head(&nexthop_nh->nh_hash, head); 10891da177e4SLinus Torvalds } endfor_nexthops(fi) 1090832b4c5eSStephen Hemminger spin_unlock_bh(&fib_info_lock); 10911da177e4SLinus Torvalds return fi; 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds err_inval: 10941da177e4SLinus Torvalds err = -EINVAL; 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds failure: 10971da177e4SLinus Torvalds if (fi) { 10981da177e4SLinus Torvalds fi->fib_dead = 1; 10991da177e4SLinus Torvalds free_fib_info(fi); 11001da177e4SLinus Torvalds } 11014e902c57SThomas Graf 11024e902c57SThomas Graf return ERR_PTR(err); 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 110515e47304SEric W. Biederman int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event, 110637e826c5SDavid S. Miller u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos, 1107b6544c0bSJamal Hadi Salim struct fib_info *fi, unsigned int flags) 11081da177e4SLinus Torvalds { 11091da177e4SLinus Torvalds struct nlmsghdr *nlh; 1110be403ea1SThomas Graf struct rtmsg *rtm; 11111da177e4SLinus Torvalds 111215e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags); 111351456b29SIan Morris if (!nlh) 111426932566SPatrick McHardy return -EMSGSIZE; 1115be403ea1SThomas Graf 1116be403ea1SThomas Graf rtm = nlmsg_data(nlh); 11171da177e4SLinus Torvalds rtm->rtm_family = AF_INET; 11181da177e4SLinus Torvalds rtm->rtm_dst_len = dst_len; 11191da177e4SLinus Torvalds rtm->rtm_src_len = 0; 11201da177e4SLinus Torvalds rtm->rtm_tos = tos; 1121709772e6SKrzysztof Piotr Oledzki if (tb_id < 256) 11221da177e4SLinus Torvalds rtm->rtm_table = tb_id; 1123709772e6SKrzysztof Piotr Oledzki else 1124709772e6SKrzysztof Piotr Oledzki rtm->rtm_table = RT_TABLE_COMPAT; 1125f3756b79SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, tb_id)) 1126f3756b79SDavid S. Miller goto nla_put_failure; 11271da177e4SLinus Torvalds rtm->rtm_type = type; 11281da177e4SLinus Torvalds rtm->rtm_flags = fi->fib_flags; 112937e826c5SDavid S. Miller rtm->rtm_scope = fi->fib_scope; 11301da177e4SLinus Torvalds rtm->rtm_protocol = fi->fib_protocol; 1131be403ea1SThomas Graf 1132f3756b79SDavid S. Miller if (rtm->rtm_dst_len && 1133930345eaSJiri Benc nla_put_in_addr(skb, RTA_DST, dst)) 1134f3756b79SDavid S. Miller goto nla_put_failure; 1135f3756b79SDavid S. Miller if (fi->fib_priority && 1136f3756b79SDavid S. Miller nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority)) 1137f3756b79SDavid S. Miller goto nla_put_failure; 11381da177e4SLinus Torvalds if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0) 1139be403ea1SThomas Graf goto nla_put_failure; 1140be403ea1SThomas Graf 1141f3756b79SDavid S. Miller if (fi->fib_prefsrc && 1142930345eaSJiri Benc nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc)) 1143f3756b79SDavid S. Miller goto nla_put_failure; 11441da177e4SLinus Torvalds if (fi->fib_nhs == 1) { 11450eeb075fSAndy Gospodarek struct in_device *in_dev; 11460eeb075fSAndy Gospodarek 1147f3756b79SDavid S. Miller if (fi->fib_nh->nh_gw && 1148930345eaSJiri Benc nla_put_in_addr(skb, RTA_GATEWAY, fi->fib_nh->nh_gw)) 1149f3756b79SDavid S. Miller goto nla_put_failure; 1150f3756b79SDavid S. Miller if (fi->fib_nh->nh_oif && 1151f3756b79SDavid S. Miller nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif)) 1152f3756b79SDavid S. Miller goto nla_put_failure; 11530eeb075fSAndy Gospodarek if (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN) { 115496ac5cc9SAndy Gospodarek in_dev = __in_dev_get_rtnl(fi->fib_nh->nh_dev); 11550eeb075fSAndy Gospodarek if (in_dev && 11560eeb075fSAndy Gospodarek IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev)) 11570eeb075fSAndy Gospodarek rtm->rtm_flags |= RTNH_F_DEAD; 11580eeb075fSAndy Gospodarek } 1159c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 1160f3756b79SDavid S. Miller if (fi->fib_nh[0].nh_tclassid && 1161f3756b79SDavid S. Miller nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid)) 1162f3756b79SDavid S. Miller goto nla_put_failure; 11638265abc0SPatrick McHardy #endif 1164571e7226SRoopa Prabhu if (fi->fib_nh->nh_lwtstate) 1165571e7226SRoopa Prabhu lwtunnel_fill_encap(skb, fi->fib_nh->nh_lwtstate); 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 11681da177e4SLinus Torvalds if (fi->fib_nhs > 1) { 1169be403ea1SThomas Graf struct rtnexthop *rtnh; 1170be403ea1SThomas Graf struct nlattr *mp; 1171be403ea1SThomas Graf 1172be403ea1SThomas Graf mp = nla_nest_start(skb, RTA_MULTIPATH); 117351456b29SIan Morris if (!mp) 1174be403ea1SThomas Graf goto nla_put_failure; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds for_nexthops(fi) { 11770eeb075fSAndy Gospodarek struct in_device *in_dev; 11780eeb075fSAndy Gospodarek 1179be403ea1SThomas Graf rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 118051456b29SIan Morris if (!rtnh) 1181be403ea1SThomas Graf goto nla_put_failure; 1182be403ea1SThomas Graf 1183be403ea1SThomas Graf rtnh->rtnh_flags = nh->nh_flags & 0xFF; 11840eeb075fSAndy Gospodarek if (nh->nh_flags & RTNH_F_LINKDOWN) { 118596ac5cc9SAndy Gospodarek in_dev = __in_dev_get_rtnl(nh->nh_dev); 11860eeb075fSAndy Gospodarek if (in_dev && 11870eeb075fSAndy Gospodarek IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev)) 11880eeb075fSAndy Gospodarek rtnh->rtnh_flags |= RTNH_F_DEAD; 11890eeb075fSAndy Gospodarek } 1190be403ea1SThomas Graf rtnh->rtnh_hops = nh->nh_weight - 1; 1191be403ea1SThomas Graf rtnh->rtnh_ifindex = nh->nh_oif; 1192be403ea1SThomas Graf 1193f3756b79SDavid S. Miller if (nh->nh_gw && 1194930345eaSJiri Benc nla_put_in_addr(skb, RTA_GATEWAY, nh->nh_gw)) 1195f3756b79SDavid S. Miller goto nla_put_failure; 1196c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 1197f3756b79SDavid S. Miller if (nh->nh_tclassid && 1198f3756b79SDavid S. Miller nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid)) 1199f3756b79SDavid S. Miller goto nla_put_failure; 12008265abc0SPatrick McHardy #endif 1201571e7226SRoopa Prabhu if (nh->nh_lwtstate) 1202571e7226SRoopa Prabhu lwtunnel_fill_encap(skb, nh->nh_lwtstate); 1203be403ea1SThomas Graf /* length of rtnetlink header + attributes */ 1204be403ea1SThomas Graf rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh; 12051da177e4SLinus Torvalds } endfor_nexthops(fi); 1206be403ea1SThomas Graf 1207be403ea1SThomas Graf nla_nest_end(skb, mp); 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds #endif 1210053c095aSJohannes Berg nlmsg_end(skb, nlh); 1211053c095aSJohannes Berg return 0; 12121da177e4SLinus Torvalds 1213be403ea1SThomas Graf nla_put_failure: 121426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 121526932566SPatrick McHardy return -EMSGSIZE; 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds /* 12196a31d2a9SEric Dumazet * Update FIB if: 12206a31d2a9SEric Dumazet * - local address disappeared -> we must delete all the entries 12216a31d2a9SEric Dumazet * referring to it. 12226a31d2a9SEric Dumazet * - device went down -> we must shutdown all nexthops going via it. 12231da177e4SLinus Torvalds */ 12244814bdbdSDenis V. Lunev int fib_sync_down_addr(struct net *net, __be32 local) 12251da177e4SLinus Torvalds { 12261da177e4SLinus Torvalds int ret = 0; 12271da177e4SLinus Torvalds unsigned int hash = fib_laddr_hashfn(local); 12281da177e4SLinus Torvalds struct hlist_head *head = &fib_info_laddrhash[hash]; 12291da177e4SLinus Torvalds struct fib_info *fi; 12301da177e4SLinus Torvalds 123151456b29SIan Morris if (!fib_info_laddrhash || local == 0) 123285326fa5SDenis V. Lunev return 0; 123385326fa5SDenis V. Lunev 1234b67bfe0dSSasha Levin hlist_for_each_entry(fi, head, fib_lhash) { 123509ad9bc7SOctavian Purdila if (!net_eq(fi->fib_net, net)) 12364814bdbdSDenis V. Lunev continue; 12371da177e4SLinus Torvalds if (fi->fib_prefsrc == local) { 12381da177e4SLinus Torvalds fi->fib_flags |= RTNH_F_DEAD; 12391da177e4SLinus Torvalds ret++; 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds } 124285326fa5SDenis V. Lunev return ret; 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds 12458a3d0316SAndy Gospodarek int fib_sync_down_dev(struct net_device *dev, unsigned long event) 124685326fa5SDenis V. Lunev { 124785326fa5SDenis V. Lunev int ret = 0; 124885326fa5SDenis V. Lunev int scope = RT_SCOPE_NOWHERE; 12491da177e4SLinus Torvalds struct fib_info *prev_fi = NULL; 12501da177e4SLinus Torvalds unsigned int hash = fib_devindex_hashfn(dev->ifindex); 12511da177e4SLinus Torvalds struct hlist_head *head = &fib_info_devhash[hash]; 12521da177e4SLinus Torvalds struct fib_nh *nh; 12531da177e4SLinus Torvalds 12548a3d0316SAndy Gospodarek if (event == NETDEV_UNREGISTER || 12558a3d0316SAndy Gospodarek event == NETDEV_DOWN) 125685326fa5SDenis V. Lunev scope = -1; 125785326fa5SDenis V. Lunev 1258b67bfe0dSSasha Levin hlist_for_each_entry(nh, head, nh_hash) { 12591da177e4SLinus Torvalds struct fib_info *fi = nh->nh_parent; 12601da177e4SLinus Torvalds int dead; 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds BUG_ON(!fi->fib_nhs); 12631da177e4SLinus Torvalds if (nh->nh_dev != dev || fi == prev_fi) 12641da177e4SLinus Torvalds continue; 12651da177e4SLinus Torvalds prev_fi = fi; 12661da177e4SLinus Torvalds dead = 0; 12671da177e4SLinus Torvalds change_nexthops(fi) { 126871fceff0SDavid S. Miller if (nexthop_nh->nh_flags & RTNH_F_DEAD) 12691da177e4SLinus Torvalds dead++; 127071fceff0SDavid S. Miller else if (nexthop_nh->nh_dev == dev && 127171fceff0SDavid S. Miller nexthop_nh->nh_scope != scope) { 12728a3d0316SAndy Gospodarek switch (event) { 12738a3d0316SAndy Gospodarek case NETDEV_DOWN: 12748a3d0316SAndy Gospodarek case NETDEV_UNREGISTER: 127571fceff0SDavid S. Miller nexthop_nh->nh_flags |= RTNH_F_DEAD; 12768a3d0316SAndy Gospodarek /* fall through */ 12778a3d0316SAndy Gospodarek case NETDEV_CHANGE: 12788a3d0316SAndy Gospodarek nexthop_nh->nh_flags |= RTNH_F_LINKDOWN; 12798a3d0316SAndy Gospodarek break; 12808a3d0316SAndy Gospodarek } 12811da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 12821da177e4SLinus Torvalds spin_lock_bh(&fib_multipath_lock); 128371fceff0SDavid S. Miller fi->fib_power -= nexthop_nh->nh_power; 128471fceff0SDavid S. Miller nexthop_nh->nh_power = 0; 12851da177e4SLinus Torvalds spin_unlock_bh(&fib_multipath_lock); 12861da177e4SLinus Torvalds #endif 12871da177e4SLinus Torvalds dead++; 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 12908a3d0316SAndy Gospodarek if (event == NETDEV_UNREGISTER && 12918a3d0316SAndy Gospodarek nexthop_nh->nh_dev == dev) { 12921da177e4SLinus Torvalds dead = fi->fib_nhs; 12931da177e4SLinus Torvalds break; 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds #endif 12961da177e4SLinus Torvalds } endfor_nexthops(fi) 12971da177e4SLinus Torvalds if (dead == fi->fib_nhs) { 12988a3d0316SAndy Gospodarek switch (event) { 12998a3d0316SAndy Gospodarek case NETDEV_DOWN: 13008a3d0316SAndy Gospodarek case NETDEV_UNREGISTER: 13011da177e4SLinus Torvalds fi->fib_flags |= RTNH_F_DEAD; 13028a3d0316SAndy Gospodarek /* fall through */ 13038a3d0316SAndy Gospodarek case NETDEV_CHANGE: 13048a3d0316SAndy Gospodarek fi->fib_flags |= RTNH_F_LINKDOWN; 13058a3d0316SAndy Gospodarek break; 13068a3d0316SAndy Gospodarek } 13071da177e4SLinus Torvalds ret++; 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds return ret; 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 13140c838ff1SDavid S. Miller /* Must be invoked inside of an RCU protected region. */ 13152392debcSJulian Anastasov void fib_select_default(const struct flowi4 *flp, struct fib_result *res) 13160c838ff1SDavid S. Miller { 13170c838ff1SDavid S. Miller struct fib_info *fi = NULL, *last_resort = NULL; 131856315f9eSAlexander Duyck struct hlist_head *fa_head = res->fa_head; 13190c838ff1SDavid S. Miller struct fib_table *tb = res->table; 132018a912e9SJulian Anastasov u8 slen = 32 - res->prefixlen; 13210c838ff1SDavid S. Miller int order = -1, last_idx = -1; 13222392debcSJulian Anastasov struct fib_alias *fa, *fa1 = NULL; 13232392debcSJulian Anastasov u32 last_prio = res->fi->fib_priority; 13242392debcSJulian Anastasov u8 last_tos = 0; 13250c838ff1SDavid S. Miller 132656315f9eSAlexander Duyck hlist_for_each_entry_rcu(fa, fa_head, fa_list) { 13270c838ff1SDavid S. Miller struct fib_info *next_fi = fa->fa_info; 13280c838ff1SDavid S. Miller 132918a912e9SJulian Anastasov if (fa->fa_slen != slen) 133018a912e9SJulian Anastasov continue; 13312392debcSJulian Anastasov if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos) 13322392debcSJulian Anastasov continue; 133318a912e9SJulian Anastasov if (fa->tb_id != tb->tb_id) 133418a912e9SJulian Anastasov continue; 13352392debcSJulian Anastasov if (next_fi->fib_priority > last_prio && 13362392debcSJulian Anastasov fa->fa_tos == last_tos) { 13372392debcSJulian Anastasov if (last_tos) 13382392debcSJulian Anastasov continue; 13392392debcSJulian Anastasov break; 13402392debcSJulian Anastasov } 13412392debcSJulian Anastasov if (next_fi->fib_flags & RTNH_F_DEAD) 13422392debcSJulian Anastasov continue; 13432392debcSJulian Anastasov last_tos = fa->fa_tos; 13442392debcSJulian Anastasov last_prio = next_fi->fib_priority; 13452392debcSJulian Anastasov 134637e826c5SDavid S. Miller if (next_fi->fib_scope != res->scope || 13470c838ff1SDavid S. Miller fa->fa_type != RTN_UNICAST) 13480c838ff1SDavid S. Miller continue; 13490c838ff1SDavid S. Miller if (!next_fi->fib_nh[0].nh_gw || 13500c838ff1SDavid S. Miller next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK) 13510c838ff1SDavid S. Miller continue; 13520c838ff1SDavid S. Miller 13530c838ff1SDavid S. Miller fib_alias_accessed(fa); 13540c838ff1SDavid S. Miller 135551456b29SIan Morris if (!fi) { 13560c838ff1SDavid S. Miller if (next_fi != res->fi) 13570c838ff1SDavid S. Miller break; 13582392debcSJulian Anastasov fa1 = fa; 13590c838ff1SDavid S. Miller } else if (!fib_detect_death(fi, order, &last_resort, 13602392debcSJulian Anastasov &last_idx, fa1->fa_default)) { 13610c838ff1SDavid S. Miller fib_result_assign(res, fi); 13622392debcSJulian Anastasov fa1->fa_default = order; 13630c838ff1SDavid S. Miller goto out; 13640c838ff1SDavid S. Miller } 13650c838ff1SDavid S. Miller fi = next_fi; 13660c838ff1SDavid S. Miller order++; 13670c838ff1SDavid S. Miller } 13680c838ff1SDavid S. Miller 136951456b29SIan Morris if (order <= 0 || !fi) { 13702392debcSJulian Anastasov if (fa1) 13712392debcSJulian Anastasov fa1->fa_default = -1; 13720c838ff1SDavid S. Miller goto out; 13730c838ff1SDavid S. Miller } 13740c838ff1SDavid S. Miller 13750c838ff1SDavid S. Miller if (!fib_detect_death(fi, order, &last_resort, &last_idx, 13762392debcSJulian Anastasov fa1->fa_default)) { 13770c838ff1SDavid S. Miller fib_result_assign(res, fi); 13782392debcSJulian Anastasov fa1->fa_default = order; 13790c838ff1SDavid S. Miller goto out; 13800c838ff1SDavid S. Miller } 13810c838ff1SDavid S. Miller 13820c838ff1SDavid S. Miller if (last_idx >= 0) 13830c838ff1SDavid S. Miller fib_result_assign(res, last_resort); 13842392debcSJulian Anastasov fa1->fa_default = last_idx; 13850c838ff1SDavid S. Miller out: 138631d40937SEric Dumazet return; 13870c838ff1SDavid S. Miller } 13880c838ff1SDavid S. Miller 13891da177e4SLinus Torvalds /* 13906a31d2a9SEric Dumazet * Dead device goes up. We wake up dead nexthops. 13916a31d2a9SEric Dumazet * It takes sense only on multipath routes. 13921da177e4SLinus Torvalds */ 13938a3d0316SAndy Gospodarek int fib_sync_up(struct net_device *dev, unsigned int nh_flags) 13941da177e4SLinus Torvalds { 13951da177e4SLinus Torvalds struct fib_info *prev_fi; 13961da177e4SLinus Torvalds unsigned int hash; 13971da177e4SLinus Torvalds struct hlist_head *head; 13981da177e4SLinus Torvalds struct fib_nh *nh; 13991da177e4SLinus Torvalds int ret; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds if (!(dev->flags & IFF_UP)) 14021da177e4SLinus Torvalds return 0; 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds prev_fi = NULL; 14051da177e4SLinus Torvalds hash = fib_devindex_hashfn(dev->ifindex); 14061da177e4SLinus Torvalds head = &fib_info_devhash[hash]; 14071da177e4SLinus Torvalds ret = 0; 14081da177e4SLinus Torvalds 1409b67bfe0dSSasha Levin hlist_for_each_entry(nh, head, nh_hash) { 14101da177e4SLinus Torvalds struct fib_info *fi = nh->nh_parent; 14111da177e4SLinus Torvalds int alive; 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds BUG_ON(!fi->fib_nhs); 14141da177e4SLinus Torvalds if (nh->nh_dev != dev || fi == prev_fi) 14151da177e4SLinus Torvalds continue; 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds prev_fi = fi; 14181da177e4SLinus Torvalds alive = 0; 14191da177e4SLinus Torvalds change_nexthops(fi) { 14208a3d0316SAndy Gospodarek if (!(nexthop_nh->nh_flags & nh_flags)) { 14211da177e4SLinus Torvalds alive++; 14221da177e4SLinus Torvalds continue; 14231da177e4SLinus Torvalds } 142451456b29SIan Morris if (!nexthop_nh->nh_dev || 142571fceff0SDavid S. Miller !(nexthop_nh->nh_dev->flags & IFF_UP)) 14261da177e4SLinus Torvalds continue; 142771fceff0SDavid S. Miller if (nexthop_nh->nh_dev != dev || 142871fceff0SDavid S. Miller !__in_dev_get_rtnl(dev)) 14291da177e4SLinus Torvalds continue; 14301da177e4SLinus Torvalds alive++; 14318a3d0316SAndy Gospodarek #ifdef CONFIG_IP_ROUTE_MULTIPATH 14321da177e4SLinus Torvalds spin_lock_bh(&fib_multipath_lock); 143371fceff0SDavid S. Miller nexthop_nh->nh_power = 0; 14348a3d0316SAndy Gospodarek nexthop_nh->nh_flags &= ~nh_flags; 14351da177e4SLinus Torvalds spin_unlock_bh(&fib_multipath_lock); 14368a3d0316SAndy Gospodarek #else 14378a3d0316SAndy Gospodarek nexthop_nh->nh_flags &= ~nh_flags; 14388a3d0316SAndy Gospodarek #endif 14391da177e4SLinus Torvalds } endfor_nexthops(fi) 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds if (alive > 0) { 14428a3d0316SAndy Gospodarek fi->fib_flags &= ~nh_flags; 14431da177e4SLinus Torvalds ret++; 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds } 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds return ret; 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds 14508a3d0316SAndy Gospodarek #ifdef CONFIG_IP_ROUTE_MULTIPATH 14518a3d0316SAndy Gospodarek 14521da177e4SLinus Torvalds /* 14536a31d2a9SEric Dumazet * The algorithm is suboptimal, but it provides really 14546a31d2a9SEric Dumazet * fair weighted route distribution. 14551da177e4SLinus Torvalds */ 14561b7fe593SDavid S. Miller void fib_select_multipath(struct fib_result *res) 14571da177e4SLinus Torvalds { 14581da177e4SLinus Torvalds struct fib_info *fi = res->fi; 14590eeb075fSAndy Gospodarek struct in_device *in_dev; 14601da177e4SLinus Torvalds int w; 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds spin_lock_bh(&fib_multipath_lock); 14631da177e4SLinus Torvalds if (fi->fib_power <= 0) { 14641da177e4SLinus Torvalds int power = 0; 14651da177e4SLinus Torvalds change_nexthops(fi) { 14660eeb075fSAndy Gospodarek in_dev = __in_dev_get_rcu(nexthop_nh->nh_dev); 14670eeb075fSAndy Gospodarek if (nexthop_nh->nh_flags & RTNH_F_DEAD) 14680eeb075fSAndy Gospodarek continue; 14690eeb075fSAndy Gospodarek if (in_dev && 14700eeb075fSAndy Gospodarek IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) && 14710eeb075fSAndy Gospodarek nexthop_nh->nh_flags & RTNH_F_LINKDOWN) 14720eeb075fSAndy Gospodarek continue; 147371fceff0SDavid S. Miller power += nexthop_nh->nh_weight; 147471fceff0SDavid S. Miller nexthop_nh->nh_power = nexthop_nh->nh_weight; 14751da177e4SLinus Torvalds } endfor_nexthops(fi); 14761da177e4SLinus Torvalds fi->fib_power = power; 14771da177e4SLinus Torvalds if (power <= 0) { 14781da177e4SLinus Torvalds spin_unlock_bh(&fib_multipath_lock); 14791da177e4SLinus Torvalds /* Race condition: route has just become dead. */ 14801da177e4SLinus Torvalds res->nh_sel = 0; 14811da177e4SLinus Torvalds return; 14821da177e4SLinus Torvalds } 14831da177e4SLinus Torvalds } 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds /* w should be random number [0..fi->fib_power-1], 14876a31d2a9SEric Dumazet * it is pretty bad approximation. 14881da177e4SLinus Torvalds */ 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds w = jiffies % fi->fib_power; 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds change_nexthops(fi) { 149371fceff0SDavid S. Miller if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) && 149471fceff0SDavid S. Miller nexthop_nh->nh_power) { 14956a31d2a9SEric Dumazet w -= nexthop_nh->nh_power; 14966a31d2a9SEric Dumazet if (w <= 0) { 149771fceff0SDavid S. Miller nexthop_nh->nh_power--; 14981da177e4SLinus Torvalds fi->fib_power--; 14991da177e4SLinus Torvalds res->nh_sel = nhsel; 15001da177e4SLinus Torvalds spin_unlock_bh(&fib_multipath_lock); 15011da177e4SLinus Torvalds return; 15021da177e4SLinus Torvalds } 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds } endfor_nexthops(fi); 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds /* Race condition: route has just become dead. */ 15071da177e4SLinus Torvalds res->nh_sel = 0; 15081da177e4SLinus Torvalds spin_unlock_bh(&fib_multipath_lock); 15091da177e4SLinus Torvalds } 15101da177e4SLinus Torvalds #endif 1511