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