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