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
13 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
14 * Mark Evans, <evansmp@uhura.aston.ac.uk>
17 * Alan Cox, <gw4pts@gw4pts.ampr.org>
18 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
21 * Alexey Kuznetsov: pa_* fields are replaced with ifaddr
23 * Cyrus Durgin: updated for kmod
24 * Matthias Andree: in devinet_ioctl, compare label and
25 * address (4.4BSD alias style support),
26 * fall back to comparing just the label
30 #include <linux/config.h>
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include <linux/bitops.h>
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/string.h>
41 #include <linux/socket.h>
42 #include <linux/sockios.h>
44 #include <linux/errno.h>
45 #include <linux/interrupt.h>
46 #include <linux/if_ether.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/skbuff.h>
51 #include <linux/rtnetlink.h>
52 #include <linux/init.h>
53 #include <linux/notifier.h>
54 #include <linux/inetdevice.h>
55 #include <linux/igmp.h>
57 #include <linux/sysctl.h>
59 #include <linux/kmod.h>
62 #include <net/route.h>
63 #include <net/ip_fib.h>
65 struct ipv4_devconf ipv4_devconf
= {
66 .accept_redirects
= 1,
68 .secure_redirects
= 1,
72 static struct ipv4_devconf ipv4_devconf_dflt
= {
73 .accept_redirects
= 1,
75 .secure_redirects
= 1,
77 .accept_source_route
= 1,
80 static void rtmsg_ifa(int event
, struct in_ifaddr
*);
82 static struct notifier_block
*inetaddr_chain
;
83 static void inet_del_ifa(struct in_device
*in_dev
, struct in_ifaddr
**ifap
,
86 static void devinet_sysctl_register(struct in_device
*in_dev
,
87 struct ipv4_devconf
*p
);
88 static void devinet_sysctl_unregister(struct ipv4_devconf
*p
);
91 /* Locks all the inet devices. */
93 static struct in_ifaddr
*inet_alloc_ifa(void)
95 struct in_ifaddr
*ifa
= kmalloc(sizeof(*ifa
), GFP_KERNEL
);
98 memset(ifa
, 0, sizeof(*ifa
));
99 INIT_RCU_HEAD(&ifa
->rcu_head
);
105 static void inet_rcu_free_ifa(struct rcu_head
*head
)
107 struct in_ifaddr
*ifa
= container_of(head
, struct in_ifaddr
, rcu_head
);
109 in_dev_put(ifa
->ifa_dev
);
113 static inline void inet_free_ifa(struct in_ifaddr
*ifa
)
115 call_rcu(&ifa
->rcu_head
, inet_rcu_free_ifa
);
118 void in_dev_finish_destroy(struct in_device
*idev
)
120 struct net_device
*dev
= idev
->dev
;
122 BUG_TRAP(!idev
->ifa_list
);
123 BUG_TRAP(!idev
->mc_list
);
124 #ifdef NET_REFCNT_DEBUG
125 printk(KERN_DEBUG
"in_dev_finish_destroy: %p=%s\n",
126 idev
, dev
? dev
->name
: "NIL");
130 printk("Freeing alive in_device %p\n", idev
);
136 struct in_device
*inetdev_init(struct net_device
*dev
)
138 struct in_device
*in_dev
;
142 in_dev
= kmalloc(sizeof(*in_dev
), GFP_KERNEL
);
145 memset(in_dev
, 0, sizeof(*in_dev
));
146 INIT_RCU_HEAD(&in_dev
->rcu_head
);
147 memcpy(&in_dev
->cnf
, &ipv4_devconf_dflt
, sizeof(in_dev
->cnf
));
148 in_dev
->cnf
.sysctl
= NULL
;
150 if ((in_dev
->arp_parms
= neigh_parms_alloc(dev
, &arp_tbl
)) == NULL
)
152 /* Reference in_dev->dev */
155 neigh_sysctl_register(dev
, in_dev
->arp_parms
, NET_IPV4
,
156 NET_IPV4_NEIGH
, "ipv4", NULL
, NULL
);
159 /* Account for reference dev->ip_ptr */
161 rcu_assign_pointer(dev
->ip_ptr
, in_dev
);
164 devinet_sysctl_register(in_dev
, &in_dev
->cnf
);
166 ip_mc_init_dev(in_dev
);
167 if (dev
->flags
& IFF_UP
)
177 static void in_dev_rcu_put(struct rcu_head
*head
)
179 struct in_device
*idev
= container_of(head
, struct in_device
, rcu_head
);
183 static void inetdev_destroy(struct in_device
*in_dev
)
185 struct in_ifaddr
*ifa
;
186 struct net_device
*dev
;
191 if (dev
== &loopback_dev
)
196 ip_mc_destroy_dev(in_dev
);
198 while ((ifa
= in_dev
->ifa_list
) != NULL
) {
199 inet_del_ifa(in_dev
, &in_dev
->ifa_list
, 0);
204 devinet_sysctl_unregister(&in_dev
->cnf
);
210 neigh_sysctl_unregister(in_dev
->arp_parms
);
212 neigh_parms_release(&arp_tbl
, in_dev
->arp_parms
);
215 call_rcu(&in_dev
->rcu_head
, in_dev_rcu_put
);
218 int inet_addr_onlink(struct in_device
*in_dev
, u32 a
, u32 b
)
221 for_primary_ifa(in_dev
) {
222 if (inet_ifa_match(a
, ifa
)) {
223 if (!b
|| inet_ifa_match(b
, ifa
)) {
228 } endfor_ifa(in_dev
);
233 static void inet_del_ifa(struct in_device
*in_dev
, struct in_ifaddr
**ifap
,
236 struct in_ifaddr
*ifa1
= *ifap
;
240 /* 1. Deleting primary ifaddr forces deletion all secondaries */
242 if (!(ifa1
->ifa_flags
& IFA_F_SECONDARY
)) {
243 struct in_ifaddr
*ifa
;
244 struct in_ifaddr
**ifap1
= &ifa1
->ifa_next
;
246 while ((ifa
= *ifap1
) != NULL
) {
247 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
) ||
248 ifa1
->ifa_mask
!= ifa
->ifa_mask
||
249 !inet_ifa_match(ifa1
->ifa_address
, ifa
)) {
250 ifap1
= &ifa
->ifa_next
;
254 *ifap1
= ifa
->ifa_next
;
256 rtmsg_ifa(RTM_DELADDR
, ifa
);
257 notifier_call_chain(&inetaddr_chain
, NETDEV_DOWN
, ifa
);
264 *ifap
= ifa1
->ifa_next
;
266 /* 3. Announce address deletion */
268 /* Send message first, then call notifier.
269 At first sight, FIB update triggered by notifier
270 will refer to already deleted ifaddr, that could confuse
271 netlink listeners. It is not true: look, gated sees
272 that route deleted and if it still thinks that ifaddr
273 is valid, it will try to restore deleted routes... Grr.
274 So that, this order is correct.
276 rtmsg_ifa(RTM_DELADDR
, ifa1
);
277 notifier_call_chain(&inetaddr_chain
, NETDEV_DOWN
, ifa1
);
281 if (!in_dev
->ifa_list
)
282 inetdev_destroy(in_dev
);
286 static int inet_insert_ifa(struct in_ifaddr
*ifa
)
288 struct in_device
*in_dev
= ifa
->ifa_dev
;
289 struct in_ifaddr
*ifa1
, **ifap
, **last_primary
;
293 if (!ifa
->ifa_local
) {
298 ifa
->ifa_flags
&= ~IFA_F_SECONDARY
;
299 last_primary
= &in_dev
->ifa_list
;
301 for (ifap
= &in_dev
->ifa_list
; (ifa1
= *ifap
) != NULL
;
302 ifap
= &ifa1
->ifa_next
) {
303 if (!(ifa1
->ifa_flags
& IFA_F_SECONDARY
) &&
304 ifa
->ifa_scope
<= ifa1
->ifa_scope
)
305 last_primary
= &ifa1
->ifa_next
;
306 if (ifa1
->ifa_mask
== ifa
->ifa_mask
&&
307 inet_ifa_match(ifa1
->ifa_address
, ifa
)) {
308 if (ifa1
->ifa_local
== ifa
->ifa_local
) {
312 if (ifa1
->ifa_scope
!= ifa
->ifa_scope
) {
316 ifa
->ifa_flags
|= IFA_F_SECONDARY
;
320 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
)) {
321 net_srandom(ifa
->ifa_local
);
325 ifa
->ifa_next
= *ifap
;
328 /* Send message first, then call notifier.
329 Notifier will trigger FIB update, so that
330 listeners of netlink will know about new ifaddr */
331 rtmsg_ifa(RTM_NEWADDR
, ifa
);
332 notifier_call_chain(&inetaddr_chain
, NETDEV_UP
, ifa
);
337 static int inet_set_ifa(struct net_device
*dev
, struct in_ifaddr
*ifa
)
339 struct in_device
*in_dev
= __in_dev_get(dev
);
344 in_dev
= inetdev_init(dev
);
350 if (ifa
->ifa_dev
!= in_dev
) {
351 BUG_TRAP(!ifa
->ifa_dev
);
353 ifa
->ifa_dev
= in_dev
;
355 if (LOOPBACK(ifa
->ifa_local
))
356 ifa
->ifa_scope
= RT_SCOPE_HOST
;
357 return inet_insert_ifa(ifa
);
360 struct in_device
*inetdev_by_index(int ifindex
)
362 struct net_device
*dev
;
363 struct in_device
*in_dev
= NULL
;
364 read_lock(&dev_base_lock
);
365 dev
= __dev_get_by_index(ifindex
);
367 in_dev
= in_dev_get(dev
);
368 read_unlock(&dev_base_lock
);
372 /* Called only from RTNL semaphored context. No locks. */
374 struct in_ifaddr
*inet_ifa_byprefix(struct in_device
*in_dev
, u32 prefix
,
379 for_primary_ifa(in_dev
) {
380 if (ifa
->ifa_mask
== mask
&& inet_ifa_match(prefix
, ifa
))
382 } endfor_ifa(in_dev
);
386 static int inet_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
388 struct rtattr
**rta
= arg
;
389 struct in_device
*in_dev
;
390 struct ifaddrmsg
*ifm
= NLMSG_DATA(nlh
);
391 struct in_ifaddr
*ifa
, **ifap
;
395 if ((in_dev
= inetdev_by_index(ifm
->ifa_index
)) == NULL
)
397 __in_dev_put(in_dev
);
399 for (ifap
= &in_dev
->ifa_list
; (ifa
= *ifap
) != NULL
;
400 ifap
= &ifa
->ifa_next
) {
401 if ((rta
[IFA_LOCAL
- 1] &&
402 memcmp(RTA_DATA(rta
[IFA_LOCAL
- 1]),
403 &ifa
->ifa_local
, 4)) ||
404 (rta
[IFA_LABEL
- 1] &&
405 rtattr_strcmp(rta
[IFA_LABEL
- 1], ifa
->ifa_label
)) ||
406 (rta
[IFA_ADDRESS
- 1] &&
407 (ifm
->ifa_prefixlen
!= ifa
->ifa_prefixlen
||
408 !inet_ifa_match(*(u32
*)RTA_DATA(rta
[IFA_ADDRESS
- 1]),
411 inet_del_ifa(in_dev
, ifap
, 1);
415 return -EADDRNOTAVAIL
;
418 static int inet_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
420 struct rtattr
**rta
= arg
;
421 struct net_device
*dev
;
422 struct in_device
*in_dev
;
423 struct ifaddrmsg
*ifm
= NLMSG_DATA(nlh
);
424 struct in_ifaddr
*ifa
;
429 if (ifm
->ifa_prefixlen
> 32 || !rta
[IFA_LOCAL
- 1])
433 if ((dev
= __dev_get_by_index(ifm
->ifa_index
)) == NULL
)
437 if ((in_dev
= __in_dev_get(dev
)) == NULL
) {
438 in_dev
= inetdev_init(dev
);
443 if ((ifa
= inet_alloc_ifa()) == NULL
)
446 if (!rta
[IFA_ADDRESS
- 1])
447 rta
[IFA_ADDRESS
- 1] = rta
[IFA_LOCAL
- 1];
448 memcpy(&ifa
->ifa_local
, RTA_DATA(rta
[IFA_LOCAL
- 1]), 4);
449 memcpy(&ifa
->ifa_address
, RTA_DATA(rta
[IFA_ADDRESS
- 1]), 4);
450 ifa
->ifa_prefixlen
= ifm
->ifa_prefixlen
;
451 ifa
->ifa_mask
= inet_make_mask(ifm
->ifa_prefixlen
);
452 if (rta
[IFA_BROADCAST
- 1])
453 memcpy(&ifa
->ifa_broadcast
,
454 RTA_DATA(rta
[IFA_BROADCAST
- 1]), 4);
455 if (rta
[IFA_ANYCAST
- 1])
456 memcpy(&ifa
->ifa_anycast
, RTA_DATA(rta
[IFA_ANYCAST
- 1]), 4);
457 ifa
->ifa_flags
= ifm
->ifa_flags
;
458 ifa
->ifa_scope
= ifm
->ifa_scope
;
460 ifa
->ifa_dev
= in_dev
;
461 if (rta
[IFA_LABEL
- 1])
462 rtattr_strlcpy(ifa
->ifa_label
, rta
[IFA_LABEL
- 1], IFNAMSIZ
);
464 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
466 rc
= inet_insert_ifa(ifa
);
472 * Determine a default network mask, based on the IP address.
475 static __inline__
int inet_abc_len(u32 addr
)
477 int rc
= -1; /* Something else, probably a multicast. */
486 else if (IN_CLASSB(addr
))
488 else if (IN_CLASSC(addr
))
496 int devinet_ioctl(unsigned int cmd
, void __user
*arg
)
499 struct sockaddr_in sin_orig
;
500 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
501 struct in_device
*in_dev
;
502 struct in_ifaddr
**ifap
= NULL
;
503 struct in_ifaddr
*ifa
= NULL
;
504 struct net_device
*dev
;
507 int tryaddrmatch
= 0;
510 * Fetch the caller's info block into kernel space
513 if (copy_from_user(&ifr
, arg
, sizeof(struct ifreq
)))
515 ifr
.ifr_name
[IFNAMSIZ
- 1] = 0;
517 /* save original address for comparison */
518 memcpy(&sin_orig
, sin
, sizeof(*sin
));
520 colon
= strchr(ifr
.ifr_name
, ':');
525 dev_load(ifr
.ifr_name
);
529 case SIOCGIFADDR
: /* Get interface address */
530 case SIOCGIFBRDADDR
: /* Get the broadcast address */
531 case SIOCGIFDSTADDR
: /* Get the destination address */
532 case SIOCGIFNETMASK
: /* Get the netmask for the interface */
533 /* Note that these ioctls will not sleep,
534 so that we do not impose a lock.
535 One day we will be forced to put shlock here (I mean SMP)
537 tryaddrmatch
= (sin_orig
.sin_family
== AF_INET
);
538 memset(sin
, 0, sizeof(*sin
));
539 sin
->sin_family
= AF_INET
;
544 if (!capable(CAP_NET_ADMIN
))
547 case SIOCSIFADDR
: /* Set interface address (and family) */
548 case SIOCSIFBRDADDR
: /* Set the broadcast address */
549 case SIOCSIFDSTADDR
: /* Set the destination address */
550 case SIOCSIFNETMASK
: /* Set the netmask for the interface */
552 if (!capable(CAP_NET_ADMIN
))
555 if (sin
->sin_family
!= AF_INET
)
566 if ((dev
= __dev_get_by_name(ifr
.ifr_name
)) == NULL
)
572 if ((in_dev
= __in_dev_get(dev
)) != NULL
) {
574 /* Matthias Andree */
575 /* compare label and address (4.4BSD style) */
576 /* note: we only do this for a limited set of ioctls
577 and only if the original address family was AF_INET.
578 This is checked above. */
579 for (ifap
= &in_dev
->ifa_list
; (ifa
= *ifap
) != NULL
;
580 ifap
= &ifa
->ifa_next
) {
581 if (!strcmp(ifr
.ifr_name
, ifa
->ifa_label
) &&
582 sin_orig
.sin_addr
.s_addr
==
588 /* we didn't get a match, maybe the application is
589 4.3BSD-style and passed in junk so we fall back to
590 comparing just the label */
592 for (ifap
= &in_dev
->ifa_list
; (ifa
= *ifap
) != NULL
;
593 ifap
= &ifa
->ifa_next
)
594 if (!strcmp(ifr
.ifr_name
, ifa
->ifa_label
))
599 ret
= -EADDRNOTAVAIL
;
600 if (!ifa
&& cmd
!= SIOCSIFADDR
&& cmd
!= SIOCSIFFLAGS
)
604 case SIOCGIFADDR
: /* Get interface address */
605 sin
->sin_addr
.s_addr
= ifa
->ifa_local
;
608 case SIOCGIFBRDADDR
: /* Get the broadcast address */
609 sin
->sin_addr
.s_addr
= ifa
->ifa_broadcast
;
612 case SIOCGIFDSTADDR
: /* Get the destination address */
613 sin
->sin_addr
.s_addr
= ifa
->ifa_address
;
616 case SIOCGIFNETMASK
: /* Get the netmask for the interface */
617 sin
->sin_addr
.s_addr
= ifa
->ifa_mask
;
622 ret
= -EADDRNOTAVAIL
;
626 if (!(ifr
.ifr_flags
& IFF_UP
))
627 inet_del_ifa(in_dev
, ifap
, 1);
630 ret
= dev_change_flags(dev
, ifr
.ifr_flags
);
633 case SIOCSIFADDR
: /* Set interface address (and family) */
635 if (inet_abc_len(sin
->sin_addr
.s_addr
) < 0)
640 if ((ifa
= inet_alloc_ifa()) == NULL
)
643 memcpy(ifa
->ifa_label
, ifr
.ifr_name
, IFNAMSIZ
);
645 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
648 if (ifa
->ifa_local
== sin
->sin_addr
.s_addr
)
650 inet_del_ifa(in_dev
, ifap
, 0);
651 ifa
->ifa_broadcast
= 0;
652 ifa
->ifa_anycast
= 0;
655 ifa
->ifa_address
= ifa
->ifa_local
= sin
->sin_addr
.s_addr
;
657 if (!(dev
->flags
& IFF_POINTOPOINT
)) {
658 ifa
->ifa_prefixlen
= inet_abc_len(ifa
->ifa_address
);
659 ifa
->ifa_mask
= inet_make_mask(ifa
->ifa_prefixlen
);
660 if ((dev
->flags
& IFF_BROADCAST
) &&
661 ifa
->ifa_prefixlen
< 31)
662 ifa
->ifa_broadcast
= ifa
->ifa_address
|
665 ifa
->ifa_prefixlen
= 32;
666 ifa
->ifa_mask
= inet_make_mask(32);
668 ret
= inet_set_ifa(dev
, ifa
);
671 case SIOCSIFBRDADDR
: /* Set the broadcast address */
673 if (ifa
->ifa_broadcast
!= sin
->sin_addr
.s_addr
) {
674 inet_del_ifa(in_dev
, ifap
, 0);
675 ifa
->ifa_broadcast
= sin
->sin_addr
.s_addr
;
676 inet_insert_ifa(ifa
);
680 case SIOCSIFDSTADDR
: /* Set the destination address */
682 if (ifa
->ifa_address
== sin
->sin_addr
.s_addr
)
685 if (inet_abc_len(sin
->sin_addr
.s_addr
) < 0)
688 inet_del_ifa(in_dev
, ifap
, 0);
689 ifa
->ifa_address
= sin
->sin_addr
.s_addr
;
690 inet_insert_ifa(ifa
);
693 case SIOCSIFNETMASK
: /* Set the netmask for the interface */
696 * The mask we set must be legal.
699 if (bad_mask(sin
->sin_addr
.s_addr
, 0))
702 if (ifa
->ifa_mask
!= sin
->sin_addr
.s_addr
) {
703 inet_del_ifa(in_dev
, ifap
, 0);
704 ifa
->ifa_mask
= sin
->sin_addr
.s_addr
;
705 ifa
->ifa_prefixlen
= inet_mask_len(ifa
->ifa_mask
);
707 /* See if current broadcast address matches
708 * with current netmask, then recalculate
709 * the broadcast address. Otherwise it's a
710 * funny address, so don't touch it since
711 * the user seems to know what (s)he's doing...
713 if ((dev
->flags
& IFF_BROADCAST
) &&
714 (ifa
->ifa_prefixlen
< 31) &&
715 (ifa
->ifa_broadcast
==
716 (ifa
->ifa_local
|~ifa
->ifa_mask
))) {
717 ifa
->ifa_broadcast
= (ifa
->ifa_local
|
718 ~sin
->sin_addr
.s_addr
);
720 inet_insert_ifa(ifa
);
730 ret
= copy_to_user(arg
, &ifr
, sizeof(struct ifreq
)) ? -EFAULT
: 0;
734 static int inet_gifconf(struct net_device
*dev
, char __user
*buf
, int len
)
736 struct in_device
*in_dev
= __in_dev_get(dev
);
737 struct in_ifaddr
*ifa
;
741 if (!in_dev
|| (ifa
= in_dev
->ifa_list
) == NULL
)
744 for (; ifa
; ifa
= ifa
->ifa_next
) {
749 if (len
< (int) sizeof(ifr
))
751 memset(&ifr
, 0, sizeof(struct ifreq
));
753 strcpy(ifr
.ifr_name
, ifa
->ifa_label
);
755 strcpy(ifr
.ifr_name
, dev
->name
);
757 (*(struct sockaddr_in
*)&ifr
.ifr_addr
).sin_family
= AF_INET
;
758 (*(struct sockaddr_in
*)&ifr
.ifr_addr
).sin_addr
.s_addr
=
761 if (copy_to_user(buf
, &ifr
, sizeof(struct ifreq
))) {
765 buf
+= sizeof(struct ifreq
);
766 len
-= sizeof(struct ifreq
);
767 done
+= sizeof(struct ifreq
);
773 u32
inet_select_addr(const struct net_device
*dev
, u32 dst
, int scope
)
776 struct in_device
*in_dev
;
779 in_dev
= __in_dev_get(dev
);
783 for_primary_ifa(in_dev
) {
784 if (ifa
->ifa_scope
> scope
)
786 if (!dst
|| inet_ifa_match(dst
, ifa
)) {
787 addr
= ifa
->ifa_local
;
791 addr
= ifa
->ifa_local
;
792 } endfor_ifa(in_dev
);
799 /* Not loopback addresses on loopback should be preferred
800 in this case. It is importnat that lo is the first interface
803 read_lock(&dev_base_lock
);
805 for (dev
= dev_base
; dev
; dev
= dev
->next
) {
806 if ((in_dev
= __in_dev_get(dev
)) == NULL
)
809 for_primary_ifa(in_dev
) {
810 if (ifa
->ifa_scope
!= RT_SCOPE_LINK
&&
811 ifa
->ifa_scope
<= scope
) {
812 addr
= ifa
->ifa_local
;
813 goto out_unlock_both
;
815 } endfor_ifa(in_dev
);
818 read_unlock(&dev_base_lock
);
824 static u32
confirm_addr_indev(struct in_device
*in_dev
, u32 dst
,
825 u32 local
, int scope
)
832 (local
== ifa
->ifa_local
|| !local
) &&
833 ifa
->ifa_scope
<= scope
) {
834 addr
= ifa
->ifa_local
;
839 same
= (!local
|| inet_ifa_match(local
, ifa
)) &&
840 (!dst
|| inet_ifa_match(dst
, ifa
));
844 /* Is the selected addr into dst subnet? */
845 if (inet_ifa_match(addr
, ifa
))
847 /* No, then can we use new local src? */
848 if (ifa
->ifa_scope
<= scope
) {
849 addr
= ifa
->ifa_local
;
852 /* search for large dst subnet for addr */
856 } endfor_ifa(in_dev
);
858 return same
? addr
: 0;
862 * Confirm that local IP address exists using wildcards:
863 * - dev: only on this interface, 0=any interface
864 * - dst: only in the same subnet as dst, 0=any dst
865 * - local: address, 0=autoselect the local address
866 * - scope: maximum allowed scope value for the local address
868 u32
inet_confirm_addr(const struct net_device
*dev
, u32 dst
, u32 local
, int scope
)
871 struct in_device
*in_dev
;
875 if ((in_dev
= __in_dev_get(dev
)))
876 addr
= confirm_addr_indev(in_dev
, dst
, local
, scope
);
882 read_lock(&dev_base_lock
);
884 for (dev
= dev_base
; dev
; dev
= dev
->next
) {
885 if ((in_dev
= __in_dev_get(dev
))) {
886 addr
= confirm_addr_indev(in_dev
, dst
, local
, scope
);
892 read_unlock(&dev_base_lock
);
901 int register_inetaddr_notifier(struct notifier_block
*nb
)
903 return notifier_chain_register(&inetaddr_chain
, nb
);
906 int unregister_inetaddr_notifier(struct notifier_block
*nb
)
908 return notifier_chain_unregister(&inetaddr_chain
, nb
);
911 /* Rename ifa_labels for a device name change. Make some effort to preserve existing
912 * alias numbering and to create unique labels if possible.
914 static void inetdev_changename(struct net_device
*dev
, struct in_device
*in_dev
)
916 struct in_ifaddr
*ifa
;
919 for (ifa
= in_dev
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
920 char old
[IFNAMSIZ
], *dot
;
922 memcpy(old
, ifa
->ifa_label
, IFNAMSIZ
);
923 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
926 dot
= strchr(ifa
->ifa_label
, ':');
928 sprintf(old
, ":%d", named
);
931 if (strlen(dot
) + strlen(dev
->name
) < IFNAMSIZ
) {
932 strcat(ifa
->ifa_label
, dot
);
934 strcpy(ifa
->ifa_label
+ (IFNAMSIZ
- strlen(dot
) - 1), dot
);
939 /* Called only under RTNL semaphore */
941 static int inetdev_event(struct notifier_block
*this, unsigned long event
,
944 struct net_device
*dev
= ptr
;
945 struct in_device
*in_dev
= __in_dev_get(dev
);
950 if (event
== NETDEV_REGISTER
&& dev
== &loopback_dev
) {
951 in_dev
= inetdev_init(dev
);
953 panic("devinet: Failed to create loopback\n");
954 in_dev
->cnf
.no_xfrm
= 1;
955 in_dev
->cnf
.no_policy
= 1;
961 case NETDEV_REGISTER
:
962 printk(KERN_DEBUG
"inetdev_event: bug\n");
968 if (dev
== &loopback_dev
) {
969 struct in_ifaddr
*ifa
;
970 if ((ifa
= inet_alloc_ifa()) != NULL
) {
972 ifa
->ifa_address
= htonl(INADDR_LOOPBACK
);
973 ifa
->ifa_prefixlen
= 8;
974 ifa
->ifa_mask
= inet_make_mask(8);
976 ifa
->ifa_dev
= in_dev
;
977 ifa
->ifa_scope
= RT_SCOPE_HOST
;
978 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
979 inet_insert_ifa(ifa
);
987 case NETDEV_CHANGEMTU
:
990 /* MTU falled under 68, disable IP */
991 case NETDEV_UNREGISTER
:
992 inetdev_destroy(in_dev
);
994 case NETDEV_CHANGENAME
:
995 /* Do not notify about label change, this event is
996 * not interesting to applications using netlink.
998 inetdev_changename(dev
, in_dev
);
1000 #ifdef CONFIG_SYSCTL
1001 devinet_sysctl_unregister(&in_dev
->cnf
);
1002 neigh_sysctl_unregister(in_dev
->arp_parms
);
1003 neigh_sysctl_register(dev
, in_dev
->arp_parms
, NET_IPV4
,
1004 NET_IPV4_NEIGH
, "ipv4", NULL
, NULL
);
1005 devinet_sysctl_register(in_dev
, &in_dev
->cnf
);
1013 static struct notifier_block ip_netdev_notifier
= {
1014 .notifier_call
=inetdev_event
,
1017 static int inet_fill_ifaddr(struct sk_buff
*skb
, struct in_ifaddr
*ifa
,
1018 u32 pid
, u32 seq
, int event
)
1020 struct ifaddrmsg
*ifm
;
1021 struct nlmsghdr
*nlh
;
1022 unsigned char *b
= skb
->tail
;
1024 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(*ifm
));
1025 if (pid
) nlh
->nlmsg_flags
|= NLM_F_MULTI
;
1026 ifm
= NLMSG_DATA(nlh
);
1027 ifm
->ifa_family
= AF_INET
;
1028 ifm
->ifa_prefixlen
= ifa
->ifa_prefixlen
;
1029 ifm
->ifa_flags
= ifa
->ifa_flags
|IFA_F_PERMANENT
;
1030 ifm
->ifa_scope
= ifa
->ifa_scope
;
1031 ifm
->ifa_index
= ifa
->ifa_dev
->dev
->ifindex
;
1032 if (ifa
->ifa_address
)
1033 RTA_PUT(skb
, IFA_ADDRESS
, 4, &ifa
->ifa_address
);
1035 RTA_PUT(skb
, IFA_LOCAL
, 4, &ifa
->ifa_local
);
1036 if (ifa
->ifa_broadcast
)
1037 RTA_PUT(skb
, IFA_BROADCAST
, 4, &ifa
->ifa_broadcast
);
1038 if (ifa
->ifa_anycast
)
1039 RTA_PUT(skb
, IFA_ANYCAST
, 4, &ifa
->ifa_anycast
);
1040 if (ifa
->ifa_label
[0])
1041 RTA_PUT(skb
, IFA_LABEL
, IFNAMSIZ
, &ifa
->ifa_label
);
1042 nlh
->nlmsg_len
= skb
->tail
- b
;
1047 skb_trim(skb
, b
- skb
->data
);
1051 static int inet_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1054 struct net_device
*dev
;
1055 struct in_device
*in_dev
;
1056 struct in_ifaddr
*ifa
;
1057 int s_ip_idx
, s_idx
= cb
->args
[0];
1059 s_ip_idx
= ip_idx
= cb
->args
[1];
1060 read_lock(&dev_base_lock
);
1061 for (dev
= dev_base
, idx
= 0; dev
; dev
= dev
->next
, idx
++) {
1067 if ((in_dev
= __in_dev_get(dev
)) == NULL
) {
1072 for (ifa
= in_dev
->ifa_list
, ip_idx
= 0; ifa
;
1073 ifa
= ifa
->ifa_next
, ip_idx
++) {
1074 if (ip_idx
< s_ip_idx
)
1076 if (inet_fill_ifaddr(skb
, ifa
, NETLINK_CB(cb
->skb
).pid
,
1078 RTM_NEWADDR
) <= 0) {
1087 read_unlock(&dev_base_lock
);
1089 cb
->args
[1] = ip_idx
;
1094 static void rtmsg_ifa(int event
, struct in_ifaddr
* ifa
)
1096 int size
= NLMSG_SPACE(sizeof(struct ifaddrmsg
) + 128);
1097 struct sk_buff
*skb
= alloc_skb(size
, GFP_KERNEL
);
1100 netlink_set_err(rtnl
, 0, RTMGRP_IPV4_IFADDR
, ENOBUFS
);
1101 else if (inet_fill_ifaddr(skb
, ifa
, 0, 0, event
) < 0) {
1103 netlink_set_err(rtnl
, 0, RTMGRP_IPV4_IFADDR
, EINVAL
);
1105 NETLINK_CB(skb
).dst_groups
= RTMGRP_IPV4_IFADDR
;
1106 netlink_broadcast(rtnl
, skb
, 0, RTMGRP_IPV4_IFADDR
, GFP_KERNEL
);
1110 static struct rtnetlink_link inet_rtnetlink_table
[RTM_NR_MSGTYPES
] = {
1111 [RTM_NEWADDR
- RTM_BASE
] = { .doit
= inet_rtm_newaddr
, },
1112 [RTM_DELADDR
- RTM_BASE
] = { .doit
= inet_rtm_deladdr
, },
1113 [RTM_GETADDR
- RTM_BASE
] = { .dumpit
= inet_dump_ifaddr
, },
1114 [RTM_NEWROUTE
- RTM_BASE
] = { .doit
= inet_rtm_newroute
, },
1115 [RTM_DELROUTE
- RTM_BASE
] = { .doit
= inet_rtm_delroute
, },
1116 [RTM_GETROUTE
- RTM_BASE
] = { .doit
= inet_rtm_getroute
,
1117 .dumpit
= inet_dump_fib
, },
1118 #ifdef CONFIG_IP_MULTIPLE_TABLES
1119 [RTM_NEWRULE
- RTM_BASE
] = { .doit
= inet_rtm_newrule
, },
1120 [RTM_DELRULE
- RTM_BASE
] = { .doit
= inet_rtm_delrule
, },
1121 [RTM_GETRULE
- RTM_BASE
] = { .dumpit
= inet_dump_rules
, },
1125 #ifdef CONFIG_SYSCTL
1127 void inet_forward_change(void)
1129 struct net_device
*dev
;
1130 int on
= ipv4_devconf
.forwarding
;
1132 ipv4_devconf
.accept_redirects
= !on
;
1133 ipv4_devconf_dflt
.forwarding
= on
;
1135 read_lock(&dev_base_lock
);
1136 for (dev
= dev_base
; dev
; dev
= dev
->next
) {
1137 struct in_device
*in_dev
;
1139 in_dev
= __in_dev_get(dev
);
1141 in_dev
->cnf
.forwarding
= on
;
1144 read_unlock(&dev_base_lock
);
1149 static int devinet_sysctl_forward(ctl_table
*ctl
, int write
,
1150 struct file
* filp
, void __user
*buffer
,
1151 size_t *lenp
, loff_t
*ppos
)
1153 int *valp
= ctl
->data
;
1155 int ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
1157 if (write
&& *valp
!= val
) {
1158 if (valp
== &ipv4_devconf
.forwarding
)
1159 inet_forward_change();
1160 else if (valp
!= &ipv4_devconf_dflt
.forwarding
)
1167 int ipv4_doint_and_flush(ctl_table
*ctl
, int write
,
1168 struct file
* filp
, void __user
*buffer
,
1169 size_t *lenp
, loff_t
*ppos
)
1171 int *valp
= ctl
->data
;
1173 int ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
1175 if (write
&& *valp
!= val
)
1181 int ipv4_doint_and_flush_strategy(ctl_table
*table
, int __user
*name
, int nlen
,
1182 void __user
*oldval
, size_t __user
*oldlenp
,
1183 void __user
*newval
, size_t newlen
,
1186 int *valp
= table
->data
;
1189 if (!newval
|| !newlen
)
1192 if (newlen
!= sizeof(int))
1195 if (get_user(new, (int __user
*)newval
))
1201 if (oldval
&& oldlenp
) {
1204 if (get_user(len
, oldlenp
))
1208 if (len
> table
->maxlen
)
1209 len
= table
->maxlen
;
1210 if (copy_to_user(oldval
, valp
, len
))
1212 if (put_user(len
, oldlenp
))
1223 static struct devinet_sysctl_table
{
1224 struct ctl_table_header
*sysctl_header
;
1225 ctl_table devinet_vars
[__NET_IPV4_CONF_MAX
];
1226 ctl_table devinet_dev
[2];
1227 ctl_table devinet_conf_dir
[2];
1228 ctl_table devinet_proto_dir
[2];
1229 ctl_table devinet_root_dir
[2];
1230 } devinet_sysctl
= {
1233 .ctl_name
= NET_IPV4_CONF_FORWARDING
,
1234 .procname
= "forwarding",
1235 .data
= &ipv4_devconf
.forwarding
,
1236 .maxlen
= sizeof(int),
1238 .proc_handler
= &devinet_sysctl_forward
,
1241 .ctl_name
= NET_IPV4_CONF_MC_FORWARDING
,
1242 .procname
= "mc_forwarding",
1243 .data
= &ipv4_devconf
.mc_forwarding
,
1244 .maxlen
= sizeof(int),
1246 .proc_handler
= &proc_dointvec
,
1249 .ctl_name
= NET_IPV4_CONF_ACCEPT_REDIRECTS
,
1250 .procname
= "accept_redirects",
1251 .data
= &ipv4_devconf
.accept_redirects
,
1252 .maxlen
= sizeof(int),
1254 .proc_handler
= &proc_dointvec
,
1257 .ctl_name
= NET_IPV4_CONF_SECURE_REDIRECTS
,
1258 .procname
= "secure_redirects",
1259 .data
= &ipv4_devconf
.secure_redirects
,
1260 .maxlen
= sizeof(int),
1262 .proc_handler
= &proc_dointvec
,
1265 .ctl_name
= NET_IPV4_CONF_SHARED_MEDIA
,
1266 .procname
= "shared_media",
1267 .data
= &ipv4_devconf
.shared_media
,
1268 .maxlen
= sizeof(int),
1270 .proc_handler
= &proc_dointvec
,
1273 .ctl_name
= NET_IPV4_CONF_RP_FILTER
,
1274 .procname
= "rp_filter",
1275 .data
= &ipv4_devconf
.rp_filter
,
1276 .maxlen
= sizeof(int),
1278 .proc_handler
= &proc_dointvec
,
1281 .ctl_name
= NET_IPV4_CONF_SEND_REDIRECTS
,
1282 .procname
= "send_redirects",
1283 .data
= &ipv4_devconf
.send_redirects
,
1284 .maxlen
= sizeof(int),
1286 .proc_handler
= &proc_dointvec
,
1289 .ctl_name
= NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE
,
1290 .procname
= "accept_source_route",
1291 .data
= &ipv4_devconf
.accept_source_route
,
1292 .maxlen
= sizeof(int),
1294 .proc_handler
= &proc_dointvec
,
1297 .ctl_name
= NET_IPV4_CONF_PROXY_ARP
,
1298 .procname
= "proxy_arp",
1299 .data
= &ipv4_devconf
.proxy_arp
,
1300 .maxlen
= sizeof(int),
1302 .proc_handler
= &proc_dointvec
,
1305 .ctl_name
= NET_IPV4_CONF_MEDIUM_ID
,
1306 .procname
= "medium_id",
1307 .data
= &ipv4_devconf
.medium_id
,
1308 .maxlen
= sizeof(int),
1310 .proc_handler
= &proc_dointvec
,
1313 .ctl_name
= NET_IPV4_CONF_BOOTP_RELAY
,
1314 .procname
= "bootp_relay",
1315 .data
= &ipv4_devconf
.bootp_relay
,
1316 .maxlen
= sizeof(int),
1318 .proc_handler
= &proc_dointvec
,
1321 .ctl_name
= NET_IPV4_CONF_LOG_MARTIANS
,
1322 .procname
= "log_martians",
1323 .data
= &ipv4_devconf
.log_martians
,
1324 .maxlen
= sizeof(int),
1326 .proc_handler
= &proc_dointvec
,
1329 .ctl_name
= NET_IPV4_CONF_TAG
,
1331 .data
= &ipv4_devconf
.tag
,
1332 .maxlen
= sizeof(int),
1334 .proc_handler
= &proc_dointvec
,
1337 .ctl_name
= NET_IPV4_CONF_ARPFILTER
,
1338 .procname
= "arp_filter",
1339 .data
= &ipv4_devconf
.arp_filter
,
1340 .maxlen
= sizeof(int),
1342 .proc_handler
= &proc_dointvec
,
1345 .ctl_name
= NET_IPV4_CONF_ARP_ANNOUNCE
,
1346 .procname
= "arp_announce",
1347 .data
= &ipv4_devconf
.arp_announce
,
1348 .maxlen
= sizeof(int),
1350 .proc_handler
= &proc_dointvec
,
1353 .ctl_name
= NET_IPV4_CONF_ARP_IGNORE
,
1354 .procname
= "arp_ignore",
1355 .data
= &ipv4_devconf
.arp_ignore
,
1356 .maxlen
= sizeof(int),
1358 .proc_handler
= &proc_dointvec
,
1361 .ctl_name
= NET_IPV4_CONF_NOXFRM
,
1362 .procname
= "disable_xfrm",
1363 .data
= &ipv4_devconf
.no_xfrm
,
1364 .maxlen
= sizeof(int),
1366 .proc_handler
= &ipv4_doint_and_flush
,
1367 .strategy
= &ipv4_doint_and_flush_strategy
,
1370 .ctl_name
= NET_IPV4_CONF_NOPOLICY
,
1371 .procname
= "disable_policy",
1372 .data
= &ipv4_devconf
.no_policy
,
1373 .maxlen
= sizeof(int),
1375 .proc_handler
= &ipv4_doint_and_flush
,
1376 .strategy
= &ipv4_doint_and_flush_strategy
,
1379 .ctl_name
= NET_IPV4_CONF_FORCE_IGMP_VERSION
,
1380 .procname
= "force_igmp_version",
1381 .data
= &ipv4_devconf
.force_igmp_version
,
1382 .maxlen
= sizeof(int),
1384 .proc_handler
= &ipv4_doint_and_flush
,
1385 .strategy
= &ipv4_doint_and_flush_strategy
,
1390 .ctl_name
= NET_PROTO_CONF_ALL
,
1393 .child
= devinet_sysctl
.devinet_vars
,
1396 .devinet_conf_dir
= {
1398 .ctl_name
= NET_IPV4_CONF
,
1401 .child
= devinet_sysctl
.devinet_dev
,
1404 .devinet_proto_dir
= {
1406 .ctl_name
= NET_IPV4
,
1409 .child
= devinet_sysctl
.devinet_conf_dir
,
1412 .devinet_root_dir
= {
1414 .ctl_name
= CTL_NET
,
1417 .child
= devinet_sysctl
.devinet_proto_dir
,
1422 static void devinet_sysctl_register(struct in_device
*in_dev
,
1423 struct ipv4_devconf
*p
)
1426 struct net_device
*dev
= in_dev
? in_dev
->dev
: NULL
;
1427 struct devinet_sysctl_table
*t
= kmalloc(sizeof(*t
), GFP_KERNEL
);
1428 char *dev_name
= NULL
;
1432 memcpy(t
, &devinet_sysctl
, sizeof(*t
));
1433 for (i
= 0; i
< ARRAY_SIZE(t
->devinet_vars
) - 1; i
++) {
1434 t
->devinet_vars
[i
].data
+= (char *)p
- (char *)&ipv4_devconf
;
1435 t
->devinet_vars
[i
].de
= NULL
;
1439 dev_name
= dev
->name
;
1440 t
->devinet_dev
[0].ctl_name
= dev
->ifindex
;
1442 dev_name
= "default";
1443 t
->devinet_dev
[0].ctl_name
= NET_PROTO_CONF_DEFAULT
;
1447 * Make a copy of dev_name, because '.procname' is regarded as const
1448 * by sysctl and we wouldn't want anyone to change it under our feet
1449 * (see SIOCSIFNAME).
1451 dev_name
= net_sysctl_strdup(dev_name
);
1455 t
->devinet_dev
[0].procname
= dev_name
;
1456 t
->devinet_dev
[0].child
= t
->devinet_vars
;
1457 t
->devinet_dev
[0].de
= NULL
;
1458 t
->devinet_conf_dir
[0].child
= t
->devinet_dev
;
1459 t
->devinet_conf_dir
[0].de
= NULL
;
1460 t
->devinet_proto_dir
[0].child
= t
->devinet_conf_dir
;
1461 t
->devinet_proto_dir
[0].de
= NULL
;
1462 t
->devinet_root_dir
[0].child
= t
->devinet_proto_dir
;
1463 t
->devinet_root_dir
[0].de
= NULL
;
1465 t
->sysctl_header
= register_sysctl_table(t
->devinet_root_dir
, 0);
1466 if (!t
->sysctl_header
)
1480 static void devinet_sysctl_unregister(struct ipv4_devconf
*p
)
1483 struct devinet_sysctl_table
*t
= p
->sysctl
;
1485 unregister_sysctl_table(t
->sysctl_header
);
1486 kfree(t
->devinet_dev
[0].procname
);
1492 void __init
devinet_init(void)
1494 register_gifconf(PF_INET
, inet_gifconf
);
1495 register_netdevice_notifier(&ip_netdev_notifier
);
1496 rtnetlink_links
[PF_INET
] = inet_rtnetlink_table
;
1497 #ifdef CONFIG_SYSCTL
1498 devinet_sysctl
.sysctl_header
=
1499 register_sysctl_table(devinet_sysctl
.devinet_root_dir
, 0);
1500 devinet_sysctl_register(NULL
, &ipv4_devconf_dflt
);
1504 EXPORT_SYMBOL(devinet_ioctl
);
1505 EXPORT_SYMBOL(in_dev_finish_destroy
);
1506 EXPORT_SYMBOL(inet_select_addr
);
1507 EXPORT_SYMBOL(inetdev_by_index
);
1508 EXPORT_SYMBOL(register_inetaddr_notifier
);
1509 EXPORT_SYMBOL(unregister_inetaddr_notifier
);