xref: /linux/net/ipv4/fib_semantics.c (revision 9c150e82ac50a611237bbebd508d17f6347d577c)
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;
521da177e4SLinus Torvalds static unsigned int fib_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 
939b5b5cffSArjan van de Ven static const struct
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	int	error;
961da177e4SLinus Torvalds 	u8	scope;
97a0ee18b9SThomas Graf } fib_props[RTN_MAX + 1] = {
986a31d2a9SEric Dumazet 	[RTN_UNSPEC] = {
991da177e4SLinus Torvalds 		.error	= 0,
1001da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1016a31d2a9SEric Dumazet 	},
1026a31d2a9SEric Dumazet 	[RTN_UNICAST] = {
1031da177e4SLinus Torvalds 		.error	= 0,
1041da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1056a31d2a9SEric Dumazet 	},
1066a31d2a9SEric Dumazet 	[RTN_LOCAL] = {
1071da177e4SLinus Torvalds 		.error	= 0,
1081da177e4SLinus Torvalds 		.scope	= RT_SCOPE_HOST,
1096a31d2a9SEric Dumazet 	},
1106a31d2a9SEric Dumazet 	[RTN_BROADCAST] = {
1111da177e4SLinus Torvalds 		.error	= 0,
1121da177e4SLinus Torvalds 		.scope	= RT_SCOPE_LINK,
1136a31d2a9SEric Dumazet 	},
1146a31d2a9SEric Dumazet 	[RTN_ANYCAST] = {
1151da177e4SLinus Torvalds 		.error	= 0,
1161da177e4SLinus Torvalds 		.scope	= RT_SCOPE_LINK,
1176a31d2a9SEric Dumazet 	},
1186a31d2a9SEric Dumazet 	[RTN_MULTICAST] = {
1191da177e4SLinus Torvalds 		.error	= 0,
1201da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1216a31d2a9SEric Dumazet 	},
1226a31d2a9SEric Dumazet 	[RTN_BLACKHOLE] = {
1231da177e4SLinus Torvalds 		.error	= -EINVAL,
1241da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1256a31d2a9SEric Dumazet 	},
1266a31d2a9SEric Dumazet 	[RTN_UNREACHABLE] = {
1271da177e4SLinus Torvalds 		.error	= -EHOSTUNREACH,
1281da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1296a31d2a9SEric Dumazet 	},
1306a31d2a9SEric Dumazet 	[RTN_PROHIBIT] = {
1311da177e4SLinus Torvalds 		.error	= -EACCES,
1321da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1336a31d2a9SEric Dumazet 	},
1346a31d2a9SEric Dumazet 	[RTN_THROW] = {
1351da177e4SLinus Torvalds 		.error	= -EAGAIN,
1361da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1376a31d2a9SEric Dumazet 	},
1386a31d2a9SEric Dumazet 	[RTN_NAT] = {
1391da177e4SLinus Torvalds 		.error	= -EINVAL,
1401da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1416a31d2a9SEric Dumazet 	},
1426a31d2a9SEric Dumazet 	[RTN_XRESOLVE] = {
1431da177e4SLinus Torvalds 		.error	= -EINVAL,
1441da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1456a31d2a9SEric Dumazet 	},
1461da177e4SLinus Torvalds };
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds /* Release a nexthop info record */
1501da177e4SLinus Torvalds 
151ebc0ffaeSEric Dumazet static void free_fib_info_rcu(struct rcu_head *head)
152ebc0ffaeSEric Dumazet {
153ebc0ffaeSEric Dumazet 	struct fib_info *fi = container_of(head, struct fib_info, rcu);
154ebc0ffaeSEric Dumazet 
155*9c150e82SDavid S. Miller 	kfree(fi->fib_metrics);
156ebc0ffaeSEric Dumazet 	kfree(fi);
157ebc0ffaeSEric Dumazet }
158ebc0ffaeSEric Dumazet 
1591da177e4SLinus Torvalds void free_fib_info(struct fib_info *fi)
1601da177e4SLinus Torvalds {
1611da177e4SLinus Torvalds 	if (fi->fib_dead == 0) {
1626a31d2a9SEric Dumazet 		pr_warning("Freeing alive fib_info %p\n", fi);
1631da177e4SLinus Torvalds 		return;
1641da177e4SLinus Torvalds 	}
1651da177e4SLinus Torvalds 	change_nexthops(fi) {
16671fceff0SDavid S. Miller 		if (nexthop_nh->nh_dev)
16771fceff0SDavid S. Miller 			dev_put(nexthop_nh->nh_dev);
16871fceff0SDavid S. Miller 		nexthop_nh->nh_dev = NULL;
1691da177e4SLinus Torvalds 	} endfor_nexthops(fi);
1701da177e4SLinus Torvalds 	fib_info_cnt--;
17157d7a600SDenis V. Lunev 	release_net(fi->fib_net);
172ebc0ffaeSEric Dumazet 	call_rcu(&fi->rcu, free_fib_info_rcu);
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds void fib_release_info(struct fib_info *fi)
1761da177e4SLinus Torvalds {
177832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
1781da177e4SLinus Torvalds 	if (fi && --fi->fib_treeref == 0) {
1791da177e4SLinus Torvalds 		hlist_del(&fi->fib_hash);
1801da177e4SLinus Torvalds 		if (fi->fib_prefsrc)
1811da177e4SLinus Torvalds 			hlist_del(&fi->fib_lhash);
1821da177e4SLinus Torvalds 		change_nexthops(fi) {
18371fceff0SDavid S. Miller 			if (!nexthop_nh->nh_dev)
1841da177e4SLinus Torvalds 				continue;
18571fceff0SDavid S. Miller 			hlist_del(&nexthop_nh->nh_hash);
1861da177e4SLinus Torvalds 		} endfor_nexthops(fi)
1871da177e4SLinus Torvalds 		fi->fib_dead = 1;
1881da177e4SLinus Torvalds 		fib_info_put(fi);
1891da177e4SLinus Torvalds 	}
190832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1936a31d2a9SEric Dumazet static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	const struct fib_nh *onh = ofi->fib_nh;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	for_nexthops(fi) {
1981da177e4SLinus Torvalds 		if (nh->nh_oif != onh->nh_oif ||
1991da177e4SLinus Torvalds 		    nh->nh_gw  != onh->nh_gw ||
2001da177e4SLinus Torvalds 		    nh->nh_scope != onh->nh_scope ||
2011da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
2021da177e4SLinus Torvalds 		    nh->nh_weight != onh->nh_weight ||
2031da177e4SLinus Torvalds #endif
204c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
2051da177e4SLinus Torvalds 		    nh->nh_tclassid != onh->nh_tclassid ||
2061da177e4SLinus Torvalds #endif
2071da177e4SLinus Torvalds 		    ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
2081da177e4SLinus Torvalds 			return -1;
2091da177e4SLinus Torvalds 		onh++;
2101da177e4SLinus Torvalds 	} endfor_nexthops(fi);
2111da177e4SLinus Torvalds 	return 0;
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
21488ebc72fSDavid S. Miller static inline unsigned int fib_devindex_hashfn(unsigned int val)
21588ebc72fSDavid S. Miller {
21688ebc72fSDavid S. Miller 	unsigned int mask = DEVINDEX_HASHSIZE - 1;
21788ebc72fSDavid S. Miller 
21888ebc72fSDavid S. Miller 	return (val ^
21988ebc72fSDavid S. Miller 		(val >> DEVINDEX_HASHBITS) ^
22088ebc72fSDavid S. Miller 		(val >> (DEVINDEX_HASHBITS * 2))) & mask;
22188ebc72fSDavid S. Miller }
22288ebc72fSDavid S. Miller 
2231da177e4SLinus Torvalds static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
2241da177e4SLinus Torvalds {
2251da177e4SLinus Torvalds 	unsigned int mask = (fib_hash_size - 1);
2261da177e4SLinus Torvalds 	unsigned int val = fi->fib_nhs;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	val ^= fi->fib_protocol;
22981f7bf6cSAl Viro 	val ^= (__force u32)fi->fib_prefsrc;
2301da177e4SLinus Torvalds 	val ^= fi->fib_priority;
23188ebc72fSDavid S. Miller 	for_nexthops(fi) {
23288ebc72fSDavid S. Miller 		val ^= fib_devindex_hashfn(nh->nh_oif);
23388ebc72fSDavid S. Miller 	} endfor_nexthops(fi)
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	return (val ^ (val >> 7) ^ (val >> 12)) & mask;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds static struct fib_info *fib_find_info(const struct fib_info *nfi)
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds 	struct hlist_head *head;
2411da177e4SLinus Torvalds 	struct hlist_node *node;
2421da177e4SLinus Torvalds 	struct fib_info *fi;
2431da177e4SLinus Torvalds 	unsigned int hash;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	hash = fib_info_hashfn(nfi);
2461da177e4SLinus Torvalds 	head = &fib_info_hash[hash];
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	hlist_for_each_entry(fi, node, head, fib_hash) {
24909ad9bc7SOctavian Purdila 		if (!net_eq(fi->fib_net, nfi->fib_net))
2504814bdbdSDenis V. Lunev 			continue;
2511da177e4SLinus Torvalds 		if (fi->fib_nhs != nfi->fib_nhs)
2521da177e4SLinus Torvalds 			continue;
2531da177e4SLinus Torvalds 		if (nfi->fib_protocol == fi->fib_protocol &&
2541da177e4SLinus Torvalds 		    nfi->fib_prefsrc == fi->fib_prefsrc &&
2551da177e4SLinus Torvalds 		    nfi->fib_priority == fi->fib_priority &&
2561da177e4SLinus Torvalds 		    memcmp(nfi->fib_metrics, fi->fib_metrics,
2571da177e4SLinus Torvalds 			   sizeof(fi->fib_metrics)) == 0 &&
2581da177e4SLinus Torvalds 		    ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
2591da177e4SLinus Torvalds 		    (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
2601da177e4SLinus Torvalds 			return fi;
2611da177e4SLinus Torvalds 	}
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	return NULL;
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds /* Check, that the gateway is already configured.
2676a31d2a9SEric Dumazet  * Used only by redirect accept routine.
2681da177e4SLinus Torvalds  */
269d878e72eSAl Viro int ip_fib_check_default(__be32 gw, struct net_device *dev)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	struct hlist_head *head;
2721da177e4SLinus Torvalds 	struct hlist_node *node;
2731da177e4SLinus Torvalds 	struct fib_nh *nh;
2741da177e4SLinus Torvalds 	unsigned int hash;
2751da177e4SLinus Torvalds 
276832b4c5eSStephen Hemminger 	spin_lock(&fib_info_lock);
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	hash = fib_devindex_hashfn(dev->ifindex);
2791da177e4SLinus Torvalds 	head = &fib_info_devhash[hash];
2801da177e4SLinus Torvalds 	hlist_for_each_entry(nh, node, head, nh_hash) {
2811da177e4SLinus Torvalds 		if (nh->nh_dev == dev &&
2821da177e4SLinus Torvalds 		    nh->nh_gw == gw &&
2831da177e4SLinus Torvalds 		    !(nh->nh_flags & RTNH_F_DEAD)) {
284832b4c5eSStephen Hemminger 			spin_unlock(&fib_info_lock);
2851da177e4SLinus Torvalds 			return 0;
2861da177e4SLinus Torvalds 		}
2871da177e4SLinus Torvalds 	}
2881da177e4SLinus Torvalds 
289832b4c5eSStephen Hemminger 	spin_unlock(&fib_info_lock);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	return -1;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
294339bf98fSThomas Graf static inline size_t fib_nlmsg_size(struct fib_info *fi)
295339bf98fSThomas Graf {
296339bf98fSThomas Graf 	size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
297339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_TABLE */
298339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_DST */
299339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_PRIORITY */
300339bf98fSThomas Graf 			 + nla_total_size(4); /* RTA_PREFSRC */
301339bf98fSThomas Graf 
302339bf98fSThomas Graf 	/* space for nested metrics */
303339bf98fSThomas Graf 	payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
304339bf98fSThomas Graf 
305339bf98fSThomas Graf 	if (fi->fib_nhs) {
306339bf98fSThomas Graf 		/* Also handles the special case fib_nhs == 1 */
307339bf98fSThomas Graf 
308339bf98fSThomas Graf 		/* each nexthop is packed in an attribute */
309339bf98fSThomas Graf 		size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
310339bf98fSThomas Graf 
311339bf98fSThomas Graf 		/* may contain flow and gateway attribute */
312339bf98fSThomas Graf 		nhsize += 2 * nla_total_size(4);
313339bf98fSThomas Graf 
314339bf98fSThomas Graf 		/* all nexthops are packed in a nested attribute */
315339bf98fSThomas Graf 		payload += nla_total_size(fi->fib_nhs * nhsize);
316339bf98fSThomas Graf 	}
317339bf98fSThomas Graf 
318339bf98fSThomas Graf 	return payload;
319339bf98fSThomas Graf }
320339bf98fSThomas Graf 
32181f7bf6cSAl Viro void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
322b8f55831SMilan Kocian 	       int dst_len, u32 tb_id, struct nl_info *info,
323b8f55831SMilan Kocian 	       unsigned int nlm_flags)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	struct sk_buff *skb;
3264e902c57SThomas Graf 	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
327f21c7bc5SThomas Graf 	int err = -ENOBUFS;
3281da177e4SLinus Torvalds 
329339bf98fSThomas Graf 	skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
330f21c7bc5SThomas Graf 	if (skb == NULL)
331f21c7bc5SThomas Graf 		goto errout;
3321da177e4SLinus Torvalds 
3334e902c57SThomas Graf 	err = fib_dump_info(skb, info->pid, seq, event, tb_id,
334be403ea1SThomas Graf 			    fa->fa_type, fa->fa_scope, key, dst_len,
335b8f55831SMilan Kocian 			    fa->fa_tos, fa->fa_info, nlm_flags);
33626932566SPatrick McHardy 	if (err < 0) {
33726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in fib_nlmsg_size() */
33826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
33926932566SPatrick McHardy 		kfree_skb(skb);
34026932566SPatrick McHardy 		goto errout;
34126932566SPatrick McHardy 	}
3421ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
3434e902c57SThomas Graf 		    info->nlh, GFP_KERNEL);
3441ce85fe4SPablo Neira Ayuso 	return;
345f21c7bc5SThomas Graf errout:
346f21c7bc5SThomas Graf 	if (err < 0)
3474d1169c1SDenis V. Lunev 		rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds /* Return the first fib alias matching TOS with
3511da177e4SLinus Torvalds  * priority less than or equal to PRIO.
3521da177e4SLinus Torvalds  */
3531da177e4SLinus Torvalds struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds 	if (fah) {
3561da177e4SLinus Torvalds 		struct fib_alias *fa;
3571da177e4SLinus Torvalds 		list_for_each_entry(fa, fah, fa_list) {
3581da177e4SLinus Torvalds 			if (fa->fa_tos > tos)
3591da177e4SLinus Torvalds 				continue;
3601da177e4SLinus Torvalds 			if (fa->fa_info->fib_priority >= prio ||
3611da177e4SLinus Torvalds 			    fa->fa_tos < tos)
3621da177e4SLinus Torvalds 				return fa;
3631da177e4SLinus Torvalds 		}
3641da177e4SLinus Torvalds 	}
3651da177e4SLinus Torvalds 	return NULL;
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds int fib_detect_death(struct fib_info *fi, int order,
369c17860a0SDenis V. Lunev 		     struct fib_info **last_resort, int *last_idx, int dflt)
3701da177e4SLinus Torvalds {
3711da177e4SLinus Torvalds 	struct neighbour *n;
3721da177e4SLinus Torvalds 	int state = NUD_NONE;
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
3751da177e4SLinus Torvalds 	if (n) {
3761da177e4SLinus Torvalds 		state = n->nud_state;
3771da177e4SLinus Torvalds 		neigh_release(n);
3781da177e4SLinus Torvalds 	}
3791da177e4SLinus Torvalds 	if (state == NUD_REACHABLE)
3801da177e4SLinus Torvalds 		return 0;
381c17860a0SDenis V. Lunev 	if ((state & NUD_VALID) && order != dflt)
3821da177e4SLinus Torvalds 		return 0;
3831da177e4SLinus Torvalds 	if ((state & NUD_VALID) ||
384c17860a0SDenis V. Lunev 	    (*last_idx < 0 && order > dflt)) {
3851da177e4SLinus Torvalds 		*last_resort = fi;
3861da177e4SLinus Torvalds 		*last_idx = order;
3871da177e4SLinus Torvalds 	}
3881da177e4SLinus Torvalds 	return 1;
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
3921da177e4SLinus Torvalds 
3934e902c57SThomas Graf static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	int nhs = 0;
3961da177e4SLinus Torvalds 
3974e902c57SThomas Graf 	while (rtnh_ok(rtnh, remaining)) {
3981da177e4SLinus Torvalds 		nhs++;
3994e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
4001da177e4SLinus Torvalds 	}
4011da177e4SLinus Torvalds 
4024e902c57SThomas Graf 	/* leftover implies invalid nexthop configuration, discard it */
4034e902c57SThomas Graf 	return remaining > 0 ? 0 : nhs;
4044e902c57SThomas Graf }
4051da177e4SLinus Torvalds 
4064e902c57SThomas Graf static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
4074e902c57SThomas Graf 		       int remaining, struct fib_config *cfg)
4084e902c57SThomas Graf {
4091da177e4SLinus Torvalds 	change_nexthops(fi) {
4104e902c57SThomas Graf 		int attrlen;
4114e902c57SThomas Graf 
4124e902c57SThomas Graf 		if (!rtnh_ok(rtnh, remaining))
4131da177e4SLinus Torvalds 			return -EINVAL;
4144e902c57SThomas Graf 
41571fceff0SDavid S. Miller 		nexthop_nh->nh_flags =
41671fceff0SDavid S. Miller 			(cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
41771fceff0SDavid S. Miller 		nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
41871fceff0SDavid S. Miller 		nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
4194e902c57SThomas Graf 
4204e902c57SThomas Graf 		attrlen = rtnh_attrlen(rtnh);
4214e902c57SThomas Graf 		if (attrlen > 0) {
4224e902c57SThomas Graf 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4234e902c57SThomas Graf 
4244e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
42571fceff0SDavid S. Miller 			nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
426c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
4274e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_FLOW);
42871fceff0SDavid S. Miller 			nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
4291da177e4SLinus Torvalds #endif
4301da177e4SLinus Torvalds 		}
4314e902c57SThomas Graf 
4324e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
4331da177e4SLinus Torvalds 	} endfor_nexthops(fi);
4344e902c57SThomas Graf 
4351da177e4SLinus Torvalds 	return 0;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds #endif
4391da177e4SLinus Torvalds 
4404e902c57SThomas Graf int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
4434e902c57SThomas Graf 	struct rtnexthop *rtnh;
4444e902c57SThomas Graf 	int remaining;
4451da177e4SLinus Torvalds #endif
4461da177e4SLinus Torvalds 
4474e902c57SThomas Graf 	if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
4481da177e4SLinus Torvalds 		return 1;
4491da177e4SLinus Torvalds 
4504e902c57SThomas Graf 	if (cfg->fc_oif || cfg->fc_gw) {
4514e902c57SThomas Graf 		if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
4524e902c57SThomas Graf 		    (!cfg->fc_gw  || cfg->fc_gw == fi->fib_nh->nh_gw))
4531da177e4SLinus Torvalds 			return 0;
4541da177e4SLinus Torvalds 		return 1;
4551da177e4SLinus Torvalds 	}
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
4584e902c57SThomas Graf 	if (cfg->fc_mp == NULL)
4591da177e4SLinus Torvalds 		return 0;
4604e902c57SThomas Graf 
4614e902c57SThomas Graf 	rtnh = cfg->fc_mp;
4624e902c57SThomas Graf 	remaining = cfg->fc_mp_len;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	for_nexthops(fi) {
4654e902c57SThomas Graf 		int attrlen;
4661da177e4SLinus Torvalds 
4674e902c57SThomas Graf 		if (!rtnh_ok(rtnh, remaining))
4681da177e4SLinus Torvalds 			return -EINVAL;
4694e902c57SThomas Graf 
4704e902c57SThomas Graf 		if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
4711da177e4SLinus Torvalds 			return 1;
4724e902c57SThomas Graf 
4734e902c57SThomas Graf 		attrlen = rtnh_attrlen(rtnh);
4744e902c57SThomas Graf 		if (attrlen < 0) {
4754e902c57SThomas Graf 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4764e902c57SThomas Graf 
4774e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
47817fb2c64SAl Viro 			if (nla && nla_get_be32(nla) != nh->nh_gw)
4791da177e4SLinus Torvalds 				return 1;
480c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
4814e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_FLOW);
4824e902c57SThomas Graf 			if (nla && nla_get_u32(nla) != nh->nh_tclassid)
4831da177e4SLinus Torvalds 				return 1;
4841da177e4SLinus Torvalds #endif
4851da177e4SLinus Torvalds 		}
4864e902c57SThomas Graf 
4874e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
4881da177e4SLinus Torvalds 	} endfor_nexthops(fi);
4891da177e4SLinus Torvalds #endif
4901da177e4SLinus Torvalds 	return 0;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds /*
4956a31d2a9SEric Dumazet  * Picture
4966a31d2a9SEric Dumazet  * -------
4976a31d2a9SEric Dumazet  *
4986a31d2a9SEric Dumazet  * Semantics of nexthop is very messy by historical reasons.
4996a31d2a9SEric Dumazet  * We have to take into account, that:
5006a31d2a9SEric Dumazet  * a) gateway can be actually local interface address,
5016a31d2a9SEric Dumazet  *    so that gatewayed route is direct.
5026a31d2a9SEric Dumazet  * b) gateway must be on-link address, possibly
5036a31d2a9SEric Dumazet  *    described not by an ifaddr, but also by a direct route.
5046a31d2a9SEric Dumazet  * c) If both gateway and interface are specified, they should not
5056a31d2a9SEric Dumazet  *    contradict.
5066a31d2a9SEric Dumazet  * d) If we use tunnel routes, gateway could be not on-link.
5076a31d2a9SEric Dumazet  *
5086a31d2a9SEric Dumazet  * Attempt to reconcile all of these (alas, self-contradictory) conditions
5096a31d2a9SEric Dumazet  * results in pretty ugly and hairy code with obscure logic.
5106a31d2a9SEric Dumazet  *
5116a31d2a9SEric Dumazet  * I chose to generalized it instead, so that the size
5126a31d2a9SEric Dumazet  * of code does not increase practically, but it becomes
5136a31d2a9SEric Dumazet  * much more general.
5146a31d2a9SEric Dumazet  * Every prefix is assigned a "scope" value: "host" is local address,
5156a31d2a9SEric Dumazet  * "link" is direct route,
5166a31d2a9SEric Dumazet  * [ ... "site" ... "interior" ... ]
5176a31d2a9SEric Dumazet  * and "universe" is true gateway route with global meaning.
5186a31d2a9SEric Dumazet  *
5196a31d2a9SEric Dumazet  * Every prefix refers to a set of "nexthop"s (gw, oif),
5206a31d2a9SEric Dumazet  * where gw must have narrower scope. This recursion stops
5216a31d2a9SEric Dumazet  * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
5226a31d2a9SEric Dumazet  * which means that gw is forced to be on link.
5236a31d2a9SEric Dumazet  *
5246a31d2a9SEric Dumazet  * Code is still hairy, but now it is apparently logically
5256a31d2a9SEric Dumazet  * consistent and very flexible. F.e. as by-product it allows
5266a31d2a9SEric Dumazet  * to co-exists in peace independent exterior and interior
5276a31d2a9SEric Dumazet  * routing processes.
5286a31d2a9SEric Dumazet  *
5296a31d2a9SEric Dumazet  * Normally it looks as following.
5306a31d2a9SEric Dumazet  *
5316a31d2a9SEric Dumazet  * {universe prefix}  -> (gw, oif) [scope link]
5326a31d2a9SEric Dumazet  *		  |
5336a31d2a9SEric Dumazet  *		  |-> {link prefix} -> (gw, oif) [scope local]
5346a31d2a9SEric Dumazet  *					|
5356a31d2a9SEric Dumazet  *					|-> {local prefix} (terminal node)
5361da177e4SLinus Torvalds  */
5374e902c57SThomas Graf static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
5384e902c57SThomas Graf 			struct fib_nh *nh)
5391da177e4SLinus Torvalds {
5401da177e4SLinus Torvalds 	int err;
54186167a37SDenis V. Lunev 	struct net *net;
5426a31d2a9SEric Dumazet 	struct net_device *dev;
5431da177e4SLinus Torvalds 
54486167a37SDenis V. Lunev 	net = cfg->fc_nlinfo.nl_net;
5451da177e4SLinus Torvalds 	if (nh->nh_gw) {
5461da177e4SLinus Torvalds 		struct fib_result res;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 		if (nh->nh_flags & RTNH_F_ONLINK) {
5491da177e4SLinus Torvalds 
5504e902c57SThomas Graf 			if (cfg->fc_scope >= RT_SCOPE_LINK)
5511da177e4SLinus Torvalds 				return -EINVAL;
55286167a37SDenis V. Lunev 			if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
5531da177e4SLinus Torvalds 				return -EINVAL;
5546a31d2a9SEric Dumazet 			dev = __dev_get_by_index(net, nh->nh_oif);
5556a31d2a9SEric Dumazet 			if (!dev)
5561da177e4SLinus Torvalds 				return -ENODEV;
5571da177e4SLinus Torvalds 			if (!(dev->flags & IFF_UP))
5581da177e4SLinus Torvalds 				return -ENETDOWN;
5591da177e4SLinus Torvalds 			nh->nh_dev = dev;
5601da177e4SLinus Torvalds 			dev_hold(dev);
5611da177e4SLinus Torvalds 			nh->nh_scope = RT_SCOPE_LINK;
5621da177e4SLinus Torvalds 			return 0;
5631da177e4SLinus Torvalds 		}
564ebc0ffaeSEric Dumazet 		rcu_read_lock();
5651da177e4SLinus Torvalds 		{
5664e902c57SThomas Graf 			struct flowi fl = {
5675811662bSChangli Gao 				.fl4_dst = nh->nh_gw,
5685811662bSChangli Gao 				.fl4_scope = cfg->fc_scope + 1,
5694e902c57SThomas Graf 				.oif = nh->nh_oif,
5704e902c57SThomas Graf 			};
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 			/* It is not necessary, but requires a bit of thinking */
5731da177e4SLinus Torvalds 			if (fl.fl4_scope < RT_SCOPE_LINK)
5741da177e4SLinus Torvalds 				fl.fl4_scope = RT_SCOPE_LINK;
5756a31d2a9SEric Dumazet 			err = fib_lookup(net, &fl, &res);
576ebc0ffaeSEric Dumazet 			if (err) {
577ebc0ffaeSEric Dumazet 				rcu_read_unlock();
5781da177e4SLinus Torvalds 				return err;
5791da177e4SLinus Torvalds 			}
580ebc0ffaeSEric Dumazet 		}
5811da177e4SLinus Torvalds 		err = -EINVAL;
5821da177e4SLinus Torvalds 		if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
5831da177e4SLinus Torvalds 			goto out;
5841da177e4SLinus Torvalds 		nh->nh_scope = res.scope;
5851da177e4SLinus Torvalds 		nh->nh_oif = FIB_RES_OIF(res);
5866a31d2a9SEric Dumazet 		nh->nh_dev = dev = FIB_RES_DEV(res);
5876a31d2a9SEric Dumazet 		if (!dev)
5881da177e4SLinus Torvalds 			goto out;
5896a31d2a9SEric Dumazet 		dev_hold(dev);
5908723e1b4SEric Dumazet 		err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
5911da177e4SLinus Torvalds 	} else {
5921da177e4SLinus Torvalds 		struct in_device *in_dev;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 		if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
5951da177e4SLinus Torvalds 			return -EINVAL;
5961da177e4SLinus Torvalds 
5978723e1b4SEric Dumazet 		rcu_read_lock();
5988723e1b4SEric Dumazet 		err = -ENODEV;
59986167a37SDenis V. Lunev 		in_dev = inetdev_by_index(net, nh->nh_oif);
6001da177e4SLinus Torvalds 		if (in_dev == NULL)
6018723e1b4SEric Dumazet 			goto out;
6028723e1b4SEric Dumazet 		err = -ENETDOWN;
6038723e1b4SEric Dumazet 		if (!(in_dev->dev->flags & IFF_UP))
6048723e1b4SEric Dumazet 			goto out;
6051da177e4SLinus Torvalds 		nh->nh_dev = in_dev->dev;
6061da177e4SLinus Torvalds 		dev_hold(nh->nh_dev);
6071da177e4SLinus Torvalds 		nh->nh_scope = RT_SCOPE_HOST;
6088723e1b4SEric Dumazet 		err = 0;
6091da177e4SLinus Torvalds 	}
6108723e1b4SEric Dumazet out:
6118723e1b4SEric Dumazet 	rcu_read_unlock();
6128723e1b4SEric Dumazet 	return err;
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
61581f7bf6cSAl Viro static inline unsigned int fib_laddr_hashfn(__be32 val)
6161da177e4SLinus Torvalds {
6171da177e4SLinus Torvalds 	unsigned int mask = (fib_hash_size - 1);
6181da177e4SLinus Torvalds 
6196a31d2a9SEric Dumazet 	return ((__force u32)val ^
6206a31d2a9SEric Dumazet 		((__force u32)val >> 7) ^
6216a31d2a9SEric Dumazet 		((__force u32)val >> 14)) & mask;
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds static struct hlist_head *fib_hash_alloc(int bytes)
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds 	if (bytes <= PAGE_SIZE)
62788f83491SJoonwoo Park 		return kzalloc(bytes, GFP_KERNEL);
6281da177e4SLinus Torvalds 	else
6291da177e4SLinus Torvalds 		return (struct hlist_head *)
6306a31d2a9SEric Dumazet 			__get_free_pages(GFP_KERNEL | __GFP_ZERO,
6316a31d2a9SEric Dumazet 					 get_order(bytes));
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds static void fib_hash_free(struct hlist_head *hash, int bytes)
6351da177e4SLinus Torvalds {
6361da177e4SLinus Torvalds 	if (!hash)
6371da177e4SLinus Torvalds 		return;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	if (bytes <= PAGE_SIZE)
6401da177e4SLinus Torvalds 		kfree(hash);
6411da177e4SLinus Torvalds 	else
6421da177e4SLinus Torvalds 		free_pages((unsigned long) hash, get_order(bytes));
6431da177e4SLinus Torvalds }
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds static void fib_hash_move(struct hlist_head *new_info_hash,
6461da177e4SLinus Torvalds 			  struct hlist_head *new_laddrhash,
6471da177e4SLinus Torvalds 			  unsigned int new_size)
6481da177e4SLinus Torvalds {
649b7656e7fSDavid S. Miller 	struct hlist_head *old_info_hash, *old_laddrhash;
6501da177e4SLinus Torvalds 	unsigned int old_size = fib_hash_size;
651b7656e7fSDavid S. Miller 	unsigned int i, bytes;
6521da177e4SLinus Torvalds 
653832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
654b7656e7fSDavid S. Miller 	old_info_hash = fib_info_hash;
655b7656e7fSDavid S. Miller 	old_laddrhash = fib_info_laddrhash;
6561da177e4SLinus Torvalds 	fib_hash_size = new_size;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	for (i = 0; i < old_size; i++) {
6591da177e4SLinus Torvalds 		struct hlist_head *head = &fib_info_hash[i];
6601da177e4SLinus Torvalds 		struct hlist_node *node, *n;
6611da177e4SLinus Torvalds 		struct fib_info *fi;
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 		hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
6641da177e4SLinus Torvalds 			struct hlist_head *dest;
6651da177e4SLinus Torvalds 			unsigned int new_hash;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 			hlist_del(&fi->fib_hash);
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 			new_hash = fib_info_hashfn(fi);
6701da177e4SLinus Torvalds 			dest = &new_info_hash[new_hash];
6711da177e4SLinus Torvalds 			hlist_add_head(&fi->fib_hash, dest);
6721da177e4SLinus Torvalds 		}
6731da177e4SLinus Torvalds 	}
6741da177e4SLinus Torvalds 	fib_info_hash = new_info_hash;
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	for (i = 0; i < old_size; i++) {
6771da177e4SLinus Torvalds 		struct hlist_head *lhead = &fib_info_laddrhash[i];
6781da177e4SLinus Torvalds 		struct hlist_node *node, *n;
6791da177e4SLinus Torvalds 		struct fib_info *fi;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 		hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
6821da177e4SLinus Torvalds 			struct hlist_head *ldest;
6831da177e4SLinus Torvalds 			unsigned int new_hash;
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 			hlist_del(&fi->fib_lhash);
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 			new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
6881da177e4SLinus Torvalds 			ldest = &new_laddrhash[new_hash];
6891da177e4SLinus Torvalds 			hlist_add_head(&fi->fib_lhash, ldest);
6901da177e4SLinus Torvalds 		}
6911da177e4SLinus Torvalds 	}
6921da177e4SLinus Torvalds 	fib_info_laddrhash = new_laddrhash;
6931da177e4SLinus Torvalds 
694832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
695b7656e7fSDavid S. Miller 
696b7656e7fSDavid S. Miller 	bytes = old_size * sizeof(struct hlist_head *);
697b7656e7fSDavid S. Miller 	fib_hash_free(old_info_hash, bytes);
698b7656e7fSDavid S. Miller 	fib_hash_free(old_laddrhash, bytes);
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
7014e902c57SThomas Graf struct fib_info *fib_create_info(struct fib_config *cfg)
7021da177e4SLinus Torvalds {
7031da177e4SLinus Torvalds 	int err;
7041da177e4SLinus Torvalds 	struct fib_info *fi = NULL;
7051da177e4SLinus Torvalds 	struct fib_info *ofi;
7061da177e4SLinus Torvalds 	int nhs = 1;
7077462bd74SDenis V. Lunev 	struct net *net = cfg->fc_nlinfo.nl_net;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	/* Fast check to catch the most weird cases */
7104e902c57SThomas Graf 	if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
7111da177e4SLinus Torvalds 		goto err_inval;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
7144e902c57SThomas Graf 	if (cfg->fc_mp) {
7154e902c57SThomas Graf 		nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
7161da177e4SLinus Torvalds 		if (nhs == 0)
7171da177e4SLinus Torvalds 			goto err_inval;
7181da177e4SLinus Torvalds 	}
7191da177e4SLinus Torvalds #endif
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	err = -ENOBUFS;
7221da177e4SLinus Torvalds 	if (fib_info_cnt >= fib_hash_size) {
7231da177e4SLinus Torvalds 		unsigned int new_size = fib_hash_size << 1;
7241da177e4SLinus Torvalds 		struct hlist_head *new_info_hash;
7251da177e4SLinus Torvalds 		struct hlist_head *new_laddrhash;
7261da177e4SLinus Torvalds 		unsigned int bytes;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 		if (!new_size)
7291da177e4SLinus Torvalds 			new_size = 1;
7301da177e4SLinus Torvalds 		bytes = new_size * sizeof(struct hlist_head *);
7311da177e4SLinus Torvalds 		new_info_hash = fib_hash_alloc(bytes);
7321da177e4SLinus Torvalds 		new_laddrhash = fib_hash_alloc(bytes);
7331da177e4SLinus Torvalds 		if (!new_info_hash || !new_laddrhash) {
7341da177e4SLinus Torvalds 			fib_hash_free(new_info_hash, bytes);
7351da177e4SLinus Torvalds 			fib_hash_free(new_laddrhash, bytes);
73688f83491SJoonwoo Park 		} else
7371da177e4SLinus Torvalds 			fib_hash_move(new_info_hash, new_laddrhash, new_size);
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 		if (!fib_hash_size)
7401da177e4SLinus Torvalds 			goto failure;
7411da177e4SLinus Torvalds 	}
7421da177e4SLinus Torvalds 
7430da974f4SPanagiotis Issaris 	fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
7441da177e4SLinus Torvalds 	if (fi == NULL)
7451da177e4SLinus Torvalds 		goto failure;
746*9c150e82SDavid S. Miller 	fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
747*9c150e82SDavid S. Miller 	if (!fi->fib_metrics)
748*9c150e82SDavid S. Miller 		goto failure;
7491da177e4SLinus Torvalds 	fib_info_cnt++;
7501da177e4SLinus Torvalds 
75157d7a600SDenis V. Lunev 	fi->fib_net = hold_net(net);
7524e902c57SThomas Graf 	fi->fib_protocol = cfg->fc_protocol;
7534e902c57SThomas Graf 	fi->fib_flags = cfg->fc_flags;
7544e902c57SThomas Graf 	fi->fib_priority = cfg->fc_priority;
7554e902c57SThomas Graf 	fi->fib_prefsrc = cfg->fc_prefsrc;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	fi->fib_nhs = nhs;
7581da177e4SLinus Torvalds 	change_nexthops(fi) {
75971fceff0SDavid S. Miller 		nexthop_nh->nh_parent = fi;
7601da177e4SLinus Torvalds 	} endfor_nexthops(fi)
7611da177e4SLinus Torvalds 
7624e902c57SThomas Graf 	if (cfg->fc_mx) {
7634e902c57SThomas Graf 		struct nlattr *nla;
7644e902c57SThomas Graf 		int remaining;
7651da177e4SLinus Torvalds 
7664e902c57SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
7678f4c1f9bSThomas Graf 			int type = nla_type(nla);
7684e902c57SThomas Graf 
7694e902c57SThomas Graf 			if (type) {
7704e902c57SThomas Graf 				if (type > RTAX_MAX)
7711da177e4SLinus Torvalds 					goto err_inval;
7724e902c57SThomas Graf 				fi->fib_metrics[type - 1] = nla_get_u32(nla);
7731da177e4SLinus Torvalds 			}
7741da177e4SLinus Torvalds 		}
7754e902c57SThomas Graf 	}
7761da177e4SLinus Torvalds 
7774e902c57SThomas Graf 	if (cfg->fc_mp) {
7781da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
7794e902c57SThomas Graf 		err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
7804e902c57SThomas Graf 		if (err != 0)
7811da177e4SLinus Torvalds 			goto failure;
7824e902c57SThomas Graf 		if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
7831da177e4SLinus Torvalds 			goto err_inval;
7844e902c57SThomas Graf 		if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
7851da177e4SLinus Torvalds 			goto err_inval;
786c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
7874e902c57SThomas Graf 		if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
7881da177e4SLinus Torvalds 			goto err_inval;
7891da177e4SLinus Torvalds #endif
7901da177e4SLinus Torvalds #else
7911da177e4SLinus Torvalds 		goto err_inval;
7921da177e4SLinus Torvalds #endif
7931da177e4SLinus Torvalds 	} else {
7941da177e4SLinus Torvalds 		struct fib_nh *nh = fi->fib_nh;
7954e902c57SThomas Graf 
7964e902c57SThomas Graf 		nh->nh_oif = cfg->fc_oif;
7974e902c57SThomas Graf 		nh->nh_gw = cfg->fc_gw;
7984e902c57SThomas Graf 		nh->nh_flags = cfg->fc_flags;
799c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
8004e902c57SThomas Graf 		nh->nh_tclassid = cfg->fc_flow;
8011da177e4SLinus Torvalds #endif
8021da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
8031da177e4SLinus Torvalds 		nh->nh_weight = 1;
8041da177e4SLinus Torvalds #endif
8051da177e4SLinus Torvalds 	}
8061da177e4SLinus Torvalds 
8074e902c57SThomas Graf 	if (fib_props[cfg->fc_type].error) {
8084e902c57SThomas Graf 		if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
8091da177e4SLinus Torvalds 			goto err_inval;
8101da177e4SLinus Torvalds 		goto link_it;
8111da177e4SLinus Torvalds 	}
8121da177e4SLinus Torvalds 
8134e902c57SThomas Graf 	if (cfg->fc_scope > RT_SCOPE_HOST)
8141da177e4SLinus Torvalds 		goto err_inval;
8151da177e4SLinus Torvalds 
8164e902c57SThomas Graf 	if (cfg->fc_scope == RT_SCOPE_HOST) {
8171da177e4SLinus Torvalds 		struct fib_nh *nh = fi->fib_nh;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 		/* Local address is added. */
8201da177e4SLinus Torvalds 		if (nhs != 1 || nh->nh_gw)
8211da177e4SLinus Torvalds 			goto err_inval;
8221da177e4SLinus Torvalds 		nh->nh_scope = RT_SCOPE_NOWHERE;
8237462bd74SDenis V. Lunev 		nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
8241da177e4SLinus Torvalds 		err = -ENODEV;
8251da177e4SLinus Torvalds 		if (nh->nh_dev == NULL)
8261da177e4SLinus Torvalds 			goto failure;
8271da177e4SLinus Torvalds 	} else {
8281da177e4SLinus Torvalds 		change_nexthops(fi) {
8296a31d2a9SEric Dumazet 			err = fib_check_nh(cfg, fi, nexthop_nh);
8306a31d2a9SEric Dumazet 			if (err != 0)
8311da177e4SLinus Torvalds 				goto failure;
8321da177e4SLinus Torvalds 		} endfor_nexthops(fi)
8331da177e4SLinus Torvalds 	}
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	if (fi->fib_prefsrc) {
8364e902c57SThomas Graf 		if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
8374e902c57SThomas Graf 		    fi->fib_prefsrc != cfg->fc_dst)
8387462bd74SDenis V. Lunev 			if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
8391da177e4SLinus Torvalds 				goto err_inval;
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds link_it:
8436a31d2a9SEric Dumazet 	ofi = fib_find_info(fi);
8446a31d2a9SEric Dumazet 	if (ofi) {
8451da177e4SLinus Torvalds 		fi->fib_dead = 1;
8461da177e4SLinus Torvalds 		free_fib_info(fi);
8471da177e4SLinus Torvalds 		ofi->fib_treeref++;
8481da177e4SLinus Torvalds 		return ofi;
8491da177e4SLinus Torvalds 	}
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	fi->fib_treeref++;
8521da177e4SLinus Torvalds 	atomic_inc(&fi->fib_clntref);
853832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
8541da177e4SLinus Torvalds 	hlist_add_head(&fi->fib_hash,
8551da177e4SLinus Torvalds 		       &fib_info_hash[fib_info_hashfn(fi)]);
8561da177e4SLinus Torvalds 	if (fi->fib_prefsrc) {
8571da177e4SLinus Torvalds 		struct hlist_head *head;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 		head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
8601da177e4SLinus Torvalds 		hlist_add_head(&fi->fib_lhash, head);
8611da177e4SLinus Torvalds 	}
8621da177e4SLinus Torvalds 	change_nexthops(fi) {
8631da177e4SLinus Torvalds 		struct hlist_head *head;
8641da177e4SLinus Torvalds 		unsigned int hash;
8651da177e4SLinus Torvalds 
86671fceff0SDavid S. Miller 		if (!nexthop_nh->nh_dev)
8671da177e4SLinus Torvalds 			continue;
86871fceff0SDavid S. Miller 		hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
8691da177e4SLinus Torvalds 		head = &fib_info_devhash[hash];
87071fceff0SDavid S. Miller 		hlist_add_head(&nexthop_nh->nh_hash, head);
8711da177e4SLinus Torvalds 	} endfor_nexthops(fi)
872832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
8731da177e4SLinus Torvalds 	return fi;
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds err_inval:
8761da177e4SLinus Torvalds 	err = -EINVAL;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds failure:
8791da177e4SLinus Torvalds 	if (fi) {
8801da177e4SLinus Torvalds 		fi->fib_dead = 1;
8811da177e4SLinus Torvalds 		free_fib_info(fi);
8821da177e4SLinus Torvalds 	}
8834e902c57SThomas Graf 
8844e902c57SThomas Graf 	return ERR_PTR(err);
8851da177e4SLinus Torvalds }
8861da177e4SLinus Torvalds 
887e5b43760SRobert Olsson /* Note! fib_semantic_match intentionally uses  RCU list functions. */
8881da177e4SLinus Torvalds int fib_semantic_match(struct list_head *head, const struct flowi *flp,
889ebc0ffaeSEric Dumazet 		       struct fib_result *res, int prefixlen, int fib_flags)
8901da177e4SLinus Torvalds {
8911da177e4SLinus Torvalds 	struct fib_alias *fa;
8921da177e4SLinus Torvalds 	int nh_sel = 0;
8931da177e4SLinus Torvalds 
894e5b43760SRobert Olsson 	list_for_each_entry_rcu(fa, head, fa_list) {
8951da177e4SLinus Torvalds 		int err;
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 		if (fa->fa_tos &&
8981da177e4SLinus Torvalds 		    fa->fa_tos != flp->fl4_tos)
8991da177e4SLinus Torvalds 			continue;
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 		if (fa->fa_scope < flp->fl4_scope)
9021da177e4SLinus Torvalds 			continue;
9031da177e4SLinus Torvalds 
9049b0c290eSEric Dumazet 		fib_alias_accessed(fa);
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 		err = fib_props[fa->fa_type].error;
9071da177e4SLinus Torvalds 		if (err == 0) {
9081da177e4SLinus Torvalds 			struct fib_info *fi = fa->fa_info;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 			if (fi->fib_flags & RTNH_F_DEAD)
9111da177e4SLinus Torvalds 				continue;
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 			switch (fa->fa_type) {
9141da177e4SLinus Torvalds 			case RTN_UNICAST:
9151da177e4SLinus Torvalds 			case RTN_LOCAL:
9161da177e4SLinus Torvalds 			case RTN_BROADCAST:
9171da177e4SLinus Torvalds 			case RTN_ANYCAST:
9181da177e4SLinus Torvalds 			case RTN_MULTICAST:
9191da177e4SLinus Torvalds 				for_nexthops(fi) {
9201da177e4SLinus Torvalds 					if (nh->nh_flags & RTNH_F_DEAD)
9211da177e4SLinus Torvalds 						continue;
9221da177e4SLinus Torvalds 					if (!flp->oif || flp->oif == nh->nh_oif)
9231da177e4SLinus Torvalds 						break;
9241da177e4SLinus Torvalds 				}
9251da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
9261da177e4SLinus Torvalds 				if (nhsel < fi->fib_nhs) {
9271da177e4SLinus Torvalds 					nh_sel = nhsel;
9281da177e4SLinus Torvalds 					goto out_fill_res;
9291da177e4SLinus Torvalds 				}
9301da177e4SLinus Torvalds #else
9316a31d2a9SEric Dumazet 				if (nhsel < 1)
9321da177e4SLinus Torvalds 					goto out_fill_res;
9331da177e4SLinus Torvalds #endif
9341da177e4SLinus Torvalds 				endfor_nexthops(fi);
9351da177e4SLinus Torvalds 				continue;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 			default:
9386a31d2a9SEric Dumazet 				pr_warning("fib_semantic_match bad type %#x\n",
939a6db9010SStephen Hemminger 					   fa->fa_type);
9401da177e4SLinus Torvalds 				return -EINVAL;
9413ff50b79SStephen Hemminger 			}
9421da177e4SLinus Torvalds 		}
9431da177e4SLinus Torvalds 		return err;
9441da177e4SLinus Torvalds 	}
9451da177e4SLinus Torvalds 	return 1;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds out_fill_res:
9481da177e4SLinus Torvalds 	res->prefixlen = prefixlen;
9491da177e4SLinus Torvalds 	res->nh_sel = nh_sel;
9501da177e4SLinus Torvalds 	res->type = fa->fa_type;
9511da177e4SLinus Torvalds 	res->scope = fa->fa_scope;
9521da177e4SLinus Torvalds 	res->fi = fa->fa_info;
953ebc0ffaeSEric Dumazet 	if (!(fib_flags & FIB_LOOKUP_NOREF))
9541da177e4SLinus Torvalds 		atomic_inc(&res->fi->fib_clntref);
9551da177e4SLinus Torvalds 	return 0;
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds /* Find appropriate source address to this destination */
9591da177e4SLinus Torvalds 
960b83738aeSAl Viro __be32 __fib_res_prefsrc(struct fib_result *res)
9611da177e4SLinus Torvalds {
9621da177e4SLinus Torvalds 	return inet_select_addr(FIB_RES_DEV(*res), FIB_RES_GW(*res), res->scope);
9631da177e4SLinus Torvalds }
9641da177e4SLinus Torvalds 
965be403ea1SThomas Graf int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
96681f7bf6cSAl Viro 		  u32 tb_id, u8 type, u8 scope, __be32 dst, int dst_len, u8 tos,
967b6544c0bSJamal Hadi Salim 		  struct fib_info *fi, unsigned int flags)
9681da177e4SLinus Torvalds {
9691da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
970be403ea1SThomas Graf 	struct rtmsg *rtm;
9711da177e4SLinus Torvalds 
972be403ea1SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
973be403ea1SThomas Graf 	if (nlh == NULL)
97426932566SPatrick McHardy 		return -EMSGSIZE;
975be403ea1SThomas Graf 
976be403ea1SThomas Graf 	rtm = nlmsg_data(nlh);
9771da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET;
9781da177e4SLinus Torvalds 	rtm->rtm_dst_len = dst_len;
9791da177e4SLinus Torvalds 	rtm->rtm_src_len = 0;
9801da177e4SLinus Torvalds 	rtm->rtm_tos = tos;
981709772e6SKrzysztof Piotr Oledzki 	if (tb_id < 256)
9821da177e4SLinus Torvalds 		rtm->rtm_table = tb_id;
983709772e6SKrzysztof Piotr Oledzki 	else
984709772e6SKrzysztof Piotr Oledzki 		rtm->rtm_table = RT_TABLE_COMPAT;
985be403ea1SThomas Graf 	NLA_PUT_U32(skb, RTA_TABLE, tb_id);
9861da177e4SLinus Torvalds 	rtm->rtm_type = type;
9871da177e4SLinus Torvalds 	rtm->rtm_flags = fi->fib_flags;
9881da177e4SLinus Torvalds 	rtm->rtm_scope = scope;
9891da177e4SLinus Torvalds 	rtm->rtm_protocol = fi->fib_protocol;
990be403ea1SThomas Graf 
991be403ea1SThomas Graf 	if (rtm->rtm_dst_len)
99217fb2c64SAl Viro 		NLA_PUT_BE32(skb, RTA_DST, dst);
993be403ea1SThomas Graf 
9941da177e4SLinus Torvalds 	if (fi->fib_priority)
995be403ea1SThomas Graf 		NLA_PUT_U32(skb, RTA_PRIORITY, fi->fib_priority);
996be403ea1SThomas Graf 
9971da177e4SLinus Torvalds 	if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
998be403ea1SThomas Graf 		goto nla_put_failure;
999be403ea1SThomas Graf 
10001da177e4SLinus Torvalds 	if (fi->fib_prefsrc)
100117fb2c64SAl Viro 		NLA_PUT_BE32(skb, RTA_PREFSRC, fi->fib_prefsrc);
1002be403ea1SThomas Graf 
10031da177e4SLinus Torvalds 	if (fi->fib_nhs == 1) {
10041da177e4SLinus Torvalds 		if (fi->fib_nh->nh_gw)
100517fb2c64SAl Viro 			NLA_PUT_BE32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw);
1006be403ea1SThomas Graf 
10071da177e4SLinus Torvalds 		if (fi->fib_nh->nh_oif)
1008be403ea1SThomas Graf 			NLA_PUT_U32(skb, RTA_OIF, fi->fib_nh->nh_oif);
1009c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
10108265abc0SPatrick McHardy 		if (fi->fib_nh[0].nh_tclassid)
1011be403ea1SThomas Graf 			NLA_PUT_U32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid);
10128265abc0SPatrick McHardy #endif
10131da177e4SLinus Torvalds 	}
10141da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
10151da177e4SLinus Torvalds 	if (fi->fib_nhs > 1) {
1016be403ea1SThomas Graf 		struct rtnexthop *rtnh;
1017be403ea1SThomas Graf 		struct nlattr *mp;
1018be403ea1SThomas Graf 
1019be403ea1SThomas Graf 		mp = nla_nest_start(skb, RTA_MULTIPATH);
1020be403ea1SThomas Graf 		if (mp == NULL)
1021be403ea1SThomas Graf 			goto nla_put_failure;
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 		for_nexthops(fi) {
1024be403ea1SThomas Graf 			rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1025be403ea1SThomas Graf 			if (rtnh == NULL)
1026be403ea1SThomas Graf 				goto nla_put_failure;
1027be403ea1SThomas Graf 
1028be403ea1SThomas Graf 			rtnh->rtnh_flags = nh->nh_flags & 0xFF;
1029be403ea1SThomas Graf 			rtnh->rtnh_hops = nh->nh_weight - 1;
1030be403ea1SThomas Graf 			rtnh->rtnh_ifindex = nh->nh_oif;
1031be403ea1SThomas Graf 
10321da177e4SLinus Torvalds 			if (nh->nh_gw)
103317fb2c64SAl Viro 				NLA_PUT_BE32(skb, RTA_GATEWAY, nh->nh_gw);
1034c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
10358265abc0SPatrick McHardy 			if (nh->nh_tclassid)
1036be403ea1SThomas Graf 				NLA_PUT_U32(skb, RTA_FLOW, nh->nh_tclassid);
10378265abc0SPatrick McHardy #endif
1038be403ea1SThomas Graf 			/* length of rtnetlink header + attributes */
1039be403ea1SThomas Graf 			rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
10401da177e4SLinus Torvalds 		} endfor_nexthops(fi);
1041be403ea1SThomas Graf 
1042be403ea1SThomas Graf 		nla_nest_end(skb, mp);
10431da177e4SLinus Torvalds 	}
10441da177e4SLinus Torvalds #endif
1045be403ea1SThomas Graf 	return nlmsg_end(skb, nlh);
10461da177e4SLinus Torvalds 
1047be403ea1SThomas Graf nla_put_failure:
104826932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
104926932566SPatrick McHardy 	return -EMSGSIZE;
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds /*
10536a31d2a9SEric Dumazet  * Update FIB if:
10546a31d2a9SEric Dumazet  * - local address disappeared -> we must delete all the entries
10556a31d2a9SEric Dumazet  *   referring to it.
10566a31d2a9SEric Dumazet  * - device went down -> we must shutdown all nexthops going via it.
10571da177e4SLinus Torvalds  */
10584814bdbdSDenis V. Lunev int fib_sync_down_addr(struct net *net, __be32 local)
10591da177e4SLinus Torvalds {
10601da177e4SLinus Torvalds 	int ret = 0;
10611da177e4SLinus Torvalds 	unsigned int hash = fib_laddr_hashfn(local);
10621da177e4SLinus Torvalds 	struct hlist_head *head = &fib_info_laddrhash[hash];
10631da177e4SLinus Torvalds 	struct hlist_node *node;
10641da177e4SLinus Torvalds 	struct fib_info *fi;
10651da177e4SLinus Torvalds 
106685326fa5SDenis V. Lunev 	if (fib_info_laddrhash == NULL || local == 0)
106785326fa5SDenis V. Lunev 		return 0;
106885326fa5SDenis V. Lunev 
10691da177e4SLinus Torvalds 	hlist_for_each_entry(fi, node, head, fib_lhash) {
107009ad9bc7SOctavian Purdila 		if (!net_eq(fi->fib_net, net))
10714814bdbdSDenis V. Lunev 			continue;
10721da177e4SLinus Torvalds 		if (fi->fib_prefsrc == local) {
10731da177e4SLinus Torvalds 			fi->fib_flags |= RTNH_F_DEAD;
10741da177e4SLinus Torvalds 			ret++;
10751da177e4SLinus Torvalds 		}
10761da177e4SLinus Torvalds 	}
107785326fa5SDenis V. Lunev 	return ret;
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
108085326fa5SDenis V. Lunev int fib_sync_down_dev(struct net_device *dev, int force)
108185326fa5SDenis V. Lunev {
108285326fa5SDenis V. Lunev 	int ret = 0;
108385326fa5SDenis V. Lunev 	int scope = RT_SCOPE_NOWHERE;
10841da177e4SLinus Torvalds 	struct fib_info *prev_fi = NULL;
10851da177e4SLinus Torvalds 	unsigned int hash = fib_devindex_hashfn(dev->ifindex);
10861da177e4SLinus Torvalds 	struct hlist_head *head = &fib_info_devhash[hash];
10871da177e4SLinus Torvalds 	struct hlist_node *node;
10881da177e4SLinus Torvalds 	struct fib_nh *nh;
10891da177e4SLinus Torvalds 
109085326fa5SDenis V. Lunev 	if (force)
109185326fa5SDenis V. Lunev 		scope = -1;
109285326fa5SDenis V. Lunev 
10931da177e4SLinus Torvalds 	hlist_for_each_entry(nh, node, head, nh_hash) {
10941da177e4SLinus Torvalds 		struct fib_info *fi = nh->nh_parent;
10951da177e4SLinus Torvalds 		int dead;
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 		BUG_ON(!fi->fib_nhs);
10981da177e4SLinus Torvalds 		if (nh->nh_dev != dev || fi == prev_fi)
10991da177e4SLinus Torvalds 			continue;
11001da177e4SLinus Torvalds 		prev_fi = fi;
11011da177e4SLinus Torvalds 		dead = 0;
11021da177e4SLinus Torvalds 		change_nexthops(fi) {
110371fceff0SDavid S. Miller 			if (nexthop_nh->nh_flags & RTNH_F_DEAD)
11041da177e4SLinus Torvalds 				dead++;
110571fceff0SDavid S. Miller 			else if (nexthop_nh->nh_dev == dev &&
110671fceff0SDavid S. Miller 				 nexthop_nh->nh_scope != scope) {
110771fceff0SDavid S. Miller 				nexthop_nh->nh_flags |= RTNH_F_DEAD;
11081da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
11091da177e4SLinus Torvalds 				spin_lock_bh(&fib_multipath_lock);
111071fceff0SDavid S. Miller 				fi->fib_power -= nexthop_nh->nh_power;
111171fceff0SDavid S. Miller 				nexthop_nh->nh_power = 0;
11121da177e4SLinus Torvalds 				spin_unlock_bh(&fib_multipath_lock);
11131da177e4SLinus Torvalds #endif
11141da177e4SLinus Torvalds 				dead++;
11151da177e4SLinus Torvalds 			}
11161da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
111771fceff0SDavid S. Miller 			if (force > 1 && nexthop_nh->nh_dev == dev) {
11181da177e4SLinus Torvalds 				dead = fi->fib_nhs;
11191da177e4SLinus Torvalds 				break;
11201da177e4SLinus Torvalds 			}
11211da177e4SLinus Torvalds #endif
11221da177e4SLinus Torvalds 		} endfor_nexthops(fi)
11231da177e4SLinus Torvalds 		if (dead == fi->fib_nhs) {
11241da177e4SLinus Torvalds 			fi->fib_flags |= RTNH_F_DEAD;
11251da177e4SLinus Torvalds 			ret++;
11261da177e4SLinus Torvalds 		}
11271da177e4SLinus Torvalds 	}
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	return ret;
11301da177e4SLinus Torvalds }
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds /*
11356a31d2a9SEric Dumazet  * Dead device goes up. We wake up dead nexthops.
11366a31d2a9SEric Dumazet  * It takes sense only on multipath routes.
11371da177e4SLinus Torvalds  */
11381da177e4SLinus Torvalds int fib_sync_up(struct net_device *dev)
11391da177e4SLinus Torvalds {
11401da177e4SLinus Torvalds 	struct fib_info *prev_fi;
11411da177e4SLinus Torvalds 	unsigned int hash;
11421da177e4SLinus Torvalds 	struct hlist_head *head;
11431da177e4SLinus Torvalds 	struct hlist_node *node;
11441da177e4SLinus Torvalds 	struct fib_nh *nh;
11451da177e4SLinus Torvalds 	int ret;
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	if (!(dev->flags & IFF_UP))
11481da177e4SLinus Torvalds 		return 0;
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	prev_fi = NULL;
11511da177e4SLinus Torvalds 	hash = fib_devindex_hashfn(dev->ifindex);
11521da177e4SLinus Torvalds 	head = &fib_info_devhash[hash];
11531da177e4SLinus Torvalds 	ret = 0;
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	hlist_for_each_entry(nh, node, head, nh_hash) {
11561da177e4SLinus Torvalds 		struct fib_info *fi = nh->nh_parent;
11571da177e4SLinus Torvalds 		int alive;
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 		BUG_ON(!fi->fib_nhs);
11601da177e4SLinus Torvalds 		if (nh->nh_dev != dev || fi == prev_fi)
11611da177e4SLinus Torvalds 			continue;
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 		prev_fi = fi;
11641da177e4SLinus Torvalds 		alive = 0;
11651da177e4SLinus Torvalds 		change_nexthops(fi) {
116671fceff0SDavid S. Miller 			if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
11671da177e4SLinus Torvalds 				alive++;
11681da177e4SLinus Torvalds 				continue;
11691da177e4SLinus Torvalds 			}
117071fceff0SDavid S. Miller 			if (nexthop_nh->nh_dev == NULL ||
117171fceff0SDavid S. Miller 			    !(nexthop_nh->nh_dev->flags & IFF_UP))
11721da177e4SLinus Torvalds 				continue;
117371fceff0SDavid S. Miller 			if (nexthop_nh->nh_dev != dev ||
117471fceff0SDavid S. Miller 			    !__in_dev_get_rtnl(dev))
11751da177e4SLinus Torvalds 				continue;
11761da177e4SLinus Torvalds 			alive++;
11771da177e4SLinus Torvalds 			spin_lock_bh(&fib_multipath_lock);
117871fceff0SDavid S. Miller 			nexthop_nh->nh_power = 0;
117971fceff0SDavid S. Miller 			nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
11801da177e4SLinus Torvalds 			spin_unlock_bh(&fib_multipath_lock);
11811da177e4SLinus Torvalds 		} endfor_nexthops(fi)
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 		if (alive > 0) {
11841da177e4SLinus Torvalds 			fi->fib_flags &= ~RTNH_F_DEAD;
11851da177e4SLinus Torvalds 			ret++;
11861da177e4SLinus Torvalds 		}
11871da177e4SLinus Torvalds 	}
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	return ret;
11901da177e4SLinus Torvalds }
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds /*
11936a31d2a9SEric Dumazet  * The algorithm is suboptimal, but it provides really
11946a31d2a9SEric Dumazet  * fair weighted route distribution.
11951da177e4SLinus Torvalds  */
11961da177e4SLinus Torvalds void fib_select_multipath(const struct flowi *flp, struct fib_result *res)
11971da177e4SLinus Torvalds {
11981da177e4SLinus Torvalds 	struct fib_info *fi = res->fi;
11991da177e4SLinus Torvalds 	int w;
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	spin_lock_bh(&fib_multipath_lock);
12021da177e4SLinus Torvalds 	if (fi->fib_power <= 0) {
12031da177e4SLinus Torvalds 		int power = 0;
12041da177e4SLinus Torvalds 		change_nexthops(fi) {
120571fceff0SDavid S. Miller 			if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
120671fceff0SDavid S. Miller 				power += nexthop_nh->nh_weight;
120771fceff0SDavid S. Miller 				nexthop_nh->nh_power = nexthop_nh->nh_weight;
12081da177e4SLinus Torvalds 			}
12091da177e4SLinus Torvalds 		} endfor_nexthops(fi);
12101da177e4SLinus Torvalds 		fi->fib_power = power;
12111da177e4SLinus Torvalds 		if (power <= 0) {
12121da177e4SLinus Torvalds 			spin_unlock_bh(&fib_multipath_lock);
12131da177e4SLinus Torvalds 			/* Race condition: route has just become dead. */
12141da177e4SLinus Torvalds 			res->nh_sel = 0;
12151da177e4SLinus Torvalds 			return;
12161da177e4SLinus Torvalds 		}
12171da177e4SLinus Torvalds 	}
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	/* w should be random number [0..fi->fib_power-1],
12216a31d2a9SEric Dumazet 	 * it is pretty bad approximation.
12221da177e4SLinus Torvalds 	 */
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	w = jiffies % fi->fib_power;
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	change_nexthops(fi) {
122771fceff0SDavid S. Miller 		if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
122871fceff0SDavid S. Miller 		    nexthop_nh->nh_power) {
12296a31d2a9SEric Dumazet 			w -= nexthop_nh->nh_power;
12306a31d2a9SEric Dumazet 			if (w <= 0) {
123171fceff0SDavid S. Miller 				nexthop_nh->nh_power--;
12321da177e4SLinus Torvalds 				fi->fib_power--;
12331da177e4SLinus Torvalds 				res->nh_sel = nhsel;
12341da177e4SLinus Torvalds 				spin_unlock_bh(&fib_multipath_lock);
12351da177e4SLinus Torvalds 				return;
12361da177e4SLinus Torvalds 			}
12371da177e4SLinus Torvalds 		}
12381da177e4SLinus Torvalds 	} endfor_nexthops(fi);
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 	/* Race condition: route has just become dead. */
12411da177e4SLinus Torvalds 	res->nh_sel = 0;
12421da177e4SLinus Torvalds 	spin_unlock_bh(&fib_multipath_lock);
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds #endif
1245