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