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