2 * NET3 IP device support routines.
4 * Version: $Id: devinet.c,v 1.44 2001/10/31 21:55:54 davem Exp $
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * Derived from the IP parts of dev.c 1.0.19
12 * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
13 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
14 * Mark Evans, <evansmp@uhura.aston.ac.uk>
17 * Alan Cox, <gw4pts@gw4pts.ampr.org>
18 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
21 * Alexey Kuznetsov: pa_* fields are replaced with ifaddr
23 * Cyrus Durgin: updated for kmod
24 * Matthias Andree: in devinet_ioctl, compare label and
25 * address (4.4BSD alias style support),
26 * fall back to comparing just the label
30 #include <linux/config.h>
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include <asm/bitops.h>
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/string.h>
41 #include <linux/socket.h>
42 #include <linux/sockios.h>
44 #include <linux/errno.h>
45 #include <linux/interrupt.h>
46 #include <linux/if_ether.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/skbuff.h>
51 #include <linux/rtnetlink.h>
52 #include <linux/init.h>
53 #include <linux/notifier.h>
54 #include <linux/inetdevice.h>
55 #include <linux/igmp.h>
57 #include <linux/sysctl.h>
59 #include <linux/kmod.h>
62 #include <net/route.h>
63 #include <net/ip_fib.h>
65 struct ipv4_devconf ipv4_devconf = {
66 .accept_redirects = 1,
68 .secure_redirects = 1,
72 static struct ipv4_devconf ipv4_devconf_dflt = {
73 .accept_redirects = 1,
75 .secure_redirects = 1,
77 .accept_source_route = 1,
80 static void rtmsg_ifa(int event, struct in_ifaddr *);
82 static struct notifier_block *inetaddr_chain;
83 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
86 static void devinet_sysctl_register(struct in_device *in_dev,
87 struct ipv4_devconf *p);
88 static void devinet_sysctl_unregister(struct ipv4_devconf *p);
91 /* Locks all the inet devices. */
93 static struct in_ifaddr *inet_alloc_ifa(void)
95 struct in_ifaddr *ifa = kmalloc(sizeof(*ifa), GFP_KERNEL);
98 memset(ifa, 0, sizeof(*ifa));
99 INIT_RCU_HEAD(&ifa->rcu_head);
105 static void inet_rcu_free_ifa(struct rcu_head *head)
107 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
109 in_dev_put(ifa->ifa_dev);
113 static inline void inet_free_ifa(struct in_ifaddr *ifa)
115 call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
118 void in_dev_finish_destroy(struct in_device *idev)
120 struct net_device *dev = idev->dev;
122 BUG_TRAP(!idev->ifa_list);
123 BUG_TRAP(!idev->mc_list);
124 #ifdef NET_REFCNT_DEBUG
125 printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n",
126 idev, dev ? dev->name : "NIL");
130 printk("Freeing alive in_device %p\n", idev);
136 struct in_device *inetdev_init(struct net_device *dev)
138 struct in_device *in_dev;
142 in_dev = kmalloc(sizeof(*in_dev), GFP_KERNEL);
145 memset(in_dev, 0, sizeof(*in_dev));
146 INIT_RCU_HEAD(&in_dev->rcu_head);
147 memcpy(&in_dev->cnf, &ipv4_devconf_dflt, sizeof(in_dev->cnf));
148 in_dev->cnf.sysctl = NULL;
150 if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL)
152 /* Reference in_dev->dev */
155 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
156 NET_IPV4_NEIGH, "ipv4", NULL);
159 /* Account for reference dev->ip_ptr */
162 dev->ip_ptr = in_dev;
165 devinet_sysctl_register(in_dev, &in_dev->cnf);
167 ip_mc_init_dev(in_dev);
168 if (dev->flags & IFF_UP)
178 static void in_dev_rcu_put(struct rcu_head *head)
180 struct in_device *idev = container_of(head, struct in_device, rcu_head);
184 static void inetdev_destroy(struct in_device *in_dev)
186 struct in_ifaddr *ifa;
187 struct net_device *dev;
193 ip_mc_destroy_dev(in_dev);
195 while ((ifa = in_dev->ifa_list) != NULL) {
196 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
201 devinet_sysctl_unregister(&in_dev->cnf);
208 neigh_sysctl_unregister(in_dev->arp_parms);
210 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
213 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
216 int inet_addr_onlink(struct in_device *in_dev, u32 a, u32 b)
219 for_primary_ifa(in_dev) {
220 if (inet_ifa_match(a, ifa)) {
221 if (!b || inet_ifa_match(b, ifa)) {
226 } endfor_ifa(in_dev);
231 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
234 struct in_ifaddr *ifa1 = *ifap;
238 /* 1. Deleting primary ifaddr forces deletion all secondaries */
240 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
241 struct in_ifaddr *ifa;
242 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
244 while ((ifa = *ifap1) != NULL) {
245 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
246 ifa1->ifa_mask != ifa->ifa_mask ||
247 !inet_ifa_match(ifa1->ifa_address, ifa)) {
248 ifap1 = &ifa->ifa_next;
252 *ifap1 = ifa->ifa_next;
254 rtmsg_ifa(RTM_DELADDR, ifa);
255 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa);
262 *ifap = ifa1->ifa_next;
264 /* 3. Announce address deletion */
266 /* Send message first, then call notifier.
267 At first sight, FIB update triggered by notifier
268 will refer to already deleted ifaddr, that could confuse
269 netlink listeners. It is not true: look, gated sees
270 that route deleted and if it still thinks that ifaddr
271 is valid, it will try to restore deleted routes... Grr.
272 So that, this order is correct.
274 rtmsg_ifa(RTM_DELADDR, ifa1);
275 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
279 if (!in_dev->ifa_list)
280 inetdev_destroy(in_dev);
284 static int inet_insert_ifa(struct in_ifaddr *ifa)
286 struct in_device *in_dev = ifa->ifa_dev;
287 struct in_ifaddr *ifa1, **ifap, **last_primary;
291 if (!ifa->ifa_local) {
296 ifa->ifa_flags &= ~IFA_F_SECONDARY;
297 last_primary = &in_dev->ifa_list;
299 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
300 ifap = &ifa1->ifa_next) {
301 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
302 ifa->ifa_scope <= ifa1->ifa_scope)
303 last_primary = &ifa1->ifa_next;
304 if (ifa1->ifa_mask == ifa->ifa_mask &&
305 inet_ifa_match(ifa1->ifa_address, ifa)) {
306 if (ifa1->ifa_local == ifa->ifa_local) {
310 if (ifa1->ifa_scope != ifa->ifa_scope) {
314 ifa->ifa_flags |= IFA_F_SECONDARY;
318 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
319 net_srandom(ifa->ifa_local);
323 ifa->ifa_next = *ifap;
326 /* Send message first, then call notifier.
327 Notifier will trigger FIB update, so that
328 listeners of netlink will know about new ifaddr */
329 rtmsg_ifa(RTM_NEWADDR, ifa);
330 notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
335 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
337 struct in_device *in_dev = __in_dev_get(dev);
342 in_dev = inetdev_init(dev);
348 if (ifa->ifa_dev != in_dev) {
349 BUG_TRAP(!ifa->ifa_dev);
351 ifa->ifa_dev = in_dev;
353 if (LOOPBACK(ifa->ifa_local))
354 ifa->ifa_scope = RT_SCOPE_HOST;
355 return inet_insert_ifa(ifa);
358 struct in_device *inetdev_by_index(int ifindex)
360 struct net_device *dev;
361 struct in_device *in_dev = NULL;
362 read_lock(&dev_base_lock);
363 dev = __dev_get_by_index(ifindex);
365 in_dev = in_dev_get(dev);
366 read_unlock(&dev_base_lock);
370 /* Called only from RTNL semaphored context. No locks. */
372 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, u32 prefix,
377 for_primary_ifa(in_dev) {
378 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
380 } endfor_ifa(in_dev);
384 int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
386 struct rtattr **rta = arg;
387 struct in_device *in_dev;
388 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
389 struct in_ifaddr *ifa, **ifap;
393 if ((in_dev = inetdev_by_index(ifm->ifa_index)) == NULL)
395 __in_dev_put(in_dev);
397 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
398 ifap = &ifa->ifa_next) {
399 if ((rta[IFA_LOCAL - 1] &&
400 memcmp(RTA_DATA(rta[IFA_LOCAL - 1]),
401 &ifa->ifa_local, 4)) ||
402 (rta[IFA_LABEL - 1] &&
403 strcmp(RTA_DATA(rta[IFA_LABEL - 1]), ifa->ifa_label)) ||
404 (rta[IFA_ADDRESS - 1] &&
405 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
406 !inet_ifa_match(*(u32*)RTA_DATA(rta[IFA_ADDRESS - 1]),
409 inet_del_ifa(in_dev, ifap, 1);
413 return -EADDRNOTAVAIL;
416 int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
418 struct rtattr **rta = arg;
419 struct net_device *dev;
420 struct in_device *in_dev;
421 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
422 struct in_ifaddr *ifa;
427 if (ifm->ifa_prefixlen > 32 || !rta[IFA_LOCAL - 1])
431 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
435 if ((in_dev = __in_dev_get(dev)) == NULL) {
436 in_dev = inetdev_init(dev);
441 if ((ifa = inet_alloc_ifa()) == NULL)
444 if (!rta[IFA_ADDRESS - 1])
445 rta[IFA_ADDRESS - 1] = rta[IFA_LOCAL - 1];
446 memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL - 1]), 4);
447 memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS - 1]), 4);
448 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
449 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
450 if (rta[IFA_BROADCAST - 1])
451 memcpy(&ifa->ifa_broadcast,
452 RTA_DATA(rta[IFA_BROADCAST - 1]), 4);
453 if (rta[IFA_ANYCAST - 1])
454 memcpy(&ifa->ifa_anycast, RTA_DATA(rta[IFA_ANYCAST - 1]), 4);
455 ifa->ifa_flags = ifm->ifa_flags;
456 ifa->ifa_scope = ifm->ifa_scope;
458 ifa->ifa_dev = in_dev;
459 if (rta[IFA_LABEL - 1])
460 memcpy(ifa->ifa_label, RTA_DATA(rta[IFA_LABEL - 1]), IFNAMSIZ);
462 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
464 rc = inet_insert_ifa(ifa);
470 * Determine a default network mask, based on the IP address.
473 static __inline__ int inet_abc_len(u32 addr)
475 int rc = -1; /* Something else, probably a multicast. */
484 else if (IN_CLASSB(addr))
486 else if (IN_CLASSC(addr))
494 Check that a device is not member of the ipv4root assigned to the process
495 Return true if this is the case
497 If the process is not bound to specific IP, then it returns 0 (all
500 static inline int devinet_notiproot (struct in_ifaddr *ifa)
505 if ((nxi = current->nx_info)) {
507 int nbip = nxi->nbipv4;
508 __u32 addr = ifa->ifa_local;
510 for (i=0; i<nbip; i++) {
511 if(nxi->ipv4[i] == addr) {
521 int devinet_ioctl(unsigned int cmd, void __user *arg)
524 struct sockaddr_in sin_orig;
525 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
526 struct in_device *in_dev;
527 struct in_ifaddr **ifap = NULL;
528 struct in_ifaddr *ifa = NULL;
529 struct net_device *dev;
532 int tryaddrmatch = 0;
535 * Fetch the caller's info block into kernel space
538 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
540 ifr.ifr_name[IFNAMSIZ - 1] = 0;
542 /* save original address for comparison */
543 memcpy(&sin_orig, sin, sizeof(*sin));
545 colon = strchr(ifr.ifr_name, ':');
550 dev_load(ifr.ifr_name);
554 case SIOCGIFADDR: /* Get interface address */
555 case SIOCGIFBRDADDR: /* Get the broadcast address */
556 case SIOCGIFDSTADDR: /* Get the destination address */
557 case SIOCGIFNETMASK: /* Get the netmask for the interface */
558 /* Note that these ioctls will not sleep,
559 so that we do not impose a lock.
560 One day we will be forced to put shlock here (I mean SMP)
562 tryaddrmatch = (sin_orig.sin_family == AF_INET);
563 memset(sin, 0, sizeof(*sin));
564 sin->sin_family = AF_INET;
569 if (!capable(CAP_NET_ADMIN))
572 case SIOCSIFADDR: /* Set interface address (and family) */
573 case SIOCSIFBRDADDR: /* Set the broadcast address */
574 case SIOCSIFDSTADDR: /* Set the destination address */
575 case SIOCSIFNETMASK: /* Set the netmask for the interface */
577 if (!capable(CAP_NET_ADMIN))
580 if (sin->sin_family != AF_INET)
591 if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL)
597 if ((in_dev = __in_dev_get(dev)) != NULL) {
599 /* Matthias Andree */
600 /* compare label and address (4.4BSD style) */
601 /* note: we only do this for a limited set of ioctls
602 and only if the original address family was AF_INET.
603 This is checked above. */
604 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
605 ifap = &ifa->ifa_next) {
606 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
607 sin_orig.sin_addr.s_addr ==
613 /* we didn't get a match, maybe the application is
614 4.3BSD-style and passed in junk so we fall back to
615 comparing just the label */
617 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
618 ifap = &ifa->ifa_next)
619 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
624 ret = -EADDRNOTAVAIL;
625 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
627 if (vx_flags(VXF_HIDE_NETIF, 0) &&
628 !ifa_in_nx_info(ifa, current->nx_info))
632 case SIOCGIFADDR: /* Get interface address */
633 sin->sin_addr.s_addr = ifa->ifa_local;
636 case SIOCGIFBRDADDR: /* Get the broadcast address */
637 sin->sin_addr.s_addr = ifa->ifa_broadcast;
640 case SIOCGIFDSTADDR: /* Get the destination address */
641 sin->sin_addr.s_addr = ifa->ifa_address;
644 case SIOCGIFNETMASK: /* Get the netmask for the interface */
645 sin->sin_addr.s_addr = ifa->ifa_mask;
650 ret = -EADDRNOTAVAIL;
654 if (!(ifr.ifr_flags & IFF_UP))
655 inet_del_ifa(in_dev, ifap, 1);
658 ret = dev_change_flags(dev, ifr.ifr_flags);
661 case SIOCSIFADDR: /* Set interface address (and family) */
663 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
668 if ((ifa = inet_alloc_ifa()) == NULL)
671 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
673 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
676 if (ifa->ifa_local == sin->sin_addr.s_addr)
678 inet_del_ifa(in_dev, ifap, 0);
679 ifa->ifa_broadcast = 0;
680 ifa->ifa_anycast = 0;
683 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
685 if (!(dev->flags & IFF_POINTOPOINT)) {
686 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
687 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
688 if ((dev->flags & IFF_BROADCAST) &&
689 ifa->ifa_prefixlen < 31)
690 ifa->ifa_broadcast = ifa->ifa_address |
693 ifa->ifa_prefixlen = 32;
694 ifa->ifa_mask = inet_make_mask(32);
696 ret = inet_set_ifa(dev, ifa);
699 case SIOCSIFBRDADDR: /* Set the broadcast address */
701 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
702 inet_del_ifa(in_dev, ifap, 0);
703 ifa->ifa_broadcast = sin->sin_addr.s_addr;
704 inet_insert_ifa(ifa);
708 case SIOCSIFDSTADDR: /* Set the destination address */
710 if (ifa->ifa_address == sin->sin_addr.s_addr)
713 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
716 inet_del_ifa(in_dev, ifap, 0);
717 ifa->ifa_address = sin->sin_addr.s_addr;
718 inet_insert_ifa(ifa);
721 case SIOCSIFNETMASK: /* Set the netmask for the interface */
724 * The mask we set must be legal.
727 if (bad_mask(sin->sin_addr.s_addr, 0))
730 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
731 inet_del_ifa(in_dev, ifap, 0);
732 ifa->ifa_mask = sin->sin_addr.s_addr;
733 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
735 /* See if current broadcast address matches
736 * with current netmask, then recalculate
737 * the broadcast address. Otherwise it's a
738 * funny address, so don't touch it since
739 * the user seems to know what (s)he's doing...
741 if ((dev->flags & IFF_BROADCAST) &&
742 (ifa->ifa_prefixlen < 31) &&
743 (ifa->ifa_broadcast ==
744 (ifa->ifa_local|~ifa->ifa_mask))) {
745 ifa->ifa_broadcast = (ifa->ifa_local |
746 ~sin->sin_addr.s_addr);
748 inet_insert_ifa(ifa);
758 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
762 static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
764 struct in_device *in_dev = __in_dev_get(dev);
765 struct in_ifaddr *ifa;
769 if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
772 for (; ifa; ifa = ifa->ifa_next) {
773 if (vx_flags(VXF_HIDE_NETIF, 0) &&
774 !ifa_in_nx_info(ifa, current->nx_info))
780 if (len < (int) sizeof(ifr))
782 memset(&ifr, 0, sizeof(struct ifreq));
784 strcpy(ifr.ifr_name, ifa->ifa_label);
786 strcpy(ifr.ifr_name, dev->name);
788 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
789 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
792 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
796 buf += sizeof(struct ifreq);
797 len -= sizeof(struct ifreq);
798 done += sizeof(struct ifreq);
804 u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
807 struct in_device *in_dev;
810 in_dev = __in_dev_get(dev);
814 for_primary_ifa(in_dev) {
815 if (ifa->ifa_scope > scope)
817 if (!dst || inet_ifa_match(dst, ifa)) {
818 addr = ifa->ifa_local;
822 addr = ifa->ifa_local;
823 } endfor_ifa(in_dev);
830 /* Not loopback addresses on loopback should be preferred
831 in this case. It is importnat that lo is the first interface
834 read_lock(&dev_base_lock);
836 for (dev = dev_base; dev; dev = dev->next) {
837 if ((in_dev = __in_dev_get(dev)) == NULL)
840 for_primary_ifa(in_dev) {
841 if (ifa->ifa_scope != RT_SCOPE_LINK &&
842 ifa->ifa_scope <= scope) {
843 addr = ifa->ifa_local;
844 goto out_unlock_both;
846 } endfor_ifa(in_dev);
849 read_unlock(&dev_base_lock);
855 static u32 confirm_addr_indev(struct in_device *in_dev, u32 dst,
856 u32 local, int scope)
863 (local == ifa->ifa_local || !local) &&
864 ifa->ifa_scope <= scope) {
865 addr = ifa->ifa_local;
870 same = (!local || inet_ifa_match(local, ifa)) &&
871 (!dst || inet_ifa_match(dst, ifa));
875 /* Is the selected addr into dst subnet? */
876 if (inet_ifa_match(addr, ifa))
878 /* No, then can we use new local src? */
879 if (ifa->ifa_scope <= scope) {
880 addr = ifa->ifa_local;
883 /* search for large dst subnet for addr */
887 } endfor_ifa(in_dev);
889 return same? addr : 0;
893 * Confirm that local IP address exists using wildcards:
894 * - dev: only on this interface, 0=any interface
895 * - dst: only in the same subnet as dst, 0=any dst
896 * - local: address, 0=autoselect the local address
897 * - scope: maximum allowed scope value for the local address
899 u32 inet_confirm_addr(const struct net_device *dev, u32 dst, u32 local, int scope)
902 struct in_device *in_dev;
906 if ((in_dev = __in_dev_get(dev)))
907 addr = confirm_addr_indev(in_dev, dst, local, scope);
913 read_lock(&dev_base_lock);
915 for (dev = dev_base; dev; dev = dev->next) {
916 if ((in_dev = __in_dev_get(dev))) {
917 addr = confirm_addr_indev(in_dev, dst, local, scope);
923 read_unlock(&dev_base_lock);
932 int register_inetaddr_notifier(struct notifier_block *nb)
934 return notifier_chain_register(&inetaddr_chain, nb);
937 int unregister_inetaddr_notifier(struct notifier_block *nb)
939 return notifier_chain_unregister(&inetaddr_chain, nb);
942 /* Rename ifa_labels for a device name change. Make some effort to preserve existing
943 * alias numbering and to create unique labels if possible.
945 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
947 struct in_ifaddr *ifa;
950 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
951 char old[IFNAMSIZ], *dot;
953 memcpy(old, ifa->ifa_label, IFNAMSIZ);
954 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
957 dot = strchr(ifa->ifa_label, ':');
959 sprintf(old, ":%d", named);
962 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) {
963 strcat(ifa->ifa_label, dot);
965 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
970 /* Called only under RTNL semaphore */
972 static int inetdev_event(struct notifier_block *this, unsigned long event,
975 struct net_device *dev = ptr;
976 struct in_device *in_dev = __in_dev_get(dev);
984 case NETDEV_REGISTER:
985 printk(KERN_DEBUG "inetdev_event: bug\n");
991 if (dev == &loopback_dev) {
992 struct in_ifaddr *ifa;
993 if ((ifa = inet_alloc_ifa()) != NULL) {
995 ifa->ifa_address = htonl(INADDR_LOOPBACK);
996 ifa->ifa_prefixlen = 8;
997 ifa->ifa_mask = inet_make_mask(8);
999 ifa->ifa_dev = in_dev;
1000 ifa->ifa_scope = RT_SCOPE_HOST;
1001 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1002 inet_insert_ifa(ifa);
1004 in_dev->cnf.no_xfrm = 1;
1005 in_dev->cnf.no_policy = 1;
1012 case NETDEV_CHANGEMTU:
1015 /* MTU falled under 68, disable IP */
1016 case NETDEV_UNREGISTER:
1017 inetdev_destroy(in_dev);
1019 case NETDEV_CHANGENAME:
1020 /* Do not notify about label change, this event is
1021 * not interesting to applications using netlink.
1023 inetdev_changename(dev, in_dev);
1025 #ifdef CONFIG_SYSCTL
1026 devinet_sysctl_unregister(&in_dev->cnf);
1027 neigh_sysctl_unregister(in_dev->arp_parms);
1028 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
1029 NET_IPV4_NEIGH, "ipv4", NULL);
1030 devinet_sysctl_register(in_dev, &in_dev->cnf);
1038 static struct notifier_block ip_netdev_notifier = {
1039 .notifier_call =inetdev_event,
1042 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1043 u32 pid, u32 seq, int event)
1045 struct ifaddrmsg *ifm;
1046 struct nlmsghdr *nlh;
1047 unsigned char *b = skb->tail;
1049 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
1050 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
1051 ifm = NLMSG_DATA(nlh);
1052 ifm->ifa_family = AF_INET;
1053 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1054 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1055 ifm->ifa_scope = ifa->ifa_scope;
1056 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1057 if (ifa->ifa_address)
1058 RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
1060 RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
1061 if (ifa->ifa_broadcast)
1062 RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
1063 if (ifa->ifa_anycast)
1064 RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
1065 if (ifa->ifa_label[0])
1066 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
1067 nlh->nlmsg_len = skb->tail - b;
1072 skb_trim(skb, b - skb->data);
1076 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1079 struct net_device *dev;
1080 struct in_device *in_dev;
1081 struct in_ifaddr *ifa;
1082 struct sock *sk = skb->sk;
1083 int s_ip_idx, s_idx = cb->args[0];
1085 s_ip_idx = ip_idx = cb->args[1];
1086 read_lock(&dev_base_lock);
1087 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
1093 if ((in_dev = __in_dev_get(dev)) == NULL) {
1098 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1099 ifa = ifa->ifa_next, ip_idx++) {
1100 if (sk && vx_info_flags(sk->sk_vx_info, VXF_HIDE_NETIF, 0) &&
1101 !ifa_in_nx_info(ifa, sk->sk_nx_info))
1103 if (ip_idx < s_ip_idx)
1105 if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1107 RTM_NEWADDR) <= 0) {
1116 read_unlock(&dev_base_lock);
1118 cb->args[1] = ip_idx;
1123 static void rtmsg_ifa(int event, struct in_ifaddr* ifa)
1125 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + 128);
1126 struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);
1129 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, ENOBUFS);
1130 else if (inet_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
1132 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, EINVAL);
1134 NETLINK_CB(skb).dst_groups = RTMGRP_IPV4_IFADDR;
1135 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV4_IFADDR, GFP_KERNEL);
1139 static struct rtnetlink_link inet_rtnetlink_table[RTM_MAX - RTM_BASE + 1] = {
1140 [4] = { .doit = inet_rtm_newaddr, },
1141 [5] = { .doit = inet_rtm_deladdr, },
1142 [6] = { .dumpit = inet_dump_ifaddr, },
1143 [8] = { .doit = inet_rtm_newroute, },
1144 [9] = { .doit = inet_rtm_delroute, },
1145 [10] = { .doit = inet_rtm_getroute, .dumpit = inet_dump_fib, },
1146 #ifdef CONFIG_IP_MULTIPLE_TABLES
1147 [16] = { .doit = inet_rtm_newrule, },
1148 [17] = { .doit = inet_rtm_delrule, },
1149 [18] = { .dumpit = inet_dump_rules, },
1153 #ifdef CONFIG_SYSCTL
1155 void inet_forward_change(void)
1157 struct net_device *dev;
1158 int on = ipv4_devconf.forwarding;
1160 ipv4_devconf.accept_redirects = !on;
1161 ipv4_devconf_dflt.forwarding = on;
1163 read_lock(&dev_base_lock);
1164 for (dev = dev_base; dev; dev = dev->next) {
1165 struct in_device *in_dev;
1167 in_dev = __in_dev_get(dev);
1169 in_dev->cnf.forwarding = on;
1172 read_unlock(&dev_base_lock);
1177 static int devinet_sysctl_forward(ctl_table *ctl, int write,
1178 struct file* filp, void __user *buffer,
1179 size_t *lenp, loff_t *ppos)
1181 int *valp = ctl->data;
1183 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1185 if (write && *valp != val) {
1186 if (valp == &ipv4_devconf.forwarding)
1187 inet_forward_change();
1188 else if (valp != &ipv4_devconf_dflt.forwarding)
1195 int ipv4_doint_and_flush(ctl_table *ctl, int write,
1196 struct file* filp, void __user *buffer,
1197 size_t *lenp, loff_t *ppos)
1199 int *valp = ctl->data;
1201 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1203 if (write && *valp != val)
1209 int ipv4_doint_and_flush_strategy(ctl_table *table, int __user *name, int nlen,
1210 void __user *oldval, size_t __user *oldlenp,
1211 void __user *newval, size_t newlen,
1214 int *valp = table->data;
1217 if (!newval || !newlen)
1220 if (newlen != sizeof(int))
1223 if (get_user(new, (int __user *)newval))
1229 if (oldval && oldlenp) {
1232 if (get_user(len, oldlenp))
1236 if (len > table->maxlen)
1237 len = table->maxlen;
1238 if (copy_to_user(oldval, valp, len))
1240 if (put_user(len, oldlenp))
1251 static struct devinet_sysctl_table {
1252 struct ctl_table_header *sysctl_header;
1253 ctl_table devinet_vars[20];
1254 ctl_table devinet_dev[2];
1255 ctl_table devinet_conf_dir[2];
1256 ctl_table devinet_proto_dir[2];
1257 ctl_table devinet_root_dir[2];
1258 } devinet_sysctl = {
1261 .ctl_name = NET_IPV4_CONF_FORWARDING,
1262 .procname = "forwarding",
1263 .data = &ipv4_devconf.forwarding,
1264 .maxlen = sizeof(int),
1266 .proc_handler = &devinet_sysctl_forward,
1269 .ctl_name = NET_IPV4_CONF_MC_FORWARDING,
1270 .procname = "mc_forwarding",
1271 .data = &ipv4_devconf.mc_forwarding,
1272 .maxlen = sizeof(int),
1274 .proc_handler = &proc_dointvec,
1277 .ctl_name = NET_IPV4_CONF_ACCEPT_REDIRECTS,
1278 .procname = "accept_redirects",
1279 .data = &ipv4_devconf.accept_redirects,
1280 .maxlen = sizeof(int),
1282 .proc_handler = &proc_dointvec,
1285 .ctl_name = NET_IPV4_CONF_SECURE_REDIRECTS,
1286 .procname = "secure_redirects",
1287 .data = &ipv4_devconf.secure_redirects,
1288 .maxlen = sizeof(int),
1290 .proc_handler = &proc_dointvec,
1293 .ctl_name = NET_IPV4_CONF_SHARED_MEDIA,
1294 .procname = "shared_media",
1295 .data = &ipv4_devconf.shared_media,
1296 .maxlen = sizeof(int),
1298 .proc_handler = &proc_dointvec,
1301 .ctl_name = NET_IPV4_CONF_RP_FILTER,
1302 .procname = "rp_filter",
1303 .data = &ipv4_devconf.rp_filter,
1304 .maxlen = sizeof(int),
1306 .proc_handler = &proc_dointvec,
1309 .ctl_name = NET_IPV4_CONF_SEND_REDIRECTS,
1310 .procname = "send_redirects",
1311 .data = &ipv4_devconf.send_redirects,
1312 .maxlen = sizeof(int),
1314 .proc_handler = &proc_dointvec,
1317 .ctl_name = NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE,
1318 .procname = "accept_source_route",
1319 .data = &ipv4_devconf.accept_source_route,
1320 .maxlen = sizeof(int),
1322 .proc_handler = &proc_dointvec,
1325 .ctl_name = NET_IPV4_CONF_PROXY_ARP,
1326 .procname = "proxy_arp",
1327 .data = &ipv4_devconf.proxy_arp,
1328 .maxlen = sizeof(int),
1330 .proc_handler = &proc_dointvec,
1333 .ctl_name = NET_IPV4_CONF_MEDIUM_ID,
1334 .procname = "medium_id",
1335 .data = &ipv4_devconf.medium_id,
1336 .maxlen = sizeof(int),
1338 .proc_handler = &proc_dointvec,
1341 .ctl_name = NET_IPV4_CONF_BOOTP_RELAY,
1342 .procname = "bootp_relay",
1343 .data = &ipv4_devconf.bootp_relay,
1344 .maxlen = sizeof(int),
1346 .proc_handler = &proc_dointvec,
1349 .ctl_name = NET_IPV4_CONF_LOG_MARTIANS,
1350 .procname = "log_martians",
1351 .data = &ipv4_devconf.log_martians,
1352 .maxlen = sizeof(int),
1354 .proc_handler = &proc_dointvec,
1357 .ctl_name = NET_IPV4_CONF_TAG,
1359 .data = &ipv4_devconf.tag,
1360 .maxlen = sizeof(int),
1362 .proc_handler = &proc_dointvec,
1365 .ctl_name = NET_IPV4_CONF_ARPFILTER,
1366 .procname = "arp_filter",
1367 .data = &ipv4_devconf.arp_filter,
1368 .maxlen = sizeof(int),
1370 .proc_handler = &proc_dointvec,
1373 .ctl_name = NET_IPV4_CONF_ARP_ANNOUNCE,
1374 .procname = "arp_announce",
1375 .data = &ipv4_devconf.arp_announce,
1376 .maxlen = sizeof(int),
1378 .proc_handler = &proc_dointvec,
1381 .ctl_name = NET_IPV4_CONF_ARP_IGNORE,
1382 .procname = "arp_ignore",
1383 .data = &ipv4_devconf.arp_ignore,
1384 .maxlen = sizeof(int),
1386 .proc_handler = &proc_dointvec,
1389 .ctl_name = NET_IPV4_CONF_NOXFRM,
1390 .procname = "disable_xfrm",
1391 .data = &ipv4_devconf.no_xfrm,
1392 .maxlen = sizeof(int),
1394 .proc_handler = &ipv4_doint_and_flush,
1395 .strategy = &ipv4_doint_and_flush_strategy,
1398 .ctl_name = NET_IPV4_CONF_NOPOLICY,
1399 .procname = "disable_policy",
1400 .data = &ipv4_devconf.no_policy,
1401 .maxlen = sizeof(int),
1403 .proc_handler = &ipv4_doint_and_flush,
1404 .strategy = &ipv4_doint_and_flush_strategy,
1407 .ctl_name = NET_IPV4_CONF_FORCE_IGMP_VERSION,
1408 .procname = "force_igmp_version",
1409 .data = &ipv4_devconf.force_igmp_version,
1410 .maxlen = sizeof(int),
1412 .proc_handler = &ipv4_doint_and_flush,
1413 .strategy = &ipv4_doint_and_flush_strategy,
1418 .ctl_name = NET_PROTO_CONF_ALL,
1421 .child = devinet_sysctl.devinet_vars,
1424 .devinet_conf_dir = {
1426 .ctl_name = NET_IPV4_CONF,
1429 .child = devinet_sysctl.devinet_dev,
1432 .devinet_proto_dir = {
1434 .ctl_name = NET_IPV4,
1437 .child = devinet_sysctl.devinet_conf_dir,
1440 .devinet_root_dir = {
1442 .ctl_name = CTL_NET,
1445 .child = devinet_sysctl.devinet_proto_dir,
1450 static void devinet_sysctl_register(struct in_device *in_dev,
1451 struct ipv4_devconf *p)
1454 struct net_device *dev = in_dev ? in_dev->dev : NULL;
1455 struct devinet_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
1456 char *dev_name = NULL;
1460 memcpy(t, &devinet_sysctl, sizeof(*t));
1461 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1462 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
1463 t->devinet_vars[i].de = NULL;
1467 dev_name = dev->name;
1468 t->devinet_dev[0].ctl_name = dev->ifindex;
1470 dev_name = "default";
1471 t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
1475 * Make a copy of dev_name, because '.procname' is regarded as const
1476 * by sysctl and we wouldn't want anyone to change it under our feet
1477 * (see SIOCSIFNAME).
1479 dev_name = net_sysctl_strdup(dev_name);
1483 t->devinet_dev[0].procname = dev_name;
1484 t->devinet_dev[0].child = t->devinet_vars;
1485 t->devinet_dev[0].de = NULL;
1486 t->devinet_conf_dir[0].child = t->devinet_dev;
1487 t->devinet_conf_dir[0].de = NULL;
1488 t->devinet_proto_dir[0].child = t->devinet_conf_dir;
1489 t->devinet_proto_dir[0].de = NULL;
1490 t->devinet_root_dir[0].child = t->devinet_proto_dir;
1491 t->devinet_root_dir[0].de = NULL;
1493 t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
1494 if (!t->sysctl_header)
1508 static void devinet_sysctl_unregister(struct ipv4_devconf *p)
1511 struct devinet_sysctl_table *t = p->sysctl;
1513 unregister_sysctl_table(t->sysctl_header);
1514 kfree(t->devinet_dev[0].procname);
1520 void __init devinet_init(void)
1522 register_gifconf(PF_INET, inet_gifconf);
1523 register_netdevice_notifier(&ip_netdev_notifier);
1524 rtnetlink_links[PF_INET] = inet_rtnetlink_table;
1525 #ifdef CONFIG_SYSCTL
1526 devinet_sysctl.sysctl_header =
1527 register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
1528 devinet_sysctl_register(NULL, &ipv4_devconf_dflt);
1532 EXPORT_SYMBOL(devinet_ioctl);
1533 EXPORT_SYMBOL(in_dev_finish_destroy);
1534 EXPORT_SYMBOL(inet_select_addr);
1535 EXPORT_SYMBOL(inetdev_by_index);
1536 EXPORT_SYMBOL(register_inetaddr_notifier);
1537 EXPORT_SYMBOL(unregister_inetaddr_notifier);