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