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