xref: /openbmc/linux/net/ipv4/fib_semantics.c (revision bf8981a2aa082d9d64771b47c8a1c9c388d8cd40)
1 /*
2  * INET		An implementation of the TCP/IP protocol suite for the LINUX
3  *		operating system.  INET is implemented using the  BSD Socket
4  *		interface as the means of communication with the user level.
5  *
6  *		IPv4 Forwarding Information Base: semantics.
7  *
8  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  *
10  *		This program is free software; you can redistribute it and/or
11  *		modify it under the terms of the GNU General Public License
12  *		as published by the Free Software Foundation; either version
13  *		2 of the License, or (at your option) any later version.
14  */
15 
16 #include <linux/uaccess.h>
17 #include <linux/bitops.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/jiffies.h>
21 #include <linux/mm.h>
22 #include <linux/string.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
25 #include <linux/errno.h>
26 #include <linux/in.h>
27 #include <linux/inet.h>
28 #include <linux/inetdevice.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/proc_fs.h>
32 #include <linux/skbuff.h>
33 #include <linux/init.h>
34 #include <linux/slab.h>
35 #include <linux/netlink.h>
36 
37 #include <net/arp.h>
38 #include <net/ip.h>
39 #include <net/protocol.h>
40 #include <net/route.h>
41 #include <net/tcp.h>
42 #include <net/sock.h>
43 #include <net/ip_fib.h>
44 #include <net/netlink.h>
45 #include <net/nexthop.h>
46 #include <net/lwtunnel.h>
47 #include <net/fib_notifier.h>
48 #include <net/addrconf.h>
49 
50 #include "fib_lookup.h"
51 
52 static DEFINE_SPINLOCK(fib_info_lock);
53 static struct hlist_head *fib_info_hash;
54 static struct hlist_head *fib_info_laddrhash;
55 static unsigned int fib_info_hash_size;
56 static unsigned int fib_info_cnt;
57 
58 #define DEVINDEX_HASHBITS 8
59 #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
60 static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
61 
62 #ifdef CONFIG_IP_ROUTE_MULTIPATH
63 
64 #define for_nexthops(fi) {						\
65 	int nhsel; const struct fib_nh *nh;				\
66 	for (nhsel = 0, nh = (fi)->fib_nh;				\
67 	     nhsel < (fi)->fib_nhs;					\
68 	     nh++, nhsel++)
69 
70 #define change_nexthops(fi) {						\
71 	int nhsel; struct fib_nh *nexthop_nh;				\
72 	for (nhsel = 0,	nexthop_nh = (struct fib_nh *)((fi)->fib_nh);	\
73 	     nhsel < (fi)->fib_nhs;					\
74 	     nexthop_nh++, nhsel++)
75 
76 #else /* CONFIG_IP_ROUTE_MULTIPATH */
77 
78 /* Hope, that gcc will optimize it to get rid of dummy loop */
79 
80 #define for_nexthops(fi) {						\
81 	int nhsel; const struct fib_nh *nh = (fi)->fib_nh;		\
82 	for (nhsel = 0; nhsel < 1; nhsel++)
83 
84 #define change_nexthops(fi) {						\
85 	int nhsel;							\
86 	struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh);	\
87 	for (nhsel = 0; nhsel < 1; nhsel++)
88 
89 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
90 
91 #define endfor_nexthops(fi) }
92 
93 
94 const struct fib_prop fib_props[RTN_MAX + 1] = {
95 	[RTN_UNSPEC] = {
96 		.error	= 0,
97 		.scope	= RT_SCOPE_NOWHERE,
98 	},
99 	[RTN_UNICAST] = {
100 		.error	= 0,
101 		.scope	= RT_SCOPE_UNIVERSE,
102 	},
103 	[RTN_LOCAL] = {
104 		.error	= 0,
105 		.scope	= RT_SCOPE_HOST,
106 	},
107 	[RTN_BROADCAST] = {
108 		.error	= 0,
109 		.scope	= RT_SCOPE_LINK,
110 	},
111 	[RTN_ANYCAST] = {
112 		.error	= 0,
113 		.scope	= RT_SCOPE_LINK,
114 	},
115 	[RTN_MULTICAST] = {
116 		.error	= 0,
117 		.scope	= RT_SCOPE_UNIVERSE,
118 	},
119 	[RTN_BLACKHOLE] = {
120 		.error	= -EINVAL,
121 		.scope	= RT_SCOPE_UNIVERSE,
122 	},
123 	[RTN_UNREACHABLE] = {
124 		.error	= -EHOSTUNREACH,
125 		.scope	= RT_SCOPE_UNIVERSE,
126 	},
127 	[RTN_PROHIBIT] = {
128 		.error	= -EACCES,
129 		.scope	= RT_SCOPE_UNIVERSE,
130 	},
131 	[RTN_THROW] = {
132 		.error	= -EAGAIN,
133 		.scope	= RT_SCOPE_UNIVERSE,
134 	},
135 	[RTN_NAT] = {
136 		.error	= -EINVAL,
137 		.scope	= RT_SCOPE_NOWHERE,
138 	},
139 	[RTN_XRESOLVE] = {
140 		.error	= -EINVAL,
141 		.scope	= RT_SCOPE_NOWHERE,
142 	},
143 };
144 
145 static void rt_fibinfo_free(struct rtable __rcu **rtp)
146 {
147 	struct rtable *rt = rcu_dereference_protected(*rtp, 1);
148 
149 	if (!rt)
150 		return;
151 
152 	/* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
153 	 * because we waited an RCU grace period before calling
154 	 * free_fib_info_rcu()
155 	 */
156 
157 	dst_dev_put(&rt->dst);
158 	dst_release_immediate(&rt->dst);
159 }
160 
161 static void free_nh_exceptions(struct fib_nh *nh)
162 {
163 	struct fnhe_hash_bucket *hash;
164 	int i;
165 
166 	hash = rcu_dereference_protected(nh->nh_exceptions, 1);
167 	if (!hash)
168 		return;
169 	for (i = 0; i < FNHE_HASH_SIZE; i++) {
170 		struct fib_nh_exception *fnhe;
171 
172 		fnhe = rcu_dereference_protected(hash[i].chain, 1);
173 		while (fnhe) {
174 			struct fib_nh_exception *next;
175 
176 			next = rcu_dereference_protected(fnhe->fnhe_next, 1);
177 
178 			rt_fibinfo_free(&fnhe->fnhe_rth_input);
179 			rt_fibinfo_free(&fnhe->fnhe_rth_output);
180 
181 			kfree(fnhe);
182 
183 			fnhe = next;
184 		}
185 	}
186 	kfree(hash);
187 }
188 
189 static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
190 {
191 	int cpu;
192 
193 	if (!rtp)
194 		return;
195 
196 	for_each_possible_cpu(cpu) {
197 		struct rtable *rt;
198 
199 		rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
200 		if (rt) {
201 			dst_dev_put(&rt->dst);
202 			dst_release_immediate(&rt->dst);
203 		}
204 	}
205 	free_percpu(rtp);
206 }
207 
208 void fib_nh_common_release(struct fib_nh_common *nhc)
209 {
210 	if (nhc->nhc_dev)
211 		dev_put(nhc->nhc_dev);
212 
213 	lwtstate_put(nhc->nhc_lwtstate);
214 }
215 EXPORT_SYMBOL_GPL(fib_nh_common_release);
216 
217 void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
218 {
219 #ifdef CONFIG_IP_ROUTE_CLASSID
220 	if (fib_nh->nh_tclassid)
221 		net->ipv4.fib_num_tclassid_users--;
222 #endif
223 	fib_nh_common_release(&fib_nh->nh_common);
224 	free_nh_exceptions(fib_nh);
225 	rt_fibinfo_free_cpus(fib_nh->nh_pcpu_rth_output);
226 	rt_fibinfo_free(&fib_nh->nh_rth_input);
227 }
228 
229 /* Release a nexthop info record */
230 static void free_fib_info_rcu(struct rcu_head *head)
231 {
232 	struct fib_info *fi = container_of(head, struct fib_info, rcu);
233 
234 	change_nexthops(fi) {
235 		fib_nh_release(fi->fib_net, nexthop_nh);
236 	} endfor_nexthops(fi);
237 
238 	ip_fib_metrics_put(fi->fib_metrics);
239 
240 	kfree(fi);
241 }
242 
243 void free_fib_info(struct fib_info *fi)
244 {
245 	if (fi->fib_dead == 0) {
246 		pr_warn("Freeing alive fib_info %p\n", fi);
247 		return;
248 	}
249 	fib_info_cnt--;
250 
251 	call_rcu(&fi->rcu, free_fib_info_rcu);
252 }
253 EXPORT_SYMBOL_GPL(free_fib_info);
254 
255 void fib_release_info(struct fib_info *fi)
256 {
257 	spin_lock_bh(&fib_info_lock);
258 	if (fi && --fi->fib_treeref == 0) {
259 		hlist_del(&fi->fib_hash);
260 		if (fi->fib_prefsrc)
261 			hlist_del(&fi->fib_lhash);
262 		change_nexthops(fi) {
263 			if (!nexthop_nh->fib_nh_dev)
264 				continue;
265 			hlist_del(&nexthop_nh->nh_hash);
266 		} endfor_nexthops(fi)
267 		fi->fib_dead = 1;
268 		fib_info_put(fi);
269 	}
270 	spin_unlock_bh(&fib_info_lock);
271 }
272 
273 static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
274 {
275 	const struct fib_nh *onh = ofi->fib_nh;
276 
277 	for_nexthops(fi) {
278 		if (nh->fib_nh_oif != onh->fib_nh_oif ||
279 		    nh->fib_nh_gw4 != onh->fib_nh_gw4 ||
280 		    nh->fib_nh_scope != onh->fib_nh_scope ||
281 #ifdef CONFIG_IP_ROUTE_MULTIPATH
282 		    nh->fib_nh_weight != onh->fib_nh_weight ||
283 #endif
284 #ifdef CONFIG_IP_ROUTE_CLASSID
285 		    nh->nh_tclassid != onh->nh_tclassid ||
286 #endif
287 		    lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
288 		    ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
289 			return -1;
290 		onh++;
291 	} endfor_nexthops(fi);
292 	return 0;
293 }
294 
295 static inline unsigned int fib_devindex_hashfn(unsigned int val)
296 {
297 	unsigned int mask = DEVINDEX_HASHSIZE - 1;
298 
299 	return (val ^
300 		(val >> DEVINDEX_HASHBITS) ^
301 		(val >> (DEVINDEX_HASHBITS * 2))) & mask;
302 }
303 
304 static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
305 {
306 	unsigned int mask = (fib_info_hash_size - 1);
307 	unsigned int val = fi->fib_nhs;
308 
309 	val ^= (fi->fib_protocol << 8) | fi->fib_scope;
310 	val ^= (__force u32)fi->fib_prefsrc;
311 	val ^= fi->fib_priority;
312 	for_nexthops(fi) {
313 		val ^= fib_devindex_hashfn(nh->fib_nh_oif);
314 	} endfor_nexthops(fi)
315 
316 	return (val ^ (val >> 7) ^ (val >> 12)) & mask;
317 }
318 
319 static struct fib_info *fib_find_info(const struct fib_info *nfi)
320 {
321 	struct hlist_head *head;
322 	struct fib_info *fi;
323 	unsigned int hash;
324 
325 	hash = fib_info_hashfn(nfi);
326 	head = &fib_info_hash[hash];
327 
328 	hlist_for_each_entry(fi, head, fib_hash) {
329 		if (!net_eq(fi->fib_net, nfi->fib_net))
330 			continue;
331 		if (fi->fib_nhs != nfi->fib_nhs)
332 			continue;
333 		if (nfi->fib_protocol == fi->fib_protocol &&
334 		    nfi->fib_scope == fi->fib_scope &&
335 		    nfi->fib_prefsrc == fi->fib_prefsrc &&
336 		    nfi->fib_priority == fi->fib_priority &&
337 		    nfi->fib_type == fi->fib_type &&
338 		    memcmp(nfi->fib_metrics, fi->fib_metrics,
339 			   sizeof(u32) * RTAX_MAX) == 0 &&
340 		    !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
341 		    (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
342 			return fi;
343 	}
344 
345 	return NULL;
346 }
347 
348 /* Check, that the gateway is already configured.
349  * Used only by redirect accept routine.
350  */
351 int ip_fib_check_default(__be32 gw, struct net_device *dev)
352 {
353 	struct hlist_head *head;
354 	struct fib_nh *nh;
355 	unsigned int hash;
356 
357 	spin_lock(&fib_info_lock);
358 
359 	hash = fib_devindex_hashfn(dev->ifindex);
360 	head = &fib_info_devhash[hash];
361 	hlist_for_each_entry(nh, head, nh_hash) {
362 		if (nh->fib_nh_dev == dev &&
363 		    nh->fib_nh_gw4 == gw &&
364 		    !(nh->fib_nh_flags & RTNH_F_DEAD)) {
365 			spin_unlock(&fib_info_lock);
366 			return 0;
367 		}
368 	}
369 
370 	spin_unlock(&fib_info_lock);
371 
372 	return -1;
373 }
374 
375 static inline size_t fib_nlmsg_size(struct fib_info *fi)
376 {
377 	size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
378 			 + nla_total_size(4) /* RTA_TABLE */
379 			 + nla_total_size(4) /* RTA_DST */
380 			 + nla_total_size(4) /* RTA_PRIORITY */
381 			 + nla_total_size(4) /* RTA_PREFSRC */
382 			 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
383 
384 	/* space for nested metrics */
385 	payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
386 
387 	if (fi->fib_nhs) {
388 		size_t nh_encapsize = 0;
389 		/* Also handles the special case fib_nhs == 1 */
390 
391 		/* each nexthop is packed in an attribute */
392 		size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
393 
394 		/* may contain flow and gateway attribute */
395 		nhsize += 2 * nla_total_size(4);
396 
397 		/* grab encap info */
398 		for_nexthops(fi) {
399 			if (nh->fib_nh_lws) {
400 				/* RTA_ENCAP_TYPE */
401 				nh_encapsize += lwtunnel_get_encap_size(
402 						nh->fib_nh_lws);
403 				/* RTA_ENCAP */
404 				nh_encapsize +=  nla_total_size(2);
405 			}
406 		} endfor_nexthops(fi);
407 
408 		/* all nexthops are packed in a nested attribute */
409 		payload += nla_total_size((fi->fib_nhs * nhsize) +
410 					  nh_encapsize);
411 
412 	}
413 
414 	return payload;
415 }
416 
417 void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
418 	       int dst_len, u32 tb_id, const struct nl_info *info,
419 	       unsigned int nlm_flags)
420 {
421 	struct sk_buff *skb;
422 	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
423 	int err = -ENOBUFS;
424 
425 	skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
426 	if (!skb)
427 		goto errout;
428 
429 	err = fib_dump_info(skb, info->portid, seq, event, tb_id,
430 			    fa->fa_type, key, dst_len,
431 			    fa->fa_tos, fa->fa_info, nlm_flags);
432 	if (err < 0) {
433 		/* -EMSGSIZE implies BUG in fib_nlmsg_size() */
434 		WARN_ON(err == -EMSGSIZE);
435 		kfree_skb(skb);
436 		goto errout;
437 	}
438 	rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
439 		    info->nlh, GFP_KERNEL);
440 	return;
441 errout:
442 	if (err < 0)
443 		rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
444 }
445 
446 static int fib_detect_death(struct fib_info *fi, int order,
447 			    struct fib_info **last_resort, int *last_idx,
448 			    int dflt)
449 {
450 	struct neighbour *n;
451 	int state = NUD_NONE;
452 
453 	n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].fib_nh_gw4, fi->fib_dev);
454 	if (n) {
455 		state = n->nud_state;
456 		neigh_release(n);
457 	} else {
458 		return 0;
459 	}
460 	if (state == NUD_REACHABLE)
461 		return 0;
462 	if ((state & NUD_VALID) && order != dflt)
463 		return 0;
464 	if ((state & NUD_VALID) ||
465 	    (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
466 		*last_resort = fi;
467 		*last_idx = order;
468 	}
469 	return 1;
470 }
471 
472 int fib_nh_common_init(struct fib_nh_common *nhc, struct nlattr *encap,
473 		       u16 encap_type, void *cfg, gfp_t gfp_flags,
474 		       struct netlink_ext_ack *extack)
475 {
476 	if (encap) {
477 		struct lwtunnel_state *lwtstate;
478 		int err;
479 
480 		if (encap_type == LWTUNNEL_ENCAP_NONE) {
481 			NL_SET_ERR_MSG(extack, "LWT encap type not specified");
482 			return -EINVAL;
483 		}
484 		err = lwtunnel_build_state(encap_type, encap, nhc->nhc_family,
485 					   cfg, &lwtstate, extack);
486 		if (err)
487 			return err;
488 
489 		nhc->nhc_lwtstate = lwtstate_get(lwtstate);
490 	}
491 
492 	return 0;
493 }
494 EXPORT_SYMBOL_GPL(fib_nh_common_init);
495 
496 int fib_nh_init(struct net *net, struct fib_nh *nh,
497 		struct fib_config *cfg, int nh_weight,
498 		struct netlink_ext_ack *extack)
499 {
500 	int err = -ENOMEM;
501 
502 	nh->fib_nh_family = AF_INET;
503 
504 	nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *);
505 	if (!nh->nh_pcpu_rth_output)
506 		goto err_out;
507 
508 	err = fib_nh_common_init(&nh->nh_common, cfg->fc_encap,
509 				 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
510 	if (err)
511 		goto init_failure;
512 
513 	nh->fib_nh_oif = cfg->fc_oif;
514 	if (cfg->fc_gw) {
515 		nh->fib_nh_gw4 = cfg->fc_gw;
516 		nh->fib_nh_has_gw = 1;
517 	}
518 	nh->fib_nh_flags = cfg->fc_flags;
519 
520 #ifdef CONFIG_IP_ROUTE_CLASSID
521 	nh->nh_tclassid = cfg->fc_flow;
522 	if (nh->nh_tclassid)
523 		net->ipv4.fib_num_tclassid_users++;
524 #endif
525 #ifdef CONFIG_IP_ROUTE_MULTIPATH
526 	nh->fib_nh_weight = nh_weight;
527 #endif
528 	return 0;
529 
530 init_failure:
531 	rt_fibinfo_free_cpus(nh->nh_pcpu_rth_output);
532 	nh->nh_pcpu_rth_output = NULL;
533 err_out:
534 	return err;
535 }
536 
537 #ifdef CONFIG_IP_ROUTE_MULTIPATH
538 
539 static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
540 			      struct netlink_ext_ack *extack)
541 {
542 	int nhs = 0;
543 
544 	while (rtnh_ok(rtnh, remaining)) {
545 		nhs++;
546 		rtnh = rtnh_next(rtnh, &remaining);
547 	}
548 
549 	/* leftover implies invalid nexthop configuration, discard it */
550 	if (remaining > 0) {
551 		NL_SET_ERR_MSG(extack,
552 			       "Invalid nexthop configuration - extra data after nexthops");
553 		nhs = 0;
554 	}
555 
556 	return nhs;
557 }
558 
559 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
560 		       int remaining, struct fib_config *cfg,
561 		       struct netlink_ext_ack *extack)
562 {
563 	struct net *net = fi->fib_net;
564 	struct fib_config fib_cfg;
565 	int ret;
566 
567 	change_nexthops(fi) {
568 		int attrlen;
569 
570 		memset(&fib_cfg, 0, sizeof(fib_cfg));
571 
572 		if (!rtnh_ok(rtnh, remaining)) {
573 			NL_SET_ERR_MSG(extack,
574 				       "Invalid nexthop configuration - extra data after nexthop");
575 			return -EINVAL;
576 		}
577 
578 		if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
579 			NL_SET_ERR_MSG(extack,
580 				       "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
581 			return -EINVAL;
582 		}
583 
584 		fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
585 		fib_cfg.fc_oif = rtnh->rtnh_ifindex;
586 
587 		attrlen = rtnh_attrlen(rtnh);
588 		if (attrlen > 0) {
589 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
590 
591 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
592 			if (nla)
593 				fib_cfg.fc_gw = nla_get_in_addr(nla);
594 
595 			nla = nla_find(attrs, attrlen, RTA_FLOW);
596 			if (nla)
597 				fib_cfg.fc_flow = nla_get_u32(nla);
598 
599 			fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
600 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
601 			if (nla)
602 				fib_cfg.fc_encap_type = nla_get_u16(nla);
603 		}
604 
605 		ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
606 				  rtnh->rtnh_hops + 1, extack);
607 		if (ret)
608 			goto errout;
609 
610 		rtnh = rtnh_next(rtnh, &remaining);
611 	} endfor_nexthops(fi);
612 
613 	ret = -EINVAL;
614 	if (cfg->fc_oif && fi->fib_nh->fib_nh_oif != cfg->fc_oif) {
615 		NL_SET_ERR_MSG(extack,
616 			       "Nexthop device index does not match RTA_OIF");
617 		goto errout;
618 	}
619 	if (cfg->fc_gw && fi->fib_nh->fib_nh_gw4 != cfg->fc_gw) {
620 		NL_SET_ERR_MSG(extack,
621 			       "Nexthop gateway does not match RTA_GATEWAY");
622 		goto errout;
623 	}
624 #ifdef CONFIG_IP_ROUTE_CLASSID
625 	if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) {
626 		NL_SET_ERR_MSG(extack,
627 			       "Nexthop class id does not match RTA_FLOW");
628 		goto errout;
629 	}
630 #endif
631 	ret = 0;
632 errout:
633 	return ret;
634 }
635 
636 static void fib_rebalance(struct fib_info *fi)
637 {
638 	int total;
639 	int w;
640 
641 	if (fi->fib_nhs < 2)
642 		return;
643 
644 	total = 0;
645 	for_nexthops(fi) {
646 		if (nh->fib_nh_flags & RTNH_F_DEAD)
647 			continue;
648 
649 		if (ip_ignore_linkdown(nh->fib_nh_dev) &&
650 		    nh->fib_nh_flags & RTNH_F_LINKDOWN)
651 			continue;
652 
653 		total += nh->fib_nh_weight;
654 	} endfor_nexthops(fi);
655 
656 	w = 0;
657 	change_nexthops(fi) {
658 		int upper_bound;
659 
660 		if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
661 			upper_bound = -1;
662 		} else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
663 			   nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
664 			upper_bound = -1;
665 		} else {
666 			w += nexthop_nh->fib_nh_weight;
667 			upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
668 							    total) - 1;
669 		}
670 
671 		atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
672 	} endfor_nexthops(fi);
673 }
674 #else /* CONFIG_IP_ROUTE_MULTIPATH */
675 
676 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
677 		       int remaining, struct fib_config *cfg,
678 		       struct netlink_ext_ack *extack)
679 {
680 	NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
681 
682 	return -EINVAL;
683 }
684 
685 #define fib_rebalance(fi) do { } while (0)
686 
687 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
688 
689 static int fib_encap_match(u16 encap_type,
690 			   struct nlattr *encap,
691 			   const struct fib_nh *nh,
692 			   const struct fib_config *cfg,
693 			   struct netlink_ext_ack *extack)
694 {
695 	struct lwtunnel_state *lwtstate;
696 	int ret, result = 0;
697 
698 	if (encap_type == LWTUNNEL_ENCAP_NONE)
699 		return 0;
700 
701 	ret = lwtunnel_build_state(encap_type, encap, AF_INET,
702 				   cfg, &lwtstate, extack);
703 	if (!ret) {
704 		result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
705 		lwtstate_free(lwtstate);
706 	}
707 
708 	return result;
709 }
710 
711 int fib_nh_match(struct fib_config *cfg, struct fib_info *fi,
712 		 struct netlink_ext_ack *extack)
713 {
714 #ifdef CONFIG_IP_ROUTE_MULTIPATH
715 	struct rtnexthop *rtnh;
716 	int remaining;
717 #endif
718 
719 	if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
720 		return 1;
721 
722 	if (cfg->fc_oif || cfg->fc_gw) {
723 		if (cfg->fc_encap) {
724 			if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap,
725 					    fi->fib_nh, cfg, extack))
726 				return 1;
727 		}
728 #ifdef CONFIG_IP_ROUTE_CLASSID
729 		if (cfg->fc_flow &&
730 		    cfg->fc_flow != fi->fib_nh->nh_tclassid)
731 			return 1;
732 #endif
733 		if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->fib_nh_oif) &&
734 		    (!cfg->fc_gw  || cfg->fc_gw == fi->fib_nh->fib_nh_gw4))
735 			return 0;
736 		return 1;
737 	}
738 
739 #ifdef CONFIG_IP_ROUTE_MULTIPATH
740 	if (!cfg->fc_mp)
741 		return 0;
742 
743 	rtnh = cfg->fc_mp;
744 	remaining = cfg->fc_mp_len;
745 
746 	for_nexthops(fi) {
747 		int attrlen;
748 
749 		if (!rtnh_ok(rtnh, remaining))
750 			return -EINVAL;
751 
752 		if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
753 			return 1;
754 
755 		attrlen = rtnh_attrlen(rtnh);
756 		if (attrlen > 0) {
757 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
758 
759 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
760 			if (nla && nla_get_in_addr(nla) != nh->fib_nh_gw4)
761 				return 1;
762 #ifdef CONFIG_IP_ROUTE_CLASSID
763 			nla = nla_find(attrs, attrlen, RTA_FLOW);
764 			if (nla && nla_get_u32(nla) != nh->nh_tclassid)
765 				return 1;
766 #endif
767 		}
768 
769 		rtnh = rtnh_next(rtnh, &remaining);
770 	} endfor_nexthops(fi);
771 #endif
772 	return 0;
773 }
774 
775 bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
776 {
777 	struct nlattr *nla;
778 	int remaining;
779 
780 	if (!cfg->fc_mx)
781 		return true;
782 
783 	nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
784 		int type = nla_type(nla);
785 		u32 fi_val, val;
786 
787 		if (!type)
788 			continue;
789 		if (type > RTAX_MAX)
790 			return false;
791 
792 		if (type == RTAX_CC_ALGO) {
793 			char tmp[TCP_CA_NAME_MAX];
794 			bool ecn_ca = false;
795 
796 			nla_strlcpy(tmp, nla, sizeof(tmp));
797 			val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
798 		} else {
799 			if (nla_len(nla) != sizeof(u32))
800 				return false;
801 			val = nla_get_u32(nla);
802 		}
803 
804 		fi_val = fi->fib_metrics->metrics[type - 1];
805 		if (type == RTAX_FEATURES)
806 			fi_val &= ~DST_FEATURE_ECN_CA;
807 
808 		if (fi_val != val)
809 			return false;
810 	}
811 
812 	return true;
813 }
814 
815 
816 /*
817  * Picture
818  * -------
819  *
820  * Semantics of nexthop is very messy by historical reasons.
821  * We have to take into account, that:
822  * a) gateway can be actually local interface address,
823  *    so that gatewayed route is direct.
824  * b) gateway must be on-link address, possibly
825  *    described not by an ifaddr, but also by a direct route.
826  * c) If both gateway and interface are specified, they should not
827  *    contradict.
828  * d) If we use tunnel routes, gateway could be not on-link.
829  *
830  * Attempt to reconcile all of these (alas, self-contradictory) conditions
831  * results in pretty ugly and hairy code with obscure logic.
832  *
833  * I chose to generalized it instead, so that the size
834  * of code does not increase practically, but it becomes
835  * much more general.
836  * Every prefix is assigned a "scope" value: "host" is local address,
837  * "link" is direct route,
838  * [ ... "site" ... "interior" ... ]
839  * and "universe" is true gateway route with global meaning.
840  *
841  * Every prefix refers to a set of "nexthop"s (gw, oif),
842  * where gw must have narrower scope. This recursion stops
843  * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
844  * which means that gw is forced to be on link.
845  *
846  * Code is still hairy, but now it is apparently logically
847  * consistent and very flexible. F.e. as by-product it allows
848  * to co-exists in peace independent exterior and interior
849  * routing processes.
850  *
851  * Normally it looks as following.
852  *
853  * {universe prefix}  -> (gw, oif) [scope link]
854  *		  |
855  *		  |-> {link prefix} -> (gw, oif) [scope local]
856  *					|
857  *					|-> {local prefix} (terminal node)
858  */
859 static int fib_check_nh(struct fib_config *cfg, struct fib_nh *nh,
860 			struct netlink_ext_ack *extack)
861 {
862 	int err = 0;
863 	struct net *net;
864 	struct net_device *dev;
865 
866 	net = cfg->fc_nlinfo.nl_net;
867 	if (nh->fib_nh_gw4) {
868 		struct fib_result res;
869 
870 		if (nh->fib_nh_flags & RTNH_F_ONLINK) {
871 			unsigned int addr_type;
872 
873 			if (cfg->fc_scope >= RT_SCOPE_LINK) {
874 				NL_SET_ERR_MSG(extack,
875 					       "Nexthop has invalid scope");
876 				return -EINVAL;
877 			}
878 			dev = __dev_get_by_index(net, nh->fib_nh_oif);
879 			if (!dev) {
880 				NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
881 				return -ENODEV;
882 			}
883 			if (!(dev->flags & IFF_UP)) {
884 				NL_SET_ERR_MSG(extack,
885 					       "Nexthop device is not up");
886 				return -ENETDOWN;
887 			}
888 			addr_type = inet_addr_type_dev_table(net, dev,
889 							     nh->fib_nh_gw4);
890 			if (addr_type != RTN_UNICAST) {
891 				NL_SET_ERR_MSG(extack,
892 					       "Nexthop has invalid gateway");
893 				return -EINVAL;
894 			}
895 			if (!netif_carrier_ok(dev))
896 				nh->fib_nh_flags |= RTNH_F_LINKDOWN;
897 			nh->fib_nh_dev = dev;
898 			dev_hold(dev);
899 			nh->fib_nh_scope = RT_SCOPE_LINK;
900 			return 0;
901 		}
902 		rcu_read_lock();
903 		{
904 			struct fib_table *tbl = NULL;
905 			struct flowi4 fl4 = {
906 				.daddr = nh->fib_nh_gw4,
907 				.flowi4_scope = cfg->fc_scope + 1,
908 				.flowi4_oif = nh->fib_nh_oif,
909 				.flowi4_iif = LOOPBACK_IFINDEX,
910 			};
911 
912 			/* It is not necessary, but requires a bit of thinking */
913 			if (fl4.flowi4_scope < RT_SCOPE_LINK)
914 				fl4.flowi4_scope = RT_SCOPE_LINK;
915 
916 			if (cfg->fc_table)
917 				tbl = fib_get_table(net, cfg->fc_table);
918 
919 			if (tbl)
920 				err = fib_table_lookup(tbl, &fl4, &res,
921 						       FIB_LOOKUP_IGNORE_LINKSTATE |
922 						       FIB_LOOKUP_NOREF);
923 
924 			/* on error or if no table given do full lookup. This
925 			 * is needed for example when nexthops are in the local
926 			 * table rather than the given table
927 			 */
928 			if (!tbl || err) {
929 				err = fib_lookup(net, &fl4, &res,
930 						 FIB_LOOKUP_IGNORE_LINKSTATE);
931 			}
932 
933 			if (err) {
934 				NL_SET_ERR_MSG(extack,
935 					       "Nexthop has invalid gateway");
936 				rcu_read_unlock();
937 				return err;
938 			}
939 		}
940 		err = -EINVAL;
941 		if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
942 			NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
943 			goto out;
944 		}
945 		nh->fib_nh_scope = res.scope;
946 		nh->fib_nh_oif = FIB_RES_OIF(res);
947 		nh->fib_nh_dev = dev = FIB_RES_DEV(res);
948 		if (!dev) {
949 			NL_SET_ERR_MSG(extack,
950 				       "No egress device for nexthop gateway");
951 			goto out;
952 		}
953 		dev_hold(dev);
954 		if (!netif_carrier_ok(dev))
955 			nh->fib_nh_flags |= RTNH_F_LINKDOWN;
956 		err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
957 	} else {
958 		struct in_device *in_dev;
959 
960 		if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
961 			NL_SET_ERR_MSG(extack,
962 				       "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
963 			return -EINVAL;
964 		}
965 		rcu_read_lock();
966 		err = -ENODEV;
967 		in_dev = inetdev_by_index(net, nh->fib_nh_oif);
968 		if (!in_dev)
969 			goto out;
970 		err = -ENETDOWN;
971 		if (!(in_dev->dev->flags & IFF_UP)) {
972 			NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
973 			goto out;
974 		}
975 		nh->fib_nh_dev = in_dev->dev;
976 		dev_hold(nh->fib_nh_dev);
977 		nh->fib_nh_scope = RT_SCOPE_HOST;
978 		if (!netif_carrier_ok(nh->fib_nh_dev))
979 			nh->fib_nh_flags |= RTNH_F_LINKDOWN;
980 		err = 0;
981 	}
982 out:
983 	rcu_read_unlock();
984 	return err;
985 }
986 
987 static inline unsigned int fib_laddr_hashfn(__be32 val)
988 {
989 	unsigned int mask = (fib_info_hash_size - 1);
990 
991 	return ((__force u32)val ^
992 		((__force u32)val >> 7) ^
993 		((__force u32)val >> 14)) & mask;
994 }
995 
996 static struct hlist_head *fib_info_hash_alloc(int bytes)
997 {
998 	if (bytes <= PAGE_SIZE)
999 		return kzalloc(bytes, GFP_KERNEL);
1000 	else
1001 		return (struct hlist_head *)
1002 			__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1003 					 get_order(bytes));
1004 }
1005 
1006 static void fib_info_hash_free(struct hlist_head *hash, int bytes)
1007 {
1008 	if (!hash)
1009 		return;
1010 
1011 	if (bytes <= PAGE_SIZE)
1012 		kfree(hash);
1013 	else
1014 		free_pages((unsigned long) hash, get_order(bytes));
1015 }
1016 
1017 static void fib_info_hash_move(struct hlist_head *new_info_hash,
1018 			       struct hlist_head *new_laddrhash,
1019 			       unsigned int new_size)
1020 {
1021 	struct hlist_head *old_info_hash, *old_laddrhash;
1022 	unsigned int old_size = fib_info_hash_size;
1023 	unsigned int i, bytes;
1024 
1025 	spin_lock_bh(&fib_info_lock);
1026 	old_info_hash = fib_info_hash;
1027 	old_laddrhash = fib_info_laddrhash;
1028 	fib_info_hash_size = new_size;
1029 
1030 	for (i = 0; i < old_size; i++) {
1031 		struct hlist_head *head = &fib_info_hash[i];
1032 		struct hlist_node *n;
1033 		struct fib_info *fi;
1034 
1035 		hlist_for_each_entry_safe(fi, n, head, fib_hash) {
1036 			struct hlist_head *dest;
1037 			unsigned int new_hash;
1038 
1039 			new_hash = fib_info_hashfn(fi);
1040 			dest = &new_info_hash[new_hash];
1041 			hlist_add_head(&fi->fib_hash, dest);
1042 		}
1043 	}
1044 	fib_info_hash = new_info_hash;
1045 
1046 	for (i = 0; i < old_size; i++) {
1047 		struct hlist_head *lhead = &fib_info_laddrhash[i];
1048 		struct hlist_node *n;
1049 		struct fib_info *fi;
1050 
1051 		hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
1052 			struct hlist_head *ldest;
1053 			unsigned int new_hash;
1054 
1055 			new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
1056 			ldest = &new_laddrhash[new_hash];
1057 			hlist_add_head(&fi->fib_lhash, ldest);
1058 		}
1059 	}
1060 	fib_info_laddrhash = new_laddrhash;
1061 
1062 	spin_unlock_bh(&fib_info_lock);
1063 
1064 	bytes = old_size * sizeof(struct hlist_head *);
1065 	fib_info_hash_free(old_info_hash, bytes);
1066 	fib_info_hash_free(old_laddrhash, bytes);
1067 }
1068 
1069 __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
1070 {
1071 	nh->nh_saddr = inet_select_addr(nh->fib_nh_dev,
1072 					nh->fib_nh_gw4,
1073 					nh->nh_parent->fib_scope);
1074 	nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1075 
1076 	return nh->nh_saddr;
1077 }
1078 
1079 __be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1080 {
1081 	struct fib_nh_common *nhc = res->nhc;
1082 	struct fib_nh *nh;
1083 
1084 	if (res->fi->fib_prefsrc)
1085 		return res->fi->fib_prefsrc;
1086 
1087 	nh = container_of(nhc, struct fib_nh, nh_common);
1088 	if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1089 		return nh->nh_saddr;
1090 
1091 	return fib_info_update_nh_saddr(net, nh);
1092 }
1093 
1094 static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1095 {
1096 	if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1097 	    fib_prefsrc != cfg->fc_dst) {
1098 		u32 tb_id = cfg->fc_table;
1099 		int rc;
1100 
1101 		if (tb_id == RT_TABLE_MAIN)
1102 			tb_id = RT_TABLE_LOCAL;
1103 
1104 		rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1105 					  fib_prefsrc, tb_id);
1106 
1107 		if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1108 			rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1109 						  fib_prefsrc, RT_TABLE_LOCAL);
1110 		}
1111 
1112 		if (rc != RTN_LOCAL)
1113 			return false;
1114 	}
1115 	return true;
1116 }
1117 
1118 struct fib_info *fib_create_info(struct fib_config *cfg,
1119 				 struct netlink_ext_ack *extack)
1120 {
1121 	int err;
1122 	struct fib_info *fi = NULL;
1123 	struct fib_info *ofi;
1124 	int nhs = 1;
1125 	struct net *net = cfg->fc_nlinfo.nl_net;
1126 
1127 	if (cfg->fc_type > RTN_MAX)
1128 		goto err_inval;
1129 
1130 	/* Fast check to catch the most weird cases */
1131 	if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1132 		NL_SET_ERR_MSG(extack, "Invalid scope");
1133 		goto err_inval;
1134 	}
1135 
1136 	if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1137 		NL_SET_ERR_MSG(extack,
1138 			       "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
1139 		goto err_inval;
1140 	}
1141 
1142 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1143 	if (cfg->fc_mp) {
1144 		nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
1145 		if (nhs == 0)
1146 			goto err_inval;
1147 	}
1148 #endif
1149 
1150 	err = -ENOBUFS;
1151 	if (fib_info_cnt >= fib_info_hash_size) {
1152 		unsigned int new_size = fib_info_hash_size << 1;
1153 		struct hlist_head *new_info_hash;
1154 		struct hlist_head *new_laddrhash;
1155 		unsigned int bytes;
1156 
1157 		if (!new_size)
1158 			new_size = 16;
1159 		bytes = new_size * sizeof(struct hlist_head *);
1160 		new_info_hash = fib_info_hash_alloc(bytes);
1161 		new_laddrhash = fib_info_hash_alloc(bytes);
1162 		if (!new_info_hash || !new_laddrhash) {
1163 			fib_info_hash_free(new_info_hash, bytes);
1164 			fib_info_hash_free(new_laddrhash, bytes);
1165 		} else
1166 			fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
1167 
1168 		if (!fib_info_hash_size)
1169 			goto failure;
1170 	}
1171 
1172 	fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
1173 	if (!fi)
1174 		goto failure;
1175 	fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
1176 					      cfg->fc_mx_len, extack);
1177 	if (unlikely(IS_ERR(fi->fib_metrics))) {
1178 		err = PTR_ERR(fi->fib_metrics);
1179 		kfree(fi);
1180 		return ERR_PTR(err);
1181 	}
1182 
1183 	fib_info_cnt++;
1184 	fi->fib_net = net;
1185 	fi->fib_protocol = cfg->fc_protocol;
1186 	fi->fib_scope = cfg->fc_scope;
1187 	fi->fib_flags = cfg->fc_flags;
1188 	fi->fib_priority = cfg->fc_priority;
1189 	fi->fib_prefsrc = cfg->fc_prefsrc;
1190 	fi->fib_type = cfg->fc_type;
1191 	fi->fib_tb_id = cfg->fc_table;
1192 
1193 	fi->fib_nhs = nhs;
1194 	change_nexthops(fi) {
1195 		nexthop_nh->nh_parent = fi;
1196 	} endfor_nexthops(fi)
1197 
1198 	if (cfg->fc_mp)
1199 		err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack);
1200 	else
1201 		err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
1202 
1203 	if (err != 0)
1204 		goto failure;
1205 
1206 	if (fib_props[cfg->fc_type].error) {
1207 		if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) {
1208 			NL_SET_ERR_MSG(extack,
1209 				       "Gateway, device and multipath can not be specified for this route type");
1210 			goto err_inval;
1211 		}
1212 		goto link_it;
1213 	} else {
1214 		switch (cfg->fc_type) {
1215 		case RTN_UNICAST:
1216 		case RTN_LOCAL:
1217 		case RTN_BROADCAST:
1218 		case RTN_ANYCAST:
1219 		case RTN_MULTICAST:
1220 			break;
1221 		default:
1222 			NL_SET_ERR_MSG(extack, "Invalid route type");
1223 			goto err_inval;
1224 		}
1225 	}
1226 
1227 	if (cfg->fc_scope > RT_SCOPE_HOST) {
1228 		NL_SET_ERR_MSG(extack, "Invalid scope");
1229 		goto err_inval;
1230 	}
1231 
1232 	if (cfg->fc_scope == RT_SCOPE_HOST) {
1233 		struct fib_nh *nh = fi->fib_nh;
1234 
1235 		/* Local address is added. */
1236 		if (nhs != 1) {
1237 			NL_SET_ERR_MSG(extack,
1238 				       "Route with host scope can not have multiple nexthops");
1239 			goto err_inval;
1240 		}
1241 		if (nh->fib_nh_gw4) {
1242 			NL_SET_ERR_MSG(extack,
1243 				       "Route with host scope can not have a gateway");
1244 			goto err_inval;
1245 		}
1246 		nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1247 		nh->fib_nh_dev = dev_get_by_index(net, fi->fib_nh->fib_nh_oif);
1248 		err = -ENODEV;
1249 		if (!nh->fib_nh_dev)
1250 			goto failure;
1251 	} else {
1252 		int linkdown = 0;
1253 
1254 		change_nexthops(fi) {
1255 			err = fib_check_nh(cfg, nexthop_nh, extack);
1256 			if (err != 0)
1257 				goto failure;
1258 			if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
1259 				linkdown++;
1260 		} endfor_nexthops(fi)
1261 		if (linkdown == fi->fib_nhs)
1262 			fi->fib_flags |= RTNH_F_LINKDOWN;
1263 	}
1264 
1265 	if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1266 		NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
1267 		goto err_inval;
1268 	}
1269 
1270 	change_nexthops(fi) {
1271 		fib_info_update_nh_saddr(net, nexthop_nh);
1272 	} endfor_nexthops(fi)
1273 
1274 	fib_rebalance(fi);
1275 
1276 link_it:
1277 	ofi = fib_find_info(fi);
1278 	if (ofi) {
1279 		fi->fib_dead = 1;
1280 		free_fib_info(fi);
1281 		ofi->fib_treeref++;
1282 		return ofi;
1283 	}
1284 
1285 	fi->fib_treeref++;
1286 	refcount_set(&fi->fib_clntref, 1);
1287 	spin_lock_bh(&fib_info_lock);
1288 	hlist_add_head(&fi->fib_hash,
1289 		       &fib_info_hash[fib_info_hashfn(fi)]);
1290 	if (fi->fib_prefsrc) {
1291 		struct hlist_head *head;
1292 
1293 		head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1294 		hlist_add_head(&fi->fib_lhash, head);
1295 	}
1296 	change_nexthops(fi) {
1297 		struct hlist_head *head;
1298 		unsigned int hash;
1299 
1300 		if (!nexthop_nh->fib_nh_dev)
1301 			continue;
1302 		hash = fib_devindex_hashfn(nexthop_nh->fib_nh_dev->ifindex);
1303 		head = &fib_info_devhash[hash];
1304 		hlist_add_head(&nexthop_nh->nh_hash, head);
1305 	} endfor_nexthops(fi)
1306 	spin_unlock_bh(&fib_info_lock);
1307 	return fi;
1308 
1309 err_inval:
1310 	err = -EINVAL;
1311 
1312 failure:
1313 	if (fi) {
1314 		fi->fib_dead = 1;
1315 		free_fib_info(fi);
1316 	}
1317 
1318 	return ERR_PTR(err);
1319 }
1320 
1321 int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
1322 		     unsigned int *flags, bool skip_oif)
1323 {
1324 	if (nhc->nhc_flags & RTNH_F_DEAD)
1325 		*flags |= RTNH_F_DEAD;
1326 
1327 	if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
1328 		*flags |= RTNH_F_LINKDOWN;
1329 
1330 		rcu_read_lock();
1331 		switch (nhc->nhc_family) {
1332 		case AF_INET:
1333 			if (ip_ignore_linkdown(nhc->nhc_dev))
1334 				*flags |= RTNH_F_DEAD;
1335 			break;
1336 		case AF_INET6:
1337 			if (ip6_ignore_linkdown(nhc->nhc_dev))
1338 				*flags |= RTNH_F_DEAD;
1339 			break;
1340 		}
1341 		rcu_read_unlock();
1342 	}
1343 
1344 	if (nhc->nhc_has_gw) {
1345 		switch (nhc->nhc_family) {
1346 		case AF_INET:
1347 			if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1348 				goto nla_put_failure;
1349 			break;
1350 		case AF_INET6:
1351 			if (nla_put_in6_addr(skb, RTA_GATEWAY,
1352 					     &nhc->nhc_gw.ipv6) < 0)
1353 				goto nla_put_failure;
1354 			break;
1355 		}
1356 	}
1357 
1358 	*flags |= (nhc->nhc_flags & RTNH_F_ONLINK);
1359 	if (nhc->nhc_flags & RTNH_F_OFFLOAD)
1360 		*flags |= RTNH_F_OFFLOAD;
1361 
1362 	if (!skip_oif && nhc->nhc_dev &&
1363 	    nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
1364 		goto nla_put_failure;
1365 
1366 	if (nhc->nhc_lwtstate &&
1367 	    lwtunnel_fill_encap(skb, nhc->nhc_lwtstate) < 0)
1368 		goto nla_put_failure;
1369 
1370 	return 0;
1371 
1372 nla_put_failure:
1373 	return -EMSGSIZE;
1374 }
1375 EXPORT_SYMBOL_GPL(fib_nexthop_info);
1376 
1377 #if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1378 int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1379 		    int nh_weight)
1380 {
1381 	const struct net_device *dev = nhc->nhc_dev;
1382 	struct rtnexthop *rtnh;
1383 	unsigned int flags = 0;
1384 
1385 	rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1386 	if (!rtnh)
1387 		goto nla_put_failure;
1388 
1389 	rtnh->rtnh_hops = nh_weight - 1;
1390 	rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1391 
1392 	if (fib_nexthop_info(skb, nhc, &flags, true) < 0)
1393 		goto nla_put_failure;
1394 
1395 	rtnh->rtnh_flags = flags;
1396 
1397 	/* length of rtnetlink header + attributes */
1398 	rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1399 
1400 	return 0;
1401 
1402 nla_put_failure:
1403 	return -EMSGSIZE;
1404 }
1405 EXPORT_SYMBOL_GPL(fib_add_nexthop);
1406 #endif
1407 
1408 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1409 static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1410 {
1411 	struct nlattr *mp;
1412 
1413 	mp = nla_nest_start(skb, RTA_MULTIPATH);
1414 	if (!mp)
1415 		goto nla_put_failure;
1416 
1417 	for_nexthops(fi) {
1418 		if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight) < 0)
1419 			goto nla_put_failure;
1420 #ifdef CONFIG_IP_ROUTE_CLASSID
1421 		if (nh->nh_tclassid &&
1422 		    nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1423 			goto nla_put_failure;
1424 #endif
1425 	} endfor_nexthops(fi);
1426 
1427 	nla_nest_end(skb, mp);
1428 
1429 	return 0;
1430 
1431 nla_put_failure:
1432 	return -EMSGSIZE;
1433 }
1434 #else
1435 static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1436 {
1437 	return 0;
1438 }
1439 #endif
1440 
1441 int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1442 		  u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
1443 		  struct fib_info *fi, unsigned int flags)
1444 {
1445 	struct nlmsghdr *nlh;
1446 	struct rtmsg *rtm;
1447 
1448 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1449 	if (!nlh)
1450 		return -EMSGSIZE;
1451 
1452 	rtm = nlmsg_data(nlh);
1453 	rtm->rtm_family = AF_INET;
1454 	rtm->rtm_dst_len = dst_len;
1455 	rtm->rtm_src_len = 0;
1456 	rtm->rtm_tos = tos;
1457 	if (tb_id < 256)
1458 		rtm->rtm_table = tb_id;
1459 	else
1460 		rtm->rtm_table = RT_TABLE_COMPAT;
1461 	if (nla_put_u32(skb, RTA_TABLE, tb_id))
1462 		goto nla_put_failure;
1463 	rtm->rtm_type = type;
1464 	rtm->rtm_flags = fi->fib_flags;
1465 	rtm->rtm_scope = fi->fib_scope;
1466 	rtm->rtm_protocol = fi->fib_protocol;
1467 
1468 	if (rtm->rtm_dst_len &&
1469 	    nla_put_in_addr(skb, RTA_DST, dst))
1470 		goto nla_put_failure;
1471 	if (fi->fib_priority &&
1472 	    nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1473 		goto nla_put_failure;
1474 	if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
1475 		goto nla_put_failure;
1476 
1477 	if (fi->fib_prefsrc &&
1478 	    nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
1479 		goto nla_put_failure;
1480 	if (fi->fib_nhs == 1) {
1481 		struct fib_nh *nh = &fi->fib_nh[0];
1482 		unsigned int flags = 0;
1483 
1484 		if (fib_nexthop_info(skb, &nh->nh_common, &flags, false) < 0)
1485 			goto nla_put_failure;
1486 
1487 		rtm->rtm_flags = flags;
1488 #ifdef CONFIG_IP_ROUTE_CLASSID
1489 		if (nh->nh_tclassid &&
1490 		    nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1491 			goto nla_put_failure;
1492 #endif
1493 	} else {
1494 		if (fib_add_multipath(skb, fi) < 0)
1495 			goto nla_put_failure;
1496 	}
1497 
1498 	nlmsg_end(skb, nlh);
1499 	return 0;
1500 
1501 nla_put_failure:
1502 	nlmsg_cancel(skb, nlh);
1503 	return -EMSGSIZE;
1504 }
1505 
1506 /*
1507  * Update FIB if:
1508  * - local address disappeared -> we must delete all the entries
1509  *   referring to it.
1510  * - device went down -> we must shutdown all nexthops going via it.
1511  */
1512 int fib_sync_down_addr(struct net_device *dev, __be32 local)
1513 {
1514 	int ret = 0;
1515 	unsigned int hash = fib_laddr_hashfn(local);
1516 	struct hlist_head *head = &fib_info_laddrhash[hash];
1517 	struct net *net = dev_net(dev);
1518 	int tb_id = l3mdev_fib_table(dev);
1519 	struct fib_info *fi;
1520 
1521 	if (!fib_info_laddrhash || local == 0)
1522 		return 0;
1523 
1524 	hlist_for_each_entry(fi, head, fib_lhash) {
1525 		if (!net_eq(fi->fib_net, net) ||
1526 		    fi->fib_tb_id != tb_id)
1527 			continue;
1528 		if (fi->fib_prefsrc == local) {
1529 			fi->fib_flags |= RTNH_F_DEAD;
1530 			ret++;
1531 		}
1532 	}
1533 	return ret;
1534 }
1535 
1536 static int call_fib_nh_notifiers(struct fib_nh *nh,
1537 				 enum fib_event_type event_type)
1538 {
1539 	bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
1540 	struct fib_nh_notifier_info info = {
1541 		.fib_nh = nh,
1542 	};
1543 
1544 	switch (event_type) {
1545 	case FIB_EVENT_NH_ADD:
1546 		if (nh->fib_nh_flags & RTNH_F_DEAD)
1547 			break;
1548 		if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
1549 			break;
1550 		return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
1551 					   &info.info);
1552 	case FIB_EVENT_NH_DEL:
1553 		if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1554 		    (nh->fib_nh_flags & RTNH_F_DEAD))
1555 			return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
1556 						   event_type, &info.info);
1557 	default:
1558 		break;
1559 	}
1560 
1561 	return NOTIFY_DONE;
1562 }
1563 
1564 /* Update the PMTU of exceptions when:
1565  * - the new MTU of the first hop becomes smaller than the PMTU
1566  * - the old MTU was the same as the PMTU, and it limited discovery of
1567  *   larger MTUs on the path. With that limit raised, we can now
1568  *   discover larger MTUs
1569  * A special case is locked exceptions, for which the PMTU is smaller
1570  * than the minimal accepted PMTU:
1571  * - if the new MTU is greater than the PMTU, don't make any change
1572  * - otherwise, unlock and set PMTU
1573  */
1574 static void nh_update_mtu(struct fib_nh *nh, u32 new, u32 orig)
1575 {
1576 	struct fnhe_hash_bucket *bucket;
1577 	int i;
1578 
1579 	bucket = rcu_dereference_protected(nh->nh_exceptions, 1);
1580 	if (!bucket)
1581 		return;
1582 
1583 	for (i = 0; i < FNHE_HASH_SIZE; i++) {
1584 		struct fib_nh_exception *fnhe;
1585 
1586 		for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1587 		     fnhe;
1588 		     fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1589 			if (fnhe->fnhe_mtu_locked) {
1590 				if (new <= fnhe->fnhe_pmtu) {
1591 					fnhe->fnhe_pmtu = new;
1592 					fnhe->fnhe_mtu_locked = false;
1593 				}
1594 			} else if (new < fnhe->fnhe_pmtu ||
1595 				   orig == fnhe->fnhe_pmtu) {
1596 				fnhe->fnhe_pmtu = new;
1597 			}
1598 		}
1599 	}
1600 }
1601 
1602 void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1603 {
1604 	unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1605 	struct hlist_head *head = &fib_info_devhash[hash];
1606 	struct fib_nh *nh;
1607 
1608 	hlist_for_each_entry(nh, head, nh_hash) {
1609 		if (nh->fib_nh_dev == dev)
1610 			nh_update_mtu(nh, dev->mtu, orig_mtu);
1611 	}
1612 }
1613 
1614 /* Event              force Flags           Description
1615  * NETDEV_CHANGE      0     LINKDOWN        Carrier OFF, not for scope host
1616  * NETDEV_DOWN        0     LINKDOWN|DEAD   Link down, not for scope host
1617  * NETDEV_DOWN        1     LINKDOWN|DEAD   Last address removed
1618  * NETDEV_UNREGISTER  1     LINKDOWN|DEAD   Device removed
1619  */
1620 int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
1621 {
1622 	int ret = 0;
1623 	int scope = RT_SCOPE_NOWHERE;
1624 	struct fib_info *prev_fi = NULL;
1625 	unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1626 	struct hlist_head *head = &fib_info_devhash[hash];
1627 	struct fib_nh *nh;
1628 
1629 	if (force)
1630 		scope = -1;
1631 
1632 	hlist_for_each_entry(nh, head, nh_hash) {
1633 		struct fib_info *fi = nh->nh_parent;
1634 		int dead;
1635 
1636 		BUG_ON(!fi->fib_nhs);
1637 		if (nh->fib_nh_dev != dev || fi == prev_fi)
1638 			continue;
1639 		prev_fi = fi;
1640 		dead = 0;
1641 		change_nexthops(fi) {
1642 			if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
1643 				dead++;
1644 			else if (nexthop_nh->fib_nh_dev == dev &&
1645 				 nexthop_nh->fib_nh_scope != scope) {
1646 				switch (event) {
1647 				case NETDEV_DOWN:
1648 				case NETDEV_UNREGISTER:
1649 					nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
1650 					/* fall through */
1651 				case NETDEV_CHANGE:
1652 					nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1653 					break;
1654 				}
1655 				call_fib_nh_notifiers(nexthop_nh,
1656 						      FIB_EVENT_NH_DEL);
1657 				dead++;
1658 			}
1659 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1660 			if (event == NETDEV_UNREGISTER &&
1661 			    nexthop_nh->fib_nh_dev == dev) {
1662 				dead = fi->fib_nhs;
1663 				break;
1664 			}
1665 #endif
1666 		} endfor_nexthops(fi)
1667 		if (dead == fi->fib_nhs) {
1668 			switch (event) {
1669 			case NETDEV_DOWN:
1670 			case NETDEV_UNREGISTER:
1671 				fi->fib_flags |= RTNH_F_DEAD;
1672 				/* fall through */
1673 			case NETDEV_CHANGE:
1674 				fi->fib_flags |= RTNH_F_LINKDOWN;
1675 				break;
1676 			}
1677 			ret++;
1678 		}
1679 
1680 		fib_rebalance(fi);
1681 	}
1682 
1683 	return ret;
1684 }
1685 
1686 /* Must be invoked inside of an RCU protected region.  */
1687 static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
1688 {
1689 	struct fib_info *fi = NULL, *last_resort = NULL;
1690 	struct hlist_head *fa_head = res->fa_head;
1691 	struct fib_table *tb = res->table;
1692 	u8 slen = 32 - res->prefixlen;
1693 	int order = -1, last_idx = -1;
1694 	struct fib_alias *fa, *fa1 = NULL;
1695 	u32 last_prio = res->fi->fib_priority;
1696 	u8 last_tos = 0;
1697 
1698 	hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
1699 		struct fib_info *next_fi = fa->fa_info;
1700 
1701 		if (fa->fa_slen != slen)
1702 			continue;
1703 		if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1704 			continue;
1705 		if (fa->tb_id != tb->tb_id)
1706 			continue;
1707 		if (next_fi->fib_priority > last_prio &&
1708 		    fa->fa_tos == last_tos) {
1709 			if (last_tos)
1710 				continue;
1711 			break;
1712 		}
1713 		if (next_fi->fib_flags & RTNH_F_DEAD)
1714 			continue;
1715 		last_tos = fa->fa_tos;
1716 		last_prio = next_fi->fib_priority;
1717 
1718 		if (next_fi->fib_scope != res->scope ||
1719 		    fa->fa_type != RTN_UNICAST)
1720 			continue;
1721 		if (!next_fi->fib_nh[0].fib_nh_gw4 ||
1722 		    next_fi->fib_nh[0].fib_nh_scope != RT_SCOPE_LINK)
1723 			continue;
1724 
1725 		fib_alias_accessed(fa);
1726 
1727 		if (!fi) {
1728 			if (next_fi != res->fi)
1729 				break;
1730 			fa1 = fa;
1731 		} else if (!fib_detect_death(fi, order, &last_resort,
1732 					     &last_idx, fa1->fa_default)) {
1733 			fib_result_assign(res, fi);
1734 			fa1->fa_default = order;
1735 			goto out;
1736 		}
1737 		fi = next_fi;
1738 		order++;
1739 	}
1740 
1741 	if (order <= 0 || !fi) {
1742 		if (fa1)
1743 			fa1->fa_default = -1;
1744 		goto out;
1745 	}
1746 
1747 	if (!fib_detect_death(fi, order, &last_resort, &last_idx,
1748 			      fa1->fa_default)) {
1749 		fib_result_assign(res, fi);
1750 		fa1->fa_default = order;
1751 		goto out;
1752 	}
1753 
1754 	if (last_idx >= 0)
1755 		fib_result_assign(res, last_resort);
1756 	fa1->fa_default = last_idx;
1757 out:
1758 	return;
1759 }
1760 
1761 /*
1762  * Dead device goes up. We wake up dead nexthops.
1763  * It takes sense only on multipath routes.
1764  */
1765 int fib_sync_up(struct net_device *dev, unsigned int nh_flags)
1766 {
1767 	struct fib_info *prev_fi;
1768 	unsigned int hash;
1769 	struct hlist_head *head;
1770 	struct fib_nh *nh;
1771 	int ret;
1772 
1773 	if (!(dev->flags & IFF_UP))
1774 		return 0;
1775 
1776 	if (nh_flags & RTNH_F_DEAD) {
1777 		unsigned int flags = dev_get_flags(dev);
1778 
1779 		if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1780 			nh_flags |= RTNH_F_LINKDOWN;
1781 	}
1782 
1783 	prev_fi = NULL;
1784 	hash = fib_devindex_hashfn(dev->ifindex);
1785 	head = &fib_info_devhash[hash];
1786 	ret = 0;
1787 
1788 	hlist_for_each_entry(nh, head, nh_hash) {
1789 		struct fib_info *fi = nh->nh_parent;
1790 		int alive;
1791 
1792 		BUG_ON(!fi->fib_nhs);
1793 		if (nh->fib_nh_dev != dev || fi == prev_fi)
1794 			continue;
1795 
1796 		prev_fi = fi;
1797 		alive = 0;
1798 		change_nexthops(fi) {
1799 			if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
1800 				alive++;
1801 				continue;
1802 			}
1803 			if (!nexthop_nh->fib_nh_dev ||
1804 			    !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
1805 				continue;
1806 			if (nexthop_nh->fib_nh_dev != dev ||
1807 			    !__in_dev_get_rtnl(dev))
1808 				continue;
1809 			alive++;
1810 			nexthop_nh->fib_nh_flags &= ~nh_flags;
1811 			call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
1812 		} endfor_nexthops(fi)
1813 
1814 		if (alive > 0) {
1815 			fi->fib_flags &= ~nh_flags;
1816 			ret++;
1817 		}
1818 
1819 		fib_rebalance(fi);
1820 	}
1821 
1822 	return ret;
1823 }
1824 
1825 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1826 static bool fib_good_nh(const struct fib_nh *nh)
1827 {
1828 	int state = NUD_REACHABLE;
1829 
1830 	if (nh->fib_nh_scope == RT_SCOPE_LINK) {
1831 		struct neighbour *n;
1832 
1833 		rcu_read_lock_bh();
1834 
1835 		n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
1836 					      (__force u32)nh->fib_nh_gw4);
1837 		if (n)
1838 			state = n->nud_state;
1839 
1840 		rcu_read_unlock_bh();
1841 	}
1842 
1843 	return !!(state & NUD_VALID);
1844 }
1845 
1846 void fib_select_multipath(struct fib_result *res, int hash)
1847 {
1848 	struct fib_info *fi = res->fi;
1849 	struct net *net = fi->fib_net;
1850 	bool first = false;
1851 
1852 	change_nexthops(fi) {
1853 		if (net->ipv4.sysctl_fib_multipath_use_neigh) {
1854 			if (!fib_good_nh(nexthop_nh))
1855 				continue;
1856 			if (!first) {
1857 				res->nh_sel = nhsel;
1858 				res->nhc = &nexthop_nh->nh_common;
1859 				first = true;
1860 			}
1861 		}
1862 
1863 		if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
1864 			continue;
1865 
1866 		res->nh_sel = nhsel;
1867 		res->nhc = &nexthop_nh->nh_common;
1868 		return;
1869 	} endfor_nexthops(fi);
1870 }
1871 #endif
1872 
1873 void fib_select_path(struct net *net, struct fib_result *res,
1874 		     struct flowi4 *fl4, const struct sk_buff *skb)
1875 {
1876 	if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
1877 		goto check_saddr;
1878 
1879 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1880 	if (res->fi->fib_nhs > 1) {
1881 		int h = fib_multipath_hash(net, fl4, skb, NULL);
1882 
1883 		fib_select_multipath(res, h);
1884 	}
1885 	else
1886 #endif
1887 	if (!res->prefixlen &&
1888 	    res->table->tb_num_default > 1 &&
1889 	    res->type == RTN_UNICAST)
1890 		fib_select_default(fl4, res);
1891 
1892 check_saddr:
1893 	if (!fl4->saddr)
1894 		fl4->saddr = fib_result_prefsrc(net, res);
1895 }
1896