xref: /linux/net/ipv4/fib_semantics.c (revision 9410645520e9b820069761f3450ef6661418e279)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
51da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *		IPv4 Forwarding Information Base: semantics.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
127c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
131da177e4SLinus Torvalds #include <linux/bitops.h>
141da177e4SLinus Torvalds #include <linux/types.h>
151da177e4SLinus Torvalds #include <linux/kernel.h>
161da177e4SLinus Torvalds #include <linux/jiffies.h>
171da177e4SLinus Torvalds #include <linux/mm.h>
181da177e4SLinus Torvalds #include <linux/string.h>
191da177e4SLinus Torvalds #include <linux/socket.h>
201da177e4SLinus Torvalds #include <linux/sockios.h>
211da177e4SLinus Torvalds #include <linux/errno.h>
221da177e4SLinus Torvalds #include <linux/in.h>
231da177e4SLinus Torvalds #include <linux/inet.h>
2414c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h>
251da177e4SLinus Torvalds #include <linux/netdevice.h>
261da177e4SLinus Torvalds #include <linux/if_arp.h>
271da177e4SLinus Torvalds #include <linux/proc_fs.h>
281da177e4SLinus Torvalds #include <linux/skbuff.h>
291da177e4SLinus Torvalds #include <linux/init.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31c3ab2b4eSDavid Ahern #include <linux/netlink.h>
32d07418afSEric Dumazet #include <linux/hash.h>
335e9398a2SEric Dumazet #include <linux/nospec.h>
341da177e4SLinus Torvalds 
3514c85021SArnaldo Carvalho de Melo #include <net/arp.h>
3632ccf110SGuillaume Nault #include <net/inet_dscp.h>
371da177e4SLinus Torvalds #include <net/ip.h>
381da177e4SLinus Torvalds #include <net/protocol.h>
391da177e4SLinus Torvalds #include <net/route.h>
401da177e4SLinus Torvalds #include <net/tcp.h>
411da177e4SLinus Torvalds #include <net/sock.h>
421da177e4SLinus Torvalds #include <net/ip_fib.h>
43717a8f5bSDavid Ahern #include <net/ip6_fib.h>
445481d73fSDavid Ahern #include <net/nexthop.h>
45f21c7bc5SThomas Graf #include <net/netlink.h>
463c618c1dSDavid Ahern #include <net/rtnh.h>
47571e7226SRoopa Prabhu #include <net/lwtunnel.h>
4804b1d4e5SIdo Schimmel #include <net/fib_notifier.h>
49c0a72077SDavid Ahern #include <net/addrconf.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #include "fib_lookup.h"
521da177e4SLinus Torvalds 
53832b4c5eSStephen Hemminger static DEFINE_SPINLOCK(fib_info_lock);
541da177e4SLinus Torvalds static struct hlist_head *fib_info_hash;
551da177e4SLinus Torvalds static struct hlist_head *fib_info_laddrhash;
56123b9731SDavid S. Miller static unsigned int fib_info_hash_size;
5779eb15daSEric Dumazet static unsigned int fib_info_hash_bits;
581da177e4SLinus Torvalds static unsigned int fib_info_cnt;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #define DEVINDEX_HASHBITS 8
611da177e4SLinus Torvalds #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
621da177e4SLinus Torvalds static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
631da177e4SLinus Torvalds 
64dcb1ecb5SDavid Ahern /* for_nexthops and change_nexthops only used when nexthop object
65dcb1ecb5SDavid Ahern  * is not set in a fib_info. The logic within can reference fib_nh.
66dcb1ecb5SDavid Ahern  */
671da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
681da177e4SLinus Torvalds 
696a31d2a9SEric Dumazet #define for_nexthops(fi) {						\
706a31d2a9SEric Dumazet 	int nhsel; const struct fib_nh *nh;				\
716a31d2a9SEric Dumazet 	for (nhsel = 0, nh = (fi)->fib_nh;				\
725481d73fSDavid Ahern 	     nhsel < fib_info_num_path((fi));				\
736a31d2a9SEric Dumazet 	     nh++, nhsel++)
741da177e4SLinus Torvalds 
756a31d2a9SEric Dumazet #define change_nexthops(fi) {						\
766a31d2a9SEric Dumazet 	int nhsel; struct fib_nh *nexthop_nh;				\
776a31d2a9SEric Dumazet 	for (nhsel = 0,	nexthop_nh = (struct fib_nh *)((fi)->fib_nh);	\
785481d73fSDavid Ahern 	     nhsel < fib_info_num_path((fi));				\
796a31d2a9SEric Dumazet 	     nexthop_nh++, nhsel++)
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds #else /* CONFIG_IP_ROUTE_MULTIPATH */
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /* Hope, that gcc will optimize it to get rid of dummy loop */
841da177e4SLinus Torvalds 
856a31d2a9SEric Dumazet #define for_nexthops(fi) {						\
866a31d2a9SEric Dumazet 	int nhsel; const struct fib_nh *nh = (fi)->fib_nh;		\
871da177e4SLinus Torvalds 	for (nhsel = 0; nhsel < 1; nhsel++)
881da177e4SLinus Torvalds 
896a31d2a9SEric Dumazet #define change_nexthops(fi) {						\
906a31d2a9SEric Dumazet 	int nhsel;							\
916a31d2a9SEric Dumazet 	struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh);	\
921da177e4SLinus Torvalds 	for (nhsel = 0; nhsel < 1; nhsel++)
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds #endif /* CONFIG_IP_ROUTE_MULTIPATH */
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds #define endfor_nexthops(fi) }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 
993be0686bSDavid S. Miller const struct fib_prop fib_props[RTN_MAX + 1] = {
1006a31d2a9SEric Dumazet 	[RTN_UNSPEC] = {
1011da177e4SLinus Torvalds 		.error	= 0,
1021da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1036a31d2a9SEric Dumazet 	},
1046a31d2a9SEric Dumazet 	[RTN_UNICAST] = {
1051da177e4SLinus Torvalds 		.error	= 0,
1061da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1076a31d2a9SEric Dumazet 	},
1086a31d2a9SEric Dumazet 	[RTN_LOCAL] = {
1091da177e4SLinus Torvalds 		.error	= 0,
1101da177e4SLinus Torvalds 		.scope	= RT_SCOPE_HOST,
1116a31d2a9SEric Dumazet 	},
1126a31d2a9SEric Dumazet 	[RTN_BROADCAST] = {
1131da177e4SLinus Torvalds 		.error	= 0,
1141da177e4SLinus Torvalds 		.scope	= RT_SCOPE_LINK,
1156a31d2a9SEric Dumazet 	},
1166a31d2a9SEric Dumazet 	[RTN_ANYCAST] = {
1171da177e4SLinus Torvalds 		.error	= 0,
1181da177e4SLinus Torvalds 		.scope	= RT_SCOPE_LINK,
1196a31d2a9SEric Dumazet 	},
1206a31d2a9SEric Dumazet 	[RTN_MULTICAST] = {
1211da177e4SLinus Torvalds 		.error	= 0,
1221da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1236a31d2a9SEric Dumazet 	},
1246a31d2a9SEric Dumazet 	[RTN_BLACKHOLE] = {
1251da177e4SLinus Torvalds 		.error	= -EINVAL,
1261da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1276a31d2a9SEric Dumazet 	},
1286a31d2a9SEric Dumazet 	[RTN_UNREACHABLE] = {
1291da177e4SLinus Torvalds 		.error	= -EHOSTUNREACH,
1301da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1316a31d2a9SEric Dumazet 	},
1326a31d2a9SEric Dumazet 	[RTN_PROHIBIT] = {
1331da177e4SLinus Torvalds 		.error	= -EACCES,
1341da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1356a31d2a9SEric Dumazet 	},
1366a31d2a9SEric Dumazet 	[RTN_THROW] = {
1371da177e4SLinus Torvalds 		.error	= -EAGAIN,
1381da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1396a31d2a9SEric Dumazet 	},
1406a31d2a9SEric Dumazet 	[RTN_NAT] = {
1411da177e4SLinus Torvalds 		.error	= -EINVAL,
1421da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1436a31d2a9SEric Dumazet 	},
1446a31d2a9SEric Dumazet 	[RTN_XRESOLVE] = {
1451da177e4SLinus Torvalds 		.error	= -EINVAL,
1461da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1476a31d2a9SEric Dumazet 	},
1481da177e4SLinus Torvalds };
1491da177e4SLinus Torvalds 
rt_fibinfo_free(struct rtable __rcu ** rtp)150c5038a83SDavid S. Miller static void rt_fibinfo_free(struct rtable __rcu **rtp)
15154764bb6SEric Dumazet {
15254764bb6SEric Dumazet 	struct rtable *rt = rcu_dereference_protected(*rtp, 1);
15354764bb6SEric Dumazet 
15454764bb6SEric Dumazet 	if (!rt)
15554764bb6SEric Dumazet 		return;
15654764bb6SEric Dumazet 
15754764bb6SEric Dumazet 	/* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
15854764bb6SEric Dumazet 	 * because we waited an RCU grace period before calling
15954764bb6SEric Dumazet 	 * free_fib_info_rcu()
16054764bb6SEric Dumazet 	 */
16154764bb6SEric Dumazet 
16295c47f9cSWei Wang 	dst_dev_put(&rt->dst);
163b838d5e1SWei Wang 	dst_release_immediate(&rt->dst);
16454764bb6SEric Dumazet }
16554764bb6SEric Dumazet 
free_nh_exceptions(struct fib_nh_common * nhc)166a5995e71SDavid Ahern static void free_nh_exceptions(struct fib_nh_common *nhc)
167c5038a83SDavid S. Miller {
168caa41527SEric Dumazet 	struct fnhe_hash_bucket *hash;
169c5038a83SDavid S. Miller 	int i;
170c5038a83SDavid S. Miller 
171a5995e71SDavid Ahern 	hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
172caa41527SEric Dumazet 	if (!hash)
173caa41527SEric Dumazet 		return;
174c5038a83SDavid S. Miller 	for (i = 0; i < FNHE_HASH_SIZE; i++) {
175c5038a83SDavid S. Miller 		struct fib_nh_exception *fnhe;
176c5038a83SDavid S. Miller 
177c5038a83SDavid S. Miller 		fnhe = rcu_dereference_protected(hash[i].chain, 1);
178c5038a83SDavid S. Miller 		while (fnhe) {
179c5038a83SDavid S. Miller 			struct fib_nh_exception *next;
180c5038a83SDavid S. Miller 
181c5038a83SDavid S. Miller 			next = rcu_dereference_protected(fnhe->fnhe_next, 1);
182c5038a83SDavid S. Miller 
1832ffae99dSTimo Teräs 			rt_fibinfo_free(&fnhe->fnhe_rth_input);
1842ffae99dSTimo Teräs 			rt_fibinfo_free(&fnhe->fnhe_rth_output);
185c5038a83SDavid S. Miller 
186c5038a83SDavid S. Miller 			kfree(fnhe);
187c5038a83SDavid S. Miller 
188c5038a83SDavid S. Miller 			fnhe = next;
189c5038a83SDavid S. Miller 		}
190c5038a83SDavid S. Miller 	}
191c5038a83SDavid S. Miller 	kfree(hash);
192c5038a83SDavid S. Miller }
193c5038a83SDavid S. Miller 
rt_fibinfo_free_cpus(struct rtable __rcu * __percpu * rtp)194c5038a83SDavid S. Miller static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
195d26b3a7cSEric Dumazet {
196d26b3a7cSEric Dumazet 	int cpu;
197d26b3a7cSEric Dumazet 
198d26b3a7cSEric Dumazet 	if (!rtp)
199d26b3a7cSEric Dumazet 		return;
200d26b3a7cSEric Dumazet 
201d26b3a7cSEric Dumazet 	for_each_possible_cpu(cpu) {
202d26b3a7cSEric Dumazet 		struct rtable *rt;
203d26b3a7cSEric Dumazet 
204d26b3a7cSEric Dumazet 		rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
2050830106cSWei Wang 		if (rt) {
20695c47f9cSWei Wang 			dst_dev_put(&rt->dst);
207b838d5e1SWei Wang 			dst_release_immediate(&rt->dst);
208d26b3a7cSEric Dumazet 		}
2090830106cSWei Wang 	}
210d26b3a7cSEric Dumazet 	free_percpu(rtp);
211d26b3a7cSEric Dumazet }
212d26b3a7cSEric Dumazet 
fib_nh_common_release(struct fib_nh_common * nhc)213979e276eSDavid Ahern void fib_nh_common_release(struct fib_nh_common *nhc)
214979e276eSDavid Ahern {
215d62607c3SJakub Kicinski 	netdev_put(nhc->nhc_dev, &nhc->nhc_dev_tracker);
216979e276eSDavid Ahern 	lwtstate_put(nhc->nhc_lwtstate);
2170f457a36SDavid Ahern 	rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
2180f457a36SDavid Ahern 	rt_fibinfo_free(&nhc->nhc_rth_input);
219a5995e71SDavid Ahern 	free_nh_exceptions(nhc);
220979e276eSDavid Ahern }
221979e276eSDavid Ahern EXPORT_SYMBOL_GPL(fib_nh_common_release);
222979e276eSDavid Ahern 
fib_nh_release(struct net * net,struct fib_nh * fib_nh)223faa041a4SDavid Ahern void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
224faa041a4SDavid Ahern {
225faa041a4SDavid Ahern #ifdef CONFIG_IP_ROUTE_CLASSID
226faa041a4SDavid Ahern 	if (fib_nh->nh_tclassid)
227213f5f8fSEric Dumazet 		atomic_dec(&net->ipv4.fib_num_tclassid_users);
228faa041a4SDavid Ahern #endif
229979e276eSDavid Ahern 	fib_nh_common_release(&fib_nh->nh_common);
230faa041a4SDavid Ahern }
231faa041a4SDavid Ahern 
2321da177e4SLinus Torvalds /* Release a nexthop info record */
free_fib_info_rcu(struct rcu_head * head)23319c1ea14SYan, Zheng static void free_fib_info_rcu(struct rcu_head *head)
23419c1ea14SYan, Zheng {
23519c1ea14SYan, Zheng 	struct fib_info *fi = container_of(head, struct fib_info, rcu);
23619c1ea14SYan, Zheng 
2374c7e8084SDavid Ahern 	if (fi->nh) {
2384c7e8084SDavid Ahern 		nexthop_put(fi->nh);
2394c7e8084SDavid Ahern 	} else {
240e49cc0daSYanmin Zhang 		change_nexthops(fi) {
241faa041a4SDavid Ahern 			fib_nh_release(fi->fib_net, nexthop_nh);
242e49cc0daSYanmin Zhang 		} endfor_nexthops(fi);
2434c7e8084SDavid Ahern 	}
244e49cc0daSYanmin Zhang 
245cc5f0eb2SDavid Ahern 	ip_fib_metrics_put(fi->fib_metrics);
246cc5f0eb2SDavid Ahern 
24719c1ea14SYan, Zheng 	kfree(fi);
24819c1ea14SYan, Zheng }
2491da177e4SLinus Torvalds 
free_fib_info(struct fib_info * fi)2501da177e4SLinus Torvalds void free_fib_info(struct fib_info *fi)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	if (fi->fib_dead == 0) {
253058bd4d2SJoe Perches 		pr_warn("Freeing alive fib_info %p\n", fi);
2541da177e4SLinus Torvalds 		return;
2551da177e4SLinus Torvalds 	}
256faa041a4SDavid Ahern 
257c4e86b43SEric Dumazet 	call_rcu_hurry(&fi->rcu, free_fib_info_rcu);
2581da177e4SLinus Torvalds }
259b423cb10SIdo Schimmel EXPORT_SYMBOL_GPL(free_fib_info);
2601da177e4SLinus Torvalds 
fib_release_info(struct fib_info * fi)2611da177e4SLinus Torvalds void fib_release_info(struct fib_info *fi)
2621da177e4SLinus Torvalds {
263832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
26479976892SYajun Deng 	if (fi && refcount_dec_and_test(&fi->fib_treeref)) {
2651da177e4SLinus Torvalds 		hlist_del(&fi->fib_hash);
2660a6e6b3cSEric Dumazet 
2670a6e6b3cSEric Dumazet 		/* Paired with READ_ONCE() in fib_create_info(). */
2680a6e6b3cSEric Dumazet 		WRITE_ONCE(fib_info_cnt, fib_info_cnt - 1);
2690a6e6b3cSEric Dumazet 
2701da177e4SLinus Torvalds 		if (fi->fib_prefsrc)
2711da177e4SLinus Torvalds 			hlist_del(&fi->fib_lhash);
2724c7e8084SDavid Ahern 		if (fi->nh) {
2734c7e8084SDavid Ahern 			list_del(&fi->nh_list);
2744c7e8084SDavid Ahern 		} else {
2751da177e4SLinus Torvalds 			change_nexthops(fi) {
276b75ed8b1SDavid Ahern 				if (!nexthop_nh->fib_nh_dev)
2771da177e4SLinus Torvalds 					continue;
27871fceff0SDavid S. Miller 				hlist_del(&nexthop_nh->nh_hash);
2791da177e4SLinus Torvalds 			} endfor_nexthops(fi)
2804c7e8084SDavid Ahern 		}
281fce92af1SEric Dumazet 		/* Paired with READ_ONCE() from fib_table_lookup() */
282fce92af1SEric Dumazet 		WRITE_ONCE(fi->fib_dead, 1);
2831da177e4SLinus Torvalds 		fib_info_put(fi);
2841da177e4SLinus Torvalds 	}
285832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
nh_comp(struct fib_info * fi,struct fib_info * ofi)2885481d73fSDavid Ahern static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
2891da177e4SLinus Torvalds {
2905481d73fSDavid Ahern 	const struct fib_nh *onh;
2911da177e4SLinus Torvalds 
2924c7e8084SDavid Ahern 	if (fi->nh || ofi->nh)
2934c7e8084SDavid Ahern 		return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1;
2944c7e8084SDavid Ahern 
2954c7e8084SDavid Ahern 	if (ofi->fib_nhs == 0)
2964c7e8084SDavid Ahern 		return 0;
2974c7e8084SDavid Ahern 
2981da177e4SLinus Torvalds 	for_nexthops(fi) {
2995481d73fSDavid Ahern 		onh = fib_info_nh(ofi, nhsel);
3005481d73fSDavid Ahern 
301b75ed8b1SDavid Ahern 		if (nh->fib_nh_oif != onh->fib_nh_oif ||
302a4ea5d43SDavid Ahern 		    nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
303b75ed8b1SDavid Ahern 		    nh->fib_nh_scope != onh->fib_nh_scope ||
3041da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
305b75ed8b1SDavid Ahern 		    nh->fib_nh_weight != onh->fib_nh_weight ||
3061da177e4SLinus Torvalds #endif
307c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
3081da177e4SLinus Torvalds 		    nh->nh_tclassid != onh->nh_tclassid ||
3091da177e4SLinus Torvalds #endif
310b75ed8b1SDavid Ahern 		    lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
311b75ed8b1SDavid Ahern 		    ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
3121da177e4SLinus Torvalds 			return -1;
313a4ea5d43SDavid Ahern 
314a4ea5d43SDavid Ahern 		if (nh->fib_nh_gw_family == AF_INET &&
315a4ea5d43SDavid Ahern 		    nh->fib_nh_gw4 != onh->fib_nh_gw4)
316a4ea5d43SDavid Ahern 			return -1;
317a4ea5d43SDavid Ahern 
318a4ea5d43SDavid Ahern 		if (nh->fib_nh_gw_family == AF_INET6 &&
319a4ea5d43SDavid Ahern 		    ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
320a4ea5d43SDavid Ahern 			return -1;
3211da177e4SLinus Torvalds 	} endfor_nexthops(fi);
3221da177e4SLinus Torvalds 	return 0;
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
fib_devindex_hashfn(unsigned int val)32588ebc72fSDavid S. Miller static inline unsigned int fib_devindex_hashfn(unsigned int val)
32688ebc72fSDavid S. Miller {
327d07418afSEric Dumazet 	return hash_32(val, DEVINDEX_HASHBITS);
328d07418afSEric Dumazet }
32988ebc72fSDavid S. Miller 
330d07418afSEric Dumazet static struct hlist_head *
fib_info_devhash_bucket(const struct net_device * dev)331d07418afSEric Dumazet fib_info_devhash_bucket(const struct net_device *dev)
332d07418afSEric Dumazet {
333d07418afSEric Dumazet 	u32 val = net_hash_mix(dev_net(dev)) ^ dev->ifindex;
334d07418afSEric Dumazet 
335d07418afSEric Dumazet 	return &fib_info_devhash[fib_devindex_hashfn(val)];
33688ebc72fSDavid S. Miller }
33788ebc72fSDavid S. Miller 
fib_info_hashfn_1(int init_val,u8 protocol,u8 scope,u32 prefsrc,u32 priority)3386c48ea5fSDavid Ahern static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope,
3396c48ea5fSDavid Ahern 				      u32 prefsrc, u32 priority)
3406c48ea5fSDavid Ahern {
3416c48ea5fSDavid Ahern 	unsigned int val = init_val;
3426c48ea5fSDavid Ahern 
3436c48ea5fSDavid Ahern 	val ^= (protocol << 8) | scope;
3446c48ea5fSDavid Ahern 	val ^= prefsrc;
3456c48ea5fSDavid Ahern 	val ^= priority;
3466c48ea5fSDavid Ahern 
3476c48ea5fSDavid Ahern 	return val;
3486c48ea5fSDavid Ahern }
3496c48ea5fSDavid Ahern 
fib_info_hashfn_result(unsigned int val)3506c48ea5fSDavid Ahern static unsigned int fib_info_hashfn_result(unsigned int val)
3511da177e4SLinus Torvalds {
352123b9731SDavid S. Miller 	unsigned int mask = (fib_info_hash_size - 1);
3531da177e4SLinus Torvalds 
3546c48ea5fSDavid Ahern 	return (val ^ (val >> 7) ^ (val >> 12)) & mask;
3556c48ea5fSDavid Ahern }
3566c48ea5fSDavid Ahern 
fib_info_hashfn(struct fib_info * fi)3576c48ea5fSDavid Ahern static inline unsigned int fib_info_hashfn(struct fib_info *fi)
3586c48ea5fSDavid Ahern {
3596c48ea5fSDavid Ahern 	unsigned int val;
3606c48ea5fSDavid Ahern 
3616c48ea5fSDavid Ahern 	val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol,
3626c48ea5fSDavid Ahern 				fi->fib_scope, (__force u32)fi->fib_prefsrc,
3636c48ea5fSDavid Ahern 				fi->fib_priority);
3644c7e8084SDavid Ahern 
3654c7e8084SDavid Ahern 	if (fi->nh) {
3664c7e8084SDavid Ahern 		val ^= fib_devindex_hashfn(fi->nh->id);
3674c7e8084SDavid Ahern 	} else {
36888ebc72fSDavid S. Miller 		for_nexthops(fi) {
369b75ed8b1SDavid Ahern 			val ^= fib_devindex_hashfn(nh->fib_nh_oif);
37088ebc72fSDavid S. Miller 		} endfor_nexthops(fi)
3714c7e8084SDavid Ahern 	}
3721da177e4SLinus Torvalds 
3736c48ea5fSDavid Ahern 	return fib_info_hashfn_result(val);
3746c48ea5fSDavid Ahern }
3756c48ea5fSDavid Ahern 
3766c48ea5fSDavid Ahern /* no metrics, only nexthop id */
fib_find_info_nh(struct net * net,const struct fib_config * cfg)3776c48ea5fSDavid Ahern static struct fib_info *fib_find_info_nh(struct net *net,
3786c48ea5fSDavid Ahern 					 const struct fib_config *cfg)
3796c48ea5fSDavid Ahern {
3806c48ea5fSDavid Ahern 	struct hlist_head *head;
3816c48ea5fSDavid Ahern 	struct fib_info *fi;
3826c48ea5fSDavid Ahern 	unsigned int hash;
3836c48ea5fSDavid Ahern 
3846c48ea5fSDavid Ahern 	hash = fib_info_hashfn_1(fib_devindex_hashfn(cfg->fc_nh_id),
3856c48ea5fSDavid Ahern 				 cfg->fc_protocol, cfg->fc_scope,
3866c48ea5fSDavid Ahern 				 (__force u32)cfg->fc_prefsrc,
3876c48ea5fSDavid Ahern 				 cfg->fc_priority);
3886c48ea5fSDavid Ahern 	hash = fib_info_hashfn_result(hash);
3896c48ea5fSDavid Ahern 	head = &fib_info_hash[hash];
3906c48ea5fSDavid Ahern 
3916c48ea5fSDavid Ahern 	hlist_for_each_entry(fi, head, fib_hash) {
3926c48ea5fSDavid Ahern 		if (!net_eq(fi->fib_net, net))
3936c48ea5fSDavid Ahern 			continue;
3946c48ea5fSDavid Ahern 		if (!fi->nh || fi->nh->id != cfg->fc_nh_id)
3956c48ea5fSDavid Ahern 			continue;
3966c48ea5fSDavid Ahern 		if (cfg->fc_protocol == fi->fib_protocol &&
3976c48ea5fSDavid Ahern 		    cfg->fc_scope == fi->fib_scope &&
3986c48ea5fSDavid Ahern 		    cfg->fc_prefsrc == fi->fib_prefsrc &&
3996c48ea5fSDavid Ahern 		    cfg->fc_priority == fi->fib_priority &&
4006c48ea5fSDavid Ahern 		    cfg->fc_type == fi->fib_type &&
4016c48ea5fSDavid Ahern 		    cfg->fc_table == fi->fib_tb_id &&
4026c48ea5fSDavid Ahern 		    !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK))
4036c48ea5fSDavid Ahern 			return fi;
4046c48ea5fSDavid Ahern 	}
4056c48ea5fSDavid Ahern 
4066c48ea5fSDavid Ahern 	return NULL;
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
fib_find_info(struct fib_info * nfi)4095481d73fSDavid Ahern static struct fib_info *fib_find_info(struct fib_info *nfi)
4101da177e4SLinus Torvalds {
4111da177e4SLinus Torvalds 	struct hlist_head *head;
4121da177e4SLinus Torvalds 	struct fib_info *fi;
4131da177e4SLinus Torvalds 	unsigned int hash;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	hash = fib_info_hashfn(nfi);
4161da177e4SLinus Torvalds 	head = &fib_info_hash[hash];
4171da177e4SLinus Torvalds 
418b67bfe0dSSasha Levin 	hlist_for_each_entry(fi, head, fib_hash) {
41909ad9bc7SOctavian Purdila 		if (!net_eq(fi->fib_net, nfi->fib_net))
4204814bdbdSDenis V. Lunev 			continue;
4211da177e4SLinus Torvalds 		if (fi->fib_nhs != nfi->fib_nhs)
4221da177e4SLinus Torvalds 			continue;
4231da177e4SLinus Torvalds 		if (nfi->fib_protocol == fi->fib_protocol &&
42437e826c5SDavid S. Miller 		    nfi->fib_scope == fi->fib_scope &&
4251da177e4SLinus Torvalds 		    nfi->fib_prefsrc == fi->fib_prefsrc &&
4261da177e4SLinus Torvalds 		    nfi->fib_priority == fi->fib_priority &&
427f4ef85bbSEric Dumazet 		    nfi->fib_type == fi->fib_type &&
428f96a3d74SIdo Schimmel 		    nfi->fib_tb_id == fi->fib_tb_id &&
4291da177e4SLinus Torvalds 		    memcmp(nfi->fib_metrics, fi->fib_metrics,
430fcd13f42SEric Dumazet 			   sizeof(u32) * RTAX_MAX) == 0 &&
4318a3d0316SAndy Gospodarek 		    !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
4324c7e8084SDavid Ahern 		    nh_comp(fi, nfi) == 0)
4331da177e4SLinus Torvalds 			return fi;
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	return NULL;
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds /* Check, that the gateway is already configured.
4406a31d2a9SEric Dumazet  * Used only by redirect accept routine.
4411da177e4SLinus Torvalds  */
ip_fib_check_default(__be32 gw,struct net_device * dev)442d878e72eSAl Viro int ip_fib_check_default(__be32 gw, struct net_device *dev)
4431da177e4SLinus Torvalds {
4441da177e4SLinus Torvalds 	struct hlist_head *head;
4451da177e4SLinus Torvalds 	struct fib_nh *nh;
4461da177e4SLinus Torvalds 
447832b4c5eSStephen Hemminger 	spin_lock(&fib_info_lock);
4481da177e4SLinus Torvalds 
449d07418afSEric Dumazet 	head = fib_info_devhash_bucket(dev);
450d07418afSEric Dumazet 
451b67bfe0dSSasha Levin 	hlist_for_each_entry(nh, head, nh_hash) {
452b75ed8b1SDavid Ahern 		if (nh->fib_nh_dev == dev &&
453b75ed8b1SDavid Ahern 		    nh->fib_nh_gw4 == gw &&
454b75ed8b1SDavid Ahern 		    !(nh->fib_nh_flags & RTNH_F_DEAD)) {
455832b4c5eSStephen Hemminger 			spin_unlock(&fib_info_lock);
4561da177e4SLinus Torvalds 			return 0;
4571da177e4SLinus Torvalds 		}
4581da177e4SLinus Torvalds 	}
4591da177e4SLinus Torvalds 
460832b4c5eSStephen Hemminger 	spin_unlock(&fib_info_lock);
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	return -1;
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
fib_nlmsg_size(struct fib_info * fi)4651e7bdec6SAmit Cohen size_t fib_nlmsg_size(struct fib_info *fi)
466339bf98fSThomas Graf {
467339bf98fSThomas Graf 	size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
468339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_TABLE */
469339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_DST */
470339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_PRIORITY */
471ea697639SDaniel Borkmann 			 + nla_total_size(4) /* RTA_PREFSRC */
472ea697639SDaniel Borkmann 			 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
4735481d73fSDavid Ahern 	unsigned int nhs = fib_info_num_path(fi);
474339bf98fSThomas Graf 
475339bf98fSThomas Graf 	/* space for nested metrics */
476339bf98fSThomas Graf 	payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
477339bf98fSThomas Graf 
4784c7e8084SDavid Ahern 	if (fi->nh)
4794c7e8084SDavid Ahern 		payload += nla_total_size(4); /* RTA_NH_ID */
4804c7e8084SDavid Ahern 
4815481d73fSDavid Ahern 	if (nhs) {
482571e7226SRoopa Prabhu 		size_t nh_encapsize = 0;
4835481d73fSDavid Ahern 		/* Also handles the special case nhs == 1 */
484339bf98fSThomas Graf 
485339bf98fSThomas Graf 		/* each nexthop is packed in an attribute */
486339bf98fSThomas Graf 		size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
487dcb1ecb5SDavid Ahern 		unsigned int i;
488339bf98fSThomas Graf 
489339bf98fSThomas Graf 		/* may contain flow and gateway attribute */
490339bf98fSThomas Graf 		nhsize += 2 * nla_total_size(4);
491339bf98fSThomas Graf 
492571e7226SRoopa Prabhu 		/* grab encap info */
493dcb1ecb5SDavid Ahern 		for (i = 0; i < fib_info_num_path(fi); i++) {
494dcb1ecb5SDavid Ahern 			struct fib_nh_common *nhc = fib_info_nhc(fi, i);
495dcb1ecb5SDavid Ahern 
496dcb1ecb5SDavid Ahern 			if (nhc->nhc_lwtstate) {
497571e7226SRoopa Prabhu 				/* RTA_ENCAP_TYPE */
498571e7226SRoopa Prabhu 				nh_encapsize += lwtunnel_get_encap_size(
499dcb1ecb5SDavid Ahern 						nhc->nhc_lwtstate);
500571e7226SRoopa Prabhu 				/* RTA_ENCAP */
501571e7226SRoopa Prabhu 				nh_encapsize +=  nla_total_size(2);
502571e7226SRoopa Prabhu 			}
503dcb1ecb5SDavid Ahern 		}
504571e7226SRoopa Prabhu 
505339bf98fSThomas Graf 		/* all nexthops are packed in a nested attribute */
5065481d73fSDavid Ahern 		payload += nla_total_size((nhs * nhsize) + nh_encapsize);
507571e7226SRoopa Prabhu 
508339bf98fSThomas Graf 	}
509339bf98fSThomas Graf 
510339bf98fSThomas Graf 	return payload;
511339bf98fSThomas Graf }
512339bf98fSThomas Graf 
rtmsg_fib(int event,__be32 key,struct fib_alias * fa,int dst_len,u32 tb_id,const struct nl_info * info,unsigned int nlm_flags)51381f7bf6cSAl Viro void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
5149877b253SJoe Perches 	       int dst_len, u32 tb_id, const struct nl_info *info,
515b8f55831SMilan Kocian 	       unsigned int nlm_flags)
5161da177e4SLinus Torvalds {
5171e301fd0SIdo Schimmel 	struct fib_rt_info fri;
5181da177e4SLinus Torvalds 	struct sk_buff *skb;
5194e902c57SThomas Graf 	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
520f21c7bc5SThomas Graf 	int err = -ENOBUFS;
5211da177e4SLinus Torvalds 
522339bf98fSThomas Graf 	skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
52351456b29SIan Morris 	if (!skb)
524f21c7bc5SThomas Graf 		goto errout;
5251da177e4SLinus Torvalds 
5261e301fd0SIdo Schimmel 	fri.fi = fa->fa_info;
5271e301fd0SIdo Schimmel 	fri.tb_id = tb_id;
5281e301fd0SIdo Schimmel 	fri.dst = key;
5291e301fd0SIdo Schimmel 	fri.dst_len = dst_len;
530888ade8fSGuillaume Nault 	fri.dscp = fa->fa_dscp;
5311e301fd0SIdo Schimmel 	fri.type = fa->fa_type;
5329fcf986cSEric Dumazet 	fri.offload = READ_ONCE(fa->offload);
5339fcf986cSEric Dumazet 	fri.trap = READ_ONCE(fa->trap);
5349fcf986cSEric Dumazet 	fri.offload_failed = READ_ONCE(fa->offload_failed);
5351e301fd0SIdo Schimmel 	err = fib_dump_info(skb, info->portid, seq, event, &fri, nlm_flags);
53626932566SPatrick McHardy 	if (err < 0) {
53726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in fib_nlmsg_size() */
53826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
53926932566SPatrick McHardy 		kfree_skb(skb);
54026932566SPatrick McHardy 		goto errout;
54126932566SPatrick McHardy 	}
54215e47304SEric W. Biederman 	rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
5434e902c57SThomas Graf 		    info->nlh, GFP_KERNEL);
5441ce85fe4SPablo Neira Ayuso 	return;
545f21c7bc5SThomas Graf errout:
5464d1169c1SDenis V. Lunev 	rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
fib_detect_death(struct fib_info * fi,int order,struct fib_info ** last_resort,int * last_idx,int dflt)549c9cb6b6eSStephen Hemminger static int fib_detect_death(struct fib_info *fi, int order,
550c9cb6b6eSStephen Hemminger 			    struct fib_info **last_resort, int *last_idx,
551c9cb6b6eSStephen Hemminger 			    int dflt)
5521da177e4SLinus Torvalds {
553619d1826SDavid Ahern 	const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
5541da177e4SLinus Torvalds 	struct neighbour *n;
5551da177e4SLinus Torvalds 	int state = NUD_NONE;
5561da177e4SLinus Torvalds 
557619d1826SDavid Ahern 	if (likely(nhc->nhc_gw_family == AF_INET))
558619d1826SDavid Ahern 		n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
559619d1826SDavid Ahern 	else if (nhc->nhc_gw_family == AF_INET6)
560619d1826SDavid Ahern 		n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
561619d1826SDavid Ahern 				 nhc->nhc_dev);
562619d1826SDavid Ahern 	else
563619d1826SDavid Ahern 		n = NULL;
564619d1826SDavid Ahern 
5651da177e4SLinus Torvalds 	if (n) {
566b071af52SEric Dumazet 		state = READ_ONCE(n->nud_state);
5671da177e4SLinus Torvalds 		neigh_release(n);
56888f64320SJulian Anastasov 	} else {
56988f64320SJulian Anastasov 		return 0;
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 	if (state == NUD_REACHABLE)
5721da177e4SLinus Torvalds 		return 0;
573c17860a0SDenis V. Lunev 	if ((state & NUD_VALID) && order != dflt)
5741da177e4SLinus Torvalds 		return 0;
5751da177e4SLinus Torvalds 	if ((state & NUD_VALID) ||
57688f64320SJulian Anastasov 	    (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
5771da177e4SLinus Torvalds 		*last_resort = fi;
5781da177e4SLinus Torvalds 		*last_idx = order;
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 	return 1;
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
fib_nh_common_init(struct net * net,struct fib_nh_common * nhc,struct nlattr * encap,u16 encap_type,void * cfg,gfp_t gfp_flags,struct netlink_ext_ack * extack)583faee6769SAlexander Aring int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
584faee6769SAlexander Aring 		       struct nlattr *encap, u16 encap_type,
585faee6769SAlexander Aring 		       void *cfg, gfp_t gfp_flags,
586979e276eSDavid Ahern 		       struct netlink_ext_ack *extack)
587979e276eSDavid Ahern {
5880f457a36SDavid Ahern 	int err;
5890f457a36SDavid Ahern 
5900f457a36SDavid Ahern 	nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
5910f457a36SDavid Ahern 						    gfp_flags);
5920f457a36SDavid Ahern 	if (!nhc->nhc_pcpu_rth_output)
5930f457a36SDavid Ahern 		return -ENOMEM;
5940f457a36SDavid Ahern 
595979e276eSDavid Ahern 	if (encap) {
596979e276eSDavid Ahern 		struct lwtunnel_state *lwtstate;
597979e276eSDavid Ahern 
598979e276eSDavid Ahern 		if (encap_type == LWTUNNEL_ENCAP_NONE) {
599979e276eSDavid Ahern 			NL_SET_ERR_MSG(extack, "LWT encap type not specified");
6000f457a36SDavid Ahern 			err = -EINVAL;
6010f457a36SDavid Ahern 			goto lwt_failure;
602979e276eSDavid Ahern 		}
603faee6769SAlexander Aring 		err = lwtunnel_build_state(net, encap_type, encap,
604faee6769SAlexander Aring 					   nhc->nhc_family, cfg, &lwtstate,
605faee6769SAlexander Aring 					   extack);
606979e276eSDavid Ahern 		if (err)
6070f457a36SDavid Ahern 			goto lwt_failure;
608979e276eSDavid Ahern 
609979e276eSDavid Ahern 		nhc->nhc_lwtstate = lwtstate_get(lwtstate);
610979e276eSDavid Ahern 	}
611979e276eSDavid Ahern 
612979e276eSDavid Ahern 	return 0;
6130f457a36SDavid Ahern 
6140f457a36SDavid Ahern lwt_failure:
6150f457a36SDavid Ahern 	rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
6160f457a36SDavid Ahern 	nhc->nhc_pcpu_rth_output = NULL;
6170f457a36SDavid Ahern 	return err;
618979e276eSDavid Ahern }
619979e276eSDavid Ahern EXPORT_SYMBOL_GPL(fib_nh_common_init);
620979e276eSDavid Ahern 
fib_nh_init(struct net * net,struct fib_nh * nh,struct fib_config * cfg,int nh_weight,struct netlink_ext_ack * extack)621e4516ef6SDavid Ahern int fib_nh_init(struct net *net, struct fib_nh *nh,
622e4516ef6SDavid Ahern 		struct fib_config *cfg, int nh_weight,
623e4516ef6SDavid Ahern 		struct netlink_ext_ack *extack)
624e4516ef6SDavid Ahern {
6250f457a36SDavid Ahern 	int err;
626e4516ef6SDavid Ahern 
627f1741730SDavid Ahern 	nh->fib_nh_family = AF_INET;
628f1741730SDavid Ahern 
629faee6769SAlexander Aring 	err = fib_nh_common_init(net, &nh->nh_common, cfg->fc_encap,
630979e276eSDavid Ahern 				 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
631e4516ef6SDavid Ahern 	if (err)
6320f457a36SDavid Ahern 		return err;
633e4516ef6SDavid Ahern 
634b75ed8b1SDavid Ahern 	nh->fib_nh_oif = cfg->fc_oif;
635a4ea5d43SDavid Ahern 	nh->fib_nh_gw_family = cfg->fc_gw_family;
636a4ea5d43SDavid Ahern 	if (cfg->fc_gw_family == AF_INET)
637f35b794bSDavid Ahern 		nh->fib_nh_gw4 = cfg->fc_gw4;
638a4ea5d43SDavid Ahern 	else if (cfg->fc_gw_family == AF_INET6)
639a4ea5d43SDavid Ahern 		nh->fib_nh_gw6 = cfg->fc_gw6;
640a4ea5d43SDavid Ahern 
641b75ed8b1SDavid Ahern 	nh->fib_nh_flags = cfg->fc_flags;
642e4516ef6SDavid Ahern 
643e4516ef6SDavid Ahern #ifdef CONFIG_IP_ROUTE_CLASSID
644e4516ef6SDavid Ahern 	nh->nh_tclassid = cfg->fc_flow;
645e4516ef6SDavid Ahern 	if (nh->nh_tclassid)
646213f5f8fSEric Dumazet 		atomic_inc(&net->ipv4.fib_num_tclassid_users);
647e4516ef6SDavid Ahern #endif
648e4516ef6SDavid Ahern #ifdef CONFIG_IP_ROUTE_MULTIPATH
649b75ed8b1SDavid Ahern 	nh->fib_nh_weight = nh_weight;
650e4516ef6SDavid Ahern #endif
651e4516ef6SDavid Ahern 	return 0;
652e4516ef6SDavid Ahern }
653e4516ef6SDavid Ahern 
6541da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
6551da177e4SLinus Torvalds 
fib_count_nexthops(struct rtnexthop * rtnh,int remaining,struct netlink_ext_ack * extack)6566d8422a1SDavid Ahern static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
6576d8422a1SDavid Ahern 			      struct netlink_ext_ack *extack)
6581da177e4SLinus Torvalds {
6591da177e4SLinus Torvalds 	int nhs = 0;
6601da177e4SLinus Torvalds 
6614e902c57SThomas Graf 	while (rtnh_ok(rtnh, remaining)) {
6621da177e4SLinus Torvalds 		nhs++;
6634e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
6641da177e4SLinus Torvalds 	}
6651da177e4SLinus Torvalds 
6664e902c57SThomas Graf 	/* leftover implies invalid nexthop configuration, discard it */
667c3ab2b4eSDavid Ahern 	if (remaining > 0) {
668c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack,
669c3ab2b4eSDavid Ahern 			       "Invalid nexthop configuration - extra data after nexthops");
670c3ab2b4eSDavid Ahern 		nhs = 0;
671c3ab2b4eSDavid Ahern 	}
672c3ab2b4eSDavid Ahern 
673c3ab2b4eSDavid Ahern 	return nhs;
6744e902c57SThomas Graf }
6751da177e4SLinus Torvalds 
fib_gw_from_attr(__be32 * gw,struct nlattr * nla,struct netlink_ext_ack * extack)6767a3429baSDavid Ahern static int fib_gw_from_attr(__be32 *gw, struct nlattr *nla,
6777a3429baSDavid Ahern 			    struct netlink_ext_ack *extack)
6787a3429baSDavid Ahern {
6797a3429baSDavid Ahern 	if (nla_len(nla) < sizeof(*gw)) {
6807a3429baSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
6817a3429baSDavid Ahern 		return -EINVAL;
6827a3429baSDavid Ahern 	}
6837a3429baSDavid Ahern 
6847a3429baSDavid Ahern 	*gw = nla_get_in_addr(nla);
6857a3429baSDavid Ahern 
6867a3429baSDavid Ahern 	return 0;
6877a3429baSDavid Ahern }
6887a3429baSDavid Ahern 
6894c7e8084SDavid Ahern /* only called when fib_nh is integrated into fib_info */
fib_get_nhs(struct fib_info * fi,struct rtnexthop * rtnh,int remaining,struct fib_config * cfg,struct netlink_ext_ack * extack)6904e902c57SThomas Graf static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
6916d8422a1SDavid Ahern 		       int remaining, struct fib_config *cfg,
6926d8422a1SDavid Ahern 		       struct netlink_ext_ack *extack)
6934e902c57SThomas Graf {
694e4516ef6SDavid Ahern 	struct net *net = fi->fib_net;
695e4516ef6SDavid Ahern 	struct fib_config fib_cfg;
6965481d73fSDavid Ahern 	struct fib_nh *nh;
697571e7226SRoopa Prabhu 	int ret;
698571e7226SRoopa Prabhu 
6991da177e4SLinus Torvalds 	change_nexthops(fi) {
7004e902c57SThomas Graf 		int attrlen;
7014e902c57SThomas Graf 
702e4516ef6SDavid Ahern 		memset(&fib_cfg, 0, sizeof(fib_cfg));
703e4516ef6SDavid Ahern 
704c3ab2b4eSDavid Ahern 		if (!rtnh_ok(rtnh, remaining)) {
705c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
706c3ab2b4eSDavid Ahern 				       "Invalid nexthop configuration - extra data after nexthop");
7071da177e4SLinus Torvalds 			return -EINVAL;
708c3ab2b4eSDavid Ahern 		}
7094e902c57SThomas Graf 
710c3ab2b4eSDavid Ahern 		if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
711c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
712c3ab2b4eSDavid Ahern 				       "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
71380610229SJulian Anastasov 			return -EINVAL;
714c3ab2b4eSDavid Ahern 		}
71580610229SJulian Anastasov 
716e4516ef6SDavid Ahern 		fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
717e4516ef6SDavid Ahern 		fib_cfg.fc_oif = rtnh->rtnh_ifindex;
7184e902c57SThomas Graf 
7194e902c57SThomas Graf 		attrlen = rtnh_attrlen(rtnh);
7204e902c57SThomas Graf 		if (attrlen > 0) {
721d1566268SDavid Ahern 			struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
7224e902c57SThomas Graf 
7234e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
724d1566268SDavid Ahern 			nlav = nla_find(attrs, attrlen, RTA_VIA);
725d1566268SDavid Ahern 			if (nla && nlav) {
726d1566268SDavid Ahern 				NL_SET_ERR_MSG(extack,
727d1566268SDavid Ahern 					       "Nexthop configuration can not contain both GATEWAY and VIA");
728d1566268SDavid Ahern 				return -EINVAL;
729d1566268SDavid Ahern 			}
730f35b794bSDavid Ahern 			if (nla) {
7317a3429baSDavid Ahern 				ret = fib_gw_from_attr(&fib_cfg.fc_gw4, nla,
7327a3429baSDavid Ahern 						       extack);
7337a3429baSDavid Ahern 				if (ret)
7347a3429baSDavid Ahern 					goto errout;
7357a3429baSDavid Ahern 
736d73f80f9SDavid Ahern 				if (fib_cfg.fc_gw4)
737d73f80f9SDavid Ahern 					fib_cfg.fc_gw_family = AF_INET;
738d1566268SDavid Ahern 			} else if (nlav) {
739d1566268SDavid Ahern 				ret = fib_gw_from_via(&fib_cfg, nlav, extack);
740d1566268SDavid Ahern 				if (ret)
741d1566268SDavid Ahern 					goto errout;
742f35b794bSDavid Ahern 			}
743571e7226SRoopa Prabhu 
744e4516ef6SDavid Ahern 			nla = nla_find(attrs, attrlen, RTA_FLOW);
745664b9c4bSDavid Ahern 			if (nla) {
746664b9c4bSDavid Ahern 				if (nla_len(nla) < sizeof(u32)) {
747664b9c4bSDavid Ahern 					NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
748664b9c4bSDavid Ahern 					return -EINVAL;
749664b9c4bSDavid Ahern 				}
750e4516ef6SDavid Ahern 				fib_cfg.fc_flow = nla_get_u32(nla);
751664b9c4bSDavid Ahern 			}
752e4516ef6SDavid Ahern 
753e4516ef6SDavid Ahern 			fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
7548bda81a4SDavid Ahern 			/* RTA_ENCAP_TYPE length checked in
7558bda81a4SDavid Ahern 			 * lwtunnel_valid_encap_type_attr
7568bda81a4SDavid Ahern 			 */
757e4516ef6SDavid Ahern 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
758e4516ef6SDavid Ahern 			if (nla)
759e4516ef6SDavid Ahern 				fib_cfg.fc_encap_type = nla_get_u16(nla);
760c3ab2b4eSDavid Ahern 		}
76130357d7dSDavid Ahern 
762e4516ef6SDavid Ahern 		ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
763e4516ef6SDavid Ahern 				  rtnh->rtnh_hops + 1, extack);
764571e7226SRoopa Prabhu 		if (ret)
765571e7226SRoopa Prabhu 			goto errout;
7664e902c57SThomas Graf 
7674e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
7681da177e4SLinus Torvalds 	} endfor_nexthops(fi);
7694e902c57SThomas Graf 
770571e7226SRoopa Prabhu 	ret = -EINVAL;
7715481d73fSDavid Ahern 	nh = fib_info_nh(fi, 0);
7725481d73fSDavid Ahern 	if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) {
773e4516ef6SDavid Ahern 		NL_SET_ERR_MSG(extack,
774e4516ef6SDavid Ahern 			       "Nexthop device index does not match RTA_OIF");
775e4516ef6SDavid Ahern 		goto errout;
776e4516ef6SDavid Ahern 	}
777f35b794bSDavid Ahern 	if (cfg->fc_gw_family) {
7785481d73fSDavid Ahern 		if (cfg->fc_gw_family != nh->fib_nh_gw_family ||
779f35b794bSDavid Ahern 		    (cfg->fc_gw_family == AF_INET &&
7805481d73fSDavid Ahern 		     nh->fib_nh_gw4 != cfg->fc_gw4) ||
781a4ea5d43SDavid Ahern 		    (cfg->fc_gw_family == AF_INET6 &&
7825481d73fSDavid Ahern 		     ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) {
783e4516ef6SDavid Ahern 			NL_SET_ERR_MSG(extack,
784a4ea5d43SDavid Ahern 				       "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
785e4516ef6SDavid Ahern 			goto errout;
786e4516ef6SDavid Ahern 		}
787f35b794bSDavid Ahern 	}
788e4516ef6SDavid Ahern #ifdef CONFIG_IP_ROUTE_CLASSID
7895481d73fSDavid Ahern 	if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) {
790e4516ef6SDavid Ahern 		NL_SET_ERR_MSG(extack,
791e4516ef6SDavid Ahern 			       "Nexthop class id does not match RTA_FLOW");
792e4516ef6SDavid Ahern 		goto errout;
793e4516ef6SDavid Ahern 	}
794e4516ef6SDavid Ahern #endif
795e4516ef6SDavid Ahern 	ret = 0;
796571e7226SRoopa Prabhu errout:
797571e7226SRoopa Prabhu 	return ret;
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds 
8004c7e8084SDavid Ahern /* only called when fib_nh is integrated into fib_info */
fib_rebalance(struct fib_info * fi)8010e884c78SPeter Nørlund static void fib_rebalance(struct fib_info *fi)
8020e884c78SPeter Nørlund {
8030e884c78SPeter Nørlund 	int total;
8040e884c78SPeter Nørlund 	int w;
8050e884c78SPeter Nørlund 
8065481d73fSDavid Ahern 	if (fib_info_num_path(fi) < 2)
8070e884c78SPeter Nørlund 		return;
8080e884c78SPeter Nørlund 
8090e884c78SPeter Nørlund 	total = 0;
8100e884c78SPeter Nørlund 	for_nexthops(fi) {
811b75ed8b1SDavid Ahern 		if (nh->fib_nh_flags & RTNH_F_DEAD)
8120e884c78SPeter Nørlund 			continue;
8130e884c78SPeter Nørlund 
814b75ed8b1SDavid Ahern 		if (ip_ignore_linkdown(nh->fib_nh_dev) &&
815b75ed8b1SDavid Ahern 		    nh->fib_nh_flags & RTNH_F_LINKDOWN)
8160e884c78SPeter Nørlund 			continue;
8170e884c78SPeter Nørlund 
818b75ed8b1SDavid Ahern 		total += nh->fib_nh_weight;
8190e884c78SPeter Nørlund 	} endfor_nexthops(fi);
8200e884c78SPeter Nørlund 
8210e884c78SPeter Nørlund 	w = 0;
8220e884c78SPeter Nørlund 	change_nexthops(fi) {
8230e884c78SPeter Nørlund 		int upper_bound;
8240e884c78SPeter Nørlund 
825b75ed8b1SDavid Ahern 		if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
8260e884c78SPeter Nørlund 			upper_bound = -1;
827b75ed8b1SDavid Ahern 		} else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
828b75ed8b1SDavid Ahern 			   nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
8290e884c78SPeter Nørlund 			upper_bound = -1;
8300e884c78SPeter Nørlund 		} else {
831b75ed8b1SDavid Ahern 			w += nexthop_nh->fib_nh_weight;
8320a837fe4SPeter Nørlund 			upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
8330e884c78SPeter Nørlund 							    total) - 1;
8340e884c78SPeter Nørlund 		}
8350e884c78SPeter Nørlund 
836b75ed8b1SDavid Ahern 		atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
8370e884c78SPeter Nørlund 	} endfor_nexthops(fi);
8380e884c78SPeter Nørlund }
8390e884c78SPeter Nørlund #else /* CONFIG_IP_ROUTE_MULTIPATH */
8400e884c78SPeter Nørlund 
fib_get_nhs(struct fib_info * fi,struct rtnexthop * rtnh,int remaining,struct fib_config * cfg,struct netlink_ext_ack * extack)8418373c6c8SDavid Ahern static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
8428373c6c8SDavid Ahern 		       int remaining, struct fib_config *cfg,
8438373c6c8SDavid Ahern 		       struct netlink_ext_ack *extack)
8448373c6c8SDavid Ahern {
8458373c6c8SDavid Ahern 	NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
8468373c6c8SDavid Ahern 
8478373c6c8SDavid Ahern 	return -EINVAL;
8488373c6c8SDavid Ahern }
8498373c6c8SDavid Ahern 
8500e884c78SPeter Nørlund #define fib_rebalance(fi) do { } while (0)
8510e884c78SPeter Nørlund 
8520e884c78SPeter Nørlund #endif /* CONFIG_IP_ROUTE_MULTIPATH */
8531da177e4SLinus Torvalds 
fib_encap_match(struct net * net,u16 encap_type,struct nlattr * encap,const struct fib_nh * nh,const struct fib_config * cfg,struct netlink_ext_ack * extack)854faee6769SAlexander Aring static int fib_encap_match(struct net *net, u16 encap_type,
855571e7226SRoopa Prabhu 			   struct nlattr *encap,
85630357d7dSDavid Ahern 			   const struct fib_nh *nh,
8579ae28727SDavid Ahern 			   const struct fib_config *cfg,
8589ae28727SDavid Ahern 			   struct netlink_ext_ack *extack)
859571e7226SRoopa Prabhu {
860571e7226SRoopa Prabhu 	struct lwtunnel_state *lwtstate;
861df383e62SJiri Benc 	int ret, result = 0;
862571e7226SRoopa Prabhu 
863571e7226SRoopa Prabhu 	if (encap_type == LWTUNNEL_ENCAP_NONE)
864571e7226SRoopa Prabhu 		return 0;
865571e7226SRoopa Prabhu 
866faee6769SAlexander Aring 	ret = lwtunnel_build_state(net, encap_type, encap, AF_INET,
8679ae28727SDavid Ahern 				   cfg, &lwtstate, extack);
868df383e62SJiri Benc 	if (!ret) {
869b75ed8b1SDavid Ahern 		result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
870df383e62SJiri Benc 		lwtstate_free(lwtstate);
871df383e62SJiri Benc 	}
872571e7226SRoopa Prabhu 
873df383e62SJiri Benc 	return result;
874571e7226SRoopa Prabhu }
875571e7226SRoopa Prabhu 
fib_nh_match(struct net * net,struct fib_config * cfg,struct fib_info * fi,struct netlink_ext_ack * extack)876faee6769SAlexander Aring int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
8779ae28727SDavid Ahern 		 struct netlink_ext_ack *extack)
8781da177e4SLinus Torvalds {
8791da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
8804e902c57SThomas Graf 	struct rtnexthop *rtnh;
8814e902c57SThomas Graf 	int remaining;
8821da177e4SLinus Torvalds #endif
8831da177e4SLinus Torvalds 
8844e902c57SThomas Graf 	if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
8851da177e4SLinus Torvalds 		return 1;
8861da177e4SLinus Torvalds 
887493ced1aSDavid Ahern 	if (cfg->fc_nh_id) {
888493ced1aSDavid Ahern 		if (fi->nh && cfg->fc_nh_id == fi->nh->id)
889493ced1aSDavid Ahern 			return 0;
890493ced1aSDavid Ahern 		return 1;
891493ced1aSDavid Ahern 	}
892493ced1aSDavid Ahern 
893d5082d38SIdo Schimmel 	if (fi->nh) {
894d5082d38SIdo Schimmel 		if (cfg->fc_oif || cfg->fc_gw_family || cfg->fc_mp)
8956bf92d70SNikolay Aleksandrov 			return 1;
896d5082d38SIdo Schimmel 		return 0;
897d5082d38SIdo Schimmel 	}
8986bf92d70SNikolay Aleksandrov 
89961b91eb3SDavid Ahern 	if (cfg->fc_oif || cfg->fc_gw_family) {
90061b91eb3SDavid Ahern 		struct fib_nh *nh;
90161b91eb3SDavid Ahern 
9026bf92d70SNikolay Aleksandrov 		nh = fib_info_nh(fi, 0);
903571e7226SRoopa Prabhu 		if (cfg->fc_encap) {
904faee6769SAlexander Aring 			if (fib_encap_match(net, cfg->fc_encap_type,
905faee6769SAlexander Aring 					    cfg->fc_encap, nh, cfg, extack))
906571e7226SRoopa Prabhu 				return 1;
907571e7226SRoopa Prabhu 		}
908a8c6db1dSStefano Brivio #ifdef CONFIG_IP_ROUTE_CLASSID
909a8c6db1dSStefano Brivio 		if (cfg->fc_flow &&
9105481d73fSDavid Ahern 		    cfg->fc_flow != nh->nh_tclassid)
911a8c6db1dSStefano Brivio 			return 1;
912a8c6db1dSStefano Brivio #endif
9135481d73fSDavid Ahern 		if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
914f35b794bSDavid Ahern 		    (cfg->fc_gw_family &&
9155481d73fSDavid Ahern 		     cfg->fc_gw_family != nh->fib_nh_gw_family))
9161da177e4SLinus Torvalds 			return 1;
917f35b794bSDavid Ahern 
918f35b794bSDavid Ahern 		if (cfg->fc_gw_family == AF_INET &&
9195481d73fSDavid Ahern 		    cfg->fc_gw4 != nh->fib_nh_gw4)
920f35b794bSDavid Ahern 			return 1;
921f35b794bSDavid Ahern 
922a4ea5d43SDavid Ahern 		if (cfg->fc_gw_family == AF_INET6 &&
9235481d73fSDavid Ahern 		    ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
924a4ea5d43SDavid Ahern 			return 1;
925a4ea5d43SDavid Ahern 
926f35b794bSDavid Ahern 		return 0;
9271da177e4SLinus Torvalds 	}
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
93051456b29SIan Morris 	if (!cfg->fc_mp)
9311da177e4SLinus Torvalds 		return 0;
9324e902c57SThomas Graf 
9334e902c57SThomas Graf 	rtnh = cfg->fc_mp;
9344e902c57SThomas Graf 	remaining = cfg->fc_mp_len;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	for_nexthops(fi) {
9374e902c57SThomas Graf 		int attrlen;
9381da177e4SLinus Torvalds 
9394e902c57SThomas Graf 		if (!rtnh_ok(rtnh, remaining))
9401da177e4SLinus Torvalds 			return -EINVAL;
9414e902c57SThomas Graf 
942b75ed8b1SDavid Ahern 		if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
9431da177e4SLinus Torvalds 			return 1;
9444e902c57SThomas Graf 
9454e902c57SThomas Graf 		attrlen = rtnh_attrlen(rtnh);
946f76936d0SJiri Pirko 		if (attrlen > 0) {
947d1566268SDavid Ahern 			struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
9487a3429baSDavid Ahern 			int err;
9494e902c57SThomas Graf 
9504e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
951d1566268SDavid Ahern 			nlav = nla_find(attrs, attrlen, RTA_VIA);
952d1566268SDavid Ahern 			if (nla && nlav) {
953d1566268SDavid Ahern 				NL_SET_ERR_MSG(extack,
954d1566268SDavid Ahern 					       "Nexthop configuration can not contain both GATEWAY and VIA");
955d1566268SDavid Ahern 				return -EINVAL;
956d1566268SDavid Ahern 			}
957d1566268SDavid Ahern 
958d1566268SDavid Ahern 			if (nla) {
9597a3429baSDavid Ahern 				__be32 gw;
9607a3429baSDavid Ahern 
9617a3429baSDavid Ahern 				err = fib_gw_from_attr(&gw, nla, extack);
9627a3429baSDavid Ahern 				if (err)
9637a3429baSDavid Ahern 					return err;
9647a3429baSDavid Ahern 
965d1566268SDavid Ahern 				if (nh->fib_nh_gw_family != AF_INET ||
9667a3429baSDavid Ahern 				    gw != nh->fib_nh_gw4)
9671da177e4SLinus Torvalds 					return 1;
968d1566268SDavid Ahern 			} else if (nlav) {
969d1566268SDavid Ahern 				struct fib_config cfg2;
970d1566268SDavid Ahern 
971d1566268SDavid Ahern 				err = fib_gw_from_via(&cfg2, nlav, extack);
972d1566268SDavid Ahern 				if (err)
973d1566268SDavid Ahern 					return err;
974d1566268SDavid Ahern 
975d1566268SDavid Ahern 				switch (nh->fib_nh_gw_family) {
976d1566268SDavid Ahern 				case AF_INET:
977d1566268SDavid Ahern 					if (cfg2.fc_gw_family != AF_INET ||
978d1566268SDavid Ahern 					    cfg2.fc_gw4 != nh->fib_nh_gw4)
979d1566268SDavid Ahern 						return 1;
980d1566268SDavid Ahern 					break;
981d1566268SDavid Ahern 				case AF_INET6:
982d1566268SDavid Ahern 					if (cfg2.fc_gw_family != AF_INET6 ||
983d1566268SDavid Ahern 					    ipv6_addr_cmp(&cfg2.fc_gw6,
984d1566268SDavid Ahern 							  &nh->fib_nh_gw6))
985d1566268SDavid Ahern 						return 1;
986d1566268SDavid Ahern 					break;
987d1566268SDavid Ahern 				}
988d1566268SDavid Ahern 			}
989d1566268SDavid Ahern 
990c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
9914e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_FLOW);
992664b9c4bSDavid Ahern 			if (nla) {
993664b9c4bSDavid Ahern 				if (nla_len(nla) < sizeof(u32)) {
994664b9c4bSDavid Ahern 					NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
995664b9c4bSDavid Ahern 					return -EINVAL;
996664b9c4bSDavid Ahern 				}
997664b9c4bSDavid Ahern 				if (nla_get_u32(nla) != nh->nh_tclassid)
9981da177e4SLinus Torvalds 					return 1;
999664b9c4bSDavid Ahern 			}
10001da177e4SLinus Torvalds #endif
10011da177e4SLinus Torvalds 		}
10024e902c57SThomas Graf 
10034e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
10041da177e4SLinus Torvalds 	} endfor_nexthops(fi);
10051da177e4SLinus Torvalds #endif
10061da177e4SLinus Torvalds 	return 0;
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
fib_metrics_match(struct fib_config * cfg,struct fib_info * fi)10095f9ae3d9SXin Long bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
10105f9ae3d9SXin Long {
10115f9ae3d9SXin Long 	struct nlattr *nla;
10125f9ae3d9SXin Long 	int remaining;
10135f9ae3d9SXin Long 
10145f9ae3d9SXin Long 	if (!cfg->fc_mx)
10155f9ae3d9SXin Long 		return true;
10165f9ae3d9SXin Long 
10175f9ae3d9SXin Long 	nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
10185f9ae3d9SXin Long 		int type = nla_type(nla);
1019d03a4557SPhil Sutter 		u32 fi_val, val;
10205f9ae3d9SXin Long 
10215f9ae3d9SXin Long 		if (!type)
10225f9ae3d9SXin Long 			continue;
10235f9ae3d9SXin Long 		if (type > RTAX_MAX)
10245f9ae3d9SXin Long 			return false;
10255f9ae3d9SXin Long 
10265e9398a2SEric Dumazet 		type = array_index_nospec(type, RTAX_MAX + 1);
10275f9ae3d9SXin Long 		if (type == RTAX_CC_ALGO) {
10285f9ae3d9SXin Long 			char tmp[TCP_CA_NAME_MAX];
10295f9ae3d9SXin Long 			bool ecn_ca = false;
10305f9ae3d9SXin Long 
1031872f6903SFrancis Laniel 			nla_strscpy(tmp, nla, sizeof(tmp));
103261e2bbafSJason Xing 			val = tcp_ca_get_key_by_name(tmp, &ecn_ca);
10335f9ae3d9SXin Long 		} else {
10345b5e7a0dSEric Dumazet 			if (nla_len(nla) != sizeof(u32))
10355b5e7a0dSEric Dumazet 				return false;
10365f9ae3d9SXin Long 			val = nla_get_u32(nla);
10375f9ae3d9SXin Long 		}
10385f9ae3d9SXin Long 
1039d03a4557SPhil Sutter 		fi_val = fi->fib_metrics->metrics[type - 1];
1040d03a4557SPhil Sutter 		if (type == RTAX_FEATURES)
1041d03a4557SPhil Sutter 			fi_val &= ~DST_FEATURE_ECN_CA;
1042d03a4557SPhil Sutter 
1043d03a4557SPhil Sutter 		if (fi_val != val)
10445f9ae3d9SXin Long 			return false;
10455f9ae3d9SXin Long 	}
10465f9ae3d9SXin Long 
10475f9ae3d9SXin Long 	return true;
10485f9ae3d9SXin Long }
10495f9ae3d9SXin Long 
fib_check_nh_v6_gw(struct net * net,struct fib_nh * nh,u32 table,struct netlink_ext_ack * extack)1050717a8f5bSDavid Ahern static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
1051717a8f5bSDavid Ahern 			      u32 table, struct netlink_ext_ack *extack)
1052717a8f5bSDavid Ahern {
1053717a8f5bSDavid Ahern 	struct fib6_config cfg = {
1054717a8f5bSDavid Ahern 		.fc_table = table,
1055717a8f5bSDavid Ahern 		.fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
1056717a8f5bSDavid Ahern 		.fc_ifindex = nh->fib_nh_oif,
1057717a8f5bSDavid Ahern 		.fc_gateway = nh->fib_nh_gw6,
1058717a8f5bSDavid Ahern 	};
1059717a8f5bSDavid Ahern 	struct fib6_nh fib6_nh = {};
1060717a8f5bSDavid Ahern 	int err;
1061717a8f5bSDavid Ahern 
1062717a8f5bSDavid Ahern 	err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
1063717a8f5bSDavid Ahern 	if (!err) {
1064717a8f5bSDavid Ahern 		nh->fib_nh_dev = fib6_nh.fib_nh_dev;
1065d62607c3SJakub Kicinski 		netdev_hold(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
1066d62607c3SJakub Kicinski 			    GFP_KERNEL);
1067717a8f5bSDavid Ahern 		nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
1068717a8f5bSDavid Ahern 		nh->fib_nh_scope = RT_SCOPE_LINK;
1069717a8f5bSDavid Ahern 
1070717a8f5bSDavid Ahern 		ipv6_stub->fib6_nh_release(&fib6_nh);
1071717a8f5bSDavid Ahern 	}
1072717a8f5bSDavid Ahern 
1073717a8f5bSDavid Ahern 	return err;
1074717a8f5bSDavid Ahern }
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds /*
10776a31d2a9SEric Dumazet  * Picture
10786a31d2a9SEric Dumazet  * -------
10796a31d2a9SEric Dumazet  *
10806a31d2a9SEric Dumazet  * Semantics of nexthop is very messy by historical reasons.
10816a31d2a9SEric Dumazet  * We have to take into account, that:
10826a31d2a9SEric Dumazet  * a) gateway can be actually local interface address,
10836a31d2a9SEric Dumazet  *    so that gatewayed route is direct.
10846a31d2a9SEric Dumazet  * b) gateway must be on-link address, possibly
10856a31d2a9SEric Dumazet  *    described not by an ifaddr, but also by a direct route.
10866a31d2a9SEric Dumazet  * c) If both gateway and interface are specified, they should not
10876a31d2a9SEric Dumazet  *    contradict.
10886a31d2a9SEric Dumazet  * d) If we use tunnel routes, gateway could be not on-link.
10896a31d2a9SEric Dumazet  *
10906a31d2a9SEric Dumazet  * Attempt to reconcile all of these (alas, self-contradictory) conditions
10916a31d2a9SEric Dumazet  * results in pretty ugly and hairy code with obscure logic.
10926a31d2a9SEric Dumazet  *
10936a31d2a9SEric Dumazet  * I chose to generalized it instead, so that the size
10946a31d2a9SEric Dumazet  * of code does not increase practically, but it becomes
10956a31d2a9SEric Dumazet  * much more general.
10966a31d2a9SEric Dumazet  * Every prefix is assigned a "scope" value: "host" is local address,
10976a31d2a9SEric Dumazet  * "link" is direct route,
10986a31d2a9SEric Dumazet  * [ ... "site" ... "interior" ... ]
10996a31d2a9SEric Dumazet  * and "universe" is true gateway route with global meaning.
11006a31d2a9SEric Dumazet  *
11016a31d2a9SEric Dumazet  * Every prefix refers to a set of "nexthop"s (gw, oif),
11026a31d2a9SEric Dumazet  * where gw must have narrower scope. This recursion stops
11036a31d2a9SEric Dumazet  * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
11046a31d2a9SEric Dumazet  * which means that gw is forced to be on link.
11056a31d2a9SEric Dumazet  *
11066a31d2a9SEric Dumazet  * Code is still hairy, but now it is apparently logically
11076a31d2a9SEric Dumazet  * consistent and very flexible. F.e. as by-product it allows
11086a31d2a9SEric Dumazet  * to co-exists in peace independent exterior and interior
11096a31d2a9SEric Dumazet  * routing processes.
11106a31d2a9SEric Dumazet  *
11116a31d2a9SEric Dumazet  * Normally it looks as following.
11126a31d2a9SEric Dumazet  *
11136a31d2a9SEric Dumazet  * {universe prefix}  -> (gw, oif) [scope link]
11146a31d2a9SEric Dumazet  *		  |
11156a31d2a9SEric Dumazet  *		  |-> {link prefix} -> (gw, oif) [scope local]
11166a31d2a9SEric Dumazet  *					|
11176a31d2a9SEric Dumazet  *					|-> {local prefix} (terminal node)
11181da177e4SLinus Torvalds  */
fib_check_nh_v4_gw(struct net * net,struct fib_nh * nh,u32 table,u8 scope,struct netlink_ext_ack * extack)1119448d7248SDavid Ahern static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
1120448d7248SDavid Ahern 			      u8 scope, struct netlink_ext_ack *extack)
11211da177e4SLinus Torvalds {
11226a31d2a9SEric Dumazet 	struct net_device *dev;
11231da177e4SLinus Torvalds 	struct fib_result res;
1124c3fee640SEnrico Weigelt 	int err = 0;
11251da177e4SLinus Torvalds 
1126b75ed8b1SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_ONLINK) {
112730bbaa19SDavid Ahern 		unsigned int addr_type;
11281da177e4SLinus Torvalds 
1129448d7248SDavid Ahern 		if (scope >= RT_SCOPE_LINK) {
1130448d7248SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
11311da177e4SLinus Torvalds 			return -EINVAL;
1132c3ab2b4eSDavid Ahern 		}
1133b75ed8b1SDavid Ahern 		dev = __dev_get_by_index(net, nh->fib_nh_oif);
1134066b1030SDavid Ahern 		if (!dev) {
1135066b1030SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
11361da177e4SLinus Torvalds 			return -ENODEV;
1137066b1030SDavid Ahern 		}
1138c3ab2b4eSDavid Ahern 		if (!(dev->flags & IFF_UP)) {
1139448d7248SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
11401da177e4SLinus Torvalds 			return -ENETDOWN;
1141c3ab2b4eSDavid Ahern 		}
1142448d7248SDavid Ahern 		addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
1143c3ab2b4eSDavid Ahern 		if (addr_type != RTN_UNICAST) {
1144448d7248SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
114530bbaa19SDavid Ahern 			return -EINVAL;
1146c3ab2b4eSDavid Ahern 		}
11478a3d0316SAndy Gospodarek 		if (!netif_carrier_ok(dev))
1148b75ed8b1SDavid Ahern 			nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1149b75ed8b1SDavid Ahern 		nh->fib_nh_dev = dev;
1150d62607c3SJakub Kicinski 		netdev_hold(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
1151b75ed8b1SDavid Ahern 		nh->fib_nh_scope = RT_SCOPE_LINK;
11521da177e4SLinus Torvalds 		return 0;
11531da177e4SLinus Torvalds 	}
1154ebc0ffaeSEric Dumazet 	rcu_read_lock();
11551da177e4SLinus Torvalds 	{
11563bfd8472SDavid Ahern 		struct fib_table *tbl = NULL;
11579ade2286SDavid S. Miller 		struct flowi4 fl4 = {
1158b75ed8b1SDavid Ahern 			.daddr = nh->fib_nh_gw4,
1159448d7248SDavid Ahern 			.flowi4_scope = scope + 1,
1160b75ed8b1SDavid Ahern 			.flowi4_oif = nh->fib_nh_oif,
11616a662719SCong Wang 			.flowi4_iif = LOOPBACK_IFINDEX,
11624e902c57SThomas Graf 		};
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 		/* It is not necessary, but requires a bit of thinking */
11659ade2286SDavid S. Miller 		if (fl4.flowi4_scope < RT_SCOPE_LINK)
11669ade2286SDavid S. Miller 			fl4.flowi4_scope = RT_SCOPE_LINK;
11673bfd8472SDavid Ahern 
11685eea3a63Sguodeqing 		if (table && table != RT_TABLE_MAIN)
1169448d7248SDavid Ahern 			tbl = fib_get_table(net, table);
11703bfd8472SDavid Ahern 
11713bfd8472SDavid Ahern 		if (tbl)
11723bfd8472SDavid Ahern 			err = fib_table_lookup(tbl, &fl4, &res,
11731e313678SEric Dumazet 					       FIB_LOOKUP_IGNORE_LINKSTATE |
11741e313678SEric Dumazet 					       FIB_LOOKUP_NOREF);
11754c9bcd11SDavid Ahern 
11764c9bcd11SDavid Ahern 		/* on error or if no table given do full lookup. This
11774c9bcd11SDavid Ahern 		 * is needed for example when nexthops are in the local
11784c9bcd11SDavid Ahern 		 * table rather than the given table
11794c9bcd11SDavid Ahern 		 */
11804c9bcd11SDavid Ahern 		if (!tbl || err) {
11810eeb075fSAndy Gospodarek 			err = fib_lookup(net, &fl4, &res,
11820eeb075fSAndy Gospodarek 					 FIB_LOOKUP_IGNORE_LINKSTATE);
11834c9bcd11SDavid Ahern 		}
11844c9bcd11SDavid Ahern 
1185ebc0ffaeSEric Dumazet 		if (err) {
1186448d7248SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1187448d7248SDavid Ahern 			goto out;
11881da177e4SLinus Torvalds 		}
1189ebc0ffaeSEric Dumazet 	}
1190448d7248SDavid Ahern 
11911da177e4SLinus Torvalds 	err = -EINVAL;
1192c3ab2b4eSDavid Ahern 	if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1193c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
11941da177e4SLinus Torvalds 		goto out;
1195c3ab2b4eSDavid Ahern 	}
1196b75ed8b1SDavid Ahern 	nh->fib_nh_scope = res.scope;
1197b75ed8b1SDavid Ahern 	nh->fib_nh_oif = FIB_RES_OIF(res);
1198b75ed8b1SDavid Ahern 	nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1199c3ab2b4eSDavid Ahern 	if (!dev) {
1200c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack,
1201c3ab2b4eSDavid Ahern 			       "No egress device for nexthop gateway");
12021da177e4SLinus Torvalds 		goto out;
1203c3ab2b4eSDavid Ahern 	}
1204d62607c3SJakub Kicinski 	netdev_hold(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
12058a3d0316SAndy Gospodarek 	if (!netif_carrier_ok(dev))
1206b75ed8b1SDavid Ahern 		nh->fib_nh_flags |= RTNH_F_LINKDOWN;
12078723e1b4SEric Dumazet 	err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
1208448d7248SDavid Ahern out:
1209448d7248SDavid Ahern 	rcu_read_unlock();
1210448d7248SDavid Ahern 	return err;
1211448d7248SDavid Ahern }
1212448d7248SDavid Ahern 
fib_check_nh_nongw(struct net * net,struct fib_nh * nh,struct netlink_ext_ack * extack)1213448d7248SDavid Ahern static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1214448d7248SDavid Ahern 			      struct netlink_ext_ack *extack)
1215448d7248SDavid Ahern {
12161da177e4SLinus Torvalds 	struct in_device *in_dev;
1217448d7248SDavid Ahern 	int err;
12181da177e4SLinus Torvalds 
1219b75ed8b1SDavid Ahern 	if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1220c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack,
1221c3ab2b4eSDavid Ahern 			       "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
12221da177e4SLinus Torvalds 		return -EINVAL;
1223c3ab2b4eSDavid Ahern 	}
1224448d7248SDavid Ahern 
12258723e1b4SEric Dumazet 	rcu_read_lock();
1226448d7248SDavid Ahern 
12278723e1b4SEric Dumazet 	err = -ENODEV;
1228b75ed8b1SDavid Ahern 	in_dev = inetdev_by_index(net, nh->fib_nh_oif);
122951456b29SIan Morris 	if (!in_dev)
12308723e1b4SEric Dumazet 		goto out;
12318723e1b4SEric Dumazet 	err = -ENETDOWN;
1232c3ab2b4eSDavid Ahern 	if (!(in_dev->dev->flags & IFF_UP)) {
1233c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
12348723e1b4SEric Dumazet 		goto out;
1235c3ab2b4eSDavid Ahern 	}
1236448d7248SDavid Ahern 
1237b75ed8b1SDavid Ahern 	nh->fib_nh_dev = in_dev->dev;
1238d62607c3SJakub Kicinski 	netdev_hold(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
1239e021c329SNicolas Dichtel 	nh->fib_nh_scope = RT_SCOPE_HOST;
1240b75ed8b1SDavid Ahern 	if (!netif_carrier_ok(nh->fib_nh_dev))
1241b75ed8b1SDavid Ahern 		nh->fib_nh_flags |= RTNH_F_LINKDOWN;
12428723e1b4SEric Dumazet 	err = 0;
12438723e1b4SEric Dumazet out:
12448723e1b4SEric Dumazet 	rcu_read_unlock();
12458723e1b4SEric Dumazet 	return err;
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds 
fib_check_nh(struct net * net,struct fib_nh * nh,u32 table,u8 scope,struct netlink_ext_ack * extack)1248ac1fab2dSDavid Ahern int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1249448d7248SDavid Ahern 		 struct netlink_ext_ack *extack)
1250448d7248SDavid Ahern {
1251448d7248SDavid Ahern 	int err;
1252448d7248SDavid Ahern 
1253448d7248SDavid Ahern 	if (nh->fib_nh_gw_family == AF_INET)
1254ac1fab2dSDavid Ahern 		err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
1255717a8f5bSDavid Ahern 	else if (nh->fib_nh_gw_family == AF_INET6)
1256717a8f5bSDavid Ahern 		err = fib_check_nh_v6_gw(net, nh, table, extack);
1257448d7248SDavid Ahern 	else
1258448d7248SDavid Ahern 		err = fib_check_nh_nongw(net, nh, extack);
1259448d7248SDavid Ahern 
1260448d7248SDavid Ahern 	return err;
1261448d7248SDavid Ahern }
1262448d7248SDavid Ahern 
126379eb15daSEric Dumazet static struct hlist_head *
fib_info_laddrhash_bucket(const struct net * net,__be32 val)126479eb15daSEric Dumazet fib_info_laddrhash_bucket(const struct net *net, __be32 val)
12651da177e4SLinus Torvalds {
126679eb15daSEric Dumazet 	u32 slot = hash_32(net_hash_mix(net) ^ (__force u32)val,
126779eb15daSEric Dumazet 			   fib_info_hash_bits);
12681da177e4SLinus Torvalds 
126979eb15daSEric Dumazet 	return &fib_info_laddrhash[slot];
12701da177e4SLinus Torvalds }
12711da177e4SLinus Torvalds 
fib_info_hash_move(struct hlist_head * new_info_hash,struct hlist_head * new_laddrhash,unsigned int new_size)1272123b9731SDavid S. Miller static void fib_info_hash_move(struct hlist_head *new_info_hash,
12731da177e4SLinus Torvalds 			       struct hlist_head *new_laddrhash,
12741da177e4SLinus Torvalds 			       unsigned int new_size)
12751da177e4SLinus Torvalds {
1276b7656e7fSDavid S. Miller 	struct hlist_head *old_info_hash, *old_laddrhash;
1277123b9731SDavid S. Miller 	unsigned int old_size = fib_info_hash_size;
1278ca73b68aSEric Dumazet 	unsigned int i;
12791da177e4SLinus Torvalds 
1280832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
1281b7656e7fSDavid S. Miller 	old_info_hash = fib_info_hash;
1282b7656e7fSDavid S. Miller 	old_laddrhash = fib_info_laddrhash;
1283123b9731SDavid S. Miller 	fib_info_hash_size = new_size;
128479eb15daSEric Dumazet 	fib_info_hash_bits = ilog2(new_size);
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	for (i = 0; i < old_size; i++) {
12871da177e4SLinus Torvalds 		struct hlist_head *head = &fib_info_hash[i];
1288b67bfe0dSSasha Levin 		struct hlist_node *n;
12891da177e4SLinus Torvalds 		struct fib_info *fi;
12901da177e4SLinus Torvalds 
1291b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(fi, n, head, fib_hash) {
12921da177e4SLinus Torvalds 			struct hlist_head *dest;
12931da177e4SLinus Torvalds 			unsigned int new_hash;
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 			new_hash = fib_info_hashfn(fi);
12961da177e4SLinus Torvalds 			dest = &new_info_hash[new_hash];
12971da177e4SLinus Torvalds 			hlist_add_head(&fi->fib_hash, dest);
12981da177e4SLinus Torvalds 		}
12991da177e4SLinus Torvalds 	}
13001da177e4SLinus Torvalds 	fib_info_hash = new_info_hash;
13011da177e4SLinus Torvalds 
130279eb15daSEric Dumazet 	fib_info_laddrhash = new_laddrhash;
13031da177e4SLinus Torvalds 	for (i = 0; i < old_size; i++) {
130479eb15daSEric Dumazet 		struct hlist_head *lhead = &old_laddrhash[i];
1305b67bfe0dSSasha Levin 		struct hlist_node *n;
13061da177e4SLinus Torvalds 		struct fib_info *fi;
13071da177e4SLinus Torvalds 
1308b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
13091da177e4SLinus Torvalds 			struct hlist_head *ldest;
13101da177e4SLinus Torvalds 
131179eb15daSEric Dumazet 			ldest = fib_info_laddrhash_bucket(fi->fib_net,
131279eb15daSEric Dumazet 							  fi->fib_prefsrc);
13131da177e4SLinus Torvalds 			hlist_add_head(&fi->fib_lhash, ldest);
13141da177e4SLinus Torvalds 		}
13151da177e4SLinus Torvalds 	}
13161da177e4SLinus Torvalds 
1317832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
1318b7656e7fSDavid S. Miller 
1319ca73b68aSEric Dumazet 	kvfree(old_info_hash);
1320ca73b68aSEric Dumazet 	kvfree(old_laddrhash);
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds 
fib_info_update_nhc_saddr(struct net * net,struct fib_nh_common * nhc,unsigned char scope)1323dcb1ecb5SDavid Ahern __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
1324c3669486SDavid Ahern 				 unsigned char scope)
1325436c3b66SDavid S. Miller {
1326dcb1ecb5SDavid Ahern 	struct fib_nh *nh;
1327195374d8SEric Dumazet 	__be32 saddr;
1328dcb1ecb5SDavid Ahern 
1329dcb1ecb5SDavid Ahern 	if (nhc->nhc_family != AF_INET)
1330dcb1ecb5SDavid Ahern 		return inet_select_addr(nhc->nhc_dev, 0, scope);
1331dcb1ecb5SDavid Ahern 
1332dcb1ecb5SDavid Ahern 	nh = container_of(nhc, struct fib_nh, nh_common);
1333195374d8SEric Dumazet 	saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
1334436c3b66SDavid S. Miller 
1335195374d8SEric Dumazet 	WRITE_ONCE(nh->nh_saddr, saddr);
1336195374d8SEric Dumazet 	WRITE_ONCE(nh->nh_saddr_genid, atomic_read(&net->ipv4.dev_addr_genid));
1337195374d8SEric Dumazet 
1338195374d8SEric Dumazet 	return saddr;
1339436c3b66SDavid S. Miller }
1340436c3b66SDavid S. Miller 
fib_result_prefsrc(struct net * net,struct fib_result * res)1341eba618abSDavid Ahern __be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1342eba618abSDavid Ahern {
1343eba618abSDavid Ahern 	struct fib_nh_common *nhc = res->nhc;
1344eba618abSDavid Ahern 
1345eba618abSDavid Ahern 	if (res->fi->fib_prefsrc)
1346eba618abSDavid Ahern 		return res->fi->fib_prefsrc;
1347eba618abSDavid Ahern 
1348dcb1ecb5SDavid Ahern 	if (nhc->nhc_family == AF_INET) {
1349dcb1ecb5SDavid Ahern 		struct fib_nh *nh;
1350dcb1ecb5SDavid Ahern 
1351eba618abSDavid Ahern 		nh = container_of(nhc, struct fib_nh, nh_common);
1352195374d8SEric Dumazet 		if (READ_ONCE(nh->nh_saddr_genid) ==
1353195374d8SEric Dumazet 		    atomic_read(&net->ipv4.dev_addr_genid))
1354195374d8SEric Dumazet 			return READ_ONCE(nh->nh_saddr);
1355dcb1ecb5SDavid Ahern 	}
1356eba618abSDavid Ahern 
1357dcb1ecb5SDavid Ahern 	return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
1358eba618abSDavid Ahern }
1359eba618abSDavid Ahern 
fib_valid_prefsrc(struct fib_config * cfg,__be32 fib_prefsrc)1360021dd3b8SDavid Ahern static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1361021dd3b8SDavid Ahern {
1362021dd3b8SDavid Ahern 	if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1363021dd3b8SDavid Ahern 	    fib_prefsrc != cfg->fc_dst) {
13649b8ff518SDavid Ahern 		u32 tb_id = cfg->fc_table;
1365e1b8d903SDavid Ahern 		int rc;
1366021dd3b8SDavid Ahern 
1367021dd3b8SDavid Ahern 		if (tb_id == RT_TABLE_MAIN)
1368021dd3b8SDavid Ahern 			tb_id = RT_TABLE_LOCAL;
1369021dd3b8SDavid Ahern 
1370e1b8d903SDavid Ahern 		rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1371e1b8d903SDavid Ahern 					  fib_prefsrc, tb_id);
1372e1b8d903SDavid Ahern 
1373e1b8d903SDavid Ahern 		if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1374e1b8d903SDavid Ahern 			rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1375e1b8d903SDavid Ahern 						  fib_prefsrc, RT_TABLE_LOCAL);
1376021dd3b8SDavid Ahern 		}
1377e1b8d903SDavid Ahern 
1378e1b8d903SDavid Ahern 		if (rc != RTN_LOCAL)
1379e1b8d903SDavid Ahern 			return false;
1380021dd3b8SDavid Ahern 	}
1381021dd3b8SDavid Ahern 	return true;
1382021dd3b8SDavid Ahern }
1383021dd3b8SDavid Ahern 
fib_create_info(struct fib_config * cfg,struct netlink_ext_ack * extack)13846d8422a1SDavid Ahern struct fib_info *fib_create_info(struct fib_config *cfg,
13856d8422a1SDavid Ahern 				 struct netlink_ext_ack *extack)
13861da177e4SLinus Torvalds {
13871da177e4SLinus Torvalds 	int err;
13881da177e4SLinus Torvalds 	struct fib_info *fi = NULL;
13894c7e8084SDavid Ahern 	struct nexthop *nh = NULL;
13901da177e4SLinus Torvalds 	struct fib_info *ofi;
13911da177e4SLinus Torvalds 	int nhs = 1;
13927462bd74SDenis V. Lunev 	struct net *net = cfg->fc_nlinfo.nl_net;
13931da177e4SLinus Torvalds 
13944c8237cdSDavid S. Miller 	if (cfg->fc_type > RTN_MAX)
13954c8237cdSDavid S. Miller 		goto err_inval;
13964c8237cdSDavid S. Miller 
13971da177e4SLinus Torvalds 	/* Fast check to catch the most weird cases */
1398c3ab2b4eSDavid Ahern 	if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1399c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid scope");
14001da177e4SLinus Torvalds 		goto err_inval;
1401c3ab2b4eSDavid Ahern 	}
14021da177e4SLinus Torvalds 
1403c3ab2b4eSDavid Ahern 	if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1404c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack,
1405c3ab2b4eSDavid Ahern 			       "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
140680610229SJulian Anastasov 		goto err_inval;
1407c3ab2b4eSDavid Ahern 	}
140880610229SJulian Anastasov 
1409493ced1aSDavid Ahern 	if (cfg->fc_nh_id) {
14106c48ea5fSDavid Ahern 		if (!cfg->fc_mx) {
14116c48ea5fSDavid Ahern 			fi = fib_find_info_nh(net, cfg);
14126c48ea5fSDavid Ahern 			if (fi) {
141379976892SYajun Deng 				refcount_inc(&fi->fib_treeref);
14146c48ea5fSDavid Ahern 				return fi;
14156c48ea5fSDavid Ahern 			}
14166c48ea5fSDavid Ahern 		}
14176c48ea5fSDavid Ahern 
1418493ced1aSDavid Ahern 		nh = nexthop_find_by_id(net, cfg->fc_nh_id);
1419493ced1aSDavid Ahern 		if (!nh) {
1420493ced1aSDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
1421493ced1aSDavid Ahern 			goto err_inval;
1422493ced1aSDavid Ahern 		}
1423493ced1aSDavid Ahern 		nhs = 0;
1424493ced1aSDavid Ahern 	}
1425493ced1aSDavid Ahern 
14261da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
14274e902c57SThomas Graf 	if (cfg->fc_mp) {
14286d8422a1SDavid Ahern 		nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
14291da177e4SLinus Torvalds 		if (nhs == 0)
14301da177e4SLinus Torvalds 			goto err_inval;
14311da177e4SLinus Torvalds 	}
14321da177e4SLinus Torvalds #endif
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 	err = -ENOBUFS;
14350a6e6b3cSEric Dumazet 
14360a6e6b3cSEric Dumazet 	/* Paired with WRITE_ONCE() in fib_release_info() */
14370a6e6b3cSEric Dumazet 	if (READ_ONCE(fib_info_cnt) >= fib_info_hash_size) {
1438123b9731SDavid S. Miller 		unsigned int new_size = fib_info_hash_size << 1;
14391da177e4SLinus Torvalds 		struct hlist_head *new_info_hash;
14401da177e4SLinus Torvalds 		struct hlist_head *new_laddrhash;
1441ca73b68aSEric Dumazet 		size_t bytes;
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 		if (!new_size)
1444d94ce9b2SEric Dumazet 			new_size = 16;
1445ca73b68aSEric Dumazet 		bytes = (size_t)new_size * sizeof(struct hlist_head *);
1446ca73b68aSEric Dumazet 		new_info_hash = kvzalloc(bytes, GFP_KERNEL);
1447ca73b68aSEric Dumazet 		new_laddrhash = kvzalloc(bytes, GFP_KERNEL);
14481da177e4SLinus Torvalds 		if (!new_info_hash || !new_laddrhash) {
1449ca73b68aSEric Dumazet 			kvfree(new_info_hash);
1450ca73b68aSEric Dumazet 			kvfree(new_laddrhash);
1451ca73b68aSEric Dumazet 		} else {
1452123b9731SDavid S. Miller 			fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
1453ca73b68aSEric Dumazet 		}
1454123b9731SDavid S. Miller 		if (!fib_info_hash_size)
14551da177e4SLinus Torvalds 			goto failure;
14561da177e4SLinus Torvalds 	}
14571da177e4SLinus Torvalds 
14581f533ba6SGustavo A. R. Silva 	fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
145951456b29SIan Morris 	if (!fi)
14601da177e4SLinus Torvalds 		goto failure;
146161e2bbafSJason Xing 	fi->fib_metrics = ip_fib_metrics_init(cfg->fc_mx, cfg->fc_mx_len, extack);
146288e235b8SEnrico Weigelt 	if (IS_ERR(fi->fib_metrics)) {
1463767a2217SDavid Ahern 		err = PTR_ERR(fi->fib_metrics);
1464187e5b3aSEric Dumazet 		kfree(fi);
1465187e5b3aSEric Dumazet 		return ERR_PTR(err);
1466187e5b3aSEric Dumazet 	}
1467767a2217SDavid Ahern 
1468efd7ef1cSEric W. Biederman 	fi->fib_net = net;
14694e902c57SThomas Graf 	fi->fib_protocol = cfg->fc_protocol;
147037e826c5SDavid S. Miller 	fi->fib_scope = cfg->fc_scope;
14714e902c57SThomas Graf 	fi->fib_flags = cfg->fc_flags;
14724e902c57SThomas Graf 	fi->fib_priority = cfg->fc_priority;
14734e902c57SThomas Graf 	fi->fib_prefsrc = cfg->fc_prefsrc;
1474f4ef85bbSEric Dumazet 	fi->fib_type = cfg->fc_type;
14755a56a0b3SMark Tomlinson 	fi->fib_tb_id = cfg->fc_table;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	fi->fib_nhs = nhs;
14784c7e8084SDavid Ahern 	if (nh) {
14794c7e8084SDavid Ahern 		if (!nexthop_get(nh)) {
14804c7e8084SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
14814c7e8084SDavid Ahern 			err = -EINVAL;
14824c7e8084SDavid Ahern 		} else {
14834c7e8084SDavid Ahern 			err = 0;
14844c7e8084SDavid Ahern 			fi->nh = nh;
14854c7e8084SDavid Ahern 		}
14864c7e8084SDavid Ahern 	} else {
14871da177e4SLinus Torvalds 		change_nexthops(fi) {
148871fceff0SDavid S. Miller 			nexthop_nh->nh_parent = fi;
14891da177e4SLinus Torvalds 		} endfor_nexthops(fi)
14901da177e4SLinus Torvalds 
1491e4516ef6SDavid Ahern 		if (cfg->fc_mp)
14924c7e8084SDavid Ahern 			err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
14934c7e8084SDavid Ahern 					  extack);
1494e4516ef6SDavid Ahern 		else
1495e4516ef6SDavid Ahern 			err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
14964c7e8084SDavid Ahern 	}
1497e4516ef6SDavid Ahern 
14984e902c57SThomas Graf 	if (err != 0)
14991da177e4SLinus Torvalds 		goto failure;
15001da177e4SLinus Torvalds 
15014e902c57SThomas Graf 	if (fib_props[cfg->fc_type].error) {
1502f35b794bSDavid Ahern 		if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
1503c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
1504c3ab2b4eSDavid Ahern 				       "Gateway, device and multipath can not be specified for this route type");
15051da177e4SLinus Torvalds 			goto err_inval;
1506c3ab2b4eSDavid Ahern 		}
15071da177e4SLinus Torvalds 		goto link_it;
15084c8237cdSDavid S. Miller 	} else {
15094c8237cdSDavid S. Miller 		switch (cfg->fc_type) {
15104c8237cdSDavid S. Miller 		case RTN_UNICAST:
15114c8237cdSDavid S. Miller 		case RTN_LOCAL:
15124c8237cdSDavid S. Miller 		case RTN_BROADCAST:
15134c8237cdSDavid S. Miller 		case RTN_ANYCAST:
15144c8237cdSDavid S. Miller 		case RTN_MULTICAST:
15154c8237cdSDavid S. Miller 			break;
15164c8237cdSDavid S. Miller 		default:
1517c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid route type");
15184c8237cdSDavid S. Miller 			goto err_inval;
15194c8237cdSDavid S. Miller 		}
15201da177e4SLinus Torvalds 	}
15211da177e4SLinus Torvalds 
1522c3ab2b4eSDavid Ahern 	if (cfg->fc_scope > RT_SCOPE_HOST) {
1523c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid scope");
15241da177e4SLinus Torvalds 		goto err_inval;
1525c3ab2b4eSDavid Ahern 	}
15261da177e4SLinus Torvalds 
15274c7e8084SDavid Ahern 	if (fi->nh) {
15284c7e8084SDavid Ahern 		err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
15294c7e8084SDavid Ahern 		if (err)
15304c7e8084SDavid Ahern 			goto failure;
15314c7e8084SDavid Ahern 	} else if (cfg->fc_scope == RT_SCOPE_HOST) {
15321da177e4SLinus Torvalds 		struct fib_nh *nh = fi->fib_nh;
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 		/* Local address is added. */
1535c3ab2b4eSDavid Ahern 		if (nhs != 1) {
1536c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
1537c3ab2b4eSDavid Ahern 				       "Route with host scope can not have multiple nexthops");
15386d8422a1SDavid Ahern 			goto err_inval;
1539c3ab2b4eSDavid Ahern 		}
1540bdf00467SDavid Ahern 		if (nh->fib_nh_gw_family) {
1541c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
1542c3ab2b4eSDavid Ahern 				       "Route with host scope can not have a gateway");
15431da177e4SLinus Torvalds 			goto err_inval;
1544c3ab2b4eSDavid Ahern 		}
1545b75ed8b1SDavid Ahern 		nh->fib_nh_scope = RT_SCOPE_NOWHERE;
15465481d73fSDavid Ahern 		nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
15471da177e4SLinus Torvalds 		err = -ENODEV;
1548b75ed8b1SDavid Ahern 		if (!nh->fib_nh_dev)
15491da177e4SLinus Torvalds 			goto failure;
1550e44b14ebSEric Dumazet 		netdev_tracker_alloc(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
1551e44b14ebSEric Dumazet 				     GFP_KERNEL);
15521da177e4SLinus Torvalds 	} else {
15538a3d0316SAndy Gospodarek 		int linkdown = 0;
15548a3d0316SAndy Gospodarek 
15551da177e4SLinus Torvalds 		change_nexthops(fi) {
1556ac1fab2dSDavid Ahern 			err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1557ac1fab2dSDavid Ahern 					   cfg->fc_table, cfg->fc_scope,
1558ac1fab2dSDavid Ahern 					   extack);
15596a31d2a9SEric Dumazet 			if (err != 0)
15601da177e4SLinus Torvalds 				goto failure;
1561b75ed8b1SDavid Ahern 			if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
15628a3d0316SAndy Gospodarek 				linkdown++;
15631da177e4SLinus Torvalds 		} endfor_nexthops(fi)
15648a3d0316SAndy Gospodarek 		if (linkdown == fi->fib_nhs)
15658a3d0316SAndy Gospodarek 			fi->fib_flags |= RTNH_F_LINKDOWN;
15661da177e4SLinus Torvalds 	}
15671da177e4SLinus Torvalds 
1568c3ab2b4eSDavid Ahern 	if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1569c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
15701da177e4SLinus Torvalds 		goto err_inval;
1571c3ab2b4eSDavid Ahern 	}
15721da177e4SLinus Torvalds 
15734c7e8084SDavid Ahern 	if (!fi->nh) {
15741fc050a1SDavid S. Miller 		change_nexthops(fi) {
1575dcb1ecb5SDavid Ahern 			fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
1576dcb1ecb5SDavid Ahern 						  fi->fib_scope);
157719a9d136SDavid Ahern 			if (nexthop_nh->fib_nh_gw_family == AF_INET6)
157819a9d136SDavid Ahern 				fi->fib_nh_is_v6 = true;
15791fc050a1SDavid S. Miller 		} endfor_nexthops(fi)
15801fc050a1SDavid S. Miller 
15810e884c78SPeter Nørlund 		fib_rebalance(fi);
15824c7e8084SDavid Ahern 	}
15830e884c78SPeter Nørlund 
15841da177e4SLinus Torvalds link_it:
15856a31d2a9SEric Dumazet 	ofi = fib_find_info(fi);
15866a31d2a9SEric Dumazet 	if (ofi) {
1587fce92af1SEric Dumazet 		/* fib_table_lookup() should not see @fi yet. */
15881da177e4SLinus Torvalds 		fi->fib_dead = 1;
15891da177e4SLinus Torvalds 		free_fib_info(fi);
159079976892SYajun Deng 		refcount_inc(&ofi->fib_treeref);
15911da177e4SLinus Torvalds 		return ofi;
15921da177e4SLinus Torvalds 	}
15931da177e4SLinus Torvalds 
159428814cd1SDavid Ahern 	refcount_set(&fi->fib_treeref, 1);
15950029c0deSReshetova, Elena 	refcount_set(&fi->fib_clntref, 1);
1596832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
15970a6e6b3cSEric Dumazet 	fib_info_cnt++;
15981da177e4SLinus Torvalds 	hlist_add_head(&fi->fib_hash,
15991da177e4SLinus Torvalds 		       &fib_info_hash[fib_info_hashfn(fi)]);
16001da177e4SLinus Torvalds 	if (fi->fib_prefsrc) {
16011da177e4SLinus Torvalds 		struct hlist_head *head;
16021da177e4SLinus Torvalds 
160379eb15daSEric Dumazet 		head = fib_info_laddrhash_bucket(net, fi->fib_prefsrc);
16041da177e4SLinus Torvalds 		hlist_add_head(&fi->fib_lhash, head);
16051da177e4SLinus Torvalds 	}
16064c7e8084SDavid Ahern 	if (fi->nh) {
16074c7e8084SDavid Ahern 		list_add(&fi->nh_list, &nh->fi_list);
16084c7e8084SDavid Ahern 	} else {
16091da177e4SLinus Torvalds 		change_nexthops(fi) {
16101da177e4SLinus Torvalds 			struct hlist_head *head;
16111da177e4SLinus Torvalds 
1612b75ed8b1SDavid Ahern 			if (!nexthop_nh->fib_nh_dev)
16131da177e4SLinus Torvalds 				continue;
1614d07418afSEric Dumazet 			head = fib_info_devhash_bucket(nexthop_nh->fib_nh_dev);
161571fceff0SDavid S. Miller 			hlist_add_head(&nexthop_nh->nh_hash, head);
16161da177e4SLinus Torvalds 		} endfor_nexthops(fi)
16174c7e8084SDavid Ahern 	}
1618832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
16191da177e4SLinus Torvalds 	return fi;
16201da177e4SLinus Torvalds 
16211da177e4SLinus Torvalds err_inval:
16221da177e4SLinus Torvalds 	err = -EINVAL;
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds failure:
16251da177e4SLinus Torvalds 	if (fi) {
1626fce92af1SEric Dumazet 		/* fib_table_lookup() should not see @fi yet. */
16271da177e4SLinus Torvalds 		fi->fib_dead = 1;
16281da177e4SLinus Torvalds 		free_fib_info(fi);
16291da177e4SLinus Torvalds 	}
16304e902c57SThomas Graf 
16314e902c57SThomas Graf 	return ERR_PTR(err);
16321da177e4SLinus Torvalds }
16331da177e4SLinus Torvalds 
fib_nexthop_info(struct sk_buff * skb,const struct fib_nh_common * nhc,u8 rt_family,unsigned char * flags,bool skip_oif)1634c0a72077SDavid Ahern int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
16357bdf4de1SDonald Sharp 		     u8 rt_family, unsigned char *flags, bool skip_oif)
1636b0f60193SDavid Ahern {
1637c2364199SDavid Ahern 	if (nhc->nhc_flags & RTNH_F_DEAD)
1638b0f60193SDavid Ahern 		*flags |= RTNH_F_DEAD;
1639b0f60193SDavid Ahern 
1640c2364199SDavid Ahern 	if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
1641b0f60193SDavid Ahern 		*flags |= RTNH_F_LINKDOWN;
1642b0f60193SDavid Ahern 
1643b0f60193SDavid Ahern 		rcu_read_lock();
1644c2364199SDavid Ahern 		switch (nhc->nhc_family) {
1645c2364199SDavid Ahern 		case AF_INET:
1646c2364199SDavid Ahern 			if (ip_ignore_linkdown(nhc->nhc_dev))
1647b0f60193SDavid Ahern 				*flags |= RTNH_F_DEAD;
1648c2364199SDavid Ahern 			break;
1649c0a72077SDavid Ahern 		case AF_INET6:
1650c0a72077SDavid Ahern 			if (ip6_ignore_linkdown(nhc->nhc_dev))
1651c0a72077SDavid Ahern 				*flags |= RTNH_F_DEAD;
1652c0a72077SDavid Ahern 			break;
1653c2364199SDavid Ahern 		}
1654b0f60193SDavid Ahern 		rcu_read_unlock();
1655b0f60193SDavid Ahern 	}
1656b0f60193SDavid Ahern 
1657bdf00467SDavid Ahern 	switch (nhc->nhc_gw_family) {
1658c2364199SDavid Ahern 	case AF_INET:
1659c2364199SDavid Ahern 		if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1660b0f60193SDavid Ahern 			goto nla_put_failure;
1661c2364199SDavid Ahern 		break;
1662c0a72077SDavid Ahern 	case AF_INET6:
1663d1566268SDavid Ahern 		/* if gateway family does not match nexthop family
1664d1566268SDavid Ahern 		 * gateway is encoded as RTA_VIA
1665d1566268SDavid Ahern 		 */
16667bdf4de1SDonald Sharp 		if (rt_family != nhc->nhc_gw_family) {
1667d1566268SDavid Ahern 			int alen = sizeof(struct in6_addr);
1668d1566268SDavid Ahern 			struct nlattr *nla;
1669d1566268SDavid Ahern 			struct rtvia *via;
1670d1566268SDavid Ahern 
1671d1566268SDavid Ahern 			nla = nla_reserve(skb, RTA_VIA, alen + 2);
1672d1566268SDavid Ahern 			if (!nla)
1673c0a72077SDavid Ahern 				goto nla_put_failure;
1674d1566268SDavid Ahern 
1675d1566268SDavid Ahern 			via = nla_data(nla);
1676d1566268SDavid Ahern 			via->rtvia_family = AF_INET6;
1677d1566268SDavid Ahern 			memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
1678d1566268SDavid Ahern 		} else if (nla_put_in6_addr(skb, RTA_GATEWAY,
1679d1566268SDavid Ahern 					    &nhc->nhc_gw.ipv6) < 0) {
1680d1566268SDavid Ahern 			goto nla_put_failure;
1681d1566268SDavid Ahern 		}
1682c0a72077SDavid Ahern 		break;
1683c2364199SDavid Ahern 	}
1684b0f60193SDavid Ahern 
1685ca787e0bSIdo Schimmel 	*flags |= (nhc->nhc_flags &
1686ca787e0bSIdo Schimmel 		   (RTNH_F_ONLINK | RTNH_F_OFFLOAD | RTNH_F_TRAP));
1687b0f60193SDavid Ahern 
1688c2364199SDavid Ahern 	if (!skip_oif && nhc->nhc_dev &&
1689c2364199SDavid Ahern 	    nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
1690b0f60193SDavid Ahern 		goto nla_put_failure;
1691b0f60193SDavid Ahern 
1692c2364199SDavid Ahern 	if (nhc->nhc_lwtstate &&
1693ffa8ce54SDavid Ahern 	    lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1694ffa8ce54SDavid Ahern 				RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
1695b0f60193SDavid Ahern 		goto nla_put_failure;
1696b0f60193SDavid Ahern 
1697b0f60193SDavid Ahern 	return 0;
1698b0f60193SDavid Ahern 
1699b0f60193SDavid Ahern nla_put_failure:
1700b0f60193SDavid Ahern 	return -EMSGSIZE;
1701b0f60193SDavid Ahern }
1702c0a72077SDavid Ahern EXPORT_SYMBOL_GPL(fib_nexthop_info);
1703b0f60193SDavid Ahern 
1704c0a72077SDavid Ahern #if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
fib_add_nexthop(struct sk_buff * skb,const struct fib_nh_common * nhc,int nh_weight,u8 rt_family,u32 nh_tclassid)1705c0a72077SDavid Ahern int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1706597aa16cSXiao Liang 		    int nh_weight, u8 rt_family, u32 nh_tclassid)
1707b0f60193SDavid Ahern {
1708c2364199SDavid Ahern 	const struct net_device *dev = nhc->nhc_dev;
1709b0f60193SDavid Ahern 	struct rtnexthop *rtnh;
1710ecc5663cSDavid Ahern 	unsigned char flags = 0;
1711b0f60193SDavid Ahern 
1712b0f60193SDavid Ahern 	rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1713b0f60193SDavid Ahern 	if (!rtnh)
1714b0f60193SDavid Ahern 		goto nla_put_failure;
1715b0f60193SDavid Ahern 
1716c2364199SDavid Ahern 	rtnh->rtnh_hops = nh_weight - 1;
1717b0f60193SDavid Ahern 	rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1718b0f60193SDavid Ahern 
17197bdf4de1SDonald Sharp 	if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
1720b0f60193SDavid Ahern 		goto nla_put_failure;
1721b0f60193SDavid Ahern 
1722b0f60193SDavid Ahern 	rtnh->rtnh_flags = flags;
1723b0f60193SDavid Ahern 
1724597aa16cSXiao Liang 	if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
1725597aa16cSXiao Liang 		goto nla_put_failure;
1726597aa16cSXiao Liang 
1727b0f60193SDavid Ahern 	/* length of rtnetlink header + attributes */
1728b0f60193SDavid Ahern 	rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1729b0f60193SDavid Ahern 
1730b0f60193SDavid Ahern 	return 0;
1731b0f60193SDavid Ahern 
1732b0f60193SDavid Ahern nla_put_failure:
1733b0f60193SDavid Ahern 	return -EMSGSIZE;
1734b0f60193SDavid Ahern }
1735c0a72077SDavid Ahern EXPORT_SYMBOL_GPL(fib_add_nexthop);
1736c2364199SDavid Ahern #endif
1737b0f60193SDavid Ahern 
1738c2364199SDavid Ahern #ifdef CONFIG_IP_ROUTE_MULTIPATH
fib_add_multipath(struct sk_buff * skb,struct fib_info * fi)1739b0f60193SDavid Ahern static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1740b0f60193SDavid Ahern {
1741b0f60193SDavid Ahern 	struct nlattr *mp;
1742b0f60193SDavid Ahern 
1743ae0be8deSMichal Kubecek 	mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
1744b0f60193SDavid Ahern 	if (!mp)
1745b0f60193SDavid Ahern 		goto nla_put_failure;
1746b0f60193SDavid Ahern 
17474c7e8084SDavid Ahern 	if (unlikely(fi->nh)) {
17487bdf4de1SDonald Sharp 		if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
17494c7e8084SDavid Ahern 			goto nla_put_failure;
17504c7e8084SDavid Ahern 		goto mp_end;
17514c7e8084SDavid Ahern 	}
17524c7e8084SDavid Ahern 
1753b0f60193SDavid Ahern 	for_nexthops(fi) {
1754597aa16cSXiao Liang 		u32 nh_tclassid = 0;
1755b0f60193SDavid Ahern #ifdef CONFIG_IP_ROUTE_CLASSID
1756597aa16cSXiao Liang 		nh_tclassid = nh->nh_tclassid;
1757b0f60193SDavid Ahern #endif
1758597aa16cSXiao Liang 		if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
1759597aa16cSXiao Liang 				    AF_INET, nh_tclassid) < 0)
1760597aa16cSXiao Liang 			goto nla_put_failure;
1761b0f60193SDavid Ahern 	} endfor_nexthops(fi);
1762b0f60193SDavid Ahern 
17634c7e8084SDavid Ahern mp_end:
1764b0f60193SDavid Ahern 	nla_nest_end(skb, mp);
1765b0f60193SDavid Ahern 
1766b0f60193SDavid Ahern 	return 0;
1767b0f60193SDavid Ahern 
1768b0f60193SDavid Ahern nla_put_failure:
1769b0f60193SDavid Ahern 	return -EMSGSIZE;
1770b0f60193SDavid Ahern }
1771b0f60193SDavid Ahern #else
fib_add_multipath(struct sk_buff * skb,struct fib_info * fi)1772b0f60193SDavid Ahern static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1773b0f60193SDavid Ahern {
1774b0f60193SDavid Ahern 	return 0;
1775b0f60193SDavid Ahern }
1776b0f60193SDavid Ahern #endif
1777b0f60193SDavid Ahern 
fib_dump_info(struct sk_buff * skb,u32 portid,u32 seq,int event,const struct fib_rt_info * fri,unsigned int flags)177815e47304SEric W. Biederman int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
177908554789SAmit Cohen 		  const struct fib_rt_info *fri, unsigned int flags)
17801da177e4SLinus Torvalds {
17811e301fd0SIdo Schimmel 	unsigned int nhs = fib_info_num_path(fri->fi);
17821e301fd0SIdo Schimmel 	struct fib_info *fi = fri->fi;
17831e301fd0SIdo Schimmel 	u32 tb_id = fri->tb_id;
17841da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
1785be403ea1SThomas Graf 	struct rtmsg *rtm;
17861da177e4SLinus Torvalds 
178715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
178851456b29SIan Morris 	if (!nlh)
178926932566SPatrick McHardy 		return -EMSGSIZE;
1790be403ea1SThomas Graf 
1791be403ea1SThomas Graf 	rtm = nlmsg_data(nlh);
17921da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET;
17931e301fd0SIdo Schimmel 	rtm->rtm_dst_len = fri->dst_len;
17941da177e4SLinus Torvalds 	rtm->rtm_src_len = 0;
1795888ade8fSGuillaume Nault 	rtm->rtm_tos = inet_dscp_to_dsfield(fri->dscp);
1796709772e6SKrzysztof Piotr Oledzki 	if (tb_id < 256)
17971da177e4SLinus Torvalds 		rtm->rtm_table = tb_id;
1798709772e6SKrzysztof Piotr Oledzki 	else
1799709772e6SKrzysztof Piotr Oledzki 		rtm->rtm_table = RT_TABLE_COMPAT;
1800f3756b79SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, tb_id))
1801f3756b79SDavid S. Miller 		goto nla_put_failure;
18021e301fd0SIdo Schimmel 	rtm->rtm_type = fri->type;
18031da177e4SLinus Torvalds 	rtm->rtm_flags = fi->fib_flags;
180437e826c5SDavid S. Miller 	rtm->rtm_scope = fi->fib_scope;
18051da177e4SLinus Torvalds 	rtm->rtm_protocol = fi->fib_protocol;
1806be403ea1SThomas Graf 
1807f3756b79SDavid S. Miller 	if (rtm->rtm_dst_len &&
18081e301fd0SIdo Schimmel 	    nla_put_in_addr(skb, RTA_DST, fri->dst))
1809f3756b79SDavid S. Miller 		goto nla_put_failure;
1810f3756b79SDavid S. Miller 	if (fi->fib_priority &&
1811f3756b79SDavid S. Miller 	    nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1812f3756b79SDavid S. Miller 		goto nla_put_failure;
18133fb07dafSEric Dumazet 	if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
1814be403ea1SThomas Graf 		goto nla_put_failure;
1815be403ea1SThomas Graf 
1816f3756b79SDavid S. Miller 	if (fi->fib_prefsrc &&
1817930345eaSJiri Benc 	    nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
1818f3756b79SDavid S. Miller 		goto nla_put_failure;
18194c7e8084SDavid Ahern 
18204c7e8084SDavid Ahern 	if (fi->nh) {
18214c7e8084SDavid Ahern 		if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
18224c7e8084SDavid Ahern 			goto nla_put_failure;
18234c7e8084SDavid Ahern 		if (nexthop_is_blackhole(fi->nh))
18244c7e8084SDavid Ahern 			rtm->rtm_type = RTN_BLACKHOLE;
1825bdf00bf2SKuniyuki Iwashima 		if (!READ_ONCE(fi->fib_net->ipv4.sysctl_nexthop_compat_mode))
18264f80116dSRoopa Prabhu 			goto offload;
18274c7e8084SDavid Ahern 	}
18284c7e8084SDavid Ahern 
18295481d73fSDavid Ahern 	if (nhs == 1) {
1830dcb1ecb5SDavid Ahern 		const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
1831ecc5663cSDavid Ahern 		unsigned char flags = 0;
1832be403ea1SThomas Graf 
18337bdf4de1SDonald Sharp 		if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
1834be403ea1SThomas Graf 			goto nla_put_failure;
18351da177e4SLinus Torvalds 
1836b0f60193SDavid Ahern 		rtm->rtm_flags = flags;
1837c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1838dcb1ecb5SDavid Ahern 		if (nhc->nhc_family == AF_INET) {
1839dcb1ecb5SDavid Ahern 			struct fib_nh *nh;
1840dcb1ecb5SDavid Ahern 
1841dcb1ecb5SDavid Ahern 			nh = container_of(nhc, struct fib_nh, nh_common);
1842f3756b79SDavid S. Miller 			if (nh->nh_tclassid &&
1843f3756b79SDavid S. Miller 			    nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1844f3756b79SDavid S. Miller 				goto nla_put_failure;
1845dcb1ecb5SDavid Ahern 		}
18468265abc0SPatrick McHardy #endif
1847b0f60193SDavid Ahern 	} else {
1848b0f60193SDavid Ahern 		if (fib_add_multipath(skb, fi) < 0)
1849ea7a8085SDavid Ahern 			goto nla_put_failure;
18501da177e4SLinus Torvalds 	}
1851b0f60193SDavid Ahern 
18524f80116dSRoopa Prabhu offload:
185390b93f1bSIdo Schimmel 	if (fri->offload)
185490b93f1bSIdo Schimmel 		rtm->rtm_flags |= RTM_F_OFFLOAD;
185590b93f1bSIdo Schimmel 	if (fri->trap)
185690b93f1bSIdo Schimmel 		rtm->rtm_flags |= RTM_F_TRAP;
185736c5100eSAmit Cohen 	if (fri->offload_failed)
185836c5100eSAmit Cohen 		rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
185990b93f1bSIdo Schimmel 
1860053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1861053c095aSJohannes Berg 	return 0;
18621da177e4SLinus Torvalds 
1863be403ea1SThomas Graf nla_put_failure:
186426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
186526932566SPatrick McHardy 	return -EMSGSIZE;
18661da177e4SLinus Torvalds }
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds /*
18696a31d2a9SEric Dumazet  * Update FIB if:
18706a31d2a9SEric Dumazet  * - local address disappeared -> we must delete all the entries
18716a31d2a9SEric Dumazet  *   referring to it.
18726a31d2a9SEric Dumazet  * - device went down -> we must shutdown all nexthops going via it.
18731da177e4SLinus Torvalds  */
fib_sync_down_addr(struct net_device * dev,__be32 local)18745a56a0b3SMark Tomlinson int fib_sync_down_addr(struct net_device *dev, __be32 local)
18751da177e4SLinus Torvalds {
1876e0a31262SDavid Ahern 	int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
18775a56a0b3SMark Tomlinson 	struct net *net = dev_net(dev);
187879eb15daSEric Dumazet 	struct hlist_head *head;
18791da177e4SLinus Torvalds 	struct fib_info *fi;
188079eb15daSEric Dumazet 	int ret = 0;
18811da177e4SLinus Torvalds 
188251456b29SIan Morris 	if (!fib_info_laddrhash || local == 0)
188385326fa5SDenis V. Lunev 		return 0;
188485326fa5SDenis V. Lunev 
188579eb15daSEric Dumazet 	head = fib_info_laddrhash_bucket(net, local);
1886b67bfe0dSSasha Levin 	hlist_for_each_entry(fi, head, fib_lhash) {
18875a56a0b3SMark Tomlinson 		if (!net_eq(fi->fib_net, net) ||
18885a56a0b3SMark Tomlinson 		    fi->fib_tb_id != tb_id)
18894814bdbdSDenis V. Lunev 			continue;
18901da177e4SLinus Torvalds 		if (fi->fib_prefsrc == local) {
18911da177e4SLinus Torvalds 			fi->fib_flags |= RTNH_F_DEAD;
18924b2b6060SHangbin Liu 			fi->pfsrc_removed = true;
18931da177e4SLinus Torvalds 			ret++;
18941da177e4SLinus Torvalds 		}
18951da177e4SLinus Torvalds 	}
189685326fa5SDenis V. Lunev 	return ret;
18971da177e4SLinus Torvalds }
18981da177e4SLinus Torvalds 
call_fib_nh_notifiers(struct fib_nh * nh,enum fib_event_type event_type)1899b75ed8b1SDavid Ahern static int call_fib_nh_notifiers(struct fib_nh *nh,
1900982acb97SIdo Schimmel 				 enum fib_event_type event_type)
1901982acb97SIdo Schimmel {
1902b75ed8b1SDavid Ahern 	bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
1903982acb97SIdo Schimmel 	struct fib_nh_notifier_info info = {
1904b75ed8b1SDavid Ahern 		.fib_nh = nh,
1905982acb97SIdo Schimmel 	};
1906982acb97SIdo Schimmel 
1907982acb97SIdo Schimmel 	switch (event_type) {
1908982acb97SIdo Schimmel 	case FIB_EVENT_NH_ADD:
1909b75ed8b1SDavid Ahern 		if (nh->fib_nh_flags & RTNH_F_DEAD)
1910982acb97SIdo Schimmel 			break;
1911b75ed8b1SDavid Ahern 		if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
1912982acb97SIdo Schimmel 			break;
1913b75ed8b1SDavid Ahern 		return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
1914982acb97SIdo Schimmel 					   &info.info);
1915982acb97SIdo Schimmel 	case FIB_EVENT_NH_DEL:
1916b75ed8b1SDavid Ahern 		if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1917b75ed8b1SDavid Ahern 		    (nh->fib_nh_flags & RTNH_F_DEAD))
1918b75ed8b1SDavid Ahern 			return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
1919982acb97SIdo Schimmel 						   event_type, &info.info);
192079121184SGustavo A. R. Silva 		break;
1921982acb97SIdo Schimmel 	default:
1922982acb97SIdo Schimmel 		break;
1923982acb97SIdo Schimmel 	}
1924982acb97SIdo Schimmel 
1925982acb97SIdo Schimmel 	return NOTIFY_DONE;
1926982acb97SIdo Schimmel }
1927982acb97SIdo Schimmel 
1928af7d6cceSSabrina Dubroca /* Update the PMTU of exceptions when:
1929af7d6cceSSabrina Dubroca  * - the new MTU of the first hop becomes smaller than the PMTU
1930af7d6cceSSabrina Dubroca  * - the old MTU was the same as the PMTU, and it limited discovery of
1931af7d6cceSSabrina Dubroca  *   larger MTUs on the path. With that limit raised, we can now
1932af7d6cceSSabrina Dubroca  *   discover larger MTUs
1933af7d6cceSSabrina Dubroca  * A special case is locked exceptions, for which the PMTU is smaller
1934af7d6cceSSabrina Dubroca  * than the minimal accepted PMTU:
1935af7d6cceSSabrina Dubroca  * - if the new MTU is greater than the PMTU, don't make any change
1936af7d6cceSSabrina Dubroca  * - otherwise, unlock and set PMTU
1937af7d6cceSSabrina Dubroca  */
fib_nhc_update_mtu(struct fib_nh_common * nhc,u32 new,u32 orig)193806c77c3eSDavid Ahern void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
1939af7d6cceSSabrina Dubroca {
1940af7d6cceSSabrina Dubroca 	struct fnhe_hash_bucket *bucket;
1941af7d6cceSSabrina Dubroca 	int i;
1942af7d6cceSSabrina Dubroca 
1943a5995e71SDavid Ahern 	bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
1944af7d6cceSSabrina Dubroca 	if (!bucket)
1945af7d6cceSSabrina Dubroca 		return;
1946af7d6cceSSabrina Dubroca 
1947af7d6cceSSabrina Dubroca 	for (i = 0; i < FNHE_HASH_SIZE; i++) {
1948af7d6cceSSabrina Dubroca 		struct fib_nh_exception *fnhe;
1949af7d6cceSSabrina Dubroca 
1950af7d6cceSSabrina Dubroca 		for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1951af7d6cceSSabrina Dubroca 		     fnhe;
1952af7d6cceSSabrina Dubroca 		     fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1953af7d6cceSSabrina Dubroca 			if (fnhe->fnhe_mtu_locked) {
1954af7d6cceSSabrina Dubroca 				if (new <= fnhe->fnhe_pmtu) {
1955af7d6cceSSabrina Dubroca 					fnhe->fnhe_pmtu = new;
1956af7d6cceSSabrina Dubroca 					fnhe->fnhe_mtu_locked = false;
1957af7d6cceSSabrina Dubroca 				}
1958af7d6cceSSabrina Dubroca 			} else if (new < fnhe->fnhe_pmtu ||
1959af7d6cceSSabrina Dubroca 				   orig == fnhe->fnhe_pmtu) {
1960af7d6cceSSabrina Dubroca 				fnhe->fnhe_pmtu = new;
1961af7d6cceSSabrina Dubroca 			}
1962af7d6cceSSabrina Dubroca 		}
1963af7d6cceSSabrina Dubroca 	}
1964af7d6cceSSabrina Dubroca }
1965af7d6cceSSabrina Dubroca 
fib_sync_mtu(struct net_device * dev,u32 orig_mtu)1966af7d6cceSSabrina Dubroca void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1967af7d6cceSSabrina Dubroca {
1968d07418afSEric Dumazet 	struct hlist_head *head = fib_info_devhash_bucket(dev);
1969af7d6cceSSabrina Dubroca 	struct fib_nh *nh;
1970af7d6cceSSabrina Dubroca 
1971af7d6cceSSabrina Dubroca 	hlist_for_each_entry(nh, head, nh_hash) {
1972b75ed8b1SDavid Ahern 		if (nh->fib_nh_dev == dev)
197306c77c3eSDavid Ahern 			fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
1974af7d6cceSSabrina Dubroca 	}
1975af7d6cceSSabrina Dubroca }
1976af7d6cceSSabrina Dubroca 
19774f823defSJulian Anastasov /* Event              force Flags           Description
19784f823defSJulian Anastasov  * NETDEV_CHANGE      0     LINKDOWN        Carrier OFF, not for scope host
19794f823defSJulian Anastasov  * NETDEV_DOWN        0     LINKDOWN|DEAD   Link down, not for scope host
19804f823defSJulian Anastasov  * NETDEV_DOWN        1     LINKDOWN|DEAD   Last address removed
19814f823defSJulian Anastasov  * NETDEV_UNREGISTER  1     LINKDOWN|DEAD   Device removed
19824c7e8084SDavid Ahern  *
19834c7e8084SDavid Ahern  * only used when fib_nh is built into fib_info
19844f823defSJulian Anastasov  */
fib_sync_down_dev(struct net_device * dev,unsigned long event,bool force)19854f823defSJulian Anastasov int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
198685326fa5SDenis V. Lunev {
1987d07418afSEric Dumazet 	struct hlist_head *head = fib_info_devhash_bucket(dev);
19881da177e4SLinus Torvalds 	struct fib_info *prev_fi = NULL;
1989d07418afSEric Dumazet 	int scope = RT_SCOPE_NOWHERE;
19901da177e4SLinus Torvalds 	struct fib_nh *nh;
1991d07418afSEric Dumazet 	int ret = 0;
19921da177e4SLinus Torvalds 
19934f823defSJulian Anastasov 	if (force)
199485326fa5SDenis V. Lunev 		scope = -1;
199585326fa5SDenis V. Lunev 
1996b67bfe0dSSasha Levin 	hlist_for_each_entry(nh, head, nh_hash) {
19971da177e4SLinus Torvalds 		struct fib_info *fi = nh->nh_parent;
19981da177e4SLinus Torvalds 		int dead;
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 		BUG_ON(!fi->fib_nhs);
2001b75ed8b1SDavid Ahern 		if (nh->fib_nh_dev != dev || fi == prev_fi)
20021da177e4SLinus Torvalds 			continue;
20031da177e4SLinus Torvalds 		prev_fi = fi;
20041da177e4SLinus Torvalds 		dead = 0;
20051da177e4SLinus Torvalds 		change_nexthops(fi) {
2006b75ed8b1SDavid Ahern 			if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
20071da177e4SLinus Torvalds 				dead++;
2008b75ed8b1SDavid Ahern 			else if (nexthop_nh->fib_nh_dev == dev &&
2009b75ed8b1SDavid Ahern 				 nexthop_nh->fib_nh_scope != scope) {
20108a3d0316SAndy Gospodarek 				switch (event) {
20118a3d0316SAndy Gospodarek 				case NETDEV_DOWN:
20128a3d0316SAndy Gospodarek 				case NETDEV_UNREGISTER:
2013b75ed8b1SDavid Ahern 					nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
2014a8eceea8SJoe Perches 					fallthrough;
20158a3d0316SAndy Gospodarek 				case NETDEV_CHANGE:
2016b75ed8b1SDavid Ahern 					nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
20178a3d0316SAndy Gospodarek 					break;
20188a3d0316SAndy Gospodarek 				}
2019982acb97SIdo Schimmel 				call_fib_nh_notifiers(nexthop_nh,
2020982acb97SIdo Schimmel 						      FIB_EVENT_NH_DEL);
20211da177e4SLinus Torvalds 				dead++;
20221da177e4SLinus Torvalds 			}
20231da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
20248a3d0316SAndy Gospodarek 			if (event == NETDEV_UNREGISTER &&
2025b75ed8b1SDavid Ahern 			    nexthop_nh->fib_nh_dev == dev) {
20261da177e4SLinus Torvalds 				dead = fi->fib_nhs;
20271da177e4SLinus Torvalds 				break;
20281da177e4SLinus Torvalds 			}
20291da177e4SLinus Torvalds #endif
20301da177e4SLinus Torvalds 		} endfor_nexthops(fi)
20311da177e4SLinus Torvalds 		if (dead == fi->fib_nhs) {
20328a3d0316SAndy Gospodarek 			switch (event) {
20338a3d0316SAndy Gospodarek 			case NETDEV_DOWN:
20348a3d0316SAndy Gospodarek 			case NETDEV_UNREGISTER:
20351da177e4SLinus Torvalds 				fi->fib_flags |= RTNH_F_DEAD;
2036a8eceea8SJoe Perches 				fallthrough;
20378a3d0316SAndy Gospodarek 			case NETDEV_CHANGE:
20388a3d0316SAndy Gospodarek 				fi->fib_flags |= RTNH_F_LINKDOWN;
20398a3d0316SAndy Gospodarek 				break;
20408a3d0316SAndy Gospodarek 			}
20411da177e4SLinus Torvalds 			ret++;
20421da177e4SLinus Torvalds 		}
20430e884c78SPeter Nørlund 
20440e884c78SPeter Nørlund 		fib_rebalance(fi);
20451da177e4SLinus Torvalds 	}
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds 	return ret;
20481da177e4SLinus Torvalds }
20491da177e4SLinus Torvalds 
20500c838ff1SDavid S. Miller /* Must be invoked inside of an RCU protected region.  */
fib_select_default(const struct flowi4 * flp,struct fib_result * res)2051c7b371e3SDavid Ahern static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
20520c838ff1SDavid S. Miller {
20530c838ff1SDavid S. Miller 	struct fib_info *fi = NULL, *last_resort = NULL;
205456315f9eSAlexander Duyck 	struct hlist_head *fa_head = res->fa_head;
20550c838ff1SDavid S. Miller 	struct fib_table *tb = res->table;
205618a912e9SJulian Anastasov 	u8 slen = 32 - res->prefixlen;
20570c838ff1SDavid S. Miller 	int order = -1, last_idx = -1;
20582392debcSJulian Anastasov 	struct fib_alias *fa, *fa1 = NULL;
20592392debcSJulian Anastasov 	u32 last_prio = res->fi->fib_priority;
206032ccf110SGuillaume Nault 	dscp_t last_dscp = 0;
20610c838ff1SDavid S. Miller 
206256315f9eSAlexander Duyck 	hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
20630c838ff1SDavid S. Miller 		struct fib_info *next_fi = fa->fa_info;
20647c74b0beSDavid Ahern 		struct fib_nh_common *nhc;
20650c838ff1SDavid S. Miller 
206618a912e9SJulian Anastasov 		if (fa->fa_slen != slen)
206718a912e9SJulian Anastasov 			continue;
2068*1fa3314cSIdo Schimmel 		if (fa->fa_dscp && !fib_dscp_masked_match(fa->fa_dscp, flp))
20692392debcSJulian Anastasov 			continue;
207018a912e9SJulian Anastasov 		if (fa->tb_id != tb->tb_id)
207118a912e9SJulian Anastasov 			continue;
20722392debcSJulian Anastasov 		if (next_fi->fib_priority > last_prio &&
207332ccf110SGuillaume Nault 		    fa->fa_dscp == last_dscp) {
207432ccf110SGuillaume Nault 			if (last_dscp)
20752392debcSJulian Anastasov 				continue;
20762392debcSJulian Anastasov 			break;
20772392debcSJulian Anastasov 		}
20782392debcSJulian Anastasov 		if (next_fi->fib_flags & RTNH_F_DEAD)
20792392debcSJulian Anastasov 			continue;
208032ccf110SGuillaume Nault 		last_dscp = fa->fa_dscp;
20812392debcSJulian Anastasov 		last_prio = next_fi->fib_priority;
20822392debcSJulian Anastasov 
208337e826c5SDavid S. Miller 		if (next_fi->fib_scope != res->scope ||
20840c838ff1SDavid S. Miller 		    fa->fa_type != RTN_UNICAST)
20850c838ff1SDavid S. Miller 			continue;
20865481d73fSDavid Ahern 
20877c74b0beSDavid Ahern 		nhc = fib_info_nhc(next_fi, 0);
20887c74b0beSDavid Ahern 		if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
20890c838ff1SDavid S. Miller 			continue;
20900c838ff1SDavid S. Miller 
20910c838ff1SDavid S. Miller 		fib_alias_accessed(fa);
20920c838ff1SDavid S. Miller 
209351456b29SIan Morris 		if (!fi) {
20940c838ff1SDavid S. Miller 			if (next_fi != res->fi)
20950c838ff1SDavid S. Miller 				break;
20962392debcSJulian Anastasov 			fa1 = fa;
20970c838ff1SDavid S. Miller 		} else if (!fib_detect_death(fi, order, &last_resort,
20982392debcSJulian Anastasov 					     &last_idx, fa1->fa_default)) {
20990c838ff1SDavid S. Miller 			fib_result_assign(res, fi);
21002392debcSJulian Anastasov 			fa1->fa_default = order;
21010c838ff1SDavid S. Miller 			goto out;
21020c838ff1SDavid S. Miller 		}
21030c838ff1SDavid S. Miller 		fi = next_fi;
21040c838ff1SDavid S. Miller 		order++;
21050c838ff1SDavid S. Miller 	}
21060c838ff1SDavid S. Miller 
210751456b29SIan Morris 	if (order <= 0 || !fi) {
21082392debcSJulian Anastasov 		if (fa1)
21092392debcSJulian Anastasov 			fa1->fa_default = -1;
21100c838ff1SDavid S. Miller 		goto out;
21110c838ff1SDavid S. Miller 	}
21120c838ff1SDavid S. Miller 
21130c838ff1SDavid S. Miller 	if (!fib_detect_death(fi, order, &last_resort, &last_idx,
21142392debcSJulian Anastasov 			      fa1->fa_default)) {
21150c838ff1SDavid S. Miller 		fib_result_assign(res, fi);
21162392debcSJulian Anastasov 		fa1->fa_default = order;
21170c838ff1SDavid S. Miller 		goto out;
21180c838ff1SDavid S. Miller 	}
21190c838ff1SDavid S. Miller 
21200c838ff1SDavid S. Miller 	if (last_idx >= 0)
21210c838ff1SDavid S. Miller 		fib_result_assign(res, last_resort);
21222392debcSJulian Anastasov 	fa1->fa_default = last_idx;
21230c838ff1SDavid S. Miller out:
212431d40937SEric Dumazet 	return;
21250c838ff1SDavid S. Miller }
21260c838ff1SDavid S. Miller 
21271da177e4SLinus Torvalds /*
21286a31d2a9SEric Dumazet  * Dead device goes up. We wake up dead nexthops.
21296a31d2a9SEric Dumazet  * It takes sense only on multipath routes.
21304c7e8084SDavid Ahern  *
21314c7e8084SDavid Ahern  * only used when fib_nh is built into fib_info
21321da177e4SLinus Torvalds  */
fib_sync_up(struct net_device * dev,unsigned char nh_flags)2133ecc5663cSDavid Ahern int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
21341da177e4SLinus Torvalds {
21351da177e4SLinus Torvalds 	struct fib_info *prev_fi;
21361da177e4SLinus Torvalds 	struct hlist_head *head;
21371da177e4SLinus Torvalds 	struct fib_nh *nh;
21381da177e4SLinus Torvalds 	int ret;
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 	if (!(dev->flags & IFF_UP))
21411da177e4SLinus Torvalds 		return 0;
21421da177e4SLinus Torvalds 
2143c9b3292eSJulian Anastasov 	if (nh_flags & RTNH_F_DEAD) {
2144c9b3292eSJulian Anastasov 		unsigned int flags = dev_get_flags(dev);
2145c9b3292eSJulian Anastasov 
2146c9b3292eSJulian Anastasov 		if (flags & (IFF_RUNNING | IFF_LOWER_UP))
2147c9b3292eSJulian Anastasov 			nh_flags |= RTNH_F_LINKDOWN;
2148c9b3292eSJulian Anastasov 	}
2149c9b3292eSJulian Anastasov 
21501da177e4SLinus Torvalds 	prev_fi = NULL;
2151d07418afSEric Dumazet 	head = fib_info_devhash_bucket(dev);
21521da177e4SLinus Torvalds 	ret = 0;
21531da177e4SLinus Torvalds 
2154b67bfe0dSSasha Levin 	hlist_for_each_entry(nh, head, nh_hash) {
21551da177e4SLinus Torvalds 		struct fib_info *fi = nh->nh_parent;
21561da177e4SLinus Torvalds 		int alive;
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds 		BUG_ON(!fi->fib_nhs);
2159b75ed8b1SDavid Ahern 		if (nh->fib_nh_dev != dev || fi == prev_fi)
21601da177e4SLinus Torvalds 			continue;
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds 		prev_fi = fi;
21631da177e4SLinus Torvalds 		alive = 0;
21641da177e4SLinus Torvalds 		change_nexthops(fi) {
2165b75ed8b1SDavid Ahern 			if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
21661da177e4SLinus Torvalds 				alive++;
21671da177e4SLinus Torvalds 				continue;
21681da177e4SLinus Torvalds 			}
2169b75ed8b1SDavid Ahern 			if (!nexthop_nh->fib_nh_dev ||
2170b75ed8b1SDavid Ahern 			    !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
21711da177e4SLinus Torvalds 				continue;
2172b75ed8b1SDavid Ahern 			if (nexthop_nh->fib_nh_dev != dev ||
217371fceff0SDavid S. Miller 			    !__in_dev_get_rtnl(dev))
21741da177e4SLinus Torvalds 				continue;
21751da177e4SLinus Torvalds 			alive++;
2176b75ed8b1SDavid Ahern 			nexthop_nh->fib_nh_flags &= ~nh_flags;
2177982acb97SIdo Schimmel 			call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
21781da177e4SLinus Torvalds 		} endfor_nexthops(fi)
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds 		if (alive > 0) {
21818a3d0316SAndy Gospodarek 			fi->fib_flags &= ~nh_flags;
21821da177e4SLinus Torvalds 			ret++;
21831da177e4SLinus Torvalds 		}
21840e884c78SPeter Nørlund 
21850e884c78SPeter Nørlund 		fib_rebalance(fi);
21861da177e4SLinus Torvalds 	}
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds 	return ret;
21891da177e4SLinus Torvalds }
21901da177e4SLinus Torvalds 
21918a3d0316SAndy Gospodarek #ifdef CONFIG_IP_ROUTE_MULTIPATH
fib_good_nh(const struct fib_nh * nh)2192a6db4494SDavid Ahern static bool fib_good_nh(const struct fib_nh *nh)
2193a6db4494SDavid Ahern {
2194a6db4494SDavid Ahern 	int state = NUD_REACHABLE;
2195a6db4494SDavid Ahern 
2196b75ed8b1SDavid Ahern 	if (nh->fib_nh_scope == RT_SCOPE_LINK) {
2197a6db4494SDavid Ahern 		struct neighbour *n;
2198a6db4494SDavid Ahern 
219909eed119SEric Dumazet 		rcu_read_lock();
2200a6db4494SDavid Ahern 
22011a38c43dSDavid Ahern 		if (likely(nh->fib_nh_gw_family == AF_INET))
2202b75ed8b1SDavid Ahern 			n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
2203b75ed8b1SDavid Ahern 						   (__force u32)nh->fib_nh_gw4);
22041a38c43dSDavid Ahern 		else if (nh->fib_nh_gw_family == AF_INET6)
22051a38c43dSDavid Ahern 			n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
22061a38c43dSDavid Ahern 							   &nh->fib_nh_gw6);
22071a38c43dSDavid Ahern 		else
22081a38c43dSDavid Ahern 			n = NULL;
2209a6db4494SDavid Ahern 		if (n)
2210b071af52SEric Dumazet 			state = READ_ONCE(n->nud_state);
2211a6db4494SDavid Ahern 
221209eed119SEric Dumazet 		rcu_read_unlock();
2213a6db4494SDavid Ahern 	}
2214a6db4494SDavid Ahern 
2215a6db4494SDavid Ahern 	return !!(state & NUD_VALID);
2216a6db4494SDavid Ahern }
22178a3d0316SAndy Gospodarek 
fib_select_multipath(struct fib_result * res,int hash)22180e884c78SPeter Nørlund void fib_select_multipath(struct fib_result *res, int hash)
22191da177e4SLinus Torvalds {
22201da177e4SLinus Torvalds 	struct fib_info *fi = res->fi;
2221a6db4494SDavid Ahern 	struct net *net = fi->fib_net;
2222a6db4494SDavid Ahern 	bool first = false;
22231da177e4SLinus Torvalds 
22244c7e8084SDavid Ahern 	if (unlikely(res->fi->nh)) {
22254c7e8084SDavid Ahern 		nexthop_path_fib_result(res, hash);
22264c7e8084SDavid Ahern 		return;
22274c7e8084SDavid Ahern 	}
22284c7e8084SDavid Ahern 
2229eba618abSDavid Ahern 	change_nexthops(fi) {
223087507bcbSKuniyuki Iwashima 		if (READ_ONCE(net->ipv4.sysctl_fib_multipath_use_neigh)) {
2231eba618abSDavid Ahern 			if (!fib_good_nh(nexthop_nh))
22320eeb075fSAndy Gospodarek 				continue;
2233a6db4494SDavid Ahern 			if (!first) {
2234a6db4494SDavid Ahern 				res->nh_sel = nhsel;
2235eba618abSDavid Ahern 				res->nhc = &nexthop_nh->nh_common;
2236a6db4494SDavid Ahern 				first = true;
2237a6db4494SDavid Ahern 			}
22386174a30dSXin Long 		}
22396174a30dSXin Long 
2240eba618abSDavid Ahern 		if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
22416174a30dSXin Long 			continue;
22426174a30dSXin Long 
22436174a30dSXin Long 		res->nh_sel = nhsel;
2244eba618abSDavid Ahern 		res->nhc = &nexthop_nh->nh_common;
22456174a30dSXin Long 		return;
22461da177e4SLinus Torvalds 	} endfor_nexthops(fi);
22471da177e4SLinus Torvalds }
22481da177e4SLinus Torvalds #endif
22493ce58d84SDavid Ahern 
fib_select_path(struct net * net,struct fib_result * res,struct flowi4 * fl4,const struct sk_buff * skb)22503ce58d84SDavid Ahern void fib_select_path(struct net *net, struct fib_result *res,
2251bf4e0a3dSNikolay Aleksandrov 		     struct flowi4 *fl4, const struct sk_buff *skb)
22523ce58d84SDavid Ahern {
225340867d74SDavid Ahern 	if (fl4->flowi4_oif)
22540d876f2cSDavid Ahern 		goto check_saddr;
22557a18c5b9SDavid Ahern 
22563ce58d84SDavid Ahern #ifdef CONFIG_IP_ROUTE_MULTIPATH
22575481d73fSDavid Ahern 	if (fib_info_num_path(res->fi) > 1) {
22587efc0b6bSDavid Ahern 		int h = fib_multipath_hash(net, fl4, skb, NULL);
22599920e48bSPaolo Abeni 
2260bf4e0a3dSNikolay Aleksandrov 		fib_select_multipath(res, h);
22613ce58d84SDavid Ahern 	}
22623ce58d84SDavid Ahern 	else
22633ce58d84SDavid Ahern #endif
22643ce58d84SDavid Ahern 	if (!res->prefixlen &&
22653ce58d84SDavid Ahern 	    res->table->tb_num_default > 1 &&
22660d876f2cSDavid Ahern 	    res->type == RTN_UNICAST)
22673ce58d84SDavid Ahern 		fib_select_default(fl4, res);
22683ce58d84SDavid Ahern 
22690d876f2cSDavid Ahern check_saddr:
227068073523SNicolas Dichtel 	if (!fl4->saddr) {
227168073523SNicolas Dichtel 		struct net_device *l3mdev;
227268073523SNicolas Dichtel 
227368073523SNicolas Dichtel 		l3mdev = dev_get_by_index_rcu(net, fl4->flowi4_l3mdev);
227468073523SNicolas Dichtel 
227568073523SNicolas Dichtel 		if (!l3mdev ||
227668073523SNicolas Dichtel 		    l3mdev_master_dev_rcu(FIB_RES_DEV(*res)) == l3mdev)
2277eba618abSDavid Ahern 			fl4->saddr = fib_result_prefsrc(net, res);
227868073523SNicolas Dichtel 		else
227968073523SNicolas Dichtel 			fl4->saddr = inet_select_addr(l3mdev, 0, RT_SCOPE_LINK);
228068073523SNicolas Dichtel 	}
22813ce58d84SDavid Ahern }
2282