2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1982, 1986, 1991, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * @(#)in.c 8.4 (Berkeley) 1/9/95
58 * $FreeBSD: src/sys/netinet/in.c,v 1.44.2.5 2001/08/13 16:26:17 ume Exp $
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/sockio.h>
64 #include <sys/socketvar.h>
65 #include <sys/malloc.h>
67 #include <sys/socket.h>
68 #include <sys/kernel.h>
69 #include <sys/sysctl.h>
70 #include <sys/kern_event.h>
73 #include <net/if_types.h>
74 #include <net/route.h>
76 #include <netinet/in.h>
77 #include <netinet/in_var.h>
78 #include <netinet/in_pcb.h>
80 #include <netinet/igmp_var.h>
83 #include <netinet/ip_var.h>
85 #include <netinet/tcp.h>
86 #include <netinet/tcp_timer.h>
87 #include <netinet/tcp_var.h>
92 static int in_mask2len
__P((struct in_addr
*));
93 static void in_len2mask
__P((struct in_addr
*, int));
94 static int in_lifaddr_ioctl
__P((struct socket
*, u_long
, caddr_t
,
95 struct ifnet
*, struct proc
*));
97 static void in_socktrim
__P((struct sockaddr_in
*));
98 static int in_ifinit
__P((struct ifnet
*,
99 struct in_ifaddr
*, struct sockaddr_in
*, int));
101 static int subnetsarelocal
= 0;
102 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, subnets_are_local
, CTLFLAG_RW
,
103 &subnetsarelocal
, 0, "");
105 struct in_multihead in_multihead
; /* XXX BSS initialization */
107 extern void arp_rtrequest();
110 * Return 1 if an internet address is for a ``local'' host
111 * (one to which we have a connection). If subnetsarelocal
112 * is true, this includes other subnets of the local net.
113 * Otherwise, it includes only the directly-connected (sub)nets.
119 register u_long i
= ntohl(in
.s_addr
);
120 register struct in_ifaddr
*ia
;
122 if (subnetsarelocal
) {
123 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
124 ia
= ia
->ia_link
.tqe_next
)
125 if ((i
& ia
->ia_netmask
) == ia
->ia_net
)
128 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
129 ia
= ia
->ia_link
.tqe_next
)
130 if ((i
& ia
->ia_subnetmask
) == ia
->ia_subnet
)
137 * Determine whether an IP address is in a reserved set of addresses
138 * that may not be forwarded, or whether datagrams to that destination
145 register u_long i
= ntohl(in
.s_addr
);
148 if (IN_EXPERIMENTAL(i
) || IN_MULTICAST(i
))
151 net
= i
& IN_CLASSA_NET
;
152 if (net
== 0 || net
== (IN_LOOPBACKNET
<< IN_CLASSA_NSHIFT
))
159 * Trim a mask in a sockaddr
163 struct sockaddr_in
*ap
;
165 register char *cplim
= (char *) &ap
->sin_addr
;
166 register char *cp
= (char *) (&ap
->sin_addr
+ 1);
169 while (--cp
>= cplim
)
171 (ap
)->sin_len
= cp
- (char *) (ap
) + 1;
178 struct in_addr
*mask
;
184 for (x
= 0; x
< sizeof(*mask
); x
++) {
189 if (x
< sizeof(*mask
)) {
190 for (y
= 0; y
< 8; y
++) {
191 if ((p
[x
] & (0x80 >> y
)) == 0)
199 in_len2mask(mask
, len
)
200 struct in_addr
*mask
;
207 bzero(mask
, sizeof(*mask
));
208 for (i
= 0; i
< len
/ 8; i
++)
211 p
[i
] = (0xff00 >> (len
% 8)) & 0xff;
214 static int in_interfaces
; /* number of external internet interfaces */
217 * Generic internet control operations (ioctl's).
218 * Ifp is 0 if not an interface-specific ioctl.
222 in_control(so
, cmd
, data
, ifp
, p
)
226 register struct ifnet
*ifp
;
229 register struct ifreq
*ifr
= (struct ifreq
*)data
;
230 register struct in_ifaddr
*ia
= 0, *iap
;
231 register struct ifaddr
*ifa
;
232 struct in_ifaddr
*oia
;
233 struct in_aliasreq
*ifra
= (struct in_aliasreq
*)data
;
234 struct sockaddr_in oldaddr
;
235 int error
, hostIsNew
, maskIsNew
, s
;
237 struct kev_msg ev_msg
;
238 struct kev_in_data in_event_data
;
244 if (p
&& (error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
250 return in_lifaddr_ioctl(so
, cmd
, data
, ifp
, p
);
254 * Find address for this interface, if it exists.
256 * If an alias address was specified, find that one instead of
257 * the first one on the interface.
260 for (iap
= in_ifaddrhead
.tqh_first
; iap
;
261 iap
= iap
->ia_link
.tqe_next
)
262 if (iap
->ia_ifp
== ifp
) {
263 if (((struct sockaddr_in
*)&ifr
->ifr_addr
)->sin_addr
.s_addr
==
264 iap
->ia_addr
.sin_addr
.s_addr
) {
267 } else if (ia
== NULL
) {
269 if (ifr
->ifr_addr
.sa_family
!= AF_INET
)
276 if (p
&& (error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
283 return (EADDRNOTAVAIL
);
284 if (ifra
->ifra_addr
.sin_family
== AF_INET
) {
285 for (oia
= ia
; ia
; ia
= ia
->ia_link
.tqe_next
) {
286 if (ia
->ia_ifp
== ifp
&&
287 ia
->ia_addr
.sin_addr
.s_addr
==
288 ifra
->ifra_addr
.sin_addr
.s_addr
)
291 if ((ifp
->if_flags
& IFF_POINTOPOINT
)
292 && (cmd
== SIOCAIFADDR
)
293 && (ifra
->ifra_dstaddr
.sin_addr
.s_addr
298 else if (cmd
== SIOCAIFADDR
)
300 if (cmd
== SIOCDIFADDR
&& ia
== 0)
301 return (EADDRNOTAVAIL
);
307 if ((so
->so_state
& SS_PRIV
) == 0)
310 if (p
&& (error
= suser(p
)) != 0)
315 return (EADDRNOTAVAIL
);
316 if (ifra
->ifra_addr
.sin_family
!= AF_INET
&& cmd
== SIOCSIFADDR
)
318 if (ia
== (struct in_ifaddr
*)0) {
319 ia
= (struct in_ifaddr
*)
320 _MALLOC(sizeof *ia
, M_IFADDR
, M_WAITOK
);
321 if (ia
== (struct in_ifaddr
*)NULL
)
323 bzero((caddr_t
)ia
, sizeof *ia
);
325 * Protect from ipintr() traversing address list
326 * while we're modifying it.
330 TAILQ_INSERT_TAIL(&in_ifaddrhead
, ia
, ia_link
);
332 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
335 * Temorary code for protocol attachment XXX
338 /* Generic protocol plumbing */
340 if (error
= dlil_plumb_protocol(PF_INET
, ifp
, &dl_tag
)) {
341 kprintf("in.c: warning can't plumb proto if=%s%n type %d error=%d\n",
342 ifp
->if_name
, ifp
->if_unit
, ifp
->if_type
, error
);
343 error
= 0; /*discard error, can be cold with unsupported interfaces */
345 /* End of temp code */
347 ifa
->ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
348 ifa
->ifa_dstaddr
= (struct sockaddr
*)&ia
->ia_dstaddr
;
349 ifa
->ifa_netmask
= (struct sockaddr
*)&ia
->ia_sockmask
;
350 ia
->ia_sockmask
.sin_len
= 8;
351 if (ifp
->if_flags
& IFF_BROADCAST
) {
352 ia
->ia_broadaddr
.sin_len
= sizeof(ia
->ia_addr
);
353 ia
->ia_broadaddr
.sin_family
= AF_INET
;
356 if (!(ifp
->if_flags
& IFF_LOOPBACK
))
362 case SIOCPROTOATTACH
:
363 case SIOCPROTODETACH
:
364 if (p
&& (error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
367 return (EADDRNOTAVAIL
);
372 if ((so
->so_state
& SS_PRIV
) == 0)
375 if (p
&& (error
= suser(p
)) != 0)
384 if (ia
== (struct in_ifaddr
*)0)
385 return (EADDRNOTAVAIL
);
391 return (EADDRNOTAVAIL
);
393 ifp
->if_eflags
|= IFEF_AUTOCONFIGURING
;
395 ifp
->if_eflags
&= ~IFEF_AUTOCONFIGURING
;
399 *((struct sockaddr_in
*)&ifr
->ifr_addr
) = ia
->ia_addr
;
403 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
405 *((struct sockaddr_in
*)&ifr
->ifr_dstaddr
) = ia
->ia_broadaddr
;
409 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
411 *((struct sockaddr_in
*)&ifr
->ifr_dstaddr
) = ia
->ia_dstaddr
;
415 *((struct sockaddr_in
*)&ifr
->ifr_addr
) = ia
->ia_sockmask
;
419 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
421 oldaddr
= ia
->ia_dstaddr
;
422 ia
->ia_dstaddr
= *(struct sockaddr_in
*)&ifr
->ifr_dstaddr
;
423 error
= dlil_ioctl(PF_INET
, ifp
, SIOCSIFDSTADDR
, (caddr_t
)ia
);
424 if (error
== EOPNOTSUPP
)
428 ia
->ia_dstaddr
= oldaddr
;
432 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
433 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
434 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
436 ev_msg
.event_code
= KEV_INET_SIFDSTADDR
;
438 if (ia
->ia_ifa
.ifa_dstaddr
)
439 in_event_data
.ia_dstaddr
=
440 ((struct sockaddr_in
*)ia
->ia_ifa
.ifa_dstaddr
)->sin_addr
;
442 in_event_data
.ia_dstaddr
.s_addr
= 0;
444 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
445 in_event_data
.ia_net
= ia
->ia_net
;
446 in_event_data
.ia_netmask
= ia
->ia_netmask
;
447 in_event_data
.ia_subnet
= ia
->ia_subnet
;
448 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
449 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
450 strncpy(&in_event_data
.link_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
451 in_event_data
.link_data
.if_family
= ifp
->if_family
;
452 in_event_data
.link_data
.if_unit
= (unsigned long) ifp
->if_unit
;
454 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
455 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
456 ev_msg
.dv
[1].data_length
= 0;
458 kev_post_msg(&ev_msg
);
461 if (ia
->ia_flags
& IFA_ROUTE
) {
462 ia
->ia_ifa
.ifa_dstaddr
= (struct sockaddr
*)&oldaddr
;
463 rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
);
464 ia
->ia_ifa
.ifa_dstaddr
=
465 (struct sockaddr
*)&ia
->ia_dstaddr
;
466 rtinit(&(ia
->ia_ifa
), (int)RTM_ADD
, RTF_HOST
|RTF_UP
);
471 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
473 ia
->ia_broadaddr
= *(struct sockaddr_in
*)&ifr
->ifr_broadaddr
;
475 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
476 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
477 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
479 ev_msg
.event_code
= KEV_INET_SIFBRDADDR
;
481 if (ia
->ia_ifa
.ifa_dstaddr
)
482 in_event_data
.ia_dstaddr
=
483 ((struct sockaddr_in
*)ia
->ia_ifa
.ifa_dstaddr
)->sin_addr
;
485 in_event_data
.ia_dstaddr
.s_addr
= 0;
487 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
488 in_event_data
.ia_net
= ia
->ia_net
;
489 in_event_data
.ia_netmask
= ia
->ia_netmask
;
490 in_event_data
.ia_subnet
= ia
->ia_subnet
;
491 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
492 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
493 strncpy(&in_event_data
.link_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
494 in_event_data
.link_data
.if_family
= ifp
->if_family
;
495 in_event_data
.link_data
.if_unit
= (unsigned long) ifp
->if_unit
;
497 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
498 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
499 ev_msg
.dv
[1].data_length
= 0;
501 kev_post_msg(&ev_msg
);
506 return (in_ifinit(ifp
, ia
,
507 (struct sockaddr_in
*) &ifr
->ifr_addr
, 1));
509 case SIOCPROTOATTACH
:
510 error
= dlil_plumb_protocol(PF_INET
, ifp
, &dl_tag
);
515 case SIOCPROTODETACH
:
516 // if an ip address is still present, refuse to detach
517 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
)
518 if (ifa
->ifa_addr
->sa_family
== AF_INET
)
521 error
= dlil_unplumb_protocol(PF_INET
, ifp
);
528 i
= ifra
->ifra_addr
.sin_addr
.s_addr
;
529 ia
->ia_subnetmask
= ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
= i
);
530 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
531 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
532 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
534 ev_msg
.event_code
= KEV_INET_SIFNETMASK
;
536 if (ia
->ia_ifa
.ifa_dstaddr
)
537 in_event_data
.ia_dstaddr
=
538 ((struct sockaddr_in
*)ia
->ia_ifa
.ifa_dstaddr
)->sin_addr
;
540 in_event_data
.ia_dstaddr
.s_addr
= 0;
542 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
543 in_event_data
.ia_net
= ia
->ia_net
;
544 in_event_data
.ia_netmask
= ia
->ia_netmask
;
545 in_event_data
.ia_subnet
= ia
->ia_subnet
;
546 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
547 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
548 strncpy(&in_event_data
.link_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
549 in_event_data
.link_data
.if_family
= ifp
->if_family
;
550 in_event_data
.link_data
.if_unit
= (unsigned long) ifp
->if_unit
;
552 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
553 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
554 ev_msg
.dv
[1].data_length
= 0;
556 kev_post_msg(&ev_msg
);
564 if (ia
->ia_addr
.sin_family
== AF_INET
) {
565 if (ifra
->ifra_addr
.sin_len
== 0) {
566 ifra
->ifra_addr
= ia
->ia_addr
;
568 } else if (ifra
->ifra_addr
.sin_addr
.s_addr
==
569 ia
->ia_addr
.sin_addr
.s_addr
)
572 if (ifra
->ifra_mask
.sin_len
) {
574 ia
->ia_sockmask
= ifra
->ifra_mask
;
576 ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
);
579 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
580 (ifra
->ifra_dstaddr
.sin_family
== AF_INET
)) {
582 ia
->ia_dstaddr
= ifra
->ifra_dstaddr
;
583 maskIsNew
= 1; /* We lie; but the effect's the same */
585 if (ifra
->ifra_addr
.sin_family
== AF_INET
&&
586 (hostIsNew
|| maskIsNew
)) {
587 error
= in_ifinit(ifp
, ia
, &ifra
->ifra_addr
, 0);
589 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
590 (ifra
->ifra_broadaddr
.sin_family
== AF_INET
))
591 ia
->ia_broadaddr
= ifra
->ifra_broadaddr
;
597 if ((error
== 0) || (error
== EEXIST
)) {
598 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
599 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
600 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
603 ev_msg
.event_code
= KEV_INET_NEW_ADDR
;
605 ev_msg
.event_code
= KEV_INET_CHANGED_ADDR
;
607 if (ia
->ia_ifa
.ifa_dstaddr
)
608 in_event_data
.ia_dstaddr
=
609 ((struct sockaddr_in
*)ia
->ia_ifa
.ifa_dstaddr
)->sin_addr
;
611 in_event_data
.ia_dstaddr
.s_addr
= 0;
613 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
614 in_event_data
.ia_net
= ia
->ia_net
;
615 in_event_data
.ia_netmask
= ia
->ia_netmask
;
616 in_event_data
.ia_subnet
= ia
->ia_subnet
;
617 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
618 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
619 strncpy(&in_event_data
.link_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
620 in_event_data
.link_data
.if_family
= ifp
->if_family
;
621 in_event_data
.link_data
.if_unit
= (unsigned long) ifp
->if_unit
;
623 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
624 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
625 ev_msg
.dv
[1].data_length
= 0;
627 kev_post_msg(&ev_msg
);
633 error
= dlil_ioctl(PF_INET
, ifp
, SIOCDIFADDR
, (caddr_t
)ia
);
634 if (error
== EOPNOTSUPP
)
639 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
640 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
641 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
643 ev_msg
.event_code
= KEV_INET_ADDR_DELETED
;
645 if (ia
->ia_ifa
.ifa_dstaddr
)
646 in_event_data
.ia_dstaddr
=
647 ((struct sockaddr_in
*)ia
->ia_ifa
.ifa_dstaddr
)->sin_addr
;
649 in_event_data
.ia_dstaddr
.s_addr
= 0;
651 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
652 in_event_data
.ia_net
= ia
->ia_net
;
653 in_event_data
.ia_netmask
= ia
->ia_netmask
;
654 in_event_data
.ia_subnet
= ia
->ia_subnet
;
655 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
656 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
657 strncpy(&in_event_data
.link_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
658 in_event_data
.link_data
.if_family
= ifp
->if_family
;
659 in_event_data
.link_data
.if_unit
= (unsigned long) ifp
->if_unit
;
661 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
662 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
663 ev_msg
.dv
[1].data_length
= 0;
665 kev_post_msg(&ev_msg
);
668 * in_ifscrub kills the interface route.
673 * in_ifadown gets rid of all the rest of
674 * the routes. This is not quite the right
675 * thing to do, but at least if we are running
676 * a routing process they will come back.
678 in_ifadown(&ia
->ia_ifa
, 1);
680 * XXX horrible hack to detect that we are being called
683 if (!ifnet_addrs
[ifp
->if_index
- 1]) {
684 in_pcbpurgeif0(LIST_FIRST(ripcbinfo
.listhead
), ifp
);
685 in_pcbpurgeif0(LIST_FIRST(udbinfo
.listhead
), ifp
);
690 * Protect from ipintr() traversing address list
691 * while we're modifying it.
696 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
698 TAILQ_REMOVE(&in_ifaddrhead
, oia
, ia_link
);
699 ifafree(&oia
->ia_ifa
);
703 * If the interface supports multicast, and no address is left,
704 * remove the "all hosts" multicast group from that interface.
706 if (ifp
->if_flags
& IFF_MULTICAST
) {
708 struct in_multi
*inm
;
710 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
)
711 if (ifa
->ifa_addr
->sa_family
== AF_INET
)
715 addr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
716 IN_LOOKUP_MULTI(addr
, ifp
, inm
);
728 * Inspiration from tcp_ctloutput() and ip_ctloutput()
729 * Special ioctl for OpenTransport sockets
731 struct inpcb
*inp
, *cloned_inp
;
733 int cloned_fd
= *(int *)data
;
735 s
= splnet(); /* XXX */
742 /* let's make sure it's either -1 or a valid file descriptor */
743 if (cloned_fd
!= -1) {
744 struct socket
*cloned_so
;
745 struct file
*cloned_fp
;
746 error
= getsock(p
->p_fd
, cloned_fd
, &cloned_fp
);
751 cloned_so
= (struct socket
*)cloned_fp
->f_data
;
752 cloned_inp
= sotoinpcb(cloned_so
);
757 if (cloned_inp
== NULL
) {
758 /* OT always uses IP_PORTRANGE_HIGH */
759 inp
->inp_flags
&= ~(INP_LOWPORT
);
760 inp
->inp_flags
|= INP_HIGHPORT
;
761 /* For UDP, OT allows broadcast by default */
762 if (so
->so_type
== SOCK_DGRAM
)
763 so
->so_options
|= SO_BROADCAST
;
764 /* For TCP we want to see MSG_OOB when receive urgent data */
765 else if (so
->so_type
== SOCK_STREAM
)
766 so
->so_options
|= SO_WANTOOBFLAG
;
768 inp
->inp_ip_tos
= cloned_inp
->inp_ip_tos
;
769 inp
->inp_ip_ttl
= cloned_inp
->inp_ip_ttl
;
770 inp
->inp_flags
= cloned_inp
->inp_flags
;
772 /* Multicast options */
773 if (cloned_inp
->inp_moptions
!= NULL
) {
775 struct ip_moptions
*cloned_imo
= cloned_inp
->inp_moptions
;
776 struct ip_moptions
*imo
= inp
->inp_moptions
;
780 * No multicast option buffer attached to the pcb;
784 imo
= (struct ip_moptions
*)
785 _MALLOC(sizeof(*imo
), M_IPMOPTS
, M_WAITOK
);
790 s
= splnet(); /* XXX */
791 inp
->inp_moptions
= imo
;
793 imo
->imo_multicast_ifp
= cloned_imo
->imo_multicast_ifp
;
794 imo
->imo_multicast_vif
= cloned_imo
->imo_multicast_vif
;
795 imo
->imo_multicast_ttl
= cloned_imo
->imo_multicast_ttl
;
796 imo
->imo_multicast_loop
= cloned_imo
->imo_multicast_loop
;
797 imo
->imo_num_memberships
= cloned_imo
->imo_num_memberships
;
798 for (i
= 0; i
< cloned_imo
->imo_num_memberships
; i
++) {
799 imo
->imo_membership
[i
] =
800 in_addmulti(&cloned_imo
->imo_membership
[i
]->inm_addr
,
801 cloned_imo
->imo_membership
[i
]->inm_ifp
);
802 if (imo
->imo_membership
[i
] == NULL
) {
807 if (i
< cloned_imo
->imo_num_memberships
) {
808 /* Failed, perform cleanup */
809 for (i
--; i
>= 0; i
--)
810 in_delmulti(imo
->imo_membership
[i
]);
818 #endif /* __APPLE__ */
822 /* Darwin: dlil_ioctl called from ifioctl */
824 return ((*ifp
->if_ioctl
)(ifp
, cmd
, data
));
832 * SIOCGLIFADDR: get first address. (?!?)
833 * SIOCGLIFADDR with IFLR_PREFIX:
834 * get first address that matches the specified prefix.
835 * SIOCALIFADDR: add the specified address.
836 * SIOCALIFADDR with IFLR_PREFIX:
837 * EINVAL since we can't deduce hostid part of the address.
838 * SIOCDLIFADDR: delete the specified address.
839 * SIOCDLIFADDR with IFLR_PREFIX:
840 * delete the first address that matches the specified prefix.
842 * EINVAL on invalid parameters
843 * EADDRNOTAVAIL on prefix match failed/specified address not found
844 * other values may be returned from in_ioctl()
847 in_lifaddr_ioctl(so
, cmd
, data
, ifp
, p
)
854 struct if_laddrreq
*iflr
= (struct if_laddrreq
*)data
;
859 panic("invalid argument to in_lifaddr_ioctl");
865 /* address must be specified on GET with IFLR_PREFIX */
866 if ((iflr
->flags
& IFLR_PREFIX
) == 0)
871 /* address must be specified on ADD and DELETE */
872 if (iflr
->addr
.ss_family
!= AF_INET
)
874 if (iflr
->addr
.ss_len
!= sizeof(struct sockaddr_in
))
876 /* XXX need improvement */
877 if (iflr
->dstaddr
.ss_family
878 && iflr
->dstaddr
.ss_family
!= AF_INET
)
880 if (iflr
->dstaddr
.ss_family
881 && iflr
->dstaddr
.ss_len
!= sizeof(struct sockaddr_in
))
884 default: /*shouldn't happen*/
887 if (sizeof(struct in_addr
) * 8 < iflr
->prefixlen
)
893 struct in_aliasreq ifra
;
895 if (iflr
->flags
& IFLR_PREFIX
)
898 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
899 bzero(&ifra
, sizeof(ifra
));
900 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
901 sizeof(ifra
.ifra_name
));
903 bcopy(&iflr
->addr
, &ifra
.ifra_addr
, iflr
->addr
.ss_len
);
905 if (iflr
->dstaddr
.ss_family
) { /*XXX*/
906 bcopy(&iflr
->dstaddr
, &ifra
.ifra_dstaddr
,
907 iflr
->dstaddr
.ss_len
);
910 ifra
.ifra_mask
.sin_family
= AF_INET
;
911 ifra
.ifra_mask
.sin_len
= sizeof(struct sockaddr_in
);
912 in_len2mask(&ifra
.ifra_mask
.sin_addr
, iflr
->prefixlen
);
914 return in_control(so
, SIOCAIFADDR
, (caddr_t
)&ifra
, ifp
, p
);
919 struct in_ifaddr
*ia
;
920 struct in_addr mask
, candidate
, match
;
921 struct sockaddr_in
*sin
;
924 bzero(&mask
, sizeof(mask
));
925 if (iflr
->flags
& IFLR_PREFIX
) {
926 /* lookup a prefix rather than address. */
927 in_len2mask(&mask
, iflr
->prefixlen
);
929 sin
= (struct sockaddr_in
*)&iflr
->addr
;
930 match
.s_addr
= sin
->sin_addr
.s_addr
;
931 match
.s_addr
&= mask
.s_addr
;
933 /* if you set extra bits, that's wrong */
934 if (match
.s_addr
!= sin
->sin_addr
.s_addr
)
939 if (cmd
== SIOCGLIFADDR
) {
940 /* on getting an address, take the 1st match */
943 /* on deleting an address, do exact match */
944 in_len2mask(&mask
, 32);
945 sin
= (struct sockaddr_in
*)&iflr
->addr
;
946 match
.s_addr
= sin
->sin_addr
.s_addr
;
952 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
953 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
957 candidate
.s_addr
= ((struct sockaddr_in
*)&ifa
->ifa_addr
)->sin_addr
.s_addr
;
958 candidate
.s_addr
&= mask
.s_addr
;
959 if (candidate
.s_addr
== match
.s_addr
)
963 return EADDRNOTAVAIL
;
964 ia
= (struct in_ifaddr
*)ifa
;
966 if (cmd
== SIOCGLIFADDR
) {
967 /* fill in the if_laddrreq structure */
968 bcopy(&ia
->ia_addr
, &iflr
->addr
, ia
->ia_addr
.sin_len
);
970 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
971 bcopy(&ia
->ia_dstaddr
, &iflr
->dstaddr
,
972 ia
->ia_dstaddr
.sin_len
);
974 bzero(&iflr
->dstaddr
, sizeof(iflr
->dstaddr
));
977 in_mask2len(&ia
->ia_sockmask
.sin_addr
);
979 iflr
->flags
= 0; /*XXX*/
983 struct in_aliasreq ifra
;
985 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
986 bzero(&ifra
, sizeof(ifra
));
987 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
988 sizeof(ifra
.ifra_name
));
990 bcopy(&ia
->ia_addr
, &ifra
.ifra_addr
,
991 ia
->ia_addr
.sin_len
);
992 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
993 bcopy(&ia
->ia_dstaddr
, &ifra
.ifra_dstaddr
,
994 ia
->ia_dstaddr
.sin_len
);
996 bcopy(&ia
->ia_sockmask
, &ifra
.ifra_dstaddr
,
997 ia
->ia_sockmask
.sin_len
);
999 return in_control(so
, SIOCDIFADDR
, (caddr_t
)&ifra
,
1005 return EOPNOTSUPP
; /*just for safety*/
1009 * Delete any existing route for an interface.
1013 register struct ifnet
*ifp
;
1014 register struct in_ifaddr
*ia
;
1017 if ((ia
->ia_flags
& IFA_ROUTE
) == 0)
1019 if (ifp
->if_flags
& (IFF_LOOPBACK
|IFF_POINTOPOINT
))
1020 rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
);
1022 rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, 0);
1023 ia
->ia_flags
&= ~IFA_ROUTE
;
1027 * Initialize an interface's internet address
1028 * and routing table entry.
1031 in_ifinit(ifp
, ia
, sin
, scrub
)
1032 register struct ifnet
*ifp
;
1033 register struct in_ifaddr
*ia
;
1034 struct sockaddr_in
*sin
;
1037 register u_long i
= ntohl(sin
->sin_addr
.s_addr
);
1038 struct sockaddr_in oldaddr
;
1039 int s
= splimp(), flags
= RTF_UP
, error
;
1041 oldaddr
= ia
->ia_addr
;
1045 * Give the interface a chance to initialize
1046 * if this is its first address,
1047 * and to validate the address if necessary.
1049 error
= dlil_ioctl(PF_INET
, ifp
, SIOCSIFADDR
, (caddr_t
)ia
);
1050 if (error
== EOPNOTSUPP
)
1054 ia
->ia_addr
= oldaddr
;
1059 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&oldaddr
;
1060 in_ifscrub(ifp
, ia
);
1061 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
1064 ia
->ia_netmask
= IN_CLASSA_NET
;
1065 else if (IN_CLASSB(i
))
1066 ia
->ia_netmask
= IN_CLASSB_NET
;
1068 ia
->ia_netmask
= IN_CLASSC_NET
;
1070 * The subnet mask usually includes at least the standard network part,
1071 * but may may be smaller in the case of supernetting.
1072 * If it is set, we believe it.
1074 if (ia
->ia_subnetmask
== 0) {
1075 ia
->ia_subnetmask
= ia
->ia_netmask
;
1076 ia
->ia_sockmask
.sin_addr
.s_addr
= htonl(ia
->ia_subnetmask
);
1078 ia
->ia_netmask
&= ia
->ia_subnetmask
;
1079 ia
->ia_net
= i
& ia
->ia_netmask
;
1080 ia
->ia_subnet
= i
& ia
->ia_subnetmask
;
1081 in_socktrim(&ia
->ia_sockmask
);
1083 * Add route for the network.
1085 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
1086 if (ifp
->if_flags
& IFF_BROADCAST
) {
1087 ia
->ia_broadaddr
.sin_addr
.s_addr
=
1088 htonl(ia
->ia_subnet
| ~ia
->ia_subnetmask
);
1089 ia
->ia_netbroadcast
.s_addr
=
1090 htonl(ia
->ia_net
| ~ ia
->ia_netmask
);
1091 } else if (ifp
->if_flags
& IFF_LOOPBACK
) {
1092 ia
->ia_ifa
.ifa_dstaddr
= ia
->ia_ifa
.ifa_addr
;
1094 } else if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1095 if (ia
->ia_dstaddr
.sin_family
!= AF_INET
)
1099 if ((error
= rtinit(&(ia
->ia_ifa
), (int)RTM_ADD
, flags
)) == 0)
1100 ia
->ia_flags
|= IFA_ROUTE
;
1101 /* XXX check if the subnet route points to the same interface */
1102 if (error
== EEXIST
)
1106 * If the interface supports multicast, join the "all hosts"
1107 * multicast group on that interface.
1109 if (ifp
->if_flags
& IFF_MULTICAST
) {
1110 struct in_multi
*inm
;
1111 struct in_addr addr
;
1113 addr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
1114 IN_LOOKUP_MULTI(addr
, ifp
, inm
);
1116 in_addmulti(&addr
, ifp
);
1123 * Return 1 if the address might be a local broadcast address.
1126 in_broadcast(in
, ifp
)
1130 register struct ifaddr
*ifa
;
1133 if (in
.s_addr
== INADDR_BROADCAST
||
1134 in
.s_addr
== INADDR_ANY
)
1136 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
1138 t
= ntohl(in
.s_addr
);
1140 * Look through the list of addresses for a match
1141 * with a broadcast address.
1143 #define ia ((struct in_ifaddr *)ifa)
1144 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1145 ifa
= ifa
->ifa_link
.tqe_next
) {
1146 if (ifa
->ifa_addr
== NULL
)
1148 if (ifa
->ifa_addr
->sa_family
== AF_INET
&&
1149 (in
.s_addr
== ia
->ia_broadaddr
.sin_addr
.s_addr
||
1150 in
.s_addr
== ia
->ia_netbroadcast
.s_addr
||
1152 * Check for old-style (host 0) broadcast.
1154 t
== ia
->ia_subnet
|| t
== ia
->ia_net
) &&
1156 * Check for an all one subnetmask. These
1157 * only exist when an interface gets a secondary
1160 ia
->ia_subnetmask
!= (u_long
)0xffffffff)
1167 * Add an address to the list of IP multicast addresses for a given interface.
1170 in_addmulti(ap
, ifp
)
1171 register struct in_addr
*ap
;
1172 register struct ifnet
*ifp
;
1174 register struct in_multi
*inm
;
1176 struct sockaddr_in sin
;
1177 struct ifmultiaddr
*ifma
;
1181 * Call generic routine to add membership or increment
1182 * refcount. It wants addresses in the form of a sockaddr,
1183 * so we build one here (being careful to zero the unused bytes).
1185 bzero(&sin
, sizeof sin
);
1186 sin
.sin_family
= AF_INET
;
1187 sin
.sin_len
= sizeof sin
;
1189 error
= if_addmulti(ifp
, (struct sockaddr
*)&sin
, &ifma
);
1196 * If ifma->ifma_protospec is null, then if_addmulti() created
1197 * a new record. Otherwise, we are done.
1199 if (ifma
->ifma_protospec
!= 0) {
1201 return ifma
->ifma_protospec
;
1204 inm
= (struct in_multi
*) _MALLOC(sizeof(*inm
), M_IPMADDR
, M_WAITOK
);
1210 bzero(inm
, sizeof *inm
);
1211 inm
->inm_addr
= *ap
;
1213 inm
->inm_ifma
= ifma
;
1214 ifma
->ifma_protospec
= inm
;
1215 LIST_INSERT_HEAD(&in_multihead
, inm
, inm_link
);
1218 * Let IGMP know that we have joined a new IP multicast group.
1220 error
= igmp_joingroup(inm
);
1222 if_delmultiaddr(ifma
);
1223 LIST_REMOVE(inm
, inm_link
);
1224 _FREE(inm
, M_IPMADDR
);
1232 * Delete a multicast address record.
1236 register struct in_multi
*inm
;
1238 struct ifmultiaddr
*ifma
= inm
->inm_ifma
;
1241 /* We intentionally do this a bit differently than BSD */
1243 if (ifma
&& ifma
->ifma_refcount
== 1) {
1245 * No remaining claims to this record; let IGMP know that
1246 * we are leaving the multicast group.
1248 igmp_leavegroup(inm
);
1249 ifma
->ifma_protospec
= 0;
1250 LIST_REMOVE(inm
, inm_link
);
1251 FREE(inm
, M_IPMADDR
);
1253 /* XXX - should be separate API for when we have an ifma? */
1255 if_delmultiaddr(ifma
);