2 * NET3 IP device support routines.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Derived from the IP parts of dev.c 1.0.19
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
15 * Alan Cox, <gw4pts@gw4pts.ampr.org>
16 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
19 * Alexey Kuznetsov: pa_* fields are replaced with ifaddr
21 * Cyrus Durgin: updated for kmod
22 * Matthias Andree: in devinet_ioctl, compare label and
23 * address (4.4BSD alias style support),
24 * fall back to comparing just the label
29 #include <asm/uaccess.h>
30 #include <asm/system.h>
31 #include <linux/bitops.h>
32 #include <linux/capability.h>
33 #include <linux/module.h>
34 #include <linux/types.h>
35 #include <linux/kernel.h>
36 #include <linux/string.h>
38 #include <linux/socket.h>
39 #include <linux/sockios.h>
41 #include <linux/errno.h>
42 #include <linux/interrupt.h>
43 #include <linux/if_addr.h>
44 #include <linux/if_ether.h>
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <linux/etherdevice.h>
48 #include <linux/skbuff.h>
49 #include <linux/init.h>
50 #include <linux/notifier.h>
51 #include <linux/inetdevice.h>
52 #include <linux/igmp.h>
54 #include <linux/sysctl.h>
56 #include <linux/kmod.h>
60 #include <net/route.h>
61 #include <net/ip_fib.h>
62 #include <net/rtnetlink.h>
63 #include <net/net_namespace.h>
65 static struct ipv4_devconf ipv4_devconf = {
67 [NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1,
68 [NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1,
69 [NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1,
70 [NET_IPV4_CONF_SHARED_MEDIA - 1] = 1,
74 static struct ipv4_devconf ipv4_devconf_dflt = {
76 [NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1,
77 [NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1,
78 [NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1,
79 [NET_IPV4_CONF_SHARED_MEDIA - 1] = 1,
80 [NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
84 #define IPV4_DEVCONF_DFLT(net, attr) \
85 IPV4_DEVCONF((*net->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_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
94 static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
96 static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
97 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
100 static void devinet_sysctl_register(struct in_device *idev);
101 static void devinet_sysctl_unregister(struct in_device *idev);
103 static inline void devinet_sysctl_register(struct in_device *idev)
106 static inline void devinet_sysctl_unregister(struct in_device *idev)
111 /* Locks all the inet devices. */
113 static struct in_ifaddr *inet_alloc_ifa(void)
115 return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
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 WARN_ON(idev->ifa_list);
136 WARN_ON(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 memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt,
159 sizeof(in_dev->cnf));
160 in_dev->cnf.sysctl = NULL;
162 if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL)
164 if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
165 dev_disable_lro(dev);
166 /* Reference in_dev->dev */
168 /* Account for reference dev->ip_ptr (below) */
171 devinet_sysctl_register(in_dev);
172 ip_mc_init_dev(in_dev);
173 if (dev->flags & IFF_UP)
176 /* we can receive as soon as ip_ptr is set -- do this last */
177 rcu_assign_pointer(dev->ip_ptr, in_dev);
186 static void in_dev_rcu_put(struct rcu_head *head)
188 struct in_device *idev = container_of(head, struct in_device, rcu_head);
192 static void inetdev_destroy(struct in_device *in_dev)
194 struct in_ifaddr *ifa;
195 struct net_device *dev;
203 ip_mc_destroy_dev(in_dev);
205 while ((ifa = in_dev->ifa_list) != NULL) {
206 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
212 devinet_sysctl_unregister(in_dev);
213 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
216 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
219 int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
222 for_primary_ifa(in_dev) {
223 if (inet_ifa_match(a, ifa)) {
224 if (!b || inet_ifa_match(b, ifa)) {
229 } endfor_ifa(in_dev);
234 static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
235 int destroy, struct nlmsghdr *nlh, u32 pid)
237 struct in_ifaddr *promote = NULL;
238 struct in_ifaddr *ifa, *ifa1 = *ifap;
239 struct in_ifaddr *last_prim = in_dev->ifa_list;
240 struct in_ifaddr *prev_prom = NULL;
241 int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
245 /* 1. Deleting primary ifaddr forces deletion all secondaries
246 * unless alias promotion is set
249 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
250 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
252 while ((ifa = *ifap1) != NULL) {
253 if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
254 ifa1->ifa_scope <= ifa->ifa_scope)
257 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
258 ifa1->ifa_mask != ifa->ifa_mask ||
259 !inet_ifa_match(ifa1->ifa_address, ifa)) {
260 ifap1 = &ifa->ifa_next;
266 *ifap1 = ifa->ifa_next;
268 rtmsg_ifa(RTM_DELADDR, ifa, nlh, pid);
269 blocking_notifier_call_chain(&inetaddr_chain,
281 *ifap = ifa1->ifa_next;
283 /* 3. Announce address deletion */
285 /* Send message first, then call notifier.
286 At first sight, FIB update triggered by notifier
287 will refer to already deleted ifaddr, that could confuse
288 netlink listeners. It is not true: look, gated sees
289 that route deleted and if it still thinks that ifaddr
290 is valid, it will try to restore deleted routes... Grr.
291 So that, this order is correct.
293 rtmsg_ifa(RTM_DELADDR, ifa1, nlh, pid);
294 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
299 prev_prom->ifa_next = promote->ifa_next;
300 promote->ifa_next = last_prim->ifa_next;
301 last_prim->ifa_next = promote;
304 promote->ifa_flags &= ~IFA_F_SECONDARY;
305 rtmsg_ifa(RTM_NEWADDR, promote, nlh, pid);
306 blocking_notifier_call_chain(&inetaddr_chain,
308 for (ifa = promote->ifa_next; ifa; ifa = ifa->ifa_next) {
309 if (ifa1->ifa_mask != ifa->ifa_mask ||
310 !inet_ifa_match(ifa1->ifa_address, ifa))
320 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
323 __inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
326 static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
329 struct in_device *in_dev = ifa->ifa_dev;
330 struct in_ifaddr *ifa1, **ifap, **last_primary;
334 if (!ifa->ifa_local) {
339 ifa->ifa_flags &= ~IFA_F_SECONDARY;
340 last_primary = &in_dev->ifa_list;
342 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
343 ifap = &ifa1->ifa_next) {
344 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
345 ifa->ifa_scope <= ifa1->ifa_scope)
346 last_primary = &ifa1->ifa_next;
347 if (ifa1->ifa_mask == ifa->ifa_mask &&
348 inet_ifa_match(ifa1->ifa_address, ifa)) {
349 if (ifa1->ifa_local == ifa->ifa_local) {
353 if (ifa1->ifa_scope != ifa->ifa_scope) {
357 ifa->ifa_flags |= IFA_F_SECONDARY;
361 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
362 net_srandom(ifa->ifa_local);
366 ifa->ifa_next = *ifap;
369 /* Send message first, then call notifier.
370 Notifier will trigger FIB update, so that
371 listeners of netlink will know about new ifaddr */
372 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, pid);
373 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
378 static int inet_insert_ifa(struct in_ifaddr *ifa)
380 return __inet_insert_ifa(ifa, NULL, 0);
383 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
385 struct in_device *in_dev = __in_dev_get_rtnl(dev);
393 ipv4_devconf_setall(in_dev);
394 if (ifa->ifa_dev != in_dev) {
395 WARN_ON(ifa->ifa_dev);
397 ifa->ifa_dev = in_dev;
399 if (ipv4_is_loopback(ifa->ifa_local))
400 ifa->ifa_scope = RT_SCOPE_HOST;
401 return inet_insert_ifa(ifa);
404 struct in_device *inetdev_by_index(struct net *net, int ifindex)
406 struct net_device *dev;
407 struct in_device *in_dev = NULL;
408 read_lock(&dev_base_lock);
409 dev = __dev_get_by_index(net, ifindex);
411 in_dev = in_dev_get(dev);
412 read_unlock(&dev_base_lock);
416 /* Called only from RTNL semaphored context. No locks. */
418 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
423 for_primary_ifa(in_dev) {
424 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
426 } endfor_ifa(in_dev);
430 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
432 struct net *net = sock_net(skb->sk);
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(net, 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_be32(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_be32(tb[IFA_ADDRESS]), ifa)))
468 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).pid);
472 err = -EADDRNOTAVAIL;
477 static struct in_ifaddr *rtm_to_ifaddr(struct net *net, 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);
492 if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL)
495 dev = __dev_get_by_index(net, ifm->ifa_index);
500 in_dev = __in_dev_get_rtnl(dev);
505 ifa = inet_alloc_ifa();
508 * A potential indev allocation can be left alive, it stays
509 * assigned to its device and is destroy with it.
513 ipv4_devconf_setall(in_dev);
516 if (tb[IFA_ADDRESS] == NULL)
517 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
519 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
520 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
521 ifa->ifa_flags = ifm->ifa_flags;
522 ifa->ifa_scope = ifm->ifa_scope;
523 ifa->ifa_dev = in_dev;
525 ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]);
526 ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]);
528 if (tb[IFA_BROADCAST])
529 ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]);
532 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
534 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
542 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
544 struct net *net = sock_net(skb->sk);
545 struct in_ifaddr *ifa;
549 ifa = rtm_to_ifaddr(net, nlh);
553 return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).pid);
557 * Determine a default network mask, based on the IP address.
560 static __inline__ int inet_abc_len(__be32 addr)
562 int rc = -1; /* Something else, probably a multicast. */
564 if (ipv4_is_zeronet(addr))
567 __u32 haddr = ntohl(addr);
569 if (IN_CLASSA(haddr))
571 else if (IN_CLASSB(haddr))
573 else if (IN_CLASSC(haddr))
581 int devinet_ioctl(struct net *net, unsigned int cmd, void __user *arg)
584 struct sockaddr_in sin_orig;
585 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
586 struct in_device *in_dev;
587 struct in_ifaddr **ifap = NULL;
588 struct in_ifaddr *ifa = NULL;
589 struct net_device *dev;
592 int tryaddrmatch = 0;
595 * Fetch the caller's info block into kernel space
598 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
600 ifr.ifr_name[IFNAMSIZ - 1] = 0;
602 /* save original address for comparison */
603 memcpy(&sin_orig, sin, sizeof(*sin));
605 colon = strchr(ifr.ifr_name, ':');
609 dev_load(net, ifr.ifr_name);
612 case SIOCGIFADDR: /* Get interface address */
613 case SIOCGIFBRDADDR: /* Get the broadcast address */
614 case SIOCGIFDSTADDR: /* Get the destination address */
615 case SIOCGIFNETMASK: /* Get the netmask for the interface */
616 /* Note that these ioctls will not sleep,
617 so that we do not impose a lock.
618 One day we will be forced to put shlock here (I mean SMP)
620 tryaddrmatch = (sin_orig.sin_family == AF_INET);
621 memset(sin, 0, sizeof(*sin));
622 sin->sin_family = AF_INET;
627 if (!capable(CAP_NET_ADMIN))
630 case SIOCSIFADDR: /* Set interface address (and family) */
631 case SIOCSIFBRDADDR: /* Set the broadcast address */
632 case SIOCSIFDSTADDR: /* Set the destination address */
633 case SIOCSIFNETMASK: /* Set the netmask for the interface */
635 if (!capable(CAP_NET_ADMIN))
638 if (sin->sin_family != AF_INET)
649 if ((dev = __dev_get_by_name(net, ifr.ifr_name)) == NULL)
655 if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) {
657 /* Matthias Andree */
658 /* compare label and address (4.4BSD style) */
659 /* note: we only do this for a limited set of ioctls
660 and only if the original address family was AF_INET.
661 This is checked above. */
662 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
663 ifap = &ifa->ifa_next) {
664 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
665 sin_orig.sin_addr.s_addr ==
671 /* we didn't get a match, maybe the application is
672 4.3BSD-style and passed in junk so we fall back to
673 comparing just the label */
675 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
676 ifap = &ifa->ifa_next)
677 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
682 ret = -EADDRNOTAVAIL;
683 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
687 case SIOCGIFADDR: /* Get interface address */
688 sin->sin_addr.s_addr = ifa->ifa_local;
691 case SIOCGIFBRDADDR: /* Get the broadcast address */
692 sin->sin_addr.s_addr = ifa->ifa_broadcast;
695 case SIOCGIFDSTADDR: /* Get the destination address */
696 sin->sin_addr.s_addr = ifa->ifa_address;
699 case SIOCGIFNETMASK: /* Get the netmask for the interface */
700 sin->sin_addr.s_addr = ifa->ifa_mask;
705 ret = -EADDRNOTAVAIL;
709 if (!(ifr.ifr_flags & IFF_UP))
710 inet_del_ifa(in_dev, ifap, 1);
713 ret = dev_change_flags(dev, ifr.ifr_flags);
716 case SIOCSIFADDR: /* Set interface address (and family) */
718 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
723 if ((ifa = inet_alloc_ifa()) == NULL)
726 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
728 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
731 if (ifa->ifa_local == sin->sin_addr.s_addr)
733 inet_del_ifa(in_dev, ifap, 0);
734 ifa->ifa_broadcast = 0;
738 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
740 if (!(dev->flags & IFF_POINTOPOINT)) {
741 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
742 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
743 if ((dev->flags & IFF_BROADCAST) &&
744 ifa->ifa_prefixlen < 31)
745 ifa->ifa_broadcast = ifa->ifa_address |
748 ifa->ifa_prefixlen = 32;
749 ifa->ifa_mask = inet_make_mask(32);
751 ret = inet_set_ifa(dev, ifa);
754 case SIOCSIFBRDADDR: /* Set the broadcast address */
756 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
757 inet_del_ifa(in_dev, ifap, 0);
758 ifa->ifa_broadcast = sin->sin_addr.s_addr;
759 inet_insert_ifa(ifa);
763 case SIOCSIFDSTADDR: /* Set the destination address */
765 if (ifa->ifa_address == sin->sin_addr.s_addr)
768 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
771 inet_del_ifa(in_dev, ifap, 0);
772 ifa->ifa_address = sin->sin_addr.s_addr;
773 inet_insert_ifa(ifa);
776 case SIOCSIFNETMASK: /* Set the netmask for the interface */
779 * The mask we set must be legal.
782 if (bad_mask(sin->sin_addr.s_addr, 0))
785 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
786 __be32 old_mask = ifa->ifa_mask;
787 inet_del_ifa(in_dev, ifap, 0);
788 ifa->ifa_mask = sin->sin_addr.s_addr;
789 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
791 /* See if current broadcast address matches
792 * with current netmask, then recalculate
793 * the broadcast address. Otherwise it's a
794 * funny address, so don't touch it since
795 * the user seems to know what (s)he's doing...
797 if ((dev->flags & IFF_BROADCAST) &&
798 (ifa->ifa_prefixlen < 31) &&
799 (ifa->ifa_broadcast ==
800 (ifa->ifa_local|~old_mask))) {
801 ifa->ifa_broadcast = (ifa->ifa_local |
802 ~sin->sin_addr.s_addr);
804 inet_insert_ifa(ifa);
814 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
818 static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
820 struct in_device *in_dev = __in_dev_get_rtnl(dev);
821 struct in_ifaddr *ifa;
825 if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
828 for (; ifa; ifa = ifa->ifa_next) {
833 if (len < (int) sizeof(ifr))
835 memset(&ifr, 0, sizeof(struct ifreq));
837 strcpy(ifr.ifr_name, ifa->ifa_label);
839 strcpy(ifr.ifr_name, dev->name);
841 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
842 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
845 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
849 buf += sizeof(struct ifreq);
850 len -= sizeof(struct ifreq);
851 done += sizeof(struct ifreq);
857 __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
860 struct in_device *in_dev;
861 struct net *net = dev_net(dev);
864 in_dev = __in_dev_get_rcu(dev);
868 for_primary_ifa(in_dev) {
869 if (ifa->ifa_scope > scope)
871 if (!dst || inet_ifa_match(dst, ifa)) {
872 addr = ifa->ifa_local;
876 addr = ifa->ifa_local;
877 } endfor_ifa(in_dev);
884 /* Not loopback addresses on loopback should be preferred
885 in this case. It is importnat that lo is the first interface
888 read_lock(&dev_base_lock);
890 for_each_netdev(net, dev) {
891 if ((in_dev = __in_dev_get_rcu(dev)) == NULL)
894 for_primary_ifa(in_dev) {
895 if (ifa->ifa_scope != RT_SCOPE_LINK &&
896 ifa->ifa_scope <= scope) {
897 addr = ifa->ifa_local;
898 goto out_unlock_both;
900 } endfor_ifa(in_dev);
903 read_unlock(&dev_base_lock);
909 static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
910 __be32 local, int scope)
917 (local == ifa->ifa_local || !local) &&
918 ifa->ifa_scope <= scope) {
919 addr = ifa->ifa_local;
924 same = (!local || inet_ifa_match(local, ifa)) &&
925 (!dst || inet_ifa_match(dst, ifa));
929 /* Is the selected addr into dst subnet? */
930 if (inet_ifa_match(addr, ifa))
932 /* No, then can we use new local src? */
933 if (ifa->ifa_scope <= scope) {
934 addr = ifa->ifa_local;
937 /* search for large dst subnet for addr */
941 } endfor_ifa(in_dev);
943 return same? addr : 0;
947 * Confirm that local IP address exists using wildcards:
948 * - in_dev: only on this interface, 0=any interface
949 * - dst: only in the same subnet as dst, 0=any dst
950 * - local: address, 0=autoselect the local address
951 * - scope: maximum allowed scope value for the local address
953 __be32 inet_confirm_addr(struct in_device *in_dev,
954 __be32 dst, __be32 local, int scope)
957 struct net_device *dev;
960 if (scope != RT_SCOPE_LINK)
961 return confirm_addr_indev(in_dev, dst, local, scope);
963 net = dev_net(in_dev->dev);
964 read_lock(&dev_base_lock);
966 for_each_netdev(net, dev) {
967 if ((in_dev = __in_dev_get_rcu(dev))) {
968 addr = confirm_addr_indev(in_dev, dst, local, scope);
974 read_unlock(&dev_base_lock);
983 int register_inetaddr_notifier(struct notifier_block *nb)
985 return blocking_notifier_chain_register(&inetaddr_chain, nb);
988 int unregister_inetaddr_notifier(struct notifier_block *nb)
990 return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
993 /* Rename ifa_labels for a device name change. Make some effort to preserve existing
994 * alias numbering and to create unique labels if possible.
996 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
998 struct in_ifaddr *ifa;
1001 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1002 char old[IFNAMSIZ], *dot;
1004 memcpy(old, ifa->ifa_label, IFNAMSIZ);
1005 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1008 dot = strchr(old, ':');
1010 sprintf(old, ":%d", named);
1013 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) {
1014 strcat(ifa->ifa_label, dot);
1016 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
1019 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
1023 static inline bool inetdev_valid_mtu(unsigned mtu)
1028 /* Called only under RTNL semaphore */
1030 static int inetdev_event(struct notifier_block *this, unsigned long event,
1033 struct net_device *dev = ptr;
1034 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1039 if (event == NETDEV_REGISTER) {
1040 in_dev = inetdev_init(dev);
1042 return notifier_from_errno(-ENOMEM);
1043 if (dev->flags & IFF_LOOPBACK) {
1044 IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
1045 IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
1047 } else if (event == NETDEV_CHANGEMTU) {
1048 /* Re-enabling IP */
1049 if (inetdev_valid_mtu(dev->mtu))
1050 in_dev = inetdev_init(dev);
1056 case NETDEV_REGISTER:
1057 printk(KERN_DEBUG "inetdev_event: bug\n");
1061 if (!inetdev_valid_mtu(dev->mtu))
1063 if (dev->flags & IFF_LOOPBACK) {
1064 struct in_ifaddr *ifa;
1065 if ((ifa = inet_alloc_ifa()) != NULL) {
1067 ifa->ifa_address = htonl(INADDR_LOOPBACK);
1068 ifa->ifa_prefixlen = 8;
1069 ifa->ifa_mask = inet_make_mask(8);
1070 in_dev_hold(in_dev);
1071 ifa->ifa_dev = in_dev;
1072 ifa->ifa_scope = RT_SCOPE_HOST;
1073 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1074 inet_insert_ifa(ifa);
1082 case NETDEV_CHANGEMTU:
1083 if (inetdev_valid_mtu(dev->mtu))
1085 /* disable IP when MTU is not enough */
1086 case NETDEV_UNREGISTER:
1087 inetdev_destroy(in_dev);
1089 case NETDEV_CHANGENAME:
1090 /* Do not notify about label change, this event is
1091 * not interesting to applications using netlink.
1093 inetdev_changename(dev, in_dev);
1095 devinet_sysctl_unregister(in_dev);
1096 devinet_sysctl_register(in_dev);
1103 static struct notifier_block ip_netdev_notifier = {
1104 .notifier_call = inetdev_event,
1107 static inline size_t inet_nlmsg_size(void)
1109 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1110 + nla_total_size(4) /* IFA_ADDRESS */
1111 + nla_total_size(4) /* IFA_LOCAL */
1112 + nla_total_size(4) /* IFA_BROADCAST */
1113 + nla_total_size(IFNAMSIZ); /* IFA_LABEL */
1116 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1117 u32 pid, u32 seq, int event, unsigned int flags)
1119 struct ifaddrmsg *ifm;
1120 struct nlmsghdr *nlh;
1122 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
1126 ifm = nlmsg_data(nlh);
1127 ifm->ifa_family = AF_INET;
1128 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1129 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1130 ifm->ifa_scope = ifa->ifa_scope;
1131 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1133 if (ifa->ifa_address)
1134 NLA_PUT_BE32(skb, IFA_ADDRESS, ifa->ifa_address);
1137 NLA_PUT_BE32(skb, IFA_LOCAL, ifa->ifa_local);
1139 if (ifa->ifa_broadcast)
1140 NLA_PUT_BE32(skb, IFA_BROADCAST, ifa->ifa_broadcast);
1142 if (ifa->ifa_label[0])
1143 NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label);
1145 return nlmsg_end(skb, nlh);
1148 nlmsg_cancel(skb, nlh);
1152 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1154 struct net *net = sock_net(skb->sk);
1156 struct net_device *dev;
1157 struct in_device *in_dev;
1158 struct in_ifaddr *ifa;
1159 int s_ip_idx, s_idx = cb->args[0];
1161 s_ip_idx = ip_idx = cb->args[1];
1163 for_each_netdev(net, dev) {
1168 if ((in_dev = __in_dev_get_rtnl(dev)) == NULL)
1171 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1172 ifa = ifa->ifa_next, ip_idx++) {
1173 if (ip_idx < s_ip_idx)
1175 if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1177 RTM_NEWADDR, NLM_F_MULTI) <= 0)
1186 cb->args[1] = ip_idx;
1191 static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
1194 struct sk_buff *skb;
1195 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1199 net = dev_net(ifa->ifa_dev->dev);
1200 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
1204 err = inet_fill_ifaddr(skb, ifa, pid, seq, event, 0);
1206 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1207 WARN_ON(err == -EMSGSIZE);
1211 err = rtnl_notify(skb, net, pid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
1214 rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
1217 #ifdef CONFIG_SYSCTL
1219 static void devinet_copy_dflt_conf(struct net *net, int i)
1221 struct net_device *dev;
1223 read_lock(&dev_base_lock);
1224 for_each_netdev(net, dev) {
1225 struct in_device *in_dev;
1227 in_dev = __in_dev_get_rcu(dev);
1228 if (in_dev && !test_bit(i, in_dev->cnf.state))
1229 in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
1232 read_unlock(&dev_base_lock);
1235 static void inet_forward_change(struct net *net)
1237 struct net_device *dev;
1238 int on = IPV4_DEVCONF_ALL(net, FORWARDING);
1240 IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
1241 IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
1243 read_lock(&dev_base_lock);
1244 for_each_netdev(net, dev) {
1245 struct in_device *in_dev;
1247 dev_disable_lro(dev);
1249 in_dev = __in_dev_get_rcu(dev);
1251 IN_DEV_CONF_SET(in_dev, FORWARDING, on);
1254 read_unlock(&dev_base_lock);
1257 static int devinet_conf_proc(ctl_table *ctl, int write,
1258 struct file *filp, void __user *buffer,
1259 size_t *lenp, loff_t *ppos)
1261 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1264 struct ipv4_devconf *cnf = ctl->extra1;
1265 struct net *net = ctl->extra2;
1266 int i = (int *)ctl->data - cnf->data;
1268 set_bit(i, cnf->state);
1270 if (cnf == net->ipv4.devconf_dflt)
1271 devinet_copy_dflt_conf(net, i);
1277 static int devinet_conf_sysctl(ctl_table *table,
1278 void __user *oldval, size_t __user *oldlenp,
1279 void __user *newval, size_t newlen)
1281 struct ipv4_devconf *cnf;
1283 int *valp = table->data;
1287 if (!newval || !newlen)
1290 if (newlen != sizeof(int))
1293 if (get_user(new, (int __user *)newval))
1299 if (oldval && oldlenp) {
1302 if (get_user(len, oldlenp))
1306 if (len > table->maxlen)
1307 len = table->maxlen;
1308 if (copy_to_user(oldval, valp, len))
1310 if (put_user(len, oldlenp))
1317 cnf = table->extra1;
1318 net = table->extra2;
1319 i = (int *)table->data - cnf->data;
1321 set_bit(i, cnf->state);
1323 if (cnf == net->ipv4.devconf_dflt)
1324 devinet_copy_dflt_conf(net, i);
1329 static int devinet_sysctl_forward(ctl_table *ctl, int write,
1330 struct file *filp, void __user *buffer,
1331 size_t *lenp, loff_t *ppos)
1333 int *valp = ctl->data;
1335 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1337 if (write && *valp != val) {
1338 struct net *net = ctl->extra2;
1340 if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
1342 if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
1343 inet_forward_change(net);
1345 struct ipv4_devconf *cnf = ctl->extra1;
1346 struct in_device *idev =
1347 container_of(cnf, struct in_device, cnf);
1348 dev_disable_lro(idev->dev);
1351 rt_cache_flush(net, 0);
1358 int ipv4_doint_and_flush(ctl_table *ctl, int write,
1359 struct file *filp, void __user *buffer,
1360 size_t *lenp, loff_t *ppos)
1362 int *valp = ctl->data;
1364 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1365 struct net *net = ctl->extra2;
1367 if (write && *valp != val)
1368 rt_cache_flush(net, 0);
1373 int ipv4_doint_and_flush_strategy(ctl_table *table,
1374 void __user *oldval, size_t __user *oldlenp,
1375 void __user *newval, size_t newlen)
1377 int ret = devinet_conf_sysctl(table, oldval, oldlenp, newval, newlen);
1378 struct net *net = table->extra2;
1381 rt_cache_flush(net, 0);
1387 #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc, sysctl) \
1389 .ctl_name = NET_IPV4_CONF_ ## attr, \
1391 .data = ipv4_devconf.data + \
1392 NET_IPV4_CONF_ ## attr - 1, \
1393 .maxlen = sizeof(int), \
1395 .proc_handler = proc, \
1396 .strategy = sysctl, \
1397 .extra1 = &ipv4_devconf, \
1400 #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
1401 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc, \
1402 devinet_conf_sysctl)
1404 #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
1405 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc, \
1406 devinet_conf_sysctl)
1408 #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc, sysctl) \
1409 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc, sysctl)
1411 #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
1412 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush, \
1413 ipv4_doint_and_flush_strategy)
1415 static struct devinet_sysctl_table {
1416 struct ctl_table_header *sysctl_header;
1417 struct ctl_table devinet_vars[__NET_IPV4_CONF_MAX];
1419 } devinet_sysctl = {
1421 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
1422 devinet_sysctl_forward,
1423 devinet_conf_sysctl),
1424 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
1426 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
1427 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
1428 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
1429 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
1430 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
1431 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
1432 "accept_source_route"),
1433 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
1434 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
1435 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
1436 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
1437 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
1438 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
1439 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
1440 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
1441 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
1443 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
1444 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
1445 DEVINET_SYSCTL_FLUSHING_ENTRY(FORCE_IGMP_VERSION,
1446 "force_igmp_version"),
1447 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
1448 "promote_secondaries"),
1452 static int __devinet_sysctl_register(struct net *net, char *dev_name,
1453 int ctl_name, struct ipv4_devconf *p)
1456 struct devinet_sysctl_table *t;
1458 #define DEVINET_CTL_PATH_DEV 3
1460 struct ctl_path devinet_ctl_path[] = {
1461 { .procname = "net", .ctl_name = CTL_NET, },
1462 { .procname = "ipv4", .ctl_name = NET_IPV4, },
1463 { .procname = "conf", .ctl_name = NET_IPV4_CONF, },
1464 { /* to be set */ },
1468 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
1472 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1473 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
1474 t->devinet_vars[i].extra1 = p;
1475 t->devinet_vars[i].extra2 = net;
1479 * Make a copy of dev_name, because '.procname' is regarded as const
1480 * by sysctl and we wouldn't want anyone to change it under our feet
1481 * (see SIOCSIFNAME).
1483 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
1487 devinet_ctl_path[DEVINET_CTL_PATH_DEV].procname = t->dev_name;
1488 devinet_ctl_path[DEVINET_CTL_PATH_DEV].ctl_name = ctl_name;
1490 t->sysctl_header = register_net_sysctl_table(net, devinet_ctl_path,
1492 if (!t->sysctl_header)
1506 static void __devinet_sysctl_unregister(struct ipv4_devconf *cnf)
1508 struct devinet_sysctl_table *t = cnf->sysctl;
1514 unregister_sysctl_table(t->sysctl_header);
1519 static void devinet_sysctl_register(struct in_device *idev)
1521 neigh_sysctl_register(idev->dev, idev->arp_parms, NET_IPV4,
1522 NET_IPV4_NEIGH, "ipv4", NULL, NULL);
1523 __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
1524 idev->dev->ifindex, &idev->cnf);
1527 static void devinet_sysctl_unregister(struct in_device *idev)
1529 __devinet_sysctl_unregister(&idev->cnf);
1530 neigh_sysctl_unregister(idev->arp_parms);
1533 static struct ctl_table ctl_forward_entry[] = {
1535 .ctl_name = NET_IPV4_FORWARD,
1536 .procname = "ip_forward",
1537 .data = &ipv4_devconf.data[
1538 NET_IPV4_CONF_FORWARDING - 1],
1539 .maxlen = sizeof(int),
1541 .proc_handler = devinet_sysctl_forward,
1542 .strategy = devinet_conf_sysctl,
1543 .extra1 = &ipv4_devconf,
1544 .extra2 = &init_net,
1549 static __net_initdata struct ctl_path net_ipv4_path[] = {
1550 { .procname = "net", .ctl_name = CTL_NET, },
1551 { .procname = "ipv4", .ctl_name = NET_IPV4, },
1556 static __net_init int devinet_init_net(struct net *net)
1559 struct ipv4_devconf *all, *dflt;
1560 #ifdef CONFIG_SYSCTL
1561 struct ctl_table *tbl = ctl_forward_entry;
1562 struct ctl_table_header *forw_hdr;
1566 all = &ipv4_devconf;
1567 dflt = &ipv4_devconf_dflt;
1569 if (net != &init_net) {
1570 all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL);
1574 dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
1576 goto err_alloc_dflt;
1578 #ifdef CONFIG_SYSCTL
1579 tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL);
1583 tbl[0].data = &all->data[NET_IPV4_CONF_FORWARDING - 1];
1584 tbl[0].extra1 = all;
1585 tbl[0].extra2 = net;
1589 #ifdef CONFIG_SYSCTL
1590 err = __devinet_sysctl_register(net, "all",
1591 NET_PROTO_CONF_ALL, all);
1595 err = __devinet_sysctl_register(net, "default",
1596 NET_PROTO_CONF_DEFAULT, dflt);
1601 forw_hdr = register_net_sysctl_table(net, net_ipv4_path, tbl);
1602 if (forw_hdr == NULL)
1604 net->ipv4.forw_hdr = forw_hdr;
1607 net->ipv4.devconf_all = all;
1608 net->ipv4.devconf_dflt = dflt;
1611 #ifdef CONFIG_SYSCTL
1613 __devinet_sysctl_unregister(dflt);
1615 __devinet_sysctl_unregister(all);
1617 if (tbl != ctl_forward_entry)
1621 if (dflt != &ipv4_devconf_dflt)
1624 if (all != &ipv4_devconf)
1630 static __net_exit void devinet_exit_net(struct net *net)
1632 #ifdef CONFIG_SYSCTL
1633 struct ctl_table *tbl;
1635 tbl = net->ipv4.forw_hdr->ctl_table_arg;
1636 unregister_net_sysctl_table(net->ipv4.forw_hdr);
1637 __devinet_sysctl_unregister(net->ipv4.devconf_dflt);
1638 __devinet_sysctl_unregister(net->ipv4.devconf_all);
1641 kfree(net->ipv4.devconf_dflt);
1642 kfree(net->ipv4.devconf_all);
1645 static __net_initdata struct pernet_operations devinet_ops = {
1646 .init = devinet_init_net,
1647 .exit = devinet_exit_net,
1650 void __init devinet_init(void)
1652 register_pernet_subsys(&devinet_ops);
1654 register_gifconf(PF_INET, inet_gifconf);
1655 register_netdevice_notifier(&ip_netdev_notifier);
1657 rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL);
1658 rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL);
1659 rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr);
1662 EXPORT_SYMBOL(in_dev_finish_destroy);
1663 EXPORT_SYMBOL(inet_select_addr);
1664 EXPORT_SYMBOL(inetdev_by_index);
1665 EXPORT_SYMBOL(register_inetaddr_notifier);
1666 EXPORT_SYMBOL(unregister_inetaddr_notifier);