vserver 2.0 rc7
[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/module.h>
36 #include <linux/types.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/string.h>
40 #include <linux/mm.h>
41 #include <linux/socket.h>
42 #include <linux/sockios.h>
43 #include <linux/in.h>
44 #include <linux/errno.h>
45 #include <linux/interrupt.h>
46 #include <linux/if_ether.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/skbuff.h>
51 #include <linux/rtnetlink.h>
52 #include <linux/init.h>
53 #include <linux/notifier.h>
54 #include <linux/inetdevice.h>
55 #include <linux/igmp.h>
56 #ifdef CONFIG_SYSCTL
57 #include <linux/sysctl.h>
58 #endif
59 #include <linux/kmod.h>
60
61 #include <net/ip.h>
62 #include <net/route.h>
63 #include <net/ip_fib.h>
64
65 struct ipv4_devconf ipv4_devconf = {
66         .accept_redirects = 1,
67         .send_redirects =  1,
68         .secure_redirects = 1,
69         .shared_media =   1,
70 };
71
72 static struct ipv4_devconf ipv4_devconf_dflt = {
73         .accept_redirects =  1,
74         .send_redirects =    1,
75         .secure_redirects =  1,
76         .shared_media =      1,
77         .accept_source_route = 1,
78 };
79
80 static void rtmsg_ifa(int event, struct in_ifaddr *);
81
82 static struct notifier_block *inetaddr_chain;
83 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
84                          int destroy);
85 #ifdef CONFIG_SYSCTL
86 static void devinet_sysctl_register(struct in_device *in_dev,
87                                     struct ipv4_devconf *p);
88 static void devinet_sysctl_unregister(struct ipv4_devconf *p);
89 #endif
90
91 /* Locks all the inet devices. */
92
93 static struct in_ifaddr *inet_alloc_ifa(void)
94 {
95         struct in_ifaddr *ifa = kmalloc(sizeof(*ifa), GFP_KERNEL);
96
97         if (ifa) {
98                 memset(ifa, 0, sizeof(*ifa));
99                 INIT_RCU_HEAD(&ifa->rcu_head);
100         }
101
102         return ifa;
103 }
104
105 static void inet_rcu_free_ifa(struct rcu_head *head)
106 {
107         struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
108         if (ifa->ifa_dev)
109                 in_dev_put(ifa->ifa_dev);
110         kfree(ifa);
111 }
112
113 static inline void inet_free_ifa(struct in_ifaddr *ifa)
114 {
115         call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
116 }
117
118 void in_dev_finish_destroy(struct in_device *idev)
119 {
120         struct net_device *dev = idev->dev;
121
122         BUG_TRAP(!idev->ifa_list);
123         BUG_TRAP(!idev->mc_list);
124 #ifdef NET_REFCNT_DEBUG
125         printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n",
126                idev, dev ? dev->name : "NIL");
127 #endif
128         dev_put(dev);
129         if (!idev->dead)
130                 printk("Freeing alive in_device %p\n", idev);
131         else {
132                 kfree(idev);
133         }
134 }
135
136 struct in_device *inetdev_init(struct net_device *dev)
137 {
138         struct in_device *in_dev;
139
140         ASSERT_RTNL();
141
142         in_dev = kmalloc(sizeof(*in_dev), GFP_KERNEL);
143         if (!in_dev)
144                 goto out;
145         memset(in_dev, 0, sizeof(*in_dev));
146         INIT_RCU_HEAD(&in_dev->rcu_head);
147         memcpy(&in_dev->cnf, &ipv4_devconf_dflt, sizeof(in_dev->cnf));
148         in_dev->cnf.sysctl = NULL;
149         in_dev->dev = dev;
150         if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL)
151                 goto out_kfree;
152         /* Reference in_dev->dev */
153         dev_hold(dev);
154 #ifdef CONFIG_SYSCTL
155         neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
156                               NET_IPV4_NEIGH, "ipv4", NULL, NULL);
157 #endif
158
159         /* Account for reference dev->ip_ptr */
160         in_dev_hold(in_dev);
161         rcu_assign_pointer(dev->ip_ptr, in_dev);
162
163 #ifdef CONFIG_SYSCTL
164         devinet_sysctl_register(in_dev, &in_dev->cnf);
165 #endif
166         ip_mc_init_dev(in_dev);
167         if (dev->flags & IFF_UP)
168                 ip_mc_up(in_dev);
169 out:
170         return in_dev;
171 out_kfree:
172         kfree(in_dev);
173         in_dev = NULL;
174         goto out;
175 }
176
177 static void in_dev_rcu_put(struct rcu_head *head)
178 {
179         struct in_device *idev = container_of(head, struct in_device, rcu_head);
180         in_dev_put(idev);
181 }
182
183 static void inetdev_destroy(struct in_device *in_dev)
184 {
185         struct in_ifaddr *ifa;
186         struct net_device *dev;
187
188         ASSERT_RTNL();
189
190         dev = in_dev->dev;
191         if (dev == &loopback_dev)
192                 return;
193
194         in_dev->dead = 1;
195
196         ip_mc_destroy_dev(in_dev);
197
198         while ((ifa = in_dev->ifa_list) != NULL) {
199                 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
200                 inet_free_ifa(ifa);
201         }
202
203 #ifdef CONFIG_SYSCTL
204         devinet_sysctl_unregister(&in_dev->cnf);
205 #endif
206
207         dev->ip_ptr = NULL;
208
209 #ifdef CONFIG_SYSCTL
210         neigh_sysctl_unregister(in_dev->arp_parms);
211 #endif
212         neigh_parms_release(&arp_tbl, in_dev->arp_parms);
213         arp_ifdown(dev);
214
215         call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
216 }
217
218 int inet_addr_onlink(struct in_device *in_dev, u32 a, u32 b)
219 {
220         rcu_read_lock();
221         for_primary_ifa(in_dev) {
222                 if (inet_ifa_match(a, ifa)) {
223                         if (!b || inet_ifa_match(b, ifa)) {
224                                 rcu_read_unlock();
225                                 return 1;
226                         }
227                 }
228         } endfor_ifa(in_dev);
229         rcu_read_unlock();
230         return 0;
231 }
232
233 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
234                          int destroy)
235 {
236         struct in_ifaddr *promote = NULL;
237         struct in_ifaddr *ifa1 = *ifap;
238
239         ASSERT_RTNL();
240
241         /* 1. Deleting primary ifaddr forces deletion all secondaries 
242          * unless alias promotion is set
243          **/
244
245         if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
246                 struct in_ifaddr *ifa;
247                 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
248
249                 while ((ifa = *ifap1) != NULL) {
250                         if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
251                             ifa1->ifa_mask != ifa->ifa_mask ||
252                             !inet_ifa_match(ifa1->ifa_address, ifa)) {
253                                 ifap1 = &ifa->ifa_next;
254                                 continue;
255                         }
256
257                         if (!IN_DEV_PROMOTE_SECONDARIES(in_dev)) {
258                                 *ifap1 = ifa->ifa_next;
259
260                                 rtmsg_ifa(RTM_DELADDR, ifa);
261                                 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa);
262                                 inet_free_ifa(ifa);
263                         } else {
264                                 promote = ifa;
265                                 break;
266                         }
267                 }
268         }
269
270         /* 2. Unlink it */
271
272         *ifap = ifa1->ifa_next;
273
274         /* 3. Announce address deletion */
275
276         /* Send message first, then call notifier.
277            At first sight, FIB update triggered by notifier
278            will refer to already deleted ifaddr, that could confuse
279            netlink listeners. It is not true: look, gated sees
280            that route deleted and if it still thinks that ifaddr
281            is valid, it will try to restore deleted routes... Grr.
282            So that, this order is correct.
283          */
284         rtmsg_ifa(RTM_DELADDR, ifa1);
285         notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
286         if (destroy) {
287                 inet_free_ifa(ifa1);
288
289                 if (!in_dev->ifa_list)
290                         inetdev_destroy(in_dev);
291         }
292
293         if (promote && IN_DEV_PROMOTE_SECONDARIES(in_dev)) {
294                 /* not sure if we should send a delete notify first? */
295                 promote->ifa_flags &= ~IFA_F_SECONDARY;
296                 rtmsg_ifa(RTM_NEWADDR, promote);
297                 notifier_call_chain(&inetaddr_chain, NETDEV_UP, promote);
298         }
299 }
300
301 static int inet_insert_ifa(struct in_ifaddr *ifa)
302 {
303         struct in_device *in_dev = ifa->ifa_dev;
304         struct in_ifaddr *ifa1, **ifap, **last_primary;
305
306         ASSERT_RTNL();
307
308         if (!ifa->ifa_local) {
309                 inet_free_ifa(ifa);
310                 return 0;
311         }
312
313         ifa->ifa_flags &= ~IFA_F_SECONDARY;
314         last_primary = &in_dev->ifa_list;
315
316         for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
317              ifap = &ifa1->ifa_next) {
318                 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
319                     ifa->ifa_scope <= ifa1->ifa_scope)
320                         last_primary = &ifa1->ifa_next;
321                 if (ifa1->ifa_mask == ifa->ifa_mask &&
322                     inet_ifa_match(ifa1->ifa_address, ifa)) {
323                         if (ifa1->ifa_local == ifa->ifa_local) {
324                                 inet_free_ifa(ifa);
325                                 return -EEXIST;
326                         }
327                         if (ifa1->ifa_scope != ifa->ifa_scope) {
328                                 inet_free_ifa(ifa);
329                                 return -EINVAL;
330                         }
331                         ifa->ifa_flags |= IFA_F_SECONDARY;
332                 }
333         }
334
335         if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
336                 net_srandom(ifa->ifa_local);
337                 ifap = last_primary;
338         }
339
340         ifa->ifa_next = *ifap;
341         *ifap = ifa;
342
343         /* Send message first, then call notifier.
344            Notifier will trigger FIB update, so that
345            listeners of netlink will know about new ifaddr */
346         rtmsg_ifa(RTM_NEWADDR, ifa);
347         notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
348
349         return 0;
350 }
351
352 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
353 {
354         struct in_device *in_dev = __in_dev_get(dev);
355
356         ASSERT_RTNL();
357
358         if (!in_dev) {
359                 in_dev = inetdev_init(dev);
360                 if (!in_dev) {
361                         inet_free_ifa(ifa);
362                         return -ENOBUFS;
363                 }
364         }
365         if (ifa->ifa_dev != in_dev) {
366                 BUG_TRAP(!ifa->ifa_dev);
367                 in_dev_hold(in_dev);
368                 ifa->ifa_dev = in_dev;
369         }
370         if (LOOPBACK(ifa->ifa_local))
371                 ifa->ifa_scope = RT_SCOPE_HOST;
372         return inet_insert_ifa(ifa);
373 }
374
375 struct in_device *inetdev_by_index(int ifindex)
376 {
377         struct net_device *dev;
378         struct in_device *in_dev = NULL;
379         read_lock(&dev_base_lock);
380         dev = __dev_get_by_index(ifindex);
381         if (dev)
382                 in_dev = in_dev_get(dev);
383         read_unlock(&dev_base_lock);
384         return in_dev;
385 }
386
387 /* Called only from RTNL semaphored context. No locks. */
388
389 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, u32 prefix,
390                                     u32 mask)
391 {
392         ASSERT_RTNL();
393
394         for_primary_ifa(in_dev) {
395                 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
396                         return ifa;
397         } endfor_ifa(in_dev);
398         return NULL;
399 }
400
401 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
402 {
403         struct rtattr **rta = arg;
404         struct in_device *in_dev;
405         struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
406         struct in_ifaddr *ifa, **ifap;
407
408         ASSERT_RTNL();
409
410         if ((in_dev = inetdev_by_index(ifm->ifa_index)) == NULL)
411                 goto out;
412         __in_dev_put(in_dev);
413
414         for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
415              ifap = &ifa->ifa_next) {
416                 if ((rta[IFA_LOCAL - 1] &&
417                      memcmp(RTA_DATA(rta[IFA_LOCAL - 1]),
418                             &ifa->ifa_local, 4)) ||
419                     (rta[IFA_LABEL - 1] &&
420                      rtattr_strcmp(rta[IFA_LABEL - 1], ifa->ifa_label)) ||
421                     (rta[IFA_ADDRESS - 1] &&
422                      (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
423                       !inet_ifa_match(*(u32*)RTA_DATA(rta[IFA_ADDRESS - 1]),
424                                       ifa))))
425                         continue;
426                 inet_del_ifa(in_dev, ifap, 1);
427                 return 0;
428         }
429 out:
430         return -EADDRNOTAVAIL;
431 }
432
433 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
434 {
435         struct rtattr **rta = arg;
436         struct net_device *dev;
437         struct in_device *in_dev;
438         struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
439         struct in_ifaddr *ifa;
440         int rc = -EINVAL;
441
442         ASSERT_RTNL();
443
444         if (ifm->ifa_prefixlen > 32 || !rta[IFA_LOCAL - 1])
445                 goto out;
446
447         rc = -ENODEV;
448         if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
449                 goto out;
450
451         rc = -ENOBUFS;
452         if ((in_dev = __in_dev_get(dev)) == NULL) {
453                 in_dev = inetdev_init(dev);
454                 if (!in_dev)
455                         goto out;
456         }
457
458         if ((ifa = inet_alloc_ifa()) == NULL)
459                 goto out;
460
461         if (!rta[IFA_ADDRESS - 1])
462                 rta[IFA_ADDRESS - 1] = rta[IFA_LOCAL - 1];
463         memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL - 1]), 4);
464         memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS - 1]), 4);
465         ifa->ifa_prefixlen = ifm->ifa_prefixlen;
466         ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
467         if (rta[IFA_BROADCAST - 1])
468                 memcpy(&ifa->ifa_broadcast,
469                        RTA_DATA(rta[IFA_BROADCAST - 1]), 4);
470         if (rta[IFA_ANYCAST - 1])
471                 memcpy(&ifa->ifa_anycast, RTA_DATA(rta[IFA_ANYCAST - 1]), 4);
472         ifa->ifa_flags = ifm->ifa_flags;
473         ifa->ifa_scope = ifm->ifa_scope;
474         in_dev_hold(in_dev);
475         ifa->ifa_dev   = in_dev;
476         if (rta[IFA_LABEL - 1])
477                 rtattr_strlcpy(ifa->ifa_label, rta[IFA_LABEL - 1], IFNAMSIZ);
478         else
479                 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
480
481         rc = inet_insert_ifa(ifa);
482 out:
483         return rc;
484 }
485
486 /*
487  *      Determine a default network mask, based on the IP address.
488  */
489
490 static __inline__ int inet_abc_len(u32 addr)
491 {
492         int rc = -1;    /* Something else, probably a multicast. */
493
494         if (ZERONET(addr))
495                 rc = 0;
496         else {
497                 addr = ntohl(addr);
498
499                 if (IN_CLASSA(addr))
500                         rc = 8;
501                 else if (IN_CLASSB(addr))
502                         rc = 16;
503                 else if (IN_CLASSC(addr))
504                         rc = 24;
505         }
506
507         return rc;
508 }
509
510 /*
511         Check that a device is not member of the ipv4root assigned to the process
512         Return true if this is the case
513
514         If the process is not bound to specific IP, then it returns 0 (all
515         interface are fine).
516 */
517 static inline int devinet_notiproot (struct in_ifaddr *ifa)
518 {
519         int ret = 0;
520         struct nx_info *nxi;
521
522         if ((nxi = current->nx_info)) {
523                 int i;
524                 int nbip = nxi->nbipv4;
525                 __u32 addr = ifa->ifa_local;
526                 ret = 1;
527                 for (i=0; i<nbip; i++) {
528                         if(nxi->ipv4[i] == addr) {
529                                 ret = 0;
530                                 break;
531                         }
532                 }
533         }
534         return ret;
535 }
536
537
538 int devinet_ioctl(unsigned int cmd, void __user *arg)
539 {
540         struct ifreq ifr;
541         struct sockaddr_in sin_orig;
542         struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
543         struct in_device *in_dev;
544         struct in_ifaddr **ifap = NULL;
545         struct in_ifaddr *ifa = NULL;
546         struct net_device *dev;
547         char *colon;
548         int ret = -EFAULT;
549         int tryaddrmatch = 0;
550
551         /*
552          *      Fetch the caller's info block into kernel space
553          */
554
555         if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
556                 goto out;
557         ifr.ifr_name[IFNAMSIZ - 1] = 0;
558
559         /* save original address for comparison */
560         memcpy(&sin_orig, sin, sizeof(*sin));
561
562         colon = strchr(ifr.ifr_name, ':');
563         if (colon)
564                 *colon = 0;
565
566 #ifdef CONFIG_KMOD
567         dev_load(ifr.ifr_name);
568 #endif
569
570         switch(cmd) {
571         case SIOCGIFADDR:       /* Get interface address */
572         case SIOCGIFBRDADDR:    /* Get the broadcast address */
573         case SIOCGIFDSTADDR:    /* Get the destination address */
574         case SIOCGIFNETMASK:    /* Get the netmask for the interface */
575                 /* Note that these ioctls will not sleep,
576                    so that we do not impose a lock.
577                    One day we will be forced to put shlock here (I mean SMP)
578                  */
579                 tryaddrmatch = (sin_orig.sin_family == AF_INET);
580                 memset(sin, 0, sizeof(*sin));
581                 sin->sin_family = AF_INET;
582                 break;
583
584         case SIOCSIFFLAGS:
585                 ret = -EACCES;
586                 if (!capable(CAP_NET_ADMIN))
587                         goto out;
588                 break;
589         case SIOCSIFADDR:       /* Set interface address (and family) */
590         case SIOCSIFBRDADDR:    /* Set the broadcast address */
591         case SIOCSIFDSTADDR:    /* Set the destination address */
592         case SIOCSIFNETMASK:    /* Set the netmask for the interface */
593                 ret = -EACCES;
594                 if (!capable(CAP_NET_ADMIN))
595                         goto out;
596                 ret = -EINVAL;
597                 if (sin->sin_family != AF_INET)
598                         goto out;
599                 break;
600         default:
601                 ret = -EINVAL;
602                 goto out;
603         }
604
605         rtnl_lock();
606
607         ret = -ENODEV;
608         if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL)
609                 goto done;
610
611         if (colon)
612                 *colon = ':';
613
614         if ((in_dev = __in_dev_get(dev)) != NULL) {
615                 if (tryaddrmatch) {
616                         /* Matthias Andree */
617                         /* compare label and address (4.4BSD style) */
618                         /* note: we only do this for a limited set of ioctls
619                            and only if the original address family was AF_INET.
620                            This is checked above. */
621                         for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
622                              ifap = &ifa->ifa_next) {
623                                 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
624                                     sin_orig.sin_addr.s_addr ==
625                                                         ifa->ifa_address) {
626                                         break; /* found */
627                                 }
628                         }
629                 }
630                 /* we didn't get a match, maybe the application is
631                    4.3BSD-style and passed in junk so we fall back to
632                    comparing just the label */
633                 if (!ifa) {
634                         for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
635                              ifap = &ifa->ifa_next)
636                                 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
637                                         break;
638                 }
639         }
640
641         ret = -EADDRNOTAVAIL;
642         if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
643                 goto done;
644         if (vx_flags(VXF_HIDE_NETIF, 0) &&
645                 !ifa_in_nx_info(ifa, current->nx_info))
646                 goto done;
647
648         switch(cmd) {
649         case SIOCGIFADDR:       /* Get interface address */
650                 sin->sin_addr.s_addr = ifa->ifa_local;
651                 goto rarok;
652
653         case SIOCGIFBRDADDR:    /* Get the broadcast address */
654                 sin->sin_addr.s_addr = ifa->ifa_broadcast;
655                 goto rarok;
656
657         case SIOCGIFDSTADDR:    /* Get the destination address */
658                 sin->sin_addr.s_addr = ifa->ifa_address;
659                 goto rarok;
660
661         case SIOCGIFNETMASK:    /* Get the netmask for the interface */
662                 sin->sin_addr.s_addr = ifa->ifa_mask;
663                 goto rarok;
664
665         case SIOCSIFFLAGS:
666                 if (colon) {
667                         ret = -EADDRNOTAVAIL;
668                         if (!ifa)
669                                 break;
670                         ret = 0;
671                         if (!(ifr.ifr_flags & IFF_UP))
672                                 inet_del_ifa(in_dev, ifap, 1);
673                         break;
674                 }
675                 ret = dev_change_flags(dev, ifr.ifr_flags);
676                 break;
677
678         case SIOCSIFADDR:       /* Set interface address (and family) */
679                 ret = -EINVAL;
680                 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
681                         break;
682
683                 if (!ifa) {
684                         ret = -ENOBUFS;
685                         if ((ifa = inet_alloc_ifa()) == NULL)
686                                 break;
687                         if (colon)
688                                 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
689                         else
690                                 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
691                 } else {
692                         ret = 0;
693                         if (ifa->ifa_local == sin->sin_addr.s_addr)
694                                 break;
695                         inet_del_ifa(in_dev, ifap, 0);
696                         ifa->ifa_broadcast = 0;
697                         ifa->ifa_anycast = 0;
698                 }
699
700                 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
701
702                 if (!(dev->flags & IFF_POINTOPOINT)) {
703                         ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
704                         ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
705                         if ((dev->flags & IFF_BROADCAST) &&
706                             ifa->ifa_prefixlen < 31)
707                                 ifa->ifa_broadcast = ifa->ifa_address |
708                                                      ~ifa->ifa_mask;
709                 } else {
710                         ifa->ifa_prefixlen = 32;
711                         ifa->ifa_mask = inet_make_mask(32);
712                 }
713                 ret = inet_set_ifa(dev, ifa);
714                 break;
715
716         case SIOCSIFBRDADDR:    /* Set the broadcast address */
717                 ret = 0;
718                 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
719                         inet_del_ifa(in_dev, ifap, 0);
720                         ifa->ifa_broadcast = sin->sin_addr.s_addr;
721                         inet_insert_ifa(ifa);
722                 }
723                 break;
724
725         case SIOCSIFDSTADDR:    /* Set the destination address */
726                 ret = 0;
727                 if (ifa->ifa_address == sin->sin_addr.s_addr)
728                         break;
729                 ret = -EINVAL;
730                 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
731                         break;
732                 ret = 0;
733                 inet_del_ifa(in_dev, ifap, 0);
734                 ifa->ifa_address = sin->sin_addr.s_addr;
735                 inet_insert_ifa(ifa);
736                 break;
737
738         case SIOCSIFNETMASK:    /* Set the netmask for the interface */
739
740                 /*
741                  *      The mask we set must be legal.
742                  */
743                 ret = -EINVAL;
744                 if (bad_mask(sin->sin_addr.s_addr, 0))
745                         break;
746                 ret = 0;
747                 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
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|~ifa->ifa_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(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(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(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(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(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 notifier_chain_register(&inetaddr_chain, nb);
952 }
953
954 int unregister_inetaddr_notifier(struct notifier_block *nb)
955 {
956         return 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(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)
1067 {
1068         struct ifaddrmsg *ifm;
1069         struct nlmsghdr  *nlh;
1070         unsigned char    *b = skb->tail;
1071
1072         nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
1073         if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
1074         ifm = NLMSG_DATA(nlh);
1075         ifm->ifa_family = AF_INET;
1076         ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1077         ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1078         ifm->ifa_scope = ifa->ifa_scope;
1079         ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1080         if (ifa->ifa_address)
1081                 RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
1082         if (ifa->ifa_local)
1083                 RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
1084         if (ifa->ifa_broadcast)
1085                 RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
1086         if (ifa->ifa_anycast)
1087                 RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
1088         if (ifa->ifa_label[0])
1089                 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
1090         nlh->nlmsg_len = skb->tail - b;
1091         return skb->len;
1092
1093 nlmsg_failure:
1094 rtattr_failure:
1095         skb_trim(skb, b - skb->data);
1096         return -1;
1097 }
1098
1099 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1100 {
1101         int idx, ip_idx;
1102         struct net_device *dev;
1103         struct in_device *in_dev;
1104         struct in_ifaddr *ifa;
1105         struct sock *sk = skb->sk;
1106         int s_ip_idx, s_idx = cb->args[0];
1107
1108         s_ip_idx = ip_idx = cb->args[1];
1109         read_lock(&dev_base_lock);
1110         for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
1111                 if (idx < s_idx)
1112                         continue;
1113                 if (idx > s_idx)
1114                         s_ip_idx = 0;
1115                 rcu_read_lock();
1116                 if ((in_dev = __in_dev_get(dev)) == NULL) {
1117                         rcu_read_unlock();
1118                         continue;
1119                 }
1120
1121                 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1122                      ifa = ifa->ifa_next, ip_idx++) {
1123                         if (sk && vx_info_flags(sk->sk_vx_info, VXF_HIDE_NETIF, 0) &&
1124                                 !ifa_in_nx_info(ifa, sk->sk_nx_info))
1125                                 continue;
1126                         if (ip_idx < s_ip_idx)
1127                                 continue;
1128                         if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1129                                              cb->nlh->nlmsg_seq,
1130                                              RTM_NEWADDR) <= 0) {
1131                                 rcu_read_unlock();
1132                                 goto done;
1133                         }
1134                 }
1135                 rcu_read_unlock();
1136         }
1137
1138 done:
1139         read_unlock(&dev_base_lock);
1140         cb->args[0] = idx;
1141         cb->args[1] = ip_idx;
1142
1143         return skb->len;
1144 }
1145
1146 static void rtmsg_ifa(int event, struct in_ifaddr* ifa)
1147 {
1148         int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + 128);
1149         struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);
1150
1151         if (!skb)
1152                 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, ENOBUFS);
1153         else if (inet_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
1154                 kfree_skb(skb);
1155                 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, EINVAL);
1156         } else {
1157                 NETLINK_CB(skb).dst_groups = RTMGRP_IPV4_IFADDR;
1158                 netlink_broadcast(rtnl, skb, 0, RTMGRP_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(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 = net_sysctl_strdup(dev_name);
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);