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