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.37 2008/06/08 08:38:05 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>
45 #include <sys/msgport.h>
46 #include <sys/socket.h>
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
50 #include <sys/thread2.h>
53 #include <net/if_types.h>
54 #include <net/route.h>
55 #include <net/netmsg2.h>
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
59 #include <netinet/in_pcb.h>
61 #include <netinet/igmp_var.h>
63 MALLOC_DEFINE(M_IPMADDR
, "in_multi", "internet multicast address");
65 static int in_mask2len (struct in_addr
*);
66 static void in_len2mask (struct in_addr
*, int);
67 static int in_lifaddr_ioctl (struct socket
*, u_long
, caddr_t
,
68 struct ifnet
*, struct thread
*);
70 static void in_socktrim (struct sockaddr_in
*);
71 static int in_ifinit(struct ifnet
*, struct in_ifaddr
*,
72 const struct sockaddr_in
*, int);
74 static void in_control_dispatch(struct netmsg
*);
75 static int in_control_internal(u_long
, caddr_t
, struct ifnet
*,
78 static int subnetsarelocal
= 0;
79 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, subnets_are_local
, CTLFLAG_RW
,
80 &subnetsarelocal
, 0, "");
82 struct in_multihead in_multihead
; /* XXX BSS initialization */
84 extern struct inpcbinfo ripcbinfo
;
85 extern struct inpcbinfo udbinfo
;
88 * Return 1 if an internet address is for a ``local'' host
89 * (one to which we have a connection). If subnetsarelocal
90 * is true, this includes other subnets of the local net.
91 * Otherwise, it includes only the directly-connected (sub)nets.
94 in_localaddr(struct in_addr in
)
96 u_long i
= ntohl(in
.s_addr
);
97 struct in_ifaddr_container
*iac
;
100 if (subnetsarelocal
) {
101 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
104 if ((i
& ia
->ia_netmask
) == ia
->ia_net
)
108 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
111 if ((i
& ia
->ia_subnetmask
) == ia
->ia_subnet
)
119 * Determine whether an IP address is in a reserved set of addresses
120 * that may not be forwarded, or whether datagrams to that destination
124 in_canforward(struct in_addr in
)
126 u_long i
= ntohl(in
.s_addr
);
129 if (IN_EXPERIMENTAL(i
) || IN_MULTICAST(i
))
132 net
= i
& IN_CLASSA_NET
;
133 if (net
== 0 || net
== (IN_LOOPBACKNET
<< IN_CLASSA_NSHIFT
))
140 * Trim a mask in a sockaddr
143 in_socktrim(struct sockaddr_in
*ap
)
145 char *cplim
= (char *) &ap
->sin_addr
;
146 char *cp
= (char *) (&ap
->sin_addr
+ 1);
149 while (--cp
>= cplim
)
151 (ap
)->sin_len
= cp
- (char *) (ap
) + 1;
157 in_mask2len(struct in_addr
*mask
)
163 for (x
= 0; x
< sizeof *mask
; x
++) {
168 if (x
< sizeof *mask
) {
169 for (y
= 0; y
< 8; y
++) {
170 if ((p
[x
] & (0x80 >> y
)) == 0)
178 in_len2mask(struct in_addr
*mask
, int len
)
184 bzero(mask
, sizeof *mask
);
185 for (i
= 0; i
< len
/ 8; i
++)
188 p
[i
] = (0xff00 >> (len
% 8)) & 0xff;
191 static int in_interfaces
; /* number of external internet interfaces */
193 struct in_control_arg
{
201 in_control_dispatch(struct netmsg
*nmsg
)
203 struct lwkt_msg
*msg
= &nmsg
->nm_lmsg
;
204 const struct in_control_arg
*arg
= msg
->u
.ms_resultp
;
207 error
= in_control_internal(arg
->cmd
, arg
->data
, arg
->ifp
, arg
->td
);
208 lwkt_replymsg(msg
, error
);
212 * Generic internet control operations (ioctl's).
213 * Ifp is 0 if not an interface-specific ioctl.
215 * NOTE! td might be NULL.
219 in_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
223 struct in_control_arg arg
;
224 struct lwkt_msg
*msg
;
230 if (td
&& (error
= suser(td
)) != 0)
236 return in_lifaddr_ioctl(so
, cmd
, data
, ifp
, td
);
239 KASSERT(cmd
!= SIOCALIFADDR
&& cmd
!= SIOCDLIFADDR
,
240 ("recursive SIOC%cLIFADDR!\n",
241 cmd
== SIOCDLIFADDR
? 'D' : 'A'));
244 * IFADDR alterations are serialized by netisr0
253 bzero(&arg
, sizeof(arg
));
259 netmsg_init(&nmsg
, &curthread
->td_msgport
, 0,
260 in_control_dispatch
);
262 msg
->u
.ms_resultp
= &arg
;
264 lwkt_domsg(cpu_portfn(0), msg
, 0);
265 return msg
->ms_error
;
267 return in_control_internal(cmd
, data
, ifp
, td
);
272 in_ialink_dispatch(struct netmsg
*nmsg
)
274 struct lwkt_msg
*lmsg
= &nmsg
->nm_lmsg
;
275 struct in_ifaddr
*ia
= lmsg
->u
.ms_resultp
;
276 struct ifaddr_container
*ifac
;
277 struct in_ifaddr_container
*iac
;
282 ifac
= &ia
->ia_ifa
.ifa_containers
[cpu
];
283 ASSERT_IFAC_VALID(ifac
);
284 KASSERT((ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHEAD
) == 0,
285 ("ia is on in_ifaddrheads\n"));
287 ifac
->ifa_listmask
|= IFA_LIST_IN_IFADDRHEAD
;
288 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
289 TAILQ_INSERT_TAIL(&in_ifaddrheads
[cpu
], iac
, ia_link
);
293 ifa_forwardmsg(lmsg
, cpu
+ 1);
297 in_iaunlink_dispatch(struct netmsg
*nmsg
)
299 struct lwkt_msg
*lmsg
= &nmsg
->nm_lmsg
;
300 struct in_ifaddr
*ia
= lmsg
->u
.ms_resultp
;
301 struct ifaddr_container
*ifac
;
302 struct in_ifaddr_container
*iac
;
307 ifac
= &ia
->ia_ifa
.ifa_containers
[cpu
];
308 ASSERT_IFAC_VALID(ifac
);
309 KASSERT(ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHEAD
,
310 ("ia is not on in_ifaddrheads\n"));
312 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
313 TAILQ_REMOVE(&in_ifaddrheads
[cpu
], iac
, ia_link
);
314 ifac
->ifa_listmask
&= ~IFA_LIST_IN_IFADDRHEAD
;
318 ifa_forwardmsg(lmsg
, cpu
+ 1);
322 in_ialink(struct in_ifaddr
*ia
)
325 struct lwkt_msg
*lmsg
;
327 netmsg_init(&nmsg
, &curthread
->td_msgport
, 0, in_ialink_dispatch
);
328 lmsg
= &nmsg
.nm_lmsg
;
329 lmsg
->u
.ms_resultp
= ia
;
335 in_iaunlink(struct in_ifaddr
*ia
)
338 struct lwkt_msg
*lmsg
;
340 netmsg_init(&nmsg
, &curthread
->td_msgport
, 0, in_iaunlink_dispatch
);
341 lmsg
= &nmsg
.nm_lmsg
;
342 lmsg
->u
.ms_resultp
= ia
;
347 static __inline
struct in_ifaddr
*
348 in_ianext(struct in_ifaddr
*oia
)
350 struct ifaddr_container
*ifac
;
351 struct in_ifaddr_container
*iac
;
353 ifac
= &oia
->ia_ifa
.ifa_containers
[mycpuid
];
354 ASSERT_IFAC_VALID(ifac
);
355 KASSERT(ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHEAD
,
356 ("ia is not on in_ifaddrheads\n"));
358 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
359 iac
= TAILQ_NEXT(iac
, ia_link
);
367 in_control_internal(u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
370 struct ifreq
*ifr
= (struct ifreq
*)data
;
371 struct in_ifaddr
*ia
= 0, *iap
;
373 struct in_aliasreq
*ifra
= (struct in_aliasreq
*)data
;
374 struct ifaddr_container
*ifac
;
375 struct sockaddr_in oldaddr
;
376 int hostIsNew
, iaIsNew
, maskIsNew
;
382 * Find address for this interface, if it exists.
384 * If an alias address was specified, find that one instead of
385 * the first one on the interface, if possible
388 dst
= ((struct sockaddr_in
*)&ifr
->ifr_addr
)->sin_addr
;
389 LIST_FOREACH(iap
, INADDR_HASH(dst
.s_addr
), ia_hash
)
390 if (iap
->ia_ifp
== ifp
&&
391 iap
->ia_addr
.sin_addr
.s_addr
== dst
.s_addr
) {
396 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
],
398 iap
= ifatoia(ifac
->ifa
);
399 if (iap
->ia_addr
.sin_family
== AF_INET
) {
411 return (EADDRNOTAVAIL
);
412 if (ifra
->ifra_addr
.sin_family
== AF_INET
) {
414 if (ia
->ia_ifp
== ifp
&&
415 ia
->ia_addr
.sin_addr
.s_addr
==
416 ifra
->ifra_addr
.sin_addr
.s_addr
)
420 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
421 cmd
== SIOCAIFADDR
&&
422 ifra
->ifra_dstaddr
.sin_addr
.s_addr
== INADDR_ANY
) {
426 if (cmd
== SIOCDIFADDR
&& ia
== NULL
)
427 return (EADDRNOTAVAIL
);
432 if (td
&& (error
= suser(td
)) != 0)
436 return (EADDRNOTAVAIL
);
438 if (cmd
== SIOCSIFDSTADDR
&&
439 (ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
446 ia
= ifa_create(sizeof(*ia
), M_WAITOK
);
450 * Setup per-CPU information
452 for (i
= 0; i
< ncpus
; ++i
) {
453 struct in_ifaddr_container
*iac
;
455 ifac
= &ifa
->ifa_containers
[i
];
456 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
462 * Protect from NETISR_IP traversing address list
463 * while we're modifying it.
468 ifa_iflink(ifa
, ifp
, 1);
470 ifa
->ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
471 ifa
->ifa_dstaddr
= (struct sockaddr
*)&ia
->ia_dstaddr
;
472 ifa
->ifa_netmask
= (struct sockaddr
*)&ia
->ia_sockmask
;
473 ia
->ia_sockmask
.sin_len
= 8;
474 ia
->ia_sockmask
.sin_family
= AF_INET
;
475 if (ifp
->if_flags
& IFF_BROADCAST
) {
476 ia
->ia_broadaddr
.sin_len
= sizeof ia
->ia_addr
;
477 ia
->ia_broadaddr
.sin_family
= AF_INET
;
480 if (!(ifp
->if_flags
& IFF_LOOPBACK
))
489 if (td
&& (error
= suser(td
)) != 0)
498 return (EADDRNOTAVAIL
);
504 *((struct sockaddr_in
*)&ifr
->ifr_addr
) = ia
->ia_addr
;
508 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
510 *((struct sockaddr_in
*)&ifr
->ifr_dstaddr
) = ia
->ia_broadaddr
;
514 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
516 *((struct sockaddr_in
*)&ifr
->ifr_dstaddr
) = ia
->ia_dstaddr
;
520 *((struct sockaddr_in
*)&ifr
->ifr_addr
) = ia
->ia_sockmask
;
524 KKASSERT(ifp
->if_flags
& IFF_POINTOPOINT
);
526 oldaddr
= ia
->ia_dstaddr
;
527 ia
->ia_dstaddr
= *(struct sockaddr_in
*)&ifr
->ifr_dstaddr
;
528 if (ifp
->if_ioctl
!= NULL
) {
529 lwkt_serialize_enter(ifp
->if_serializer
);
530 error
= ifp
->if_ioctl(ifp
, SIOCSIFDSTADDR
, (caddr_t
)ia
,
531 td
->td_proc
->p_ucred
);
532 lwkt_serialize_exit(ifp
->if_serializer
);
534 ia
->ia_dstaddr
= oldaddr
;
538 if (ia
->ia_flags
& IFA_ROUTE
) {
539 ia
->ia_ifa
.ifa_dstaddr
= (struct sockaddr
*)&oldaddr
;
540 rtinit(&ia
->ia_ifa
, RTM_DELETE
, RTF_HOST
);
541 ia
->ia_ifa
.ifa_dstaddr
=
542 (struct sockaddr
*)&ia
->ia_dstaddr
;
543 rtinit(&ia
->ia_ifa
, RTM_ADD
, RTF_HOST
| RTF_UP
);
548 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
550 ia
->ia_broadaddr
= *(struct sockaddr_in
*)&ifr
->ifr_broadaddr
;
554 error
= in_ifinit(ifp
, ia
,
555 (const struct sockaddr_in
*)&ifr
->ifr_addr
, 1);
556 if (error
!= 0 && iaIsNew
)
559 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
);
563 ia
->ia_sockmask
.sin_addr
= ifra
->ifra_addr
.sin_addr
;
564 ia
->ia_subnetmask
= ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
);
571 if (ia
->ia_addr
.sin_family
== AF_INET
) {
572 if (ifra
->ifra_addr
.sin_len
== 0) {
573 ifra
->ifra_addr
= ia
->ia_addr
;
575 } else if (ifra
->ifra_addr
.sin_addr
.s_addr
==
576 ia
->ia_addr
.sin_addr
.s_addr
) {
580 if (ifra
->ifra_mask
.sin_len
) {
582 ia
->ia_sockmask
= ifra
->ifra_mask
;
583 ia
->ia_sockmask
.sin_family
= AF_INET
;
585 ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
);
588 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
589 ifra
->ifra_dstaddr
.sin_family
== AF_INET
) {
591 ia
->ia_dstaddr
= ifra
->ifra_dstaddr
;
592 maskIsNew
= 1; /* We lie; but the effect's the same */
594 if (ifra
->ifra_addr
.sin_family
== AF_INET
&&
595 (hostIsNew
|| maskIsNew
))
596 error
= in_ifinit(ifp
, ia
, &ifra
->ifra_addr
, 0);
598 if (error
!= 0 && iaIsNew
)
601 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
602 ifra
->ifra_broadaddr
.sin_family
== AF_INET
)
603 ia
->ia_broadaddr
= ifra
->ifra_broadaddr
;
605 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
);
610 * in_ifscrub kills the interface route.
614 * in_ifadown gets rid of all the rest of
615 * the routes. This is not quite the right
616 * thing to do, but at least if we are running
617 * a routing process they will come back.
619 in_ifadown(&ia
->ia_ifa
, 1);
620 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
);
625 if (ifp
== NULL
|| ifp
->if_ioctl
== NULL
)
627 lwkt_serialize_enter(ifp
->if_serializer
);
628 error
= ifp
->if_ioctl(ifp
, cmd
, data
, td
->td_proc
->p_ucred
);
629 lwkt_serialize_exit(ifp
->if_serializer
);
633 ifa_ifunlink(&ia
->ia_ifa
, ifp
);
637 * Protect from NETISR_IP traversing address list while we're modifying
640 crit_enter(); /* XXX MP */
641 if (cmd
== SIOCDIFADDR
&& ia
->ia_addr
.sin_family
== AF_INET
) {
642 /* XXX Assume that 'ia' is in hash table */
643 LIST_REMOVE(ia
, ia_hash
);
645 crit_exit(); /* XXX MP */
647 ifa_destroy(&ia
->ia_ifa
);
654 * SIOCGLIFADDR: get first address. (?!?)
655 * SIOCGLIFADDR with IFLR_PREFIX:
656 * get first address that matches the specified prefix.
657 * SIOCALIFADDR: add the specified address.
658 * SIOCALIFADDR with IFLR_PREFIX:
659 * EINVAL since we can't deduce hostid part of the address.
660 * SIOCDLIFADDR: delete the specified address.
661 * SIOCDLIFADDR with IFLR_PREFIX:
662 * delete the first address that matches the specified prefix.
664 * EINVAL on invalid parameters
665 * EADDRNOTAVAIL on prefix match failed/specified address not found
666 * other values may be returned from in_ioctl()
668 * NOTE! td might be NULL.
671 in_lifaddr_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
674 struct if_laddrreq
*iflr
= (struct if_laddrreq
*)data
;
678 panic("invalid argument to in_lifaddr_ioctl");
684 /* address must be specified on GET with IFLR_PREFIX */
685 if ((iflr
->flags
& IFLR_PREFIX
) == 0)
690 /* address must be specified on ADD and DELETE */
691 if (iflr
->addr
.ss_family
!= AF_INET
)
693 if (iflr
->addr
.ss_len
!= sizeof(struct sockaddr_in
))
695 /* XXX need improvement */
696 if (iflr
->dstaddr
.ss_family
697 && iflr
->dstaddr
.ss_family
!= AF_INET
)
699 if (iflr
->dstaddr
.ss_family
700 && iflr
->dstaddr
.ss_len
!= sizeof(struct sockaddr_in
))
703 default: /*shouldn't happen*/
706 if (sizeof(struct in_addr
) * 8 < iflr
->prefixlen
)
712 struct in_aliasreq ifra
;
714 if (iflr
->flags
& IFLR_PREFIX
)
717 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
718 bzero(&ifra
, sizeof ifra
);
719 bcopy(iflr
->iflr_name
, ifra
.ifra_name
, sizeof ifra
.ifra_name
);
721 bcopy(&iflr
->addr
, &ifra
.ifra_addr
, iflr
->addr
.ss_len
);
723 if (iflr
->dstaddr
.ss_family
) { /*XXX*/
724 bcopy(&iflr
->dstaddr
, &ifra
.ifra_dstaddr
,
725 iflr
->dstaddr
.ss_len
);
728 ifra
.ifra_mask
.sin_family
= AF_INET
;
729 ifra
.ifra_mask
.sin_len
= sizeof(struct sockaddr_in
);
730 in_len2mask(&ifra
.ifra_mask
.sin_addr
, iflr
->prefixlen
);
732 return in_control(so
, SIOCAIFADDR
, (caddr_t
)&ifra
, ifp
, td
);
737 struct ifaddr_container
*ifac
;
738 struct in_ifaddr
*ia
;
739 struct in_addr mask
, candidate
, match
;
740 struct sockaddr_in
*sin
;
743 bzero(&mask
, sizeof mask
);
744 if (iflr
->flags
& IFLR_PREFIX
) {
745 /* lookup a prefix rather than address. */
746 in_len2mask(&mask
, iflr
->prefixlen
);
748 sin
= (struct sockaddr_in
*)&iflr
->addr
;
749 match
.s_addr
= sin
->sin_addr
.s_addr
;
750 match
.s_addr
&= mask
.s_addr
;
752 /* if you set extra bits, that's wrong */
753 if (match
.s_addr
!= sin
->sin_addr
.s_addr
)
758 if (cmd
== SIOCGLIFADDR
) {
759 /* on getting an address, take the 1st match */
760 match
.s_addr
= 0; /* gcc4 warning */
763 /* on deleting an address, do exact match */
764 in_len2mask(&mask
, 32);
765 sin
= (struct sockaddr_in
*)&iflr
->addr
;
766 match
.s_addr
= sin
->sin_addr
.s_addr
;
772 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
773 struct ifaddr
*ifa
= ifac
->ifa
;
775 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
780 ((struct sockaddr_in
*)&ifa
->ifa_addr
)->sin_addr
.s_addr
;
781 candidate
.s_addr
&= mask
.s_addr
;
782 if (candidate
.s_addr
== match
.s_addr
)
786 return EADDRNOTAVAIL
;
787 ia
= (struct in_ifaddr
*)(ifac
->ifa
);
789 if (cmd
== SIOCGLIFADDR
) {
790 /* fill in the if_laddrreq structure */
791 bcopy(&ia
->ia_addr
, &iflr
->addr
, ia
->ia_addr
.sin_len
);
793 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
794 bcopy(&ia
->ia_dstaddr
, &iflr
->dstaddr
,
795 ia
->ia_dstaddr
.sin_len
);
797 bzero(&iflr
->dstaddr
, sizeof iflr
->dstaddr
);
800 in_mask2len(&ia
->ia_sockmask
.sin_addr
);
802 iflr
->flags
= 0; /*XXX*/
806 struct in_aliasreq ifra
;
808 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
809 bzero(&ifra
, sizeof ifra
);
810 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
811 sizeof ifra
.ifra_name
);
813 bcopy(&ia
->ia_addr
, &ifra
.ifra_addr
,
814 ia
->ia_addr
.sin_len
);
815 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
816 bcopy(&ia
->ia_dstaddr
, &ifra
.ifra_dstaddr
,
817 ia
->ia_dstaddr
.sin_len
);
819 bcopy(&ia
->ia_sockmask
, &ifra
.ifra_dstaddr
,
820 ia
->ia_sockmask
.sin_len
);
822 return in_control(so
, SIOCDIFADDR
, (caddr_t
)&ifra
,
828 return EOPNOTSUPP
; /*just for safety*/
832 * Delete any existing route for an interface.
835 in_ifscrub(struct ifnet
*ifp
, struct in_ifaddr
*ia
)
838 if ((ia
->ia_flags
& IFA_ROUTE
) == 0)
840 if (ifp
->if_flags
& (IFF_LOOPBACK
|IFF_POINTOPOINT
))
841 rtinit(&ia
->ia_ifa
, RTM_DELETE
, RTF_HOST
);
843 rtinit(&ia
->ia_ifa
, RTM_DELETE
, 0);
844 ia
->ia_flags
&= ~IFA_ROUTE
;
848 * Initialize an interface's internet address
849 * and routing table entry.
852 in_ifinit(struct ifnet
*ifp
, struct in_ifaddr
*ia
,
853 const struct sockaddr_in
*sin
, int scrub
)
855 u_long i
= ntohl(sin
->sin_addr
.s_addr
);
856 struct sockaddr_in oldaddr
;
857 int flags
= RTF_UP
, error
= 0;
858 int old_hash
= 0, new_hash
= 0;
861 oldaddr
= ia
->ia_addr
;
862 if (oldaddr
.sin_family
== AF_INET
) {
864 LIST_REMOVE(ia
, ia_hash
);
868 if (ia
->ia_addr
.sin_family
== AF_INET
) {
870 LIST_INSERT_HEAD(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
876 * Give the interface a chance to initialize
877 * if this is its first address,
878 * and to validate the address if necessary.
880 if (ifp
->if_ioctl
!= NULL
) {
881 lwkt_serialize_enter(ifp
->if_serializer
);
882 error
= ifp
->if_ioctl(ifp
, SIOCSIFADDR
, (caddr_t
)ia
, NULL
);
883 lwkt_serialize_exit(ifp
->if_serializer
);
889 * Delete old route, if requested.
892 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&oldaddr
;
894 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
898 * Calculate netmask/subnetmask.
901 ia
->ia_netmask
= IN_CLASSA_NET
;
902 else if (IN_CLASSB(i
))
903 ia
->ia_netmask
= IN_CLASSB_NET
;
905 ia
->ia_netmask
= IN_CLASSC_NET
;
907 * The subnet mask usually includes at least the standard network part,
908 * but may may be smaller in the case of supernetting.
909 * If it is set, we believe it.
911 if (ia
->ia_subnetmask
== 0) {
912 ia
->ia_subnetmask
= ia
->ia_netmask
;
913 ia
->ia_sockmask
.sin_addr
.s_addr
= htonl(ia
->ia_subnetmask
);
915 ia
->ia_netmask
&= ia
->ia_subnetmask
;
917 ia
->ia_net
= i
& ia
->ia_netmask
;
918 ia
->ia_subnet
= i
& ia
->ia_subnetmask
;
919 in_socktrim(&ia
->ia_sockmask
);
922 * Add route for the network.
924 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
925 if (ifp
->if_flags
& IFF_BROADCAST
) {
926 ia
->ia_broadaddr
.sin_addr
.s_addr
=
927 htonl(ia
->ia_subnet
| ~ia
->ia_subnetmask
);
928 ia
->ia_netbroadcast
.s_addr
=
929 htonl(ia
->ia_net
| ~ ia
->ia_netmask
);
930 } else if (ifp
->if_flags
& IFF_LOOPBACK
) {
931 ia
->ia_ifa
.ifa_dstaddr
= ia
->ia_ifa
.ifa_addr
;
933 } else if (ifp
->if_flags
& IFF_POINTOPOINT
) {
934 if (ia
->ia_dstaddr
.sin_family
!= AF_INET
)
940 * Don't add host routes for interface addresses of
941 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
942 * possible to assign several such address pairs with consistent
943 * results (no host route) and is required by BOOTP.
945 * XXX: This is ugly ! There should be a way for the caller to
946 * say that they don't want a host route.
948 if (ia
->ia_addr
.sin_addr
.s_addr
!= INADDR_ANY
||
949 ia
->ia_netmask
!= IN_CLASSA_NET
||
950 ia
->ia_dstaddr
.sin_addr
.s_addr
!= htonl(IN_CLASSA_HOST
)) {
951 if ((error
= rtinit(&ia
->ia_ifa
, RTM_ADD
, flags
)) != 0)
953 ia
->ia_flags
|= IFA_ROUTE
;
957 * If the interface supports multicast, join the "all hosts"
958 * multicast group on that interface.
960 if (ifp
->if_flags
& IFF_MULTICAST
) {
963 addr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
964 in_addmulti(&addr
, ifp
);
970 LIST_REMOVE(ia
, ia_hash
);
972 ia
->ia_addr
= oldaddr
;
974 LIST_INSERT_HEAD(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
983 * Return 1 if the address might be a local broadcast address.
986 in_broadcast(struct in_addr in
, struct ifnet
*ifp
)
988 struct ifaddr_container
*ifac
;
991 if (in
.s_addr
== INADDR_BROADCAST
||
992 in
.s_addr
== INADDR_ANY
)
994 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
996 t
= ntohl(in
.s_addr
);
998 * Look through the list of addresses for a match
999 * with a broadcast address.
1001 #define ia ((struct in_ifaddr *)ifa)
1002 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1003 struct ifaddr
*ifa
= ifac
->ifa
;
1005 if (ifa
->ifa_addr
->sa_family
== AF_INET
&&
1006 (in
.s_addr
== ia
->ia_broadaddr
.sin_addr
.s_addr
||
1007 in
.s_addr
== ia
->ia_netbroadcast
.s_addr
||
1009 * Check for old-style (host 0) broadcast.
1011 t
== ia
->ia_subnet
|| t
== ia
->ia_net
) &&
1013 * Check for an all one subnetmask. These
1014 * only exist when an interface gets a secondary
1017 ia
->ia_subnetmask
!= (u_long
)0xffffffff)
1024 * Add an address to the list of IP multicast addresses for a given interface.
1027 in_addmulti(struct in_addr
*ap
, struct ifnet
*ifp
)
1029 struct in_multi
*inm
;
1031 struct sockaddr_in sin
;
1032 struct ifmultiaddr
*ifma
;
1035 * Call generic routine to add membership or increment
1036 * refcount. It wants addresses in the form of a sockaddr,
1037 * so we build one here (being careful to zero the unused bytes).
1039 bzero(&sin
, sizeof sin
);
1040 sin
.sin_family
= AF_INET
;
1041 sin
.sin_len
= sizeof sin
;
1044 error
= if_addmulti(ifp
, (struct sockaddr
*)&sin
, &ifma
);
1051 * If ifma->ifma_protospec is null, then if_addmulti() created
1052 * a new record. Otherwise, we are done.
1054 if (ifma
->ifma_protospec
!= 0) {
1056 return ifma
->ifma_protospec
;
1059 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1060 at interrupt time? If so, need to fix if_addmulti. XXX */
1061 inm
= kmalloc(sizeof *inm
, M_IPMADDR
, M_WAITOK
| M_ZERO
);
1062 inm
->inm_addr
= *ap
;
1064 inm
->inm_ifma
= ifma
;
1065 ifma
->ifma_protospec
= inm
;
1066 LIST_INSERT_HEAD(&in_multihead
, inm
, inm_link
);
1069 * Let IGMP know that we have joined a new IP multicast group.
1071 igmp_joingroup(inm
);
1077 * Delete a multicast address record.
1080 in_delmulti(struct in_multi
*inm
)
1082 struct ifmultiaddr
*ifma
;
1083 struct in_multi my_inm
;
1086 ifma
= inm
->inm_ifma
;
1087 my_inm
.inm_ifp
= NULL
; /* don't send the leave msg */
1088 if (ifma
->ifma_refcount
== 1) {
1090 * No remaining claims to this record; let IGMP know that
1091 * we are leaving the multicast group.
1092 * But do it after the if_delmulti() which might reset
1093 * the interface and nuke the packet.
1096 ifma
->ifma_protospec
= 0;
1097 LIST_REMOVE(inm
, inm_link
);
1098 kfree(inm
, M_IPMADDR
);
1100 /* XXX - should be separate API for when we have an ifma? */
1101 if_delmulti(ifma
->ifma_ifp
, ifma
->ifma_addr
);
1102 if (my_inm
.inm_ifp
!= NULL
)
1103 igmp_leavegroup(&my_inm
);
1108 in_ifdetach(struct ifnet
*ifp
)
1110 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo
.pcblisthead
), ifp
);
1111 in_pcbpurgeif0(LIST_FIRST(&udbinfo
.pcblisthead
), ifp
);