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);
94 /* Locks all the inet devices. */
96 rwlock_t inetdev_lock = RW_LOCK_UNLOCKED;
98 static struct in_ifaddr *inet_alloc_ifa(void)
100 struct in_ifaddr *ifa = kmalloc(sizeof(*ifa), GFP_KERNEL);
103 memset(ifa, 0, sizeof(*ifa));
110 static __inline__ void inet_free_ifa(struct in_ifaddr *ifa)
113 __in_dev_put(ifa->ifa_dev);
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);
137 struct in_device *inetdev_init(struct net_device *dev)
139 struct in_device *in_dev;
143 in_dev = kmalloc(sizeof(*in_dev), GFP_KERNEL);
146 memset(in_dev, 0, sizeof(*in_dev));
147 in_dev->lock = RW_LOCK_UNLOCKED;
148 memcpy(&in_dev->cnf, &ipv4_devconf_dflt, sizeof(in_dev->cnf));
149 in_dev->cnf.sysctl = NULL;
151 if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL)
154 /* Reference in_dev->dev */
157 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
158 NET_IPV4_NEIGH, "ipv4", NULL);
160 write_lock_bh(&inetdev_lock);
161 dev->ip_ptr = in_dev;
162 /* Account for reference dev->ip_ptr */
164 write_unlock_bh(&inetdev_lock);
166 devinet_sysctl_register(in_dev, &in_dev->cnf);
168 ip_mc_init_dev(in_dev);
169 if (dev->flags & IFF_UP)
179 static void inetdev_destroy(struct in_device *in_dev)
181 struct in_ifaddr *ifa;
187 ip_mc_destroy_dev(in_dev);
189 while ((ifa = in_dev->ifa_list) != NULL) {
190 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
195 devinet_sysctl_unregister(&in_dev->cnf);
197 write_lock_bh(&inetdev_lock);
198 in_dev->dev->ip_ptr = NULL;
199 /* in_dev_put following below will kill the in_device */
200 write_unlock_bh(&inetdev_lock);
203 neigh_sysctl_unregister(in_dev->arp_parms);
205 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
209 int inet_addr_onlink(struct in_device *in_dev, u32 a, u32 b)
211 read_lock(&in_dev->lock);
212 for_primary_ifa(in_dev) {
213 if (inet_ifa_match(a, ifa)) {
214 if (!b || inet_ifa_match(b, ifa)) {
215 read_unlock(&in_dev->lock);
219 } endfor_ifa(in_dev);
220 read_unlock(&in_dev->lock);
224 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
227 struct in_ifaddr *ifa1 = *ifap;
231 /* 1. Deleting primary ifaddr forces deletion all secondaries */
233 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
234 struct in_ifaddr *ifa;
235 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
237 while ((ifa = *ifap1) != NULL) {
238 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
239 ifa1->ifa_mask != ifa->ifa_mask ||
240 !inet_ifa_match(ifa1->ifa_address, ifa)) {
241 ifap1 = &ifa->ifa_next;
244 write_lock_bh(&in_dev->lock);
245 *ifap1 = ifa->ifa_next;
246 write_unlock_bh(&in_dev->lock);
248 rtmsg_ifa(RTM_DELADDR, ifa);
249 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa);
256 write_lock_bh(&in_dev->lock);
257 *ifap = ifa1->ifa_next;
258 write_unlock_bh(&in_dev->lock);
260 /* 3. Announce address deletion */
262 /* Send message first, then call notifier.
263 At first sight, FIB update triggered by notifier
264 will refer to already deleted ifaddr, that could confuse
265 netlink listeners. It is not true: look, gated sees
266 that route deleted and if it still thinks that ifaddr
267 is valid, it will try to restore deleted routes... Grr.
268 So that, this order is correct.
270 rtmsg_ifa(RTM_DELADDR, ifa1);
271 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
275 if (!in_dev->ifa_list)
276 inetdev_destroy(in_dev);
280 static int inet_insert_ifa(struct in_ifaddr *ifa)
282 struct in_device *in_dev = ifa->ifa_dev;
283 struct in_ifaddr *ifa1, **ifap, **last_primary;
287 if (!ifa->ifa_local) {
292 ifa->ifa_flags &= ~IFA_F_SECONDARY;
293 last_primary = &in_dev->ifa_list;
295 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
296 ifap = &ifa1->ifa_next) {
297 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
298 ifa->ifa_scope <= ifa1->ifa_scope)
299 last_primary = &ifa1->ifa_next;
300 if (ifa1->ifa_mask == ifa->ifa_mask &&
301 inet_ifa_match(ifa1->ifa_address, ifa)) {
302 if (ifa1->ifa_local == ifa->ifa_local) {
306 if (ifa1->ifa_scope != ifa->ifa_scope) {
310 ifa->ifa_flags |= IFA_F_SECONDARY;
314 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
315 net_srandom(ifa->ifa_local);
319 ifa->ifa_next = *ifap;
320 write_lock_bh(&in_dev->lock);
322 write_unlock_bh(&in_dev->lock);
324 /* Send message first, then call notifier.
325 Notifier will trigger FIB update, so that
326 listeners of netlink will know about new ifaddr */
327 rtmsg_ifa(RTM_NEWADDR, ifa);
328 notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
333 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
335 struct in_device *in_dev = __in_dev_get(dev);
340 in_dev = inetdev_init(dev);
346 if (ifa->ifa_dev != in_dev) {
347 BUG_TRAP(!ifa->ifa_dev);
349 ifa->ifa_dev = in_dev;
351 if (LOOPBACK(ifa->ifa_local))
352 ifa->ifa_scope = RT_SCOPE_HOST;
353 return inet_insert_ifa(ifa);
356 struct in_device *inetdev_by_index(int ifindex)
358 struct net_device *dev;
359 struct in_device *in_dev = NULL;
360 read_lock(&dev_base_lock);
361 dev = __dev_get_by_index(ifindex);
363 in_dev = in_dev_get(dev);
364 read_unlock(&dev_base_lock);
368 /* Called only from RTNL semaphored context. No locks. */
370 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, u32 prefix,
375 for_primary_ifa(in_dev) {
376 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
378 } endfor_ifa(in_dev);
382 int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
384 struct rtattr **rta = arg;
385 struct in_device *in_dev;
386 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
387 struct in_ifaddr *ifa, **ifap;
391 if ((in_dev = inetdev_by_index(ifm->ifa_index)) == NULL)
393 __in_dev_put(in_dev);
395 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
396 ifap = &ifa->ifa_next) {
397 if ((rta[IFA_LOCAL - 1] &&
398 memcmp(RTA_DATA(rta[IFA_LOCAL - 1]),
399 &ifa->ifa_local, 4)) ||
400 (rta[IFA_LABEL - 1] &&
401 strcmp(RTA_DATA(rta[IFA_LABEL - 1]), ifa->ifa_label)) ||
402 (rta[IFA_ADDRESS - 1] &&
403 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
404 !inet_ifa_match(*(u32*)RTA_DATA(rta[IFA_ADDRESS - 1]),
407 inet_del_ifa(in_dev, ifap, 1);
411 return -EADDRNOTAVAIL;
414 int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
416 struct rtattr **rta = arg;
417 struct net_device *dev;
418 struct in_device *in_dev;
419 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
420 struct in_ifaddr *ifa;
425 if (ifm->ifa_prefixlen > 32 || !rta[IFA_LOCAL - 1])
429 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
433 if ((in_dev = __in_dev_get(dev)) == NULL) {
434 in_dev = inetdev_init(dev);
439 if ((ifa = inet_alloc_ifa()) == NULL)
442 if (!rta[IFA_ADDRESS - 1])
443 rta[IFA_ADDRESS - 1] = rta[IFA_LOCAL - 1];
444 memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL - 1]), 4);
445 memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS - 1]), 4);
446 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
447 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
448 if (rta[IFA_BROADCAST - 1])
449 memcpy(&ifa->ifa_broadcast,
450 RTA_DATA(rta[IFA_BROADCAST - 1]), 4);
451 if (rta[IFA_ANYCAST - 1])
452 memcpy(&ifa->ifa_anycast, RTA_DATA(rta[IFA_ANYCAST - 1]), 4);
453 ifa->ifa_flags = ifm->ifa_flags;
454 ifa->ifa_scope = ifm->ifa_scope;
456 ifa->ifa_dev = in_dev;
457 if (rta[IFA_LABEL - 1])
458 memcpy(ifa->ifa_label, RTA_DATA(rta[IFA_LABEL - 1]), IFNAMSIZ);
460 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
462 rc = inet_insert_ifa(ifa);
468 * Determine a default network mask, based on the IP address.
471 static __inline__ int inet_abc_len(u32 addr)
473 int rc = -1; /* Something else, probably a multicast. */
482 else if (IN_CLASSB(addr))
484 else if (IN_CLASSC(addr))
492 Check that a device is not member of the ipv4root assigned to the process
493 Return true if this is the case
495 If the process is not bound to specific IP, then it returns 0 (all
498 static inline int devinet_notiproot (struct in_ifaddr *ifa)
503 if ((nxi = current->nx_info)) {
505 int nbip = nxi->nbipv4;
506 __u32 addr = ifa->ifa_local;
508 for (i=0; i<nbip; i++) {
509 if(nxi->ipv4[i] == addr) {
519 int devinet_ioctl(unsigned int cmd, void *arg)
522 struct sockaddr_in sin_orig;
523 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
524 struct in_device *in_dev;
525 struct in_ifaddr **ifap = NULL;
526 struct in_ifaddr *ifa = NULL;
527 struct net_device *dev;
530 int tryaddrmatch = 0;
533 * Fetch the caller's info block into kernel space
536 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
538 ifr.ifr_name[IFNAMSIZ - 1] = 0;
540 /* save original address for comparison */
541 memcpy(&sin_orig, sin, sizeof(*sin));
543 colon = strchr(ifr.ifr_name, ':');
548 dev_load(ifr.ifr_name);
552 case SIOCGIFADDR: /* Get interface address */
553 case SIOCGIFBRDADDR: /* Get the broadcast address */
554 case SIOCGIFDSTADDR: /* Get the destination address */
555 case SIOCGIFNETMASK: /* Get the netmask for the interface */
556 /* Note that these ioctls will not sleep,
557 so that we do not impose a lock.
558 One day we will be forced to put shlock here (I mean SMP)
560 tryaddrmatch = (sin_orig.sin_family == AF_INET);
561 memset(sin, 0, sizeof(*sin));
562 sin->sin_family = AF_INET;
567 if (!capable(CAP_NET_ADMIN))
570 case SIOCSIFADDR: /* Set interface address (and family) */
571 case SIOCSIFBRDADDR: /* Set the broadcast address */
572 case SIOCSIFDSTADDR: /* Set the destination address */
573 case SIOCSIFNETMASK: /* Set the netmask for the interface */
575 if (!capable(CAP_NET_ADMIN))
578 if (sin->sin_family != AF_INET)
589 if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL)
595 if ((in_dev = __in_dev_get(dev)) != NULL) {
597 /* Matthias Andree */
598 /* compare label and address (4.4BSD style) */
599 /* note: we only do this for a limited set of ioctls
600 and only if the original address family was AF_INET.
601 This is checked above. */
602 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
603 ifap = &ifa->ifa_next) {
604 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
605 sin_orig.sin_addr.s_addr ==
611 /* we didn't get a match, maybe the application is
612 4.3BSD-style and passed in junk so we fall back to
613 comparing just the label */
615 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
616 ifap = &ifa->ifa_next)
617 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
622 ret = -EADDRNOTAVAIL;
623 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
625 if (!ifa_in_nx_info(ifa, current->nx_info))
629 case SIOCGIFADDR: /* Get interface address */
630 sin->sin_addr.s_addr = ifa->ifa_local;
633 case SIOCGIFBRDADDR: /* Get the broadcast address */
634 sin->sin_addr.s_addr = ifa->ifa_broadcast;
637 case SIOCGIFDSTADDR: /* Get the destination address */
638 sin->sin_addr.s_addr = ifa->ifa_address;
641 case SIOCGIFNETMASK: /* Get the netmask for the interface */
642 sin->sin_addr.s_addr = ifa->ifa_mask;
647 ret = -EADDRNOTAVAIL;
651 if (!(ifr.ifr_flags & IFF_UP))
652 inet_del_ifa(in_dev, ifap, 1);
655 ret = dev_change_flags(dev, ifr.ifr_flags);
658 case SIOCSIFADDR: /* Set interface address (and family) */
660 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
665 if ((ifa = inet_alloc_ifa()) == NULL)
668 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
670 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
673 if (ifa->ifa_local == sin->sin_addr.s_addr)
675 inet_del_ifa(in_dev, ifap, 0);
676 ifa->ifa_broadcast = 0;
677 ifa->ifa_anycast = 0;
680 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
682 if (!(dev->flags & IFF_POINTOPOINT)) {
683 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
684 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
685 if ((dev->flags & IFF_BROADCAST) &&
686 ifa->ifa_prefixlen < 31)
687 ifa->ifa_broadcast = ifa->ifa_address |
690 ifa->ifa_prefixlen = 32;
691 ifa->ifa_mask = inet_make_mask(32);
693 ret = inet_set_ifa(dev, ifa);
696 case SIOCSIFBRDADDR: /* Set the broadcast address */
698 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
699 inet_del_ifa(in_dev, ifap, 0);
700 ifa->ifa_broadcast = sin->sin_addr.s_addr;
701 inet_insert_ifa(ifa);
705 case SIOCSIFDSTADDR: /* Set the destination address */
707 if (ifa->ifa_address == sin->sin_addr.s_addr)
710 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
713 inet_del_ifa(in_dev, ifap, 0);
714 ifa->ifa_address = sin->sin_addr.s_addr;
715 inet_insert_ifa(ifa);
718 case SIOCSIFNETMASK: /* Set the netmask for the interface */
721 * The mask we set must be legal.
724 if (bad_mask(sin->sin_addr.s_addr, 0))
727 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
728 inet_del_ifa(in_dev, ifap, 0);
729 ifa->ifa_mask = sin->sin_addr.s_addr;
730 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
731 inet_insert_ifa(ifa);
741 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
745 static int inet_gifconf(struct net_device *dev, char *buf, int len)
747 struct in_device *in_dev = __in_dev_get(dev);
748 struct in_ifaddr *ifa;
752 if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
755 for (; ifa; ifa = ifa->ifa_next) {
756 if (!ifa_in_nx_info(ifa, current->nx_info))
762 if (len < (int) sizeof(ifr))
764 memset(&ifr, 0, sizeof(struct ifreq));
766 strcpy(ifr.ifr_name, ifa->ifa_label);
768 strcpy(ifr.ifr_name, dev->name);
770 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
771 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
774 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
778 buf += sizeof(struct ifreq);
779 len -= sizeof(struct ifreq);
780 done += sizeof(struct ifreq);
786 u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
789 struct in_device *in_dev;
791 read_lock(&inetdev_lock);
792 in_dev = __in_dev_get(dev);
794 goto out_unlock_inetdev;
796 read_lock(&in_dev->lock);
797 for_primary_ifa(in_dev) {
798 if (ifa->ifa_scope > scope)
800 if (!dst || inet_ifa_match(dst, ifa)) {
801 addr = ifa->ifa_local;
805 addr = ifa->ifa_local;
806 } endfor_ifa(in_dev);
807 read_unlock(&in_dev->lock);
808 read_unlock(&inetdev_lock);
813 /* Not loopback addresses on loopback should be preferred
814 in this case. It is importnat that lo is the first interface
817 read_lock(&dev_base_lock);
818 read_lock(&inetdev_lock);
819 for (dev = dev_base; dev; dev = dev->next) {
820 if ((in_dev = __in_dev_get(dev)) == NULL)
823 read_lock(&in_dev->lock);
824 for_primary_ifa(in_dev) {
825 if (ifa->ifa_scope != RT_SCOPE_LINK &&
826 ifa->ifa_scope <= scope) {
827 read_unlock(&in_dev->lock);
828 addr = ifa->ifa_local;
829 goto out_unlock_both;
831 } endfor_ifa(in_dev);
832 read_unlock(&in_dev->lock);
835 read_unlock(&inetdev_lock);
836 read_unlock(&dev_base_lock);
840 read_unlock(&inetdev_lock);
844 static u32 confirm_addr_indev(struct in_device *in_dev, u32 dst,
845 u32 local, int scope)
852 (local == ifa->ifa_local || !local) &&
853 ifa->ifa_scope <= scope) {
854 addr = ifa->ifa_local;
859 same = (!local || inet_ifa_match(local, ifa)) &&
860 (!dst || inet_ifa_match(dst, ifa));
864 /* Is the selected addr into dst subnet? */
865 if (inet_ifa_match(addr, ifa))
867 /* No, then can we use new local src? */
868 if (ifa->ifa_scope <= scope) {
869 addr = ifa->ifa_local;
872 /* search for large dst subnet for addr */
876 } endfor_ifa(in_dev);
878 return same? addr : 0;
882 * Confirm that local IP address exists using wildcards:
883 * - dev: only on this interface, 0=any interface
884 * - dst: only in the same subnet as dst, 0=any dst
885 * - local: address, 0=autoselect the local address
886 * - scope: maximum allowed scope value for the local address
888 u32 inet_confirm_addr(const struct net_device *dev, u32 dst, u32 local, int scope)
891 struct in_device *in_dev;
894 read_lock(&inetdev_lock);
895 if ((in_dev = __in_dev_get(dev))) {
896 read_lock(&in_dev->lock);
897 addr = confirm_addr_indev(in_dev, dst, local, scope);
898 read_unlock(&in_dev->lock);
900 read_unlock(&inetdev_lock);
905 read_lock(&dev_base_lock);
906 read_lock(&inetdev_lock);
907 for (dev = dev_base; dev; dev = dev->next) {
908 if ((in_dev = __in_dev_get(dev))) {
909 read_lock(&in_dev->lock);
910 addr = confirm_addr_indev(in_dev, dst, local, scope);
911 read_unlock(&in_dev->lock);
916 read_unlock(&inetdev_lock);
917 read_unlock(&dev_base_lock);
926 int register_inetaddr_notifier(struct notifier_block *nb)
928 return notifier_chain_register(&inetaddr_chain, nb);
931 int unregister_inetaddr_notifier(struct notifier_block *nb)
933 return notifier_chain_unregister(&inetaddr_chain, nb);
936 /* Rename ifa_labels for a device name change. Make some effort to preserve existing
937 * alias numbering and to create unique labels if possible.
939 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
941 struct in_ifaddr *ifa;
944 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
945 char old[IFNAMSIZ], *dot;
947 memcpy(old, ifa->ifa_label, IFNAMSIZ);
948 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
951 dot = strchr(ifa->ifa_label, ':');
953 sprintf(old, ":%d", named);
956 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) {
957 strcat(ifa->ifa_label, dot);
959 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
964 /* Called only under RTNL semaphore */
966 static int inetdev_event(struct notifier_block *this, unsigned long event,
969 struct net_device *dev = ptr;
970 struct in_device *in_dev = __in_dev_get(dev);
978 case NETDEV_REGISTER:
979 printk(KERN_DEBUG "inetdev_event: bug\n");
985 if (dev == &loopback_dev) {
986 struct in_ifaddr *ifa;
987 if ((ifa = inet_alloc_ifa()) != NULL) {
989 ifa->ifa_address = htonl(INADDR_LOOPBACK);
990 ifa->ifa_prefixlen = 8;
991 ifa->ifa_mask = inet_make_mask(8);
993 ifa->ifa_dev = in_dev;
994 ifa->ifa_scope = RT_SCOPE_HOST;
995 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
996 inet_insert_ifa(ifa);
998 in_dev->cnf.no_xfrm = 1;
999 in_dev->cnf.no_policy = 1;
1006 case NETDEV_CHANGEMTU:
1009 /* MTU falled under 68, disable IP */
1010 case NETDEV_UNREGISTER:
1011 inetdev_destroy(in_dev);
1013 case NETDEV_CHANGENAME:
1014 /* Do not notify about label change, this event is
1015 * not interesting to applications using netlink.
1017 inetdev_changename(dev, in_dev);
1019 #ifdef CONFIG_SYSCTL
1020 devinet_sysctl_unregister(&in_dev->cnf);
1021 neigh_sysctl_unregister(in_dev->arp_parms);
1022 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
1023 NET_IPV4_NEIGH, "ipv4", NULL);
1024 devinet_sysctl_register(in_dev, &in_dev->cnf);
1032 static struct notifier_block ip_netdev_notifier = {
1033 .notifier_call =inetdev_event,
1036 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1037 u32 pid, u32 seq, int event)
1039 struct ifaddrmsg *ifm;
1040 struct nlmsghdr *nlh;
1041 unsigned char *b = skb->tail;
1043 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
1044 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
1045 ifm = NLMSG_DATA(nlh);
1046 ifm->ifa_family = AF_INET;
1047 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
1048 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
1049 ifm->ifa_scope = ifa->ifa_scope;
1050 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
1051 if (ifa->ifa_address)
1052 RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
1054 RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
1055 if (ifa->ifa_broadcast)
1056 RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
1057 if (ifa->ifa_anycast)
1058 RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
1059 if (ifa->ifa_label[0])
1060 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
1061 nlh->nlmsg_len = skb->tail - b;
1066 skb_trim(skb, b - skb->data);
1070 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1073 struct net_device *dev;
1074 struct in_device *in_dev;
1075 struct in_ifaddr *ifa;
1076 int s_ip_idx, s_idx = cb->args[0];
1078 s_ip_idx = ip_idx = cb->args[1];
1079 read_lock(&dev_base_lock);
1080 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
1085 read_lock(&inetdev_lock);
1086 if ((in_dev = __in_dev_get(dev)) == NULL) {
1087 read_unlock(&inetdev_lock);
1090 read_lock(&in_dev->lock);
1091 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1092 ifa = ifa->ifa_next, ip_idx++) {
1093 if (!ifa_in_nx_info(ifa, current->nx_info))
1095 if (ip_idx < s_ip_idx)
1097 if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1099 RTM_NEWADDR) <= 0) {
1100 read_unlock(&in_dev->lock);
1101 read_unlock(&inetdev_lock);
1105 read_unlock(&in_dev->lock);
1106 read_unlock(&inetdev_lock);
1110 read_unlock(&dev_base_lock);
1112 cb->args[1] = ip_idx;
1117 static void rtmsg_ifa(int event, struct in_ifaddr* ifa)
1119 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + 128);
1120 struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);
1123 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, ENOBUFS);
1124 else if (inet_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
1126 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, EINVAL);
1128 NETLINK_CB(skb).dst_groups = RTMGRP_IPV4_IFADDR;
1129 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV4_IFADDR, GFP_KERNEL);
1133 static struct rtnetlink_link inet_rtnetlink_table[RTM_MAX - RTM_BASE + 1] = {
1134 [4] = { .doit = inet_rtm_newaddr, },
1135 [5] = { .doit = inet_rtm_deladdr, },
1136 [6] = { .dumpit = inet_dump_ifaddr, },
1137 [8] = { .doit = inet_rtm_newroute, },
1138 [9] = { .doit = inet_rtm_delroute, },
1139 [10] = { .doit = inet_rtm_getroute, .dumpit = inet_dump_fib, },
1140 #ifdef CONFIG_IP_MULTIPLE_TABLES
1141 [16] = { .doit = inet_rtm_newrule, },
1142 [17] = { .doit = inet_rtm_delrule, },
1143 [18] = { .dumpit = inet_dump_rules, },
1147 #ifdef CONFIG_SYSCTL
1149 void inet_forward_change(void)
1151 struct net_device *dev;
1152 int on = ipv4_devconf.forwarding;
1154 ipv4_devconf.accept_redirects = !on;
1155 ipv4_devconf_dflt.forwarding = on;
1157 read_lock(&dev_base_lock);
1158 for (dev = dev_base; dev; dev = dev->next) {
1159 struct in_device *in_dev;
1160 read_lock(&inetdev_lock);
1161 in_dev = __in_dev_get(dev);
1163 in_dev->cnf.forwarding = on;
1164 read_unlock(&inetdev_lock);
1166 read_unlock(&dev_base_lock);
1171 static int devinet_sysctl_forward(ctl_table *ctl, int write,
1172 struct file* filp, void *buffer,
1175 int *valp = ctl->data;
1177 int ret = proc_dointvec(ctl, write, filp, buffer, lenp);
1179 if (write && *valp != val) {
1180 if (valp == &ipv4_devconf.forwarding)
1181 inet_forward_change();
1182 else if (valp != &ipv4_devconf_dflt.forwarding)
1189 int ipv4_doint_and_flush(ctl_table *ctl, int write,
1190 struct file* filp, void *buffer,
1193 int *valp = ctl->data;
1195 int ret = proc_dointvec(ctl, write, filp, buffer, lenp);
1197 if (write && *valp != val)
1203 int ipv4_doint_and_flush_strategy(ctl_table *table, int *name, int nlen,
1204 void *oldval, size_t *oldlenp,
1205 void *newval, size_t newlen,
1208 int *valp = table->data;
1211 if (!newval || !newlen)
1214 if (newlen != sizeof(int))
1217 if (get_user(new, (int *)newval))
1223 if (oldval && oldlenp) {
1226 if (get_user(len, oldlenp))
1230 if (len > table->maxlen)
1231 len = table->maxlen;
1232 if (copy_to_user(oldval, valp, len))
1234 if (put_user(len, oldlenp))
1245 static struct devinet_sysctl_table {
1246 struct ctl_table_header *sysctl_header;
1247 ctl_table devinet_vars[20];
1248 ctl_table devinet_dev[2];
1249 ctl_table devinet_conf_dir[2];
1250 ctl_table devinet_proto_dir[2];
1251 ctl_table devinet_root_dir[2];
1252 } devinet_sysctl = {
1255 .ctl_name = NET_IPV4_CONF_FORWARDING,
1256 .procname = "forwarding",
1257 .data = &ipv4_devconf.forwarding,
1258 .maxlen = sizeof(int),
1260 .proc_handler = &devinet_sysctl_forward,
1263 .ctl_name = NET_IPV4_CONF_MC_FORWARDING,
1264 .procname = "mc_forwarding",
1265 .data = &ipv4_devconf.mc_forwarding,
1266 .maxlen = sizeof(int),
1268 .proc_handler = &proc_dointvec,
1271 .ctl_name = NET_IPV4_CONF_ACCEPT_REDIRECTS,
1272 .procname = "accept_redirects",
1273 .data = &ipv4_devconf.accept_redirects,
1274 .maxlen = sizeof(int),
1276 .proc_handler = &proc_dointvec,
1279 .ctl_name = NET_IPV4_CONF_SECURE_REDIRECTS,
1280 .procname = "secure_redirects",
1281 .data = &ipv4_devconf.secure_redirects,
1282 .maxlen = sizeof(int),
1284 .proc_handler = &proc_dointvec,
1287 .ctl_name = NET_IPV4_CONF_SHARED_MEDIA,
1288 .procname = "shared_media",
1289 .data = &ipv4_devconf.shared_media,
1290 .maxlen = sizeof(int),
1292 .proc_handler = &proc_dointvec,
1295 .ctl_name = NET_IPV4_CONF_RP_FILTER,
1296 .procname = "rp_filter",
1297 .data = &ipv4_devconf.rp_filter,
1298 .maxlen = sizeof(int),
1300 .proc_handler = &proc_dointvec,
1303 .ctl_name = NET_IPV4_CONF_SEND_REDIRECTS,
1304 .procname = "send_redirects",
1305 .data = &ipv4_devconf.send_redirects,
1306 .maxlen = sizeof(int),
1308 .proc_handler = &proc_dointvec,
1311 .ctl_name = NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE,
1312 .procname = "accept_source_route",
1313 .data = &ipv4_devconf.accept_source_route,
1314 .maxlen = sizeof(int),
1316 .proc_handler = &proc_dointvec,
1319 .ctl_name = NET_IPV4_CONF_PROXY_ARP,
1320 .procname = "proxy_arp",
1321 .data = &ipv4_devconf.proxy_arp,
1322 .maxlen = sizeof(int),
1324 .proc_handler = &proc_dointvec,
1327 .ctl_name = NET_IPV4_CONF_MEDIUM_ID,
1328 .procname = "medium_id",
1329 .data = &ipv4_devconf.medium_id,
1330 .maxlen = sizeof(int),
1332 .proc_handler = &proc_dointvec,
1335 .ctl_name = NET_IPV4_CONF_BOOTP_RELAY,
1336 .procname = "bootp_relay",
1337 .data = &ipv4_devconf.bootp_relay,
1338 .maxlen = sizeof(int),
1340 .proc_handler = &proc_dointvec,
1343 .ctl_name = NET_IPV4_CONF_LOG_MARTIANS,
1344 .procname = "log_martians",
1345 .data = &ipv4_devconf.log_martians,
1346 .maxlen = sizeof(int),
1348 .proc_handler = &proc_dointvec,
1351 .ctl_name = NET_IPV4_CONF_TAG,
1353 .data = &ipv4_devconf.tag,
1354 .maxlen = sizeof(int),
1356 .proc_handler = &proc_dointvec,
1359 .ctl_name = NET_IPV4_CONF_ARPFILTER,
1360 .procname = "arp_filter",
1361 .data = &ipv4_devconf.arp_filter,
1362 .maxlen = sizeof(int),
1364 .proc_handler = &proc_dointvec,
1367 .ctl_name = NET_IPV4_CONF_ARP_ANNOUNCE,
1368 .procname = "arp_announce",
1369 .data = &ipv4_devconf.arp_announce,
1370 .maxlen = sizeof(int),
1372 .proc_handler = &proc_dointvec,
1375 .ctl_name = NET_IPV4_CONF_ARP_IGNORE,
1376 .procname = "arp_ignore",
1377 .data = &ipv4_devconf.arp_ignore,
1378 .maxlen = sizeof(int),
1380 .proc_handler = &proc_dointvec,
1383 .ctl_name = NET_IPV4_CONF_NOXFRM,
1384 .procname = "disable_xfrm",
1385 .data = &ipv4_devconf.no_xfrm,
1386 .maxlen = sizeof(int),
1388 .proc_handler = &ipv4_doint_and_flush,
1389 .strategy = &ipv4_doint_and_flush_strategy,
1392 .ctl_name = NET_IPV4_CONF_NOPOLICY,
1393 .procname = "disable_policy",
1394 .data = &ipv4_devconf.no_policy,
1395 .maxlen = sizeof(int),
1397 .proc_handler = &ipv4_doint_and_flush,
1398 .strategy = &ipv4_doint_and_flush_strategy,
1401 .ctl_name = NET_IPV4_CONF_FORCE_IGMP_VERSION,
1402 .procname = "force_igmp_version",
1403 .data = &ipv4_devconf.force_igmp_version,
1404 .maxlen = sizeof(int),
1406 .proc_handler = &ipv4_doint_and_flush,
1407 .strategy = &ipv4_doint_and_flush_strategy,
1412 .ctl_name = NET_PROTO_CONF_ALL,
1415 .child = devinet_sysctl.devinet_vars,
1418 .devinet_conf_dir = {
1420 .ctl_name = NET_IPV4_CONF,
1423 .child = devinet_sysctl.devinet_dev,
1426 .devinet_proto_dir = {
1428 .ctl_name = NET_IPV4,
1431 .child = devinet_sysctl.devinet_conf_dir,
1434 .devinet_root_dir = {
1436 .ctl_name = CTL_NET,
1439 .child = devinet_sysctl.devinet_proto_dir,
1444 static void devinet_sysctl_register(struct in_device *in_dev,
1445 struct ipv4_devconf *p)
1448 struct net_device *dev = in_dev ? in_dev->dev : NULL;
1449 struct devinet_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
1450 char *dev_name = NULL;
1454 memcpy(t, &devinet_sysctl, sizeof(*t));
1455 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1456 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
1457 t->devinet_vars[i].de = NULL;
1461 dev_name = dev->name;
1462 t->devinet_dev[0].ctl_name = dev->ifindex;
1464 dev_name = "default";
1465 t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
1469 * Make a copy of dev_name, because '.procname' is regarded as const
1470 * by sysctl and we wouldn't want anyone to change it under our feet
1471 * (see SIOCSIFNAME).
1473 dev_name = net_sysctl_strdup(dev_name);
1477 t->devinet_dev[0].procname = dev_name;
1478 t->devinet_dev[0].child = t->devinet_vars;
1479 t->devinet_dev[0].de = NULL;
1480 t->devinet_conf_dir[0].child = t->devinet_dev;
1481 t->devinet_conf_dir[0].de = NULL;
1482 t->devinet_proto_dir[0].child = t->devinet_conf_dir;
1483 t->devinet_proto_dir[0].de = NULL;
1484 t->devinet_root_dir[0].child = t->devinet_proto_dir;
1485 t->devinet_root_dir[0].de = NULL;
1487 t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
1488 if (!t->sysctl_header)
1502 static void devinet_sysctl_unregister(struct ipv4_devconf *p)
1505 struct devinet_sysctl_table *t = p->sysctl;
1507 unregister_sysctl_table(t->sysctl_header);
1508 kfree(t->devinet_dev[0].procname);
1514 void __init devinet_init(void)
1516 register_gifconf(PF_INET, inet_gifconf);
1517 register_netdevice_notifier(&ip_netdev_notifier);
1518 rtnetlink_links[PF_INET] = inet_rtnetlink_table;
1519 #ifdef CONFIG_SYSCTL
1520 devinet_sysctl.sysctl_header =
1521 register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
1522 devinet_sysctl_register(NULL, &ipv4_devconf_dflt);
1526 EXPORT_SYMBOL(devinet_ioctl);
1527 EXPORT_SYMBOL(in_dev_finish_destroy);
1528 EXPORT_SYMBOL(inet_select_addr);
1529 EXPORT_SYMBOL(inetdev_by_index);
1530 EXPORT_SYMBOL(inetdev_lock);
1531 EXPORT_SYMBOL(register_inetaddr_notifier);
1532 EXPORT_SYMBOL(unregister_inetaddr_notifier);