xref: /linux/net/ipv4/devinet.c (revision 72ebc685bca99f466ef35600b5263d1c21c790d9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	NET3	IP device support routines.
4  *
5  *	Derived from the IP parts of dev.c 1.0.19
6  * 		Authors:	Ross Biro
7  *				Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
8  *				Mark Evans, <evansmp@uhura.aston.ac.uk>
9  *
10  *	Additional Authors:
11  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
12  *		Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
13  *
14  *	Changes:
15  *		Alexey Kuznetsov:	pa_* fields are replaced with ifaddr
16  *					lists.
17  *		Cyrus Durgin:		updated for kmod
18  *		Matthias Andree:	in devinet_ioctl, compare label and
19  *					address (4.4BSD alias style support),
20  *					fall back to comparing just the label
21  *					if no match found.
22  */
23 
24 
25 #include <linux/uaccess.h>
26 #include <linux/bitops.h>
27 #include <linux/capability.h>
28 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/kernel.h>
31 #include <linux/sched/signal.h>
32 #include <linux/string.h>
33 #include <linux/mm.h>
34 #include <linux/socket.h>
35 #include <linux/sockios.h>
36 #include <linux/in.h>
37 #include <linux/errno.h>
38 #include <linux/interrupt.h>
39 #include <linux/if_addr.h>
40 #include <linux/if_ether.h>
41 #include <linux/inet.h>
42 #include <linux/netdevice.h>
43 #include <linux/etherdevice.h>
44 #include <linux/skbuff.h>
45 #include <linux/init.h>
46 #include <linux/notifier.h>
47 #include <linux/inetdevice.h>
48 #include <linux/igmp.h>
49 #include "igmp_internal.h"
50 #include <linux/slab.h>
51 #include <linux/hash.h>
52 #ifdef CONFIG_SYSCTL
53 #include <linux/sysctl.h>
54 #endif
55 #include <linux/kmod.h>
56 #include <linux/netconf.h>
57 
58 #include <net/arp.h>
59 #include <net/ip.h>
60 #include <net/route.h>
61 #include <net/ip_fib.h>
62 #include <net/rtnetlink.h>
63 #include <net/net_namespace.h>
64 #include <net/addrconf.h>
65 
66 #define IPV6ONLY_FLAGS	\
67 		(IFA_F_NODAD | IFA_F_OPTIMISTIC | IFA_F_DADFAILED | \
68 		 IFA_F_HOMEADDRESS | IFA_F_TENTATIVE | \
69 		 IFA_F_MANAGETEMPADDR | IFA_F_STABLE_PRIVACY)
70 
71 static struct ipv4_devconf ipv4_devconf = {
72 	.data = {
73 		[IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
74 		[IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
75 		[IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
76 		[IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
77 		[IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
78 		[IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] =  1000 /*ms*/,
79 		[IPV4_DEVCONF_ARP_EVICT_NOCARRIER - 1] = 1,
80 	},
81 };
82 
83 static struct ipv4_devconf ipv4_devconf_dflt = {
84 	.data = {
85 		[IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
86 		[IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
87 		[IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
88 		[IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
89 		[IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
90 		[IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
91 		[IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] =  1000 /*ms*/,
92 		[IPV4_DEVCONF_ARP_EVICT_NOCARRIER - 1] = 1,
93 	},
94 };
95 
96 #define IPV4_DEVCONF_DFLT(net, attr) \
97 	IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
98 
99 static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
100 	[IFA_LOCAL]     	= { .type = NLA_U32 },
101 	[IFA_ADDRESS]   	= { .type = NLA_U32 },
102 	[IFA_BROADCAST] 	= { .type = NLA_U32 },
103 	[IFA_LABEL]     	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
104 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
105 	[IFA_FLAGS]		= { .type = NLA_U32 },
106 	[IFA_RT_PRIORITY]	= { .type = NLA_U32 },
107 	[IFA_TARGET_NETNSID]	= { .type = NLA_S32 },
108 	[IFA_PROTO]		= { .type = NLA_U8 },
109 };
110 
111 #define IN4_ADDR_HSIZE_SHIFT	8
112 #define IN4_ADDR_HSIZE		(1U << IN4_ADDR_HSIZE_SHIFT)
113 
114 static u32 inet_addr_hash(const struct net *net, __be32 addr)
115 {
116 	u32 val = __ipv4_addr_hash(addr, net_hash_mix(net));
117 
118 	return hash_32(val, IN4_ADDR_HSIZE_SHIFT);
119 }
120 
121 static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa)
122 {
123 	u32 hash = inet_addr_hash(net, ifa->ifa_local);
124 
125 	ASSERT_RTNL();
126 	hlist_add_head_rcu(&ifa->addr_lst, &net->ipv4.inet_addr_lst[hash]);
127 }
128 
129 static void inet_hash_remove(struct in_ifaddr *ifa)
130 {
131 	ASSERT_RTNL();
132 	hlist_del_init_rcu(&ifa->addr_lst);
133 }
134 
135 /**
136  * __ip_dev_find - find the first device with a given source address.
137  * @net: the net namespace
138  * @addr: the source address
139  * @devref: if true, take a reference on the found device
140  *
141  * If a caller uses devref=false, it should be protected by RCU, or RTNL
142  */
143 struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
144 {
145 	struct net_device *result = NULL;
146 	struct in_ifaddr *ifa;
147 
148 	rcu_read_lock();
149 	ifa = inet_lookup_ifaddr_rcu(net, addr);
150 	if (!ifa) {
151 		struct flowi4 fl4 = { .daddr = addr };
152 		struct fib_result res = { 0 };
153 		struct fib_table *local;
154 
155 		/* Fallback to FIB local table so that communication
156 		 * over loopback subnets work.
157 		 */
158 		local = fib_get_table(net, RT_TABLE_LOCAL);
159 		if (local &&
160 		    !fib_table_lookup(net, local, &fl4, &res,
161 				      FIB_LOOKUP_NOREF) &&
162 		    res.type == RTN_LOCAL)
163 			result = FIB_RES_DEV(res);
164 	} else {
165 		result = ifa->ifa_dev->dev;
166 	}
167 	if (result && devref)
168 		dev_hold(result);
169 	rcu_read_unlock();
170 	return result;
171 }
172 EXPORT_SYMBOL(__ip_dev_find);
173 
174 /* called under RCU lock */
175 struct in_ifaddr *inet_lookup_ifaddr_rcu(struct net *net, __be32 addr)
176 {
177 	u32 hash = inet_addr_hash(net, addr);
178 	struct in_ifaddr *ifa;
179 
180 	hlist_for_each_entry_rcu(ifa, &net->ipv4.inet_addr_lst[hash], addr_lst)
181 		if (ifa->ifa_local == addr)
182 			return ifa;
183 
184 	return NULL;
185 }
186 
187 static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
188 
189 static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
190 static BLOCKING_NOTIFIER_HEAD(inetaddr_validator_chain);
191 static void inet_del_ifa(struct in_device *in_dev,
192 			 struct in_ifaddr __rcu **ifap,
193 			 int destroy);
194 #ifdef CONFIG_SYSCTL
195 static int devinet_sysctl_register(struct in_device *idev);
196 static void devinet_sysctl_unregister(struct in_device *idev);
197 #else
198 static int devinet_sysctl_register(struct in_device *idev)
199 {
200 	return 0;
201 }
202 static void devinet_sysctl_unregister(struct in_device *idev)
203 {
204 }
205 #endif
206 
207 /* Locks all the inet devices. */
208 
209 static struct in_ifaddr *inet_alloc_ifa(struct in_device *in_dev)
210 {
211 	struct in_ifaddr *ifa;
212 
213 	ifa = kzalloc_obj(*ifa, GFP_KERNEL_ACCOUNT);
214 	if (!ifa)
215 		return NULL;
216 
217 	in_dev_hold(in_dev);
218 	ifa->ifa_dev = in_dev;
219 
220 	INIT_HLIST_NODE(&ifa->addr_lst);
221 
222 	return ifa;
223 }
224 
225 static void inet_rcu_free_ifa(struct rcu_head *head)
226 {
227 	struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
228 
229 	in_dev_put(ifa->ifa_dev);
230 	kfree(ifa);
231 }
232 
233 static void inet_free_ifa(struct in_ifaddr *ifa)
234 {
235 	/* Our reference to ifa->ifa_dev must be freed ASAP
236 	 * to release the reference to the netdev the same way.
237 	 * in_dev_put() -> in_dev_finish_destroy() -> netdev_put()
238 	 */
239 	call_rcu_hurry(&ifa->rcu_head, inet_rcu_free_ifa);
240 }
241 
242 static void in_dev_free_rcu(struct rcu_head *head)
243 {
244 	struct in_device *idev = container_of(head, struct in_device, rcu_head);
245 
246 	kfree(rcu_dereference_protected(idev->mc_hash, 1));
247 	kfree(idev);
248 }
249 
250 void in_dev_finish_destroy(struct in_device *idev)
251 {
252 	struct net_device *dev = idev->dev;
253 
254 	WARN_ON(idev->ifa_list);
255 	WARN_ON(idev->mc_list);
256 #ifdef NET_REFCNT_DEBUG
257 	pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL");
258 #endif
259 	netdev_put(dev, &idev->dev_tracker);
260 	if (!idev->dead)
261 		pr_err("Freeing alive in_device %p\n", idev);
262 	else
263 		call_rcu(&idev->rcu_head, in_dev_free_rcu);
264 }
265 EXPORT_SYMBOL(in_dev_finish_destroy);
266 
267 static struct in_device *inetdev_init(struct net_device *dev)
268 {
269 	struct net *net = dev_net(dev);
270 	struct in_device *in_dev;
271 	struct neigh_table *tbl;
272 	int err = -ENOMEM;
273 
274 	ASSERT_RTNL();
275 
276 	in_dev = kzalloc_obj(*in_dev);
277 	if (!in_dev)
278 		goto out;
279 
280 	tbl = arp_table(net);
281 	memcpy(&in_dev->cnf, net->ipv4.devconf_dflt, sizeof(in_dev->cnf));
282 	in_dev->cnf.sysctl = NULL;
283 	in_dev->dev = dev;
284 	in_dev->arp_parms = neigh_parms_alloc(dev, tbl);
285 	if (!in_dev->arp_parms)
286 		goto out_kfree;
287 	if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
288 		netif_disable_lro(dev);
289 	/* Reference in_dev->dev */
290 	netdev_hold(dev, &in_dev->dev_tracker, GFP_KERNEL);
291 	/* Account for reference dev->ip_ptr (below) */
292 	refcount_set(&in_dev->refcnt, 1);
293 
294 	if (dev != blackhole_netdev) {
295 		err = devinet_sysctl_register(in_dev);
296 		if (err) {
297 			in_dev->dead = 1;
298 			neigh_parms_release(tbl, in_dev->arp_parms);
299 			in_dev_put(in_dev);
300 			in_dev = NULL;
301 			goto out;
302 		}
303 		ip_mc_init_dev(in_dev);
304 		if (dev->flags & IFF_UP)
305 			ip_mc_up(in_dev);
306 	}
307 
308 	/* we can receive as soon as ip_ptr is set -- do this last */
309 	rcu_assign_pointer(dev->ip_ptr, in_dev);
310 out:
311 	return in_dev ?: ERR_PTR(err);
312 out_kfree:
313 	kfree(in_dev);
314 	in_dev = NULL;
315 	goto out;
316 }
317 
318 static void inetdev_destroy(struct in_device *in_dev)
319 {
320 	struct net_device *dev = in_dev->dev;
321 	struct net *net = dev_net(dev);
322 	struct in_ifaddr *ifa;
323 
324 	ASSERT_RTNL();
325 
326 	in_dev->dead = 1;
327 
328 	RCU_INIT_POINTER(dev->ip_ptr, NULL);
329 
330 	ip_mc_destroy_dev(in_dev);
331 
332 	while ((ifa = rtnl_dereference(in_dev->ifa_list)) != NULL) {
333 		inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
334 		inet_free_ifa(ifa);
335 	}
336 
337 	devinet_sysctl_unregister(in_dev);
338 	neigh_parms_release(arp_table(net), in_dev->arp_parms);
339 	arp_ifdown(dev);
340 
341 	in_dev_put(in_dev);
342 }
343 
344 int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
345 {
346 	const struct in_ifaddr *ifa;
347 
348 	rcu_read_lock();
349 	in_dev_for_each_ifa_rcu(ifa, in_dev) {
350 		if (inet_ifa_match(a, ifa)) {
351 			if (!b || inet_ifa_match(b, ifa)) {
352 				rcu_read_unlock();
353 				return 1;
354 			}
355 		}
356 	}
357 	rcu_read_unlock();
358 	return 0;
359 }
360 
361 static void __inet_del_ifa(struct in_device *in_dev,
362 			   struct in_ifaddr __rcu **ifap,
363 			   int destroy, struct nlmsghdr *nlh, u32 portid)
364 {
365 	struct in_ifaddr *promote = NULL;
366 	struct in_ifaddr *ifa, *ifa1;
367 	struct in_ifaddr __rcu **last_prim;
368 	struct in_ifaddr *prev_prom = NULL;
369 	int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
370 
371 	ASSERT_RTNL();
372 
373 	ifa1 = rtnl_dereference(*ifap);
374 	last_prim = ifap;
375 	if (in_dev->dead)
376 		goto no_promotions;
377 
378 	/* 1. Deleting primary ifaddr forces deletion all secondaries
379 	 * unless alias promotion is set
380 	 **/
381 
382 	if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
383 		struct in_ifaddr __rcu **ifap1 = &ifa1->ifa_next;
384 
385 		while ((ifa = rtnl_dereference(*ifap1)) != NULL) {
386 			if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
387 			    ifa1->ifa_scope <= ifa->ifa_scope)
388 				last_prim = &ifa->ifa_next;
389 
390 			if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
391 			    ifa1->ifa_mask != ifa->ifa_mask ||
392 			    !inet_ifa_match(ifa1->ifa_address, ifa)) {
393 				ifap1 = &ifa->ifa_next;
394 				prev_prom = ifa;
395 				continue;
396 			}
397 
398 			if (!do_promote) {
399 				inet_hash_remove(ifa);
400 				*ifap1 = ifa->ifa_next;
401 
402 				rtmsg_ifa(RTM_DELADDR, ifa, nlh, portid);
403 				blocking_notifier_call_chain(&inetaddr_chain,
404 						NETDEV_DOWN, ifa);
405 				inet_free_ifa(ifa);
406 			} else {
407 				promote = ifa;
408 				break;
409 			}
410 		}
411 	}
412 
413 	/* On promotion all secondaries from subnet are changing
414 	 * the primary IP, we must remove all their routes silently
415 	 * and later to add them back with new prefsrc. Do this
416 	 * while all addresses are on the device list.
417 	 */
418 	for (ifa = promote; ifa; ifa = rtnl_dereference(ifa->ifa_next)) {
419 		if (ifa1->ifa_mask == ifa->ifa_mask &&
420 		    inet_ifa_match(ifa1->ifa_address, ifa))
421 			fib_del_ifaddr(ifa, ifa1);
422 	}
423 
424 no_promotions:
425 	/* 2. Unlink it */
426 
427 	*ifap = ifa1->ifa_next;
428 	inet_hash_remove(ifa1);
429 
430 	/* 3. Announce address deletion */
431 
432 	/* Send message first, then call notifier.
433 	   At first sight, FIB update triggered by notifier
434 	   will refer to already deleted ifaddr, that could confuse
435 	   netlink listeners. It is not true: look, gated sees
436 	   that route deleted and if it still thinks that ifaddr
437 	   is valid, it will try to restore deleted routes... Grr.
438 	   So that, this order is correct.
439 	 */
440 	rtmsg_ifa(RTM_DELADDR, ifa1, nlh, portid);
441 	blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
442 
443 	if (promote) {
444 		struct in_ifaddr *next_sec;
445 
446 		next_sec = rtnl_dereference(promote->ifa_next);
447 		if (prev_prom) {
448 			struct in_ifaddr *last_sec;
449 
450 			rcu_assign_pointer(prev_prom->ifa_next, next_sec);
451 
452 			last_sec = rtnl_dereference(*last_prim);
453 			rcu_assign_pointer(promote->ifa_next, last_sec);
454 			rcu_assign_pointer(*last_prim, promote);
455 		}
456 
457 		promote->ifa_flags &= ~IFA_F_SECONDARY;
458 		rtmsg_ifa(RTM_NEWADDR, promote, nlh, portid);
459 		blocking_notifier_call_chain(&inetaddr_chain,
460 				NETDEV_UP, promote);
461 		for (ifa = next_sec; ifa;
462 		     ifa = rtnl_dereference(ifa->ifa_next)) {
463 			if (ifa1->ifa_mask != ifa->ifa_mask ||
464 			    !inet_ifa_match(ifa1->ifa_address, ifa))
465 					continue;
466 			fib_add_ifaddr(ifa);
467 		}
468 
469 	}
470 	if (destroy)
471 		inet_free_ifa(ifa1);
472 }
473 
474 static void inet_del_ifa(struct in_device *in_dev,
475 			 struct in_ifaddr __rcu **ifap,
476 			 int destroy)
477 {
478 	__inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
479 }
480 
481 static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
482 			     u32 portid, struct netlink_ext_ack *extack)
483 {
484 	struct in_ifaddr __rcu **last_primary, **ifap;
485 	struct in_device *in_dev = ifa->ifa_dev;
486 	struct net *net = dev_net(in_dev->dev);
487 	struct in_validator_info ivi;
488 	struct in_ifaddr *ifa1;
489 	int ret;
490 
491 	ASSERT_RTNL();
492 
493 	ifa->ifa_flags &= ~IFA_F_SECONDARY;
494 	last_primary = &in_dev->ifa_list;
495 
496 	/* Don't set IPv6 only flags to IPv4 addresses */
497 	ifa->ifa_flags &= ~IPV6ONLY_FLAGS;
498 
499 	ifap = &in_dev->ifa_list;
500 	ifa1 = rtnl_dereference(*ifap);
501 
502 	while (ifa1) {
503 		if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
504 		    ifa->ifa_scope <= ifa1->ifa_scope)
505 			last_primary = &ifa1->ifa_next;
506 		if (ifa1->ifa_mask == ifa->ifa_mask &&
507 		    inet_ifa_match(ifa1->ifa_address, ifa)) {
508 			if (ifa1->ifa_local == ifa->ifa_local) {
509 				inet_free_ifa(ifa);
510 				return -EEXIST;
511 			}
512 			if (ifa1->ifa_scope != ifa->ifa_scope) {
513 				NL_SET_ERR_MSG(extack, "ipv4: Invalid scope value");
514 				inet_free_ifa(ifa);
515 				return -EINVAL;
516 			}
517 			ifa->ifa_flags |= IFA_F_SECONDARY;
518 		}
519 
520 		ifap = &ifa1->ifa_next;
521 		ifa1 = rtnl_dereference(*ifap);
522 	}
523 
524 	/* Allow any devices that wish to register ifaddr validtors to weigh
525 	 * in now, before changes are committed.  The rntl lock is serializing
526 	 * access here, so the state should not change between a validator call
527 	 * and a final notify on commit.  This isn't invoked on promotion under
528 	 * the assumption that validators are checking the address itself, and
529 	 * not the flags.
530 	 */
531 	ivi.ivi_addr = ifa->ifa_address;
532 	ivi.ivi_dev = ifa->ifa_dev;
533 	ivi.extack = extack;
534 	ret = blocking_notifier_call_chain(&inetaddr_validator_chain,
535 					   NETDEV_UP, &ivi);
536 	ret = notifier_to_errno(ret);
537 	if (ret) {
538 		inet_free_ifa(ifa);
539 		return ret;
540 	}
541 
542 	if (!(ifa->ifa_flags & IFA_F_SECONDARY))
543 		ifap = last_primary;
544 
545 	rcu_assign_pointer(ifa->ifa_next, *ifap);
546 	rcu_assign_pointer(*ifap, ifa);
547 
548 	inet_hash_insert(dev_net(in_dev->dev), ifa);
549 
550 	cancel_delayed_work(&net->ipv4.addr_chk_work);
551 	queue_delayed_work(system_power_efficient_wq, &net->ipv4.addr_chk_work, 0);
552 
553 	/* Send message first, then call notifier.
554 	   Notifier will trigger FIB update, so that
555 	   listeners of netlink will know about new ifaddr */
556 	rtmsg_ifa(RTM_NEWADDR, ifa, nlh, portid);
557 	blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
558 
559 	return 0;
560 }
561 
562 static int inet_insert_ifa(struct in_ifaddr *ifa)
563 {
564 	if (!ifa->ifa_local) {
565 		inet_free_ifa(ifa);
566 		return 0;
567 	}
568 
569 	return __inet_insert_ifa(ifa, NULL, 0, NULL);
570 }
571 
572 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
573 {
574 	struct in_device *in_dev = __in_dev_get_rtnl_net(dev);
575 
576 	ipv4_devconf_setall(in_dev);
577 	neigh_parms_data_state_setall(in_dev->arp_parms);
578 
579 	if (ipv4_is_loopback(ifa->ifa_local))
580 		ifa->ifa_scope = RT_SCOPE_HOST;
581 	return inet_insert_ifa(ifa);
582 }
583 
584 /* Caller must hold RCU or RTNL :
585  * We dont take a reference on found in_device
586  */
587 struct in_device *inetdev_by_index(struct net *net, int ifindex)
588 {
589 	struct net_device *dev;
590 	struct in_device *in_dev = NULL;
591 
592 	rcu_read_lock();
593 	dev = dev_get_by_index_rcu(net, ifindex);
594 	if (dev)
595 		in_dev = rcu_dereference_rtnl(dev->ip_ptr);
596 	rcu_read_unlock();
597 	return in_dev;
598 }
599 EXPORT_SYMBOL(inetdev_by_index);
600 
601 /* Called only from RTNL semaphored context. No locks. */
602 
603 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
604 				    __be32 mask)
605 {
606 	struct in_ifaddr *ifa;
607 
608 	ASSERT_RTNL();
609 
610 	in_dev_for_each_ifa_rtnl(ifa, in_dev) {
611 		if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
612 			return ifa;
613 	}
614 	return NULL;
615 }
616 
617 static int ip_mc_autojoin_config(struct net *net, bool join,
618 				 const struct in_ifaddr *ifa)
619 {
620 #if defined(CONFIG_IP_MULTICAST)
621 	struct ip_mreqn mreq = {
622 		.imr_multiaddr.s_addr = ifa->ifa_address,
623 		.imr_ifindex = ifa->ifa_dev->dev->ifindex,
624 	};
625 	struct sock *sk = net->ipv4.mc_autojoin_sk;
626 	int ret;
627 
628 	ASSERT_RTNL_NET(net);
629 
630 	lock_sock(sk);
631 	if (join)
632 		ret = ip_mc_join_group(sk, &mreq);
633 	else
634 		ret = ip_mc_leave_group(sk, &mreq);
635 	release_sock(sk);
636 
637 	return ret;
638 #else
639 	return -EOPNOTSUPP;
640 #endif
641 }
642 
643 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
644 			    struct netlink_ext_ack *extack)
645 {
646 	struct net *net = sock_net(skb->sk);
647 	struct in_ifaddr __rcu **ifap;
648 	struct nlattr *tb[IFA_MAX+1];
649 	struct in_device *in_dev;
650 	struct ifaddrmsg *ifm;
651 	struct in_ifaddr *ifa;
652 	int err;
653 
654 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
655 				     ifa_ipv4_policy, extack);
656 	if (err < 0)
657 		goto out;
658 
659 	ifm = nlmsg_data(nlh);
660 
661 	rtnl_net_lock(net);
662 
663 	in_dev = inetdev_by_index(net, ifm->ifa_index);
664 	if (!in_dev) {
665 		NL_SET_ERR_MSG(extack, "ipv4: Device not found");
666 		err = -ENODEV;
667 		goto unlock;
668 	}
669 
670 	for (ifap = &in_dev->ifa_list;
671 	     (ifa = rtnl_net_dereference(net, *ifap)) != NULL;
672 	     ifap = &ifa->ifa_next) {
673 		if (tb[IFA_LOCAL] &&
674 		    ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL]))
675 			continue;
676 
677 		if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
678 			continue;
679 
680 		if (tb[IFA_ADDRESS] &&
681 		    (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
682 		    !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa)))
683 			continue;
684 
685 		if (ipv4_is_multicast(ifa->ifa_address))
686 			ip_mc_autojoin_config(net, false, ifa);
687 
688 		__inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid);
689 		goto unlock;
690 	}
691 
692 	NL_SET_ERR_MSG(extack, "ipv4: Address not found");
693 	err = -EADDRNOTAVAIL;
694 unlock:
695 	rtnl_net_unlock(net);
696 out:
697 	return err;
698 }
699 
700 static void check_lifetime(struct work_struct *work)
701 {
702 	unsigned long now, next, next_sec, next_sched;
703 	struct in_ifaddr *ifa;
704 	struct hlist_node *n;
705 	struct net *net;
706 	int i;
707 
708 	net = container_of(to_delayed_work(work), struct net, ipv4.addr_chk_work);
709 	now = jiffies;
710 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
711 
712 	for (i = 0; i < IN4_ADDR_HSIZE; i++) {
713 		struct hlist_head *head = &net->ipv4.inet_addr_lst[i];
714 		bool change_needed = false;
715 
716 		rcu_read_lock();
717 		hlist_for_each_entry_rcu(ifa, head, addr_lst) {
718 			unsigned long age, tstamp;
719 			u32 preferred_lft;
720 			u32 valid_lft;
721 			u32 flags;
722 
723 			flags = READ_ONCE(ifa->ifa_flags);
724 			if (flags & IFA_F_PERMANENT)
725 				continue;
726 
727 			preferred_lft = READ_ONCE(ifa->ifa_preferred_lft);
728 			valid_lft = READ_ONCE(ifa->ifa_valid_lft);
729 			tstamp = READ_ONCE(ifa->ifa_tstamp);
730 			/* We try to batch several events at once. */
731 			age = (now - tstamp +
732 			       ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
733 
734 			if (valid_lft != INFINITY_LIFE_TIME &&
735 			    age >= valid_lft) {
736 				change_needed = true;
737 			} else if (preferred_lft ==
738 				   INFINITY_LIFE_TIME) {
739 				continue;
740 			} else if (age >= preferred_lft) {
741 				if (time_before(tstamp + valid_lft * HZ, next))
742 					next = tstamp + valid_lft * HZ;
743 
744 				if (!(flags & IFA_F_DEPRECATED))
745 					change_needed = true;
746 			} else if (time_before(tstamp + preferred_lft * HZ,
747 					       next)) {
748 				next = tstamp + preferred_lft * HZ;
749 			}
750 		}
751 		rcu_read_unlock();
752 		if (!change_needed)
753 			continue;
754 
755 		rtnl_net_lock(net);
756 		hlist_for_each_entry_safe(ifa, n, head, addr_lst) {
757 			unsigned long age;
758 
759 			if (ifa->ifa_flags & IFA_F_PERMANENT)
760 				continue;
761 
762 			/* We try to batch several events at once. */
763 			age = (now - ifa->ifa_tstamp +
764 			       ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
765 
766 			if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
767 			    age >= ifa->ifa_valid_lft) {
768 				struct in_ifaddr __rcu **ifap;
769 				struct in_ifaddr *tmp;
770 
771 				ifap = &ifa->ifa_dev->ifa_list;
772 				tmp = rtnl_net_dereference(net, *ifap);
773 				while (tmp) {
774 					if (tmp == ifa) {
775 						inet_del_ifa(ifa->ifa_dev,
776 							     ifap, 1);
777 						break;
778 					}
779 					ifap = &tmp->ifa_next;
780 					tmp = rtnl_net_dereference(net, *ifap);
781 				}
782 			} else if (ifa->ifa_preferred_lft !=
783 				   INFINITY_LIFE_TIME &&
784 				   age >= ifa->ifa_preferred_lft &&
785 				   !(ifa->ifa_flags & IFA_F_DEPRECATED)) {
786 				ifa->ifa_flags |= IFA_F_DEPRECATED;
787 				rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
788 			}
789 		}
790 		rtnl_net_unlock(net);
791 	}
792 
793 	next_sec = round_jiffies_up(next);
794 	next_sched = next;
795 
796 	/* If rounded timeout is accurate enough, accept it. */
797 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
798 		next_sched = next_sec;
799 
800 	now = jiffies;
801 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
802 	if (time_before(next_sched, now + ADDRCONF_TIMER_FUZZ_MAX))
803 		next_sched = now + ADDRCONF_TIMER_FUZZ_MAX;
804 
805 	queue_delayed_work(system_power_efficient_wq, &net->ipv4.addr_chk_work,
806 			   next_sched - now);
807 }
808 
809 static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft,
810 			     __u32 prefered_lft)
811 {
812 	unsigned long timeout;
813 	u32 flags;
814 
815 	flags = ifa->ifa_flags & ~(IFA_F_PERMANENT | IFA_F_DEPRECATED);
816 
817 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
818 	if (addrconf_finite_timeout(timeout))
819 		WRITE_ONCE(ifa->ifa_valid_lft, timeout);
820 	else
821 		flags |= IFA_F_PERMANENT;
822 
823 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
824 	if (addrconf_finite_timeout(timeout)) {
825 		if (timeout == 0)
826 			flags |= IFA_F_DEPRECATED;
827 		WRITE_ONCE(ifa->ifa_preferred_lft, timeout);
828 	}
829 	WRITE_ONCE(ifa->ifa_flags, flags);
830 	WRITE_ONCE(ifa->ifa_tstamp, jiffies);
831 	if (!ifa->ifa_cstamp)
832 		WRITE_ONCE(ifa->ifa_cstamp, ifa->ifa_tstamp);
833 }
834 
835 static int inet_validate_rtm(struct nlmsghdr *nlh, struct nlattr **tb,
836 			     struct netlink_ext_ack *extack,
837 			     __u32 *valid_lft, __u32 *prefered_lft)
838 {
839 	struct ifaddrmsg *ifm = nlmsg_data(nlh);
840 	int err;
841 
842 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
843 				     ifa_ipv4_policy, extack);
844 	if (err < 0)
845 		return err;
846 
847 	if (ifm->ifa_prefixlen > 32) {
848 		NL_SET_ERR_MSG(extack, "ipv4: Invalid prefix length");
849 		return -EINVAL;
850 	}
851 
852 	if (!tb[IFA_LOCAL]) {
853 		NL_SET_ERR_MSG(extack, "ipv4: Local address is not supplied");
854 		return -EINVAL;
855 	}
856 
857 	if (tb[IFA_CACHEINFO]) {
858 		struct ifa_cacheinfo *ci;
859 
860 		ci = nla_data(tb[IFA_CACHEINFO]);
861 		if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) {
862 			NL_SET_ERR_MSG(extack, "ipv4: address lifetime invalid");
863 			return -EINVAL;
864 		}
865 
866 		*valid_lft = ci->ifa_valid;
867 		*prefered_lft = ci->ifa_prefered;
868 	}
869 
870 	return 0;
871 }
872 
873 static struct in_ifaddr *inet_rtm_to_ifa(struct net *net, struct nlmsghdr *nlh,
874 					 struct nlattr **tb,
875 					 struct netlink_ext_ack *extack)
876 {
877 	struct ifaddrmsg *ifm = nlmsg_data(nlh);
878 	struct in_device *in_dev;
879 	struct net_device *dev;
880 	struct in_ifaddr *ifa;
881 	int err;
882 
883 	dev = __dev_get_by_index(net, ifm->ifa_index);
884 	err = -ENODEV;
885 	if (!dev) {
886 		NL_SET_ERR_MSG(extack, "ipv4: Device not found");
887 		goto errout;
888 	}
889 
890 	in_dev = __in_dev_get_rtnl_net(dev);
891 	err = -ENOBUFS;
892 	if (!in_dev)
893 		goto errout;
894 
895 	ifa = inet_alloc_ifa(in_dev);
896 	if (!ifa)
897 		/*
898 		 * A potential indev allocation can be left alive, it stays
899 		 * assigned to its device and is destroy with it.
900 		 */
901 		goto errout;
902 
903 	ipv4_devconf_setall(in_dev);
904 	neigh_parms_data_state_setall(in_dev->arp_parms);
905 
906 	if (!tb[IFA_ADDRESS])
907 		tb[IFA_ADDRESS] = tb[IFA_LOCAL];
908 
909 	ifa->ifa_prefixlen = ifm->ifa_prefixlen;
910 	ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
911 	ifa->ifa_flags = nla_get_u32_default(tb[IFA_FLAGS], ifm->ifa_flags);
912 	ifa->ifa_scope = ifm->ifa_scope;
913 	ifa->ifa_local = nla_get_in_addr(tb[IFA_LOCAL]);
914 	ifa->ifa_address = nla_get_in_addr(tb[IFA_ADDRESS]);
915 
916 	if (tb[IFA_BROADCAST])
917 		ifa->ifa_broadcast = nla_get_in_addr(tb[IFA_BROADCAST]);
918 
919 	if (tb[IFA_LABEL])
920 		nla_strscpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
921 	else
922 		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
923 
924 	if (tb[IFA_RT_PRIORITY])
925 		ifa->ifa_rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
926 
927 	if (tb[IFA_PROTO])
928 		ifa->ifa_proto = nla_get_u8(tb[IFA_PROTO]);
929 
930 	return ifa;
931 
932 errout:
933 	return ERR_PTR(err);
934 }
935 
936 static struct in_ifaddr *find_matching_ifa(struct net *net, struct in_ifaddr *ifa)
937 {
938 	struct in_device *in_dev = ifa->ifa_dev;
939 	struct in_ifaddr *ifa1;
940 
941 	in_dev_for_each_ifa_rtnl_net(net, ifa1, in_dev) {
942 		if (ifa1->ifa_mask == ifa->ifa_mask &&
943 		    inet_ifa_match(ifa1->ifa_address, ifa) &&
944 		    ifa1->ifa_local == ifa->ifa_local)
945 			return ifa1;
946 	}
947 
948 	return NULL;
949 }
950 
951 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
952 			    struct netlink_ext_ack *extack)
953 {
954 	__u32 prefered_lft = INFINITY_LIFE_TIME;
955 	__u32 valid_lft = INFINITY_LIFE_TIME;
956 	struct net *net = sock_net(skb->sk);
957 	struct in_ifaddr *ifa_existing;
958 	struct nlattr *tb[IFA_MAX + 1];
959 	struct in_ifaddr *ifa;
960 	int ret;
961 
962 	ret = inet_validate_rtm(nlh, tb, extack, &valid_lft, &prefered_lft);
963 	if (ret < 0)
964 		return ret;
965 
966 	if (!nla_get_in_addr(tb[IFA_LOCAL]))
967 		return 0;
968 
969 	rtnl_net_lock(net);
970 
971 	ifa = inet_rtm_to_ifa(net, nlh, tb, extack);
972 	if (IS_ERR(ifa)) {
973 		ret = PTR_ERR(ifa);
974 		goto unlock;
975 	}
976 
977 	ifa_existing = find_matching_ifa(net, ifa);
978 	if (!ifa_existing) {
979 		/* It would be best to check for !NLM_F_CREATE here but
980 		 * userspace already relies on not having to provide this.
981 		 */
982 		set_ifa_lifetime(ifa, valid_lft, prefered_lft);
983 		if (ifa->ifa_flags & IFA_F_MCAUTOJOIN) {
984 			ret = ip_mc_autojoin_config(net, true, ifa);
985 			if (ret < 0) {
986 				NL_SET_ERR_MSG(extack, "ipv4: Multicast auto join failed");
987 				inet_free_ifa(ifa);
988 				goto unlock;
989 			}
990 		}
991 
992 		ret = __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid, extack);
993 	} else {
994 		u32 new_metric = ifa->ifa_rt_priority;
995 		u8 new_proto = ifa->ifa_proto;
996 
997 		inet_free_ifa(ifa);
998 
999 		if (nlh->nlmsg_flags & NLM_F_EXCL ||
1000 		    !(nlh->nlmsg_flags & NLM_F_REPLACE)) {
1001 			NL_SET_ERR_MSG(extack, "ipv4: Address already assigned");
1002 			ret = -EEXIST;
1003 			goto unlock;
1004 		}
1005 		ifa = ifa_existing;
1006 
1007 		if (ifa->ifa_rt_priority != new_metric) {
1008 			fib_modify_prefix_metric(ifa, new_metric);
1009 			ifa->ifa_rt_priority = new_metric;
1010 		}
1011 
1012 		ifa->ifa_proto = new_proto;
1013 
1014 		set_ifa_lifetime(ifa, valid_lft, prefered_lft);
1015 		cancel_delayed_work(&net->ipv4.addr_chk_work);
1016 		queue_delayed_work(system_power_efficient_wq,
1017 				   &net->ipv4.addr_chk_work, 0);
1018 		rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid);
1019 	}
1020 
1021 unlock:
1022 	rtnl_net_unlock(net);
1023 
1024 	return ret;
1025 }
1026 
1027 /*
1028  *	Determine a default network mask, based on the IP address.
1029  */
1030 
1031 static int inet_abc_len(__be32 addr)
1032 {
1033 	int rc = -1;	/* Something else, probably a multicast. */
1034 
1035 	if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
1036 		rc = 0;
1037 	else {
1038 		__u32 haddr = ntohl(addr);
1039 		if (IN_CLASSA(haddr))
1040 			rc = 8;
1041 		else if (IN_CLASSB(haddr))
1042 			rc = 16;
1043 		else if (IN_CLASSC(haddr))
1044 			rc = 24;
1045 		else if (IN_CLASSE(haddr))
1046 			rc = 32;
1047 	}
1048 
1049 	return rc;
1050 }
1051 
1052 
1053 int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
1054 {
1055 	struct sockaddr_in sin_orig;
1056 	struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr;
1057 	struct in_ifaddr __rcu **ifap = NULL;
1058 	struct in_device *in_dev;
1059 	struct in_ifaddr *ifa = NULL;
1060 	struct net_device *dev;
1061 	char *colon;
1062 	int ret = -EFAULT;
1063 	int tryaddrmatch = 0;
1064 
1065 	ifr->ifr_name[IFNAMSIZ - 1] = 0;
1066 
1067 	/* save original address for comparison */
1068 	memcpy(&sin_orig, sin, sizeof(*sin));
1069 
1070 	colon = strchr(ifr->ifr_name, ':');
1071 	if (colon)
1072 		*colon = 0;
1073 
1074 	dev_load(net, ifr->ifr_name);
1075 
1076 	switch (cmd) {
1077 	case SIOCGIFADDR:	/* Get interface address */
1078 	case SIOCGIFBRDADDR:	/* Get the broadcast address */
1079 	case SIOCGIFDSTADDR:	/* Get the destination address */
1080 	case SIOCGIFNETMASK:	/* Get the netmask for the interface */
1081 		/* Note that these ioctls will not sleep,
1082 		   so that we do not impose a lock.
1083 		   One day we will be forced to put shlock here (I mean SMP)
1084 		 */
1085 		tryaddrmatch = (sin_orig.sin_family == AF_INET);
1086 		memset(sin, 0, sizeof(*sin));
1087 		sin->sin_family = AF_INET;
1088 		break;
1089 
1090 	case SIOCSIFFLAGS:
1091 		ret = -EPERM;
1092 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1093 			goto out;
1094 		break;
1095 	case SIOCSIFADDR:	/* Set interface address (and family) */
1096 	case SIOCSIFBRDADDR:	/* Set the broadcast address */
1097 	case SIOCSIFDSTADDR:	/* Set the destination address */
1098 	case SIOCSIFNETMASK: 	/* Set the netmask for the interface */
1099 		ret = -EPERM;
1100 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1101 			goto out;
1102 		ret = -EINVAL;
1103 		if (sin->sin_family != AF_INET)
1104 			goto out;
1105 		break;
1106 	default:
1107 		ret = -EINVAL;
1108 		goto out;
1109 	}
1110 
1111 	rtnl_net_lock(net);
1112 
1113 	ret = -ENODEV;
1114 	dev = __dev_get_by_name(net, ifr->ifr_name);
1115 	if (!dev)
1116 		goto done;
1117 
1118 	if (colon)
1119 		*colon = ':';
1120 
1121 	in_dev = __in_dev_get_rtnl_net(dev);
1122 	if (in_dev) {
1123 		if (tryaddrmatch) {
1124 			/* Matthias Andree */
1125 			/* compare label and address (4.4BSD style) */
1126 			/* note: we only do this for a limited set of ioctls
1127 			   and only if the original address family was AF_INET.
1128 			   This is checked above. */
1129 
1130 			for (ifap = &in_dev->ifa_list;
1131 			     (ifa = rtnl_net_dereference(net, *ifap)) != NULL;
1132 			     ifap = &ifa->ifa_next) {
1133 				if (!strcmp(ifr->ifr_name, ifa->ifa_label) &&
1134 				    sin_orig.sin_addr.s_addr ==
1135 							ifa->ifa_local) {
1136 					break; /* found */
1137 				}
1138 			}
1139 		}
1140 		/* we didn't get a match, maybe the application is
1141 		   4.3BSD-style and passed in junk so we fall back to
1142 		   comparing just the label */
1143 		if (!ifa) {
1144 			for (ifap = &in_dev->ifa_list;
1145 			     (ifa = rtnl_net_dereference(net, *ifap)) != NULL;
1146 			     ifap = &ifa->ifa_next)
1147 				if (!strcmp(ifr->ifr_name, ifa->ifa_label))
1148 					break;
1149 		}
1150 	}
1151 
1152 	ret = -EADDRNOTAVAIL;
1153 	if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
1154 		goto done;
1155 
1156 	switch (cmd) {
1157 	case SIOCGIFADDR:	/* Get interface address */
1158 		ret = 0;
1159 		sin->sin_addr.s_addr = ifa->ifa_local;
1160 		break;
1161 
1162 	case SIOCGIFBRDADDR:	/* Get the broadcast address */
1163 		ret = 0;
1164 		sin->sin_addr.s_addr = ifa->ifa_broadcast;
1165 		break;
1166 
1167 	case SIOCGIFDSTADDR:	/* Get the destination address */
1168 		ret = 0;
1169 		sin->sin_addr.s_addr = ifa->ifa_address;
1170 		break;
1171 
1172 	case SIOCGIFNETMASK:	/* Get the netmask for the interface */
1173 		ret = 0;
1174 		sin->sin_addr.s_addr = ifa->ifa_mask;
1175 		break;
1176 
1177 	case SIOCSIFFLAGS:
1178 		if (colon) {
1179 			ret = -EADDRNOTAVAIL;
1180 			if (!ifa)
1181 				break;
1182 			ret = 0;
1183 			if (!(ifr->ifr_flags & IFF_UP))
1184 				inet_del_ifa(in_dev, ifap, 1);
1185 			break;
1186 		}
1187 
1188 		/* NETDEV_UP/DOWN/CHANGE could touch a peer dev */
1189 		ASSERT_RTNL();
1190 		ret = dev_change_flags(dev, ifr->ifr_flags, NULL);
1191 		break;
1192 
1193 	case SIOCSIFADDR:	/* Set interface address (and family) */
1194 		ret = -EINVAL;
1195 		if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1196 			break;
1197 
1198 		if (!ifa) {
1199 			ret = -ENOBUFS;
1200 			if (!in_dev)
1201 				break;
1202 			ifa = inet_alloc_ifa(in_dev);
1203 			if (!ifa)
1204 				break;
1205 
1206 			if (colon)
1207 				memcpy(ifa->ifa_label, ifr->ifr_name, IFNAMSIZ);
1208 			else
1209 				memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1210 		} else {
1211 			ret = 0;
1212 			if (ifa->ifa_local == sin->sin_addr.s_addr)
1213 				break;
1214 			inet_del_ifa(in_dev, ifap, 0);
1215 			ifa->ifa_broadcast = 0;
1216 			ifa->ifa_scope = 0;
1217 		}
1218 
1219 		ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
1220 
1221 		if (!(dev->flags & IFF_POINTOPOINT)) {
1222 			ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
1223 			ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
1224 			if ((dev->flags & IFF_BROADCAST) &&
1225 			    ifa->ifa_prefixlen < 31)
1226 				ifa->ifa_broadcast = ifa->ifa_address |
1227 						     ~ifa->ifa_mask;
1228 		} else {
1229 			ifa->ifa_prefixlen = 32;
1230 			ifa->ifa_mask = inet_make_mask(32);
1231 		}
1232 		set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
1233 		ret = inet_set_ifa(dev, ifa);
1234 		break;
1235 
1236 	case SIOCSIFBRDADDR:	/* Set the broadcast address */
1237 		ret = 0;
1238 		if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
1239 			inet_del_ifa(in_dev, ifap, 0);
1240 			ifa->ifa_broadcast = sin->sin_addr.s_addr;
1241 			inet_insert_ifa(ifa);
1242 		}
1243 		break;
1244 
1245 	case SIOCSIFDSTADDR:	/* Set the destination address */
1246 		ret = 0;
1247 		if (ifa->ifa_address == sin->sin_addr.s_addr)
1248 			break;
1249 		ret = -EINVAL;
1250 		if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1251 			break;
1252 		ret = 0;
1253 		inet_del_ifa(in_dev, ifap, 0);
1254 		ifa->ifa_address = sin->sin_addr.s_addr;
1255 		inet_insert_ifa(ifa);
1256 		break;
1257 
1258 	case SIOCSIFNETMASK: 	/* Set the netmask for the interface */
1259 
1260 		/*
1261 		 *	The mask we set must be legal.
1262 		 */
1263 		ret = -EINVAL;
1264 		if (bad_mask(sin->sin_addr.s_addr, 0))
1265 			break;
1266 		ret = 0;
1267 		if (ifa->ifa_mask != sin->sin_addr.s_addr) {
1268 			__be32 old_mask = ifa->ifa_mask;
1269 			inet_del_ifa(in_dev, ifap, 0);
1270 			ifa->ifa_mask = sin->sin_addr.s_addr;
1271 			ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
1272 
1273 			/* See if current broadcast address matches
1274 			 * with current netmask, then recalculate
1275 			 * the broadcast address. Otherwise it's a
1276 			 * funny address, so don't touch it since
1277 			 * the user seems to know what (s)he's doing...
1278 			 */
1279 			if ((dev->flags & IFF_BROADCAST) &&
1280 			    (ifa->ifa_prefixlen < 31) &&
1281 			    (ifa->ifa_broadcast ==
1282 			     (ifa->ifa_local|~old_mask))) {
1283 				ifa->ifa_broadcast = (ifa->ifa_local |
1284 						      ~sin->sin_addr.s_addr);
1285 			}
1286 			inet_insert_ifa(ifa);
1287 		}
1288 		break;
1289 	}
1290 done:
1291 	rtnl_net_unlock(net);
1292 out:
1293 	return ret;
1294 }
1295 
1296 int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size)
1297 {
1298 	struct in_device *in_dev = __in_dev_get_rtnl_net(dev);
1299 	const struct in_ifaddr *ifa;
1300 	struct ifreq ifr;
1301 	int done = 0;
1302 
1303 	if (WARN_ON(size > sizeof(struct ifreq)))
1304 		goto out;
1305 
1306 	if (!in_dev)
1307 		goto out;
1308 
1309 	in_dev_for_each_ifa_rtnl_net(dev_net(dev), ifa, in_dev) {
1310 		if (!buf) {
1311 			done += size;
1312 			continue;
1313 		}
1314 		if (len < size)
1315 			break;
1316 		memset(&ifr, 0, sizeof(struct ifreq));
1317 		strcpy(ifr.ifr_name, ifa->ifa_label);
1318 
1319 		(*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
1320 		(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
1321 								ifa->ifa_local;
1322 
1323 		if (copy_to_user(buf + done, &ifr, size)) {
1324 			done = -EFAULT;
1325 			break;
1326 		}
1327 		len  -= size;
1328 		done += size;
1329 	}
1330 out:
1331 	return done;
1332 }
1333 
1334 static __be32 in_dev_select_addr(const struct in_device *in_dev,
1335 				 int scope)
1336 {
1337 	const struct in_ifaddr *ifa;
1338 
1339 	in_dev_for_each_ifa_rcu(ifa, in_dev) {
1340 		if (READ_ONCE(ifa->ifa_flags) & IFA_F_SECONDARY)
1341 			continue;
1342 		if (ifa->ifa_scope != RT_SCOPE_LINK &&
1343 		    ifa->ifa_scope <= scope)
1344 			return ifa->ifa_local;
1345 	}
1346 
1347 	return 0;
1348 }
1349 
1350 __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
1351 {
1352 	const struct in_ifaddr *ifa;
1353 	__be32 addr = 0;
1354 	unsigned char localnet_scope = RT_SCOPE_HOST;
1355 	struct in_device *in_dev;
1356 	struct net *net;
1357 	int master_idx;
1358 
1359 	rcu_read_lock();
1360 	net = dev_net_rcu(dev);
1361 	in_dev = __in_dev_get_rcu(dev);
1362 	if (!in_dev)
1363 		goto no_in_dev;
1364 
1365 	if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev)))
1366 		localnet_scope = RT_SCOPE_LINK;
1367 
1368 	in_dev_for_each_ifa_rcu(ifa, in_dev) {
1369 		if (READ_ONCE(ifa->ifa_flags) & IFA_F_SECONDARY)
1370 			continue;
1371 		if (min(ifa->ifa_scope, localnet_scope) > scope)
1372 			continue;
1373 		if (!dst || inet_ifa_match(dst, ifa)) {
1374 			addr = ifa->ifa_local;
1375 			break;
1376 		}
1377 		if (!addr)
1378 			addr = ifa->ifa_local;
1379 	}
1380 
1381 	if (addr)
1382 		goto out_unlock;
1383 no_in_dev:
1384 	master_idx = l3mdev_master_ifindex_rcu(dev);
1385 
1386 	/* For VRFs, the VRF device takes the place of the loopback device,
1387 	 * with addresses on it being preferred.  Note in such cases the
1388 	 * loopback device will be among the devices that fail the master_idx
1389 	 * equality check in the loop below.
1390 	 */
1391 	if (master_idx &&
1392 	    (dev = dev_get_by_index_rcu(net, master_idx)) &&
1393 	    (in_dev = __in_dev_get_rcu(dev))) {
1394 		addr = in_dev_select_addr(in_dev, scope);
1395 		if (addr)
1396 			goto out_unlock;
1397 	}
1398 
1399 	/* Not loopback addresses on loopback should be preferred
1400 	   in this case. It is important that lo is the first interface
1401 	   in dev_base list.
1402 	 */
1403 	for_each_netdev_rcu(net, dev) {
1404 		if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1405 			continue;
1406 
1407 		in_dev = __in_dev_get_rcu(dev);
1408 		if (!in_dev)
1409 			continue;
1410 
1411 		addr = in_dev_select_addr(in_dev, scope);
1412 		if (addr)
1413 			goto out_unlock;
1414 	}
1415 out_unlock:
1416 	rcu_read_unlock();
1417 	return addr;
1418 }
1419 EXPORT_SYMBOL(inet_select_addr);
1420 
1421 static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
1422 			      __be32 local, int scope)
1423 {
1424 	unsigned char localnet_scope = RT_SCOPE_HOST;
1425 	const struct in_ifaddr *ifa;
1426 	__be32 addr = 0;
1427 	int same = 0;
1428 
1429 	if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev)))
1430 		localnet_scope = RT_SCOPE_LINK;
1431 
1432 	in_dev_for_each_ifa_rcu(ifa, in_dev) {
1433 		unsigned char min_scope = min(ifa->ifa_scope, localnet_scope);
1434 
1435 		if (!addr &&
1436 		    (local == ifa->ifa_local || !local) &&
1437 		    min_scope <= scope) {
1438 			addr = ifa->ifa_local;
1439 			if (same)
1440 				break;
1441 		}
1442 		if (!same) {
1443 			same = (!local || inet_ifa_match(local, ifa)) &&
1444 				(!dst || inet_ifa_match(dst, ifa));
1445 			if (same && addr) {
1446 				if (local || !dst)
1447 					break;
1448 				/* Is the selected addr into dst subnet? */
1449 				if (inet_ifa_match(addr, ifa))
1450 					break;
1451 				/* No, then can we use new local src? */
1452 				if (min_scope <= scope) {
1453 					addr = ifa->ifa_local;
1454 					break;
1455 				}
1456 				/* search for large dst subnet for addr */
1457 				same = 0;
1458 			}
1459 		}
1460 	}
1461 
1462 	return same ? addr : 0;
1463 }
1464 
1465 /*
1466  * Confirm that local IP address exists using wildcards:
1467  * - net: netns to check, cannot be NULL
1468  * - in_dev: only on this interface, NULL=any interface
1469  * - dst: only in the same subnet as dst, 0=any dst
1470  * - local: address, 0=autoselect the local address
1471  * - scope: maximum allowed scope value for the local address
1472  */
1473 __be32 inet_confirm_addr(struct net *net, struct in_device *in_dev,
1474 			 __be32 dst, __be32 local, int scope)
1475 {
1476 	__be32 addr = 0;
1477 	struct net_device *dev;
1478 
1479 	if (in_dev)
1480 		return confirm_addr_indev(in_dev, dst, local, scope);
1481 
1482 	rcu_read_lock();
1483 	for_each_netdev_rcu(net, dev) {
1484 		in_dev = __in_dev_get_rcu(dev);
1485 		if (in_dev) {
1486 			addr = confirm_addr_indev(in_dev, dst, local, scope);
1487 			if (addr)
1488 				break;
1489 		}
1490 	}
1491 	rcu_read_unlock();
1492 
1493 	return addr;
1494 }
1495 EXPORT_SYMBOL(inet_confirm_addr);
1496 
1497 /*
1498  *	Device notifier
1499  */
1500 
1501 int register_inetaddr_notifier(struct notifier_block *nb)
1502 {
1503 	return blocking_notifier_chain_register(&inetaddr_chain, nb);
1504 }
1505 EXPORT_SYMBOL(register_inetaddr_notifier);
1506 
1507 int unregister_inetaddr_notifier(struct notifier_block *nb)
1508 {
1509 	return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
1510 }
1511 EXPORT_SYMBOL(unregister_inetaddr_notifier);
1512 
1513 int register_inetaddr_validator_notifier(struct notifier_block *nb)
1514 {
1515 	return blocking_notifier_chain_register(&inetaddr_validator_chain, nb);
1516 }
1517 EXPORT_SYMBOL(register_inetaddr_validator_notifier);
1518 
1519 int unregister_inetaddr_validator_notifier(struct notifier_block *nb)
1520 {
1521 	return blocking_notifier_chain_unregister(&inetaddr_validator_chain,
1522 	    nb);
1523 }
1524 EXPORT_SYMBOL(unregister_inetaddr_validator_notifier);
1525 
1526 /* Rename ifa_labels for a device name change. Make some effort to preserve
1527  * existing alias numbering and to create unique labels if possible.
1528 */
1529 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
1530 {
1531 	struct in_ifaddr *ifa;
1532 	int named = 0;
1533 
1534 	in_dev_for_each_ifa_rtnl(ifa, in_dev) {
1535 		char old[IFNAMSIZ], *dot;
1536 
1537 		memcpy(old, ifa->ifa_label, IFNAMSIZ);
1538 		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1539 		if (named++ == 0)
1540 			goto skip;
1541 		dot = strchr(old, ':');
1542 		if (!dot) {
1543 			sprintf(old, ":%d", named);
1544 			dot = old;
1545 		}
1546 		if (strlen(dot) + strlen(dev->name) < IFNAMSIZ)
1547 			strcat(ifa->ifa_label, dot);
1548 		else
1549 			strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
1550 skip:
1551 		rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
1552 	}
1553 }
1554 
1555 static void inetdev_send_gratuitous_arp(struct net_device *dev,
1556 					struct in_device *in_dev)
1557 
1558 {
1559 	const struct in_ifaddr *ifa;
1560 
1561 	in_dev_for_each_ifa_rtnl(ifa, in_dev) {
1562 		arp_send(ARPOP_REQUEST, ETH_P_ARP,
1563 			 ifa->ifa_local, dev,
1564 			 ifa->ifa_local, NULL,
1565 			 dev->dev_addr, NULL);
1566 	}
1567 }
1568 
1569 /* Called only under RTNL semaphore */
1570 
1571 static int inetdev_event(struct notifier_block *this, unsigned long event,
1572 			 void *ptr)
1573 {
1574 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1575 	struct in_device *in_dev = __in_dev_get_rtnl(dev);
1576 
1577 	ASSERT_RTNL();
1578 
1579 	if (!in_dev) {
1580 		if (event == NETDEV_REGISTER) {
1581 			in_dev = inetdev_init(dev);
1582 			if (IS_ERR(in_dev))
1583 				return notifier_from_errno(PTR_ERR(in_dev));
1584 			if (dev->flags & IFF_LOOPBACK) {
1585 				IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
1586 				IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
1587 			}
1588 		} else if (event == NETDEV_CHANGEMTU) {
1589 			/* Re-enabling IP */
1590 			if (inetdev_valid_mtu(dev->mtu))
1591 				in_dev = inetdev_init(dev);
1592 		}
1593 		goto out;
1594 	}
1595 
1596 	switch (event) {
1597 	case NETDEV_REGISTER:
1598 		pr_debug("%s: bug\n", __func__);
1599 		RCU_INIT_POINTER(dev->ip_ptr, NULL);
1600 		break;
1601 	case NETDEV_UP:
1602 		if (!inetdev_valid_mtu(dev->mtu))
1603 			break;
1604 		if (dev->flags & IFF_LOOPBACK) {
1605 			struct in_ifaddr *ifa = inet_alloc_ifa(in_dev);
1606 
1607 			if (ifa) {
1608 				ifa->ifa_local =
1609 				  ifa->ifa_address = htonl(INADDR_LOOPBACK);
1610 				ifa->ifa_prefixlen = 8;
1611 				ifa->ifa_mask = inet_make_mask(8);
1612 				ifa->ifa_scope = RT_SCOPE_HOST;
1613 				memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1614 				set_ifa_lifetime(ifa, INFINITY_LIFE_TIME,
1615 						 INFINITY_LIFE_TIME);
1616 				ipv4_devconf_setall(in_dev);
1617 				neigh_parms_data_state_setall(in_dev->arp_parms);
1618 				inet_insert_ifa(ifa);
1619 			}
1620 		}
1621 		ip_mc_up(in_dev);
1622 		fallthrough;
1623 	case NETDEV_CHANGEADDR:
1624 		if (!IN_DEV_ARP_NOTIFY(in_dev))
1625 			break;
1626 		fallthrough;
1627 	case NETDEV_NOTIFY_PEERS:
1628 		/* Send gratuitous ARP to notify of link change */
1629 		inetdev_send_gratuitous_arp(dev, in_dev);
1630 		break;
1631 	case NETDEV_DOWN:
1632 		ip_mc_down(in_dev);
1633 		break;
1634 	case NETDEV_PRE_TYPE_CHANGE:
1635 		ip_mc_unmap(in_dev);
1636 		break;
1637 	case NETDEV_POST_TYPE_CHANGE:
1638 		ip_mc_remap(in_dev);
1639 		break;
1640 	case NETDEV_CHANGEMTU:
1641 		if (inetdev_valid_mtu(dev->mtu))
1642 			break;
1643 		/* disable IP when MTU is not enough */
1644 		fallthrough;
1645 	case NETDEV_UNREGISTER:
1646 		inetdev_destroy(in_dev);
1647 		break;
1648 	case NETDEV_CHANGENAME:
1649 		/* Do not notify about label change, this event is
1650 		 * not interesting to applications using netlink.
1651 		 */
1652 		inetdev_changename(dev, in_dev);
1653 
1654 		devinet_sysctl_unregister(in_dev);
1655 		devinet_sysctl_register(in_dev);
1656 		break;
1657 	}
1658 out:
1659 	return NOTIFY_DONE;
1660 }
1661 
1662 static struct notifier_block ip_netdev_notifier = {
1663 	.notifier_call = inetdev_event,
1664 };
1665 
1666 static size_t inet_nlmsg_size(void)
1667 {
1668 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1669 	       + nla_total_size(4) /* IFA_ADDRESS */
1670 	       + nla_total_size(4) /* IFA_LOCAL */
1671 	       + nla_total_size(4) /* IFA_BROADCAST */
1672 	       + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
1673 	       + nla_total_size(4)  /* IFA_FLAGS */
1674 	       + nla_total_size(1)  /* IFA_PROTO */
1675 	       + nla_total_size(4)  /* IFA_RT_PRIORITY */
1676 	       + nla_total_size(sizeof(struct ifa_cacheinfo)); /* IFA_CACHEINFO */
1677 }
1678 
1679 static inline u32 cstamp_delta(unsigned long cstamp)
1680 {
1681 	return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
1682 }
1683 
1684 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
1685 			 unsigned long tstamp, u32 preferred, u32 valid)
1686 {
1687 	struct ifa_cacheinfo ci;
1688 
1689 	ci.cstamp = cstamp_delta(cstamp);
1690 	ci.tstamp = cstamp_delta(tstamp);
1691 	ci.ifa_prefered = preferred;
1692 	ci.ifa_valid = valid;
1693 
1694 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
1695 }
1696 
1697 static int inet_fill_ifaddr(struct sk_buff *skb, const struct in_ifaddr *ifa,
1698 			    struct inet_fill_args *args)
1699 {
1700 	struct ifaddrmsg *ifm;
1701 	struct nlmsghdr  *nlh;
1702 	unsigned long tstamp;
1703 	u32 preferred, valid;
1704 	u32 flags;
1705 
1706 	nlh = nlmsg_put(skb, args->portid, args->seq, args->event, sizeof(*ifm),
1707 			args->flags);
1708 	if (!nlh)
1709 		return -EMSGSIZE;
1710 
1711 	ifm = nlmsg_data(nlh);
1712 	ifm->ifa_family = AF_INET;
1713 	ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1714 
1715 	flags = READ_ONCE(ifa->ifa_flags);
1716 	/* Warning : ifm->ifa_flags is an __u8, it holds only 8 bits.
1717 	 * The 32bit value is given in IFA_FLAGS attribute.
1718 	 */
1719 	ifm->ifa_flags = (__u8)flags;
1720 
1721 	ifm->ifa_scope = ifa->ifa_scope;
1722 	ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1723 
1724 	if (args->netnsid >= 0 &&
1725 	    nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
1726 		goto nla_put_failure;
1727 
1728 	tstamp = READ_ONCE(ifa->ifa_tstamp);
1729 	if (!(flags & IFA_F_PERMANENT)) {
1730 		preferred = READ_ONCE(ifa->ifa_preferred_lft);
1731 		valid = READ_ONCE(ifa->ifa_valid_lft);
1732 		if (preferred != INFINITY_LIFE_TIME) {
1733 			long tval = (jiffies - tstamp) / HZ;
1734 
1735 			if (preferred > tval)
1736 				preferred -= tval;
1737 			else
1738 				preferred = 0;
1739 			if (valid != INFINITY_LIFE_TIME) {
1740 				if (valid > tval)
1741 					valid -= tval;
1742 				else
1743 					valid = 0;
1744 			}
1745 		}
1746 	} else {
1747 		preferred = INFINITY_LIFE_TIME;
1748 		valid = INFINITY_LIFE_TIME;
1749 	}
1750 	if ((ifa->ifa_address &&
1751 	     nla_put_in_addr(skb, IFA_ADDRESS, ifa->ifa_address)) ||
1752 	    (ifa->ifa_local &&
1753 	     nla_put_in_addr(skb, IFA_LOCAL, ifa->ifa_local)) ||
1754 	    (ifa->ifa_broadcast &&
1755 	     nla_put_in_addr(skb, IFA_BROADCAST, ifa->ifa_broadcast)) ||
1756 	    (ifa->ifa_label[0] &&
1757 	     nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) ||
1758 	    (ifa->ifa_proto &&
1759 	     nla_put_u8(skb, IFA_PROTO, ifa->ifa_proto)) ||
1760 	    nla_put_u32(skb, IFA_FLAGS, flags) ||
1761 	    (ifa->ifa_rt_priority &&
1762 	     nla_put_u32(skb, IFA_RT_PRIORITY, ifa->ifa_rt_priority)) ||
1763 	    put_cacheinfo(skb, READ_ONCE(ifa->ifa_cstamp), tstamp,
1764 			  preferred, valid))
1765 		goto nla_put_failure;
1766 
1767 	nlmsg_end(skb, nlh);
1768 	return 0;
1769 
1770 nla_put_failure:
1771 	nlmsg_cancel(skb, nlh);
1772 	return -EMSGSIZE;
1773 }
1774 
1775 static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
1776 				      struct inet_fill_args *fillargs,
1777 				      struct net **tgt_net, struct sock *sk,
1778 				      struct netlink_callback *cb)
1779 {
1780 	struct netlink_ext_ack *extack = cb->extack;
1781 	struct nlattr *tb[IFA_MAX+1];
1782 	struct ifaddrmsg *ifm;
1783 	int err, i;
1784 
1785 	ifm = nlmsg_payload(nlh, sizeof(*ifm));
1786 	if (!ifm) {
1787 		NL_SET_ERR_MSG(extack, "ipv4: Invalid header for address dump request");
1788 		return -EINVAL;
1789 	}
1790 
1791 	if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
1792 		NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for address dump request");
1793 		return -EINVAL;
1794 	}
1795 
1796 	fillargs->ifindex = ifm->ifa_index;
1797 	if (fillargs->ifindex) {
1798 		cb->answer_flags |= NLM_F_DUMP_FILTERED;
1799 		fillargs->flags |= NLM_F_DUMP_FILTERED;
1800 	}
1801 
1802 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
1803 					    ifa_ipv4_policy, extack);
1804 	if (err < 0)
1805 		return err;
1806 
1807 	for (i = 0; i <= IFA_MAX; ++i) {
1808 		if (!tb[i])
1809 			continue;
1810 
1811 		if (i == IFA_TARGET_NETNSID) {
1812 			struct net *net;
1813 
1814 			fillargs->netnsid = nla_get_s32(tb[i]);
1815 
1816 			net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
1817 			if (IS_ERR(net)) {
1818 				fillargs->netnsid = -1;
1819 				NL_SET_ERR_MSG(extack, "ipv4: Invalid target network namespace id");
1820 				return PTR_ERR(net);
1821 			}
1822 			*tgt_net = net;
1823 		} else {
1824 			NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in dump request");
1825 			return -EINVAL;
1826 		}
1827 	}
1828 
1829 	return 0;
1830 }
1831 
1832 static int in_dev_dump_ifmcaddr(struct in_device *in_dev, struct sk_buff *skb,
1833 				struct netlink_callback *cb, int *s_ip_idx,
1834 				struct inet_fill_args *fillargs)
1835 {
1836 	struct ip_mc_list *im;
1837 	int ip_idx = 0;
1838 	int err;
1839 
1840 	for (im = rcu_dereference(in_dev->mc_list);
1841 	     im;
1842 	     im = rcu_dereference(im->next_rcu)) {
1843 		if (ip_idx < *s_ip_idx) {
1844 			ip_idx++;
1845 			continue;
1846 		}
1847 		err = inet_fill_ifmcaddr(skb, in_dev->dev, im, fillargs);
1848 		if (err < 0)
1849 			goto done;
1850 
1851 		nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1852 		ip_idx++;
1853 	}
1854 	err = 0;
1855 	ip_idx = 0;
1856 done:
1857 	*s_ip_idx = ip_idx;
1858 	return err;
1859 }
1860 
1861 static int in_dev_dump_ifaddr(struct in_device *in_dev, struct sk_buff *skb,
1862 			      struct netlink_callback *cb, int *s_ip_idx,
1863 			      struct inet_fill_args *fillargs)
1864 {
1865 	struct in_ifaddr *ifa;
1866 	int ip_idx = 0;
1867 	int err;
1868 
1869 	in_dev_for_each_ifa_rcu(ifa, in_dev) {
1870 		if (ip_idx < *s_ip_idx) {
1871 			ip_idx++;
1872 			continue;
1873 		}
1874 		err = inet_fill_ifaddr(skb, ifa, fillargs);
1875 		if (err < 0)
1876 			goto done;
1877 
1878 		nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1879 		ip_idx++;
1880 	}
1881 	err = 0;
1882 	ip_idx = 0;
1883 done:
1884 	*s_ip_idx = ip_idx;
1885 
1886 	return err;
1887 }
1888 
1889 static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb,
1890 			    struct netlink_callback *cb, int *s_ip_idx,
1891 			    struct inet_fill_args *fillargs)
1892 {
1893 	switch (fillargs->event) {
1894 	case RTM_NEWADDR:
1895 		return in_dev_dump_ifaddr(in_dev, skb, cb, s_ip_idx, fillargs);
1896 	case RTM_GETMULTICAST:
1897 		return in_dev_dump_ifmcaddr(in_dev, skb, cb, s_ip_idx,
1898 					    fillargs);
1899 	default:
1900 		return -EINVAL;
1901 	}
1902 }
1903 
1904 /* Combine dev_addr_genid and dev_base_seq to detect changes.
1905  */
1906 static u32 inet_base_seq(const struct net *net)
1907 {
1908 	u32 res = atomic_read(&net->ipv4.dev_addr_genid) +
1909 		  READ_ONCE(net->dev_base_seq);
1910 
1911 	/* Must not return 0 (see nl_dump_check_consistent()).
1912 	 * Chose a value far away from 0.
1913 	 */
1914 	if (!res)
1915 		res = 0x80000000;
1916 	return res;
1917 }
1918 
1919 static int inet_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
1920 			  int event)
1921 {
1922 	const struct nlmsghdr *nlh = cb->nlh;
1923 	struct inet_fill_args fillargs = {
1924 		.portid = NETLINK_CB(cb->skb).portid,
1925 		.seq = nlh->nlmsg_seq,
1926 		.event = event,
1927 		.flags = NLM_F_MULTI,
1928 		.netnsid = -1,
1929 	};
1930 	struct net *net = sock_net(skb->sk);
1931 	struct net *tgt_net = net;
1932 	struct {
1933 		unsigned long ifindex;
1934 		int ip_idx;
1935 	} *ctx = (void *)cb->ctx;
1936 	struct in_device *in_dev;
1937 	struct net_device *dev;
1938 	int err = 0;
1939 
1940 	rcu_read_lock();
1941 	if (cb->strict_check) {
1942 		err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
1943 						 skb->sk, cb);
1944 		if (err < 0)
1945 			goto done;
1946 
1947 		if (fillargs.ifindex) {
1948 			dev = dev_get_by_index_rcu(tgt_net, fillargs.ifindex);
1949 			if (!dev) {
1950 				err = -ENODEV;
1951 				goto done;
1952 			}
1953 			in_dev = __in_dev_get_rcu(dev);
1954 			if (!in_dev)
1955 				goto done;
1956 			err = in_dev_dump_addr(in_dev, skb, cb, &ctx->ip_idx,
1957 					       &fillargs);
1958 			goto done;
1959 		}
1960 	}
1961 
1962 	cb->seq = inet_base_seq(tgt_net);
1963 
1964 	for_each_netdev_dump(tgt_net, dev, ctx->ifindex) {
1965 		in_dev = __in_dev_get_rcu(dev);
1966 		if (!in_dev)
1967 			continue;
1968 		err = in_dev_dump_addr(in_dev, skb, cb, &ctx->ip_idx,
1969 				       &fillargs);
1970 		if (err < 0)
1971 			goto done;
1972 	}
1973 done:
1974 	if (fillargs.netnsid >= 0)
1975 		put_net(tgt_net);
1976 	rcu_read_unlock();
1977 	return err;
1978 }
1979 
1980 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1981 {
1982 	return inet_dump_addr(skb, cb, RTM_NEWADDR);
1983 }
1984 
1985 static int inet_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
1986 {
1987 	return inet_dump_addr(skb, cb, RTM_GETMULTICAST);
1988 }
1989 
1990 static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
1991 		      u32 portid)
1992 {
1993 	struct inet_fill_args fillargs = {
1994 		.portid = portid,
1995 		.seq = nlh ? nlh->nlmsg_seq : 0,
1996 		.event = event,
1997 		.flags = 0,
1998 		.netnsid = -1,
1999 	};
2000 	struct sk_buff *skb;
2001 	int err = -ENOBUFS;
2002 	struct net *net;
2003 
2004 	net = dev_net(ifa->ifa_dev->dev);
2005 	skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
2006 	if (!skb)
2007 		goto errout;
2008 
2009 	err = inet_fill_ifaddr(skb, ifa, &fillargs);
2010 	if (err < 0) {
2011 		/* -EMSGSIZE implies BUG in inet_nlmsg_size() */
2012 		WARN_ON(err == -EMSGSIZE);
2013 		kfree_skb(skb);
2014 		goto errout;
2015 	}
2016 	rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
2017 	return;
2018 errout:
2019 	rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
2020 }
2021 
2022 static size_t inet_get_link_af_size(const struct net_device *dev,
2023 				    u32 ext_filter_mask)
2024 {
2025 	struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
2026 
2027 	if (!in_dev)
2028 		return 0;
2029 
2030 	return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
2031 }
2032 
2033 static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
2034 			     u32 ext_filter_mask)
2035 {
2036 	struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
2037 	struct nlattr *nla;
2038 	int i;
2039 
2040 	if (!in_dev)
2041 		return -ENODATA;
2042 
2043 	nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
2044 	if (!nla)
2045 		return -EMSGSIZE;
2046 
2047 	for (i = 0; i < IPV4_DEVCONF_MAX; i++)
2048 		((u32 *) nla_data(nla))[i] = READ_ONCE(in_dev->cnf.data[i]);
2049 
2050 	return 0;
2051 }
2052 
2053 static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
2054 	[IFLA_INET_CONF]	= { .type = NLA_NESTED },
2055 };
2056 
2057 static const struct nla_policy inet_devconf_policy[IPV4_DEVCONF_MAX + 1] = {
2058 	[IPV4_DEVCONF_FORWARDING]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2059 	[IPV4_DEVCONF_MC_FORWARDING]	= { .type = NLA_REJECT },
2060 	[IPV4_DEVCONF_PROXY_ARP]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2061 	[IPV4_DEVCONF_ACCEPT_REDIRECTS]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2062 	[IPV4_DEVCONF_SECURE_REDIRECTS]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2063 	[IPV4_DEVCONF_SEND_REDIRECTS]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2064 	[IPV4_DEVCONF_SHARED_MEDIA]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2065 	[IPV4_DEVCONF_RP_FILTER]	= NLA_POLICY_RANGE(NLA_U32, 0, 2),
2066 	[IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE] = NLA_POLICY_RANGE(NLA_U32, 0, 1),
2067 	[IPV4_DEVCONF_BOOTP_RELAY]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2068 	[IPV4_DEVCONF_LOG_MARTIANS]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2069 	[IPV4_DEVCONF_TAG]		= { .type = NLA_U32 },
2070 	[IPV4_DEVCONF_ARPFILTER]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2071 	[IPV4_DEVCONF_MEDIUM_ID]	= NLA_POLICY_MIN(NLA_S32, -1),
2072 	[IPV4_DEVCONF_NOXFRM]		= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2073 	[IPV4_DEVCONF_NOPOLICY]		= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2074 	[IPV4_DEVCONF_FORCE_IGMP_VERSION] = NLA_POLICY_RANGE(NLA_U32, 0, 3),
2075 	[IPV4_DEVCONF_ARP_ANNOUNCE]	= NLA_POLICY_RANGE(NLA_U32, 0, 2),
2076 	[IPV4_DEVCONF_ARP_IGNORE]	= NLA_POLICY_RANGE(NLA_U32, 0, 8),
2077 	[IPV4_DEVCONF_PROMOTE_SECONDARIES] = NLA_POLICY_RANGE(NLA_U32, 0, 1),
2078 	[IPV4_DEVCONF_ARP_ACCEPT]	= NLA_POLICY_RANGE(NLA_U32, 0, 2),
2079 	[IPV4_DEVCONF_ARP_NOTIFY]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2080 	[IPV4_DEVCONF_ACCEPT_LOCAL]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2081 	[IPV4_DEVCONF_SRC_VMARK]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2082 	[IPV4_DEVCONF_PROXY_ARP_PVLAN]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2083 	[IPV4_DEVCONF_ROUTE_LOCALNET]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2084 	[IPV4_DEVCONF_BC_FORWARDING]	= NLA_POLICY_RANGE(NLA_U32, 0, 1),
2085 	[IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL] = { .type = NLA_U32 },
2086 	[IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL] = { .type = NLA_U32 },
2087 	[IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] =
2088 		NLA_POLICY_RANGE(NLA_U32, 0, 1),
2089 	[IPV4_DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] =
2090 		NLA_POLICY_RANGE(NLA_U32, 0, 1),
2091 	[IPV4_DEVCONF_DROP_GRATUITOUS_ARP] = NLA_POLICY_RANGE(NLA_U32, 0, 1),
2092 	[IPV4_DEVCONF_ARP_EVICT_NOCARRIER] = NLA_POLICY_RANGE(NLA_U32, 0, 1),
2093 };
2094 
2095 static int inet_validate_link_af(const struct net_device *dev,
2096 				 const struct nlattr *nla,
2097 				 struct netlink_ext_ack *extack)
2098 {
2099 	struct nlattr *tb[IFLA_INET_MAX + 1], *nested_tb[IPV4_DEVCONF_MAX + 1];
2100 	int err;
2101 
2102 	if (dev && !__in_dev_get_rtnl(dev))
2103 		return -EAFNOSUPPORT;
2104 
2105 	err = nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla,
2106 					  inet_af_policy, extack);
2107 	if (err < 0)
2108 		return err;
2109 
2110 	if (tb[IFLA_INET_CONF]) {
2111 		err = nla_parse(nested_tb, IPV4_DEVCONF_MAX,
2112 				nla_data(tb[IFLA_INET_CONF]),
2113 				nla_len(tb[IFLA_INET_CONF]),
2114 				inet_devconf_policy, extack);
2115 
2116 		if (err < 0)
2117 			return err;
2118 	}
2119 
2120 	return 0;
2121 }
2122 
2123 static bool devinet_conf_post_set(struct net *net, struct ipv4_devconf *cnf,
2124 				  int attr, int new, int old, int ifindex)
2125 {
2126 	if (new == old)
2127 		return false;
2128 
2129 	switch (attr) {
2130 	case IPV4_DEVCONF_ROUTE_LOCALNET:
2131 	case IPV4_DEVCONF_ACCEPT_LOCAL:
2132 		if (new == 0)
2133 			return true;
2134 		break;
2135 	case IPV4_DEVCONF_NOXFRM:
2136 	case IPV4_DEVCONF_NOPOLICY:
2137 	case IPV4_DEVCONF_PROMOTE_SECONDARIES:
2138 	case IPV4_DEVCONF_DROP_UNICAST_IN_L2_MULTICAST:
2139 	case IPV4_DEVCONF_BC_FORWARDING:
2140 		return true;
2141 	case IPV4_DEVCONF_RP_FILTER:
2142 		inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2143 					    NETCONFA_RP_FILTER,
2144 					    ifindex, cnf);
2145 		break;
2146 	case IPV4_DEVCONF_PROXY_ARP:
2147 		inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2148 					    NETCONFA_PROXY_NEIGH,
2149 					    ifindex, cnf);
2150 		break;
2151 	case IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN:
2152 		inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2153 					    NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
2154 					    ifindex, cnf);
2155 		break;
2156 	case IPV4_DEVCONF_FORWARDING:
2157 		if (new == 1) {
2158 			/* it is safe to use container_of() because forwarding case
2159 			 * is only used by the netlink path
2160 			 */
2161 			struct in_device *idev = container_of(cnf, struct in_device, cnf);
2162 
2163 			netif_disable_lro(idev->dev);
2164 		}
2165 
2166 		inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2167 					    NETCONFA_FORWARDING,
2168 					    ifindex, cnf);
2169 		return true;
2170 	default:
2171 		break;
2172 	}
2173 
2174 	return false;
2175 }
2176 
2177 static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla,
2178 			    struct netlink_ext_ack *extack)
2179 {
2180 	struct in_device *in_dev = __in_dev_get_rtnl(dev);
2181 	struct nlattr *a, *tb[IFLA_INET_MAX+1];
2182 	struct net *net = dev_net(dev);
2183 	bool flush_cache = false;
2184 	int rem;
2185 
2186 	if (!in_dev)
2187 		return -EAFNOSUPPORT;
2188 
2189 	if (nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla, NULL, NULL) < 0)
2190 		return -EINVAL;
2191 
2192 	if (tb[IFLA_INET_CONF]) {
2193 		nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
2194 			int old_value = ipv4_devconf_get(in_dev, nla_type(a));
2195 			int new_value = nla_get_u32(a);
2196 
2197 			ipv4_devconf_set(in_dev, nla_type(a), new_value);
2198 			if (devinet_conf_post_set(net, &in_dev->cnf, nla_type(a), new_value,
2199 						  old_value, dev->ifindex))
2200 				flush_cache = true;
2201 		}
2202 		if (flush_cache)
2203 			rt_cache_flush(net);
2204 	}
2205 
2206 	return 0;
2207 }
2208 
2209 static int inet_netconf_msgsize_devconf(int type)
2210 {
2211 	int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
2212 		   + nla_total_size(4);	/* NETCONFA_IFINDEX */
2213 	bool all = false;
2214 
2215 	if (type == NETCONFA_ALL)
2216 		all = true;
2217 
2218 	if (all || type == NETCONFA_FORWARDING)
2219 		size += nla_total_size(4);
2220 	if (all || type == NETCONFA_RP_FILTER)
2221 		size += nla_total_size(4);
2222 	if (all || type == NETCONFA_MC_FORWARDING)
2223 		size += nla_total_size(4);
2224 	if (all || type == NETCONFA_BC_FORWARDING)
2225 		size += nla_total_size(4);
2226 	if (all || type == NETCONFA_PROXY_NEIGH)
2227 		size += nla_total_size(4);
2228 	if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
2229 		size += nla_total_size(4);
2230 
2231 	return size;
2232 }
2233 
2234 static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
2235 				     const struct ipv4_devconf *devconf,
2236 				     u32 portid, u32 seq, int event,
2237 				     unsigned int flags, int type)
2238 {
2239 	struct nlmsghdr  *nlh;
2240 	struct netconfmsg *ncm;
2241 	bool all = false;
2242 
2243 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
2244 			flags);
2245 	if (!nlh)
2246 		return -EMSGSIZE;
2247 
2248 	if (type == NETCONFA_ALL)
2249 		all = true;
2250 
2251 	ncm = nlmsg_data(nlh);
2252 	ncm->ncm_family = AF_INET;
2253 
2254 	if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
2255 		goto nla_put_failure;
2256 
2257 	if (!devconf)
2258 		goto out;
2259 
2260 	if ((all || type == NETCONFA_FORWARDING) &&
2261 	    nla_put_s32(skb, NETCONFA_FORWARDING,
2262 			IPV4_DEVCONF_RO(*devconf, FORWARDING)) < 0)
2263 		goto nla_put_failure;
2264 	if ((all || type == NETCONFA_RP_FILTER) &&
2265 	    nla_put_s32(skb, NETCONFA_RP_FILTER,
2266 			IPV4_DEVCONF_RO(*devconf, RP_FILTER)) < 0)
2267 		goto nla_put_failure;
2268 	if ((all || type == NETCONFA_MC_FORWARDING) &&
2269 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
2270 			IPV4_DEVCONF_RO(*devconf, MC_FORWARDING)) < 0)
2271 		goto nla_put_failure;
2272 	if ((all || type == NETCONFA_BC_FORWARDING) &&
2273 	    nla_put_s32(skb, NETCONFA_BC_FORWARDING,
2274 			IPV4_DEVCONF_RO(*devconf, BC_FORWARDING)) < 0)
2275 		goto nla_put_failure;
2276 	if ((all || type == NETCONFA_PROXY_NEIGH) &&
2277 	    nla_put_s32(skb, NETCONFA_PROXY_NEIGH,
2278 			IPV4_DEVCONF_RO(*devconf, PROXY_ARP)) < 0)
2279 		goto nla_put_failure;
2280 	if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
2281 	    nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
2282 			IPV4_DEVCONF_RO(*devconf,
2283 					IGNORE_ROUTES_WITH_LINKDOWN)) < 0)
2284 		goto nla_put_failure;
2285 
2286 out:
2287 	nlmsg_end(skb, nlh);
2288 	return 0;
2289 
2290 nla_put_failure:
2291 	nlmsg_cancel(skb, nlh);
2292 	return -EMSGSIZE;
2293 }
2294 
2295 void inet_netconf_notify_devconf(struct net *net, int event, int type,
2296 				 int ifindex, struct ipv4_devconf *devconf)
2297 {
2298 	struct sk_buff *skb;
2299 	int err = -ENOBUFS;
2300 
2301 	skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_KERNEL);
2302 	if (!skb)
2303 		goto errout;
2304 
2305 	err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
2306 					event, 0, type);
2307 	if (err < 0) {
2308 		/* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
2309 		WARN_ON(err == -EMSGSIZE);
2310 		kfree_skb(skb);
2311 		goto errout;
2312 	}
2313 	rtnl_notify(skb, net, 0, RTNLGRP_IPV4_NETCONF, NULL, GFP_KERNEL);
2314 	return;
2315 errout:
2316 	rtnl_set_sk_err(net, RTNLGRP_IPV4_NETCONF, err);
2317 }
2318 
2319 static const struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = {
2320 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
2321 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
2322 	[NETCONFA_RP_FILTER]	= { .len = sizeof(int) },
2323 	[NETCONFA_PROXY_NEIGH]	= { .len = sizeof(int) },
2324 	[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]	= { .len = sizeof(int) },
2325 };
2326 
2327 static int inet_netconf_valid_get_req(struct sk_buff *skb,
2328 				      const struct nlmsghdr *nlh,
2329 				      struct nlattr **tb,
2330 				      struct netlink_ext_ack *extack)
2331 {
2332 	int i, err;
2333 
2334 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
2335 		NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf get request");
2336 		return -EINVAL;
2337 	}
2338 
2339 	if (!netlink_strict_get_check(skb))
2340 		return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
2341 					      tb, NETCONFA_MAX,
2342 					      devconf_ipv4_policy, extack);
2343 
2344 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
2345 					    tb, NETCONFA_MAX,
2346 					    devconf_ipv4_policy, extack);
2347 	if (err)
2348 		return err;
2349 
2350 	for (i = 0; i <= NETCONFA_MAX; i++) {
2351 		if (!tb[i])
2352 			continue;
2353 
2354 		switch (i) {
2355 		case NETCONFA_IFINDEX:
2356 			break;
2357 		default:
2358 			NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in netconf get request");
2359 			return -EINVAL;
2360 		}
2361 	}
2362 
2363 	return 0;
2364 }
2365 
2366 static int inet_netconf_get_devconf(struct sk_buff *in_skb,
2367 				    struct nlmsghdr *nlh,
2368 				    struct netlink_ext_ack *extack)
2369 {
2370 	struct net *net = sock_net(in_skb->sk);
2371 	struct nlattr *tb[NETCONFA_MAX + 1];
2372 	const struct ipv4_devconf *devconf;
2373 	struct in_device *in_dev = NULL;
2374 	struct net_device *dev = NULL;
2375 	struct sk_buff *skb;
2376 	int ifindex;
2377 	int err;
2378 
2379 	err = inet_netconf_valid_get_req(in_skb, nlh, tb, extack);
2380 	if (err)
2381 		return err;
2382 
2383 	if (!tb[NETCONFA_IFINDEX])
2384 		return -EINVAL;
2385 
2386 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
2387 	switch (ifindex) {
2388 	case NETCONFA_IFINDEX_ALL:
2389 		devconf = net->ipv4.devconf_all;
2390 		break;
2391 	case NETCONFA_IFINDEX_DEFAULT:
2392 		devconf = net->ipv4.devconf_dflt;
2393 		break;
2394 	default:
2395 		err = -ENODEV;
2396 		dev = dev_get_by_index(net, ifindex);
2397 		if (dev)
2398 			in_dev = in_dev_get(dev);
2399 		if (!in_dev)
2400 			goto errout;
2401 		devconf = &in_dev->cnf;
2402 		break;
2403 	}
2404 
2405 	err = -ENOBUFS;
2406 	skb = nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
2407 	if (!skb)
2408 		goto errout;
2409 
2410 	err = inet_netconf_fill_devconf(skb, ifindex, devconf,
2411 					NETLINK_CB(in_skb).portid,
2412 					nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
2413 					NETCONFA_ALL);
2414 	if (err < 0) {
2415 		/* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
2416 		WARN_ON(err == -EMSGSIZE);
2417 		kfree_skb(skb);
2418 		goto errout;
2419 	}
2420 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2421 errout:
2422 	if (in_dev)
2423 		in_dev_put(in_dev);
2424 	dev_put(dev);
2425 	return err;
2426 }
2427 
2428 static int inet_netconf_dump_devconf(struct sk_buff *skb,
2429 				     struct netlink_callback *cb)
2430 {
2431 	const struct nlmsghdr *nlh = cb->nlh;
2432 	struct net *net = sock_net(skb->sk);
2433 	struct {
2434 		unsigned long ifindex;
2435 		unsigned int all_default;
2436 	} *ctx = (void *)cb->ctx;
2437 	const struct in_device *in_dev;
2438 	struct net_device *dev;
2439 	int err = 0;
2440 
2441 	if (cb->strict_check) {
2442 		struct netlink_ext_ack *extack = cb->extack;
2443 		struct netconfmsg *ncm;
2444 
2445 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
2446 			NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf dump request");
2447 			return -EINVAL;
2448 		}
2449 
2450 		if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
2451 			NL_SET_ERR_MSG(extack, "ipv4: Invalid data after header in netconf dump request");
2452 			return -EINVAL;
2453 		}
2454 	}
2455 
2456 	rcu_read_lock();
2457 	for_each_netdev_dump(net, dev, ctx->ifindex) {
2458 		in_dev = __in_dev_get_rcu(dev);
2459 		if (!in_dev)
2460 			continue;
2461 		err = inet_netconf_fill_devconf(skb, dev->ifindex,
2462 						&in_dev->cnf,
2463 						NETLINK_CB(cb->skb).portid,
2464 						nlh->nlmsg_seq,
2465 						RTM_NEWNETCONF, NLM_F_MULTI,
2466 						NETCONFA_ALL);
2467 		if (err < 0)
2468 			goto done;
2469 	}
2470 	if (ctx->all_default == 0) {
2471 		err = inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
2472 						net->ipv4.devconf_all,
2473 						NETLINK_CB(cb->skb).portid,
2474 						nlh->nlmsg_seq,
2475 						RTM_NEWNETCONF, NLM_F_MULTI,
2476 						NETCONFA_ALL);
2477 		if (err < 0)
2478 			goto done;
2479 		ctx->all_default++;
2480 	}
2481 	if (ctx->all_default == 1) {
2482 		err = inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
2483 						net->ipv4.devconf_dflt,
2484 						NETLINK_CB(cb->skb).portid,
2485 						nlh->nlmsg_seq,
2486 						RTM_NEWNETCONF, NLM_F_MULTI,
2487 						NETCONFA_ALL);
2488 		if (err < 0)
2489 			goto done;
2490 		ctx->all_default++;
2491 	}
2492 done:
2493 	rcu_read_unlock();
2494 	return err;
2495 }
2496 
2497 #ifdef CONFIG_SYSCTL
2498 
2499 static void devinet_copy_dflt_conf(struct net *net, int i)
2500 {
2501 	struct net_device *dev;
2502 
2503 	rcu_read_lock();
2504 	for_each_netdev_rcu(net, dev) {
2505 		struct in_device *in_dev;
2506 
2507 		in_dev = __in_dev_get_rcu(dev);
2508 		if (in_dev && !test_bit(i, in_dev->cnf.state))
2509 			in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
2510 	}
2511 	rcu_read_unlock();
2512 }
2513 
2514 /* called with RTNL locked */
2515 static void inet_forward_change(struct net *net)
2516 {
2517 	struct net_device *dev;
2518 	int on = IPV4_DEVCONF_ALL(net, FORWARDING);
2519 
2520 	IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
2521 	IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
2522 	inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2523 				    NETCONFA_FORWARDING,
2524 				    NETCONFA_IFINDEX_ALL,
2525 				    net->ipv4.devconf_all);
2526 	inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2527 				    NETCONFA_FORWARDING,
2528 				    NETCONFA_IFINDEX_DEFAULT,
2529 				    net->ipv4.devconf_dflt);
2530 
2531 	for_each_netdev(net, dev) {
2532 		struct in_device *in_dev;
2533 
2534 		if (on)
2535 			dev_disable_lro(dev);
2536 
2537 		in_dev = __in_dev_get_rtnl_net(dev);
2538 		if (in_dev) {
2539 			IN_DEV_CONF_SET(in_dev, FORWARDING, on);
2540 			inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2541 						    NETCONFA_FORWARDING,
2542 						    dev->ifindex, &in_dev->cnf);
2543 		}
2544 	}
2545 }
2546 
2547 static int devinet_conf_ifindex(struct net *net, struct ipv4_devconf *cnf)
2548 {
2549 	if (cnf == net->ipv4.devconf_dflt)
2550 		return NETCONFA_IFINDEX_DEFAULT;
2551 	else if (cnf == net->ipv4.devconf_all)
2552 		return NETCONFA_IFINDEX_ALL;
2553 	else {
2554 		struct in_device *idev
2555 			= container_of(cnf, struct in_device, cnf);
2556 		return idev->dev->ifindex;
2557 	}
2558 }
2559 
2560 static int devinet_conf_proc(const struct ctl_table *ctl, int write,
2561 			     void *buffer, size_t *lenp, loff_t *ppos)
2562 {
2563 	int old_value = *(int *)ctl->data;
2564 	int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2565 	int new_value = *(int *)ctl->data;
2566 
2567 	if (write) {
2568 		struct ipv4_devconf *cnf = ctl->extra1;
2569 		int i = (int *)ctl->data - cnf->data;
2570 		struct net *net = ctl->extra2;
2571 		int ifindex;
2572 
2573 		/* These attributes are bypassing the tracking state,
2574 		 * for the rest track the state and propagate the changes
2575 		 * to default config
2576 		 */
2577 		switch (i + 1) {
2578 		case IPV4_DEVCONF_NOXFRM:
2579 		case IPV4_DEVCONF_NOPOLICY:
2580 		case IPV4_DEVCONF_PROMOTE_SECONDARIES:
2581 		case IPV4_DEVCONF_DROP_UNICAST_IN_L2_MULTICAST:
2582 			break;
2583 		default:
2584 			set_bit(i, cnf->state);
2585 			if (cnf == net->ipv4.devconf_dflt)
2586 				devinet_copy_dflt_conf(net, i);
2587 			break;
2588 		}
2589 
2590 		ifindex = devinet_conf_ifindex(net, cnf);
2591 		if (devinet_conf_post_set(net, cnf, i + 1, new_value,
2592 					  old_value, ifindex))
2593 			rt_cache_flush(net);
2594 	}
2595 
2596 	return ret;
2597 }
2598 
2599 static int devinet_sysctl_forward(const struct ctl_table *ctl, int write,
2600 				  void *buffer, size_t *lenp, loff_t *ppos)
2601 {
2602 	struct net *net = ctl->extra2;
2603 	int *valp = ctl->data;
2604 	int val = *valp;
2605 	int ret;
2606 
2607 	if (write && !ns_capable(net->user_ns, CAP_NET_ADMIN))
2608 		return -EPERM;
2609 
2610 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
2611 
2612 	if (write && *valp != val) {
2613 		if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
2614 			if (!rtnl_net_trylock(net)) {
2615 				/* Restore the original values before restarting */
2616 				*valp = val;
2617 				return restart_syscall();
2618 			}
2619 			if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
2620 				inet_forward_change(net);
2621 			} else {
2622 				struct ipv4_devconf *cnf = ctl->extra1;
2623 				struct in_device *idev =
2624 					container_of(cnf, struct in_device, cnf);
2625 				if (*valp)
2626 					dev_disable_lro(idev->dev);
2627 				inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2628 							    NETCONFA_FORWARDING,
2629 							    idev->dev->ifindex,
2630 							    cnf);
2631 			}
2632 			rtnl_net_unlock(net);
2633 			rt_cache_flush(net);
2634 		} else
2635 			inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2636 						    NETCONFA_FORWARDING,
2637 						    NETCONFA_IFINDEX_DEFAULT,
2638 						    net->ipv4.devconf_dflt);
2639 	}
2640 
2641 	return ret;
2642 }
2643 
2644 #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
2645 	{ \
2646 		.procname	= name, \
2647 		.data		= ipv4_devconf.data + \
2648 				  IPV4_DEVCONF_ ## attr - 1, \
2649 		.maxlen		= sizeof(int), \
2650 		.mode		= mval, \
2651 		.proc_handler	= proc, \
2652 		.extra1		= &ipv4_devconf, \
2653 	}
2654 
2655 #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
2656 	DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
2657 
2658 #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
2659 	DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
2660 
2661 #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
2662 	DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
2663 
2664 static struct devinet_sysctl_table {
2665 	struct ctl_table_header *sysctl_header;
2666 	struct ctl_table devinet_vars[IPV4_DEVCONF_MAX];
2667 } devinet_sysctl = {
2668 	.devinet_vars = {
2669 		DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
2670 					     devinet_sysctl_forward),
2671 		DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
2672 		DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING, "bc_forwarding"),
2673 
2674 		DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
2675 		DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
2676 		DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
2677 		DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
2678 		DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
2679 		DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
2680 					"accept_source_route"),
2681 		DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
2682 		DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
2683 		DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
2684 		DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
2685 		DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
2686 		DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
2687 		DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
2688 		DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
2689 		DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
2690 		DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
2691 		DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
2692 		DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
2693 		DEVINET_SYSCTL_RW_ENTRY(ARP_EVICT_NOCARRIER,
2694 					"arp_evict_nocarrier"),
2695 		DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
2696 		DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION,
2697 					"force_igmp_version"),
2698 		DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL,
2699 					"igmpv2_unsolicited_report_interval"),
2700 		DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL,
2701 					"igmpv3_unsolicited_report_interval"),
2702 		DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN,
2703 					"ignore_routes_with_linkdown"),
2704 		DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP,
2705 					"drop_gratuitous_arp"),
2706 		DEVINET_SYSCTL_RW_ENTRY(NOXFRM, "disable_xfrm"),
2707 		DEVINET_SYSCTL_RW_ENTRY(NOPOLICY, "disable_policy"),
2708 		DEVINET_SYSCTL_RW_ENTRY(PROMOTE_SECONDARIES,
2709 					"promote_secondaries"),
2710 		DEVINET_SYSCTL_RW_ENTRY(ROUTE_LOCALNET,
2711 					"route_localnet"),
2712 		DEVINET_SYSCTL_RW_ENTRY(DROP_UNICAST_IN_L2_MULTICAST,
2713 					"drop_unicast_in_l2_multicast"),
2714 	},
2715 };
2716 
2717 static int __devinet_sysctl_register(struct net *net, char *dev_name,
2718 				     int ifindex, struct ipv4_devconf *p)
2719 {
2720 	int i;
2721 	struct devinet_sysctl_table *t;
2722 	char path[sizeof("net/ipv4/conf/") + IFNAMSIZ];
2723 
2724 	t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL_ACCOUNT);
2725 	if (!t)
2726 		goto out;
2727 
2728 	for (i = 0; i < ARRAY_SIZE(t->devinet_vars); i++) {
2729 		t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
2730 		t->devinet_vars[i].extra1 = p;
2731 		t->devinet_vars[i].extra2 = net;
2732 	}
2733 
2734 	snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name);
2735 
2736 	t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars);
2737 	if (!t->sysctl_header)
2738 		goto free;
2739 
2740 	p->sysctl = t;
2741 
2742 	inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
2743 				    ifindex, p);
2744 	return 0;
2745 
2746 free:
2747 	kfree(t);
2748 out:
2749 	return -ENOMEM;
2750 }
2751 
2752 static void __devinet_sysctl_unregister(struct net *net,
2753 					struct ipv4_devconf *cnf, int ifindex)
2754 {
2755 	struct devinet_sysctl_table *t = cnf->sysctl;
2756 
2757 	if (t) {
2758 		cnf->sysctl = NULL;
2759 		unregister_net_sysctl_table(t->sysctl_header);
2760 		kfree(t);
2761 	}
2762 
2763 	inet_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
2764 }
2765 
2766 static int devinet_sysctl_register(struct in_device *idev)
2767 {
2768 	int err;
2769 
2770 	if (!sysctl_dev_name_is_allowed(idev->dev->name))
2771 		return -EINVAL;
2772 
2773 	err = neigh_sysctl_register(idev->dev, idev->arp_parms, NULL);
2774 	if (err)
2775 		return err;
2776 	err = __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
2777 					idev->dev->ifindex, &idev->cnf);
2778 	if (err)
2779 		neigh_sysctl_unregister(idev->arp_parms);
2780 	return err;
2781 }
2782 
2783 static void devinet_sysctl_unregister(struct in_device *idev)
2784 {
2785 	struct net *net = dev_net(idev->dev);
2786 
2787 	__devinet_sysctl_unregister(net, &idev->cnf, idev->dev->ifindex);
2788 	neigh_sysctl_unregister(idev->arp_parms);
2789 }
2790 
2791 static const struct ctl_table ctl_forward_entry[] = {
2792 	{
2793 		.procname	= "ip_forward",
2794 		.data		= &ipv4_devconf.data[
2795 					IPV4_DEVCONF_FORWARDING - 1],
2796 		.maxlen		= sizeof(int),
2797 		.mode		= 0644,
2798 		.proc_handler	= devinet_sysctl_forward,
2799 		.extra1		= &ipv4_devconf,
2800 		.extra2		= &init_net,
2801 	},
2802 };
2803 #endif
2804 
2805 static __net_init int devinet_init_net(struct net *net)
2806 {
2807 #ifdef CONFIG_SYSCTL
2808 	struct ctl_table_header *forw_hdr;
2809 	struct ctl_table *tbl;
2810 #endif
2811 	struct ipv4_devconf *all, *dflt;
2812 	int err;
2813 	int i;
2814 
2815 	err = -ENOMEM;
2816 	net->ipv4.inet_addr_lst = kmalloc_objs(struct hlist_head,
2817 					       IN4_ADDR_HSIZE);
2818 	if (!net->ipv4.inet_addr_lst)
2819 		goto err_alloc_hash;
2820 
2821 	all = kmemdup(&ipv4_devconf, sizeof(ipv4_devconf), GFP_KERNEL);
2822 	if (!all)
2823 		goto err_alloc_all;
2824 
2825 	dflt = kmemdup(&ipv4_devconf_dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
2826 	if (!dflt)
2827 		goto err_alloc_dflt;
2828 
2829 #ifdef CONFIG_SYSCTL
2830 	tbl = kmemdup(ctl_forward_entry, sizeof(ctl_forward_entry), GFP_KERNEL);
2831 	if (!tbl)
2832 		goto err_alloc_ctl;
2833 
2834 	tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
2835 	tbl[0].extra1 = all;
2836 	tbl[0].extra2 = net;
2837 #endif
2838 
2839 	if (!net_eq(net, &init_net)) {
2840 		switch (net_inherit_devconf()) {
2841 		case 3:
2842 			/* copy from the current netns */
2843 			memcpy(all, current->nsproxy->net_ns->ipv4.devconf_all,
2844 			       sizeof(ipv4_devconf));
2845 			memcpy(dflt,
2846 			       current->nsproxy->net_ns->ipv4.devconf_dflt,
2847 			       sizeof(ipv4_devconf_dflt));
2848 			break;
2849 		case 0:
2850 		case 1:
2851 			/* copy from init_net */
2852 			memcpy(all, init_net.ipv4.devconf_all,
2853 			       sizeof(ipv4_devconf));
2854 			memcpy(dflt, init_net.ipv4.devconf_dflt,
2855 			       sizeof(ipv4_devconf_dflt));
2856 			break;
2857 		case 2:
2858 			/* use compiled values */
2859 			break;
2860 		}
2861 	}
2862 
2863 #ifdef CONFIG_SYSCTL
2864 	err = __devinet_sysctl_register(net, "all", NETCONFA_IFINDEX_ALL, all);
2865 	if (err < 0)
2866 		goto err_reg_all;
2867 
2868 	err = __devinet_sysctl_register(net, "default",
2869 					NETCONFA_IFINDEX_DEFAULT, dflt);
2870 	if (err < 0)
2871 		goto err_reg_dflt;
2872 
2873 	err = -ENOMEM;
2874 	forw_hdr = register_net_sysctl_sz(net, "net/ipv4", tbl,
2875 					  ARRAY_SIZE(ctl_forward_entry));
2876 	if (!forw_hdr)
2877 		goto err_reg_ctl;
2878 	net->ipv4.forw_hdr = forw_hdr;
2879 #endif
2880 
2881 	for (i = 0; i < IN4_ADDR_HSIZE; i++)
2882 		INIT_HLIST_HEAD(&net->ipv4.inet_addr_lst[i]);
2883 
2884 	INIT_DEFERRABLE_WORK(&net->ipv4.addr_chk_work, check_lifetime);
2885 
2886 	net->ipv4.devconf_all = all;
2887 	net->ipv4.devconf_dflt = dflt;
2888 	return 0;
2889 
2890 #ifdef CONFIG_SYSCTL
2891 err_reg_ctl:
2892 	__devinet_sysctl_unregister(net, dflt, NETCONFA_IFINDEX_DEFAULT);
2893 err_reg_dflt:
2894 	__devinet_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
2895 err_reg_all:
2896 	kfree(tbl);
2897 err_alloc_ctl:
2898 #endif
2899 	kfree(dflt);
2900 err_alloc_dflt:
2901 	kfree(all);
2902 err_alloc_all:
2903 	kfree(net->ipv4.inet_addr_lst);
2904 err_alloc_hash:
2905 	return err;
2906 }
2907 
2908 static __net_exit void devinet_exit_net(struct net *net)
2909 {
2910 #ifdef CONFIG_SYSCTL
2911 	const struct ctl_table *tbl;
2912 #endif
2913 
2914 	cancel_delayed_work_sync(&net->ipv4.addr_chk_work);
2915 
2916 #ifdef CONFIG_SYSCTL
2917 	tbl = net->ipv4.forw_hdr->ctl_table_arg;
2918 	unregister_net_sysctl_table(net->ipv4.forw_hdr);
2919 	__devinet_sysctl_unregister(net, net->ipv4.devconf_dflt,
2920 				    NETCONFA_IFINDEX_DEFAULT);
2921 	__devinet_sysctl_unregister(net, net->ipv4.devconf_all,
2922 				    NETCONFA_IFINDEX_ALL);
2923 	kfree(tbl);
2924 #endif
2925 	kfree(net->ipv4.devconf_dflt);
2926 	kfree(net->ipv4.devconf_all);
2927 	kfree(net->ipv4.inet_addr_lst);
2928 }
2929 
2930 static __net_initdata struct pernet_operations devinet_ops = {
2931 	.init = devinet_init_net,
2932 	.exit = devinet_exit_net,
2933 };
2934 
2935 static struct rtnl_af_ops inet_af_ops __read_mostly = {
2936 	.family		  = AF_INET,
2937 	.fill_link_af	  = inet_fill_link_af,
2938 	.get_link_af_size = inet_get_link_af_size,
2939 	.validate_link_af = inet_validate_link_af,
2940 	.set_link_af	  = inet_set_link_af,
2941 };
2942 
2943 static const struct rtnl_msg_handler devinet_rtnl_msg_handlers[] __initconst = {
2944 	{.protocol = PF_INET, .msgtype = RTM_NEWADDR, .doit = inet_rtm_newaddr,
2945 	 .flags = RTNL_FLAG_DOIT_PERNET},
2946 	{.protocol = PF_INET, .msgtype = RTM_DELADDR, .doit = inet_rtm_deladdr,
2947 	 .flags = RTNL_FLAG_DOIT_PERNET},
2948 	{.protocol = PF_INET, .msgtype = RTM_GETADDR, .dumpit = inet_dump_ifaddr,
2949 	 .flags = RTNL_FLAG_DUMP_UNLOCKED | RTNL_FLAG_DUMP_SPLIT_NLM_DONE},
2950 	{.protocol = PF_INET, .msgtype = RTM_GETNETCONF,
2951 	 .doit = inet_netconf_get_devconf, .dumpit = inet_netconf_dump_devconf,
2952 	 .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
2953 	{.owner = THIS_MODULE, .protocol = PF_INET, .msgtype = RTM_GETMULTICAST,
2954 	 .dumpit = inet_dump_ifmcaddr, .flags = RTNL_FLAG_DUMP_UNLOCKED},
2955 };
2956 
2957 void __init devinet_init(void)
2958 {
2959 	register_pernet_subsys(&devinet_ops);
2960 	register_netdevice_notifier(&ip_netdev_notifier);
2961 
2962 	if (rtnl_af_register(&inet_af_ops))
2963 		panic("Unable to register inet_af_ops\n");
2964 
2965 	rtnl_register_many(devinet_rtnl_msg_handlers);
2966 
2967 	rtnl_lock();
2968 	if (IS_ERR(inetdev_init(blackhole_netdev)))
2969 		panic("IP: failed to allocate in_device for blackhole_netdev\n");
2970 	rtnl_unlock();
2971 }
2972