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