2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1989, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
63 * support for mandatory and extensible security protections. This notice
64 * is included in support of clause 2.2 (b) of the Apple Public License,
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/malloc.h>
74 #include <sys/socket.h>
75 #include <sys/sockio.h>
76 #include <sys/sysctl.h>
77 #include <kern/lock.h>
80 #include <net/route.h>
81 #include <net/if_llc.h>
82 #include <net/if_dl.h>
83 #include <net/if_types.h>
84 #include <net/kpi_protocol.h>
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet/if_ether.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/in_arp.h>
93 #include <sys/socketvar.h>
98 #include <net/bridge.h>
101 /* #include "vlan.h" */
103 #include <net/if_vlan_var.h>
104 #endif /* NVLAN > 0 */
105 #include <net/ether_if_module.h>
107 #include <security/mac_framework.h>
110 /* Local function declerations */
111 extern void *kdp_get_interface(void);
112 extern void kdp_set_ip_and_mac_addresses(struct in_addr
*ipaddr
,
113 struct ether_addr
*macaddr
);
115 #if defined (__arm__)
116 static __inline__
void
117 _ip_copy(struct in_addr
* dst
, const struct in_addr
* src
)
119 memcpy(dst
, src
, sizeof(*dst
));
124 static __inline__
void
125 _ip_copy(struct in_addr
* dst
, const struct in_addr
* src
)
133 ether_inet_arp_input(
136 struct ether_arp
*ea
;
137 struct sockaddr_dl sender_hw
;
138 struct sockaddr_in sender_ip
;
139 struct sockaddr_in target_ip
;
141 if (mbuf_len(m
) < sizeof(*ea
) &&
142 mbuf_pullup(&m
, sizeof(*ea
)) != 0)
147 /* Verify this is an ethernet/ip arp and address lengths are correct */
148 if (ntohs(ea
->arp_hrd
) != ARPHRD_ETHER
||
149 ntohs(ea
->arp_pro
) != ETHERTYPE_IP
||
150 ea
->arp_pln
!= sizeof(struct in_addr
) ||
151 ea
->arp_hln
!= ETHER_ADDR_LEN
) {
156 /* Verify the sender is not broadcast */
157 if (bcmp(ea
->arp_sha
, etherbroadcastaddr
, ETHER_ADDR_LEN
) == 0) {
162 bzero(&sender_ip
, sizeof(sender_ip
));
163 sender_ip
.sin_len
= sizeof(sender_ip
);
164 sender_ip
.sin_family
= AF_INET
;
165 _ip_copy(&sender_ip
.sin_addr
, (const struct in_addr
*)ea
->arp_spa
);
166 target_ip
= sender_ip
;
167 _ip_copy(&target_ip
.sin_addr
, (const struct in_addr
*)ea
->arp_tpa
);
169 bzero(&sender_hw
, sizeof(sender_hw
));
170 sender_hw
.sdl_len
= sizeof(sender_hw
);
171 sender_hw
.sdl_family
= AF_LINK
;
172 sender_hw
.sdl_type
= IFT_ETHER
;
173 sender_hw
.sdl_alen
= ETHER_ADDR_LEN
;
174 bcopy(ea
->arp_sha
, LLADDR(&sender_hw
), ETHER_ADDR_LEN
);
176 arp_ip_handle_input(mbuf_pkthdr_rcvif(m
), ntohs(ea
->arp_op
), &sender_hw
, &sender_ip
, &target_ip
);
181 * Process a received Ethernet packet;
182 * the packet is in the mbuf chain m without
183 * the ether header, which is provided separately.
187 __unused ifnet_t ifp
,
188 __unused protocol_family_t protocol_family
,
192 mbuf_t
*tailptr
= &m_list
;
195 /* Strip ARP and non-IP packets out of the list */
196 for (m
= m_list
; m
; m
= nextpkt
) {
197 struct ether_header
*eh
= mbuf_pkthdr_header(m
);
199 nextpkt
= m
->m_nextpkt
;
201 if (eh
->ether_type
== htons(ETHERTYPE_IP
)) {
202 /* put this packet in the list */
204 tailptr
= &m
->m_nextpkt
;
207 /* Pass ARP packets to arp input */
209 if (eh
->ether_type
== htons(ETHERTYPE_ARP
))
210 ether_inet_arp_input(m
);
218 /* Pass IP list to ip input */
219 if (m_list
!= NULL
&& proto_input(PF_INET
, m_list
) != 0)
221 mbuf_freem_list(m_list
);
228 ether_inet_pre_output(
230 __unused protocol_family_t protocol_family
,
232 const struct sockaddr
*dst_netaddr
,
237 register struct mbuf
*m
= *m0
;
238 const struct ether_header
*eh
;
242 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
246 * Tell ether_frameout it's ok to loop packet unless negated below.
248 m
->m_flags
|= M_LOOP
;
250 switch (dst_netaddr
->sa_family
) {
253 struct sockaddr_dl ll_dest
;
254 result
= arp_lookup_ip(ifp
, (const struct sockaddr_in
*)dst_netaddr
,
255 &ll_dest
, sizeof(ll_dest
), (route_t
)route
, *m0
);
257 bcopy(LLADDR(&ll_dest
), edst
, ETHER_ADDR_LEN
);
258 *(u_int16_t
*)type
= htons(ETHERTYPE_IP
);
263 case pseudo_AF_HDRCMPLT
:
265 m
->m_flags
&= ~M_LOOP
;
266 eh
= (const struct ether_header
*)dst_netaddr
->sa_data
;
267 (void)memcpy(edst
, eh
->ether_dhost
, 6);
268 *(u_short
*)type
= eh
->ether_type
;
272 printf("%s%d: can't handle af%d\n", ifp
->if_name
, ifp
->if_unit
,
273 dst_netaddr
->sa_family
);
275 result
= EAFNOSUPPORT
;
282 ether_inet_resolve_multi(
284 const struct sockaddr
*proto_addr
,
285 struct sockaddr_dl
*out_ll
,
288 static const size_t minsize
= offsetof(struct sockaddr_dl
, sdl_data
[0]) + ETHER_ADDR_LEN
;
289 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)proto_addr
;
291 if (proto_addr
->sa_family
!= AF_INET
)
294 if (proto_addr
->sa_len
< sizeof(struct sockaddr_in
))
297 if (ll_len
< minsize
)
300 bzero(out_ll
, minsize
);
301 out_ll
->sdl_len
= minsize
;
302 out_ll
->sdl_family
= AF_LINK
;
303 out_ll
->sdl_index
= ifp
->if_index
;
304 out_ll
->sdl_type
= IFT_ETHER
;
305 out_ll
->sdl_nlen
= 0;
306 out_ll
->sdl_alen
= ETHER_ADDR_LEN
;
307 out_ll
->sdl_slen
= 0;
308 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, LLADDR(out_ll
));
314 ether_inet_prmod_ioctl(
316 __unused protocol_family_t protocol_family
,
321 struct ifreq
*ifr
= data
;
328 if ((ifnet_flags(ifp
) & IFF_RUNNING
) == 0) {
329 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
330 ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
333 switch (ifaddr_address_family(ifa
)) {
337 inet_arp_init_ifaddr(ifp
, ifa
);
339 * Register new IP and MAC addresses with the kernel
340 * debugger if the interface is the same as was registered
341 * by IOKernelDebugger. If no interface was registered,
342 * fall back and just match against en0 interface.
343 * Do this only for the first address of the interface
344 * and not for aliases.
346 if (command
== SIOCSIFADDR
&&
347 ((kdp_get_interface() != 0 &&
348 kdp_get_interface() == ifp
->if_softc
) ||
349 (kdp_get_interface() == 0 && ifp
->if_unit
== 0)))
350 kdp_set_ip_and_mac_addresses(&(IA_SIN(ifa
)->sin_addr
),
362 ifnet_lladdr_copy_bytes(ifp
, ifr
->ifr_addr
.sa_data
, ETHER_ADDR_LEN
);
376 __unused protocol_family_t protocol
,
377 const struct kev_msg
*event
)
381 if (event
->vendor_code
!= KEV_VENDOR_APPLE
||
382 event
->kev_class
!= KEV_NETWORK_CLASS
||
383 event
->kev_subclass
!= KEV_DL_SUBCLASS
||
384 event
->event_code
!= KEV_DL_LINK_ADDRESS_CHANGED
) {
388 if (ifnet_get_address_list_family(ifp
, &addresses
, AF_INET
) == 0) {
391 for (i
= 0; addresses
[i
] != NULL
; i
++) {
392 inet_arp_init_ifaddr(ifp
, addresses
[i
]);
395 ifnet_free_address_list(addresses
);
403 const struct sockaddr_dl
* sender_hw
,
404 const struct sockaddr
* sender_proto
,
405 const struct sockaddr_dl
* target_hw
,
406 const struct sockaddr
* target_proto
)
410 struct ether_header
*eh
;
411 struct ether_arp
*ea
;
412 const struct sockaddr_in
* sender_ip
= (const struct sockaddr_in
*)sender_proto
;
413 const struct sockaddr_in
* target_ip
= (const struct sockaddr_in
*)target_proto
;
416 if (target_ip
== NULL
)
419 if ((sender_ip
&& sender_ip
->sin_family
!= AF_INET
) ||
420 target_ip
->sin_family
!= AF_INET
)
423 result
= mbuf_gethdr(MBUF_DONTWAIT
, MBUF_TYPE_DATA
, &m
);
427 mbuf_setlen(m
, sizeof(*ea
));
428 mbuf_pkthdr_setlen(m
, sizeof(*ea
));
430 /* Move the data pointer in the mbuf to the end, aligned to 4 bytes */
431 datap
= mbuf_datastart(m
);
432 datap
+= mbuf_trailingspace(m
);
433 datap
-= (((u_long
)datap
) & 0x3);
434 mbuf_setdata(m
, datap
, sizeof(*ea
));
438 * Prepend the ethernet header, we will send the raw frame;
439 * callee frees the original mbuf when allocation fails.
441 result
= mbuf_prepend(&m
, sizeof(*eh
), MBUF_DONTWAIT
);
446 eh
->ether_type
= htons(ETHERTYPE_ARP
);
449 mac_mbuf_label_associate_linklayer(ifp
, m
);
452 /* Fill out the arp header */
453 ea
->arp_pro
= htons(ETHERTYPE_IP
);
454 ea
->arp_hln
= sizeof(ea
->arp_sha
);
455 ea
->arp_pln
= sizeof(ea
->arp_spa
);
456 ea
->arp_hrd
= htons(ARPHRD_ETHER
);
457 ea
->arp_op
= htons(arpop
);
459 /* Sender Hardware */
460 if (sender_hw
!= NULL
) {
461 bcopy(CONST_LLADDR(sender_hw
), ea
->arp_sha
, sizeof(ea
->arp_sha
));
464 ifnet_lladdr_copy_bytes(ifp
, ea
->arp_sha
, ETHER_ADDR_LEN
);
466 ifnet_lladdr_copy_bytes(ifp
, eh
->ether_shost
, sizeof(eh
->ether_shost
));
469 if (sender_ip
!= NULL
) {
470 bcopy(&sender_ip
->sin_addr
, ea
->arp_spa
, sizeof(ea
->arp_spa
));
475 /* Look for an IP address to use as our source */
476 ifnet_lock_shared(ifp
);
477 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
478 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET
)
482 bcopy(&((struct sockaddr_in
*)ifa
->ifa_addr
)->sin_addr
, ea
->arp_spa
,
483 sizeof(ea
->arp_spa
));
485 ifnet_lock_done(ifp
);
493 /* Target Hardware */
494 if (target_hw
== 0) {
495 bzero(ea
->arp_tha
, sizeof(ea
->arp_tha
));
496 bcopy(etherbroadcastaddr
, eh
->ether_dhost
, sizeof(eh
->ether_dhost
));
499 bcopy(CONST_LLADDR(target_hw
), ea
->arp_tha
, sizeof(ea
->arp_tha
));
500 bcopy(CONST_LLADDR(target_hw
), eh
->ether_dhost
, sizeof(eh
->ether_dhost
));
504 bcopy(&target_ip
->sin_addr
, ea
->arp_tpa
, sizeof(ea
->arp_tpa
));
506 ifnet_output_raw(ifp
, PF_INET
, m
);
514 __unused protocol_family_t proto_family
)
516 struct ifnet_attach_proto_param_v2 proto
;
517 struct ifnet_demux_desc demux
[2];
518 u_short en_native
=htons(ETHERTYPE_IP
);
519 u_short arp_native
=htons(ETHERTYPE_ARP
);
522 bzero(&demux
[0], sizeof(demux
));
523 demux
[0].type
= DLIL_DESC_ETYPE2
;
524 demux
[0].data
= &en_native
;
525 demux
[0].datalen
= sizeof(en_native
);
526 demux
[1].type
= DLIL_DESC_ETYPE2
;
527 demux
[1].data
= &arp_native
;
528 demux
[1].datalen
= sizeof(arp_native
);
530 bzero(&proto
, sizeof(proto
));
531 proto
.demux_list
= demux
;
532 proto
.demux_count
= sizeof(demux
) / sizeof(demux
[0]);
533 proto
.input
= ether_inet_input
;
534 proto
.pre_output
= ether_inet_pre_output
;
535 proto
.ioctl
= ether_inet_prmod_ioctl
;
536 proto
.event
= ether_inet_event
;
537 proto
.resolve
= ether_inet_resolve_multi
;
538 proto
.send_arp
= ether_inet_arp
;
540 error
= ifnet_attach_protocol_v2(ifp
, proto_family
, &proto
);
541 if (error
&& error
!= EEXIST
) {
542 printf("WARNING: ether_attach_inet can't attach ip to %s%d\n",
543 ifp
->if_name
, ifp
->if_unit
);
551 protocol_family_t proto_family
)
553 (void)ifnet_detach_protocol(ifp
, proto_family
);