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