xref: /linux/net/ipv4/fib_semantics.c (revision 36c5100e859d93b3436ae24810612b05addb1e89)
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>
321da177e4SLinus Torvalds 
3314c85021SArnaldo Carvalho de Melo #include <net/arp.h>
341da177e4SLinus Torvalds #include <net/ip.h>
351da177e4SLinus Torvalds #include <net/protocol.h>
361da177e4SLinus Torvalds #include <net/route.h>
371da177e4SLinus Torvalds #include <net/tcp.h>
381da177e4SLinus Torvalds #include <net/sock.h>
391da177e4SLinus Torvalds #include <net/ip_fib.h>
40717a8f5bSDavid Ahern #include <net/ip6_fib.h>
415481d73fSDavid Ahern #include <net/nexthop.h>
42f21c7bc5SThomas Graf #include <net/netlink.h>
433c618c1dSDavid Ahern #include <net/rtnh.h>
44571e7226SRoopa Prabhu #include <net/lwtunnel.h>
4504b1d4e5SIdo Schimmel #include <net/fib_notifier.h>
46c0a72077SDavid Ahern #include <net/addrconf.h>
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #include "fib_lookup.h"
491da177e4SLinus Torvalds 
50832b4c5eSStephen Hemminger static DEFINE_SPINLOCK(fib_info_lock);
511da177e4SLinus Torvalds static struct hlist_head *fib_info_hash;
521da177e4SLinus Torvalds static struct hlist_head *fib_info_laddrhash;
53123b9731SDavid S. Miller static unsigned int fib_info_hash_size;
541da177e4SLinus Torvalds static unsigned int fib_info_cnt;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #define DEVINDEX_HASHBITS 8
571da177e4SLinus Torvalds #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
581da177e4SLinus Torvalds static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
591da177e4SLinus Torvalds 
60dcb1ecb5SDavid Ahern /* for_nexthops and change_nexthops only used when nexthop object
61dcb1ecb5SDavid Ahern  * is not set in a fib_info. The logic within can reference fib_nh.
62dcb1ecb5SDavid Ahern  */
631da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
641da177e4SLinus Torvalds 
656a31d2a9SEric Dumazet #define for_nexthops(fi) {						\
666a31d2a9SEric Dumazet 	int nhsel; const struct fib_nh *nh;				\
676a31d2a9SEric Dumazet 	for (nhsel = 0, nh = (fi)->fib_nh;				\
685481d73fSDavid Ahern 	     nhsel < fib_info_num_path((fi));				\
696a31d2a9SEric Dumazet 	     nh++, nhsel++)
701da177e4SLinus Torvalds 
716a31d2a9SEric Dumazet #define change_nexthops(fi) {						\
726a31d2a9SEric Dumazet 	int nhsel; struct fib_nh *nexthop_nh;				\
736a31d2a9SEric Dumazet 	for (nhsel = 0,	nexthop_nh = (struct fib_nh *)((fi)->fib_nh);	\
745481d73fSDavid Ahern 	     nhsel < fib_info_num_path((fi));				\
756a31d2a9SEric Dumazet 	     nexthop_nh++, nhsel++)
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds #else /* CONFIG_IP_ROUTE_MULTIPATH */
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /* Hope, that gcc will optimize it to get rid of dummy loop */
801da177e4SLinus Torvalds 
816a31d2a9SEric Dumazet #define for_nexthops(fi) {						\
826a31d2a9SEric Dumazet 	int nhsel; const struct fib_nh *nh = (fi)->fib_nh;		\
831da177e4SLinus Torvalds 	for (nhsel = 0; nhsel < 1; nhsel++)
841da177e4SLinus Torvalds 
856a31d2a9SEric Dumazet #define change_nexthops(fi) {						\
866a31d2a9SEric Dumazet 	int nhsel;							\
876a31d2a9SEric Dumazet 	struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh);	\
881da177e4SLinus Torvalds 	for (nhsel = 0; nhsel < 1; nhsel++)
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds #endif /* CONFIG_IP_ROUTE_MULTIPATH */
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds #define endfor_nexthops(fi) }
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 
953be0686bSDavid S. Miller const struct fib_prop fib_props[RTN_MAX + 1] = {
966a31d2a9SEric Dumazet 	[RTN_UNSPEC] = {
971da177e4SLinus Torvalds 		.error	= 0,
981da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
996a31d2a9SEric Dumazet 	},
1006a31d2a9SEric Dumazet 	[RTN_UNICAST] = {
1011da177e4SLinus Torvalds 		.error	= 0,
1021da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1036a31d2a9SEric Dumazet 	},
1046a31d2a9SEric Dumazet 	[RTN_LOCAL] = {
1051da177e4SLinus Torvalds 		.error	= 0,
1061da177e4SLinus Torvalds 		.scope	= RT_SCOPE_HOST,
1076a31d2a9SEric Dumazet 	},
1086a31d2a9SEric Dumazet 	[RTN_BROADCAST] = {
1091da177e4SLinus Torvalds 		.error	= 0,
1101da177e4SLinus Torvalds 		.scope	= RT_SCOPE_LINK,
1116a31d2a9SEric Dumazet 	},
1126a31d2a9SEric Dumazet 	[RTN_ANYCAST] = {
1131da177e4SLinus Torvalds 		.error	= 0,
1141da177e4SLinus Torvalds 		.scope	= RT_SCOPE_LINK,
1156a31d2a9SEric Dumazet 	},
1166a31d2a9SEric Dumazet 	[RTN_MULTICAST] = {
1171da177e4SLinus Torvalds 		.error	= 0,
1181da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1196a31d2a9SEric Dumazet 	},
1206a31d2a9SEric Dumazet 	[RTN_BLACKHOLE] = {
1211da177e4SLinus Torvalds 		.error	= -EINVAL,
1221da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1236a31d2a9SEric Dumazet 	},
1246a31d2a9SEric Dumazet 	[RTN_UNREACHABLE] = {
1251da177e4SLinus Torvalds 		.error	= -EHOSTUNREACH,
1261da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1276a31d2a9SEric Dumazet 	},
1286a31d2a9SEric Dumazet 	[RTN_PROHIBIT] = {
1291da177e4SLinus Torvalds 		.error	= -EACCES,
1301da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1316a31d2a9SEric Dumazet 	},
1326a31d2a9SEric Dumazet 	[RTN_THROW] = {
1331da177e4SLinus Torvalds 		.error	= -EAGAIN,
1341da177e4SLinus Torvalds 		.scope	= RT_SCOPE_UNIVERSE,
1356a31d2a9SEric Dumazet 	},
1366a31d2a9SEric Dumazet 	[RTN_NAT] = {
1371da177e4SLinus Torvalds 		.error	= -EINVAL,
1381da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1396a31d2a9SEric Dumazet 	},
1406a31d2a9SEric Dumazet 	[RTN_XRESOLVE] = {
1411da177e4SLinus Torvalds 		.error	= -EINVAL,
1421da177e4SLinus Torvalds 		.scope	= RT_SCOPE_NOWHERE,
1436a31d2a9SEric Dumazet 	},
1441da177e4SLinus Torvalds };
1451da177e4SLinus Torvalds 
146c5038a83SDavid S. Miller static void rt_fibinfo_free(struct rtable __rcu **rtp)
14754764bb6SEric Dumazet {
14854764bb6SEric Dumazet 	struct rtable *rt = rcu_dereference_protected(*rtp, 1);
14954764bb6SEric Dumazet 
15054764bb6SEric Dumazet 	if (!rt)
15154764bb6SEric Dumazet 		return;
15254764bb6SEric Dumazet 
15354764bb6SEric Dumazet 	/* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
15454764bb6SEric Dumazet 	 * because we waited an RCU grace period before calling
15554764bb6SEric Dumazet 	 * free_fib_info_rcu()
15654764bb6SEric Dumazet 	 */
15754764bb6SEric Dumazet 
15895c47f9cSWei Wang 	dst_dev_put(&rt->dst);
159b838d5e1SWei Wang 	dst_release_immediate(&rt->dst);
16054764bb6SEric Dumazet }
16154764bb6SEric Dumazet 
162a5995e71SDavid Ahern static void free_nh_exceptions(struct fib_nh_common *nhc)
163c5038a83SDavid S. Miller {
164caa41527SEric Dumazet 	struct fnhe_hash_bucket *hash;
165c5038a83SDavid S. Miller 	int i;
166c5038a83SDavid S. Miller 
167a5995e71SDavid Ahern 	hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
168caa41527SEric Dumazet 	if (!hash)
169caa41527SEric Dumazet 		return;
170c5038a83SDavid S. Miller 	for (i = 0; i < FNHE_HASH_SIZE; i++) {
171c5038a83SDavid S. Miller 		struct fib_nh_exception *fnhe;
172c5038a83SDavid S. Miller 
173c5038a83SDavid S. Miller 		fnhe = rcu_dereference_protected(hash[i].chain, 1);
174c5038a83SDavid S. Miller 		while (fnhe) {
175c5038a83SDavid S. Miller 			struct fib_nh_exception *next;
176c5038a83SDavid S. Miller 
177c5038a83SDavid S. Miller 			next = rcu_dereference_protected(fnhe->fnhe_next, 1);
178c5038a83SDavid S. Miller 
1792ffae99dSTimo Teräs 			rt_fibinfo_free(&fnhe->fnhe_rth_input);
1802ffae99dSTimo Teräs 			rt_fibinfo_free(&fnhe->fnhe_rth_output);
181c5038a83SDavid S. Miller 
182c5038a83SDavid S. Miller 			kfree(fnhe);
183c5038a83SDavid S. Miller 
184c5038a83SDavid S. Miller 			fnhe = next;
185c5038a83SDavid S. Miller 		}
186c5038a83SDavid S. Miller 	}
187c5038a83SDavid S. Miller 	kfree(hash);
188c5038a83SDavid S. Miller }
189c5038a83SDavid S. Miller 
190c5038a83SDavid S. Miller static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
191d26b3a7cSEric Dumazet {
192d26b3a7cSEric Dumazet 	int cpu;
193d26b3a7cSEric Dumazet 
194d26b3a7cSEric Dumazet 	if (!rtp)
195d26b3a7cSEric Dumazet 		return;
196d26b3a7cSEric Dumazet 
197d26b3a7cSEric Dumazet 	for_each_possible_cpu(cpu) {
198d26b3a7cSEric Dumazet 		struct rtable *rt;
199d26b3a7cSEric Dumazet 
200d26b3a7cSEric Dumazet 		rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
2010830106cSWei Wang 		if (rt) {
20295c47f9cSWei Wang 			dst_dev_put(&rt->dst);
203b838d5e1SWei Wang 			dst_release_immediate(&rt->dst);
204d26b3a7cSEric Dumazet 		}
2050830106cSWei Wang 	}
206d26b3a7cSEric Dumazet 	free_percpu(rtp);
207d26b3a7cSEric Dumazet }
208d26b3a7cSEric Dumazet 
209979e276eSDavid Ahern void fib_nh_common_release(struct fib_nh_common *nhc)
210979e276eSDavid Ahern {
211979e276eSDavid Ahern 	if (nhc->nhc_dev)
212979e276eSDavid Ahern 		dev_put(nhc->nhc_dev);
213979e276eSDavid Ahern 
214979e276eSDavid Ahern 	lwtstate_put(nhc->nhc_lwtstate);
2150f457a36SDavid Ahern 	rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
2160f457a36SDavid Ahern 	rt_fibinfo_free(&nhc->nhc_rth_input);
217a5995e71SDavid Ahern 	free_nh_exceptions(nhc);
218979e276eSDavid Ahern }
219979e276eSDavid Ahern EXPORT_SYMBOL_GPL(fib_nh_common_release);
220979e276eSDavid Ahern 
221faa041a4SDavid Ahern void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
222faa041a4SDavid Ahern {
223faa041a4SDavid Ahern #ifdef CONFIG_IP_ROUTE_CLASSID
224faa041a4SDavid Ahern 	if (fib_nh->nh_tclassid)
225faa041a4SDavid Ahern 		net->ipv4.fib_num_tclassid_users--;
226faa041a4SDavid Ahern #endif
227979e276eSDavid Ahern 	fib_nh_common_release(&fib_nh->nh_common);
228faa041a4SDavid Ahern }
229faa041a4SDavid Ahern 
2301da177e4SLinus Torvalds /* Release a nexthop info record */
23119c1ea14SYan, Zheng static void free_fib_info_rcu(struct rcu_head *head)
23219c1ea14SYan, Zheng {
23319c1ea14SYan, Zheng 	struct fib_info *fi = container_of(head, struct fib_info, rcu);
23419c1ea14SYan, Zheng 
2354c7e8084SDavid Ahern 	if (fi->nh) {
2364c7e8084SDavid Ahern 		nexthop_put(fi->nh);
2374c7e8084SDavid Ahern 	} else {
238e49cc0daSYanmin Zhang 		change_nexthops(fi) {
239faa041a4SDavid Ahern 			fib_nh_release(fi->fib_net, nexthop_nh);
240e49cc0daSYanmin Zhang 		} endfor_nexthops(fi);
2414c7e8084SDavid Ahern 	}
242e49cc0daSYanmin Zhang 
243cc5f0eb2SDavid Ahern 	ip_fib_metrics_put(fi->fib_metrics);
244cc5f0eb2SDavid Ahern 
24519c1ea14SYan, Zheng 	kfree(fi);
24619c1ea14SYan, Zheng }
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds void free_fib_info(struct fib_info *fi)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds 	if (fi->fib_dead == 0) {
251058bd4d2SJoe Perches 		pr_warn("Freeing alive fib_info %p\n", fi);
2521da177e4SLinus Torvalds 		return;
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 	fib_info_cnt--;
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);
2631da177e4SLinus Torvalds 	if (fi && --fi->fib_treeref == 0) {
2641da177e4SLinus Torvalds 		hlist_del(&fi->fib_hash);
2651da177e4SLinus Torvalds 		if (fi->fib_prefsrc)
2661da177e4SLinus Torvalds 			hlist_del(&fi->fib_lhash);
2674c7e8084SDavid Ahern 		if (fi->nh) {
2684c7e8084SDavid Ahern 			list_del(&fi->nh_list);
2694c7e8084SDavid Ahern 		} else {
2701da177e4SLinus Torvalds 			change_nexthops(fi) {
271b75ed8b1SDavid Ahern 				if (!nexthop_nh->fib_nh_dev)
2721da177e4SLinus Torvalds 					continue;
27371fceff0SDavid S. Miller 				hlist_del(&nexthop_nh->nh_hash);
2741da177e4SLinus Torvalds 			} endfor_nexthops(fi)
2754c7e8084SDavid Ahern 		}
2761da177e4SLinus Torvalds 		fi->fib_dead = 1;
2771da177e4SLinus Torvalds 		fib_info_put(fi);
2781da177e4SLinus Torvalds 	}
279832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
2825481d73fSDavid Ahern static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
2831da177e4SLinus Torvalds {
2845481d73fSDavid Ahern 	const struct fib_nh *onh;
2851da177e4SLinus Torvalds 
2864c7e8084SDavid Ahern 	if (fi->nh || ofi->nh)
2874c7e8084SDavid Ahern 		return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1;
2884c7e8084SDavid Ahern 
2894c7e8084SDavid Ahern 	if (ofi->fib_nhs == 0)
2904c7e8084SDavid Ahern 		return 0;
2914c7e8084SDavid Ahern 
2921da177e4SLinus Torvalds 	for_nexthops(fi) {
2935481d73fSDavid Ahern 		onh = fib_info_nh(ofi, nhsel);
2945481d73fSDavid Ahern 
295b75ed8b1SDavid Ahern 		if (nh->fib_nh_oif != onh->fib_nh_oif ||
296a4ea5d43SDavid Ahern 		    nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
297b75ed8b1SDavid Ahern 		    nh->fib_nh_scope != onh->fib_nh_scope ||
2981da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
299b75ed8b1SDavid Ahern 		    nh->fib_nh_weight != onh->fib_nh_weight ||
3001da177e4SLinus Torvalds #endif
301c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
3021da177e4SLinus Torvalds 		    nh->nh_tclassid != onh->nh_tclassid ||
3031da177e4SLinus Torvalds #endif
304b75ed8b1SDavid Ahern 		    lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
305b75ed8b1SDavid Ahern 		    ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
3061da177e4SLinus Torvalds 			return -1;
307a4ea5d43SDavid Ahern 
308a4ea5d43SDavid Ahern 		if (nh->fib_nh_gw_family == AF_INET &&
309a4ea5d43SDavid Ahern 		    nh->fib_nh_gw4 != onh->fib_nh_gw4)
310a4ea5d43SDavid Ahern 			return -1;
311a4ea5d43SDavid Ahern 
312a4ea5d43SDavid Ahern 		if (nh->fib_nh_gw_family == AF_INET6 &&
313a4ea5d43SDavid Ahern 		    ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
314a4ea5d43SDavid Ahern 			return -1;
3151da177e4SLinus Torvalds 	} endfor_nexthops(fi);
3161da177e4SLinus Torvalds 	return 0;
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
31988ebc72fSDavid S. Miller static inline unsigned int fib_devindex_hashfn(unsigned int val)
32088ebc72fSDavid S. Miller {
32188ebc72fSDavid S. Miller 	unsigned int mask = DEVINDEX_HASHSIZE - 1;
32288ebc72fSDavid S. Miller 
32388ebc72fSDavid S. Miller 	return (val ^
32488ebc72fSDavid S. Miller 		(val >> DEVINDEX_HASHBITS) ^
32588ebc72fSDavid S. Miller 		(val >> (DEVINDEX_HASHBITS * 2))) & mask;
32688ebc72fSDavid S. Miller }
32788ebc72fSDavid S. Miller 
3286c48ea5fSDavid Ahern static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope,
3296c48ea5fSDavid Ahern 				      u32 prefsrc, u32 priority)
3306c48ea5fSDavid Ahern {
3316c48ea5fSDavid Ahern 	unsigned int val = init_val;
3326c48ea5fSDavid Ahern 
3336c48ea5fSDavid Ahern 	val ^= (protocol << 8) | scope;
3346c48ea5fSDavid Ahern 	val ^= prefsrc;
3356c48ea5fSDavid Ahern 	val ^= priority;
3366c48ea5fSDavid Ahern 
3376c48ea5fSDavid Ahern 	return val;
3386c48ea5fSDavid Ahern }
3396c48ea5fSDavid Ahern 
3406c48ea5fSDavid Ahern static unsigned int fib_info_hashfn_result(unsigned int val)
3411da177e4SLinus Torvalds {
342123b9731SDavid S. Miller 	unsigned int mask = (fib_info_hash_size - 1);
3431da177e4SLinus Torvalds 
3446c48ea5fSDavid Ahern 	return (val ^ (val >> 7) ^ (val >> 12)) & mask;
3456c48ea5fSDavid Ahern }
3466c48ea5fSDavid Ahern 
3476c48ea5fSDavid Ahern static inline unsigned int fib_info_hashfn(struct fib_info *fi)
3486c48ea5fSDavid Ahern {
3496c48ea5fSDavid Ahern 	unsigned int val;
3506c48ea5fSDavid Ahern 
3516c48ea5fSDavid Ahern 	val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol,
3526c48ea5fSDavid Ahern 				fi->fib_scope, (__force u32)fi->fib_prefsrc,
3536c48ea5fSDavid Ahern 				fi->fib_priority);
3544c7e8084SDavid Ahern 
3554c7e8084SDavid Ahern 	if (fi->nh) {
3564c7e8084SDavid Ahern 		val ^= fib_devindex_hashfn(fi->nh->id);
3574c7e8084SDavid Ahern 	} else {
35888ebc72fSDavid S. Miller 		for_nexthops(fi) {
359b75ed8b1SDavid Ahern 			val ^= fib_devindex_hashfn(nh->fib_nh_oif);
36088ebc72fSDavid S. Miller 		} endfor_nexthops(fi)
3614c7e8084SDavid Ahern 	}
3621da177e4SLinus Torvalds 
3636c48ea5fSDavid Ahern 	return fib_info_hashfn_result(val);
3646c48ea5fSDavid Ahern }
3656c48ea5fSDavid Ahern 
3666c48ea5fSDavid Ahern /* no metrics, only nexthop id */
3676c48ea5fSDavid Ahern static struct fib_info *fib_find_info_nh(struct net *net,
3686c48ea5fSDavid Ahern 					 const struct fib_config *cfg)
3696c48ea5fSDavid Ahern {
3706c48ea5fSDavid Ahern 	struct hlist_head *head;
3716c48ea5fSDavid Ahern 	struct fib_info *fi;
3726c48ea5fSDavid Ahern 	unsigned int hash;
3736c48ea5fSDavid Ahern 
3746c48ea5fSDavid Ahern 	hash = fib_info_hashfn_1(fib_devindex_hashfn(cfg->fc_nh_id),
3756c48ea5fSDavid Ahern 				 cfg->fc_protocol, cfg->fc_scope,
3766c48ea5fSDavid Ahern 				 (__force u32)cfg->fc_prefsrc,
3776c48ea5fSDavid Ahern 				 cfg->fc_priority);
3786c48ea5fSDavid Ahern 	hash = fib_info_hashfn_result(hash);
3796c48ea5fSDavid Ahern 	head = &fib_info_hash[hash];
3806c48ea5fSDavid Ahern 
3816c48ea5fSDavid Ahern 	hlist_for_each_entry(fi, head, fib_hash) {
3826c48ea5fSDavid Ahern 		if (!net_eq(fi->fib_net, net))
3836c48ea5fSDavid Ahern 			continue;
3846c48ea5fSDavid Ahern 		if (!fi->nh || fi->nh->id != cfg->fc_nh_id)
3856c48ea5fSDavid Ahern 			continue;
3866c48ea5fSDavid Ahern 		if (cfg->fc_protocol == fi->fib_protocol &&
3876c48ea5fSDavid Ahern 		    cfg->fc_scope == fi->fib_scope &&
3886c48ea5fSDavid Ahern 		    cfg->fc_prefsrc == fi->fib_prefsrc &&
3896c48ea5fSDavid Ahern 		    cfg->fc_priority == fi->fib_priority &&
3906c48ea5fSDavid Ahern 		    cfg->fc_type == fi->fib_type &&
3916c48ea5fSDavid Ahern 		    cfg->fc_table == fi->fib_tb_id &&
3926c48ea5fSDavid Ahern 		    !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK))
3936c48ea5fSDavid Ahern 			return fi;
3946c48ea5fSDavid Ahern 	}
3956c48ea5fSDavid Ahern 
3966c48ea5fSDavid Ahern 	return NULL;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3995481d73fSDavid Ahern static struct fib_info *fib_find_info(struct fib_info *nfi)
4001da177e4SLinus Torvalds {
4011da177e4SLinus Torvalds 	struct hlist_head *head;
4021da177e4SLinus Torvalds 	struct fib_info *fi;
4031da177e4SLinus Torvalds 	unsigned int hash;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	hash = fib_info_hashfn(nfi);
4061da177e4SLinus Torvalds 	head = &fib_info_hash[hash];
4071da177e4SLinus Torvalds 
408b67bfe0dSSasha Levin 	hlist_for_each_entry(fi, head, fib_hash) {
40909ad9bc7SOctavian Purdila 		if (!net_eq(fi->fib_net, nfi->fib_net))
4104814bdbdSDenis V. Lunev 			continue;
4111da177e4SLinus Torvalds 		if (fi->fib_nhs != nfi->fib_nhs)
4121da177e4SLinus Torvalds 			continue;
4131da177e4SLinus Torvalds 		if (nfi->fib_protocol == fi->fib_protocol &&
41437e826c5SDavid S. Miller 		    nfi->fib_scope == fi->fib_scope &&
4151da177e4SLinus Torvalds 		    nfi->fib_prefsrc == fi->fib_prefsrc &&
4161da177e4SLinus Torvalds 		    nfi->fib_priority == fi->fib_priority &&
417f4ef85bbSEric Dumazet 		    nfi->fib_type == fi->fib_type &&
4181da177e4SLinus Torvalds 		    memcmp(nfi->fib_metrics, fi->fib_metrics,
419fcd13f42SEric Dumazet 			   sizeof(u32) * RTAX_MAX) == 0 &&
4208a3d0316SAndy Gospodarek 		    !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
4214c7e8084SDavid Ahern 		    nh_comp(fi, nfi) == 0)
4221da177e4SLinus Torvalds 			return fi;
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	return NULL;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds /* Check, that the gateway is already configured.
4296a31d2a9SEric Dumazet  * Used only by redirect accept routine.
4301da177e4SLinus Torvalds  */
431d878e72eSAl Viro int ip_fib_check_default(__be32 gw, struct net_device *dev)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	struct hlist_head *head;
4341da177e4SLinus Torvalds 	struct fib_nh *nh;
4351da177e4SLinus Torvalds 	unsigned int hash;
4361da177e4SLinus Torvalds 
437832b4c5eSStephen Hemminger 	spin_lock(&fib_info_lock);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	hash = fib_devindex_hashfn(dev->ifindex);
4401da177e4SLinus Torvalds 	head = &fib_info_devhash[hash];
441b67bfe0dSSasha Levin 	hlist_for_each_entry(nh, head, nh_hash) {
442b75ed8b1SDavid Ahern 		if (nh->fib_nh_dev == dev &&
443b75ed8b1SDavid Ahern 		    nh->fib_nh_gw4 == gw &&
444b75ed8b1SDavid Ahern 		    !(nh->fib_nh_flags & RTNH_F_DEAD)) {
445832b4c5eSStephen Hemminger 			spin_unlock(&fib_info_lock);
4461da177e4SLinus Torvalds 			return 0;
4471da177e4SLinus Torvalds 		}
4481da177e4SLinus Torvalds 	}
4491da177e4SLinus Torvalds 
450832b4c5eSStephen Hemminger 	spin_unlock(&fib_info_lock);
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	return -1;
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4551e7bdec6SAmit Cohen size_t fib_nlmsg_size(struct fib_info *fi)
456339bf98fSThomas Graf {
457339bf98fSThomas Graf 	size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
458339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_TABLE */
459339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_DST */
460339bf98fSThomas Graf 			 + nla_total_size(4) /* RTA_PRIORITY */
461ea697639SDaniel Borkmann 			 + nla_total_size(4) /* RTA_PREFSRC */
462ea697639SDaniel Borkmann 			 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
4635481d73fSDavid Ahern 	unsigned int nhs = fib_info_num_path(fi);
464339bf98fSThomas Graf 
465339bf98fSThomas Graf 	/* space for nested metrics */
466339bf98fSThomas Graf 	payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
467339bf98fSThomas Graf 
4684c7e8084SDavid Ahern 	if (fi->nh)
4694c7e8084SDavid Ahern 		payload += nla_total_size(4); /* RTA_NH_ID */
4704c7e8084SDavid Ahern 
4715481d73fSDavid Ahern 	if (nhs) {
472571e7226SRoopa Prabhu 		size_t nh_encapsize = 0;
4735481d73fSDavid Ahern 		/* Also handles the special case nhs == 1 */
474339bf98fSThomas Graf 
475339bf98fSThomas Graf 		/* each nexthop is packed in an attribute */
476339bf98fSThomas Graf 		size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
477dcb1ecb5SDavid Ahern 		unsigned int i;
478339bf98fSThomas Graf 
479339bf98fSThomas Graf 		/* may contain flow and gateway attribute */
480339bf98fSThomas Graf 		nhsize += 2 * nla_total_size(4);
481339bf98fSThomas Graf 
482571e7226SRoopa Prabhu 		/* grab encap info */
483dcb1ecb5SDavid Ahern 		for (i = 0; i < fib_info_num_path(fi); i++) {
484dcb1ecb5SDavid Ahern 			struct fib_nh_common *nhc = fib_info_nhc(fi, i);
485dcb1ecb5SDavid Ahern 
486dcb1ecb5SDavid Ahern 			if (nhc->nhc_lwtstate) {
487571e7226SRoopa Prabhu 				/* RTA_ENCAP_TYPE */
488571e7226SRoopa Prabhu 				nh_encapsize += lwtunnel_get_encap_size(
489dcb1ecb5SDavid Ahern 						nhc->nhc_lwtstate);
490571e7226SRoopa Prabhu 				/* RTA_ENCAP */
491571e7226SRoopa Prabhu 				nh_encapsize +=  nla_total_size(2);
492571e7226SRoopa Prabhu 			}
493dcb1ecb5SDavid Ahern 		}
494571e7226SRoopa Prabhu 
495339bf98fSThomas Graf 		/* all nexthops are packed in a nested attribute */
4965481d73fSDavid Ahern 		payload += nla_total_size((nhs * nhsize) + nh_encapsize);
497571e7226SRoopa Prabhu 
498339bf98fSThomas Graf 	}
499339bf98fSThomas Graf 
500339bf98fSThomas Graf 	return payload;
501339bf98fSThomas Graf }
502339bf98fSThomas Graf 
50381f7bf6cSAl Viro void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
5049877b253SJoe Perches 	       int dst_len, u32 tb_id, const struct nl_info *info,
505b8f55831SMilan Kocian 	       unsigned int nlm_flags)
5061da177e4SLinus Torvalds {
5071e301fd0SIdo Schimmel 	struct fib_rt_info fri;
5081da177e4SLinus Torvalds 	struct sk_buff *skb;
5094e902c57SThomas Graf 	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
510f21c7bc5SThomas Graf 	int err = -ENOBUFS;
5111da177e4SLinus Torvalds 
512339bf98fSThomas Graf 	skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
51351456b29SIan Morris 	if (!skb)
514f21c7bc5SThomas Graf 		goto errout;
5151da177e4SLinus Torvalds 
5161e301fd0SIdo Schimmel 	fri.fi = fa->fa_info;
5171e301fd0SIdo Schimmel 	fri.tb_id = tb_id;
5181e301fd0SIdo Schimmel 	fri.dst = key;
5191e301fd0SIdo Schimmel 	fri.dst_len = dst_len;
5201e301fd0SIdo Schimmel 	fri.tos = fa->fa_tos;
5211e301fd0SIdo Schimmel 	fri.type = fa->fa_type;
52290b93f1bSIdo Schimmel 	fri.offload = fa->offload;
52390b93f1bSIdo Schimmel 	fri.trap = fa->trap;
524*36c5100eSAmit Cohen 	fri.offload_failed = fa->offload_failed;
5251e301fd0SIdo Schimmel 	err = fib_dump_info(skb, info->portid, seq, event, &fri, nlm_flags);
52626932566SPatrick McHardy 	if (err < 0) {
52726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in fib_nlmsg_size() */
52826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
52926932566SPatrick McHardy 		kfree_skb(skb);
53026932566SPatrick McHardy 		goto errout;
53126932566SPatrick McHardy 	}
53215e47304SEric W. Biederman 	rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
5334e902c57SThomas Graf 		    info->nlh, GFP_KERNEL);
5341ce85fe4SPablo Neira Ayuso 	return;
535f21c7bc5SThomas Graf errout:
536f21c7bc5SThomas Graf 	if (err < 0)
5374d1169c1SDenis V. Lunev 		rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
540c9cb6b6eSStephen Hemminger static int fib_detect_death(struct fib_info *fi, int order,
541c9cb6b6eSStephen Hemminger 			    struct fib_info **last_resort, int *last_idx,
542c9cb6b6eSStephen Hemminger 			    int dflt)
5431da177e4SLinus Torvalds {
544619d1826SDavid Ahern 	const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
5451da177e4SLinus Torvalds 	struct neighbour *n;
5461da177e4SLinus Torvalds 	int state = NUD_NONE;
5471da177e4SLinus Torvalds 
548619d1826SDavid Ahern 	if (likely(nhc->nhc_gw_family == AF_INET))
549619d1826SDavid Ahern 		n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
550619d1826SDavid Ahern 	else if (nhc->nhc_gw_family == AF_INET6)
551619d1826SDavid Ahern 		n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
552619d1826SDavid Ahern 				 nhc->nhc_dev);
553619d1826SDavid Ahern 	else
554619d1826SDavid Ahern 		n = NULL;
555619d1826SDavid Ahern 
5561da177e4SLinus Torvalds 	if (n) {
5571da177e4SLinus Torvalds 		state = n->nud_state;
5581da177e4SLinus Torvalds 		neigh_release(n);
55988f64320SJulian Anastasov 	} else {
56088f64320SJulian Anastasov 		return 0;
5611da177e4SLinus Torvalds 	}
5621da177e4SLinus Torvalds 	if (state == NUD_REACHABLE)
5631da177e4SLinus Torvalds 		return 0;
564c17860a0SDenis V. Lunev 	if ((state & NUD_VALID) && order != dflt)
5651da177e4SLinus Torvalds 		return 0;
5661da177e4SLinus Torvalds 	if ((state & NUD_VALID) ||
56788f64320SJulian Anastasov 	    (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
5681da177e4SLinus Torvalds 		*last_resort = fi;
5691da177e4SLinus Torvalds 		*last_idx = order;
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 	return 1;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
574faee6769SAlexander Aring int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
575faee6769SAlexander Aring 		       struct nlattr *encap, u16 encap_type,
576faee6769SAlexander Aring 		       void *cfg, gfp_t gfp_flags,
577979e276eSDavid Ahern 		       struct netlink_ext_ack *extack)
578979e276eSDavid Ahern {
5790f457a36SDavid Ahern 	int err;
5800f457a36SDavid Ahern 
5810f457a36SDavid Ahern 	nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
5820f457a36SDavid Ahern 						    gfp_flags);
5830f457a36SDavid Ahern 	if (!nhc->nhc_pcpu_rth_output)
5840f457a36SDavid Ahern 		return -ENOMEM;
5850f457a36SDavid Ahern 
586979e276eSDavid Ahern 	if (encap) {
587979e276eSDavid Ahern 		struct lwtunnel_state *lwtstate;
588979e276eSDavid Ahern 
589979e276eSDavid Ahern 		if (encap_type == LWTUNNEL_ENCAP_NONE) {
590979e276eSDavid Ahern 			NL_SET_ERR_MSG(extack, "LWT encap type not specified");
5910f457a36SDavid Ahern 			err = -EINVAL;
5920f457a36SDavid Ahern 			goto lwt_failure;
593979e276eSDavid Ahern 		}
594faee6769SAlexander Aring 		err = lwtunnel_build_state(net, encap_type, encap,
595faee6769SAlexander Aring 					   nhc->nhc_family, cfg, &lwtstate,
596faee6769SAlexander Aring 					   extack);
597979e276eSDavid Ahern 		if (err)
5980f457a36SDavid Ahern 			goto lwt_failure;
599979e276eSDavid Ahern 
600979e276eSDavid Ahern 		nhc->nhc_lwtstate = lwtstate_get(lwtstate);
601979e276eSDavid Ahern 	}
602979e276eSDavid Ahern 
603979e276eSDavid Ahern 	return 0;
6040f457a36SDavid Ahern 
6050f457a36SDavid Ahern lwt_failure:
6060f457a36SDavid Ahern 	rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
6070f457a36SDavid Ahern 	nhc->nhc_pcpu_rth_output = NULL;
6080f457a36SDavid Ahern 	return err;
609979e276eSDavid Ahern }
610979e276eSDavid Ahern EXPORT_SYMBOL_GPL(fib_nh_common_init);
611979e276eSDavid Ahern 
612e4516ef6SDavid Ahern int fib_nh_init(struct net *net, struct fib_nh *nh,
613e4516ef6SDavid Ahern 		struct fib_config *cfg, int nh_weight,
614e4516ef6SDavid Ahern 		struct netlink_ext_ack *extack)
615e4516ef6SDavid Ahern {
6160f457a36SDavid Ahern 	int err;
617e4516ef6SDavid Ahern 
618f1741730SDavid Ahern 	nh->fib_nh_family = AF_INET;
619f1741730SDavid Ahern 
620faee6769SAlexander Aring 	err = fib_nh_common_init(net, &nh->nh_common, cfg->fc_encap,
621979e276eSDavid Ahern 				 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
622e4516ef6SDavid Ahern 	if (err)
6230f457a36SDavid Ahern 		return err;
624e4516ef6SDavid Ahern 
625b75ed8b1SDavid Ahern 	nh->fib_nh_oif = cfg->fc_oif;
626a4ea5d43SDavid Ahern 	nh->fib_nh_gw_family = cfg->fc_gw_family;
627a4ea5d43SDavid Ahern 	if (cfg->fc_gw_family == AF_INET)
628f35b794bSDavid Ahern 		nh->fib_nh_gw4 = cfg->fc_gw4;
629a4ea5d43SDavid Ahern 	else if (cfg->fc_gw_family == AF_INET6)
630a4ea5d43SDavid Ahern 		nh->fib_nh_gw6 = cfg->fc_gw6;
631a4ea5d43SDavid Ahern 
632b75ed8b1SDavid Ahern 	nh->fib_nh_flags = cfg->fc_flags;
633e4516ef6SDavid Ahern 
634e4516ef6SDavid Ahern #ifdef CONFIG_IP_ROUTE_CLASSID
635e4516ef6SDavid Ahern 	nh->nh_tclassid = cfg->fc_flow;
636e4516ef6SDavid Ahern 	if (nh->nh_tclassid)
637e4516ef6SDavid Ahern 		net->ipv4.fib_num_tclassid_users++;
638e4516ef6SDavid Ahern #endif
639e4516ef6SDavid Ahern #ifdef CONFIG_IP_ROUTE_MULTIPATH
640b75ed8b1SDavid Ahern 	nh->fib_nh_weight = nh_weight;
641e4516ef6SDavid Ahern #endif
642e4516ef6SDavid Ahern 	return 0;
643e4516ef6SDavid Ahern }
644e4516ef6SDavid Ahern 
6451da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
6461da177e4SLinus Torvalds 
6476d8422a1SDavid Ahern static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
6486d8422a1SDavid Ahern 			      struct netlink_ext_ack *extack)
6491da177e4SLinus Torvalds {
6501da177e4SLinus Torvalds 	int nhs = 0;
6511da177e4SLinus Torvalds 
6524e902c57SThomas Graf 	while (rtnh_ok(rtnh, remaining)) {
6531da177e4SLinus Torvalds 		nhs++;
6544e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 
6574e902c57SThomas Graf 	/* leftover implies invalid nexthop configuration, discard it */
658c3ab2b4eSDavid Ahern 	if (remaining > 0) {
659c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack,
660c3ab2b4eSDavid Ahern 			       "Invalid nexthop configuration - extra data after nexthops");
661c3ab2b4eSDavid Ahern 		nhs = 0;
662c3ab2b4eSDavid Ahern 	}
663c3ab2b4eSDavid Ahern 
664c3ab2b4eSDavid Ahern 	return nhs;
6654e902c57SThomas Graf }
6661da177e4SLinus Torvalds 
6674c7e8084SDavid Ahern /* only called when fib_nh is integrated into fib_info */
6684e902c57SThomas Graf static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
6696d8422a1SDavid Ahern 		       int remaining, struct fib_config *cfg,
6706d8422a1SDavid Ahern 		       struct netlink_ext_ack *extack)
6714e902c57SThomas Graf {
672e4516ef6SDavid Ahern 	struct net *net = fi->fib_net;
673e4516ef6SDavid Ahern 	struct fib_config fib_cfg;
6745481d73fSDavid Ahern 	struct fib_nh *nh;
675571e7226SRoopa Prabhu 	int ret;
676571e7226SRoopa Prabhu 
6771da177e4SLinus Torvalds 	change_nexthops(fi) {
6784e902c57SThomas Graf 		int attrlen;
6794e902c57SThomas Graf 
680e4516ef6SDavid Ahern 		memset(&fib_cfg, 0, sizeof(fib_cfg));
681e4516ef6SDavid Ahern 
682c3ab2b4eSDavid Ahern 		if (!rtnh_ok(rtnh, remaining)) {
683c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
684c3ab2b4eSDavid Ahern 				       "Invalid nexthop configuration - extra data after nexthop");
6851da177e4SLinus Torvalds 			return -EINVAL;
686c3ab2b4eSDavid Ahern 		}
6874e902c57SThomas Graf 
688c3ab2b4eSDavid Ahern 		if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
689c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
690c3ab2b4eSDavid Ahern 				       "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
69180610229SJulian Anastasov 			return -EINVAL;
692c3ab2b4eSDavid Ahern 		}
69380610229SJulian Anastasov 
694e4516ef6SDavid Ahern 		fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
695e4516ef6SDavid Ahern 		fib_cfg.fc_oif = rtnh->rtnh_ifindex;
6964e902c57SThomas Graf 
6974e902c57SThomas Graf 		attrlen = rtnh_attrlen(rtnh);
6984e902c57SThomas Graf 		if (attrlen > 0) {
699d1566268SDavid Ahern 			struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
7004e902c57SThomas Graf 
7014e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
702d1566268SDavid Ahern 			nlav = nla_find(attrs, attrlen, RTA_VIA);
703d1566268SDavid Ahern 			if (nla && nlav) {
704d1566268SDavid Ahern 				NL_SET_ERR_MSG(extack,
705d1566268SDavid Ahern 					       "Nexthop configuration can not contain both GATEWAY and VIA");
706d1566268SDavid Ahern 				return -EINVAL;
707d1566268SDavid Ahern 			}
708f35b794bSDavid Ahern 			if (nla) {
709f35b794bSDavid Ahern 				fib_cfg.fc_gw4 = nla_get_in_addr(nla);
710d73f80f9SDavid Ahern 				if (fib_cfg.fc_gw4)
711d73f80f9SDavid Ahern 					fib_cfg.fc_gw_family = AF_INET;
712d1566268SDavid Ahern 			} else if (nlav) {
713d1566268SDavid Ahern 				ret = fib_gw_from_via(&fib_cfg, nlav, extack);
714d1566268SDavid Ahern 				if (ret)
715d1566268SDavid Ahern 					goto errout;
716f35b794bSDavid Ahern 			}
717571e7226SRoopa Prabhu 
718e4516ef6SDavid Ahern 			nla = nla_find(attrs, attrlen, RTA_FLOW);
719e4516ef6SDavid Ahern 			if (nla)
720e4516ef6SDavid Ahern 				fib_cfg.fc_flow = nla_get_u32(nla);
721e4516ef6SDavid Ahern 
722e4516ef6SDavid Ahern 			fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
723e4516ef6SDavid Ahern 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
724e4516ef6SDavid Ahern 			if (nla)
725e4516ef6SDavid Ahern 				fib_cfg.fc_encap_type = nla_get_u16(nla);
726c3ab2b4eSDavid Ahern 		}
72730357d7dSDavid Ahern 
728e4516ef6SDavid Ahern 		ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
729e4516ef6SDavid Ahern 				  rtnh->rtnh_hops + 1, extack);
730571e7226SRoopa Prabhu 		if (ret)
731571e7226SRoopa Prabhu 			goto errout;
7324e902c57SThomas Graf 
7334e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
7341da177e4SLinus Torvalds 	} endfor_nexthops(fi);
7354e902c57SThomas Graf 
736571e7226SRoopa Prabhu 	ret = -EINVAL;
7375481d73fSDavid Ahern 	nh = fib_info_nh(fi, 0);
7385481d73fSDavid Ahern 	if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) {
739e4516ef6SDavid Ahern 		NL_SET_ERR_MSG(extack,
740e4516ef6SDavid Ahern 			       "Nexthop device index does not match RTA_OIF");
741e4516ef6SDavid Ahern 		goto errout;
742e4516ef6SDavid Ahern 	}
743f35b794bSDavid Ahern 	if (cfg->fc_gw_family) {
7445481d73fSDavid Ahern 		if (cfg->fc_gw_family != nh->fib_nh_gw_family ||
745f35b794bSDavid Ahern 		    (cfg->fc_gw_family == AF_INET &&
7465481d73fSDavid Ahern 		     nh->fib_nh_gw4 != cfg->fc_gw4) ||
747a4ea5d43SDavid Ahern 		    (cfg->fc_gw_family == AF_INET6 &&
7485481d73fSDavid Ahern 		     ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) {
749e4516ef6SDavid Ahern 			NL_SET_ERR_MSG(extack,
750a4ea5d43SDavid Ahern 				       "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
751e4516ef6SDavid Ahern 			goto errout;
752e4516ef6SDavid Ahern 		}
753f35b794bSDavid Ahern 	}
754e4516ef6SDavid Ahern #ifdef CONFIG_IP_ROUTE_CLASSID
7555481d73fSDavid Ahern 	if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) {
756e4516ef6SDavid Ahern 		NL_SET_ERR_MSG(extack,
757e4516ef6SDavid Ahern 			       "Nexthop class id does not match RTA_FLOW");
758e4516ef6SDavid Ahern 		goto errout;
759e4516ef6SDavid Ahern 	}
760e4516ef6SDavid Ahern #endif
761e4516ef6SDavid Ahern 	ret = 0;
762571e7226SRoopa Prabhu errout:
763571e7226SRoopa Prabhu 	return ret;
7641da177e4SLinus Torvalds }
7651da177e4SLinus Torvalds 
7664c7e8084SDavid Ahern /* only called when fib_nh is integrated into fib_info */
7670e884c78SPeter Nørlund static void fib_rebalance(struct fib_info *fi)
7680e884c78SPeter Nørlund {
7690e884c78SPeter Nørlund 	int total;
7700e884c78SPeter Nørlund 	int w;
7710e884c78SPeter Nørlund 
7725481d73fSDavid Ahern 	if (fib_info_num_path(fi) < 2)
7730e884c78SPeter Nørlund 		return;
7740e884c78SPeter Nørlund 
7750e884c78SPeter Nørlund 	total = 0;
7760e884c78SPeter Nørlund 	for_nexthops(fi) {
777b75ed8b1SDavid Ahern 		if (nh->fib_nh_flags & RTNH_F_DEAD)
7780e884c78SPeter Nørlund 			continue;
7790e884c78SPeter Nørlund 
780b75ed8b1SDavid Ahern 		if (ip_ignore_linkdown(nh->fib_nh_dev) &&
781b75ed8b1SDavid Ahern 		    nh->fib_nh_flags & RTNH_F_LINKDOWN)
7820e884c78SPeter Nørlund 			continue;
7830e884c78SPeter Nørlund 
784b75ed8b1SDavid Ahern 		total += nh->fib_nh_weight;
7850e884c78SPeter Nørlund 	} endfor_nexthops(fi);
7860e884c78SPeter Nørlund 
7870e884c78SPeter Nørlund 	w = 0;
7880e884c78SPeter Nørlund 	change_nexthops(fi) {
7890e884c78SPeter Nørlund 		int upper_bound;
7900e884c78SPeter Nørlund 
791b75ed8b1SDavid Ahern 		if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
7920e884c78SPeter Nørlund 			upper_bound = -1;
793b75ed8b1SDavid Ahern 		} else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
794b75ed8b1SDavid Ahern 			   nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
7950e884c78SPeter Nørlund 			upper_bound = -1;
7960e884c78SPeter Nørlund 		} else {
797b75ed8b1SDavid Ahern 			w += nexthop_nh->fib_nh_weight;
7980a837fe4SPeter Nørlund 			upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
7990e884c78SPeter Nørlund 							    total) - 1;
8000e884c78SPeter Nørlund 		}
8010e884c78SPeter Nørlund 
802b75ed8b1SDavid Ahern 		atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
8030e884c78SPeter Nørlund 	} endfor_nexthops(fi);
8040e884c78SPeter Nørlund }
8050e884c78SPeter Nørlund #else /* CONFIG_IP_ROUTE_MULTIPATH */
8060e884c78SPeter Nørlund 
8078373c6c8SDavid Ahern static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
8088373c6c8SDavid Ahern 		       int remaining, struct fib_config *cfg,
8098373c6c8SDavid Ahern 		       struct netlink_ext_ack *extack)
8108373c6c8SDavid Ahern {
8118373c6c8SDavid Ahern 	NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
8128373c6c8SDavid Ahern 
8138373c6c8SDavid Ahern 	return -EINVAL;
8148373c6c8SDavid Ahern }
8158373c6c8SDavid Ahern 
8160e884c78SPeter Nørlund #define fib_rebalance(fi) do { } while (0)
8170e884c78SPeter Nørlund 
8180e884c78SPeter Nørlund #endif /* CONFIG_IP_ROUTE_MULTIPATH */
8191da177e4SLinus Torvalds 
820faee6769SAlexander Aring static int fib_encap_match(struct net *net, u16 encap_type,
821571e7226SRoopa Prabhu 			   struct nlattr *encap,
82230357d7dSDavid Ahern 			   const struct fib_nh *nh,
8239ae28727SDavid Ahern 			   const struct fib_config *cfg,
8249ae28727SDavid Ahern 			   struct netlink_ext_ack *extack)
825571e7226SRoopa Prabhu {
826571e7226SRoopa Prabhu 	struct lwtunnel_state *lwtstate;
827df383e62SJiri Benc 	int ret, result = 0;
828571e7226SRoopa Prabhu 
829571e7226SRoopa Prabhu 	if (encap_type == LWTUNNEL_ENCAP_NONE)
830571e7226SRoopa Prabhu 		return 0;
831571e7226SRoopa Prabhu 
832faee6769SAlexander Aring 	ret = lwtunnel_build_state(net, encap_type, encap, AF_INET,
8339ae28727SDavid Ahern 				   cfg, &lwtstate, extack);
834df383e62SJiri Benc 	if (!ret) {
835b75ed8b1SDavid Ahern 		result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
836df383e62SJiri Benc 		lwtstate_free(lwtstate);
837df383e62SJiri Benc 	}
838571e7226SRoopa Prabhu 
839df383e62SJiri Benc 	return result;
840571e7226SRoopa Prabhu }
841571e7226SRoopa Prabhu 
842faee6769SAlexander Aring int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
8439ae28727SDavid Ahern 		 struct netlink_ext_ack *extack)
8441da177e4SLinus Torvalds {
8451da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
8464e902c57SThomas Graf 	struct rtnexthop *rtnh;
8474e902c57SThomas Graf 	int remaining;
8481da177e4SLinus Torvalds #endif
8491da177e4SLinus Torvalds 
8504e902c57SThomas Graf 	if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
8511da177e4SLinus Torvalds 		return 1;
8521da177e4SLinus Torvalds 
853493ced1aSDavid Ahern 	if (cfg->fc_nh_id) {
854493ced1aSDavid Ahern 		if (fi->nh && cfg->fc_nh_id == fi->nh->id)
855493ced1aSDavid Ahern 			return 0;
856493ced1aSDavid Ahern 		return 1;
857493ced1aSDavid Ahern 	}
858493ced1aSDavid Ahern 
859f35b794bSDavid Ahern 	if (cfg->fc_oif || cfg->fc_gw_family) {
8605481d73fSDavid Ahern 		struct fib_nh *nh = fib_info_nh(fi, 0);
8615481d73fSDavid Ahern 
862571e7226SRoopa Prabhu 		if (cfg->fc_encap) {
863faee6769SAlexander Aring 			if (fib_encap_match(net, cfg->fc_encap_type,
864faee6769SAlexander Aring 					    cfg->fc_encap, nh, cfg, extack))
865571e7226SRoopa Prabhu 				return 1;
866571e7226SRoopa Prabhu 		}
867a8c6db1dSStefano Brivio #ifdef CONFIG_IP_ROUTE_CLASSID
868a8c6db1dSStefano Brivio 		if (cfg->fc_flow &&
8695481d73fSDavid Ahern 		    cfg->fc_flow != nh->nh_tclassid)
870a8c6db1dSStefano Brivio 			return 1;
871a8c6db1dSStefano Brivio #endif
8725481d73fSDavid Ahern 		if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
873f35b794bSDavid Ahern 		    (cfg->fc_gw_family &&
8745481d73fSDavid Ahern 		     cfg->fc_gw_family != nh->fib_nh_gw_family))
8751da177e4SLinus Torvalds 			return 1;
876f35b794bSDavid Ahern 
877f35b794bSDavid Ahern 		if (cfg->fc_gw_family == AF_INET &&
8785481d73fSDavid Ahern 		    cfg->fc_gw4 != nh->fib_nh_gw4)
879f35b794bSDavid Ahern 			return 1;
880f35b794bSDavid Ahern 
881a4ea5d43SDavid Ahern 		if (cfg->fc_gw_family == AF_INET6 &&
8825481d73fSDavid Ahern 		    ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
883a4ea5d43SDavid Ahern 			return 1;
884a4ea5d43SDavid Ahern 
885f35b794bSDavid Ahern 		return 0;
8861da177e4SLinus Torvalds 	}
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
88951456b29SIan Morris 	if (!cfg->fc_mp)
8901da177e4SLinus Torvalds 		return 0;
8914e902c57SThomas Graf 
8924e902c57SThomas Graf 	rtnh = cfg->fc_mp;
8934e902c57SThomas Graf 	remaining = cfg->fc_mp_len;
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	for_nexthops(fi) {
8964e902c57SThomas Graf 		int attrlen;
8971da177e4SLinus Torvalds 
8984e902c57SThomas Graf 		if (!rtnh_ok(rtnh, remaining))
8991da177e4SLinus Torvalds 			return -EINVAL;
9004e902c57SThomas Graf 
901b75ed8b1SDavid Ahern 		if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
9021da177e4SLinus Torvalds 			return 1;
9034e902c57SThomas Graf 
9044e902c57SThomas Graf 		attrlen = rtnh_attrlen(rtnh);
905f76936d0SJiri Pirko 		if (attrlen > 0) {
906d1566268SDavid Ahern 			struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
9074e902c57SThomas Graf 
9084e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
909d1566268SDavid Ahern 			nlav = nla_find(attrs, attrlen, RTA_VIA);
910d1566268SDavid Ahern 			if (nla && nlav) {
911d1566268SDavid Ahern 				NL_SET_ERR_MSG(extack,
912d1566268SDavid Ahern 					       "Nexthop configuration can not contain both GATEWAY and VIA");
913d1566268SDavid Ahern 				return -EINVAL;
914d1566268SDavid Ahern 			}
915d1566268SDavid Ahern 
916d1566268SDavid Ahern 			if (nla) {
917d1566268SDavid Ahern 				if (nh->fib_nh_gw_family != AF_INET ||
918d1566268SDavid Ahern 				    nla_get_in_addr(nla) != nh->fib_nh_gw4)
9191da177e4SLinus Torvalds 					return 1;
920d1566268SDavid Ahern 			} else if (nlav) {
921d1566268SDavid Ahern 				struct fib_config cfg2;
922d1566268SDavid Ahern 				int err;
923d1566268SDavid Ahern 
924d1566268SDavid Ahern 				err = fib_gw_from_via(&cfg2, nlav, extack);
925d1566268SDavid Ahern 				if (err)
926d1566268SDavid Ahern 					return err;
927d1566268SDavid Ahern 
928d1566268SDavid Ahern 				switch (nh->fib_nh_gw_family) {
929d1566268SDavid Ahern 				case AF_INET:
930d1566268SDavid Ahern 					if (cfg2.fc_gw_family != AF_INET ||
931d1566268SDavid Ahern 					    cfg2.fc_gw4 != nh->fib_nh_gw4)
932d1566268SDavid Ahern 						return 1;
933d1566268SDavid Ahern 					break;
934d1566268SDavid Ahern 				case AF_INET6:
935d1566268SDavid Ahern 					if (cfg2.fc_gw_family != AF_INET6 ||
936d1566268SDavid Ahern 					    ipv6_addr_cmp(&cfg2.fc_gw6,
937d1566268SDavid Ahern 							  &nh->fib_nh_gw6))
938d1566268SDavid Ahern 						return 1;
939d1566268SDavid Ahern 					break;
940d1566268SDavid Ahern 				}
941d1566268SDavid Ahern 			}
942d1566268SDavid Ahern 
943c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
9444e902c57SThomas Graf 			nla = nla_find(attrs, attrlen, RTA_FLOW);
9454e902c57SThomas Graf 			if (nla && nla_get_u32(nla) != nh->nh_tclassid)
9461da177e4SLinus Torvalds 				return 1;
9471da177e4SLinus Torvalds #endif
9481da177e4SLinus Torvalds 		}
9494e902c57SThomas Graf 
9504e902c57SThomas Graf 		rtnh = rtnh_next(rtnh, &remaining);
9511da177e4SLinus Torvalds 	} endfor_nexthops(fi);
9521da177e4SLinus Torvalds #endif
9531da177e4SLinus Torvalds 	return 0;
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds 
9565f9ae3d9SXin Long bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
9575f9ae3d9SXin Long {
9585f9ae3d9SXin Long 	struct nlattr *nla;
9595f9ae3d9SXin Long 	int remaining;
9605f9ae3d9SXin Long 
9615f9ae3d9SXin Long 	if (!cfg->fc_mx)
9625f9ae3d9SXin Long 		return true;
9635f9ae3d9SXin Long 
9645f9ae3d9SXin Long 	nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
9655f9ae3d9SXin Long 		int type = nla_type(nla);
966d03a4557SPhil Sutter 		u32 fi_val, val;
9675f9ae3d9SXin Long 
9685f9ae3d9SXin Long 		if (!type)
9695f9ae3d9SXin Long 			continue;
9705f9ae3d9SXin Long 		if (type > RTAX_MAX)
9715f9ae3d9SXin Long 			return false;
9725f9ae3d9SXin Long 
9735f9ae3d9SXin Long 		if (type == RTAX_CC_ALGO) {
9745f9ae3d9SXin Long 			char tmp[TCP_CA_NAME_MAX];
9755f9ae3d9SXin Long 			bool ecn_ca = false;
9765f9ae3d9SXin Long 
977872f6903SFrancis Laniel 			nla_strscpy(tmp, nla, sizeof(tmp));
9786670e152SStephen Hemminger 			val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
9795f9ae3d9SXin Long 		} else {
9805b5e7a0dSEric Dumazet 			if (nla_len(nla) != sizeof(u32))
9815b5e7a0dSEric Dumazet 				return false;
9825f9ae3d9SXin Long 			val = nla_get_u32(nla);
9835f9ae3d9SXin Long 		}
9845f9ae3d9SXin Long 
985d03a4557SPhil Sutter 		fi_val = fi->fib_metrics->metrics[type - 1];
986d03a4557SPhil Sutter 		if (type == RTAX_FEATURES)
987d03a4557SPhil Sutter 			fi_val &= ~DST_FEATURE_ECN_CA;
988d03a4557SPhil Sutter 
989d03a4557SPhil Sutter 		if (fi_val != val)
9905f9ae3d9SXin Long 			return false;
9915f9ae3d9SXin Long 	}
9925f9ae3d9SXin Long 
9935f9ae3d9SXin Long 	return true;
9945f9ae3d9SXin Long }
9955f9ae3d9SXin Long 
996717a8f5bSDavid Ahern static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
997717a8f5bSDavid Ahern 			      u32 table, struct netlink_ext_ack *extack)
998717a8f5bSDavid Ahern {
999717a8f5bSDavid Ahern 	struct fib6_config cfg = {
1000717a8f5bSDavid Ahern 		.fc_table = table,
1001717a8f5bSDavid Ahern 		.fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
1002717a8f5bSDavid Ahern 		.fc_ifindex = nh->fib_nh_oif,
1003717a8f5bSDavid Ahern 		.fc_gateway = nh->fib_nh_gw6,
1004717a8f5bSDavid Ahern 	};
1005717a8f5bSDavid Ahern 	struct fib6_nh fib6_nh = {};
1006717a8f5bSDavid Ahern 	int err;
1007717a8f5bSDavid Ahern 
1008717a8f5bSDavid Ahern 	err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
1009717a8f5bSDavid Ahern 	if (!err) {
1010717a8f5bSDavid Ahern 		nh->fib_nh_dev = fib6_nh.fib_nh_dev;
1011717a8f5bSDavid Ahern 		dev_hold(nh->fib_nh_dev);
1012717a8f5bSDavid Ahern 		nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
1013717a8f5bSDavid Ahern 		nh->fib_nh_scope = RT_SCOPE_LINK;
1014717a8f5bSDavid Ahern 
1015717a8f5bSDavid Ahern 		ipv6_stub->fib6_nh_release(&fib6_nh);
1016717a8f5bSDavid Ahern 	}
1017717a8f5bSDavid Ahern 
1018717a8f5bSDavid Ahern 	return err;
1019717a8f5bSDavid Ahern }
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds /*
10226a31d2a9SEric Dumazet  * Picture
10236a31d2a9SEric Dumazet  * -------
10246a31d2a9SEric Dumazet  *
10256a31d2a9SEric Dumazet  * Semantics of nexthop is very messy by historical reasons.
10266a31d2a9SEric Dumazet  * We have to take into account, that:
10276a31d2a9SEric Dumazet  * a) gateway can be actually local interface address,
10286a31d2a9SEric Dumazet  *    so that gatewayed route is direct.
10296a31d2a9SEric Dumazet  * b) gateway must be on-link address, possibly
10306a31d2a9SEric Dumazet  *    described not by an ifaddr, but also by a direct route.
10316a31d2a9SEric Dumazet  * c) If both gateway and interface are specified, they should not
10326a31d2a9SEric Dumazet  *    contradict.
10336a31d2a9SEric Dumazet  * d) If we use tunnel routes, gateway could be not on-link.
10346a31d2a9SEric Dumazet  *
10356a31d2a9SEric Dumazet  * Attempt to reconcile all of these (alas, self-contradictory) conditions
10366a31d2a9SEric Dumazet  * results in pretty ugly and hairy code with obscure logic.
10376a31d2a9SEric Dumazet  *
10386a31d2a9SEric Dumazet  * I chose to generalized it instead, so that the size
10396a31d2a9SEric Dumazet  * of code does not increase practically, but it becomes
10406a31d2a9SEric Dumazet  * much more general.
10416a31d2a9SEric Dumazet  * Every prefix is assigned a "scope" value: "host" is local address,
10426a31d2a9SEric Dumazet  * "link" is direct route,
10436a31d2a9SEric Dumazet  * [ ... "site" ... "interior" ... ]
10446a31d2a9SEric Dumazet  * and "universe" is true gateway route with global meaning.
10456a31d2a9SEric Dumazet  *
10466a31d2a9SEric Dumazet  * Every prefix refers to a set of "nexthop"s (gw, oif),
10476a31d2a9SEric Dumazet  * where gw must have narrower scope. This recursion stops
10486a31d2a9SEric Dumazet  * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
10496a31d2a9SEric Dumazet  * which means that gw is forced to be on link.
10506a31d2a9SEric Dumazet  *
10516a31d2a9SEric Dumazet  * Code is still hairy, but now it is apparently logically
10526a31d2a9SEric Dumazet  * consistent and very flexible. F.e. as by-product it allows
10536a31d2a9SEric Dumazet  * to co-exists in peace independent exterior and interior
10546a31d2a9SEric Dumazet  * routing processes.
10556a31d2a9SEric Dumazet  *
10566a31d2a9SEric Dumazet  * Normally it looks as following.
10576a31d2a9SEric Dumazet  *
10586a31d2a9SEric Dumazet  * {universe prefix}  -> (gw, oif) [scope link]
10596a31d2a9SEric Dumazet  *		  |
10606a31d2a9SEric Dumazet  *		  |-> {link prefix} -> (gw, oif) [scope local]
10616a31d2a9SEric Dumazet  *					|
10626a31d2a9SEric Dumazet  *					|-> {local prefix} (terminal node)
10631da177e4SLinus Torvalds  */
1064448d7248SDavid Ahern static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
1065448d7248SDavid Ahern 			      u8 scope, struct netlink_ext_ack *extack)
10661da177e4SLinus Torvalds {
10676a31d2a9SEric Dumazet 	struct net_device *dev;
10681da177e4SLinus Torvalds 	struct fib_result res;
1069c3fee640SEnrico Weigelt 	int err = 0;
10701da177e4SLinus Torvalds 
1071b75ed8b1SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_ONLINK) {
107230bbaa19SDavid Ahern 		unsigned int addr_type;
10731da177e4SLinus Torvalds 
1074448d7248SDavid Ahern 		if (scope >= RT_SCOPE_LINK) {
1075448d7248SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
10761da177e4SLinus Torvalds 			return -EINVAL;
1077c3ab2b4eSDavid Ahern 		}
1078b75ed8b1SDavid Ahern 		dev = __dev_get_by_index(net, nh->fib_nh_oif);
1079066b1030SDavid Ahern 		if (!dev) {
1080066b1030SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
10811da177e4SLinus Torvalds 			return -ENODEV;
1082066b1030SDavid Ahern 		}
1083c3ab2b4eSDavid Ahern 		if (!(dev->flags & IFF_UP)) {
1084448d7248SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
10851da177e4SLinus Torvalds 			return -ENETDOWN;
1086c3ab2b4eSDavid Ahern 		}
1087448d7248SDavid Ahern 		addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
1088c3ab2b4eSDavid Ahern 		if (addr_type != RTN_UNICAST) {
1089448d7248SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
109030bbaa19SDavid Ahern 			return -EINVAL;
1091c3ab2b4eSDavid Ahern 		}
10928a3d0316SAndy Gospodarek 		if (!netif_carrier_ok(dev))
1093b75ed8b1SDavid Ahern 			nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1094b75ed8b1SDavid Ahern 		nh->fib_nh_dev = dev;
10951da177e4SLinus Torvalds 		dev_hold(dev);
1096b75ed8b1SDavid Ahern 		nh->fib_nh_scope = RT_SCOPE_LINK;
10971da177e4SLinus Torvalds 		return 0;
10981da177e4SLinus Torvalds 	}
1099ebc0ffaeSEric Dumazet 	rcu_read_lock();
11001da177e4SLinus Torvalds 	{
11013bfd8472SDavid Ahern 		struct fib_table *tbl = NULL;
11029ade2286SDavid S. Miller 		struct flowi4 fl4 = {
1103b75ed8b1SDavid Ahern 			.daddr = nh->fib_nh_gw4,
1104448d7248SDavid Ahern 			.flowi4_scope = scope + 1,
1105b75ed8b1SDavid Ahern 			.flowi4_oif = nh->fib_nh_oif,
11066a662719SCong Wang 			.flowi4_iif = LOOPBACK_IFINDEX,
11074e902c57SThomas Graf 		};
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 		/* It is not necessary, but requires a bit of thinking */
11109ade2286SDavid S. Miller 		if (fl4.flowi4_scope < RT_SCOPE_LINK)
11119ade2286SDavid S. Miller 			fl4.flowi4_scope = RT_SCOPE_LINK;
11123bfd8472SDavid Ahern 
11135eea3a63Sguodeqing 		if (table && table != RT_TABLE_MAIN)
1114448d7248SDavid Ahern 			tbl = fib_get_table(net, table);
11153bfd8472SDavid Ahern 
11163bfd8472SDavid Ahern 		if (tbl)
11173bfd8472SDavid Ahern 			err = fib_table_lookup(tbl, &fl4, &res,
11181e313678SEric Dumazet 					       FIB_LOOKUP_IGNORE_LINKSTATE |
11191e313678SEric Dumazet 					       FIB_LOOKUP_NOREF);
11204c9bcd11SDavid Ahern 
11214c9bcd11SDavid Ahern 		/* on error or if no table given do full lookup. This
11224c9bcd11SDavid Ahern 		 * is needed for example when nexthops are in the local
11234c9bcd11SDavid Ahern 		 * table rather than the given table
11244c9bcd11SDavid Ahern 		 */
11254c9bcd11SDavid Ahern 		if (!tbl || err) {
11260eeb075fSAndy Gospodarek 			err = fib_lookup(net, &fl4, &res,
11270eeb075fSAndy Gospodarek 					 FIB_LOOKUP_IGNORE_LINKSTATE);
11284c9bcd11SDavid Ahern 		}
11294c9bcd11SDavid Ahern 
1130ebc0ffaeSEric Dumazet 		if (err) {
1131448d7248SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1132448d7248SDavid Ahern 			goto out;
11331da177e4SLinus Torvalds 		}
1134ebc0ffaeSEric Dumazet 	}
1135448d7248SDavid Ahern 
11361da177e4SLinus Torvalds 	err = -EINVAL;
1137c3ab2b4eSDavid Ahern 	if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1138c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
11391da177e4SLinus Torvalds 		goto out;
1140c3ab2b4eSDavid Ahern 	}
1141b75ed8b1SDavid Ahern 	nh->fib_nh_scope = res.scope;
1142b75ed8b1SDavid Ahern 	nh->fib_nh_oif = FIB_RES_OIF(res);
1143b75ed8b1SDavid Ahern 	nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1144c3ab2b4eSDavid Ahern 	if (!dev) {
1145c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack,
1146c3ab2b4eSDavid Ahern 			       "No egress device for nexthop gateway");
11471da177e4SLinus Torvalds 		goto out;
1148c3ab2b4eSDavid Ahern 	}
11496a31d2a9SEric Dumazet 	dev_hold(dev);
11508a3d0316SAndy Gospodarek 	if (!netif_carrier_ok(dev))
1151b75ed8b1SDavid Ahern 		nh->fib_nh_flags |= RTNH_F_LINKDOWN;
11528723e1b4SEric Dumazet 	err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
1153448d7248SDavid Ahern out:
1154448d7248SDavid Ahern 	rcu_read_unlock();
1155448d7248SDavid Ahern 	return err;
1156448d7248SDavid Ahern }
1157448d7248SDavid Ahern 
1158448d7248SDavid Ahern static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1159448d7248SDavid Ahern 			      struct netlink_ext_ack *extack)
1160448d7248SDavid Ahern {
11611da177e4SLinus Torvalds 	struct in_device *in_dev;
1162448d7248SDavid Ahern 	int err;
11631da177e4SLinus Torvalds 
1164b75ed8b1SDavid Ahern 	if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1165c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack,
1166c3ab2b4eSDavid Ahern 			       "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
11671da177e4SLinus Torvalds 		return -EINVAL;
1168c3ab2b4eSDavid Ahern 	}
1169448d7248SDavid Ahern 
11708723e1b4SEric Dumazet 	rcu_read_lock();
1171448d7248SDavid Ahern 
11728723e1b4SEric Dumazet 	err = -ENODEV;
1173b75ed8b1SDavid Ahern 	in_dev = inetdev_by_index(net, nh->fib_nh_oif);
117451456b29SIan Morris 	if (!in_dev)
11758723e1b4SEric Dumazet 		goto out;
11768723e1b4SEric Dumazet 	err = -ENETDOWN;
1177c3ab2b4eSDavid Ahern 	if (!(in_dev->dev->flags & IFF_UP)) {
1178c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
11798723e1b4SEric Dumazet 		goto out;
1180c3ab2b4eSDavid Ahern 	}
1181448d7248SDavid Ahern 
1182b75ed8b1SDavid Ahern 	nh->fib_nh_dev = in_dev->dev;
1183b75ed8b1SDavid Ahern 	dev_hold(nh->fib_nh_dev);
1184b75ed8b1SDavid Ahern 	nh->fib_nh_scope = RT_SCOPE_HOST;
1185b75ed8b1SDavid Ahern 	if (!netif_carrier_ok(nh->fib_nh_dev))
1186b75ed8b1SDavid Ahern 		nh->fib_nh_flags |= RTNH_F_LINKDOWN;
11878723e1b4SEric Dumazet 	err = 0;
11888723e1b4SEric Dumazet out:
11898723e1b4SEric Dumazet 	rcu_read_unlock();
11908723e1b4SEric Dumazet 	return err;
11911da177e4SLinus Torvalds }
11921da177e4SLinus Torvalds 
1193ac1fab2dSDavid Ahern int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1194448d7248SDavid Ahern 		 struct netlink_ext_ack *extack)
1195448d7248SDavid Ahern {
1196448d7248SDavid Ahern 	int err;
1197448d7248SDavid Ahern 
1198448d7248SDavid Ahern 	if (nh->fib_nh_gw_family == AF_INET)
1199ac1fab2dSDavid Ahern 		err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
1200717a8f5bSDavid Ahern 	else if (nh->fib_nh_gw_family == AF_INET6)
1201717a8f5bSDavid Ahern 		err = fib_check_nh_v6_gw(net, nh, table, extack);
1202448d7248SDavid Ahern 	else
1203448d7248SDavid Ahern 		err = fib_check_nh_nongw(net, nh, extack);
1204448d7248SDavid Ahern 
1205448d7248SDavid Ahern 	return err;
1206448d7248SDavid Ahern }
1207448d7248SDavid Ahern 
120881f7bf6cSAl Viro static inline unsigned int fib_laddr_hashfn(__be32 val)
12091da177e4SLinus Torvalds {
1210123b9731SDavid S. Miller 	unsigned int mask = (fib_info_hash_size - 1);
12111da177e4SLinus Torvalds 
12126a31d2a9SEric Dumazet 	return ((__force u32)val ^
12136a31d2a9SEric Dumazet 		((__force u32)val >> 7) ^
12146a31d2a9SEric Dumazet 		((__force u32)val >> 14)) & mask;
12151da177e4SLinus Torvalds }
12161da177e4SLinus Torvalds 
1217123b9731SDavid S. Miller static struct hlist_head *fib_info_hash_alloc(int bytes)
12181da177e4SLinus Torvalds {
12191da177e4SLinus Torvalds 	if (bytes <= PAGE_SIZE)
122088f83491SJoonwoo Park 		return kzalloc(bytes, GFP_KERNEL);
12211da177e4SLinus Torvalds 	else
12221da177e4SLinus Torvalds 		return (struct hlist_head *)
12236a31d2a9SEric Dumazet 			__get_free_pages(GFP_KERNEL | __GFP_ZERO,
12246a31d2a9SEric Dumazet 					 get_order(bytes));
12251da177e4SLinus Torvalds }
12261da177e4SLinus Torvalds 
1227123b9731SDavid S. Miller static void fib_info_hash_free(struct hlist_head *hash, int bytes)
12281da177e4SLinus Torvalds {
12291da177e4SLinus Torvalds 	if (!hash)
12301da177e4SLinus Torvalds 		return;
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	if (bytes <= PAGE_SIZE)
12331da177e4SLinus Torvalds 		kfree(hash);
12341da177e4SLinus Torvalds 	else
12351da177e4SLinus Torvalds 		free_pages((unsigned long) hash, get_order(bytes));
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds 
1238123b9731SDavid S. Miller static void fib_info_hash_move(struct hlist_head *new_info_hash,
12391da177e4SLinus Torvalds 			       struct hlist_head *new_laddrhash,
12401da177e4SLinus Torvalds 			       unsigned int new_size)
12411da177e4SLinus Torvalds {
1242b7656e7fSDavid S. Miller 	struct hlist_head *old_info_hash, *old_laddrhash;
1243123b9731SDavid S. Miller 	unsigned int old_size = fib_info_hash_size;
1244b7656e7fSDavid S. Miller 	unsigned int i, bytes;
12451da177e4SLinus Torvalds 
1246832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
1247b7656e7fSDavid S. Miller 	old_info_hash = fib_info_hash;
1248b7656e7fSDavid S. Miller 	old_laddrhash = fib_info_laddrhash;
1249123b9731SDavid S. Miller 	fib_info_hash_size = new_size;
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	for (i = 0; i < old_size; i++) {
12521da177e4SLinus Torvalds 		struct hlist_head *head = &fib_info_hash[i];
1253b67bfe0dSSasha Levin 		struct hlist_node *n;
12541da177e4SLinus Torvalds 		struct fib_info *fi;
12551da177e4SLinus Torvalds 
1256b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(fi, n, head, fib_hash) {
12571da177e4SLinus Torvalds 			struct hlist_head *dest;
12581da177e4SLinus Torvalds 			unsigned int new_hash;
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 			new_hash = fib_info_hashfn(fi);
12611da177e4SLinus Torvalds 			dest = &new_info_hash[new_hash];
12621da177e4SLinus Torvalds 			hlist_add_head(&fi->fib_hash, dest);
12631da177e4SLinus Torvalds 		}
12641da177e4SLinus Torvalds 	}
12651da177e4SLinus Torvalds 	fib_info_hash = new_info_hash;
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	for (i = 0; i < old_size; i++) {
12681da177e4SLinus Torvalds 		struct hlist_head *lhead = &fib_info_laddrhash[i];
1269b67bfe0dSSasha Levin 		struct hlist_node *n;
12701da177e4SLinus Torvalds 		struct fib_info *fi;
12711da177e4SLinus Torvalds 
1272b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
12731da177e4SLinus Torvalds 			struct hlist_head *ldest;
12741da177e4SLinus Torvalds 			unsigned int new_hash;
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 			new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
12771da177e4SLinus Torvalds 			ldest = &new_laddrhash[new_hash];
12781da177e4SLinus Torvalds 			hlist_add_head(&fi->fib_lhash, ldest);
12791da177e4SLinus Torvalds 		}
12801da177e4SLinus Torvalds 	}
12811da177e4SLinus Torvalds 	fib_info_laddrhash = new_laddrhash;
12821da177e4SLinus Torvalds 
1283832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
1284b7656e7fSDavid S. Miller 
1285b7656e7fSDavid S. Miller 	bytes = old_size * sizeof(struct hlist_head *);
1286123b9731SDavid S. Miller 	fib_info_hash_free(old_info_hash, bytes);
1287123b9731SDavid S. Miller 	fib_info_hash_free(old_laddrhash, bytes);
12881da177e4SLinus Torvalds }
12891da177e4SLinus Torvalds 
1290dcb1ecb5SDavid Ahern __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
1291c3669486SDavid Ahern 				 unsigned char scope)
1292436c3b66SDavid S. Miller {
1293dcb1ecb5SDavid Ahern 	struct fib_nh *nh;
1294dcb1ecb5SDavid Ahern 
1295dcb1ecb5SDavid Ahern 	if (nhc->nhc_family != AF_INET)
1296dcb1ecb5SDavid Ahern 		return inet_select_addr(nhc->nhc_dev, 0, scope);
1297dcb1ecb5SDavid Ahern 
1298dcb1ecb5SDavid Ahern 	nh = container_of(nhc, struct fib_nh, nh_common);
1299c3669486SDavid Ahern 	nh->nh_saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
1300436c3b66SDavid S. Miller 	nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1301436c3b66SDavid S. Miller 
1302436c3b66SDavid S. Miller 	return nh->nh_saddr;
1303436c3b66SDavid S. Miller }
1304436c3b66SDavid S. Miller 
1305eba618abSDavid Ahern __be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1306eba618abSDavid Ahern {
1307eba618abSDavid Ahern 	struct fib_nh_common *nhc = res->nhc;
1308eba618abSDavid Ahern 
1309eba618abSDavid Ahern 	if (res->fi->fib_prefsrc)
1310eba618abSDavid Ahern 		return res->fi->fib_prefsrc;
1311eba618abSDavid Ahern 
1312dcb1ecb5SDavid Ahern 	if (nhc->nhc_family == AF_INET) {
1313dcb1ecb5SDavid Ahern 		struct fib_nh *nh;
1314dcb1ecb5SDavid Ahern 
1315eba618abSDavid Ahern 		nh = container_of(nhc, struct fib_nh, nh_common);
1316eba618abSDavid Ahern 		if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1317eba618abSDavid Ahern 			return nh->nh_saddr;
1318dcb1ecb5SDavid Ahern 	}
1319eba618abSDavid Ahern 
1320dcb1ecb5SDavid Ahern 	return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
1321eba618abSDavid Ahern }
1322eba618abSDavid Ahern 
1323021dd3b8SDavid Ahern static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1324021dd3b8SDavid Ahern {
1325021dd3b8SDavid Ahern 	if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1326021dd3b8SDavid Ahern 	    fib_prefsrc != cfg->fc_dst) {
13279b8ff518SDavid Ahern 		u32 tb_id = cfg->fc_table;
1328e1b8d903SDavid Ahern 		int rc;
1329021dd3b8SDavid Ahern 
1330021dd3b8SDavid Ahern 		if (tb_id == RT_TABLE_MAIN)
1331021dd3b8SDavid Ahern 			tb_id = RT_TABLE_LOCAL;
1332021dd3b8SDavid Ahern 
1333e1b8d903SDavid Ahern 		rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1334e1b8d903SDavid Ahern 					  fib_prefsrc, tb_id);
1335e1b8d903SDavid Ahern 
1336e1b8d903SDavid Ahern 		if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1337e1b8d903SDavid Ahern 			rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1338e1b8d903SDavid Ahern 						  fib_prefsrc, RT_TABLE_LOCAL);
1339021dd3b8SDavid Ahern 		}
1340e1b8d903SDavid Ahern 
1341e1b8d903SDavid Ahern 		if (rc != RTN_LOCAL)
1342e1b8d903SDavid Ahern 			return false;
1343021dd3b8SDavid Ahern 	}
1344021dd3b8SDavid Ahern 	return true;
1345021dd3b8SDavid Ahern }
1346021dd3b8SDavid Ahern 
13476d8422a1SDavid Ahern struct fib_info *fib_create_info(struct fib_config *cfg,
13486d8422a1SDavid Ahern 				 struct netlink_ext_ack *extack)
13491da177e4SLinus Torvalds {
13501da177e4SLinus Torvalds 	int err;
13511da177e4SLinus Torvalds 	struct fib_info *fi = NULL;
13524c7e8084SDavid Ahern 	struct nexthop *nh = NULL;
13531da177e4SLinus Torvalds 	struct fib_info *ofi;
13541da177e4SLinus Torvalds 	int nhs = 1;
13557462bd74SDenis V. Lunev 	struct net *net = cfg->fc_nlinfo.nl_net;
13561da177e4SLinus Torvalds 
13574c8237cdSDavid S. Miller 	if (cfg->fc_type > RTN_MAX)
13584c8237cdSDavid S. Miller 		goto err_inval;
13594c8237cdSDavid S. Miller 
13601da177e4SLinus Torvalds 	/* Fast check to catch the most weird cases */
1361c3ab2b4eSDavid Ahern 	if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1362c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid scope");
13631da177e4SLinus Torvalds 		goto err_inval;
1364c3ab2b4eSDavid Ahern 	}
13651da177e4SLinus Torvalds 
1366c3ab2b4eSDavid Ahern 	if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1367c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack,
1368c3ab2b4eSDavid Ahern 			       "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
136980610229SJulian Anastasov 		goto err_inval;
1370c3ab2b4eSDavid Ahern 	}
137180610229SJulian Anastasov 
1372493ced1aSDavid Ahern 	if (cfg->fc_nh_id) {
13736c48ea5fSDavid Ahern 		if (!cfg->fc_mx) {
13746c48ea5fSDavid Ahern 			fi = fib_find_info_nh(net, cfg);
13756c48ea5fSDavid Ahern 			if (fi) {
13766c48ea5fSDavid Ahern 				fi->fib_treeref++;
13776c48ea5fSDavid Ahern 				return fi;
13786c48ea5fSDavid Ahern 			}
13796c48ea5fSDavid Ahern 		}
13806c48ea5fSDavid Ahern 
1381493ced1aSDavid Ahern 		nh = nexthop_find_by_id(net, cfg->fc_nh_id);
1382493ced1aSDavid Ahern 		if (!nh) {
1383493ced1aSDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
1384493ced1aSDavid Ahern 			goto err_inval;
1385493ced1aSDavid Ahern 		}
1386493ced1aSDavid Ahern 		nhs = 0;
1387493ced1aSDavid Ahern 	}
1388493ced1aSDavid Ahern 
13891da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
13904e902c57SThomas Graf 	if (cfg->fc_mp) {
13916d8422a1SDavid Ahern 		nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
13921da177e4SLinus Torvalds 		if (nhs == 0)
13931da177e4SLinus Torvalds 			goto err_inval;
13941da177e4SLinus Torvalds 	}
13951da177e4SLinus Torvalds #endif
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds 	err = -ENOBUFS;
1398123b9731SDavid S. Miller 	if (fib_info_cnt >= fib_info_hash_size) {
1399123b9731SDavid S. Miller 		unsigned int new_size = fib_info_hash_size << 1;
14001da177e4SLinus Torvalds 		struct hlist_head *new_info_hash;
14011da177e4SLinus Torvalds 		struct hlist_head *new_laddrhash;
14021da177e4SLinus Torvalds 		unsigned int bytes;
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 		if (!new_size)
1405d94ce9b2SEric Dumazet 			new_size = 16;
14061da177e4SLinus Torvalds 		bytes = new_size * sizeof(struct hlist_head *);
1407123b9731SDavid S. Miller 		new_info_hash = fib_info_hash_alloc(bytes);
1408123b9731SDavid S. Miller 		new_laddrhash = fib_info_hash_alloc(bytes);
14091da177e4SLinus Torvalds 		if (!new_info_hash || !new_laddrhash) {
1410123b9731SDavid S. Miller 			fib_info_hash_free(new_info_hash, bytes);
1411123b9731SDavid S. Miller 			fib_info_hash_free(new_laddrhash, bytes);
141288f83491SJoonwoo Park 		} else
1413123b9731SDavid S. Miller 			fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
14141da177e4SLinus Torvalds 
1415123b9731SDavid S. Miller 		if (!fib_info_hash_size)
14161da177e4SLinus Torvalds 			goto failure;
14171da177e4SLinus Torvalds 	}
14181da177e4SLinus Torvalds 
14191f533ba6SGustavo A. R. Silva 	fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
142051456b29SIan Morris 	if (!fi)
14211da177e4SLinus Torvalds 		goto failure;
1422767a2217SDavid Ahern 	fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
1423d7e774f3SDavid Ahern 					      cfg->fc_mx_len, extack);
142488e235b8SEnrico Weigelt 	if (IS_ERR(fi->fib_metrics)) {
1425767a2217SDavid Ahern 		err = PTR_ERR(fi->fib_metrics);
1426187e5b3aSEric Dumazet 		kfree(fi);
1427187e5b3aSEric Dumazet 		return ERR_PTR(err);
1428187e5b3aSEric Dumazet 	}
1429767a2217SDavid Ahern 
1430187e5b3aSEric Dumazet 	fib_info_cnt++;
1431efd7ef1cSEric W. Biederman 	fi->fib_net = net;
14324e902c57SThomas Graf 	fi->fib_protocol = cfg->fc_protocol;
143337e826c5SDavid S. Miller 	fi->fib_scope = cfg->fc_scope;
14344e902c57SThomas Graf 	fi->fib_flags = cfg->fc_flags;
14354e902c57SThomas Graf 	fi->fib_priority = cfg->fc_priority;
14364e902c57SThomas Graf 	fi->fib_prefsrc = cfg->fc_prefsrc;
1437f4ef85bbSEric Dumazet 	fi->fib_type = cfg->fc_type;
14385a56a0b3SMark Tomlinson 	fi->fib_tb_id = cfg->fc_table;
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 	fi->fib_nhs = nhs;
14414c7e8084SDavid Ahern 	if (nh) {
14424c7e8084SDavid Ahern 		if (!nexthop_get(nh)) {
14434c7e8084SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
14444c7e8084SDavid Ahern 			err = -EINVAL;
14454c7e8084SDavid Ahern 		} else {
14464c7e8084SDavid Ahern 			err = 0;
14474c7e8084SDavid Ahern 			fi->nh = nh;
14484c7e8084SDavid Ahern 		}
14494c7e8084SDavid Ahern 	} else {
14501da177e4SLinus Torvalds 		change_nexthops(fi) {
145171fceff0SDavid S. Miller 			nexthop_nh->nh_parent = fi;
14521da177e4SLinus Torvalds 		} endfor_nexthops(fi)
14531da177e4SLinus Torvalds 
1454e4516ef6SDavid Ahern 		if (cfg->fc_mp)
14554c7e8084SDavid Ahern 			err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
14564c7e8084SDavid Ahern 					  extack);
1457e4516ef6SDavid Ahern 		else
1458e4516ef6SDavid Ahern 			err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
14594c7e8084SDavid Ahern 	}
1460e4516ef6SDavid Ahern 
14614e902c57SThomas Graf 	if (err != 0)
14621da177e4SLinus Torvalds 		goto failure;
14631da177e4SLinus Torvalds 
14644e902c57SThomas Graf 	if (fib_props[cfg->fc_type].error) {
1465f35b794bSDavid Ahern 		if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
1466c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
1467c3ab2b4eSDavid Ahern 				       "Gateway, device and multipath can not be specified for this route type");
14681da177e4SLinus Torvalds 			goto err_inval;
1469c3ab2b4eSDavid Ahern 		}
14701da177e4SLinus Torvalds 		goto link_it;
14714c8237cdSDavid S. Miller 	} else {
14724c8237cdSDavid S. Miller 		switch (cfg->fc_type) {
14734c8237cdSDavid S. Miller 		case RTN_UNICAST:
14744c8237cdSDavid S. Miller 		case RTN_LOCAL:
14754c8237cdSDavid S. Miller 		case RTN_BROADCAST:
14764c8237cdSDavid S. Miller 		case RTN_ANYCAST:
14774c8237cdSDavid S. Miller 		case RTN_MULTICAST:
14784c8237cdSDavid S. Miller 			break;
14794c8237cdSDavid S. Miller 		default:
1480c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid route type");
14814c8237cdSDavid S. Miller 			goto err_inval;
14824c8237cdSDavid S. Miller 		}
14831da177e4SLinus Torvalds 	}
14841da177e4SLinus Torvalds 
1485c3ab2b4eSDavid Ahern 	if (cfg->fc_scope > RT_SCOPE_HOST) {
1486c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid scope");
14871da177e4SLinus Torvalds 		goto err_inval;
1488c3ab2b4eSDavid Ahern 	}
14891da177e4SLinus Torvalds 
14904c7e8084SDavid Ahern 	if (fi->nh) {
14914c7e8084SDavid Ahern 		err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
14924c7e8084SDavid Ahern 		if (err)
14934c7e8084SDavid Ahern 			goto failure;
14944c7e8084SDavid Ahern 	} else if (cfg->fc_scope == RT_SCOPE_HOST) {
14951da177e4SLinus Torvalds 		struct fib_nh *nh = fi->fib_nh;
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 		/* Local address is added. */
1498c3ab2b4eSDavid Ahern 		if (nhs != 1) {
1499c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
1500c3ab2b4eSDavid Ahern 				       "Route with host scope can not have multiple nexthops");
15016d8422a1SDavid Ahern 			goto err_inval;
1502c3ab2b4eSDavid Ahern 		}
1503bdf00467SDavid Ahern 		if (nh->fib_nh_gw_family) {
1504c3ab2b4eSDavid Ahern 			NL_SET_ERR_MSG(extack,
1505c3ab2b4eSDavid Ahern 				       "Route with host scope can not have a gateway");
15061da177e4SLinus Torvalds 			goto err_inval;
1507c3ab2b4eSDavid Ahern 		}
1508b75ed8b1SDavid Ahern 		nh->fib_nh_scope = RT_SCOPE_NOWHERE;
15095481d73fSDavid Ahern 		nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
15101da177e4SLinus Torvalds 		err = -ENODEV;
1511b75ed8b1SDavid Ahern 		if (!nh->fib_nh_dev)
15121da177e4SLinus Torvalds 			goto failure;
15131da177e4SLinus Torvalds 	} else {
15148a3d0316SAndy Gospodarek 		int linkdown = 0;
15158a3d0316SAndy Gospodarek 
15161da177e4SLinus Torvalds 		change_nexthops(fi) {
1517ac1fab2dSDavid Ahern 			err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1518ac1fab2dSDavid Ahern 					   cfg->fc_table, cfg->fc_scope,
1519ac1fab2dSDavid Ahern 					   extack);
15206a31d2a9SEric Dumazet 			if (err != 0)
15211da177e4SLinus Torvalds 				goto failure;
1522b75ed8b1SDavid Ahern 			if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
15238a3d0316SAndy Gospodarek 				linkdown++;
15241da177e4SLinus Torvalds 		} endfor_nexthops(fi)
15258a3d0316SAndy Gospodarek 		if (linkdown == fi->fib_nhs)
15268a3d0316SAndy Gospodarek 			fi->fib_flags |= RTNH_F_LINKDOWN;
15271da177e4SLinus Torvalds 	}
15281da177e4SLinus Torvalds 
1529c3ab2b4eSDavid Ahern 	if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1530c3ab2b4eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
15311da177e4SLinus Torvalds 		goto err_inval;
1532c3ab2b4eSDavid Ahern 	}
15331da177e4SLinus Torvalds 
15344c7e8084SDavid Ahern 	if (!fi->nh) {
15351fc050a1SDavid S. Miller 		change_nexthops(fi) {
1536dcb1ecb5SDavid Ahern 			fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
1537dcb1ecb5SDavid Ahern 						  fi->fib_scope);
153819a9d136SDavid Ahern 			if (nexthop_nh->fib_nh_gw_family == AF_INET6)
153919a9d136SDavid Ahern 				fi->fib_nh_is_v6 = true;
15401fc050a1SDavid S. Miller 		} endfor_nexthops(fi)
15411fc050a1SDavid S. Miller 
15420e884c78SPeter Nørlund 		fib_rebalance(fi);
15434c7e8084SDavid Ahern 	}
15440e884c78SPeter Nørlund 
15451da177e4SLinus Torvalds link_it:
15466a31d2a9SEric Dumazet 	ofi = fib_find_info(fi);
15476a31d2a9SEric Dumazet 	if (ofi) {
15481da177e4SLinus Torvalds 		fi->fib_dead = 1;
15491da177e4SLinus Torvalds 		free_fib_info(fi);
15501da177e4SLinus Torvalds 		ofi->fib_treeref++;
15511da177e4SLinus Torvalds 		return ofi;
15521da177e4SLinus Torvalds 	}
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds 	fi->fib_treeref++;
15550029c0deSReshetova, Elena 	refcount_set(&fi->fib_clntref, 1);
1556832b4c5eSStephen Hemminger 	spin_lock_bh(&fib_info_lock);
15571da177e4SLinus Torvalds 	hlist_add_head(&fi->fib_hash,
15581da177e4SLinus Torvalds 		       &fib_info_hash[fib_info_hashfn(fi)]);
15591da177e4SLinus Torvalds 	if (fi->fib_prefsrc) {
15601da177e4SLinus Torvalds 		struct hlist_head *head;
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 		head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
15631da177e4SLinus Torvalds 		hlist_add_head(&fi->fib_lhash, head);
15641da177e4SLinus Torvalds 	}
15654c7e8084SDavid Ahern 	if (fi->nh) {
15664c7e8084SDavid Ahern 		list_add(&fi->nh_list, &nh->fi_list);
15674c7e8084SDavid Ahern 	} else {
15681da177e4SLinus Torvalds 		change_nexthops(fi) {
15691da177e4SLinus Torvalds 			struct hlist_head *head;
15701da177e4SLinus Torvalds 			unsigned int hash;
15711da177e4SLinus Torvalds 
1572b75ed8b1SDavid Ahern 			if (!nexthop_nh->fib_nh_dev)
15731da177e4SLinus Torvalds 				continue;
1574b75ed8b1SDavid Ahern 			hash = fib_devindex_hashfn(nexthop_nh->fib_nh_dev->ifindex);
15751da177e4SLinus Torvalds 			head = &fib_info_devhash[hash];
157671fceff0SDavid S. Miller 			hlist_add_head(&nexthop_nh->nh_hash, head);
15771da177e4SLinus Torvalds 		} endfor_nexthops(fi)
15784c7e8084SDavid Ahern 	}
1579832b4c5eSStephen Hemminger 	spin_unlock_bh(&fib_info_lock);
15801da177e4SLinus Torvalds 	return fi;
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds err_inval:
15831da177e4SLinus Torvalds 	err = -EINVAL;
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds failure:
15861da177e4SLinus Torvalds 	if (fi) {
15871da177e4SLinus Torvalds 		fi->fib_dead = 1;
15881da177e4SLinus Torvalds 		free_fib_info(fi);
15891da177e4SLinus Torvalds 	}
15904e902c57SThomas Graf 
15914e902c57SThomas Graf 	return ERR_PTR(err);
15921da177e4SLinus Torvalds }
15931da177e4SLinus Torvalds 
1594c0a72077SDavid Ahern int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
15957bdf4de1SDonald Sharp 		     u8 rt_family, unsigned char *flags, bool skip_oif)
1596b0f60193SDavid Ahern {
1597c2364199SDavid Ahern 	if (nhc->nhc_flags & RTNH_F_DEAD)
1598b0f60193SDavid Ahern 		*flags |= RTNH_F_DEAD;
1599b0f60193SDavid Ahern 
1600c2364199SDavid Ahern 	if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
1601b0f60193SDavid Ahern 		*flags |= RTNH_F_LINKDOWN;
1602b0f60193SDavid Ahern 
1603b0f60193SDavid Ahern 		rcu_read_lock();
1604c2364199SDavid Ahern 		switch (nhc->nhc_family) {
1605c2364199SDavid Ahern 		case AF_INET:
1606c2364199SDavid Ahern 			if (ip_ignore_linkdown(nhc->nhc_dev))
1607b0f60193SDavid Ahern 				*flags |= RTNH_F_DEAD;
1608c2364199SDavid Ahern 			break;
1609c0a72077SDavid Ahern 		case AF_INET6:
1610c0a72077SDavid Ahern 			if (ip6_ignore_linkdown(nhc->nhc_dev))
1611c0a72077SDavid Ahern 				*flags |= RTNH_F_DEAD;
1612c0a72077SDavid Ahern 			break;
1613c2364199SDavid Ahern 		}
1614b0f60193SDavid Ahern 		rcu_read_unlock();
1615b0f60193SDavid Ahern 	}
1616b0f60193SDavid Ahern 
1617bdf00467SDavid Ahern 	switch (nhc->nhc_gw_family) {
1618c2364199SDavid Ahern 	case AF_INET:
1619c2364199SDavid Ahern 		if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1620b0f60193SDavid Ahern 			goto nla_put_failure;
1621c2364199SDavid Ahern 		break;
1622c0a72077SDavid Ahern 	case AF_INET6:
1623d1566268SDavid Ahern 		/* if gateway family does not match nexthop family
1624d1566268SDavid Ahern 		 * gateway is encoded as RTA_VIA
1625d1566268SDavid Ahern 		 */
16267bdf4de1SDonald Sharp 		if (rt_family != nhc->nhc_gw_family) {
1627d1566268SDavid Ahern 			int alen = sizeof(struct in6_addr);
1628d1566268SDavid Ahern 			struct nlattr *nla;
1629d1566268SDavid Ahern 			struct rtvia *via;
1630d1566268SDavid Ahern 
1631d1566268SDavid Ahern 			nla = nla_reserve(skb, RTA_VIA, alen + 2);
1632d1566268SDavid Ahern 			if (!nla)
1633c0a72077SDavid Ahern 				goto nla_put_failure;
1634d1566268SDavid Ahern 
1635d1566268SDavid Ahern 			via = nla_data(nla);
1636d1566268SDavid Ahern 			via->rtvia_family = AF_INET6;
1637d1566268SDavid Ahern 			memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
1638d1566268SDavid Ahern 		} else if (nla_put_in6_addr(skb, RTA_GATEWAY,
1639d1566268SDavid Ahern 					    &nhc->nhc_gw.ipv6) < 0) {
1640d1566268SDavid Ahern 			goto nla_put_failure;
1641d1566268SDavid Ahern 		}
1642c0a72077SDavid Ahern 		break;
1643c2364199SDavid Ahern 	}
1644b0f60193SDavid Ahern 
1645ca787e0bSIdo Schimmel 	*flags |= (nhc->nhc_flags &
1646ca787e0bSIdo Schimmel 		   (RTNH_F_ONLINK | RTNH_F_OFFLOAD | RTNH_F_TRAP));
1647b0f60193SDavid Ahern 
1648c2364199SDavid Ahern 	if (!skip_oif && nhc->nhc_dev &&
1649c2364199SDavid Ahern 	    nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
1650b0f60193SDavid Ahern 		goto nla_put_failure;
1651b0f60193SDavid Ahern 
1652c2364199SDavid Ahern 	if (nhc->nhc_lwtstate &&
1653ffa8ce54SDavid Ahern 	    lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1654ffa8ce54SDavid Ahern 				RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
1655b0f60193SDavid Ahern 		goto nla_put_failure;
1656b0f60193SDavid Ahern 
1657b0f60193SDavid Ahern 	return 0;
1658b0f60193SDavid Ahern 
1659b0f60193SDavid Ahern nla_put_failure:
1660b0f60193SDavid Ahern 	return -EMSGSIZE;
1661b0f60193SDavid Ahern }
1662c0a72077SDavid Ahern EXPORT_SYMBOL_GPL(fib_nexthop_info);
1663b0f60193SDavid Ahern 
1664c0a72077SDavid Ahern #if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1665c0a72077SDavid Ahern int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
16667bdf4de1SDonald Sharp 		    int nh_weight, u8 rt_family)
1667b0f60193SDavid Ahern {
1668c2364199SDavid Ahern 	const struct net_device *dev = nhc->nhc_dev;
1669b0f60193SDavid Ahern 	struct rtnexthop *rtnh;
1670ecc5663cSDavid Ahern 	unsigned char flags = 0;
1671b0f60193SDavid Ahern 
1672b0f60193SDavid Ahern 	rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1673b0f60193SDavid Ahern 	if (!rtnh)
1674b0f60193SDavid Ahern 		goto nla_put_failure;
1675b0f60193SDavid Ahern 
1676c2364199SDavid Ahern 	rtnh->rtnh_hops = nh_weight - 1;
1677b0f60193SDavid Ahern 	rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1678b0f60193SDavid Ahern 
16797bdf4de1SDonald Sharp 	if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
1680b0f60193SDavid Ahern 		goto nla_put_failure;
1681b0f60193SDavid Ahern 
1682b0f60193SDavid Ahern 	rtnh->rtnh_flags = flags;
1683b0f60193SDavid Ahern 
1684b0f60193SDavid Ahern 	/* length of rtnetlink header + attributes */
1685b0f60193SDavid Ahern 	rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
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_add_nexthop);
1693c2364199SDavid Ahern #endif
1694b0f60193SDavid Ahern 
1695c2364199SDavid Ahern #ifdef CONFIG_IP_ROUTE_MULTIPATH
1696b0f60193SDavid Ahern static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1697b0f60193SDavid Ahern {
1698b0f60193SDavid Ahern 	struct nlattr *mp;
1699b0f60193SDavid Ahern 
1700ae0be8deSMichal Kubecek 	mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
1701b0f60193SDavid Ahern 	if (!mp)
1702b0f60193SDavid Ahern 		goto nla_put_failure;
1703b0f60193SDavid Ahern 
17044c7e8084SDavid Ahern 	if (unlikely(fi->nh)) {
17057bdf4de1SDonald Sharp 		if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
17064c7e8084SDavid Ahern 			goto nla_put_failure;
17074c7e8084SDavid Ahern 		goto mp_end;
17084c7e8084SDavid Ahern 	}
17094c7e8084SDavid Ahern 
1710b0f60193SDavid Ahern 	for_nexthops(fi) {
17117bdf4de1SDonald Sharp 		if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
17127bdf4de1SDonald Sharp 				    AF_INET) < 0)
1713b0f60193SDavid Ahern 			goto nla_put_failure;
1714b0f60193SDavid Ahern #ifdef CONFIG_IP_ROUTE_CLASSID
1715b0f60193SDavid Ahern 		if (nh->nh_tclassid &&
1716b0f60193SDavid Ahern 		    nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1717b0f60193SDavid Ahern 			goto nla_put_failure;
1718b0f60193SDavid Ahern #endif
1719b0f60193SDavid Ahern 	} endfor_nexthops(fi);
1720b0f60193SDavid Ahern 
17214c7e8084SDavid Ahern mp_end:
1722b0f60193SDavid Ahern 	nla_nest_end(skb, mp);
1723b0f60193SDavid Ahern 
1724b0f60193SDavid Ahern 	return 0;
1725b0f60193SDavid Ahern 
1726b0f60193SDavid Ahern nla_put_failure:
1727b0f60193SDavid Ahern 	return -EMSGSIZE;
1728b0f60193SDavid Ahern }
1729b0f60193SDavid Ahern #else
1730b0f60193SDavid Ahern static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1731b0f60193SDavid Ahern {
1732b0f60193SDavid Ahern 	return 0;
1733b0f60193SDavid Ahern }
1734b0f60193SDavid Ahern #endif
1735b0f60193SDavid Ahern 
173615e47304SEric W. Biederman int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
173708554789SAmit Cohen 		  const struct fib_rt_info *fri, unsigned int flags)
17381da177e4SLinus Torvalds {
17391e301fd0SIdo Schimmel 	unsigned int nhs = fib_info_num_path(fri->fi);
17401e301fd0SIdo Schimmel 	struct fib_info *fi = fri->fi;
17411e301fd0SIdo Schimmel 	u32 tb_id = fri->tb_id;
17421da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
1743be403ea1SThomas Graf 	struct rtmsg *rtm;
17441da177e4SLinus Torvalds 
174515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
174651456b29SIan Morris 	if (!nlh)
174726932566SPatrick McHardy 		return -EMSGSIZE;
1748be403ea1SThomas Graf 
1749be403ea1SThomas Graf 	rtm = nlmsg_data(nlh);
17501da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET;
17511e301fd0SIdo Schimmel 	rtm->rtm_dst_len = fri->dst_len;
17521da177e4SLinus Torvalds 	rtm->rtm_src_len = 0;
17531e301fd0SIdo Schimmel 	rtm->rtm_tos = fri->tos;
1754709772e6SKrzysztof Piotr Oledzki 	if (tb_id < 256)
17551da177e4SLinus Torvalds 		rtm->rtm_table = tb_id;
1756709772e6SKrzysztof Piotr Oledzki 	else
1757709772e6SKrzysztof Piotr Oledzki 		rtm->rtm_table = RT_TABLE_COMPAT;
1758f3756b79SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, tb_id))
1759f3756b79SDavid S. Miller 		goto nla_put_failure;
17601e301fd0SIdo Schimmel 	rtm->rtm_type = fri->type;
17611da177e4SLinus Torvalds 	rtm->rtm_flags = fi->fib_flags;
176237e826c5SDavid S. Miller 	rtm->rtm_scope = fi->fib_scope;
17631da177e4SLinus Torvalds 	rtm->rtm_protocol = fi->fib_protocol;
1764be403ea1SThomas Graf 
1765f3756b79SDavid S. Miller 	if (rtm->rtm_dst_len &&
17661e301fd0SIdo Schimmel 	    nla_put_in_addr(skb, RTA_DST, fri->dst))
1767f3756b79SDavid S. Miller 		goto nla_put_failure;
1768f3756b79SDavid S. Miller 	if (fi->fib_priority &&
1769f3756b79SDavid S. Miller 	    nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1770f3756b79SDavid S. Miller 		goto nla_put_failure;
17713fb07dafSEric Dumazet 	if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
1772be403ea1SThomas Graf 		goto nla_put_failure;
1773be403ea1SThomas Graf 
1774f3756b79SDavid S. Miller 	if (fi->fib_prefsrc &&
1775930345eaSJiri Benc 	    nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
1776f3756b79SDavid S. Miller 		goto nla_put_failure;
17774c7e8084SDavid Ahern 
17784c7e8084SDavid Ahern 	if (fi->nh) {
17794c7e8084SDavid Ahern 		if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
17804c7e8084SDavid Ahern 			goto nla_put_failure;
17814c7e8084SDavid Ahern 		if (nexthop_is_blackhole(fi->nh))
17824c7e8084SDavid Ahern 			rtm->rtm_type = RTN_BLACKHOLE;
17834f80116dSRoopa Prabhu 		if (!fi->fib_net->ipv4.sysctl_nexthop_compat_mode)
17844f80116dSRoopa Prabhu 			goto offload;
17854c7e8084SDavid Ahern 	}
17864c7e8084SDavid Ahern 
17875481d73fSDavid Ahern 	if (nhs == 1) {
1788dcb1ecb5SDavid Ahern 		const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
1789ecc5663cSDavid Ahern 		unsigned char flags = 0;
1790be403ea1SThomas Graf 
17917bdf4de1SDonald Sharp 		if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
1792be403ea1SThomas Graf 			goto nla_put_failure;
17931da177e4SLinus Torvalds 
1794b0f60193SDavid Ahern 		rtm->rtm_flags = flags;
1795c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1796dcb1ecb5SDavid Ahern 		if (nhc->nhc_family == AF_INET) {
1797dcb1ecb5SDavid Ahern 			struct fib_nh *nh;
1798dcb1ecb5SDavid Ahern 
1799dcb1ecb5SDavid Ahern 			nh = container_of(nhc, struct fib_nh, nh_common);
1800f3756b79SDavid S. Miller 			if (nh->nh_tclassid &&
1801f3756b79SDavid S. Miller 			    nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1802f3756b79SDavid S. Miller 				goto nla_put_failure;
1803dcb1ecb5SDavid Ahern 		}
18048265abc0SPatrick McHardy #endif
1805b0f60193SDavid Ahern 	} else {
1806b0f60193SDavid Ahern 		if (fib_add_multipath(skb, fi) < 0)
1807ea7a8085SDavid Ahern 			goto nla_put_failure;
18081da177e4SLinus Torvalds 	}
1809b0f60193SDavid Ahern 
18104f80116dSRoopa Prabhu offload:
181190b93f1bSIdo Schimmel 	if (fri->offload)
181290b93f1bSIdo Schimmel 		rtm->rtm_flags |= RTM_F_OFFLOAD;
181390b93f1bSIdo Schimmel 	if (fri->trap)
181490b93f1bSIdo Schimmel 		rtm->rtm_flags |= RTM_F_TRAP;
1815*36c5100eSAmit Cohen 	if (fri->offload_failed)
1816*36c5100eSAmit Cohen 		rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
181790b93f1bSIdo Schimmel 
1818053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1819053c095aSJohannes Berg 	return 0;
18201da177e4SLinus Torvalds 
1821be403ea1SThomas Graf nla_put_failure:
182226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
182326932566SPatrick McHardy 	return -EMSGSIZE;
18241da177e4SLinus Torvalds }
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds /*
18276a31d2a9SEric Dumazet  * Update FIB if:
18286a31d2a9SEric Dumazet  * - local address disappeared -> we must delete all the entries
18296a31d2a9SEric Dumazet  *   referring to it.
18306a31d2a9SEric Dumazet  * - device went down -> we must shutdown all nexthops going via it.
18311da177e4SLinus Torvalds  */
18325a56a0b3SMark Tomlinson int fib_sync_down_addr(struct net_device *dev, __be32 local)
18331da177e4SLinus Torvalds {
18341da177e4SLinus Torvalds 	int ret = 0;
18351da177e4SLinus Torvalds 	unsigned int hash = fib_laddr_hashfn(local);
18361da177e4SLinus Torvalds 	struct hlist_head *head = &fib_info_laddrhash[hash];
1837e0a31262SDavid Ahern 	int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
18385a56a0b3SMark Tomlinson 	struct net *net = dev_net(dev);
18391da177e4SLinus Torvalds 	struct fib_info *fi;
18401da177e4SLinus Torvalds 
184151456b29SIan Morris 	if (!fib_info_laddrhash || local == 0)
184285326fa5SDenis V. Lunev 		return 0;
184385326fa5SDenis V. Lunev 
1844b67bfe0dSSasha Levin 	hlist_for_each_entry(fi, head, fib_lhash) {
18455a56a0b3SMark Tomlinson 		if (!net_eq(fi->fib_net, net) ||
18465a56a0b3SMark Tomlinson 		    fi->fib_tb_id != tb_id)
18474814bdbdSDenis V. Lunev 			continue;
18481da177e4SLinus Torvalds 		if (fi->fib_prefsrc == local) {
18491da177e4SLinus Torvalds 			fi->fib_flags |= RTNH_F_DEAD;
18501da177e4SLinus Torvalds 			ret++;
18511da177e4SLinus Torvalds 		}
18521da177e4SLinus Torvalds 	}
185385326fa5SDenis V. Lunev 	return ret;
18541da177e4SLinus Torvalds }
18551da177e4SLinus Torvalds 
1856b75ed8b1SDavid Ahern static int call_fib_nh_notifiers(struct fib_nh *nh,
1857982acb97SIdo Schimmel 				 enum fib_event_type event_type)
1858982acb97SIdo Schimmel {
1859b75ed8b1SDavid Ahern 	bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
1860982acb97SIdo Schimmel 	struct fib_nh_notifier_info info = {
1861b75ed8b1SDavid Ahern 		.fib_nh = nh,
1862982acb97SIdo Schimmel 	};
1863982acb97SIdo Schimmel 
1864982acb97SIdo Schimmel 	switch (event_type) {
1865982acb97SIdo Schimmel 	case FIB_EVENT_NH_ADD:
1866b75ed8b1SDavid Ahern 		if (nh->fib_nh_flags & RTNH_F_DEAD)
1867982acb97SIdo Schimmel 			break;
1868b75ed8b1SDavid Ahern 		if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
1869982acb97SIdo Schimmel 			break;
1870b75ed8b1SDavid Ahern 		return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
1871982acb97SIdo Schimmel 					   &info.info);
1872982acb97SIdo Schimmel 	case FIB_EVENT_NH_DEL:
1873b75ed8b1SDavid Ahern 		if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1874b75ed8b1SDavid Ahern 		    (nh->fib_nh_flags & RTNH_F_DEAD))
1875b75ed8b1SDavid Ahern 			return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
1876982acb97SIdo Schimmel 						   event_type, &info.info);
1877982acb97SIdo Schimmel 	default:
1878982acb97SIdo Schimmel 		break;
1879982acb97SIdo Schimmel 	}
1880982acb97SIdo Schimmel 
1881982acb97SIdo Schimmel 	return NOTIFY_DONE;
1882982acb97SIdo Schimmel }
1883982acb97SIdo Schimmel 
1884af7d6cceSSabrina Dubroca /* Update the PMTU of exceptions when:
1885af7d6cceSSabrina Dubroca  * - the new MTU of the first hop becomes smaller than the PMTU
1886af7d6cceSSabrina Dubroca  * - the old MTU was the same as the PMTU, and it limited discovery of
1887af7d6cceSSabrina Dubroca  *   larger MTUs on the path. With that limit raised, we can now
1888af7d6cceSSabrina Dubroca  *   discover larger MTUs
1889af7d6cceSSabrina Dubroca  * A special case is locked exceptions, for which the PMTU is smaller
1890af7d6cceSSabrina Dubroca  * than the minimal accepted PMTU:
1891af7d6cceSSabrina Dubroca  * - if the new MTU is greater than the PMTU, don't make any change
1892af7d6cceSSabrina Dubroca  * - otherwise, unlock and set PMTU
1893af7d6cceSSabrina Dubroca  */
189406c77c3eSDavid Ahern void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
1895af7d6cceSSabrina Dubroca {
1896af7d6cceSSabrina Dubroca 	struct fnhe_hash_bucket *bucket;
1897af7d6cceSSabrina Dubroca 	int i;
1898af7d6cceSSabrina Dubroca 
1899a5995e71SDavid Ahern 	bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
1900af7d6cceSSabrina Dubroca 	if (!bucket)
1901af7d6cceSSabrina Dubroca 		return;
1902af7d6cceSSabrina Dubroca 
1903af7d6cceSSabrina Dubroca 	for (i = 0; i < FNHE_HASH_SIZE; i++) {
1904af7d6cceSSabrina Dubroca 		struct fib_nh_exception *fnhe;
1905af7d6cceSSabrina Dubroca 
1906af7d6cceSSabrina Dubroca 		for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1907af7d6cceSSabrina Dubroca 		     fnhe;
1908af7d6cceSSabrina Dubroca 		     fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1909af7d6cceSSabrina Dubroca 			if (fnhe->fnhe_mtu_locked) {
1910af7d6cceSSabrina Dubroca 				if (new <= fnhe->fnhe_pmtu) {
1911af7d6cceSSabrina Dubroca 					fnhe->fnhe_pmtu = new;
1912af7d6cceSSabrina Dubroca 					fnhe->fnhe_mtu_locked = false;
1913af7d6cceSSabrina Dubroca 				}
1914af7d6cceSSabrina Dubroca 			} else if (new < fnhe->fnhe_pmtu ||
1915af7d6cceSSabrina Dubroca 				   orig == fnhe->fnhe_pmtu) {
1916af7d6cceSSabrina Dubroca 				fnhe->fnhe_pmtu = new;
1917af7d6cceSSabrina Dubroca 			}
1918af7d6cceSSabrina Dubroca 		}
1919af7d6cceSSabrina Dubroca 	}
1920af7d6cceSSabrina Dubroca }
1921af7d6cceSSabrina Dubroca 
1922af7d6cceSSabrina Dubroca void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1923af7d6cceSSabrina Dubroca {
1924af7d6cceSSabrina Dubroca 	unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1925af7d6cceSSabrina Dubroca 	struct hlist_head *head = &fib_info_devhash[hash];
1926af7d6cceSSabrina Dubroca 	struct fib_nh *nh;
1927af7d6cceSSabrina Dubroca 
1928af7d6cceSSabrina Dubroca 	hlist_for_each_entry(nh, head, nh_hash) {
1929b75ed8b1SDavid Ahern 		if (nh->fib_nh_dev == dev)
193006c77c3eSDavid Ahern 			fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
1931af7d6cceSSabrina Dubroca 	}
1932af7d6cceSSabrina Dubroca }
1933af7d6cceSSabrina Dubroca 
19344f823defSJulian Anastasov /* Event              force Flags           Description
19354f823defSJulian Anastasov  * NETDEV_CHANGE      0     LINKDOWN        Carrier OFF, not for scope host
19364f823defSJulian Anastasov  * NETDEV_DOWN        0     LINKDOWN|DEAD   Link down, not for scope host
19374f823defSJulian Anastasov  * NETDEV_DOWN        1     LINKDOWN|DEAD   Last address removed
19384f823defSJulian Anastasov  * NETDEV_UNREGISTER  1     LINKDOWN|DEAD   Device removed
19394c7e8084SDavid Ahern  *
19404c7e8084SDavid Ahern  * only used when fib_nh is built into fib_info
19414f823defSJulian Anastasov  */
19424f823defSJulian Anastasov int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
194385326fa5SDenis V. Lunev {
194485326fa5SDenis V. Lunev 	int ret = 0;
194585326fa5SDenis V. Lunev 	int scope = RT_SCOPE_NOWHERE;
19461da177e4SLinus Torvalds 	struct fib_info *prev_fi = NULL;
19471da177e4SLinus Torvalds 	unsigned int hash = fib_devindex_hashfn(dev->ifindex);
19481da177e4SLinus Torvalds 	struct hlist_head *head = &fib_info_devhash[hash];
19491da177e4SLinus Torvalds 	struct fib_nh *nh;
19501da177e4SLinus Torvalds 
19514f823defSJulian Anastasov 	if (force)
195285326fa5SDenis V. Lunev 		scope = -1;
195385326fa5SDenis V. Lunev 
1954b67bfe0dSSasha Levin 	hlist_for_each_entry(nh, head, nh_hash) {
19551da177e4SLinus Torvalds 		struct fib_info *fi = nh->nh_parent;
19561da177e4SLinus Torvalds 		int dead;
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds 		BUG_ON(!fi->fib_nhs);
1959b75ed8b1SDavid Ahern 		if (nh->fib_nh_dev != dev || fi == prev_fi)
19601da177e4SLinus Torvalds 			continue;
19611da177e4SLinus Torvalds 		prev_fi = fi;
19621da177e4SLinus Torvalds 		dead = 0;
19631da177e4SLinus Torvalds 		change_nexthops(fi) {
1964b75ed8b1SDavid Ahern 			if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
19651da177e4SLinus Torvalds 				dead++;
1966b75ed8b1SDavid Ahern 			else if (nexthop_nh->fib_nh_dev == dev &&
1967b75ed8b1SDavid Ahern 				 nexthop_nh->fib_nh_scope != scope) {
19688a3d0316SAndy Gospodarek 				switch (event) {
19698a3d0316SAndy Gospodarek 				case NETDEV_DOWN:
19708a3d0316SAndy Gospodarek 				case NETDEV_UNREGISTER:
1971b75ed8b1SDavid Ahern 					nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
1972a8eceea8SJoe Perches 					fallthrough;
19738a3d0316SAndy Gospodarek 				case NETDEV_CHANGE:
1974b75ed8b1SDavid Ahern 					nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
19758a3d0316SAndy Gospodarek 					break;
19768a3d0316SAndy Gospodarek 				}
1977982acb97SIdo Schimmel 				call_fib_nh_notifiers(nexthop_nh,
1978982acb97SIdo Schimmel 						      FIB_EVENT_NH_DEL);
19791da177e4SLinus Torvalds 				dead++;
19801da177e4SLinus Torvalds 			}
19811da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
19828a3d0316SAndy Gospodarek 			if (event == NETDEV_UNREGISTER &&
1983b75ed8b1SDavid Ahern 			    nexthop_nh->fib_nh_dev == dev) {
19841da177e4SLinus Torvalds 				dead = fi->fib_nhs;
19851da177e4SLinus Torvalds 				break;
19861da177e4SLinus Torvalds 			}
19871da177e4SLinus Torvalds #endif
19881da177e4SLinus Torvalds 		} endfor_nexthops(fi)
19891da177e4SLinus Torvalds 		if (dead == fi->fib_nhs) {
19908a3d0316SAndy Gospodarek 			switch (event) {
19918a3d0316SAndy Gospodarek 			case NETDEV_DOWN:
19928a3d0316SAndy Gospodarek 			case NETDEV_UNREGISTER:
19931da177e4SLinus Torvalds 				fi->fib_flags |= RTNH_F_DEAD;
1994a8eceea8SJoe Perches 				fallthrough;
19958a3d0316SAndy Gospodarek 			case NETDEV_CHANGE:
19968a3d0316SAndy Gospodarek 				fi->fib_flags |= RTNH_F_LINKDOWN;
19978a3d0316SAndy Gospodarek 				break;
19988a3d0316SAndy Gospodarek 			}
19991da177e4SLinus Torvalds 			ret++;
20001da177e4SLinus Torvalds 		}
20010e884c78SPeter Nørlund 
20020e884c78SPeter Nørlund 		fib_rebalance(fi);
20031da177e4SLinus Torvalds 	}
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds 	return ret;
20061da177e4SLinus Torvalds }
20071da177e4SLinus Torvalds 
20080c838ff1SDavid S. Miller /* Must be invoked inside of an RCU protected region.  */
2009c7b371e3SDavid Ahern static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
20100c838ff1SDavid S. Miller {
20110c838ff1SDavid S. Miller 	struct fib_info *fi = NULL, *last_resort = NULL;
201256315f9eSAlexander Duyck 	struct hlist_head *fa_head = res->fa_head;
20130c838ff1SDavid S. Miller 	struct fib_table *tb = res->table;
201418a912e9SJulian Anastasov 	u8 slen = 32 - res->prefixlen;
20150c838ff1SDavid S. Miller 	int order = -1, last_idx = -1;
20162392debcSJulian Anastasov 	struct fib_alias *fa, *fa1 = NULL;
20172392debcSJulian Anastasov 	u32 last_prio = res->fi->fib_priority;
20182392debcSJulian Anastasov 	u8 last_tos = 0;
20190c838ff1SDavid S. Miller 
202056315f9eSAlexander Duyck 	hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
20210c838ff1SDavid S. Miller 		struct fib_info *next_fi = fa->fa_info;
20227c74b0beSDavid Ahern 		struct fib_nh_common *nhc;
20230c838ff1SDavid S. Miller 
202418a912e9SJulian Anastasov 		if (fa->fa_slen != slen)
202518a912e9SJulian Anastasov 			continue;
20262392debcSJulian Anastasov 		if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
20272392debcSJulian Anastasov 			continue;
202818a912e9SJulian Anastasov 		if (fa->tb_id != tb->tb_id)
202918a912e9SJulian Anastasov 			continue;
20302392debcSJulian Anastasov 		if (next_fi->fib_priority > last_prio &&
20312392debcSJulian Anastasov 		    fa->fa_tos == last_tos) {
20322392debcSJulian Anastasov 			if (last_tos)
20332392debcSJulian Anastasov 				continue;
20342392debcSJulian Anastasov 			break;
20352392debcSJulian Anastasov 		}
20362392debcSJulian Anastasov 		if (next_fi->fib_flags & RTNH_F_DEAD)
20372392debcSJulian Anastasov 			continue;
20382392debcSJulian Anastasov 		last_tos = fa->fa_tos;
20392392debcSJulian Anastasov 		last_prio = next_fi->fib_priority;
20402392debcSJulian Anastasov 
204137e826c5SDavid S. Miller 		if (next_fi->fib_scope != res->scope ||
20420c838ff1SDavid S. Miller 		    fa->fa_type != RTN_UNICAST)
20430c838ff1SDavid S. Miller 			continue;
20445481d73fSDavid Ahern 
20457c74b0beSDavid Ahern 		nhc = fib_info_nhc(next_fi, 0);
20467c74b0beSDavid Ahern 		if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
20470c838ff1SDavid S. Miller 			continue;
20480c838ff1SDavid S. Miller 
20490c838ff1SDavid S. Miller 		fib_alias_accessed(fa);
20500c838ff1SDavid S. Miller 
205151456b29SIan Morris 		if (!fi) {
20520c838ff1SDavid S. Miller 			if (next_fi != res->fi)
20530c838ff1SDavid S. Miller 				break;
20542392debcSJulian Anastasov 			fa1 = fa;
20550c838ff1SDavid S. Miller 		} else if (!fib_detect_death(fi, order, &last_resort,
20562392debcSJulian Anastasov 					     &last_idx, fa1->fa_default)) {
20570c838ff1SDavid S. Miller 			fib_result_assign(res, fi);
20582392debcSJulian Anastasov 			fa1->fa_default = order;
20590c838ff1SDavid S. Miller 			goto out;
20600c838ff1SDavid S. Miller 		}
20610c838ff1SDavid S. Miller 		fi = next_fi;
20620c838ff1SDavid S. Miller 		order++;
20630c838ff1SDavid S. Miller 	}
20640c838ff1SDavid S. Miller 
206551456b29SIan Morris 	if (order <= 0 || !fi) {
20662392debcSJulian Anastasov 		if (fa1)
20672392debcSJulian Anastasov 			fa1->fa_default = -1;
20680c838ff1SDavid S. Miller 		goto out;
20690c838ff1SDavid S. Miller 	}
20700c838ff1SDavid S. Miller 
20710c838ff1SDavid S. Miller 	if (!fib_detect_death(fi, order, &last_resort, &last_idx,
20722392debcSJulian Anastasov 			      fa1->fa_default)) {
20730c838ff1SDavid S. Miller 		fib_result_assign(res, fi);
20742392debcSJulian Anastasov 		fa1->fa_default = order;
20750c838ff1SDavid S. Miller 		goto out;
20760c838ff1SDavid S. Miller 	}
20770c838ff1SDavid S. Miller 
20780c838ff1SDavid S. Miller 	if (last_idx >= 0)
20790c838ff1SDavid S. Miller 		fib_result_assign(res, last_resort);
20802392debcSJulian Anastasov 	fa1->fa_default = last_idx;
20810c838ff1SDavid S. Miller out:
208231d40937SEric Dumazet 	return;
20830c838ff1SDavid S. Miller }
20840c838ff1SDavid S. Miller 
20851da177e4SLinus Torvalds /*
20866a31d2a9SEric Dumazet  * Dead device goes up. We wake up dead nexthops.
20876a31d2a9SEric Dumazet  * It takes sense only on multipath routes.
20884c7e8084SDavid Ahern  *
20894c7e8084SDavid Ahern  * only used when fib_nh is built into fib_info
20901da177e4SLinus Torvalds  */
2091ecc5663cSDavid Ahern int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
20921da177e4SLinus Torvalds {
20931da177e4SLinus Torvalds 	struct fib_info *prev_fi;
20941da177e4SLinus Torvalds 	unsigned int hash;
20951da177e4SLinus Torvalds 	struct hlist_head *head;
20961da177e4SLinus Torvalds 	struct fib_nh *nh;
20971da177e4SLinus Torvalds 	int ret;
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds 	if (!(dev->flags & IFF_UP))
21001da177e4SLinus Torvalds 		return 0;
21011da177e4SLinus Torvalds 
2102c9b3292eSJulian Anastasov 	if (nh_flags & RTNH_F_DEAD) {
2103c9b3292eSJulian Anastasov 		unsigned int flags = dev_get_flags(dev);
2104c9b3292eSJulian Anastasov 
2105c9b3292eSJulian Anastasov 		if (flags & (IFF_RUNNING | IFF_LOWER_UP))
2106c9b3292eSJulian Anastasov 			nh_flags |= RTNH_F_LINKDOWN;
2107c9b3292eSJulian Anastasov 	}
2108c9b3292eSJulian Anastasov 
21091da177e4SLinus Torvalds 	prev_fi = NULL;
21101da177e4SLinus Torvalds 	hash = fib_devindex_hashfn(dev->ifindex);
21111da177e4SLinus Torvalds 	head = &fib_info_devhash[hash];
21121da177e4SLinus Torvalds 	ret = 0;
21131da177e4SLinus Torvalds 
2114b67bfe0dSSasha Levin 	hlist_for_each_entry(nh, head, nh_hash) {
21151da177e4SLinus Torvalds 		struct fib_info *fi = nh->nh_parent;
21161da177e4SLinus Torvalds 		int alive;
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds 		BUG_ON(!fi->fib_nhs);
2119b75ed8b1SDavid Ahern 		if (nh->fib_nh_dev != dev || fi == prev_fi)
21201da177e4SLinus Torvalds 			continue;
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds 		prev_fi = fi;
21231da177e4SLinus Torvalds 		alive = 0;
21241da177e4SLinus Torvalds 		change_nexthops(fi) {
2125b75ed8b1SDavid Ahern 			if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
21261da177e4SLinus Torvalds 				alive++;
21271da177e4SLinus Torvalds 				continue;
21281da177e4SLinus Torvalds 			}
2129b75ed8b1SDavid Ahern 			if (!nexthop_nh->fib_nh_dev ||
2130b75ed8b1SDavid Ahern 			    !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
21311da177e4SLinus Torvalds 				continue;
2132b75ed8b1SDavid Ahern 			if (nexthop_nh->fib_nh_dev != dev ||
213371fceff0SDavid S. Miller 			    !__in_dev_get_rtnl(dev))
21341da177e4SLinus Torvalds 				continue;
21351da177e4SLinus Torvalds 			alive++;
2136b75ed8b1SDavid Ahern 			nexthop_nh->fib_nh_flags &= ~nh_flags;
2137982acb97SIdo Schimmel 			call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
21381da177e4SLinus Torvalds 		} endfor_nexthops(fi)
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 		if (alive > 0) {
21418a3d0316SAndy Gospodarek 			fi->fib_flags &= ~nh_flags;
21421da177e4SLinus Torvalds 			ret++;
21431da177e4SLinus Torvalds 		}
21440e884c78SPeter Nørlund 
21450e884c78SPeter Nørlund 		fib_rebalance(fi);
21461da177e4SLinus Torvalds 	}
21471da177e4SLinus Torvalds 
21481da177e4SLinus Torvalds 	return ret;
21491da177e4SLinus Torvalds }
21501da177e4SLinus Torvalds 
21518a3d0316SAndy Gospodarek #ifdef CONFIG_IP_ROUTE_MULTIPATH
2152a6db4494SDavid Ahern static bool fib_good_nh(const struct fib_nh *nh)
2153a6db4494SDavid Ahern {
2154a6db4494SDavid Ahern 	int state = NUD_REACHABLE;
2155a6db4494SDavid Ahern 
2156b75ed8b1SDavid Ahern 	if (nh->fib_nh_scope == RT_SCOPE_LINK) {
2157a6db4494SDavid Ahern 		struct neighbour *n;
2158a6db4494SDavid Ahern 
2159a6db4494SDavid Ahern 		rcu_read_lock_bh();
2160a6db4494SDavid Ahern 
21611a38c43dSDavid Ahern 		if (likely(nh->fib_nh_gw_family == AF_INET))
2162b75ed8b1SDavid Ahern 			n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
2163b75ed8b1SDavid Ahern 						   (__force u32)nh->fib_nh_gw4);
21641a38c43dSDavid Ahern 		else if (nh->fib_nh_gw_family == AF_INET6)
21651a38c43dSDavid Ahern 			n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
21661a38c43dSDavid Ahern 							   &nh->fib_nh_gw6);
21671a38c43dSDavid Ahern 		else
21681a38c43dSDavid Ahern 			n = NULL;
2169a6db4494SDavid Ahern 		if (n)
2170a6db4494SDavid Ahern 			state = n->nud_state;
2171a6db4494SDavid Ahern 
2172a6db4494SDavid Ahern 		rcu_read_unlock_bh();
2173a6db4494SDavid Ahern 	}
2174a6db4494SDavid Ahern 
2175a6db4494SDavid Ahern 	return !!(state & NUD_VALID);
2176a6db4494SDavid Ahern }
21778a3d0316SAndy Gospodarek 
21780e884c78SPeter Nørlund void fib_select_multipath(struct fib_result *res, int hash)
21791da177e4SLinus Torvalds {
21801da177e4SLinus Torvalds 	struct fib_info *fi = res->fi;
2181a6db4494SDavid Ahern 	struct net *net = fi->fib_net;
2182a6db4494SDavid Ahern 	bool first = false;
21831da177e4SLinus Torvalds 
21844c7e8084SDavid Ahern 	if (unlikely(res->fi->nh)) {
21854c7e8084SDavid Ahern 		nexthop_path_fib_result(res, hash);
21864c7e8084SDavid Ahern 		return;
21874c7e8084SDavid Ahern 	}
21884c7e8084SDavid Ahern 
2189eba618abSDavid Ahern 	change_nexthops(fi) {
21906174a30dSXin Long 		if (net->ipv4.sysctl_fib_multipath_use_neigh) {
2191eba618abSDavid Ahern 			if (!fib_good_nh(nexthop_nh))
21920eeb075fSAndy Gospodarek 				continue;
2193a6db4494SDavid Ahern 			if (!first) {
2194a6db4494SDavid Ahern 				res->nh_sel = nhsel;
2195eba618abSDavid Ahern 				res->nhc = &nexthop_nh->nh_common;
2196a6db4494SDavid Ahern 				first = true;
2197a6db4494SDavid Ahern 			}
21986174a30dSXin Long 		}
21996174a30dSXin Long 
2200eba618abSDavid Ahern 		if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
22016174a30dSXin Long 			continue;
22026174a30dSXin Long 
22036174a30dSXin Long 		res->nh_sel = nhsel;
2204eba618abSDavid Ahern 		res->nhc = &nexthop_nh->nh_common;
22056174a30dSXin Long 		return;
22061da177e4SLinus Torvalds 	} endfor_nexthops(fi);
22071da177e4SLinus Torvalds }
22081da177e4SLinus Torvalds #endif
22093ce58d84SDavid Ahern 
22103ce58d84SDavid Ahern void fib_select_path(struct net *net, struct fib_result *res,
2211bf4e0a3dSNikolay Aleksandrov 		     struct flowi4 *fl4, const struct sk_buff *skb)
22123ce58d84SDavid Ahern {
22130d876f2cSDavid Ahern 	if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
22140d876f2cSDavid Ahern 		goto check_saddr;
22157a18c5b9SDavid Ahern 
22163ce58d84SDavid Ahern #ifdef CONFIG_IP_ROUTE_MULTIPATH
22175481d73fSDavid Ahern 	if (fib_info_num_path(res->fi) > 1) {
22187efc0b6bSDavid Ahern 		int h = fib_multipath_hash(net, fl4, skb, NULL);
22199920e48bSPaolo Abeni 
2220bf4e0a3dSNikolay Aleksandrov 		fib_select_multipath(res, h);
22213ce58d84SDavid Ahern 	}
22223ce58d84SDavid Ahern 	else
22233ce58d84SDavid Ahern #endif
22243ce58d84SDavid Ahern 	if (!res->prefixlen &&
22253ce58d84SDavid Ahern 	    res->table->tb_num_default > 1 &&
22260d876f2cSDavid Ahern 	    res->type == RTN_UNICAST)
22273ce58d84SDavid Ahern 		fib_select_default(fl4, res);
22283ce58d84SDavid Ahern 
22290d876f2cSDavid Ahern check_saddr:
22303ce58d84SDavid Ahern 	if (!fl4->saddr)
2231eba618abSDavid Ahern 		fl4->saddr = fib_result_prefsrc(net, res);
22323ce58d84SDavid Ahern }
2233