xref: /linux/net/ipv4/fib_semantics.c (revision 61b91eb33a69c3be11b259c5ea484505cd79f883)
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>
331da177e4SLinus Torvalds 
3414c85021SArnaldo Carvalho de Melo #include <net/arp.h>
3532ccf110SGuillaume Nault #include <net/inet_dscp.h>
361da177e4SLinus Torvalds #include <net/ip.h>
371da177e4SLinus Torvalds #include <net/protocol.h>
381da177e4SLinus Torvalds #include <net/route.h>
391da177e4SLinus Torvalds #include <net/tcp.h>
401da177e4SLinus Torvalds #include <net/sock.h>
411da177e4SLinus Torvalds #include <net/ip_fib.h>
42717a8f5bSDavid Ahern #include <net/ip6_fib.h>
435481d73fSDavid Ahern #include <net/nexthop.h>
44f21c7bc5SThomas Graf #include <net/netlink.h>
453c618c1dSDavid Ahern #include <net/rtnh.h>
46571e7226SRoopa Prabhu #include <net/lwtunnel.h>
4704b1d4e5SIdo Schimmel #include <net/fib_notifier.h>
48c0a72077SDavid Ahern #include <net/addrconf.h>
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include "fib_lookup.h"
511da177e4SLinus Torvalds 
52832b4c5eSStephen Hemminger static DEFINE_SPINLOCK(fib_info_lock);
531da177e4SLinus Torvalds static struct hlist_head *fib_info_hash;
541da177e4SLinus Torvalds static struct hlist_head *fib_info_laddrhash;
55123b9731SDavid S. Miller static unsigned int fib_info_hash_size;
5679eb15daSEric Dumazet static unsigned int fib_info_hash_bits;
571da177e4SLinus Torvalds static unsigned int fib_info_cnt;
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #define DEVINDEX_HASHBITS 8
601da177e4SLinus Torvalds #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
611da177e4SLinus Torvalds static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
621da177e4SLinus Torvalds 
63dcb1ecb5SDavid Ahern /* for_nexthops and change_nexthops only used when nexthop object
64dcb1ecb5SDavid Ahern  * is not set in a fib_info. The logic within can reference fib_nh.
65dcb1ecb5SDavid Ahern  */
661da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
671da177e4SLinus Torvalds 
686a31d2a9SEric Dumazet #define for_nexthops(fi) {						\
696a31d2a9SEric Dumazet 	int nhsel; const struct fib_nh *nh;				\
706a31d2a9SEric Dumazet 	for (nhsel = 0, nh = (fi)->fib_nh;				\
715481d73fSDavid Ahern 	     nhsel < fib_info_num_path((fi));				\
726a31d2a9SEric Dumazet 	     nh++, nhsel++)
731da177e4SLinus Torvalds 
746a31d2a9SEric Dumazet #define change_nexthops(fi) {						\
756a31d2a9SEric Dumazet 	int nhsel; struct fib_nh *nexthop_nh;				\
766a31d2a9SEric Dumazet 	for (nhsel = 0,	nexthop_nh = (struct fib_nh *)((fi)->fib_nh);	\
775481d73fSDavid Ahern 	     nhsel < fib_info_num_path((fi));				\
786a31d2a9SEric Dumazet 	     nexthop_nh++, nhsel++)
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds #else /* CONFIG_IP_ROUTE_MULTIPATH */
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds /* Hope, that gcc will optimize it to get rid of dummy loop */
831da177e4SLinus Torvalds 
846a31d2a9SEric Dumazet #define for_nexthops(fi) {						\
856a31d2a9SEric Dumazet 	int nhsel; const struct fib_nh *nh = (fi)->fib_nh;		\
861da177e4SLinus Torvalds 	for (nhsel = 0; nhsel < 1; nhsel++)
871da177e4SLinus Torvalds 
886a31d2a9SEric Dumazet #define change_nexthops(fi) {						\
896a31d2a9SEric Dumazet 	int nhsel;							\
906a31d2a9SEric Dumazet 	struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh);	\
911da177e4SLinus Torvalds 	for (nhsel = 0; nhsel < 1; nhsel++)
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #endif /* CONFIG_IP_ROUTE_MULTIPATH */
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds #define endfor_nexthops(fi) }
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 
983be0686bSDavid S. Miller const struct fib_prop fib_props[RTN_MAX + 1] = {
996a31d2a9SEric Dumazet 	[RTN_UNSPEC] = {
1001da177e4SLinus Torvalds 		.error	= 0,
1011da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1026a31d2a9SEric Dumazet 	},
1036a31d2a9SEric Dumazet 	[RTN_UNICAST] = {
1041da177e4SLinus Torvalds 		.error	= 0,
1051da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1066a31d2a9SEric Dumazet 	},
1076a31d2a9SEric Dumazet 	[RTN_LOCAL] = {
1081da177e4SLinus Torvalds 		.error	= 0,
1091da177e4SLinus Torvalds 		.scope	= RT_SCOPE_HOST,
1106a31d2a9SEric Dumazet 	},
1116a31d2a9SEric Dumazet 	[RTN_BROADCAST] = {
1121da177e4SLinus Torvalds 		.error	= 0,
1131da177e4SLinus Torvalds 		.scope	= RT_SCOPE_LINK,
1146a31d2a9SEric Dumazet 	},
1156a31d2a9SEric Dumazet 	[RTN_ANYCAST] = {
1161da177e4SLinus Torvalds 		.error	= 0,
1171da177e4SLinus Torvalds 		.scope	= RT_SCOPE_LINK,
1186a31d2a9SEric Dumazet 	},
1196a31d2a9SEric Dumazet 	[RTN_MULTICAST] = {
1201da177e4SLinus Torvalds 		.error	= 0,
1211da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1226a31d2a9SEric Dumazet 	},
1236a31d2a9SEric Dumazet 	[RTN_BLACKHOLE] = {
1241da177e4SLinus Torvalds 		.error	= -EINVAL,
1251da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1266a31d2a9SEric Dumazet 	},
1276a31d2a9SEric Dumazet 	[RTN_UNREACHABLE] = {
1281da177e4SLinus Torvalds 		.error	= -EHOSTUNREACH,
1291da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1306a31d2a9SEric Dumazet 	},
1316a31d2a9SEric Dumazet 	[RTN_PROHIBIT] = {
1321da177e4SLinus Torvalds 		.error	= -EACCES,
1331da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1346a31d2a9SEric Dumazet 	},
1356a31d2a9SEric Dumazet 	[RTN_THROW] = {
1361da177e4SLinus Torvalds 		.error	= -EAGAIN,
1371da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1386a31d2a9SEric Dumazet 	},
1396a31d2a9SEric Dumazet 	[RTN_NAT] = {
1401da177e4SLinus Torvalds 		.error	= -EINVAL,
1411da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1426a31d2a9SEric Dumazet 	},
1436a31d2a9SEric Dumazet 	[RTN_XRESOLVE] = {
1441da177e4SLinus Torvalds 		.error	= -EINVAL,
1451da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1466a31d2a9SEric Dumazet 	},
1471da177e4SLinus Torvalds };
1481da177e4SLinus Torvalds 
149c5038a83SDavid S. Miller static void rt_fibinfo_free(struct rtable __rcu **rtp)
15054764bb6SEric Dumazet {
15154764bb6SEric Dumazet 	struct rtable *rt = rcu_dereference_protected(*rtp, 1);
15254764bb6SEric Dumazet 
15354764bb6SEric Dumazet 	if (!rt)
15454764bb6SEric Dumazet 		return;
15554764bb6SEric Dumazet 
15654764bb6SEric Dumazet 	/* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
15754764bb6SEric Dumazet 	 * because we waited an RCU grace period before calling
15854764bb6SEric Dumazet 	 * free_fib_info_rcu()
15954764bb6SEric Dumazet 	 */
16054764bb6SEric Dumazet 
16195c47f9cSWei Wang 	dst_dev_put(&rt->dst);
162b838d5e1SWei Wang 	dst_release_immediate(&rt->dst);
16354764bb6SEric Dumazet }
16454764bb6SEric Dumazet 
165a5995e71SDavid Ahern static void free_nh_exceptions(struct fib_nh_common *nhc)
166c5038a83SDavid S. Miller {
167caa41527SEric Dumazet 	struct fnhe_hash_bucket *hash;
168c5038a83SDavid S. Miller 	int i;
169c5038a83SDavid S. Miller 
170a5995e71SDavid Ahern 	hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
171caa41527SEric Dumazet 	if (!hash)
172caa41527SEric Dumazet 		return;
173c5038a83SDavid S. Miller 	for (i = 0; i < FNHE_HASH_SIZE; i++) {
174c5038a83SDavid S. Miller 		struct fib_nh_exception *fnhe;
175c5038a83SDavid S. Miller 
176c5038a83SDavid S. Miller 		fnhe = rcu_dereference_protected(hash[i].chain, 1);
177c5038a83SDavid S. Miller 		while (fnhe) {
178c5038a83SDavid S. Miller 			struct fib_nh_exception *next;
179c5038a83SDavid S. Miller 
180c5038a83SDavid S. Miller 			next = rcu_dereference_protected(fnhe->fnhe_next, 1);
181c5038a83SDavid S. Miller 
1822ffae99dSTimo Teräs 			rt_fibinfo_free(&fnhe->fnhe_rth_input);
1832ffae99dSTimo Teräs 			rt_fibinfo_free(&fnhe->fnhe_rth_output);
184c5038a83SDavid S. Miller 
185c5038a83SDavid S. Miller 			kfree(fnhe);
186c5038a83SDavid S. Miller 
187c5038a83SDavid S. Miller 			fnhe = next;
188c5038a83SDavid S. Miller 		}
189c5038a83SDavid S. Miller 	}
190c5038a83SDavid S. Miller 	kfree(hash);
191c5038a83SDavid S. Miller }
192c5038a83SDavid S. Miller 
193c5038a83SDavid S. Miller static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
194d26b3a7cSEric Dumazet {
195d26b3a7cSEric Dumazet 	int cpu;
196d26b3a7cSEric Dumazet 
197d26b3a7cSEric Dumazet 	if (!rtp)
198d26b3a7cSEric Dumazet 		return;
199d26b3a7cSEric Dumazet 
200d26b3a7cSEric Dumazet 	for_each_possible_cpu(cpu) {
201d26b3a7cSEric Dumazet 		struct rtable *rt;
202d26b3a7cSEric Dumazet 
203d26b3a7cSEric Dumazet 		rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
2040830106cSWei Wang 		if (rt) {
20595c47f9cSWei Wang 			dst_dev_put(&rt->dst);
206b838d5e1SWei Wang 			dst_release_immediate(&rt->dst);
207d26b3a7cSEric Dumazet 		}
2080830106cSWei Wang 	}
209d26b3a7cSEric Dumazet 	free_percpu(rtp);
210d26b3a7cSEric Dumazet }
211d26b3a7cSEric Dumazet 
212979e276eSDavid Ahern void fib_nh_common_release(struct fib_nh_common *nhc)
213979e276eSDavid Ahern {
214d62607c3SJakub Kicinski 	netdev_put(nhc->nhc_dev, &nhc->nhc_dev_tracker);
215979e276eSDavid Ahern 	lwtstate_put(nhc->nhc_lwtstate);
2160f457a36SDavid Ahern 	rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
2170f457a36SDavid Ahern 	rt_fibinfo_free(&nhc->nhc_rth_input);
218a5995e71SDavid Ahern 	free_nh_exceptions(nhc);
219979e276eSDavid Ahern }
220979e276eSDavid Ahern EXPORT_SYMBOL_GPL(fib_nh_common_release);
221979e276eSDavid Ahern 
222faa041a4SDavid Ahern void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
223faa041a4SDavid Ahern {
224faa041a4SDavid Ahern #ifdef CONFIG_IP_ROUTE_CLASSID
225faa041a4SDavid Ahern 	if (fib_nh->nh_tclassid)
226213f5f8fSEric Dumazet 		atomic_dec(&net->ipv4.fib_num_tclassid_users);
227faa041a4SDavid Ahern #endif
228979e276eSDavid Ahern 	fib_nh_common_release(&fib_nh->nh_common);
229faa041a4SDavid Ahern }
230faa041a4SDavid Ahern 
2311da177e4SLinus Torvalds /* Release a nexthop info record */
23219c1ea14SYan, Zheng static void free_fib_info_rcu(struct rcu_head *head)
23319c1ea14SYan, Zheng {
23419c1ea14SYan, Zheng 	struct fib_info *fi = container_of(head, struct fib_info, rcu);
23519c1ea14SYan, Zheng 
2364c7e8084SDavid Ahern 	if (fi->nh) {
2374c7e8084SDavid Ahern 		nexthop_put(fi->nh);
2384c7e8084SDavid Ahern 	} else {
239e49cc0daSYanmin Zhang 		change_nexthops(fi) {
240faa041a4SDavid Ahern 			fib_nh_release(fi->fib_net, nexthop_nh);
241e49cc0daSYanmin Zhang 		} endfor_nexthops(fi);
2424c7e8084SDavid Ahern 	}
243e49cc0daSYanmin Zhang 
244cc5f0eb2SDavid Ahern 	ip_fib_metrics_put(fi->fib_metrics);
245cc5f0eb2SDavid Ahern 
24619c1ea14SYan, Zheng 	kfree(fi);
24719c1ea14SYan, Zheng }
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds void free_fib_info(struct fib_info *fi)
2501da177e4SLinus Torvalds {
2511da177e4SLinus Torvalds 	if (fi->fib_dead == 0) {
252058bd4d2SJoe Perches 		pr_warn("Freeing alive fib_info %p\n", fi);
2531da177e4SLinus Torvalds 		return;
2541da177e4SLinus Torvalds 	}
255faa041a4SDavid Ahern 
25619c1ea14SYan, Zheng 	call_rcu(&fi->rcu, free_fib_info_rcu);
2571da177e4SLinus Torvalds }
258b423cb10SIdo Schimmel EXPORT_SYMBOL_GPL(free_fib_info);
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds void fib_release_info(struct fib_info *fi)
2611da177e4SLinus Torvalds {
262832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
26379976892SYajun Deng 	if (fi && refcount_dec_and_test(&fi->fib_treeref)) {
2641da177e4SLinus Torvalds 		hlist_del(&fi->fib_hash);
2650a6e6b3cSEric Dumazet 
2660a6e6b3cSEric Dumazet 		/* Paired with READ_ONCE() in fib_create_info(). */
2670a6e6b3cSEric Dumazet 		WRITE_ONCE(fib_info_cnt, fib_info_cnt - 1);
2680a6e6b3cSEric Dumazet 
2691da177e4SLinus Torvalds 		if (fi->fib_prefsrc)
2701da177e4SLinus Torvalds 			hlist_del(&fi->fib_lhash);
2714c7e8084SDavid Ahern 		if (fi->nh) {
2724c7e8084SDavid Ahern 			list_del(&fi->nh_list);
2734c7e8084SDavid Ahern 		} else {
2741da177e4SLinus Torvalds 			change_nexthops(fi) {
275b75ed8b1SDavid Ahern 				if (!nexthop_nh->fib_nh_dev)
2761da177e4SLinus Torvalds 					continue;
27771fceff0SDavid S. Miller 				hlist_del(&nexthop_nh->nh_hash);
2781da177e4SLinus Torvalds 			} endfor_nexthops(fi)
2794c7e8084SDavid Ahern 		}
2801da177e4SLinus Torvalds 		fi->fib_dead = 1;
2811da177e4SLinus Torvalds 		fib_info_put(fi);
2821da177e4SLinus Torvalds 	}
283832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2865481d73fSDavid Ahern static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
2871da177e4SLinus Torvalds {
2885481d73fSDavid Ahern 	const struct fib_nh *onh;
2891da177e4SLinus Torvalds 
2904c7e8084SDavid Ahern 	if (fi->nh || ofi->nh)
2914c7e8084SDavid Ahern 		return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1;
2924c7e8084SDavid Ahern 
2934c7e8084SDavid Ahern 	if (ofi->fib_nhs == 0)
2944c7e8084SDavid Ahern 		return 0;
2954c7e8084SDavid Ahern 
2961da177e4SLinus Torvalds 	for_nexthops(fi) {
2975481d73fSDavid Ahern 		onh = fib_info_nh(ofi, nhsel);
2985481d73fSDavid Ahern 
299b75ed8b1SDavid Ahern 		if (nh->fib_nh_oif != onh->fib_nh_oif ||
300a4ea5d43SDavid Ahern 		    nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
301b75ed8b1SDavid Ahern 		    nh->fib_nh_scope != onh->fib_nh_scope ||
3021da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
303b75ed8b1SDavid Ahern 		    nh->fib_nh_weight != onh->fib_nh_weight ||
3041da177e4SLinus Torvalds #endif
305c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
3061da177e4SLinus Torvalds 		    nh->nh_tclassid != onh->nh_tclassid ||
3071da177e4SLinus Torvalds #endif
308b75ed8b1SDavid Ahern 		    lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
309b75ed8b1SDavid Ahern 		    ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
3101da177e4SLinus Torvalds 			return -1;
311a4ea5d43SDavid Ahern 
312a4ea5d43SDavid Ahern 		if (nh->fib_nh_gw_family == AF_INET &&
313a4ea5d43SDavid Ahern 		    nh->fib_nh_gw4 != onh->fib_nh_gw4)
314a4ea5d43SDavid Ahern 			return -1;
315a4ea5d43SDavid Ahern 
316a4ea5d43SDavid Ahern 		if (nh->fib_nh_gw_family == AF_INET6 &&
317a4ea5d43SDavid Ahern 		    ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
318a4ea5d43SDavid Ahern 			return -1;
3191da177e4SLinus Torvalds 	} endfor_nexthops(fi);
3201da177e4SLinus Torvalds 	return 0;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
32388ebc72fSDavid S. Miller static inline unsigned int fib_devindex_hashfn(unsigned int val)
32488ebc72fSDavid S. Miller {
325d07418afSEric Dumazet 	return hash_32(val, DEVINDEX_HASHBITS);
326d07418afSEric Dumazet }
32788ebc72fSDavid S. Miller 
328d07418afSEric Dumazet static struct hlist_head *
329d07418afSEric Dumazet fib_info_devhash_bucket(const struct net_device *dev)
330d07418afSEric Dumazet {
331d07418afSEric Dumazet 	u32 val = net_hash_mix(dev_net(dev)) ^ dev->ifindex;
332d07418afSEric Dumazet 
333d07418afSEric Dumazet 	return &fib_info_devhash[fib_devindex_hashfn(val)];
33488ebc72fSDavid S. Miller }
33588ebc72fSDavid S. Miller 
3366c48ea5fSDavid Ahern static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope,
3376c48ea5fSDavid Ahern 				      u32 prefsrc, u32 priority)
3386c48ea5fSDavid Ahern {
3396c48ea5fSDavid Ahern 	unsigned int val = init_val;
3406c48ea5fSDavid Ahern 
3416c48ea5fSDavid Ahern 	val ^= (protocol << 8) | scope;
3426c48ea5fSDavid Ahern 	val ^= prefsrc;
3436c48ea5fSDavid Ahern 	val ^= priority;
3446c48ea5fSDavid Ahern 
3456c48ea5fSDavid Ahern 	return val;
3466c48ea5fSDavid Ahern }
3476c48ea5fSDavid Ahern 
3486c48ea5fSDavid Ahern static unsigned int fib_info_hashfn_result(unsigned int val)
3491da177e4SLinus Torvalds {
350123b9731SDavid S. Miller 	unsigned int mask = (fib_info_hash_size - 1);
3511da177e4SLinus Torvalds 
3526c48ea5fSDavid Ahern 	return (val ^ (val >> 7) ^ (val >> 12)) & mask;
3536c48ea5fSDavid Ahern }
3546c48ea5fSDavid Ahern 
3556c48ea5fSDavid Ahern static inline unsigned int fib_info_hashfn(struct fib_info *fi)
3566c48ea5fSDavid Ahern {
3576c48ea5fSDavid Ahern 	unsigned int val;
3586c48ea5fSDavid Ahern 
3596c48ea5fSDavid Ahern 	val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol,
3606c48ea5fSDavid Ahern 				fi->fib_scope, (__force u32)fi->fib_prefsrc,
3616c48ea5fSDavid Ahern 				fi->fib_priority);
3624c7e8084SDavid Ahern 
3634c7e8084SDavid Ahern 	if (fi->nh) {
3644c7e8084SDavid Ahern 		val ^= fib_devindex_hashfn(fi->nh->id);
3654c7e8084SDavid Ahern 	} else {
36688ebc72fSDavid S. Miller 		for_nexthops(fi) {
367b75ed8b1SDavid Ahern 			val ^= fib_devindex_hashfn(nh->fib_nh_oif);
36888ebc72fSDavid S. Miller 		} endfor_nexthops(fi)
3694c7e8084SDavid Ahern 	}
3701da177e4SLinus Torvalds 
3716c48ea5fSDavid Ahern 	return fib_info_hashfn_result(val);
3726c48ea5fSDavid Ahern }
3736c48ea5fSDavid Ahern 
3746c48ea5fSDavid Ahern /* no metrics, only nexthop id */
3756c48ea5fSDavid Ahern static struct fib_info *fib_find_info_nh(struct net *net,
3766c48ea5fSDavid Ahern 					 const struct fib_config *cfg)
3776c48ea5fSDavid Ahern {
3786c48ea5fSDavid Ahern 	struct hlist_head *head;
3796c48ea5fSDavid Ahern 	struct fib_info *fi;
3806c48ea5fSDavid Ahern 	unsigned int hash;
3816c48ea5fSDavid Ahern 
3826c48ea5fSDavid Ahern 	hash = fib_info_hashfn_1(fib_devindex_hashfn(cfg->fc_nh_id),
3836c48ea5fSDavid Ahern 				 cfg->fc_protocol, cfg->fc_scope,
3846c48ea5fSDavid Ahern 				 (__force u32)cfg->fc_prefsrc,
3856c48ea5fSDavid Ahern 				 cfg->fc_priority);
3866c48ea5fSDavid Ahern 	hash = fib_info_hashfn_result(hash);
3876c48ea5fSDavid Ahern 	head = &fib_info_hash[hash];
3886c48ea5fSDavid Ahern 
3896c48ea5fSDavid Ahern 	hlist_for_each_entry(fi, head, fib_hash) {
3906c48ea5fSDavid Ahern 		if (!net_eq(fi->fib_net, net))
3916c48ea5fSDavid Ahern 			continue;
3926c48ea5fSDavid Ahern 		if (!fi->nh || fi->nh->id != cfg->fc_nh_id)
3936c48ea5fSDavid Ahern 			continue;
3946c48ea5fSDavid Ahern 		if (cfg->fc_protocol == fi->fib_protocol &&
3956c48ea5fSDavid Ahern 		    cfg->fc_scope == fi->fib_scope &&
3966c48ea5fSDavid Ahern 		    cfg->fc_prefsrc == fi->fib_prefsrc &&
3976c48ea5fSDavid Ahern 		    cfg->fc_priority == fi->fib_priority &&
3986c48ea5fSDavid Ahern 		    cfg->fc_type == fi->fib_type &&
3996c48ea5fSDavid Ahern 		    cfg->fc_table == fi->fib_tb_id &&
4006c48ea5fSDavid Ahern 		    !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK))
4016c48ea5fSDavid Ahern 			return fi;
4026c48ea5fSDavid Ahern 	}
4036c48ea5fSDavid Ahern 
4046c48ea5fSDavid Ahern 	return NULL;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
4075481d73fSDavid Ahern static struct fib_info *fib_find_info(struct fib_info *nfi)
4081da177e4SLinus Torvalds {
4091da177e4SLinus Torvalds 	struct hlist_head *head;
4101da177e4SLinus Torvalds 	struct fib_info *fi;
4111da177e4SLinus Torvalds 	unsigned int hash;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	hash = fib_info_hashfn(nfi);
4141da177e4SLinus Torvalds 	head = &fib_info_hash[hash];
4151da177e4SLinus Torvalds 
416b67bfe0dSSasha Levin 	hlist_for_each_entry(fi, head, fib_hash) {
41709ad9bc7SOctavian Purdila 		if (!net_eq(fi->fib_net, nfi->fib_net))
4184814bdbdSDenis V. Lunev 			continue;
4191da177e4SLinus Torvalds 		if (fi->fib_nhs != nfi->fib_nhs)
4201da177e4SLinus Torvalds 			continue;
4211da177e4SLinus Torvalds 		if (nfi->fib_protocol == fi->fib_protocol &&
42237e826c5SDavid S. Miller 		    nfi->fib_scope == fi->fib_scope &&
4231da177e4SLinus Torvalds 		    nfi->fib_prefsrc == fi->fib_prefsrc &&
4241da177e4SLinus Torvalds 		    nfi->fib_priority == fi->fib_priority &&
425f4ef85bbSEric Dumazet 		    nfi->fib_type == fi->fib_type &&
4261da177e4SLinus Torvalds 		    memcmp(nfi->fib_metrics, fi->fib_metrics,
427fcd13f42SEric Dumazet 			   sizeof(u32) * RTAX_MAX) == 0 &&
4288a3d0316SAndy Gospodarek 		    !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
4294c7e8084SDavid Ahern 		    nh_comp(fi, nfi) == 0)
4301da177e4SLinus Torvalds 			return fi;
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	return NULL;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds /* Check, that the gateway is already configured.
4376a31d2a9SEric Dumazet  * Used only by redirect accept routine.
4381da177e4SLinus Torvalds  */
439d878e72eSAl Viro int ip_fib_check_default(__be32 gw, struct net_device *dev)
4401da177e4SLinus Torvalds {
4411da177e4SLinus Torvalds 	struct hlist_head *head;
4421da177e4SLinus Torvalds 	struct fib_nh *nh;
4431da177e4SLinus Torvalds 
444832b4c5eSStephen Hemminger 	spin_lock(&fib_info_lock);
4451da177e4SLinus Torvalds 
446d07418afSEric Dumazet 	head = fib_info_devhash_bucket(dev);
447d07418afSEric Dumazet 
448b67bfe0dSSasha Levin 	hlist_for_each_entry(nh, head, nh_hash) {
449b75ed8b1SDavid Ahern 		if (nh->fib_nh_dev == dev &&
450b75ed8b1SDavid Ahern 		    nh->fib_nh_gw4 == gw &&
451b75ed8b1SDavid Ahern 		    !(nh->fib_nh_flags & RTNH_F_DEAD)) {
452832b4c5eSStephen Hemminger 			spin_unlock(&fib_info_lock);
4531da177e4SLinus Torvalds 			return 0;
4541da177e4SLinus Torvalds 		}
4551da177e4SLinus Torvalds 	}
4561da177e4SLinus Torvalds 
457832b4c5eSStephen Hemminger 	spin_unlock(&fib_info_lock);
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	return -1;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621e7bdec6SAmit Cohen size_t fib_nlmsg_size(struct fib_info *fi)
463339bf98fSThomas Graf {
464339bf98fSThomas Graf 	size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
465339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_TABLE */
466339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_DST */
467339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_PRIORITY */
468ea697639SDaniel Borkmann 			 + nla_total_size(4) /* RTA_PREFSRC */
469ea697639SDaniel Borkmann 			 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
4705481d73fSDavid Ahern 	unsigned int nhs = fib_info_num_path(fi);
471339bf98fSThomas Graf 
472339bf98fSThomas Graf 	/* space for nested metrics */
473339bf98fSThomas Graf 	payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
474339bf98fSThomas Graf 
4754c7e8084SDavid Ahern 	if (fi->nh)
4764c7e8084SDavid Ahern 		payload += nla_total_size(4); /* RTA_NH_ID */
4774c7e8084SDavid Ahern 
4785481d73fSDavid Ahern 	if (nhs) {
479571e7226SRoopa Prabhu 		size_t nh_encapsize = 0;
4805481d73fSDavid Ahern 		/* Also handles the special case nhs == 1 */
481339bf98fSThomas Graf 
482339bf98fSThomas Graf 		/* each nexthop is packed in an attribute */
483339bf98fSThomas Graf 		size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
484dcb1ecb5SDavid Ahern 		unsigned int i;
485339bf98fSThomas Graf 
486339bf98fSThomas Graf 		/* may contain flow and gateway attribute */
487339bf98fSThomas Graf 		nhsize += 2 * nla_total_size(4);
488339bf98fSThomas Graf 
489571e7226SRoopa Prabhu 		/* grab encap info */
490dcb1ecb5SDavid Ahern 		for (i = 0; i < fib_info_num_path(fi); i++) {
491dcb1ecb5SDavid Ahern 			struct fib_nh_common *nhc = fib_info_nhc(fi, i);
492dcb1ecb5SDavid Ahern 
493dcb1ecb5SDavid Ahern 			if (nhc->nhc_lwtstate) {
494571e7226SRoopa Prabhu 				/* RTA_ENCAP_TYPE */
495571e7226SRoopa Prabhu 				nh_encapsize += lwtunnel_get_encap_size(
496dcb1ecb5SDavid Ahern 						nhc->nhc_lwtstate);
497571e7226SRoopa Prabhu 				/* RTA_ENCAP */
498571e7226SRoopa Prabhu 				nh_encapsize +=  nla_total_size(2);
499571e7226SRoopa Prabhu 			}
500dcb1ecb5SDavid Ahern 		}
501571e7226SRoopa Prabhu 
502339bf98fSThomas Graf 		/* all nexthops are packed in a nested attribute */
5035481d73fSDavid Ahern 		payload += nla_total_size((nhs * nhsize) + nh_encapsize);
504571e7226SRoopa Prabhu 
505339bf98fSThomas Graf 	}
506339bf98fSThomas Graf 
507339bf98fSThomas Graf 	return payload;
508339bf98fSThomas Graf }
509339bf98fSThomas Graf 
51081f7bf6cSAl Viro void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
5119877b253SJoe Perches 	       int dst_len, u32 tb_id, const struct nl_info *info,
512b8f55831SMilan Kocian 	       unsigned int nlm_flags)
5131da177e4SLinus Torvalds {
5141e301fd0SIdo Schimmel 	struct fib_rt_info fri;
5151da177e4SLinus Torvalds 	struct sk_buff *skb;
5164e902c57SThomas Graf 	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
517f21c7bc5SThomas Graf 	int err = -ENOBUFS;
5181da177e4SLinus Torvalds 
519339bf98fSThomas Graf 	skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
52051456b29SIan Morris 	if (!skb)
521f21c7bc5SThomas Graf 		goto errout;
5221da177e4SLinus Torvalds 
5231e301fd0SIdo Schimmel 	fri.fi = fa->fa_info;
5241e301fd0SIdo Schimmel 	fri.tb_id = tb_id;
5251e301fd0SIdo Schimmel 	fri.dst = key;
5261e301fd0SIdo Schimmel 	fri.dst_len = dst_len;
527888ade8fSGuillaume Nault 	fri.dscp = fa->fa_dscp;
5281e301fd0SIdo Schimmel 	fri.type = fa->fa_type;
5299fcf986cSEric Dumazet 	fri.offload = READ_ONCE(fa->offload);
5309fcf986cSEric Dumazet 	fri.trap = READ_ONCE(fa->trap);
5319fcf986cSEric Dumazet 	fri.offload_failed = READ_ONCE(fa->offload_failed);
5321e301fd0SIdo Schimmel 	err = fib_dump_info(skb, info->portid, seq, event, &fri, nlm_flags);
53326932566SPatrick McHardy 	if (err < 0) {
53426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in fib_nlmsg_size() */
53526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
53626932566SPatrick McHardy 		kfree_skb(skb);
53726932566SPatrick McHardy 		goto errout;
53826932566SPatrick McHardy 	}
53915e47304SEric W. Biederman 	rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
5404e902c57SThomas Graf 		    info->nlh, GFP_KERNEL);
5411ce85fe4SPablo Neira Ayuso 	return;
542f21c7bc5SThomas Graf errout:
543f21c7bc5SThomas Graf 	if (err < 0)
5444d1169c1SDenis V. Lunev 		rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
547c9cb6b6eSStephen Hemminger static int fib_detect_death(struct fib_info *fi, int order,
548c9cb6b6eSStephen Hemminger 			    struct fib_info **last_resort, int *last_idx,
549c9cb6b6eSStephen Hemminger 			    int dflt)
5501da177e4SLinus Torvalds {
551619d1826SDavid Ahern 	const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
5521da177e4SLinus Torvalds 	struct neighbour *n;
5531da177e4SLinus Torvalds 	int state = NUD_NONE;
5541da177e4SLinus Torvalds 
555619d1826SDavid Ahern 	if (likely(nhc->nhc_gw_family == AF_INET))
556619d1826SDavid Ahern 		n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
557619d1826SDavid Ahern 	else if (nhc->nhc_gw_family == AF_INET6)
558619d1826SDavid Ahern 		n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
559619d1826SDavid Ahern 				 nhc->nhc_dev);
560619d1826SDavid Ahern 	else
561619d1826SDavid Ahern 		n = NULL;
562619d1826SDavid Ahern 
5631da177e4SLinus Torvalds 	if (n) {
5641da177e4SLinus Torvalds 		state = n->nud_state;
5651da177e4SLinus Torvalds 		neigh_release(n);
56688f64320SJulian Anastasov 	} else {
56788f64320SJulian Anastasov 		return 0;
5681da177e4SLinus Torvalds 	}
5691da177e4SLinus Torvalds 	if (state == NUD_REACHABLE)
5701da177e4SLinus Torvalds 		return 0;
571c17860a0SDenis V. Lunev 	if ((state & NUD_VALID) && order != dflt)
5721da177e4SLinus Torvalds 		return 0;
5731da177e4SLinus Torvalds 	if ((state & NUD_VALID) ||
57488f64320SJulian Anastasov 	    (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
5751da177e4SLinus Torvalds 		*last_resort = fi;
5761da177e4SLinus Torvalds 		*last_idx = order;
5771da177e4SLinus Torvalds 	}
5781da177e4SLinus Torvalds 	return 1;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
581faee6769SAlexander Aring int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
582faee6769SAlexander Aring 		       struct nlattr *encap, u16 encap_type,
583faee6769SAlexander Aring 		       void *cfg, gfp_t gfp_flags,
584979e276eSDavid Ahern 		       struct netlink_ext_ack *extack)
585979e276eSDavid Ahern {
5860f457a36SDavid Ahern 	int err;
5870f457a36SDavid Ahern 
5880f457a36SDavid Ahern 	nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
5890f457a36SDavid Ahern 						    gfp_flags);
5900f457a36SDavid Ahern 	if (!nhc->nhc_pcpu_rth_output)
5910f457a36SDavid Ahern 		return -ENOMEM;
5920f457a36SDavid Ahern 
593979e276eSDavid Ahern 	if (encap) {
594979e276eSDavid Ahern 		struct lwtunnel_state *lwtstate;
595979e276eSDavid Ahern 
596979e276eSDavid Ahern 		if (encap_type == LWTUNNEL_ENCAP_NONE) {
597979e276eSDavid Ahern 			NL_SET_ERR_MSG(extack, "LWT encap type not specified");
5980f457a36SDavid Ahern 			err = -EINVAL;
5990f457a36SDavid Ahern 			goto lwt_failure;
600979e276eSDavid Ahern 		}
601faee6769SAlexander Aring 		err = lwtunnel_build_state(net, encap_type, encap,
602faee6769SAlexander Aring 					   nhc->nhc_family, cfg, &lwtstate,
603faee6769SAlexander Aring 					   extack);
604979e276eSDavid Ahern 		if (err)
6050f457a36SDavid Ahern 			goto lwt_failure;
606979e276eSDavid Ahern 
607979e276eSDavid Ahern 		nhc->nhc_lwtstate = lwtstate_get(lwtstate);
608979e276eSDavid Ahern 	}
609979e276eSDavid Ahern 
610979e276eSDavid Ahern 	return 0;
6110f457a36SDavid Ahern 
6120f457a36SDavid Ahern lwt_failure:
6130f457a36SDavid Ahern 	rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
6140f457a36SDavid Ahern 	nhc->nhc_pcpu_rth_output = NULL;
6150f457a36SDavid Ahern 	return err;
616979e276eSDavid Ahern }
617979e276eSDavid Ahern EXPORT_SYMBOL_GPL(fib_nh_common_init);
618979e276eSDavid Ahern 
619e4516ef6SDavid Ahern int fib_nh_init(struct net *net, struct fib_nh *nh,
620e4516ef6SDavid Ahern 		struct fib_config *cfg, int nh_weight,
621e4516ef6SDavid Ahern 		struct netlink_ext_ack *extack)
622e4516ef6SDavid Ahern {
6230f457a36SDavid Ahern 	int err;
624e4516ef6SDavid Ahern 
625f1741730SDavid Ahern 	nh->fib_nh_family = AF_INET;
626f1741730SDavid Ahern 
627faee6769SAlexander Aring 	err = fib_nh_common_init(net, &nh->nh_common, cfg->fc_encap,
628979e276eSDavid Ahern 				 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
629e4516ef6SDavid Ahern 	if (err)
6300f457a36SDavid Ahern 		return err;
631e4516ef6SDavid Ahern 
632b75ed8b1SDavid Ahern 	nh->fib_nh_oif = cfg->fc_oif;
633a4ea5d43SDavid Ahern 	nh->fib_nh_gw_family = cfg->fc_gw_family;
634a4ea5d43SDavid Ahern 	if (cfg->fc_gw_family == AF_INET)
635f35b794bSDavid Ahern 		nh->fib_nh_gw4 = cfg->fc_gw4;
636a4ea5d43SDavid Ahern 	else if (cfg->fc_gw_family == AF_INET6)
637a4ea5d43SDavid Ahern 		nh->fib_nh_gw6 = cfg->fc_gw6;
638a4ea5d43SDavid Ahern 
639b75ed8b1SDavid Ahern 	nh->fib_nh_flags = cfg->fc_flags;
640e4516ef6SDavid Ahern 
641e4516ef6SDavid Ahern #ifdef CONFIG_IP_ROUTE_CLASSID
642e4516ef6SDavid Ahern 	nh->nh_tclassid = cfg->fc_flow;
643e4516ef6SDavid Ahern 	if (nh->nh_tclassid)
644213f5f8fSEric Dumazet 		atomic_inc(&net->ipv4.fib_num_tclassid_users);
645e4516ef6SDavid Ahern #endif
646e4516ef6SDavid Ahern #ifdef CONFIG_IP_ROUTE_MULTIPATH
647b75ed8b1SDavid Ahern 	nh->fib_nh_weight = nh_weight;
648e4516ef6SDavid Ahern #endif
649e4516ef6SDavid Ahern 	return 0;
650e4516ef6SDavid Ahern }
651e4516ef6SDavid Ahern 
6521da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
6531da177e4SLinus Torvalds 
6546d8422a1SDavid Ahern static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
6556d8422a1SDavid Ahern 			      struct netlink_ext_ack *extack)
6561da177e4SLinus Torvalds {
6571da177e4SLinus Torvalds 	int nhs = 0;
6581da177e4SLinus Torvalds 
6594e902c57SThomas Graf 	while (rtnh_ok(rtnh, remaining)) {
6601da177e4SLinus Torvalds 		nhs++;
6614e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
6621da177e4SLinus Torvalds 	}
6631da177e4SLinus Torvalds 
6644e902c57SThomas Graf 	/* leftover implies invalid nexthop configuration, discard it */
665c3ab2b4eSDavid Ahern 	if (remaining > 0) {
666c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack,
667c3ab2b4eSDavid Ahern 			       "Invalid nexthop configuration - extra data after nexthops");
668c3ab2b4eSDavid Ahern 		nhs = 0;
669c3ab2b4eSDavid Ahern 	}
670c3ab2b4eSDavid Ahern 
671c3ab2b4eSDavid Ahern 	return nhs;
6724e902c57SThomas Graf }
6731da177e4SLinus Torvalds 
6747a3429baSDavid Ahern static int fib_gw_from_attr(__be32 *gw, struct nlattr *nla,
6757a3429baSDavid Ahern 			    struct netlink_ext_ack *extack)
6767a3429baSDavid Ahern {
6777a3429baSDavid Ahern 	if (nla_len(nla) < sizeof(*gw)) {
6787a3429baSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
6797a3429baSDavid Ahern 		return -EINVAL;
6807a3429baSDavid Ahern 	}
6817a3429baSDavid Ahern 
6827a3429baSDavid Ahern 	*gw = nla_get_in_addr(nla);
6837a3429baSDavid Ahern 
6847a3429baSDavid Ahern 	return 0;
6857a3429baSDavid Ahern }
6867a3429baSDavid Ahern 
6874c7e8084SDavid Ahern /* only called when fib_nh is integrated into fib_info */
6884e902c57SThomas Graf static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
6896d8422a1SDavid Ahern 		       int remaining, struct fib_config *cfg,
6906d8422a1SDavid Ahern 		       struct netlink_ext_ack *extack)
6914e902c57SThomas Graf {
692e4516ef6SDavid Ahern 	struct net *net = fi->fib_net;
693e4516ef6SDavid Ahern 	struct fib_config fib_cfg;
6945481d73fSDavid Ahern 	struct fib_nh *nh;
695571e7226SRoopa Prabhu 	int ret;
696571e7226SRoopa Prabhu 
6971da177e4SLinus Torvalds 	change_nexthops(fi) {
6984e902c57SThomas Graf 		int attrlen;
6994e902c57SThomas Graf 
700e4516ef6SDavid Ahern 		memset(&fib_cfg, 0, sizeof(fib_cfg));
701e4516ef6SDavid Ahern 
702c3ab2b4eSDavid Ahern 		if (!rtnh_ok(rtnh, remaining)) {
703c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
704c3ab2b4eSDavid Ahern 				       "Invalid nexthop configuration - extra data after nexthop");
7051da177e4SLinus Torvalds 			return -EINVAL;
706c3ab2b4eSDavid Ahern 		}
7074e902c57SThomas Graf 
708c3ab2b4eSDavid Ahern 		if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
709c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
710c3ab2b4eSDavid Ahern 				       "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
71180610229SJulian Anastasov 			return -EINVAL;
712c3ab2b4eSDavid Ahern 		}
71380610229SJulian Anastasov 
714e4516ef6SDavid Ahern 		fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
715e4516ef6SDavid Ahern 		fib_cfg.fc_oif = rtnh->rtnh_ifindex;
7164e902c57SThomas Graf 
7174e902c57SThomas Graf 		attrlen = rtnh_attrlen(rtnh);
7184e902c57SThomas Graf 		if (attrlen > 0) {
719d1566268SDavid Ahern 			struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
7204e902c57SThomas Graf 
7214e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
722d1566268SDavid Ahern 			nlav = nla_find(attrs, attrlen, RTA_VIA);
723d1566268SDavid Ahern 			if (nla && nlav) {
724d1566268SDavid Ahern 				NL_SET_ERR_MSG(extack,
725d1566268SDavid Ahern 					       "Nexthop configuration can not contain both GATEWAY and VIA");
726d1566268SDavid Ahern 				return -EINVAL;
727d1566268SDavid Ahern 			}
728f35b794bSDavid Ahern 			if (nla) {
7297a3429baSDavid Ahern 				ret = fib_gw_from_attr(&fib_cfg.fc_gw4, nla,
7307a3429baSDavid Ahern 						       extack);
7317a3429baSDavid Ahern 				if (ret)
7327a3429baSDavid Ahern 					goto errout;
7337a3429baSDavid Ahern 
734d73f80f9SDavid Ahern 				if (fib_cfg.fc_gw4)
735d73f80f9SDavid Ahern 					fib_cfg.fc_gw_family = AF_INET;
736d1566268SDavid Ahern 			} else if (nlav) {
737d1566268SDavid Ahern 				ret = fib_gw_from_via(&fib_cfg, nlav, extack);
738d1566268SDavid Ahern 				if (ret)
739d1566268SDavid Ahern 					goto errout;
740f35b794bSDavid Ahern 			}
741571e7226SRoopa Prabhu 
742e4516ef6SDavid Ahern 			nla = nla_find(attrs, attrlen, RTA_FLOW);
743664b9c4bSDavid Ahern 			if (nla) {
744664b9c4bSDavid Ahern 				if (nla_len(nla) < sizeof(u32)) {
745664b9c4bSDavid Ahern 					NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
746664b9c4bSDavid Ahern 					return -EINVAL;
747664b9c4bSDavid Ahern 				}
748e4516ef6SDavid Ahern 				fib_cfg.fc_flow = nla_get_u32(nla);
749664b9c4bSDavid Ahern 			}
750e4516ef6SDavid Ahern 
751e4516ef6SDavid Ahern 			fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
7528bda81a4SDavid Ahern 			/* RTA_ENCAP_TYPE length checked in
7538bda81a4SDavid Ahern 			 * lwtunnel_valid_encap_type_attr
7548bda81a4SDavid Ahern 			 */
755e4516ef6SDavid Ahern 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
756e4516ef6SDavid Ahern 			if (nla)
757e4516ef6SDavid Ahern 				fib_cfg.fc_encap_type = nla_get_u16(nla);
758c3ab2b4eSDavid Ahern 		}
75930357d7dSDavid Ahern 
760e4516ef6SDavid Ahern 		ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
761e4516ef6SDavid Ahern 				  rtnh->rtnh_hops + 1, extack);
762571e7226SRoopa Prabhu 		if (ret)
763571e7226SRoopa Prabhu 			goto errout;
7644e902c57SThomas Graf 
7654e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
7661da177e4SLinus Torvalds 	} endfor_nexthops(fi);
7674e902c57SThomas Graf 
768571e7226SRoopa Prabhu 	ret = -EINVAL;
7695481d73fSDavid Ahern 	nh = fib_info_nh(fi, 0);
7705481d73fSDavid Ahern 	if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) {
771e4516ef6SDavid Ahern 		NL_SET_ERR_MSG(extack,
772e4516ef6SDavid Ahern 			       "Nexthop device index does not match RTA_OIF");
773e4516ef6SDavid Ahern 		goto errout;
774e4516ef6SDavid Ahern 	}
775f35b794bSDavid Ahern 	if (cfg->fc_gw_family) {
7765481d73fSDavid Ahern 		if (cfg->fc_gw_family != nh->fib_nh_gw_family ||
777f35b794bSDavid Ahern 		    (cfg->fc_gw_family == AF_INET &&
7785481d73fSDavid Ahern 		     nh->fib_nh_gw4 != cfg->fc_gw4) ||
779a4ea5d43SDavid Ahern 		    (cfg->fc_gw_family == AF_INET6 &&
7805481d73fSDavid Ahern 		     ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) {
781e4516ef6SDavid Ahern 			NL_SET_ERR_MSG(extack,
782a4ea5d43SDavid Ahern 				       "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
783e4516ef6SDavid Ahern 			goto errout;
784e4516ef6SDavid Ahern 		}
785f35b794bSDavid Ahern 	}
786e4516ef6SDavid Ahern #ifdef CONFIG_IP_ROUTE_CLASSID
7875481d73fSDavid Ahern 	if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) {
788e4516ef6SDavid Ahern 		NL_SET_ERR_MSG(extack,
789e4516ef6SDavid Ahern 			       "Nexthop class id does not match RTA_FLOW");
790e4516ef6SDavid Ahern 		goto errout;
791e4516ef6SDavid Ahern 	}
792e4516ef6SDavid Ahern #endif
793e4516ef6SDavid Ahern 	ret = 0;
794571e7226SRoopa Prabhu errout:
795571e7226SRoopa Prabhu 	return ret;
7961da177e4SLinus Torvalds }
7971da177e4SLinus Torvalds 
7984c7e8084SDavid Ahern /* only called when fib_nh is integrated into fib_info */
7990e884c78SPeter Nørlund static void fib_rebalance(struct fib_info *fi)
8000e884c78SPeter Nørlund {
8010e884c78SPeter Nørlund 	int total;
8020e884c78SPeter Nørlund 	int w;
8030e884c78SPeter Nørlund 
8045481d73fSDavid Ahern 	if (fib_info_num_path(fi) < 2)
8050e884c78SPeter Nørlund 		return;
8060e884c78SPeter Nørlund 
8070e884c78SPeter Nørlund 	total = 0;
8080e884c78SPeter Nørlund 	for_nexthops(fi) {
809b75ed8b1SDavid Ahern 		if (nh->fib_nh_flags & RTNH_F_DEAD)
8100e884c78SPeter Nørlund 			continue;
8110e884c78SPeter Nørlund 
812b75ed8b1SDavid Ahern 		if (ip_ignore_linkdown(nh->fib_nh_dev) &&
813b75ed8b1SDavid Ahern 		    nh->fib_nh_flags & RTNH_F_LINKDOWN)
8140e884c78SPeter Nørlund 			continue;
8150e884c78SPeter Nørlund 
816b75ed8b1SDavid Ahern 		total += nh->fib_nh_weight;
8170e884c78SPeter Nørlund 	} endfor_nexthops(fi);
8180e884c78SPeter Nørlund 
8190e884c78SPeter Nørlund 	w = 0;
8200e884c78SPeter Nørlund 	change_nexthops(fi) {
8210e884c78SPeter Nørlund 		int upper_bound;
8220e884c78SPeter Nørlund 
823b75ed8b1SDavid Ahern 		if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
8240e884c78SPeter Nørlund 			upper_bound = -1;
825b75ed8b1SDavid Ahern 		} else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
826b75ed8b1SDavid Ahern 			   nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
8270e884c78SPeter Nørlund 			upper_bound = -1;
8280e884c78SPeter Nørlund 		} else {
829b75ed8b1SDavid Ahern 			w += nexthop_nh->fib_nh_weight;
8300a837fe4SPeter Nørlund 			upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
8310e884c78SPeter Nørlund 							    total) - 1;
8320e884c78SPeter Nørlund 		}
8330e884c78SPeter Nørlund 
834b75ed8b1SDavid Ahern 		atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
8350e884c78SPeter Nørlund 	} endfor_nexthops(fi);
8360e884c78SPeter Nørlund }
8370e884c78SPeter Nørlund #else /* CONFIG_IP_ROUTE_MULTIPATH */
8380e884c78SPeter Nørlund 
8398373c6c8SDavid Ahern static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
8408373c6c8SDavid Ahern 		       int remaining, struct fib_config *cfg,
8418373c6c8SDavid Ahern 		       struct netlink_ext_ack *extack)
8428373c6c8SDavid Ahern {
8438373c6c8SDavid Ahern 	NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
8448373c6c8SDavid Ahern 
8458373c6c8SDavid Ahern 	return -EINVAL;
8468373c6c8SDavid Ahern }
8478373c6c8SDavid Ahern 
8480e884c78SPeter Nørlund #define fib_rebalance(fi) do { } while (0)
8490e884c78SPeter Nørlund 
8500e884c78SPeter Nørlund #endif /* CONFIG_IP_ROUTE_MULTIPATH */
8511da177e4SLinus Torvalds 
852faee6769SAlexander Aring static int fib_encap_match(struct net *net, u16 encap_type,
853571e7226SRoopa Prabhu 			   struct nlattr *encap,
85430357d7dSDavid Ahern 			   const struct fib_nh *nh,
8559ae28727SDavid Ahern 			   const struct fib_config *cfg,
8569ae28727SDavid Ahern 			   struct netlink_ext_ack *extack)
857571e7226SRoopa Prabhu {
858571e7226SRoopa Prabhu 	struct lwtunnel_state *lwtstate;
859df383e62SJiri Benc 	int ret, result = 0;
860571e7226SRoopa Prabhu 
861571e7226SRoopa Prabhu 	if (encap_type == LWTUNNEL_ENCAP_NONE)
862571e7226SRoopa Prabhu 		return 0;
863571e7226SRoopa Prabhu 
864faee6769SAlexander Aring 	ret = lwtunnel_build_state(net, encap_type, encap, AF_INET,
8659ae28727SDavid Ahern 				   cfg, &lwtstate, extack);
866df383e62SJiri Benc 	if (!ret) {
867b75ed8b1SDavid Ahern 		result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
868df383e62SJiri Benc 		lwtstate_free(lwtstate);
869df383e62SJiri Benc 	}
870571e7226SRoopa Prabhu 
871df383e62SJiri Benc 	return result;
872571e7226SRoopa Prabhu }
873571e7226SRoopa Prabhu 
874faee6769SAlexander Aring int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
8759ae28727SDavid Ahern 		 struct netlink_ext_ack *extack)
8761da177e4SLinus Torvalds {
8771da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
8784e902c57SThomas Graf 	struct rtnexthop *rtnh;
8794e902c57SThomas Graf 	int remaining;
8801da177e4SLinus Torvalds #endif
8811da177e4SLinus Torvalds 
8824e902c57SThomas Graf 	if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
8831da177e4SLinus Torvalds 		return 1;
8841da177e4SLinus Torvalds 
885493ced1aSDavid Ahern 	if (cfg->fc_nh_id) {
886493ced1aSDavid Ahern 		if (fi->nh && cfg->fc_nh_id == fi->nh->id)
887493ced1aSDavid Ahern 			return 0;
888493ced1aSDavid Ahern 		return 1;
889493ced1aSDavid Ahern 	}
890493ced1aSDavid Ahern 
8916bf92d70SNikolay Aleksandrov 	/* cannot match on nexthop object attributes */
8926bf92d70SNikolay Aleksandrov 	if (fi->nh)
8936bf92d70SNikolay Aleksandrov 		return 1;
8946bf92d70SNikolay Aleksandrov 
895*61b91eb3SDavid Ahern 	if (cfg->fc_oif || cfg->fc_gw_family) {
896*61b91eb3SDavid Ahern 		struct fib_nh *nh;
897*61b91eb3SDavid Ahern 
8986bf92d70SNikolay Aleksandrov 		nh = fib_info_nh(fi, 0);
899571e7226SRoopa Prabhu 		if (cfg->fc_encap) {
900faee6769SAlexander Aring 			if (fib_encap_match(net, cfg->fc_encap_type,
901faee6769SAlexander Aring 					    cfg->fc_encap, nh, cfg, extack))
902571e7226SRoopa Prabhu 				return 1;
903571e7226SRoopa Prabhu 		}
904a8c6db1dSStefano Brivio #ifdef CONFIG_IP_ROUTE_CLASSID
905a8c6db1dSStefano Brivio 		if (cfg->fc_flow &&
9065481d73fSDavid Ahern 		    cfg->fc_flow != nh->nh_tclassid)
907a8c6db1dSStefano Brivio 			return 1;
908a8c6db1dSStefano Brivio #endif
9095481d73fSDavid Ahern 		if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
910f35b794bSDavid Ahern 		    (cfg->fc_gw_family &&
9115481d73fSDavid Ahern 		     cfg->fc_gw_family != nh->fib_nh_gw_family))
9121da177e4SLinus Torvalds 			return 1;
913f35b794bSDavid Ahern 
914f35b794bSDavid Ahern 		if (cfg->fc_gw_family == AF_INET &&
9155481d73fSDavid Ahern 		    cfg->fc_gw4 != nh->fib_nh_gw4)
916f35b794bSDavid Ahern 			return 1;
917f35b794bSDavid Ahern 
918a4ea5d43SDavid Ahern 		if (cfg->fc_gw_family == AF_INET6 &&
9195481d73fSDavid Ahern 		    ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
920a4ea5d43SDavid Ahern 			return 1;
921a4ea5d43SDavid Ahern 
922f35b794bSDavid Ahern 		return 0;
9231da177e4SLinus Torvalds 	}
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
92651456b29SIan Morris 	if (!cfg->fc_mp)
9271da177e4SLinus Torvalds 		return 0;
9284e902c57SThomas Graf 
9294e902c57SThomas Graf 	rtnh = cfg->fc_mp;
9304e902c57SThomas Graf 	remaining = cfg->fc_mp_len;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	for_nexthops(fi) {
9334e902c57SThomas Graf 		int attrlen;
9341da177e4SLinus Torvalds 
9354e902c57SThomas Graf 		if (!rtnh_ok(rtnh, remaining))
9361da177e4SLinus Torvalds 			return -EINVAL;
9374e902c57SThomas Graf 
938b75ed8b1SDavid Ahern 		if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
9391da177e4SLinus Torvalds 			return 1;
9404e902c57SThomas Graf 
9414e902c57SThomas Graf 		attrlen = rtnh_attrlen(rtnh);
942f76936d0SJiri Pirko 		if (attrlen > 0) {
943d1566268SDavid Ahern 			struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
9447a3429baSDavid Ahern 			int err;
9454e902c57SThomas Graf 
9464e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
947d1566268SDavid Ahern 			nlav = nla_find(attrs, attrlen, RTA_VIA);
948d1566268SDavid Ahern 			if (nla && nlav) {
949d1566268SDavid Ahern 				NL_SET_ERR_MSG(extack,
950d1566268SDavid Ahern 					       "Nexthop configuration can not contain both GATEWAY and VIA");
951d1566268SDavid Ahern 				return -EINVAL;
952d1566268SDavid Ahern 			}
953d1566268SDavid Ahern 
954d1566268SDavid Ahern 			if (nla) {
9557a3429baSDavid Ahern 				__be32 gw;
9567a3429baSDavid Ahern 
9577a3429baSDavid Ahern 				err = fib_gw_from_attr(&gw, nla, extack);
9587a3429baSDavid Ahern 				if (err)
9597a3429baSDavid Ahern 					return err;
9607a3429baSDavid Ahern 
961d1566268SDavid Ahern 				if (nh->fib_nh_gw_family != AF_INET ||
9627a3429baSDavid Ahern 				    gw != nh->fib_nh_gw4)
9631da177e4SLinus Torvalds 					return 1;
964d1566268SDavid Ahern 			} else if (nlav) {
965d1566268SDavid Ahern 				struct fib_config cfg2;
966d1566268SDavid Ahern 
967d1566268SDavid Ahern 				err = fib_gw_from_via(&cfg2, nlav, extack);
968d1566268SDavid Ahern 				if (err)
969d1566268SDavid Ahern 					return err;
970d1566268SDavid Ahern 
971d1566268SDavid Ahern 				switch (nh->fib_nh_gw_family) {
972d1566268SDavid Ahern 				case AF_INET:
973d1566268SDavid Ahern 					if (cfg2.fc_gw_family != AF_INET ||
974d1566268SDavid Ahern 					    cfg2.fc_gw4 != nh->fib_nh_gw4)
975d1566268SDavid Ahern 						return 1;
976d1566268SDavid Ahern 					break;
977d1566268SDavid Ahern 				case AF_INET6:
978d1566268SDavid Ahern 					if (cfg2.fc_gw_family != AF_INET6 ||
979d1566268SDavid Ahern 					    ipv6_addr_cmp(&cfg2.fc_gw6,
980d1566268SDavid Ahern 							  &nh->fib_nh_gw6))
981d1566268SDavid Ahern 						return 1;
982d1566268SDavid Ahern 					break;
983d1566268SDavid Ahern 				}
984d1566268SDavid Ahern 			}
985d1566268SDavid Ahern 
986c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
9874e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_FLOW);
988664b9c4bSDavid Ahern 			if (nla) {
989664b9c4bSDavid Ahern 				if (nla_len(nla) < sizeof(u32)) {
990664b9c4bSDavid Ahern 					NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
991664b9c4bSDavid Ahern 					return -EINVAL;
992664b9c4bSDavid Ahern 				}
993664b9c4bSDavid Ahern 				if (nla_get_u32(nla) != nh->nh_tclassid)
9941da177e4SLinus Torvalds 					return 1;
995664b9c4bSDavid Ahern 			}
9961da177e4SLinus Torvalds #endif
9971da177e4SLinus Torvalds 		}
9984e902c57SThomas Graf 
9994e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
10001da177e4SLinus Torvalds 	} endfor_nexthops(fi);
10011da177e4SLinus Torvalds #endif
10021da177e4SLinus Torvalds 	return 0;
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds 
10055f9ae3d9SXin Long bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
10065f9ae3d9SXin Long {
10075f9ae3d9SXin Long 	struct nlattr *nla;
10085f9ae3d9SXin Long 	int remaining;
10095f9ae3d9SXin Long 
10105f9ae3d9SXin Long 	if (!cfg->fc_mx)
10115f9ae3d9SXin Long 		return true;
10125f9ae3d9SXin Long 
10135f9ae3d9SXin Long 	nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
10145f9ae3d9SXin Long 		int type = nla_type(nla);
1015d03a4557SPhil Sutter 		u32 fi_val, val;
10165f9ae3d9SXin Long 
10175f9ae3d9SXin Long 		if (!type)
10185f9ae3d9SXin Long 			continue;
10195f9ae3d9SXin Long 		if (type > RTAX_MAX)
10205f9ae3d9SXin Long 			return false;
10215f9ae3d9SXin Long 
10225f9ae3d9SXin Long 		if (type == RTAX_CC_ALGO) {
10235f9ae3d9SXin Long 			char tmp[TCP_CA_NAME_MAX];
10245f9ae3d9SXin Long 			bool ecn_ca = false;
10255f9ae3d9SXin Long 
1026872f6903SFrancis Laniel 			nla_strscpy(tmp, nla, sizeof(tmp));
10276670e152SStephen Hemminger 			val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
10285f9ae3d9SXin Long 		} else {
10295b5e7a0dSEric Dumazet 			if (nla_len(nla) != sizeof(u32))
10305b5e7a0dSEric Dumazet 				return false;
10315f9ae3d9SXin Long 			val = nla_get_u32(nla);
10325f9ae3d9SXin Long 		}
10335f9ae3d9SXin Long 
1034d03a4557SPhil Sutter 		fi_val = fi->fib_metrics->metrics[type - 1];
1035d03a4557SPhil Sutter 		if (type == RTAX_FEATURES)
1036d03a4557SPhil Sutter 			fi_val &= ~DST_FEATURE_ECN_CA;
1037d03a4557SPhil Sutter 
1038d03a4557SPhil Sutter 		if (fi_val != val)
10395f9ae3d9SXin Long 			return false;
10405f9ae3d9SXin Long 	}
10415f9ae3d9SXin Long 
10425f9ae3d9SXin Long 	return true;
10435f9ae3d9SXin Long }
10445f9ae3d9SXin Long 
1045717a8f5bSDavid Ahern static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
1046717a8f5bSDavid Ahern 			      u32 table, struct netlink_ext_ack *extack)
1047717a8f5bSDavid Ahern {
1048717a8f5bSDavid Ahern 	struct fib6_config cfg = {
1049717a8f5bSDavid Ahern 		.fc_table = table,
1050717a8f5bSDavid Ahern 		.fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
1051717a8f5bSDavid Ahern 		.fc_ifindex = nh->fib_nh_oif,
1052717a8f5bSDavid Ahern 		.fc_gateway = nh->fib_nh_gw6,
1053717a8f5bSDavid Ahern 	};
1054717a8f5bSDavid Ahern 	struct fib6_nh fib6_nh = {};
1055717a8f5bSDavid Ahern 	int err;
1056717a8f5bSDavid Ahern 
1057717a8f5bSDavid Ahern 	err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
1058717a8f5bSDavid Ahern 	if (!err) {
1059717a8f5bSDavid Ahern 		nh->fib_nh_dev = fib6_nh.fib_nh_dev;
1060d62607c3SJakub Kicinski 		netdev_hold(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
1061d62607c3SJakub Kicinski 			    GFP_KERNEL);
1062717a8f5bSDavid Ahern 		nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
1063717a8f5bSDavid Ahern 		nh->fib_nh_scope = RT_SCOPE_LINK;
1064717a8f5bSDavid Ahern 
1065717a8f5bSDavid Ahern 		ipv6_stub->fib6_nh_release(&fib6_nh);
1066717a8f5bSDavid Ahern 	}
1067717a8f5bSDavid Ahern 
1068717a8f5bSDavid Ahern 	return err;
1069717a8f5bSDavid Ahern }
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds /*
10726a31d2a9SEric Dumazet  * Picture
10736a31d2a9SEric Dumazet  * -------
10746a31d2a9SEric Dumazet  *
10756a31d2a9SEric Dumazet  * Semantics of nexthop is very messy by historical reasons.
10766a31d2a9SEric Dumazet  * We have to take into account, that:
10776a31d2a9SEric Dumazet  * a) gateway can be actually local interface address,
10786a31d2a9SEric Dumazet  *    so that gatewayed route is direct.
10796a31d2a9SEric Dumazet  * b) gateway must be on-link address, possibly
10806a31d2a9SEric Dumazet  *    described not by an ifaddr, but also by a direct route.
10816a31d2a9SEric Dumazet  * c) If both gateway and interface are specified, they should not
10826a31d2a9SEric Dumazet  *    contradict.
10836a31d2a9SEric Dumazet  * d) If we use tunnel routes, gateway could be not on-link.
10846a31d2a9SEric Dumazet  *
10856a31d2a9SEric Dumazet  * Attempt to reconcile all of these (alas, self-contradictory) conditions
10866a31d2a9SEric Dumazet  * results in pretty ugly and hairy code with obscure logic.
10876a31d2a9SEric Dumazet  *
10886a31d2a9SEric Dumazet  * I chose to generalized it instead, so that the size
10896a31d2a9SEric Dumazet  * of code does not increase practically, but it becomes
10906a31d2a9SEric Dumazet  * much more general.
10916a31d2a9SEric Dumazet  * Every prefix is assigned a "scope" value: "host" is local address,
10926a31d2a9SEric Dumazet  * "link" is direct route,
10936a31d2a9SEric Dumazet  * [ ... "site" ... "interior" ... ]
10946a31d2a9SEric Dumazet  * and "universe" is true gateway route with global meaning.
10956a31d2a9SEric Dumazet  *
10966a31d2a9SEric Dumazet  * Every prefix refers to a set of "nexthop"s (gw, oif),
10976a31d2a9SEric Dumazet  * where gw must have narrower scope. This recursion stops
10986a31d2a9SEric Dumazet  * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
10996a31d2a9SEric Dumazet  * which means that gw is forced to be on link.
11006a31d2a9SEric Dumazet  *
11016a31d2a9SEric Dumazet  * Code is still hairy, but now it is apparently logically
11026a31d2a9SEric Dumazet  * consistent and very flexible. F.e. as by-product it allows
11036a31d2a9SEric Dumazet  * to co-exists in peace independent exterior and interior
11046a31d2a9SEric Dumazet  * routing processes.
11056a31d2a9SEric Dumazet  *
11066a31d2a9SEric Dumazet  * Normally it looks as following.
11076a31d2a9SEric Dumazet  *
11086a31d2a9SEric Dumazet  * {universe prefix}  -> (gw, oif) [scope link]
11096a31d2a9SEric Dumazet  *		  |
11106a31d2a9SEric Dumazet  *		  |-> {link prefix} -> (gw, oif) [scope local]
11116a31d2a9SEric Dumazet  *					|
11126a31d2a9SEric Dumazet  *					|-> {local prefix} (terminal node)
11131da177e4SLinus Torvalds  */
1114448d7248SDavid Ahern static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
1115448d7248SDavid Ahern 			      u8 scope, struct netlink_ext_ack *extack)
11161da177e4SLinus Torvalds {
11176a31d2a9SEric Dumazet 	struct net_device *dev;
11181da177e4SLinus Torvalds 	struct fib_result res;
1119c3fee640SEnrico Weigelt 	int err = 0;
11201da177e4SLinus Torvalds 
1121b75ed8b1SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_ONLINK) {
112230bbaa19SDavid Ahern 		unsigned int addr_type;
11231da177e4SLinus Torvalds 
1124448d7248SDavid Ahern 		if (scope >= RT_SCOPE_LINK) {
1125448d7248SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
11261da177e4SLinus Torvalds 			return -EINVAL;
1127c3ab2b4eSDavid Ahern 		}
1128b75ed8b1SDavid Ahern 		dev = __dev_get_by_index(net, nh->fib_nh_oif);
1129066b1030SDavid Ahern 		if (!dev) {
1130066b1030SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
11311da177e4SLinus Torvalds 			return -ENODEV;
1132066b1030SDavid Ahern 		}
1133c3ab2b4eSDavid Ahern 		if (!(dev->flags & IFF_UP)) {
1134448d7248SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
11351da177e4SLinus Torvalds 			return -ENETDOWN;
1136c3ab2b4eSDavid Ahern 		}
1137448d7248SDavid Ahern 		addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
1138c3ab2b4eSDavid Ahern 		if (addr_type != RTN_UNICAST) {
1139448d7248SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
114030bbaa19SDavid Ahern 			return -EINVAL;
1141c3ab2b4eSDavid Ahern 		}
11428a3d0316SAndy Gospodarek 		if (!netif_carrier_ok(dev))
1143b75ed8b1SDavid Ahern 			nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1144b75ed8b1SDavid Ahern 		nh->fib_nh_dev = dev;
1145d62607c3SJakub Kicinski 		netdev_hold(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
1146b75ed8b1SDavid Ahern 		nh->fib_nh_scope = RT_SCOPE_LINK;
11471da177e4SLinus Torvalds 		return 0;
11481da177e4SLinus Torvalds 	}
1149ebc0ffaeSEric Dumazet 	rcu_read_lock();
11501da177e4SLinus Torvalds 	{
11513bfd8472SDavid Ahern 		struct fib_table *tbl = NULL;
11529ade2286SDavid S. Miller 		struct flowi4 fl4 = {
1153b75ed8b1SDavid Ahern 			.daddr = nh->fib_nh_gw4,
1154448d7248SDavid Ahern 			.flowi4_scope = scope + 1,
1155b75ed8b1SDavid Ahern 			.flowi4_oif = nh->fib_nh_oif,
11566a662719SCong Wang 			.flowi4_iif = LOOPBACK_IFINDEX,
11574e902c57SThomas Graf 		};
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 		/* It is not necessary, but requires a bit of thinking */
11609ade2286SDavid S. Miller 		if (fl4.flowi4_scope < RT_SCOPE_LINK)
11619ade2286SDavid S. Miller 			fl4.flowi4_scope = RT_SCOPE_LINK;
11623bfd8472SDavid Ahern 
11635eea3a63Sguodeqing 		if (table && table != RT_TABLE_MAIN)
1164448d7248SDavid Ahern 			tbl = fib_get_table(net, table);
11653bfd8472SDavid Ahern 
11663bfd8472SDavid Ahern 		if (tbl)
11673bfd8472SDavid Ahern 			err = fib_table_lookup(tbl, &fl4, &res,
11681e313678SEric Dumazet 					       FIB_LOOKUP_IGNORE_LINKSTATE |
11691e313678SEric Dumazet 					       FIB_LOOKUP_NOREF);
11704c9bcd11SDavid Ahern 
11714c9bcd11SDavid Ahern 		/* on error or if no table given do full lookup. This
11724c9bcd11SDavid Ahern 		 * is needed for example when nexthops are in the local
11734c9bcd11SDavid Ahern 		 * table rather than the given table
11744c9bcd11SDavid Ahern 		 */
11754c9bcd11SDavid Ahern 		if (!tbl || err) {
11760eeb075fSAndy Gospodarek 			err = fib_lookup(net, &fl4, &res,
11770eeb075fSAndy Gospodarek 					 FIB_LOOKUP_IGNORE_LINKSTATE);
11784c9bcd11SDavid Ahern 		}
11794c9bcd11SDavid Ahern 
1180ebc0ffaeSEric Dumazet 		if (err) {
1181448d7248SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1182448d7248SDavid Ahern 			goto out;
11831da177e4SLinus Torvalds 		}
1184ebc0ffaeSEric Dumazet 	}
1185448d7248SDavid Ahern 
11861da177e4SLinus Torvalds 	err = -EINVAL;
1187c3ab2b4eSDavid Ahern 	if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1188c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
11891da177e4SLinus Torvalds 		goto out;
1190c3ab2b4eSDavid Ahern 	}
1191b75ed8b1SDavid Ahern 	nh->fib_nh_scope = res.scope;
1192b75ed8b1SDavid Ahern 	nh->fib_nh_oif = FIB_RES_OIF(res);
1193b75ed8b1SDavid Ahern 	nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1194c3ab2b4eSDavid Ahern 	if (!dev) {
1195c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack,
1196c3ab2b4eSDavid Ahern 			       "No egress device for nexthop gateway");
11971da177e4SLinus Torvalds 		goto out;
1198c3ab2b4eSDavid Ahern 	}
1199d62607c3SJakub Kicinski 	netdev_hold(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
12008a3d0316SAndy Gospodarek 	if (!netif_carrier_ok(dev))
1201b75ed8b1SDavid Ahern 		nh->fib_nh_flags |= RTNH_F_LINKDOWN;
12028723e1b4SEric Dumazet 	err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
1203448d7248SDavid Ahern out:
1204448d7248SDavid Ahern 	rcu_read_unlock();
1205448d7248SDavid Ahern 	return err;
1206448d7248SDavid Ahern }
1207448d7248SDavid Ahern 
1208448d7248SDavid Ahern static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1209448d7248SDavid Ahern 			      struct netlink_ext_ack *extack)
1210448d7248SDavid Ahern {
12111da177e4SLinus Torvalds 	struct in_device *in_dev;
1212448d7248SDavid Ahern 	int err;
12131da177e4SLinus Torvalds 
1214b75ed8b1SDavid Ahern 	if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1215c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack,
1216c3ab2b4eSDavid Ahern 			       "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
12171da177e4SLinus Torvalds 		return -EINVAL;
1218c3ab2b4eSDavid Ahern 	}
1219448d7248SDavid Ahern 
12208723e1b4SEric Dumazet 	rcu_read_lock();
1221448d7248SDavid Ahern 
12228723e1b4SEric Dumazet 	err = -ENODEV;
1223b75ed8b1SDavid Ahern 	in_dev = inetdev_by_index(net, nh->fib_nh_oif);
122451456b29SIan Morris 	if (!in_dev)
12258723e1b4SEric Dumazet 		goto out;
12268723e1b4SEric Dumazet 	err = -ENETDOWN;
1227c3ab2b4eSDavid Ahern 	if (!(in_dev->dev->flags & IFF_UP)) {
1228c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
12298723e1b4SEric Dumazet 		goto out;
1230c3ab2b4eSDavid Ahern 	}
1231448d7248SDavid Ahern 
1232b75ed8b1SDavid Ahern 	nh->fib_nh_dev = in_dev->dev;
1233d62607c3SJakub Kicinski 	netdev_hold(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
1234747c1430SNicolas Dichtel 	nh->fib_nh_scope = RT_SCOPE_LINK;
1235b75ed8b1SDavid Ahern 	if (!netif_carrier_ok(nh->fib_nh_dev))
1236b75ed8b1SDavid Ahern 		nh->fib_nh_flags |= RTNH_F_LINKDOWN;
12378723e1b4SEric Dumazet 	err = 0;
12388723e1b4SEric Dumazet out:
12398723e1b4SEric Dumazet 	rcu_read_unlock();
12408723e1b4SEric Dumazet 	return err;
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
1243ac1fab2dSDavid Ahern int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1244448d7248SDavid Ahern 		 struct netlink_ext_ack *extack)
1245448d7248SDavid Ahern {
1246448d7248SDavid Ahern 	int err;
1247448d7248SDavid Ahern 
1248448d7248SDavid Ahern 	if (nh->fib_nh_gw_family == AF_INET)
1249ac1fab2dSDavid Ahern 		err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
1250717a8f5bSDavid Ahern 	else if (nh->fib_nh_gw_family == AF_INET6)
1251717a8f5bSDavid Ahern 		err = fib_check_nh_v6_gw(net, nh, table, extack);
1252448d7248SDavid Ahern 	else
1253448d7248SDavid Ahern 		err = fib_check_nh_nongw(net, nh, extack);
1254448d7248SDavid Ahern 
1255448d7248SDavid Ahern 	return err;
1256448d7248SDavid Ahern }
1257448d7248SDavid Ahern 
125879eb15daSEric Dumazet static struct hlist_head *
125979eb15daSEric Dumazet fib_info_laddrhash_bucket(const struct net *net, __be32 val)
12601da177e4SLinus Torvalds {
126179eb15daSEric Dumazet 	u32 slot = hash_32(net_hash_mix(net) ^ (__force u32)val,
126279eb15daSEric Dumazet 			   fib_info_hash_bits);
12631da177e4SLinus Torvalds 
126479eb15daSEric Dumazet 	return &fib_info_laddrhash[slot];
12651da177e4SLinus Torvalds }
12661da177e4SLinus Torvalds 
1267123b9731SDavid S. Miller static void fib_info_hash_move(struct hlist_head *new_info_hash,
12681da177e4SLinus Torvalds 			       struct hlist_head *new_laddrhash,
12691da177e4SLinus Torvalds 			       unsigned int new_size)
12701da177e4SLinus Torvalds {
1271b7656e7fSDavid S. Miller 	struct hlist_head *old_info_hash, *old_laddrhash;
1272123b9731SDavid S. Miller 	unsigned int old_size = fib_info_hash_size;
1273ca73b68aSEric Dumazet 	unsigned int i;
12741da177e4SLinus Torvalds 
1275832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
1276b7656e7fSDavid S. Miller 	old_info_hash = fib_info_hash;
1277b7656e7fSDavid S. Miller 	old_laddrhash = fib_info_laddrhash;
1278123b9731SDavid S. Miller 	fib_info_hash_size = new_size;
127979eb15daSEric Dumazet 	fib_info_hash_bits = ilog2(new_size);
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	for (i = 0; i < old_size; i++) {
12821da177e4SLinus Torvalds 		struct hlist_head *head = &fib_info_hash[i];
1283b67bfe0dSSasha Levin 		struct hlist_node *n;
12841da177e4SLinus Torvalds 		struct fib_info *fi;
12851da177e4SLinus Torvalds 
1286b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(fi, n, head, fib_hash) {
12871da177e4SLinus Torvalds 			struct hlist_head *dest;
12881da177e4SLinus Torvalds 			unsigned int new_hash;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 			new_hash = fib_info_hashfn(fi);
12911da177e4SLinus Torvalds 			dest = &new_info_hash[new_hash];
12921da177e4SLinus Torvalds 			hlist_add_head(&fi->fib_hash, dest);
12931da177e4SLinus Torvalds 		}
12941da177e4SLinus Torvalds 	}
12951da177e4SLinus Torvalds 	fib_info_hash = new_info_hash;
12961da177e4SLinus Torvalds 
129779eb15daSEric Dumazet 	fib_info_laddrhash = new_laddrhash;
12981da177e4SLinus Torvalds 	for (i = 0; i < old_size; i++) {
129979eb15daSEric Dumazet 		struct hlist_head *lhead = &old_laddrhash[i];
1300b67bfe0dSSasha Levin 		struct hlist_node *n;
13011da177e4SLinus Torvalds 		struct fib_info *fi;
13021da177e4SLinus Torvalds 
1303b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
13041da177e4SLinus Torvalds 			struct hlist_head *ldest;
13051da177e4SLinus Torvalds 
130679eb15daSEric Dumazet 			ldest = fib_info_laddrhash_bucket(fi->fib_net,
130779eb15daSEric Dumazet 							  fi->fib_prefsrc);
13081da177e4SLinus Torvalds 			hlist_add_head(&fi->fib_lhash, ldest);
13091da177e4SLinus Torvalds 		}
13101da177e4SLinus Torvalds 	}
13111da177e4SLinus Torvalds 
1312832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
1313b7656e7fSDavid S. Miller 
1314ca73b68aSEric Dumazet 	kvfree(old_info_hash);
1315ca73b68aSEric Dumazet 	kvfree(old_laddrhash);
13161da177e4SLinus Torvalds }
13171da177e4SLinus Torvalds 
1318dcb1ecb5SDavid Ahern __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
1319c3669486SDavid Ahern 				 unsigned char scope)
1320436c3b66SDavid S. Miller {
1321dcb1ecb5SDavid Ahern 	struct fib_nh *nh;
1322dcb1ecb5SDavid Ahern 
1323dcb1ecb5SDavid Ahern 	if (nhc->nhc_family != AF_INET)
1324dcb1ecb5SDavid Ahern 		return inet_select_addr(nhc->nhc_dev, 0, scope);
1325dcb1ecb5SDavid Ahern 
1326dcb1ecb5SDavid Ahern 	nh = container_of(nhc, struct fib_nh, nh_common);
1327c3669486SDavid Ahern 	nh->nh_saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
1328436c3b66SDavid S. Miller 	nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1329436c3b66SDavid S. Miller 
1330436c3b66SDavid S. Miller 	return nh->nh_saddr;
1331436c3b66SDavid S. Miller }
1332436c3b66SDavid S. Miller 
1333eba618abSDavid Ahern __be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1334eba618abSDavid Ahern {
1335eba618abSDavid Ahern 	struct fib_nh_common *nhc = res->nhc;
1336eba618abSDavid Ahern 
1337eba618abSDavid Ahern 	if (res->fi->fib_prefsrc)
1338eba618abSDavid Ahern 		return res->fi->fib_prefsrc;
1339eba618abSDavid Ahern 
1340dcb1ecb5SDavid Ahern 	if (nhc->nhc_family == AF_INET) {
1341dcb1ecb5SDavid Ahern 		struct fib_nh *nh;
1342dcb1ecb5SDavid Ahern 
1343eba618abSDavid Ahern 		nh = container_of(nhc, struct fib_nh, nh_common);
1344eba618abSDavid Ahern 		if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1345eba618abSDavid Ahern 			return nh->nh_saddr;
1346dcb1ecb5SDavid Ahern 	}
1347eba618abSDavid Ahern 
1348dcb1ecb5SDavid Ahern 	return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
1349eba618abSDavid Ahern }
1350eba618abSDavid Ahern 
1351021dd3b8SDavid Ahern static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1352021dd3b8SDavid Ahern {
1353021dd3b8SDavid Ahern 	if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1354021dd3b8SDavid Ahern 	    fib_prefsrc != cfg->fc_dst) {
13559b8ff518SDavid Ahern 		u32 tb_id = cfg->fc_table;
1356e1b8d903SDavid Ahern 		int rc;
1357021dd3b8SDavid Ahern 
1358021dd3b8SDavid Ahern 		if (tb_id == RT_TABLE_MAIN)
1359021dd3b8SDavid Ahern 			tb_id = RT_TABLE_LOCAL;
1360021dd3b8SDavid Ahern 
1361e1b8d903SDavid Ahern 		rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1362e1b8d903SDavid Ahern 					  fib_prefsrc, tb_id);
1363e1b8d903SDavid Ahern 
1364e1b8d903SDavid Ahern 		if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1365e1b8d903SDavid Ahern 			rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1366e1b8d903SDavid Ahern 						  fib_prefsrc, RT_TABLE_LOCAL);
1367021dd3b8SDavid Ahern 		}
1368e1b8d903SDavid Ahern 
1369e1b8d903SDavid Ahern 		if (rc != RTN_LOCAL)
1370e1b8d903SDavid Ahern 			return false;
1371021dd3b8SDavid Ahern 	}
1372021dd3b8SDavid Ahern 	return true;
1373021dd3b8SDavid Ahern }
1374021dd3b8SDavid Ahern 
13756d8422a1SDavid Ahern struct fib_info *fib_create_info(struct fib_config *cfg,
13766d8422a1SDavid Ahern 				 struct netlink_ext_ack *extack)
13771da177e4SLinus Torvalds {
13781da177e4SLinus Torvalds 	int err;
13791da177e4SLinus Torvalds 	struct fib_info *fi = NULL;
13804c7e8084SDavid Ahern 	struct nexthop *nh = NULL;
13811da177e4SLinus Torvalds 	struct fib_info *ofi;
13821da177e4SLinus Torvalds 	int nhs = 1;
13837462bd74SDenis V. Lunev 	struct net *net = cfg->fc_nlinfo.nl_net;
13841da177e4SLinus Torvalds 
13854c8237cdSDavid S. Miller 	if (cfg->fc_type > RTN_MAX)
13864c8237cdSDavid S. Miller 		goto err_inval;
13874c8237cdSDavid S. Miller 
13881da177e4SLinus Torvalds 	/* Fast check to catch the most weird cases */
1389c3ab2b4eSDavid Ahern 	if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1390c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid scope");
13911da177e4SLinus Torvalds 		goto err_inval;
1392c3ab2b4eSDavid Ahern 	}
13931da177e4SLinus Torvalds 
1394c3ab2b4eSDavid Ahern 	if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1395c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack,
1396c3ab2b4eSDavid Ahern 			       "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
139780610229SJulian Anastasov 		goto err_inval;
1398c3ab2b4eSDavid Ahern 	}
139980610229SJulian Anastasov 
1400493ced1aSDavid Ahern 	if (cfg->fc_nh_id) {
14016c48ea5fSDavid Ahern 		if (!cfg->fc_mx) {
14026c48ea5fSDavid Ahern 			fi = fib_find_info_nh(net, cfg);
14036c48ea5fSDavid Ahern 			if (fi) {
140479976892SYajun Deng 				refcount_inc(&fi->fib_treeref);
14056c48ea5fSDavid Ahern 				return fi;
14066c48ea5fSDavid Ahern 			}
14076c48ea5fSDavid Ahern 		}
14086c48ea5fSDavid Ahern 
1409493ced1aSDavid Ahern 		nh = nexthop_find_by_id(net, cfg->fc_nh_id);
1410493ced1aSDavid Ahern 		if (!nh) {
1411493ced1aSDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
1412493ced1aSDavid Ahern 			goto err_inval;
1413493ced1aSDavid Ahern 		}
1414493ced1aSDavid Ahern 		nhs = 0;
1415493ced1aSDavid Ahern 	}
1416493ced1aSDavid Ahern 
14171da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
14184e902c57SThomas Graf 	if (cfg->fc_mp) {
14196d8422a1SDavid Ahern 		nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
14201da177e4SLinus Torvalds 		if (nhs == 0)
14211da177e4SLinus Torvalds 			goto err_inval;
14221da177e4SLinus Torvalds 	}
14231da177e4SLinus Torvalds #endif
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	err = -ENOBUFS;
14260a6e6b3cSEric Dumazet 
14270a6e6b3cSEric Dumazet 	/* Paired with WRITE_ONCE() in fib_release_info() */
14280a6e6b3cSEric Dumazet 	if (READ_ONCE(fib_info_cnt) >= fib_info_hash_size) {
1429123b9731SDavid S. Miller 		unsigned int new_size = fib_info_hash_size << 1;
14301da177e4SLinus Torvalds 		struct hlist_head *new_info_hash;
14311da177e4SLinus Torvalds 		struct hlist_head *new_laddrhash;
1432ca73b68aSEric Dumazet 		size_t bytes;
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 		if (!new_size)
1435d94ce9b2SEric Dumazet 			new_size = 16;
1436ca73b68aSEric Dumazet 		bytes = (size_t)new_size * sizeof(struct hlist_head *);
1437ca73b68aSEric Dumazet 		new_info_hash = kvzalloc(bytes, GFP_KERNEL);
1438ca73b68aSEric Dumazet 		new_laddrhash = kvzalloc(bytes, GFP_KERNEL);
14391da177e4SLinus Torvalds 		if (!new_info_hash || !new_laddrhash) {
1440ca73b68aSEric Dumazet 			kvfree(new_info_hash);
1441ca73b68aSEric Dumazet 			kvfree(new_laddrhash);
1442ca73b68aSEric Dumazet 		} else {
1443123b9731SDavid S. Miller 			fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
1444ca73b68aSEric Dumazet 		}
1445123b9731SDavid S. Miller 		if (!fib_info_hash_size)
14461da177e4SLinus Torvalds 			goto failure;
14471da177e4SLinus Torvalds 	}
14481da177e4SLinus Torvalds 
14491f533ba6SGustavo A. R. Silva 	fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
145051456b29SIan Morris 	if (!fi)
14511da177e4SLinus Torvalds 		goto failure;
1452767a2217SDavid Ahern 	fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
1453d7e774f3SDavid Ahern 					      cfg->fc_mx_len, extack);
145488e235b8SEnrico Weigelt 	if (IS_ERR(fi->fib_metrics)) {
1455767a2217SDavid Ahern 		err = PTR_ERR(fi->fib_metrics);
1456187e5b3aSEric Dumazet 		kfree(fi);
1457187e5b3aSEric Dumazet 		return ERR_PTR(err);
1458187e5b3aSEric Dumazet 	}
1459767a2217SDavid Ahern 
1460efd7ef1cSEric W. Biederman 	fi->fib_net = net;
14614e902c57SThomas Graf 	fi->fib_protocol = cfg->fc_protocol;
146237e826c5SDavid S. Miller 	fi->fib_scope = cfg->fc_scope;
14634e902c57SThomas Graf 	fi->fib_flags = cfg->fc_flags;
14644e902c57SThomas Graf 	fi->fib_priority = cfg->fc_priority;
14654e902c57SThomas Graf 	fi->fib_prefsrc = cfg->fc_prefsrc;
1466f4ef85bbSEric Dumazet 	fi->fib_type = cfg->fc_type;
14675a56a0b3SMark Tomlinson 	fi->fib_tb_id = cfg->fc_table;
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	fi->fib_nhs = nhs;
14704c7e8084SDavid Ahern 	if (nh) {
14714c7e8084SDavid Ahern 		if (!nexthop_get(nh)) {
14724c7e8084SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
14734c7e8084SDavid Ahern 			err = -EINVAL;
14744c7e8084SDavid Ahern 		} else {
14754c7e8084SDavid Ahern 			err = 0;
14764c7e8084SDavid Ahern 			fi->nh = nh;
14774c7e8084SDavid Ahern 		}
14784c7e8084SDavid Ahern 	} else {
14791da177e4SLinus Torvalds 		change_nexthops(fi) {
148071fceff0SDavid S. Miller 			nexthop_nh->nh_parent = fi;
14811da177e4SLinus Torvalds 		} endfor_nexthops(fi)
14821da177e4SLinus Torvalds 
1483e4516ef6SDavid Ahern 		if (cfg->fc_mp)
14844c7e8084SDavid Ahern 			err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
14854c7e8084SDavid Ahern 					  extack);
1486e4516ef6SDavid Ahern 		else
1487e4516ef6SDavid Ahern 			err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
14884c7e8084SDavid Ahern 	}
1489e4516ef6SDavid Ahern 
14904e902c57SThomas Graf 	if (err != 0)
14911da177e4SLinus Torvalds 		goto failure;
14921da177e4SLinus Torvalds 
14934e902c57SThomas Graf 	if (fib_props[cfg->fc_type].error) {
1494f35b794bSDavid Ahern 		if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
1495c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
1496c3ab2b4eSDavid Ahern 				       "Gateway, device and multipath can not be specified for this route type");
14971da177e4SLinus Torvalds 			goto err_inval;
1498c3ab2b4eSDavid Ahern 		}
14991da177e4SLinus Torvalds 		goto link_it;
15004c8237cdSDavid S. Miller 	} else {
15014c8237cdSDavid S. Miller 		switch (cfg->fc_type) {
15024c8237cdSDavid S. Miller 		case RTN_UNICAST:
15034c8237cdSDavid S. Miller 		case RTN_LOCAL:
15044c8237cdSDavid S. Miller 		case RTN_BROADCAST:
15054c8237cdSDavid S. Miller 		case RTN_ANYCAST:
15064c8237cdSDavid S. Miller 		case RTN_MULTICAST:
15074c8237cdSDavid S. Miller 			break;
15084c8237cdSDavid S. Miller 		default:
1509c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid route type");
15104c8237cdSDavid S. Miller 			goto err_inval;
15114c8237cdSDavid S. Miller 		}
15121da177e4SLinus Torvalds 	}
15131da177e4SLinus Torvalds 
1514c3ab2b4eSDavid Ahern 	if (cfg->fc_scope > RT_SCOPE_HOST) {
1515c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid scope");
15161da177e4SLinus Torvalds 		goto err_inval;
1517c3ab2b4eSDavid Ahern 	}
15181da177e4SLinus Torvalds 
15194c7e8084SDavid Ahern 	if (fi->nh) {
15204c7e8084SDavid Ahern 		err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
15214c7e8084SDavid Ahern 		if (err)
15224c7e8084SDavid Ahern 			goto failure;
15234c7e8084SDavid Ahern 	} else if (cfg->fc_scope == RT_SCOPE_HOST) {
15241da177e4SLinus Torvalds 		struct fib_nh *nh = fi->fib_nh;
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds 		/* Local address is added. */
1527c3ab2b4eSDavid Ahern 		if (nhs != 1) {
1528c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
1529c3ab2b4eSDavid Ahern 				       "Route with host scope can not have multiple nexthops");
15306d8422a1SDavid Ahern 			goto err_inval;
1531c3ab2b4eSDavid Ahern 		}
1532bdf00467SDavid Ahern 		if (nh->fib_nh_gw_family) {
1533c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
1534c3ab2b4eSDavid Ahern 				       "Route with host scope can not have a gateway");
15351da177e4SLinus Torvalds 			goto err_inval;
1536c3ab2b4eSDavid Ahern 		}
1537b75ed8b1SDavid Ahern 		nh->fib_nh_scope = RT_SCOPE_NOWHERE;
15385481d73fSDavid Ahern 		nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
15391da177e4SLinus Torvalds 		err = -ENODEV;
1540b75ed8b1SDavid Ahern 		if (!nh->fib_nh_dev)
15411da177e4SLinus Torvalds 			goto failure;
1542e44b14ebSEric Dumazet 		netdev_tracker_alloc(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
1543e44b14ebSEric Dumazet 				     GFP_KERNEL);
15441da177e4SLinus Torvalds 	} else {
15458a3d0316SAndy Gospodarek 		int linkdown = 0;
15468a3d0316SAndy Gospodarek 
15471da177e4SLinus Torvalds 		change_nexthops(fi) {
1548ac1fab2dSDavid Ahern 			err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1549ac1fab2dSDavid Ahern 					   cfg->fc_table, cfg->fc_scope,
1550ac1fab2dSDavid Ahern 					   extack);
15516a31d2a9SEric Dumazet 			if (err != 0)
15521da177e4SLinus Torvalds 				goto failure;
1553b75ed8b1SDavid Ahern 			if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
15548a3d0316SAndy Gospodarek 				linkdown++;
15551da177e4SLinus Torvalds 		} endfor_nexthops(fi)
15568a3d0316SAndy Gospodarek 		if (linkdown == fi->fib_nhs)
15578a3d0316SAndy Gospodarek 			fi->fib_flags |= RTNH_F_LINKDOWN;
15581da177e4SLinus Torvalds 	}
15591da177e4SLinus Torvalds 
1560c3ab2b4eSDavid Ahern 	if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1561c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
15621da177e4SLinus Torvalds 		goto err_inval;
1563c3ab2b4eSDavid Ahern 	}
15641da177e4SLinus Torvalds 
15654c7e8084SDavid Ahern 	if (!fi->nh) {
15661fc050a1SDavid S. Miller 		change_nexthops(fi) {
1567dcb1ecb5SDavid Ahern 			fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
1568dcb1ecb5SDavid Ahern 						  fi->fib_scope);
156919a9d136SDavid Ahern 			if (nexthop_nh->fib_nh_gw_family == AF_INET6)
157019a9d136SDavid Ahern 				fi->fib_nh_is_v6 = true;
15711fc050a1SDavid S. Miller 		} endfor_nexthops(fi)
15721fc050a1SDavid S. Miller 
15730e884c78SPeter Nørlund 		fib_rebalance(fi);
15744c7e8084SDavid Ahern 	}
15750e884c78SPeter Nørlund 
15761da177e4SLinus Torvalds link_it:
15776a31d2a9SEric Dumazet 	ofi = fib_find_info(fi);
15786a31d2a9SEric Dumazet 	if (ofi) {
15791da177e4SLinus Torvalds 		fi->fib_dead = 1;
15801da177e4SLinus Torvalds 		free_fib_info(fi);
158179976892SYajun Deng 		refcount_inc(&ofi->fib_treeref);
15821da177e4SLinus Torvalds 		return ofi;
15831da177e4SLinus Torvalds 	}
15841da177e4SLinus Torvalds 
158528814cd1SDavid Ahern 	refcount_set(&fi->fib_treeref, 1);
15860029c0deSReshetova, Elena 	refcount_set(&fi->fib_clntref, 1);
1587832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
15880a6e6b3cSEric Dumazet 	fib_info_cnt++;
15891da177e4SLinus Torvalds 	hlist_add_head(&fi->fib_hash,
15901da177e4SLinus Torvalds 		       &fib_info_hash[fib_info_hashfn(fi)]);
15911da177e4SLinus Torvalds 	if (fi->fib_prefsrc) {
15921da177e4SLinus Torvalds 		struct hlist_head *head;
15931da177e4SLinus Torvalds 
159479eb15daSEric Dumazet 		head = fib_info_laddrhash_bucket(net, fi->fib_prefsrc);
15951da177e4SLinus Torvalds 		hlist_add_head(&fi->fib_lhash, head);
15961da177e4SLinus Torvalds 	}
15974c7e8084SDavid Ahern 	if (fi->nh) {
15984c7e8084SDavid Ahern 		list_add(&fi->nh_list, &nh->fi_list);
15994c7e8084SDavid Ahern 	} else {
16001da177e4SLinus Torvalds 		change_nexthops(fi) {
16011da177e4SLinus Torvalds 			struct hlist_head *head;
16021da177e4SLinus Torvalds 
1603b75ed8b1SDavid Ahern 			if (!nexthop_nh->fib_nh_dev)
16041da177e4SLinus Torvalds 				continue;
1605d07418afSEric Dumazet 			head = fib_info_devhash_bucket(nexthop_nh->fib_nh_dev);
160671fceff0SDavid S. Miller 			hlist_add_head(&nexthop_nh->nh_hash, head);
16071da177e4SLinus Torvalds 		} endfor_nexthops(fi)
16084c7e8084SDavid Ahern 	}
1609832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
16101da177e4SLinus Torvalds 	return fi;
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds err_inval:
16131da177e4SLinus Torvalds 	err = -EINVAL;
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds failure:
16161da177e4SLinus Torvalds 	if (fi) {
16171da177e4SLinus Torvalds 		fi->fib_dead = 1;
16181da177e4SLinus Torvalds 		free_fib_info(fi);
16191da177e4SLinus Torvalds 	}
16204e902c57SThomas Graf 
16214e902c57SThomas Graf 	return ERR_PTR(err);
16221da177e4SLinus Torvalds }
16231da177e4SLinus Torvalds 
1624c0a72077SDavid Ahern int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
16257bdf4de1SDonald Sharp 		     u8 rt_family, unsigned char *flags, bool skip_oif)
1626b0f60193SDavid Ahern {
1627c2364199SDavid Ahern 	if (nhc->nhc_flags & RTNH_F_DEAD)
1628b0f60193SDavid Ahern 		*flags |= RTNH_F_DEAD;
1629b0f60193SDavid Ahern 
1630c2364199SDavid Ahern 	if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
1631b0f60193SDavid Ahern 		*flags |= RTNH_F_LINKDOWN;
1632b0f60193SDavid Ahern 
1633b0f60193SDavid Ahern 		rcu_read_lock();
1634c2364199SDavid Ahern 		switch (nhc->nhc_family) {
1635c2364199SDavid Ahern 		case AF_INET:
1636c2364199SDavid Ahern 			if (ip_ignore_linkdown(nhc->nhc_dev))
1637b0f60193SDavid Ahern 				*flags |= RTNH_F_DEAD;
1638c2364199SDavid Ahern 			break;
1639c0a72077SDavid Ahern 		case AF_INET6:
1640c0a72077SDavid Ahern 			if (ip6_ignore_linkdown(nhc->nhc_dev))
1641c0a72077SDavid Ahern 				*flags |= RTNH_F_DEAD;
1642c0a72077SDavid Ahern 			break;
1643c2364199SDavid Ahern 		}
1644b0f60193SDavid Ahern 		rcu_read_unlock();
1645b0f60193SDavid Ahern 	}
1646b0f60193SDavid Ahern 
1647bdf00467SDavid Ahern 	switch (nhc->nhc_gw_family) {
1648c2364199SDavid Ahern 	case AF_INET:
1649c2364199SDavid Ahern 		if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1650b0f60193SDavid Ahern 			goto nla_put_failure;
1651c2364199SDavid Ahern 		break;
1652c0a72077SDavid Ahern 	case AF_INET6:
1653d1566268SDavid Ahern 		/* if gateway family does not match nexthop family
1654d1566268SDavid Ahern 		 * gateway is encoded as RTA_VIA
1655d1566268SDavid Ahern 		 */
16567bdf4de1SDonald Sharp 		if (rt_family != nhc->nhc_gw_family) {
1657d1566268SDavid Ahern 			int alen = sizeof(struct in6_addr);
1658d1566268SDavid Ahern 			struct nlattr *nla;
1659d1566268SDavid Ahern 			struct rtvia *via;
1660d1566268SDavid Ahern 
1661d1566268SDavid Ahern 			nla = nla_reserve(skb, RTA_VIA, alen + 2);
1662d1566268SDavid Ahern 			if (!nla)
1663c0a72077SDavid Ahern 				goto nla_put_failure;
1664d1566268SDavid Ahern 
1665d1566268SDavid Ahern 			via = nla_data(nla);
1666d1566268SDavid Ahern 			via->rtvia_family = AF_INET6;
1667d1566268SDavid Ahern 			memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
1668d1566268SDavid Ahern 		} else if (nla_put_in6_addr(skb, RTA_GATEWAY,
1669d1566268SDavid Ahern 					    &nhc->nhc_gw.ipv6) < 0) {
1670d1566268SDavid Ahern 			goto nla_put_failure;
1671d1566268SDavid Ahern 		}
1672c0a72077SDavid Ahern 		break;
1673c2364199SDavid Ahern 	}
1674b0f60193SDavid Ahern 
1675ca787e0bSIdo Schimmel 	*flags |= (nhc->nhc_flags &
1676ca787e0bSIdo Schimmel 		   (RTNH_F_ONLINK | RTNH_F_OFFLOAD | RTNH_F_TRAP));
1677b0f60193SDavid Ahern 
1678c2364199SDavid Ahern 	if (!skip_oif && nhc->nhc_dev &&
1679c2364199SDavid Ahern 	    nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
1680b0f60193SDavid Ahern 		goto nla_put_failure;
1681b0f60193SDavid Ahern 
1682c2364199SDavid Ahern 	if (nhc->nhc_lwtstate &&
1683ffa8ce54SDavid Ahern 	    lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1684ffa8ce54SDavid Ahern 				RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
1685b0f60193SDavid Ahern 		goto nla_put_failure;
1686b0f60193SDavid Ahern 
1687b0f60193SDavid Ahern 	return 0;
1688b0f60193SDavid Ahern 
1689b0f60193SDavid Ahern nla_put_failure:
1690b0f60193SDavid Ahern 	return -EMSGSIZE;
1691b0f60193SDavid Ahern }
1692c0a72077SDavid Ahern EXPORT_SYMBOL_GPL(fib_nexthop_info);
1693b0f60193SDavid Ahern 
1694c0a72077SDavid Ahern #if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1695c0a72077SDavid Ahern int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1696597aa16cSXiao Liang 		    int nh_weight, u8 rt_family, u32 nh_tclassid)
1697b0f60193SDavid Ahern {
1698c2364199SDavid Ahern 	const struct net_device *dev = nhc->nhc_dev;
1699b0f60193SDavid Ahern 	struct rtnexthop *rtnh;
1700ecc5663cSDavid Ahern 	unsigned char flags = 0;
1701b0f60193SDavid Ahern 
1702b0f60193SDavid Ahern 	rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1703b0f60193SDavid Ahern 	if (!rtnh)
1704b0f60193SDavid Ahern 		goto nla_put_failure;
1705b0f60193SDavid Ahern 
1706c2364199SDavid Ahern 	rtnh->rtnh_hops = nh_weight - 1;
1707b0f60193SDavid Ahern 	rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1708b0f60193SDavid Ahern 
17097bdf4de1SDonald Sharp 	if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
1710b0f60193SDavid Ahern 		goto nla_put_failure;
1711b0f60193SDavid Ahern 
1712b0f60193SDavid Ahern 	rtnh->rtnh_flags = flags;
1713b0f60193SDavid Ahern 
1714597aa16cSXiao Liang 	if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
1715597aa16cSXiao Liang 		goto nla_put_failure;
1716597aa16cSXiao Liang 
1717b0f60193SDavid Ahern 	/* length of rtnetlink header + attributes */
1718b0f60193SDavid Ahern 	rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1719b0f60193SDavid Ahern 
1720b0f60193SDavid Ahern 	return 0;
1721b0f60193SDavid Ahern 
1722b0f60193SDavid Ahern nla_put_failure:
1723b0f60193SDavid Ahern 	return -EMSGSIZE;
1724b0f60193SDavid Ahern }
1725c0a72077SDavid Ahern EXPORT_SYMBOL_GPL(fib_add_nexthop);
1726c2364199SDavid Ahern #endif
1727b0f60193SDavid Ahern 
1728c2364199SDavid Ahern #ifdef CONFIG_IP_ROUTE_MULTIPATH
1729b0f60193SDavid Ahern static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1730b0f60193SDavid Ahern {
1731b0f60193SDavid Ahern 	struct nlattr *mp;
1732b0f60193SDavid Ahern 
1733ae0be8deSMichal Kubecek 	mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
1734b0f60193SDavid Ahern 	if (!mp)
1735b0f60193SDavid Ahern 		goto nla_put_failure;
1736b0f60193SDavid Ahern 
17374c7e8084SDavid Ahern 	if (unlikely(fi->nh)) {
17387bdf4de1SDonald Sharp 		if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
17394c7e8084SDavid Ahern 			goto nla_put_failure;
17404c7e8084SDavid Ahern 		goto mp_end;
17414c7e8084SDavid Ahern 	}
17424c7e8084SDavid Ahern 
1743b0f60193SDavid Ahern 	for_nexthops(fi) {
1744597aa16cSXiao Liang 		u32 nh_tclassid = 0;
1745b0f60193SDavid Ahern #ifdef CONFIG_IP_ROUTE_CLASSID
1746597aa16cSXiao Liang 		nh_tclassid = nh->nh_tclassid;
1747b0f60193SDavid Ahern #endif
1748597aa16cSXiao Liang 		if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
1749597aa16cSXiao Liang 				    AF_INET, nh_tclassid) < 0)
1750597aa16cSXiao Liang 			goto nla_put_failure;
1751b0f60193SDavid Ahern 	} endfor_nexthops(fi);
1752b0f60193SDavid Ahern 
17534c7e8084SDavid Ahern mp_end:
1754b0f60193SDavid Ahern 	nla_nest_end(skb, mp);
1755b0f60193SDavid Ahern 
1756b0f60193SDavid Ahern 	return 0;
1757b0f60193SDavid Ahern 
1758b0f60193SDavid Ahern nla_put_failure:
1759b0f60193SDavid Ahern 	return -EMSGSIZE;
1760b0f60193SDavid Ahern }
1761b0f60193SDavid Ahern #else
1762b0f60193SDavid Ahern static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1763b0f60193SDavid Ahern {
1764b0f60193SDavid Ahern 	return 0;
1765b0f60193SDavid Ahern }
1766b0f60193SDavid Ahern #endif
1767b0f60193SDavid Ahern 
176815e47304SEric W. Biederman int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
176908554789SAmit Cohen 		  const struct fib_rt_info *fri, unsigned int flags)
17701da177e4SLinus Torvalds {
17711e301fd0SIdo Schimmel 	unsigned int nhs = fib_info_num_path(fri->fi);
17721e301fd0SIdo Schimmel 	struct fib_info *fi = fri->fi;
17731e301fd0SIdo Schimmel 	u32 tb_id = fri->tb_id;
17741da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
1775be403ea1SThomas Graf 	struct rtmsg *rtm;
17761da177e4SLinus Torvalds 
177715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
177851456b29SIan Morris 	if (!nlh)
177926932566SPatrick McHardy 		return -EMSGSIZE;
1780be403ea1SThomas Graf 
1781be403ea1SThomas Graf 	rtm = nlmsg_data(nlh);
17821da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET;
17831e301fd0SIdo Schimmel 	rtm->rtm_dst_len = fri->dst_len;
17841da177e4SLinus Torvalds 	rtm->rtm_src_len = 0;
1785888ade8fSGuillaume Nault 	rtm->rtm_tos = inet_dscp_to_dsfield(fri->dscp);
1786709772e6SKrzysztof Piotr Oledzki 	if (tb_id < 256)
17871da177e4SLinus Torvalds 		rtm->rtm_table = tb_id;
1788709772e6SKrzysztof Piotr Oledzki 	else
1789709772e6SKrzysztof Piotr Oledzki 		rtm->rtm_table = RT_TABLE_COMPAT;
1790f3756b79SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, tb_id))
1791f3756b79SDavid S. Miller 		goto nla_put_failure;
17921e301fd0SIdo Schimmel 	rtm->rtm_type = fri->type;
17931da177e4SLinus Torvalds 	rtm->rtm_flags = fi->fib_flags;
179437e826c5SDavid S. Miller 	rtm->rtm_scope = fi->fib_scope;
17951da177e4SLinus Torvalds 	rtm->rtm_protocol = fi->fib_protocol;
1796be403ea1SThomas Graf 
1797f3756b79SDavid S. Miller 	if (rtm->rtm_dst_len &&
17981e301fd0SIdo Schimmel 	    nla_put_in_addr(skb, RTA_DST, fri->dst))
1799f3756b79SDavid S. Miller 		goto nla_put_failure;
1800f3756b79SDavid S. Miller 	if (fi->fib_priority &&
1801f3756b79SDavid S. Miller 	    nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1802f3756b79SDavid S. Miller 		goto nla_put_failure;
18033fb07dafSEric Dumazet 	if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
1804be403ea1SThomas Graf 		goto nla_put_failure;
1805be403ea1SThomas Graf 
1806f3756b79SDavid S. Miller 	if (fi->fib_prefsrc &&
1807930345eaSJiri Benc 	    nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
1808f3756b79SDavid S. Miller 		goto nla_put_failure;
18094c7e8084SDavid Ahern 
18104c7e8084SDavid Ahern 	if (fi->nh) {
18114c7e8084SDavid Ahern 		if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
18124c7e8084SDavid Ahern 			goto nla_put_failure;
18134c7e8084SDavid Ahern 		if (nexthop_is_blackhole(fi->nh))
18144c7e8084SDavid Ahern 			rtm->rtm_type = RTN_BLACKHOLE;
1815bdf00bf2SKuniyuki Iwashima 		if (!READ_ONCE(fi->fib_net->ipv4.sysctl_nexthop_compat_mode))
18164f80116dSRoopa Prabhu 			goto offload;
18174c7e8084SDavid Ahern 	}
18184c7e8084SDavid Ahern 
18195481d73fSDavid Ahern 	if (nhs == 1) {
1820dcb1ecb5SDavid Ahern 		const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
1821ecc5663cSDavid Ahern 		unsigned char flags = 0;
1822be403ea1SThomas Graf 
18237bdf4de1SDonald Sharp 		if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
1824be403ea1SThomas Graf 			goto nla_put_failure;
18251da177e4SLinus Torvalds 
1826b0f60193SDavid Ahern 		rtm->rtm_flags = flags;
1827c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1828dcb1ecb5SDavid Ahern 		if (nhc->nhc_family == AF_INET) {
1829dcb1ecb5SDavid Ahern 			struct fib_nh *nh;
1830dcb1ecb5SDavid Ahern 
1831dcb1ecb5SDavid Ahern 			nh = container_of(nhc, struct fib_nh, nh_common);
1832f3756b79SDavid S. Miller 			if (nh->nh_tclassid &&
1833f3756b79SDavid S. Miller 			    nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1834f3756b79SDavid S. Miller 				goto nla_put_failure;
1835dcb1ecb5SDavid Ahern 		}
18368265abc0SPatrick McHardy #endif
1837b0f60193SDavid Ahern 	} else {
1838b0f60193SDavid Ahern 		if (fib_add_multipath(skb, fi) < 0)
1839ea7a8085SDavid Ahern 			goto nla_put_failure;
18401da177e4SLinus Torvalds 	}
1841b0f60193SDavid Ahern 
18424f80116dSRoopa Prabhu offload:
184390b93f1bSIdo Schimmel 	if (fri->offload)
184490b93f1bSIdo Schimmel 		rtm->rtm_flags |= RTM_F_OFFLOAD;
184590b93f1bSIdo Schimmel 	if (fri->trap)
184690b93f1bSIdo Schimmel 		rtm->rtm_flags |= RTM_F_TRAP;
184736c5100eSAmit Cohen 	if (fri->offload_failed)
184836c5100eSAmit Cohen 		rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
184990b93f1bSIdo Schimmel 
1850053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1851053c095aSJohannes Berg 	return 0;
18521da177e4SLinus Torvalds 
1853be403ea1SThomas Graf nla_put_failure:
185426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
185526932566SPatrick McHardy 	return -EMSGSIZE;
18561da177e4SLinus Torvalds }
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds /*
18596a31d2a9SEric Dumazet  * Update FIB if:
18606a31d2a9SEric Dumazet  * - local address disappeared -> we must delete all the entries
18616a31d2a9SEric Dumazet  *   referring to it.
18626a31d2a9SEric Dumazet  * - device went down -> we must shutdown all nexthops going via it.
18631da177e4SLinus Torvalds  */
18645a56a0b3SMark Tomlinson int fib_sync_down_addr(struct net_device *dev, __be32 local)
18651da177e4SLinus Torvalds {
1866e0a31262SDavid Ahern 	int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
18675a56a0b3SMark Tomlinson 	struct net *net = dev_net(dev);
186879eb15daSEric Dumazet 	struct hlist_head *head;
18691da177e4SLinus Torvalds 	struct fib_info *fi;
187079eb15daSEric Dumazet 	int ret = 0;
18711da177e4SLinus Torvalds 
187251456b29SIan Morris 	if (!fib_info_laddrhash || local == 0)
187385326fa5SDenis V. Lunev 		return 0;
187485326fa5SDenis V. Lunev 
187579eb15daSEric Dumazet 	head = fib_info_laddrhash_bucket(net, local);
1876b67bfe0dSSasha Levin 	hlist_for_each_entry(fi, head, fib_lhash) {
18775a56a0b3SMark Tomlinson 		if (!net_eq(fi->fib_net, net) ||
18785a56a0b3SMark Tomlinson 		    fi->fib_tb_id != tb_id)
18794814bdbdSDenis V. Lunev 			continue;
18801da177e4SLinus Torvalds 		if (fi->fib_prefsrc == local) {
18811da177e4SLinus Torvalds 			fi->fib_flags |= RTNH_F_DEAD;
18821da177e4SLinus Torvalds 			ret++;
18831da177e4SLinus Torvalds 		}
18841da177e4SLinus Torvalds 	}
188585326fa5SDenis V. Lunev 	return ret;
18861da177e4SLinus Torvalds }
18871da177e4SLinus Torvalds 
1888b75ed8b1SDavid Ahern static int call_fib_nh_notifiers(struct fib_nh *nh,
1889982acb97SIdo Schimmel 				 enum fib_event_type event_type)
1890982acb97SIdo Schimmel {
1891b75ed8b1SDavid Ahern 	bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
1892982acb97SIdo Schimmel 	struct fib_nh_notifier_info info = {
1893b75ed8b1SDavid Ahern 		.fib_nh = nh,
1894982acb97SIdo Schimmel 	};
1895982acb97SIdo Schimmel 
1896982acb97SIdo Schimmel 	switch (event_type) {
1897982acb97SIdo Schimmel 	case FIB_EVENT_NH_ADD:
1898b75ed8b1SDavid Ahern 		if (nh->fib_nh_flags & RTNH_F_DEAD)
1899982acb97SIdo Schimmel 			break;
1900b75ed8b1SDavid Ahern 		if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
1901982acb97SIdo Schimmel 			break;
1902b75ed8b1SDavid Ahern 		return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
1903982acb97SIdo Schimmel 					   &info.info);
1904982acb97SIdo Schimmel 	case FIB_EVENT_NH_DEL:
1905b75ed8b1SDavid Ahern 		if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1906b75ed8b1SDavid Ahern 		    (nh->fib_nh_flags & RTNH_F_DEAD))
1907b75ed8b1SDavid Ahern 			return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
1908982acb97SIdo Schimmel 						   event_type, &info.info);
190979121184SGustavo A. R. Silva 		break;
1910982acb97SIdo Schimmel 	default:
1911982acb97SIdo Schimmel 		break;
1912982acb97SIdo Schimmel 	}
1913982acb97SIdo Schimmel 
1914982acb97SIdo Schimmel 	return NOTIFY_DONE;
1915982acb97SIdo Schimmel }
1916982acb97SIdo Schimmel 
1917af7d6cceSSabrina Dubroca /* Update the PMTU of exceptions when:
1918af7d6cceSSabrina Dubroca  * - the new MTU of the first hop becomes smaller than the PMTU
1919af7d6cceSSabrina Dubroca  * - the old MTU was the same as the PMTU, and it limited discovery of
1920af7d6cceSSabrina Dubroca  *   larger MTUs on the path. With that limit raised, we can now
1921af7d6cceSSabrina Dubroca  *   discover larger MTUs
1922af7d6cceSSabrina Dubroca  * A special case is locked exceptions, for which the PMTU is smaller
1923af7d6cceSSabrina Dubroca  * than the minimal accepted PMTU:
1924af7d6cceSSabrina Dubroca  * - if the new MTU is greater than the PMTU, don't make any change
1925af7d6cceSSabrina Dubroca  * - otherwise, unlock and set PMTU
1926af7d6cceSSabrina Dubroca  */
192706c77c3eSDavid Ahern void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
1928af7d6cceSSabrina Dubroca {
1929af7d6cceSSabrina Dubroca 	struct fnhe_hash_bucket *bucket;
1930af7d6cceSSabrina Dubroca 	int i;
1931af7d6cceSSabrina Dubroca 
1932a5995e71SDavid Ahern 	bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
1933af7d6cceSSabrina Dubroca 	if (!bucket)
1934af7d6cceSSabrina Dubroca 		return;
1935af7d6cceSSabrina Dubroca 
1936af7d6cceSSabrina Dubroca 	for (i = 0; i < FNHE_HASH_SIZE; i++) {
1937af7d6cceSSabrina Dubroca 		struct fib_nh_exception *fnhe;
1938af7d6cceSSabrina Dubroca 
1939af7d6cceSSabrina Dubroca 		for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1940af7d6cceSSabrina Dubroca 		     fnhe;
1941af7d6cceSSabrina Dubroca 		     fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1942af7d6cceSSabrina Dubroca 			if (fnhe->fnhe_mtu_locked) {
1943af7d6cceSSabrina Dubroca 				if (new <= fnhe->fnhe_pmtu) {
1944af7d6cceSSabrina Dubroca 					fnhe->fnhe_pmtu = new;
1945af7d6cceSSabrina Dubroca 					fnhe->fnhe_mtu_locked = false;
1946af7d6cceSSabrina Dubroca 				}
1947af7d6cceSSabrina Dubroca 			} else if (new < fnhe->fnhe_pmtu ||
1948af7d6cceSSabrina Dubroca 				   orig == fnhe->fnhe_pmtu) {
1949af7d6cceSSabrina Dubroca 				fnhe->fnhe_pmtu = new;
1950af7d6cceSSabrina Dubroca 			}
1951af7d6cceSSabrina Dubroca 		}
1952af7d6cceSSabrina Dubroca 	}
1953af7d6cceSSabrina Dubroca }
1954af7d6cceSSabrina Dubroca 
1955af7d6cceSSabrina Dubroca void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1956af7d6cceSSabrina Dubroca {
1957d07418afSEric Dumazet 	struct hlist_head *head = fib_info_devhash_bucket(dev);
1958af7d6cceSSabrina Dubroca 	struct fib_nh *nh;
1959af7d6cceSSabrina Dubroca 
1960af7d6cceSSabrina Dubroca 	hlist_for_each_entry(nh, head, nh_hash) {
1961b75ed8b1SDavid Ahern 		if (nh->fib_nh_dev == dev)
196206c77c3eSDavid Ahern 			fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
1963af7d6cceSSabrina Dubroca 	}
1964af7d6cceSSabrina Dubroca }
1965af7d6cceSSabrina Dubroca 
19664f823defSJulian Anastasov /* Event              force Flags           Description
19674f823defSJulian Anastasov  * NETDEV_CHANGE      0     LINKDOWN        Carrier OFF, not for scope host
19684f823defSJulian Anastasov  * NETDEV_DOWN        0     LINKDOWN|DEAD   Link down, not for scope host
19694f823defSJulian Anastasov  * NETDEV_DOWN        1     LINKDOWN|DEAD   Last address removed
19704f823defSJulian Anastasov  * NETDEV_UNREGISTER  1     LINKDOWN|DEAD   Device removed
19714c7e8084SDavid Ahern  *
19724c7e8084SDavid Ahern  * only used when fib_nh is built into fib_info
19734f823defSJulian Anastasov  */
19744f823defSJulian Anastasov int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
197585326fa5SDenis V. Lunev {
1976d07418afSEric Dumazet 	struct hlist_head *head = fib_info_devhash_bucket(dev);
19771da177e4SLinus Torvalds 	struct fib_info *prev_fi = NULL;
1978d07418afSEric Dumazet 	int scope = RT_SCOPE_NOWHERE;
19791da177e4SLinus Torvalds 	struct fib_nh *nh;
1980d07418afSEric Dumazet 	int ret = 0;
19811da177e4SLinus Torvalds 
19824f823defSJulian Anastasov 	if (force)
198385326fa5SDenis V. Lunev 		scope = -1;
198485326fa5SDenis V. Lunev 
1985b67bfe0dSSasha Levin 	hlist_for_each_entry(nh, head, nh_hash) {
19861da177e4SLinus Torvalds 		struct fib_info *fi = nh->nh_parent;
19871da177e4SLinus Torvalds 		int dead;
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds 		BUG_ON(!fi->fib_nhs);
1990b75ed8b1SDavid Ahern 		if (nh->fib_nh_dev != dev || fi == prev_fi)
19911da177e4SLinus Torvalds 			continue;
19921da177e4SLinus Torvalds 		prev_fi = fi;
19931da177e4SLinus Torvalds 		dead = 0;
19941da177e4SLinus Torvalds 		change_nexthops(fi) {
1995b75ed8b1SDavid Ahern 			if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
19961da177e4SLinus Torvalds 				dead++;
1997b75ed8b1SDavid Ahern 			else if (nexthop_nh->fib_nh_dev == dev &&
1998b75ed8b1SDavid Ahern 				 nexthop_nh->fib_nh_scope != scope) {
19998a3d0316SAndy Gospodarek 				switch (event) {
20008a3d0316SAndy Gospodarek 				case NETDEV_DOWN:
20018a3d0316SAndy Gospodarek 				case NETDEV_UNREGISTER:
2002b75ed8b1SDavid Ahern 					nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
2003a8eceea8SJoe Perches 					fallthrough;
20048a3d0316SAndy Gospodarek 				case NETDEV_CHANGE:
2005b75ed8b1SDavid Ahern 					nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
20068a3d0316SAndy Gospodarek 					break;
20078a3d0316SAndy Gospodarek 				}
2008982acb97SIdo Schimmel 				call_fib_nh_notifiers(nexthop_nh,
2009982acb97SIdo Schimmel 						      FIB_EVENT_NH_DEL);
20101da177e4SLinus Torvalds 				dead++;
20111da177e4SLinus Torvalds 			}
20121da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
20138a3d0316SAndy Gospodarek 			if (event == NETDEV_UNREGISTER &&
2014b75ed8b1SDavid Ahern 			    nexthop_nh->fib_nh_dev == dev) {
20151da177e4SLinus Torvalds 				dead = fi->fib_nhs;
20161da177e4SLinus Torvalds 				break;
20171da177e4SLinus Torvalds 			}
20181da177e4SLinus Torvalds #endif
20191da177e4SLinus Torvalds 		} endfor_nexthops(fi)
20201da177e4SLinus Torvalds 		if (dead == fi->fib_nhs) {
20218a3d0316SAndy Gospodarek 			switch (event) {
20228a3d0316SAndy Gospodarek 			case NETDEV_DOWN:
20238a3d0316SAndy Gospodarek 			case NETDEV_UNREGISTER:
20241da177e4SLinus Torvalds 				fi->fib_flags |= RTNH_F_DEAD;
2025a8eceea8SJoe Perches 				fallthrough;
20268a3d0316SAndy Gospodarek 			case NETDEV_CHANGE:
20278a3d0316SAndy Gospodarek 				fi->fib_flags |= RTNH_F_LINKDOWN;
20288a3d0316SAndy Gospodarek 				break;
20298a3d0316SAndy Gospodarek 			}
20301da177e4SLinus Torvalds 			ret++;
20311da177e4SLinus Torvalds 		}
20320e884c78SPeter Nørlund 
20330e884c78SPeter Nørlund 		fib_rebalance(fi);
20341da177e4SLinus Torvalds 	}
20351da177e4SLinus Torvalds 
20361da177e4SLinus Torvalds 	return ret;
20371da177e4SLinus Torvalds }
20381da177e4SLinus Torvalds 
20390c838ff1SDavid S. Miller /* Must be invoked inside of an RCU protected region.  */
2040c7b371e3SDavid Ahern static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
20410c838ff1SDavid S. Miller {
20420c838ff1SDavid S. Miller 	struct fib_info *fi = NULL, *last_resort = NULL;
204356315f9eSAlexander Duyck 	struct hlist_head *fa_head = res->fa_head;
20440c838ff1SDavid S. Miller 	struct fib_table *tb = res->table;
204518a912e9SJulian Anastasov 	u8 slen = 32 - res->prefixlen;
20460c838ff1SDavid S. Miller 	int order = -1, last_idx = -1;
20472392debcSJulian Anastasov 	struct fib_alias *fa, *fa1 = NULL;
20482392debcSJulian Anastasov 	u32 last_prio = res->fi->fib_priority;
204932ccf110SGuillaume Nault 	dscp_t last_dscp = 0;
20500c838ff1SDavid S. Miller 
205156315f9eSAlexander Duyck 	hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
20520c838ff1SDavid S. Miller 		struct fib_info *next_fi = fa->fa_info;
20537c74b0beSDavid Ahern 		struct fib_nh_common *nhc;
20540c838ff1SDavid S. Miller 
205518a912e9SJulian Anastasov 		if (fa->fa_slen != slen)
205618a912e9SJulian Anastasov 			continue;
205732ccf110SGuillaume Nault 		if (fa->fa_dscp &&
205832ccf110SGuillaume Nault 		    fa->fa_dscp != inet_dsfield_to_dscp(flp->flowi4_tos))
20592392debcSJulian Anastasov 			continue;
206018a912e9SJulian Anastasov 		if (fa->tb_id != tb->tb_id)
206118a912e9SJulian Anastasov 			continue;
20622392debcSJulian Anastasov 		if (next_fi->fib_priority > last_prio &&
206332ccf110SGuillaume Nault 		    fa->fa_dscp == last_dscp) {
206432ccf110SGuillaume Nault 			if (last_dscp)
20652392debcSJulian Anastasov 				continue;
20662392debcSJulian Anastasov 			break;
20672392debcSJulian Anastasov 		}
20682392debcSJulian Anastasov 		if (next_fi->fib_flags & RTNH_F_DEAD)
20692392debcSJulian Anastasov 			continue;
207032ccf110SGuillaume Nault 		last_dscp = fa->fa_dscp;
20712392debcSJulian Anastasov 		last_prio = next_fi->fib_priority;
20722392debcSJulian Anastasov 
207337e826c5SDavid S. Miller 		if (next_fi->fib_scope != res->scope ||
20740c838ff1SDavid S. Miller 		    fa->fa_type != RTN_UNICAST)
20750c838ff1SDavid S. Miller 			continue;
20765481d73fSDavid Ahern 
20777c74b0beSDavid Ahern 		nhc = fib_info_nhc(next_fi, 0);
20787c74b0beSDavid Ahern 		if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
20790c838ff1SDavid S. Miller 			continue;
20800c838ff1SDavid S. Miller 
20810c838ff1SDavid S. Miller 		fib_alias_accessed(fa);
20820c838ff1SDavid S. Miller 
208351456b29SIan Morris 		if (!fi) {
20840c838ff1SDavid S. Miller 			if (next_fi != res->fi)
20850c838ff1SDavid S. Miller 				break;
20862392debcSJulian Anastasov 			fa1 = fa;
20870c838ff1SDavid S. Miller 		} else if (!fib_detect_death(fi, order, &last_resort,
20882392debcSJulian Anastasov 					     &last_idx, fa1->fa_default)) {
20890c838ff1SDavid S. Miller 			fib_result_assign(res, fi);
20902392debcSJulian Anastasov 			fa1->fa_default = order;
20910c838ff1SDavid S. Miller 			goto out;
20920c838ff1SDavid S. Miller 		}
20930c838ff1SDavid S. Miller 		fi = next_fi;
20940c838ff1SDavid S. Miller 		order++;
20950c838ff1SDavid S. Miller 	}
20960c838ff1SDavid S. Miller 
209751456b29SIan Morris 	if (order <= 0 || !fi) {
20982392debcSJulian Anastasov 		if (fa1)
20992392debcSJulian Anastasov 			fa1->fa_default = -1;
21000c838ff1SDavid S. Miller 		goto out;
21010c838ff1SDavid S. Miller 	}
21020c838ff1SDavid S. Miller 
21030c838ff1SDavid S. Miller 	if (!fib_detect_death(fi, order, &last_resort, &last_idx,
21042392debcSJulian Anastasov 			      fa1->fa_default)) {
21050c838ff1SDavid S. Miller 		fib_result_assign(res, fi);
21062392debcSJulian Anastasov 		fa1->fa_default = order;
21070c838ff1SDavid S. Miller 		goto out;
21080c838ff1SDavid S. Miller 	}
21090c838ff1SDavid S. Miller 
21100c838ff1SDavid S. Miller 	if (last_idx >= 0)
21110c838ff1SDavid S. Miller 		fib_result_assign(res, last_resort);
21122392debcSJulian Anastasov 	fa1->fa_default = last_idx;
21130c838ff1SDavid S. Miller out:
211431d40937SEric Dumazet 	return;
21150c838ff1SDavid S. Miller }
21160c838ff1SDavid S. Miller 
21171da177e4SLinus Torvalds /*
21186a31d2a9SEric Dumazet  * Dead device goes up. We wake up dead nexthops.
21196a31d2a9SEric Dumazet  * It takes sense only on multipath routes.
21204c7e8084SDavid Ahern  *
21214c7e8084SDavid Ahern  * only used when fib_nh is built into fib_info
21221da177e4SLinus Torvalds  */
2123ecc5663cSDavid Ahern int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
21241da177e4SLinus Torvalds {
21251da177e4SLinus Torvalds 	struct fib_info *prev_fi;
21261da177e4SLinus Torvalds 	struct hlist_head *head;
21271da177e4SLinus Torvalds 	struct fib_nh *nh;
21281da177e4SLinus Torvalds 	int ret;
21291da177e4SLinus Torvalds 
21301da177e4SLinus Torvalds 	if (!(dev->flags & IFF_UP))
21311da177e4SLinus Torvalds 		return 0;
21321da177e4SLinus Torvalds 
2133c9b3292eSJulian Anastasov 	if (nh_flags & RTNH_F_DEAD) {
2134c9b3292eSJulian Anastasov 		unsigned int flags = dev_get_flags(dev);
2135c9b3292eSJulian Anastasov 
2136c9b3292eSJulian Anastasov 		if (flags & (IFF_RUNNING | IFF_LOWER_UP))
2137c9b3292eSJulian Anastasov 			nh_flags |= RTNH_F_LINKDOWN;
2138c9b3292eSJulian Anastasov 	}
2139c9b3292eSJulian Anastasov 
21401da177e4SLinus Torvalds 	prev_fi = NULL;
2141d07418afSEric Dumazet 	head = fib_info_devhash_bucket(dev);
21421da177e4SLinus Torvalds 	ret = 0;
21431da177e4SLinus Torvalds 
2144b67bfe0dSSasha Levin 	hlist_for_each_entry(nh, head, nh_hash) {
21451da177e4SLinus Torvalds 		struct fib_info *fi = nh->nh_parent;
21461da177e4SLinus Torvalds 		int alive;
21471da177e4SLinus Torvalds 
21481da177e4SLinus Torvalds 		BUG_ON(!fi->fib_nhs);
2149b75ed8b1SDavid Ahern 		if (nh->fib_nh_dev != dev || fi == prev_fi)
21501da177e4SLinus Torvalds 			continue;
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds 		prev_fi = fi;
21531da177e4SLinus Torvalds 		alive = 0;
21541da177e4SLinus Torvalds 		change_nexthops(fi) {
2155b75ed8b1SDavid Ahern 			if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
21561da177e4SLinus Torvalds 				alive++;
21571da177e4SLinus Torvalds 				continue;
21581da177e4SLinus Torvalds 			}
2159b75ed8b1SDavid Ahern 			if (!nexthop_nh->fib_nh_dev ||
2160b75ed8b1SDavid Ahern 			    !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
21611da177e4SLinus Torvalds 				continue;
2162b75ed8b1SDavid Ahern 			if (nexthop_nh->fib_nh_dev != dev ||
216371fceff0SDavid S. Miller 			    !__in_dev_get_rtnl(dev))
21641da177e4SLinus Torvalds 				continue;
21651da177e4SLinus Torvalds 			alive++;
2166b75ed8b1SDavid Ahern 			nexthop_nh->fib_nh_flags &= ~nh_flags;
2167982acb97SIdo Schimmel 			call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
21681da177e4SLinus Torvalds 		} endfor_nexthops(fi)
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds 		if (alive > 0) {
21718a3d0316SAndy Gospodarek 			fi->fib_flags &= ~nh_flags;
21721da177e4SLinus Torvalds 			ret++;
21731da177e4SLinus Torvalds 		}
21740e884c78SPeter Nørlund 
21750e884c78SPeter Nørlund 		fib_rebalance(fi);
21761da177e4SLinus Torvalds 	}
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds 	return ret;
21791da177e4SLinus Torvalds }
21801da177e4SLinus Torvalds 
21818a3d0316SAndy Gospodarek #ifdef CONFIG_IP_ROUTE_MULTIPATH
2182a6db4494SDavid Ahern static bool fib_good_nh(const struct fib_nh *nh)
2183a6db4494SDavid Ahern {
2184a6db4494SDavid Ahern 	int state = NUD_REACHABLE;
2185a6db4494SDavid Ahern 
2186b75ed8b1SDavid Ahern 	if (nh->fib_nh_scope == RT_SCOPE_LINK) {
2187a6db4494SDavid Ahern 		struct neighbour *n;
2188a6db4494SDavid Ahern 
2189a6db4494SDavid Ahern 		rcu_read_lock_bh();
2190a6db4494SDavid Ahern 
21911a38c43dSDavid Ahern 		if (likely(nh->fib_nh_gw_family == AF_INET))
2192b75ed8b1SDavid Ahern 			n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
2193b75ed8b1SDavid Ahern 						   (__force u32)nh->fib_nh_gw4);
21941a38c43dSDavid Ahern 		else if (nh->fib_nh_gw_family == AF_INET6)
21951a38c43dSDavid Ahern 			n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
21961a38c43dSDavid Ahern 							   &nh->fib_nh_gw6);
21971a38c43dSDavid Ahern 		else
21981a38c43dSDavid Ahern 			n = NULL;
2199a6db4494SDavid Ahern 		if (n)
2200a6db4494SDavid Ahern 			state = n->nud_state;
2201a6db4494SDavid Ahern 
2202a6db4494SDavid Ahern 		rcu_read_unlock_bh();
2203a6db4494SDavid Ahern 	}
2204a6db4494SDavid Ahern 
2205a6db4494SDavid Ahern 	return !!(state & NUD_VALID);
2206a6db4494SDavid Ahern }
22078a3d0316SAndy Gospodarek 
22080e884c78SPeter Nørlund void fib_select_multipath(struct fib_result *res, int hash)
22091da177e4SLinus Torvalds {
22101da177e4SLinus Torvalds 	struct fib_info *fi = res->fi;
2211a6db4494SDavid Ahern 	struct net *net = fi->fib_net;
2212a6db4494SDavid Ahern 	bool first = false;
22131da177e4SLinus Torvalds 
22144c7e8084SDavid Ahern 	if (unlikely(res->fi->nh)) {
22154c7e8084SDavid Ahern 		nexthop_path_fib_result(res, hash);
22164c7e8084SDavid Ahern 		return;
22174c7e8084SDavid Ahern 	}
22184c7e8084SDavid Ahern 
2219eba618abSDavid Ahern 	change_nexthops(fi) {
222087507bcbSKuniyuki Iwashima 		if (READ_ONCE(net->ipv4.sysctl_fib_multipath_use_neigh)) {
2221eba618abSDavid Ahern 			if (!fib_good_nh(nexthop_nh))
22220eeb075fSAndy Gospodarek 				continue;
2223a6db4494SDavid Ahern 			if (!first) {
2224a6db4494SDavid Ahern 				res->nh_sel = nhsel;
2225eba618abSDavid Ahern 				res->nhc = &nexthop_nh->nh_common;
2226a6db4494SDavid Ahern 				first = true;
2227a6db4494SDavid Ahern 			}
22286174a30dSXin Long 		}
22296174a30dSXin Long 
2230eba618abSDavid Ahern 		if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
22316174a30dSXin Long 			continue;
22326174a30dSXin Long 
22336174a30dSXin Long 		res->nh_sel = nhsel;
2234eba618abSDavid Ahern 		res->nhc = &nexthop_nh->nh_common;
22356174a30dSXin Long 		return;
22361da177e4SLinus Torvalds 	} endfor_nexthops(fi);
22371da177e4SLinus Torvalds }
22381da177e4SLinus Torvalds #endif
22393ce58d84SDavid Ahern 
22403ce58d84SDavid Ahern void fib_select_path(struct net *net, struct fib_result *res,
2241bf4e0a3dSNikolay Aleksandrov 		     struct flowi4 *fl4, const struct sk_buff *skb)
22423ce58d84SDavid Ahern {
224340867d74SDavid Ahern 	if (fl4->flowi4_oif)
22440d876f2cSDavid Ahern 		goto check_saddr;
22457a18c5b9SDavid Ahern 
22463ce58d84SDavid Ahern #ifdef CONFIG_IP_ROUTE_MULTIPATH
22475481d73fSDavid Ahern 	if (fib_info_num_path(res->fi) > 1) {
22487efc0b6bSDavid Ahern 		int h = fib_multipath_hash(net, fl4, skb, NULL);
22499920e48bSPaolo Abeni 
2250bf4e0a3dSNikolay Aleksandrov 		fib_select_multipath(res, h);
22513ce58d84SDavid Ahern 	}
22523ce58d84SDavid Ahern 	else
22533ce58d84SDavid Ahern #endif
22543ce58d84SDavid Ahern 	if (!res->prefixlen &&
22553ce58d84SDavid Ahern 	    res->table->tb_num_default > 1 &&
22560d876f2cSDavid Ahern 	    res->type == RTN_UNICAST)
22573ce58d84SDavid Ahern 		fib_select_default(fl4, res);
22583ce58d84SDavid Ahern 
22590d876f2cSDavid Ahern check_saddr:
22603ce58d84SDavid Ahern 	if (!fl4->saddr)
2261eba618abSDavid Ahern 		fl4->saddr = fib_result_prefsrc(net, res);
22623ce58d84SDavid Ahern }
2263