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