xref: /linux/net/ipv4/fib_frontend.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4  *		operating system.  INET is implemented using the  BSD Socket
5  *		interface as the means of communication with the user level.
6  *
7  *		IPv4 Forwarding Information Base: FIB frontend.
8  *
9  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  */
11 
12 #include <linux/module.h>
13 #include <linux/uaccess.h>
14 #include <linux/bitops.h>
15 #include <linux/capability.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/string.h>
20 #include <linux/socket.h>
21 #include <linux/sockios.h>
22 #include <linux/errno.h>
23 #include <linux/in.h>
24 #include <linux/inet.h>
25 #include <linux/inetdevice.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_addr.h>
28 #include <linux/if_arp.h>
29 #include <linux/skbuff.h>
30 #include <linux/cache.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <linux/slab.h>
34 
35 #include <net/flow.h>
36 #include <net/inet_dscp.h>
37 #include <net/ip.h>
38 #include <net/protocol.h>
39 #include <net/route.h>
40 #include <net/tcp.h>
41 #include <net/sock.h>
42 #include <net/arp.h>
43 #include <net/ip_fib.h>
44 #include <net/nexthop.h>
45 #include <net/rtnetlink.h>
46 #include <net/xfrm.h>
47 #include <net/l3mdev.h>
48 #include <net/lwtunnel.h>
49 #include <trace/events/fib.h>
50 
51 #ifndef CONFIG_IP_MULTIPLE_TABLES
52 
53 static int __net_init fib4_rules_init(struct net *net)
54 {
55 	struct fib_table *local_table, *main_table;
56 
57 	main_table  = fib_trie_table(RT_TABLE_MAIN, NULL);
58 	if (!main_table)
59 		return -ENOMEM;
60 
61 	local_table = fib_trie_table(RT_TABLE_LOCAL, main_table);
62 	if (!local_table)
63 		goto fail;
64 
65 	hlist_add_head_rcu(&local_table->tb_hlist,
66 				&net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX]);
67 	hlist_add_head_rcu(&main_table->tb_hlist,
68 				&net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]);
69 	return 0;
70 
71 fail:
72 	fib_free_table(main_table);
73 	return -ENOMEM;
74 }
75 #else
76 
77 struct fib_table *fib_new_table(struct net *net, u32 id)
78 {
79 	struct fib_table *tb, *alias = NULL;
80 	unsigned int h;
81 
82 	if (id == 0)
83 		id = RT_TABLE_MAIN;
84 	tb = fib_get_table(net, id);
85 	if (tb)
86 		return tb;
87 
88 	if (id == RT_TABLE_LOCAL && !net->ipv4.fib_has_custom_rules)
89 		alias = fib_new_table(net, RT_TABLE_MAIN);
90 
91 	tb = fib_trie_table(id, alias);
92 	if (!tb)
93 		return NULL;
94 
95 	switch (id) {
96 	case RT_TABLE_MAIN:
97 		rcu_assign_pointer(net->ipv4.fib_main, tb);
98 		break;
99 	case RT_TABLE_DEFAULT:
100 		rcu_assign_pointer(net->ipv4.fib_default, tb);
101 		break;
102 	default:
103 		break;
104 	}
105 
106 	h = id & (FIB_TABLE_HASHSZ - 1);
107 	hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]);
108 	return tb;
109 }
110 EXPORT_SYMBOL_GPL(fib_new_table);
111 
112 /* caller must hold either rtnl or rcu read lock */
113 struct fib_table *fib_get_table(struct net *net, u32 id)
114 {
115 	struct fib_table *tb;
116 	struct hlist_head *head;
117 	unsigned int h;
118 
119 	if (id == 0)
120 		id = RT_TABLE_MAIN;
121 	h = id & (FIB_TABLE_HASHSZ - 1);
122 
123 	head = &net->ipv4.fib_table_hash[h];
124 	hlist_for_each_entry_rcu(tb, head, tb_hlist,
125 				 lockdep_rtnl_is_held()) {
126 		if (tb->tb_id == id)
127 			return tb;
128 	}
129 	return NULL;
130 }
131 #endif /* CONFIG_IP_MULTIPLE_TABLES */
132 
133 static void fib_replace_table(struct net *net, struct fib_table *old,
134 			      struct fib_table *new)
135 {
136 #ifdef CONFIG_IP_MULTIPLE_TABLES
137 	switch (new->tb_id) {
138 	case RT_TABLE_MAIN:
139 		rcu_assign_pointer(net->ipv4.fib_main, new);
140 		break;
141 	case RT_TABLE_DEFAULT:
142 		rcu_assign_pointer(net->ipv4.fib_default, new);
143 		break;
144 	default:
145 		break;
146 	}
147 
148 #endif
149 	/* replace the old table in the hlist */
150 	hlist_replace_rcu(&old->tb_hlist, &new->tb_hlist);
151 }
152 
153 int fib_unmerge(struct net *net)
154 {
155 	struct fib_table *old, *new, *main_table;
156 
157 	/* attempt to fetch local table if it has been allocated */
158 	old = fib_get_table(net, RT_TABLE_LOCAL);
159 	if (!old)
160 		return 0;
161 
162 	new = fib_trie_unmerge(old);
163 	if (!new)
164 		return -ENOMEM;
165 
166 	/* table is already unmerged */
167 	if (new == old)
168 		return 0;
169 
170 	/* replace merged table with clean table */
171 	fib_replace_table(net, old, new);
172 	fib_free_table(old);
173 
174 	/* attempt to fetch main table if it has been allocated */
175 	main_table = fib_get_table(net, RT_TABLE_MAIN);
176 	if (!main_table)
177 		return 0;
178 
179 	/* flush local entries from main table */
180 	fib_table_flush_external(main_table);
181 
182 	return 0;
183 }
184 
185 void fib_flush(struct net *net)
186 {
187 	int flushed = 0;
188 	unsigned int h;
189 
190 	for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
191 		struct hlist_head *head = &net->ipv4.fib_table_hash[h];
192 		struct hlist_node *tmp;
193 		struct fib_table *tb;
194 
195 		hlist_for_each_entry_safe(tb, tmp, head, tb_hlist)
196 			flushed += fib_table_flush(net, tb, false);
197 	}
198 
199 	if (flushed)
200 		rt_cache_flush(net);
201 }
202 
203 /*
204  * Find address type as if only "dev" was present in the system. If
205  * on_dev is NULL then all interfaces are taken into consideration.
206  */
207 static inline unsigned int __inet_dev_addr_type(struct net *net,
208 						const struct net_device *dev,
209 						__be32 addr, u32 tb_id)
210 {
211 	struct flowi4		fl4 = { .daddr = addr };
212 	struct fib_result	res;
213 	unsigned int ret = RTN_BROADCAST;
214 	struct fib_table *table;
215 
216 	if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
217 		return RTN_BROADCAST;
218 	if (ipv4_is_multicast(addr))
219 		return RTN_MULTICAST;
220 
221 	rcu_read_lock();
222 
223 	table = fib_get_table(net, tb_id);
224 	if (table) {
225 		ret = RTN_UNICAST;
226 		if (!fib_table_lookup(table, &fl4, &res, FIB_LOOKUP_NOREF)) {
227 			struct fib_nh_common *nhc = fib_info_nhc(res.fi, 0);
228 
229 			if (!dev || dev == nhc->nhc_dev)
230 				ret = res.type;
231 		}
232 	}
233 
234 	rcu_read_unlock();
235 	return ret;
236 }
237 
238 unsigned int inet_addr_type_table(struct net *net, __be32 addr, u32 tb_id)
239 {
240 	return __inet_dev_addr_type(net, NULL, addr, tb_id);
241 }
242 EXPORT_SYMBOL(inet_addr_type_table);
243 
244 unsigned int inet_addr_type(struct net *net, __be32 addr)
245 {
246 	return __inet_dev_addr_type(net, NULL, addr, RT_TABLE_LOCAL);
247 }
248 EXPORT_SYMBOL(inet_addr_type);
249 
250 unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
251 				__be32 addr)
252 {
253 	u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL;
254 
255 	return __inet_dev_addr_type(net, dev, addr, rt_table);
256 }
257 EXPORT_SYMBOL(inet_dev_addr_type);
258 
259 /* inet_addr_type with dev == NULL but using the table from a dev
260  * if one is associated
261  */
262 unsigned int inet_addr_type_dev_table(struct net *net,
263 				      const struct net_device *dev,
264 				      __be32 addr)
265 {
266 	u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL;
267 
268 	return __inet_dev_addr_type(net, NULL, addr, rt_table);
269 }
270 EXPORT_SYMBOL(inet_addr_type_dev_table);
271 
272 __be32 fib_compute_spec_dst(struct sk_buff *skb)
273 {
274 	struct net_device *dev = skb->dev;
275 	struct in_device *in_dev;
276 	struct fib_result res;
277 	struct rtable *rt;
278 	struct net *net;
279 	int scope;
280 
281 	rt = skb_rtable(skb);
282 	if ((rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST | RTCF_LOCAL)) ==
283 	    RTCF_LOCAL)
284 		return ip_hdr(skb)->daddr;
285 
286 	in_dev = __in_dev_get_rcu(dev);
287 
288 	net = dev_net(dev);
289 
290 	scope = RT_SCOPE_UNIVERSE;
291 	if (!ipv4_is_zeronet(ip_hdr(skb)->saddr)) {
292 		bool vmark = in_dev && IN_DEV_SRC_VMARK(in_dev);
293 		struct flowi4 fl4 = {
294 			.flowi4_iif = LOOPBACK_IFINDEX,
295 			.flowi4_l3mdev = l3mdev_master_ifindex_rcu(dev),
296 			.daddr = ip_hdr(skb)->saddr,
297 			.flowi4_dscp = ip4h_dscp(ip_hdr(skb)),
298 			.flowi4_scope = scope,
299 			.flowi4_mark = vmark ? skb->mark : 0,
300 		};
301 		if (!fib_lookup(net, &fl4, &res, 0))
302 			return fib_result_prefsrc(net, &res);
303 	} else {
304 		scope = RT_SCOPE_LINK;
305 	}
306 
307 	return inet_select_addr(dev, ip_hdr(skb)->saddr, scope);
308 }
309 
310 bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev)
311 {
312 	bool dev_match = false;
313 #ifdef CONFIG_IP_ROUTE_MULTIPATH
314 	if (unlikely(fi->nh)) {
315 		dev_match = nexthop_uses_dev(fi->nh, dev);
316 	} else {
317 		int ret;
318 
319 		for (ret = 0; ret < fib_info_num_path(fi); ret++) {
320 			const struct fib_nh_common *nhc = fib_info_nhc(fi, ret);
321 
322 			if (nhc_l3mdev_matches_dev(nhc, dev)) {
323 				dev_match = true;
324 				break;
325 			}
326 		}
327 	}
328 #else
329 	if (fib_info_nhc(fi, 0)->nhc_dev == dev)
330 		dev_match = true;
331 #endif
332 
333 	return dev_match;
334 }
335 EXPORT_SYMBOL_GPL(fib_info_nh_uses_dev);
336 
337 /* Given (packet source, input interface) and optional (dst, oif, tos):
338  * - (main) check, that source is valid i.e. not broadcast or our local
339  *   address.
340  * - figure out what "logical" interface this packet arrived
341  *   and calculate "specific destination" address.
342  * - check, that packet arrived from expected physical interface.
343  * called with rcu_read_lock()
344  */
345 static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
346 				 dscp_t dscp, int oif, struct net_device *dev,
347 				 int rpf, struct in_device *idev, u32 *itag)
348 {
349 	struct net *net = dev_net(dev);
350 	enum skb_drop_reason reason;
351 	struct flow_keys flkeys;
352 	int ret, no_addr;
353 	struct fib_result res;
354 	struct flowi4 fl4;
355 	bool dev_match;
356 
357 	fl4.flowi4_oif = 0;
358 	fl4.flowi4_l3mdev = l3mdev_master_ifindex_rcu(dev);
359 	fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX;
360 	fl4.daddr = src;
361 	fl4.saddr = dst;
362 	fl4.flowi4_dscp = dscp;
363 	fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
364 	fl4.flowi4_tun_key.tun_id = 0;
365 	fl4.flowi4_flags = 0;
366 	fl4.flowi4_uid = sock_net_uid(net, NULL);
367 	fl4.flowi4_multipath_hash = 0;
368 
369 	no_addr = idev->ifa_list == NULL;
370 
371 	fl4.flowi4_mark = IN_DEV_SRC_VMARK(idev) ? skb->mark : 0;
372 	if (!fib4_rules_early_flow_dissect(net, skb, &fl4, &flkeys)) {
373 		fl4.flowi4_proto = 0;
374 		fl4.fl4_sport = 0;
375 		fl4.fl4_dport = 0;
376 	} else {
377 		swap(fl4.fl4_sport, fl4.fl4_dport);
378 	}
379 
380 	if (fib_lookup(net, &fl4, &res, 0))
381 		goto last_resort;
382 	if (res.type != RTN_UNICAST) {
383 		if (res.type != RTN_LOCAL) {
384 			reason = SKB_DROP_REASON_IP_INVALID_SOURCE;
385 			goto e_inval;
386 		} else if (!IN_DEV_ACCEPT_LOCAL(idev)) {
387 			reason = SKB_DROP_REASON_IP_LOCAL_SOURCE;
388 			goto e_inval;
389 		}
390 	}
391 	fib_combine_itag(itag, &res);
392 
393 	dev_match = fib_info_nh_uses_dev(res.fi, dev);
394 	/* This is not common, loopback packets retain skb_dst so normally they
395 	 * would not even hit this slow path.
396 	 */
397 	dev_match = dev_match || (res.type == RTN_LOCAL &&
398 				  dev == net->loopback_dev);
399 	if (dev_match) {
400 		ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST;
401 		return ret;
402 	}
403 	if (no_addr)
404 		goto last_resort;
405 	if (rpf == 1)
406 		goto e_rpf;
407 	fl4.flowi4_oif = dev->ifindex;
408 
409 	ret = 0;
410 	if (fib_lookup(net, &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE) == 0) {
411 		if (res.type == RTN_UNICAST)
412 			ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST;
413 	}
414 	return ret;
415 
416 last_resort:
417 	if (rpf)
418 		goto e_rpf;
419 	*itag = 0;
420 	return 0;
421 
422 e_inval:
423 	return -reason;
424 e_rpf:
425 	return -SKB_DROP_REASON_IP_RPFILTER;
426 }
427 
428 /* Ignore rp_filter for packets protected by IPsec. */
429 int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
430 			dscp_t dscp, int oif, struct net_device *dev,
431 			struct in_device *idev, u32 *itag)
432 {
433 	int r = secpath_exists(skb) ? 0 : IN_DEV_RPFILTER(idev);
434 	struct net *net = dev_net(dev);
435 
436 	if (!r && !fib_num_tclassid_users(net) &&
437 	    (dev->ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) {
438 		if (IN_DEV_ACCEPT_LOCAL(idev))
439 			goto ok;
440 		/* with custom local routes in place, checking local addresses
441 		 * only will be too optimistic, with custom rules, checking
442 		 * local addresses only can be too strict, e.g. due to vrf
443 		 */
444 		if (net->ipv4.fib_has_custom_local_routes ||
445 		    fib4_has_custom_rules(net))
446 			goto full_check;
447 		/* Within the same container, it is regarded as a martian source,
448 		 * and the same host but different containers are not.
449 		 */
450 		if (inet_lookup_ifaddr_rcu(net, src))
451 			return -SKB_DROP_REASON_IP_LOCAL_SOURCE;
452 
453 ok:
454 		*itag = 0;
455 		return 0;
456 	}
457 
458 full_check:
459 	return __fib_validate_source(skb, src, dst, dscp, oif, dev, r, idev,
460 				     itag);
461 }
462 
463 static inline __be32 sk_extract_addr(struct sockaddr *addr)
464 {
465 	return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
466 }
467 
468 static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
469 {
470 	struct nlattr *nla;
471 
472 	nla = (struct nlattr *) ((char *) mx + len);
473 	nla->nla_type = type;
474 	nla->nla_len = nla_attr_size(4);
475 	*(u32 *) nla_data(nla) = value;
476 
477 	return len + nla_total_size(4);
478 }
479 
480 static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,
481 				 struct fib_config *cfg)
482 {
483 	__be32 addr;
484 	int plen;
485 
486 	memset(cfg, 0, sizeof(*cfg));
487 	cfg->fc_nlinfo.nl_net = net;
488 
489 	if (rt->rt_dst.sa_family != AF_INET)
490 		return -EAFNOSUPPORT;
491 
492 	/*
493 	 * Check mask for validity:
494 	 * a) it must be contiguous.
495 	 * b) destination must have all host bits clear.
496 	 * c) if application forgot to set correct family (AF_INET),
497 	 *    reject request unless it is absolutely clear i.e.
498 	 *    both family and mask are zero.
499 	 */
500 	plen = 32;
501 	addr = sk_extract_addr(&rt->rt_dst);
502 	if (!(rt->rt_flags & RTF_HOST)) {
503 		__be32 mask = sk_extract_addr(&rt->rt_genmask);
504 
505 		if (rt->rt_genmask.sa_family != AF_INET) {
506 			if (mask || rt->rt_genmask.sa_family)
507 				return -EAFNOSUPPORT;
508 		}
509 
510 		if (bad_mask(mask, addr))
511 			return -EINVAL;
512 
513 		plen = inet_mask_len(mask);
514 	}
515 
516 	cfg->fc_dst_len = plen;
517 	cfg->fc_dst = addr;
518 
519 	if (cmd != SIOCDELRT) {
520 		cfg->fc_nlflags = NLM_F_CREATE;
521 		cfg->fc_protocol = RTPROT_BOOT;
522 	}
523 
524 	if (rt->rt_metric)
525 		cfg->fc_priority = rt->rt_metric - 1;
526 
527 	if (rt->rt_flags & RTF_REJECT) {
528 		cfg->fc_scope = RT_SCOPE_HOST;
529 		cfg->fc_type = RTN_UNREACHABLE;
530 		return 0;
531 	}
532 
533 	cfg->fc_scope = RT_SCOPE_NOWHERE;
534 	cfg->fc_type = RTN_UNICAST;
535 
536 	if (rt->rt_dev) {
537 		char *colon;
538 		struct net_device *dev;
539 		char devname[IFNAMSIZ];
540 
541 		if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
542 			return -EFAULT;
543 
544 		devname[IFNAMSIZ-1] = 0;
545 		colon = strchr(devname, ':');
546 		if (colon)
547 			*colon = 0;
548 		dev = __dev_get_by_name(net, devname);
549 		if (!dev)
550 			return -ENODEV;
551 		cfg->fc_oif = dev->ifindex;
552 		cfg->fc_table = l3mdev_fib_table(dev);
553 		if (colon) {
554 			const struct in_ifaddr *ifa;
555 			struct in_device *in_dev;
556 
557 			in_dev = __in_dev_get_rtnl_net(dev);
558 			if (!in_dev)
559 				return -ENODEV;
560 
561 			*colon = ':';
562 
563 			in_dev_for_each_ifa_rtnl_net(net, ifa, in_dev) {
564 				if (strcmp(ifa->ifa_label, devname) == 0)
565 					break;
566 			}
567 
568 			if (!ifa)
569 				return -ENODEV;
570 			cfg->fc_prefsrc = ifa->ifa_local;
571 		}
572 	}
573 
574 	addr = sk_extract_addr(&rt->rt_gateway);
575 	if (rt->rt_gateway.sa_family == AF_INET && addr) {
576 		unsigned int addr_type;
577 
578 		cfg->fc_gw4 = addr;
579 		cfg->fc_gw_family = AF_INET;
580 		addr_type = inet_addr_type_table(net, addr, cfg->fc_table);
581 		if (rt->rt_flags & RTF_GATEWAY &&
582 		    addr_type == RTN_UNICAST)
583 			cfg->fc_scope = RT_SCOPE_UNIVERSE;
584 	}
585 
586 	if (!cfg->fc_table)
587 		cfg->fc_table = RT_TABLE_MAIN;
588 
589 	if (cmd == SIOCDELRT)
590 		return 0;
591 
592 	if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw_family)
593 		return -EINVAL;
594 
595 	if (cfg->fc_scope == RT_SCOPE_NOWHERE)
596 		cfg->fc_scope = RT_SCOPE_LINK;
597 
598 	if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
599 		struct nlattr *mx;
600 		int len = 0;
601 
602 		mx = kcalloc(3, nla_total_size(4), GFP_KERNEL);
603 		if (!mx)
604 			return -ENOMEM;
605 
606 		if (rt->rt_flags & RTF_MTU)
607 			len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
608 
609 		if (rt->rt_flags & RTF_WINDOW)
610 			len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
611 
612 		if (rt->rt_flags & RTF_IRTT)
613 			len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
614 
615 		cfg->fc_mx = mx;
616 		cfg->fc_mx_len = len;
617 	}
618 
619 	return 0;
620 }
621 
622 /*
623  * Handle IP routing ioctl calls.
624  * These are used to manipulate the routing tables
625  */
626 int ip_rt_ioctl(struct net *net, unsigned int cmd, struct rtentry *rt)
627 {
628 	struct fib_config cfg;
629 	int err;
630 
631 	switch (cmd) {
632 	case SIOCADDRT:		/* Add a route */
633 	case SIOCDELRT:		/* Delete a route */
634 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
635 			return -EPERM;
636 
637 		rtnl_net_lock(net);
638 		err = rtentry_to_fib_config(net, cmd, rt, &cfg);
639 		if (err == 0) {
640 			struct fib_table *tb;
641 
642 			if (cmd == SIOCDELRT) {
643 				tb = fib_get_table(net, cfg.fc_table);
644 				if (tb)
645 					err = fib_table_delete(net, tb, &cfg,
646 							       NULL);
647 				else
648 					err = -ESRCH;
649 			} else {
650 				tb = fib_new_table(net, cfg.fc_table);
651 				if (tb)
652 					err = fib_table_insert(net, tb,
653 							       &cfg, NULL);
654 				else
655 					err = -ENOBUFS;
656 			}
657 
658 			/* allocated by rtentry_to_fib_config() */
659 			kfree(cfg.fc_mx);
660 		}
661 		rtnl_net_unlock(net);
662 		return err;
663 	}
664 	return -EINVAL;
665 }
666 
667 const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = {
668 	[RTA_UNSPEC]		= { .strict_start_type = RTA_DPORT + 1 },
669 	[RTA_DST]		= { .type = NLA_U32 },
670 	[RTA_SRC]		= { .type = NLA_U32 },
671 	[RTA_IIF]		= { .type = NLA_U32 },
672 	[RTA_OIF]		= { .type = NLA_U32 },
673 	[RTA_GATEWAY]		= { .type = NLA_U32 },
674 	[RTA_PRIORITY]		= { .type = NLA_U32 },
675 	[RTA_PREFSRC]		= { .type = NLA_U32 },
676 	[RTA_METRICS]		= { .type = NLA_NESTED },
677 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
678 	[RTA_FLOW]		= { .type = NLA_U32 },
679 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
680 	[RTA_ENCAP]		= { .type = NLA_NESTED },
681 	[RTA_UID]		= { .type = NLA_U32 },
682 	[RTA_MARK]		= { .type = NLA_U32 },
683 	[RTA_TABLE]		= { .type = NLA_U32 },
684 	[RTA_IP_PROTO]		= { .type = NLA_U8 },
685 	[RTA_SPORT]		= { .type = NLA_U16 },
686 	[RTA_DPORT]		= { .type = NLA_U16 },
687 	[RTA_NH_ID]		= { .type = NLA_U32 },
688 };
689 
690 int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla,
691 		    struct netlink_ext_ack *extack)
692 {
693 	struct rtvia *via;
694 	int alen;
695 
696 	if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) {
697 		NL_SET_ERR_MSG(extack, "Invalid attribute length for RTA_VIA");
698 		return -EINVAL;
699 	}
700 
701 	via = nla_data(nla);
702 	alen = nla_len(nla) - offsetof(struct rtvia, rtvia_addr);
703 
704 	switch (via->rtvia_family) {
705 	case AF_INET:
706 		if (alen != sizeof(__be32)) {
707 			NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_VIA");
708 			return -EINVAL;
709 		}
710 		cfg->fc_gw_family = AF_INET;
711 		cfg->fc_gw4 = *((__be32 *)via->rtvia_addr);
712 		break;
713 	case AF_INET6:
714 #if IS_ENABLED(CONFIG_IPV6)
715 		if (alen != sizeof(struct in6_addr)) {
716 			NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_VIA");
717 			return -EINVAL;
718 		}
719 		cfg->fc_gw_family = AF_INET6;
720 		cfg->fc_gw6 = *((struct in6_addr *)via->rtvia_addr);
721 #else
722 		NL_SET_ERR_MSG(extack, "IPv6 support not enabled in kernel");
723 		return -EINVAL;
724 #endif
725 		break;
726 	default:
727 		NL_SET_ERR_MSG(extack, "Unsupported address family in RTA_VIA");
728 		return -EINVAL;
729 	}
730 
731 	return 0;
732 }
733 
734 static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
735 			     struct nlmsghdr *nlh, struct fib_config *cfg,
736 			     struct netlink_ext_ack *extack)
737 {
738 	bool has_gw = false, has_via = false;
739 	struct nlattr *attr;
740 	int err, remaining;
741 	struct rtmsg *rtm;
742 
743 	err = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
744 					rtm_ipv4_policy, extack);
745 	if (err < 0)
746 		goto errout;
747 
748 	memset(cfg, 0, sizeof(*cfg));
749 
750 	rtm = nlmsg_data(nlh);
751 
752 	if (!inet_validate_dscp(rtm->rtm_tos)) {
753 		NL_SET_ERR_MSG(extack,
754 			       "Invalid dsfield (tos): ECN bits must be 0");
755 		err = -EINVAL;
756 		goto errout;
757 	}
758 	cfg->fc_dscp = inet_dsfield_to_dscp(rtm->rtm_tos);
759 
760 	cfg->fc_dst_len = rtm->rtm_dst_len;
761 	cfg->fc_table = rtm->rtm_table;
762 	cfg->fc_protocol = rtm->rtm_protocol;
763 	cfg->fc_scope = rtm->rtm_scope;
764 	cfg->fc_type = rtm->rtm_type;
765 	cfg->fc_flags = rtm->rtm_flags;
766 	cfg->fc_nlflags = nlh->nlmsg_flags;
767 
768 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
769 	cfg->fc_nlinfo.nlh = nlh;
770 	cfg->fc_nlinfo.nl_net = net;
771 
772 	if (cfg->fc_type > RTN_MAX) {
773 		NL_SET_ERR_MSG(extack, "Invalid route type");
774 		err = -EINVAL;
775 		goto errout;
776 	}
777 
778 	nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
779 		switch (nla_type(attr)) {
780 		case RTA_DST:
781 			cfg->fc_dst = nla_get_be32(attr);
782 			break;
783 		case RTA_OIF:
784 			cfg->fc_oif = nla_get_u32(attr);
785 			break;
786 		case RTA_GATEWAY:
787 			has_gw = true;
788 			cfg->fc_gw4 = nla_get_be32(attr);
789 			if (cfg->fc_gw4)
790 				cfg->fc_gw_family = AF_INET;
791 			break;
792 		case RTA_VIA:
793 			has_via = true;
794 			err = fib_gw_from_via(cfg, attr, extack);
795 			if (err)
796 				goto errout;
797 			break;
798 		case RTA_PRIORITY:
799 			cfg->fc_priority = nla_get_u32(attr);
800 			break;
801 		case RTA_PREFSRC:
802 			cfg->fc_prefsrc = nla_get_be32(attr);
803 			break;
804 		case RTA_METRICS:
805 			cfg->fc_mx = nla_data(attr);
806 			cfg->fc_mx_len = nla_len(attr);
807 			break;
808 		case RTA_MULTIPATH:
809 			err = lwtunnel_valid_encap_type_attr(nla_data(attr),
810 							     nla_len(attr),
811 							     extack);
812 			if (err < 0)
813 				goto errout;
814 			cfg->fc_mp = nla_data(attr);
815 			cfg->fc_mp_len = nla_len(attr);
816 			break;
817 		case RTA_FLOW:
818 			cfg->fc_flow = nla_get_u32(attr);
819 			break;
820 		case RTA_TABLE:
821 			cfg->fc_table = nla_get_u32(attr);
822 			break;
823 		case RTA_ENCAP:
824 			cfg->fc_encap = attr;
825 			break;
826 		case RTA_ENCAP_TYPE:
827 			cfg->fc_encap_type = nla_get_u16(attr);
828 			err = lwtunnel_valid_encap_type(cfg->fc_encap_type,
829 							extack);
830 			if (err < 0)
831 				goto errout;
832 			break;
833 		case RTA_NH_ID:
834 			cfg->fc_nh_id = nla_get_u32(attr);
835 			break;
836 		}
837 	}
838 
839 	if (cfg->fc_dst_len > 32) {
840 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
841 		err = -EINVAL;
842 		goto errout;
843 	}
844 
845 	if (cfg->fc_dst_len < 32 && (ntohl(cfg->fc_dst) << cfg->fc_dst_len)) {
846 		NL_SET_ERR_MSG(extack, "Invalid prefix for given prefix length");
847 		err = -EINVAL;
848 		goto errout;
849 	}
850 
851 	if (cfg->fc_nh_id) {
852 		if (cfg->fc_oif || cfg->fc_gw_family ||
853 		    cfg->fc_encap || cfg->fc_mp) {
854 			NL_SET_ERR_MSG(extack,
855 				       "Nexthop specification and nexthop id are mutually exclusive");
856 			err = -EINVAL;
857 			goto errout;
858 		}
859 	}
860 
861 	if (has_gw && has_via) {
862 		NL_SET_ERR_MSG(extack,
863 			       "Nexthop configuration can not contain both GATEWAY and VIA");
864 		err = -EINVAL;
865 		goto errout;
866 	}
867 
868 	if (!cfg->fc_table)
869 		cfg->fc_table = RT_TABLE_MAIN;
870 
871 	return 0;
872 errout:
873 	return err;
874 }
875 
876 static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
877 			     struct netlink_ext_ack *extack)
878 {
879 	struct net *net = sock_net(skb->sk);
880 	struct fib_config cfg;
881 	struct fib_table *tb;
882 	int err;
883 
884 	err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
885 	if (err < 0)
886 		goto errout;
887 
888 	rtnl_net_lock(net);
889 
890 	if (cfg.fc_nh_id && !nexthop_find_by_id(net, cfg.fc_nh_id)) {
891 		NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
892 		err = -EINVAL;
893 		goto unlock;
894 	}
895 
896 	tb = fib_get_table(net, cfg.fc_table);
897 	if (!tb) {
898 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
899 		err = -ESRCH;
900 		goto unlock;
901 	}
902 
903 	err = fib_table_delete(net, tb, &cfg, extack);
904 unlock:
905 	rtnl_net_unlock(net);
906 errout:
907 	return err;
908 }
909 
910 static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
911 			     struct netlink_ext_ack *extack)
912 {
913 	struct net *net = sock_net(skb->sk);
914 	struct fib_config cfg;
915 	struct fib_table *tb;
916 	int err;
917 
918 	err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
919 	if (err < 0)
920 		goto errout;
921 
922 	rtnl_net_lock(net);
923 
924 	tb = fib_new_table(net, cfg.fc_table);
925 	if (!tb) {
926 		err = -ENOBUFS;
927 		goto unlock;
928 	}
929 
930 	err = fib_table_insert(net, tb, &cfg, extack);
931 	if (!err && cfg.fc_type == RTN_LOCAL)
932 		net->ipv4.fib_has_custom_local_routes = true;
933 
934 unlock:
935 	rtnl_net_unlock(net);
936 errout:
937 	return err;
938 }
939 
940 int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
941 			  struct fib_dump_filter *filter,
942 			  struct netlink_callback *cb)
943 {
944 	struct netlink_ext_ack *extack = cb->extack;
945 	struct nlattr *tb[RTA_MAX + 1];
946 	struct rtmsg *rtm;
947 	int err, i;
948 
949 	if (filter->rtnl_held)
950 		ASSERT_RTNL();
951 
952 	rtm = nlmsg_payload(nlh, sizeof(*rtm));
953 	if (!rtm) {
954 		NL_SET_ERR_MSG(extack, "Invalid header for FIB dump request");
955 		return -EINVAL;
956 	}
957 
958 	if (rtm->rtm_dst_len || rtm->rtm_src_len  || rtm->rtm_tos   ||
959 	    rtm->rtm_scope) {
960 		NL_SET_ERR_MSG(extack, "Invalid values in header for FIB dump request");
961 		return -EINVAL;
962 	}
963 
964 	if (rtm->rtm_flags & ~(RTM_F_CLONED | RTM_F_PREFIX)) {
965 		NL_SET_ERR_MSG(extack, "Invalid flags for FIB dump request");
966 		return -EINVAL;
967 	}
968 	if (rtm->rtm_flags & RTM_F_CLONED)
969 		filter->dump_routes = false;
970 	else
971 		filter->dump_exceptions = false;
972 
973 	filter->flags    = rtm->rtm_flags;
974 	filter->protocol = rtm->rtm_protocol;
975 	filter->rt_type  = rtm->rtm_type;
976 	filter->table_id = rtm->rtm_table;
977 
978 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
979 					    rtm_ipv4_policy, extack);
980 	if (err < 0)
981 		return err;
982 
983 	for (i = 0; i <= RTA_MAX; ++i) {
984 		int ifindex;
985 
986 		if (!tb[i])
987 			continue;
988 
989 		switch (i) {
990 		case RTA_TABLE:
991 			filter->table_id = nla_get_u32(tb[i]);
992 			break;
993 		case RTA_OIF:
994 			ifindex = nla_get_u32(tb[i]);
995 			if (filter->rtnl_held)
996 				filter->dev = __dev_get_by_index(net, ifindex);
997 			else
998 				filter->dev = dev_get_by_index_rcu(net, ifindex);
999 			if (!filter->dev)
1000 				return -ENODEV;
1001 			break;
1002 		default:
1003 			NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request");
1004 			return -EINVAL;
1005 		}
1006 	}
1007 
1008 	if (filter->flags || filter->protocol || filter->rt_type ||
1009 	    filter->table_id || filter->dev) {
1010 		filter->filter_set = 1;
1011 		cb->answer_flags = NLM_F_DUMP_FILTERED;
1012 	}
1013 
1014 	return 0;
1015 }
1016 EXPORT_SYMBOL_GPL(ip_valid_fib_dump_req);
1017 
1018 static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
1019 {
1020 	struct fib_dump_filter filter = {
1021 		.dump_routes = true,
1022 		.dump_exceptions = true,
1023 		.rtnl_held = false,
1024 	};
1025 	const struct nlmsghdr *nlh = cb->nlh;
1026 	struct net *net = sock_net(skb->sk);
1027 	unsigned int h, s_h;
1028 	unsigned int e = 0, s_e;
1029 	struct fib_table *tb;
1030 	struct hlist_head *head;
1031 	int dumped = 0, err = 0;
1032 
1033 	rcu_read_lock();
1034 	if (cb->strict_check) {
1035 		err = ip_valid_fib_dump_req(net, nlh, &filter, cb);
1036 		if (err < 0)
1037 			goto unlock;
1038 	} else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
1039 		struct rtmsg *rtm = nlmsg_data(nlh);
1040 
1041 		filter.flags = rtm->rtm_flags & (RTM_F_PREFIX | RTM_F_CLONED);
1042 	}
1043 
1044 	/* ipv4 does not use prefix flag */
1045 	if (filter.flags & RTM_F_PREFIX)
1046 		goto unlock;
1047 
1048 	if (filter.table_id) {
1049 		tb = fib_get_table(net, filter.table_id);
1050 		if (!tb) {
1051 			if (rtnl_msg_family(cb->nlh) != PF_INET)
1052 				goto unlock;
1053 
1054 			NL_SET_ERR_MSG(cb->extack, "ipv4: FIB table does not exist");
1055 			err = -ENOENT;
1056 			goto unlock;
1057 		}
1058 		err = fib_table_dump(tb, skb, cb, &filter);
1059 		goto unlock;
1060 	}
1061 
1062 	s_h = cb->args[0];
1063 	s_e = cb->args[1];
1064 
1065 	err = 0;
1066 	for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
1067 		e = 0;
1068 		head = &net->ipv4.fib_table_hash[h];
1069 		hlist_for_each_entry_rcu(tb, head, tb_hlist) {
1070 			if (e < s_e)
1071 				goto next;
1072 			if (dumped)
1073 				memset(&cb->args[2], 0, sizeof(cb->args) -
1074 						 2 * sizeof(cb->args[0]));
1075 			err = fib_table_dump(tb, skb, cb, &filter);
1076 			if (err < 0)
1077 				goto out;
1078 			dumped = 1;
1079 next:
1080 			e++;
1081 		}
1082 	}
1083 out:
1084 
1085 	cb->args[1] = e;
1086 	cb->args[0] = h;
1087 
1088 unlock:
1089 	rcu_read_unlock();
1090 	return err;
1091 }
1092 
1093 /* Prepare and feed intra-kernel routing request.
1094  * Really, it should be netlink message, but :-( netlink
1095  * can be not configured, so that we feed it directly
1096  * to fib engine. It is legal, because all events occur
1097  * only when netlink is already locked.
1098  */
1099 static void fib_magic(int cmd, int type, __be32 dst, int dst_len,
1100 		      struct in_ifaddr *ifa, u32 rt_priority)
1101 {
1102 	struct net *net = dev_net(ifa->ifa_dev->dev);
1103 	u32 tb_id = l3mdev_fib_table(ifa->ifa_dev->dev);
1104 	struct fib_table *tb;
1105 	struct fib_config cfg = {
1106 		.fc_protocol = RTPROT_KERNEL,
1107 		.fc_type = type,
1108 		.fc_dst = dst,
1109 		.fc_dst_len = dst_len,
1110 		.fc_priority = rt_priority,
1111 		.fc_prefsrc = ifa->ifa_local,
1112 		.fc_oif = ifa->ifa_dev->dev->ifindex,
1113 		.fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
1114 		.fc_nlinfo = {
1115 			.nl_net = net,
1116 		},
1117 	};
1118 
1119 	if (!tb_id)
1120 		tb_id = (type == RTN_UNICAST) ? RT_TABLE_MAIN : RT_TABLE_LOCAL;
1121 
1122 	tb = fib_new_table(net, tb_id);
1123 	if (!tb)
1124 		return;
1125 
1126 	cfg.fc_table = tb->tb_id;
1127 
1128 	if (type != RTN_LOCAL)
1129 		cfg.fc_scope = RT_SCOPE_LINK;
1130 	else
1131 		cfg.fc_scope = RT_SCOPE_HOST;
1132 
1133 	if (cmd == RTM_NEWROUTE)
1134 		fib_table_insert(net, tb, &cfg, NULL);
1135 	else
1136 		fib_table_delete(net, tb, &cfg, NULL);
1137 }
1138 
1139 void fib_add_ifaddr(struct in_ifaddr *ifa)
1140 {
1141 	struct in_device *in_dev = ifa->ifa_dev;
1142 	struct net_device *dev = in_dev->dev;
1143 	struct in_ifaddr *prim = ifa;
1144 	__be32 mask = ifa->ifa_mask;
1145 	__be32 addr = ifa->ifa_local;
1146 	__be32 prefix = ifa->ifa_address & mask;
1147 
1148 	if (ifa->ifa_flags & IFA_F_SECONDARY) {
1149 		prim = inet_ifa_byprefix(in_dev, prefix, mask);
1150 		if (!prim) {
1151 			pr_warn("%s: bug: prim == NULL\n", __func__);
1152 			return;
1153 		}
1154 	}
1155 
1156 	fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim, 0);
1157 
1158 	if (!(dev->flags & IFF_UP))
1159 		return;
1160 
1161 	/* Add broadcast address, if it is explicitly assigned. */
1162 	if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF)) {
1163 		fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32,
1164 			  prim, 0);
1165 		arp_invalidate(dev, ifa->ifa_broadcast, false);
1166 	}
1167 
1168 	if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) &&
1169 	    (prefix != addr || ifa->ifa_prefixlen < 32)) {
1170 		if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
1171 			fib_magic(RTM_NEWROUTE,
1172 				  dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
1173 				  prefix, ifa->ifa_prefixlen, prim,
1174 				  ifa->ifa_rt_priority);
1175 
1176 		/* Add the network broadcast address, when it makes sense */
1177 		if (ifa->ifa_prefixlen < 31) {
1178 			fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask,
1179 				  32, prim, 0);
1180 			arp_invalidate(dev, prefix | ~mask, false);
1181 		}
1182 	}
1183 }
1184 
1185 void fib_modify_prefix_metric(struct in_ifaddr *ifa, u32 new_metric)
1186 {
1187 	__be32 prefix = ifa->ifa_address & ifa->ifa_mask;
1188 	struct in_device *in_dev = ifa->ifa_dev;
1189 	struct net_device *dev = in_dev->dev;
1190 
1191 	if (!(dev->flags & IFF_UP) ||
1192 	    ifa->ifa_flags & (IFA_F_SECONDARY | IFA_F_NOPREFIXROUTE) ||
1193 	    ipv4_is_zeronet(prefix) ||
1194 	    (prefix == ifa->ifa_local && ifa->ifa_prefixlen == 32))
1195 		return;
1196 
1197 	/* add the new */
1198 	fib_magic(RTM_NEWROUTE,
1199 		  dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
1200 		  prefix, ifa->ifa_prefixlen, ifa, new_metric);
1201 
1202 	/* delete the old */
1203 	fib_magic(RTM_DELROUTE,
1204 		  dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
1205 		  prefix, ifa->ifa_prefixlen, ifa, ifa->ifa_rt_priority);
1206 }
1207 
1208 /* Delete primary or secondary address.
1209  * Optionally, on secondary address promotion consider the addresses
1210  * from subnet iprim as deleted, even if they are in device list.
1211  * In this case the secondary ifa can be in device list.
1212  */
1213 void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim)
1214 {
1215 	struct in_device *in_dev = ifa->ifa_dev;
1216 	struct net_device *dev = in_dev->dev;
1217 	struct in_ifaddr *ifa1;
1218 	struct in_ifaddr *prim = ifa, *prim1 = NULL;
1219 	__be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
1220 	__be32 any = ifa->ifa_address & ifa->ifa_mask;
1221 #define LOCAL_OK	1
1222 #define BRD_OK		2
1223 #define BRD0_OK		4
1224 #define BRD1_OK		8
1225 	unsigned int ok = 0;
1226 	int subnet = 0;		/* Primary network */
1227 	int gone = 1;		/* Address is missing */
1228 	int same_prefsrc = 0;	/* Another primary with same IP */
1229 
1230 	if (ifa->ifa_flags & IFA_F_SECONDARY) {
1231 		prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
1232 		if (!prim) {
1233 			/* if the device has been deleted, we don't perform
1234 			 * address promotion
1235 			 */
1236 			if (!in_dev->dead)
1237 				pr_warn("%s: bug: prim == NULL\n", __func__);
1238 			return;
1239 		}
1240 		if (iprim && iprim != prim) {
1241 			pr_warn("%s: bug: iprim != prim\n", __func__);
1242 			return;
1243 		}
1244 	} else if (!ipv4_is_zeronet(any) &&
1245 		   (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) {
1246 		if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
1247 			fib_magic(RTM_DELROUTE,
1248 				  dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
1249 				  any, ifa->ifa_prefixlen, prim, 0);
1250 		subnet = 1;
1251 	}
1252 
1253 	if (in_dev->dead)
1254 		goto no_promotions;
1255 
1256 	/* Deletion is more complicated than add.
1257 	 * We should take care of not to delete too much :-)
1258 	 *
1259 	 * Scan address list to be sure that addresses are really gone.
1260 	 */
1261 	rcu_read_lock();
1262 	in_dev_for_each_ifa_rcu(ifa1, in_dev) {
1263 		if (ifa1 == ifa) {
1264 			/* promotion, keep the IP */
1265 			gone = 0;
1266 			continue;
1267 		}
1268 		/* Ignore IFAs from our subnet */
1269 		if (iprim && ifa1->ifa_mask == iprim->ifa_mask &&
1270 		    inet_ifa_match(ifa1->ifa_address, iprim))
1271 			continue;
1272 
1273 		/* Ignore ifa1 if it uses different primary IP (prefsrc) */
1274 		if (ifa1->ifa_flags & IFA_F_SECONDARY) {
1275 			/* Another address from our subnet? */
1276 			if (ifa1->ifa_mask == prim->ifa_mask &&
1277 			    inet_ifa_match(ifa1->ifa_address, prim))
1278 				prim1 = prim;
1279 			else {
1280 				/* We reached the secondaries, so
1281 				 * same_prefsrc should be determined.
1282 				 */
1283 				if (!same_prefsrc)
1284 					continue;
1285 				/* Search new prim1 if ifa1 is not
1286 				 * using the current prim1
1287 				 */
1288 				if (!prim1 ||
1289 				    ifa1->ifa_mask != prim1->ifa_mask ||
1290 				    !inet_ifa_match(ifa1->ifa_address, prim1))
1291 					prim1 = inet_ifa_byprefix(in_dev,
1292 							ifa1->ifa_address,
1293 							ifa1->ifa_mask);
1294 				if (!prim1)
1295 					continue;
1296 				if (prim1->ifa_local != prim->ifa_local)
1297 					continue;
1298 			}
1299 		} else {
1300 			if (prim->ifa_local != ifa1->ifa_local)
1301 				continue;
1302 			prim1 = ifa1;
1303 			if (prim != prim1)
1304 				same_prefsrc = 1;
1305 		}
1306 		if (ifa->ifa_local == ifa1->ifa_local)
1307 			ok |= LOCAL_OK;
1308 		if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
1309 			ok |= BRD_OK;
1310 		if (brd == ifa1->ifa_broadcast)
1311 			ok |= BRD1_OK;
1312 		if (any == ifa1->ifa_broadcast)
1313 			ok |= BRD0_OK;
1314 		/* primary has network specific broadcasts */
1315 		if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) {
1316 			__be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask;
1317 			__be32 any1 = ifa1->ifa_address & ifa1->ifa_mask;
1318 
1319 			if (!ipv4_is_zeronet(any1)) {
1320 				if (ifa->ifa_broadcast == brd1 ||
1321 				    ifa->ifa_broadcast == any1)
1322 					ok |= BRD_OK;
1323 				if (brd == brd1 || brd == any1)
1324 					ok |= BRD1_OK;
1325 				if (any == brd1 || any == any1)
1326 					ok |= BRD0_OK;
1327 			}
1328 		}
1329 	}
1330 	rcu_read_unlock();
1331 
1332 no_promotions:
1333 	if (!(ok & BRD_OK))
1334 		fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32,
1335 			  prim, 0);
1336 	if (subnet && ifa->ifa_prefixlen < 31) {
1337 		if (!(ok & BRD1_OK))
1338 			fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32,
1339 				  prim, 0);
1340 		if (!(ok & BRD0_OK))
1341 			fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32,
1342 				  prim, 0);
1343 	}
1344 	if (!(ok & LOCAL_OK)) {
1345 		unsigned int addr_type;
1346 
1347 		fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim, 0);
1348 
1349 		/* Check, that this local address finally disappeared. */
1350 		addr_type = inet_addr_type_dev_table(dev_net(dev), dev,
1351 						     ifa->ifa_local);
1352 		if (gone && addr_type != RTN_LOCAL) {
1353 			/* And the last, but not the least thing.
1354 			 * We must flush stray FIB entries.
1355 			 *
1356 			 * First of all, we scan fib_info list searching
1357 			 * for stray nexthop entries, then ignite fib_flush.
1358 			 */
1359 			if (fib_sync_down_addr(dev, ifa->ifa_local))
1360 				fib_flush(dev_net(dev));
1361 		}
1362 	}
1363 #undef LOCAL_OK
1364 #undef BRD_OK
1365 #undef BRD0_OK
1366 #undef BRD1_OK
1367 }
1368 
1369 static void nl_fib_lookup(struct net *net, struct fib_result_nl *frn)
1370 {
1371 
1372 	struct fib_result       res;
1373 	struct flowi4           fl4 = {
1374 		.flowi4_mark = frn->fl_mark,
1375 		.daddr = frn->fl_addr,
1376 		.flowi4_dscp = inet_dsfield_to_dscp(frn->fl_tos),
1377 		.flowi4_scope = frn->fl_scope,
1378 	};
1379 	struct fib_table *tb;
1380 
1381 	rcu_read_lock();
1382 
1383 	tb = fib_get_table(net, frn->tb_id_in);
1384 
1385 	frn->err = -ENOENT;
1386 	if (tb) {
1387 		local_bh_disable();
1388 
1389 		frn->tb_id = tb->tb_id;
1390 		frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
1391 
1392 		if (!frn->err) {
1393 			frn->prefixlen = res.prefixlen;
1394 			frn->nh_sel = res.nh_sel;
1395 			frn->type = res.type;
1396 			frn->scope = res.scope;
1397 		}
1398 		local_bh_enable();
1399 	}
1400 
1401 	rcu_read_unlock();
1402 }
1403 
1404 static void nl_fib_input(struct sk_buff *skb)
1405 {
1406 	struct net *net;
1407 	struct fib_result_nl *frn;
1408 	struct nlmsghdr *nlh;
1409 	u32 portid;
1410 
1411 	net = sock_net(skb->sk);
1412 	nlh = nlmsg_hdr(skb);
1413 	if (skb->len < nlmsg_total_size(sizeof(*frn)) ||
1414 	    skb->len < nlh->nlmsg_len ||
1415 	    nlmsg_len(nlh) < sizeof(*frn))
1416 		return;
1417 
1418 	skb = netlink_skb_clone(skb, GFP_KERNEL);
1419 	if (!skb)
1420 		return;
1421 	nlh = nlmsg_hdr(skb);
1422 
1423 	frn = nlmsg_data(nlh);
1424 	nl_fib_lookup(net, frn);
1425 
1426 	portid = NETLINK_CB(skb).portid;      /* netlink portid */
1427 	NETLINK_CB(skb).portid = 0;        /* from kernel */
1428 	NETLINK_CB(skb).dst_group = 0;  /* unicast */
1429 	nlmsg_unicast(net->ipv4.fibnl, skb, portid);
1430 }
1431 
1432 static int __net_init nl_fib_lookup_init(struct net *net)
1433 {
1434 	struct sock *sk;
1435 	struct netlink_kernel_cfg cfg = {
1436 		.input	= nl_fib_input,
1437 	};
1438 
1439 	sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg);
1440 	if (!sk)
1441 		return -EAFNOSUPPORT;
1442 	net->ipv4.fibnl = sk;
1443 	return 0;
1444 }
1445 
1446 static void nl_fib_lookup_exit(struct net *net)
1447 {
1448 	netlink_kernel_release(net->ipv4.fibnl);
1449 	net->ipv4.fibnl = NULL;
1450 }
1451 
1452 static void fib_disable_ip(struct net_device *dev, unsigned long event,
1453 			   bool force)
1454 {
1455 	if (fib_sync_down_dev(dev, event, force))
1456 		fib_flush(dev_net(dev));
1457 	else
1458 		rt_cache_flush(dev_net(dev));
1459 	arp_ifdown(dev);
1460 }
1461 
1462 static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
1463 {
1464 	struct in_ifaddr *ifa = ptr;
1465 	struct net_device *dev = ifa->ifa_dev->dev;
1466 	struct net *net = dev_net(dev);
1467 
1468 	switch (event) {
1469 	case NETDEV_UP:
1470 		fib_add_ifaddr(ifa);
1471 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1472 		fib_sync_up(dev, RTNH_F_DEAD);
1473 #endif
1474 		atomic_inc(&net->ipv4.dev_addr_genid);
1475 		rt_cache_flush(net);
1476 		break;
1477 	case NETDEV_DOWN:
1478 		fib_del_ifaddr(ifa, NULL);
1479 		atomic_inc(&net->ipv4.dev_addr_genid);
1480 		if (!ifa->ifa_dev->ifa_list) {
1481 			/* Last address was deleted from this interface.
1482 			 * Disable IP.
1483 			 */
1484 			fib_disable_ip(dev, event, true);
1485 		} else {
1486 			rt_cache_flush(net);
1487 		}
1488 		break;
1489 	}
1490 	return NOTIFY_DONE;
1491 }
1492 
1493 static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
1494 {
1495 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1496 	struct netdev_notifier_changeupper_info *upper_info = ptr;
1497 	struct netdev_notifier_info_ext *info_ext = ptr;
1498 	struct in_device *in_dev;
1499 	struct net *net = dev_net(dev);
1500 	struct in_ifaddr *ifa;
1501 	unsigned int flags;
1502 
1503 	if (event == NETDEV_UNREGISTER) {
1504 		fib_disable_ip(dev, event, true);
1505 		rt_flush_dev(dev);
1506 		return NOTIFY_DONE;
1507 	}
1508 
1509 	in_dev = __in_dev_get_rtnl(dev);
1510 	if (!in_dev)
1511 		return NOTIFY_DONE;
1512 
1513 	switch (event) {
1514 	case NETDEV_UP:
1515 		in_dev_for_each_ifa_rtnl(ifa, in_dev) {
1516 			fib_add_ifaddr(ifa);
1517 		}
1518 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1519 		fib_sync_up(dev, RTNH_F_DEAD);
1520 #endif
1521 		atomic_inc(&net->ipv4.dev_addr_genid);
1522 		rt_cache_flush(net);
1523 		break;
1524 	case NETDEV_DOWN:
1525 		fib_disable_ip(dev, event, false);
1526 		break;
1527 	case NETDEV_CHANGE:
1528 		flags = netif_get_flags(dev);
1529 		if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1530 			fib_sync_up(dev, RTNH_F_LINKDOWN);
1531 		else
1532 			fib_sync_down_dev(dev, event, false);
1533 		rt_cache_flush(net);
1534 		break;
1535 	case NETDEV_CHANGEMTU:
1536 		fib_sync_mtu(dev, info_ext->ext.mtu);
1537 		rt_cache_flush(net);
1538 		break;
1539 	case NETDEV_CHANGEUPPER:
1540 		upper_info = ptr;
1541 		/* flush all routes if dev is linked to or unlinked from
1542 		 * an L3 master device (e.g., VRF)
1543 		 */
1544 		if (upper_info->upper_dev &&
1545 		    netif_is_l3_master(upper_info->upper_dev))
1546 			fib_disable_ip(dev, NETDEV_DOWN, true);
1547 		break;
1548 	}
1549 	return NOTIFY_DONE;
1550 }
1551 
1552 static struct notifier_block fib_inetaddr_notifier = {
1553 	.notifier_call = fib_inetaddr_event,
1554 };
1555 
1556 static struct notifier_block fib_netdev_notifier = {
1557 	.notifier_call = fib_netdev_event,
1558 };
1559 
1560 static int __net_init ip_fib_net_init(struct net *net)
1561 {
1562 	int err;
1563 	size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ;
1564 
1565 	err = fib4_notifier_init(net);
1566 	if (err)
1567 		return err;
1568 
1569 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1570 	/* Default to 3-tuple */
1571 	net->ipv4.sysctl_fib_multipath_hash_fields =
1572 		FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK;
1573 #endif
1574 
1575 	/* Avoid false sharing : Use at least a full cache line */
1576 	size = max_t(size_t, size, L1_CACHE_BYTES);
1577 
1578 	net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
1579 	if (!net->ipv4.fib_table_hash) {
1580 		err = -ENOMEM;
1581 		goto err_table_hash_alloc;
1582 	}
1583 
1584 	err = fib4_rules_init(net);
1585 	if (err < 0)
1586 		goto err_rules_init;
1587 	return 0;
1588 
1589 err_rules_init:
1590 	kfree(net->ipv4.fib_table_hash);
1591 err_table_hash_alloc:
1592 	fib4_notifier_exit(net);
1593 	return err;
1594 }
1595 
1596 static void ip_fib_net_exit(struct net *net)
1597 {
1598 	int i;
1599 
1600 	ASSERT_RTNL_NET(net);
1601 #ifdef CONFIG_IP_MULTIPLE_TABLES
1602 	RCU_INIT_POINTER(net->ipv4.fib_main, NULL);
1603 	RCU_INIT_POINTER(net->ipv4.fib_default, NULL);
1604 #endif
1605 	/* Destroy the tables in reverse order to guarantee that the
1606 	 * local table, ID 255, is destroyed before the main table, ID
1607 	 * 254. This is necessary as the local table may contain
1608 	 * references to data contained in the main table.
1609 	 */
1610 	for (i = FIB_TABLE_HASHSZ - 1; i >= 0; i--) {
1611 		struct hlist_head *head = &net->ipv4.fib_table_hash[i];
1612 		struct hlist_node *tmp;
1613 		struct fib_table *tb;
1614 
1615 		hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) {
1616 			hlist_del(&tb->tb_hlist);
1617 			fib_table_flush(net, tb, true);
1618 			fib_free_table(tb);
1619 		}
1620 	}
1621 
1622 #ifdef CONFIG_IP_MULTIPLE_TABLES
1623 	fib4_rules_exit(net);
1624 #endif
1625 
1626 	kfree(net->ipv4.fib_table_hash);
1627 	fib4_notifier_exit(net);
1628 }
1629 
1630 static int __net_init fib_net_init(struct net *net)
1631 {
1632 	int error;
1633 
1634 #ifdef CONFIG_IP_ROUTE_CLASSID
1635 	atomic_set(&net->ipv4.fib_num_tclassid_users, 0);
1636 #endif
1637 	error = ip_fib_net_init(net);
1638 	if (error < 0)
1639 		goto out;
1640 
1641 	error = fib4_semantics_init(net);
1642 	if (error)
1643 		goto out_semantics;
1644 
1645 	error = nl_fib_lookup_init(net);
1646 	if (error < 0)
1647 		goto out_nlfl;
1648 
1649 	error = fib_proc_init(net);
1650 	if (error < 0)
1651 		goto out_proc;
1652 out:
1653 	return error;
1654 
1655 out_proc:
1656 	nl_fib_lookup_exit(net);
1657 out_nlfl:
1658 	fib4_semantics_exit(net);
1659 out_semantics:
1660 	rtnl_net_lock(net);
1661 	ip_fib_net_exit(net);
1662 	rtnl_net_unlock(net);
1663 	goto out;
1664 }
1665 
1666 static void __net_exit fib_net_exit(struct net *net)
1667 {
1668 	fib_proc_exit(net);
1669 	nl_fib_lookup_exit(net);
1670 }
1671 
1672 static void __net_exit fib_net_exit_batch(struct list_head *net_list)
1673 {
1674 	struct net *net;
1675 
1676 	rtnl_lock();
1677 	list_for_each_entry(net, net_list, exit_list) {
1678 		__rtnl_net_lock(net);
1679 		ip_fib_net_exit(net);
1680 		__rtnl_net_unlock(net);
1681 	}
1682 	rtnl_unlock();
1683 
1684 	list_for_each_entry(net, net_list, exit_list)
1685 		fib4_semantics_exit(net);
1686 }
1687 
1688 static struct pernet_operations fib_net_ops = {
1689 	.init = fib_net_init,
1690 	.exit = fib_net_exit,
1691 	.exit_batch = fib_net_exit_batch,
1692 };
1693 
1694 static const struct rtnl_msg_handler fib_rtnl_msg_handlers[] __initconst = {
1695 	{.protocol = PF_INET, .msgtype = RTM_NEWROUTE,
1696 	 .doit = inet_rtm_newroute, .flags = RTNL_FLAG_DOIT_PERNET},
1697 	{.protocol = PF_INET, .msgtype = RTM_DELROUTE,
1698 	 .doit = inet_rtm_delroute, .flags = RTNL_FLAG_DOIT_PERNET},
1699 	{.protocol = PF_INET, .msgtype = RTM_GETROUTE, .dumpit = inet_dump_fib,
1700 	 .flags = RTNL_FLAG_DUMP_UNLOCKED | RTNL_FLAG_DUMP_SPLIT_NLM_DONE},
1701 };
1702 
1703 void __init ip_fib_init(void)
1704 {
1705 	fib_trie_init();
1706 
1707 	register_pernet_subsys(&fib_net_ops);
1708 
1709 	register_netdevice_notifier(&fib_netdev_notifier);
1710 	register_inetaddr_notifier(&fib_inetaddr_notifier);
1711 
1712 	rtnl_register_many(fib_rtnl_msg_handlers);
1713 }
1714