[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / net / ipv4 / devinet.c
blob3cc96730c4ed285d4a1f65e69ca659290b26abe0
1 /*
2 * NET3 IP device support routines.
4 * Version: $Id: devinet.c,v 1.44 2001/10/31 21:55:54 davem Exp $
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.
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>
16 * Additional Authors:
17 * Alan Cox, <gw4pts@gw4pts.ampr.org>
18 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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.
30 #include <linux/config.h>
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include <linux/bitops.h>
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/string.h>
40 #include <linux/mm.h>
41 #include <linux/socket.h>
42 #include <linux/sockios.h>
43 #include <linux/in.h>
44 #include <linux/errno.h>
45 #include <linux/interrupt.h>
46 #include <linux/if_ether.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/skbuff.h>
51 #include <linux/rtnetlink.h>
52 #include <linux/init.h>
53 #include <linux/notifier.h>
54 #include <linux/inetdevice.h>
55 #include <linux/igmp.h>
56 #ifdef CONFIG_SYSCTL
57 #include <linux/sysctl.h>
58 #endif
59 #include <linux/kmod.h>
61 #include <net/ip.h>
62 #include <net/route.h>
63 #include <net/ip_fib.h>
65 struct ipv4_devconf ipv4_devconf = {
66 .accept_redirects = 1,
67 .send_redirects = 1,
68 .secure_redirects = 1,
69 .shared_media = 1,
72 static struct ipv4_devconf ipv4_devconf_dflt = {
73 .accept_redirects = 1,
74 .send_redirects = 1,
75 .secure_redirects = 1,
76 .shared_media = 1,
77 .accept_source_route = 1,
80 static void rtmsg_ifa(int event, struct in_ifaddr *);
82 static struct notifier_block *inetaddr_chain;
83 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
84 int destroy);
85 #ifdef CONFIG_SYSCTL
86 static void devinet_sysctl_register(struct in_device *in_dev,
87 struct ipv4_devconf *p);
88 static void devinet_sysctl_unregister(struct ipv4_devconf *p);
89 #endif
91 /* Locks all the inet devices. */
93 static struct in_ifaddr *inet_alloc_ifa(void)
95 struct in_ifaddr *ifa = kmalloc(sizeof(*ifa), GFP_KERNEL);
97 if (ifa) {
98 memset(ifa, 0, sizeof(*ifa));
99 INIT_RCU_HEAD(&ifa->rcu_head);
102 return ifa;
105 static void inet_rcu_free_ifa(struct rcu_head *head)
107 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
108 if (ifa->ifa_dev)
109 in_dev_put(ifa->ifa_dev);
110 kfree(ifa);
113 static inline void inet_free_ifa(struct in_ifaddr *ifa)
115 call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
118 void in_dev_finish_destroy(struct in_device *idev)
120 struct net_device *dev = idev->dev;
122 BUG_TRAP(!idev->ifa_list);
123 BUG_TRAP(!idev->mc_list);
124 #ifdef NET_REFCNT_DEBUG
125 printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n",
126 idev, dev ? dev->name : "NIL");
127 #endif
128 dev_put(dev);
129 if (!idev->dead)
130 printk("Freeing alive in_device %p\n", idev);
131 else {
132 kfree(idev);
136 struct in_device *inetdev_init(struct net_device *dev)
138 struct in_device *in_dev;
140 ASSERT_RTNL();
142 in_dev = kmalloc(sizeof(*in_dev), GFP_KERNEL);
143 if (!in_dev)
144 goto out;
145 memset(in_dev, 0, sizeof(*in_dev));
146 INIT_RCU_HEAD(&in_dev->rcu_head);
147 memcpy(&in_dev->cnf, &ipv4_devconf_dflt, sizeof(in_dev->cnf));
148 in_dev->cnf.sysctl = NULL;
149 in_dev->dev = dev;
150 if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL)
151 goto out_kfree;
152 /* Reference in_dev->dev */
153 dev_hold(dev);
154 #ifdef CONFIG_SYSCTL
155 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
156 NET_IPV4_NEIGH, "ipv4", NULL, NULL);
157 #endif
159 /* Account for reference dev->ip_ptr */
160 in_dev_hold(in_dev);
161 rcu_assign_pointer(dev->ip_ptr, in_dev);
163 #ifdef CONFIG_SYSCTL
164 devinet_sysctl_register(in_dev, &in_dev->cnf);
165 #endif
166 ip_mc_init_dev(in_dev);
167 if (dev->flags & IFF_UP)
168 ip_mc_up(in_dev);
169 out:
170 return in_dev;
171 out_kfree:
172 kfree(in_dev);
173 in_dev = NULL;
174 goto out;
177 static void in_dev_rcu_put(struct rcu_head *head)
179 struct in_device *idev = container_of(head, struct in_device, rcu_head);
180 in_dev_put(idev);
183 static void inetdev_destroy(struct in_device *in_dev)
185 struct in_ifaddr *ifa;
186 struct net_device *dev;
188 ASSERT_RTNL();
190 dev = in_dev->dev;
191 if (dev == &loopback_dev)
192 return;
194 in_dev->dead = 1;
196 ip_mc_destroy_dev(in_dev);
198 while ((ifa = in_dev->ifa_list) != NULL) {
199 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
200 inet_free_ifa(ifa);
203 #ifdef CONFIG_SYSCTL
204 devinet_sysctl_unregister(&in_dev->cnf);
205 #endif
207 dev->ip_ptr = NULL;
209 #ifdef CONFIG_SYSCTL
210 neigh_sysctl_unregister(in_dev->arp_parms);
211 #endif
212 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
213 arp_ifdown(dev);
215 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
218 int inet_addr_onlink(struct in_device *in_dev, u32 a, u32 b)
220 rcu_read_lock();
221 for_primary_ifa(in_dev) {
222 if (inet_ifa_match(a, ifa)) {
223 if (!b || inet_ifa_match(b, ifa)) {
224 rcu_read_unlock();
225 return 1;
228 } endfor_ifa(in_dev);
229 rcu_read_unlock();
230 return 0;
233 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
234 int destroy)
236 struct in_ifaddr *ifa1 = *ifap;
238 ASSERT_RTNL();
240 /* 1. Deleting primary ifaddr forces deletion all secondaries */
242 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
243 struct in_ifaddr *ifa;
244 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
246 while ((ifa = *ifap1) != NULL) {
247 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
248 ifa1->ifa_mask != ifa->ifa_mask ||
249 !inet_ifa_match(ifa1->ifa_address, ifa)) {
250 ifap1 = &ifa->ifa_next;
251 continue;
254 *ifap1 = ifa->ifa_next;
256 rtmsg_ifa(RTM_DELADDR, ifa);
257 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa);
258 inet_free_ifa(ifa);
262 /* 2. Unlink it */
264 *ifap = ifa1->ifa_next;
266 /* 3. Announce address deletion */
268 /* Send message first, then call notifier.
269 At first sight, FIB update triggered by notifier
270 will refer to already deleted ifaddr, that could confuse
271 netlink listeners. It is not true: look, gated sees
272 that route deleted and if it still thinks that ifaddr
273 is valid, it will try to restore deleted routes... Grr.
274 So that, this order is correct.
276 rtmsg_ifa(RTM_DELADDR, ifa1);
277 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
278 if (destroy) {
279 inet_free_ifa(ifa1);
281 if (!in_dev->ifa_list)
282 inetdev_destroy(in_dev);
286 static int inet_insert_ifa(struct in_ifaddr *ifa)
288 struct in_device *in_dev = ifa->ifa_dev;
289 struct in_ifaddr *ifa1, **ifap, **last_primary;
291 ASSERT_RTNL();
293 if (!ifa->ifa_local) {
294 inet_free_ifa(ifa);
295 return 0;
298 ifa->ifa_flags &= ~IFA_F_SECONDARY;
299 last_primary = &in_dev->ifa_list;
301 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
302 ifap = &ifa1->ifa_next) {
303 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
304 ifa->ifa_scope <= ifa1->ifa_scope)
305 last_primary = &ifa1->ifa_next;
306 if (ifa1->ifa_mask == ifa->ifa_mask &&
307 inet_ifa_match(ifa1->ifa_address, ifa)) {
308 if (ifa1->ifa_local == ifa->ifa_local) {
309 inet_free_ifa(ifa);
310 return -EEXIST;
312 if (ifa1->ifa_scope != ifa->ifa_scope) {
313 inet_free_ifa(ifa);
314 return -EINVAL;
316 ifa->ifa_flags |= IFA_F_SECONDARY;
320 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
321 net_srandom(ifa->ifa_local);
322 ifap = last_primary;
325 ifa->ifa_next = *ifap;
326 *ifap = ifa;
328 /* Send message first, then call notifier.
329 Notifier will trigger FIB update, so that
330 listeners of netlink will know about new ifaddr */
331 rtmsg_ifa(RTM_NEWADDR, ifa);
332 notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
334 return 0;
337 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
339 struct in_device *in_dev = __in_dev_get(dev);
341 ASSERT_RTNL();
343 if (!in_dev) {
344 in_dev = inetdev_init(dev);
345 if (!in_dev) {
346 inet_free_ifa(ifa);
347 return -ENOBUFS;
350 if (ifa->ifa_dev != in_dev) {
351 BUG_TRAP(!ifa->ifa_dev);
352 in_dev_hold(in_dev);
353 ifa->ifa_dev = in_dev;
355 if (LOOPBACK(ifa->ifa_local))
356 ifa->ifa_scope = RT_SCOPE_HOST;
357 return inet_insert_ifa(ifa);
360 struct in_device *inetdev_by_index(int ifindex)
362 struct net_device *dev;
363 struct in_device *in_dev = NULL;
364 read_lock(&dev_base_lock);
365 dev = __dev_get_by_index(ifindex);
366 if (dev)
367 in_dev = in_dev_get(dev);
368 read_unlock(&dev_base_lock);
369 return in_dev;
372 /* Called only from RTNL semaphored context. No locks. */
374 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, u32 prefix,
375 u32 mask)
377 ASSERT_RTNL();
379 for_primary_ifa(in_dev) {
380 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
381 return ifa;
382 } endfor_ifa(in_dev);
383 return NULL;
386 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
388 struct rtattr **rta = arg;
389 struct in_device *in_dev;
390 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
391 struct in_ifaddr *ifa, **ifap;
393 ASSERT_RTNL();
395 if ((in_dev = inetdev_by_index(ifm->ifa_index)) == NULL)
396 goto out;
397 __in_dev_put(in_dev);
399 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
400 ifap = &ifa->ifa_next) {
401 if ((rta[IFA_LOCAL - 1] &&
402 memcmp(RTA_DATA(rta[IFA_LOCAL - 1]),
403 &ifa->ifa_local, 4)) ||
404 (rta[IFA_LABEL - 1] &&
405 rtattr_strcmp(rta[IFA_LABEL - 1], ifa->ifa_label)) ||
406 (rta[IFA_ADDRESS - 1] &&
407 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
408 !inet_ifa_match(*(u32*)RTA_DATA(rta[IFA_ADDRESS - 1]),
409 ifa))))
410 continue;
411 inet_del_ifa(in_dev, ifap, 1);
412 return 0;
414 out:
415 return -EADDRNOTAVAIL;
418 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
420 struct rtattr **rta = arg;
421 struct net_device *dev;
422 struct in_device *in_dev;
423 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
424 struct in_ifaddr *ifa;
425 int rc = -EINVAL;
427 ASSERT_RTNL();
429 if (ifm->ifa_prefixlen > 32 || !rta[IFA_LOCAL - 1])
430 goto out;
432 rc = -ENODEV;
433 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
434 goto out;
436 rc = -ENOBUFS;
437 if ((in_dev = __in_dev_get(dev)) == NULL) {
438 in_dev = inetdev_init(dev);
439 if (!in_dev)
440 goto out;
443 if ((ifa = inet_alloc_ifa()) == NULL)
444 goto out;
446 if (!rta[IFA_ADDRESS - 1])
447 rta[IFA_ADDRESS - 1] = rta[IFA_LOCAL - 1];
448 memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL - 1]), 4);
449 memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS - 1]), 4);
450 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
451 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
452 if (rta[IFA_BROADCAST - 1])
453 memcpy(&ifa->ifa_broadcast,
454 RTA_DATA(rta[IFA_BROADCAST - 1]), 4);
455 if (rta[IFA_ANYCAST - 1])
456 memcpy(&ifa->ifa_anycast, RTA_DATA(rta[IFA_ANYCAST - 1]), 4);
457 ifa->ifa_flags = ifm->ifa_flags;
458 ifa->ifa_scope = ifm->ifa_scope;
459 in_dev_hold(in_dev);
460 ifa->ifa_dev = in_dev;
461 if (rta[IFA_LABEL - 1])
462 rtattr_strlcpy(ifa->ifa_label, rta[IFA_LABEL - 1], IFNAMSIZ);
463 else
464 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
466 rc = inet_insert_ifa(ifa);
467 out:
468 return rc;
472 * Determine a default network mask, based on the IP address.
475 static __inline__ int inet_abc_len(u32 addr)
477 int rc = -1; /* Something else, probably a multicast. */
479 if (ZERONET(addr))
480 rc = 0;
481 else {
482 addr = ntohl(addr);
484 if (IN_CLASSA(addr))
485 rc = 8;
486 else if (IN_CLASSB(addr))
487 rc = 16;
488 else if (IN_CLASSC(addr))
489 rc = 24;
492 return rc;
496 int devinet_ioctl(unsigned int cmd, void __user *arg)
498 struct ifreq ifr;
499 struct sockaddr_in sin_orig;
500 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
501 struct in_device *in_dev;
502 struct in_ifaddr **ifap = NULL;
503 struct in_ifaddr *ifa = NULL;
504 struct net_device *dev;
505 char *colon;
506 int ret = -EFAULT;
507 int tryaddrmatch = 0;
510 * Fetch the caller's info block into kernel space
513 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
514 goto out;
515 ifr.ifr_name[IFNAMSIZ - 1] = 0;
517 /* save original address for comparison */
518 memcpy(&sin_orig, sin, sizeof(*sin));
520 colon = strchr(ifr.ifr_name, ':');
521 if (colon)
522 *colon = 0;
524 #ifdef CONFIG_KMOD
525 dev_load(ifr.ifr_name);
526 #endif
528 switch(cmd) {
529 case SIOCGIFADDR: /* Get interface address */
530 case SIOCGIFBRDADDR: /* Get the broadcast address */
531 case SIOCGIFDSTADDR: /* Get the destination address */
532 case SIOCGIFNETMASK: /* Get the netmask for the interface */
533 /* Note that these ioctls will not sleep,
534 so that we do not impose a lock.
535 One day we will be forced to put shlock here (I mean SMP)
537 tryaddrmatch = (sin_orig.sin_family == AF_INET);
538 memset(sin, 0, sizeof(*sin));
539 sin->sin_family = AF_INET;
540 break;
542 case SIOCSIFFLAGS:
543 ret = -EACCES;
544 if (!capable(CAP_NET_ADMIN))
545 goto out;
546 break;
547 case SIOCSIFADDR: /* Set interface address (and family) */
548 case SIOCSIFBRDADDR: /* Set the broadcast address */
549 case SIOCSIFDSTADDR: /* Set the destination address */
550 case SIOCSIFNETMASK: /* Set the netmask for the interface */
551 ret = -EACCES;
552 if (!capable(CAP_NET_ADMIN))
553 goto out;
554 ret = -EINVAL;
555 if (sin->sin_family != AF_INET)
556 goto out;
557 break;
558 default:
559 ret = -EINVAL;
560 goto out;
563 rtnl_lock();
565 ret = -ENODEV;
566 if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL)
567 goto done;
569 if (colon)
570 *colon = ':';
572 if ((in_dev = __in_dev_get(dev)) != NULL) {
573 if (tryaddrmatch) {
574 /* Matthias Andree */
575 /* compare label and address (4.4BSD style) */
576 /* note: we only do this for a limited set of ioctls
577 and only if the original address family was AF_INET.
578 This is checked above. */
579 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
580 ifap = &ifa->ifa_next) {
581 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
582 sin_orig.sin_addr.s_addr ==
583 ifa->ifa_address) {
584 break; /* found */
588 /* we didn't get a match, maybe the application is
589 4.3BSD-style and passed in junk so we fall back to
590 comparing just the label */
591 if (!ifa) {
592 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
593 ifap = &ifa->ifa_next)
594 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
595 break;
599 ret = -EADDRNOTAVAIL;
600 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
601 goto done;
603 switch(cmd) {
604 case SIOCGIFADDR: /* Get interface address */
605 sin->sin_addr.s_addr = ifa->ifa_local;
606 goto rarok;
608 case SIOCGIFBRDADDR: /* Get the broadcast address */
609 sin->sin_addr.s_addr = ifa->ifa_broadcast;
610 goto rarok;
612 case SIOCGIFDSTADDR: /* Get the destination address */
613 sin->sin_addr.s_addr = ifa->ifa_address;
614 goto rarok;
616 case SIOCGIFNETMASK: /* Get the netmask for the interface */
617 sin->sin_addr.s_addr = ifa->ifa_mask;
618 goto rarok;
620 case SIOCSIFFLAGS:
621 if (colon) {
622 ret = -EADDRNOTAVAIL;
623 if (!ifa)
624 break;
625 ret = 0;
626 if (!(ifr.ifr_flags & IFF_UP))
627 inet_del_ifa(in_dev, ifap, 1);
628 break;
630 ret = dev_change_flags(dev, ifr.ifr_flags);
631 break;
633 case SIOCSIFADDR: /* Set interface address (and family) */
634 ret = -EINVAL;
635 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
636 break;
638 if (!ifa) {
639 ret = -ENOBUFS;
640 if ((ifa = inet_alloc_ifa()) == NULL)
641 break;
642 if (colon)
643 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
644 else
645 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
646 } else {
647 ret = 0;
648 if (ifa->ifa_local == sin->sin_addr.s_addr)
649 break;
650 inet_del_ifa(in_dev, ifap, 0);
651 ifa->ifa_broadcast = 0;
652 ifa->ifa_anycast = 0;
655 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
657 if (!(dev->flags & IFF_POINTOPOINT)) {
658 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
659 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
660 if ((dev->flags & IFF_BROADCAST) &&
661 ifa->ifa_prefixlen < 31)
662 ifa->ifa_broadcast = ifa->ifa_address |
663 ~ifa->ifa_mask;
664 } else {
665 ifa->ifa_prefixlen = 32;
666 ifa->ifa_mask = inet_make_mask(32);
668 ret = inet_set_ifa(dev, ifa);
669 break;
671 case SIOCSIFBRDADDR: /* Set the broadcast address */
672 ret = 0;
673 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
674 inet_del_ifa(in_dev, ifap, 0);
675 ifa->ifa_broadcast = sin->sin_addr.s_addr;
676 inet_insert_ifa(ifa);
678 break;
680 case SIOCSIFDSTADDR: /* Set the destination address */
681 ret = 0;
682 if (ifa->ifa_address == sin->sin_addr.s_addr)
683 break;
684 ret = -EINVAL;
685 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
686 break;
687 ret = 0;
688 inet_del_ifa(in_dev, ifap, 0);
689 ifa->ifa_address = sin->sin_addr.s_addr;
690 inet_insert_ifa(ifa);
691 break;
693 case SIOCSIFNETMASK: /* Set the netmask for the interface */
696 * The mask we set must be legal.
698 ret = -EINVAL;
699 if (bad_mask(sin->sin_addr.s_addr, 0))
700 break;
701 ret = 0;
702 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
703 inet_del_ifa(in_dev, ifap, 0);
704 ifa->ifa_mask = sin->sin_addr.s_addr;
705 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
707 /* See if current broadcast address matches
708 * with current netmask, then recalculate
709 * the broadcast address. Otherwise it's a
710 * funny address, so don't touch it since
711 * the user seems to know what (s)he's doing...
713 if ((dev->flags & IFF_BROADCAST) &&
714 (ifa->ifa_prefixlen < 31) &&
715 (ifa->ifa_broadcast ==
716 (ifa->ifa_local|~ifa->ifa_mask))) {
717 ifa->ifa_broadcast = (ifa->ifa_local |
718 ~sin->sin_addr.s_addr);
720 inet_insert_ifa(ifa);
722 break;
724 done:
725 rtnl_unlock();
726 out:
727 return ret;
728 rarok:
729 rtnl_unlock();
730 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
731 goto out;
734 static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
736 struct in_device *in_dev = __in_dev_get(dev);
737 struct in_ifaddr *ifa;
738 struct ifreq ifr;
739 int done = 0;
741 if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
742 goto out;
744 for (; ifa; ifa = ifa->ifa_next) {
745 if (!buf) {
746 done += sizeof(ifr);
747 continue;
749 if (len < (int) sizeof(ifr))
750 break;
751 memset(&ifr, 0, sizeof(struct ifreq));
752 if (ifa->ifa_label)
753 strcpy(ifr.ifr_name, ifa->ifa_label);
754 else
755 strcpy(ifr.ifr_name, dev->name);
757 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
758 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
759 ifa->ifa_local;
761 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
762 done = -EFAULT;
763 break;
765 buf += sizeof(struct ifreq);
766 len -= sizeof(struct ifreq);
767 done += sizeof(struct ifreq);
769 out:
770 return done;
773 u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
775 u32 addr = 0;
776 struct in_device *in_dev;
778 rcu_read_lock();
779 in_dev = __in_dev_get(dev);
780 if (!in_dev)
781 goto no_in_dev;
783 for_primary_ifa(in_dev) {
784 if (ifa->ifa_scope > scope)
785 continue;
786 if (!dst || inet_ifa_match(dst, ifa)) {
787 addr = ifa->ifa_local;
788 break;
790 if (!addr)
791 addr = ifa->ifa_local;
792 } endfor_ifa(in_dev);
793 no_in_dev:
794 rcu_read_unlock();
796 if (addr)
797 goto out;
799 /* Not loopback addresses on loopback should be preferred
800 in this case. It is importnat that lo is the first interface
801 in dev_base list.
803 read_lock(&dev_base_lock);
804 rcu_read_lock();
805 for (dev = dev_base; dev; dev = dev->next) {
806 if ((in_dev = __in_dev_get(dev)) == NULL)
807 continue;
809 for_primary_ifa(in_dev) {
810 if (ifa->ifa_scope != RT_SCOPE_LINK &&
811 ifa->ifa_scope <= scope) {
812 addr = ifa->ifa_local;
813 goto out_unlock_both;
815 } endfor_ifa(in_dev);
817 out_unlock_both:
818 read_unlock(&dev_base_lock);
819 rcu_read_unlock();
820 out:
821 return addr;
824 static u32 confirm_addr_indev(struct in_device *in_dev, u32 dst,
825 u32 local, int scope)
827 int same = 0;
828 u32 addr = 0;
830 for_ifa(in_dev) {
831 if (!addr &&
832 (local == ifa->ifa_local || !local) &&
833 ifa->ifa_scope <= scope) {
834 addr = ifa->ifa_local;
835 if (same)
836 break;
838 if (!same) {
839 same = (!local || inet_ifa_match(local, ifa)) &&
840 (!dst || inet_ifa_match(dst, ifa));
841 if (same && addr) {
842 if (local || !dst)
843 break;
844 /* Is the selected addr into dst subnet? */
845 if (inet_ifa_match(addr, ifa))
846 break;
847 /* No, then can we use new local src? */
848 if (ifa->ifa_scope <= scope) {
849 addr = ifa->ifa_local;
850 break;
852 /* search for large dst subnet for addr */
853 same = 0;
856 } endfor_ifa(in_dev);
858 return same? addr : 0;
862 * Confirm that local IP address exists using wildcards:
863 * - dev: only on this interface, 0=any interface
864 * - dst: only in the same subnet as dst, 0=any dst
865 * - local: address, 0=autoselect the local address
866 * - scope: maximum allowed scope value for the local address
868 u32 inet_confirm_addr(const struct net_device *dev, u32 dst, u32 local, int scope)
870 u32 addr = 0;
871 struct in_device *in_dev;
873 if (dev) {
874 rcu_read_lock();
875 if ((in_dev = __in_dev_get(dev)))
876 addr = confirm_addr_indev(in_dev, dst, local, scope);
877 rcu_read_unlock();
879 return addr;
882 read_lock(&dev_base_lock);
883 rcu_read_lock();
884 for (dev = dev_base; dev; dev = dev->next) {
885 if ((in_dev = __in_dev_get(dev))) {
886 addr = confirm_addr_indev(in_dev, dst, local, scope);
887 if (addr)
888 break;
891 rcu_read_unlock();
892 read_unlock(&dev_base_lock);
894 return addr;
898 * Device notifier
901 int register_inetaddr_notifier(struct notifier_block *nb)
903 return notifier_chain_register(&inetaddr_chain, nb);
906 int unregister_inetaddr_notifier(struct notifier_block *nb)
908 return notifier_chain_unregister(&inetaddr_chain, nb);
911 /* Rename ifa_labels for a device name change. Make some effort to preserve existing
912 * alias numbering and to create unique labels if possible.
914 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
916 struct in_ifaddr *ifa;
917 int named = 0;
919 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
920 char old[IFNAMSIZ], *dot;
922 memcpy(old, ifa->ifa_label, IFNAMSIZ);
923 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
924 if (named++ == 0)
925 continue;
926 dot = strchr(ifa->ifa_label, ':');
927 if (dot == NULL) {
928 sprintf(old, ":%d", named);
929 dot = old;
931 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) {
932 strcat(ifa->ifa_label, dot);
933 } else {
934 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
939 /* Called only under RTNL semaphore */
941 static int inetdev_event(struct notifier_block *this, unsigned long event,
942 void *ptr)
944 struct net_device *dev = ptr;
945 struct in_device *in_dev = __in_dev_get(dev);
947 ASSERT_RTNL();
949 if (!in_dev) {
950 if (event == NETDEV_REGISTER && dev == &loopback_dev) {
951 in_dev = inetdev_init(dev);
952 if (!in_dev)
953 panic("devinet: Failed to create loopback\n");
954 in_dev->cnf.no_xfrm = 1;
955 in_dev->cnf.no_policy = 1;
957 goto out;
960 switch (event) {
961 case NETDEV_REGISTER:
962 printk(KERN_DEBUG "inetdev_event: bug\n");
963 dev->ip_ptr = NULL;
964 break;
965 case NETDEV_UP:
966 if (dev->mtu < 68)
967 break;
968 if (dev == &loopback_dev) {
969 struct in_ifaddr *ifa;
970 if ((ifa = inet_alloc_ifa()) != NULL) {
971 ifa->ifa_local =
972 ifa->ifa_address = htonl(INADDR_LOOPBACK);
973 ifa->ifa_prefixlen = 8;
974 ifa->ifa_mask = inet_make_mask(8);
975 in_dev_hold(in_dev);
976 ifa->ifa_dev = in_dev;
977 ifa->ifa_scope = RT_SCOPE_HOST;
978 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
979 inet_insert_ifa(ifa);
982 ip_mc_up(in_dev);
983 break;
984 case NETDEV_DOWN:
985 ip_mc_down(in_dev);
986 break;
987 case NETDEV_CHANGEMTU:
988 if (dev->mtu >= 68)
989 break;
990 /* MTU falled under 68, disable IP */
991 case NETDEV_UNREGISTER:
992 inetdev_destroy(in_dev);
993 break;
994 case NETDEV_CHANGENAME:
995 /* Do not notify about label change, this event is
996 * not interesting to applications using netlink.
998 inetdev_changename(dev, in_dev);
1000 #ifdef CONFIG_SYSCTL
1001 devinet_sysctl_unregister(&in_dev->cnf);
1002 neigh_sysctl_unregister(in_dev->arp_parms);
1003 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
1004 NET_IPV4_NEIGH, "ipv4", NULL, NULL);
1005 devinet_sysctl_register(in_dev, &in_dev->cnf);
1006 #endif
1007 break;
1009 out:
1010 return NOTIFY_DONE;
1013 static struct notifier_block ip_netdev_notifier = {
1014 .notifier_call =inetdev_event,
1017 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1018 u32 pid, u32 seq, int event)
1020 struct ifaddrmsg *ifm;
1021 struct nlmsghdr *nlh;
1022 unsigned char *b = skb->tail;
1024 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
1025 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
1026 ifm = NLMSG_DATA(nlh);
1027 ifm->ifa_family = AF_INET;
1028 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1029 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1030 ifm->ifa_scope = ifa->ifa_scope;
1031 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1032 if (ifa->ifa_address)
1033 RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
1034 if (ifa->ifa_local)
1035 RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
1036 if (ifa->ifa_broadcast)
1037 RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
1038 if (ifa->ifa_anycast)
1039 RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
1040 if (ifa->ifa_label[0])
1041 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
1042 nlh->nlmsg_len = skb->tail - b;
1043 return skb->len;
1045 nlmsg_failure:
1046 rtattr_failure:
1047 skb_trim(skb, b - skb->data);
1048 return -1;
1051 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1053 int idx, ip_idx;
1054 struct net_device *dev;
1055 struct in_device *in_dev;
1056 struct in_ifaddr *ifa;
1057 int s_ip_idx, s_idx = cb->args[0];
1059 s_ip_idx = ip_idx = cb->args[1];
1060 read_lock(&dev_base_lock);
1061 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
1062 if (idx < s_idx)
1063 continue;
1064 if (idx > s_idx)
1065 s_ip_idx = 0;
1066 rcu_read_lock();
1067 if ((in_dev = __in_dev_get(dev)) == NULL) {
1068 rcu_read_unlock();
1069 continue;
1072 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1073 ifa = ifa->ifa_next, ip_idx++) {
1074 if (ip_idx < s_ip_idx)
1075 continue;
1076 if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1077 cb->nlh->nlmsg_seq,
1078 RTM_NEWADDR) <= 0) {
1079 rcu_read_unlock();
1080 goto done;
1083 rcu_read_unlock();
1086 done:
1087 read_unlock(&dev_base_lock);
1088 cb->args[0] = idx;
1089 cb->args[1] = ip_idx;
1091 return skb->len;
1094 static void rtmsg_ifa(int event, struct in_ifaddr* ifa)
1096 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + 128);
1097 struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);
1099 if (!skb)
1100 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, ENOBUFS);
1101 else if (inet_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
1102 kfree_skb(skb);
1103 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, EINVAL);
1104 } else {
1105 NETLINK_CB(skb).dst_groups = RTMGRP_IPV4_IFADDR;
1106 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV4_IFADDR, GFP_KERNEL);
1110 static struct rtnetlink_link inet_rtnetlink_table[RTM_NR_MSGTYPES] = {
1111 [RTM_NEWADDR - RTM_BASE] = { .doit = inet_rtm_newaddr, },
1112 [RTM_DELADDR - RTM_BASE] = { .doit = inet_rtm_deladdr, },
1113 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet_dump_ifaddr, },
1114 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet_rtm_newroute, },
1115 [RTM_DELROUTE - RTM_BASE] = { .doit = inet_rtm_delroute, },
1116 [RTM_GETROUTE - RTM_BASE] = { .doit = inet_rtm_getroute,
1117 .dumpit = inet_dump_fib, },
1118 #ifdef CONFIG_IP_MULTIPLE_TABLES
1119 [RTM_NEWRULE - RTM_BASE] = { .doit = inet_rtm_newrule, },
1120 [RTM_DELRULE - RTM_BASE] = { .doit = inet_rtm_delrule, },
1121 [RTM_GETRULE - RTM_BASE] = { .dumpit = inet_dump_rules, },
1122 #endif
1125 #ifdef CONFIG_SYSCTL
1127 void inet_forward_change(void)
1129 struct net_device *dev;
1130 int on = ipv4_devconf.forwarding;
1132 ipv4_devconf.accept_redirects = !on;
1133 ipv4_devconf_dflt.forwarding = on;
1135 read_lock(&dev_base_lock);
1136 for (dev = dev_base; dev; dev = dev->next) {
1137 struct in_device *in_dev;
1138 rcu_read_lock();
1139 in_dev = __in_dev_get(dev);
1140 if (in_dev)
1141 in_dev->cnf.forwarding = on;
1142 rcu_read_unlock();
1144 read_unlock(&dev_base_lock);
1146 rt_cache_flush(0);
1149 static int devinet_sysctl_forward(ctl_table *ctl, int write,
1150 struct file* filp, void __user *buffer,
1151 size_t *lenp, loff_t *ppos)
1153 int *valp = ctl->data;
1154 int val = *valp;
1155 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1157 if (write && *valp != val) {
1158 if (valp == &ipv4_devconf.forwarding)
1159 inet_forward_change();
1160 else if (valp != &ipv4_devconf_dflt.forwarding)
1161 rt_cache_flush(0);
1164 return ret;
1167 int ipv4_doint_and_flush(ctl_table *ctl, int write,
1168 struct file* filp, void __user *buffer,
1169 size_t *lenp, loff_t *ppos)
1171 int *valp = ctl->data;
1172 int val = *valp;
1173 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1175 if (write && *valp != val)
1176 rt_cache_flush(0);
1178 return ret;
1181 int ipv4_doint_and_flush_strategy(ctl_table *table, int __user *name, int nlen,
1182 void __user *oldval, size_t __user *oldlenp,
1183 void __user *newval, size_t newlen,
1184 void **context)
1186 int *valp = table->data;
1187 int new;
1189 if (!newval || !newlen)
1190 return 0;
1192 if (newlen != sizeof(int))
1193 return -EINVAL;
1195 if (get_user(new, (int __user *)newval))
1196 return -EFAULT;
1198 if (new == *valp)
1199 return 0;
1201 if (oldval && oldlenp) {
1202 size_t len;
1204 if (get_user(len, oldlenp))
1205 return -EFAULT;
1207 if (len) {
1208 if (len > table->maxlen)
1209 len = table->maxlen;
1210 if (copy_to_user(oldval, valp, len))
1211 return -EFAULT;
1212 if (put_user(len, oldlenp))
1213 return -EFAULT;
1217 *valp = new;
1218 rt_cache_flush(0);
1219 return 1;
1223 static struct devinet_sysctl_table {
1224 struct ctl_table_header *sysctl_header;
1225 ctl_table devinet_vars[__NET_IPV4_CONF_MAX];
1226 ctl_table devinet_dev[2];
1227 ctl_table devinet_conf_dir[2];
1228 ctl_table devinet_proto_dir[2];
1229 ctl_table devinet_root_dir[2];
1230 } devinet_sysctl = {
1231 .devinet_vars = {
1233 .ctl_name = NET_IPV4_CONF_FORWARDING,
1234 .procname = "forwarding",
1235 .data = &ipv4_devconf.forwarding,
1236 .maxlen = sizeof(int),
1237 .mode = 0644,
1238 .proc_handler = &devinet_sysctl_forward,
1241 .ctl_name = NET_IPV4_CONF_MC_FORWARDING,
1242 .procname = "mc_forwarding",
1243 .data = &ipv4_devconf.mc_forwarding,
1244 .maxlen = sizeof(int),
1245 .mode = 0444,
1246 .proc_handler = &proc_dointvec,
1249 .ctl_name = NET_IPV4_CONF_ACCEPT_REDIRECTS,
1250 .procname = "accept_redirects",
1251 .data = &ipv4_devconf.accept_redirects,
1252 .maxlen = sizeof(int),
1253 .mode = 0644,
1254 .proc_handler = &proc_dointvec,
1257 .ctl_name = NET_IPV4_CONF_SECURE_REDIRECTS,
1258 .procname = "secure_redirects",
1259 .data = &ipv4_devconf.secure_redirects,
1260 .maxlen = sizeof(int),
1261 .mode = 0644,
1262 .proc_handler = &proc_dointvec,
1265 .ctl_name = NET_IPV4_CONF_SHARED_MEDIA,
1266 .procname = "shared_media",
1267 .data = &ipv4_devconf.shared_media,
1268 .maxlen = sizeof(int),
1269 .mode = 0644,
1270 .proc_handler = &proc_dointvec,
1273 .ctl_name = NET_IPV4_CONF_RP_FILTER,
1274 .procname = "rp_filter",
1275 .data = &ipv4_devconf.rp_filter,
1276 .maxlen = sizeof(int),
1277 .mode = 0644,
1278 .proc_handler = &proc_dointvec,
1281 .ctl_name = NET_IPV4_CONF_SEND_REDIRECTS,
1282 .procname = "send_redirects",
1283 .data = &ipv4_devconf.send_redirects,
1284 .maxlen = sizeof(int),
1285 .mode = 0644,
1286 .proc_handler = &proc_dointvec,
1289 .ctl_name = NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE,
1290 .procname = "accept_source_route",
1291 .data = &ipv4_devconf.accept_source_route,
1292 .maxlen = sizeof(int),
1293 .mode = 0644,
1294 .proc_handler = &proc_dointvec,
1297 .ctl_name = NET_IPV4_CONF_PROXY_ARP,
1298 .procname = "proxy_arp",
1299 .data = &ipv4_devconf.proxy_arp,
1300 .maxlen = sizeof(int),
1301 .mode = 0644,
1302 .proc_handler = &proc_dointvec,
1305 .ctl_name = NET_IPV4_CONF_MEDIUM_ID,
1306 .procname = "medium_id",
1307 .data = &ipv4_devconf.medium_id,
1308 .maxlen = sizeof(int),
1309 .mode = 0644,
1310 .proc_handler = &proc_dointvec,
1313 .ctl_name = NET_IPV4_CONF_BOOTP_RELAY,
1314 .procname = "bootp_relay",
1315 .data = &ipv4_devconf.bootp_relay,
1316 .maxlen = sizeof(int),
1317 .mode = 0644,
1318 .proc_handler = &proc_dointvec,
1321 .ctl_name = NET_IPV4_CONF_LOG_MARTIANS,
1322 .procname = "log_martians",
1323 .data = &ipv4_devconf.log_martians,
1324 .maxlen = sizeof(int),
1325 .mode = 0644,
1326 .proc_handler = &proc_dointvec,
1329 .ctl_name = NET_IPV4_CONF_TAG,
1330 .procname = "tag",
1331 .data = &ipv4_devconf.tag,
1332 .maxlen = sizeof(int),
1333 .mode = 0644,
1334 .proc_handler = &proc_dointvec,
1337 .ctl_name = NET_IPV4_CONF_ARPFILTER,
1338 .procname = "arp_filter",
1339 .data = &ipv4_devconf.arp_filter,
1340 .maxlen = sizeof(int),
1341 .mode = 0644,
1342 .proc_handler = &proc_dointvec,
1345 .ctl_name = NET_IPV4_CONF_ARP_ANNOUNCE,
1346 .procname = "arp_announce",
1347 .data = &ipv4_devconf.arp_announce,
1348 .maxlen = sizeof(int),
1349 .mode = 0644,
1350 .proc_handler = &proc_dointvec,
1353 .ctl_name = NET_IPV4_CONF_ARP_IGNORE,
1354 .procname = "arp_ignore",
1355 .data = &ipv4_devconf.arp_ignore,
1356 .maxlen = sizeof(int),
1357 .mode = 0644,
1358 .proc_handler = &proc_dointvec,
1361 .ctl_name = NET_IPV4_CONF_NOXFRM,
1362 .procname = "disable_xfrm",
1363 .data = &ipv4_devconf.no_xfrm,
1364 .maxlen = sizeof(int),
1365 .mode = 0644,
1366 .proc_handler = &ipv4_doint_and_flush,
1367 .strategy = &ipv4_doint_and_flush_strategy,
1370 .ctl_name = NET_IPV4_CONF_NOPOLICY,
1371 .procname = "disable_policy",
1372 .data = &ipv4_devconf.no_policy,
1373 .maxlen = sizeof(int),
1374 .mode = 0644,
1375 .proc_handler = &ipv4_doint_and_flush,
1376 .strategy = &ipv4_doint_and_flush_strategy,
1379 .ctl_name = NET_IPV4_CONF_FORCE_IGMP_VERSION,
1380 .procname = "force_igmp_version",
1381 .data = &ipv4_devconf.force_igmp_version,
1382 .maxlen = sizeof(int),
1383 .mode = 0644,
1384 .proc_handler = &ipv4_doint_and_flush,
1385 .strategy = &ipv4_doint_and_flush_strategy,
1388 .devinet_dev = {
1390 .ctl_name = NET_PROTO_CONF_ALL,
1391 .procname = "all",
1392 .mode = 0555,
1393 .child = devinet_sysctl.devinet_vars,
1396 .devinet_conf_dir = {
1398 .ctl_name = NET_IPV4_CONF,
1399 .procname = "conf",
1400 .mode = 0555,
1401 .child = devinet_sysctl.devinet_dev,
1404 .devinet_proto_dir = {
1406 .ctl_name = NET_IPV4,
1407 .procname = "ipv4",
1408 .mode = 0555,
1409 .child = devinet_sysctl.devinet_conf_dir,
1412 .devinet_root_dir = {
1414 .ctl_name = CTL_NET,
1415 .procname = "net",
1416 .mode = 0555,
1417 .child = devinet_sysctl.devinet_proto_dir,
1422 static void devinet_sysctl_register(struct in_device *in_dev,
1423 struct ipv4_devconf *p)
1425 int i;
1426 struct net_device *dev = in_dev ? in_dev->dev : NULL;
1427 struct devinet_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
1428 char *dev_name = NULL;
1430 if (!t)
1431 return;
1432 memcpy(t, &devinet_sysctl, sizeof(*t));
1433 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1434 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
1435 t->devinet_vars[i].de = NULL;
1438 if (dev) {
1439 dev_name = dev->name;
1440 t->devinet_dev[0].ctl_name = dev->ifindex;
1441 } else {
1442 dev_name = "default";
1443 t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
1447 * Make a copy of dev_name, because '.procname' is regarded as const
1448 * by sysctl and we wouldn't want anyone to change it under our feet
1449 * (see SIOCSIFNAME).
1451 dev_name = net_sysctl_strdup(dev_name);
1452 if (!dev_name)
1453 goto free;
1455 t->devinet_dev[0].procname = dev_name;
1456 t->devinet_dev[0].child = t->devinet_vars;
1457 t->devinet_dev[0].de = NULL;
1458 t->devinet_conf_dir[0].child = t->devinet_dev;
1459 t->devinet_conf_dir[0].de = NULL;
1460 t->devinet_proto_dir[0].child = t->devinet_conf_dir;
1461 t->devinet_proto_dir[0].de = NULL;
1462 t->devinet_root_dir[0].child = t->devinet_proto_dir;
1463 t->devinet_root_dir[0].de = NULL;
1465 t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
1466 if (!t->sysctl_header)
1467 goto free_procname;
1469 p->sysctl = t;
1470 return;
1472 /* error path */
1473 free_procname:
1474 kfree(dev_name);
1475 free:
1476 kfree(t);
1477 return;
1480 static void devinet_sysctl_unregister(struct ipv4_devconf *p)
1482 if (p->sysctl) {
1483 struct devinet_sysctl_table *t = p->sysctl;
1484 p->sysctl = NULL;
1485 unregister_sysctl_table(t->sysctl_header);
1486 kfree(t->devinet_dev[0].procname);
1487 kfree(t);
1490 #endif
1492 void __init devinet_init(void)
1494 register_gifconf(PF_INET, inet_gifconf);
1495 register_netdevice_notifier(&ip_netdev_notifier);
1496 rtnetlink_links[PF_INET] = inet_rtnetlink_table;
1497 #ifdef CONFIG_SYSCTL
1498 devinet_sysctl.sysctl_header =
1499 register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
1500 devinet_sysctl_register(NULL, &ipv4_devconf_dflt);
1501 #endif
1504 EXPORT_SYMBOL(devinet_ioctl);
1505 EXPORT_SYMBOL(in_dev_finish_destroy);
1506 EXPORT_SYMBOL(inet_select_addr);
1507 EXPORT_SYMBOL(inetdev_by_index);
1508 EXPORT_SYMBOL(register_inetaddr_notifier);
1509 EXPORT_SYMBOL(unregister_inetaddr_notifier);