xref: /linux/net/ipv4/devinet.c (revision 9ce7677cfd7cd871adb457c80bea3b581b839641)
1 /*
2  *	NET3	IP device support routines.
3  *
4  *	Version: $Id: devinet.c,v 1.44 2001/10/31 21:55:54 davem Exp $
5  *
6  *		This program is free software; you can redistribute it and/or
7  *		modify it under the terms of the GNU General Public License
8  *		as published by the Free Software Foundation; either version
9  *		2 of the License, or (at your option) any later version.
10  *
11  *	Derived from the IP parts of dev.c 1.0.19
12  * 		Authors:	Ross Biro
13  *				Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
14  *				Mark Evans, <evansmp@uhura.aston.ac.uk>
15  *
16  *	Additional Authors:
17  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
18  *		Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
19  *
20  *	Changes:
21  *		Alexey Kuznetsov:	pa_* fields are replaced with ifaddr
22  *					lists.
23  *		Cyrus Durgin:		updated for kmod
24  *		Matthias Andree:	in devinet_ioctl, compare label and
25  *					address (4.4BSD alias style support),
26  *					fall back to comparing just the label
27  *					if no match found.
28  */
29 
30 #include <linux/config.h>
31 
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include <linux/bitops.h>
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/string.h>
40 #include <linux/mm.h>
41 #include <linux/socket.h>
42 #include <linux/sockios.h>
43 #include <linux/in.h>
44 #include <linux/errno.h>
45 #include <linux/interrupt.h>
46 #include <linux/if_ether.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/skbuff.h>
51 #include <linux/rtnetlink.h>
52 #include <linux/init.h>
53 #include <linux/notifier.h>
54 #include <linux/inetdevice.h>
55 #include <linux/igmp.h>
56 #ifdef CONFIG_SYSCTL
57 #include <linux/sysctl.h>
58 #endif
59 #include <linux/kmod.h>
60 
61 #include <net/ip.h>
62 #include <net/route.h>
63 #include <net/ip_fib.h>
64 
65 struct ipv4_devconf ipv4_devconf = {
66 	.accept_redirects = 1,
67 	.send_redirects =  1,
68 	.secure_redirects = 1,
69 	.shared_media =	  1,
70 };
71 
72 static struct ipv4_devconf ipv4_devconf_dflt = {
73 	.accept_redirects =  1,
74 	.send_redirects =    1,
75 	.secure_redirects =  1,
76 	.shared_media =	     1,
77 	.accept_source_route = 1,
78 };
79 
80 static void rtmsg_ifa(int event, struct in_ifaddr *);
81 
82 static struct notifier_block *inetaddr_chain;
83 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
84 			 int destroy);
85 #ifdef CONFIG_SYSCTL
86 static void devinet_sysctl_register(struct in_device *in_dev,
87 				    struct ipv4_devconf *p);
88 static void devinet_sysctl_unregister(struct ipv4_devconf *p);
89 #endif
90 
91 /* Locks all the inet devices. */
92 
93 static struct in_ifaddr *inet_alloc_ifa(void)
94 {
95 	struct in_ifaddr *ifa = kmalloc(sizeof(*ifa), GFP_KERNEL);
96 
97 	if (ifa) {
98 		memset(ifa, 0, sizeof(*ifa));
99 		INIT_RCU_HEAD(&ifa->rcu_head);
100 	}
101 
102 	return ifa;
103 }
104 
105 static void inet_rcu_free_ifa(struct rcu_head *head)
106 {
107 	struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
108 	if (ifa->ifa_dev)
109 		in_dev_put(ifa->ifa_dev);
110 	kfree(ifa);
111 }
112 
113 static inline void inet_free_ifa(struct in_ifaddr *ifa)
114 {
115 	call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
116 }
117 
118 void in_dev_finish_destroy(struct in_device *idev)
119 {
120 	struct net_device *dev = idev->dev;
121 
122 	BUG_TRAP(!idev->ifa_list);
123 	BUG_TRAP(!idev->mc_list);
124 #ifdef NET_REFCNT_DEBUG
125 	printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n",
126 	       idev, dev ? dev->name : "NIL");
127 #endif
128 	dev_put(dev);
129 	if (!idev->dead)
130 		printk("Freeing alive in_device %p\n", idev);
131 	else {
132 		kfree(idev);
133 	}
134 }
135 
136 struct in_device *inetdev_init(struct net_device *dev)
137 {
138 	struct in_device *in_dev;
139 
140 	ASSERT_RTNL();
141 
142 	in_dev = kmalloc(sizeof(*in_dev), GFP_KERNEL);
143 	if (!in_dev)
144 		goto out;
145 	memset(in_dev, 0, sizeof(*in_dev));
146 	INIT_RCU_HEAD(&in_dev->rcu_head);
147 	memcpy(&in_dev->cnf, &ipv4_devconf_dflt, sizeof(in_dev->cnf));
148 	in_dev->cnf.sysctl = NULL;
149 	in_dev->dev = dev;
150 	if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL)
151 		goto out_kfree;
152 	/* Reference in_dev->dev */
153 	dev_hold(dev);
154 #ifdef CONFIG_SYSCTL
155 	neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
156 			      NET_IPV4_NEIGH, "ipv4", NULL, NULL);
157 #endif
158 
159 	/* Account for reference dev->ip_ptr */
160 	in_dev_hold(in_dev);
161 	rcu_assign_pointer(dev->ip_ptr, in_dev);
162 
163 #ifdef CONFIG_SYSCTL
164 	devinet_sysctl_register(in_dev, &in_dev->cnf);
165 #endif
166 	ip_mc_init_dev(in_dev);
167 	if (dev->flags & IFF_UP)
168 		ip_mc_up(in_dev);
169 out:
170 	return in_dev;
171 out_kfree:
172 	kfree(in_dev);
173 	in_dev = NULL;
174 	goto out;
175 }
176 
177 static void in_dev_rcu_put(struct rcu_head *head)
178 {
179 	struct in_device *idev = container_of(head, struct in_device, rcu_head);
180 	in_dev_put(idev);
181 }
182 
183 static void inetdev_destroy(struct in_device *in_dev)
184 {
185 	struct in_ifaddr *ifa;
186 	struct net_device *dev;
187 
188 	ASSERT_RTNL();
189 
190 	dev = in_dev->dev;
191 	if (dev == &loopback_dev)
192 		return;
193 
194 	in_dev->dead = 1;
195 
196 	ip_mc_destroy_dev(in_dev);
197 
198 	while ((ifa = in_dev->ifa_list) != NULL) {
199 		inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
200 		inet_free_ifa(ifa);
201 	}
202 
203 #ifdef CONFIG_SYSCTL
204 	devinet_sysctl_unregister(&in_dev->cnf);
205 #endif
206 
207 	dev->ip_ptr = NULL;
208 
209 #ifdef CONFIG_SYSCTL
210 	neigh_sysctl_unregister(in_dev->arp_parms);
211 #endif
212 	neigh_parms_release(&arp_tbl, in_dev->arp_parms);
213 	arp_ifdown(dev);
214 
215 	call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
216 }
217 
218 int inet_addr_onlink(struct in_device *in_dev, u32 a, u32 b)
219 {
220 	rcu_read_lock();
221 	for_primary_ifa(in_dev) {
222 		if (inet_ifa_match(a, ifa)) {
223 			if (!b || inet_ifa_match(b, ifa)) {
224 				rcu_read_unlock();
225 				return 1;
226 			}
227 		}
228 	} endfor_ifa(in_dev);
229 	rcu_read_unlock();
230 	return 0;
231 }
232 
233 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
234 			 int destroy)
235 {
236 	struct in_ifaddr *promote = NULL;
237 	struct in_ifaddr *ifa, *ifa1 = *ifap;
238 	struct in_ifaddr *last_prim = in_dev->ifa_list;
239 	struct in_ifaddr *prev_prom = NULL;
240 	int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
241 
242 	ASSERT_RTNL();
243 
244 	/* 1. Deleting primary ifaddr forces deletion all secondaries
245 	 * unless alias promotion is set
246 	 **/
247 
248 	if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
249 		struct in_ifaddr **ifap1 = &ifa1->ifa_next;
250 
251 		while ((ifa = *ifap1) != NULL) {
252 			if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
253 			    ifa1->ifa_scope <= ifa->ifa_scope)
254 				last_prim = ifa;
255 
256 			if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
257 			    ifa1->ifa_mask != ifa->ifa_mask ||
258 			    !inet_ifa_match(ifa1->ifa_address, ifa)) {
259 				ifap1 = &ifa->ifa_next;
260 				prev_prom = ifa;
261 				continue;
262 			}
263 
264 			if (!do_promote) {
265 				*ifap1 = ifa->ifa_next;
266 
267 				rtmsg_ifa(RTM_DELADDR, ifa);
268 				notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa);
269 				inet_free_ifa(ifa);
270 			} else {
271 				promote = ifa;
272 				break;
273 			}
274 		}
275 	}
276 
277 	/* 2. Unlink it */
278 
279 	*ifap = ifa1->ifa_next;
280 
281 	/* 3. Announce address deletion */
282 
283 	/* Send message first, then call notifier.
284 	   At first sight, FIB update triggered by notifier
285 	   will refer to already deleted ifaddr, that could confuse
286 	   netlink listeners. It is not true: look, gated sees
287 	   that route deleted and if it still thinks that ifaddr
288 	   is valid, it will try to restore deleted routes... Grr.
289 	   So that, this order is correct.
290 	 */
291 	rtmsg_ifa(RTM_DELADDR, ifa1);
292 	notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
293 
294 	if (promote) {
295 
296 		if (prev_prom) {
297 			prev_prom->ifa_next = promote->ifa_next;
298 			promote->ifa_next = last_prim->ifa_next;
299 			last_prim->ifa_next = promote;
300 		}
301 
302 		promote->ifa_flags &= ~IFA_F_SECONDARY;
303 		rtmsg_ifa(RTM_NEWADDR, promote);
304 		notifier_call_chain(&inetaddr_chain, NETDEV_UP, promote);
305 		for (ifa = promote->ifa_next; ifa; ifa = ifa->ifa_next) {
306 			if (ifa1->ifa_mask != ifa->ifa_mask ||
307 			    !inet_ifa_match(ifa1->ifa_address, ifa))
308 					continue;
309 			fib_add_ifaddr(ifa);
310 		}
311 
312 	}
313 	if (destroy) {
314 		inet_free_ifa(ifa1);
315 
316 		if (!in_dev->ifa_list)
317 			inetdev_destroy(in_dev);
318 	}
319 }
320 
321 static int inet_insert_ifa(struct in_ifaddr *ifa)
322 {
323 	struct in_device *in_dev = ifa->ifa_dev;
324 	struct in_ifaddr *ifa1, **ifap, **last_primary;
325 
326 	ASSERT_RTNL();
327 
328 	if (!ifa->ifa_local) {
329 		inet_free_ifa(ifa);
330 		return 0;
331 	}
332 
333 	ifa->ifa_flags &= ~IFA_F_SECONDARY;
334 	last_primary = &in_dev->ifa_list;
335 
336 	for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
337 	     ifap = &ifa1->ifa_next) {
338 		if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
339 		    ifa->ifa_scope <= ifa1->ifa_scope)
340 			last_primary = &ifa1->ifa_next;
341 		if (ifa1->ifa_mask == ifa->ifa_mask &&
342 		    inet_ifa_match(ifa1->ifa_address, ifa)) {
343 			if (ifa1->ifa_local == ifa->ifa_local) {
344 				inet_free_ifa(ifa);
345 				return -EEXIST;
346 			}
347 			if (ifa1->ifa_scope != ifa->ifa_scope) {
348 				inet_free_ifa(ifa);
349 				return -EINVAL;
350 			}
351 			ifa->ifa_flags |= IFA_F_SECONDARY;
352 		}
353 	}
354 
355 	if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
356 		net_srandom(ifa->ifa_local);
357 		ifap = last_primary;
358 	}
359 
360 	ifa->ifa_next = *ifap;
361 	*ifap = ifa;
362 
363 	/* Send message first, then call notifier.
364 	   Notifier will trigger FIB update, so that
365 	   listeners of netlink will know about new ifaddr */
366 	rtmsg_ifa(RTM_NEWADDR, ifa);
367 	notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
368 
369 	return 0;
370 }
371 
372 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
373 {
374 	struct in_device *in_dev = __in_dev_get_rtnl(dev);
375 
376 	ASSERT_RTNL();
377 
378 	if (!in_dev) {
379 		in_dev = inetdev_init(dev);
380 		if (!in_dev) {
381 			inet_free_ifa(ifa);
382 			return -ENOBUFS;
383 		}
384 	}
385 	if (ifa->ifa_dev != in_dev) {
386 		BUG_TRAP(!ifa->ifa_dev);
387 		in_dev_hold(in_dev);
388 		ifa->ifa_dev = in_dev;
389 	}
390 	if (LOOPBACK(ifa->ifa_local))
391 		ifa->ifa_scope = RT_SCOPE_HOST;
392 	return inet_insert_ifa(ifa);
393 }
394 
395 struct in_device *inetdev_by_index(int ifindex)
396 {
397 	struct net_device *dev;
398 	struct in_device *in_dev = NULL;
399 	read_lock(&dev_base_lock);
400 	dev = __dev_get_by_index(ifindex);
401 	if (dev)
402 		in_dev = in_dev_get(dev);
403 	read_unlock(&dev_base_lock);
404 	return in_dev;
405 }
406 
407 /* Called only from RTNL semaphored context. No locks. */
408 
409 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, u32 prefix,
410 				    u32 mask)
411 {
412 	ASSERT_RTNL();
413 
414 	for_primary_ifa(in_dev) {
415 		if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
416 			return ifa;
417 	} endfor_ifa(in_dev);
418 	return NULL;
419 }
420 
421 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
422 {
423 	struct rtattr **rta = arg;
424 	struct in_device *in_dev;
425 	struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
426 	struct in_ifaddr *ifa, **ifap;
427 
428 	ASSERT_RTNL();
429 
430 	if ((in_dev = inetdev_by_index(ifm->ifa_index)) == NULL)
431 		goto out;
432 	__in_dev_put(in_dev);
433 
434 	for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
435 	     ifap = &ifa->ifa_next) {
436 		if ((rta[IFA_LOCAL - 1] &&
437 		     memcmp(RTA_DATA(rta[IFA_LOCAL - 1]),
438 			    &ifa->ifa_local, 4)) ||
439 		    (rta[IFA_LABEL - 1] &&
440 		     rtattr_strcmp(rta[IFA_LABEL - 1], ifa->ifa_label)) ||
441 		    (rta[IFA_ADDRESS - 1] &&
442 		     (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
443 		      !inet_ifa_match(*(u32*)RTA_DATA(rta[IFA_ADDRESS - 1]),
444 			      	      ifa))))
445 			continue;
446 		inet_del_ifa(in_dev, ifap, 1);
447 		return 0;
448 	}
449 out:
450 	return -EADDRNOTAVAIL;
451 }
452 
453 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
454 {
455 	struct rtattr **rta = arg;
456 	struct net_device *dev;
457 	struct in_device *in_dev;
458 	struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
459 	struct in_ifaddr *ifa;
460 	int rc = -EINVAL;
461 
462 	ASSERT_RTNL();
463 
464 	if (ifm->ifa_prefixlen > 32 || !rta[IFA_LOCAL - 1])
465 		goto out;
466 
467 	rc = -ENODEV;
468 	if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
469 		goto out;
470 
471 	rc = -ENOBUFS;
472 	if ((in_dev = __in_dev_get_rtnl(dev)) == NULL) {
473 		in_dev = inetdev_init(dev);
474 		if (!in_dev)
475 			goto out;
476 	}
477 
478 	if ((ifa = inet_alloc_ifa()) == NULL)
479 		goto out;
480 
481 	if (!rta[IFA_ADDRESS - 1])
482 		rta[IFA_ADDRESS - 1] = rta[IFA_LOCAL - 1];
483 	memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL - 1]), 4);
484 	memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS - 1]), 4);
485 	ifa->ifa_prefixlen = ifm->ifa_prefixlen;
486 	ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
487 	if (rta[IFA_BROADCAST - 1])
488 		memcpy(&ifa->ifa_broadcast,
489 		       RTA_DATA(rta[IFA_BROADCAST - 1]), 4);
490 	if (rta[IFA_ANYCAST - 1])
491 		memcpy(&ifa->ifa_anycast, RTA_DATA(rta[IFA_ANYCAST - 1]), 4);
492 	ifa->ifa_flags = ifm->ifa_flags;
493 	ifa->ifa_scope = ifm->ifa_scope;
494 	in_dev_hold(in_dev);
495 	ifa->ifa_dev   = in_dev;
496 	if (rta[IFA_LABEL - 1])
497 		rtattr_strlcpy(ifa->ifa_label, rta[IFA_LABEL - 1], IFNAMSIZ);
498 	else
499 		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
500 
501 	rc = inet_insert_ifa(ifa);
502 out:
503 	return rc;
504 }
505 
506 /*
507  *	Determine a default network mask, based on the IP address.
508  */
509 
510 static __inline__ int inet_abc_len(u32 addr)
511 {
512 	int rc = -1;	/* Something else, probably a multicast. */
513 
514   	if (ZERONET(addr))
515   		rc = 0;
516 	else {
517 		addr = ntohl(addr);
518 
519 		if (IN_CLASSA(addr))
520 			rc = 8;
521 		else if (IN_CLASSB(addr))
522 			rc = 16;
523 		else if (IN_CLASSC(addr))
524 			rc = 24;
525 	}
526 
527   	return rc;
528 }
529 
530 
531 int devinet_ioctl(unsigned int cmd, void __user *arg)
532 {
533 	struct ifreq ifr;
534 	struct sockaddr_in sin_orig;
535 	struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
536 	struct in_device *in_dev;
537 	struct in_ifaddr **ifap = NULL;
538 	struct in_ifaddr *ifa = NULL;
539 	struct net_device *dev;
540 	char *colon;
541 	int ret = -EFAULT;
542 	int tryaddrmatch = 0;
543 
544 	/*
545 	 *	Fetch the caller's info block into kernel space
546 	 */
547 
548 	if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
549 		goto out;
550 	ifr.ifr_name[IFNAMSIZ - 1] = 0;
551 
552 	/* save original address for comparison */
553 	memcpy(&sin_orig, sin, sizeof(*sin));
554 
555 	colon = strchr(ifr.ifr_name, ':');
556 	if (colon)
557 		*colon = 0;
558 
559 #ifdef CONFIG_KMOD
560 	dev_load(ifr.ifr_name);
561 #endif
562 
563 	switch(cmd) {
564 	case SIOCGIFADDR:	/* Get interface address */
565 	case SIOCGIFBRDADDR:	/* Get the broadcast address */
566 	case SIOCGIFDSTADDR:	/* Get the destination address */
567 	case SIOCGIFNETMASK:	/* Get the netmask for the interface */
568 		/* Note that these ioctls will not sleep,
569 		   so that we do not impose a lock.
570 		   One day we will be forced to put shlock here (I mean SMP)
571 		 */
572 		tryaddrmatch = (sin_orig.sin_family == AF_INET);
573 		memset(sin, 0, sizeof(*sin));
574 		sin->sin_family = AF_INET;
575 		break;
576 
577 	case SIOCSIFFLAGS:
578 		ret = -EACCES;
579 		if (!capable(CAP_NET_ADMIN))
580 			goto out;
581 		break;
582 	case SIOCSIFADDR:	/* Set interface address (and family) */
583 	case SIOCSIFBRDADDR:	/* Set the broadcast address */
584 	case SIOCSIFDSTADDR:	/* Set the destination address */
585 	case SIOCSIFNETMASK: 	/* Set the netmask for the interface */
586 		ret = -EACCES;
587 		if (!capable(CAP_NET_ADMIN))
588 			goto out;
589 		ret = -EINVAL;
590 		if (sin->sin_family != AF_INET)
591 			goto out;
592 		break;
593 	default:
594 		ret = -EINVAL;
595 		goto out;
596 	}
597 
598 	rtnl_lock();
599 
600 	ret = -ENODEV;
601 	if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL)
602 		goto done;
603 
604 	if (colon)
605 		*colon = ':';
606 
607 	if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) {
608 		if (tryaddrmatch) {
609 			/* Matthias Andree */
610 			/* compare label and address (4.4BSD style) */
611 			/* note: we only do this for a limited set of ioctls
612 			   and only if the original address family was AF_INET.
613 			   This is checked above. */
614 			for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
615 			     ifap = &ifa->ifa_next) {
616 				if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
617 				    sin_orig.sin_addr.s_addr ==
618 							ifa->ifa_address) {
619 					break; /* found */
620 				}
621 			}
622 		}
623 		/* we didn't get a match, maybe the application is
624 		   4.3BSD-style and passed in junk so we fall back to
625 		   comparing just the label */
626 		if (!ifa) {
627 			for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
628 			     ifap = &ifa->ifa_next)
629 				if (!strcmp(ifr.ifr_name, ifa->ifa_label))
630 					break;
631 		}
632 	}
633 
634 	ret = -EADDRNOTAVAIL;
635 	if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
636 		goto done;
637 
638 	switch(cmd) {
639 	case SIOCGIFADDR:	/* Get interface address */
640 		sin->sin_addr.s_addr = ifa->ifa_local;
641 		goto rarok;
642 
643 	case SIOCGIFBRDADDR:	/* Get the broadcast address */
644 		sin->sin_addr.s_addr = ifa->ifa_broadcast;
645 		goto rarok;
646 
647 	case SIOCGIFDSTADDR:	/* Get the destination address */
648 		sin->sin_addr.s_addr = ifa->ifa_address;
649 		goto rarok;
650 
651 	case SIOCGIFNETMASK:	/* Get the netmask for the interface */
652 		sin->sin_addr.s_addr = ifa->ifa_mask;
653 		goto rarok;
654 
655 	case SIOCSIFFLAGS:
656 		if (colon) {
657 			ret = -EADDRNOTAVAIL;
658 			if (!ifa)
659 				break;
660 			ret = 0;
661 			if (!(ifr.ifr_flags & IFF_UP))
662 				inet_del_ifa(in_dev, ifap, 1);
663 			break;
664 		}
665 		ret = dev_change_flags(dev, ifr.ifr_flags);
666 		break;
667 
668 	case SIOCSIFADDR:	/* Set interface address (and family) */
669 		ret = -EINVAL;
670 		if (inet_abc_len(sin->sin_addr.s_addr) < 0)
671 			break;
672 
673 		if (!ifa) {
674 			ret = -ENOBUFS;
675 			if ((ifa = inet_alloc_ifa()) == NULL)
676 				break;
677 			if (colon)
678 				memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
679 			else
680 				memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
681 		} else {
682 			ret = 0;
683 			if (ifa->ifa_local == sin->sin_addr.s_addr)
684 				break;
685 			inet_del_ifa(in_dev, ifap, 0);
686 			ifa->ifa_broadcast = 0;
687 			ifa->ifa_anycast = 0;
688 		}
689 
690 		ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
691 
692 		if (!(dev->flags & IFF_POINTOPOINT)) {
693 			ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
694 			ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
695 			if ((dev->flags & IFF_BROADCAST) &&
696 			    ifa->ifa_prefixlen < 31)
697 				ifa->ifa_broadcast = ifa->ifa_address |
698 						     ~ifa->ifa_mask;
699 		} else {
700 			ifa->ifa_prefixlen = 32;
701 			ifa->ifa_mask = inet_make_mask(32);
702 		}
703 		ret = inet_set_ifa(dev, ifa);
704 		break;
705 
706 	case SIOCSIFBRDADDR:	/* Set the broadcast address */
707 		ret = 0;
708 		if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
709 			inet_del_ifa(in_dev, ifap, 0);
710 			ifa->ifa_broadcast = sin->sin_addr.s_addr;
711 			inet_insert_ifa(ifa);
712 		}
713 		break;
714 
715 	case SIOCSIFDSTADDR:	/* Set the destination address */
716 		ret = 0;
717 		if (ifa->ifa_address == sin->sin_addr.s_addr)
718 			break;
719 		ret = -EINVAL;
720 		if (inet_abc_len(sin->sin_addr.s_addr) < 0)
721 			break;
722 		ret = 0;
723 		inet_del_ifa(in_dev, ifap, 0);
724 		ifa->ifa_address = sin->sin_addr.s_addr;
725 		inet_insert_ifa(ifa);
726 		break;
727 
728 	case SIOCSIFNETMASK: 	/* Set the netmask for the interface */
729 
730 		/*
731 		 *	The mask we set must be legal.
732 		 */
733 		ret = -EINVAL;
734 		if (bad_mask(sin->sin_addr.s_addr, 0))
735 			break;
736 		ret = 0;
737 		if (ifa->ifa_mask != sin->sin_addr.s_addr) {
738 			u32 old_mask = ifa->ifa_mask;
739 			inet_del_ifa(in_dev, ifap, 0);
740 			ifa->ifa_mask = sin->sin_addr.s_addr;
741 			ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
742 
743 			/* See if current broadcast address matches
744 			 * with current netmask, then recalculate
745 			 * the broadcast address. Otherwise it's a
746 			 * funny address, so don't touch it since
747 			 * the user seems to know what (s)he's doing...
748 			 */
749 			if ((dev->flags & IFF_BROADCAST) &&
750 			    (ifa->ifa_prefixlen < 31) &&
751 			    (ifa->ifa_broadcast ==
752 			     (ifa->ifa_local|~old_mask))) {
753 				ifa->ifa_broadcast = (ifa->ifa_local |
754 						      ~sin->sin_addr.s_addr);
755 			}
756 			inet_insert_ifa(ifa);
757 		}
758 		break;
759 	}
760 done:
761 	rtnl_unlock();
762 out:
763 	return ret;
764 rarok:
765 	rtnl_unlock();
766 	ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
767 	goto out;
768 }
769 
770 static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
771 {
772 	struct in_device *in_dev = __in_dev_get_rtnl(dev);
773 	struct in_ifaddr *ifa;
774 	struct ifreq ifr;
775 	int done = 0;
776 
777 	if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
778 		goto out;
779 
780 	for (; ifa; ifa = ifa->ifa_next) {
781 		if (!buf) {
782 			done += sizeof(ifr);
783 			continue;
784 		}
785 		if (len < (int) sizeof(ifr))
786 			break;
787 		memset(&ifr, 0, sizeof(struct ifreq));
788 		if (ifa->ifa_label)
789 			strcpy(ifr.ifr_name, ifa->ifa_label);
790 		else
791 			strcpy(ifr.ifr_name, dev->name);
792 
793 		(*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
794 		(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
795 								ifa->ifa_local;
796 
797 		if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
798 			done = -EFAULT;
799 			break;
800 		}
801 		buf  += sizeof(struct ifreq);
802 		len  -= sizeof(struct ifreq);
803 		done += sizeof(struct ifreq);
804 	}
805 out:
806 	return done;
807 }
808 
809 u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
810 {
811 	u32 addr = 0;
812 	struct in_device *in_dev;
813 
814 	rcu_read_lock();
815 	in_dev = __in_dev_get_rcu(dev);
816 	if (!in_dev)
817 		goto no_in_dev;
818 
819 	for_primary_ifa(in_dev) {
820 		if (ifa->ifa_scope > scope)
821 			continue;
822 		if (!dst || inet_ifa_match(dst, ifa)) {
823 			addr = ifa->ifa_local;
824 			break;
825 		}
826 		if (!addr)
827 			addr = ifa->ifa_local;
828 	} endfor_ifa(in_dev);
829 no_in_dev:
830 	rcu_read_unlock();
831 
832 	if (addr)
833 		goto out;
834 
835 	/* Not loopback addresses on loopback should be preferred
836 	   in this case. It is importnat that lo is the first interface
837 	   in dev_base list.
838 	 */
839 	read_lock(&dev_base_lock);
840 	rcu_read_lock();
841 	for (dev = dev_base; dev; dev = dev->next) {
842 		if ((in_dev = __in_dev_get_rcu(dev)) == NULL)
843 			continue;
844 
845 		for_primary_ifa(in_dev) {
846 			if (ifa->ifa_scope != RT_SCOPE_LINK &&
847 			    ifa->ifa_scope <= scope) {
848 				addr = ifa->ifa_local;
849 				goto out_unlock_both;
850 			}
851 		} endfor_ifa(in_dev);
852 	}
853 out_unlock_both:
854 	read_unlock(&dev_base_lock);
855 	rcu_read_unlock();
856 out:
857 	return addr;
858 }
859 
860 static u32 confirm_addr_indev(struct in_device *in_dev, u32 dst,
861 			      u32 local, int scope)
862 {
863 	int same = 0;
864 	u32 addr = 0;
865 
866 	for_ifa(in_dev) {
867 		if (!addr &&
868 		    (local == ifa->ifa_local || !local) &&
869 		    ifa->ifa_scope <= scope) {
870 			addr = ifa->ifa_local;
871 			if (same)
872 				break;
873 		}
874 		if (!same) {
875 			same = (!local || inet_ifa_match(local, ifa)) &&
876 				(!dst || inet_ifa_match(dst, ifa));
877 			if (same && addr) {
878 				if (local || !dst)
879 					break;
880 				/* Is the selected addr into dst subnet? */
881 				if (inet_ifa_match(addr, ifa))
882 					break;
883 				/* No, then can we use new local src? */
884 				if (ifa->ifa_scope <= scope) {
885 					addr = ifa->ifa_local;
886 					break;
887 				}
888 				/* search for large dst subnet for addr */
889 				same = 0;
890 			}
891 		}
892 	} endfor_ifa(in_dev);
893 
894 	return same? addr : 0;
895 }
896 
897 /*
898  * Confirm that local IP address exists using wildcards:
899  * - dev: only on this interface, 0=any interface
900  * - dst: only in the same subnet as dst, 0=any dst
901  * - local: address, 0=autoselect the local address
902  * - scope: maximum allowed scope value for the local address
903  */
904 u32 inet_confirm_addr(const struct net_device *dev, u32 dst, u32 local, int scope)
905 {
906 	u32 addr = 0;
907 	struct in_device *in_dev;
908 
909 	if (dev) {
910 		rcu_read_lock();
911 		if ((in_dev = __in_dev_get_rcu(dev)))
912 			addr = confirm_addr_indev(in_dev, dst, local, scope);
913 		rcu_read_unlock();
914 
915 		return addr;
916 	}
917 
918 	read_lock(&dev_base_lock);
919 	rcu_read_lock();
920 	for (dev = dev_base; dev; dev = dev->next) {
921 		if ((in_dev = __in_dev_get_rcu(dev))) {
922 			addr = confirm_addr_indev(in_dev, dst, local, scope);
923 			if (addr)
924 				break;
925 		}
926 	}
927 	rcu_read_unlock();
928 	read_unlock(&dev_base_lock);
929 
930 	return addr;
931 }
932 
933 /*
934  *	Device notifier
935  */
936 
937 int register_inetaddr_notifier(struct notifier_block *nb)
938 {
939 	return notifier_chain_register(&inetaddr_chain, nb);
940 }
941 
942 int unregister_inetaddr_notifier(struct notifier_block *nb)
943 {
944 	return notifier_chain_unregister(&inetaddr_chain, nb);
945 }
946 
947 /* Rename ifa_labels for a device name change. Make some effort to preserve existing
948  * alias numbering and to create unique labels if possible.
949 */
950 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
951 {
952 	struct in_ifaddr *ifa;
953 	int named = 0;
954 
955 	for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
956 		char old[IFNAMSIZ], *dot;
957 
958 		memcpy(old, ifa->ifa_label, IFNAMSIZ);
959 		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
960 		if (named++ == 0)
961 			continue;
962 		dot = strchr(ifa->ifa_label, ':');
963 		if (dot == NULL) {
964 			sprintf(old, ":%d", named);
965 			dot = old;
966 		}
967 		if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) {
968 			strcat(ifa->ifa_label, dot);
969 		} else {
970 			strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
971 		}
972 	}
973 }
974 
975 /* Called only under RTNL semaphore */
976 
977 static int inetdev_event(struct notifier_block *this, unsigned long event,
978 			 void *ptr)
979 {
980 	struct net_device *dev = ptr;
981 	struct in_device *in_dev = __in_dev_get_rtnl(dev);
982 
983 	ASSERT_RTNL();
984 
985 	if (!in_dev) {
986 		if (event == NETDEV_REGISTER && dev == &loopback_dev) {
987 			in_dev = inetdev_init(dev);
988 			if (!in_dev)
989 				panic("devinet: Failed to create loopback\n");
990 			in_dev->cnf.no_xfrm = 1;
991 			in_dev->cnf.no_policy = 1;
992 		}
993 		goto out;
994 	}
995 
996 	switch (event) {
997 	case NETDEV_REGISTER:
998 		printk(KERN_DEBUG "inetdev_event: bug\n");
999 		dev->ip_ptr = NULL;
1000 		break;
1001 	case NETDEV_UP:
1002 		if (dev->mtu < 68)
1003 			break;
1004 		if (dev == &loopback_dev) {
1005 			struct in_ifaddr *ifa;
1006 			if ((ifa = inet_alloc_ifa()) != NULL) {
1007 				ifa->ifa_local =
1008 				  ifa->ifa_address = htonl(INADDR_LOOPBACK);
1009 				ifa->ifa_prefixlen = 8;
1010 				ifa->ifa_mask = inet_make_mask(8);
1011 				in_dev_hold(in_dev);
1012 				ifa->ifa_dev = in_dev;
1013 				ifa->ifa_scope = RT_SCOPE_HOST;
1014 				memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1015 				inet_insert_ifa(ifa);
1016 			}
1017 		}
1018 		ip_mc_up(in_dev);
1019 		break;
1020 	case NETDEV_DOWN:
1021 		ip_mc_down(in_dev);
1022 		break;
1023 	case NETDEV_CHANGEMTU:
1024 		if (dev->mtu >= 68)
1025 			break;
1026 		/* MTU falled under 68, disable IP */
1027 	case NETDEV_UNREGISTER:
1028 		inetdev_destroy(in_dev);
1029 		break;
1030 	case NETDEV_CHANGENAME:
1031 		/* Do not notify about label change, this event is
1032 		 * not interesting to applications using netlink.
1033 		 */
1034 		inetdev_changename(dev, in_dev);
1035 
1036 #ifdef CONFIG_SYSCTL
1037 		devinet_sysctl_unregister(&in_dev->cnf);
1038 		neigh_sysctl_unregister(in_dev->arp_parms);
1039 		neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
1040 				      NET_IPV4_NEIGH, "ipv4", NULL, NULL);
1041 		devinet_sysctl_register(in_dev, &in_dev->cnf);
1042 #endif
1043 		break;
1044 	}
1045 out:
1046 	return NOTIFY_DONE;
1047 }
1048 
1049 static struct notifier_block ip_netdev_notifier = {
1050 	.notifier_call =inetdev_event,
1051 };
1052 
1053 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1054 			    u32 pid, u32 seq, int event, unsigned int flags)
1055 {
1056 	struct ifaddrmsg *ifm;
1057 	struct nlmsghdr  *nlh;
1058 	unsigned char	 *b = skb->tail;
1059 
1060 	nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
1061 	ifm = NLMSG_DATA(nlh);
1062 	ifm->ifa_family = AF_INET;
1063 	ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1064 	ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1065 	ifm->ifa_scope = ifa->ifa_scope;
1066 	ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1067 	if (ifa->ifa_address)
1068 		RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
1069 	if (ifa->ifa_local)
1070 		RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
1071 	if (ifa->ifa_broadcast)
1072 		RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
1073 	if (ifa->ifa_anycast)
1074 		RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
1075 	if (ifa->ifa_label[0])
1076 		RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
1077 	nlh->nlmsg_len = skb->tail - b;
1078 	return skb->len;
1079 
1080 nlmsg_failure:
1081 rtattr_failure:
1082 	skb_trim(skb, b - skb->data);
1083 	return -1;
1084 }
1085 
1086 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1087 {
1088 	int idx, ip_idx;
1089 	struct net_device *dev;
1090 	struct in_device *in_dev;
1091 	struct in_ifaddr *ifa;
1092 	int s_ip_idx, s_idx = cb->args[0];
1093 
1094 	s_ip_idx = ip_idx = cb->args[1];
1095 	read_lock(&dev_base_lock);
1096 	for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
1097 		if (idx < s_idx)
1098 			continue;
1099 		if (idx > s_idx)
1100 			s_ip_idx = 0;
1101 		rcu_read_lock();
1102 		if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
1103 			rcu_read_unlock();
1104 			continue;
1105 		}
1106 
1107 		for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1108 		     ifa = ifa->ifa_next, ip_idx++) {
1109 			if (ip_idx < s_ip_idx)
1110 				continue;
1111 			if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1112 					     cb->nlh->nlmsg_seq,
1113 					     RTM_NEWADDR, NLM_F_MULTI) <= 0) {
1114 				rcu_read_unlock();
1115 				goto done;
1116 			}
1117 		}
1118 		rcu_read_unlock();
1119 	}
1120 
1121 done:
1122 	read_unlock(&dev_base_lock);
1123 	cb->args[0] = idx;
1124 	cb->args[1] = ip_idx;
1125 
1126 	return skb->len;
1127 }
1128 
1129 static void rtmsg_ifa(int event, struct in_ifaddr* ifa)
1130 {
1131 	int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + 128);
1132 	struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);
1133 
1134 	if (!skb)
1135 		netlink_set_err(rtnl, 0, RTNLGRP_IPV4_IFADDR, ENOBUFS);
1136 	else if (inet_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
1137 		kfree_skb(skb);
1138 		netlink_set_err(rtnl, 0, RTNLGRP_IPV4_IFADDR, EINVAL);
1139 	} else {
1140 		netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV4_IFADDR, GFP_KERNEL);
1141 	}
1142 }
1143 
1144 static struct rtnetlink_link inet_rtnetlink_table[RTM_NR_MSGTYPES] = {
1145 	[RTM_NEWADDR  - RTM_BASE] = { .doit	= inet_rtm_newaddr,	},
1146 	[RTM_DELADDR  - RTM_BASE] = { .doit	= inet_rtm_deladdr,	},
1147 	[RTM_GETADDR  - RTM_BASE] = { .dumpit	= inet_dump_ifaddr,	},
1148 	[RTM_NEWROUTE - RTM_BASE] = { .doit	= inet_rtm_newroute,	},
1149 	[RTM_DELROUTE - RTM_BASE] = { .doit	= inet_rtm_delroute,	},
1150 	[RTM_GETROUTE - RTM_BASE] = { .doit	= inet_rtm_getroute,
1151 				      .dumpit	= inet_dump_fib,	},
1152 #ifdef CONFIG_IP_MULTIPLE_TABLES
1153 	[RTM_NEWRULE  - RTM_BASE] = { .doit	= inet_rtm_newrule,	},
1154 	[RTM_DELRULE  - RTM_BASE] = { .doit	= inet_rtm_delrule,	},
1155 	[RTM_GETRULE  - RTM_BASE] = { .dumpit	= inet_dump_rules,	},
1156 #endif
1157 };
1158 
1159 #ifdef CONFIG_SYSCTL
1160 
1161 void inet_forward_change(void)
1162 {
1163 	struct net_device *dev;
1164 	int on = ipv4_devconf.forwarding;
1165 
1166 	ipv4_devconf.accept_redirects = !on;
1167 	ipv4_devconf_dflt.forwarding = on;
1168 
1169 	read_lock(&dev_base_lock);
1170 	for (dev = dev_base; dev; dev = dev->next) {
1171 		struct in_device *in_dev;
1172 		rcu_read_lock();
1173 		in_dev = __in_dev_get_rcu(dev);
1174 		if (in_dev)
1175 			in_dev->cnf.forwarding = on;
1176 		rcu_read_unlock();
1177 	}
1178 	read_unlock(&dev_base_lock);
1179 
1180 	rt_cache_flush(0);
1181 }
1182 
1183 static int devinet_sysctl_forward(ctl_table *ctl, int write,
1184 				  struct file* filp, void __user *buffer,
1185 				  size_t *lenp, loff_t *ppos)
1186 {
1187 	int *valp = ctl->data;
1188 	int val = *valp;
1189 	int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1190 
1191 	if (write && *valp != val) {
1192 		if (valp == &ipv4_devconf.forwarding)
1193 			inet_forward_change();
1194 		else if (valp != &ipv4_devconf_dflt.forwarding)
1195 			rt_cache_flush(0);
1196 	}
1197 
1198 	return ret;
1199 }
1200 
1201 int ipv4_doint_and_flush(ctl_table *ctl, int write,
1202 			 struct file* filp, void __user *buffer,
1203 			 size_t *lenp, loff_t *ppos)
1204 {
1205 	int *valp = ctl->data;
1206 	int val = *valp;
1207 	int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1208 
1209 	if (write && *valp != val)
1210 		rt_cache_flush(0);
1211 
1212 	return ret;
1213 }
1214 
1215 int ipv4_doint_and_flush_strategy(ctl_table *table, int __user *name, int nlen,
1216 				  void __user *oldval, size_t __user *oldlenp,
1217 				  void __user *newval, size_t newlen,
1218 				  void **context)
1219 {
1220 	int *valp = table->data;
1221 	int new;
1222 
1223 	if (!newval || !newlen)
1224 		return 0;
1225 
1226 	if (newlen != sizeof(int))
1227 		return -EINVAL;
1228 
1229 	if (get_user(new, (int __user *)newval))
1230 		return -EFAULT;
1231 
1232 	if (new == *valp)
1233 		return 0;
1234 
1235 	if (oldval && oldlenp) {
1236 		size_t len;
1237 
1238 		if (get_user(len, oldlenp))
1239 			return -EFAULT;
1240 
1241 		if (len) {
1242 			if (len > table->maxlen)
1243 				len = table->maxlen;
1244 			if (copy_to_user(oldval, valp, len))
1245 				return -EFAULT;
1246 			if (put_user(len, oldlenp))
1247 				return -EFAULT;
1248 		}
1249 	}
1250 
1251 	*valp = new;
1252 	rt_cache_flush(0);
1253 	return 1;
1254 }
1255 
1256 
1257 static struct devinet_sysctl_table {
1258 	struct ctl_table_header *sysctl_header;
1259 	ctl_table		devinet_vars[__NET_IPV4_CONF_MAX];
1260 	ctl_table		devinet_dev[2];
1261 	ctl_table		devinet_conf_dir[2];
1262 	ctl_table		devinet_proto_dir[2];
1263 	ctl_table		devinet_root_dir[2];
1264 } devinet_sysctl = {
1265 	.devinet_vars = {
1266 		{
1267 			.ctl_name	= NET_IPV4_CONF_FORWARDING,
1268 			.procname	= "forwarding",
1269 			.data		= &ipv4_devconf.forwarding,
1270 			.maxlen		= sizeof(int),
1271 			.mode		= 0644,
1272 			.proc_handler	= &devinet_sysctl_forward,
1273 		},
1274 		{
1275 			.ctl_name	= NET_IPV4_CONF_MC_FORWARDING,
1276 			.procname	= "mc_forwarding",
1277 			.data		= &ipv4_devconf.mc_forwarding,
1278 			.maxlen		= sizeof(int),
1279 			.mode		= 0444,
1280 			.proc_handler	= &proc_dointvec,
1281 		},
1282 		{
1283 			.ctl_name	= NET_IPV4_CONF_ACCEPT_REDIRECTS,
1284 			.procname	= "accept_redirects",
1285 			.data		= &ipv4_devconf.accept_redirects,
1286 			.maxlen		= sizeof(int),
1287 			.mode		= 0644,
1288 			.proc_handler	= &proc_dointvec,
1289 		},
1290 		{
1291 			.ctl_name	= NET_IPV4_CONF_SECURE_REDIRECTS,
1292 			.procname	= "secure_redirects",
1293 			.data		= &ipv4_devconf.secure_redirects,
1294 			.maxlen		= sizeof(int),
1295 			.mode		= 0644,
1296 			.proc_handler	= &proc_dointvec,
1297 		},
1298 		{
1299 			.ctl_name	= NET_IPV4_CONF_SHARED_MEDIA,
1300 			.procname	= "shared_media",
1301 			.data		= &ipv4_devconf.shared_media,
1302 			.maxlen		= sizeof(int),
1303 			.mode		= 0644,
1304 			.proc_handler	= &proc_dointvec,
1305 		},
1306 		{
1307 			.ctl_name	= NET_IPV4_CONF_RP_FILTER,
1308 			.procname	= "rp_filter",
1309 			.data		= &ipv4_devconf.rp_filter,
1310 			.maxlen		= sizeof(int),
1311 			.mode		= 0644,
1312 			.proc_handler	= &proc_dointvec,
1313 		},
1314 		{
1315 			.ctl_name	= NET_IPV4_CONF_SEND_REDIRECTS,
1316 			.procname	= "send_redirects",
1317 			.data		= &ipv4_devconf.send_redirects,
1318 			.maxlen		= sizeof(int),
1319 			.mode		= 0644,
1320 			.proc_handler	= &proc_dointvec,
1321 		},
1322 		{
1323 			.ctl_name	= NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE,
1324 			.procname	= "accept_source_route",
1325 			.data		= &ipv4_devconf.accept_source_route,
1326 			.maxlen		= sizeof(int),
1327 			.mode		= 0644,
1328 			.proc_handler	= &proc_dointvec,
1329 		},
1330 		{
1331 			.ctl_name	= NET_IPV4_CONF_PROXY_ARP,
1332 			.procname	= "proxy_arp",
1333 			.data		= &ipv4_devconf.proxy_arp,
1334 			.maxlen		= sizeof(int),
1335 			.mode		= 0644,
1336 			.proc_handler	= &proc_dointvec,
1337 		},
1338 		{
1339 			.ctl_name	= NET_IPV4_CONF_MEDIUM_ID,
1340 			.procname	= "medium_id",
1341 			.data		= &ipv4_devconf.medium_id,
1342 			.maxlen		= sizeof(int),
1343 			.mode		= 0644,
1344 			.proc_handler	= &proc_dointvec,
1345 		},
1346 		{
1347 			.ctl_name	= NET_IPV4_CONF_BOOTP_RELAY,
1348 			.procname	= "bootp_relay",
1349 			.data		= &ipv4_devconf.bootp_relay,
1350 			.maxlen		= sizeof(int),
1351 			.mode		= 0644,
1352 			.proc_handler	= &proc_dointvec,
1353 		},
1354 		{
1355 			.ctl_name	= NET_IPV4_CONF_LOG_MARTIANS,
1356 			.procname	= "log_martians",
1357 			.data		= &ipv4_devconf.log_martians,
1358 			.maxlen		= sizeof(int),
1359 			.mode		= 0644,
1360 			.proc_handler	= &proc_dointvec,
1361 		},
1362 		{
1363 			.ctl_name	= NET_IPV4_CONF_TAG,
1364 			.procname	= "tag",
1365 			.data		= &ipv4_devconf.tag,
1366 			.maxlen		= sizeof(int),
1367 			.mode		= 0644,
1368 			.proc_handler	= &proc_dointvec,
1369 		},
1370 		{
1371 			.ctl_name	= NET_IPV4_CONF_ARPFILTER,
1372 			.procname	= "arp_filter",
1373 			.data		= &ipv4_devconf.arp_filter,
1374 			.maxlen		= sizeof(int),
1375 			.mode		= 0644,
1376 			.proc_handler	= &proc_dointvec,
1377 		},
1378 		{
1379 			.ctl_name	= NET_IPV4_CONF_ARP_ANNOUNCE,
1380 			.procname	= "arp_announce",
1381 			.data		= &ipv4_devconf.arp_announce,
1382 			.maxlen		= sizeof(int),
1383 			.mode		= 0644,
1384 			.proc_handler	= &proc_dointvec,
1385 		},
1386 		{
1387 			.ctl_name	= NET_IPV4_CONF_ARP_IGNORE,
1388 			.procname	= "arp_ignore",
1389 			.data		= &ipv4_devconf.arp_ignore,
1390 			.maxlen		= sizeof(int),
1391 			.mode		= 0644,
1392 			.proc_handler	= &proc_dointvec,
1393 		},
1394 		{
1395 			.ctl_name	= NET_IPV4_CONF_NOXFRM,
1396 			.procname	= "disable_xfrm",
1397 			.data		= &ipv4_devconf.no_xfrm,
1398 			.maxlen		= sizeof(int),
1399 			.mode		= 0644,
1400 			.proc_handler	= &ipv4_doint_and_flush,
1401 			.strategy	= &ipv4_doint_and_flush_strategy,
1402 		},
1403 		{
1404 			.ctl_name	= NET_IPV4_CONF_NOPOLICY,
1405 			.procname	= "disable_policy",
1406 			.data		= &ipv4_devconf.no_policy,
1407 			.maxlen		= sizeof(int),
1408 			.mode		= 0644,
1409 			.proc_handler	= &ipv4_doint_and_flush,
1410 			.strategy	= &ipv4_doint_and_flush_strategy,
1411 		},
1412 		{
1413 			.ctl_name	= NET_IPV4_CONF_FORCE_IGMP_VERSION,
1414 			.procname	= "force_igmp_version",
1415 			.data		= &ipv4_devconf.force_igmp_version,
1416 			.maxlen		= sizeof(int),
1417 			.mode		= 0644,
1418 			.proc_handler	= &ipv4_doint_and_flush,
1419 			.strategy	= &ipv4_doint_and_flush_strategy,
1420 		},
1421 		{
1422 			.ctl_name	= NET_IPV4_CONF_PROMOTE_SECONDARIES,
1423 			.procname	= "promote_secondaries",
1424 			.data		= &ipv4_devconf.promote_secondaries,
1425 			.maxlen		= sizeof(int),
1426 			.mode		= 0644,
1427 			.proc_handler	= &ipv4_doint_and_flush,
1428 			.strategy	= &ipv4_doint_and_flush_strategy,
1429 		},
1430 	},
1431 	.devinet_dev = {
1432 		{
1433 			.ctl_name	= NET_PROTO_CONF_ALL,
1434 			.procname	= "all",
1435 			.mode		= 0555,
1436 			.child		= devinet_sysctl.devinet_vars,
1437 		},
1438 	},
1439 	.devinet_conf_dir = {
1440 	        {
1441 			.ctl_name	= NET_IPV4_CONF,
1442 			.procname	= "conf",
1443 			.mode		= 0555,
1444 			.child		= devinet_sysctl.devinet_dev,
1445 		},
1446 	},
1447 	.devinet_proto_dir = {
1448 		{
1449 			.ctl_name	= NET_IPV4,
1450 			.procname	= "ipv4",
1451 			.mode		= 0555,
1452 			.child 		= devinet_sysctl.devinet_conf_dir,
1453 		},
1454 	},
1455 	.devinet_root_dir = {
1456 		{
1457 			.ctl_name	= CTL_NET,
1458 			.procname 	= "net",
1459 			.mode		= 0555,
1460 			.child		= devinet_sysctl.devinet_proto_dir,
1461 		},
1462 	},
1463 };
1464 
1465 static void devinet_sysctl_register(struct in_device *in_dev,
1466 				    struct ipv4_devconf *p)
1467 {
1468 	int i;
1469 	struct net_device *dev = in_dev ? in_dev->dev : NULL;
1470 	struct devinet_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
1471 	char *dev_name = NULL;
1472 
1473 	if (!t)
1474 		return;
1475 	memcpy(t, &devinet_sysctl, sizeof(*t));
1476 	for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1477 		t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
1478 		t->devinet_vars[i].de = NULL;
1479 	}
1480 
1481 	if (dev) {
1482 		dev_name = dev->name;
1483 		t->devinet_dev[0].ctl_name = dev->ifindex;
1484 	} else {
1485 		dev_name = "default";
1486 		t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
1487 	}
1488 
1489 	/*
1490 	 * Make a copy of dev_name, because '.procname' is regarded as const
1491 	 * by sysctl and we wouldn't want anyone to change it under our feet
1492 	 * (see SIOCSIFNAME).
1493 	 */
1494 	dev_name = kstrdup(dev_name, GFP_KERNEL);
1495 	if (!dev_name)
1496 	    goto free;
1497 
1498 	t->devinet_dev[0].procname    = dev_name;
1499 	t->devinet_dev[0].child	      = t->devinet_vars;
1500 	t->devinet_dev[0].de	      = NULL;
1501 	t->devinet_conf_dir[0].child  = t->devinet_dev;
1502 	t->devinet_conf_dir[0].de     = NULL;
1503 	t->devinet_proto_dir[0].child = t->devinet_conf_dir;
1504 	t->devinet_proto_dir[0].de    = NULL;
1505 	t->devinet_root_dir[0].child  = t->devinet_proto_dir;
1506 	t->devinet_root_dir[0].de     = NULL;
1507 
1508 	t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
1509 	if (!t->sysctl_header)
1510 	    goto free_procname;
1511 
1512 	p->sysctl = t;
1513 	return;
1514 
1515 	/* error path */
1516  free_procname:
1517 	kfree(dev_name);
1518  free:
1519 	kfree(t);
1520 	return;
1521 }
1522 
1523 static void devinet_sysctl_unregister(struct ipv4_devconf *p)
1524 {
1525 	if (p->sysctl) {
1526 		struct devinet_sysctl_table *t = p->sysctl;
1527 		p->sysctl = NULL;
1528 		unregister_sysctl_table(t->sysctl_header);
1529 		kfree(t->devinet_dev[0].procname);
1530 		kfree(t);
1531 	}
1532 }
1533 #endif
1534 
1535 void __init devinet_init(void)
1536 {
1537 	register_gifconf(PF_INET, inet_gifconf);
1538 	register_netdevice_notifier(&ip_netdev_notifier);
1539 	rtnetlink_links[PF_INET] = inet_rtnetlink_table;
1540 #ifdef CONFIG_SYSCTL
1541 	devinet_sysctl.sysctl_header =
1542 		register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
1543 	devinet_sysctl_register(NULL, &ipv4_devconf_dflt);
1544 #endif
1545 }
1546 
1547 EXPORT_SYMBOL(devinet_ioctl);
1548 EXPORT_SYMBOL(in_dev_finish_destroy);
1549 EXPORT_SYMBOL(inet_select_addr);
1550 EXPORT_SYMBOL(inetdev_by_index);
1551 EXPORT_SYMBOL(register_inetaddr_notifier);
1552 EXPORT_SYMBOL(unregister_inetaddr_notifier);
1553