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