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 if (dev->flags & IFF_UP)
178 static void inetdev_destroy(struct in_device *in_dev)
180 struct in_ifaddr *ifa;
186 ip_mc_destroy_dev(in_dev);
188 while ((ifa = in_dev->ifa_list) != NULL) {
189 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
194 devinet_sysctl_unregister(&in_dev->cnf);
196 write_lock_bh(&inetdev_lock);
197 in_dev->dev->ip_ptr = NULL;
198 /* in_dev_put following below will kill the in_device */
199 write_unlock_bh(&inetdev_lock);
202 neigh_sysctl_unregister(in_dev->arp_parms);
204 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
208 int inet_addr_onlink(struct in_device *in_dev, u32 a, u32 b)
210 read_lock(&in_dev->lock);
211 for_primary_ifa(in_dev) {
212 if (inet_ifa_match(a, ifa)) {
213 if (!b || inet_ifa_match(b, ifa)) {
214 read_unlock(&in_dev->lock);
218 } endfor_ifa(in_dev);
219 read_unlock(&in_dev->lock);
223 static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
226 struct in_ifaddr *ifa1 = *ifap;
230 /* 1. Deleting primary ifaddr forces deletion all secondaries */
232 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
233 struct in_ifaddr *ifa;
234 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
236 while ((ifa = *ifap1) != NULL) {
237 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
238 ifa1->ifa_mask != ifa->ifa_mask ||
239 !inet_ifa_match(ifa1->ifa_address, ifa)) {
240 ifap1 = &ifa->ifa_next;
243 write_lock_bh(&in_dev->lock);
244 *ifap1 = ifa->ifa_next;
245 write_unlock_bh(&in_dev->lock);
247 rtmsg_ifa(RTM_DELADDR, ifa);
248 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa);
255 write_lock_bh(&in_dev->lock);
256 *ifap = ifa1->ifa_next;
257 write_unlock_bh(&in_dev->lock);
259 /* 3. Announce address deletion */
261 /* Send message first, then call notifier.
262 At first sight, FIB update triggered by notifier
263 will refer to already deleted ifaddr, that could confuse
264 netlink listeners. It is not true: look, gated sees
265 that route deleted and if it still thinks that ifaddr
266 is valid, it will try to restore deleted routes... Grr.
267 So that, this order is correct.
269 rtmsg_ifa(RTM_DELADDR, ifa1);
270 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
274 if (!in_dev->ifa_list)
275 inetdev_destroy(in_dev);
279 static int inet_insert_ifa(struct in_ifaddr *ifa)
281 struct in_device *in_dev = ifa->ifa_dev;
282 struct in_ifaddr *ifa1, **ifap, **last_primary;
286 if (!ifa->ifa_local) {
291 ifa->ifa_flags &= ~IFA_F_SECONDARY;
292 last_primary = &in_dev->ifa_list;
294 for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
295 ifap = &ifa1->ifa_next) {
296 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
297 ifa->ifa_scope <= ifa1->ifa_scope)
298 last_primary = &ifa1->ifa_next;
299 if (ifa1->ifa_mask == ifa->ifa_mask &&
300 inet_ifa_match(ifa1->ifa_address, ifa)) {
301 if (ifa1->ifa_local == ifa->ifa_local) {
305 if (ifa1->ifa_scope != ifa->ifa_scope) {
309 ifa->ifa_flags |= IFA_F_SECONDARY;
313 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
314 net_srandom(ifa->ifa_local);
318 ifa->ifa_next = *ifap;
319 write_lock_bh(&in_dev->lock);
321 write_unlock_bh(&in_dev->lock);
323 /* Send message first, then call notifier.
324 Notifier will trigger FIB update, so that
325 listeners of netlink will know about new ifaddr */
326 rtmsg_ifa(RTM_NEWADDR, ifa);
327 notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
332 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
334 struct in_device *in_dev = __in_dev_get(dev);
339 in_dev = inetdev_init(dev);
345 if (ifa->ifa_dev != in_dev) {
346 BUG_TRAP(!ifa->ifa_dev);
348 ifa->ifa_dev = in_dev;
350 if (LOOPBACK(ifa->ifa_local))
351 ifa->ifa_scope = RT_SCOPE_HOST;
352 return inet_insert_ifa(ifa);
355 struct in_device *inetdev_by_index(int ifindex)
357 struct net_device *dev;
358 struct in_device *in_dev = NULL;
359 read_lock(&dev_base_lock);
360 dev = __dev_get_by_index(ifindex);
362 in_dev = in_dev_get(dev);
363 read_unlock(&dev_base_lock);
367 /* Called only from RTNL semaphored context. No locks. */
369 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, u32 prefix,
374 for_primary_ifa(in_dev) {
375 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
377 } endfor_ifa(in_dev);
381 int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
383 struct rtattr **rta = arg;
384 struct in_device *in_dev;
385 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
386 struct in_ifaddr *ifa, **ifap;
390 if ((in_dev = inetdev_by_index(ifm->ifa_index)) == NULL)
392 __in_dev_put(in_dev);
394 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
395 ifap = &ifa->ifa_next) {
396 if ((rta[IFA_LOCAL - 1] &&
397 memcmp(RTA_DATA(rta[IFA_LOCAL - 1]),
398 &ifa->ifa_local, 4)) ||
399 (rta[IFA_LABEL - 1] &&
400 strcmp(RTA_DATA(rta[IFA_LABEL - 1]), ifa->ifa_label)) ||
401 (rta[IFA_ADDRESS - 1] &&
402 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
403 !inet_ifa_match(*(u32*)RTA_DATA(rta[IFA_ADDRESS - 1]),
406 inet_del_ifa(in_dev, ifap, 1);
410 return -EADDRNOTAVAIL;
413 int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
415 struct rtattr **rta = arg;
416 struct net_device *dev;
417 struct in_device *in_dev;
418 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
419 struct in_ifaddr *ifa;
424 if (ifm->ifa_prefixlen > 32 || !rta[IFA_LOCAL - 1])
428 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
432 if ((in_dev = __in_dev_get(dev)) == NULL) {
433 in_dev = inetdev_init(dev);
438 if ((ifa = inet_alloc_ifa()) == NULL)
441 if (!rta[IFA_ADDRESS - 1])
442 rta[IFA_ADDRESS - 1] = rta[IFA_LOCAL - 1];
443 memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL - 1]), 4);
444 memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS - 1]), 4);
445 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
446 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
447 if (rta[IFA_BROADCAST - 1])
448 memcpy(&ifa->ifa_broadcast,
449 RTA_DATA(rta[IFA_BROADCAST - 1]), 4);
450 if (rta[IFA_ANYCAST - 1])
451 memcpy(&ifa->ifa_anycast, RTA_DATA(rta[IFA_ANYCAST - 1]), 4);
452 ifa->ifa_flags = ifm->ifa_flags;
453 ifa->ifa_scope = ifm->ifa_scope;
455 ifa->ifa_dev = in_dev;
456 if (rta[IFA_LABEL - 1])
457 memcpy(ifa->ifa_label, RTA_DATA(rta[IFA_LABEL - 1]), IFNAMSIZ);
459 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
461 rc = inet_insert_ifa(ifa);
467 * Determine a default network mask, based on the IP address.
470 static __inline__ int inet_abc_len(u32 addr)
472 int rc = -1; /* Something else, probably a multicast. */
481 else if (IN_CLASSB(addr))
483 else if (IN_CLASSC(addr))
491 int devinet_ioctl(unsigned int cmd, void *arg)
494 struct sockaddr_in sin_orig;
495 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
496 struct in_device *in_dev;
497 struct in_ifaddr **ifap = NULL;
498 struct in_ifaddr *ifa = NULL;
499 struct net_device *dev;
502 int tryaddrmatch = 0;
505 * Fetch the caller's info block into kernel space
508 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
510 ifr.ifr_name[IFNAMSIZ - 1] = 0;
512 /* save original address for comparison */
513 memcpy(&sin_orig, sin, sizeof(*sin));
515 colon = strchr(ifr.ifr_name, ':');
520 dev_load(ifr.ifr_name);
524 case SIOCGIFADDR: /* Get interface address */
525 case SIOCGIFBRDADDR: /* Get the broadcast address */
526 case SIOCGIFDSTADDR: /* Get the destination address */
527 case SIOCGIFNETMASK: /* Get the netmask for the interface */
528 /* Note that these ioctls will not sleep,
529 so that we do not impose a lock.
530 One day we will be forced to put shlock here (I mean SMP)
532 tryaddrmatch = (sin_orig.sin_family == AF_INET);
533 memset(sin, 0, sizeof(*sin));
534 sin->sin_family = AF_INET;
539 if (!capable(CAP_NET_ADMIN))
542 case SIOCSIFADDR: /* Set interface address (and family) */
543 case SIOCSIFBRDADDR: /* Set the broadcast address */
544 case SIOCSIFDSTADDR: /* Set the destination address */
545 case SIOCSIFNETMASK: /* Set the netmask for the interface */
547 if (!capable(CAP_NET_ADMIN))
550 if (sin->sin_family != AF_INET)
561 if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL)
567 if ((in_dev = __in_dev_get(dev)) != NULL) {
569 /* Matthias Andree */
570 /* compare label and address (4.4BSD style) */
571 /* note: we only do this for a limited set of ioctls
572 and only if the original address family was AF_INET.
573 This is checked above. */
574 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
575 ifap = &ifa->ifa_next) {
576 if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
577 sin_orig.sin_addr.s_addr ==
583 /* we didn't get a match, maybe the application is
584 4.3BSD-style and passed in junk so we fall back to
585 comparing just the label */
587 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
588 ifap = &ifa->ifa_next)
589 if (!strcmp(ifr.ifr_name, ifa->ifa_label))
594 ret = -EADDRNOTAVAIL;
595 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
599 case SIOCGIFADDR: /* Get interface address */
600 sin->sin_addr.s_addr = ifa->ifa_local;
603 case SIOCGIFBRDADDR: /* Get the broadcast address */
604 sin->sin_addr.s_addr = ifa->ifa_broadcast;
607 case SIOCGIFDSTADDR: /* Get the destination address */
608 sin->sin_addr.s_addr = ifa->ifa_address;
611 case SIOCGIFNETMASK: /* Get the netmask for the interface */
612 sin->sin_addr.s_addr = ifa->ifa_mask;
617 ret = -EADDRNOTAVAIL;
621 if (!(ifr.ifr_flags & IFF_UP))
622 inet_del_ifa(in_dev, ifap, 1);
625 ret = dev_change_flags(dev, ifr.ifr_flags);
628 case SIOCSIFADDR: /* Set interface address (and family) */
630 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
635 if ((ifa = inet_alloc_ifa()) == NULL)
638 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
640 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
643 if (ifa->ifa_local == sin->sin_addr.s_addr)
645 inet_del_ifa(in_dev, ifap, 0);
646 ifa->ifa_broadcast = 0;
647 ifa->ifa_anycast = 0;
650 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
652 if (!(dev->flags & IFF_POINTOPOINT)) {
653 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
654 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
655 if ((dev->flags & IFF_BROADCAST) &&
656 ifa->ifa_prefixlen < 31)
657 ifa->ifa_broadcast = ifa->ifa_address |
660 ifa->ifa_prefixlen = 32;
661 ifa->ifa_mask = inet_make_mask(32);
663 ret = inet_set_ifa(dev, ifa);
666 case SIOCSIFBRDADDR: /* Set the broadcast address */
668 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
669 inet_del_ifa(in_dev, ifap, 0);
670 ifa->ifa_broadcast = sin->sin_addr.s_addr;
671 inet_insert_ifa(ifa);
675 case SIOCSIFDSTADDR: /* Set the destination address */
677 if (ifa->ifa_address == sin->sin_addr.s_addr)
680 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
683 inet_del_ifa(in_dev, ifap, 0);
684 ifa->ifa_address = sin->sin_addr.s_addr;
685 inet_insert_ifa(ifa);
688 case SIOCSIFNETMASK: /* Set the netmask for the interface */
691 * The mask we set must be legal.
694 if (bad_mask(sin->sin_addr.s_addr, 0))
697 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
698 inet_del_ifa(in_dev, ifap, 0);
699 ifa->ifa_mask = sin->sin_addr.s_addr;
700 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
701 inet_insert_ifa(ifa);
711 ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
715 static int inet_gifconf(struct net_device *dev, char *buf, int len)
717 struct in_device *in_dev = __in_dev_get(dev);
718 struct in_ifaddr *ifa;
722 if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
725 for (; ifa; ifa = ifa->ifa_next) {
730 if (len < (int) sizeof(ifr))
732 memset(&ifr, 0, sizeof(struct ifreq));
734 strcpy(ifr.ifr_name, ifa->ifa_label);
736 strcpy(ifr.ifr_name, dev->name);
738 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
739 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
742 if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
746 buf += sizeof(struct ifreq);
747 len -= sizeof(struct ifreq);
748 done += sizeof(struct ifreq);
754 u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
757 struct in_device *in_dev;
759 read_lock(&inetdev_lock);
760 in_dev = __in_dev_get(dev);
762 goto out_unlock_inetdev;
764 read_lock(&in_dev->lock);
765 for_primary_ifa(in_dev) {
766 if (ifa->ifa_scope > scope)
768 if (!dst || inet_ifa_match(dst, ifa)) {
769 addr = ifa->ifa_local;
773 addr = ifa->ifa_local;
774 } endfor_ifa(in_dev);
775 read_unlock(&in_dev->lock);
776 read_unlock(&inetdev_lock);
781 /* Not loopback addresses on loopback should be preferred
782 in this case. It is importnat that lo is the first interface
785 read_lock(&dev_base_lock);
786 read_lock(&inetdev_lock);
787 for (dev = dev_base; dev; dev = dev->next) {
788 if ((in_dev = __in_dev_get(dev)) == NULL)
791 read_lock(&in_dev->lock);
792 for_primary_ifa(in_dev) {
793 if (ifa->ifa_scope != RT_SCOPE_LINK &&
794 ifa->ifa_scope <= scope) {
795 read_unlock(&in_dev->lock);
796 addr = ifa->ifa_local;
797 goto out_unlock_both;
799 } endfor_ifa(in_dev);
800 read_unlock(&in_dev->lock);
803 read_unlock(&inetdev_lock);
804 read_unlock(&dev_base_lock);
808 read_unlock(&inetdev_lock);
816 int register_inetaddr_notifier(struct notifier_block *nb)
818 return notifier_chain_register(&inetaddr_chain, nb);
821 int unregister_inetaddr_notifier(struct notifier_block *nb)
823 return notifier_chain_unregister(&inetaddr_chain, nb);
826 /* Rename ifa_labels for a device name change. Make some effort to preserve existing
827 * alias numbering and to create unique labels if possible.
829 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
831 struct in_ifaddr *ifa;
834 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
835 char old[IFNAMSIZ], *dot;
837 memcpy(old, ifa->ifa_label, IFNAMSIZ);
838 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
841 dot = strchr(ifa->ifa_label, ':');
843 sprintf(old, ":%d", named);
846 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) {
847 strcat(ifa->ifa_label, dot);
849 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
854 /* Called only under RTNL semaphore */
856 static int inetdev_event(struct notifier_block *this, unsigned long event,
859 struct net_device *dev = ptr;
860 struct in_device *in_dev = __in_dev_get(dev);
868 case NETDEV_REGISTER:
869 printk(KERN_DEBUG "inetdev_event: bug\n");
875 if (dev == &loopback_dev) {
876 struct in_ifaddr *ifa;
877 if ((ifa = inet_alloc_ifa()) != NULL) {
879 ifa->ifa_address = htonl(INADDR_LOOPBACK);
880 ifa->ifa_prefixlen = 8;
881 ifa->ifa_mask = inet_make_mask(8);
883 ifa->ifa_dev = in_dev;
884 ifa->ifa_scope = RT_SCOPE_HOST;
885 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
886 inet_insert_ifa(ifa);
888 in_dev->cnf.no_xfrm = 1;
889 in_dev->cnf.no_policy = 1;
896 case NETDEV_CHANGEMTU:
899 /* MTU falled under 68, disable IP */
900 case NETDEV_UNREGISTER:
901 inetdev_destroy(in_dev);
903 case NETDEV_CHANGENAME:
904 /* Do not notify about label change, this event is
905 * not interesting to applications using netlink.
907 inetdev_changename(dev, in_dev);
910 devinet_sysctl_unregister(&in_dev->cnf);
911 neigh_sysctl_unregister(in_dev->arp_parms);
912 neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
913 NET_IPV4_NEIGH, "ipv4", NULL);
914 devinet_sysctl_register(in_dev, &in_dev->cnf);
922 struct notifier_block ip_netdev_notifier = {
923 .notifier_call =inetdev_event,
926 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
927 u32 pid, u32 seq, int event)
929 struct ifaddrmsg *ifm;
930 struct nlmsghdr *nlh;
931 unsigned char *b = skb->tail;
933 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
934 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
935 ifm = NLMSG_DATA(nlh);
936 ifm->ifa_family = AF_INET;
937 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
938 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
939 ifm->ifa_scope = ifa->ifa_scope;
940 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
941 if (ifa->ifa_address)
942 RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
944 RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
945 if (ifa->ifa_broadcast)
946 RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
947 if (ifa->ifa_anycast)
948 RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
949 if (ifa->ifa_label[0])
950 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
951 nlh->nlmsg_len = skb->tail - b;
956 skb_trim(skb, b - skb->data);
960 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
963 struct net_device *dev;
964 struct in_device *in_dev;
965 struct in_ifaddr *ifa;
966 int s_ip_idx, s_idx = cb->args[0];
968 s_ip_idx = ip_idx = cb->args[1];
969 read_lock(&dev_base_lock);
970 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
975 read_lock(&inetdev_lock);
976 if ((in_dev = __in_dev_get(dev)) == NULL) {
977 read_unlock(&inetdev_lock);
980 read_lock(&in_dev->lock);
981 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
982 ifa = ifa->ifa_next, ip_idx++) {
983 if (ip_idx < s_ip_idx)
985 if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
988 read_unlock(&in_dev->lock);
989 read_unlock(&inetdev_lock);
993 read_unlock(&in_dev->lock);
994 read_unlock(&inetdev_lock);
998 read_unlock(&dev_base_lock);
1000 cb->args[1] = ip_idx;
1005 static void rtmsg_ifa(int event, struct in_ifaddr* ifa)
1007 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + 128);
1008 struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);
1011 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, ENOBUFS);
1012 else if (inet_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
1014 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, EINVAL);
1016 NETLINK_CB(skb).dst_groups = RTMGRP_IPV4_IFADDR;
1017 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV4_IFADDR, GFP_KERNEL);
1021 static struct rtnetlink_link inet_rtnetlink_table[RTM_MAX - RTM_BASE + 1] = {
1022 [4] = { .doit = inet_rtm_newaddr, },
1023 [5] = { .doit = inet_rtm_deladdr, },
1024 [6] = { .dumpit = inet_dump_ifaddr, },
1025 [8] = { .doit = inet_rtm_newroute, },
1026 [9] = { .doit = inet_rtm_delroute, },
1027 [10] = { .doit = inet_rtm_getroute, .dumpit = inet_dump_fib, },
1028 #ifdef CONFIG_IP_MULTIPLE_TABLES
1029 [16] = { .doit = inet_rtm_newrule, },
1030 [17] = { .doit = inet_rtm_delrule, },
1031 [18] = { .dumpit = inet_dump_rules, },
1035 #ifdef CONFIG_SYSCTL
1037 void inet_forward_change(void)
1039 struct net_device *dev;
1040 int on = ipv4_devconf.forwarding;
1042 ipv4_devconf.accept_redirects = !on;
1043 ipv4_devconf_dflt.forwarding = on;
1045 read_lock(&dev_base_lock);
1046 for (dev = dev_base; dev; dev = dev->next) {
1047 struct in_device *in_dev;
1048 read_lock(&inetdev_lock);
1049 in_dev = __in_dev_get(dev);
1051 in_dev->cnf.forwarding = on;
1052 read_unlock(&inetdev_lock);
1054 read_unlock(&dev_base_lock);
1059 static int devinet_sysctl_forward(ctl_table *ctl, int write,
1060 struct file* filp, void *buffer,
1063 int *valp = ctl->data;
1065 int ret = proc_dointvec(ctl, write, filp, buffer, lenp);
1067 if (write && *valp != val) {
1068 if (valp == &ipv4_devconf.forwarding)
1069 inet_forward_change();
1070 else if (valp != &ipv4_devconf_dflt.forwarding)
1077 int ipv4_doint_and_flush(ctl_table *ctl, int write,
1078 struct file* filp, void *buffer,
1081 int *valp = ctl->data;
1083 int ret = proc_dointvec(ctl, write, filp, buffer, lenp);
1085 if (write && *valp != val)
1091 int ipv4_doint_and_flush_strategy(ctl_table *table, int *name, int nlen,
1092 void *oldval, size_t *oldlenp,
1093 void *newval, size_t newlen,
1096 int *valp = table->data;
1099 if (!newval || !newlen)
1102 if (newlen != sizeof(int))
1105 if (get_user(new, (int *)newval))
1111 if (oldval && oldlenp) {
1114 if (get_user(len, oldlenp))
1118 if (len > table->maxlen)
1119 len = table->maxlen;
1120 if (copy_to_user(oldval, valp, len))
1122 if (put_user(len, oldlenp))
1133 static struct devinet_sysctl_table {
1134 struct ctl_table_header *sysctl_header;
1135 ctl_table devinet_vars[18];
1136 ctl_table devinet_dev[2];
1137 ctl_table devinet_conf_dir[2];
1138 ctl_table devinet_proto_dir[2];
1139 ctl_table devinet_root_dir[2];
1140 } devinet_sysctl = {
1143 .ctl_name = NET_IPV4_CONF_FORWARDING,
1144 .procname = "forwarding",
1145 .data = &ipv4_devconf.forwarding,
1146 .maxlen = sizeof(int),
1148 .proc_handler = &devinet_sysctl_forward,
1151 .ctl_name = NET_IPV4_CONF_MC_FORWARDING,
1152 .procname = "mc_forwarding",
1153 .data = &ipv4_devconf.mc_forwarding,
1154 .maxlen = sizeof(int),
1156 .proc_handler = &proc_dointvec,
1159 .ctl_name = NET_IPV4_CONF_ACCEPT_REDIRECTS,
1160 .procname = "accept_redirects",
1161 .data = &ipv4_devconf.accept_redirects,
1162 .maxlen = sizeof(int),
1164 .proc_handler = &proc_dointvec,
1167 .ctl_name = NET_IPV4_CONF_SECURE_REDIRECTS,
1168 .procname = "secure_redirects",
1169 .data = &ipv4_devconf.secure_redirects,
1170 .maxlen = sizeof(int),
1172 .proc_handler = &proc_dointvec,
1175 .ctl_name = NET_IPV4_CONF_SHARED_MEDIA,
1176 .procname = "shared_media",
1177 .data = &ipv4_devconf.shared_media,
1178 .maxlen = sizeof(int),
1180 .proc_handler = &proc_dointvec,
1183 .ctl_name = NET_IPV4_CONF_RP_FILTER,
1184 .procname = "rp_filter",
1185 .data = &ipv4_devconf.rp_filter,
1186 .maxlen = sizeof(int),
1188 .proc_handler = &proc_dointvec,
1191 .ctl_name = NET_IPV4_CONF_SEND_REDIRECTS,
1192 .procname = "send_redirects",
1193 .data = &ipv4_devconf.send_redirects,
1194 .maxlen = sizeof(int),
1196 .proc_handler = &proc_dointvec,
1199 .ctl_name = NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE,
1200 .procname = "accept_source_route",
1201 .data = &ipv4_devconf.accept_source_route,
1202 .maxlen = sizeof(int),
1204 .proc_handler = &proc_dointvec,
1207 .ctl_name = NET_IPV4_CONF_PROXY_ARP,
1208 .procname = "proxy_arp",
1209 .data = &ipv4_devconf.proxy_arp,
1210 .maxlen = sizeof(int),
1212 .proc_handler = &proc_dointvec,
1215 .ctl_name = NET_IPV4_CONF_MEDIUM_ID,
1216 .procname = "medium_id",
1217 .data = &ipv4_devconf.medium_id,
1218 .maxlen = sizeof(int),
1220 .proc_handler = &proc_dointvec,
1223 .ctl_name = NET_IPV4_CONF_BOOTP_RELAY,
1224 .procname = "bootp_relay",
1225 .data = &ipv4_devconf.bootp_relay,
1226 .maxlen = sizeof(int),
1228 .proc_handler = &proc_dointvec,
1231 .ctl_name = NET_IPV4_CONF_LOG_MARTIANS,
1232 .procname = "log_martians",
1233 .data = &ipv4_devconf.log_martians,
1234 .maxlen = sizeof(int),
1236 .proc_handler = &proc_dointvec,
1239 .ctl_name = NET_IPV4_CONF_TAG,
1241 .data = &ipv4_devconf.tag,
1242 .maxlen = sizeof(int),
1244 .proc_handler = &proc_dointvec,
1247 .ctl_name = NET_IPV4_CONF_ARPFILTER,
1248 .procname = "arp_filter",
1249 .data = &ipv4_devconf.arp_filter,
1250 .maxlen = sizeof(int),
1252 .proc_handler = &proc_dointvec,
1255 .ctl_name = NET_IPV4_CONF_NOXFRM,
1256 .procname = "disable_xfrm",
1257 .data = &ipv4_devconf.no_xfrm,
1258 .maxlen = sizeof(int),
1260 .proc_handler = &ipv4_doint_and_flush,
1261 .strategy = &ipv4_doint_and_flush_strategy,
1264 .ctl_name = NET_IPV4_CONF_NOPOLICY,
1265 .procname = "disable_policy",
1266 .data = &ipv4_devconf.no_policy,
1267 .maxlen = sizeof(int),
1269 .proc_handler = &ipv4_doint_and_flush,
1270 .strategy = &ipv4_doint_and_flush_strategy,
1273 .ctl_name = NET_IPV4_CONF_FORCE_IGMP_VERSION,
1274 .procname = "force_igmp_version",
1275 .data = &ipv4_devconf.force_igmp_version,
1276 .maxlen = sizeof(int),
1278 .proc_handler = &ipv4_doint_and_flush,
1279 .strategy = &ipv4_doint_and_flush_strategy,
1284 .ctl_name = NET_PROTO_CONF_ALL,
1287 .child = devinet_sysctl.devinet_vars,
1290 .devinet_conf_dir = {
1292 .ctl_name = NET_IPV4_CONF,
1295 .child = devinet_sysctl.devinet_dev,
1298 .devinet_proto_dir = {
1300 .ctl_name = NET_IPV4,
1303 .child = devinet_sysctl.devinet_conf_dir,
1306 .devinet_root_dir = {
1308 .ctl_name = CTL_NET,
1311 .child = devinet_sysctl.devinet_proto_dir,
1316 static void devinet_sysctl_register(struct in_device *in_dev,
1317 struct ipv4_devconf *p)
1320 struct net_device *dev = in_dev ? in_dev->dev : NULL;
1321 struct devinet_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
1322 char *dev_name = NULL;
1326 memcpy(t, &devinet_sysctl, sizeof(*t));
1327 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
1328 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
1329 t->devinet_vars[i].de = NULL;
1333 dev_name = dev->name;
1334 t->devinet_dev[0].ctl_name = dev->ifindex;
1336 dev_name = "default";
1337 t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
1341 * Make a copy of dev_name, because '.procname' is regarded as const
1342 * by sysctl and we wouldn't want anyone to change it under our feet
1343 * (see SIOCSIFNAME).
1345 dev_name = net_sysctl_strdup(dev_name);
1349 t->devinet_dev[0].procname = dev_name;
1350 t->devinet_dev[0].child = t->devinet_vars;
1351 t->devinet_dev[0].de = NULL;
1352 t->devinet_conf_dir[0].child = t->devinet_dev;
1353 t->devinet_conf_dir[0].de = NULL;
1354 t->devinet_proto_dir[0].child = t->devinet_conf_dir;
1355 t->devinet_proto_dir[0].de = NULL;
1356 t->devinet_root_dir[0].child = t->devinet_proto_dir;
1357 t->devinet_root_dir[0].de = NULL;
1359 t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
1360 if (!t->sysctl_header)
1374 static void devinet_sysctl_unregister(struct ipv4_devconf *p)
1377 struct devinet_sysctl_table *t = p->sysctl;
1379 unregister_sysctl_table(t->sysctl_header);
1380 kfree(t->devinet_dev[0].procname);
1386 void __init devinet_init(void)
1388 register_gifconf(PF_INET, inet_gifconf);
1389 register_netdevice_notifier(&ip_netdev_notifier);
1390 rtnetlink_links[PF_INET] = inet_rtnetlink_table;
1391 #ifdef CONFIG_SYSCTL
1392 devinet_sysctl.sysctl_header =
1393 register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
1394 devinet_sysctl_register(NULL, &ipv4_devconf_dflt);
1398 EXPORT_SYMBOL(devinet_ioctl);
1399 EXPORT_SYMBOL(in_dev_finish_destroy);
1400 EXPORT_SYMBOL(inet_select_addr);
1401 EXPORT_SYMBOL(inetdev_by_index);
1402 EXPORT_SYMBOL(inetdev_lock);
1403 EXPORT_SYMBOL(register_inetaddr_notifier);
1404 EXPORT_SYMBOL(unregister_inetaddr_notifier);