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
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>
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>
63 #include <net/route.h>
64 #include <net/ip_fib.h>
65 #include <net/netlink.h>
67 struct ipv4_devconf ipv4_devconf = {
68 .accept_redirects = 1,
70 .secure_redirects = 1,
74 static struct ipv4_devconf ipv4_devconf_dflt = {
75 .accept_redirects = 1,
77 .secure_redirects = 1,
79 .accept_source_route = 1,
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 },
90 static void rtmsg_ifa(int event, struct in_ifaddr *);
92 static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
93 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
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);
101 /* Locks all the inet devices. */
103 static struct in_ifaddr *inet_alloc_ifa(void)
105 struct in_ifaddr *ifa = kzalloc(sizeof(*ifa), GFP_KERNEL);
108 INIT_RCU_HEAD(&ifa->rcu_head);
114 static void inet_rcu_free_ifa(struct rcu_head *head)
116 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
118 in_dev_put(ifa->ifa_dev);
122 static inline void inet_free_ifa(struct in_ifaddr *ifa)
124 call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
127 void in_dev_finish_destroy(struct in_device *idev)
129 struct net_device *dev = idev->dev;
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");
139 printk("Freeing alive in_device %p\n", idev);
145 struct in_device *inetdev_init(struct net_device *dev)
147 struct in_device *in_dev;
151 in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
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;
158 if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL)
160 /* Reference in_dev->dev */
163 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
164 NET_IPV4_NEIGH, "ipv4", NULL, NULL);
167 /* Account for reference dev->ip_ptr */
169 rcu_assign_pointer(dev->ip_ptr, in_dev);
172 devinet_sysctl_register(in_dev, &in_dev->cnf);
174 ip_mc_init_dev(in_dev);
175 if (dev->flags & IFF_UP)
185 static void in_dev_rcu_put(struct rcu_head *head)
187 struct in_device *idev = container_of(head, struct in_device, rcu_head);
191 static void inetdev_destroy(struct in_device *in_dev)
193 struct in_ifaddr *ifa;
194 struct net_device *dev;
199 if (dev == &loopback_dev)
204 ip_mc_destroy_dev(in_dev);
206 while ((ifa = in_dev->ifa_list) != NULL) {
207 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
212 devinet_sysctl_unregister(&in_dev->cnf);
218 neigh_sysctl_unregister(in_dev->arp_parms);
220 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
223 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
226 int inet_addr_onlink(struct in_device *in_dev, u32 a, u32 b)
229 for_primary_ifa(in_dev) {
230 if (inet_ifa_match(a, ifa)) {
231 if (!b || inet_ifa_match(b, ifa)) {
236 } endfor_ifa(in_dev);
241 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
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);
252 /* 1. Deleting primary ifaddr forces deletion all secondaries
253 * unless alias promotion is set
256 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
257 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
259 while ((ifa = *ifap1) != NULL) {
260 if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
261 ifa1->ifa_scope <= ifa->ifa_scope)
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;
273 *ifap1 = ifa->ifa_next;
275 rtmsg_ifa(RTM_DELADDR, ifa);
276 blocking_notifier_call_chain(&inetaddr_chain,
288 *ifap = ifa1->ifa_next;
290 /* 3. Announce address deletion */
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.
300 rtmsg_ifa(RTM_DELADDR, ifa1);
301 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
306 prev_prom->ifa_next = promote->ifa_next;
307 promote->ifa_next = last_prim->ifa_next;
308 last_prim->ifa_next = promote;
311 promote->ifa_flags &= ~IFA_F_SECONDARY;
312 rtmsg_ifa(RTM_NEWADDR, promote);
313 blocking_notifier_call_chain(&inetaddr_chain,
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))
326 if (!in_dev->ifa_list)
327 inetdev_destroy(in_dev);
331 static int inet_insert_ifa(struct in_ifaddr *ifa)
333 struct in_device *in_dev = ifa->ifa_dev;
334 struct in_ifaddr *ifa1, **ifap, **last_primary;
338 if (!ifa->ifa_local) {
343 ifa->ifa_flags &= ~IFA_F_SECONDARY;
344 last_primary = &in_dev->ifa_list;
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) {
357 if (ifa1->ifa_scope != ifa->ifa_scope) {
361 ifa->ifa_flags |= IFA_F_SECONDARY;
365 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
366 net_srandom(ifa->ifa_local);
370 ifa->ifa_next = *ifap;
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);
382 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
384 struct in_device *in_dev = __in_dev_get_rtnl(dev);
389 in_dev = inetdev_init(dev);
395 if (ifa->ifa_dev != in_dev) {
396 BUG_TRAP(!ifa->ifa_dev);
398 ifa->ifa_dev = in_dev;
400 if (LOOPBACK(ifa->ifa_local))
401 ifa->ifa_scope = RT_SCOPE_HOST;
402 return inet_insert_ifa(ifa);
405 struct in_device *inetdev_by_index(int ifindex)
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);
412 in_dev = in_dev_get(dev);
413 read_unlock(&dev_base_lock);
417 /* Called only from RTNL semaphored context. No locks. */
419 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, u32 prefix,
424 for_primary_ifa(in_dev) {
425 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
427 } endfor_ifa(in_dev);
431 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
433 struct nlattr *tb[IFA_MAX+1];
434 struct in_device *in_dev;
435 struct ifaddrmsg *ifm;
436 struct in_ifaddr *ifa, **ifap;
441 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
445 ifm = nlmsg_data(nlh);
446 in_dev = inetdev_by_index(ifm->ifa_index);
447 if (in_dev == NULL) {
452 __in_dev_put(in_dev);
454 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
455 ifap = &ifa->ifa_next) {
457 ifa->ifa_local != nla_get_u32(tb[IFA_LOCAL]))
460 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
463 if (tb[IFA_ADDRESS] &&
464 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
465 !inet_ifa_match(nla_get_u32(tb[IFA_ADDRESS]), ifa)))
468 inet_del_ifa(in_dev, ifap, 1);
472 err = -EADDRNOTAVAIL;
477 static struct in_ifaddr *rtm_to_ifaddr(struct nlmsghdr *nlh)
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;
486 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
490 ifm = nlmsg_data(nlh);
491 if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL)
494 dev = __dev_get_by_index(ifm->ifa_index);
500 in_dev = __in_dev_get_rtnl(dev);
501 if (in_dev == NULL) {
502 in_dev = inetdev_init(dev);
503 if (in_dev == NULL) {
509 ifa = inet_alloc_ifa();
512 * A potential indev allocation can be left alive, it stays
513 * assigned to its device and is destroy with it.
521 if (tb[IFA_ADDRESS] == NULL)
522 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
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;
530 ifa->ifa_local = nla_get_u32(tb[IFA_LOCAL]);
531 ifa->ifa_address = nla_get_u32(tb[IFA_ADDRESS]);
533 if (tb[IFA_BROADCAST])
534 ifa->ifa_broadcast = nla_get_u32(tb[IFA_BROADCAST]);
537 ifa->ifa_anycast = nla_get_u32(tb[IFA_ANYCAST]);
540 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
542 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
550 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
552 struct in_ifaddr *ifa;
556 ifa = rtm_to_ifaddr(nlh);
560 return inet_insert_ifa(ifa);
564 * Determine a default network mask, based on the IP address.
567 static __inline__ int inet_abc_len(u32 addr)
569 int rc = -1; /* Something else, probably a multicast. */
578 else if (IN_CLASSB(addr))
580 else if (IN_CLASSC(addr))
588 int devinet_ioctl(unsigned int cmd, void __user *arg)
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;
599 int tryaddrmatch = 0;
602 * Fetch the caller's info block into kernel space
605 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
607 ifr.ifr_name[IFNAMSIZ - 1] = 0;
609 /* save original address for comparison */
610 memcpy(&sin_orig, sin, sizeof(*sin));
612 colon = strchr(ifr.ifr_name, ':');
617 dev_load(ifr.ifr_name);
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)
629 tryaddrmatch = (sin_orig.sin_family == AF_INET);
630 memset(sin, 0, sizeof(*sin));
631 sin->sin_family = AF_INET;
636 if (!capable(CAP_NET_ADMIN))
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 */
644 if (!capable(CAP_NET_ADMIN))
647 if (sin->sin_family != AF_INET)
658 if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL)
664 if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) {
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 ==
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 */
684 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
685 ifap = &ifa->ifa_next)
686 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
691 ret = -EADDRNOTAVAIL;
692 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
696 case SIOCGIFADDR: /* Get interface address */
697 sin->sin_addr.s_addr = ifa->ifa_local;
700 case SIOCGIFBRDADDR: /* Get the broadcast address */
701 sin->sin_addr.s_addr = ifa->ifa_broadcast;
704 case SIOCGIFDSTADDR: /* Get the destination address */
705 sin->sin_addr.s_addr = ifa->ifa_address;
708 case SIOCGIFNETMASK: /* Get the netmask for the interface */
709 sin->sin_addr.s_addr = ifa->ifa_mask;
714 ret = -EADDRNOTAVAIL;
718 if (!(ifr.ifr_flags & IFF_UP))
719 inet_del_ifa(in_dev, ifap, 1);
722 ret = dev_change_flags(dev, ifr.ifr_flags);
725 case SIOCSIFADDR: /* Set interface address (and family) */
727 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
732 if ((ifa = inet_alloc_ifa()) == NULL)
735 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
737 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
740 if (ifa->ifa_local == sin->sin_addr.s_addr)
742 inet_del_ifa(in_dev, ifap, 0);
743 ifa->ifa_broadcast = 0;
744 ifa->ifa_anycast = 0;
747 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
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 |
757 ifa->ifa_prefixlen = 32;
758 ifa->ifa_mask = inet_make_mask(32);
760 ret = inet_set_ifa(dev, ifa);
763 case SIOCSIFBRDADDR: /* Set the broadcast address */
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);
772 case SIOCSIFDSTADDR: /* Set the destination address */
774 if (ifa->ifa_address == sin->sin_addr.s_addr)
777 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
780 inet_del_ifa(in_dev, ifap, 0);
781 ifa->ifa_address = sin->sin_addr.s_addr;
782 inet_insert_ifa(ifa);
785 case SIOCSIFNETMASK: /* Set the netmask for the interface */
788 * The mask we set must be legal.
791 if (bad_mask(sin->sin_addr.s_addr, 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);
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...
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);
813 inet_insert_ifa(ifa);
823 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
827 static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
829 struct in_device *in_dev = __in_dev_get_rtnl(dev);
830 struct in_ifaddr *ifa;
834 if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
837 for (; ifa; ifa = ifa->ifa_next) {
842 if (len < (int) sizeof(ifr))
844 memset(&ifr, 0, sizeof(struct ifreq));
846 strcpy(ifr.ifr_name, ifa->ifa_label);
848 strcpy(ifr.ifr_name, dev->name);
850 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
851 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
854 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
858 buf += sizeof(struct ifreq);
859 len -= sizeof(struct ifreq);
860 done += sizeof(struct ifreq);
866 u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
869 struct in_device *in_dev;
872 in_dev = __in_dev_get_rcu(dev);
876 for_primary_ifa(in_dev) {
877 if (ifa->ifa_scope > scope)
879 if (!dst || inet_ifa_match(dst, ifa)) {
880 addr = ifa->ifa_local;
884 addr = ifa->ifa_local;
885 } endfor_ifa(in_dev);
892 /* Not loopback addresses on loopback should be preferred
893 in this case. It is importnat that lo is the first interface
896 read_lock(&dev_base_lock);
898 for (dev = dev_base; dev; dev = dev->next) {
899 if ((in_dev = __in_dev_get_rcu(dev)) == NULL)
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;
908 } endfor_ifa(in_dev);
911 read_unlock(&dev_base_lock);
917 static u32 confirm_addr_indev(struct in_device *in_dev, u32 dst,
918 u32 local, int scope)
925 (local == ifa->ifa_local || !local) &&
926 ifa->ifa_scope <= scope) {
927 addr = ifa->ifa_local;
932 same = (!local || inet_ifa_match(local, ifa)) &&
933 (!dst || inet_ifa_match(dst, ifa));
937 /* Is the selected addr into dst subnet? */
938 if (inet_ifa_match(addr, ifa))
940 /* No, then can we use new local src? */
941 if (ifa->ifa_scope <= scope) {
942 addr = ifa->ifa_local;
945 /* search for large dst subnet for addr */
949 } endfor_ifa(in_dev);
951 return same? addr : 0;
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
961 u32 inet_confirm_addr(const struct net_device *dev, u32 dst, u32 local, int scope)
964 struct in_device *in_dev;
968 if ((in_dev = __in_dev_get_rcu(dev)))
969 addr = confirm_addr_indev(in_dev, dst, local, scope);
975 read_lock(&dev_base_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);
985 read_unlock(&dev_base_lock);
994 int register_inetaddr_notifier(struct notifier_block *nb)
996 return blocking_notifier_chain_register(&inetaddr_chain, nb);
999 int unregister_inetaddr_notifier(struct notifier_block *nb)
1001 return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
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.
1007 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
1009 struct in_ifaddr *ifa;
1012 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1013 char old[IFNAMSIZ], *dot;
1015 memcpy(old, ifa->ifa_label, IFNAMSIZ);
1016 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1019 dot = strchr(ifa->ifa_label, ':');
1021 sprintf(old, ":%d", named);
1024 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) {
1025 strcat(ifa->ifa_label, dot);
1027 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
1032 /* Called only under RTNL semaphore */
1034 static int inetdev_event(struct notifier_block *this, unsigned long event,
1037 struct net_device *dev = ptr;
1038 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1043 if (event == NETDEV_REGISTER && dev == &loopback_dev) {
1044 in_dev = inetdev_init(dev);
1046 panic("devinet: Failed to create loopback\n");
1047 in_dev->cnf.no_xfrm = 1;
1048 in_dev->cnf.no_policy = 1;
1054 case NETDEV_REGISTER:
1055 printk(KERN_DEBUG "inetdev_event: bug\n");
1061 if (dev == &loopback_dev) {
1062 struct in_ifaddr *ifa;
1063 if ((ifa = inet_alloc_ifa()) != NULL) {
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);
1080 case NETDEV_CHANGEMTU:
1083 /* MTU falled under 68, disable IP */
1084 case NETDEV_UNREGISTER:
1085 inetdev_destroy(in_dev);
1087 case NETDEV_CHANGENAME:
1088 /* Do not notify about label change, this event is
1089 * not interesting to applications using netlink.
1091 inetdev_changename(dev, in_dev);
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);
1106 static struct notifier_block ip_netdev_notifier = {
1107 .notifier_call =inetdev_event,
1110 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1111 u32 pid, u32 seq, int event, unsigned int flags)
1113 struct ifaddrmsg *ifm;
1114 struct nlmsghdr *nlh;
1116 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
1120 ifm = nlmsg_data(nlh);
1121 ifm->ifa_family = AF_INET;
1122 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1123 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1124 ifm->ifa_scope = ifa->ifa_scope;
1125 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1127 if (ifa->ifa_address)
1128 NLA_PUT_U32(skb, IFA_ADDRESS, ifa->ifa_address);
1131 NLA_PUT_U32(skb, IFA_LOCAL, ifa->ifa_local);
1133 if (ifa->ifa_broadcast)
1134 NLA_PUT_U32(skb, IFA_BROADCAST, ifa->ifa_broadcast);
1136 if (ifa->ifa_anycast)
1137 NLA_PUT_U32(skb, IFA_ANYCAST, ifa->ifa_anycast);
1139 if (ifa->ifa_label[0])
1140 NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label);
1142 return nlmsg_end(skb, nlh);
1145 return nlmsg_cancel(skb, nlh);
1148 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1151 struct net_device *dev;
1152 struct in_device *in_dev;
1153 struct in_ifaddr *ifa;
1154 int s_ip_idx, s_idx = cb->args[0];
1156 s_ip_idx = ip_idx = cb->args[1];
1157 read_lock(&dev_base_lock);
1158 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
1164 if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
1169 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1170 ifa = ifa->ifa_next, ip_idx++) {
1171 if (ip_idx < s_ip_idx)
1173 if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1175 RTM_NEWADDR, NLM_F_MULTI) <= 0) {
1184 read_unlock(&dev_base_lock);
1186 cb->args[1] = ip_idx;
1191 static void rtmsg_ifa(int event, struct in_ifaddr* ifa)
1193 struct sk_buff *skb;
1195 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1197 netlink_set_err(rtnl, 0, RTNLGRP_IPV4_IFADDR, ENOBUFS);
1198 else if (inet_fill_ifaddr(skb, ifa, 0, 0, event, 0) < 0) {
1200 netlink_set_err(rtnl, 0, RTNLGRP_IPV4_IFADDR, EINVAL);
1202 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV4_IFADDR, GFP_KERNEL);
1205 static struct rtnetlink_link inet_rtnetlink_table[RTM_NR_MSGTYPES] = {
1206 [RTM_NEWADDR - RTM_BASE] = { .doit = inet_rtm_newaddr, },
1207 [RTM_DELADDR - RTM_BASE] = { .doit = inet_rtm_deladdr, },
1208 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet_dump_ifaddr, },
1209 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet_rtm_newroute, },
1210 [RTM_DELROUTE - RTM_BASE] = { .doit = inet_rtm_delroute, },
1211 [RTM_GETROUTE - RTM_BASE] = { .doit = inet_rtm_getroute,
1212 .dumpit = inet_dump_fib, },
1213 #ifdef CONFIG_IP_MULTIPLE_TABLES
1214 [RTM_GETRULE - RTM_BASE] = { .dumpit = fib4_rules_dump, },
1218 #ifdef CONFIG_SYSCTL
1220 void inet_forward_change(void)
1222 struct net_device *dev;
1223 int on = ipv4_devconf.forwarding;
1225 ipv4_devconf.accept_redirects = !on;
1226 ipv4_devconf_dflt.forwarding = on;
1228 read_lock(&dev_base_lock);
1229 for (dev = dev_base; dev; dev = dev->next) {
1230 struct in_device *in_dev;
1232 in_dev = __in_dev_get_rcu(dev);
1234 in_dev->cnf.forwarding = on;
1237 read_unlock(&dev_base_lock);
1242 static int devinet_sysctl_forward(ctl_table *ctl, int write,
1243 struct file* filp, void __user *buffer,
1244 size_t *lenp, loff_t *ppos)
1246 int *valp = ctl->data;
1248 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1250 if (write && *valp != val) {
1251 if (valp == &ipv4_devconf.forwarding)
1252 inet_forward_change();
1253 else if (valp != &ipv4_devconf_dflt.forwarding)
1260 int ipv4_doint_and_flush(ctl_table *ctl, int write,
1261 struct file* filp, void __user *buffer,
1262 size_t *lenp, loff_t *ppos)
1264 int *valp = ctl->data;
1266 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1268 if (write && *valp != val)
1274 int ipv4_doint_and_flush_strategy(ctl_table *table, int __user *name, int nlen,
1275 void __user *oldval, size_t __user *oldlenp,
1276 void __user *newval, size_t newlen,
1279 int *valp = table->data;
1282 if (!newval || !newlen)
1285 if (newlen != sizeof(int))
1288 if (get_user(new, (int __user *)newval))
1294 if (oldval && oldlenp) {
1297 if (get_user(len, oldlenp))
1301 if (len > table->maxlen)
1302 len = table->maxlen;
1303 if (copy_to_user(oldval, valp, len))
1305 if (put_user(len, oldlenp))
1316 static struct devinet_sysctl_table {
1317 struct ctl_table_header *sysctl_header;
1318 ctl_table devinet_vars[__NET_IPV4_CONF_MAX];
1319 ctl_table devinet_dev[2];
1320 ctl_table devinet_conf_dir[2];
1321 ctl_table devinet_proto_dir[2];
1322 ctl_table devinet_root_dir[2];
1323 } devinet_sysctl = {
1326 .ctl_name = NET_IPV4_CONF_FORWARDING,
1327 .procname = "forwarding",
1328 .data = &ipv4_devconf.forwarding,
1329 .maxlen = sizeof(int),
1331 .proc_handler = &devinet_sysctl_forward,
1334 .ctl_name = NET_IPV4_CONF_MC_FORWARDING,
1335 .procname = "mc_forwarding",
1336 .data = &ipv4_devconf.mc_forwarding,
1337 .maxlen = sizeof(int),
1339 .proc_handler = &proc_dointvec,
1342 .ctl_name = NET_IPV4_CONF_ACCEPT_REDIRECTS,
1343 .procname = "accept_redirects",
1344 .data = &ipv4_devconf.accept_redirects,
1345 .maxlen = sizeof(int),
1347 .proc_handler = &proc_dointvec,
1350 .ctl_name = NET_IPV4_CONF_SECURE_REDIRECTS,
1351 .procname = "secure_redirects",
1352 .data = &ipv4_devconf.secure_redirects,
1353 .maxlen = sizeof(int),
1355 .proc_handler = &proc_dointvec,
1358 .ctl_name = NET_IPV4_CONF_SHARED_MEDIA,
1359 .procname = "shared_media",
1360 .data = &ipv4_devconf.shared_media,
1361 .maxlen = sizeof(int),
1363 .proc_handler = &proc_dointvec,
1366 .ctl_name = NET_IPV4_CONF_RP_FILTER,
1367 .procname = "rp_filter",
1368 .data = &ipv4_devconf.rp_filter,
1369 .maxlen = sizeof(int),
1371 .proc_handler = &proc_dointvec,
1374 .ctl_name = NET_IPV4_CONF_SEND_REDIRECTS,
1375 .procname = "send_redirects",
1376 .data = &ipv4_devconf.send_redirects,
1377 .maxlen = sizeof(int),
1379 .proc_handler = &proc_dointvec,
1382 .ctl_name = NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE,
1383 .procname = "accept_source_route",
1384 .data = &ipv4_devconf.accept_source_route,
1385 .maxlen = sizeof(int),
1387 .proc_handler = &proc_dointvec,
1390 .ctl_name = NET_IPV4_CONF_PROXY_ARP,
1391 .procname = "proxy_arp",
1392 .data = &ipv4_devconf.proxy_arp,
1393 .maxlen = sizeof(int),
1395 .proc_handler = &proc_dointvec,
1398 .ctl_name = NET_IPV4_CONF_MEDIUM_ID,
1399 .procname = "medium_id",
1400 .data = &ipv4_devconf.medium_id,
1401 .maxlen = sizeof(int),
1403 .proc_handler = &proc_dointvec,
1406 .ctl_name = NET_IPV4_CONF_BOOTP_RELAY,
1407 .procname = "bootp_relay",
1408 .data = &ipv4_devconf.bootp_relay,
1409 .maxlen = sizeof(int),
1411 .proc_handler = &proc_dointvec,
1414 .ctl_name = NET_IPV4_CONF_LOG_MARTIANS,
1415 .procname = "log_martians",
1416 .data = &ipv4_devconf.log_martians,
1417 .maxlen = sizeof(int),
1419 .proc_handler = &proc_dointvec,
1422 .ctl_name = NET_IPV4_CONF_TAG,
1424 .data = &ipv4_devconf.tag,
1425 .maxlen = sizeof(int),
1427 .proc_handler = &proc_dointvec,
1430 .ctl_name = NET_IPV4_CONF_ARPFILTER,
1431 .procname = "arp_filter",
1432 .data = &ipv4_devconf.arp_filter,
1433 .maxlen = sizeof(int),
1435 .proc_handler = &proc_dointvec,
1438 .ctl_name = NET_IPV4_CONF_ARP_ANNOUNCE,
1439 .procname = "arp_announce",
1440 .data = &ipv4_devconf.arp_announce,
1441 .maxlen = sizeof(int),
1443 .proc_handler = &proc_dointvec,
1446 .ctl_name = NET_IPV4_CONF_ARP_IGNORE,
1447 .procname = "arp_ignore",
1448 .data = &ipv4_devconf.arp_ignore,
1449 .maxlen = sizeof(int),
1451 .proc_handler = &proc_dointvec,
1454 .ctl_name = NET_IPV4_CONF_ARP_ACCEPT,
1455 .procname = "arp_accept",
1456 .data = &ipv4_devconf.arp_accept,
1457 .maxlen = sizeof(int),
1459 .proc_handler = &proc_dointvec,
1462 .ctl_name = NET_IPV4_CONF_NOXFRM,
1463 .procname = "disable_xfrm",
1464 .data = &ipv4_devconf.no_xfrm,
1465 .maxlen = sizeof(int),
1467 .proc_handler = &ipv4_doint_and_flush,
1468 .strategy = &ipv4_doint_and_flush_strategy,
1471 .ctl_name = NET_IPV4_CONF_NOPOLICY,
1472 .procname = "disable_policy",
1473 .data = &ipv4_devconf.no_policy,
1474 .maxlen = sizeof(int),
1476 .proc_handler = &ipv4_doint_and_flush,
1477 .strategy = &ipv4_doint_and_flush_strategy,
1480 .ctl_name = NET_IPV4_CONF_FORCE_IGMP_VERSION,
1481 .procname = "force_igmp_version",
1482 .data = &ipv4_devconf.force_igmp_version,
1483 .maxlen = sizeof(int),
1485 .proc_handler = &ipv4_doint_and_flush,
1486 .strategy = &ipv4_doint_and_flush_strategy,
1489 .ctl_name = NET_IPV4_CONF_PROMOTE_SECONDARIES,
1490 .procname = "promote_secondaries",
1491 .data = &ipv4_devconf.promote_secondaries,
1492 .maxlen = sizeof(int),
1494 .proc_handler = &ipv4_doint_and_flush,
1495 .strategy = &ipv4_doint_and_flush_strategy,
1500 .ctl_name = NET_PROTO_CONF_ALL,
1503 .child = devinet_sysctl.devinet_vars,
1506 .devinet_conf_dir = {
1508 .ctl_name = NET_IPV4_CONF,
1511 .child = devinet_sysctl.devinet_dev,
1514 .devinet_proto_dir = {
1516 .ctl_name = NET_IPV4,
1519 .child = devinet_sysctl.devinet_conf_dir,
1522 .devinet_root_dir = {
1524 .ctl_name = CTL_NET,
1527 .child = devinet_sysctl.devinet_proto_dir,
1532 static void devinet_sysctl_register(struct in_device *in_dev,
1533 struct ipv4_devconf *p)
1536 struct net_device *dev = in_dev ? in_dev->dev : NULL;
1537 struct devinet_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
1538 char *dev_name = NULL;
1542 memcpy(t, &devinet_sysctl, sizeof(*t));
1543 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1544 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
1545 t->devinet_vars[i].de = NULL;
1549 dev_name = dev->name;
1550 t->devinet_dev[0].ctl_name = dev->ifindex;
1552 dev_name = "default";
1553 t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
1557 * Make a copy of dev_name, because '.procname' is regarded as const
1558 * by sysctl and we wouldn't want anyone to change it under our feet
1559 * (see SIOCSIFNAME).
1561 dev_name = kstrdup(dev_name, GFP_KERNEL);
1565 t->devinet_dev[0].procname = dev_name;
1566 t->devinet_dev[0].child = t->devinet_vars;
1567 t->devinet_dev[0].de = NULL;
1568 t->devinet_conf_dir[0].child = t->devinet_dev;
1569 t->devinet_conf_dir[0].de = NULL;
1570 t->devinet_proto_dir[0].child = t->devinet_conf_dir;
1571 t->devinet_proto_dir[0].de = NULL;
1572 t->devinet_root_dir[0].child = t->devinet_proto_dir;
1573 t->devinet_root_dir[0].de = NULL;
1575 t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
1576 if (!t->sysctl_header)
1590 static void devinet_sysctl_unregister(struct ipv4_devconf *p)
1593 struct devinet_sysctl_table *t = p->sysctl;
1595 unregister_sysctl_table(t->sysctl_header);
1596 kfree(t->devinet_dev[0].procname);
1602 void __init devinet_init(void)
1604 register_gifconf(PF_INET, inet_gifconf);
1605 register_netdevice_notifier(&ip_netdev_notifier);
1606 rtnetlink_links[PF_INET] = inet_rtnetlink_table;
1607 #ifdef CONFIG_SYSCTL
1608 devinet_sysctl.sysctl_header =
1609 register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
1610 devinet_sysctl_register(NULL, &ipv4_devconf_dflt);
1614 EXPORT_SYMBOL(in_dev_finish_destroy);
1615 EXPORT_SYMBOL(inet_select_addr);
1616 EXPORT_SYMBOL(inetdev_by_index);
1617 EXPORT_SYMBOL(register_inetaddr_notifier);
1618 EXPORT_SYMBOL(unregister_inetaddr_notifier);