2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)in.c 8.4 (Berkeley) 1/9/95
34 * $FreeBSD: src/sys/netinet/in.c,v 1.44.2.14 2002/11/08 00:45:50 suz Exp $
35 * $DragonFly: src/sys/netinet/in.c,v 1.41 2008/08/17 05:20:10 sephe Exp $
38 #include "opt_bootp.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sockio.h>
43 #include <sys/malloc.h>
46 #include <sys/msgport.h>
47 #include <sys/socket.h>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
51 #include <sys/thread2.h>
54 #include <net/if_types.h>
55 #include <net/route.h>
56 #include <net/netmsg2.h>
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet/in_pcb.h>
62 #include <netinet/igmp_var.h>
64 MALLOC_DEFINE(M_IPMADDR
, "in_multi", "internet multicast address");
66 static int in_mask2len (struct in_addr
*);
67 static void in_len2mask (struct in_addr
*, int);
68 static int in_lifaddr_ioctl (struct socket
*, u_long
, caddr_t
,
69 struct ifnet
*, struct thread
*);
71 static void in_socktrim (struct sockaddr_in
*);
72 static int in_ifinit(struct ifnet
*, struct in_ifaddr
*,
73 const struct sockaddr_in
*, int);
75 static void in_control_dispatch(struct netmsg
*);
76 static int in_control_internal(u_long
, caddr_t
, struct ifnet
*,
79 static int in_addprefix(struct in_ifaddr
*, int);
80 static void in_scrubprefix(struct in_ifaddr
*);
82 static int subnetsarelocal
= 0;
83 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, subnets_are_local
, CTLFLAG_RW
,
84 &subnetsarelocal
, 0, "");
86 struct in_multihead in_multihead
; /* XXX BSS initialization */
88 extern struct inpcbinfo ripcbinfo
;
89 extern struct inpcbinfo udbinfo
;
92 * Return 1 if an internet address is for a ``local'' host
93 * (one to which we have a connection). If subnetsarelocal
94 * is true, this includes other subnets of the local net.
95 * Otherwise, it includes only the directly-connected (sub)nets.
98 in_localaddr(struct in_addr in
)
100 u_long i
= ntohl(in
.s_addr
);
101 struct in_ifaddr_container
*iac
;
102 struct in_ifaddr
*ia
;
104 if (subnetsarelocal
) {
105 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
108 if ((i
& ia
->ia_netmask
) == ia
->ia_net
)
112 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
115 if ((i
& ia
->ia_subnetmask
) == ia
->ia_subnet
)
123 * Determine whether an IP address is in a reserved set of addresses
124 * that may not be forwarded, or whether datagrams to that destination
128 in_canforward(struct in_addr in
)
130 u_long i
= ntohl(in
.s_addr
);
133 if (IN_EXPERIMENTAL(i
) || IN_MULTICAST(i
))
136 net
= i
& IN_CLASSA_NET
;
137 if (net
== 0 || net
== (IN_LOOPBACKNET
<< IN_CLASSA_NSHIFT
))
144 * Trim a mask in a sockaddr
147 in_socktrim(struct sockaddr_in
*ap
)
149 char *cplim
= (char *) &ap
->sin_addr
;
150 char *cp
= (char *) (&ap
->sin_addr
+ 1);
153 while (--cp
>= cplim
)
155 (ap
)->sin_len
= cp
- (char *) (ap
) + 1;
161 in_mask2len(struct in_addr
*mask
)
167 for (x
= 0; x
< sizeof *mask
; x
++) {
172 if (x
< sizeof *mask
) {
173 for (y
= 0; y
< 8; y
++) {
174 if ((p
[x
] & (0x80 >> y
)) == 0)
182 in_len2mask(struct in_addr
*mask
, int len
)
188 bzero(mask
, sizeof *mask
);
189 for (i
= 0; i
< len
/ 8; i
++)
192 p
[i
] = (0xff00 >> (len
% 8)) & 0xff;
195 static int in_interfaces
; /* number of external internet interfaces */
197 struct in_control_arg
{
205 in_control_dispatch(struct netmsg
*nmsg
)
207 struct lwkt_msg
*msg
= &nmsg
->nm_lmsg
;
208 const struct in_control_arg
*arg
= msg
->u
.ms_resultp
;
211 error
= in_control_internal(arg
->cmd
, arg
->data
, arg
->ifp
, arg
->td
);
212 lwkt_replymsg(msg
, error
);
216 * Generic internet control operations (ioctl's).
217 * Ifp is 0 if not an interface-specific ioctl.
219 * NOTE! td might be NULL.
223 in_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
227 struct in_control_arg arg
;
228 struct lwkt_msg
*msg
;
234 if (td
&& (error
= priv_check(td
, PRIV_ROOT
)) != 0)
240 return in_lifaddr_ioctl(so
, cmd
, data
, ifp
, td
);
243 KASSERT(cmd
!= SIOCALIFADDR
&& cmd
!= SIOCDLIFADDR
,
244 ("recursive SIOC%cLIFADDR!\n",
245 cmd
== SIOCDLIFADDR
? 'D' : 'A'));
248 * IFADDR alterations are serialized by netisr0
257 bzero(&arg
, sizeof(arg
));
263 netmsg_init(&nmsg
, NULL
, &curthread
->td_msgport
,
264 0, in_control_dispatch
);
266 msg
->u
.ms_resultp
= &arg
;
268 lwkt_domsg(cpu_portfn(0), msg
, 0);
269 return msg
->ms_error
;
271 return in_control_internal(cmd
, data
, ifp
, td
);
276 in_ialink_dispatch(struct netmsg
*nmsg
)
278 struct lwkt_msg
*lmsg
= &nmsg
->nm_lmsg
;
279 struct in_ifaddr
*ia
= lmsg
->u
.ms_resultp
;
280 struct ifaddr_container
*ifac
;
281 struct in_ifaddr_container
*iac
;
286 ifac
= &ia
->ia_ifa
.ifa_containers
[cpu
];
287 ASSERT_IFAC_VALID(ifac
);
288 KASSERT((ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHEAD
) == 0,
289 ("ia is on in_ifaddrheads\n"));
291 ifac
->ifa_listmask
|= IFA_LIST_IN_IFADDRHEAD
;
292 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
293 TAILQ_INSERT_TAIL(&in_ifaddrheads
[cpu
], iac
, ia_link
);
297 ifa_forwardmsg(lmsg
, cpu
+ 1);
301 in_iaunlink_dispatch(struct netmsg
*nmsg
)
303 struct lwkt_msg
*lmsg
= &nmsg
->nm_lmsg
;
304 struct in_ifaddr
*ia
= lmsg
->u
.ms_resultp
;
305 struct ifaddr_container
*ifac
;
306 struct in_ifaddr_container
*iac
;
311 ifac
= &ia
->ia_ifa
.ifa_containers
[cpu
];
312 ASSERT_IFAC_VALID(ifac
);
313 KASSERT(ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHEAD
,
314 ("ia is not on in_ifaddrheads\n"));
316 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
317 TAILQ_REMOVE(&in_ifaddrheads
[cpu
], iac
, ia_link
);
318 ifac
->ifa_listmask
&= ~IFA_LIST_IN_IFADDRHEAD
;
322 ifa_forwardmsg(lmsg
, cpu
+ 1);
326 in_iahashins_dispatch(struct netmsg
*nmsg
)
328 struct lwkt_msg
*lmsg
= &nmsg
->nm_lmsg
;
329 struct in_ifaddr
*ia
= lmsg
->u
.ms_resultp
;
330 struct ifaddr_container
*ifac
;
331 struct in_ifaddr_container
*iac
;
336 ifac
= &ia
->ia_ifa
.ifa_containers
[cpu
];
337 ASSERT_IFAC_VALID(ifac
);
338 KASSERT((ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
) == 0,
339 ("ia is on in_ifaddrhashtbls\n"));
341 ifac
->ifa_listmask
|= IFA_LIST_IN_IFADDRHASH
;
342 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
343 LIST_INSERT_HEAD(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
348 ifa_forwardmsg(lmsg
, cpu
+ 1);
352 in_iahashrem_dispatch(struct netmsg
*nmsg
)
354 struct lwkt_msg
*lmsg
= &nmsg
->nm_lmsg
;
355 struct in_ifaddr
*ia
= lmsg
->u
.ms_resultp
;
356 struct ifaddr_container
*ifac
;
357 struct in_ifaddr_container
*iac
;
362 ifac
= &ia
->ia_ifa
.ifa_containers
[cpu
];
363 ASSERT_IFAC_VALID(ifac
);
364 KASSERT(ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
,
365 ("ia is not on in_ifaddrhashtbls\n"));
367 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
368 LIST_REMOVE(iac
, ia_hash
);
369 ifac
->ifa_listmask
&= ~IFA_LIST_IN_IFADDRHASH
;
373 ifa_forwardmsg(lmsg
, cpu
+ 1);
377 in_ialink(struct in_ifaddr
*ia
)
380 struct lwkt_msg
*lmsg
;
382 netmsg_init(&nmsg
, NULL
, &curthread
->td_msgport
,
383 0, in_ialink_dispatch
);
384 lmsg
= &nmsg
.nm_lmsg
;
385 lmsg
->u
.ms_resultp
= ia
;
391 in_iaunlink(struct in_ifaddr
*ia
)
394 struct lwkt_msg
*lmsg
;
396 netmsg_init(&nmsg
, NULL
, &curthread
->td_msgport
,
397 0, in_iaunlink_dispatch
);
398 lmsg
= &nmsg
.nm_lmsg
;
399 lmsg
->u
.ms_resultp
= ia
;
405 in_iahash_insert(struct in_ifaddr
*ia
)
408 struct lwkt_msg
*lmsg
;
410 netmsg_init(&nmsg
, NULL
, &curthread
->td_msgport
,
411 0, in_iahashins_dispatch
);
412 lmsg
= &nmsg
.nm_lmsg
;
413 lmsg
->u
.ms_resultp
= ia
;
419 in_iahash_remove(struct in_ifaddr
*ia
)
422 struct lwkt_msg
*lmsg
;
424 netmsg_init(&nmsg
, NULL
, &curthread
->td_msgport
,
425 0, in_iahashrem_dispatch
);
426 lmsg
= &nmsg
.nm_lmsg
;
427 lmsg
->u
.ms_resultp
= ia
;
432 static __inline
struct in_ifaddr
*
433 in_ianext(struct in_ifaddr
*oia
)
435 struct ifaddr_container
*ifac
;
436 struct in_ifaddr_container
*iac
;
438 ifac
= &oia
->ia_ifa
.ifa_containers
[mycpuid
];
439 ASSERT_IFAC_VALID(ifac
);
440 KASSERT(ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHEAD
,
441 ("ia is not on in_ifaddrheads\n"));
443 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
444 iac
= TAILQ_NEXT(iac
, ia_link
);
452 in_control_internal(u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
455 struct ifreq
*ifr
= (struct ifreq
*)data
;
456 struct in_ifaddr
*ia
= NULL
;
458 struct in_aliasreq
*ifra
= (struct in_aliasreq
*)data
;
459 struct ifaddr_container
*ifac
;
460 struct in_ifaddr_container
*iac
;
461 struct sockaddr_in oldaddr
;
462 int hostIsNew
, iaIsNew
, maskIsNew
, ifpWasUp
;
469 * Find address for this interface, if it exists.
471 * If an alias address was specified, find that one instead of
472 * the first one on the interface, if possible
475 struct in_ifaddr
*iap
;
477 dst
= ((struct sockaddr_in
*)&ifr
->ifr_addr
)->sin_addr
;
478 LIST_FOREACH(iac
, INADDR_HASH(dst
.s_addr
), ia_hash
) {
480 if (iap
->ia_ifp
== ifp
&&
481 iap
->ia_addr
.sin_addr
.s_addr
== dst
.s_addr
) {
487 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
],
489 iap
= ifatoia(ifac
->ifa
);
490 if (iap
->ia_addr
.sin_family
== AF_INET
) {
497 if (ifp
->if_flags
& IFF_UP
)
505 return (EADDRNOTAVAIL
);
506 if (ifra
->ifra_addr
.sin_family
== AF_INET
) {
508 if (ia
->ia_ifp
== ifp
&&
509 ia
->ia_addr
.sin_addr
.s_addr
==
510 ifra
->ifra_addr
.sin_addr
.s_addr
)
514 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
515 cmd
== SIOCAIFADDR
&&
516 ifra
->ifra_dstaddr
.sin_addr
.s_addr
== INADDR_ANY
) {
520 if (cmd
== SIOCDIFADDR
&& ia
== NULL
)
521 return (EADDRNOTAVAIL
);
526 if (td
&& (error
= priv_check(td
, PRIV_ROOT
)) != 0)
530 return (EADDRNOTAVAIL
);
532 if (cmd
== SIOCSIFDSTADDR
&&
533 (ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
540 ia
= ifa_create(sizeof(*ia
), M_WAITOK
);
544 * Setup per-CPU information
546 for (i
= 0; i
< ncpus
; ++i
) {
547 ifac
= &ifa
->ifa_containers
[i
];
548 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
554 * Protect from NETISR_IP traversing address list
555 * while we're modifying it.
560 ifa_iflink(ifa
, ifp
, 1);
562 ifa
->ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
563 ifa
->ifa_dstaddr
= (struct sockaddr
*)&ia
->ia_dstaddr
;
564 ifa
->ifa_netmask
= (struct sockaddr
*)&ia
->ia_sockmask
;
565 ia
->ia_sockmask
.sin_len
= 8;
566 ia
->ia_sockmask
.sin_family
= AF_INET
;
567 if (ifp
->if_flags
& IFF_BROADCAST
) {
568 ia
->ia_broadaddr
.sin_len
= sizeof ia
->ia_addr
;
569 ia
->ia_broadaddr
.sin_family
= AF_INET
;
572 if (!(ifp
->if_flags
& IFF_LOOPBACK
))
581 if (td
&& (error
= priv_check(td
, PRIV_ROOT
)) != 0)
590 return (EADDRNOTAVAIL
);
596 *((struct sockaddr_in
*)&ifr
->ifr_addr
) = ia
->ia_addr
;
600 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
602 *((struct sockaddr_in
*)&ifr
->ifr_dstaddr
) = ia
->ia_broadaddr
;
606 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
608 *((struct sockaddr_in
*)&ifr
->ifr_dstaddr
) = ia
->ia_dstaddr
;
612 *((struct sockaddr_in
*)&ifr
->ifr_addr
) = ia
->ia_sockmask
;
616 KKASSERT(ifp
->if_flags
& IFF_POINTOPOINT
);
618 oldaddr
= ia
->ia_dstaddr
;
619 ia
->ia_dstaddr
= *(struct sockaddr_in
*)&ifr
->ifr_dstaddr
;
620 if (ifp
->if_ioctl
!= NULL
) {
621 ifnet_serialize_all(ifp
);
622 error
= ifp
->if_ioctl(ifp
, SIOCSIFDSTADDR
, (caddr_t
)ia
,
623 td
->td_proc
->p_ucred
);
624 ifnet_deserialize_all(ifp
);
626 ia
->ia_dstaddr
= oldaddr
;
630 if (ia
->ia_flags
& IFA_ROUTE
) {
631 ia
->ia_ifa
.ifa_dstaddr
= (struct sockaddr
*)&oldaddr
;
632 rtinit(&ia
->ia_ifa
, RTM_DELETE
, RTF_HOST
);
633 ia
->ia_ifa
.ifa_dstaddr
=
634 (struct sockaddr
*)&ia
->ia_dstaddr
;
635 rtinit(&ia
->ia_ifa
, RTM_ADD
, RTF_HOST
| RTF_UP
);
640 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
642 ia
->ia_broadaddr
= *(struct sockaddr_in
*)&ifr
->ifr_broadaddr
;
646 error
= in_ifinit(ifp
, ia
,
647 (const struct sockaddr_in
*)&ifr
->ifr_addr
, 1);
648 if (error
!= 0 && iaIsNew
)
651 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
,
652 iaIsNew
? IFADDR_EVENT_ADD
: IFADDR_EVENT_CHANGE
,
655 if (!ifpWasUp
&& (ifp
->if_flags
& IFF_UP
)) {
657 * Interface is brought up by in_ifinit()
658 * (via ifp->if_ioctl). We act as if the
659 * interface got IFF_UP flag turned on.
666 ia
->ia_sockmask
.sin_addr
= ifra
->ifra_addr
.sin_addr
;
667 ia
->ia_subnetmask
= ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
);
674 if (ia
->ia_addr
.sin_family
== AF_INET
) {
675 if (ifra
->ifra_addr
.sin_len
== 0) {
676 ifra
->ifra_addr
= ia
->ia_addr
;
678 } else if (ifra
->ifra_addr
.sin_addr
.s_addr
==
679 ia
->ia_addr
.sin_addr
.s_addr
) {
683 if (ifra
->ifra_mask
.sin_len
) {
685 ia
->ia_sockmask
= ifra
->ifra_mask
;
686 ia
->ia_sockmask
.sin_family
= AF_INET
;
688 ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
);
691 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
692 ifra
->ifra_dstaddr
.sin_family
== AF_INET
) {
694 ia
->ia_dstaddr
= ifra
->ifra_dstaddr
;
695 maskIsNew
= 1; /* We lie; but the effect's the same */
697 if (ifra
->ifra_addr
.sin_family
== AF_INET
&&
698 (hostIsNew
|| maskIsNew
))
699 error
= in_ifinit(ifp
, ia
, &ifra
->ifra_addr
, 0);
701 if (error
!= 0 && iaIsNew
)
704 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
705 ifra
->ifra_broadaddr
.sin_family
== AF_INET
)
706 ia
->ia_broadaddr
= ifra
->ifra_broadaddr
;
708 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
,
709 iaIsNew
? IFADDR_EVENT_ADD
: IFADDR_EVENT_CHANGE
,
712 if (!ifpWasUp
&& (ifp
->if_flags
& IFF_UP
)) {
713 /* See the comment in SIOCSIFADDR */
720 * in_ifscrub kills the interface route.
724 * in_ifadown gets rid of all the rest of
725 * the routes. This is not quite the right
726 * thing to do, but at least if we are running
727 * a routing process they will come back.
729 in_ifadown(&ia
->ia_ifa
, 1);
730 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
, IFADDR_EVENT_DELETE
,
736 if (ifp
== NULL
|| ifp
->if_ioctl
== NULL
)
738 ifnet_serialize_all(ifp
);
739 error
= ifp
->if_ioctl(ifp
, cmd
, data
, td
->td_proc
->p_ucred
);
740 ifnet_deserialize_all(ifp
);
744 KKASSERT(cmd
== SIOCDIFADDR
||
745 ((cmd
== SIOCAIFADDR
|| cmd
== SIOCSIFADDR
) && iaIsNew
));
747 ifa_ifunlink(&ia
->ia_ifa
, ifp
);
750 if (cmd
== SIOCDIFADDR
) {
751 ifac
= &ia
->ia_ifa
.ifa_containers
[mycpuid
];
752 if (ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
)
753 in_iahash_remove(ia
);
758 * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
759 * already taken care of the deletion from hash table
761 ifac
= &ia
->ia_ifa
.ifa_containers
[mycpuid
];
762 KASSERT((ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
) == 0,
763 ("SIOC%cIFADDR failed on new ia, "
764 "but the new ia is still in hash table\n",
765 cmd
== SIOCSIFADDR
? 'S' : 'A'));
769 ifa_destroy(&ia
->ia_ifa
);
771 if ((cmd
== SIOCAIFADDR
|| cmd
== SIOCSIFADDR
) &&
772 !ifpWasUp
&& (ifp
->if_flags
& IFF_UP
)) {
774 * Though the address assignment failed, the
775 * interface is brought up by in_ifinit()
776 * (via ifp->if_ioctl). With the hope that
777 * the interface has some valid addresses, we
778 * act as if IFF_UP flag was just set on the
782 * This could only be done after the failed
783 * address is unlinked from the global address
794 * SIOCGLIFADDR: get first address. (?!?)
795 * SIOCGLIFADDR with IFLR_PREFIX:
796 * get first address that matches the specified prefix.
797 * SIOCALIFADDR: add the specified address.
798 * SIOCALIFADDR with IFLR_PREFIX:
799 * EINVAL since we can't deduce hostid part of the address.
800 * SIOCDLIFADDR: delete the specified address.
801 * SIOCDLIFADDR with IFLR_PREFIX:
802 * delete the first address that matches the specified prefix.
804 * EINVAL on invalid parameters
805 * EADDRNOTAVAIL on prefix match failed/specified address not found
806 * other values may be returned from in_ioctl()
808 * NOTE! td might be NULL.
811 in_lifaddr_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
814 struct if_laddrreq
*iflr
= (struct if_laddrreq
*)data
;
818 panic("invalid argument to in_lifaddr_ioctl");
824 /* address must be specified on GET with IFLR_PREFIX */
825 if ((iflr
->flags
& IFLR_PREFIX
) == 0)
830 /* address must be specified on ADD and DELETE */
831 if (iflr
->addr
.ss_family
!= AF_INET
)
833 if (iflr
->addr
.ss_len
!= sizeof(struct sockaddr_in
))
835 /* XXX need improvement */
836 if (iflr
->dstaddr
.ss_family
837 && iflr
->dstaddr
.ss_family
!= AF_INET
)
839 if (iflr
->dstaddr
.ss_family
840 && iflr
->dstaddr
.ss_len
!= sizeof(struct sockaddr_in
))
843 default: /*shouldn't happen*/
846 if (sizeof(struct in_addr
) * 8 < iflr
->prefixlen
)
852 struct in_aliasreq ifra
;
854 if (iflr
->flags
& IFLR_PREFIX
)
857 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
858 bzero(&ifra
, sizeof ifra
);
859 bcopy(iflr
->iflr_name
, ifra
.ifra_name
, sizeof ifra
.ifra_name
);
861 bcopy(&iflr
->addr
, &ifra
.ifra_addr
, iflr
->addr
.ss_len
);
863 if (iflr
->dstaddr
.ss_family
) { /*XXX*/
864 bcopy(&iflr
->dstaddr
, &ifra
.ifra_dstaddr
,
865 iflr
->dstaddr
.ss_len
);
868 ifra
.ifra_mask
.sin_family
= AF_INET
;
869 ifra
.ifra_mask
.sin_len
= sizeof(struct sockaddr_in
);
870 in_len2mask(&ifra
.ifra_mask
.sin_addr
, iflr
->prefixlen
);
872 return in_control(so
, SIOCAIFADDR
, (caddr_t
)&ifra
, ifp
, td
);
877 struct ifaddr_container
*ifac
;
878 struct in_ifaddr
*ia
;
879 struct in_addr mask
, candidate
, match
;
880 struct sockaddr_in
*sin
;
883 bzero(&mask
, sizeof mask
);
884 if (iflr
->flags
& IFLR_PREFIX
) {
885 /* lookup a prefix rather than address. */
886 in_len2mask(&mask
, iflr
->prefixlen
);
888 sin
= (struct sockaddr_in
*)&iflr
->addr
;
889 match
.s_addr
= sin
->sin_addr
.s_addr
;
890 match
.s_addr
&= mask
.s_addr
;
892 /* if you set extra bits, that's wrong */
893 if (match
.s_addr
!= sin
->sin_addr
.s_addr
)
898 if (cmd
== SIOCGLIFADDR
) {
899 /* on getting an address, take the 1st match */
900 match
.s_addr
= 0; /* gcc4 warning */
903 /* on deleting an address, do exact match */
904 in_len2mask(&mask
, 32);
905 sin
= (struct sockaddr_in
*)&iflr
->addr
;
906 match
.s_addr
= sin
->sin_addr
.s_addr
;
912 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
913 struct ifaddr
*ifa
= ifac
->ifa
;
915 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
920 ((struct sockaddr_in
*)&ifa
->ifa_addr
)->sin_addr
.s_addr
;
921 candidate
.s_addr
&= mask
.s_addr
;
922 if (candidate
.s_addr
== match
.s_addr
)
926 return EADDRNOTAVAIL
;
927 ia
= (struct in_ifaddr
*)(ifac
->ifa
);
929 if (cmd
== SIOCGLIFADDR
) {
930 /* fill in the if_laddrreq structure */
931 bcopy(&ia
->ia_addr
, &iflr
->addr
, ia
->ia_addr
.sin_len
);
933 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
934 bcopy(&ia
->ia_dstaddr
, &iflr
->dstaddr
,
935 ia
->ia_dstaddr
.sin_len
);
937 bzero(&iflr
->dstaddr
, sizeof iflr
->dstaddr
);
940 in_mask2len(&ia
->ia_sockmask
.sin_addr
);
942 iflr
->flags
= 0; /*XXX*/
946 struct in_aliasreq ifra
;
948 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
949 bzero(&ifra
, sizeof ifra
);
950 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
951 sizeof ifra
.ifra_name
);
953 bcopy(&ia
->ia_addr
, &ifra
.ifra_addr
,
954 ia
->ia_addr
.sin_len
);
955 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
956 bcopy(&ia
->ia_dstaddr
, &ifra
.ifra_dstaddr
,
957 ia
->ia_dstaddr
.sin_len
);
959 bcopy(&ia
->ia_sockmask
, &ifra
.ifra_dstaddr
,
960 ia
->ia_sockmask
.sin_len
);
962 return in_control(so
, SIOCDIFADDR
, (caddr_t
)&ifra
,
968 return EOPNOTSUPP
; /*just for safety*/
972 * Delete any existing route for an interface.
975 in_ifscrub(struct ifnet
*ifp __unused
, struct in_ifaddr
*ia
)
981 * Initialize an interface's internet address
982 * and routing table entry.
985 in_ifinit(struct ifnet
*ifp
, struct in_ifaddr
*ia
,
986 const struct sockaddr_in
*sin
, int scrub
)
988 u_long i
= ntohl(sin
->sin_addr
.s_addr
);
989 struct sockaddr_in oldaddr
;
990 struct ifaddr_container
*ifac
;
991 int flags
= RTF_UP
, error
= 0;
994 ifac
= &ia
->ia_ifa
.ifa_containers
[mycpuid
];
995 oldaddr
= ia
->ia_addr
;
997 if (ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
) {
999 in_iahash_remove(ia
);
1003 if (ia
->ia_addr
.sin_family
== AF_INET
)
1004 in_iahash_insert(ia
);
1007 * Give the interface a chance to initialize
1008 * if this is its first address,
1009 * and to validate the address if necessary.
1011 if (ifp
->if_ioctl
!= NULL
) {
1012 ifnet_serialize_all(ifp
);
1013 error
= ifp
->if_ioctl(ifp
, SIOCSIFADDR
, (caddr_t
)ia
, NULL
);
1014 ifnet_deserialize_all(ifp
);
1020 * Delete old route, if requested.
1023 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&oldaddr
;
1024 in_ifscrub(ifp
, ia
);
1025 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
1029 * Calculate netmask/subnetmask.
1032 ia
->ia_netmask
= IN_CLASSA_NET
;
1033 else if (IN_CLASSB(i
))
1034 ia
->ia_netmask
= IN_CLASSB_NET
;
1036 ia
->ia_netmask
= IN_CLASSC_NET
;
1038 * The subnet mask usually includes at least the standard network part,
1039 * but may may be smaller in the case of supernetting.
1040 * If it is set, we believe it.
1042 if (ia
->ia_subnetmask
== 0) {
1043 ia
->ia_subnetmask
= ia
->ia_netmask
;
1044 ia
->ia_sockmask
.sin_addr
.s_addr
= htonl(ia
->ia_subnetmask
);
1046 ia
->ia_netmask
&= ia
->ia_subnetmask
;
1048 ia
->ia_net
= i
& ia
->ia_netmask
;
1049 ia
->ia_subnet
= i
& ia
->ia_subnetmask
;
1050 in_socktrim(&ia
->ia_sockmask
);
1053 * Add route for the network.
1055 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
1056 if (ifp
->if_flags
& IFF_BROADCAST
) {
1057 ia
->ia_broadaddr
.sin_addr
.s_addr
=
1058 htonl(ia
->ia_subnet
| ~ia
->ia_subnetmask
);
1059 ia
->ia_netbroadcast
.s_addr
=
1060 htonl(ia
->ia_net
| ~ ia
->ia_netmask
);
1061 } else if (ifp
->if_flags
& IFF_LOOPBACK
) {
1062 ia
->ia_dstaddr
= ia
->ia_addr
;
1064 } else if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1065 if (ia
->ia_dstaddr
.sin_family
!= AF_INET
)
1071 * Don't add host routes for interface addresses of
1072 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
1073 * possible to assign several such address pairs with consistent
1074 * results (no host route) and is required by BOOTP.
1076 * XXX: This is ugly ! There should be a way for the caller to
1077 * say that they don't want a host route.
1079 if (ia
->ia_addr
.sin_addr
.s_addr
!= INADDR_ANY
||
1080 ia
->ia_netmask
!= IN_CLASSA_NET
||
1081 ia
->ia_dstaddr
.sin_addr
.s_addr
!= htonl(IN_CLASSA_HOST
)) {
1082 error
= in_addprefix(ia
, flags
);
1088 * If the interface supports multicast, join the "all hosts"
1089 * multicast group on that interface.
1091 if (ifp
->if_flags
& IFF_MULTICAST
) {
1092 struct in_addr addr
;
1094 addr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
1095 in_addmulti(&addr
, ifp
);
1099 if (ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
)
1100 in_iahash_remove(ia
);
1102 ia
->ia_addr
= oldaddr
;
1104 in_iahash_insert(ia
);
1108 #define rtinitflags(x) \
1109 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
1113 * Add a route to prefix ("connected route" in cisco terminology).
1114 * Do nothing, if there are some interface addresses with the same
1115 * prefix already. This function assumes that the 'target' parent
1119 in_addprefix(struct in_ifaddr
*target
, int flags
)
1121 struct in_ifaddr_container
*iac
;
1122 struct in_addr prefix
, mask
;
1125 mask
= target
->ia_sockmask
.sin_addr
;
1126 if (flags
& RTF_HOST
) {
1127 prefix
= target
->ia_dstaddr
.sin_addr
;
1129 prefix
= target
->ia_addr
.sin_addr
;
1130 prefix
.s_addr
&= mask
.s_addr
;
1133 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
1134 struct in_ifaddr
*ia
= iac
->ia
;
1137 /* Don't test against self */
1141 /* The tested address does not own a route entry */
1142 if ((ia
->ia_flags
& IFA_ROUTE
) == 0)
1146 if (rtinitflags(ia
)) {
1147 p
= ia
->ia_dstaddr
.sin_addr
;
1149 p
= ia
->ia_addr
.sin_addr
;
1150 p
.s_addr
&= ia
->ia_sockmask
.sin_addr
.s_addr
;
1152 if (prefix
.s_addr
!= p
.s_addr
)
1156 * If the to-be-added address and the curretly being
1157 * tested address are not host addresses, we need to
1158 * take subnetmask into consideration.
1160 if (!(flags
& RTF_HOST
) && !rtinitflags(ia
) &&
1161 mask
.s_addr
!= ia
->ia_sockmask
.sin_addr
.s_addr
)
1165 * If we got a matching prefix route inserted by other
1166 * interface address, we don't need to bother.
1172 * No one seem to have prefix route; insert it.
1174 error
= rtinit(&target
->ia_ifa
, RTM_ADD
, flags
);
1176 target
->ia_flags
|= IFA_ROUTE
;
1181 * Remove a route to prefix ("connected route" in cisco terminology).
1182 * Re-installs the route by using another interface address, if there's
1183 * one with the same prefix (otherwise we lose the route mistakenly).
1186 in_scrubprefix(struct in_ifaddr
*target
)
1188 struct in_ifaddr_container
*iac
;
1189 struct in_addr prefix
, mask
;
1192 if ((target
->ia_flags
& IFA_ROUTE
) == 0)
1195 mask
= target
->ia_sockmask
.sin_addr
;
1196 if (rtinitflags(target
)) {
1197 prefix
= target
->ia_dstaddr
.sin_addr
;
1199 prefix
= target
->ia_addr
.sin_addr
;
1200 prefix
.s_addr
&= mask
.s_addr
;
1203 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
1204 struct in_ifaddr
*ia
= iac
->ia
;
1207 /* Don't test against self */
1211 /* The tested address already owns a route entry */
1212 if (ia
->ia_flags
& IFA_ROUTE
)
1216 * The prefix route of the tested address should
1217 * never be installed if its parent interface is
1220 if ((ia
->ia_ifp
->if_flags
& IFF_UP
) == 0)
1224 if (rtinitflags(ia
)) {
1225 p
= ia
->ia_dstaddr
.sin_addr
;
1227 p
= ia
->ia_addr
.sin_addr
;
1228 p
.s_addr
&= ia
->ia_sockmask
.sin_addr
.s_addr
;
1230 if (prefix
.s_addr
!= p
.s_addr
)
1234 * We don't need to test subnetmask here, as what we do
1235 * in in_addprefix(), since if the the tested address's
1236 * parent interface is UP, the tested address should own
1237 * a prefix route entry and we would never reach here.
1241 * If we got a matching prefix route, move IFA_ROUTE to him
1243 rtinit(&target
->ia_ifa
, RTM_DELETE
, rtinitflags(target
));
1244 target
->ia_flags
&= ~IFA_ROUTE
;
1246 error
= rtinit(&ia
->ia_ifa
, RTM_ADD
, rtinitflags(ia
) | RTF_UP
);
1248 ia
->ia_flags
|= IFA_ROUTE
;
1253 * No candidates for this prefix route; just remove it.
1255 rtinit(&target
->ia_ifa
, RTM_DELETE
, rtinitflags(target
));
1256 target
->ia_flags
&= ~IFA_ROUTE
;
1262 * Return 1 if the address might be a local broadcast address.
1265 in_broadcast(struct in_addr in
, struct ifnet
*ifp
)
1267 struct ifaddr_container
*ifac
;
1270 if (in
.s_addr
== INADDR_BROADCAST
||
1271 in
.s_addr
== INADDR_ANY
)
1273 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
1275 t
= ntohl(in
.s_addr
);
1277 * Look through the list of addresses for a match
1278 * with a broadcast address.
1280 #define ia ((struct in_ifaddr *)ifa)
1281 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1282 struct ifaddr
*ifa
= ifac
->ifa
;
1284 if (ifa
->ifa_addr
->sa_family
== AF_INET
&&
1285 (in
.s_addr
== ia
->ia_broadaddr
.sin_addr
.s_addr
||
1286 in
.s_addr
== ia
->ia_netbroadcast
.s_addr
||
1288 * Check for old-style (host 0) broadcast.
1290 t
== ia
->ia_subnet
|| t
== ia
->ia_net
) &&
1292 * Check for an all one subnetmask. These
1293 * only exist when an interface gets a secondary
1296 ia
->ia_subnetmask
!= (u_long
)0xffffffff)
1303 * Add an address to the list of IP multicast addresses for a given interface.
1306 in_addmulti(struct in_addr
*ap
, struct ifnet
*ifp
)
1308 struct in_multi
*inm
;
1310 struct sockaddr_in sin
;
1311 struct ifmultiaddr
*ifma
;
1314 * Call generic routine to add membership or increment
1315 * refcount. It wants addresses in the form of a sockaddr,
1316 * so we build one here (being careful to zero the unused bytes).
1318 bzero(&sin
, sizeof sin
);
1319 sin
.sin_family
= AF_INET
;
1320 sin
.sin_len
= sizeof sin
;
1323 error
= if_addmulti(ifp
, (struct sockaddr
*)&sin
, &ifma
);
1330 * If ifma->ifma_protospec is null, then if_addmulti() created
1331 * a new record. Otherwise, we are done.
1333 if (ifma
->ifma_protospec
!= 0) {
1335 return ifma
->ifma_protospec
;
1338 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1339 at interrupt time? If so, need to fix if_addmulti. XXX */
1340 inm
= kmalloc(sizeof *inm
, M_IPMADDR
, M_WAITOK
| M_ZERO
);
1341 inm
->inm_addr
= *ap
;
1343 inm
->inm_ifma
= ifma
;
1344 ifma
->ifma_protospec
= inm
;
1345 LIST_INSERT_HEAD(&in_multihead
, inm
, inm_link
);
1348 * Let IGMP know that we have joined a new IP multicast group.
1350 igmp_joingroup(inm
);
1356 * Delete a multicast address record.
1359 in_delmulti(struct in_multi
*inm
)
1361 struct ifmultiaddr
*ifma
;
1362 struct in_multi my_inm
;
1365 ifma
= inm
->inm_ifma
;
1366 my_inm
.inm_ifp
= NULL
; /* don't send the leave msg */
1367 if (ifma
->ifma_refcount
== 1) {
1369 * No remaining claims to this record; let IGMP know that
1370 * we are leaving the multicast group.
1371 * But do it after the if_delmulti() which might reset
1372 * the interface and nuke the packet.
1375 ifma
->ifma_protospec
= 0;
1376 LIST_REMOVE(inm
, inm_link
);
1377 kfree(inm
, M_IPMADDR
);
1379 /* XXX - should be separate API for when we have an ifma? */
1380 if_delmulti(ifma
->ifma_ifp
, ifma
->ifma_addr
);
1381 if (my_inm
.inm_ifp
!= NULL
)
1382 igmp_leavegroup(&my_inm
);
1387 in_ifdetach(struct ifnet
*ifp
)
1389 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo
.pcblisthead
), ifp
);
1390 in_pcbpurgeif0(LIST_FIRST(&udbinfo
.pcblisthead
), ifp
);