2 * NET3 IP device support routines.
4 * Version: $Id: devinet.c,v 1.1.1.1 2005/04/11 02:51:13 jack 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 if (dev->flags&IFF_UP)
159 static void inetdev_destroy(struct in_device *in_dev)
161 struct in_ifaddr *ifa;
167 ip_mc_destroy_dev(in_dev);
169 while ((ifa = in_dev->ifa_list) != NULL) {
170 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
175 devinet_sysctl_unregister(&in_dev->cnf);
177 write_lock_bh(&inetdev_lock);
178 in_dev->dev->ip_ptr = NULL;
179 /* in_dev_put following below will kill the in_device */
180 write_unlock_bh(&inetdev_lock);
183 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
187 int inet_addr_onlink(struct in_device *in_dev, u32 a, u32 b)
189 read_lock(&in_dev->lock);
190 for_primary_ifa(in_dev) {
191 if (inet_ifa_match(a, ifa)) {
192 if (!b || inet_ifa_match(b, ifa)) {
193 read_unlock(&in_dev->lock);
197 } endfor_ifa(in_dev);
198 read_unlock(&in_dev->lock);
203 inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, int destroy)
205 struct in_ifaddr *ifa1 = *ifap;
209 /* 1. Deleting primary ifaddr forces deletion all secondaries */
211 if (!(ifa1->ifa_flags&IFA_F_SECONDARY)) {
212 struct in_ifaddr *ifa;
213 struct in_ifaddr **ifap1 = &ifa1->ifa_next;
215 while ((ifa=*ifap1) != NULL) {
216 if (!(ifa->ifa_flags&IFA_F_SECONDARY) ||
217 ifa1->ifa_mask != ifa->ifa_mask ||
218 !inet_ifa_match(ifa1->ifa_address, ifa)) {
219 ifap1 = &ifa->ifa_next;
222 write_lock_bh(&in_dev->lock);
223 *ifap1 = ifa->ifa_next;
224 write_unlock_bh(&in_dev->lock);
226 rtmsg_ifa(RTM_DELADDR, ifa);
227 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa);
234 write_lock_bh(&in_dev->lock);
235 *ifap = ifa1->ifa_next;
236 write_unlock_bh(&in_dev->lock);
238 /* 3. Announce address deletion */
240 /* Send message first, then call notifier.
241 At first sight, FIB update triggered by notifier
242 will refer to already deleted ifaddr, that could confuse
243 netlink listeners. It is not true: look, gated sees
244 that route deleted and if it still thinks that ifaddr
245 is valid, it will try to restore deleted routes... Grr.
246 So that, this order is correct.
248 rtmsg_ifa(RTM_DELADDR, ifa1);
249 notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
253 if (in_dev->ifa_list == NULL)
254 inetdev_destroy(in_dev);
259 inet_insert_ifa(struct in_ifaddr *ifa)
261 struct in_device *in_dev = ifa->ifa_dev;
262 struct in_ifaddr *ifa1, **ifap, **last_primary;
266 if (ifa->ifa_local == 0) {
271 ifa->ifa_flags &= ~IFA_F_SECONDARY;
272 last_primary = &in_dev->ifa_list;
274 for (ifap=&in_dev->ifa_list; (ifa1=*ifap)!=NULL; ifap=&ifa1->ifa_next) {
275 if (!(ifa1->ifa_flags&IFA_F_SECONDARY) && ifa->ifa_scope <= ifa1->ifa_scope)
276 last_primary = &ifa1->ifa_next;
277 if (ifa1->ifa_mask == ifa->ifa_mask && inet_ifa_match(ifa1->ifa_address, ifa)) {
278 if (ifa1->ifa_local == ifa->ifa_local) {
282 if (ifa1->ifa_scope != ifa->ifa_scope) {
286 ifa->ifa_flags |= IFA_F_SECONDARY;
290 if (!(ifa->ifa_flags&IFA_F_SECONDARY)) {
291 net_srandom(ifa->ifa_local);
295 ifa->ifa_next = *ifap;
296 write_lock_bh(&in_dev->lock);
298 write_unlock_bh(&in_dev->lock);
300 /* Send message first, then call notifier.
301 Notifier will trigger FIB update, so that
302 listeners of netlink will know about new ifaddr */
303 rtmsg_ifa(RTM_NEWADDR, ifa);
304 notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
310 inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
312 struct in_device *in_dev = __in_dev_get(dev);
316 if (in_dev == NULL) {
317 in_dev = inetdev_init(dev);
318 if (in_dev == NULL) {
323 if (ifa->ifa_dev != in_dev) {
324 BUG_TRAP(ifa->ifa_dev==NULL);
328 if (LOOPBACK(ifa->ifa_local))
329 ifa->ifa_scope = RT_SCOPE_HOST;
330 return inet_insert_ifa(ifa);
333 struct in_device *inetdev_by_index(int ifindex)
335 struct net_device *dev;
336 struct in_device *in_dev = NULL;
337 read_lock(&dev_base_lock);
338 dev = __dev_get_by_index(ifindex);
340 in_dev = in_dev_get(dev);
341 read_unlock(&dev_base_lock);
345 /* Called only from RTNL semaphored context. No locks. */
347 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, u32 prefix, u32 mask)
351 for_primary_ifa(in_dev) {
352 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
354 } endfor_ifa(in_dev);
359 inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
361 struct rtattr **rta = arg;
362 struct in_device *in_dev;
363 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
364 struct in_ifaddr *ifa, **ifap;
368 if ((in_dev = inetdev_by_index(ifm->ifa_index)) == NULL)
369 return -EADDRNOTAVAIL;
370 __in_dev_put(in_dev);
372 for (ifap=&in_dev->ifa_list; (ifa=*ifap)!=NULL; ifap=&ifa->ifa_next) {
373 if ((rta[IFA_LOCAL-1] && memcmp(RTA_DATA(rta[IFA_LOCAL-1]), &ifa->ifa_local, 4)) ||
374 (rta[IFA_LABEL-1] && strcmp(RTA_DATA(rta[IFA_LABEL-1]), ifa->ifa_label)) ||
375 (rta[IFA_ADDRESS-1] &&
376 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
377 !inet_ifa_match(*(u32*)RTA_DATA(rta[IFA_ADDRESS-1]), ifa))))
379 inet_del_ifa(in_dev, ifap, 1);
383 return -EADDRNOTAVAIL;
387 inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
389 struct rtattr **rta = arg;
390 struct net_device *dev;
391 struct in_device *in_dev;
392 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
393 struct in_ifaddr *ifa;
397 if (ifm->ifa_prefixlen > 32 || rta[IFA_LOCAL-1] == NULL)
400 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
403 if ((in_dev = __in_dev_get(dev)) == NULL) {
404 in_dev = inetdev_init(dev);
409 if ((ifa = inet_alloc_ifa()) == NULL)
412 if (rta[IFA_ADDRESS-1] == NULL)
413 rta[IFA_ADDRESS-1] = rta[IFA_LOCAL-1];
414 memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL-1]), 4);
415 memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS-1]), 4);
416 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
417 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
418 if (rta[IFA_BROADCAST-1])
419 memcpy(&ifa->ifa_broadcast, RTA_DATA(rta[IFA_BROADCAST-1]), 4);
420 if (rta[IFA_ANYCAST-1])
421 memcpy(&ifa->ifa_anycast, RTA_DATA(rta[IFA_ANYCAST-1]), 4);
422 ifa->ifa_flags = ifm->ifa_flags;
423 ifa->ifa_scope = ifm->ifa_scope;
425 ifa->ifa_dev = in_dev;
426 if (rta[IFA_LABEL-1])
427 memcpy(ifa->ifa_label, RTA_DATA(rta[IFA_LABEL-1]), IFNAMSIZ);
429 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
431 return inet_insert_ifa(ifa);
435 * Determine a default network mask, based on the IP address.
438 static __inline__ int inet_abc_len(u32 addr)
452 * Something else, probably a multicast.
459 int devinet_ioctl(unsigned int cmd, void *arg)
462 struct sockaddr_in sin_orig;
463 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
464 struct in_device *in_dev;
465 struct in_ifaddr **ifap = NULL;
466 struct in_ifaddr *ifa = NULL;
467 struct net_device *dev;
470 int tryaddrmatch = 0;
473 * Fetch the caller's info block into kernel space
476 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
478 ifr.ifr_name[IFNAMSIZ-1] = 0;
480 /* save original address for comparison */
481 memcpy(&sin_orig, sin, sizeof(*sin));
483 colon = strchr(ifr.ifr_name, ':');
488 dev_load(ifr.ifr_name);
492 case SIOCGIFADDR: /* Get interface address */
493 case SIOCGIFBRDADDR: /* Get the broadcast address */
494 case SIOCGIFDSTADDR: /* Get the destination address */
495 case SIOCGIFNETMASK: /* Get the netmask for the interface */
496 /* Note that these ioctls will not sleep,
497 so that we do not impose a lock.
498 One day we will be forced to put shlock here (I mean SMP)
500 tryaddrmatch = (sin_orig.sin_family == AF_INET);
501 memset(sin, 0, sizeof(*sin));
502 sin->sin_family = AF_INET;
506 if (!capable(CAP_NET_ADMIN))
509 case SIOCSIFADDR: /* Set interface address (and family) */
510 case SIOCSIFBRDADDR: /* Set the broadcast address */
511 case SIOCSIFDSTADDR: /* Set the destination address */
512 case SIOCSIFNETMASK: /* Set the netmask for the interface */
513 if (!capable(CAP_NET_ADMIN))
515 if (sin->sin_family != AF_INET)
525 if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL) {
533 if ((in_dev=__in_dev_get(dev)) != NULL) {
535 /* Matthias Andree */
536 /* compare label and address (4.4BSD style) */
537 /* note: we only do this for a limited set of ioctls
538 and only if the original address family was AF_INET.
539 This is checked above. */
540 for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next) {
541 if ((strcmp(ifr.ifr_name, ifa->ifa_label) == 0)
542 && (sin_orig.sin_addr.s_addr == ifa->ifa_address)) {
547 /* we didn't get a match, maybe the application is
548 4.3BSD-style and passed in junk so we fall back to
549 comparing just the label */
551 for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next)
552 if (strcmp(ifr.ifr_name, ifa->ifa_label) == 0)
557 if (ifa == NULL && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS) {
558 ret = -EADDRNOTAVAIL;
563 case SIOCGIFADDR: /* Get interface address */
564 sin->sin_addr.s_addr = ifa->ifa_local;
567 case SIOCGIFBRDADDR: /* Get the broadcast address */
568 sin->sin_addr.s_addr = ifa->ifa_broadcast;
571 case SIOCGIFDSTADDR: /* Get the destination address */
572 sin->sin_addr.s_addr = ifa->ifa_address;
575 case SIOCGIFNETMASK: /* Get the netmask for the interface */
576 sin->sin_addr.s_addr = ifa->ifa_mask;
582 ret = -EADDRNOTAVAIL;
585 if (!(ifr.ifr_flags&IFF_UP))
586 inet_del_ifa(in_dev, ifap, 1);
589 ret = dev_change_flags(dev, ifr.ifr_flags);
592 case SIOCSIFADDR: /* Set interface address (and family) */
593 if (inet_abc_len(sin->sin_addr.s_addr) < 0) {
599 if ((ifa = inet_alloc_ifa()) == NULL) {
604 memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
606 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
609 if (ifa->ifa_local == sin->sin_addr.s_addr)
611 inet_del_ifa(in_dev, ifap, 0);
612 ifa->ifa_broadcast = 0;
613 ifa->ifa_anycast = 0;
617 ifa->ifa_local = sin->sin_addr.s_addr;
619 if (!(dev->flags&IFF_POINTOPOINT)) {
620 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
621 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
622 if ((dev->flags&IFF_BROADCAST) && ifa->ifa_prefixlen < 31)
623 ifa->ifa_broadcast = ifa->ifa_address|~ifa->ifa_mask;
625 ifa->ifa_prefixlen = 32;
626 ifa->ifa_mask = inet_make_mask(32);
628 ret = inet_set_ifa(dev, ifa);
631 case SIOCSIFBRDADDR: /* Set the broadcast address */
632 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
633 inet_del_ifa(in_dev, ifap, 0);
634 ifa->ifa_broadcast = sin->sin_addr.s_addr;
635 inet_insert_ifa(ifa);
639 case SIOCSIFDSTADDR: /* Set the destination address */
640 if (ifa->ifa_address != sin->sin_addr.s_addr) {
641 if (inet_abc_len(sin->sin_addr.s_addr) < 0) {
645 inet_del_ifa(in_dev, ifap, 0);
646 ifa->ifa_address = sin->sin_addr.s_addr;
647 inet_insert_ifa(ifa);
651 case SIOCSIFNETMASK: /* Set the netmask for the interface */
654 * The mask we set must be legal.
656 if (bad_mask(sin->sin_addr.s_addr, 0)) {
661 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
662 inet_del_ifa(in_dev, ifap, 0);
663 ifa->ifa_mask = sin->sin_addr.s_addr;
664 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
665 inet_insert_ifa(ifa);
677 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
683 inet_gifconf(struct net_device *dev, char *buf, int len)
685 struct in_device *in_dev = __in_dev_get(dev);
686 struct in_ifaddr *ifa;
690 if (in_dev==NULL || (ifa=in_dev->ifa_list)==NULL)
693 for ( ; ifa; ifa = ifa->ifa_next) {
698 if (len < (int) sizeof(ifr))
700 memset(&ifr, 0, sizeof(struct ifreq));
702 strcpy(ifr.ifr_name, ifa->ifa_label);
704 strcpy(ifr.ifr_name, dev->name);
706 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_family = AF_INET;
707 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_addr.s_addr = ifa->ifa_local;
709 if (copy_to_user(buf, &ifr, sizeof(struct ifreq)))
711 buf += sizeof(struct ifreq);
712 len -= sizeof(struct ifreq);
713 done += sizeof(struct ifreq);
718 u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
721 struct in_device *in_dev;
723 read_lock(&inetdev_lock);
724 in_dev = __in_dev_get(dev);
725 if (in_dev == NULL) {
726 read_unlock(&inetdev_lock);
730 read_lock(&in_dev->lock);
731 for_primary_ifa(in_dev) {
732 if (ifa->ifa_scope > scope)
734 if (!dst || inet_ifa_match(dst, ifa)) {
735 addr = ifa->ifa_local;
739 addr = ifa->ifa_local;
740 } endfor_ifa(in_dev);
741 read_unlock(&in_dev->lock);
742 read_unlock(&inetdev_lock);
747 /* Not loopback addresses on loopback should be preferred
748 in this case. It is importnat that lo is the first interface
751 read_lock(&dev_base_lock);
752 read_lock(&inetdev_lock);
753 for (dev=dev_base; dev; dev=dev->next) {
754 if ((in_dev=__in_dev_get(dev)) == NULL)
757 read_lock(&in_dev->lock);
758 for_primary_ifa(in_dev) {
759 if (ifa->ifa_scope != RT_SCOPE_LINK &&
760 ifa->ifa_scope <= scope) {
761 read_unlock(&in_dev->lock);
762 read_unlock(&inetdev_lock);
763 read_unlock(&dev_base_lock);
764 return ifa->ifa_local;
766 } endfor_ifa(in_dev);
767 read_unlock(&in_dev->lock);
769 read_unlock(&inetdev_lock);
770 read_unlock(&dev_base_lock);
779 int register_inetaddr_notifier(struct notifier_block *nb)
781 return notifier_chain_register(&inetaddr_chain, nb);
784 int unregister_inetaddr_notifier(struct notifier_block *nb)
786 return notifier_chain_unregister(&inetaddr_chain,nb);
789 /* Called only under RTNL semaphore */
791 static int inetdev_event(struct notifier_block *this, unsigned long event, void *ptr)
793 struct net_device *dev = ptr;
794 struct in_device *in_dev = __in_dev_get(dev);
802 case NETDEV_REGISTER:
803 printk(KERN_DEBUG "inetdev_event: bug\n");
809 if (dev == &loopback_dev) {
810 struct in_ifaddr *ifa;
811 if ((ifa = inet_alloc_ifa()) != NULL) {
813 ifa->ifa_address = htonl(INADDR_LOOPBACK);
814 ifa->ifa_prefixlen = 8;
815 ifa->ifa_mask = inet_make_mask(8);
817 ifa->ifa_dev = in_dev;
818 ifa->ifa_scope = RT_SCOPE_HOST;
819 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
820 inet_insert_ifa(ifa);
828 case NETDEV_CHANGEMTU:
831 /* MTU falled under 68, disable IP */
832 case NETDEV_UNREGISTER:
833 inetdev_destroy(in_dev);
835 case NETDEV_CHANGENAME:
836 if (in_dev->ifa_list) {
837 struct in_ifaddr *ifa;
838 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
839 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
840 /* Do not notify about label change, this event is
841 not interesting to applications using netlink.
850 struct notifier_block ip_netdev_notifier = {
851 notifier_call: inetdev_event,
854 static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
855 u32 pid, u32 seq, int event)
857 struct ifaddrmsg *ifm;
858 struct nlmsghdr *nlh;
859 unsigned char *b = skb->tail;
861 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
862 ifm = NLMSG_DATA(nlh);
863 ifm->ifa_family = AF_INET;
864 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
865 ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
866 ifm->ifa_scope = ifa->ifa_scope;
867 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
868 if (ifa->ifa_address)
869 RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
871 RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
872 if (ifa->ifa_broadcast)
873 RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
874 if (ifa->ifa_anycast)
875 RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
876 if (ifa->ifa_label[0])
877 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
878 nlh->nlmsg_len = skb->tail - b;
883 skb_trim(skb, b - skb->data);
887 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
891 struct net_device *dev;
892 struct in_device *in_dev;
893 struct in_ifaddr *ifa;
896 s_ip_idx = ip_idx = cb->args[1];
897 read_lock(&dev_base_lock);
898 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
903 read_lock(&inetdev_lock);
904 if ((in_dev = __in_dev_get(dev)) == NULL) {
905 read_unlock(&inetdev_lock);
908 read_lock(&in_dev->lock);
909 for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
910 ifa = ifa->ifa_next, ip_idx++) {
911 if (ip_idx < s_ip_idx)
913 if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
914 cb->nlh->nlmsg_seq, RTM_NEWADDR) <= 0) {
915 read_unlock(&in_dev->lock);
916 read_unlock(&inetdev_lock);
920 read_unlock(&in_dev->lock);
921 read_unlock(&inetdev_lock);
925 read_unlock(&dev_base_lock);
927 cb->args[1] = ip_idx;
932 static void rtmsg_ifa(int event, struct in_ifaddr * ifa)
935 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
937 skb = alloc_skb(size, GFP_KERNEL);
939 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, ENOBUFS);
942 if (inet_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
944 netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, EINVAL);
947 NETLINK_CB(skb).dst_groups = RTMGRP_IPV4_IFADDR;
948 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV4_IFADDR, GFP_KERNEL);
952 static struct rtnetlink_link inet_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
959 { inet_rtm_newaddr, NULL, },
960 { inet_rtm_deladdr, NULL, },
961 { NULL, inet_dump_ifaddr, },
964 { inet_rtm_newroute, NULL, },
965 { inet_rtm_delroute, NULL, },
966 { inet_rtm_getroute, inet_dump_fib, },
974 #ifdef CONFIG_IP_MULTIPLE_TABLES
975 { inet_rtm_newrule, NULL, },
976 { inet_rtm_delrule, NULL, },
977 { NULL, inet_dump_rules, },
990 void inet_forward_change()
992 struct net_device *dev;
993 int on = ipv4_devconf.forwarding;
995 ipv4_devconf.accept_redirects = !on;
996 ipv4_devconf_dflt.forwarding = on;
998 read_lock(&dev_base_lock);
999 for (dev = dev_base; dev; dev = dev->next) {
1000 struct in_device *in_dev;
1001 read_lock(&inetdev_lock);
1002 in_dev = __in_dev_get(dev);
1004 in_dev->cnf.forwarding = on;
1005 read_unlock(&inetdev_lock);
1007 read_unlock(&dev_base_lock);
1013 int devinet_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
1014 void *buffer, size_t *lenp)
1016 int *valp = ctl->data;
1020 ret = proc_dointvec(ctl, write, filp, buffer, lenp);
1022 if (write && *valp != val) {
1023 if (valp == &ipv4_devconf.forwarding)
1024 inet_forward_change();
1025 else if (valp != &ipv4_devconf_dflt.forwarding)
1032 static struct devinet_sysctl_table
1034 struct ctl_table_header *sysctl_header;
1035 ctl_table devinet_vars[15];
1036 ctl_table devinet_dev[2];
1037 ctl_table devinet_conf_dir[2];
1038 ctl_table devinet_proto_dir[2];
1039 ctl_table devinet_root_dir[2];
1040 } devinet_sysctl = {
1042 {{NET_IPV4_CONF_FORWARDING, "forwarding",
1043 &ipv4_devconf.forwarding, sizeof(int), 0644, NULL,
1044 &devinet_sysctl_forward},
1045 {NET_IPV4_CONF_MC_FORWARDING, "mc_forwarding",
1046 &ipv4_devconf.mc_forwarding, sizeof(int), 0444, NULL,
1048 {NET_IPV4_CONF_ACCEPT_REDIRECTS, "accept_redirects",
1049 &ipv4_devconf.accept_redirects, sizeof(int), 0644, NULL,
1051 {NET_IPV4_CONF_SECURE_REDIRECTS, "secure_redirects",
1052 &ipv4_devconf.secure_redirects, sizeof(int), 0644, NULL,
1054 {NET_IPV4_CONF_SHARED_MEDIA, "shared_media",
1055 &ipv4_devconf.shared_media, sizeof(int), 0644, NULL,
1057 {NET_IPV4_CONF_RP_FILTER, "rp_filter",
1058 &ipv4_devconf.rp_filter, sizeof(int), 0644, NULL,
1060 {NET_IPV4_CONF_SEND_REDIRECTS, "send_redirects",
1061 &ipv4_devconf.send_redirects, sizeof(int), 0644, NULL,
1063 {NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE, "accept_source_route",
1064 &ipv4_devconf.accept_source_route, sizeof(int), 0644, NULL,
1066 {NET_IPV4_CONF_PROXY_ARP, "proxy_arp",
1067 &ipv4_devconf.proxy_arp, sizeof(int), 0644, NULL,
1069 {NET_IPV4_CONF_MEDIUM_ID, "medium_id",
1070 &ipv4_devconf.medium_id, sizeof(int), 0644, NULL,
1072 {NET_IPV4_CONF_BOOTP_RELAY, "bootp_relay",
1073 &ipv4_devconf.bootp_relay, sizeof(int), 0644, NULL,
1075 {NET_IPV4_CONF_LOG_MARTIANS, "log_martians",
1076 &ipv4_devconf.log_martians, sizeof(int), 0644, NULL,
1078 {NET_IPV4_CONF_TAG, "tag",
1079 &ipv4_devconf.tag, sizeof(int), 0644, NULL,
1081 {NET_IPV4_CONF_ARPFILTER, "arp_filter",
1082 &ipv4_devconf.arp_filter, sizeof(int), 0644, NULL,
1086 {{NET_PROTO_CONF_ALL, "all", NULL, 0, 0555, devinet_sysctl.devinet_vars},{0}},
1087 {{NET_IPV4_CONF, "conf", NULL, 0, 0555, devinet_sysctl.devinet_dev},{0}},
1088 {{NET_IPV4, "ipv4", NULL, 0, 0555, devinet_sysctl.devinet_conf_dir},{0}},
1089 {{CTL_NET, "net", NULL, 0, 0555, devinet_sysctl.devinet_proto_dir},{0}}
1092 static void devinet_sysctl_register(struct in_device *in_dev, struct ipv4_devconf *p)
1095 struct net_device *dev = in_dev ? in_dev->dev : NULL;
1096 struct devinet_sysctl_table *t;
1098 t = kmalloc(sizeof(*t), GFP_KERNEL);
1101 memcpy(t, &devinet_sysctl, sizeof(*t));
1102 for (i=0; i<sizeof(t->devinet_vars)/sizeof(t->devinet_vars[0])-1; i++) {
1103 t->devinet_vars[i].data += (char*)p - (char*)&ipv4_devconf;
1104 t->devinet_vars[i].de = NULL;
1107 t->devinet_dev[0].procname = dev->name;
1108 t->devinet_dev[0].ctl_name = dev->ifindex;
1110 t->devinet_dev[0].procname = "default";
1111 t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
1113 t->devinet_dev[0].child = t->devinet_vars;
1114 t->devinet_dev[0].de = NULL;
1115 t->devinet_conf_dir[0].child = t->devinet_dev;
1116 t->devinet_conf_dir[0].de = NULL;
1117 t->devinet_proto_dir[0].child = t->devinet_conf_dir;
1118 t->devinet_proto_dir[0].de = NULL;
1119 t->devinet_root_dir[0].child = t->devinet_proto_dir;
1120 t->devinet_root_dir[0].de = NULL;
1122 t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
1123 if (t->sysctl_header == NULL)
1129 static void devinet_sysctl_unregister(struct ipv4_devconf *p)
1132 struct devinet_sysctl_table *t = p->sysctl;
1134 unregister_sysctl_table(t->sysctl_header);
1140 void __init devinet_init(void)
1142 register_gifconf(PF_INET, inet_gifconf);
1143 register_netdevice_notifier(&ip_netdev_notifier);
1144 rtnetlink_links[PF_INET] = inet_rtnetlink_table;
1145 #ifdef CONFIG_SYSCTL
1146 devinet_sysctl.sysctl_header =
1147 register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
1148 devinet_sysctl_register(NULL, &ipv4_devconf_dflt);