xref: /linux/net/ipv4/fib_semantics.c (revision f3756b79e8f76cb92830383c215deba146fe0a26)
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 <asm/system.h>
181da177e4SLinus Torvalds #include <linux/bitops.h>
191da177e4SLinus Torvalds #include <linux/types.h>
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/jiffies.h>
221da177e4SLinus Torvalds #include <linux/mm.h>
231da177e4SLinus Torvalds #include <linux/string.h>
241da177e4SLinus Torvalds #include <linux/socket.h>
251da177e4SLinus Torvalds #include <linux/sockios.h>
261da177e4SLinus Torvalds #include <linux/errno.h>
271da177e4SLinus Torvalds #include <linux/in.h>
281da177e4SLinus Torvalds #include <linux/inet.h>
2914c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h>
301da177e4SLinus Torvalds #include <linux/netdevice.h>
311da177e4SLinus Torvalds #include <linux/if_arp.h>
321da177e4SLinus Torvalds #include <linux/proc_fs.h>
331da177e4SLinus Torvalds #include <linux/skbuff.h>
341da177e4SLinus Torvalds #include <linux/init.h>
355a0e3ad6STejun Heo #include <linux/slab.h>
361da177e4SLinus Torvalds 
3714c85021SArnaldo Carvalho de Melo #include <net/arp.h>
381da177e4SLinus Torvalds #include <net/ip.h>
391da177e4SLinus Torvalds #include <net/protocol.h>
401da177e4SLinus Torvalds #include <net/route.h>
411da177e4SLinus Torvalds #include <net/tcp.h>
421da177e4SLinus Torvalds #include <net/sock.h>
431da177e4SLinus Torvalds #include <net/ip_fib.h>
44f21c7bc5SThomas Graf #include <net/netlink.h>
454e902c57SThomas Graf #include <net/nexthop.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include "fib_lookup.h"
481da177e4SLinus Torvalds 
49832b4c5eSStephen Hemminger static DEFINE_SPINLOCK(fib_info_lock);
501da177e4SLinus Torvalds static struct hlist_head *fib_info_hash;
511da177e4SLinus Torvalds static struct hlist_head *fib_info_laddrhash;
52123b9731SDavid S. Miller static unsigned int fib_info_hash_size;
531da177e4SLinus Torvalds static unsigned int fib_info_cnt;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds #define DEVINDEX_HASHBITS 8
561da177e4SLinus Torvalds #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
571da177e4SLinus Torvalds static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static DEFINE_SPINLOCK(fib_multipath_lock);
621da177e4SLinus Torvalds 
636a31d2a9SEric Dumazet #define for_nexthops(fi) {						\
646a31d2a9SEric Dumazet 	int nhsel; const struct fib_nh *nh;				\
656a31d2a9SEric Dumazet 	for (nhsel = 0, nh = (fi)->fib_nh;				\
666a31d2a9SEric Dumazet 	     nhsel < (fi)->fib_nhs;					\
676a31d2a9SEric Dumazet 	     nh++, nhsel++)
681da177e4SLinus Torvalds 
696a31d2a9SEric Dumazet #define change_nexthops(fi) {						\
706a31d2a9SEric Dumazet 	int nhsel; struct fib_nh *nexthop_nh;				\
716a31d2a9SEric Dumazet 	for (nhsel = 0,	nexthop_nh = (struct fib_nh *)((fi)->fib_nh);	\
726a31d2a9SEric Dumazet 	     nhsel < (fi)->fib_nhs;					\
736a31d2a9SEric Dumazet 	     nexthop_nh++, nhsel++)
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds #else /* CONFIG_IP_ROUTE_MULTIPATH */
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds /* Hope, that gcc will optimize it to get rid of dummy loop */
781da177e4SLinus Torvalds 
796a31d2a9SEric Dumazet #define for_nexthops(fi) {						\
806a31d2a9SEric Dumazet 	int nhsel; const struct fib_nh *nh = (fi)->fib_nh;		\
811da177e4SLinus Torvalds 	for (nhsel = 0; nhsel < 1; nhsel++)
821da177e4SLinus Torvalds 
836a31d2a9SEric Dumazet #define change_nexthops(fi) {						\
846a31d2a9SEric Dumazet 	int nhsel;							\
856a31d2a9SEric Dumazet 	struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh);	\
861da177e4SLinus Torvalds 	for (nhsel = 0; nhsel < 1; nhsel++)
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds #endif /* CONFIG_IP_ROUTE_MULTIPATH */
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds #define endfor_nexthops(fi) }
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 
933be0686bSDavid S. Miller const struct fib_prop fib_props[RTN_MAX + 1] = {
946a31d2a9SEric Dumazet 	[RTN_UNSPEC] = {
951da177e4SLinus Torvalds 		.error	= 0,
961da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
976a31d2a9SEric Dumazet 	},
986a31d2a9SEric Dumazet 	[RTN_UNICAST] = {
991da177e4SLinus Torvalds 		.error	= 0,
1001da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1016a31d2a9SEric Dumazet 	},
1026a31d2a9SEric Dumazet 	[RTN_LOCAL] = {
1031da177e4SLinus Torvalds 		.error	= 0,
1041da177e4SLinus Torvalds 		.scope	= RT_SCOPE_HOST,
1056a31d2a9SEric Dumazet 	},
1066a31d2a9SEric Dumazet 	[RTN_BROADCAST] = {
1071da177e4SLinus Torvalds 		.error	= 0,
1081da177e4SLinus Torvalds 		.scope	= RT_SCOPE_LINK,
1096a31d2a9SEric Dumazet 	},
1106a31d2a9SEric Dumazet 	[RTN_ANYCAST] = {
1111da177e4SLinus Torvalds 		.error	= 0,
1121da177e4SLinus Torvalds 		.scope	= RT_SCOPE_LINK,
1136a31d2a9SEric Dumazet 	},
1146a31d2a9SEric Dumazet 	[RTN_MULTICAST] = {
1151da177e4SLinus Torvalds 		.error	= 0,
1161da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1176a31d2a9SEric Dumazet 	},
1186a31d2a9SEric Dumazet 	[RTN_BLACKHOLE] = {
1191da177e4SLinus Torvalds 		.error	= -EINVAL,
1201da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1216a31d2a9SEric Dumazet 	},
1226a31d2a9SEric Dumazet 	[RTN_UNREACHABLE] = {
1231da177e4SLinus Torvalds 		.error	= -EHOSTUNREACH,
1241da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1256a31d2a9SEric Dumazet 	},
1266a31d2a9SEric Dumazet 	[RTN_PROHIBIT] = {
1271da177e4SLinus Torvalds 		.error	= -EACCES,
1281da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1296a31d2a9SEric Dumazet 	},
1306a31d2a9SEric Dumazet 	[RTN_THROW] = {
1311da177e4SLinus Torvalds 		.error	= -EAGAIN,
1321da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1336a31d2a9SEric Dumazet 	},
1346a31d2a9SEric Dumazet 	[RTN_NAT] = {
1351da177e4SLinus Torvalds 		.error	= -EINVAL,
1361da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1376a31d2a9SEric Dumazet 	},
1386a31d2a9SEric Dumazet 	[RTN_XRESOLVE] = {
1391da177e4SLinus Torvalds 		.error	= -EINVAL,
1401da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1416a31d2a9SEric Dumazet 	},
1421da177e4SLinus Torvalds };
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds /* Release a nexthop info record */
14519c1ea14SYan, Zheng static void free_fib_info_rcu(struct rcu_head *head)
14619c1ea14SYan, Zheng {
14719c1ea14SYan, Zheng 	struct fib_info *fi = container_of(head, struct fib_info, rcu);
14819c1ea14SYan, Zheng 
14919c1ea14SYan, Zheng 	if (fi->fib_metrics != (u32 *) dst_default_metrics)
15019c1ea14SYan, Zheng 		kfree(fi->fib_metrics);
15119c1ea14SYan, Zheng 	kfree(fi);
15219c1ea14SYan, Zheng }
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds void free_fib_info(struct fib_info *fi)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	if (fi->fib_dead == 0) {
157058bd4d2SJoe Perches 		pr_warn("Freeing alive fib_info %p\n", fi);
1581da177e4SLinus Torvalds 		return;
1591da177e4SLinus Torvalds 	}
1601da177e4SLinus Torvalds 	change_nexthops(fi) {
16171fceff0SDavid S. Miller 		if (nexthop_nh->nh_dev)
16271fceff0SDavid S. Miller 			dev_put(nexthop_nh->nh_dev);
16371fceff0SDavid S. Miller 		nexthop_nh->nh_dev = NULL;
1641da177e4SLinus Torvalds 	} endfor_nexthops(fi);
1651da177e4SLinus Torvalds 	fib_info_cnt--;
16657d7a600SDenis V. Lunev 	release_net(fi->fib_net);
16719c1ea14SYan, Zheng 	call_rcu(&fi->rcu, free_fib_info_rcu);
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds void fib_release_info(struct fib_info *fi)
1711da177e4SLinus Torvalds {
172832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
1731da177e4SLinus Torvalds 	if (fi && --fi->fib_treeref == 0) {
1741da177e4SLinus Torvalds 		hlist_del(&fi->fib_hash);
1751da177e4SLinus Torvalds 		if (fi->fib_prefsrc)
1761da177e4SLinus Torvalds 			hlist_del(&fi->fib_lhash);
1771da177e4SLinus Torvalds 		change_nexthops(fi) {
17871fceff0SDavid S. Miller 			if (!nexthop_nh->nh_dev)
1791da177e4SLinus Torvalds 				continue;
18071fceff0SDavid S. Miller 			hlist_del(&nexthop_nh->nh_hash);
1811da177e4SLinus Torvalds 		} endfor_nexthops(fi)
1821da177e4SLinus Torvalds 		fi->fib_dead = 1;
1831da177e4SLinus Torvalds 		fib_info_put(fi);
1841da177e4SLinus Torvalds 	}
185832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1886a31d2a9SEric Dumazet static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	const struct fib_nh *onh = ofi->fib_nh;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	for_nexthops(fi) {
1931da177e4SLinus Torvalds 		if (nh->nh_oif != onh->nh_oif ||
1941da177e4SLinus Torvalds 		    nh->nh_gw  != onh->nh_gw ||
1951da177e4SLinus Torvalds 		    nh->nh_scope != onh->nh_scope ||
1961da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
1971da177e4SLinus Torvalds 		    nh->nh_weight != onh->nh_weight ||
1981da177e4SLinus Torvalds #endif
199c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
2001da177e4SLinus Torvalds 		    nh->nh_tclassid != onh->nh_tclassid ||
2011da177e4SLinus Torvalds #endif
2021da177e4SLinus Torvalds 		    ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
2031da177e4SLinus Torvalds 			return -1;
2041da177e4SLinus Torvalds 		onh++;
2051da177e4SLinus Torvalds 	} endfor_nexthops(fi);
2061da177e4SLinus Torvalds 	return 0;
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
20988ebc72fSDavid S. Miller static inline unsigned int fib_devindex_hashfn(unsigned int val)
21088ebc72fSDavid S. Miller {
21188ebc72fSDavid S. Miller 	unsigned int mask = DEVINDEX_HASHSIZE - 1;
21288ebc72fSDavid S. Miller 
21388ebc72fSDavid S. Miller 	return (val ^
21488ebc72fSDavid S. Miller 		(val >> DEVINDEX_HASHBITS) ^
21588ebc72fSDavid S. Miller 		(val >> (DEVINDEX_HASHBITS * 2))) & mask;
21688ebc72fSDavid S. Miller }
21788ebc72fSDavid S. Miller 
2181da177e4SLinus Torvalds static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
2191da177e4SLinus Torvalds {
220123b9731SDavid S. Miller 	unsigned int mask = (fib_info_hash_size - 1);
2211da177e4SLinus Torvalds 	unsigned int val = fi->fib_nhs;
2221da177e4SLinus Torvalds 
22337e826c5SDavid S. Miller 	val ^= (fi->fib_protocol << 8) | fi->fib_scope;
22481f7bf6cSAl Viro 	val ^= (__force u32)fi->fib_prefsrc;
2251da177e4SLinus Torvalds 	val ^= fi->fib_priority;
22688ebc72fSDavid S. Miller 	for_nexthops(fi) {
22788ebc72fSDavid S. Miller 		val ^= fib_devindex_hashfn(nh->nh_oif);
22888ebc72fSDavid S. Miller 	} endfor_nexthops(fi)
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	return (val ^ (val >> 7) ^ (val >> 12)) & mask;
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static struct fib_info *fib_find_info(const struct fib_info *nfi)
2341da177e4SLinus Torvalds {
2351da177e4SLinus Torvalds 	struct hlist_head *head;
2361da177e4SLinus Torvalds 	struct hlist_node *node;
2371da177e4SLinus Torvalds 	struct fib_info *fi;
2381da177e4SLinus Torvalds 	unsigned int hash;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	hash = fib_info_hashfn(nfi);
2411da177e4SLinus Torvalds 	head = &fib_info_hash[hash];
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	hlist_for_each_entry(fi, node, head, fib_hash) {
24409ad9bc7SOctavian Purdila 		if (!net_eq(fi->fib_net, nfi->fib_net))
2454814bdbdSDenis V. Lunev 			continue;
2461da177e4SLinus Torvalds 		if (fi->fib_nhs != nfi->fib_nhs)
2471da177e4SLinus Torvalds 			continue;
2481da177e4SLinus Torvalds 		if (nfi->fib_protocol == fi->fib_protocol &&
24937e826c5SDavid S. Miller 		    nfi->fib_scope == fi->fib_scope &&
2501da177e4SLinus Torvalds 		    nfi->fib_prefsrc == fi->fib_prefsrc &&
2511da177e4SLinus Torvalds 		    nfi->fib_priority == fi->fib_priority &&
2521da177e4SLinus Torvalds 		    memcmp(nfi->fib_metrics, fi->fib_metrics,
253fcd13f42SEric Dumazet 			   sizeof(u32) * RTAX_MAX) == 0 &&
2541da177e4SLinus Torvalds 		    ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
2551da177e4SLinus Torvalds 		    (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
2561da177e4SLinus Torvalds 			return fi;
2571da177e4SLinus Torvalds 	}
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	return NULL;
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds /* Check, that the gateway is already configured.
2636a31d2a9SEric Dumazet  * Used only by redirect accept routine.
2641da177e4SLinus Torvalds  */
265d878e72eSAl Viro int ip_fib_check_default(__be32 gw, struct net_device *dev)
2661da177e4SLinus Torvalds {
2671da177e4SLinus Torvalds 	struct hlist_head *head;
2681da177e4SLinus Torvalds 	struct hlist_node *node;
2691da177e4SLinus Torvalds 	struct fib_nh *nh;
2701da177e4SLinus Torvalds 	unsigned int hash;
2711da177e4SLinus Torvalds 
272832b4c5eSStephen Hemminger 	spin_lock(&fib_info_lock);
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	hash = fib_devindex_hashfn(dev->ifindex);
2751da177e4SLinus Torvalds 	head = &fib_info_devhash[hash];
2761da177e4SLinus Torvalds 	hlist_for_each_entry(nh, node, head, nh_hash) {
2771da177e4SLinus Torvalds 		if (nh->nh_dev == dev &&
2781da177e4SLinus Torvalds 		    nh->nh_gw == gw &&
2791da177e4SLinus Torvalds 		    !(nh->nh_flags & RTNH_F_DEAD)) {
280832b4c5eSStephen Hemminger 			spin_unlock(&fib_info_lock);
2811da177e4SLinus Torvalds 			return 0;
2821da177e4SLinus Torvalds 		}
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds 
285832b4c5eSStephen Hemminger 	spin_unlock(&fib_info_lock);
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	return -1;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
290339bf98fSThomas Graf static inline size_t fib_nlmsg_size(struct fib_info *fi)
291339bf98fSThomas Graf {
292339bf98fSThomas Graf 	size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
293339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_TABLE */
294339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_DST */
295339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_PRIORITY */
296339bf98fSThomas Graf 			 + nla_total_size(4); /* RTA_PREFSRC */
297339bf98fSThomas Graf 
298339bf98fSThomas Graf 	/* space for nested metrics */
299339bf98fSThomas Graf 	payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
300339bf98fSThomas Graf 
301339bf98fSThomas Graf 	if (fi->fib_nhs) {
302339bf98fSThomas Graf 		/* Also handles the special case fib_nhs == 1 */
303339bf98fSThomas Graf 
304339bf98fSThomas Graf 		/* each nexthop is packed in an attribute */
305339bf98fSThomas Graf 		size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
306339bf98fSThomas Graf 
307339bf98fSThomas Graf 		/* may contain flow and gateway attribute */
308339bf98fSThomas Graf 		nhsize += 2 * nla_total_size(4);
309339bf98fSThomas Graf 
310339bf98fSThomas Graf 		/* all nexthops are packed in a nested attribute */
311339bf98fSThomas Graf 		payload += nla_total_size(fi->fib_nhs * nhsize);
312339bf98fSThomas Graf 	}
313339bf98fSThomas Graf 
314339bf98fSThomas Graf 	return payload;
315339bf98fSThomas Graf }
316339bf98fSThomas Graf 
31781f7bf6cSAl Viro void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
318b8f55831SMilan Kocian 	       int dst_len, u32 tb_id, struct nl_info *info,
319b8f55831SMilan Kocian 	       unsigned int nlm_flags)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	struct sk_buff *skb;
3224e902c57SThomas Graf 	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
323f21c7bc5SThomas Graf 	int err = -ENOBUFS;
3241da177e4SLinus Torvalds 
325339bf98fSThomas Graf 	skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
326f21c7bc5SThomas Graf 	if (skb == NULL)
327f21c7bc5SThomas Graf 		goto errout;
3281da177e4SLinus Torvalds 
3294e902c57SThomas Graf 	err = fib_dump_info(skb, info->pid, seq, event, tb_id,
33037e826c5SDavid S. Miller 			    fa->fa_type, key, dst_len,
331b8f55831SMilan Kocian 			    fa->fa_tos, fa->fa_info, nlm_flags);
33226932566SPatrick McHardy 	if (err < 0) {
33326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in fib_nlmsg_size() */
33426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
33526932566SPatrick McHardy 		kfree_skb(skb);
33626932566SPatrick McHardy 		goto errout;
33726932566SPatrick McHardy 	}
3381ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
3394e902c57SThomas Graf 		    info->nlh, GFP_KERNEL);
3401ce85fe4SPablo Neira Ayuso 	return;
341f21c7bc5SThomas Graf errout:
342f21c7bc5SThomas Graf 	if (err < 0)
3434d1169c1SDenis V. Lunev 		rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds /* Return the first fib alias matching TOS with
3471da177e4SLinus Torvalds  * priority less than or equal to PRIO.
3481da177e4SLinus Torvalds  */
3491da177e4SLinus Torvalds struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
3501da177e4SLinus Torvalds {
3511da177e4SLinus Torvalds 	if (fah) {
3521da177e4SLinus Torvalds 		struct fib_alias *fa;
3531da177e4SLinus Torvalds 		list_for_each_entry(fa, fah, fa_list) {
3541da177e4SLinus Torvalds 			if (fa->fa_tos > tos)
3551da177e4SLinus Torvalds 				continue;
3561da177e4SLinus Torvalds 			if (fa->fa_info->fib_priority >= prio ||
3571da177e4SLinus Torvalds 			    fa->fa_tos < tos)
3581da177e4SLinus Torvalds 				return fa;
3591da177e4SLinus Torvalds 		}
3601da177e4SLinus Torvalds 	}
3611da177e4SLinus Torvalds 	return NULL;
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds int fib_detect_death(struct fib_info *fi, int order,
365c17860a0SDenis V. Lunev 		     struct fib_info **last_resort, int *last_idx, int dflt)
3661da177e4SLinus Torvalds {
3671da177e4SLinus Torvalds 	struct neighbour *n;
3681da177e4SLinus Torvalds 	int state = NUD_NONE;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
3711da177e4SLinus Torvalds 	if (n) {
3721da177e4SLinus Torvalds 		state = n->nud_state;
3731da177e4SLinus Torvalds 		neigh_release(n);
3741da177e4SLinus Torvalds 	}
3751da177e4SLinus Torvalds 	if (state == NUD_REACHABLE)
3761da177e4SLinus Torvalds 		return 0;
377c17860a0SDenis V. Lunev 	if ((state & NUD_VALID) && order != dflt)
3781da177e4SLinus Torvalds 		return 0;
3791da177e4SLinus Torvalds 	if ((state & NUD_VALID) ||
380c17860a0SDenis V. Lunev 	    (*last_idx < 0 && order > dflt)) {
3811da177e4SLinus Torvalds 		*last_resort = fi;
3821da177e4SLinus Torvalds 		*last_idx = order;
3831da177e4SLinus Torvalds 	}
3841da177e4SLinus Torvalds 	return 1;
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
3881da177e4SLinus Torvalds 
3894e902c57SThomas Graf static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds 	int nhs = 0;
3921da177e4SLinus Torvalds 
3934e902c57SThomas Graf 	while (rtnh_ok(rtnh, remaining)) {
3941da177e4SLinus Torvalds 		nhs++;
3954e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 
3984e902c57SThomas Graf 	/* leftover implies invalid nexthop configuration, discard it */
3994e902c57SThomas Graf 	return remaining > 0 ? 0 : nhs;
4004e902c57SThomas Graf }
4011da177e4SLinus Torvalds 
4024e902c57SThomas Graf static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
4034e902c57SThomas Graf 		       int remaining, struct fib_config *cfg)
4044e902c57SThomas Graf {
4051da177e4SLinus Torvalds 	change_nexthops(fi) {
4064e902c57SThomas Graf 		int attrlen;
4074e902c57SThomas Graf 
4084e902c57SThomas Graf 		if (!rtnh_ok(rtnh, remaining))
4091da177e4SLinus Torvalds 			return -EINVAL;
4104e902c57SThomas Graf 
41171fceff0SDavid S. Miller 		nexthop_nh->nh_flags =
41271fceff0SDavid S. Miller 			(cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
41371fceff0SDavid S. Miller 		nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
41471fceff0SDavid S. Miller 		nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
4154e902c57SThomas Graf 
4164e902c57SThomas Graf 		attrlen = rtnh_attrlen(rtnh);
4174e902c57SThomas Graf 		if (attrlen > 0) {
4184e902c57SThomas Graf 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4194e902c57SThomas Graf 
4204e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
42171fceff0SDavid S. Miller 			nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
422c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
4234e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_FLOW);
42471fceff0SDavid S. Miller 			nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
4251da177e4SLinus Torvalds #endif
4261da177e4SLinus Torvalds 		}
4274e902c57SThomas Graf 
4284e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
4291da177e4SLinus Torvalds 	} endfor_nexthops(fi);
4304e902c57SThomas Graf 
4311da177e4SLinus Torvalds 	return 0;
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds #endif
4351da177e4SLinus Torvalds 
4364e902c57SThomas Graf int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
4394e902c57SThomas Graf 	struct rtnexthop *rtnh;
4404e902c57SThomas Graf 	int remaining;
4411da177e4SLinus Torvalds #endif
4421da177e4SLinus Torvalds 
4434e902c57SThomas Graf 	if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
4441da177e4SLinus Torvalds 		return 1;
4451da177e4SLinus Torvalds 
4464e902c57SThomas Graf 	if (cfg->fc_oif || cfg->fc_gw) {
4474e902c57SThomas Graf 		if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
4484e902c57SThomas Graf 		    (!cfg->fc_gw  || cfg->fc_gw == fi->fib_nh->nh_gw))
4491da177e4SLinus Torvalds 			return 0;
4501da177e4SLinus Torvalds 		return 1;
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
4544e902c57SThomas Graf 	if (cfg->fc_mp == NULL)
4551da177e4SLinus Torvalds 		return 0;
4564e902c57SThomas Graf 
4574e902c57SThomas Graf 	rtnh = cfg->fc_mp;
4584e902c57SThomas Graf 	remaining = cfg->fc_mp_len;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	for_nexthops(fi) {
4614e902c57SThomas Graf 		int attrlen;
4621da177e4SLinus Torvalds 
4634e902c57SThomas Graf 		if (!rtnh_ok(rtnh, remaining))
4641da177e4SLinus Torvalds 			return -EINVAL;
4654e902c57SThomas Graf 
4664e902c57SThomas Graf 		if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
4671da177e4SLinus Torvalds 			return 1;
4684e902c57SThomas Graf 
4694e902c57SThomas Graf 		attrlen = rtnh_attrlen(rtnh);
4704e902c57SThomas Graf 		if (attrlen < 0) {
4714e902c57SThomas Graf 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4724e902c57SThomas Graf 
4734e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
47417fb2c64SAl Viro 			if (nla && nla_get_be32(nla) != nh->nh_gw)
4751da177e4SLinus Torvalds 				return 1;
476c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
4774e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_FLOW);
4784e902c57SThomas Graf 			if (nla && nla_get_u32(nla) != nh->nh_tclassid)
4791da177e4SLinus Torvalds 				return 1;
4801da177e4SLinus Torvalds #endif
4811da177e4SLinus Torvalds 		}
4824e902c57SThomas Graf 
4834e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
4841da177e4SLinus Torvalds 	} endfor_nexthops(fi);
4851da177e4SLinus Torvalds #endif
4861da177e4SLinus Torvalds 	return 0;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds /*
4916a31d2a9SEric Dumazet  * Picture
4926a31d2a9SEric Dumazet  * -------
4936a31d2a9SEric Dumazet  *
4946a31d2a9SEric Dumazet  * Semantics of nexthop is very messy by historical reasons.
4956a31d2a9SEric Dumazet  * We have to take into account, that:
4966a31d2a9SEric Dumazet  * a) gateway can be actually local interface address,
4976a31d2a9SEric Dumazet  *    so that gatewayed route is direct.
4986a31d2a9SEric Dumazet  * b) gateway must be on-link address, possibly
4996a31d2a9SEric Dumazet  *    described not by an ifaddr, but also by a direct route.
5006a31d2a9SEric Dumazet  * c) If both gateway and interface are specified, they should not
5016a31d2a9SEric Dumazet  *    contradict.
5026a31d2a9SEric Dumazet  * d) If we use tunnel routes, gateway could be not on-link.
5036a31d2a9SEric Dumazet  *
5046a31d2a9SEric Dumazet  * Attempt to reconcile all of these (alas, self-contradictory) conditions
5056a31d2a9SEric Dumazet  * results in pretty ugly and hairy code with obscure logic.
5066a31d2a9SEric Dumazet  *
5076a31d2a9SEric Dumazet  * I chose to generalized it instead, so that the size
5086a31d2a9SEric Dumazet  * of code does not increase practically, but it becomes
5096a31d2a9SEric Dumazet  * much more general.
5106a31d2a9SEric Dumazet  * Every prefix is assigned a "scope" value: "host" is local address,
5116a31d2a9SEric Dumazet  * "link" is direct route,
5126a31d2a9SEric Dumazet  * [ ... "site" ... "interior" ... ]
5136a31d2a9SEric Dumazet  * and "universe" is true gateway route with global meaning.
5146a31d2a9SEric Dumazet  *
5156a31d2a9SEric Dumazet  * Every prefix refers to a set of "nexthop"s (gw, oif),
5166a31d2a9SEric Dumazet  * where gw must have narrower scope. This recursion stops
5176a31d2a9SEric Dumazet  * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
5186a31d2a9SEric Dumazet  * which means that gw is forced to be on link.
5196a31d2a9SEric Dumazet  *
5206a31d2a9SEric Dumazet  * Code is still hairy, but now it is apparently logically
5216a31d2a9SEric Dumazet  * consistent and very flexible. F.e. as by-product it allows
5226a31d2a9SEric Dumazet  * to co-exists in peace independent exterior and interior
5236a31d2a9SEric Dumazet  * routing processes.
5246a31d2a9SEric Dumazet  *
5256a31d2a9SEric Dumazet  * Normally it looks as following.
5266a31d2a9SEric Dumazet  *
5276a31d2a9SEric Dumazet  * {universe prefix}  -> (gw, oif) [scope link]
5286a31d2a9SEric Dumazet  *		  |
5296a31d2a9SEric Dumazet  *		  |-> {link prefix} -> (gw, oif) [scope local]
5306a31d2a9SEric Dumazet  *					|
5316a31d2a9SEric Dumazet  *					|-> {local prefix} (terminal node)
5321da177e4SLinus Torvalds  */
5334e902c57SThomas Graf static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
5344e902c57SThomas Graf 			struct fib_nh *nh)
5351da177e4SLinus Torvalds {
5361da177e4SLinus Torvalds 	int err;
53786167a37SDenis V. Lunev 	struct net *net;
5386a31d2a9SEric Dumazet 	struct net_device *dev;
5391da177e4SLinus Torvalds 
54086167a37SDenis V. Lunev 	net = cfg->fc_nlinfo.nl_net;
5411da177e4SLinus Torvalds 	if (nh->nh_gw) {
5421da177e4SLinus Torvalds 		struct fib_result res;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 		if (nh->nh_flags & RTNH_F_ONLINK) {
5451da177e4SLinus Torvalds 
5464e902c57SThomas Graf 			if (cfg->fc_scope >= RT_SCOPE_LINK)
5471da177e4SLinus Torvalds 				return -EINVAL;
54886167a37SDenis V. Lunev 			if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
5491da177e4SLinus Torvalds 				return -EINVAL;
5506a31d2a9SEric Dumazet 			dev = __dev_get_by_index(net, nh->nh_oif);
5516a31d2a9SEric Dumazet 			if (!dev)
5521da177e4SLinus Torvalds 				return -ENODEV;
5531da177e4SLinus Torvalds 			if (!(dev->flags & IFF_UP))
5541da177e4SLinus Torvalds 				return -ENETDOWN;
5551da177e4SLinus Torvalds 			nh->nh_dev = dev;
5561da177e4SLinus Torvalds 			dev_hold(dev);
5571da177e4SLinus Torvalds 			nh->nh_scope = RT_SCOPE_LINK;
5581da177e4SLinus Torvalds 			return 0;
5591da177e4SLinus Torvalds 		}
560ebc0ffaeSEric Dumazet 		rcu_read_lock();
5611da177e4SLinus Torvalds 		{
5629ade2286SDavid S. Miller 			struct flowi4 fl4 = {
5639ade2286SDavid S. Miller 				.daddr = nh->nh_gw,
5649ade2286SDavid S. Miller 				.flowi4_scope = cfg->fc_scope + 1,
5659ade2286SDavid S. Miller 				.flowi4_oif = nh->nh_oif,
5664e902c57SThomas Graf 			};
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 			/* It is not necessary, but requires a bit of thinking */
5699ade2286SDavid S. Miller 			if (fl4.flowi4_scope < RT_SCOPE_LINK)
5709ade2286SDavid S. Miller 				fl4.flowi4_scope = RT_SCOPE_LINK;
5719ade2286SDavid S. Miller 			err = fib_lookup(net, &fl4, &res);
572ebc0ffaeSEric Dumazet 			if (err) {
573ebc0ffaeSEric Dumazet 				rcu_read_unlock();
5741da177e4SLinus Torvalds 				return err;
5751da177e4SLinus Torvalds 			}
576ebc0ffaeSEric Dumazet 		}
5771da177e4SLinus Torvalds 		err = -EINVAL;
5781da177e4SLinus Torvalds 		if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
5791da177e4SLinus Torvalds 			goto out;
5801da177e4SLinus Torvalds 		nh->nh_scope = res.scope;
5811da177e4SLinus Torvalds 		nh->nh_oif = FIB_RES_OIF(res);
5826a31d2a9SEric Dumazet 		nh->nh_dev = dev = FIB_RES_DEV(res);
5836a31d2a9SEric Dumazet 		if (!dev)
5841da177e4SLinus Torvalds 			goto out;
5856a31d2a9SEric Dumazet 		dev_hold(dev);
5868723e1b4SEric Dumazet 		err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
5871da177e4SLinus Torvalds 	} else {
5881da177e4SLinus Torvalds 		struct in_device *in_dev;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 		if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
5911da177e4SLinus Torvalds 			return -EINVAL;
5921da177e4SLinus Torvalds 
5938723e1b4SEric Dumazet 		rcu_read_lock();
5948723e1b4SEric Dumazet 		err = -ENODEV;
59586167a37SDenis V. Lunev 		in_dev = inetdev_by_index(net, nh->nh_oif);
5961da177e4SLinus Torvalds 		if (in_dev == NULL)
5978723e1b4SEric Dumazet 			goto out;
5988723e1b4SEric Dumazet 		err = -ENETDOWN;
5998723e1b4SEric Dumazet 		if (!(in_dev->dev->flags & IFF_UP))
6008723e1b4SEric Dumazet 			goto out;
6011da177e4SLinus Torvalds 		nh->nh_dev = in_dev->dev;
6021da177e4SLinus Torvalds 		dev_hold(nh->nh_dev);
6031da177e4SLinus Torvalds 		nh->nh_scope = RT_SCOPE_HOST;
6048723e1b4SEric Dumazet 		err = 0;
6051da177e4SLinus Torvalds 	}
6068723e1b4SEric Dumazet out:
6078723e1b4SEric Dumazet 	rcu_read_unlock();
6088723e1b4SEric Dumazet 	return err;
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
61181f7bf6cSAl Viro static inline unsigned int fib_laddr_hashfn(__be32 val)
6121da177e4SLinus Torvalds {
613123b9731SDavid S. Miller 	unsigned int mask = (fib_info_hash_size - 1);
6141da177e4SLinus Torvalds 
6156a31d2a9SEric Dumazet 	return ((__force u32)val ^
6166a31d2a9SEric Dumazet 		((__force u32)val >> 7) ^
6176a31d2a9SEric Dumazet 		((__force u32)val >> 14)) & mask;
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
620123b9731SDavid S. Miller static struct hlist_head *fib_info_hash_alloc(int bytes)
6211da177e4SLinus Torvalds {
6221da177e4SLinus Torvalds 	if (bytes <= PAGE_SIZE)
62388f83491SJoonwoo Park 		return kzalloc(bytes, GFP_KERNEL);
6241da177e4SLinus Torvalds 	else
6251da177e4SLinus Torvalds 		return (struct hlist_head *)
6266a31d2a9SEric Dumazet 			__get_free_pages(GFP_KERNEL | __GFP_ZERO,
6276a31d2a9SEric Dumazet 					 get_order(bytes));
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
630123b9731SDavid S. Miller static void fib_info_hash_free(struct hlist_head *hash, int bytes)
6311da177e4SLinus Torvalds {
6321da177e4SLinus Torvalds 	if (!hash)
6331da177e4SLinus Torvalds 		return;
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	if (bytes <= PAGE_SIZE)
6361da177e4SLinus Torvalds 		kfree(hash);
6371da177e4SLinus Torvalds 	else
6381da177e4SLinus Torvalds 		free_pages((unsigned long) hash, get_order(bytes));
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
641123b9731SDavid S. Miller static void fib_info_hash_move(struct hlist_head *new_info_hash,
6421da177e4SLinus Torvalds 			       struct hlist_head *new_laddrhash,
6431da177e4SLinus Torvalds 			       unsigned int new_size)
6441da177e4SLinus Torvalds {
645b7656e7fSDavid S. Miller 	struct hlist_head *old_info_hash, *old_laddrhash;
646123b9731SDavid S. Miller 	unsigned int old_size = fib_info_hash_size;
647b7656e7fSDavid S. Miller 	unsigned int i, bytes;
6481da177e4SLinus Torvalds 
649832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
650b7656e7fSDavid S. Miller 	old_info_hash = fib_info_hash;
651b7656e7fSDavid S. Miller 	old_laddrhash = fib_info_laddrhash;
652123b9731SDavid S. Miller 	fib_info_hash_size = new_size;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	for (i = 0; i < old_size; i++) {
6551da177e4SLinus Torvalds 		struct hlist_head *head = &fib_info_hash[i];
6561da177e4SLinus Torvalds 		struct hlist_node *node, *n;
6571da177e4SLinus Torvalds 		struct fib_info *fi;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 		hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
6601da177e4SLinus Torvalds 			struct hlist_head *dest;
6611da177e4SLinus Torvalds 			unsigned int new_hash;
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 			hlist_del(&fi->fib_hash);
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 			new_hash = fib_info_hashfn(fi);
6661da177e4SLinus Torvalds 			dest = &new_info_hash[new_hash];
6671da177e4SLinus Torvalds 			hlist_add_head(&fi->fib_hash, dest);
6681da177e4SLinus Torvalds 		}
6691da177e4SLinus Torvalds 	}
6701da177e4SLinus Torvalds 	fib_info_hash = new_info_hash;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	for (i = 0; i < old_size; i++) {
6731da177e4SLinus Torvalds 		struct hlist_head *lhead = &fib_info_laddrhash[i];
6741da177e4SLinus Torvalds 		struct hlist_node *node, *n;
6751da177e4SLinus Torvalds 		struct fib_info *fi;
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 		hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
6781da177e4SLinus Torvalds 			struct hlist_head *ldest;
6791da177e4SLinus Torvalds 			unsigned int new_hash;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 			hlist_del(&fi->fib_lhash);
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 			new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
6841da177e4SLinus Torvalds 			ldest = &new_laddrhash[new_hash];
6851da177e4SLinus Torvalds 			hlist_add_head(&fi->fib_lhash, ldest);
6861da177e4SLinus Torvalds 		}
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 	fib_info_laddrhash = new_laddrhash;
6891da177e4SLinus Torvalds 
690832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
691b7656e7fSDavid S. Miller 
692b7656e7fSDavid S. Miller 	bytes = old_size * sizeof(struct hlist_head *);
693123b9731SDavid S. Miller 	fib_info_hash_free(old_info_hash, bytes);
694123b9731SDavid S. Miller 	fib_info_hash_free(old_laddrhash, bytes);
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
697436c3b66SDavid S. Miller __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
698436c3b66SDavid S. Miller {
699436c3b66SDavid S. Miller 	nh->nh_saddr = inet_select_addr(nh->nh_dev,
700436c3b66SDavid S. Miller 					nh->nh_gw,
70137e826c5SDavid S. Miller 					nh->nh_parent->fib_scope);
702436c3b66SDavid S. Miller 	nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
703436c3b66SDavid S. Miller 
704436c3b66SDavid S. Miller 	return nh->nh_saddr;
705436c3b66SDavid S. Miller }
706436c3b66SDavid S. Miller 
7074e902c57SThomas Graf struct fib_info *fib_create_info(struct fib_config *cfg)
7081da177e4SLinus Torvalds {
7091da177e4SLinus Torvalds 	int err;
7101da177e4SLinus Torvalds 	struct fib_info *fi = NULL;
7111da177e4SLinus Torvalds 	struct fib_info *ofi;
7121da177e4SLinus Torvalds 	int nhs = 1;
7137462bd74SDenis V. Lunev 	struct net *net = cfg->fc_nlinfo.nl_net;
7141da177e4SLinus Torvalds 
7154c8237cdSDavid S. Miller 	if (cfg->fc_type > RTN_MAX)
7164c8237cdSDavid S. Miller 		goto err_inval;
7174c8237cdSDavid S. Miller 
7181da177e4SLinus Torvalds 	/* Fast check to catch the most weird cases */
7194e902c57SThomas Graf 	if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
7201da177e4SLinus Torvalds 		goto err_inval;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
7234e902c57SThomas Graf 	if (cfg->fc_mp) {
7244e902c57SThomas Graf 		nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
7251da177e4SLinus Torvalds 		if (nhs == 0)
7261da177e4SLinus Torvalds 			goto err_inval;
7271da177e4SLinus Torvalds 	}
7281da177e4SLinus Torvalds #endif
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	err = -ENOBUFS;
731123b9731SDavid S. Miller 	if (fib_info_cnt >= fib_info_hash_size) {
732123b9731SDavid S. Miller 		unsigned int new_size = fib_info_hash_size << 1;
7331da177e4SLinus Torvalds 		struct hlist_head *new_info_hash;
7341da177e4SLinus Torvalds 		struct hlist_head *new_laddrhash;
7351da177e4SLinus Torvalds 		unsigned int bytes;
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 		if (!new_size)
7381da177e4SLinus Torvalds 			new_size = 1;
7391da177e4SLinus Torvalds 		bytes = new_size * sizeof(struct hlist_head *);
740123b9731SDavid S. Miller 		new_info_hash = fib_info_hash_alloc(bytes);
741123b9731SDavid S. Miller 		new_laddrhash = fib_info_hash_alloc(bytes);
7421da177e4SLinus Torvalds 		if (!new_info_hash || !new_laddrhash) {
743123b9731SDavid S. Miller 			fib_info_hash_free(new_info_hash, bytes);
744123b9731SDavid S. Miller 			fib_info_hash_free(new_laddrhash, bytes);
74588f83491SJoonwoo Park 		} else
746123b9731SDavid S. Miller 			fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
7471da177e4SLinus Torvalds 
748123b9731SDavid S. Miller 		if (!fib_info_hash_size)
7491da177e4SLinus Torvalds 			goto failure;
7501da177e4SLinus Torvalds 	}
7511da177e4SLinus Torvalds 
7520da974f4SPanagiotis Issaris 	fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
7531da177e4SLinus Torvalds 	if (fi == NULL)
7541da177e4SLinus Torvalds 		goto failure;
755725d1e1bSDavid S. Miller 	if (cfg->fc_mx) {
7569c150e82SDavid S. Miller 		fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
7579c150e82SDavid S. Miller 		if (!fi->fib_metrics)
7589c150e82SDavid S. Miller 			goto failure;
759725d1e1bSDavid S. Miller 	} else
760725d1e1bSDavid S. Miller 		fi->fib_metrics = (u32 *) dst_default_metrics;
7611da177e4SLinus Torvalds 	fib_info_cnt++;
7621da177e4SLinus Torvalds 
76357d7a600SDenis V. Lunev 	fi->fib_net = hold_net(net);
7644e902c57SThomas Graf 	fi->fib_protocol = cfg->fc_protocol;
76537e826c5SDavid S. Miller 	fi->fib_scope = cfg->fc_scope;
7664e902c57SThomas Graf 	fi->fib_flags = cfg->fc_flags;
7674e902c57SThomas Graf 	fi->fib_priority = cfg->fc_priority;
7684e902c57SThomas Graf 	fi->fib_prefsrc = cfg->fc_prefsrc;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	fi->fib_nhs = nhs;
7711da177e4SLinus Torvalds 	change_nexthops(fi) {
77271fceff0SDavid S. Miller 		nexthop_nh->nh_parent = fi;
7731da177e4SLinus Torvalds 	} endfor_nexthops(fi)
7741da177e4SLinus Torvalds 
7754e902c57SThomas Graf 	if (cfg->fc_mx) {
7764e902c57SThomas Graf 		struct nlattr *nla;
7774e902c57SThomas Graf 		int remaining;
7781da177e4SLinus Torvalds 
7794e902c57SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
7808f4c1f9bSThomas Graf 			int type = nla_type(nla);
7814e902c57SThomas Graf 
7824e902c57SThomas Graf 			if (type) {
7834e902c57SThomas Graf 				if (type > RTAX_MAX)
7841da177e4SLinus Torvalds 					goto err_inval;
7854e902c57SThomas Graf 				fi->fib_metrics[type - 1] = nla_get_u32(nla);
7861da177e4SLinus Torvalds 			}
7871da177e4SLinus Torvalds 		}
7884e902c57SThomas Graf 	}
7891da177e4SLinus Torvalds 
7904e902c57SThomas Graf 	if (cfg->fc_mp) {
7911da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
7924e902c57SThomas Graf 		err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
7934e902c57SThomas Graf 		if (err != 0)
7941da177e4SLinus Torvalds 			goto failure;
7954e902c57SThomas Graf 		if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
7961da177e4SLinus Torvalds 			goto err_inval;
7974e902c57SThomas Graf 		if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
7981da177e4SLinus Torvalds 			goto err_inval;
799c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
8004e902c57SThomas Graf 		if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
8011da177e4SLinus Torvalds 			goto err_inval;
8021da177e4SLinus Torvalds #endif
8031da177e4SLinus Torvalds #else
8041da177e4SLinus Torvalds 		goto err_inval;
8051da177e4SLinus Torvalds #endif
8061da177e4SLinus Torvalds 	} else {
8071da177e4SLinus Torvalds 		struct fib_nh *nh = fi->fib_nh;
8084e902c57SThomas Graf 
8094e902c57SThomas Graf 		nh->nh_oif = cfg->fc_oif;
8104e902c57SThomas Graf 		nh->nh_gw = cfg->fc_gw;
8114e902c57SThomas Graf 		nh->nh_flags = cfg->fc_flags;
812c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
8134e902c57SThomas Graf 		nh->nh_tclassid = cfg->fc_flow;
8141da177e4SLinus Torvalds #endif
8151da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
8161da177e4SLinus Torvalds 		nh->nh_weight = 1;
8171da177e4SLinus Torvalds #endif
8181da177e4SLinus Torvalds 	}
8191da177e4SLinus Torvalds 
8204e902c57SThomas Graf 	if (fib_props[cfg->fc_type].error) {
8214e902c57SThomas Graf 		if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
8221da177e4SLinus Torvalds 			goto err_inval;
8231da177e4SLinus Torvalds 		goto link_it;
8244c8237cdSDavid S. Miller 	} else {
8254c8237cdSDavid S. Miller 		switch (cfg->fc_type) {
8264c8237cdSDavid S. Miller 		case RTN_UNICAST:
8274c8237cdSDavid S. Miller 		case RTN_LOCAL:
8284c8237cdSDavid S. Miller 		case RTN_BROADCAST:
8294c8237cdSDavid S. Miller 		case RTN_ANYCAST:
8304c8237cdSDavid S. Miller 		case RTN_MULTICAST:
8314c8237cdSDavid S. Miller 			break;
8324c8237cdSDavid S. Miller 		default:
8334c8237cdSDavid S. Miller 			goto err_inval;
8344c8237cdSDavid S. Miller 		}
8351da177e4SLinus Torvalds 	}
8361da177e4SLinus Torvalds 
8374e902c57SThomas Graf 	if (cfg->fc_scope > RT_SCOPE_HOST)
8381da177e4SLinus Torvalds 		goto err_inval;
8391da177e4SLinus Torvalds 
8404e902c57SThomas Graf 	if (cfg->fc_scope == RT_SCOPE_HOST) {
8411da177e4SLinus Torvalds 		struct fib_nh *nh = fi->fib_nh;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 		/* Local address is added. */
8441da177e4SLinus Torvalds 		if (nhs != 1 || nh->nh_gw)
8451da177e4SLinus Torvalds 			goto err_inval;
8461da177e4SLinus Torvalds 		nh->nh_scope = RT_SCOPE_NOWHERE;
8477462bd74SDenis V. Lunev 		nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
8481da177e4SLinus Torvalds 		err = -ENODEV;
8491da177e4SLinus Torvalds 		if (nh->nh_dev == NULL)
8501da177e4SLinus Torvalds 			goto failure;
8511da177e4SLinus Torvalds 	} else {
8521da177e4SLinus Torvalds 		change_nexthops(fi) {
8536a31d2a9SEric Dumazet 			err = fib_check_nh(cfg, fi, nexthop_nh);
8546a31d2a9SEric Dumazet 			if (err != 0)
8551da177e4SLinus Torvalds 				goto failure;
8561da177e4SLinus Torvalds 		} endfor_nexthops(fi)
8571da177e4SLinus Torvalds 	}
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	if (fi->fib_prefsrc) {
8604e902c57SThomas Graf 		if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
8614e902c57SThomas Graf 		    fi->fib_prefsrc != cfg->fc_dst)
8627462bd74SDenis V. Lunev 			if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
8631da177e4SLinus Torvalds 				goto err_inval;
8641da177e4SLinus Torvalds 	}
8651da177e4SLinus Torvalds 
8661fc050a1SDavid S. Miller 	change_nexthops(fi) {
867436c3b66SDavid S. Miller 		fib_info_update_nh_saddr(net, nexthop_nh);
8681fc050a1SDavid S. Miller 	} endfor_nexthops(fi)
8691fc050a1SDavid S. Miller 
8701da177e4SLinus Torvalds link_it:
8716a31d2a9SEric Dumazet 	ofi = fib_find_info(fi);
8726a31d2a9SEric Dumazet 	if (ofi) {
8731da177e4SLinus Torvalds 		fi->fib_dead = 1;
8741da177e4SLinus Torvalds 		free_fib_info(fi);
8751da177e4SLinus Torvalds 		ofi->fib_treeref++;
8761da177e4SLinus Torvalds 		return ofi;
8771da177e4SLinus Torvalds 	}
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	fi->fib_treeref++;
8801da177e4SLinus Torvalds 	atomic_inc(&fi->fib_clntref);
881832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
8821da177e4SLinus Torvalds 	hlist_add_head(&fi->fib_hash,
8831da177e4SLinus Torvalds 		       &fib_info_hash[fib_info_hashfn(fi)]);
8841da177e4SLinus Torvalds 	if (fi->fib_prefsrc) {
8851da177e4SLinus Torvalds 		struct hlist_head *head;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 		head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
8881da177e4SLinus Torvalds 		hlist_add_head(&fi->fib_lhash, head);
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds 	change_nexthops(fi) {
8911da177e4SLinus Torvalds 		struct hlist_head *head;
8921da177e4SLinus Torvalds 		unsigned int hash;
8931da177e4SLinus Torvalds 
89471fceff0SDavid S. Miller 		if (!nexthop_nh->nh_dev)
8951da177e4SLinus Torvalds 			continue;
89671fceff0SDavid S. Miller 		hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
8971da177e4SLinus Torvalds 		head = &fib_info_devhash[hash];
89871fceff0SDavid S. Miller 		hlist_add_head(&nexthop_nh->nh_hash, head);
8991da177e4SLinus Torvalds 	} endfor_nexthops(fi)
900832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
9011da177e4SLinus Torvalds 	return fi;
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds err_inval:
9041da177e4SLinus Torvalds 	err = -EINVAL;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds failure:
9071da177e4SLinus Torvalds 	if (fi) {
9081da177e4SLinus Torvalds 		fi->fib_dead = 1;
9091da177e4SLinus Torvalds 		free_fib_info(fi);
9101da177e4SLinus Torvalds 	}
9114e902c57SThomas Graf 
9124e902c57SThomas Graf 	return ERR_PTR(err);
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
915be403ea1SThomas Graf int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
91637e826c5SDavid S. Miller 		  u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
917b6544c0bSJamal Hadi Salim 		  struct fib_info *fi, unsigned int flags)
9181da177e4SLinus Torvalds {
9191da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
920be403ea1SThomas Graf 	struct rtmsg *rtm;
9211da177e4SLinus Torvalds 
922be403ea1SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
923be403ea1SThomas Graf 	if (nlh == NULL)
92426932566SPatrick McHardy 		return -EMSGSIZE;
925be403ea1SThomas Graf 
926be403ea1SThomas Graf 	rtm = nlmsg_data(nlh);
9271da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET;
9281da177e4SLinus Torvalds 	rtm->rtm_dst_len = dst_len;
9291da177e4SLinus Torvalds 	rtm->rtm_src_len = 0;
9301da177e4SLinus Torvalds 	rtm->rtm_tos = tos;
931709772e6SKrzysztof Piotr Oledzki 	if (tb_id < 256)
9321da177e4SLinus Torvalds 		rtm->rtm_table = tb_id;
933709772e6SKrzysztof Piotr Oledzki 	else
934709772e6SKrzysztof Piotr Oledzki 		rtm->rtm_table = RT_TABLE_COMPAT;
935*f3756b79SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, tb_id))
936*f3756b79SDavid S. Miller 		goto nla_put_failure;
9371da177e4SLinus Torvalds 	rtm->rtm_type = type;
9381da177e4SLinus Torvalds 	rtm->rtm_flags = fi->fib_flags;
93937e826c5SDavid S. Miller 	rtm->rtm_scope = fi->fib_scope;
9401da177e4SLinus Torvalds 	rtm->rtm_protocol = fi->fib_protocol;
941be403ea1SThomas Graf 
942*f3756b79SDavid S. Miller 	if (rtm->rtm_dst_len &&
943*f3756b79SDavid S. Miller 	    nla_put_be32(skb, RTA_DST, dst))
944*f3756b79SDavid S. Miller 		goto nla_put_failure;
945*f3756b79SDavid S. Miller 	if (fi->fib_priority &&
946*f3756b79SDavid S. Miller 	    nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
947*f3756b79SDavid S. Miller 		goto nla_put_failure;
9481da177e4SLinus Torvalds 	if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
949be403ea1SThomas Graf 		goto nla_put_failure;
950be403ea1SThomas Graf 
951*f3756b79SDavid S. Miller 	if (fi->fib_prefsrc &&
952*f3756b79SDavid S. Miller 	    nla_put_be32(skb, RTA_PREFSRC, fi->fib_prefsrc))
953*f3756b79SDavid S. Miller 		goto nla_put_failure;
9541da177e4SLinus Torvalds 	if (fi->fib_nhs == 1) {
955*f3756b79SDavid S. Miller 		if (fi->fib_nh->nh_gw &&
956*f3756b79SDavid S. Miller 		    nla_put_be32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
957*f3756b79SDavid S. Miller 			goto nla_put_failure;
958*f3756b79SDavid S. Miller 		if (fi->fib_nh->nh_oif &&
959*f3756b79SDavid S. Miller 		    nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
960*f3756b79SDavid S. Miller 			goto nla_put_failure;
961c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
962*f3756b79SDavid S. Miller 		if (fi->fib_nh[0].nh_tclassid &&
963*f3756b79SDavid S. Miller 		    nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
964*f3756b79SDavid S. Miller 			goto nla_put_failure;
9658265abc0SPatrick McHardy #endif
9661da177e4SLinus Torvalds 	}
9671da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
9681da177e4SLinus Torvalds 	if (fi->fib_nhs > 1) {
969be403ea1SThomas Graf 		struct rtnexthop *rtnh;
970be403ea1SThomas Graf 		struct nlattr *mp;
971be403ea1SThomas Graf 
972be403ea1SThomas Graf 		mp = nla_nest_start(skb, RTA_MULTIPATH);
973be403ea1SThomas Graf 		if (mp == NULL)
974be403ea1SThomas Graf 			goto nla_put_failure;
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 		for_nexthops(fi) {
977be403ea1SThomas Graf 			rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
978be403ea1SThomas Graf 			if (rtnh == NULL)
979be403ea1SThomas Graf 				goto nla_put_failure;
980be403ea1SThomas Graf 
981be403ea1SThomas Graf 			rtnh->rtnh_flags = nh->nh_flags & 0xFF;
982be403ea1SThomas Graf 			rtnh->rtnh_hops = nh->nh_weight - 1;
983be403ea1SThomas Graf 			rtnh->rtnh_ifindex = nh->nh_oif;
984be403ea1SThomas Graf 
985*f3756b79SDavid S. Miller 			if (nh->nh_gw &&
986*f3756b79SDavid S. Miller 			    nla_put_be32(skb, RTA_GATEWAY, nh->nh_gw))
987*f3756b79SDavid S. Miller 				goto nla_put_failure;
988c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
989*f3756b79SDavid S. Miller 			if (nh->nh_tclassid &&
990*f3756b79SDavid S. Miller 			    nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
991*f3756b79SDavid S. Miller 				goto nla_put_failure;
9928265abc0SPatrick McHardy #endif
993be403ea1SThomas Graf 			/* length of rtnetlink header + attributes */
994be403ea1SThomas Graf 			rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
9951da177e4SLinus Torvalds 		} endfor_nexthops(fi);
996be403ea1SThomas Graf 
997be403ea1SThomas Graf 		nla_nest_end(skb, mp);
9981da177e4SLinus Torvalds 	}
9991da177e4SLinus Torvalds #endif
1000be403ea1SThomas Graf 	return nlmsg_end(skb, nlh);
10011da177e4SLinus Torvalds 
1002be403ea1SThomas Graf nla_put_failure:
100326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
100426932566SPatrick McHardy 	return -EMSGSIZE;
10051da177e4SLinus Torvalds }
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds /*
10086a31d2a9SEric Dumazet  * Update FIB if:
10096a31d2a9SEric Dumazet  * - local address disappeared -> we must delete all the entries
10106a31d2a9SEric Dumazet  *   referring to it.
10116a31d2a9SEric Dumazet  * - device went down -> we must shutdown all nexthops going via it.
10121da177e4SLinus Torvalds  */
10134814bdbdSDenis V. Lunev int fib_sync_down_addr(struct net *net, __be32 local)
10141da177e4SLinus Torvalds {
10151da177e4SLinus Torvalds 	int ret = 0;
10161da177e4SLinus Torvalds 	unsigned int hash = fib_laddr_hashfn(local);
10171da177e4SLinus Torvalds 	struct hlist_head *head = &fib_info_laddrhash[hash];
10181da177e4SLinus Torvalds 	struct hlist_node *node;
10191da177e4SLinus Torvalds 	struct fib_info *fi;
10201da177e4SLinus Torvalds 
102185326fa5SDenis V. Lunev 	if (fib_info_laddrhash == NULL || local == 0)
102285326fa5SDenis V. Lunev 		return 0;
102385326fa5SDenis V. Lunev 
10241da177e4SLinus Torvalds 	hlist_for_each_entry(fi, node, head, fib_lhash) {
102509ad9bc7SOctavian Purdila 		if (!net_eq(fi->fib_net, net))
10264814bdbdSDenis V. Lunev 			continue;
10271da177e4SLinus Torvalds 		if (fi->fib_prefsrc == local) {
10281da177e4SLinus Torvalds 			fi->fib_flags |= RTNH_F_DEAD;
10291da177e4SLinus Torvalds 			ret++;
10301da177e4SLinus Torvalds 		}
10311da177e4SLinus Torvalds 	}
103285326fa5SDenis V. Lunev 	return ret;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
103585326fa5SDenis V. Lunev int fib_sync_down_dev(struct net_device *dev, int force)
103685326fa5SDenis V. Lunev {
103785326fa5SDenis V. Lunev 	int ret = 0;
103885326fa5SDenis V. Lunev 	int scope = RT_SCOPE_NOWHERE;
10391da177e4SLinus Torvalds 	struct fib_info *prev_fi = NULL;
10401da177e4SLinus Torvalds 	unsigned int hash = fib_devindex_hashfn(dev->ifindex);
10411da177e4SLinus Torvalds 	struct hlist_head *head = &fib_info_devhash[hash];
10421da177e4SLinus Torvalds 	struct hlist_node *node;
10431da177e4SLinus Torvalds 	struct fib_nh *nh;
10441da177e4SLinus Torvalds 
104585326fa5SDenis V. Lunev 	if (force)
104685326fa5SDenis V. Lunev 		scope = -1;
104785326fa5SDenis V. Lunev 
10481da177e4SLinus Torvalds 	hlist_for_each_entry(nh, node, head, nh_hash) {
10491da177e4SLinus Torvalds 		struct fib_info *fi = nh->nh_parent;
10501da177e4SLinus Torvalds 		int dead;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 		BUG_ON(!fi->fib_nhs);
10531da177e4SLinus Torvalds 		if (nh->nh_dev != dev || fi == prev_fi)
10541da177e4SLinus Torvalds 			continue;
10551da177e4SLinus Torvalds 		prev_fi = fi;
10561da177e4SLinus Torvalds 		dead = 0;
10571da177e4SLinus Torvalds 		change_nexthops(fi) {
105871fceff0SDavid S. Miller 			if (nexthop_nh->nh_flags & RTNH_F_DEAD)
10591da177e4SLinus Torvalds 				dead++;
106071fceff0SDavid S. Miller 			else if (nexthop_nh->nh_dev == dev &&
106171fceff0SDavid S. Miller 				 nexthop_nh->nh_scope != scope) {
106271fceff0SDavid S. Miller 				nexthop_nh->nh_flags |= RTNH_F_DEAD;
10631da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
10641da177e4SLinus Torvalds 				spin_lock_bh(&fib_multipath_lock);
106571fceff0SDavid S. Miller 				fi->fib_power -= nexthop_nh->nh_power;
106671fceff0SDavid S. Miller 				nexthop_nh->nh_power = 0;
10671da177e4SLinus Torvalds 				spin_unlock_bh(&fib_multipath_lock);
10681da177e4SLinus Torvalds #endif
10691da177e4SLinus Torvalds 				dead++;
10701da177e4SLinus Torvalds 			}
10711da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
107271fceff0SDavid S. Miller 			if (force > 1 && nexthop_nh->nh_dev == dev) {
10731da177e4SLinus Torvalds 				dead = fi->fib_nhs;
10741da177e4SLinus Torvalds 				break;
10751da177e4SLinus Torvalds 			}
10761da177e4SLinus Torvalds #endif
10771da177e4SLinus Torvalds 		} endfor_nexthops(fi)
10781da177e4SLinus Torvalds 		if (dead == fi->fib_nhs) {
10791da177e4SLinus Torvalds 			fi->fib_flags |= RTNH_F_DEAD;
10801da177e4SLinus Torvalds 			ret++;
10811da177e4SLinus Torvalds 		}
10821da177e4SLinus Torvalds 	}
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	return ret;
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds 
10870c838ff1SDavid S. Miller /* Must be invoked inside of an RCU protected region.  */
10880c838ff1SDavid S. Miller void fib_select_default(struct fib_result *res)
10890c838ff1SDavid S. Miller {
10900c838ff1SDavid S. Miller 	struct fib_info *fi = NULL, *last_resort = NULL;
10910c838ff1SDavid S. Miller 	struct list_head *fa_head = res->fa_head;
10920c838ff1SDavid S. Miller 	struct fib_table *tb = res->table;
10930c838ff1SDavid S. Miller 	int order = -1, last_idx = -1;
10940c838ff1SDavid S. Miller 	struct fib_alias *fa;
10950c838ff1SDavid S. Miller 
10960c838ff1SDavid S. Miller 	list_for_each_entry_rcu(fa, fa_head, fa_list) {
10970c838ff1SDavid S. Miller 		struct fib_info *next_fi = fa->fa_info;
10980c838ff1SDavid S. Miller 
109937e826c5SDavid S. Miller 		if (next_fi->fib_scope != res->scope ||
11000c838ff1SDavid S. Miller 		    fa->fa_type != RTN_UNICAST)
11010c838ff1SDavid S. Miller 			continue;
11020c838ff1SDavid S. Miller 
11030c838ff1SDavid S. Miller 		if (next_fi->fib_priority > res->fi->fib_priority)
11040c838ff1SDavid S. Miller 			break;
11050c838ff1SDavid S. Miller 		if (!next_fi->fib_nh[0].nh_gw ||
11060c838ff1SDavid S. Miller 		    next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
11070c838ff1SDavid S. Miller 			continue;
11080c838ff1SDavid S. Miller 
11090c838ff1SDavid S. Miller 		fib_alias_accessed(fa);
11100c838ff1SDavid S. Miller 
11110c838ff1SDavid S. Miller 		if (fi == NULL) {
11120c838ff1SDavid S. Miller 			if (next_fi != res->fi)
11130c838ff1SDavid S. Miller 				break;
11140c838ff1SDavid S. Miller 		} else if (!fib_detect_death(fi, order, &last_resort,
11150c838ff1SDavid S. Miller 					     &last_idx, tb->tb_default)) {
11160c838ff1SDavid S. Miller 			fib_result_assign(res, fi);
11170c838ff1SDavid S. Miller 			tb->tb_default = order;
11180c838ff1SDavid S. Miller 			goto out;
11190c838ff1SDavid S. Miller 		}
11200c838ff1SDavid S. Miller 		fi = next_fi;
11210c838ff1SDavid S. Miller 		order++;
11220c838ff1SDavid S. Miller 	}
11230c838ff1SDavid S. Miller 
11240c838ff1SDavid S. Miller 	if (order <= 0 || fi == NULL) {
11250c838ff1SDavid S. Miller 		tb->tb_default = -1;
11260c838ff1SDavid S. Miller 		goto out;
11270c838ff1SDavid S. Miller 	}
11280c838ff1SDavid S. Miller 
11290c838ff1SDavid S. Miller 	if (!fib_detect_death(fi, order, &last_resort, &last_idx,
11300c838ff1SDavid S. Miller 				tb->tb_default)) {
11310c838ff1SDavid S. Miller 		fib_result_assign(res, fi);
11320c838ff1SDavid S. Miller 		tb->tb_default = order;
11330c838ff1SDavid S. Miller 		goto out;
11340c838ff1SDavid S. Miller 	}
11350c838ff1SDavid S. Miller 
11360c838ff1SDavid S. Miller 	if (last_idx >= 0)
11370c838ff1SDavid S. Miller 		fib_result_assign(res, last_resort);
11380c838ff1SDavid S. Miller 	tb->tb_default = last_idx;
11390c838ff1SDavid S. Miller out:
114031d40937SEric Dumazet 	return;
11410c838ff1SDavid S. Miller }
11420c838ff1SDavid S. Miller 
11431da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds /*
11466a31d2a9SEric Dumazet  * Dead device goes up. We wake up dead nexthops.
11476a31d2a9SEric Dumazet  * It takes sense only on multipath routes.
11481da177e4SLinus Torvalds  */
11491da177e4SLinus Torvalds int fib_sync_up(struct net_device *dev)
11501da177e4SLinus Torvalds {
11511da177e4SLinus Torvalds 	struct fib_info *prev_fi;
11521da177e4SLinus Torvalds 	unsigned int hash;
11531da177e4SLinus Torvalds 	struct hlist_head *head;
11541da177e4SLinus Torvalds 	struct hlist_node *node;
11551da177e4SLinus Torvalds 	struct fib_nh *nh;
11561da177e4SLinus Torvalds 	int ret;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	if (!(dev->flags & IFF_UP))
11591da177e4SLinus Torvalds 		return 0;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	prev_fi = NULL;
11621da177e4SLinus Torvalds 	hash = fib_devindex_hashfn(dev->ifindex);
11631da177e4SLinus Torvalds 	head = &fib_info_devhash[hash];
11641da177e4SLinus Torvalds 	ret = 0;
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 	hlist_for_each_entry(nh, node, head, nh_hash) {
11671da177e4SLinus Torvalds 		struct fib_info *fi = nh->nh_parent;
11681da177e4SLinus Torvalds 		int alive;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 		BUG_ON(!fi->fib_nhs);
11711da177e4SLinus Torvalds 		if (nh->nh_dev != dev || fi == prev_fi)
11721da177e4SLinus Torvalds 			continue;
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 		prev_fi = fi;
11751da177e4SLinus Torvalds 		alive = 0;
11761da177e4SLinus Torvalds 		change_nexthops(fi) {
117771fceff0SDavid S. Miller 			if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
11781da177e4SLinus Torvalds 				alive++;
11791da177e4SLinus Torvalds 				continue;
11801da177e4SLinus Torvalds 			}
118171fceff0SDavid S. Miller 			if (nexthop_nh->nh_dev == NULL ||
118271fceff0SDavid S. Miller 			    !(nexthop_nh->nh_dev->flags & IFF_UP))
11831da177e4SLinus Torvalds 				continue;
118471fceff0SDavid S. Miller 			if (nexthop_nh->nh_dev != dev ||
118571fceff0SDavid S. Miller 			    !__in_dev_get_rtnl(dev))
11861da177e4SLinus Torvalds 				continue;
11871da177e4SLinus Torvalds 			alive++;
11881da177e4SLinus Torvalds 			spin_lock_bh(&fib_multipath_lock);
118971fceff0SDavid S. Miller 			nexthop_nh->nh_power = 0;
119071fceff0SDavid S. Miller 			nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
11911da177e4SLinus Torvalds 			spin_unlock_bh(&fib_multipath_lock);
11921da177e4SLinus Torvalds 		} endfor_nexthops(fi)
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 		if (alive > 0) {
11951da177e4SLinus Torvalds 			fi->fib_flags &= ~RTNH_F_DEAD;
11961da177e4SLinus Torvalds 			ret++;
11971da177e4SLinus Torvalds 		}
11981da177e4SLinus Torvalds 	}
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	return ret;
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds /*
12046a31d2a9SEric Dumazet  * The algorithm is suboptimal, but it provides really
12056a31d2a9SEric Dumazet  * fair weighted route distribution.
12061da177e4SLinus Torvalds  */
12071b7fe593SDavid S. Miller void fib_select_multipath(struct fib_result *res)
12081da177e4SLinus Torvalds {
12091da177e4SLinus Torvalds 	struct fib_info *fi = res->fi;
12101da177e4SLinus Torvalds 	int w;
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	spin_lock_bh(&fib_multipath_lock);
12131da177e4SLinus Torvalds 	if (fi->fib_power <= 0) {
12141da177e4SLinus Torvalds 		int power = 0;
12151da177e4SLinus Torvalds 		change_nexthops(fi) {
121671fceff0SDavid S. Miller 			if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
121771fceff0SDavid S. Miller 				power += nexthop_nh->nh_weight;
121871fceff0SDavid S. Miller 				nexthop_nh->nh_power = nexthop_nh->nh_weight;
12191da177e4SLinus Torvalds 			}
12201da177e4SLinus Torvalds 		} endfor_nexthops(fi);
12211da177e4SLinus Torvalds 		fi->fib_power = power;
12221da177e4SLinus Torvalds 		if (power <= 0) {
12231da177e4SLinus Torvalds 			spin_unlock_bh(&fib_multipath_lock);
12241da177e4SLinus Torvalds 			/* Race condition: route has just become dead. */
12251da177e4SLinus Torvalds 			res->nh_sel = 0;
12261da177e4SLinus Torvalds 			return;
12271da177e4SLinus Torvalds 		}
12281da177e4SLinus Torvalds 	}
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	/* w should be random number [0..fi->fib_power-1],
12326a31d2a9SEric Dumazet 	 * it is pretty bad approximation.
12331da177e4SLinus Torvalds 	 */
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	w = jiffies % fi->fib_power;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	change_nexthops(fi) {
123871fceff0SDavid S. Miller 		if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
123971fceff0SDavid S. Miller 		    nexthop_nh->nh_power) {
12406a31d2a9SEric Dumazet 			w -= nexthop_nh->nh_power;
12416a31d2a9SEric Dumazet 			if (w <= 0) {
124271fceff0SDavid S. Miller 				nexthop_nh->nh_power--;
12431da177e4SLinus Torvalds 				fi->fib_power--;
12441da177e4SLinus Torvalds 				res->nh_sel = nhsel;
12451da177e4SLinus Torvalds 				spin_unlock_bh(&fib_multipath_lock);
12461da177e4SLinus Torvalds 				return;
12471da177e4SLinus Torvalds 			}
12481da177e4SLinus Torvalds 		}
12491da177e4SLinus Torvalds 	} endfor_nexthops(fi);
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	/* Race condition: route has just become dead. */
12521da177e4SLinus Torvalds 	res->nh_sel = 0;
12531da177e4SLinus Torvalds 	spin_unlock_bh(&fib_multipath_lock);
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds #endif
1256