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