xref: /linux/net/ipv4/fib_semantics.c (revision d26b3a7c4b3b26319f18bb645de93eba8f4bdcd5)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		IPv4 Forwarding Information Base: semantics.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
111da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
121da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
131da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <asm/uaccess.h>
171da177e4SLinus Torvalds #include <linux/bitops.h>
181da177e4SLinus Torvalds #include <linux/types.h>
191da177e4SLinus Torvalds #include <linux/kernel.h>
201da177e4SLinus Torvalds #include <linux/jiffies.h>
211da177e4SLinus Torvalds #include <linux/mm.h>
221da177e4SLinus Torvalds #include <linux/string.h>
231da177e4SLinus Torvalds #include <linux/socket.h>
241da177e4SLinus Torvalds #include <linux/sockios.h>
251da177e4SLinus Torvalds #include <linux/errno.h>
261da177e4SLinus Torvalds #include <linux/in.h>
271da177e4SLinus Torvalds #include <linux/inet.h>
2814c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h>
291da177e4SLinus Torvalds #include <linux/netdevice.h>
301da177e4SLinus Torvalds #include <linux/if_arp.h>
311da177e4SLinus Torvalds #include <linux/proc_fs.h>
321da177e4SLinus Torvalds #include <linux/skbuff.h>
331da177e4SLinus Torvalds #include <linux/init.h>
345a0e3ad6STejun Heo #include <linux/slab.h>
351da177e4SLinus Torvalds 
3614c85021SArnaldo Carvalho de Melo #include <net/arp.h>
371da177e4SLinus Torvalds #include <net/ip.h>
381da177e4SLinus Torvalds #include <net/protocol.h>
391da177e4SLinus Torvalds #include <net/route.h>
401da177e4SLinus Torvalds #include <net/tcp.h>
411da177e4SLinus Torvalds #include <net/sock.h>
421da177e4SLinus Torvalds #include <net/ip_fib.h>
43f21c7bc5SThomas Graf #include <net/netlink.h>
444e902c57SThomas Graf #include <net/nexthop.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include "fib_lookup.h"
471da177e4SLinus Torvalds 
48832b4c5eSStephen Hemminger static DEFINE_SPINLOCK(fib_info_lock);
491da177e4SLinus Torvalds static struct hlist_head *fib_info_hash;
501da177e4SLinus Torvalds static struct hlist_head *fib_info_laddrhash;
51123b9731SDavid S. Miller static unsigned int fib_info_hash_size;
521da177e4SLinus Torvalds static unsigned int fib_info_cnt;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #define DEVINDEX_HASHBITS 8
551da177e4SLinus Torvalds #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
561da177e4SLinus Torvalds static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds static DEFINE_SPINLOCK(fib_multipath_lock);
611da177e4SLinus Torvalds 
626a31d2a9SEric Dumazet #define for_nexthops(fi) {						\
636a31d2a9SEric Dumazet 	int nhsel; const struct fib_nh *nh;				\
646a31d2a9SEric Dumazet 	for (nhsel = 0, nh = (fi)->fib_nh;				\
656a31d2a9SEric Dumazet 	     nhsel < (fi)->fib_nhs;					\
666a31d2a9SEric Dumazet 	     nh++, nhsel++)
671da177e4SLinus Torvalds 
686a31d2a9SEric Dumazet #define change_nexthops(fi) {						\
696a31d2a9SEric Dumazet 	int nhsel; struct fib_nh *nexthop_nh;				\
706a31d2a9SEric Dumazet 	for (nhsel = 0,	nexthop_nh = (struct fib_nh *)((fi)->fib_nh);	\
716a31d2a9SEric Dumazet 	     nhsel < (fi)->fib_nhs;					\
726a31d2a9SEric Dumazet 	     nexthop_nh++, nhsel++)
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds #else /* CONFIG_IP_ROUTE_MULTIPATH */
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds /* Hope, that gcc will optimize it to get rid of dummy loop */
771da177e4SLinus Torvalds 
786a31d2a9SEric Dumazet #define for_nexthops(fi) {						\
796a31d2a9SEric Dumazet 	int nhsel; const struct fib_nh *nh = (fi)->fib_nh;		\
801da177e4SLinus Torvalds 	for (nhsel = 0; nhsel < 1; nhsel++)
811da177e4SLinus Torvalds 
826a31d2a9SEric Dumazet #define change_nexthops(fi) {						\
836a31d2a9SEric Dumazet 	int nhsel;							\
846a31d2a9SEric Dumazet 	struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh);	\
851da177e4SLinus Torvalds 	for (nhsel = 0; nhsel < 1; nhsel++)
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #endif /* CONFIG_IP_ROUTE_MULTIPATH */
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds #define endfor_nexthops(fi) }
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 
923be0686bSDavid S. Miller const struct fib_prop fib_props[RTN_MAX + 1] = {
936a31d2a9SEric Dumazet 	[RTN_UNSPEC] = {
941da177e4SLinus Torvalds 		.error	= 0,
951da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
966a31d2a9SEric Dumazet 	},
976a31d2a9SEric Dumazet 	[RTN_UNICAST] = {
981da177e4SLinus Torvalds 		.error	= 0,
991da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1006a31d2a9SEric Dumazet 	},
1016a31d2a9SEric Dumazet 	[RTN_LOCAL] = {
1021da177e4SLinus Torvalds 		.error	= 0,
1031da177e4SLinus Torvalds 		.scope	= RT_SCOPE_HOST,
1046a31d2a9SEric Dumazet 	},
1056a31d2a9SEric Dumazet 	[RTN_BROADCAST] = {
1061da177e4SLinus Torvalds 		.error	= 0,
1071da177e4SLinus Torvalds 		.scope	= RT_SCOPE_LINK,
1086a31d2a9SEric Dumazet 	},
1096a31d2a9SEric Dumazet 	[RTN_ANYCAST] = {
1101da177e4SLinus Torvalds 		.error	= 0,
1111da177e4SLinus Torvalds 		.scope	= RT_SCOPE_LINK,
1126a31d2a9SEric Dumazet 	},
1136a31d2a9SEric Dumazet 	[RTN_MULTICAST] = {
1141da177e4SLinus Torvalds 		.error	= 0,
1151da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1166a31d2a9SEric Dumazet 	},
1176a31d2a9SEric Dumazet 	[RTN_BLACKHOLE] = {
1181da177e4SLinus Torvalds 		.error	= -EINVAL,
1191da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1206a31d2a9SEric Dumazet 	},
1216a31d2a9SEric Dumazet 	[RTN_UNREACHABLE] = {
1221da177e4SLinus Torvalds 		.error	= -EHOSTUNREACH,
1231da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1246a31d2a9SEric Dumazet 	},
1256a31d2a9SEric Dumazet 	[RTN_PROHIBIT] = {
1261da177e4SLinus Torvalds 		.error	= -EACCES,
1271da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1286a31d2a9SEric Dumazet 	},
1296a31d2a9SEric Dumazet 	[RTN_THROW] = {
1301da177e4SLinus Torvalds 		.error	= -EAGAIN,
1311da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1326a31d2a9SEric Dumazet 	},
1336a31d2a9SEric Dumazet 	[RTN_NAT] = {
1341da177e4SLinus Torvalds 		.error	= -EINVAL,
1351da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1366a31d2a9SEric Dumazet 	},
1376a31d2a9SEric Dumazet 	[RTN_XRESOLVE] = {
1381da177e4SLinus Torvalds 		.error	= -EINVAL,
1391da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1406a31d2a9SEric Dumazet 	},
1411da177e4SLinus Torvalds };
1421da177e4SLinus Torvalds 
1434895c771SDavid S. Miller static void free_nh_exceptions(struct fib_nh *nh)
1444895c771SDavid S. Miller {
1454895c771SDavid S. Miller 	struct fnhe_hash_bucket *hash = nh->nh_exceptions;
1464895c771SDavid S. Miller 	int i;
1474895c771SDavid S. Miller 
1484895c771SDavid S. Miller 	for (i = 0; i < FNHE_HASH_SIZE; i++) {
1494895c771SDavid S. Miller 		struct fib_nh_exception *fnhe;
1504895c771SDavid S. Miller 
1515abf7f7eSEric Dumazet 		fnhe = rcu_dereference_protected(hash[i].chain, 1);
1524895c771SDavid S. Miller 		while (fnhe) {
1534895c771SDavid S. Miller 			struct fib_nh_exception *next;
1544895c771SDavid S. Miller 
1555abf7f7eSEric Dumazet 			next = rcu_dereference_protected(fnhe->fnhe_next, 1);
1564895c771SDavid S. Miller 			kfree(fnhe);
1574895c771SDavid S. Miller 
1584895c771SDavid S. Miller 			fnhe = next;
1594895c771SDavid S. Miller 		}
1604895c771SDavid S. Miller 	}
1614895c771SDavid S. Miller 	kfree(hash);
1624895c771SDavid S. Miller }
1634895c771SDavid S. Miller 
16454764bb6SEric Dumazet static void rt_nexthop_free(struct rtable __rcu **rtp)
16554764bb6SEric Dumazet {
16654764bb6SEric Dumazet 	struct rtable *rt = rcu_dereference_protected(*rtp, 1);
16754764bb6SEric Dumazet 
16854764bb6SEric Dumazet 	if (!rt)
16954764bb6SEric Dumazet 		return;
17054764bb6SEric Dumazet 
17154764bb6SEric Dumazet 	/* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
17254764bb6SEric Dumazet 	 * because we waited an RCU grace period before calling
17354764bb6SEric Dumazet 	 * free_fib_info_rcu()
17454764bb6SEric Dumazet 	 */
17554764bb6SEric Dumazet 
17654764bb6SEric Dumazet 	dst_free(&rt->dst);
17754764bb6SEric Dumazet }
17854764bb6SEric Dumazet 
179*d26b3a7cSEric Dumazet static void rt_nexthop_free_cpus(struct rtable __rcu * __percpu *rtp)
180*d26b3a7cSEric Dumazet {
181*d26b3a7cSEric Dumazet 	int cpu;
182*d26b3a7cSEric Dumazet 
183*d26b3a7cSEric Dumazet 	if (!rtp)
184*d26b3a7cSEric Dumazet 		return;
185*d26b3a7cSEric Dumazet 
186*d26b3a7cSEric Dumazet 	for_each_possible_cpu(cpu) {
187*d26b3a7cSEric Dumazet 		struct rtable *rt;
188*d26b3a7cSEric Dumazet 
189*d26b3a7cSEric Dumazet 		rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
190*d26b3a7cSEric Dumazet 		if (rt)
191*d26b3a7cSEric Dumazet 			dst_free(&rt->dst);
192*d26b3a7cSEric Dumazet 	}
193*d26b3a7cSEric Dumazet 	free_percpu(rtp);
194*d26b3a7cSEric Dumazet }
195*d26b3a7cSEric Dumazet 
1961da177e4SLinus Torvalds /* Release a nexthop info record */
19719c1ea14SYan, Zheng static void free_fib_info_rcu(struct rcu_head *head)
19819c1ea14SYan, Zheng {
19919c1ea14SYan, Zheng 	struct fib_info *fi = container_of(head, struct fib_info, rcu);
20019c1ea14SYan, Zheng 
201e49cc0daSYanmin Zhang 	change_nexthops(fi) {
202e49cc0daSYanmin Zhang 		if (nexthop_nh->nh_dev)
203e49cc0daSYanmin Zhang 			dev_put(nexthop_nh->nh_dev);
2044895c771SDavid S. Miller 		if (nexthop_nh->nh_exceptions)
2054895c771SDavid S. Miller 			free_nh_exceptions(nexthop_nh);
206*d26b3a7cSEric Dumazet 		rt_nexthop_free_cpus(nexthop_nh->nh_pcpu_rth_output);
20754764bb6SEric Dumazet 		rt_nexthop_free(&nexthop_nh->nh_rth_input);
208e49cc0daSYanmin Zhang 	} endfor_nexthops(fi);
209e49cc0daSYanmin Zhang 
210e49cc0daSYanmin Zhang 	release_net(fi->fib_net);
21119c1ea14SYan, Zheng 	if (fi->fib_metrics != (u32 *) dst_default_metrics)
21219c1ea14SYan, Zheng 		kfree(fi->fib_metrics);
21319c1ea14SYan, Zheng 	kfree(fi);
21419c1ea14SYan, Zheng }
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds void free_fib_info(struct fib_info *fi)
2171da177e4SLinus Torvalds {
2181da177e4SLinus Torvalds 	if (fi->fib_dead == 0) {
219058bd4d2SJoe Perches 		pr_warn("Freeing alive fib_info %p\n", fi);
2201da177e4SLinus Torvalds 		return;
2211da177e4SLinus Torvalds 	}
2221da177e4SLinus Torvalds 	fib_info_cnt--;
2237a9bc9b8SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
2247a9bc9b8SDavid S. Miller 	change_nexthops(fi) {
2257a9bc9b8SDavid S. Miller 		if (nexthop_nh->nh_tclassid)
226f4530fa5SDavid S. Miller 			fi->fib_net->ipv4.fib_num_tclassid_users--;
2277a9bc9b8SDavid S. Miller 	} endfor_nexthops(fi);
2287a9bc9b8SDavid S. Miller #endif
22919c1ea14SYan, Zheng 	call_rcu(&fi->rcu, free_fib_info_rcu);
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds void fib_release_info(struct fib_info *fi)
2331da177e4SLinus Torvalds {
234832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
2351da177e4SLinus Torvalds 	if (fi && --fi->fib_treeref == 0) {
2361da177e4SLinus Torvalds 		hlist_del(&fi->fib_hash);
2371da177e4SLinus Torvalds 		if (fi->fib_prefsrc)
2381da177e4SLinus Torvalds 			hlist_del(&fi->fib_lhash);
2391da177e4SLinus Torvalds 		change_nexthops(fi) {
24071fceff0SDavid S. Miller 			if (!nexthop_nh->nh_dev)
2411da177e4SLinus Torvalds 				continue;
24271fceff0SDavid S. Miller 			hlist_del(&nexthop_nh->nh_hash);
2431da177e4SLinus Torvalds 		} endfor_nexthops(fi)
2441da177e4SLinus Torvalds 		fi->fib_dead = 1;
2451da177e4SLinus Torvalds 		fib_info_put(fi);
2461da177e4SLinus Torvalds 	}
247832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2506a31d2a9SEric Dumazet static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	const struct fib_nh *onh = ofi->fib_nh;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	for_nexthops(fi) {
2551da177e4SLinus Torvalds 		if (nh->nh_oif != onh->nh_oif ||
2561da177e4SLinus Torvalds 		    nh->nh_gw  != onh->nh_gw ||
2571da177e4SLinus Torvalds 		    nh->nh_scope != onh->nh_scope ||
2581da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
2591da177e4SLinus Torvalds 		    nh->nh_weight != onh->nh_weight ||
2601da177e4SLinus Torvalds #endif
261c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
2621da177e4SLinus Torvalds 		    nh->nh_tclassid != onh->nh_tclassid ||
2631da177e4SLinus Torvalds #endif
2641da177e4SLinus Torvalds 		    ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
2651da177e4SLinus Torvalds 			return -1;
2661da177e4SLinus Torvalds 		onh++;
2671da177e4SLinus Torvalds 	} endfor_nexthops(fi);
2681da177e4SLinus Torvalds 	return 0;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
27188ebc72fSDavid S. Miller static inline unsigned int fib_devindex_hashfn(unsigned int val)
27288ebc72fSDavid S. Miller {
27388ebc72fSDavid S. Miller 	unsigned int mask = DEVINDEX_HASHSIZE - 1;
27488ebc72fSDavid S. Miller 
27588ebc72fSDavid S. Miller 	return (val ^
27688ebc72fSDavid S. Miller 		(val >> DEVINDEX_HASHBITS) ^
27788ebc72fSDavid S. Miller 		(val >> (DEVINDEX_HASHBITS * 2))) & mask;
27888ebc72fSDavid S. Miller }
27988ebc72fSDavid S. Miller 
2801da177e4SLinus Torvalds static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
2811da177e4SLinus Torvalds {
282123b9731SDavid S. Miller 	unsigned int mask = (fib_info_hash_size - 1);
2831da177e4SLinus Torvalds 	unsigned int val = fi->fib_nhs;
2841da177e4SLinus Torvalds 
28537e826c5SDavid S. Miller 	val ^= (fi->fib_protocol << 8) | fi->fib_scope;
28681f7bf6cSAl Viro 	val ^= (__force u32)fi->fib_prefsrc;
2871da177e4SLinus Torvalds 	val ^= fi->fib_priority;
28888ebc72fSDavid S. Miller 	for_nexthops(fi) {
28988ebc72fSDavid S. Miller 		val ^= fib_devindex_hashfn(nh->nh_oif);
29088ebc72fSDavid S. Miller 	} endfor_nexthops(fi)
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	return (val ^ (val >> 7) ^ (val >> 12)) & mask;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds static struct fib_info *fib_find_info(const struct fib_info *nfi)
2961da177e4SLinus Torvalds {
2971da177e4SLinus Torvalds 	struct hlist_head *head;
2981da177e4SLinus Torvalds 	struct hlist_node *node;
2991da177e4SLinus Torvalds 	struct fib_info *fi;
3001da177e4SLinus Torvalds 	unsigned int hash;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	hash = fib_info_hashfn(nfi);
3031da177e4SLinus Torvalds 	head = &fib_info_hash[hash];
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	hlist_for_each_entry(fi, node, head, fib_hash) {
30609ad9bc7SOctavian Purdila 		if (!net_eq(fi->fib_net, nfi->fib_net))
3074814bdbdSDenis V. Lunev 			continue;
3081da177e4SLinus Torvalds 		if (fi->fib_nhs != nfi->fib_nhs)
3091da177e4SLinus Torvalds 			continue;
3101da177e4SLinus Torvalds 		if (nfi->fib_protocol == fi->fib_protocol &&
31137e826c5SDavid S. Miller 		    nfi->fib_scope == fi->fib_scope &&
3121da177e4SLinus Torvalds 		    nfi->fib_prefsrc == fi->fib_prefsrc &&
3131da177e4SLinus Torvalds 		    nfi->fib_priority == fi->fib_priority &&
3141da177e4SLinus Torvalds 		    memcmp(nfi->fib_metrics, fi->fib_metrics,
315fcd13f42SEric Dumazet 			   sizeof(u32) * RTAX_MAX) == 0 &&
3161da177e4SLinus Torvalds 		    ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
3171da177e4SLinus Torvalds 		    (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
3181da177e4SLinus Torvalds 			return fi;
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	return NULL;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds /* Check, that the gateway is already configured.
3256a31d2a9SEric Dumazet  * Used only by redirect accept routine.
3261da177e4SLinus Torvalds  */
327d878e72eSAl Viro int ip_fib_check_default(__be32 gw, struct net_device *dev)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	struct hlist_head *head;
3301da177e4SLinus Torvalds 	struct hlist_node *node;
3311da177e4SLinus Torvalds 	struct fib_nh *nh;
3321da177e4SLinus Torvalds 	unsigned int hash;
3331da177e4SLinus Torvalds 
334832b4c5eSStephen Hemminger 	spin_lock(&fib_info_lock);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	hash = fib_devindex_hashfn(dev->ifindex);
3371da177e4SLinus Torvalds 	head = &fib_info_devhash[hash];
3381da177e4SLinus Torvalds 	hlist_for_each_entry(nh, node, head, nh_hash) {
3391da177e4SLinus Torvalds 		if (nh->nh_dev == dev &&
3401da177e4SLinus Torvalds 		    nh->nh_gw == gw &&
3411da177e4SLinus Torvalds 		    !(nh->nh_flags & RTNH_F_DEAD)) {
342832b4c5eSStephen Hemminger 			spin_unlock(&fib_info_lock);
3431da177e4SLinus Torvalds 			return 0;
3441da177e4SLinus Torvalds 		}
3451da177e4SLinus Torvalds 	}
3461da177e4SLinus Torvalds 
347832b4c5eSStephen Hemminger 	spin_unlock(&fib_info_lock);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	return -1;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
352339bf98fSThomas Graf static inline size_t fib_nlmsg_size(struct fib_info *fi)
353339bf98fSThomas Graf {
354339bf98fSThomas Graf 	size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
355339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_TABLE */
356339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_DST */
357339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_PRIORITY */
358339bf98fSThomas Graf 			 + nla_total_size(4); /* RTA_PREFSRC */
359339bf98fSThomas Graf 
360339bf98fSThomas Graf 	/* space for nested metrics */
361339bf98fSThomas Graf 	payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
362339bf98fSThomas Graf 
363339bf98fSThomas Graf 	if (fi->fib_nhs) {
364339bf98fSThomas Graf 		/* Also handles the special case fib_nhs == 1 */
365339bf98fSThomas Graf 
366339bf98fSThomas Graf 		/* each nexthop is packed in an attribute */
367339bf98fSThomas Graf 		size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
368339bf98fSThomas Graf 
369339bf98fSThomas Graf 		/* may contain flow and gateway attribute */
370339bf98fSThomas Graf 		nhsize += 2 * nla_total_size(4);
371339bf98fSThomas Graf 
372339bf98fSThomas Graf 		/* all nexthops are packed in a nested attribute */
373339bf98fSThomas Graf 		payload += nla_total_size(fi->fib_nhs * nhsize);
374339bf98fSThomas Graf 	}
375339bf98fSThomas Graf 
376339bf98fSThomas Graf 	return payload;
377339bf98fSThomas Graf }
378339bf98fSThomas Graf 
37981f7bf6cSAl Viro void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
380b8f55831SMilan Kocian 	       int dst_len, u32 tb_id, struct nl_info *info,
381b8f55831SMilan Kocian 	       unsigned int nlm_flags)
3821da177e4SLinus Torvalds {
3831da177e4SLinus Torvalds 	struct sk_buff *skb;
3844e902c57SThomas Graf 	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
385f21c7bc5SThomas Graf 	int err = -ENOBUFS;
3861da177e4SLinus Torvalds 
387339bf98fSThomas Graf 	skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
388f21c7bc5SThomas Graf 	if (skb == NULL)
389f21c7bc5SThomas Graf 		goto errout;
3901da177e4SLinus Torvalds 
3914e902c57SThomas Graf 	err = fib_dump_info(skb, info->pid, seq, event, tb_id,
39237e826c5SDavid S. Miller 			    fa->fa_type, key, dst_len,
393b8f55831SMilan Kocian 			    fa->fa_tos, fa->fa_info, nlm_flags);
39426932566SPatrick McHardy 	if (err < 0) {
39526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in fib_nlmsg_size() */
39626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
39726932566SPatrick McHardy 		kfree_skb(skb);
39826932566SPatrick McHardy 		goto errout;
39926932566SPatrick McHardy 	}
4001ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
4014e902c57SThomas Graf 		    info->nlh, GFP_KERNEL);
4021ce85fe4SPablo Neira Ayuso 	return;
403f21c7bc5SThomas Graf errout:
404f21c7bc5SThomas Graf 	if (err < 0)
4054d1169c1SDenis V. Lunev 		rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds /* Return the first fib alias matching TOS with
4091da177e4SLinus Torvalds  * priority less than or equal to PRIO.
4101da177e4SLinus Torvalds  */
4111da177e4SLinus Torvalds struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	if (fah) {
4141da177e4SLinus Torvalds 		struct fib_alias *fa;
4151da177e4SLinus Torvalds 		list_for_each_entry(fa, fah, fa_list) {
4161da177e4SLinus Torvalds 			if (fa->fa_tos > tos)
4171da177e4SLinus Torvalds 				continue;
4181da177e4SLinus Torvalds 			if (fa->fa_info->fib_priority >= prio ||
4191da177e4SLinus Torvalds 			    fa->fa_tos < tos)
4201da177e4SLinus Torvalds 				return fa;
4211da177e4SLinus Torvalds 		}
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds 	return NULL;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds int fib_detect_death(struct fib_info *fi, int order,
427c17860a0SDenis V. Lunev 		     struct fib_info **last_resort, int *last_idx, int dflt)
4281da177e4SLinus Torvalds {
4291da177e4SLinus Torvalds 	struct neighbour *n;
4301da177e4SLinus Torvalds 	int state = NUD_NONE;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
4331da177e4SLinus Torvalds 	if (n) {
4341da177e4SLinus Torvalds 		state = n->nud_state;
4351da177e4SLinus Torvalds 		neigh_release(n);
4361da177e4SLinus Torvalds 	}
4371da177e4SLinus Torvalds 	if (state == NUD_REACHABLE)
4381da177e4SLinus Torvalds 		return 0;
439c17860a0SDenis V. Lunev 	if ((state & NUD_VALID) && order != dflt)
4401da177e4SLinus Torvalds 		return 0;
4411da177e4SLinus Torvalds 	if ((state & NUD_VALID) ||
442c17860a0SDenis V. Lunev 	    (*last_idx < 0 && order > dflt)) {
4431da177e4SLinus Torvalds 		*last_resort = fi;
4441da177e4SLinus Torvalds 		*last_idx = order;
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds 	return 1;
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
4501da177e4SLinus Torvalds 
4514e902c57SThomas Graf static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds 	int nhs = 0;
4541da177e4SLinus Torvalds 
4554e902c57SThomas Graf 	while (rtnh_ok(rtnh, remaining)) {
4561da177e4SLinus Torvalds 		nhs++;
4574e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
4581da177e4SLinus Torvalds 	}
4591da177e4SLinus Torvalds 
4604e902c57SThomas Graf 	/* leftover implies invalid nexthop configuration, discard it */
4614e902c57SThomas Graf 	return remaining > 0 ? 0 : nhs;
4624e902c57SThomas Graf }
4631da177e4SLinus Torvalds 
4644e902c57SThomas Graf static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
4654e902c57SThomas Graf 		       int remaining, struct fib_config *cfg)
4664e902c57SThomas Graf {
4671da177e4SLinus Torvalds 	change_nexthops(fi) {
4684e902c57SThomas Graf 		int attrlen;
4694e902c57SThomas Graf 
4704e902c57SThomas Graf 		if (!rtnh_ok(rtnh, remaining))
4711da177e4SLinus Torvalds 			return -EINVAL;
4724e902c57SThomas Graf 
47371fceff0SDavid S. Miller 		nexthop_nh->nh_flags =
47471fceff0SDavid S. Miller 			(cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
47571fceff0SDavid S. Miller 		nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
47671fceff0SDavid S. Miller 		nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
4774e902c57SThomas Graf 
4784e902c57SThomas Graf 		attrlen = rtnh_attrlen(rtnh);
4794e902c57SThomas Graf 		if (attrlen > 0) {
4804e902c57SThomas Graf 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4814e902c57SThomas Graf 
4824e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
48371fceff0SDavid S. Miller 			nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
484c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
4854e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_FLOW);
48671fceff0SDavid S. Miller 			nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
4877a9bc9b8SDavid S. Miller 			if (nexthop_nh->nh_tclassid)
488f4530fa5SDavid S. Miller 				fi->fib_net->ipv4.fib_num_tclassid_users++;
4891da177e4SLinus Torvalds #endif
4901da177e4SLinus Torvalds 		}
4914e902c57SThomas Graf 
4924e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
4931da177e4SLinus Torvalds 	} endfor_nexthops(fi);
4944e902c57SThomas Graf 
4951da177e4SLinus Torvalds 	return 0;
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds #endif
4991da177e4SLinus Torvalds 
5004e902c57SThomas Graf int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
5011da177e4SLinus Torvalds {
5021da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
5034e902c57SThomas Graf 	struct rtnexthop *rtnh;
5044e902c57SThomas Graf 	int remaining;
5051da177e4SLinus Torvalds #endif
5061da177e4SLinus Torvalds 
5074e902c57SThomas Graf 	if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
5081da177e4SLinus Torvalds 		return 1;
5091da177e4SLinus Torvalds 
5104e902c57SThomas Graf 	if (cfg->fc_oif || cfg->fc_gw) {
5114e902c57SThomas Graf 		if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
5124e902c57SThomas Graf 		    (!cfg->fc_gw  || cfg->fc_gw == fi->fib_nh->nh_gw))
5131da177e4SLinus Torvalds 			return 0;
5141da177e4SLinus Torvalds 		return 1;
5151da177e4SLinus Torvalds 	}
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
5184e902c57SThomas Graf 	if (cfg->fc_mp == NULL)
5191da177e4SLinus Torvalds 		return 0;
5204e902c57SThomas Graf 
5214e902c57SThomas Graf 	rtnh = cfg->fc_mp;
5224e902c57SThomas Graf 	remaining = cfg->fc_mp_len;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	for_nexthops(fi) {
5254e902c57SThomas Graf 		int attrlen;
5261da177e4SLinus Torvalds 
5274e902c57SThomas Graf 		if (!rtnh_ok(rtnh, remaining))
5281da177e4SLinus Torvalds 			return -EINVAL;
5294e902c57SThomas Graf 
5304e902c57SThomas Graf 		if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
5311da177e4SLinus Torvalds 			return 1;
5324e902c57SThomas Graf 
5334e902c57SThomas Graf 		attrlen = rtnh_attrlen(rtnh);
5344e902c57SThomas Graf 		if (attrlen < 0) {
5354e902c57SThomas Graf 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5364e902c57SThomas Graf 
5374e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
53817fb2c64SAl Viro 			if (nla && nla_get_be32(nla) != nh->nh_gw)
5391da177e4SLinus Torvalds 				return 1;
540c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
5414e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_FLOW);
5424e902c57SThomas Graf 			if (nla && nla_get_u32(nla) != nh->nh_tclassid)
5431da177e4SLinus Torvalds 				return 1;
5441da177e4SLinus Torvalds #endif
5451da177e4SLinus Torvalds 		}
5464e902c57SThomas Graf 
5474e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
5481da177e4SLinus Torvalds 	} endfor_nexthops(fi);
5491da177e4SLinus Torvalds #endif
5501da177e4SLinus Torvalds 	return 0;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds /*
5556a31d2a9SEric Dumazet  * Picture
5566a31d2a9SEric Dumazet  * -------
5576a31d2a9SEric Dumazet  *
5586a31d2a9SEric Dumazet  * Semantics of nexthop is very messy by historical reasons.
5596a31d2a9SEric Dumazet  * We have to take into account, that:
5606a31d2a9SEric Dumazet  * a) gateway can be actually local interface address,
5616a31d2a9SEric Dumazet  *    so that gatewayed route is direct.
5626a31d2a9SEric Dumazet  * b) gateway must be on-link address, possibly
5636a31d2a9SEric Dumazet  *    described not by an ifaddr, but also by a direct route.
5646a31d2a9SEric Dumazet  * c) If both gateway and interface are specified, they should not
5656a31d2a9SEric Dumazet  *    contradict.
5666a31d2a9SEric Dumazet  * d) If we use tunnel routes, gateway could be not on-link.
5676a31d2a9SEric Dumazet  *
5686a31d2a9SEric Dumazet  * Attempt to reconcile all of these (alas, self-contradictory) conditions
5696a31d2a9SEric Dumazet  * results in pretty ugly and hairy code with obscure logic.
5706a31d2a9SEric Dumazet  *
5716a31d2a9SEric Dumazet  * I chose to generalized it instead, so that the size
5726a31d2a9SEric Dumazet  * of code does not increase practically, but it becomes
5736a31d2a9SEric Dumazet  * much more general.
5746a31d2a9SEric Dumazet  * Every prefix is assigned a "scope" value: "host" is local address,
5756a31d2a9SEric Dumazet  * "link" is direct route,
5766a31d2a9SEric Dumazet  * [ ... "site" ... "interior" ... ]
5776a31d2a9SEric Dumazet  * and "universe" is true gateway route with global meaning.
5786a31d2a9SEric Dumazet  *
5796a31d2a9SEric Dumazet  * Every prefix refers to a set of "nexthop"s (gw, oif),
5806a31d2a9SEric Dumazet  * where gw must have narrower scope. This recursion stops
5816a31d2a9SEric Dumazet  * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
5826a31d2a9SEric Dumazet  * which means that gw is forced to be on link.
5836a31d2a9SEric Dumazet  *
5846a31d2a9SEric Dumazet  * Code is still hairy, but now it is apparently logically
5856a31d2a9SEric Dumazet  * consistent and very flexible. F.e. as by-product it allows
5866a31d2a9SEric Dumazet  * to co-exists in peace independent exterior and interior
5876a31d2a9SEric Dumazet  * routing processes.
5886a31d2a9SEric Dumazet  *
5896a31d2a9SEric Dumazet  * Normally it looks as following.
5906a31d2a9SEric Dumazet  *
5916a31d2a9SEric Dumazet  * {universe prefix}  -> (gw, oif) [scope link]
5926a31d2a9SEric Dumazet  *		  |
5936a31d2a9SEric Dumazet  *		  |-> {link prefix} -> (gw, oif) [scope local]
5946a31d2a9SEric Dumazet  *					|
5956a31d2a9SEric Dumazet  *					|-> {local prefix} (terminal node)
5961da177e4SLinus Torvalds  */
5974e902c57SThomas Graf static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
5984e902c57SThomas Graf 			struct fib_nh *nh)
5991da177e4SLinus Torvalds {
6001da177e4SLinus Torvalds 	int err;
60186167a37SDenis V. Lunev 	struct net *net;
6026a31d2a9SEric Dumazet 	struct net_device *dev;
6031da177e4SLinus Torvalds 
60486167a37SDenis V. Lunev 	net = cfg->fc_nlinfo.nl_net;
6051da177e4SLinus Torvalds 	if (nh->nh_gw) {
6061da177e4SLinus Torvalds 		struct fib_result res;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 		if (nh->nh_flags & RTNH_F_ONLINK) {
6091da177e4SLinus Torvalds 
6104e902c57SThomas Graf 			if (cfg->fc_scope >= RT_SCOPE_LINK)
6111da177e4SLinus Torvalds 				return -EINVAL;
61286167a37SDenis V. Lunev 			if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
6131da177e4SLinus Torvalds 				return -EINVAL;
6146a31d2a9SEric Dumazet 			dev = __dev_get_by_index(net, nh->nh_oif);
6156a31d2a9SEric Dumazet 			if (!dev)
6161da177e4SLinus Torvalds 				return -ENODEV;
6171da177e4SLinus Torvalds 			if (!(dev->flags & IFF_UP))
6181da177e4SLinus Torvalds 				return -ENETDOWN;
6191da177e4SLinus Torvalds 			nh->nh_dev = dev;
6201da177e4SLinus Torvalds 			dev_hold(dev);
6211da177e4SLinus Torvalds 			nh->nh_scope = RT_SCOPE_LINK;
6221da177e4SLinus Torvalds 			return 0;
6231da177e4SLinus Torvalds 		}
624ebc0ffaeSEric Dumazet 		rcu_read_lock();
6251da177e4SLinus Torvalds 		{
6269ade2286SDavid S. Miller 			struct flowi4 fl4 = {
6279ade2286SDavid S. Miller 				.daddr = nh->nh_gw,
6289ade2286SDavid S. Miller 				.flowi4_scope = cfg->fc_scope + 1,
6299ade2286SDavid S. Miller 				.flowi4_oif = nh->nh_oif,
6304e902c57SThomas Graf 			};
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 			/* It is not necessary, but requires a bit of thinking */
6339ade2286SDavid S. Miller 			if (fl4.flowi4_scope < RT_SCOPE_LINK)
6349ade2286SDavid S. Miller 				fl4.flowi4_scope = RT_SCOPE_LINK;
6359ade2286SDavid S. Miller 			err = fib_lookup(net, &fl4, &res);
636ebc0ffaeSEric Dumazet 			if (err) {
637ebc0ffaeSEric Dumazet 				rcu_read_unlock();
6381da177e4SLinus Torvalds 				return err;
6391da177e4SLinus Torvalds 			}
640ebc0ffaeSEric Dumazet 		}
6411da177e4SLinus Torvalds 		err = -EINVAL;
6421da177e4SLinus Torvalds 		if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
6431da177e4SLinus Torvalds 			goto out;
6441da177e4SLinus Torvalds 		nh->nh_scope = res.scope;
6451da177e4SLinus Torvalds 		nh->nh_oif = FIB_RES_OIF(res);
6466a31d2a9SEric Dumazet 		nh->nh_dev = dev = FIB_RES_DEV(res);
6476a31d2a9SEric Dumazet 		if (!dev)
6481da177e4SLinus Torvalds 			goto out;
6496a31d2a9SEric Dumazet 		dev_hold(dev);
6508723e1b4SEric Dumazet 		err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
6511da177e4SLinus Torvalds 	} else {
6521da177e4SLinus Torvalds 		struct in_device *in_dev;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 		if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
6551da177e4SLinus Torvalds 			return -EINVAL;
6561da177e4SLinus Torvalds 
6578723e1b4SEric Dumazet 		rcu_read_lock();
6588723e1b4SEric Dumazet 		err = -ENODEV;
65986167a37SDenis V. Lunev 		in_dev = inetdev_by_index(net, nh->nh_oif);
6601da177e4SLinus Torvalds 		if (in_dev == NULL)
6618723e1b4SEric Dumazet 			goto out;
6628723e1b4SEric Dumazet 		err = -ENETDOWN;
6638723e1b4SEric Dumazet 		if (!(in_dev->dev->flags & IFF_UP))
6648723e1b4SEric Dumazet 			goto out;
6651da177e4SLinus Torvalds 		nh->nh_dev = in_dev->dev;
6661da177e4SLinus Torvalds 		dev_hold(nh->nh_dev);
6671da177e4SLinus Torvalds 		nh->nh_scope = RT_SCOPE_HOST;
6688723e1b4SEric Dumazet 		err = 0;
6691da177e4SLinus Torvalds 	}
6708723e1b4SEric Dumazet out:
6718723e1b4SEric Dumazet 	rcu_read_unlock();
6728723e1b4SEric Dumazet 	return err;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
67581f7bf6cSAl Viro static inline unsigned int fib_laddr_hashfn(__be32 val)
6761da177e4SLinus Torvalds {
677123b9731SDavid S. Miller 	unsigned int mask = (fib_info_hash_size - 1);
6781da177e4SLinus Torvalds 
6796a31d2a9SEric Dumazet 	return ((__force u32)val ^
6806a31d2a9SEric Dumazet 		((__force u32)val >> 7) ^
6816a31d2a9SEric Dumazet 		((__force u32)val >> 14)) & mask;
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds 
684123b9731SDavid S. Miller static struct hlist_head *fib_info_hash_alloc(int bytes)
6851da177e4SLinus Torvalds {
6861da177e4SLinus Torvalds 	if (bytes <= PAGE_SIZE)
68788f83491SJoonwoo Park 		return kzalloc(bytes, GFP_KERNEL);
6881da177e4SLinus Torvalds 	else
6891da177e4SLinus Torvalds 		return (struct hlist_head *)
6906a31d2a9SEric Dumazet 			__get_free_pages(GFP_KERNEL | __GFP_ZERO,
6916a31d2a9SEric Dumazet 					 get_order(bytes));
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
694123b9731SDavid S. Miller static void fib_info_hash_free(struct hlist_head *hash, int bytes)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	if (!hash)
6971da177e4SLinus Torvalds 		return;
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	if (bytes <= PAGE_SIZE)
7001da177e4SLinus Torvalds 		kfree(hash);
7011da177e4SLinus Torvalds 	else
7021da177e4SLinus Torvalds 		free_pages((unsigned long) hash, get_order(bytes));
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
705123b9731SDavid S. Miller static void fib_info_hash_move(struct hlist_head *new_info_hash,
7061da177e4SLinus Torvalds 			       struct hlist_head *new_laddrhash,
7071da177e4SLinus Torvalds 			       unsigned int new_size)
7081da177e4SLinus Torvalds {
709b7656e7fSDavid S. Miller 	struct hlist_head *old_info_hash, *old_laddrhash;
710123b9731SDavid S. Miller 	unsigned int old_size = fib_info_hash_size;
711b7656e7fSDavid S. Miller 	unsigned int i, bytes;
7121da177e4SLinus Torvalds 
713832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
714b7656e7fSDavid S. Miller 	old_info_hash = fib_info_hash;
715b7656e7fSDavid S. Miller 	old_laddrhash = fib_info_laddrhash;
716123b9731SDavid S. Miller 	fib_info_hash_size = new_size;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	for (i = 0; i < old_size; i++) {
7191da177e4SLinus Torvalds 		struct hlist_head *head = &fib_info_hash[i];
7201da177e4SLinus Torvalds 		struct hlist_node *node, *n;
7211da177e4SLinus Torvalds 		struct fib_info *fi;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 		hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
7241da177e4SLinus Torvalds 			struct hlist_head *dest;
7251da177e4SLinus Torvalds 			unsigned int new_hash;
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 			hlist_del(&fi->fib_hash);
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 			new_hash = fib_info_hashfn(fi);
7301da177e4SLinus Torvalds 			dest = &new_info_hash[new_hash];
7311da177e4SLinus Torvalds 			hlist_add_head(&fi->fib_hash, dest);
7321da177e4SLinus Torvalds 		}
7331da177e4SLinus Torvalds 	}
7341da177e4SLinus Torvalds 	fib_info_hash = new_info_hash;
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	for (i = 0; i < old_size; i++) {
7371da177e4SLinus Torvalds 		struct hlist_head *lhead = &fib_info_laddrhash[i];
7381da177e4SLinus Torvalds 		struct hlist_node *node, *n;
7391da177e4SLinus Torvalds 		struct fib_info *fi;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 		hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
7421da177e4SLinus Torvalds 			struct hlist_head *ldest;
7431da177e4SLinus Torvalds 			unsigned int new_hash;
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 			hlist_del(&fi->fib_lhash);
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 			new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
7481da177e4SLinus Torvalds 			ldest = &new_laddrhash[new_hash];
7491da177e4SLinus Torvalds 			hlist_add_head(&fi->fib_lhash, ldest);
7501da177e4SLinus Torvalds 		}
7511da177e4SLinus Torvalds 	}
7521da177e4SLinus Torvalds 	fib_info_laddrhash = new_laddrhash;
7531da177e4SLinus Torvalds 
754832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
755b7656e7fSDavid S. Miller 
756b7656e7fSDavid S. Miller 	bytes = old_size * sizeof(struct hlist_head *);
757123b9731SDavid S. Miller 	fib_info_hash_free(old_info_hash, bytes);
758123b9731SDavid S. Miller 	fib_info_hash_free(old_laddrhash, bytes);
7591da177e4SLinus Torvalds }
7601da177e4SLinus Torvalds 
761436c3b66SDavid S. Miller __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
762436c3b66SDavid S. Miller {
763436c3b66SDavid S. Miller 	nh->nh_saddr = inet_select_addr(nh->nh_dev,
764436c3b66SDavid S. Miller 					nh->nh_gw,
76537e826c5SDavid S. Miller 					nh->nh_parent->fib_scope);
766436c3b66SDavid S. Miller 	nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
767436c3b66SDavid S. Miller 
768436c3b66SDavid S. Miller 	return nh->nh_saddr;
769436c3b66SDavid S. Miller }
770436c3b66SDavid S. Miller 
7714e902c57SThomas Graf struct fib_info *fib_create_info(struct fib_config *cfg)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds 	int err;
7741da177e4SLinus Torvalds 	struct fib_info *fi = NULL;
7751da177e4SLinus Torvalds 	struct fib_info *ofi;
7761da177e4SLinus Torvalds 	int nhs = 1;
7777462bd74SDenis V. Lunev 	struct net *net = cfg->fc_nlinfo.nl_net;
7781da177e4SLinus Torvalds 
7794c8237cdSDavid S. Miller 	if (cfg->fc_type > RTN_MAX)
7804c8237cdSDavid S. Miller 		goto err_inval;
7814c8237cdSDavid S. Miller 
7821da177e4SLinus Torvalds 	/* Fast check to catch the most weird cases */
7834e902c57SThomas Graf 	if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
7841da177e4SLinus Torvalds 		goto err_inval;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
7874e902c57SThomas Graf 	if (cfg->fc_mp) {
7884e902c57SThomas Graf 		nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
7891da177e4SLinus Torvalds 		if (nhs == 0)
7901da177e4SLinus Torvalds 			goto err_inval;
7911da177e4SLinus Torvalds 	}
7921da177e4SLinus Torvalds #endif
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	err = -ENOBUFS;
795123b9731SDavid S. Miller 	if (fib_info_cnt >= fib_info_hash_size) {
796123b9731SDavid S. Miller 		unsigned int new_size = fib_info_hash_size << 1;
7971da177e4SLinus Torvalds 		struct hlist_head *new_info_hash;
7981da177e4SLinus Torvalds 		struct hlist_head *new_laddrhash;
7991da177e4SLinus Torvalds 		unsigned int bytes;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 		if (!new_size)
8021da177e4SLinus Torvalds 			new_size = 1;
8031da177e4SLinus Torvalds 		bytes = new_size * sizeof(struct hlist_head *);
804123b9731SDavid S. Miller 		new_info_hash = fib_info_hash_alloc(bytes);
805123b9731SDavid S. Miller 		new_laddrhash = fib_info_hash_alloc(bytes);
8061da177e4SLinus Torvalds 		if (!new_info_hash || !new_laddrhash) {
807123b9731SDavid S. Miller 			fib_info_hash_free(new_info_hash, bytes);
808123b9731SDavid S. Miller 			fib_info_hash_free(new_laddrhash, bytes);
80988f83491SJoonwoo Park 		} else
810123b9731SDavid S. Miller 			fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
8111da177e4SLinus Torvalds 
812123b9731SDavid S. Miller 		if (!fib_info_hash_size)
8131da177e4SLinus Torvalds 			goto failure;
8141da177e4SLinus Torvalds 	}
8151da177e4SLinus Torvalds 
8160da974f4SPanagiotis Issaris 	fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
8171da177e4SLinus Torvalds 	if (fi == NULL)
8181da177e4SLinus Torvalds 		goto failure;
819725d1e1bSDavid S. Miller 	if (cfg->fc_mx) {
8209c150e82SDavid S. Miller 		fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
8219c150e82SDavid S. Miller 		if (!fi->fib_metrics)
8229c150e82SDavid S. Miller 			goto failure;
823725d1e1bSDavid S. Miller 	} else
824725d1e1bSDavid S. Miller 		fi->fib_metrics = (u32 *) dst_default_metrics;
8251da177e4SLinus Torvalds 	fib_info_cnt++;
8261da177e4SLinus Torvalds 
82757d7a600SDenis V. Lunev 	fi->fib_net = hold_net(net);
8284e902c57SThomas Graf 	fi->fib_protocol = cfg->fc_protocol;
82937e826c5SDavid S. Miller 	fi->fib_scope = cfg->fc_scope;
8304e902c57SThomas Graf 	fi->fib_flags = cfg->fc_flags;
8314e902c57SThomas Graf 	fi->fib_priority = cfg->fc_priority;
8324e902c57SThomas Graf 	fi->fib_prefsrc = cfg->fc_prefsrc;
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	fi->fib_nhs = nhs;
8351da177e4SLinus Torvalds 	change_nexthops(fi) {
83671fceff0SDavid S. Miller 		nexthop_nh->nh_parent = fi;
837*d26b3a7cSEric Dumazet 		nexthop_nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *);
8381da177e4SLinus Torvalds 	} endfor_nexthops(fi)
8391da177e4SLinus Torvalds 
8404e902c57SThomas Graf 	if (cfg->fc_mx) {
8414e902c57SThomas Graf 		struct nlattr *nla;
8424e902c57SThomas Graf 		int remaining;
8431da177e4SLinus Torvalds 
8444e902c57SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
8458f4c1f9bSThomas Graf 			int type = nla_type(nla);
8464e902c57SThomas Graf 
8474e902c57SThomas Graf 			if (type) {
8486fac2625SDavid S. Miller 				u32 val;
8496fac2625SDavid S. Miller 
8504e902c57SThomas Graf 				if (type > RTAX_MAX)
8511da177e4SLinus Torvalds 					goto err_inval;
8526fac2625SDavid S. Miller 				val = nla_get_u32(nla);
8536fac2625SDavid S. Miller 				if (type == RTAX_ADVMSS && val > 65535 - 40)
8546fac2625SDavid S. Miller 					val = 65535 - 40;
855710ab6c0SDavid S. Miller 				if (type == RTAX_MTU && val > 65535 - 15)
856710ab6c0SDavid S. Miller 					val = 65535 - 15;
8576fac2625SDavid S. Miller 				fi->fib_metrics[type - 1] = val;
8581da177e4SLinus Torvalds 			}
8591da177e4SLinus Torvalds 		}
8604e902c57SThomas Graf 	}
8611da177e4SLinus Torvalds 
8624e902c57SThomas Graf 	if (cfg->fc_mp) {
8631da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
8644e902c57SThomas Graf 		err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
8654e902c57SThomas Graf 		if (err != 0)
8661da177e4SLinus Torvalds 			goto failure;
8674e902c57SThomas Graf 		if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
8681da177e4SLinus Torvalds 			goto err_inval;
8694e902c57SThomas Graf 		if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
8701da177e4SLinus Torvalds 			goto err_inval;
871c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
8724e902c57SThomas Graf 		if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
8731da177e4SLinus Torvalds 			goto err_inval;
8741da177e4SLinus Torvalds #endif
8751da177e4SLinus Torvalds #else
8761da177e4SLinus Torvalds 		goto err_inval;
8771da177e4SLinus Torvalds #endif
8781da177e4SLinus Torvalds 	} else {
8791da177e4SLinus Torvalds 		struct fib_nh *nh = fi->fib_nh;
8804e902c57SThomas Graf 
8814e902c57SThomas Graf 		nh->nh_oif = cfg->fc_oif;
8824e902c57SThomas Graf 		nh->nh_gw = cfg->fc_gw;
8834e902c57SThomas Graf 		nh->nh_flags = cfg->fc_flags;
884c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
8854e902c57SThomas Graf 		nh->nh_tclassid = cfg->fc_flow;
8867a9bc9b8SDavid S. Miller 		if (nh->nh_tclassid)
887f4530fa5SDavid S. Miller 			fi->fib_net->ipv4.fib_num_tclassid_users++;
8881da177e4SLinus Torvalds #endif
8891da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
8901da177e4SLinus Torvalds 		nh->nh_weight = 1;
8911da177e4SLinus Torvalds #endif
8921da177e4SLinus Torvalds 	}
8931da177e4SLinus Torvalds 
8944e902c57SThomas Graf 	if (fib_props[cfg->fc_type].error) {
8954e902c57SThomas Graf 		if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
8961da177e4SLinus Torvalds 			goto err_inval;
8971da177e4SLinus Torvalds 		goto link_it;
8984c8237cdSDavid S. Miller 	} else {
8994c8237cdSDavid S. Miller 		switch (cfg->fc_type) {
9004c8237cdSDavid S. Miller 		case RTN_UNICAST:
9014c8237cdSDavid S. Miller 		case RTN_LOCAL:
9024c8237cdSDavid S. Miller 		case RTN_BROADCAST:
9034c8237cdSDavid S. Miller 		case RTN_ANYCAST:
9044c8237cdSDavid S. Miller 		case RTN_MULTICAST:
9054c8237cdSDavid S. Miller 			break;
9064c8237cdSDavid S. Miller 		default:
9074c8237cdSDavid S. Miller 			goto err_inval;
9084c8237cdSDavid S. Miller 		}
9091da177e4SLinus Torvalds 	}
9101da177e4SLinus Torvalds 
9114e902c57SThomas Graf 	if (cfg->fc_scope > RT_SCOPE_HOST)
9121da177e4SLinus Torvalds 		goto err_inval;
9131da177e4SLinus Torvalds 
9144e902c57SThomas Graf 	if (cfg->fc_scope == RT_SCOPE_HOST) {
9151da177e4SLinus Torvalds 		struct fib_nh *nh = fi->fib_nh;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 		/* Local address is added. */
9181da177e4SLinus Torvalds 		if (nhs != 1 || nh->nh_gw)
9191da177e4SLinus Torvalds 			goto err_inval;
9201da177e4SLinus Torvalds 		nh->nh_scope = RT_SCOPE_NOWHERE;
9217462bd74SDenis V. Lunev 		nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
9221da177e4SLinus Torvalds 		err = -ENODEV;
9231da177e4SLinus Torvalds 		if (nh->nh_dev == NULL)
9241da177e4SLinus Torvalds 			goto failure;
9251da177e4SLinus Torvalds 	} else {
9261da177e4SLinus Torvalds 		change_nexthops(fi) {
9276a31d2a9SEric Dumazet 			err = fib_check_nh(cfg, fi, nexthop_nh);
9286a31d2a9SEric Dumazet 			if (err != 0)
9291da177e4SLinus Torvalds 				goto failure;
9301da177e4SLinus Torvalds 		} endfor_nexthops(fi)
9311da177e4SLinus Torvalds 	}
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	if (fi->fib_prefsrc) {
9344e902c57SThomas Graf 		if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
9354e902c57SThomas Graf 		    fi->fib_prefsrc != cfg->fc_dst)
9367462bd74SDenis V. Lunev 			if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
9371da177e4SLinus Torvalds 				goto err_inval;
9381da177e4SLinus Torvalds 	}
9391da177e4SLinus Torvalds 
9401fc050a1SDavid S. Miller 	change_nexthops(fi) {
941436c3b66SDavid S. Miller 		fib_info_update_nh_saddr(net, nexthop_nh);
9421fc050a1SDavid S. Miller 	} endfor_nexthops(fi)
9431fc050a1SDavid S. Miller 
9441da177e4SLinus Torvalds link_it:
9456a31d2a9SEric Dumazet 	ofi = fib_find_info(fi);
9466a31d2a9SEric Dumazet 	if (ofi) {
9471da177e4SLinus Torvalds 		fi->fib_dead = 1;
9481da177e4SLinus Torvalds 		free_fib_info(fi);
9491da177e4SLinus Torvalds 		ofi->fib_treeref++;
9501da177e4SLinus Torvalds 		return ofi;
9511da177e4SLinus Torvalds 	}
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	fi->fib_treeref++;
9541da177e4SLinus Torvalds 	atomic_inc(&fi->fib_clntref);
955832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
9561da177e4SLinus Torvalds 	hlist_add_head(&fi->fib_hash,
9571da177e4SLinus Torvalds 		       &fib_info_hash[fib_info_hashfn(fi)]);
9581da177e4SLinus Torvalds 	if (fi->fib_prefsrc) {
9591da177e4SLinus Torvalds 		struct hlist_head *head;
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 		head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
9621da177e4SLinus Torvalds 		hlist_add_head(&fi->fib_lhash, head);
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds 	change_nexthops(fi) {
9651da177e4SLinus Torvalds 		struct hlist_head *head;
9661da177e4SLinus Torvalds 		unsigned int hash;
9671da177e4SLinus Torvalds 
96871fceff0SDavid S. Miller 		if (!nexthop_nh->nh_dev)
9691da177e4SLinus Torvalds 			continue;
97071fceff0SDavid S. Miller 		hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
9711da177e4SLinus Torvalds 		head = &fib_info_devhash[hash];
97271fceff0SDavid S. Miller 		hlist_add_head(&nexthop_nh->nh_hash, head);
9731da177e4SLinus Torvalds 	} endfor_nexthops(fi)
974832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
9751da177e4SLinus Torvalds 	return fi;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds err_inval:
9781da177e4SLinus Torvalds 	err = -EINVAL;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds failure:
9811da177e4SLinus Torvalds 	if (fi) {
9821da177e4SLinus Torvalds 		fi->fib_dead = 1;
9831da177e4SLinus Torvalds 		free_fib_info(fi);
9841da177e4SLinus Torvalds 	}
9854e902c57SThomas Graf 
9864e902c57SThomas Graf 	return ERR_PTR(err);
9871da177e4SLinus Torvalds }
9881da177e4SLinus Torvalds 
989be403ea1SThomas Graf int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
99037e826c5SDavid S. Miller 		  u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
991b6544c0bSJamal Hadi Salim 		  struct fib_info *fi, unsigned int flags)
9921da177e4SLinus Torvalds {
9931da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
994be403ea1SThomas Graf 	struct rtmsg *rtm;
9951da177e4SLinus Torvalds 
996be403ea1SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
997be403ea1SThomas Graf 	if (nlh == NULL)
99826932566SPatrick McHardy 		return -EMSGSIZE;
999be403ea1SThomas Graf 
1000be403ea1SThomas Graf 	rtm = nlmsg_data(nlh);
10011da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET;
10021da177e4SLinus Torvalds 	rtm->rtm_dst_len = dst_len;
10031da177e4SLinus Torvalds 	rtm->rtm_src_len = 0;
10041da177e4SLinus Torvalds 	rtm->rtm_tos = tos;
1005709772e6SKrzysztof Piotr Oledzki 	if (tb_id < 256)
10061da177e4SLinus Torvalds 		rtm->rtm_table = tb_id;
1007709772e6SKrzysztof Piotr Oledzki 	else
1008709772e6SKrzysztof Piotr Oledzki 		rtm->rtm_table = RT_TABLE_COMPAT;
1009f3756b79SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, tb_id))
1010f3756b79SDavid S. Miller 		goto nla_put_failure;
10111da177e4SLinus Torvalds 	rtm->rtm_type = type;
10121da177e4SLinus Torvalds 	rtm->rtm_flags = fi->fib_flags;
101337e826c5SDavid S. Miller 	rtm->rtm_scope = fi->fib_scope;
10141da177e4SLinus Torvalds 	rtm->rtm_protocol = fi->fib_protocol;
1015be403ea1SThomas Graf 
1016f3756b79SDavid S. Miller 	if (rtm->rtm_dst_len &&
1017f3756b79SDavid S. Miller 	    nla_put_be32(skb, RTA_DST, dst))
1018f3756b79SDavid S. Miller 		goto nla_put_failure;
1019f3756b79SDavid S. Miller 	if (fi->fib_priority &&
1020f3756b79SDavid S. Miller 	    nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1021f3756b79SDavid S. Miller 		goto nla_put_failure;
10221da177e4SLinus Torvalds 	if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
1023be403ea1SThomas Graf 		goto nla_put_failure;
1024be403ea1SThomas Graf 
1025f3756b79SDavid S. Miller 	if (fi->fib_prefsrc &&
1026f3756b79SDavid S. Miller 	    nla_put_be32(skb, RTA_PREFSRC, fi->fib_prefsrc))
1027f3756b79SDavid S. Miller 		goto nla_put_failure;
10281da177e4SLinus Torvalds 	if (fi->fib_nhs == 1) {
1029f3756b79SDavid S. Miller 		if (fi->fib_nh->nh_gw &&
1030f3756b79SDavid S. Miller 		    nla_put_be32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
1031f3756b79SDavid S. Miller 			goto nla_put_failure;
1032f3756b79SDavid S. Miller 		if (fi->fib_nh->nh_oif &&
1033f3756b79SDavid S. Miller 		    nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
1034f3756b79SDavid S. Miller 			goto nla_put_failure;
1035c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1036f3756b79SDavid S. Miller 		if (fi->fib_nh[0].nh_tclassid &&
1037f3756b79SDavid S. Miller 		    nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
1038f3756b79SDavid S. Miller 			goto nla_put_failure;
10398265abc0SPatrick McHardy #endif
10401da177e4SLinus Torvalds 	}
10411da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
10421da177e4SLinus Torvalds 	if (fi->fib_nhs > 1) {
1043be403ea1SThomas Graf 		struct rtnexthop *rtnh;
1044be403ea1SThomas Graf 		struct nlattr *mp;
1045be403ea1SThomas Graf 
1046be403ea1SThomas Graf 		mp = nla_nest_start(skb, RTA_MULTIPATH);
1047be403ea1SThomas Graf 		if (mp == NULL)
1048be403ea1SThomas Graf 			goto nla_put_failure;
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 		for_nexthops(fi) {
1051be403ea1SThomas Graf 			rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1052be403ea1SThomas Graf 			if (rtnh == NULL)
1053be403ea1SThomas Graf 				goto nla_put_failure;
1054be403ea1SThomas Graf 
1055be403ea1SThomas Graf 			rtnh->rtnh_flags = nh->nh_flags & 0xFF;
1056be403ea1SThomas Graf 			rtnh->rtnh_hops = nh->nh_weight - 1;
1057be403ea1SThomas Graf 			rtnh->rtnh_ifindex = nh->nh_oif;
1058be403ea1SThomas Graf 
1059f3756b79SDavid S. Miller 			if (nh->nh_gw &&
1060f3756b79SDavid S. Miller 			    nla_put_be32(skb, RTA_GATEWAY, nh->nh_gw))
1061f3756b79SDavid S. Miller 				goto nla_put_failure;
1062c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1063f3756b79SDavid S. Miller 			if (nh->nh_tclassid &&
1064f3756b79SDavid S. Miller 			    nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1065f3756b79SDavid S. Miller 				goto nla_put_failure;
10668265abc0SPatrick McHardy #endif
1067be403ea1SThomas Graf 			/* length of rtnetlink header + attributes */
1068be403ea1SThomas Graf 			rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
10691da177e4SLinus Torvalds 		} endfor_nexthops(fi);
1070be403ea1SThomas Graf 
1071be403ea1SThomas Graf 		nla_nest_end(skb, mp);
10721da177e4SLinus Torvalds 	}
10731da177e4SLinus Torvalds #endif
1074be403ea1SThomas Graf 	return nlmsg_end(skb, nlh);
10751da177e4SLinus Torvalds 
1076be403ea1SThomas Graf nla_put_failure:
107726932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
107826932566SPatrick McHardy 	return -EMSGSIZE;
10791da177e4SLinus Torvalds }
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds /*
10826a31d2a9SEric Dumazet  * Update FIB if:
10836a31d2a9SEric Dumazet  * - local address disappeared -> we must delete all the entries
10846a31d2a9SEric Dumazet  *   referring to it.
10856a31d2a9SEric Dumazet  * - device went down -> we must shutdown all nexthops going via it.
10861da177e4SLinus Torvalds  */
10874814bdbdSDenis V. Lunev int fib_sync_down_addr(struct net *net, __be32 local)
10881da177e4SLinus Torvalds {
10891da177e4SLinus Torvalds 	int ret = 0;
10901da177e4SLinus Torvalds 	unsigned int hash = fib_laddr_hashfn(local);
10911da177e4SLinus Torvalds 	struct hlist_head *head = &fib_info_laddrhash[hash];
10921da177e4SLinus Torvalds 	struct hlist_node *node;
10931da177e4SLinus Torvalds 	struct fib_info *fi;
10941da177e4SLinus Torvalds 
109585326fa5SDenis V. Lunev 	if (fib_info_laddrhash == NULL || local == 0)
109685326fa5SDenis V. Lunev 		return 0;
109785326fa5SDenis V. Lunev 
10981da177e4SLinus Torvalds 	hlist_for_each_entry(fi, node, head, fib_lhash) {
109909ad9bc7SOctavian Purdila 		if (!net_eq(fi->fib_net, net))
11004814bdbdSDenis V. Lunev 			continue;
11011da177e4SLinus Torvalds 		if (fi->fib_prefsrc == local) {
11021da177e4SLinus Torvalds 			fi->fib_flags |= RTNH_F_DEAD;
11031da177e4SLinus Torvalds 			ret++;
11041da177e4SLinus Torvalds 		}
11051da177e4SLinus Torvalds 	}
110685326fa5SDenis V. Lunev 	return ret;
11071da177e4SLinus Torvalds }
11081da177e4SLinus Torvalds 
110985326fa5SDenis V. Lunev int fib_sync_down_dev(struct net_device *dev, int force)
111085326fa5SDenis V. Lunev {
111185326fa5SDenis V. Lunev 	int ret = 0;
111285326fa5SDenis V. Lunev 	int scope = RT_SCOPE_NOWHERE;
11131da177e4SLinus Torvalds 	struct fib_info *prev_fi = NULL;
11141da177e4SLinus Torvalds 	unsigned int hash = fib_devindex_hashfn(dev->ifindex);
11151da177e4SLinus Torvalds 	struct hlist_head *head = &fib_info_devhash[hash];
11161da177e4SLinus Torvalds 	struct hlist_node *node;
11171da177e4SLinus Torvalds 	struct fib_nh *nh;
11181da177e4SLinus Torvalds 
111985326fa5SDenis V. Lunev 	if (force)
112085326fa5SDenis V. Lunev 		scope = -1;
112185326fa5SDenis V. Lunev 
11221da177e4SLinus Torvalds 	hlist_for_each_entry(nh, node, head, nh_hash) {
11231da177e4SLinus Torvalds 		struct fib_info *fi = nh->nh_parent;
11241da177e4SLinus Torvalds 		int dead;
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 		BUG_ON(!fi->fib_nhs);
11271da177e4SLinus Torvalds 		if (nh->nh_dev != dev || fi == prev_fi)
11281da177e4SLinus Torvalds 			continue;
11291da177e4SLinus Torvalds 		prev_fi = fi;
11301da177e4SLinus Torvalds 		dead = 0;
11311da177e4SLinus Torvalds 		change_nexthops(fi) {
113271fceff0SDavid S. Miller 			if (nexthop_nh->nh_flags & RTNH_F_DEAD)
11331da177e4SLinus Torvalds 				dead++;
113471fceff0SDavid S. Miller 			else if (nexthop_nh->nh_dev == dev &&
113571fceff0SDavid S. Miller 				 nexthop_nh->nh_scope != scope) {
113671fceff0SDavid S. Miller 				nexthop_nh->nh_flags |= RTNH_F_DEAD;
11371da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
11381da177e4SLinus Torvalds 				spin_lock_bh(&fib_multipath_lock);
113971fceff0SDavid S. Miller 				fi->fib_power -= nexthop_nh->nh_power;
114071fceff0SDavid S. Miller 				nexthop_nh->nh_power = 0;
11411da177e4SLinus Torvalds 				spin_unlock_bh(&fib_multipath_lock);
11421da177e4SLinus Torvalds #endif
11431da177e4SLinus Torvalds 				dead++;
11441da177e4SLinus Torvalds 			}
11451da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
114671fceff0SDavid S. Miller 			if (force > 1 && nexthop_nh->nh_dev == dev) {
11471da177e4SLinus Torvalds 				dead = fi->fib_nhs;
11481da177e4SLinus Torvalds 				break;
11491da177e4SLinus Torvalds 			}
11501da177e4SLinus Torvalds #endif
11511da177e4SLinus Torvalds 		} endfor_nexthops(fi)
11521da177e4SLinus Torvalds 		if (dead == fi->fib_nhs) {
11531da177e4SLinus Torvalds 			fi->fib_flags |= RTNH_F_DEAD;
11541da177e4SLinus Torvalds 			ret++;
11551da177e4SLinus Torvalds 		}
11561da177e4SLinus Torvalds 	}
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	return ret;
11591da177e4SLinus Torvalds }
11601da177e4SLinus Torvalds 
11610c838ff1SDavid S. Miller /* Must be invoked inside of an RCU protected region.  */
11620c838ff1SDavid S. Miller void fib_select_default(struct fib_result *res)
11630c838ff1SDavid S. Miller {
11640c838ff1SDavid S. Miller 	struct fib_info *fi = NULL, *last_resort = NULL;
11650c838ff1SDavid S. Miller 	struct list_head *fa_head = res->fa_head;
11660c838ff1SDavid S. Miller 	struct fib_table *tb = res->table;
11670c838ff1SDavid S. Miller 	int order = -1, last_idx = -1;
11680c838ff1SDavid S. Miller 	struct fib_alias *fa;
11690c838ff1SDavid S. Miller 
11700c838ff1SDavid S. Miller 	list_for_each_entry_rcu(fa, fa_head, fa_list) {
11710c838ff1SDavid S. Miller 		struct fib_info *next_fi = fa->fa_info;
11720c838ff1SDavid S. Miller 
117337e826c5SDavid S. Miller 		if (next_fi->fib_scope != res->scope ||
11740c838ff1SDavid S. Miller 		    fa->fa_type != RTN_UNICAST)
11750c838ff1SDavid S. Miller 			continue;
11760c838ff1SDavid S. Miller 
11770c838ff1SDavid S. Miller 		if (next_fi->fib_priority > res->fi->fib_priority)
11780c838ff1SDavid S. Miller 			break;
11790c838ff1SDavid S. Miller 		if (!next_fi->fib_nh[0].nh_gw ||
11800c838ff1SDavid S. Miller 		    next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
11810c838ff1SDavid S. Miller 			continue;
11820c838ff1SDavid S. Miller 
11830c838ff1SDavid S. Miller 		fib_alias_accessed(fa);
11840c838ff1SDavid S. Miller 
11850c838ff1SDavid S. Miller 		if (fi == NULL) {
11860c838ff1SDavid S. Miller 			if (next_fi != res->fi)
11870c838ff1SDavid S. Miller 				break;
11880c838ff1SDavid S. Miller 		} else if (!fib_detect_death(fi, order, &last_resort,
11890c838ff1SDavid S. Miller 					     &last_idx, tb->tb_default)) {
11900c838ff1SDavid S. Miller 			fib_result_assign(res, fi);
11910c838ff1SDavid S. Miller 			tb->tb_default = order;
11920c838ff1SDavid S. Miller 			goto out;
11930c838ff1SDavid S. Miller 		}
11940c838ff1SDavid S. Miller 		fi = next_fi;
11950c838ff1SDavid S. Miller 		order++;
11960c838ff1SDavid S. Miller 	}
11970c838ff1SDavid S. Miller 
11980c838ff1SDavid S. Miller 	if (order <= 0 || fi == NULL) {
11990c838ff1SDavid S. Miller 		tb->tb_default = -1;
12000c838ff1SDavid S. Miller 		goto out;
12010c838ff1SDavid S. Miller 	}
12020c838ff1SDavid S. Miller 
12030c838ff1SDavid S. Miller 	if (!fib_detect_death(fi, order, &last_resort, &last_idx,
12040c838ff1SDavid S. Miller 				tb->tb_default)) {
12050c838ff1SDavid S. Miller 		fib_result_assign(res, fi);
12060c838ff1SDavid S. Miller 		tb->tb_default = order;
12070c838ff1SDavid S. Miller 		goto out;
12080c838ff1SDavid S. Miller 	}
12090c838ff1SDavid S. Miller 
12100c838ff1SDavid S. Miller 	if (last_idx >= 0)
12110c838ff1SDavid S. Miller 		fib_result_assign(res, last_resort);
12120c838ff1SDavid S. Miller 	tb->tb_default = last_idx;
12130c838ff1SDavid S. Miller out:
121431d40937SEric Dumazet 	return;
12150c838ff1SDavid S. Miller }
12160c838ff1SDavid S. Miller 
12171da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds /*
12206a31d2a9SEric Dumazet  * Dead device goes up. We wake up dead nexthops.
12216a31d2a9SEric Dumazet  * It takes sense only on multipath routes.
12221da177e4SLinus Torvalds  */
12231da177e4SLinus Torvalds int fib_sync_up(struct net_device *dev)
12241da177e4SLinus Torvalds {
12251da177e4SLinus Torvalds 	struct fib_info *prev_fi;
12261da177e4SLinus Torvalds 	unsigned int hash;
12271da177e4SLinus Torvalds 	struct hlist_head *head;
12281da177e4SLinus Torvalds 	struct hlist_node *node;
12291da177e4SLinus Torvalds 	struct fib_nh *nh;
12301da177e4SLinus Torvalds 	int ret;
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	if (!(dev->flags & IFF_UP))
12331da177e4SLinus Torvalds 		return 0;
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	prev_fi = NULL;
12361da177e4SLinus Torvalds 	hash = fib_devindex_hashfn(dev->ifindex);
12371da177e4SLinus Torvalds 	head = &fib_info_devhash[hash];
12381da177e4SLinus Torvalds 	ret = 0;
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 	hlist_for_each_entry(nh, node, head, nh_hash) {
12411da177e4SLinus Torvalds 		struct fib_info *fi = nh->nh_parent;
12421da177e4SLinus Torvalds 		int alive;
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 		BUG_ON(!fi->fib_nhs);
12451da177e4SLinus Torvalds 		if (nh->nh_dev != dev || fi == prev_fi)
12461da177e4SLinus Torvalds 			continue;
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 		prev_fi = fi;
12491da177e4SLinus Torvalds 		alive = 0;
12501da177e4SLinus Torvalds 		change_nexthops(fi) {
125171fceff0SDavid S. Miller 			if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
12521da177e4SLinus Torvalds 				alive++;
12531da177e4SLinus Torvalds 				continue;
12541da177e4SLinus Torvalds 			}
125571fceff0SDavid S. Miller 			if (nexthop_nh->nh_dev == NULL ||
125671fceff0SDavid S. Miller 			    !(nexthop_nh->nh_dev->flags & IFF_UP))
12571da177e4SLinus Torvalds 				continue;
125871fceff0SDavid S. Miller 			if (nexthop_nh->nh_dev != dev ||
125971fceff0SDavid S. Miller 			    !__in_dev_get_rtnl(dev))
12601da177e4SLinus Torvalds 				continue;
12611da177e4SLinus Torvalds 			alive++;
12621da177e4SLinus Torvalds 			spin_lock_bh(&fib_multipath_lock);
126371fceff0SDavid S. Miller 			nexthop_nh->nh_power = 0;
126471fceff0SDavid S. Miller 			nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
12651da177e4SLinus Torvalds 			spin_unlock_bh(&fib_multipath_lock);
12661da177e4SLinus Torvalds 		} endfor_nexthops(fi)
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 		if (alive > 0) {
12691da177e4SLinus Torvalds 			fi->fib_flags &= ~RTNH_F_DEAD;
12701da177e4SLinus Torvalds 			ret++;
12711da177e4SLinus Torvalds 		}
12721da177e4SLinus Torvalds 	}
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	return ret;
12751da177e4SLinus Torvalds }
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds /*
12786a31d2a9SEric Dumazet  * The algorithm is suboptimal, but it provides really
12796a31d2a9SEric Dumazet  * fair weighted route distribution.
12801da177e4SLinus Torvalds  */
12811b7fe593SDavid S. Miller void fib_select_multipath(struct fib_result *res)
12821da177e4SLinus Torvalds {
12831da177e4SLinus Torvalds 	struct fib_info *fi = res->fi;
12841da177e4SLinus Torvalds 	int w;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	spin_lock_bh(&fib_multipath_lock);
12871da177e4SLinus Torvalds 	if (fi->fib_power <= 0) {
12881da177e4SLinus Torvalds 		int power = 0;
12891da177e4SLinus Torvalds 		change_nexthops(fi) {
129071fceff0SDavid S. Miller 			if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
129171fceff0SDavid S. Miller 				power += nexthop_nh->nh_weight;
129271fceff0SDavid S. Miller 				nexthop_nh->nh_power = nexthop_nh->nh_weight;
12931da177e4SLinus Torvalds 			}
12941da177e4SLinus Torvalds 		} endfor_nexthops(fi);
12951da177e4SLinus Torvalds 		fi->fib_power = power;
12961da177e4SLinus Torvalds 		if (power <= 0) {
12971da177e4SLinus Torvalds 			spin_unlock_bh(&fib_multipath_lock);
12981da177e4SLinus Torvalds 			/* Race condition: route has just become dead. */
12991da177e4SLinus Torvalds 			res->nh_sel = 0;
13001da177e4SLinus Torvalds 			return;
13011da177e4SLinus Torvalds 		}
13021da177e4SLinus Torvalds 	}
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 	/* w should be random number [0..fi->fib_power-1],
13066a31d2a9SEric Dumazet 	 * it is pretty bad approximation.
13071da177e4SLinus Torvalds 	 */
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	w = jiffies % fi->fib_power;
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	change_nexthops(fi) {
131271fceff0SDavid S. Miller 		if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
131371fceff0SDavid S. Miller 		    nexthop_nh->nh_power) {
13146a31d2a9SEric Dumazet 			w -= nexthop_nh->nh_power;
13156a31d2a9SEric Dumazet 			if (w <= 0) {
131671fceff0SDavid S. Miller 				nexthop_nh->nh_power--;
13171da177e4SLinus Torvalds 				fi->fib_power--;
13181da177e4SLinus Torvalds 				res->nh_sel = nhsel;
13191da177e4SLinus Torvalds 				spin_unlock_bh(&fib_multipath_lock);
13201da177e4SLinus Torvalds 				return;
13211da177e4SLinus Torvalds 			}
13221da177e4SLinus Torvalds 		}
13231da177e4SLinus Torvalds 	} endfor_nexthops(fi);
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 	/* Race condition: route has just become dead. */
13261da177e4SLinus Torvalds 	res->nh_sel = 0;
13271da177e4SLinus Torvalds 	spin_unlock_bh(&fib_multipath_lock);
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds #endif
1330