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 lists.
22 * Cyrus Durgin: updated for kmod
23 * Matthias Andree: in devinet_ioctl, compare label and
24 * address (4.4BSD alias style support),
25 * fall back to comparing just the label
29 #include <linux/config.h>
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
33 #include <asm/bitops.h>
34 #include <linux/types.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/string.h>
39 #include <linux/socket.h>
40 #include <linux/sockios.h>
42 #include <linux/errno.h>
43 #include <linux/interrupt.h>
44 #include <linux/if_ether.h>
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <linux/etherdevice.h>
48 #include <linux/skbuff.h>
49 #include <linux/rtnetlink.h>
50 #include <linux/init.h>
51 #include <linux/notifier.h>
52 #include <linux/inetdevice.h>
53 #include <linux/igmp.h>
55 #include <linux/sysctl.h>
57 #include <linux/kmod.h>
60 #include <net/route.h>
61 #include <net/ip_fib.h>
63 struct ipv4_devconf ipv4_devconf = { 1, 1, 1, 1, 0, };
64 static struct ipv4_devconf ipv4_devconf_dflt = { 1, 1, 1, 1, 1, };
66 static void rtmsg_ifa(int event, struct in_ifaddr *);
68 static struct notifier_block *inetaddr_chain;
69 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, int destroy);
71 static void devinet_sysctl_register(struct in_device *in_dev, struct ipv4_devconf *p);
72 static void devinet_sysctl_unregister(struct ipv4_devconf *p);
78 /* Locks all the inet devices. */
80 rwlock_t inetdev_lock = RW_LOCK_UNLOCKED;
83 static struct in_ifaddr * inet_alloc_ifa(void)
85 struct in_ifaddr *ifa;
87 ifa = kmalloc(sizeof(*ifa), GFP_KERNEL);
89 memset(ifa, 0, sizeof(*ifa));
96 static __inline__ void inet_free_ifa(struct in_ifaddr *ifa)
99 __in_dev_put(ifa->ifa_dev);
104 void in_dev_finish_destroy(struct in_device *idev)
106 struct net_device *dev = idev->dev;
108 BUG_TRAP(idev->ifa_list==NULL);
109 BUG_TRAP(idev->mc_list==NULL);
110 #ifdef NET_REFCNT_DEBUG
111 printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n", idev, dev ? dev->name : "NIL");
115 printk("Freeing alive in_device %p\n", idev);
122 struct in_device *inetdev_init(struct net_device *dev)
124 struct in_device *in_dev;
128 in_dev = kmalloc(sizeof(*in_dev), GFP_KERNEL);
131 memset(in_dev, 0, sizeof(*in_dev));
132 in_dev->lock = RW_LOCK_UNLOCKED;
133 memcpy(&in_dev->cnf, &ipv4_devconf_dflt, sizeof(in_dev->cnf));
134 in_dev->cnf.sysctl = NULL;
136 if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL) {
141 /* Reference in_dev->dev */
144 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4, NET_IPV4_NEIGH, "ipv4");
146 write_lock_bh(&inetdev_lock);
147 dev->ip_ptr = in_dev;
148 /* Account for reference dev->ip_ptr */
150 write_unlock_bh(&inetdev_lock);
152 devinet_sysctl_register(in_dev, &in_dev->cnf);
154 ip_mc_init_dev(in_dev);
155 if (dev->flags & IFF_UP)
160 static void inetdev_destroy(struct in_device *in_dev)
162 struct in_ifaddr *ifa;
168 ip_mc_destroy_dev(in_dev);
170 while ((ifa = in_dev->ifa_list) != NULL) {
171 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
176 devinet_sysctl_unregister(&in_dev->cnf);
178 write_lock_bh(&inetdev_lock);
179 in_dev->dev->ip_ptr = NULL;
180 /* in_dev_put following below will kill the in_device */
181 write_unlock_bh(&inetdev_lock);
184 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
188 int inet_addr_onlink(struct in_device *in_dev, u32 a, u32 b)
190 read_lock(&in_dev->lock);
191 for_primary_ifa(in_dev) {
192 if (inet_ifa_match(a, ifa)) {
193 if (!b || inet_ifa_match(b, ifa)) {
194 read_unlock(&in_dev->lock);
198 } endfor_ifa(in_dev);
199 read_unlock(&in_dev->lock);
204 inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, int destroy)
206 struct in_ifaddr *ifa1 = *ifap;
210 /* 1. Deleting primary ifaddr forces deletion all secondaries */
212 if (!(ifa1->ifa_flags&IFA_F_SECONDARY)) {
213 struct in_ifaddr *ifa;
214 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
216 while ((ifa=*ifap1) != NULL) {
217 if (!(ifa->ifa_flags&IFA_F_SECONDARY) ||
218 ifa1->ifa_mask != ifa->ifa_mask ||
219 !inet_ifa_match(ifa1->ifa_address, ifa)) {
220 ifap1 = &ifa->ifa_next;
223 write_lock_bh(&in_dev->lock);
224 *ifap1 = ifa->ifa_next;
225 write_unlock_bh(&in_dev->lock);
227 rtmsg_ifa(RTM_DELADDR, ifa);
228 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa);
235 write_lock_bh(&in_dev->lock);
236 *ifap = ifa1->ifa_next;
237 write_unlock_bh(&in_dev->lock);
239 /* 3. Announce address deletion */
241 /* Send message first, then call notifier.
242 At first sight, FIB update triggered by notifier
243 will refer to already deleted ifaddr, that could confuse
244 netlink listeners. It is not true: look, gated sees
245 that route deleted and if it still thinks that ifaddr
246 is valid, it will try to restore deleted routes... Grr.
247 So that, this order is correct.
249 rtmsg_ifa(RTM_DELADDR, ifa1);
250 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
254 if (in_dev->ifa_list == NULL)
255 inetdev_destroy(in_dev);
260 inet_insert_ifa(struct in_ifaddr *ifa)
262 struct in_device *in_dev = ifa->ifa_dev;
263 struct in_ifaddr *ifa1, **ifap, **last_primary;
267 if (ifa->ifa_local == 0) {
272 ifa->ifa_flags &= ~IFA_F_SECONDARY;
273 last_primary = &in_dev->ifa_list;
275 for (ifap=&in_dev->ifa_list; (ifa1=*ifap)!=NULL; ifap=&ifa1->ifa_next) {
276 if (!(ifa1->ifa_flags&IFA_F_SECONDARY) && ifa->ifa_scope <= ifa1->ifa_scope)
277 last_primary = &ifa1->ifa_next;
278 if (ifa1->ifa_mask == ifa->ifa_mask && inet_ifa_match(ifa1->ifa_address, ifa)) {
279 if (ifa1->ifa_local == ifa->ifa_local) {
283 if (ifa1->ifa_scope != ifa->ifa_scope) {
287 ifa->ifa_flags |= IFA_F_SECONDARY;
291 if (!(ifa->ifa_flags&IFA_F_SECONDARY)) {
292 net_srandom(ifa->ifa_local);
296 ifa->ifa_next = *ifap;
297 write_lock_bh(&in_dev->lock);
299 write_unlock_bh(&in_dev->lock);
301 /* Send message first, then call notifier.
302 Notifier will trigger FIB update, so that
303 listeners of netlink will know about new ifaddr */
304 rtmsg_ifa(RTM_NEWADDR, ifa);
305 notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
311 inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
313 struct in_device *in_dev = __in_dev_get(dev);
317 if (in_dev == NULL) {
318 in_dev = inetdev_init(dev);
319 if (in_dev == NULL) {
324 if (ifa->ifa_dev != in_dev) {
325 BUG_TRAP(ifa->ifa_dev==NULL);
329 if (LOOPBACK(ifa->ifa_local))
330 ifa->ifa_scope = RT_SCOPE_HOST;
331 return inet_insert_ifa(ifa);
334 struct in_device *inetdev_by_index(int ifindex)
336 struct net_device *dev;
337 struct in_device *in_dev = NULL;
338 read_lock(&dev_base_lock);
339 dev = __dev_get_by_index(ifindex);
341 in_dev = in_dev_get(dev);
342 read_unlock(&dev_base_lock);
346 /* Called only from RTNL semaphored context. No locks. */
348 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, u32 prefix, u32 mask)
352 for_primary_ifa(in_dev) {
353 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
355 } endfor_ifa(in_dev);
360 inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
362 struct rtattr **rta = arg;
363 struct in_device *in_dev;
364 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
365 struct in_ifaddr *ifa, **ifap;
369 if ((in_dev = inetdev_by_index(ifm->ifa_index)) == NULL)
370 return -EADDRNOTAVAIL;
371 __in_dev_put(in_dev);
373 for (ifap=&in_dev->ifa_list; (ifa=*ifap)!=NULL; ifap=&ifa->ifa_next) {
374 if ((rta[IFA_LOCAL-1] && memcmp(RTA_DATA(rta[IFA_LOCAL-1]), &ifa->ifa_local, 4)) ||
375 (rta[IFA_LABEL-1] && strcmp(RTA_DATA(rta[IFA_LABEL-1]), ifa->ifa_label)) ||
376 (rta[IFA_ADDRESS-1] &&
377 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
378 !inet_ifa_match(*(u32*)RTA_DATA(rta[IFA_ADDRESS-1]), ifa))))
380 inet_del_ifa(in_dev, ifap, 1);
384 return -EADDRNOTAVAIL;
388 inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
390 struct rtattr **rta = arg;
391 struct net_device *dev;
392 struct in_device *in_dev;
393 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
394 struct in_ifaddr *ifa;
398 if (ifm->ifa_prefixlen > 32 || rta[IFA_LOCAL-1] == NULL)
401 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
404 if ((in_dev = __in_dev_get(dev)) == NULL) {
405 in_dev = inetdev_init(dev);
410 if ((ifa = inet_alloc_ifa()) == NULL)
413 if (rta[IFA_ADDRESS-1] == NULL)
414 rta[IFA_ADDRESS-1] = rta[IFA_LOCAL-1];
415 memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL-1]), 4);
416 memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS-1]), 4);
417 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
418 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
419 if (rta[IFA_BROADCAST-1])
420 memcpy(&ifa->ifa_broadcast, RTA_DATA(rta[IFA_BROADCAST-1]), 4);
421 if (rta[IFA_ANYCAST-1])
422 memcpy(&ifa->ifa_anycast, RTA_DATA(rta[IFA_ANYCAST-1]), 4);
423 ifa->ifa_flags = ifm->ifa_flags;
424 ifa->ifa_scope = ifm->ifa_scope;
426 ifa->ifa_dev = in_dev;
427 if (rta[IFA_LABEL-1])
428 memcpy(ifa->ifa_label, RTA_DATA(rta[IFA_LABEL-1]), IFNAMSIZ);
430 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
432 return inet_insert_ifa(ifa);
436 * Determine a default network mask, based on the IP address.
439 static __inline__ int inet_abc_len(u32 addr)
453 * Something else, probably a multicast.
460 int devinet_ioctl(unsigned int cmd, void *arg)
463 struct sockaddr_in sin_orig;
464 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
465 struct in_device *in_dev;
466 struct in_ifaddr **ifap = NULL;
467 struct in_ifaddr *ifa = NULL;
468 struct net_device *dev;
471 int tryaddrmatch = 0;
474 * Fetch the caller's info block into kernel space
477 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
479 ifr.ifr_name[IFNAMSIZ-1] = 0;
481 /* save original address for comparison */
482 memcpy(&sin_orig, sin, sizeof(*sin));
484 colon = strchr(ifr.ifr_name, ':');
489 dev_load(ifr.ifr_name);
493 case SIOCGIFADDR: /* Get interface address */
494 case SIOCGIFBRDADDR: /* Get the broadcast address */
495 case SIOCGIFDSTADDR: /* Get the destination address */
496 case SIOCGIFNETMASK: /* Get the netmask for the interface */
497 /* Note that these ioctls will not sleep,
498 so that we do not impose a lock.
499 One day we will be forced to put shlock here (I mean SMP)
501 tryaddrmatch = (sin_orig.sin_family == AF_INET);
502 memset(sin, 0, sizeof(*sin));
503 sin->sin_family = AF_INET;
507 if (!capable(CAP_NET_ADMIN))
510 case SIOCSIFADDR: /* Set interface address (and family) */
511 case SIOCSIFBRDADDR: /* Set the broadcast address */
512 case SIOCSIFDSTADDR: /* Set the destination address */
513 case SIOCSIFNETMASK: /* Set the netmask for the interface */
514 if (!capable(CAP_NET_ADMIN))
516 if (sin->sin_family != AF_INET)
526 if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL) {
534 if ((in_dev=__in_dev_get(dev)) != NULL) {
536 /* Matthias Andree */
537 /* compare label and address (4.4BSD style) */
538 /* note: we only do this for a limited set of ioctls
539 and only if the original address family was AF_INET.
540 This is checked above. */
541 for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next) {
542 if ((strcmp(ifr.ifr_name, ifa->ifa_label) == 0)
543 && (sin_orig.sin_addr.s_addr == ifa->ifa_address)) {
548 /* we didn't get a match, maybe the application is
549 4.3BSD-style and passed in junk so we fall back to
550 comparing just the label */
552 for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next)
553 if (strcmp(ifr.ifr_name, ifa->ifa_label) == 0)
558 if (ifa == NULL && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS) {
559 ret = -EADDRNOTAVAIL;
564 case SIOCGIFADDR: /* Get interface address */
565 sin->sin_addr.s_addr = ifa->ifa_local;
568 case SIOCGIFBRDADDR: /* Get the broadcast address */
569 sin->sin_addr.s_addr = ifa->ifa_broadcast;
572 case SIOCGIFDSTADDR: /* Get the destination address */
573 sin->sin_addr.s_addr = ifa->ifa_address;
576 case SIOCGIFNETMASK: /* Get the netmask for the interface */
577 sin->sin_addr.s_addr = ifa->ifa_mask;
583 ret = -EADDRNOTAVAIL;
586 if (!(ifr.ifr_flags&IFF_UP))
587 inet_del_ifa(in_dev, ifap, 1);
590 ret = dev_change_flags(dev, ifr.ifr_flags);
593 case SIOCSIFADDR: /* Set interface address (and family) */
594 if (inet_abc_len(sin->sin_addr.s_addr) < 0) {
600 if ((ifa = inet_alloc_ifa()) == NULL) {
605 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
607 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
610 if (ifa->ifa_local == sin->sin_addr.s_addr)
612 inet_del_ifa(in_dev, ifap, 0);
613 ifa->ifa_broadcast = 0;
614 ifa->ifa_anycast = 0;
618 ifa->ifa_local = sin->sin_addr.s_addr;
620 if (!(dev->flags&IFF_POINTOPOINT)) {
621 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
622 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
623 if ((dev->flags&IFF_BROADCAST) && ifa->ifa_prefixlen < 31)
624 ifa->ifa_broadcast = ifa->ifa_address|~ifa->ifa_mask;
626 ifa->ifa_prefixlen = 32;
627 ifa->ifa_mask = inet_make_mask(32);
629 ret = inet_set_ifa(dev, ifa);
632 case SIOCSIFBRDADDR: /* Set the broadcast address */
633 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
634 inet_del_ifa(in_dev, ifap, 0);
635 ifa->ifa_broadcast = sin->sin_addr.s_addr;
636 inet_insert_ifa(ifa);
640 case SIOCSIFDSTADDR: /* Set the destination address */
641 if (ifa->ifa_address != sin->sin_addr.s_addr) {
642 if (inet_abc_len(sin->sin_addr.s_addr) < 0) {
646 inet_del_ifa(in_dev, ifap, 0);
647 ifa->ifa_address = sin->sin_addr.s_addr;
648 inet_insert_ifa(ifa);
652 case SIOCSIFNETMASK: /* Set the netmask for the interface */
655 * The mask we set must be legal.
657 if (bad_mask(sin->sin_addr.s_addr, 0)) {
662 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
663 inet_del_ifa(in_dev, ifap, 0);
664 ifa->ifa_mask = sin->sin_addr.s_addr;
666 inet_mask_len(ifa->ifa_mask);
668 /* See if current broadcast address matches
669 * with current netmask, then recalculate
670 * the broadcast address. Otherwise it's a
671 * funny address, so don't touch it since
672 * the user seems to know what (s)he's doing...
674 if ((dev->flags & IFF_BROADCAST) &&
675 (ifa->ifa_prefixlen < 31) &&
676 (ifa->ifa_broadcast ==
677 (ifa->ifa_local|~ifa->ifa_mask))) {
680 ~sin->sin_addr.s_addr);
682 inet_insert_ifa(ifa);
694 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
700 inet_gifconf(struct net_device *dev, char *buf, int len)
702 struct in_device *in_dev = __in_dev_get(dev);
703 struct in_ifaddr *ifa;
707 if (in_dev==NULL || (ifa=in_dev->ifa_list)==NULL)
710 for ( ; ifa; ifa = ifa->ifa_next) {
715 if (len < (int) sizeof(ifr))
717 memset(&ifr, 0, sizeof(struct ifreq));
719 strcpy(ifr.ifr_name, ifa->ifa_label);
721 strcpy(ifr.ifr_name, dev->name);
723 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_family = AF_INET;
724 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_addr.s_addr = ifa->ifa_local;
726 if (copy_to_user(buf, &ifr, sizeof(struct ifreq)))
728 buf += sizeof(struct ifreq);
729 len -= sizeof(struct ifreq);
730 done += sizeof(struct ifreq);
735 u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
738 struct in_device *in_dev;
740 read_lock(&inetdev_lock);
741 in_dev = __in_dev_get(dev);
742 if (in_dev == NULL) {
743 read_unlock(&inetdev_lock);
747 read_lock(&in_dev->lock);
748 for_primary_ifa(in_dev) {
749 if (ifa->ifa_scope > scope)
751 if (!dst || inet_ifa_match(dst, ifa)) {
752 addr = ifa->ifa_local;
756 addr = ifa->ifa_local;
757 } endfor_ifa(in_dev);
758 read_unlock(&in_dev->lock);
759 read_unlock(&inetdev_lock);
764 /* Not loopback addresses on loopback should be preferred
765 in this case. It is importnat that lo is the first interface
768 read_lock(&dev_base_lock);
769 read_lock(&inetdev_lock);
770 for (dev=dev_base; dev; dev=dev->next) {
771 if ((in_dev=__in_dev_get(dev)) == NULL)
774 read_lock(&in_dev->lock);
775 for_primary_ifa(in_dev) {
776 if (ifa->ifa_scope != RT_SCOPE_LINK &&
777 ifa->ifa_scope <= scope) {
778 read_unlock(&in_dev->lock);
779 read_unlock(&inetdev_lock);
780 read_unlock(&dev_base_lock);
781 return ifa->ifa_local;
783 } endfor_ifa(in_dev);
784 read_unlock(&in_dev->lock);
786 read_unlock(&inetdev_lock);
787 read_unlock(&dev_base_lock);
792 static u32 confirm_addr_indev(struct in_device *in_dev, u32 dst,
793 u32 local, int scope)
800 (local == ifa->ifa_local || !local) &&
801 ifa->ifa_scope <= scope) {
802 addr = ifa->ifa_local;
807 same = (!local || inet_ifa_match(local, ifa)) &&
808 (!dst || inet_ifa_match(dst, ifa));
812 /* Is the selected addr into dst subnet? */
813 if (inet_ifa_match(addr, ifa))
815 /* No, then can we use new local src? */
816 if (ifa->ifa_scope <= scope) {
817 addr = ifa->ifa_local;
820 /* search for large dst subnet for addr */
824 } endfor_ifa(in_dev);
826 return same? addr : 0;
830 * Confirm that local IP address exists using wildcards:
831 * - dev: only on this interface, 0=any interface
832 * - dst: only in the same subnet as dst, 0=any dst
833 * - local: address, 0=autoselect the local address
834 * - scope: maximum allowed scope value for the local address
836 u32 inet_confirm_addr(const struct net_device *dev, u32 dst, u32 local, int scope)
839 struct in_device *in_dev;
842 read_lock(&inetdev_lock);
843 if ((in_dev = __in_dev_get(dev))) {
844 read_lock(&in_dev->lock);
845 addr = confirm_addr_indev(in_dev, dst, local, scope);
846 read_unlock(&in_dev->lock);
848 read_unlock(&inetdev_lock);
853 read_lock(&dev_base_lock);
854 read_lock(&inetdev_lock);
855 for (dev = dev_base; dev; dev = dev->next) {
856 if ((in_dev = __in_dev_get(dev))) {
857 read_lock(&in_dev->lock);
858 addr = confirm_addr_indev(in_dev, dst, local, scope);
859 read_unlock(&in_dev->lock);
864 read_unlock(&inetdev_lock);
865 read_unlock(&dev_base_lock);
874 int register_inetaddr_notifier(struct notifier_block *nb)
876 return notifier_chain_register(&inetaddr_chain, nb);
879 int unregister_inetaddr_notifier(struct notifier_block *nb)
881 return notifier_chain_unregister(&inetaddr_chain,nb);
884 /* Rename ifa_labels for a device name change. Make some effort to preserve existing
885 * alias numbering and to create unique labels if possible.
887 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
889 struct in_ifaddr *ifa;
892 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
893 char old[IFNAMSIZ], *dot;
895 memcpy(old, ifa->ifa_label, IFNAMSIZ);
896 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
899 dot = strchr(ifa->ifa_label, ':');
901 sprintf(old, ":%d", named);
904 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) {
905 strcat(ifa->ifa_label, dot);
907 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
912 /* Called only under RTNL semaphore */
914 static int inetdev_event(struct notifier_block *this, unsigned long event, void *ptr)
916 struct net_device *dev = ptr;
917 struct in_device *in_dev = __in_dev_get(dev);
925 case NETDEV_REGISTER:
926 printk(KERN_DEBUG "inetdev_event: bug\n");
932 if (dev == &loopback_dev) {
933 struct in_ifaddr *ifa;
934 if ((ifa = inet_alloc_ifa()) != NULL) {
936 ifa->ifa_address = htonl(INADDR_LOOPBACK);
937 ifa->ifa_prefixlen = 8;
938 ifa->ifa_mask = inet_make_mask(8);
940 ifa->ifa_dev = in_dev;
941 ifa->ifa_scope = RT_SCOPE_HOST;
942 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
943 inet_insert_ifa(ifa);
951 case NETDEV_CHANGEMTU:
954 /* MTU falled under 68, disable IP */
955 case NETDEV_UNREGISTER:
956 inetdev_destroy(in_dev);
958 case NETDEV_CHANGENAME:
959 /* Do not notify about label change, this event is
960 * not interesting to applications using netlink.
962 inetdev_changename(dev, in_dev);
969 struct notifier_block ip_netdev_notifier = {
970 notifier_call: inetdev_event,
973 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
974 u32 pid, u32 seq, int event)
976 struct ifaddrmsg *ifm;
977 struct nlmsghdr *nlh;
978 unsigned char *b = skb->tail;
980 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
981 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
982 ifm = NLMSG_DATA(nlh);
983 ifm->ifa_family = AF_INET;
984 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
985 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
986 ifm->ifa_scope = ifa->ifa_scope;
987 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
988 if (ifa->ifa_address)
989 RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
991 RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
992 if (ifa->ifa_broadcast)
993 RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
994 if (ifa->ifa_anycast)
995 RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
996 if (ifa->ifa_label[0])
997 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
998 nlh->nlmsg_len = skb->tail - b;
1003 skb_trim(skb, b - skb->data);
1007 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1010 int s_idx, s_ip_idx;
1011 struct net_device *dev;
1012 struct in_device *in_dev;
1013 struct in_ifaddr *ifa;
1015 s_idx = cb->args[0];
1016 s_ip_idx = ip_idx = cb->args[1];
1017 read_lock(&dev_base_lock);
1018 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
1023 read_lock(&inetdev_lock);
1024 if ((in_dev = __in_dev_get(dev)) == NULL) {
1025 read_unlock(&inetdev_lock);
1028 read_lock(&in_dev->lock);
1029 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
1030 ifa = ifa->ifa_next, ip_idx++) {
1031 if (ip_idx < s_ip_idx)
1033 if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1034 cb->nlh->nlmsg_seq, RTM_NEWADDR) <= 0) {
1035 read_unlock(&in_dev->lock);
1036 read_unlock(&inetdev_lock);
1040 read_unlock(&in_dev->lock);
1041 read_unlock(&inetdev_lock);
1045 read_unlock(&dev_base_lock);
1047 cb->args[1] = ip_idx;
1052 static void rtmsg_ifa(int event, struct in_ifaddr * ifa)
1054 struct sk_buff *skb;
1055 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
1057 skb = alloc_skb(size, GFP_KERNEL);
1059 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, ENOBUFS);
1062 if (inet_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
1064 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, EINVAL);
1067 NETLINK_CB(skb).dst_groups = RTMGRP_IPV4_IFADDR;
1068 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV4_IFADDR, GFP_KERNEL);
1072 static struct rtnetlink_link inet_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
1079 { inet_rtm_newaddr, NULL, },
1080 { inet_rtm_deladdr, NULL, },
1081 { NULL, inet_dump_ifaddr, },
1084 { inet_rtm_newroute, NULL, },
1085 { inet_rtm_delroute, NULL, },
1086 { inet_rtm_getroute, inet_dump_fib, },
1094 #ifdef CONFIG_IP_MULTIPLE_TABLES
1095 { inet_rtm_newrule, NULL, },
1096 { inet_rtm_delrule, NULL, },
1097 { NULL, inet_dump_rules, },
1108 #ifdef CONFIG_SYSCTL
1110 void inet_forward_change(int on)
1112 struct net_device *dev;
1114 ipv4_devconf.accept_redirects = !on;
1115 ipv4_devconf_dflt.forwarding = on;
1117 read_lock(&dev_base_lock);
1118 for (dev = dev_base; dev; dev = dev->next) {
1119 struct in_device *in_dev;
1120 read_lock(&inetdev_lock);
1121 in_dev = __in_dev_get(dev);
1123 in_dev->cnf.forwarding = on;
1124 read_unlock(&inetdev_lock);
1126 read_unlock(&dev_base_lock);
1132 int devinet_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
1133 void *buffer, size_t *lenp)
1135 int *valp = ctl->data;
1139 ret = proc_dointvec(ctl, write, filp, buffer, lenp);
1141 if (write && *valp != val) {
1142 if (valp == &ipv4_devconf.forwarding)
1143 inet_forward_change(*valp);
1144 else if (valp != &ipv4_devconf_dflt.forwarding)
1151 static struct devinet_sysctl_table
1153 struct ctl_table_header *sysctl_header;
1154 ctl_table devinet_vars[20];
1155 ctl_table devinet_dev[2];
1156 ctl_table devinet_conf_dir[2];
1157 ctl_table devinet_proto_dir[2];
1158 ctl_table devinet_root_dir[2];
1159 } devinet_sysctl = {
1161 {{NET_IPV4_CONF_FORWARDING, "forwarding",
1162 &ipv4_devconf.forwarding, sizeof(int), 0644, NULL,
1163 &devinet_sysctl_forward},
1164 {NET_IPV4_CONF_MC_FORWARDING, "mc_forwarding",
1165 &ipv4_devconf.mc_forwarding, sizeof(int), 0444, NULL,
1167 {NET_IPV4_CONF_ACCEPT_REDIRECTS, "accept_redirects",
1168 &ipv4_devconf.accept_redirects, sizeof(int), 0644, NULL,
1170 {NET_IPV4_CONF_SECURE_REDIRECTS, "secure_redirects",
1171 &ipv4_devconf.secure_redirects, sizeof(int), 0644, NULL,
1173 {NET_IPV4_CONF_SHARED_MEDIA, "shared_media",
1174 &ipv4_devconf.shared_media, sizeof(int), 0644, NULL,
1176 {NET_IPV4_CONF_RP_FILTER, "rp_filter",
1177 &ipv4_devconf.rp_filter, sizeof(int), 0644, NULL,
1179 {NET_IPV4_CONF_SEND_REDIRECTS, "send_redirects",
1180 &ipv4_devconf.send_redirects, sizeof(int), 0644, NULL,
1182 {NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE, "accept_source_route",
1183 &ipv4_devconf.accept_source_route, sizeof(int), 0644, NULL,
1185 {NET_IPV4_CONF_PROXY_ARP, "proxy_arp",
1186 &ipv4_devconf.proxy_arp, sizeof(int), 0644, NULL,
1188 {NET_IPV4_CONF_MEDIUM_ID, "medium_id",
1189 &ipv4_devconf.medium_id, sizeof(int), 0644, NULL,
1191 {NET_IPV4_CONF_BOOTP_RELAY, "bootp_relay",
1192 &ipv4_devconf.bootp_relay, sizeof(int), 0644, NULL,
1194 {NET_IPV4_CONF_LOG_MARTIANS, "log_martians",
1195 &ipv4_devconf.log_martians, sizeof(int), 0644, NULL,
1197 {NET_IPV4_CONF_TAG, "tag",
1198 &ipv4_devconf.tag, sizeof(int), 0644, NULL,
1200 {NET_IPV4_CONF_ARPFILTER, "arp_filter",
1201 &ipv4_devconf.arp_filter, sizeof(int), 0644, NULL,
1203 {NET_IPV4_CONF_ARP_ANNOUNCE, "arp_announce",
1204 &ipv4_devconf.arp_announce, sizeof(int), 0644, NULL,
1206 {NET_IPV4_CONF_ARP_IGNORE, "arp_ignore",
1207 &ipv4_devconf.arp_ignore, sizeof(int), 0644, NULL,
1209 {NET_IPV4_CONF_FORCE_IGMP_VERSION, "force_igmp_version",
1210 &ipv4_devconf.force_igmp_version, sizeof(int), 0644, NULL,
1214 {{NET_PROTO_CONF_ALL, "all", NULL, 0, 0555, devinet_sysctl.devinet_vars},{0}},
1215 {{NET_IPV4_CONF, "conf", NULL, 0, 0555, devinet_sysctl.devinet_dev},{0}},
1216 {{NET_IPV4, "ipv4", NULL, 0, 0555, devinet_sysctl.devinet_conf_dir},{0}},
1217 {{CTL_NET, "net", NULL, 0, 0555, devinet_sysctl.devinet_proto_dir},{0}}
1220 static void devinet_sysctl_register(struct in_device *in_dev, struct ipv4_devconf *p)
1223 struct net_device *dev = in_dev ? in_dev->dev : NULL;
1224 struct devinet_sysctl_table *t;
1226 t = kmalloc(sizeof(*t), GFP_KERNEL);
1229 memcpy(t, &devinet_sysctl, sizeof(*t));
1230 for (i=0; i<sizeof(t->devinet_vars)/sizeof(t->devinet_vars[0])-1; i++) {
1231 t->devinet_vars[i].data += (char*)p - (char*)&ipv4_devconf;
1232 t->devinet_vars[i].de = NULL;
1235 t->devinet_dev[0].procname = dev->name;
1236 t->devinet_dev[0].ctl_name = dev->ifindex;
1238 t->devinet_dev[0].procname = "default";
1239 t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
1241 t->devinet_dev[0].child = t->devinet_vars;
1242 t->devinet_dev[0].de = NULL;
1243 t->devinet_conf_dir[0].child = t->devinet_dev;
1244 t->devinet_conf_dir[0].de = NULL;
1245 t->devinet_proto_dir[0].child = t->devinet_conf_dir;
1246 t->devinet_proto_dir[0].de = NULL;
1247 t->devinet_root_dir[0].child = t->devinet_proto_dir;
1248 t->devinet_root_dir[0].de = NULL;
1250 t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
1251 if (t->sysctl_header == NULL)
1257 static void devinet_sysctl_unregister(struct ipv4_devconf *p)
1260 struct devinet_sysctl_table *t = p->sysctl;
1262 unregister_sysctl_table(t->sysctl_header);
1268 void __init devinet_init(void)
1270 register_gifconf(PF_INET, inet_gifconf);
1271 register_netdevice_notifier(&ip_netdev_notifier);
1272 rtnetlink_links[PF_INET] = inet_rtnetlink_table;
1273 #ifdef CONFIG_SYSCTL
1274 devinet_sysctl.sysctl_header =
1275 register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
1276 devinet_sysctl_register(NULL, &ipv4_devconf_dflt);