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 int devinet_ioctl(unsigned int cmd, void __user *arg)
497 struct sockaddr_in sin_orig;
498 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
499 struct in_device *in_dev;
500 struct in_ifaddr **ifap = NULL;
501 struct in_ifaddr *ifa = NULL;
502 struct net_device *dev;
505 int tryaddrmatch = 0;
508 * Fetch the caller's info block into kernel space
511 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
513 ifr.ifr_name[IFNAMSIZ - 1] = 0;
515 /* save original address for comparison */
516 memcpy(&sin_orig, sin, sizeof(*sin));
518 colon = strchr(ifr.ifr_name, ':');
523 dev_load(ifr.ifr_name);
527 case SIOCGIFADDR: /* Get interface address */
528 case SIOCGIFBRDADDR: /* Get the broadcast address */
529 case SIOCGIFDSTADDR: /* Get the destination address */
530 case SIOCGIFNETMASK: /* Get the netmask for the interface */
531 /* Note that these ioctls will not sleep,
532 so that we do not impose a lock.
533 One day we will be forced to put shlock here (I mean SMP)
535 tryaddrmatch = (sin_orig.sin_family == AF_INET);
536 memset(sin, 0, sizeof(*sin));
537 sin->sin_family = AF_INET;
542 if (!capable(CAP_NET_ADMIN))
545 case SIOCSIFADDR: /* Set interface address (and family) */
546 case SIOCSIFBRDADDR: /* Set the broadcast address */
547 case SIOCSIFDSTADDR: /* Set the destination address */
548 case SIOCSIFNETMASK: /* Set the netmask for the interface */
550 if (!capable(CAP_NET_ADMIN))
553 if (sin->sin_family != AF_INET)
564 if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL)
570 if ((in_dev = __in_dev_get(dev)) != NULL) {
572 /* Matthias Andree */
573 /* compare label and address (4.4BSD style) */
574 /* note: we only do this for a limited set of ioctls
575 and only if the original address family was AF_INET.
576 This is checked above. */
577 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
578 ifap = &ifa->ifa_next) {
579 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
580 sin_orig.sin_addr.s_addr ==
586 /* we didn't get a match, maybe the application is
587 4.3BSD-style and passed in junk so we fall back to
588 comparing just the label */
590 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
591 ifap = &ifa->ifa_next)
592 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
597 ret = -EADDRNOTAVAIL;
598 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
602 case SIOCGIFADDR: /* Get interface address */
603 sin->sin_addr.s_addr = ifa->ifa_local;
606 case SIOCGIFBRDADDR: /* Get the broadcast address */
607 sin->sin_addr.s_addr = ifa->ifa_broadcast;
610 case SIOCGIFDSTADDR: /* Get the destination address */
611 sin->sin_addr.s_addr = ifa->ifa_address;
614 case SIOCGIFNETMASK: /* Get the netmask for the interface */
615 sin->sin_addr.s_addr = ifa->ifa_mask;
620 ret = -EADDRNOTAVAIL;
624 if (!(ifr.ifr_flags & IFF_UP))
625 inet_del_ifa(in_dev, ifap, 1);
628 ret = dev_change_flags(dev, ifr.ifr_flags);
631 case SIOCSIFADDR: /* Set interface address (and family) */
633 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
638 if ((ifa = inet_alloc_ifa()) == NULL)
641 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
643 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
646 if (ifa->ifa_local == sin->sin_addr.s_addr)
648 inet_del_ifa(in_dev, ifap, 0);
649 ifa->ifa_broadcast = 0;
650 ifa->ifa_anycast = 0;
653 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
655 if (!(dev->flags & IFF_POINTOPOINT)) {
656 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
657 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
658 if ((dev->flags & IFF_BROADCAST) &&
659 ifa->ifa_prefixlen < 31)
660 ifa->ifa_broadcast = ifa->ifa_address |
663 ifa->ifa_prefixlen = 32;
664 ifa->ifa_mask = inet_make_mask(32);
666 ret = inet_set_ifa(dev, ifa);
669 case SIOCSIFBRDADDR: /* Set the broadcast address */
671 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
672 inet_del_ifa(in_dev, ifap, 0);
673 ifa->ifa_broadcast = sin->sin_addr.s_addr;
674 inet_insert_ifa(ifa);
678 case SIOCSIFDSTADDR: /* Set the destination address */
680 if (ifa->ifa_address == sin->sin_addr.s_addr)
683 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
686 inet_del_ifa(in_dev, ifap, 0);
687 ifa->ifa_address = sin->sin_addr.s_addr;
688 inet_insert_ifa(ifa);
691 case SIOCSIFNETMASK: /* Set the netmask for the interface */
694 * The mask we set must be legal.
697 if (bad_mask(sin->sin_addr.s_addr, 0))
700 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
701 inet_del_ifa(in_dev, ifap, 0);
702 ifa->ifa_mask = sin->sin_addr.s_addr;
703 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
705 /* See if current broadcast address matches
706 * with current netmask, then recalculate
707 * the broadcast address. Otherwise it's a
708 * funny address, so don't touch it since
709 * the user seems to know what (s)he's doing...
711 if ((dev->flags & IFF_BROADCAST) &&
712 (ifa->ifa_prefixlen < 31) &&
713 (ifa->ifa_broadcast ==
714 (ifa->ifa_local|~ifa->ifa_mask))) {
715 ifa->ifa_broadcast = (ifa->ifa_local |
716 ~sin->sin_addr.s_addr);
718 inet_insert_ifa(ifa);
728 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
732 static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
734 struct in_device *in_dev = __in_dev_get(dev);
735 struct in_ifaddr *ifa;
739 if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
742 for (; ifa; ifa = ifa->ifa_next) {
747 if (len < (int) sizeof(ifr))
749 memset(&ifr, 0, sizeof(struct ifreq));
751 strcpy(ifr.ifr_name, ifa->ifa_label);
753 strcpy(ifr.ifr_name, dev->name);
755 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
756 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
759 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
763 buf += sizeof(struct ifreq);
764 len -= sizeof(struct ifreq);
765 done += sizeof(struct ifreq);
771 u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
774 struct in_device *in_dev;
777 in_dev = __in_dev_get(dev);
781 for_primary_ifa(in_dev) {
782 if (ifa->ifa_scope > scope)
784 if (!dst || inet_ifa_match(dst, ifa)) {
785 addr = ifa->ifa_local;
789 addr = ifa->ifa_local;
790 } endfor_ifa(in_dev);
797 /* Not loopback addresses on loopback should be preferred
798 in this case. It is importnat that lo is the first interface
801 read_lock(&dev_base_lock);
803 for (dev = dev_base; dev; dev = dev->next) {
804 if ((in_dev = __in_dev_get(dev)) == NULL)
807 for_primary_ifa(in_dev) {
808 if (ifa->ifa_scope != RT_SCOPE_LINK &&
809 ifa->ifa_scope <= scope) {
810 addr = ifa->ifa_local;
811 goto out_unlock_both;
813 } endfor_ifa(in_dev);
816 read_unlock(&dev_base_lock);
822 static u32 confirm_addr_indev(struct in_device *in_dev, u32 dst,
823 u32 local, int scope)
830 (local == ifa->ifa_local || !local) &&
831 ifa->ifa_scope <= scope) {
832 addr = ifa->ifa_local;
837 same = (!local || inet_ifa_match(local, ifa)) &&
838 (!dst || inet_ifa_match(dst, ifa));
842 /* Is the selected addr into dst subnet? */
843 if (inet_ifa_match(addr, ifa))
845 /* No, then can we use new local src? */
846 if (ifa->ifa_scope <= scope) {
847 addr = ifa->ifa_local;
850 /* search for large dst subnet for addr */
854 } endfor_ifa(in_dev);
856 return same? addr : 0;
860 * Confirm that local IP address exists using wildcards:
861 * - dev: only on this interface, 0=any interface
862 * - dst: only in the same subnet as dst, 0=any dst
863 * - local: address, 0=autoselect the local address
864 * - scope: maximum allowed scope value for the local address
866 u32 inet_confirm_addr(const struct net_device *dev, u32 dst, u32 local, int scope)
869 struct in_device *in_dev;
873 if ((in_dev = __in_dev_get(dev)))
874 addr = confirm_addr_indev(in_dev, dst, local, scope);
880 read_lock(&dev_base_lock);
882 for (dev = dev_base; dev; dev = dev->next) {
883 if ((in_dev = __in_dev_get(dev))) {
884 addr = confirm_addr_indev(in_dev, dst, local, scope);
890 read_unlock(&dev_base_lock);
899 int register_inetaddr_notifier(struct notifier_block *nb)
901 return notifier_chain_register(&inetaddr_chain, nb);
904 int unregister_inetaddr_notifier(struct notifier_block *nb)
906 return notifier_chain_unregister(&inetaddr_chain, nb);
909 /* Rename ifa_labels for a device name change. Make some effort to preserve existing
910 * alias numbering and to create unique labels if possible.
912 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
914 struct in_ifaddr *ifa;
917 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
918 char old[IFNAMSIZ], *dot;
920 memcpy(old, ifa->ifa_label, IFNAMSIZ);
921 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
924 dot = strchr(ifa->ifa_label, ':');
926 sprintf(old, ":%d", named);
929 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) {
930 strcat(ifa->ifa_label, dot);
932 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
937 /* Called only under RTNL semaphore */
939 static int inetdev_event(struct notifier_block *this, unsigned long event,
942 struct net_device *dev = ptr;
943 struct in_device *in_dev = __in_dev_get(dev);
951 case NETDEV_REGISTER:
952 printk(KERN_DEBUG "inetdev_event: bug\n");
958 if (dev == &loopback_dev) {
959 struct in_ifaddr *ifa;
960 if ((ifa = inet_alloc_ifa()) != NULL) {
962 ifa->ifa_address = htonl(INADDR_LOOPBACK);
963 ifa->ifa_prefixlen = 8;
964 ifa->ifa_mask = inet_make_mask(8);
966 ifa->ifa_dev = in_dev;
967 ifa->ifa_scope = RT_SCOPE_HOST;
968 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
969 inet_insert_ifa(ifa);
971 in_dev->cnf.no_xfrm = 1;
972 in_dev->cnf.no_policy = 1;
979 case NETDEV_CHANGEMTU:
982 /* MTU falled under 68, disable IP */
983 case NETDEV_UNREGISTER:
984 inetdev_destroy(in_dev);
986 case NETDEV_CHANGENAME:
987 /* Do not notify about label change, this event is
988 * not interesting to applications using netlink.
990 inetdev_changename(dev, in_dev);
993 devinet_sysctl_unregister(&in_dev->cnf);
994 neigh_sysctl_unregister(in_dev->arp_parms);
995 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
996 NET_IPV4_NEIGH, "ipv4", NULL);
997 devinet_sysctl_register(in_dev, &in_dev->cnf);
1005 static struct notifier_block ip_netdev_notifier = {
1006 .notifier_call =inetdev_event,
1009 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1010 u32 pid, u32 seq, int event)
1012 struct ifaddrmsg *ifm;
1013 struct nlmsghdr *nlh;
1014 unsigned char *b = skb->tail;
1016 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
1017 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
1018 ifm = NLMSG_DATA(nlh);
1019 ifm->ifa_family = AF_INET;
1020 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1021 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1022 ifm->ifa_scope = ifa->ifa_scope;
1023 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1024 if (ifa->ifa_address)
1025 RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
1027 RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
1028 if (ifa->ifa_broadcast)
1029 RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
1030 if (ifa->ifa_anycast)
1031 RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
1032 if (ifa->ifa_label[0])
1033 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
1034 nlh->nlmsg_len = skb->tail - b;
1039 skb_trim(skb, b - skb->data);
1043 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1046 struct net_device *dev;
1047 struct in_device *in_dev;
1048 struct in_ifaddr *ifa;
1049 struct sock *sk = skb->sk;
1050 int s_ip_idx, s_idx = cb->args[0];
1052 s_ip_idx = ip_idx = cb->args[1];
1053 read_lock(&dev_base_lock);
1054 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
1060 if ((in_dev = __in_dev_get(dev)) == NULL) {
1065 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1066 ifa = ifa->ifa_next, ip_idx++) {
1067 if (ip_idx < s_ip_idx)
1069 if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1071 RTM_NEWADDR) <= 0) {
1080 read_unlock(&dev_base_lock);
1082 cb->args[1] = ip_idx;
1087 static void rtmsg_ifa(int event, struct in_ifaddr* ifa)
1089 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + 128);
1090 struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);
1093 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, ENOBUFS);
1094 else if (inet_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
1096 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, EINVAL);
1098 NETLINK_CB(skb).dst_groups = RTMGRP_IPV4_IFADDR;
1099 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV4_IFADDR, GFP_KERNEL);
1103 static struct rtnetlink_link inet_rtnetlink_table[RTM_MAX - RTM_BASE + 1] = {
1104 [4] = { .doit = inet_rtm_newaddr, },
1105 [5] = { .doit = inet_rtm_deladdr, },
1106 [6] = { .dumpit = inet_dump_ifaddr, },
1107 [8] = { .doit = inet_rtm_newroute, },
1108 [9] = { .doit = inet_rtm_delroute, },
1109 [10] = { .doit = inet_rtm_getroute, .dumpit = inet_dump_fib, },
1110 #ifdef CONFIG_IP_MULTIPLE_TABLES
1111 [16] = { .doit = inet_rtm_newrule, },
1112 [17] = { .doit = inet_rtm_delrule, },
1113 [18] = { .dumpit = inet_dump_rules, },
1117 #ifdef CONFIG_SYSCTL
1119 void inet_forward_change(void)
1121 struct net_device *dev;
1122 int on = ipv4_devconf.forwarding;
1124 ipv4_devconf.accept_redirects = !on;
1125 ipv4_devconf_dflt.forwarding = on;
1127 read_lock(&dev_base_lock);
1128 for (dev = dev_base; dev; dev = dev->next) {
1129 struct in_device *in_dev;
1131 in_dev = __in_dev_get(dev);
1133 in_dev->cnf.forwarding = on;
1136 read_unlock(&dev_base_lock);
1141 static int devinet_sysctl_forward(ctl_table *ctl, int write,
1142 struct file* filp, void __user *buffer,
1143 size_t *lenp, loff_t *ppos)
1145 int *valp = ctl->data;
1147 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1149 if (write && *valp != val) {
1150 if (valp == &ipv4_devconf.forwarding)
1151 inet_forward_change();
1152 else if (valp != &ipv4_devconf_dflt.forwarding)
1159 int ipv4_doint_and_flush(ctl_table *ctl, int write,
1160 struct file* filp, void __user *buffer,
1161 size_t *lenp, loff_t *ppos)
1163 int *valp = ctl->data;
1165 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1167 if (write && *valp != val)
1173 int ipv4_doint_and_flush_strategy(ctl_table *table, int __user *name, int nlen,
1174 void __user *oldval, size_t __user *oldlenp,
1175 void __user *newval, size_t newlen,
1178 int *valp = table->data;
1181 if (!newval || !newlen)
1184 if (newlen != sizeof(int))
1187 if (get_user(new, (int __user *)newval))
1193 if (oldval && oldlenp) {
1196 if (get_user(len, oldlenp))
1200 if (len > table->maxlen)
1201 len = table->maxlen;
1202 if (copy_to_user(oldval, valp, len))
1204 if (put_user(len, oldlenp))
1215 static struct devinet_sysctl_table {
1216 struct ctl_table_header *sysctl_header;
1217 ctl_table devinet_vars[20];
1218 ctl_table devinet_dev[2];
1219 ctl_table devinet_conf_dir[2];
1220 ctl_table devinet_proto_dir[2];
1221 ctl_table devinet_root_dir[2];
1222 } devinet_sysctl = {
1225 .ctl_name = NET_IPV4_CONF_FORWARDING,
1226 .procname = "forwarding",
1227 .data = &ipv4_devconf.forwarding,
1228 .maxlen = sizeof(int),
1230 .proc_handler = &devinet_sysctl_forward,
1233 .ctl_name = NET_IPV4_CONF_MC_FORWARDING,
1234 .procname = "mc_forwarding",
1235 .data = &ipv4_devconf.mc_forwarding,
1236 .maxlen = sizeof(int),
1238 .proc_handler = &proc_dointvec,
1241 .ctl_name = NET_IPV4_CONF_ACCEPT_REDIRECTS,
1242 .procname = "accept_redirects",
1243 .data = &ipv4_devconf.accept_redirects,
1244 .maxlen = sizeof(int),
1246 .proc_handler = &proc_dointvec,
1249 .ctl_name = NET_IPV4_CONF_SECURE_REDIRECTS,
1250 .procname = "secure_redirects",
1251 .data = &ipv4_devconf.secure_redirects,
1252 .maxlen = sizeof(int),
1254 .proc_handler = &proc_dointvec,
1257 .ctl_name = NET_IPV4_CONF_SHARED_MEDIA,
1258 .procname = "shared_media",
1259 .data = &ipv4_devconf.shared_media,
1260 .maxlen = sizeof(int),
1262 .proc_handler = &proc_dointvec,
1265 .ctl_name = NET_IPV4_CONF_RP_FILTER,
1266 .procname = "rp_filter",
1267 .data = &ipv4_devconf.rp_filter,
1268 .maxlen = sizeof(int),
1270 .proc_handler = &proc_dointvec,
1273 .ctl_name = NET_IPV4_CONF_SEND_REDIRECTS,
1274 .procname = "send_redirects",
1275 .data = &ipv4_devconf.send_redirects,
1276 .maxlen = sizeof(int),
1278 .proc_handler = &proc_dointvec,
1281 .ctl_name = NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE,
1282 .procname = "accept_source_route",
1283 .data = &ipv4_devconf.accept_source_route,
1284 .maxlen = sizeof(int),
1286 .proc_handler = &proc_dointvec,
1289 .ctl_name = NET_IPV4_CONF_PROXY_ARP,
1290 .procname = "proxy_arp",
1291 .data = &ipv4_devconf.proxy_arp,
1292 .maxlen = sizeof(int),
1294 .proc_handler = &proc_dointvec,
1297 .ctl_name = NET_IPV4_CONF_MEDIUM_ID,
1298 .procname = "medium_id",
1299 .data = &ipv4_devconf.medium_id,
1300 .maxlen = sizeof(int),
1302 .proc_handler = &proc_dointvec,
1305 .ctl_name = NET_IPV4_CONF_BOOTP_RELAY,
1306 .procname = "bootp_relay",
1307 .data = &ipv4_devconf.bootp_relay,
1308 .maxlen = sizeof(int),
1310 .proc_handler = &proc_dointvec,
1313 .ctl_name = NET_IPV4_CONF_LOG_MARTIANS,
1314 .procname = "log_martians",
1315 .data = &ipv4_devconf.log_martians,
1316 .maxlen = sizeof(int),
1318 .proc_handler = &proc_dointvec,
1321 .ctl_name = NET_IPV4_CONF_TAG,
1323 .data = &ipv4_devconf.tag,
1324 .maxlen = sizeof(int),
1326 .proc_handler = &proc_dointvec,
1329 .ctl_name = NET_IPV4_CONF_ARPFILTER,
1330 .procname = "arp_filter",
1331 .data = &ipv4_devconf.arp_filter,
1332 .maxlen = sizeof(int),
1334 .proc_handler = &proc_dointvec,
1337 .ctl_name = NET_IPV4_CONF_ARP_ANNOUNCE,
1338 .procname = "arp_announce",
1339 .data = &ipv4_devconf.arp_announce,
1340 .maxlen = sizeof(int),
1342 .proc_handler = &proc_dointvec,
1345 .ctl_name = NET_IPV4_CONF_ARP_IGNORE,
1346 .procname = "arp_ignore",
1347 .data = &ipv4_devconf.arp_ignore,
1348 .maxlen = sizeof(int),
1350 .proc_handler = &proc_dointvec,
1353 .ctl_name = NET_IPV4_CONF_NOXFRM,
1354 .procname = "disable_xfrm",
1355 .data = &ipv4_devconf.no_xfrm,
1356 .maxlen = sizeof(int),
1358 .proc_handler = &ipv4_doint_and_flush,
1359 .strategy = &ipv4_doint_and_flush_strategy,
1362 .ctl_name = NET_IPV4_CONF_NOPOLICY,
1363 .procname = "disable_policy",
1364 .data = &ipv4_devconf.no_policy,
1365 .maxlen = sizeof(int),
1367 .proc_handler = &ipv4_doint_and_flush,
1368 .strategy = &ipv4_doint_and_flush_strategy,
1371 .ctl_name = NET_IPV4_CONF_FORCE_IGMP_VERSION,
1372 .procname = "force_igmp_version",
1373 .data = &ipv4_devconf.force_igmp_version,
1374 .maxlen = sizeof(int),
1376 .proc_handler = &ipv4_doint_and_flush,
1377 .strategy = &ipv4_doint_and_flush_strategy,
1382 .ctl_name = NET_PROTO_CONF_ALL,
1385 .child = devinet_sysctl.devinet_vars,
1388 .devinet_conf_dir = {
1390 .ctl_name = NET_IPV4_CONF,
1393 .child = devinet_sysctl.devinet_dev,
1396 .devinet_proto_dir = {
1398 .ctl_name = NET_IPV4,
1401 .child = devinet_sysctl.devinet_conf_dir,
1404 .devinet_root_dir = {
1406 .ctl_name = CTL_NET,
1409 .child = devinet_sysctl.devinet_proto_dir,
1414 static void devinet_sysctl_register(struct in_device *in_dev,
1415 struct ipv4_devconf *p)
1418 struct net_device *dev = in_dev ? in_dev->dev : NULL;
1419 struct devinet_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
1420 char *dev_name = NULL;
1424 memcpy(t, &devinet_sysctl, sizeof(*t));
1425 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1426 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
1427 t->devinet_vars[i].de = NULL;
1431 dev_name = dev->name;
1432 t->devinet_dev[0].ctl_name = dev->ifindex;
1434 dev_name = "default";
1435 t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
1439 * Make a copy of dev_name, because '.procname' is regarded as const
1440 * by sysctl and we wouldn't want anyone to change it under our feet
1441 * (see SIOCSIFNAME).
1443 dev_name = net_sysctl_strdup(dev_name);
1447 t->devinet_dev[0].procname = dev_name;
1448 t->devinet_dev[0].child = t->devinet_vars;
1449 t->devinet_dev[0].de = NULL;
1450 t->devinet_conf_dir[0].child = t->devinet_dev;
1451 t->devinet_conf_dir[0].de = NULL;
1452 t->devinet_proto_dir[0].child = t->devinet_conf_dir;
1453 t->devinet_proto_dir[0].de = NULL;
1454 t->devinet_root_dir[0].child = t->devinet_proto_dir;
1455 t->devinet_root_dir[0].de = NULL;
1457 t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
1458 if (!t->sysctl_header)
1472 static void devinet_sysctl_unregister(struct ipv4_devconf *p)
1475 struct devinet_sysctl_table *t = p->sysctl;
1477 unregister_sysctl_table(t->sysctl_header);
1478 kfree(t->devinet_dev[0].procname);
1484 void __init devinet_init(void)
1486 register_gifconf(PF_INET, inet_gifconf);
1487 register_netdevice_notifier(&ip_netdev_notifier);
1488 rtnetlink_links[PF_INET] = inet_rtnetlink_table;
1489 #ifdef CONFIG_SYSCTL
1490 devinet_sysctl.sysctl_header =
1491 register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
1492 devinet_sysctl_register(NULL, &ipv4_devconf_dflt);
1496 EXPORT_SYMBOL(devinet_ioctl);
1497 EXPORT_SYMBOL(in_dev_finish_destroy);
1498 EXPORT_SYMBOL(inet_select_addr);
1499 EXPORT_SYMBOL(inetdev_by_index);
1500 EXPORT_SYMBOL(register_inetaddr_notifier);
1501 EXPORT_SYMBOL(unregister_inetaddr_notifier);