This commit was manufactured by cvs2svn to create tag
[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 int devinet_ioctl(unsigned int cmd, void __user *arg)
495 {
496         struct ifreq ifr;
497         struct sockaddr_in sin_orig;
498         struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
499         struct in_device *in_dev;
500         struct in_ifaddr **ifap = NULL;
501         struct in_ifaddr *ifa = NULL;
502         struct net_device *dev;
503         char *colon;
504         int ret = -EFAULT;
505         int tryaddrmatch = 0;
506
507         /*
508          *      Fetch the caller's info block into kernel space
509          */
510
511         if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
512                 goto out;
513         ifr.ifr_name[IFNAMSIZ - 1] = 0;
514
515         /* save original address for comparison */
516         memcpy(&sin_orig, sin, sizeof(*sin));
517
518         colon = strchr(ifr.ifr_name, ':');
519         if (colon)
520                 *colon = 0;
521
522 #ifdef CONFIG_KMOD
523         dev_load(ifr.ifr_name);
524 #endif
525
526         switch(cmd) {
527         case SIOCGIFADDR:       /* Get interface address */
528         case SIOCGIFBRDADDR:    /* Get the broadcast address */
529         case SIOCGIFDSTADDR:    /* Get the destination address */
530         case SIOCGIFNETMASK:    /* Get the netmask for the interface */
531                 /* Note that these ioctls will not sleep,
532                    so that we do not impose a lock.
533                    One day we will be forced to put shlock here (I mean SMP)
534                  */
535                 tryaddrmatch = (sin_orig.sin_family == AF_INET);
536                 memset(sin, 0, sizeof(*sin));
537                 sin->sin_family = AF_INET;
538                 break;
539
540         case SIOCSIFFLAGS:
541                 ret = -EACCES;
542                 if (!capable(CAP_NET_ADMIN))
543                         goto out;
544                 break;
545         case SIOCSIFADDR:       /* Set interface address (and family) */
546         case SIOCSIFBRDADDR:    /* Set the broadcast address */
547         case SIOCSIFDSTADDR:    /* Set the destination address */
548         case SIOCSIFNETMASK:    /* Set the netmask for the interface */
549                 ret = -EACCES;
550                 if (!capable(CAP_NET_ADMIN))
551                         goto out;
552                 ret = -EINVAL;
553                 if (sin->sin_family != AF_INET)
554                         goto out;
555                 break;
556         default:
557                 ret = -EINVAL;
558                 goto out;
559         }
560
561         rtnl_lock();
562
563         ret = -ENODEV;
564         if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL)
565                 goto done;
566
567         if (colon)
568                 *colon = ':';
569
570         if ((in_dev = __in_dev_get(dev)) != NULL) {
571                 if (tryaddrmatch) {
572                         /* Matthias Andree */
573                         /* compare label and address (4.4BSD style) */
574                         /* note: we only do this for a limited set of ioctls
575                            and only if the original address family was AF_INET.
576                            This is checked above. */
577                         for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
578                              ifap = &ifa->ifa_next) {
579                                 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
580                                     sin_orig.sin_addr.s_addr ==
581                                                         ifa->ifa_address) {
582                                         break; /* found */
583                                 }
584                         }
585                 }
586                 /* we didn't get a match, maybe the application is
587                    4.3BSD-style and passed in junk so we fall back to
588                    comparing just the label */
589                 if (!ifa) {
590                         for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
591                              ifap = &ifa->ifa_next)
592                                 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
593                                         break;
594                 }
595         }
596
597         ret = -EADDRNOTAVAIL;
598         if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
599                 goto done;
600
601         switch(cmd) {
602         case SIOCGIFADDR:       /* Get interface address */
603                 sin->sin_addr.s_addr = ifa->ifa_local;
604                 goto rarok;
605
606         case SIOCGIFBRDADDR:    /* Get the broadcast address */
607                 sin->sin_addr.s_addr = ifa->ifa_broadcast;
608                 goto rarok;
609
610         case SIOCGIFDSTADDR:    /* Get the destination address */
611                 sin->sin_addr.s_addr = ifa->ifa_address;
612                 goto rarok;
613
614         case SIOCGIFNETMASK:    /* Get the netmask for the interface */
615                 sin->sin_addr.s_addr = ifa->ifa_mask;
616                 goto rarok;
617
618         case SIOCSIFFLAGS:
619                 if (colon) {
620                         ret = -EADDRNOTAVAIL;
621                         if (!ifa)
622                                 break;
623                         ret = 0;
624                         if (!(ifr.ifr_flags & IFF_UP))
625                                 inet_del_ifa(in_dev, ifap, 1);
626                         break;
627                 }
628                 ret = dev_change_flags(dev, ifr.ifr_flags);
629                 break;
630
631         case SIOCSIFADDR:       /* Set interface address (and family) */
632                 ret = -EINVAL;
633                 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
634                         break;
635
636                 if (!ifa) {
637                         ret = -ENOBUFS;
638                         if ((ifa = inet_alloc_ifa()) == NULL)
639                                 break;
640                         if (colon)
641                                 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
642                         else
643                                 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
644                 } else {
645                         ret = 0;
646                         if (ifa->ifa_local == sin->sin_addr.s_addr)
647                                 break;
648                         inet_del_ifa(in_dev, ifap, 0);
649                         ifa->ifa_broadcast = 0;
650                         ifa->ifa_anycast = 0;
651                 }
652
653                 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
654
655                 if (!(dev->flags & IFF_POINTOPOINT)) {
656                         ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
657                         ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
658                         if ((dev->flags & IFF_BROADCAST) &&
659                             ifa->ifa_prefixlen < 31)
660                                 ifa->ifa_broadcast = ifa->ifa_address |
661                                                      ~ifa->ifa_mask;
662                 } else {
663                         ifa->ifa_prefixlen = 32;
664                         ifa->ifa_mask = inet_make_mask(32);
665                 }
666                 ret = inet_set_ifa(dev, ifa);
667                 break;
668
669         case SIOCSIFBRDADDR:    /* Set the broadcast address */
670                 ret = 0;
671                 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
672                         inet_del_ifa(in_dev, ifap, 0);
673                         ifa->ifa_broadcast = sin->sin_addr.s_addr;
674                         inet_insert_ifa(ifa);
675                 }
676                 break;
677
678         case SIOCSIFDSTADDR:    /* Set the destination address */
679                 ret = 0;
680                 if (ifa->ifa_address == sin->sin_addr.s_addr)
681                         break;
682                 ret = -EINVAL;
683                 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
684                         break;
685                 ret = 0;
686                 inet_del_ifa(in_dev, ifap, 0);
687                 ifa->ifa_address = sin->sin_addr.s_addr;
688                 inet_insert_ifa(ifa);
689                 break;
690
691         case SIOCSIFNETMASK:    /* Set the netmask for the interface */
692
693                 /*
694                  *      The mask we set must be legal.
695                  */
696                 ret = -EINVAL;
697                 if (bad_mask(sin->sin_addr.s_addr, 0))
698                         break;
699                 ret = 0;
700                 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
701                         inet_del_ifa(in_dev, ifap, 0);
702                         ifa->ifa_mask = sin->sin_addr.s_addr;
703                         ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
704
705                         /* See if current broadcast address matches
706                          * with current netmask, then recalculate
707                          * the broadcast address. Otherwise it's a
708                          * funny address, so don't touch it since
709                          * the user seems to know what (s)he's doing...
710                          */
711                         if ((dev->flags & IFF_BROADCAST) &&
712                             (ifa->ifa_prefixlen < 31) &&
713                             (ifa->ifa_broadcast ==
714                              (ifa->ifa_local|~ifa->ifa_mask))) {
715                                 ifa->ifa_broadcast = (ifa->ifa_local |
716                                                       ~sin->sin_addr.s_addr);
717                         }
718                         inet_insert_ifa(ifa);
719                 }
720                 break;
721         }
722 done:
723         rtnl_unlock();
724 out:
725         return ret;
726 rarok:
727         rtnl_unlock();
728         ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
729         goto out;
730 }
731
732 static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
733 {
734         struct in_device *in_dev = __in_dev_get(dev);
735         struct in_ifaddr *ifa;
736         struct ifreq ifr;
737         int done = 0;
738
739         if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
740                 goto out;
741
742         for (; ifa; ifa = ifa->ifa_next) {
743                 if (!buf) {
744                         done += sizeof(ifr);
745                         continue;
746                 }
747                 if (len < (int) sizeof(ifr))
748                         break;
749                 memset(&ifr, 0, sizeof(struct ifreq));
750                 if (ifa->ifa_label)
751                         strcpy(ifr.ifr_name, ifa->ifa_label);
752                 else
753                         strcpy(ifr.ifr_name, dev->name);
754
755                 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
756                 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
757                                                                 ifa->ifa_local;
758
759                 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
760                         done = -EFAULT;
761                         break;
762                 }
763                 buf  += sizeof(struct ifreq);
764                 len  -= sizeof(struct ifreq);
765                 done += sizeof(struct ifreq);
766         }
767 out:
768         return done;
769 }
770
771 u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
772 {
773         u32 addr = 0;
774         struct in_device *in_dev;
775
776         rcu_read_lock();
777         in_dev = __in_dev_get(dev);
778         if (!in_dev)
779                 goto no_in_dev;
780
781         for_primary_ifa(in_dev) {
782                 if (ifa->ifa_scope > scope)
783                         continue;
784                 if (!dst || inet_ifa_match(dst, ifa)) {
785                         addr = ifa->ifa_local;
786                         break;
787                 }
788                 if (!addr)
789                         addr = ifa->ifa_local;
790         } endfor_ifa(in_dev);
791 no_in_dev:
792         rcu_read_unlock();
793
794         if (addr)
795                 goto out;
796
797         /* Not loopback addresses on loopback should be preferred
798            in this case. It is importnat that lo is the first interface
799            in dev_base list.
800          */
801         read_lock(&dev_base_lock);
802         rcu_read_lock();
803         for (dev = dev_base; dev; dev = dev->next) {
804                 if ((in_dev = __in_dev_get(dev)) == NULL)
805                         continue;
806
807                 for_primary_ifa(in_dev) {
808                         if (ifa->ifa_scope != RT_SCOPE_LINK &&
809                             ifa->ifa_scope <= scope) {
810                                 addr = ifa->ifa_local;
811                                 goto out_unlock_both;
812                         }
813                 } endfor_ifa(in_dev);
814         }
815 out_unlock_both:
816         read_unlock(&dev_base_lock);
817         rcu_read_unlock();
818 out:
819         return addr;
820 }
821
822 static u32 confirm_addr_indev(struct in_device *in_dev, u32 dst,
823                               u32 local, int scope)
824 {
825         int same = 0;
826         u32 addr = 0;
827
828         for_ifa(in_dev) {
829                 if (!addr &&
830                     (local == ifa->ifa_local || !local) &&
831                     ifa->ifa_scope <= scope) {
832                         addr = ifa->ifa_local;
833                         if (same)
834                                 break;
835                 }
836                 if (!same) {
837                         same = (!local || inet_ifa_match(local, ifa)) &&
838                                 (!dst || inet_ifa_match(dst, ifa));
839                         if (same && addr) {
840                                 if (local || !dst)
841                                         break;
842                                 /* Is the selected addr into dst subnet? */
843                                 if (inet_ifa_match(addr, ifa))
844                                         break;
845                                 /* No, then can we use new local src? */
846                                 if (ifa->ifa_scope <= scope) {
847                                         addr = ifa->ifa_local;
848                                         break;
849                                 }
850                                 /* search for large dst subnet for addr */
851                                 same = 0;
852                         }
853                 }
854         } endfor_ifa(in_dev);
855
856         return same? addr : 0;
857 }
858
859 /*
860  * Confirm that local IP address exists using wildcards:
861  * - dev: only on this interface, 0=any interface
862  * - dst: only in the same subnet as dst, 0=any dst
863  * - local: address, 0=autoselect the local address
864  * - scope: maximum allowed scope value for the local address
865  */
866 u32 inet_confirm_addr(const struct net_device *dev, u32 dst, u32 local, int scope)
867 {
868         u32 addr = 0;
869         struct in_device *in_dev;
870
871         if (dev) {
872                 rcu_read_lock();
873                 if ((in_dev = __in_dev_get(dev)))
874                         addr = confirm_addr_indev(in_dev, dst, local, scope);
875                 rcu_read_unlock();
876
877                 return addr;
878         }
879
880         read_lock(&dev_base_lock);
881         rcu_read_lock();
882         for (dev = dev_base; dev; dev = dev->next) {
883                 if ((in_dev = __in_dev_get(dev))) {
884                         addr = confirm_addr_indev(in_dev, dst, local, scope);
885                         if (addr)
886                                 break;
887                 }
888         }
889         rcu_read_unlock();
890         read_unlock(&dev_base_lock);
891
892         return addr;
893 }
894
895 /*
896  *      Device notifier
897  */
898
899 int register_inetaddr_notifier(struct notifier_block *nb)
900 {
901         return notifier_chain_register(&inetaddr_chain, nb);
902 }
903
904 int unregister_inetaddr_notifier(struct notifier_block *nb)
905 {
906         return notifier_chain_unregister(&inetaddr_chain, nb);
907 }
908
909 /* Rename ifa_labels for a device name change. Make some effort to preserve existing
910  * alias numbering and to create unique labels if possible.
911 */
912 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
913
914         struct in_ifaddr *ifa;
915         int named = 0;
916
917         for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 
918                 char old[IFNAMSIZ], *dot; 
919
920                 memcpy(old, ifa->ifa_label, IFNAMSIZ);
921                 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); 
922                 if (named++ == 0)
923                         continue;
924                 dot = strchr(ifa->ifa_label, ':');
925                 if (dot == NULL) { 
926                         sprintf(old, ":%d", named); 
927                         dot = old;
928                 }
929                 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) { 
930                         strcat(ifa->ifa_label, dot); 
931                 } else { 
932                         strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot); 
933                 } 
934         }       
935
936
937 /* Called only under RTNL semaphore */
938
939 static int inetdev_event(struct notifier_block *this, unsigned long event,
940                          void *ptr)
941 {
942         struct net_device *dev = ptr;
943         struct in_device *in_dev = __in_dev_get(dev);
944
945         ASSERT_RTNL();
946
947         if (!in_dev)
948                 goto out;
949
950         switch (event) {
951         case NETDEV_REGISTER:
952                 printk(KERN_DEBUG "inetdev_event: bug\n");
953                 dev->ip_ptr = NULL;
954                 break;
955         case NETDEV_UP:
956                 if (dev->mtu < 68)
957                         break;
958                 if (dev == &loopback_dev) {
959                         struct in_ifaddr *ifa;
960                         if ((ifa = inet_alloc_ifa()) != NULL) {
961                                 ifa->ifa_local =
962                                   ifa->ifa_address = htonl(INADDR_LOOPBACK);
963                                 ifa->ifa_prefixlen = 8;
964                                 ifa->ifa_mask = inet_make_mask(8);
965                                 in_dev_hold(in_dev);
966                                 ifa->ifa_dev = in_dev;
967                                 ifa->ifa_scope = RT_SCOPE_HOST;
968                                 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
969                                 inet_insert_ifa(ifa);
970                         }
971                         in_dev->cnf.no_xfrm = 1;
972                         in_dev->cnf.no_policy = 1;
973                 }
974                 ip_mc_up(in_dev);
975                 break;
976         case NETDEV_DOWN:
977                 ip_mc_down(in_dev);
978                 break;
979         case NETDEV_CHANGEMTU:
980                 if (dev->mtu >= 68)
981                         break;
982                 /* MTU falled under 68, disable IP */
983         case NETDEV_UNREGISTER:
984                 inetdev_destroy(in_dev);
985                 break;
986         case NETDEV_CHANGENAME:
987                 /* Do not notify about label change, this event is
988                  * not interesting to applications using netlink.
989                  */
990                 inetdev_changename(dev, in_dev);
991
992 #ifdef CONFIG_SYSCTL
993                 devinet_sysctl_unregister(&in_dev->cnf);
994                 neigh_sysctl_unregister(in_dev->arp_parms);
995                 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
996                                       NET_IPV4_NEIGH, "ipv4", NULL);
997                 devinet_sysctl_register(in_dev, &in_dev->cnf);
998 #endif
999                 break;
1000         }
1001 out:
1002         return NOTIFY_DONE;
1003 }
1004
1005 static struct notifier_block ip_netdev_notifier = {
1006         .notifier_call =inetdev_event,
1007 };
1008
1009 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1010                             u32 pid, u32 seq, int event)
1011 {
1012         struct ifaddrmsg *ifm;
1013         struct nlmsghdr  *nlh;
1014         unsigned char    *b = skb->tail;
1015
1016         nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
1017         if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
1018         ifm = NLMSG_DATA(nlh);
1019         ifm->ifa_family = AF_INET;
1020         ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1021         ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1022         ifm->ifa_scope = ifa->ifa_scope;
1023         ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1024         if (ifa->ifa_address)
1025                 RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
1026         if (ifa->ifa_local)
1027                 RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
1028         if (ifa->ifa_broadcast)
1029                 RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
1030         if (ifa->ifa_anycast)
1031                 RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
1032         if (ifa->ifa_label[0])
1033                 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
1034         nlh->nlmsg_len = skb->tail - b;
1035         return skb->len;
1036
1037 nlmsg_failure:
1038 rtattr_failure:
1039         skb_trim(skb, b - skb->data);
1040         return -1;
1041 }
1042
1043 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1044 {
1045         int idx, ip_idx;
1046         struct net_device *dev;
1047         struct in_device *in_dev;
1048         struct in_ifaddr *ifa;
1049         int s_ip_idx, s_idx = cb->args[0];
1050
1051         s_ip_idx = ip_idx = cb->args[1];
1052         read_lock(&dev_base_lock);
1053         for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
1054                 if (idx < s_idx)
1055                         continue;
1056                 if (idx > s_idx)
1057                         s_ip_idx = 0;
1058                 rcu_read_lock();
1059                 if ((in_dev = __in_dev_get(dev)) == NULL) {
1060                         rcu_read_unlock();
1061                         continue;
1062                 }
1063
1064                 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1065                      ifa = ifa->ifa_next, ip_idx++) {
1066                         if (ip_idx < s_ip_idx)
1067                                 continue;
1068                         if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1069                                              cb->nlh->nlmsg_seq,
1070                                              RTM_NEWADDR) <= 0) {
1071                                 rcu_read_unlock();
1072                                 goto done;
1073                         }
1074                 }
1075                 rcu_read_unlock();
1076         }
1077
1078 done:
1079         read_unlock(&dev_base_lock);
1080         cb->args[0] = idx;
1081         cb->args[1] = ip_idx;
1082
1083         return skb->len;
1084 }
1085
1086 static void rtmsg_ifa(int event, struct in_ifaddr* ifa)
1087 {
1088         int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + 128);
1089         struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);
1090
1091         if (!skb)
1092                 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, ENOBUFS);
1093         else if (inet_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
1094                 kfree_skb(skb);
1095                 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, EINVAL);
1096         } else {
1097                 NETLINK_CB(skb).dst_groups = RTMGRP_IPV4_IFADDR;
1098                 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV4_IFADDR, GFP_KERNEL);
1099         }
1100 }
1101
1102 static struct rtnetlink_link inet_rtnetlink_table[RTM_MAX - RTM_BASE + 1] = {
1103          [4] = { .doit   = inet_rtm_newaddr,  },
1104          [5] = { .doit   = inet_rtm_deladdr,  },
1105          [6] = { .dumpit = inet_dump_ifaddr,  },
1106          [8] = { .doit   = inet_rtm_newroute, },
1107          [9] = { .doit   = inet_rtm_delroute, },
1108         [10] = { .doit   = inet_rtm_getroute, .dumpit = inet_dump_fib, },
1109 #ifdef CONFIG_IP_MULTIPLE_TABLES
1110         [16] = { .doit   = inet_rtm_newrule, },
1111         [17] = { .doit   = inet_rtm_delrule, },
1112         [18] = { .dumpit = inet_dump_rules,  },
1113 #endif
1114 };
1115
1116 #ifdef CONFIG_SYSCTL
1117
1118 void inet_forward_change(void)
1119 {
1120         struct net_device *dev;
1121         int on = ipv4_devconf.forwarding;
1122
1123         ipv4_devconf.accept_redirects = !on;
1124         ipv4_devconf_dflt.forwarding = on;
1125
1126         read_lock(&dev_base_lock);
1127         for (dev = dev_base; dev; dev = dev->next) {
1128                 struct in_device *in_dev;
1129                 rcu_read_lock();
1130                 in_dev = __in_dev_get(dev);
1131                 if (in_dev)
1132                         in_dev->cnf.forwarding = on;
1133                 rcu_read_unlock();
1134         }
1135         read_unlock(&dev_base_lock);
1136
1137         rt_cache_flush(0);
1138 }
1139
1140 static int devinet_sysctl_forward(ctl_table *ctl, int write,
1141                                   struct file* filp, void __user *buffer,
1142                                   size_t *lenp, loff_t *ppos)
1143 {
1144         int *valp = ctl->data;
1145         int val = *valp;
1146         int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1147
1148         if (write && *valp != val) {
1149                 if (valp == &ipv4_devconf.forwarding)
1150                         inet_forward_change();
1151                 else if (valp != &ipv4_devconf_dflt.forwarding)
1152                         rt_cache_flush(0);
1153         }
1154
1155         return ret;
1156 }
1157
1158 int ipv4_doint_and_flush(ctl_table *ctl, int write,
1159                          struct file* filp, void __user *buffer,
1160                          size_t *lenp, loff_t *ppos)
1161 {
1162         int *valp = ctl->data;
1163         int val = *valp;
1164         int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1165
1166         if (write && *valp != val)
1167                 rt_cache_flush(0);
1168
1169         return ret;
1170 }
1171
1172 int ipv4_doint_and_flush_strategy(ctl_table *table, int __user *name, int nlen,
1173                                   void __user *oldval, size_t __user *oldlenp,
1174                                   void __user *newval, size_t newlen, 
1175                                   void **context)
1176 {
1177         int *valp = table->data;
1178         int new;
1179
1180         if (!newval || !newlen)
1181                 return 0;
1182
1183         if (newlen != sizeof(int))
1184                 return -EINVAL;
1185
1186         if (get_user(new, (int __user *)newval))
1187                 return -EFAULT;
1188
1189         if (new == *valp)
1190                 return 0;
1191
1192         if (oldval && oldlenp) {
1193                 size_t len;
1194
1195                 if (get_user(len, oldlenp))
1196                         return -EFAULT;
1197
1198                 if (len) {
1199                         if (len > table->maxlen)
1200                                 len = table->maxlen;
1201                         if (copy_to_user(oldval, valp, len))
1202                                 return -EFAULT;
1203                         if (put_user(len, oldlenp))
1204                                 return -EFAULT;
1205                 }
1206         }
1207
1208         *valp = new;
1209         rt_cache_flush(0);
1210         return 1;
1211 }
1212
1213
1214 static struct devinet_sysctl_table {
1215         struct ctl_table_header *sysctl_header;
1216         ctl_table               devinet_vars[20];
1217         ctl_table               devinet_dev[2];
1218         ctl_table               devinet_conf_dir[2];
1219         ctl_table               devinet_proto_dir[2];
1220         ctl_table               devinet_root_dir[2];
1221 } devinet_sysctl = {
1222         .devinet_vars = {
1223                 {
1224                         .ctl_name       = NET_IPV4_CONF_FORWARDING,
1225                         .procname       = "forwarding",
1226                         .data           = &ipv4_devconf.forwarding,
1227                         .maxlen         = sizeof(int),
1228                         .mode           = 0644,
1229                         .proc_handler   = &devinet_sysctl_forward,
1230                 },
1231                 {
1232                         .ctl_name       = NET_IPV4_CONF_MC_FORWARDING,
1233                         .procname       = "mc_forwarding",
1234                         .data           = &ipv4_devconf.mc_forwarding,
1235                         .maxlen         = sizeof(int),
1236                         .mode           = 0444,
1237                         .proc_handler   = &proc_dointvec,
1238                 },
1239                 {
1240                         .ctl_name       = NET_IPV4_CONF_ACCEPT_REDIRECTS,
1241                         .procname       = "accept_redirects",
1242                         .data           = &ipv4_devconf.accept_redirects,
1243                         .maxlen         = sizeof(int),
1244                         .mode           = 0644,
1245                         .proc_handler   = &proc_dointvec,
1246                 },
1247                 {
1248                         .ctl_name       = NET_IPV4_CONF_SECURE_REDIRECTS,
1249                         .procname       = "secure_redirects",
1250                         .data           = &ipv4_devconf.secure_redirects,
1251                         .maxlen         = sizeof(int),
1252                         .mode           = 0644,
1253                         .proc_handler   = &proc_dointvec,
1254                 },
1255                 {
1256                         .ctl_name       = NET_IPV4_CONF_SHARED_MEDIA,
1257                         .procname       = "shared_media",
1258                         .data           = &ipv4_devconf.shared_media,
1259                         .maxlen         = sizeof(int),
1260                         .mode           = 0644,
1261                         .proc_handler   = &proc_dointvec,
1262                 },
1263                 {
1264                         .ctl_name       = NET_IPV4_CONF_RP_FILTER,
1265                         .procname       = "rp_filter",
1266                         .data           = &ipv4_devconf.rp_filter,
1267                         .maxlen         = sizeof(int),
1268                         .mode           = 0644,
1269                         .proc_handler   = &proc_dointvec,
1270                 },
1271                 {
1272                         .ctl_name       = NET_IPV4_CONF_SEND_REDIRECTS,
1273                         .procname       = "send_redirects",
1274                         .data           = &ipv4_devconf.send_redirects,
1275                         .maxlen         = sizeof(int),
1276                         .mode           = 0644,
1277                         .proc_handler   = &proc_dointvec,
1278                 },
1279                 {
1280                         .ctl_name       = NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE,
1281                         .procname       = "accept_source_route",
1282                         .data           = &ipv4_devconf.accept_source_route,
1283                         .maxlen         = sizeof(int),
1284                         .mode           = 0644,
1285                         .proc_handler   = &proc_dointvec,
1286                 },
1287                 {
1288                         .ctl_name       = NET_IPV4_CONF_PROXY_ARP,
1289                         .procname       = "proxy_arp",
1290                         .data           = &ipv4_devconf.proxy_arp,
1291                         .maxlen         = sizeof(int),
1292                         .mode           = 0644,
1293                         .proc_handler   = &proc_dointvec,
1294                 },
1295                 {
1296                         .ctl_name       = NET_IPV4_CONF_MEDIUM_ID,
1297                         .procname       = "medium_id",
1298                         .data           = &ipv4_devconf.medium_id,
1299                         .maxlen         = sizeof(int),
1300                         .mode           = 0644,
1301                         .proc_handler   = &proc_dointvec,
1302                 },
1303                 {
1304                         .ctl_name       = NET_IPV4_CONF_BOOTP_RELAY,
1305                         .procname       = "bootp_relay",
1306                         .data           = &ipv4_devconf.bootp_relay,
1307                         .maxlen         = sizeof(int),
1308                         .mode           = 0644,
1309                         .proc_handler   = &proc_dointvec,
1310                 },
1311                 {
1312                         .ctl_name       = NET_IPV4_CONF_LOG_MARTIANS,
1313                         .procname       = "log_martians",
1314                         .data           = &ipv4_devconf.log_martians,
1315                         .maxlen         = sizeof(int),
1316                         .mode           = 0644,
1317                         .proc_handler   = &proc_dointvec,
1318                 },
1319                 {
1320                         .ctl_name       = NET_IPV4_CONF_TAG,
1321                         .procname       = "tag",
1322                         .data           = &ipv4_devconf.tag,
1323                         .maxlen         = sizeof(int),
1324                         .mode           = 0644,
1325                         .proc_handler   = &proc_dointvec,
1326                 },
1327                 {
1328                         .ctl_name       = NET_IPV4_CONF_ARPFILTER,
1329                         .procname       = "arp_filter",
1330                         .data           = &ipv4_devconf.arp_filter,
1331                         .maxlen         = sizeof(int),
1332                         .mode           = 0644,
1333                         .proc_handler   = &proc_dointvec,
1334                 },
1335                 {
1336                         .ctl_name       = NET_IPV4_CONF_ARP_ANNOUNCE,
1337                         .procname       = "arp_announce",
1338                         .data           = &ipv4_devconf.arp_announce,
1339                         .maxlen         = sizeof(int),
1340                         .mode           = 0644,
1341                         .proc_handler   = &proc_dointvec,
1342                 },
1343                 {
1344                         .ctl_name       = NET_IPV4_CONF_ARP_IGNORE,
1345                         .procname       = "arp_ignore",
1346                         .data           = &ipv4_devconf.arp_ignore,
1347                         .maxlen         = sizeof(int),
1348                         .mode           = 0644,
1349                         .proc_handler   = &proc_dointvec,
1350                 },
1351                 {
1352                         .ctl_name       = NET_IPV4_CONF_NOXFRM,
1353                         .procname       = "disable_xfrm",
1354                         .data           = &ipv4_devconf.no_xfrm,
1355                         .maxlen         = sizeof(int),
1356                         .mode           = 0644,
1357                         .proc_handler   = &ipv4_doint_and_flush,
1358                         .strategy       = &ipv4_doint_and_flush_strategy,
1359                 },
1360                 {
1361                         .ctl_name       = NET_IPV4_CONF_NOPOLICY,
1362                         .procname       = "disable_policy",
1363                         .data           = &ipv4_devconf.no_policy,
1364                         .maxlen         = sizeof(int),
1365                         .mode           = 0644,
1366                         .proc_handler   = &ipv4_doint_and_flush,
1367                         .strategy       = &ipv4_doint_and_flush_strategy,
1368                 },
1369                 {
1370                         .ctl_name       = NET_IPV4_CONF_FORCE_IGMP_VERSION,
1371                         .procname       = "force_igmp_version",
1372                         .data           = &ipv4_devconf.force_igmp_version,
1373                         .maxlen         = sizeof(int),
1374                         .mode           = 0644,
1375                         .proc_handler   = &ipv4_doint_and_flush,
1376                         .strategy       = &ipv4_doint_and_flush_strategy,
1377                 },
1378         },
1379         .devinet_dev = {
1380                 {
1381                         .ctl_name       = NET_PROTO_CONF_ALL,
1382                         .procname       = "all",
1383                         .mode           = 0555,
1384                         .child          = devinet_sysctl.devinet_vars,
1385                 },
1386         },
1387         .devinet_conf_dir = {
1388                 {
1389                         .ctl_name       = NET_IPV4_CONF,
1390                         .procname       = "conf",
1391                         .mode           = 0555,
1392                         .child          = devinet_sysctl.devinet_dev,
1393                 },
1394         },
1395         .devinet_proto_dir = {
1396                 {
1397                         .ctl_name       = NET_IPV4,
1398                         .procname       = "ipv4",
1399                         .mode           = 0555,
1400                         .child          = devinet_sysctl.devinet_conf_dir,
1401                 },
1402         },
1403         .devinet_root_dir = {
1404                 {
1405                         .ctl_name       = CTL_NET,
1406                         .procname       = "net",
1407                         .mode           = 0555,
1408                         .child          = devinet_sysctl.devinet_proto_dir,
1409                 },
1410         },
1411 };
1412
1413 static void devinet_sysctl_register(struct in_device *in_dev,
1414                                     struct ipv4_devconf *p)
1415 {
1416         int i;
1417         struct net_device *dev = in_dev ? in_dev->dev : NULL;
1418         struct devinet_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
1419         char *dev_name = NULL;
1420
1421         if (!t)
1422                 return;
1423         memcpy(t, &devinet_sysctl, sizeof(*t));
1424         for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1425                 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
1426                 t->devinet_vars[i].de = NULL;
1427         }
1428
1429         if (dev) {
1430                 dev_name = dev->name; 
1431                 t->devinet_dev[0].ctl_name = dev->ifindex;
1432         } else {
1433                 dev_name = "default";
1434                 t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
1435         }
1436
1437         /* 
1438          * Make a copy of dev_name, because '.procname' is regarded as const 
1439          * by sysctl and we wouldn't want anyone to change it under our feet
1440          * (see SIOCSIFNAME).
1441          */     
1442         dev_name = net_sysctl_strdup(dev_name);
1443         if (!dev_name)
1444             goto free;
1445
1446         t->devinet_dev[0].procname    = dev_name;
1447         t->devinet_dev[0].child       = t->devinet_vars;
1448         t->devinet_dev[0].de          = NULL;
1449         t->devinet_conf_dir[0].child  = t->devinet_dev;
1450         t->devinet_conf_dir[0].de     = NULL;
1451         t->devinet_proto_dir[0].child = t->devinet_conf_dir;
1452         t->devinet_proto_dir[0].de    = NULL;
1453         t->devinet_root_dir[0].child  = t->devinet_proto_dir;
1454         t->devinet_root_dir[0].de     = NULL;
1455
1456         t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
1457         if (!t->sysctl_header)
1458             goto free_procname;
1459
1460         p->sysctl = t;
1461         return;
1462
1463         /* error path */
1464  free_procname:
1465         kfree(dev_name);
1466  free:
1467         kfree(t);
1468         return;
1469 }
1470
1471 static void devinet_sysctl_unregister(struct ipv4_devconf *p)
1472 {
1473         if (p->sysctl) {
1474                 struct devinet_sysctl_table *t = p->sysctl;
1475                 p->sysctl = NULL;
1476                 unregister_sysctl_table(t->sysctl_header);
1477                 kfree(t->devinet_dev[0].procname);
1478                 kfree(t);
1479         }
1480 }
1481 #endif
1482
1483 void __init devinet_init(void)
1484 {
1485         register_gifconf(PF_INET, inet_gifconf);
1486         register_netdevice_notifier(&ip_netdev_notifier);
1487         rtnetlink_links[PF_INET] = inet_rtnetlink_table;
1488 #ifdef CONFIG_SYSCTL
1489         devinet_sysctl.sysctl_header =
1490                 register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
1491         devinet_sysctl_register(NULL, &ipv4_devconf_dflt);
1492 #endif
1493 }
1494
1495 EXPORT_SYMBOL(devinet_ioctl);
1496 EXPORT_SYMBOL(in_dev_finish_destroy);
1497 EXPORT_SYMBOL(inet_select_addr);
1498 EXPORT_SYMBOL(inetdev_by_index);
1499 EXPORT_SYMBOL(register_inetaddr_notifier);
1500 EXPORT_SYMBOL(unregister_inetaddr_notifier);