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