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/string.h>
40 #include <linux/socket.h>
41 #include <linux/sockios.h>
43 #include <linux/errno.h>
44 #include <linux/interrupt.h>
45 #include <linux/if_addr.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/init.h>
52 #include <linux/notifier.h>
53 #include <linux/inetdevice.h>
54 #include <linux/igmp.h>
56 #include <linux/sysctl.h>
58 #include <linux/kmod.h>
62 #include <net/route.h>
63 #include <net/ip_fib.h>
64 #include <net/rtnetlink.h>
66 struct ipv4_devconf ipv4_devconf = {
68 [NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1,
69 [NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1,
70 [NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1,
71 [NET_IPV4_CONF_SHARED_MEDIA - 1] = 1,
75 static struct ipv4_devconf ipv4_devconf_dflt = {
77 [NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1,
78 [NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1,
79 [NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1,
80 [NET_IPV4_CONF_SHARED_MEDIA - 1] = 1,
81 [NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
85 #define IPV4_DEVCONF_DFLT(attr) IPV4_DEVCONF(ipv4_devconf_dflt, attr)
87 static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
88 [IFA_LOCAL] = { .type = NLA_U32 },
89 [IFA_ADDRESS] = { .type = NLA_U32 },
90 [IFA_BROADCAST] = { .type = NLA_U32 },
91 [IFA_ANYCAST] = { .type = NLA_U32 },
92 [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
95 static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
97 static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
98 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
101 static void devinet_sysctl_register(struct in_device *idev);
102 static void devinet_sysctl_unregister(struct ipv4_devconf *p);
105 /* Locks all the inet devices. */
107 static struct in_ifaddr *inet_alloc_ifa(void)
109 struct in_ifaddr *ifa = kzalloc(sizeof(*ifa), GFP_KERNEL);
112 INIT_RCU_HEAD(&ifa->rcu_head);
118 static void inet_rcu_free_ifa(struct rcu_head *head)
120 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
122 in_dev_put(ifa->ifa_dev);
126 static inline void inet_free_ifa(struct in_ifaddr *ifa)
128 call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
131 void in_dev_finish_destroy(struct in_device *idev)
133 struct net_device *dev = idev->dev;
135 BUG_TRAP(!idev->ifa_list);
136 BUG_TRAP(!idev->mc_list);
137 #ifdef NET_REFCNT_DEBUG
138 printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n",
139 idev, dev ? dev->name : "NIL");
143 printk("Freeing alive in_device %p\n", idev);
149 static struct in_device *inetdev_init(struct net_device *dev)
151 struct in_device *in_dev;
155 in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
158 INIT_RCU_HEAD(&in_dev->rcu_head);
159 memcpy(&in_dev->cnf, &ipv4_devconf_dflt, sizeof(in_dev->cnf));
160 in_dev->cnf.sysctl = NULL;
162 if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL)
164 /* Reference in_dev->dev */
167 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
168 NET_IPV4_NEIGH, "ipv4", NULL, NULL);
171 /* Account for reference dev->ip_ptr (below) */
175 devinet_sysctl_register(in_dev);
177 ip_mc_init_dev(in_dev);
178 if (dev->flags & IFF_UP)
181 /* we can receive as soon as ip_ptr is set -- do this last */
182 rcu_assign_pointer(dev->ip_ptr, in_dev);
191 static void in_dev_rcu_put(struct rcu_head *head)
193 struct in_device *idev = container_of(head, struct in_device, rcu_head);
197 static void inetdev_destroy(struct in_device *in_dev)
199 struct in_ifaddr *ifa;
200 struct net_device *dev;
208 ip_mc_destroy_dev(in_dev);
210 while ((ifa = in_dev->ifa_list) != NULL) {
211 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
216 devinet_sysctl_unregister(&in_dev->cnf);
222 neigh_sysctl_unregister(in_dev->arp_parms);
224 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
227 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
230 int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
233 for_primary_ifa(in_dev) {
234 if (inet_ifa_match(a, ifa)) {
235 if (!b || inet_ifa_match(b, ifa)) {
240 } endfor_ifa(in_dev);
245 static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
246 int destroy, struct nlmsghdr *nlh, u32 pid)
248 struct in_ifaddr *promote = NULL;
249 struct in_ifaddr *ifa, *ifa1 = *ifap;
250 struct in_ifaddr *last_prim = in_dev->ifa_list;
251 struct in_ifaddr *prev_prom = NULL;
252 int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
256 /* 1. Deleting primary ifaddr forces deletion all secondaries
257 * unless alias promotion is set
260 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
261 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
263 while ((ifa = *ifap1) != NULL) {
264 if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
265 ifa1->ifa_scope <= ifa->ifa_scope)
268 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
269 ifa1->ifa_mask != ifa->ifa_mask ||
270 !inet_ifa_match(ifa1->ifa_address, ifa)) {
271 ifap1 = &ifa->ifa_next;
277 *ifap1 = ifa->ifa_next;
279 rtmsg_ifa(RTM_DELADDR, ifa, nlh, pid);
280 blocking_notifier_call_chain(&inetaddr_chain,
292 *ifap = ifa1->ifa_next;
294 /* 3. Announce address deletion */
296 /* Send message first, then call notifier.
297 At first sight, FIB update triggered by notifier
298 will refer to already deleted ifaddr, that could confuse
299 netlink listeners. It is not true: look, gated sees
300 that route deleted and if it still thinks that ifaddr
301 is valid, it will try to restore deleted routes... Grr.
302 So that, this order is correct.
304 rtmsg_ifa(RTM_DELADDR, ifa1, nlh, pid);
305 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
310 prev_prom->ifa_next = promote->ifa_next;
311 promote->ifa_next = last_prim->ifa_next;
312 last_prim->ifa_next = promote;
315 promote->ifa_flags &= ~IFA_F_SECONDARY;
316 rtmsg_ifa(RTM_NEWADDR, promote, nlh, pid);
317 blocking_notifier_call_chain(&inetaddr_chain,
319 for (ifa = promote->ifa_next; ifa; ifa = ifa->ifa_next) {
320 if (ifa1->ifa_mask != ifa->ifa_mask ||
321 !inet_ifa_match(ifa1->ifa_address, ifa))
331 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
334 __inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
337 static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
340 struct in_device *in_dev = ifa->ifa_dev;
341 struct in_ifaddr *ifa1, **ifap, **last_primary;
345 if (!ifa->ifa_local) {
350 ifa->ifa_flags &= ~IFA_F_SECONDARY;
351 last_primary = &in_dev->ifa_list;
353 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
354 ifap = &ifa1->ifa_next) {
355 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
356 ifa->ifa_scope <= ifa1->ifa_scope)
357 last_primary = &ifa1->ifa_next;
358 if (ifa1->ifa_mask == ifa->ifa_mask &&
359 inet_ifa_match(ifa1->ifa_address, ifa)) {
360 if (ifa1->ifa_local == ifa->ifa_local) {
364 if (ifa1->ifa_scope != ifa->ifa_scope) {
368 ifa->ifa_flags |= IFA_F_SECONDARY;
372 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
373 net_srandom(ifa->ifa_local);
377 ifa->ifa_next = *ifap;
380 /* Send message first, then call notifier.
381 Notifier will trigger FIB update, so that
382 listeners of netlink will know about new ifaddr */
383 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, pid);
384 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
389 static int inet_insert_ifa(struct in_ifaddr *ifa)
391 return __inet_insert_ifa(ifa, NULL, 0);
394 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
396 struct in_device *in_dev = __in_dev_get_rtnl(dev);
404 ipv4_devconf_setall(in_dev);
405 if (ifa->ifa_dev != in_dev) {
406 BUG_TRAP(!ifa->ifa_dev);
408 ifa->ifa_dev = in_dev;
410 if (LOOPBACK(ifa->ifa_local))
411 ifa->ifa_scope = RT_SCOPE_HOST;
412 return inet_insert_ifa(ifa);
415 struct in_device *inetdev_by_index(int ifindex)
417 struct net_device *dev;
418 struct in_device *in_dev = NULL;
419 read_lock(&dev_base_lock);
420 dev = __dev_get_by_index(&init_net, ifindex);
422 in_dev = in_dev_get(dev);
423 read_unlock(&dev_base_lock);
427 /* Called only from RTNL semaphored context. No locks. */
429 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
434 for_primary_ifa(in_dev) {
435 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
437 } endfor_ifa(in_dev);
441 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
443 struct net *net = skb->sk->sk_net;
444 struct nlattr *tb[IFA_MAX+1];
445 struct in_device *in_dev;
446 struct ifaddrmsg *ifm;
447 struct in_ifaddr *ifa, **ifap;
452 if (net != &init_net)
455 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
459 ifm = nlmsg_data(nlh);
460 in_dev = inetdev_by_index(ifm->ifa_index);
461 if (in_dev == NULL) {
466 __in_dev_put(in_dev);
468 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
469 ifap = &ifa->ifa_next) {
471 ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL]))
474 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
477 if (tb[IFA_ADDRESS] &&
478 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
479 !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa)))
482 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).pid);
486 err = -EADDRNOTAVAIL;
491 static struct in_ifaddr *rtm_to_ifaddr(struct nlmsghdr *nlh)
493 struct nlattr *tb[IFA_MAX+1];
494 struct in_ifaddr *ifa;
495 struct ifaddrmsg *ifm;
496 struct net_device *dev;
497 struct in_device *in_dev;
500 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
504 ifm = nlmsg_data(nlh);
505 if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL) {
510 dev = __dev_get_by_index(&init_net, ifm->ifa_index);
516 in_dev = __in_dev_get_rtnl(dev);
517 if (in_dev == NULL) {
522 ifa = inet_alloc_ifa();
525 * A potential indev allocation can be left alive, it stays
526 * assigned to its device and is destroy with it.
532 ipv4_devconf_setall(in_dev);
535 if (tb[IFA_ADDRESS] == NULL)
536 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
538 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
539 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
540 ifa->ifa_flags = ifm->ifa_flags;
541 ifa->ifa_scope = ifm->ifa_scope;
542 ifa->ifa_dev = in_dev;
544 ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]);
545 ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]);
547 if (tb[IFA_BROADCAST])
548 ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]);
551 ifa->ifa_anycast = nla_get_be32(tb[IFA_ANYCAST]);
554 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
556 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
564 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
566 struct net *net = skb->sk->sk_net;
567 struct in_ifaddr *ifa;
571 if (net != &init_net)
574 ifa = rtm_to_ifaddr(nlh);
578 return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).pid);
582 * Determine a default network mask, based on the IP address.
585 static __inline__ int inet_abc_len(__be32 addr)
587 int rc = -1; /* Something else, probably a multicast. */
592 __u32 haddr = ntohl(addr);
594 if (IN_CLASSA(haddr))
596 else if (IN_CLASSB(haddr))
598 else if (IN_CLASSC(haddr))
606 int devinet_ioctl(unsigned int cmd, void __user *arg)
609 struct sockaddr_in sin_orig;
610 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
611 struct in_device *in_dev;
612 struct in_ifaddr **ifap = NULL;
613 struct in_ifaddr *ifa = NULL;
614 struct net_device *dev;
617 int tryaddrmatch = 0;
620 * Fetch the caller's info block into kernel space
623 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
625 ifr.ifr_name[IFNAMSIZ - 1] = 0;
627 /* save original address for comparison */
628 memcpy(&sin_orig, sin, sizeof(*sin));
630 colon = strchr(ifr.ifr_name, ':');
635 dev_load(&init_net, ifr.ifr_name);
639 case SIOCGIFADDR: /* Get interface address */
640 case SIOCGIFBRDADDR: /* Get the broadcast address */
641 case SIOCGIFDSTADDR: /* Get the destination address */
642 case SIOCGIFNETMASK: /* Get the netmask for the interface */
643 /* Note that these ioctls will not sleep,
644 so that we do not impose a lock.
645 One day we will be forced to put shlock here (I mean SMP)
647 tryaddrmatch = (sin_orig.sin_family == AF_INET);
648 memset(sin, 0, sizeof(*sin));
649 sin->sin_family = AF_INET;
654 if (!capable(CAP_NET_ADMIN))
657 case SIOCSIFADDR: /* Set interface address (and family) */
658 case SIOCSIFBRDADDR: /* Set the broadcast address */
659 case SIOCSIFDSTADDR: /* Set the destination address */
660 case SIOCSIFNETMASK: /* Set the netmask for the interface */
662 if (!capable(CAP_NET_ADMIN))
665 if (sin->sin_family != AF_INET)
676 if ((dev = __dev_get_by_name(&init_net, ifr.ifr_name)) == NULL)
682 if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) {
684 /* Matthias Andree */
685 /* compare label and address (4.4BSD style) */
686 /* note: we only do this for a limited set of ioctls
687 and only if the original address family was AF_INET.
688 This is checked above. */
689 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
690 ifap = &ifa->ifa_next) {
691 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
692 sin_orig.sin_addr.s_addr ==
698 /* we didn't get a match, maybe the application is
699 4.3BSD-style and passed in junk so we fall back to
700 comparing just the label */
702 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
703 ifap = &ifa->ifa_next)
704 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
709 ret = -EADDRNOTAVAIL;
710 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
714 case SIOCGIFADDR: /* Get interface address */
715 sin->sin_addr.s_addr = ifa->ifa_local;
718 case SIOCGIFBRDADDR: /* Get the broadcast address */
719 sin->sin_addr.s_addr = ifa->ifa_broadcast;
722 case SIOCGIFDSTADDR: /* Get the destination address */
723 sin->sin_addr.s_addr = ifa->ifa_address;
726 case SIOCGIFNETMASK: /* Get the netmask for the interface */
727 sin->sin_addr.s_addr = ifa->ifa_mask;
732 ret = -EADDRNOTAVAIL;
736 if (!(ifr.ifr_flags & IFF_UP))
737 inet_del_ifa(in_dev, ifap, 1);
740 ret = dev_change_flags(dev, ifr.ifr_flags);
743 case SIOCSIFADDR: /* Set interface address (and family) */
745 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
750 if ((ifa = inet_alloc_ifa()) == NULL)
753 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
755 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
758 if (ifa->ifa_local == sin->sin_addr.s_addr)
760 inet_del_ifa(in_dev, ifap, 0);
761 ifa->ifa_broadcast = 0;
762 ifa->ifa_anycast = 0;
765 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
767 if (!(dev->flags & IFF_POINTOPOINT)) {
768 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
769 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
770 if ((dev->flags & IFF_BROADCAST) &&
771 ifa->ifa_prefixlen < 31)
772 ifa->ifa_broadcast = ifa->ifa_address |
775 ifa->ifa_prefixlen = 32;
776 ifa->ifa_mask = inet_make_mask(32);
778 ret = inet_set_ifa(dev, ifa);
781 case SIOCSIFBRDADDR: /* Set the broadcast address */
783 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
784 inet_del_ifa(in_dev, ifap, 0);
785 ifa->ifa_broadcast = sin->sin_addr.s_addr;
786 inet_insert_ifa(ifa);
790 case SIOCSIFDSTADDR: /* Set the destination address */
792 if (ifa->ifa_address == sin->sin_addr.s_addr)
795 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
798 inet_del_ifa(in_dev, ifap, 0);
799 ifa->ifa_address = sin->sin_addr.s_addr;
800 inet_insert_ifa(ifa);
803 case SIOCSIFNETMASK: /* Set the netmask for the interface */
806 * The mask we set must be legal.
809 if (bad_mask(sin->sin_addr.s_addr, 0))
812 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
813 __be32 old_mask = ifa->ifa_mask;
814 inet_del_ifa(in_dev, ifap, 0);
815 ifa->ifa_mask = sin->sin_addr.s_addr;
816 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
818 /* See if current broadcast address matches
819 * with current netmask, then recalculate
820 * the broadcast address. Otherwise it's a
821 * funny address, so don't touch it since
822 * the user seems to know what (s)he's doing...
824 if ((dev->flags & IFF_BROADCAST) &&
825 (ifa->ifa_prefixlen < 31) &&
826 (ifa->ifa_broadcast ==
827 (ifa->ifa_local|~old_mask))) {
828 ifa->ifa_broadcast = (ifa->ifa_local |
829 ~sin->sin_addr.s_addr);
831 inet_insert_ifa(ifa);
841 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
845 static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
847 struct in_device *in_dev = __in_dev_get_rtnl(dev);
848 struct in_ifaddr *ifa;
852 if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
855 for (; ifa; ifa = ifa->ifa_next) {
860 if (len < (int) sizeof(ifr))
862 memset(&ifr, 0, sizeof(struct ifreq));
864 strcpy(ifr.ifr_name, ifa->ifa_label);
866 strcpy(ifr.ifr_name, dev->name);
868 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
869 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
872 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
876 buf += sizeof(struct ifreq);
877 len -= sizeof(struct ifreq);
878 done += sizeof(struct ifreq);
884 __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
887 struct in_device *in_dev;
890 in_dev = __in_dev_get_rcu(dev);
894 for_primary_ifa(in_dev) {
895 if (ifa->ifa_scope > scope)
897 if (!dst || inet_ifa_match(dst, ifa)) {
898 addr = ifa->ifa_local;
902 addr = ifa->ifa_local;
903 } endfor_ifa(in_dev);
910 /* Not loopback addresses on loopback should be preferred
911 in this case. It is importnat that lo is the first interface
914 read_lock(&dev_base_lock);
916 for_each_netdev(&init_net, dev) {
917 if ((in_dev = __in_dev_get_rcu(dev)) == NULL)
920 for_primary_ifa(in_dev) {
921 if (ifa->ifa_scope != RT_SCOPE_LINK &&
922 ifa->ifa_scope <= scope) {
923 addr = ifa->ifa_local;
924 goto out_unlock_both;
926 } endfor_ifa(in_dev);
929 read_unlock(&dev_base_lock);
935 static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
936 __be32 local, int scope)
943 (local == ifa->ifa_local || !local) &&
944 ifa->ifa_scope <= scope) {
945 addr = ifa->ifa_local;
950 same = (!local || inet_ifa_match(local, ifa)) &&
951 (!dst || inet_ifa_match(dst, ifa));
955 /* Is the selected addr into dst subnet? */
956 if (inet_ifa_match(addr, ifa))
958 /* No, then can we use new local src? */
959 if (ifa->ifa_scope <= scope) {
960 addr = ifa->ifa_local;
963 /* search for large dst subnet for addr */
967 } endfor_ifa(in_dev);
969 return same? addr : 0;
973 * Confirm that local IP address exists using wildcards:
974 * - dev: only on this interface, 0=any interface
975 * - dst: only in the same subnet as dst, 0=any dst
976 * - local: address, 0=autoselect the local address
977 * - scope: maximum allowed scope value for the local address
979 __be32 inet_confirm_addr(const struct net_device *dev, __be32 dst, __be32 local, int scope)
982 struct in_device *in_dev;
986 if ((in_dev = __in_dev_get_rcu(dev)))
987 addr = confirm_addr_indev(in_dev, dst, local, scope);
993 read_lock(&dev_base_lock);
995 for_each_netdev(&init_net, dev) {
996 if ((in_dev = __in_dev_get_rcu(dev))) {
997 addr = confirm_addr_indev(in_dev, dst, local, scope);
1003 read_unlock(&dev_base_lock);
1012 int register_inetaddr_notifier(struct notifier_block *nb)
1014 return blocking_notifier_chain_register(&inetaddr_chain, nb);
1017 int unregister_inetaddr_notifier(struct notifier_block *nb)
1019 return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
1022 /* Rename ifa_labels for a device name change. Make some effort to preserve existing
1023 * alias numbering and to create unique labels if possible.
1025 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
1027 struct in_ifaddr *ifa;
1030 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1031 char old[IFNAMSIZ], *dot;
1033 memcpy(old, ifa->ifa_label, IFNAMSIZ);
1034 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1037 dot = strchr(old, ':');
1039 sprintf(old, ":%d", named);
1042 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) {
1043 strcat(ifa->ifa_label, dot);
1045 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
1050 /* Called only under RTNL semaphore */
1052 static int inetdev_event(struct notifier_block *this, unsigned long event,
1055 struct net_device *dev = ptr;
1056 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1058 if (dev->nd_net != &init_net)
1064 if (event == NETDEV_REGISTER) {
1065 in_dev = inetdev_init(dev);
1067 return notifier_from_errno(-ENOMEM);
1068 if (dev->flags & IFF_LOOPBACK) {
1069 IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
1070 IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
1077 case NETDEV_REGISTER:
1078 printk(KERN_DEBUG "inetdev_event: bug\n");
1084 if (dev->flags & IFF_LOOPBACK) {
1085 struct in_ifaddr *ifa;
1086 if ((ifa = inet_alloc_ifa()) != NULL) {
1088 ifa->ifa_address = htonl(INADDR_LOOPBACK);
1089 ifa->ifa_prefixlen = 8;
1090 ifa->ifa_mask = inet_make_mask(8);
1091 in_dev_hold(in_dev);
1092 ifa->ifa_dev = in_dev;
1093 ifa->ifa_scope = RT_SCOPE_HOST;
1094 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1095 inet_insert_ifa(ifa);
1103 case NETDEV_CHANGEMTU:
1106 /* MTU falled under 68, disable IP */
1107 case NETDEV_UNREGISTER:
1108 inetdev_destroy(in_dev);
1110 case NETDEV_CHANGENAME:
1111 /* Do not notify about label change, this event is
1112 * not interesting to applications using netlink.
1114 inetdev_changename(dev, in_dev);
1116 #ifdef CONFIG_SYSCTL
1117 devinet_sysctl_unregister(&in_dev->cnf);
1118 neigh_sysctl_unregister(in_dev->arp_parms);
1119 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
1120 NET_IPV4_NEIGH, "ipv4", NULL, NULL);
1121 devinet_sysctl_register(in_dev);
1129 static struct notifier_block ip_netdev_notifier = {
1130 .notifier_call =inetdev_event,
1133 static inline size_t inet_nlmsg_size(void)
1135 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1136 + nla_total_size(4) /* IFA_ADDRESS */
1137 + nla_total_size(4) /* IFA_LOCAL */
1138 + nla_total_size(4) /* IFA_BROADCAST */
1139 + nla_total_size(4) /* IFA_ANYCAST */
1140 + nla_total_size(IFNAMSIZ); /* IFA_LABEL */
1143 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1144 u32 pid, u32 seq, int event, unsigned int flags)
1146 struct ifaddrmsg *ifm;
1147 struct nlmsghdr *nlh;
1149 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
1153 ifm = nlmsg_data(nlh);
1154 ifm->ifa_family = AF_INET;
1155 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1156 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1157 ifm->ifa_scope = ifa->ifa_scope;
1158 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1160 if (ifa->ifa_address)
1161 NLA_PUT_BE32(skb, IFA_ADDRESS, ifa->ifa_address);
1164 NLA_PUT_BE32(skb, IFA_LOCAL, ifa->ifa_local);
1166 if (ifa->ifa_broadcast)
1167 NLA_PUT_BE32(skb, IFA_BROADCAST, ifa->ifa_broadcast);
1169 if (ifa->ifa_anycast)
1170 NLA_PUT_BE32(skb, IFA_ANYCAST, ifa->ifa_anycast);
1172 if (ifa->ifa_label[0])
1173 NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label);
1175 return nlmsg_end(skb, nlh);
1178 nlmsg_cancel(skb, nlh);
1182 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1184 struct net *net = skb->sk->sk_net;
1186 struct net_device *dev;
1187 struct in_device *in_dev;
1188 struct in_ifaddr *ifa;
1189 int s_ip_idx, s_idx = cb->args[0];
1191 if (net != &init_net)
1194 s_ip_idx = ip_idx = cb->args[1];
1196 for_each_netdev(&init_net, dev) {
1201 if ((in_dev = __in_dev_get_rtnl(dev)) == NULL)
1204 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1205 ifa = ifa->ifa_next, ip_idx++) {
1206 if (ip_idx < s_ip_idx)
1208 if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1210 RTM_NEWADDR, NLM_F_MULTI) <= 0)
1219 cb->args[1] = ip_idx;
1224 static void rtmsg_ifa(int event, struct in_ifaddr* ifa, struct nlmsghdr *nlh,
1227 struct sk_buff *skb;
1228 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1231 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
1235 err = inet_fill_ifaddr(skb, ifa, pid, seq, event, 0);
1237 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1238 WARN_ON(err == -EMSGSIZE);
1242 err = rtnl_notify(skb, &init_net, pid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
1245 rtnl_set_sk_err(&init_net, RTNLGRP_IPV4_IFADDR, err);
1248 #ifdef CONFIG_SYSCTL
1250 static void devinet_copy_dflt_conf(int i)
1252 struct net_device *dev;
1254 read_lock(&dev_base_lock);
1255 for_each_netdev(&init_net, dev) {
1256 struct in_device *in_dev;
1258 in_dev = __in_dev_get_rcu(dev);
1259 if (in_dev && !test_bit(i, in_dev->cnf.state))
1260 in_dev->cnf.data[i] = ipv4_devconf_dflt.data[i];
1263 read_unlock(&dev_base_lock);
1266 static int devinet_conf_proc(ctl_table *ctl, int write,
1267 struct file* filp, void __user *buffer,
1268 size_t *lenp, loff_t *ppos)
1270 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1273 struct ipv4_devconf *cnf = ctl->extra1;
1274 int i = (int *)ctl->data - cnf->data;
1276 set_bit(i, cnf->state);
1278 if (cnf == &ipv4_devconf_dflt)
1279 devinet_copy_dflt_conf(i);
1285 static int devinet_conf_sysctl(ctl_table *table, int __user *name, int nlen,
1286 void __user *oldval, size_t __user *oldlenp,
1287 void __user *newval, size_t newlen)
1289 struct ipv4_devconf *cnf;
1290 int *valp = table->data;
1294 if (!newval || !newlen)
1297 if (newlen != sizeof(int))
1300 if (get_user(new, (int __user *)newval))
1306 if (oldval && oldlenp) {
1309 if (get_user(len, oldlenp))
1313 if (len > table->maxlen)
1314 len = table->maxlen;
1315 if (copy_to_user(oldval, valp, len))
1317 if (put_user(len, oldlenp))
1324 cnf = table->extra1;
1325 i = (int *)table->data - cnf->data;
1327 set_bit(i, cnf->state);
1329 if (cnf == &ipv4_devconf_dflt)
1330 devinet_copy_dflt_conf(i);
1335 void inet_forward_change(void)
1337 struct net_device *dev;
1338 int on = IPV4_DEVCONF_ALL(FORWARDING);
1340 IPV4_DEVCONF_ALL(ACCEPT_REDIRECTS) = !on;
1341 IPV4_DEVCONF_DFLT(FORWARDING) = on;
1343 read_lock(&dev_base_lock);
1344 for_each_netdev(&init_net, dev) {
1345 struct in_device *in_dev;
1347 in_dev = __in_dev_get_rcu(dev);
1349 IN_DEV_CONF_SET(in_dev, FORWARDING, on);
1352 read_unlock(&dev_base_lock);
1357 static int devinet_sysctl_forward(ctl_table *ctl, int write,
1358 struct file* filp, void __user *buffer,
1359 size_t *lenp, loff_t *ppos)
1361 int *valp = ctl->data;
1363 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1365 if (write && *valp != val) {
1366 if (valp == &IPV4_DEVCONF_ALL(FORWARDING))
1367 inet_forward_change();
1368 else if (valp != &IPV4_DEVCONF_DFLT(FORWARDING))
1375 int ipv4_doint_and_flush(ctl_table *ctl, int write,
1376 struct file* filp, void __user *buffer,
1377 size_t *lenp, loff_t *ppos)
1379 int *valp = ctl->data;
1381 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1383 if (write && *valp != val)
1389 int ipv4_doint_and_flush_strategy(ctl_table *table, int __user *name, int nlen,
1390 void __user *oldval, size_t __user *oldlenp,
1391 void __user *newval, size_t newlen)
1393 int ret = devinet_conf_sysctl(table, name, nlen, oldval, oldlenp,
1403 #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc, sysctl) \
1405 .ctl_name = NET_IPV4_CONF_ ## attr, \
1407 .data = ipv4_devconf.data + \
1408 NET_IPV4_CONF_ ## attr - 1, \
1409 .maxlen = sizeof(int), \
1411 .proc_handler = proc, \
1412 .strategy = sysctl, \
1413 .extra1 = &ipv4_devconf, \
1416 #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
1417 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc, \
1418 devinet_conf_sysctl)
1420 #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
1421 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc, \
1422 devinet_conf_sysctl)
1424 #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc, sysctl) \
1425 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc, sysctl)
1427 #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
1428 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush, \
1429 ipv4_doint_and_flush_strategy)
1431 static struct devinet_sysctl_table {
1432 struct ctl_table_header *sysctl_header;
1433 struct ctl_table devinet_vars[__NET_IPV4_CONF_MAX];
1435 } devinet_sysctl = {
1437 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
1438 devinet_sysctl_forward,
1439 devinet_conf_sysctl),
1440 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
1442 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
1443 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
1444 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
1445 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
1446 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
1447 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
1448 "accept_source_route"),
1449 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
1450 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
1451 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
1452 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
1453 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
1454 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
1455 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
1456 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
1457 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
1459 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
1460 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
1461 DEVINET_SYSCTL_FLUSHING_ENTRY(FORCE_IGMP_VERSION,
1462 "force_igmp_version"),
1463 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
1464 "promote_secondaries"),
1468 static void __devinet_sysctl_register(char *dev_name, int ctl_name,
1469 struct ipv4_devconf *p)
1472 struct devinet_sysctl_table *t;
1474 #define DEVINET_CTL_PATH_DEV 3
1476 struct ctl_path devinet_ctl_path[] = {
1477 { .procname = "net", .ctl_name = CTL_NET, },
1478 { .procname = "ipv4", .ctl_name = NET_IPV4, },
1479 { .procname = "conf", .ctl_name = NET_IPV4_CONF, },
1480 { /* to be set */ },
1484 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
1488 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1489 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
1490 t->devinet_vars[i].extra1 = p;
1494 * Make a copy of dev_name, because '.procname' is regarded as const
1495 * by sysctl and we wouldn't want anyone to change it under our feet
1496 * (see SIOCSIFNAME).
1498 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
1502 devinet_ctl_path[DEVINET_CTL_PATH_DEV].procname = t->dev_name;
1503 devinet_ctl_path[DEVINET_CTL_PATH_DEV].ctl_name = ctl_name;
1505 t->sysctl_header = register_sysctl_paths(devinet_ctl_path,
1507 if (!t->sysctl_header)
1521 static void devinet_sysctl_register(struct in_device *idev)
1523 return __devinet_sysctl_register(idev->dev->name, idev->dev->ifindex,
1527 static void devinet_sysctl_unregister(struct ipv4_devconf *p)
1530 struct devinet_sysctl_table *t = p->sysctl;
1532 unregister_sysctl_table(t->sysctl_header);
1539 void __init devinet_init(void)
1541 register_gifconf(PF_INET, inet_gifconf);
1542 register_netdevice_notifier(&ip_netdev_notifier);
1544 rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL);
1545 rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL);
1546 rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr);
1547 #ifdef CONFIG_SYSCTL
1548 __devinet_sysctl_register("all", NET_PROTO_CONF_ALL,
1550 __devinet_sysctl_register("default", NET_PROTO_CONF_DEFAULT,
1551 &ipv4_devconf_dflt);
1555 EXPORT_SYMBOL(in_dev_finish_destroy);
1556 EXPORT_SYMBOL(inet_select_addr);
1557 EXPORT_SYMBOL(inetdev_by_index);
1558 EXPORT_SYMBOL(register_inetaddr_notifier);
1559 EXPORT_SYMBOL(unregister_inetaddr_notifier);