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