Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 3 | * operating system. INET is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * IPv4 Forwarding Information Base: semantics. |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * as published by the Free Software Foundation; either version |
| 13 | * 2 of the License, or (at your option) any later version. |
| 14 | */ |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/uaccess.h> |
| 17 | #include <asm/system.h> |
| 18 | #include <linux/bitops.h> |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/jiffies.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/socket.h> |
| 25 | #include <linux/sockios.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/in.h> |
| 28 | #include <linux/inet.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 29 | #include <linux/inetdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/netdevice.h> |
| 31 | #include <linux/if_arp.h> |
| 32 | #include <linux/proc_fs.h> |
| 33 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 37 | #include <net/arp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <net/ip.h> |
| 39 | #include <net/protocol.h> |
| 40 | #include <net/route.h> |
| 41 | #include <net/tcp.h> |
| 42 | #include <net/sock.h> |
| 43 | #include <net/ip_fib.h> |
Thomas Graf | f21c7bc | 2006-08-15 00:34:17 -0700 | [diff] [blame] | 44 | #include <net/netlink.h> |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 45 | #include <net/nexthop.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | #include "fib_lookup.h" |
| 48 | |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 49 | static DEFINE_SPINLOCK(fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | static struct hlist_head *fib_info_hash; |
| 51 | static struct hlist_head *fib_info_laddrhash; |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 52 | static unsigned int fib_info_hash_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | static unsigned int fib_info_cnt; |
| 54 | |
| 55 | #define DEVINDEX_HASHBITS 8 |
| 56 | #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS) |
| 57 | static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE]; |
| 58 | |
| 59 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
| 60 | |
| 61 | static DEFINE_SPINLOCK(fib_multipath_lock); |
| 62 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 63 | #define for_nexthops(fi) { \ |
| 64 | int nhsel; const struct fib_nh *nh; \ |
| 65 | for (nhsel = 0, nh = (fi)->fib_nh; \ |
| 66 | nhsel < (fi)->fib_nhs; \ |
| 67 | nh++, nhsel++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 69 | #define change_nexthops(fi) { \ |
| 70 | int nhsel; struct fib_nh *nexthop_nh; \ |
| 71 | for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \ |
| 72 | nhsel < (fi)->fib_nhs; \ |
| 73 | nexthop_nh++, nhsel++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | #else /* CONFIG_IP_ROUTE_MULTIPATH */ |
| 76 | |
| 77 | /* Hope, that gcc will optimize it to get rid of dummy loop */ |
| 78 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 79 | #define for_nexthops(fi) { \ |
| 80 | int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \ |
| 81 | for (nhsel = 0; nhsel < 1; nhsel++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 83 | #define change_nexthops(fi) { \ |
| 84 | int nhsel; \ |
| 85 | struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \ |
| 86 | for (nhsel = 0; nhsel < 1; nhsel++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | #endif /* CONFIG_IP_ROUTE_MULTIPATH */ |
| 89 | |
| 90 | #define endfor_nexthops(fi) } |
| 91 | |
| 92 | |
David S. Miller | 3be0686 | 2011-03-07 15:01:10 -0800 | [diff] [blame] | 93 | const struct fib_prop fib_props[RTN_MAX + 1] = { |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 94 | [RTN_UNSPEC] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | .error = 0, |
| 96 | .scope = RT_SCOPE_NOWHERE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 97 | }, |
| 98 | [RTN_UNICAST] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | .error = 0, |
| 100 | .scope = RT_SCOPE_UNIVERSE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 101 | }, |
| 102 | [RTN_LOCAL] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | .error = 0, |
| 104 | .scope = RT_SCOPE_HOST, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 105 | }, |
| 106 | [RTN_BROADCAST] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | .error = 0, |
| 108 | .scope = RT_SCOPE_LINK, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 109 | }, |
| 110 | [RTN_ANYCAST] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | .error = 0, |
| 112 | .scope = RT_SCOPE_LINK, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 113 | }, |
| 114 | [RTN_MULTICAST] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | .error = 0, |
| 116 | .scope = RT_SCOPE_UNIVERSE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 117 | }, |
| 118 | [RTN_BLACKHOLE] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | .error = -EINVAL, |
| 120 | .scope = RT_SCOPE_UNIVERSE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 121 | }, |
| 122 | [RTN_UNREACHABLE] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | .error = -EHOSTUNREACH, |
| 124 | .scope = RT_SCOPE_UNIVERSE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 125 | }, |
| 126 | [RTN_PROHIBIT] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | .error = -EACCES, |
| 128 | .scope = RT_SCOPE_UNIVERSE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 129 | }, |
| 130 | [RTN_THROW] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | .error = -EAGAIN, |
| 132 | .scope = RT_SCOPE_UNIVERSE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 133 | }, |
| 134 | [RTN_NAT] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | .error = -EINVAL, |
| 136 | .scope = RT_SCOPE_NOWHERE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 137 | }, |
| 138 | [RTN_XRESOLVE] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | .error = -EINVAL, |
| 140 | .scope = RT_SCOPE_NOWHERE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 141 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | /* Release a nexthop info record */ |
| 145 | |
| 146 | void free_fib_info(struct fib_info *fi) |
| 147 | { |
| 148 | if (fi->fib_dead == 0) { |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 149 | pr_warning("Freeing alive fib_info %p\n", fi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | change_nexthops(fi) { |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 153 | if (nexthop_nh->nh_dev) |
| 154 | dev_put(nexthop_nh->nh_dev); |
| 155 | nexthop_nh->nh_dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } endfor_nexthops(fi); |
| 157 | fib_info_cnt--; |
Denis V. Lunev | 57d7a60 | 2008-04-16 02:00:50 -0700 | [diff] [blame] | 158 | release_net(fi->fib_net); |
Lai Jiangshan | 4670994 | 2011-03-18 11:42:11 +0800 | [diff] [blame] | 159 | kfree_rcu(fi, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void fib_release_info(struct fib_info *fi) |
| 163 | { |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 164 | spin_lock_bh(&fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | if (fi && --fi->fib_treeref == 0) { |
| 166 | hlist_del(&fi->fib_hash); |
| 167 | if (fi->fib_prefsrc) |
| 168 | hlist_del(&fi->fib_lhash); |
| 169 | change_nexthops(fi) { |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 170 | if (!nexthop_nh->nh_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | continue; |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 172 | hlist_del(&nexthop_nh->nh_hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } endfor_nexthops(fi) |
| 174 | fi->fib_dead = 1; |
| 175 | fib_info_put(fi); |
| 176 | } |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 177 | spin_unlock_bh(&fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 180 | static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
| 182 | const struct fib_nh *onh = ofi->fib_nh; |
| 183 | |
| 184 | for_nexthops(fi) { |
| 185 | if (nh->nh_oif != onh->nh_oif || |
| 186 | nh->nh_gw != onh->nh_gw || |
| 187 | nh->nh_scope != onh->nh_scope || |
| 188 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
| 189 | nh->nh_weight != onh->nh_weight || |
| 190 | #endif |
Patrick McHardy | c7066f7 | 2011-01-14 13:36:42 +0100 | [diff] [blame] | 191 | #ifdef CONFIG_IP_ROUTE_CLASSID |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | nh->nh_tclassid != onh->nh_tclassid || |
| 193 | #endif |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 194 | ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | return -1; |
| 196 | onh++; |
| 197 | } endfor_nexthops(fi); |
| 198 | return 0; |
| 199 | } |
| 200 | |
David S. Miller | 88ebc72 | 2008-01-12 21:49:01 -0800 | [diff] [blame] | 201 | static inline unsigned int fib_devindex_hashfn(unsigned int val) |
| 202 | { |
| 203 | unsigned int mask = DEVINDEX_HASHSIZE - 1; |
| 204 | |
| 205 | return (val ^ |
| 206 | (val >> DEVINDEX_HASHBITS) ^ |
| 207 | (val >> (DEVINDEX_HASHBITS * 2))) & mask; |
| 208 | } |
| 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | static inline unsigned int fib_info_hashfn(const struct fib_info *fi) |
| 211 | { |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 212 | unsigned int mask = (fib_info_hash_size - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | unsigned int val = fi->fib_nhs; |
| 214 | |
David S. Miller | 37e826c | 2011-03-24 18:06:47 -0700 | [diff] [blame] | 215 | val ^= (fi->fib_protocol << 8) | fi->fib_scope; |
Al Viro | 81f7bf6 | 2006-09-27 18:40:00 -0700 | [diff] [blame] | 216 | val ^= (__force u32)fi->fib_prefsrc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | val ^= fi->fib_priority; |
David S. Miller | 88ebc72 | 2008-01-12 21:49:01 -0800 | [diff] [blame] | 218 | for_nexthops(fi) { |
| 219 | val ^= fib_devindex_hashfn(nh->nh_oif); |
| 220 | } endfor_nexthops(fi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
| 222 | return (val ^ (val >> 7) ^ (val >> 12)) & mask; |
| 223 | } |
| 224 | |
| 225 | static struct fib_info *fib_find_info(const struct fib_info *nfi) |
| 226 | { |
| 227 | struct hlist_head *head; |
| 228 | struct hlist_node *node; |
| 229 | struct fib_info *fi; |
| 230 | unsigned int hash; |
| 231 | |
| 232 | hash = fib_info_hashfn(nfi); |
| 233 | head = &fib_info_hash[hash]; |
| 234 | |
| 235 | hlist_for_each_entry(fi, node, head, fib_hash) { |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 236 | if (!net_eq(fi->fib_net, nfi->fib_net)) |
Denis V. Lunev | 4814bdb | 2008-01-31 18:50:07 -0800 | [diff] [blame] | 237 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | if (fi->fib_nhs != nfi->fib_nhs) |
| 239 | continue; |
| 240 | if (nfi->fib_protocol == fi->fib_protocol && |
David S. Miller | 37e826c | 2011-03-24 18:06:47 -0700 | [diff] [blame] | 241 | nfi->fib_scope == fi->fib_scope && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | nfi->fib_prefsrc == fi->fib_prefsrc && |
| 243 | nfi->fib_priority == fi->fib_priority && |
| 244 | memcmp(nfi->fib_metrics, fi->fib_metrics, |
Eric Dumazet | fcd13f4 | 2011-03-24 07:01:24 +0000 | [diff] [blame] | 245 | sizeof(u32) * RTAX_MAX) == 0 && |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 246 | ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0)) |
| 248 | return fi; |
| 249 | } |
| 250 | |
| 251 | return NULL; |
| 252 | } |
| 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | /* Check, that the gateway is already configured. |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 255 | * Used only by redirect accept routine. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | */ |
Al Viro | d878e72e | 2006-09-26 22:18:13 -0700 | [diff] [blame] | 257 | int ip_fib_check_default(__be32 gw, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
| 259 | struct hlist_head *head; |
| 260 | struct hlist_node *node; |
| 261 | struct fib_nh *nh; |
| 262 | unsigned int hash; |
| 263 | |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 264 | spin_lock(&fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
| 266 | hash = fib_devindex_hashfn(dev->ifindex); |
| 267 | head = &fib_info_devhash[hash]; |
| 268 | hlist_for_each_entry(nh, node, head, nh_hash) { |
| 269 | if (nh->nh_dev == dev && |
| 270 | nh->nh_gw == gw && |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 271 | !(nh->nh_flags & RTNH_F_DEAD)) { |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 272 | spin_unlock(&fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | } |
| 276 | |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 277 | spin_unlock(&fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
| 279 | return -1; |
| 280 | } |
| 281 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 282 | static inline size_t fib_nlmsg_size(struct fib_info *fi) |
| 283 | { |
| 284 | size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg)) |
| 285 | + nla_total_size(4) /* RTA_TABLE */ |
| 286 | + nla_total_size(4) /* RTA_DST */ |
| 287 | + nla_total_size(4) /* RTA_PRIORITY */ |
| 288 | + nla_total_size(4); /* RTA_PREFSRC */ |
| 289 | |
| 290 | /* space for nested metrics */ |
| 291 | payload += nla_total_size((RTAX_MAX * nla_total_size(4))); |
| 292 | |
| 293 | if (fi->fib_nhs) { |
| 294 | /* Also handles the special case fib_nhs == 1 */ |
| 295 | |
| 296 | /* each nexthop is packed in an attribute */ |
| 297 | size_t nhsize = nla_total_size(sizeof(struct rtnexthop)); |
| 298 | |
| 299 | /* may contain flow and gateway attribute */ |
| 300 | nhsize += 2 * nla_total_size(4); |
| 301 | |
| 302 | /* all nexthops are packed in a nested attribute */ |
| 303 | payload += nla_total_size(fi->fib_nhs * nhsize); |
| 304 | } |
| 305 | |
| 306 | return payload; |
| 307 | } |
| 308 | |
Al Viro | 81f7bf6 | 2006-09-27 18:40:00 -0700 | [diff] [blame] | 309 | void rtmsg_fib(int event, __be32 key, struct fib_alias *fa, |
Milan Kocian | b8f5583 | 2007-05-23 14:55:06 -0700 | [diff] [blame] | 310 | int dst_len, u32 tb_id, struct nl_info *info, |
| 311 | unsigned int nlm_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { |
| 313 | struct sk_buff *skb; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 314 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
Thomas Graf | f21c7bc | 2006-08-15 00:34:17 -0700 | [diff] [blame] | 315 | int err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 317 | skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL); |
Thomas Graf | f21c7bc | 2006-08-15 00:34:17 -0700 | [diff] [blame] | 318 | if (skb == NULL) |
| 319 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 321 | err = fib_dump_info(skb, info->pid, seq, event, tb_id, |
David S. Miller | 37e826c | 2011-03-24 18:06:47 -0700 | [diff] [blame] | 322 | fa->fa_type, key, dst_len, |
Milan Kocian | b8f5583 | 2007-05-23 14:55:06 -0700 | [diff] [blame] | 323 | fa->fa_tos, fa->fa_info, nlm_flags); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 324 | if (err < 0) { |
| 325 | /* -EMSGSIZE implies BUG in fib_nlmsg_size() */ |
| 326 | WARN_ON(err == -EMSGSIZE); |
| 327 | kfree_skb(skb); |
| 328 | goto errout; |
| 329 | } |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 330 | rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE, |
| 331 | info->nlh, GFP_KERNEL); |
| 332 | return; |
Thomas Graf | f21c7bc | 2006-08-15 00:34:17 -0700 | [diff] [blame] | 333 | errout: |
| 334 | if (err < 0) |
Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 335 | rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* Return the first fib alias matching TOS with |
| 339 | * priority less than or equal to PRIO. |
| 340 | */ |
| 341 | struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio) |
| 342 | { |
| 343 | if (fah) { |
| 344 | struct fib_alias *fa; |
| 345 | list_for_each_entry(fa, fah, fa_list) { |
| 346 | if (fa->fa_tos > tos) |
| 347 | continue; |
| 348 | if (fa->fa_info->fib_priority >= prio || |
| 349 | fa->fa_tos < tos) |
| 350 | return fa; |
| 351 | } |
| 352 | } |
| 353 | return NULL; |
| 354 | } |
| 355 | |
| 356 | int fib_detect_death(struct fib_info *fi, int order, |
Denis V. Lunev | c17860a | 2007-12-08 00:22:13 -0800 | [diff] [blame] | 357 | struct fib_info **last_resort, int *last_idx, int dflt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
| 359 | struct neighbour *n; |
| 360 | int state = NUD_NONE; |
| 361 | |
| 362 | n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev); |
| 363 | if (n) { |
| 364 | state = n->nud_state; |
| 365 | neigh_release(n); |
| 366 | } |
Jianjun Kong | d931910 | 2008-11-03 00:23:42 -0800 | [diff] [blame] | 367 | if (state == NUD_REACHABLE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | return 0; |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 369 | if ((state & NUD_VALID) && order != dflt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | return 0; |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 371 | if ((state & NUD_VALID) || |
| 372 | (*last_idx < 0 && order > dflt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | *last_resort = fi; |
| 374 | *last_idx = order; |
| 375 | } |
| 376 | return 1; |
| 377 | } |
| 378 | |
| 379 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
| 380 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 381 | static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | { |
| 383 | int nhs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 385 | while (rtnh_ok(rtnh, remaining)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | nhs++; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 387 | rtnh = rtnh_next(rtnh, &remaining); |
| 388 | } |
| 389 | |
| 390 | /* leftover implies invalid nexthop configuration, discard it */ |
| 391 | return remaining > 0 ? 0 : nhs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 394 | static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh, |
| 395 | int remaining, struct fib_config *cfg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | change_nexthops(fi) { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 398 | int attrlen; |
| 399 | |
| 400 | if (!rtnh_ok(rtnh, remaining)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | return -EINVAL; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 402 | |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 403 | nexthop_nh->nh_flags = |
| 404 | (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags; |
| 405 | nexthop_nh->nh_oif = rtnh->rtnh_ifindex; |
| 406 | nexthop_nh->nh_weight = rtnh->rtnh_hops + 1; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 407 | |
| 408 | attrlen = rtnh_attrlen(rtnh); |
| 409 | if (attrlen > 0) { |
| 410 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 411 | |
| 412 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 413 | nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0; |
Patrick McHardy | c7066f7 | 2011-01-14 13:36:42 +0100 | [diff] [blame] | 414 | #ifdef CONFIG_IP_ROUTE_CLASSID |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 415 | nla = nla_find(attrs, attrlen, RTA_FLOW); |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 416 | nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | #endif |
| 418 | } |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 419 | |
| 420 | rtnh = rtnh_next(rtnh, &remaining); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } endfor_nexthops(fi); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | #endif |
| 427 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 428 | int fib_nh_match(struct fib_config *cfg, struct fib_info *fi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { |
| 430 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 431 | struct rtnexthop *rtnh; |
| 432 | int remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | #endif |
| 434 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 435 | if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | return 1; |
| 437 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 438 | if (cfg->fc_oif || cfg->fc_gw) { |
| 439 | if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) && |
| 440 | (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | return 0; |
| 442 | return 1; |
| 443 | } |
| 444 | |
| 445 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 446 | if (cfg->fc_mp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | return 0; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 448 | |
| 449 | rtnh = cfg->fc_mp; |
| 450 | remaining = cfg->fc_mp_len; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | for_nexthops(fi) { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 453 | int attrlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 455 | if (!rtnh_ok(rtnh, remaining)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | return -EINVAL; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 457 | |
| 458 | if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | return 1; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 460 | |
| 461 | attrlen = rtnh_attrlen(rtnh); |
| 462 | if (attrlen < 0) { |
| 463 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 464 | |
| 465 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
Al Viro | 17fb2c6 | 2006-09-26 22:15:25 -0700 | [diff] [blame] | 466 | if (nla && nla_get_be32(nla) != nh->nh_gw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | return 1; |
Patrick McHardy | c7066f7 | 2011-01-14 13:36:42 +0100 | [diff] [blame] | 468 | #ifdef CONFIG_IP_ROUTE_CLASSID |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 469 | nla = nla_find(attrs, attrlen, RTA_FLOW); |
| 470 | if (nla && nla_get_u32(nla) != nh->nh_tclassid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | return 1; |
| 472 | #endif |
| 473 | } |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 474 | |
| 475 | rtnh = rtnh_next(rtnh, &remaining); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } endfor_nexthops(fi); |
| 477 | #endif |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | |
| 482 | /* |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 483 | * Picture |
| 484 | * ------- |
| 485 | * |
| 486 | * Semantics of nexthop is very messy by historical reasons. |
| 487 | * We have to take into account, that: |
| 488 | * a) gateway can be actually local interface address, |
| 489 | * so that gatewayed route is direct. |
| 490 | * b) gateway must be on-link address, possibly |
| 491 | * described not by an ifaddr, but also by a direct route. |
| 492 | * c) If both gateway and interface are specified, they should not |
| 493 | * contradict. |
| 494 | * d) If we use tunnel routes, gateway could be not on-link. |
| 495 | * |
| 496 | * Attempt to reconcile all of these (alas, self-contradictory) conditions |
| 497 | * results in pretty ugly and hairy code with obscure logic. |
| 498 | * |
| 499 | * I chose to generalized it instead, so that the size |
| 500 | * of code does not increase practically, but it becomes |
| 501 | * much more general. |
| 502 | * Every prefix is assigned a "scope" value: "host" is local address, |
| 503 | * "link" is direct route, |
| 504 | * [ ... "site" ... "interior" ... ] |
| 505 | * and "universe" is true gateway route with global meaning. |
| 506 | * |
| 507 | * Every prefix refers to a set of "nexthop"s (gw, oif), |
| 508 | * where gw must have narrower scope. This recursion stops |
| 509 | * when gw has LOCAL scope or if "nexthop" is declared ONLINK, |
| 510 | * which means that gw is forced to be on link. |
| 511 | * |
| 512 | * Code is still hairy, but now it is apparently logically |
| 513 | * consistent and very flexible. F.e. as by-product it allows |
| 514 | * to co-exists in peace independent exterior and interior |
| 515 | * routing processes. |
| 516 | * |
| 517 | * Normally it looks as following. |
| 518 | * |
| 519 | * {universe prefix} -> (gw, oif) [scope link] |
| 520 | * | |
| 521 | * |-> {link prefix} -> (gw, oif) [scope local] |
| 522 | * | |
| 523 | * |-> {local prefix} (terminal node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | */ |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 525 | static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi, |
| 526 | struct fib_nh *nh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | { |
| 528 | int err; |
Denis V. Lunev | 86167a3 | 2008-01-21 17:34:00 -0800 | [diff] [blame] | 529 | struct net *net; |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 530 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Denis V. Lunev | 86167a3 | 2008-01-21 17:34:00 -0800 | [diff] [blame] | 532 | net = cfg->fc_nlinfo.nl_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | if (nh->nh_gw) { |
| 534 | struct fib_result res; |
| 535 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 536 | if (nh->nh_flags & RTNH_F_ONLINK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 538 | if (cfg->fc_scope >= RT_SCOPE_LINK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | return -EINVAL; |
Denis V. Lunev | 86167a3 | 2008-01-21 17:34:00 -0800 | [diff] [blame] | 540 | if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | return -EINVAL; |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 542 | dev = __dev_get_by_index(net, nh->nh_oif); |
| 543 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | return -ENODEV; |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 545 | if (!(dev->flags & IFF_UP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | return -ENETDOWN; |
| 547 | nh->nh_dev = dev; |
| 548 | dev_hold(dev); |
| 549 | nh->nh_scope = RT_SCOPE_LINK; |
| 550 | return 0; |
| 551 | } |
Eric Dumazet | ebc0ffa | 2010-10-05 10:41:36 +0000 | [diff] [blame] | 552 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
David S. Miller | 9ade228 | 2011-03-12 02:02:42 -0500 | [diff] [blame] | 554 | struct flowi4 fl4 = { |
| 555 | .daddr = nh->nh_gw, |
| 556 | .flowi4_scope = cfg->fc_scope + 1, |
| 557 | .flowi4_oif = nh->nh_oif, |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 558 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
| 560 | /* It is not necessary, but requires a bit of thinking */ |
David S. Miller | 9ade228 | 2011-03-12 02:02:42 -0500 | [diff] [blame] | 561 | if (fl4.flowi4_scope < RT_SCOPE_LINK) |
| 562 | fl4.flowi4_scope = RT_SCOPE_LINK; |
| 563 | err = fib_lookup(net, &fl4, &res); |
Eric Dumazet | ebc0ffa | 2010-10-05 10:41:36 +0000 | [diff] [blame] | 564 | if (err) { |
| 565 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | return err; |
Eric Dumazet | ebc0ffa | 2010-10-05 10:41:36 +0000 | [diff] [blame] | 567 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } |
| 569 | err = -EINVAL; |
| 570 | if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) |
| 571 | goto out; |
| 572 | nh->nh_scope = res.scope; |
| 573 | nh->nh_oif = FIB_RES_OIF(res); |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 574 | nh->nh_dev = dev = FIB_RES_DEV(res); |
| 575 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | goto out; |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 577 | dev_hold(dev); |
Eric Dumazet | 8723e1b | 2010-10-19 00:39:26 +0000 | [diff] [blame] | 578 | err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } else { |
| 580 | struct in_device *in_dev; |
| 581 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 582 | if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | return -EINVAL; |
| 584 | |
Eric Dumazet | 8723e1b | 2010-10-19 00:39:26 +0000 | [diff] [blame] | 585 | rcu_read_lock(); |
| 586 | err = -ENODEV; |
Denis V. Lunev | 86167a3 | 2008-01-21 17:34:00 -0800 | [diff] [blame] | 587 | in_dev = inetdev_by_index(net, nh->nh_oif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | if (in_dev == NULL) |
Eric Dumazet | 8723e1b | 2010-10-19 00:39:26 +0000 | [diff] [blame] | 589 | goto out; |
| 590 | err = -ENETDOWN; |
| 591 | if (!(in_dev->dev->flags & IFF_UP)) |
| 592 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | nh->nh_dev = in_dev->dev; |
| 594 | dev_hold(nh->nh_dev); |
| 595 | nh->nh_scope = RT_SCOPE_HOST; |
Eric Dumazet | 8723e1b | 2010-10-19 00:39:26 +0000 | [diff] [blame] | 596 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
Eric Dumazet | 8723e1b | 2010-10-19 00:39:26 +0000 | [diff] [blame] | 598 | out: |
| 599 | rcu_read_unlock(); |
| 600 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Al Viro | 81f7bf6 | 2006-09-27 18:40:00 -0700 | [diff] [blame] | 603 | static inline unsigned int fib_laddr_hashfn(__be32 val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 605 | unsigned int mask = (fib_info_hash_size - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 607 | return ((__force u32)val ^ |
| 608 | ((__force u32)val >> 7) ^ |
| 609 | ((__force u32)val >> 14)) & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | } |
| 611 | |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 612 | static struct hlist_head *fib_info_hash_alloc(int bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | { |
| 614 | if (bytes <= PAGE_SIZE) |
Joonwoo Park | 88f8349 | 2007-11-26 23:29:32 +0800 | [diff] [blame] | 615 | return kzalloc(bytes, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | else |
| 617 | return (struct hlist_head *) |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 618 | __get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 619 | get_order(bytes)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | } |
| 621 | |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 622 | static void fib_info_hash_free(struct hlist_head *hash, int bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | { |
| 624 | if (!hash) |
| 625 | return; |
| 626 | |
| 627 | if (bytes <= PAGE_SIZE) |
| 628 | kfree(hash); |
| 629 | else |
| 630 | free_pages((unsigned long) hash, get_order(bytes)); |
| 631 | } |
| 632 | |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 633 | static void fib_info_hash_move(struct hlist_head *new_info_hash, |
| 634 | struct hlist_head *new_laddrhash, |
| 635 | unsigned int new_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | { |
David S. Miller | b7656e7 | 2005-08-05 04:12:48 -0700 | [diff] [blame] | 637 | struct hlist_head *old_info_hash, *old_laddrhash; |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 638 | unsigned int old_size = fib_info_hash_size; |
David S. Miller | b7656e7 | 2005-08-05 04:12:48 -0700 | [diff] [blame] | 639 | unsigned int i, bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 641 | spin_lock_bh(&fib_info_lock); |
David S. Miller | b7656e7 | 2005-08-05 04:12:48 -0700 | [diff] [blame] | 642 | old_info_hash = fib_info_hash; |
| 643 | old_laddrhash = fib_info_laddrhash; |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 644 | fib_info_hash_size = new_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
| 646 | for (i = 0; i < old_size; i++) { |
| 647 | struct hlist_head *head = &fib_info_hash[i]; |
| 648 | struct hlist_node *node, *n; |
| 649 | struct fib_info *fi; |
| 650 | |
| 651 | hlist_for_each_entry_safe(fi, node, n, head, fib_hash) { |
| 652 | struct hlist_head *dest; |
| 653 | unsigned int new_hash; |
| 654 | |
| 655 | hlist_del(&fi->fib_hash); |
| 656 | |
| 657 | new_hash = fib_info_hashfn(fi); |
| 658 | dest = &new_info_hash[new_hash]; |
| 659 | hlist_add_head(&fi->fib_hash, dest); |
| 660 | } |
| 661 | } |
| 662 | fib_info_hash = new_info_hash; |
| 663 | |
| 664 | for (i = 0; i < old_size; i++) { |
| 665 | struct hlist_head *lhead = &fib_info_laddrhash[i]; |
| 666 | struct hlist_node *node, *n; |
| 667 | struct fib_info *fi; |
| 668 | |
| 669 | hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) { |
| 670 | struct hlist_head *ldest; |
| 671 | unsigned int new_hash; |
| 672 | |
| 673 | hlist_del(&fi->fib_lhash); |
| 674 | |
| 675 | new_hash = fib_laddr_hashfn(fi->fib_prefsrc); |
| 676 | ldest = &new_laddrhash[new_hash]; |
| 677 | hlist_add_head(&fi->fib_lhash, ldest); |
| 678 | } |
| 679 | } |
| 680 | fib_info_laddrhash = new_laddrhash; |
| 681 | |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 682 | spin_unlock_bh(&fib_info_lock); |
David S. Miller | b7656e7 | 2005-08-05 04:12:48 -0700 | [diff] [blame] | 683 | |
| 684 | bytes = old_size * sizeof(struct hlist_head *); |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 685 | fib_info_hash_free(old_info_hash, bytes); |
| 686 | fib_info_hash_free(old_laddrhash, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } |
| 688 | |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 689 | __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh) |
| 690 | { |
| 691 | nh->nh_saddr = inet_select_addr(nh->nh_dev, |
| 692 | nh->nh_gw, |
David S. Miller | 37e826c | 2011-03-24 18:06:47 -0700 | [diff] [blame] | 693 | nh->nh_parent->fib_scope); |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 694 | nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid); |
| 695 | |
| 696 | return nh->nh_saddr; |
| 697 | } |
| 698 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 699 | struct fib_info *fib_create_info(struct fib_config *cfg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | { |
| 701 | int err; |
| 702 | struct fib_info *fi = NULL; |
| 703 | struct fib_info *ofi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | int nhs = 1; |
Denis V. Lunev | 7462bd74 | 2008-01-31 18:49:32 -0800 | [diff] [blame] | 705 | struct net *net = cfg->fc_nlinfo.nl_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
David S. Miller | 4c8237c | 2011-03-07 14:27:38 -0800 | [diff] [blame] | 707 | if (cfg->fc_type > RTN_MAX) |
| 708 | goto err_inval; |
| 709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | /* Fast check to catch the most weird cases */ |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 711 | if (fib_props[cfg->fc_type].scope > cfg->fc_scope) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | goto err_inval; |
| 713 | |
| 714 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 715 | if (cfg->fc_mp) { |
| 716 | nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | if (nhs == 0) |
| 718 | goto err_inval; |
| 719 | } |
| 720 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
| 722 | err = -ENOBUFS; |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 723 | if (fib_info_cnt >= fib_info_hash_size) { |
| 724 | unsigned int new_size = fib_info_hash_size << 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | struct hlist_head *new_info_hash; |
| 726 | struct hlist_head *new_laddrhash; |
| 727 | unsigned int bytes; |
| 728 | |
| 729 | if (!new_size) |
| 730 | new_size = 1; |
| 731 | bytes = new_size * sizeof(struct hlist_head *); |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 732 | new_info_hash = fib_info_hash_alloc(bytes); |
| 733 | new_laddrhash = fib_info_hash_alloc(bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | if (!new_info_hash || !new_laddrhash) { |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 735 | fib_info_hash_free(new_info_hash, bytes); |
| 736 | fib_info_hash_free(new_laddrhash, bytes); |
Joonwoo Park | 88f8349 | 2007-11-26 23:29:32 +0800 | [diff] [blame] | 737 | } else |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 738 | fib_info_hash_move(new_info_hash, new_laddrhash, new_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 740 | if (!fib_info_hash_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | goto failure; |
| 742 | } |
| 743 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 744 | fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | if (fi == NULL) |
| 746 | goto failure; |
David S. Miller | 725d1e1 | 2011-01-28 14:05:05 -0800 | [diff] [blame] | 747 | if (cfg->fc_mx) { |
| 748 | fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); |
| 749 | if (!fi->fib_metrics) |
| 750 | goto failure; |
| 751 | } else |
| 752 | fi->fib_metrics = (u32 *) dst_default_metrics; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | fib_info_cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
Denis V. Lunev | 57d7a60 | 2008-04-16 02:00:50 -0700 | [diff] [blame] | 755 | fi->fib_net = hold_net(net); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 756 | fi->fib_protocol = cfg->fc_protocol; |
David S. Miller | 37e826c | 2011-03-24 18:06:47 -0700 | [diff] [blame] | 757 | fi->fib_scope = cfg->fc_scope; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 758 | fi->fib_flags = cfg->fc_flags; |
| 759 | fi->fib_priority = cfg->fc_priority; |
| 760 | fi->fib_prefsrc = cfg->fc_prefsrc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
| 762 | fi->fib_nhs = nhs; |
| 763 | change_nexthops(fi) { |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 764 | nexthop_nh->nh_parent = fi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | } endfor_nexthops(fi) |
| 766 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 767 | if (cfg->fc_mx) { |
| 768 | struct nlattr *nla; |
| 769 | int remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 771 | nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { |
Thomas Graf | 8f4c1f9 | 2007-09-12 14:44:36 +0200 | [diff] [blame] | 772 | int type = nla_type(nla); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 773 | |
| 774 | if (type) { |
| 775 | if (type > RTAX_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | goto err_inval; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 777 | fi->fib_metrics[type - 1] = nla_get_u32(nla); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | } |
| 780 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 782 | if (cfg->fc_mp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 784 | err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg); |
| 785 | if (err != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | goto failure; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 787 | if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | goto err_inval; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 789 | if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | goto err_inval; |
Patrick McHardy | c7066f7 | 2011-01-14 13:36:42 +0100 | [diff] [blame] | 791 | #ifdef CONFIG_IP_ROUTE_CLASSID |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 792 | if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | goto err_inval; |
| 794 | #endif |
| 795 | #else |
| 796 | goto err_inval; |
| 797 | #endif |
| 798 | } else { |
| 799 | struct fib_nh *nh = fi->fib_nh; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 800 | |
| 801 | nh->nh_oif = cfg->fc_oif; |
| 802 | nh->nh_gw = cfg->fc_gw; |
| 803 | nh->nh_flags = cfg->fc_flags; |
Patrick McHardy | c7066f7 | 2011-01-14 13:36:42 +0100 | [diff] [blame] | 804 | #ifdef CONFIG_IP_ROUTE_CLASSID |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 805 | nh->nh_tclassid = cfg->fc_flow; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
| 808 | nh->nh_weight = 1; |
| 809 | #endif |
| 810 | } |
| 811 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 812 | if (fib_props[cfg->fc_type].error) { |
| 813 | if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | goto err_inval; |
| 815 | goto link_it; |
David S. Miller | 4c8237c | 2011-03-07 14:27:38 -0800 | [diff] [blame] | 816 | } else { |
| 817 | switch (cfg->fc_type) { |
| 818 | case RTN_UNICAST: |
| 819 | case RTN_LOCAL: |
| 820 | case RTN_BROADCAST: |
| 821 | case RTN_ANYCAST: |
| 822 | case RTN_MULTICAST: |
| 823 | break; |
| 824 | default: |
| 825 | goto err_inval; |
| 826 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 829 | if (cfg->fc_scope > RT_SCOPE_HOST) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | goto err_inval; |
| 831 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 832 | if (cfg->fc_scope == RT_SCOPE_HOST) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | struct fib_nh *nh = fi->fib_nh; |
| 834 | |
| 835 | /* Local address is added. */ |
| 836 | if (nhs != 1 || nh->nh_gw) |
| 837 | goto err_inval; |
| 838 | nh->nh_scope = RT_SCOPE_NOWHERE; |
Denis V. Lunev | 7462bd74 | 2008-01-31 18:49:32 -0800 | [diff] [blame] | 839 | nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | err = -ENODEV; |
| 841 | if (nh->nh_dev == NULL) |
| 842 | goto failure; |
| 843 | } else { |
| 844 | change_nexthops(fi) { |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 845 | err = fib_check_nh(cfg, fi, nexthop_nh); |
| 846 | if (err != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | goto failure; |
| 848 | } endfor_nexthops(fi) |
| 849 | } |
| 850 | |
| 851 | if (fi->fib_prefsrc) { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 852 | if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst || |
| 853 | fi->fib_prefsrc != cfg->fc_dst) |
Denis V. Lunev | 7462bd74 | 2008-01-31 18:49:32 -0800 | [diff] [blame] | 854 | if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | goto err_inval; |
| 856 | } |
| 857 | |
David S. Miller | 1fc050a | 2011-03-07 20:54:48 -0800 | [diff] [blame] | 858 | change_nexthops(fi) { |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 859 | fib_info_update_nh_saddr(net, nexthop_nh); |
David S. Miller | 1fc050a | 2011-03-07 20:54:48 -0800 | [diff] [blame] | 860 | } endfor_nexthops(fi) |
| 861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | link_it: |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 863 | ofi = fib_find_info(fi); |
| 864 | if (ofi) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | fi->fib_dead = 1; |
| 866 | free_fib_info(fi); |
| 867 | ofi->fib_treeref++; |
| 868 | return ofi; |
| 869 | } |
| 870 | |
| 871 | fi->fib_treeref++; |
| 872 | atomic_inc(&fi->fib_clntref); |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 873 | spin_lock_bh(&fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | hlist_add_head(&fi->fib_hash, |
| 875 | &fib_info_hash[fib_info_hashfn(fi)]); |
| 876 | if (fi->fib_prefsrc) { |
| 877 | struct hlist_head *head; |
| 878 | |
| 879 | head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)]; |
| 880 | hlist_add_head(&fi->fib_lhash, head); |
| 881 | } |
| 882 | change_nexthops(fi) { |
| 883 | struct hlist_head *head; |
| 884 | unsigned int hash; |
| 885 | |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 886 | if (!nexthop_nh->nh_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | continue; |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 888 | hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | head = &fib_info_devhash[hash]; |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 890 | hlist_add_head(&nexthop_nh->nh_hash, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | } endfor_nexthops(fi) |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 892 | spin_unlock_bh(&fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | return fi; |
| 894 | |
| 895 | err_inval: |
| 896 | err = -EINVAL; |
| 897 | |
| 898 | failure: |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 899 | if (fi) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | fi->fib_dead = 1; |
| 901 | free_fib_info(fi); |
| 902 | } |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 903 | |
| 904 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 907 | int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event, |
David S. Miller | 37e826c | 2011-03-24 18:06:47 -0700 | [diff] [blame] | 908 | u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos, |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 909 | struct fib_info *fi, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | { |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 911 | struct nlmsghdr *nlh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | struct rtmsg *rtm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 914 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags); |
| 915 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 916 | return -EMSGSIZE; |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 917 | |
| 918 | rtm = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | rtm->rtm_family = AF_INET; |
| 920 | rtm->rtm_dst_len = dst_len; |
| 921 | rtm->rtm_src_len = 0; |
| 922 | rtm->rtm_tos = tos; |
Krzysztof Piotr Oledzki | 709772e | 2008-06-10 15:44:49 -0700 | [diff] [blame] | 923 | if (tb_id < 256) |
| 924 | rtm->rtm_table = tb_id; |
| 925 | else |
| 926 | rtm->rtm_table = RT_TABLE_COMPAT; |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 927 | NLA_PUT_U32(skb, RTA_TABLE, tb_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | rtm->rtm_type = type; |
| 929 | rtm->rtm_flags = fi->fib_flags; |
David S. Miller | 37e826c | 2011-03-24 18:06:47 -0700 | [diff] [blame] | 930 | rtm->rtm_scope = fi->fib_scope; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | rtm->rtm_protocol = fi->fib_protocol; |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 932 | |
| 933 | if (rtm->rtm_dst_len) |
Al Viro | 17fb2c6 | 2006-09-26 22:15:25 -0700 | [diff] [blame] | 934 | NLA_PUT_BE32(skb, RTA_DST, dst); |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | if (fi->fib_priority) |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 937 | NLA_PUT_U32(skb, RTA_PRIORITY, fi->fib_priority); |
| 938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0) |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 940 | goto nla_put_failure; |
| 941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | if (fi->fib_prefsrc) |
Al Viro | 17fb2c6 | 2006-09-26 22:15:25 -0700 | [diff] [blame] | 943 | NLA_PUT_BE32(skb, RTA_PREFSRC, fi->fib_prefsrc); |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | if (fi->fib_nhs == 1) { |
| 946 | if (fi->fib_nh->nh_gw) |
Al Viro | 17fb2c6 | 2006-09-26 22:15:25 -0700 | [diff] [blame] | 947 | NLA_PUT_BE32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw); |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 948 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | if (fi->fib_nh->nh_oif) |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 950 | NLA_PUT_U32(skb, RTA_OIF, fi->fib_nh->nh_oif); |
Patrick McHardy | c7066f7 | 2011-01-14 13:36:42 +0100 | [diff] [blame] | 951 | #ifdef CONFIG_IP_ROUTE_CLASSID |
Patrick McHardy | 8265abc | 2006-07-21 15:09:55 -0700 | [diff] [blame] | 952 | if (fi->fib_nh[0].nh_tclassid) |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 953 | NLA_PUT_U32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid); |
Patrick McHardy | 8265abc | 2006-07-21 15:09:55 -0700 | [diff] [blame] | 954 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | } |
| 956 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
| 957 | if (fi->fib_nhs > 1) { |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 958 | struct rtnexthop *rtnh; |
| 959 | struct nlattr *mp; |
| 960 | |
| 961 | mp = nla_nest_start(skb, RTA_MULTIPATH); |
| 962 | if (mp == NULL) |
| 963 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
| 965 | for_nexthops(fi) { |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 966 | rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); |
| 967 | if (rtnh == NULL) |
| 968 | goto nla_put_failure; |
| 969 | |
| 970 | rtnh->rtnh_flags = nh->nh_flags & 0xFF; |
| 971 | rtnh->rtnh_hops = nh->nh_weight - 1; |
| 972 | rtnh->rtnh_ifindex = nh->nh_oif; |
| 973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | if (nh->nh_gw) |
Al Viro | 17fb2c6 | 2006-09-26 22:15:25 -0700 | [diff] [blame] | 975 | NLA_PUT_BE32(skb, RTA_GATEWAY, nh->nh_gw); |
Patrick McHardy | c7066f7 | 2011-01-14 13:36:42 +0100 | [diff] [blame] | 976 | #ifdef CONFIG_IP_ROUTE_CLASSID |
Patrick McHardy | 8265abc | 2006-07-21 15:09:55 -0700 | [diff] [blame] | 977 | if (nh->nh_tclassid) |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 978 | NLA_PUT_U32(skb, RTA_FLOW, nh->nh_tclassid); |
Patrick McHardy | 8265abc | 2006-07-21 15:09:55 -0700 | [diff] [blame] | 979 | #endif |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 980 | /* length of rtnetlink header + attributes */ |
| 981 | rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | } endfor_nexthops(fi); |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 983 | |
| 984 | nla_nest_end(skb, mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | } |
| 986 | #endif |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 987 | return nlmsg_end(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 989 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 990 | nlmsg_cancel(skb, nlh); |
| 991 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | /* |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 995 | * Update FIB if: |
| 996 | * - local address disappeared -> we must delete all the entries |
| 997 | * referring to it. |
| 998 | * - device went down -> we must shutdown all nexthops going via it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | */ |
Denis V. Lunev | 4814bdb | 2008-01-31 18:50:07 -0800 | [diff] [blame] | 1000 | int fib_sync_down_addr(struct net *net, __be32 local) |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1001 | { |
| 1002 | int ret = 0; |
| 1003 | unsigned int hash = fib_laddr_hashfn(local); |
| 1004 | struct hlist_head *head = &fib_info_laddrhash[hash]; |
| 1005 | struct hlist_node *node; |
| 1006 | struct fib_info *fi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1008 | if (fib_info_laddrhash == NULL || local == 0) |
| 1009 | return 0; |
| 1010 | |
| 1011 | hlist_for_each_entry(fi, node, head, fib_lhash) { |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 1012 | if (!net_eq(fi->fib_net, net)) |
Denis V. Lunev | 4814bdb | 2008-01-31 18:50:07 -0800 | [diff] [blame] | 1013 | continue; |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1014 | if (fi->fib_prefsrc == local) { |
| 1015 | fi->fib_flags |= RTNH_F_DEAD; |
| 1016 | ret++; |
| 1017 | } |
| 1018 | } |
| 1019 | return ret; |
| 1020 | } |
| 1021 | |
| 1022 | int fib_sync_down_dev(struct net_device *dev, int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | { |
| 1024 | int ret = 0; |
| 1025 | int scope = RT_SCOPE_NOWHERE; |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1026 | struct fib_info *prev_fi = NULL; |
| 1027 | unsigned int hash = fib_devindex_hashfn(dev->ifindex); |
| 1028 | struct hlist_head *head = &fib_info_devhash[hash]; |
| 1029 | struct hlist_node *node; |
| 1030 | struct fib_nh *nh; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1031 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | if (force) |
| 1033 | scope = -1; |
| 1034 | |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1035 | hlist_for_each_entry(nh, node, head, nh_hash) { |
| 1036 | struct fib_info *fi = nh->nh_parent; |
| 1037 | int dead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1039 | BUG_ON(!fi->fib_nhs); |
| 1040 | if (nh->nh_dev != dev || fi == prev_fi) |
| 1041 | continue; |
| 1042 | prev_fi = fi; |
| 1043 | dead = 0; |
| 1044 | change_nexthops(fi) { |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1045 | if (nexthop_nh->nh_flags & RTNH_F_DEAD) |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1046 | dead++; |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 1047 | else if (nexthop_nh->nh_dev == dev && |
| 1048 | nexthop_nh->nh_scope != scope) { |
| 1049 | nexthop_nh->nh_flags |= RTNH_F_DEAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1051 | spin_lock_bh(&fib_multipath_lock); |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 1052 | fi->fib_power -= nexthop_nh->nh_power; |
| 1053 | nexthop_nh->nh_power = 0; |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1054 | spin_unlock_bh(&fib_multipath_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | #endif |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1056 | dead++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | } |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1058 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 1059 | if (force > 1 && nexthop_nh->nh_dev == dev) { |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1060 | dead = fi->fib_nhs; |
| 1061 | break; |
| 1062 | } |
| 1063 | #endif |
| 1064 | } endfor_nexthops(fi) |
| 1065 | if (dead == fi->fib_nhs) { |
| 1066 | fi->fib_flags |= RTNH_F_DEAD; |
| 1067 | ret++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | return ret; |
| 1072 | } |
| 1073 | |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 1074 | /* Must be invoked inside of an RCU protected region. */ |
| 1075 | void fib_select_default(struct fib_result *res) |
| 1076 | { |
| 1077 | struct fib_info *fi = NULL, *last_resort = NULL; |
| 1078 | struct list_head *fa_head = res->fa_head; |
| 1079 | struct fib_table *tb = res->table; |
| 1080 | int order = -1, last_idx = -1; |
| 1081 | struct fib_alias *fa; |
| 1082 | |
| 1083 | list_for_each_entry_rcu(fa, fa_head, fa_list) { |
| 1084 | struct fib_info *next_fi = fa->fa_info; |
| 1085 | |
David S. Miller | 37e826c | 2011-03-24 18:06:47 -0700 | [diff] [blame] | 1086 | if (next_fi->fib_scope != res->scope || |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 1087 | fa->fa_type != RTN_UNICAST) |
| 1088 | continue; |
| 1089 | |
| 1090 | if (next_fi->fib_priority > res->fi->fib_priority) |
| 1091 | break; |
| 1092 | if (!next_fi->fib_nh[0].nh_gw || |
| 1093 | next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK) |
| 1094 | continue; |
| 1095 | |
| 1096 | fib_alias_accessed(fa); |
| 1097 | |
| 1098 | if (fi == NULL) { |
| 1099 | if (next_fi != res->fi) |
| 1100 | break; |
| 1101 | } else if (!fib_detect_death(fi, order, &last_resort, |
| 1102 | &last_idx, tb->tb_default)) { |
| 1103 | fib_result_assign(res, fi); |
| 1104 | tb->tb_default = order; |
| 1105 | goto out; |
| 1106 | } |
| 1107 | fi = next_fi; |
| 1108 | order++; |
| 1109 | } |
| 1110 | |
| 1111 | if (order <= 0 || fi == NULL) { |
| 1112 | tb->tb_default = -1; |
| 1113 | goto out; |
| 1114 | } |
| 1115 | |
| 1116 | if (!fib_detect_death(fi, order, &last_resort, &last_idx, |
| 1117 | tb->tb_default)) { |
| 1118 | fib_result_assign(res, fi); |
| 1119 | tb->tb_default = order; |
| 1120 | goto out; |
| 1121 | } |
| 1122 | |
| 1123 | if (last_idx >= 0) |
| 1124 | fib_result_assign(res, last_resort); |
| 1125 | tb->tb_default = last_idx; |
| 1126 | out: |
Eric Dumazet | 31d4093 | 2011-02-14 11:23:04 -0800 | [diff] [blame] | 1127 | return; |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
| 1131 | |
| 1132 | /* |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1133 | * Dead device goes up. We wake up dead nexthops. |
| 1134 | * It takes sense only on multipath routes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | int fib_sync_up(struct net_device *dev) |
| 1137 | { |
| 1138 | struct fib_info *prev_fi; |
| 1139 | unsigned int hash; |
| 1140 | struct hlist_head *head; |
| 1141 | struct hlist_node *node; |
| 1142 | struct fib_nh *nh; |
| 1143 | int ret; |
| 1144 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1145 | if (!(dev->flags & IFF_UP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | return 0; |
| 1147 | |
| 1148 | prev_fi = NULL; |
| 1149 | hash = fib_devindex_hashfn(dev->ifindex); |
| 1150 | head = &fib_info_devhash[hash]; |
| 1151 | ret = 0; |
| 1152 | |
| 1153 | hlist_for_each_entry(nh, node, head, nh_hash) { |
| 1154 | struct fib_info *fi = nh->nh_parent; |
| 1155 | int alive; |
| 1156 | |
| 1157 | BUG_ON(!fi->fib_nhs); |
| 1158 | if (nh->nh_dev != dev || fi == prev_fi) |
| 1159 | continue; |
| 1160 | |
| 1161 | prev_fi = fi; |
| 1162 | alive = 0; |
| 1163 | change_nexthops(fi) { |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1164 | if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | alive++; |
| 1166 | continue; |
| 1167 | } |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 1168 | if (nexthop_nh->nh_dev == NULL || |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1169 | !(nexthop_nh->nh_dev->flags & IFF_UP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | continue; |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 1171 | if (nexthop_nh->nh_dev != dev || |
| 1172 | !__in_dev_get_rtnl(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | continue; |
| 1174 | alive++; |
| 1175 | spin_lock_bh(&fib_multipath_lock); |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 1176 | nexthop_nh->nh_power = 0; |
| 1177 | nexthop_nh->nh_flags &= ~RTNH_F_DEAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | spin_unlock_bh(&fib_multipath_lock); |
| 1179 | } endfor_nexthops(fi) |
| 1180 | |
| 1181 | if (alive > 0) { |
| 1182 | fi->fib_flags &= ~RTNH_F_DEAD; |
| 1183 | ret++; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | return ret; |
| 1188 | } |
| 1189 | |
| 1190 | /* |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1191 | * The algorithm is suboptimal, but it provides really |
| 1192 | * fair weighted route distribution. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | */ |
David S. Miller | 1b7fe593 | 2011-03-10 17:01:16 -0800 | [diff] [blame] | 1194 | void fib_select_multipath(struct fib_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | { |
| 1196 | struct fib_info *fi = res->fi; |
| 1197 | int w; |
| 1198 | |
| 1199 | spin_lock_bh(&fib_multipath_lock); |
| 1200 | if (fi->fib_power <= 0) { |
| 1201 | int power = 0; |
| 1202 | change_nexthops(fi) { |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1203 | if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) { |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 1204 | power += nexthop_nh->nh_weight; |
| 1205 | nexthop_nh->nh_power = nexthop_nh->nh_weight; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | } |
| 1207 | } endfor_nexthops(fi); |
| 1208 | fi->fib_power = power; |
| 1209 | if (power <= 0) { |
| 1210 | spin_unlock_bh(&fib_multipath_lock); |
| 1211 | /* Race condition: route has just become dead. */ |
| 1212 | res->nh_sel = 0; |
| 1213 | return; |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | |
| 1218 | /* w should be random number [0..fi->fib_power-1], |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1219 | * it is pretty bad approximation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | */ |
| 1221 | |
| 1222 | w = jiffies % fi->fib_power; |
| 1223 | |
| 1224 | change_nexthops(fi) { |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1225 | if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) && |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 1226 | nexthop_nh->nh_power) { |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1227 | w -= nexthop_nh->nh_power; |
| 1228 | if (w <= 0) { |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 1229 | nexthop_nh->nh_power--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | fi->fib_power--; |
| 1231 | res->nh_sel = nhsel; |
| 1232 | spin_unlock_bh(&fib_multipath_lock); |
| 1233 | return; |
| 1234 | } |
| 1235 | } |
| 1236 | } endfor_nexthops(fi); |
| 1237 | |
| 1238 | /* Race condition: route has just become dead. */ |
| 1239 | res->nh_sel = 0; |
| 1240 | spin_unlock_bh(&fib_multipath_lock); |
| 1241 | } |
| 1242 | #endif |