cleanup
[linux-2.4.21-pre4.git] / net / ipv4 / devinet.c
1 /*
2  *      NET3    IP device support routines.
3  *
4  *      Version: $Id: devinet.c,v 1.1.1.1 2005/04/11 02:51:13 jack 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
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 int
359 inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
360 {
361         struct rtattr  **rta = arg;
362         struct in_device *in_dev;
363         struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
364         struct in_ifaddr *ifa, **ifap;
365
366         ASSERT_RTNL();
367
368         if ((in_dev = inetdev_by_index(ifm->ifa_index)) == NULL)
369                 return -EADDRNOTAVAIL;
370         __in_dev_put(in_dev);
371
372         for (ifap=&in_dev->ifa_list; (ifa=*ifap)!=NULL; ifap=&ifa->ifa_next) {
373                 if ((rta[IFA_LOCAL-1] && memcmp(RTA_DATA(rta[IFA_LOCAL-1]), &ifa->ifa_local, 4)) ||
374                     (rta[IFA_LABEL-1] && strcmp(RTA_DATA(rta[IFA_LABEL-1]), ifa->ifa_label)) ||
375                     (rta[IFA_ADDRESS-1] &&
376                      (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
377                       !inet_ifa_match(*(u32*)RTA_DATA(rta[IFA_ADDRESS-1]), ifa))))
378                         continue;
379                 inet_del_ifa(in_dev, ifap, 1);
380                 return 0;
381         }
382
383         return -EADDRNOTAVAIL;
384 }
385
386 int
387 inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
388 {
389         struct rtattr **rta = arg;
390         struct net_device *dev;
391         struct in_device *in_dev;
392         struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
393         struct in_ifaddr *ifa;
394
395         ASSERT_RTNL();
396
397         if (ifm->ifa_prefixlen > 32 || rta[IFA_LOCAL-1] == NULL)
398                 return -EINVAL;
399
400         if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
401                 return -ENODEV;
402
403         if ((in_dev = __in_dev_get(dev)) == NULL) {
404                 in_dev = inetdev_init(dev);
405                 if (!in_dev)
406                         return -ENOBUFS;
407         }
408
409         if ((ifa = inet_alloc_ifa()) == NULL)
410                 return -ENOBUFS;
411
412         if (rta[IFA_ADDRESS-1] == NULL)
413                 rta[IFA_ADDRESS-1] = rta[IFA_LOCAL-1];
414         memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL-1]), 4);
415         memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS-1]), 4);
416         ifa->ifa_prefixlen = ifm->ifa_prefixlen;
417         ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
418         if (rta[IFA_BROADCAST-1])
419                 memcpy(&ifa->ifa_broadcast, RTA_DATA(rta[IFA_BROADCAST-1]), 4);
420         if (rta[IFA_ANYCAST-1])
421                 memcpy(&ifa->ifa_anycast, RTA_DATA(rta[IFA_ANYCAST-1]), 4);
422         ifa->ifa_flags = ifm->ifa_flags;
423         ifa->ifa_scope = ifm->ifa_scope;
424         in_dev_hold(in_dev);
425         ifa->ifa_dev = in_dev;
426         if (rta[IFA_LABEL-1])
427                 memcpy(ifa->ifa_label, RTA_DATA(rta[IFA_LABEL-1]), IFNAMSIZ);
428         else
429                 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
430
431         return inet_insert_ifa(ifa);
432 }
433
434 /* 
435  *      Determine a default network mask, based on the IP address. 
436  */
437
438 static __inline__ int inet_abc_len(u32 addr)
439 {
440         if (ZERONET(addr))
441                 return 0;
442
443         addr = ntohl(addr);
444         if (IN_CLASSA(addr)) 
445                 return 8;
446         if (IN_CLASSB(addr)) 
447                 return 16;
448         if (IN_CLASSC(addr)) 
449                 return 24;
450
451         /*
452          *      Something else, probably a multicast. 
453          */
454          
455         return -1;
456 }
457
458
459 int devinet_ioctl(unsigned int cmd, void *arg)
460 {
461         struct ifreq ifr;
462         struct sockaddr_in sin_orig;
463         struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
464         struct in_device *in_dev;
465         struct in_ifaddr **ifap = NULL;
466         struct in_ifaddr *ifa = NULL;
467         struct net_device *dev;
468         char *colon;
469         int ret = 0;
470         int tryaddrmatch = 0;
471
472         /*
473          *      Fetch the caller's info block into kernel space
474          */
475
476         if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
477                 return -EFAULT;
478         ifr.ifr_name[IFNAMSIZ-1] = 0;
479
480         /* save original address for comparison */
481         memcpy(&sin_orig, sin, sizeof(*sin));
482
483         colon = strchr(ifr.ifr_name, ':');
484         if (colon)
485                 *colon = 0;
486
487 #ifdef CONFIG_KMOD
488         dev_load(ifr.ifr_name);
489 #endif
490
491         switch(cmd) {
492         case SIOCGIFADDR:       /* Get interface address */
493         case SIOCGIFBRDADDR:    /* Get the broadcast address */
494         case SIOCGIFDSTADDR:    /* Get the destination address */
495         case SIOCGIFNETMASK:    /* Get the netmask for the interface */
496                 /* Note that these ioctls will not sleep,
497                    so that we do not impose a lock.
498                    One day we will be forced to put shlock here (I mean SMP)
499                  */
500                 tryaddrmatch = (sin_orig.sin_family == AF_INET);
501                 memset(sin, 0, sizeof(*sin));
502                 sin->sin_family = AF_INET;
503                 break;
504
505         case SIOCSIFFLAGS:
506                 if (!capable(CAP_NET_ADMIN))
507                         return -EACCES;
508                 break;
509         case SIOCSIFADDR:       /* Set interface address (and family) */
510         case SIOCSIFBRDADDR:    /* Set the broadcast address */
511         case SIOCSIFDSTADDR:    /* Set the destination address */
512         case SIOCSIFNETMASK:    /* Set the netmask for the interface */
513                 if (!capable(CAP_NET_ADMIN))
514                         return -EACCES;
515                 if (sin->sin_family != AF_INET)
516                         return -EINVAL;
517                 break;
518         default:
519                 return -EINVAL;
520         }
521
522         dev_probe_lock();
523         rtnl_lock();
524
525         if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL) {
526                 ret = -ENODEV;
527                 goto done;
528         }
529
530         if (colon)
531                 *colon = ':';
532
533         if ((in_dev=__in_dev_get(dev)) != NULL) {
534                 if (tryaddrmatch) {
535                         /* Matthias Andree */
536                         /* compare label and address (4.4BSD style) */
537                         /* note: we only do this for a limited set of ioctls
538                            and only if the original address family was AF_INET.
539                            This is checked above. */
540                         for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next) {
541                                 if ((strcmp(ifr.ifr_name, ifa->ifa_label) == 0)
542                                     && (sin_orig.sin_addr.s_addr == ifa->ifa_address)) {
543                                         break; /* found */
544                                 }
545                         }
546                 }
547                 /* we didn't get a match, maybe the application is
548                    4.3BSD-style and passed in junk so we fall back to 
549                    comparing just the label */
550                 if (ifa == NULL) {
551                         for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next)
552                                 if (strcmp(ifr.ifr_name, ifa->ifa_label) == 0)
553                                         break;
554                 }
555         }
556
557         if (ifa == NULL && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS) {
558                 ret = -EADDRNOTAVAIL;
559                 goto done;
560         }
561
562         switch(cmd) {
563                 case SIOCGIFADDR:       /* Get interface address */
564                         sin->sin_addr.s_addr = ifa->ifa_local;
565                         goto rarok;
566
567                 case SIOCGIFBRDADDR:    /* Get the broadcast address */
568                         sin->sin_addr.s_addr = ifa->ifa_broadcast;
569                         goto rarok;
570
571                 case SIOCGIFDSTADDR:    /* Get the destination address */
572                         sin->sin_addr.s_addr = ifa->ifa_address;
573                         goto rarok;
574
575                 case SIOCGIFNETMASK:    /* Get the netmask for the interface */
576                         sin->sin_addr.s_addr = ifa->ifa_mask;
577                         goto rarok;
578
579                 case SIOCSIFFLAGS:
580                         if (colon) {
581                                 if (ifa == NULL) {
582                                         ret = -EADDRNOTAVAIL;
583                                         break;
584                                 }
585                                 if (!(ifr.ifr_flags&IFF_UP))
586                                         inet_del_ifa(in_dev, ifap, 1);
587                                 break;
588                         }
589                         ret = dev_change_flags(dev, ifr.ifr_flags);
590                         break;
591         
592                 case SIOCSIFADDR:       /* Set interface address (and family) */
593                         if (inet_abc_len(sin->sin_addr.s_addr) < 0) {
594                                 ret = -EINVAL;
595                                 break;
596                         }
597
598                         if (!ifa) {
599                                 if ((ifa = inet_alloc_ifa()) == NULL) {
600                                         ret = -ENOBUFS;
601                                         break;
602                                 }
603                                 if (colon)
604                                         memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
605                                 else
606                                         memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
607                         } else {
608                                 ret = 0;
609                                 if (ifa->ifa_local == sin->sin_addr.s_addr)
610                                         break;
611                                 inet_del_ifa(in_dev, ifap, 0);
612                                 ifa->ifa_broadcast = 0;
613                                 ifa->ifa_anycast = 0;
614                         }
615
616                         ifa->ifa_address =
617                         ifa->ifa_local = sin->sin_addr.s_addr;
618
619                         if (!(dev->flags&IFF_POINTOPOINT)) {
620                                 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
621                                 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
622                                 if ((dev->flags&IFF_BROADCAST) && ifa->ifa_prefixlen < 31)
623                                         ifa->ifa_broadcast = ifa->ifa_address|~ifa->ifa_mask;
624                         } else {
625                                 ifa->ifa_prefixlen = 32;
626                                 ifa->ifa_mask = inet_make_mask(32);
627                         }
628                         ret = inet_set_ifa(dev, ifa);
629                         break;
630
631                 case SIOCSIFBRDADDR:    /* Set the broadcast address */
632                         if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
633                                 inet_del_ifa(in_dev, ifap, 0);
634                                 ifa->ifa_broadcast = sin->sin_addr.s_addr;
635                                 inet_insert_ifa(ifa);
636                         }
637                         break;
638         
639                 case SIOCSIFDSTADDR:    /* Set the destination address */
640                         if (ifa->ifa_address != sin->sin_addr.s_addr) {
641                                 if (inet_abc_len(sin->sin_addr.s_addr) < 0) {
642                                         ret = -EINVAL;
643                                         break;
644                                 }
645                                 inet_del_ifa(in_dev, ifap, 0);
646                                 ifa->ifa_address = sin->sin_addr.s_addr;
647                                 inet_insert_ifa(ifa);
648                         }
649                         break;
650
651                 case SIOCSIFNETMASK:    /* Set the netmask for the interface */
652
653                         /*
654                          *      The mask we set must be legal.
655                          */
656                         if (bad_mask(sin->sin_addr.s_addr, 0)) {
657                                 ret = -EINVAL;
658                                 break;
659                         }
660
661                         if (ifa->ifa_mask != sin->sin_addr.s_addr) {
662                                 inet_del_ifa(in_dev, ifap, 0);
663                                 ifa->ifa_mask = sin->sin_addr.s_addr;
664                                 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
665                                 inet_insert_ifa(ifa);
666                         }
667                         break;
668         }
669 done:
670         rtnl_unlock();
671         dev_probe_unlock();
672         return ret;
673
674 rarok:
675         rtnl_unlock();
676         dev_probe_unlock();
677         if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
678                 return -EFAULT;
679         return 0;
680 }
681
682 static int
683 inet_gifconf(struct net_device *dev, char *buf, int len)
684 {
685         struct in_device *in_dev = __in_dev_get(dev);
686         struct in_ifaddr *ifa;
687         struct ifreq ifr;
688         int done=0;
689
690         if (in_dev==NULL || (ifa=in_dev->ifa_list)==NULL)
691                 return 0;
692
693         for ( ; ifa; ifa = ifa->ifa_next) {
694                 if (!buf) {
695                         done += sizeof(ifr);
696                         continue;
697                 }
698                 if (len < (int) sizeof(ifr))
699                         return done;
700                 memset(&ifr, 0, sizeof(struct ifreq));
701                 if (ifa->ifa_label)
702                         strcpy(ifr.ifr_name, ifa->ifa_label);
703                 else
704                         strcpy(ifr.ifr_name, dev->name);
705
706                 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_family = AF_INET;
707                 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_addr.s_addr = ifa->ifa_local;
708
709                 if (copy_to_user(buf, &ifr, sizeof(struct ifreq)))
710                         return -EFAULT;
711                 buf += sizeof(struct ifreq);
712                 len -= sizeof(struct ifreq);
713                 done += sizeof(struct ifreq);
714         }
715         return done;
716 }
717
718 u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
719 {
720         u32 addr = 0;
721         struct in_device *in_dev;
722
723         read_lock(&inetdev_lock);
724         in_dev = __in_dev_get(dev);
725         if (in_dev == NULL) {
726                 read_unlock(&inetdev_lock);
727                 return 0;
728         }
729
730         read_lock(&in_dev->lock);
731         for_primary_ifa(in_dev) {
732                 if (ifa->ifa_scope > scope)
733                         continue;
734                 if (!dst || inet_ifa_match(dst, ifa)) {
735                         addr = ifa->ifa_local;
736                         break;
737                 }
738                 if (!addr)
739                         addr = ifa->ifa_local;
740         } endfor_ifa(in_dev);
741         read_unlock(&in_dev->lock);
742         read_unlock(&inetdev_lock);
743
744         if (addr)
745                 return addr;
746
747         /* Not loopback addresses on loopback should be preferred
748            in this case. It is importnat that lo is the first interface
749            in dev_base list.
750          */
751         read_lock(&dev_base_lock);
752         read_lock(&inetdev_lock);
753         for (dev=dev_base; dev; dev=dev->next) {
754                 if ((in_dev=__in_dev_get(dev)) == NULL)
755                         continue;
756
757                 read_lock(&in_dev->lock);
758                 for_primary_ifa(in_dev) {
759                         if (ifa->ifa_scope != RT_SCOPE_LINK &&
760                             ifa->ifa_scope <= scope) {
761                                 read_unlock(&in_dev->lock);
762                                 read_unlock(&inetdev_lock);
763                                 read_unlock(&dev_base_lock);
764                                 return ifa->ifa_local;
765                         }
766                 } endfor_ifa(in_dev);
767                 read_unlock(&in_dev->lock);
768         }
769         read_unlock(&inetdev_lock);
770         read_unlock(&dev_base_lock);
771
772         return 0;
773 }
774
775 /*
776  *      Device notifier
777  */
778
779 int register_inetaddr_notifier(struct notifier_block *nb)
780 {
781         return notifier_chain_register(&inetaddr_chain, nb);
782 }
783
784 int unregister_inetaddr_notifier(struct notifier_block *nb)
785 {
786         return notifier_chain_unregister(&inetaddr_chain,nb);
787 }
788
789 /* Called only under RTNL semaphore */
790
791 static int inetdev_event(struct notifier_block *this, unsigned long event, void *ptr)
792 {
793         struct net_device *dev = ptr;
794         struct in_device *in_dev = __in_dev_get(dev);
795
796         ASSERT_RTNL();
797
798         if (in_dev == NULL)
799                 return NOTIFY_DONE;
800
801         switch (event) {
802         case NETDEV_REGISTER:
803                 printk(KERN_DEBUG "inetdev_event: bug\n");
804                 dev->ip_ptr = NULL;
805                 break;
806         case NETDEV_UP:
807                 if (dev->mtu < 68)
808                         break;
809                 if (dev == &loopback_dev) {
810                         struct in_ifaddr *ifa;
811                         if ((ifa = inet_alloc_ifa()) != NULL) {
812                                 ifa->ifa_local =
813                                 ifa->ifa_address = htonl(INADDR_LOOPBACK);
814                                 ifa->ifa_prefixlen = 8;
815                                 ifa->ifa_mask = inet_make_mask(8);
816                                 in_dev_hold(in_dev);
817                                 ifa->ifa_dev = in_dev;
818                                 ifa->ifa_scope = RT_SCOPE_HOST;
819                                 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
820                                 inet_insert_ifa(ifa);
821                         }
822                 }
823                 ip_mc_up(in_dev);
824                 break;
825         case NETDEV_DOWN:
826                 ip_mc_down(in_dev);
827                 break;
828         case NETDEV_CHANGEMTU:
829                 if (dev->mtu >= 68)
830                         break;
831                 /* MTU falled under 68, disable IP */
832         case NETDEV_UNREGISTER:
833                 inetdev_destroy(in_dev);
834                 break;
835         case NETDEV_CHANGENAME:
836                 if (in_dev->ifa_list) {
837                         struct in_ifaddr *ifa;
838                         for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
839                                 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
840                         /* Do not notify about label change, this event is
841                            not interesting to applications using netlink.
842                          */
843                 }
844                 break;
845         }
846
847         return NOTIFY_DONE;
848 }
849
850 struct notifier_block ip_netdev_notifier = {
851         notifier_call:  inetdev_event,
852 };
853
854 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
855                             u32 pid, u32 seq, int event)
856 {
857         struct ifaddrmsg *ifm;
858         struct nlmsghdr  *nlh;
859         unsigned char    *b = skb->tail;
860
861         nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
862         ifm = NLMSG_DATA(nlh);
863         ifm->ifa_family = AF_INET;
864         ifm->ifa_prefixlen = ifa->ifa_prefixlen;
865         ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
866         ifm->ifa_scope = ifa->ifa_scope;
867         ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
868         if (ifa->ifa_address)
869                 RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
870         if (ifa->ifa_local)
871                 RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
872         if (ifa->ifa_broadcast)
873                 RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
874         if (ifa->ifa_anycast)
875                 RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
876         if (ifa->ifa_label[0])
877                 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
878         nlh->nlmsg_len = skb->tail - b;
879         return skb->len;
880
881 nlmsg_failure:
882 rtattr_failure:
883         skb_trim(skb, b - skb->data);
884         return -1;
885 }
886
887 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
888 {
889         int idx, ip_idx;
890         int s_idx, s_ip_idx;
891         struct net_device *dev;
892         struct in_device *in_dev;
893         struct in_ifaddr *ifa;
894
895         s_idx = cb->args[0];
896         s_ip_idx = ip_idx = cb->args[1];
897         read_lock(&dev_base_lock);
898         for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
899                 if (idx < s_idx)
900                         continue;
901                 if (idx > s_idx)
902                         s_ip_idx = 0;
903                 read_lock(&inetdev_lock);
904                 if ((in_dev = __in_dev_get(dev)) == NULL) {
905                         read_unlock(&inetdev_lock);
906                         continue;
907                 }
908                 read_lock(&in_dev->lock);
909                 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
910                      ifa = ifa->ifa_next, ip_idx++) {
911                         if (ip_idx < s_ip_idx)
912                                 continue;
913                         if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
914                                              cb->nlh->nlmsg_seq, RTM_NEWADDR) <= 0) {
915                                 read_unlock(&in_dev->lock);
916                                 read_unlock(&inetdev_lock);
917                                 goto done;
918                         }
919                 }
920                 read_unlock(&in_dev->lock);
921                 read_unlock(&inetdev_lock);
922         }
923
924 done:
925         read_unlock(&dev_base_lock);
926         cb->args[0] = idx;
927         cb->args[1] = ip_idx;
928
929         return skb->len;
930 }
931
932 static void rtmsg_ifa(int event, struct in_ifaddr * ifa)
933 {
934         struct sk_buff *skb;
935         int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
936
937         skb = alloc_skb(size, GFP_KERNEL);
938         if (!skb) {
939                 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, ENOBUFS);
940                 return;
941         }
942         if (inet_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
943                 kfree_skb(skb);
944                 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, EINVAL);
945                 return;
946         }
947         NETLINK_CB(skb).dst_groups = RTMGRP_IPV4_IFADDR;
948         netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV4_IFADDR, GFP_KERNEL);
949 }
950
951
952 static struct rtnetlink_link inet_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
953 {
954         { NULL,                 NULL,                   },
955         { NULL,                 NULL,                   },
956         { NULL,                 NULL,                   },
957         { NULL,                 NULL,                   },
958
959         { inet_rtm_newaddr,     NULL,                   },
960         { inet_rtm_deladdr,     NULL,                   },
961         { NULL,                 inet_dump_ifaddr,       },
962         { NULL,                 NULL,                   },
963
964         { inet_rtm_newroute,    NULL,                   },
965         { inet_rtm_delroute,    NULL,                   },
966         { inet_rtm_getroute,    inet_dump_fib,          },
967         { NULL,                 NULL,                   },
968
969         { NULL,                 NULL,                   },
970         { NULL,                 NULL,                   },
971         { NULL,                 NULL,                   },
972         { NULL,                 NULL,                   },
973
974 #ifdef CONFIG_IP_MULTIPLE_TABLES
975         { inet_rtm_newrule,     NULL,                   },
976         { inet_rtm_delrule,     NULL,                   },
977         { NULL,                 inet_dump_rules,        },
978         { NULL,                 NULL,                   },
979 #else
980         { NULL,                 NULL,                   },
981         { NULL,                 NULL,                   },
982         { NULL,                 NULL,                   },
983         { NULL,                 NULL,                   },
984 #endif
985 };
986
987
988 #ifdef CONFIG_SYSCTL
989
990 void inet_forward_change()
991 {
992         struct net_device *dev;
993         int on = ipv4_devconf.forwarding;
994
995         ipv4_devconf.accept_redirects = !on;
996         ipv4_devconf_dflt.forwarding = on;
997
998         read_lock(&dev_base_lock);
999         for (dev = dev_base; dev; dev = dev->next) {
1000                 struct in_device *in_dev;
1001                 read_lock(&inetdev_lock);
1002                 in_dev = __in_dev_get(dev);
1003                 if (in_dev)
1004                         in_dev->cnf.forwarding = on;
1005                 read_unlock(&inetdev_lock);
1006         }
1007         read_unlock(&dev_base_lock);
1008
1009         rt_cache_flush(0);
1010 }
1011
1012 static
1013 int devinet_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
1014                            void *buffer, size_t *lenp)
1015 {
1016         int *valp = ctl->data;
1017         int val = *valp;
1018         int ret;
1019
1020         ret = proc_dointvec(ctl, write, filp, buffer, lenp);
1021
1022         if (write && *valp != val) {
1023                 if (valp == &ipv4_devconf.forwarding)
1024                         inet_forward_change();
1025                 else if (valp != &ipv4_devconf_dflt.forwarding)
1026                         rt_cache_flush(0);
1027         }
1028
1029         return ret;
1030 }
1031
1032 static struct devinet_sysctl_table
1033 {
1034         struct ctl_table_header *sysctl_header;
1035         ctl_table devinet_vars[15];
1036         ctl_table devinet_dev[2];
1037         ctl_table devinet_conf_dir[2];
1038         ctl_table devinet_proto_dir[2];
1039         ctl_table devinet_root_dir[2];
1040 } devinet_sysctl = {
1041         NULL,
1042         {{NET_IPV4_CONF_FORWARDING, "forwarding",
1043          &ipv4_devconf.forwarding, sizeof(int), 0644, NULL,
1044          &devinet_sysctl_forward},
1045         {NET_IPV4_CONF_MC_FORWARDING, "mc_forwarding",
1046          &ipv4_devconf.mc_forwarding, sizeof(int), 0444, NULL,
1047          &proc_dointvec},
1048         {NET_IPV4_CONF_ACCEPT_REDIRECTS, "accept_redirects",
1049          &ipv4_devconf.accept_redirects, sizeof(int), 0644, NULL,
1050          &proc_dointvec},
1051         {NET_IPV4_CONF_SECURE_REDIRECTS, "secure_redirects",
1052          &ipv4_devconf.secure_redirects, sizeof(int), 0644, NULL,
1053          &proc_dointvec},
1054         {NET_IPV4_CONF_SHARED_MEDIA, "shared_media",
1055          &ipv4_devconf.shared_media, sizeof(int), 0644, NULL,
1056          &proc_dointvec},
1057         {NET_IPV4_CONF_RP_FILTER, "rp_filter",
1058          &ipv4_devconf.rp_filter, sizeof(int), 0644, NULL,
1059          &proc_dointvec},
1060         {NET_IPV4_CONF_SEND_REDIRECTS, "send_redirects",
1061          &ipv4_devconf.send_redirects, sizeof(int), 0644, NULL,
1062          &proc_dointvec},
1063         {NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE, "accept_source_route",
1064          &ipv4_devconf.accept_source_route, sizeof(int), 0644, NULL,
1065          &proc_dointvec},
1066         {NET_IPV4_CONF_PROXY_ARP, "proxy_arp",
1067          &ipv4_devconf.proxy_arp, sizeof(int), 0644, NULL,
1068          &proc_dointvec},
1069         {NET_IPV4_CONF_MEDIUM_ID, "medium_id",
1070          &ipv4_devconf.medium_id, sizeof(int), 0644, NULL,
1071          &proc_dointvec},
1072         {NET_IPV4_CONF_BOOTP_RELAY, "bootp_relay",
1073          &ipv4_devconf.bootp_relay, sizeof(int), 0644, NULL,
1074          &proc_dointvec},
1075         {NET_IPV4_CONF_LOG_MARTIANS, "log_martians",
1076          &ipv4_devconf.log_martians, sizeof(int), 0644, NULL,
1077          &proc_dointvec},
1078         {NET_IPV4_CONF_TAG, "tag",
1079          &ipv4_devconf.tag, sizeof(int), 0644, NULL,
1080          &proc_dointvec},
1081         {NET_IPV4_CONF_ARPFILTER, "arp_filter",
1082          &ipv4_devconf.arp_filter, sizeof(int), 0644, NULL,
1083          &proc_dointvec},
1084          {0}},
1085
1086         {{NET_PROTO_CONF_ALL, "all", NULL, 0, 0555, devinet_sysctl.devinet_vars},{0}},
1087         {{NET_IPV4_CONF, "conf", NULL, 0, 0555, devinet_sysctl.devinet_dev},{0}},
1088         {{NET_IPV4, "ipv4", NULL, 0, 0555, devinet_sysctl.devinet_conf_dir},{0}},
1089         {{CTL_NET, "net", NULL, 0, 0555, devinet_sysctl.devinet_proto_dir},{0}}
1090 };
1091
1092 static void devinet_sysctl_register(struct in_device *in_dev, struct ipv4_devconf *p)
1093 {
1094         int i;
1095         struct net_device *dev = in_dev ? in_dev->dev : NULL;
1096         struct devinet_sysctl_table *t;
1097
1098         t = kmalloc(sizeof(*t), GFP_KERNEL);
1099         if (t == NULL)
1100                 return;
1101         memcpy(t, &devinet_sysctl, sizeof(*t));
1102         for (i=0; i<sizeof(t->devinet_vars)/sizeof(t->devinet_vars[0])-1; i++) {
1103                 t->devinet_vars[i].data += (char*)p - (char*)&ipv4_devconf;
1104                 t->devinet_vars[i].de = NULL;
1105         }
1106         if (dev) {
1107                 t->devinet_dev[0].procname = dev->name;
1108                 t->devinet_dev[0].ctl_name = dev->ifindex;
1109         } else {
1110                 t->devinet_dev[0].procname = "default";
1111                 t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
1112         }
1113         t->devinet_dev[0].child = t->devinet_vars;
1114         t->devinet_dev[0].de = NULL;
1115         t->devinet_conf_dir[0].child = t->devinet_dev;
1116         t->devinet_conf_dir[0].de = NULL;
1117         t->devinet_proto_dir[0].child = t->devinet_conf_dir;
1118         t->devinet_proto_dir[0].de = NULL;
1119         t->devinet_root_dir[0].child = t->devinet_proto_dir;
1120         t->devinet_root_dir[0].de = NULL;
1121
1122         t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
1123         if (t->sysctl_header == NULL)
1124                 kfree(t);
1125         else
1126                 p->sysctl = t;
1127 }
1128
1129 static void devinet_sysctl_unregister(struct ipv4_devconf *p)
1130 {
1131         if (p->sysctl) {
1132                 struct devinet_sysctl_table *t = p->sysctl;
1133                 p->sysctl = NULL;
1134                 unregister_sysctl_table(t->sysctl_header);
1135                 kfree(t);
1136         }
1137 }
1138 #endif
1139
1140 void __init devinet_init(void)
1141 {
1142         register_gifconf(PF_INET, inet_gifconf);
1143         register_netdevice_notifier(&ip_netdev_notifier);
1144         rtnetlink_links[PF_INET] = inet_rtnetlink_table;
1145 #ifdef CONFIG_SYSCTL
1146         devinet_sysctl.sysctl_header =
1147                 register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
1148         devinet_sysctl_register(NULL, &ipv4_devconf_dflt);
1149 #endif
1150 }