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 <linux/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 */
161 rcu_assign_pointer(dev->ip_ptr, in_dev);
164 devinet_sysctl_register(in_dev, &in_dev->cnf);
166 ip_mc_init_dev(in_dev);
167 if (dev->flags & IFF_UP)
177 static void in_dev_rcu_put(struct rcu_head *head)
179 struct in_device *idev = container_of(head, struct in_device, rcu_head);
183 static void inetdev_destroy(struct in_device *in_dev)
185 struct in_ifaddr *ifa;
186 struct net_device *dev;
192 ip_mc_destroy_dev(in_dev);
194 while ((ifa = in_dev->ifa_list) != NULL) {
195 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
200 devinet_sysctl_unregister(&in_dev->cnf);
207 neigh_sysctl_unregister(in_dev->arp_parms);
209 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
212 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
215 int inet_addr_onlink(struct in_device *in_dev, u32 a, u32 b)
218 for_primary_ifa(in_dev) {
219 if (inet_ifa_match(a, ifa)) {
220 if (!b || inet_ifa_match(b, ifa)) {
225 } endfor_ifa(in_dev);
230 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
233 struct in_ifaddr *ifa1 = *ifap;
237 /* 1. Deleting primary ifaddr forces deletion all secondaries */
239 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
240 struct in_ifaddr *ifa;
241 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
243 while ((ifa = *ifap1) != NULL) {
244 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
245 ifa1->ifa_mask != ifa->ifa_mask ||
246 !inet_ifa_match(ifa1->ifa_address, ifa)) {
247 ifap1 = &ifa->ifa_next;
251 *ifap1 = ifa->ifa_next;
253 rtmsg_ifa(RTM_DELADDR, ifa);
254 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa);
261 *ifap = ifa1->ifa_next;
263 /* 3. Announce address deletion */
265 /* Send message first, then call notifier.
266 At first sight, FIB update triggered by notifier
267 will refer to already deleted ifaddr, that could confuse
268 netlink listeners. It is not true: look, gated sees
269 that route deleted and if it still thinks that ifaddr
270 is valid, it will try to restore deleted routes... Grr.
271 So that, this order is correct.
273 rtmsg_ifa(RTM_DELADDR, ifa1);
274 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
278 if (!in_dev->ifa_list)
279 inetdev_destroy(in_dev);
283 static int inet_insert_ifa(struct in_ifaddr *ifa)
285 struct in_device *in_dev = ifa->ifa_dev;
286 struct in_ifaddr *ifa1, **ifap, **last_primary;
290 if (!ifa->ifa_local) {
295 ifa->ifa_flags &= ~IFA_F_SECONDARY;
296 last_primary = &in_dev->ifa_list;
298 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
299 ifap = &ifa1->ifa_next) {
300 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
301 ifa->ifa_scope <= ifa1->ifa_scope)
302 last_primary = &ifa1->ifa_next;
303 if (ifa1->ifa_mask == ifa->ifa_mask &&
304 inet_ifa_match(ifa1->ifa_address, ifa)) {
305 if (ifa1->ifa_local == ifa->ifa_local) {
309 if (ifa1->ifa_scope != ifa->ifa_scope) {
313 ifa->ifa_flags |= IFA_F_SECONDARY;
317 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
318 net_srandom(ifa->ifa_local);
322 ifa->ifa_next = *ifap;
325 /* Send message first, then call notifier.
326 Notifier will trigger FIB update, so that
327 listeners of netlink will know about new ifaddr */
328 rtmsg_ifa(RTM_NEWADDR, ifa);
329 notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
334 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
336 struct in_device *in_dev = __in_dev_get(dev);
341 in_dev = inetdev_init(dev);
347 if (ifa->ifa_dev != in_dev) {
348 BUG_TRAP(!ifa->ifa_dev);
350 ifa->ifa_dev = in_dev;
352 if (LOOPBACK(ifa->ifa_local))
353 ifa->ifa_scope = RT_SCOPE_HOST;
354 return inet_insert_ifa(ifa);
357 struct in_device *inetdev_by_index(int ifindex)
359 struct net_device *dev;
360 struct in_device *in_dev = NULL;
361 read_lock(&dev_base_lock);
362 dev = __dev_get_by_index(ifindex);
364 in_dev = in_dev_get(dev);
365 read_unlock(&dev_base_lock);
369 /* Called only from RTNL semaphored context. No locks. */
371 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, u32 prefix,
376 for_primary_ifa(in_dev) {
377 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
379 } endfor_ifa(in_dev);
383 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
385 struct rtattr **rta = arg;
386 struct in_device *in_dev;
387 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
388 struct in_ifaddr *ifa, **ifap;
392 if ((in_dev = inetdev_by_index(ifm->ifa_index)) == NULL)
394 __in_dev_put(in_dev);
396 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
397 ifap = &ifa->ifa_next) {
398 if ((rta[IFA_LOCAL - 1] &&
399 memcmp(RTA_DATA(rta[IFA_LOCAL - 1]),
400 &ifa->ifa_local, 4)) ||
401 (rta[IFA_LABEL - 1] &&
402 rtattr_strcmp(rta[IFA_LABEL - 1], ifa->ifa_label)) ||
403 (rta[IFA_ADDRESS - 1] &&
404 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
405 !inet_ifa_match(*(u32*)RTA_DATA(rta[IFA_ADDRESS - 1]),
408 inet_del_ifa(in_dev, ifap, 1);
412 return -EADDRNOTAVAIL;
415 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
417 struct rtattr **rta = arg;
418 struct net_device *dev;
419 struct in_device *in_dev;
420 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
421 struct in_ifaddr *ifa;
426 if (ifm->ifa_prefixlen > 32 || !rta[IFA_LOCAL - 1])
430 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
434 if ((in_dev = __in_dev_get(dev)) == NULL) {
435 in_dev = inetdev_init(dev);
440 if ((ifa = inet_alloc_ifa()) == NULL)
443 if (!rta[IFA_ADDRESS - 1])
444 rta[IFA_ADDRESS - 1] = rta[IFA_LOCAL - 1];
445 memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL - 1]), 4);
446 memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS - 1]), 4);
447 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
448 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
449 if (rta[IFA_BROADCAST - 1])
450 memcpy(&ifa->ifa_broadcast,
451 RTA_DATA(rta[IFA_BROADCAST - 1]), 4);
452 if (rta[IFA_ANYCAST - 1])
453 memcpy(&ifa->ifa_anycast, RTA_DATA(rta[IFA_ANYCAST - 1]), 4);
454 ifa->ifa_flags = ifm->ifa_flags;
455 ifa->ifa_scope = ifm->ifa_scope;
457 ifa->ifa_dev = in_dev;
458 if (rta[IFA_LABEL - 1])
459 rtattr_strlcpy(ifa->ifa_label, rta[IFA_LABEL - 1], IFNAMSIZ);
461 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
463 rc = inet_insert_ifa(ifa);
469 * Determine a default network mask, based on the IP address.
472 static __inline__ int inet_abc_len(u32 addr)
474 int rc = -1; /* Something else, probably a multicast. */
483 else if (IN_CLASSB(addr))
485 else if (IN_CLASSC(addr))
493 Check that a device is not member of the ipv4root assigned to the process
494 Return true if this is the case
496 If the process is not bound to specific IP, then it returns 0 (all
499 static inline int devinet_notiproot (struct in_ifaddr *ifa)
504 if ((nxi = current->nx_info)) {
506 int nbip = nxi->nbipv4;
507 __u32 addr = ifa->ifa_local;
509 for (i=0; i<nbip; i++) {
510 if(nxi->ipv4[i] == addr) {
520 int devinet_ioctl(unsigned int cmd, void __user *arg)
523 struct sockaddr_in sin_orig;
524 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
525 struct in_device *in_dev;
526 struct in_ifaddr **ifap = NULL;
527 struct in_ifaddr *ifa = NULL;
528 struct net_device *dev;
531 int tryaddrmatch = 0;
534 * Fetch the caller's info block into kernel space
537 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
539 ifr.ifr_name[IFNAMSIZ - 1] = 0;
541 /* save original address for comparison */
542 memcpy(&sin_orig, sin, sizeof(*sin));
544 colon = strchr(ifr.ifr_name, ':');
549 dev_load(ifr.ifr_name);
553 case SIOCGIFADDR: /* Get interface address */
554 case SIOCGIFBRDADDR: /* Get the broadcast address */
555 case SIOCGIFDSTADDR: /* Get the destination address */
556 case SIOCGIFNETMASK: /* Get the netmask for the interface */
557 /* Note that these ioctls will not sleep,
558 so that we do not impose a lock.
559 One day we will be forced to put shlock here (I mean SMP)
561 tryaddrmatch = (sin_orig.sin_family == AF_INET);
562 memset(sin, 0, sizeof(*sin));
563 sin->sin_family = AF_INET;
568 if (!capable(CAP_NET_ADMIN))
571 case SIOCSIFADDR: /* Set interface address (and family) */
572 case SIOCSIFBRDADDR: /* Set the broadcast address */
573 case SIOCSIFDSTADDR: /* Set the destination address */
574 case SIOCSIFNETMASK: /* Set the netmask for the interface */
576 if (!capable(CAP_NET_ADMIN))
579 if (sin->sin_family != AF_INET)
590 if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL)
596 if ((in_dev = __in_dev_get(dev)) != NULL) {
598 /* Matthias Andree */
599 /* compare label and address (4.4BSD style) */
600 /* note: we only do this for a limited set of ioctls
601 and only if the original address family was AF_INET.
602 This is checked above. */
603 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
604 ifap = &ifa->ifa_next) {
605 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
606 sin_orig.sin_addr.s_addr ==
612 /* we didn't get a match, maybe the application is
613 4.3BSD-style and passed in junk so we fall back to
614 comparing just the label */
616 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
617 ifap = &ifa->ifa_next)
618 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
623 ret = -EADDRNOTAVAIL;
624 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
626 if (vx_flags(VXF_HIDE_NETIF, 0) &&
627 !ifa_in_nx_info(ifa, current->nx_info))
631 case SIOCGIFADDR: /* Get interface address */
632 sin->sin_addr.s_addr = ifa->ifa_local;
635 case SIOCGIFBRDADDR: /* Get the broadcast address */
636 sin->sin_addr.s_addr = ifa->ifa_broadcast;
639 case SIOCGIFDSTADDR: /* Get the destination address */
640 sin->sin_addr.s_addr = ifa->ifa_address;
643 case SIOCGIFNETMASK: /* Get the netmask for the interface */
644 sin->sin_addr.s_addr = ifa->ifa_mask;
649 ret = -EADDRNOTAVAIL;
653 if (!(ifr.ifr_flags & IFF_UP))
654 inet_del_ifa(in_dev, ifap, 1);
657 ret = dev_change_flags(dev, ifr.ifr_flags);
660 case SIOCSIFADDR: /* Set interface address (and family) */
662 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
667 if ((ifa = inet_alloc_ifa()) == NULL)
670 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
672 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
675 if (ifa->ifa_local == sin->sin_addr.s_addr)
677 inet_del_ifa(in_dev, ifap, 0);
678 ifa->ifa_broadcast = 0;
679 ifa->ifa_anycast = 0;
682 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
684 if (!(dev->flags & IFF_POINTOPOINT)) {
685 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
686 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
687 if ((dev->flags & IFF_BROADCAST) &&
688 ifa->ifa_prefixlen < 31)
689 ifa->ifa_broadcast = ifa->ifa_address |
692 ifa->ifa_prefixlen = 32;
693 ifa->ifa_mask = inet_make_mask(32);
695 ret = inet_set_ifa(dev, ifa);
698 case SIOCSIFBRDADDR: /* Set the broadcast address */
700 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
701 inet_del_ifa(in_dev, ifap, 0);
702 ifa->ifa_broadcast = sin->sin_addr.s_addr;
703 inet_insert_ifa(ifa);
707 case SIOCSIFDSTADDR: /* Set the destination address */
709 if (ifa->ifa_address == sin->sin_addr.s_addr)
712 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
715 inet_del_ifa(in_dev, ifap, 0);
716 ifa->ifa_address = sin->sin_addr.s_addr;
717 inet_insert_ifa(ifa);
720 case SIOCSIFNETMASK: /* Set the netmask for the interface */
723 * The mask we set must be legal.
726 if (bad_mask(sin->sin_addr.s_addr, 0))
729 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
730 inet_del_ifa(in_dev, ifap, 0);
731 ifa->ifa_mask = sin->sin_addr.s_addr;
732 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
734 /* See if current broadcast address matches
735 * with current netmask, then recalculate
736 * the broadcast address. Otherwise it's a
737 * funny address, so don't touch it since
738 * the user seems to know what (s)he's doing...
740 if ((dev->flags & IFF_BROADCAST) &&
741 (ifa->ifa_prefixlen < 31) &&
742 (ifa->ifa_broadcast ==
743 (ifa->ifa_local|~ifa->ifa_mask))) {
744 ifa->ifa_broadcast = (ifa->ifa_local |
745 ~sin->sin_addr.s_addr);
747 inet_insert_ifa(ifa);
757 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
761 static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
763 struct in_device *in_dev = __in_dev_get(dev);
764 struct in_ifaddr *ifa;
768 if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
771 for (; ifa; ifa = ifa->ifa_next) {
772 if (vx_flags(VXF_HIDE_NETIF, 0) &&
773 !ifa_in_nx_info(ifa, current->nx_info))
779 if (len < (int) sizeof(ifr))
781 memset(&ifr, 0, sizeof(struct ifreq));
783 strcpy(ifr.ifr_name, ifa->ifa_label);
785 strcpy(ifr.ifr_name, dev->name);
787 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
788 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
791 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
795 buf += sizeof(struct ifreq);
796 len -= sizeof(struct ifreq);
797 done += sizeof(struct ifreq);
803 u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
806 struct in_device *in_dev;
809 in_dev = __in_dev_get(dev);
813 for_primary_ifa(in_dev) {
814 if (ifa->ifa_scope > scope)
816 if (!dst || inet_ifa_match(dst, ifa)) {
817 addr = ifa->ifa_local;
821 addr = ifa->ifa_local;
822 } endfor_ifa(in_dev);
829 /* Not loopback addresses on loopback should be preferred
830 in this case. It is importnat that lo is the first interface
833 read_lock(&dev_base_lock);
835 for (dev = dev_base; dev; dev = dev->next) {
836 if ((in_dev = __in_dev_get(dev)) == NULL)
839 for_primary_ifa(in_dev) {
840 if (ifa->ifa_scope != RT_SCOPE_LINK &&
841 ifa->ifa_scope <= scope) {
842 addr = ifa->ifa_local;
843 goto out_unlock_both;
845 } endfor_ifa(in_dev);
848 read_unlock(&dev_base_lock);
854 static u32 confirm_addr_indev(struct in_device *in_dev, u32 dst,
855 u32 local, int scope)
862 (local == ifa->ifa_local || !local) &&
863 ifa->ifa_scope <= scope) {
864 addr = ifa->ifa_local;
869 same = (!local || inet_ifa_match(local, ifa)) &&
870 (!dst || inet_ifa_match(dst, ifa));
874 /* Is the selected addr into dst subnet? */
875 if (inet_ifa_match(addr, ifa))
877 /* No, then can we use new local src? */
878 if (ifa->ifa_scope <= scope) {
879 addr = ifa->ifa_local;
882 /* search for large dst subnet for addr */
886 } endfor_ifa(in_dev);
888 return same? addr : 0;
892 * Confirm that local IP address exists using wildcards:
893 * - dev: only on this interface, 0=any interface
894 * - dst: only in the same subnet as dst, 0=any dst
895 * - local: address, 0=autoselect the local address
896 * - scope: maximum allowed scope value for the local address
898 u32 inet_confirm_addr(const struct net_device *dev, u32 dst, u32 local, int scope)
901 struct in_device *in_dev;
905 if ((in_dev = __in_dev_get(dev)))
906 addr = confirm_addr_indev(in_dev, dst, local, scope);
912 read_lock(&dev_base_lock);
914 for (dev = dev_base; dev; dev = dev->next) {
915 if ((in_dev = __in_dev_get(dev))) {
916 addr = confirm_addr_indev(in_dev, dst, local, scope);
922 read_unlock(&dev_base_lock);
931 int register_inetaddr_notifier(struct notifier_block *nb)
933 return notifier_chain_register(&inetaddr_chain, nb);
936 int unregister_inetaddr_notifier(struct notifier_block *nb)
938 return notifier_chain_unregister(&inetaddr_chain, nb);
941 /* Rename ifa_labels for a device name change. Make some effort to preserve existing
942 * alias numbering and to create unique labels if possible.
944 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
946 struct in_ifaddr *ifa;
949 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
950 char old[IFNAMSIZ], *dot;
952 memcpy(old, ifa->ifa_label, IFNAMSIZ);
953 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
956 dot = strchr(ifa->ifa_label, ':');
958 sprintf(old, ":%d", named);
961 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) {
962 strcat(ifa->ifa_label, dot);
964 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
969 /* Called only under RTNL semaphore */
971 static int inetdev_event(struct notifier_block *this, unsigned long event,
974 struct net_device *dev = ptr;
975 struct in_device *in_dev = __in_dev_get(dev);
983 case NETDEV_REGISTER:
984 printk(KERN_DEBUG "inetdev_event: bug\n");
990 if (dev == &loopback_dev) {
991 struct in_ifaddr *ifa;
992 if ((ifa = inet_alloc_ifa()) != NULL) {
994 ifa->ifa_address = htonl(INADDR_LOOPBACK);
995 ifa->ifa_prefixlen = 8;
996 ifa->ifa_mask = inet_make_mask(8);
998 ifa->ifa_dev = in_dev;
999 ifa->ifa_scope = RT_SCOPE_HOST;
1000 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1001 inet_insert_ifa(ifa);
1003 in_dev->cnf.no_xfrm = 1;
1004 in_dev->cnf.no_policy = 1;
1011 case NETDEV_CHANGEMTU:
1014 /* MTU falled under 68, disable IP */
1015 case NETDEV_UNREGISTER:
1016 inetdev_destroy(in_dev);
1018 case NETDEV_CHANGENAME:
1019 /* Do not notify about label change, this event is
1020 * not interesting to applications using netlink.
1022 inetdev_changename(dev, in_dev);
1024 #ifdef CONFIG_SYSCTL
1025 devinet_sysctl_unregister(&in_dev->cnf);
1026 neigh_sysctl_unregister(in_dev->arp_parms);
1027 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
1028 NET_IPV4_NEIGH, "ipv4", NULL);
1029 devinet_sysctl_register(in_dev, &in_dev->cnf);
1037 static struct notifier_block ip_netdev_notifier = {
1038 .notifier_call =inetdev_event,
1041 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1042 u32 pid, u32 seq, int event)
1044 struct ifaddrmsg *ifm;
1045 struct nlmsghdr *nlh;
1046 unsigned char *b = skb->tail;
1048 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
1049 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
1050 ifm = NLMSG_DATA(nlh);
1051 ifm->ifa_family = AF_INET;
1052 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1053 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1054 ifm->ifa_scope = ifa->ifa_scope;
1055 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1056 if (ifa->ifa_address)
1057 RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
1059 RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
1060 if (ifa->ifa_broadcast)
1061 RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
1062 if (ifa->ifa_anycast)
1063 RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
1064 if (ifa->ifa_label[0])
1065 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
1066 nlh->nlmsg_len = skb->tail - b;
1071 skb_trim(skb, b - skb->data);
1075 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1078 struct net_device *dev;
1079 struct in_device *in_dev;
1080 struct in_ifaddr *ifa;
1081 struct sock *sk = skb->sk;
1082 int s_ip_idx, s_idx = cb->args[0];
1084 s_ip_idx = ip_idx = cb->args[1];
1085 read_lock(&dev_base_lock);
1086 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
1092 if ((in_dev = __in_dev_get(dev)) == NULL) {
1097 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1098 ifa = ifa->ifa_next, ip_idx++) {
1099 if (sk && vx_info_flags(sk->sk_vx_info, VXF_HIDE_NETIF, 0) &&
1100 !ifa_in_nx_info(ifa, sk->sk_nx_info))
1102 if (ip_idx < s_ip_idx)
1104 if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1106 RTM_NEWADDR) <= 0) {
1115 read_unlock(&dev_base_lock);
1117 cb->args[1] = ip_idx;
1122 static void rtmsg_ifa(int event, struct in_ifaddr* ifa)
1124 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + 128);
1125 struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);
1128 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, ENOBUFS);
1129 else if (inet_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
1131 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, EINVAL);
1133 NETLINK_CB(skb).dst_groups = RTMGRP_IPV4_IFADDR;
1134 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV4_IFADDR, GFP_KERNEL);
1138 static struct rtnetlink_link inet_rtnetlink_table[RTM_MAX - RTM_BASE + 1] = {
1139 [4] = { .doit = inet_rtm_newaddr, },
1140 [5] = { .doit = inet_rtm_deladdr, },
1141 [6] = { .dumpit = inet_dump_ifaddr, },
1142 [8] = { .doit = inet_rtm_newroute, },
1143 [9] = { .doit = inet_rtm_delroute, },
1144 [10] = { .doit = inet_rtm_getroute, .dumpit = inet_dump_fib, },
1145 #ifdef CONFIG_IP_MULTIPLE_TABLES
1146 [16] = { .doit = inet_rtm_newrule, },
1147 [17] = { .doit = inet_rtm_delrule, },
1148 [18] = { .dumpit = inet_dump_rules, },
1152 #ifdef CONFIG_SYSCTL
1154 void inet_forward_change(void)
1156 struct net_device *dev;
1157 int on = ipv4_devconf.forwarding;
1159 ipv4_devconf.accept_redirects = !on;
1160 ipv4_devconf_dflt.forwarding = on;
1162 read_lock(&dev_base_lock);
1163 for (dev = dev_base; dev; dev = dev->next) {
1164 struct in_device *in_dev;
1166 in_dev = __in_dev_get(dev);
1168 in_dev->cnf.forwarding = on;
1171 read_unlock(&dev_base_lock);
1176 static int devinet_sysctl_forward(ctl_table *ctl, int write,
1177 struct file* filp, void __user *buffer,
1178 size_t *lenp, loff_t *ppos)
1180 int *valp = ctl->data;
1182 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1184 if (write && *valp != val) {
1185 if (valp == &ipv4_devconf.forwarding)
1186 inet_forward_change();
1187 else if (valp != &ipv4_devconf_dflt.forwarding)
1194 int ipv4_doint_and_flush(ctl_table *ctl, int write,
1195 struct file* filp, void __user *buffer,
1196 size_t *lenp, loff_t *ppos)
1198 int *valp = ctl->data;
1200 int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1202 if (write && *valp != val)
1208 int ipv4_doint_and_flush_strategy(ctl_table *table, int __user *name, int nlen,
1209 void __user *oldval, size_t __user *oldlenp,
1210 void __user *newval, size_t newlen,
1213 int *valp = table->data;
1216 if (!newval || !newlen)
1219 if (newlen != sizeof(int))
1222 if (get_user(new, (int __user *)newval))
1228 if (oldval && oldlenp) {
1231 if (get_user(len, oldlenp))
1235 if (len > table->maxlen)
1236 len = table->maxlen;
1237 if (copy_to_user(oldval, valp, len))
1239 if (put_user(len, oldlenp))
1250 static struct devinet_sysctl_table {
1251 struct ctl_table_header *sysctl_header;
1252 ctl_table devinet_vars[20];
1253 ctl_table devinet_dev[2];
1254 ctl_table devinet_conf_dir[2];
1255 ctl_table devinet_proto_dir[2];
1256 ctl_table devinet_root_dir[2];
1257 } devinet_sysctl = {
1260 .ctl_name = NET_IPV4_CONF_FORWARDING,
1261 .procname = "forwarding",
1262 .data = &ipv4_devconf.forwarding,
1263 .maxlen = sizeof(int),
1265 .proc_handler = &devinet_sysctl_forward,
1268 .ctl_name = NET_IPV4_CONF_MC_FORWARDING,
1269 .procname = "mc_forwarding",
1270 .data = &ipv4_devconf.mc_forwarding,
1271 .maxlen = sizeof(int),
1273 .proc_handler = &proc_dointvec,
1276 .ctl_name = NET_IPV4_CONF_ACCEPT_REDIRECTS,
1277 .procname = "accept_redirects",
1278 .data = &ipv4_devconf.accept_redirects,
1279 .maxlen = sizeof(int),
1281 .proc_handler = &proc_dointvec,
1284 .ctl_name = NET_IPV4_CONF_SECURE_REDIRECTS,
1285 .procname = "secure_redirects",
1286 .data = &ipv4_devconf.secure_redirects,
1287 .maxlen = sizeof(int),
1289 .proc_handler = &proc_dointvec,
1292 .ctl_name = NET_IPV4_CONF_SHARED_MEDIA,
1293 .procname = "shared_media",
1294 .data = &ipv4_devconf.shared_media,
1295 .maxlen = sizeof(int),
1297 .proc_handler = &proc_dointvec,
1300 .ctl_name = NET_IPV4_CONF_RP_FILTER,
1301 .procname = "rp_filter",
1302 .data = &ipv4_devconf.rp_filter,
1303 .maxlen = sizeof(int),
1305 .proc_handler = &proc_dointvec,
1308 .ctl_name = NET_IPV4_CONF_SEND_REDIRECTS,
1309 .procname = "send_redirects",
1310 .data = &ipv4_devconf.send_redirects,
1311 .maxlen = sizeof(int),
1313 .proc_handler = &proc_dointvec,
1316 .ctl_name = NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE,
1317 .procname = "accept_source_route",
1318 .data = &ipv4_devconf.accept_source_route,
1319 .maxlen = sizeof(int),
1321 .proc_handler = &proc_dointvec,
1324 .ctl_name = NET_IPV4_CONF_PROXY_ARP,
1325 .procname = "proxy_arp",
1326 .data = &ipv4_devconf.proxy_arp,
1327 .maxlen = sizeof(int),
1329 .proc_handler = &proc_dointvec,
1332 .ctl_name = NET_IPV4_CONF_MEDIUM_ID,
1333 .procname = "medium_id",
1334 .data = &ipv4_devconf.medium_id,
1335 .maxlen = sizeof(int),
1337 .proc_handler = &proc_dointvec,
1340 .ctl_name = NET_IPV4_CONF_BOOTP_RELAY,
1341 .procname = "bootp_relay",
1342 .data = &ipv4_devconf.bootp_relay,
1343 .maxlen = sizeof(int),
1345 .proc_handler = &proc_dointvec,
1348 .ctl_name = NET_IPV4_CONF_LOG_MARTIANS,
1349 .procname = "log_martians",
1350 .data = &ipv4_devconf.log_martians,
1351 .maxlen = sizeof(int),
1353 .proc_handler = &proc_dointvec,
1356 .ctl_name = NET_IPV4_CONF_TAG,
1358 .data = &ipv4_devconf.tag,
1359 .maxlen = sizeof(int),
1361 .proc_handler = &proc_dointvec,
1364 .ctl_name = NET_IPV4_CONF_ARPFILTER,
1365 .procname = "arp_filter",
1366 .data = &ipv4_devconf.arp_filter,
1367 .maxlen = sizeof(int),
1369 .proc_handler = &proc_dointvec,
1372 .ctl_name = NET_IPV4_CONF_ARP_ANNOUNCE,
1373 .procname = "arp_announce",
1374 .data = &ipv4_devconf.arp_announce,
1375 .maxlen = sizeof(int),
1377 .proc_handler = &proc_dointvec,
1380 .ctl_name = NET_IPV4_CONF_ARP_IGNORE,
1381 .procname = "arp_ignore",
1382 .data = &ipv4_devconf.arp_ignore,
1383 .maxlen = sizeof(int),
1385 .proc_handler = &proc_dointvec,
1388 .ctl_name = NET_IPV4_CONF_NOXFRM,
1389 .procname = "disable_xfrm",
1390 .data = &ipv4_devconf.no_xfrm,
1391 .maxlen = sizeof(int),
1393 .proc_handler = &ipv4_doint_and_flush,
1394 .strategy = &ipv4_doint_and_flush_strategy,
1397 .ctl_name = NET_IPV4_CONF_NOPOLICY,
1398 .procname = "disable_policy",
1399 .data = &ipv4_devconf.no_policy,
1400 .maxlen = sizeof(int),
1402 .proc_handler = &ipv4_doint_and_flush,
1403 .strategy = &ipv4_doint_and_flush_strategy,
1406 .ctl_name = NET_IPV4_CONF_FORCE_IGMP_VERSION,
1407 .procname = "force_igmp_version",
1408 .data = &ipv4_devconf.force_igmp_version,
1409 .maxlen = sizeof(int),
1411 .proc_handler = &ipv4_doint_and_flush,
1412 .strategy = &ipv4_doint_and_flush_strategy,
1417 .ctl_name = NET_PROTO_CONF_ALL,
1420 .child = devinet_sysctl.devinet_vars,
1423 .devinet_conf_dir = {
1425 .ctl_name = NET_IPV4_CONF,
1428 .child = devinet_sysctl.devinet_dev,
1431 .devinet_proto_dir = {
1433 .ctl_name = NET_IPV4,
1436 .child = devinet_sysctl.devinet_conf_dir,
1439 .devinet_root_dir = {
1441 .ctl_name = CTL_NET,
1444 .child = devinet_sysctl.devinet_proto_dir,
1449 static void devinet_sysctl_register(struct in_device *in_dev,
1450 struct ipv4_devconf *p)
1453 struct net_device *dev = in_dev ? in_dev->dev : NULL;
1454 struct devinet_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
1455 char *dev_name = NULL;
1459 memcpy(t, &devinet_sysctl, sizeof(*t));
1460 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1461 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
1462 t->devinet_vars[i].de = NULL;
1466 dev_name = dev->name;
1467 t->devinet_dev[0].ctl_name = dev->ifindex;
1469 dev_name = "default";
1470 t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
1474 * Make a copy of dev_name, because '.procname' is regarded as const
1475 * by sysctl and we wouldn't want anyone to change it under our feet
1476 * (see SIOCSIFNAME).
1478 dev_name = net_sysctl_strdup(dev_name);
1482 t->devinet_dev[0].procname = dev_name;
1483 t->devinet_dev[0].child = t->devinet_vars;
1484 t->devinet_dev[0].de = NULL;
1485 t->devinet_conf_dir[0].child = t->devinet_dev;
1486 t->devinet_conf_dir[0].de = NULL;
1487 t->devinet_proto_dir[0].child = t->devinet_conf_dir;
1488 t->devinet_proto_dir[0].de = NULL;
1489 t->devinet_root_dir[0].child = t->devinet_proto_dir;
1490 t->devinet_root_dir[0].de = NULL;
1492 t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
1493 if (!t->sysctl_header)
1507 static void devinet_sysctl_unregister(struct ipv4_devconf *p)
1510 struct devinet_sysctl_table *t = p->sysctl;
1512 unregister_sysctl_table(t->sysctl_header);
1513 kfree(t->devinet_dev[0].procname);
1519 void __init devinet_init(void)
1521 register_gifconf(PF_INET, inet_gifconf);
1522 register_netdevice_notifier(&ip_netdev_notifier);
1523 rtnetlink_links[PF_INET] = inet_rtnetlink_table;
1524 #ifdef CONFIG_SYSCTL
1525 devinet_sysctl.sysctl_header =
1526 register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
1527 devinet_sysctl_register(NULL, &ipv4_devconf_dflt);
1531 EXPORT_SYMBOL(devinet_ioctl);
1532 EXPORT_SYMBOL(in_dev_finish_destroy);
1533 EXPORT_SYMBOL(inet_select_addr);
1534 EXPORT_SYMBOL(inetdev_by_index);
1535 EXPORT_SYMBOL(register_inetaddr_notifier);
1536 EXPORT_SYMBOL(unregister_inetaddr_notifier);