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
13 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
14 * Mark Evans, <evansmp@uhura.aston.ac.uk>
17 * Alan Cox, <gw4pts@gw4pts.ampr.org>
18 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
21 * Alexey Kuznetsov: pa_* fields are replaced with ifaddr
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
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>
41 #include <linux/socket.h>
42 #include <linux/sockios.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>
57 #include <linux/sysctl.h>
59 #include <linux/kmod.h>
62 #include <net/route.h>
63 #include <net/ip_fib.h>
65 struct ipv4_devconf ipv4_devconf = {
66 .accept_redirects = 1,
68 .secure_redirects = 1,
72 static struct ipv4_devconf ipv4_devconf_dflt = {
73 .accept_redirects = 1,
75 .secure_redirects = 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,
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);
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);
98 memset(ifa, 0, sizeof(*ifa));
99 INIT_RCU_HEAD(&ifa->rcu_head);
105 static void inet_rcu_free_ifa(struct rcu_head *head)
107 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
109 in_dev_put(ifa->ifa_dev);
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");
130 printk("Freeing alive in_device %p\n", idev);
136 struct in_device *inetdev_init(struct net_device *dev)
138 struct in_device *in_dev;
142 in_dev = kmalloc(sizeof(*in_dev), GFP_KERNEL);
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;
150 if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL)
152 /* Reference in_dev->dev */
155 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
156 NET_IPV4_NEIGH, "ipv4", NULL, NULL);
159 /* Account for reference dev->ip_ptr */
161 rcu_assign_pointer(dev->ip_ptr, in_dev);
164 devinet_sysctl_register(in_dev, &in_dev->cnf);
166 ip_mc_init_dev(in_dev);
167 if (dev->flags & IFF_UP)
177 static void in_dev_rcu_put(struct rcu_head *head)
179 struct in_device *idev = container_of(head, struct in_device, rcu_head);
183 static void inetdev_destroy(struct in_device *in_dev)
185 struct in_ifaddr *ifa;
186 struct net_device *dev;
191 if (dev == &loopback_dev)
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);
204 devinet_sysctl_unregister(&in_dev->cnf);
210 neigh_sysctl_unregister(in_dev->arp_parms);
212 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
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)
221 for_primary_ifa(in_dev) {
222 if (inet_ifa_match(a, ifa)) {
223 if (!b || inet_ifa_match(b, ifa)) {
228 } endfor_ifa(in_dev);
233 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
236 struct in_ifaddr *promote = NULL;
237 struct in_ifaddr *ifa, *ifa1 = *ifap;
238 struct in_ifaddr *last_prim = in_dev->ifa_list;
239 struct in_ifaddr *prev_prom = NULL;
240 int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
244 /* 1. Deleting primary ifaddr forces deletion all secondaries
245 * unless alias promotion is set
248 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
249 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
251 while ((ifa = *ifap1) != NULL) {
252 if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
253 ifa1->ifa_scope <= ifa->ifa_scope)
256 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
257 ifa1->ifa_mask != ifa->ifa_mask ||
258 !inet_ifa_match(ifa1->ifa_address, ifa)) {
259 ifap1 = &ifa->ifa_next;
265 *ifap1 = ifa->ifa_next;
267 rtmsg_ifa(RTM_DELADDR, ifa);
268 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa);
279 *ifap = ifa1->ifa_next;
281 /* 3. Announce address deletion */
283 /* Send message first, then call notifier.
284 At first sight, FIB update triggered by notifier
285 will refer to already deleted ifaddr, that could confuse
286 netlink listeners. It is not true: look, gated sees
287 that route deleted and if it still thinks that ifaddr
288 is valid, it will try to restore deleted routes... Grr.
289 So that, this order is correct.
291 rtmsg_ifa(RTM_DELADDR, ifa1);
292 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
297 prev_prom->ifa_next = promote->ifa_next;
298 promote->ifa_next = last_prim->ifa_next;
299 last_prim->ifa_next = promote;
302 promote->ifa_flags &= ~IFA_F_SECONDARY;
303 rtmsg_ifa(RTM_NEWADDR, promote);
304 notifier_call_chain(&inetaddr_chain, NETDEV_UP, promote);
305 for (ifa = promote->ifa_next; ifa; ifa = ifa->ifa_next) {
306 if (ifa1->ifa_mask != ifa->ifa_mask ||
307 !inet_ifa_match(ifa1->ifa_address, ifa))
316 if (!in_dev->ifa_list)
317 inetdev_destroy(in_dev);
321 static int inet_insert_ifa(struct in_ifaddr *ifa)
323 struct in_device *in_dev = ifa->ifa_dev;
324 struct in_ifaddr *ifa1, **ifap, **last_primary;
328 if (!ifa->ifa_local) {
333 ifa->ifa_flags &= ~IFA_F_SECONDARY;
334 last_primary = &in_dev->ifa_list;
336 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
337 ifap = &ifa1->ifa_next) {
338 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
339 ifa->ifa_scope <= ifa1->ifa_scope)
340 last_primary = &ifa1->ifa_next;
341 if (ifa1->ifa_mask == ifa->ifa_mask &&
342 inet_ifa_match(ifa1->ifa_address, ifa)) {
343 if (ifa1->ifa_local == ifa->ifa_local) {
347 if (ifa1->ifa_scope != ifa->ifa_scope) {
351 ifa->ifa_flags |= IFA_F_SECONDARY;
355 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
356 net_srandom(ifa->ifa_local);
360 ifa->ifa_next = *ifap;
363 /* Send message first, then call notifier.
364 Notifier will trigger FIB update, so that
365 listeners of netlink will know about new ifaddr */
366 rtmsg_ifa(RTM_NEWADDR, ifa);
367 notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
372 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
374 struct in_device *in_dev = __in_dev_get_rtnl(dev);
379 in_dev = inetdev_init(dev);
385 if (ifa->ifa_dev != in_dev) {
386 BUG_TRAP(!ifa->ifa_dev);
388 ifa->ifa_dev = in_dev;
390 if (LOOPBACK(ifa->ifa_local))
391 ifa->ifa_scope = RT_SCOPE_HOST;
392 return inet_insert_ifa(ifa);
395 struct in_device *inetdev_by_index(int ifindex)
397 struct net_device *dev;
398 struct in_device *in_dev = NULL;
399 read_lock(&dev_base_lock);
400 dev = __dev_get_by_index(ifindex);
402 in_dev = in_dev_get(dev);
403 read_unlock(&dev_base_lock);
407 /* Called only from RTNL semaphored context. No locks. */
409 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, u32 prefix,
414 for_primary_ifa(in_dev) {
415 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
417 } endfor_ifa(in_dev);
421 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
423 struct rtattr **rta = arg;
424 struct in_device *in_dev;
425 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
426 struct in_ifaddr *ifa, **ifap;
430 if ((in_dev = inetdev_by_index(ifm->ifa_index)) == NULL)
432 __in_dev_put(in_dev);
434 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
435 ifap = &ifa->ifa_next) {
436 if ((rta[IFA_LOCAL - 1] &&
437 memcmp(RTA_DATA(rta[IFA_LOCAL - 1]),
438 &ifa->ifa_local, 4)) ||
439 (rta[IFA_LABEL - 1] &&
440 rtattr_strcmp(rta[IFA_LABEL - 1], ifa->ifa_label)) ||
441 (rta[IFA_ADDRESS - 1] &&
442 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
443 !inet_ifa_match(*(u32*)RTA_DATA(rta[IFA_ADDRESS - 1]),
446 inet_del_ifa(in_dev, ifap, 1);
450 return -EADDRNOTAVAIL;
453 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
455 struct rtattr **rta = arg;
456 struct net_device *dev;
457 struct in_device *in_dev;
458 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
459 struct in_ifaddr *ifa;
464 if (ifm->ifa_prefixlen > 32 || !rta[IFA_LOCAL - 1])
468 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
472 if ((in_dev = __in_dev_get_rtnl(dev)) == NULL) {
473 in_dev = inetdev_init(dev);
478 if ((ifa = inet_alloc_ifa()) == NULL)
481 if (!rta[IFA_ADDRESS - 1])
482 rta[IFA_ADDRESS - 1] = rta[IFA_LOCAL - 1];
483 memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL - 1]), 4);
484 memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS - 1]), 4);
485 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
486 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
487 if (rta[IFA_BROADCAST - 1])
488 memcpy(&ifa->ifa_broadcast,
489 RTA_DATA(rta[IFA_BROADCAST - 1]), 4);
490 if (rta[IFA_ANYCAST - 1])
491 memcpy(&ifa->ifa_anycast, RTA_DATA(rta[IFA_ANYCAST - 1]), 4);
492 ifa->ifa_flags = ifm->ifa_flags;
493 ifa->ifa_scope = ifm->ifa_scope;
495 ifa->ifa_dev = in_dev;
496 if (rta[IFA_LABEL - 1])
497 rtattr_strlcpy(ifa->ifa_label, rta[IFA_LABEL - 1], IFNAMSIZ);
499 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
501 rc = inet_insert_ifa(ifa);
507 * Determine a default network mask, based on the IP address.
510 static __inline__ int inet_abc_len(u32 addr)
512 int rc = -1; /* Something else, probably a multicast. */
521 else if (IN_CLASSB(addr))
523 else if (IN_CLASSC(addr))
531 int devinet_ioctl(unsigned int cmd, void __user *arg)
534 struct sockaddr_in sin_orig;
535 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
536 struct in_device *in_dev;
537 struct in_ifaddr **ifap = NULL;
538 struct in_ifaddr *ifa = NULL;
539 struct net_device *dev;
542 int tryaddrmatch = 0;
545 * Fetch the caller's info block into kernel space
548 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
550 ifr.ifr_name[IFNAMSIZ - 1] = 0;
552 /* save original address for comparison */
553 memcpy(&sin_orig, sin, sizeof(*sin));
555 colon = strchr(ifr.ifr_name, ':');
560 dev_load(ifr.ifr_name);
564 case SIOCGIFADDR: /* Get interface address */
565 case SIOCGIFBRDADDR: /* Get the broadcast address */
566 case SIOCGIFDSTADDR: /* Get the destination address */
567 case SIOCGIFNETMASK: /* Get the netmask for the interface */
568 /* Note that these ioctls will not sleep,
569 so that we do not impose a lock.
570 One day we will be forced to put shlock here (I mean SMP)
572 tryaddrmatch = (sin_orig.sin_family == AF_INET);
573 memset(sin, 0, sizeof(*sin));
574 sin->sin_family = AF_INET;
579 if (!capable(CAP_NET_ADMIN))
582 case SIOCSIFADDR: /* Set interface address (and family) */
583 case SIOCSIFBRDADDR: /* Set the broadcast address */
584 case SIOCSIFDSTADDR: /* Set the destination address */
585 case SIOCSIFNETMASK: /* Set the netmask for the interface */
587 if (!capable(CAP_NET_ADMIN))
590 if (sin->sin_family != AF_INET)
601 if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL)
607 if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) {
609 /* Matthias Andree */
610 /* compare label and address (4.4BSD style) */
611 /* note: we only do this for a limited set of ioctls
612 and only if the original address family was AF_INET.
613 This is checked above. */
614 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
615 ifap = &ifa->ifa_next) {
616 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
617 sin_orig.sin_addr.s_addr ==
623 /* we didn't get a match, maybe the application is
624 4.3BSD-style and passed in junk so we fall back to
625 comparing just the label */
627 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
628 ifap = &ifa->ifa_next)
629 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
634 ret = -EADDRNOTAVAIL;
635 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
639 case SIOCGIFADDR: /* Get interface address */
640 sin->sin_addr.s_addr = ifa->ifa_local;
643 case SIOCGIFBRDADDR: /* Get the broadcast address */
644 sin->sin_addr.s_addr = ifa->ifa_broadcast;
647 case SIOCGIFDSTADDR: /* Get the destination address */
648 sin->sin_addr.s_addr = ifa->ifa_address;
651 case SIOCGIFNETMASK: /* Get the netmask for the interface */
652 sin->sin_addr.s_addr = ifa->ifa_mask;
657 ret = -EADDRNOTAVAIL;
661 if (!(ifr.ifr_flags & IFF_UP))
662 inet_del_ifa(in_dev, ifap, 1);
665 ret = dev_change_flags(dev, ifr.ifr_flags);
668 case SIOCSIFADDR: /* Set interface address (and family) */
670 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
675 if ((ifa = inet_alloc_ifa()) == NULL)
678 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
680 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
683 if (ifa->ifa_local == sin->sin_addr.s_addr)
685 inet_del_ifa(in_dev, ifap, 0);
686 ifa->ifa_broadcast = 0;
687 ifa->ifa_anycast = 0;
690 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
692 if (!(dev->flags & IFF_POINTOPOINT)) {
693 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
694 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
695 if ((dev->flags & IFF_BROADCAST) &&
696 ifa->ifa_prefixlen < 31)
697 ifa->ifa_broadcast = ifa->ifa_address |
700 ifa->ifa_prefixlen = 32;
701 ifa->ifa_mask = inet_make_mask(32);
703 ret = inet_set_ifa(dev, ifa);
706 case SIOCSIFBRDADDR: /* Set the broadcast address */
708 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
709 inet_del_ifa(in_dev, ifap, 0);
710 ifa->ifa_broadcast = sin->sin_addr.s_addr;
711 inet_insert_ifa(ifa);
715 case SIOCSIFDSTADDR: /* Set the destination address */
717 if (ifa->ifa_address == sin->sin_addr.s_addr)
720 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
723 inet_del_ifa(in_dev, ifap, 0);
724 ifa->ifa_address = sin->sin_addr.s_addr;
725 inet_insert_ifa(ifa);
728 case SIOCSIFNETMASK: /* Set the netmask for the interface */
731 * The mask we set must be legal.
734 if (bad_mask(sin->sin_addr.s_addr, 0))
737 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
738 u32 old_mask = ifa->ifa_mask;
739 inet_del_ifa(in_dev, ifap, 0);
740 ifa->ifa_mask = sin->sin_addr.s_addr;
741 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
743 /* See if current broadcast address matches
744 * with current netmask, then recalculate
745 * the broadcast address. Otherwise it's a
746 * funny address, so don't touch it since
747 * the user seems to know what (s)he's doing...
749 if ((dev->flags & IFF_BROADCAST) &&
750 (ifa->ifa_prefixlen < 31) &&
751 (ifa->ifa_broadcast ==
752 (ifa->ifa_local|~old_mask))) {
753 ifa->ifa_broadcast = (ifa->ifa_local |
754 ~sin->sin_addr.s_addr);
756 inet_insert_ifa(ifa);
766 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
770 static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
772 struct in_device *in_dev = __in_dev_get_rtnl(dev);
773 struct in_ifaddr *ifa;
777 if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
780 for (; ifa; ifa = ifa->ifa_next) {
785 if (len < (int) sizeof(ifr))
787 memset(&ifr, 0, sizeof(struct ifreq));
789 strcpy(ifr.ifr_name, ifa->ifa_label);
791 strcpy(ifr.ifr_name, dev->name);
793 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
794 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
797 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
801 buf += sizeof(struct ifreq);
802 len -= sizeof(struct ifreq);
803 done += sizeof(struct ifreq);
809 u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
812 struct in_device *in_dev;
815 in_dev = __in_dev_get_rcu(dev);
819 for_primary_ifa(in_dev) {
820 if (ifa->ifa_scope > scope)
822 if (!dst || inet_ifa_match(dst, ifa)) {
823 addr = ifa->ifa_local;
827 addr = ifa->ifa_local;
828 } endfor_ifa(in_dev);
835 /* Not loopback addresses on loopback should be preferred
836 in this case. It is importnat that lo is the first interface
839 read_lock(&dev_base_lock);
841 for (dev = dev_base; dev; dev = dev->next) {
842 if ((in_dev = __in_dev_get_rcu(dev)) == NULL)
845 for_primary_ifa(in_dev) {
846 if (ifa->ifa_scope != RT_SCOPE_LINK &&
847 ifa->ifa_scope <= scope) {
848 addr = ifa->ifa_local;
849 goto out_unlock_both;
851 } endfor_ifa(in_dev);
854 read_unlock(&dev_base_lock);
860 static u32 confirm_addr_indev(struct in_device *in_dev, u32 dst,
861 u32 local, int scope)
868 (local == ifa->ifa_local || !local) &&
869 ifa->ifa_scope <= scope) {
870 addr = ifa->ifa_local;
875 same = (!local || inet_ifa_match(local, ifa)) &&
876 (!dst || inet_ifa_match(dst, ifa));
880 /* Is the selected addr into dst subnet? */
881 if (inet_ifa_match(addr, ifa))
883 /* No, then can we use new local src? */
884 if (ifa->ifa_scope <= scope) {
885 addr = ifa->ifa_local;
888 /* search for large dst subnet for addr */
892 } endfor_ifa(in_dev);
894 return same? addr : 0;
898 * Confirm that local IP address exists using wildcards:
899 * - dev: only on this interface, 0=any interface
900 * - dst: only in the same subnet as dst, 0=any dst
901 * - local: address, 0=autoselect the local address
902 * - scope: maximum allowed scope value for the local address
904 u32 inet_confirm_addr(const struct net_device *dev, u32 dst, u32 local, int scope)
907 struct in_device *in_dev;
911 if ((in_dev = __in_dev_get_rcu(dev)))
912 addr = confirm_addr_indev(in_dev, dst, local, scope);
918 read_lock(&dev_base_lock);
920 for (dev = dev_base; dev; dev = dev->next) {
921 if ((in_dev = __in_dev_get_rcu(dev))) {
922 addr = confirm_addr_indev(in_dev, dst, local, scope);
928 read_unlock(&dev_base_lock);
937 int register_inetaddr_notifier(struct notifier_block *nb)
939 return notifier_chain_register(&inetaddr_chain, nb);
942 int unregister_inetaddr_notifier(struct notifier_block *nb)
944 return notifier_chain_unregister(&inetaddr_chain, nb);
947 /* Rename ifa_labels for a device name change. Make some effort to preserve existing
948 * alias numbering and to create unique labels if possible.
950 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
952 struct in_ifaddr *ifa;
955 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
956 char old[IFNAMSIZ], *dot;
958 memcpy(old, ifa->ifa_label, IFNAMSIZ);
959 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
962 dot = strchr(ifa->ifa_label, ':');
964 sprintf(old, ":%d", named);
967 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) {
968 strcat(ifa->ifa_label, dot);
970 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
975 /* Called only under RTNL semaphore */
977 static int inetdev_event(struct notifier_block *this, unsigned long event,
980 struct net_device *dev = ptr;
981 struct in_device *in_dev = __in_dev_get_rtnl(dev);
986 if (event == NETDEV_REGISTER && dev == &loopback_dev) {
987 in_dev = inetdev_init(dev);
989 panic("devinet: Failed to create loopback\n");
990 in_dev->cnf.no_xfrm = 1;
991 in_dev->cnf.no_policy = 1;
997 case NETDEV_REGISTER:
998 printk(KERN_DEBUG "inetdev_event: bug\n");
1004 if (dev == &loopback_dev) {
1005 struct in_ifaddr *ifa;
1006 if ((ifa = inet_alloc_ifa()) != NULL) {
1008 ifa->ifa_address = htonl(INADDR_LOOPBACK);
1009 ifa->ifa_prefixlen = 8;
1010 ifa->ifa_mask = inet_make_mask(8);
1011 in_dev_hold(in_dev);
1012 ifa->ifa_dev = in_dev;
1013 ifa->ifa_scope = RT_SCOPE_HOST;
1014 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1015 inet_insert_ifa(ifa);
1023 case NETDEV_CHANGEMTU:
1026 /* MTU falled under 68, disable IP */
1027 case NETDEV_UNREGISTER:
1028 inetdev_destroy(in_dev);
1030 case NETDEV_CHANGENAME:
1031 /* Do not notify about label change, this event is
1032 * not interesting to applications using netlink.
1034 inetdev_changename(dev, in_dev);
1036 #ifdef CONFIG_SYSCTL
1037 devinet_sysctl_unregister(&in_dev->cnf);
1038 neigh_sysctl_unregister(in_dev->arp_parms);
1039 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
1040 NET_IPV4_NEIGH, "ipv4", NULL, NULL);
1041 devinet_sysctl_register(in_dev, &in_dev->cnf);
1049 static struct notifier_block ip_netdev_notifier = {
1050 .notifier_call =inetdev_event,
1053 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1054 u32 pid, u32 seq, int event, unsigned int flags)
1056 struct ifaddrmsg *ifm;
1057 struct nlmsghdr *nlh;
1058 unsigned char *b = skb->tail;
1060 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
1061 ifm = NLMSG_DATA(nlh);
1062 ifm->ifa_family = AF_INET;
1063 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1064 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1065 ifm->ifa_scope = ifa->ifa_scope;
1066 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1067 if (ifa->ifa_address)
1068 RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
1070 RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
1071 if (ifa->ifa_broadcast)
1072 RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
1073 if (ifa->ifa_anycast)
1074 RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
1075 if (ifa->ifa_label[0])
1076 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
1077 nlh->nlmsg_len = skb->tail - b;
1082 skb_trim(skb, b - skb->data);
1086 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1089 struct net_device *dev;
1090 struct in_device *in_dev;
1091 struct in_ifaddr *ifa;
1092 int s_ip_idx, s_idx = cb->args[0];
1094 s_ip_idx = ip_idx = cb->args[1];
1095 read_lock(&dev_base_lock);
1096 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
1102 if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
1107 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1108 ifa = ifa->ifa_next, ip_idx++) {
1109 if (ip_idx < s_ip_idx)
1111 if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1113 RTM_NEWADDR, NLM_F_MULTI) <= 0) {
1122 read_unlock(&dev_base_lock);
1124 cb->args[1] = ip_idx;
1129 static void rtmsg_ifa(int event, struct in_ifaddr* ifa)
1131 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + 128);
1132 struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);
1135 netlink_set_err(rtnl, 0, RTNLGRP_IPV4_IFADDR, ENOBUFS);
1136 else if (inet_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
1138 netlink_set_err(rtnl, 0, RTNLGRP_IPV4_IFADDR, EINVAL);
1140 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV4_IFADDR, GFP_KERNEL);
1144 static struct rtnetlink_link inet_rtnetlink_table[RTM_NR_MSGTYPES] = {
1145 [RTM_NEWADDR - RTM_BASE] = { .doit = inet_rtm_newaddr, },
1146 [RTM_DELADDR - RTM_BASE] = { .doit = inet_rtm_deladdr, },
1147 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet_dump_ifaddr, },
1148 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet_rtm_newroute, },
1149 [RTM_DELROUTE - RTM_BASE] = { .doit = inet_rtm_delroute, },
1150 [RTM_GETROUTE - RTM_BASE] = { .doit = inet_rtm_getroute,
1151 .dumpit = inet_dump_fib, },
1152 #ifdef CONFIG_IP_MULTIPLE_TABLES
1153 [RTM_NEWRULE - RTM_BASE] = { .doit = inet_rtm_newrule, },
1154 [RTM_DELRULE - RTM_BASE] = { .doit = inet_rtm_delrule, },
1155 [RTM_GETRULE - RTM_BASE] = { .dumpit = inet_dump_rules, },
1159 #ifdef CONFIG_SYSCTL
1161 void inet_forward_change(void)
1163 struct net_device *dev;
1164 int on = ipv4_devconf.forwarding;
1166 ipv4_devconf.accept_redirects = !on;
1167 ipv4_devconf_dflt.forwarding = on;
1169 read_lock(&dev_base_lock);
1170 for (dev = dev_base; dev; dev = dev->next) {
1171 struct in_device *in_dev;
1173 in_dev = __in_dev_get_rcu(dev);
1175 in_dev->cnf.forwarding = on;
1178 read_unlock(&dev_base_lock);
1183 static int devinet_sysctl_forward(ctl_table *ctl, int write,
1184 struct file* filp, void __user *buffer,
1185 size_t *lenp, loff_t *ppos)
1187 int *valp = ctl->data;
1189 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1191 if (write && *valp != val) {
1192 if (valp == &ipv4_devconf.forwarding)
1193 inet_forward_change();
1194 else if (valp != &ipv4_devconf_dflt.forwarding)
1201 int ipv4_doint_and_flush(ctl_table *ctl, int write,
1202 struct file* filp, void __user *buffer,
1203 size_t *lenp, loff_t *ppos)
1205 int *valp = ctl->data;
1207 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1209 if (write && *valp != val)
1215 int ipv4_doint_and_flush_strategy(ctl_table *table, int __user *name, int nlen,
1216 void __user *oldval, size_t __user *oldlenp,
1217 void __user *newval, size_t newlen,
1220 int *valp = table->data;
1223 if (!newval || !newlen)
1226 if (newlen != sizeof(int))
1229 if (get_user(new, (int __user *)newval))
1235 if (oldval && oldlenp) {
1238 if (get_user(len, oldlenp))
1242 if (len > table->maxlen)
1243 len = table->maxlen;
1244 if (copy_to_user(oldval, valp, len))
1246 if (put_user(len, oldlenp))
1257 static struct devinet_sysctl_table {
1258 struct ctl_table_header *sysctl_header;
1259 ctl_table devinet_vars[__NET_IPV4_CONF_MAX];
1260 ctl_table devinet_dev[2];
1261 ctl_table devinet_conf_dir[2];
1262 ctl_table devinet_proto_dir[2];
1263 ctl_table devinet_root_dir[2];
1264 } devinet_sysctl = {
1267 .ctl_name = NET_IPV4_CONF_FORWARDING,
1268 .procname = "forwarding",
1269 .data = &ipv4_devconf.forwarding,
1270 .maxlen = sizeof(int),
1272 .proc_handler = &devinet_sysctl_forward,
1275 .ctl_name = NET_IPV4_CONF_MC_FORWARDING,
1276 .procname = "mc_forwarding",
1277 .data = &ipv4_devconf.mc_forwarding,
1278 .maxlen = sizeof(int),
1280 .proc_handler = &proc_dointvec,
1283 .ctl_name = NET_IPV4_CONF_ACCEPT_REDIRECTS,
1284 .procname = "accept_redirects",
1285 .data = &ipv4_devconf.accept_redirects,
1286 .maxlen = sizeof(int),
1288 .proc_handler = &proc_dointvec,
1291 .ctl_name = NET_IPV4_CONF_SECURE_REDIRECTS,
1292 .procname = "secure_redirects",
1293 .data = &ipv4_devconf.secure_redirects,
1294 .maxlen = sizeof(int),
1296 .proc_handler = &proc_dointvec,
1299 .ctl_name = NET_IPV4_CONF_SHARED_MEDIA,
1300 .procname = "shared_media",
1301 .data = &ipv4_devconf.shared_media,
1302 .maxlen = sizeof(int),
1304 .proc_handler = &proc_dointvec,
1307 .ctl_name = NET_IPV4_CONF_RP_FILTER,
1308 .procname = "rp_filter",
1309 .data = &ipv4_devconf.rp_filter,
1310 .maxlen = sizeof(int),
1312 .proc_handler = &proc_dointvec,
1315 .ctl_name = NET_IPV4_CONF_SEND_REDIRECTS,
1316 .procname = "send_redirects",
1317 .data = &ipv4_devconf.send_redirects,
1318 .maxlen = sizeof(int),
1320 .proc_handler = &proc_dointvec,
1323 .ctl_name = NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE,
1324 .procname = "accept_source_route",
1325 .data = &ipv4_devconf.accept_source_route,
1326 .maxlen = sizeof(int),
1328 .proc_handler = &proc_dointvec,
1331 .ctl_name = NET_IPV4_CONF_PROXY_ARP,
1332 .procname = "proxy_arp",
1333 .data = &ipv4_devconf.proxy_arp,
1334 .maxlen = sizeof(int),
1336 .proc_handler = &proc_dointvec,
1339 .ctl_name = NET_IPV4_CONF_MEDIUM_ID,
1340 .procname = "medium_id",
1341 .data = &ipv4_devconf.medium_id,
1342 .maxlen = sizeof(int),
1344 .proc_handler = &proc_dointvec,
1347 .ctl_name = NET_IPV4_CONF_BOOTP_RELAY,
1348 .procname = "bootp_relay",
1349 .data = &ipv4_devconf.bootp_relay,
1350 .maxlen = sizeof(int),
1352 .proc_handler = &proc_dointvec,
1355 .ctl_name = NET_IPV4_CONF_LOG_MARTIANS,
1356 .procname = "log_martians",
1357 .data = &ipv4_devconf.log_martians,
1358 .maxlen = sizeof(int),
1360 .proc_handler = &proc_dointvec,
1363 .ctl_name = NET_IPV4_CONF_TAG,
1365 .data = &ipv4_devconf.tag,
1366 .maxlen = sizeof(int),
1368 .proc_handler = &proc_dointvec,
1371 .ctl_name = NET_IPV4_CONF_ARPFILTER,
1372 .procname = "arp_filter",
1373 .data = &ipv4_devconf.arp_filter,
1374 .maxlen = sizeof(int),
1376 .proc_handler = &proc_dointvec,
1379 .ctl_name = NET_IPV4_CONF_ARP_ANNOUNCE,
1380 .procname = "arp_announce",
1381 .data = &ipv4_devconf.arp_announce,
1382 .maxlen = sizeof(int),
1384 .proc_handler = &proc_dointvec,
1387 .ctl_name = NET_IPV4_CONF_ARP_IGNORE,
1388 .procname = "arp_ignore",
1389 .data = &ipv4_devconf.arp_ignore,
1390 .maxlen = sizeof(int),
1392 .proc_handler = &proc_dointvec,
1395 .ctl_name = NET_IPV4_CONF_NOXFRM,
1396 .procname = "disable_xfrm",
1397 .data = &ipv4_devconf.no_xfrm,
1398 .maxlen = sizeof(int),
1400 .proc_handler = &ipv4_doint_and_flush,
1401 .strategy = &ipv4_doint_and_flush_strategy,
1404 .ctl_name = NET_IPV4_CONF_NOPOLICY,
1405 .procname = "disable_policy",
1406 .data = &ipv4_devconf.no_policy,
1407 .maxlen = sizeof(int),
1409 .proc_handler = &ipv4_doint_and_flush,
1410 .strategy = &ipv4_doint_and_flush_strategy,
1413 .ctl_name = NET_IPV4_CONF_FORCE_IGMP_VERSION,
1414 .procname = "force_igmp_version",
1415 .data = &ipv4_devconf.force_igmp_version,
1416 .maxlen = sizeof(int),
1418 .proc_handler = &ipv4_doint_and_flush,
1419 .strategy = &ipv4_doint_and_flush_strategy,
1422 .ctl_name = NET_IPV4_CONF_PROMOTE_SECONDARIES,
1423 .procname = "promote_secondaries",
1424 .data = &ipv4_devconf.promote_secondaries,
1425 .maxlen = sizeof(int),
1427 .proc_handler = &ipv4_doint_and_flush,
1428 .strategy = &ipv4_doint_and_flush_strategy,
1433 .ctl_name = NET_PROTO_CONF_ALL,
1436 .child = devinet_sysctl.devinet_vars,
1439 .devinet_conf_dir = {
1441 .ctl_name = NET_IPV4_CONF,
1444 .child = devinet_sysctl.devinet_dev,
1447 .devinet_proto_dir = {
1449 .ctl_name = NET_IPV4,
1452 .child = devinet_sysctl.devinet_conf_dir,
1455 .devinet_root_dir = {
1457 .ctl_name = CTL_NET,
1460 .child = devinet_sysctl.devinet_proto_dir,
1465 static void devinet_sysctl_register(struct in_device *in_dev,
1466 struct ipv4_devconf *p)
1469 struct net_device *dev = in_dev ? in_dev->dev : NULL;
1470 struct devinet_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
1471 char *dev_name = NULL;
1475 memcpy(t, &devinet_sysctl, sizeof(*t));
1476 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1477 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
1478 t->devinet_vars[i].de = NULL;
1482 dev_name = dev->name;
1483 t->devinet_dev[0].ctl_name = dev->ifindex;
1485 dev_name = "default";
1486 t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
1490 * Make a copy of dev_name, because '.procname' is regarded as const
1491 * by sysctl and we wouldn't want anyone to change it under our feet
1492 * (see SIOCSIFNAME).
1494 dev_name = kstrdup(dev_name, GFP_KERNEL);
1498 t->devinet_dev[0].procname = dev_name;
1499 t->devinet_dev[0].child = t->devinet_vars;
1500 t->devinet_dev[0].de = NULL;
1501 t->devinet_conf_dir[0].child = t->devinet_dev;
1502 t->devinet_conf_dir[0].de = NULL;
1503 t->devinet_proto_dir[0].child = t->devinet_conf_dir;
1504 t->devinet_proto_dir[0].de = NULL;
1505 t->devinet_root_dir[0].child = t->devinet_proto_dir;
1506 t->devinet_root_dir[0].de = NULL;
1508 t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
1509 if (!t->sysctl_header)
1523 static void devinet_sysctl_unregister(struct ipv4_devconf *p)
1526 struct devinet_sysctl_table *t = p->sysctl;
1528 unregister_sysctl_table(t->sysctl_header);
1529 kfree(t->devinet_dev[0].procname);
1535 void __init devinet_init(void)
1537 register_gifconf(PF_INET, inet_gifconf);
1538 register_netdevice_notifier(&ip_netdev_notifier);
1539 rtnetlink_links[PF_INET] = inet_rtnetlink_table;
1540 #ifdef CONFIG_SYSCTL
1541 devinet_sysctl.sysctl_header =
1542 register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
1543 devinet_sysctl_register(NULL, &ipv4_devconf_dflt);
1547 EXPORT_SYMBOL(devinet_ioctl);
1548 EXPORT_SYMBOL(in_dev_finish_destroy);
1549 EXPORT_SYMBOL(inet_select_addr);
1550 EXPORT_SYMBOL(inetdev_by_index);
1551 EXPORT_SYMBOL(register_inetaddr_notifier);
1552 EXPORT_SYMBOL(unregister_inetaddr_notifier);