File: /usr/src/linux/net/ipv4/devinet.c

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