2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1982, 1989, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/malloc.h>
71 #include <sys/socket.h>
72 #include <sys/sockio.h>
73 #include <sys/sysctl.h>
74 #include <kern/lock.h>
77 #include <net/route.h>
78 #include <net/if_llc.h>
79 #include <net/if_dl.h>
80 #include <net/if_types.h>
81 #include <net/kpi_protocol.h>
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
85 #include <netinet/if_ether.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/in_arp.h>
90 #include <sys/socketvar.h>
95 extern struct ifqueue pkintrq
;
99 #include <net/bridge.h>
102 /* #include "vlan.h" */
104 #include <net/if_vlan_var.h>
105 #endif /* NVLAN > 0 */
107 /* Local function declerations */
108 int ether_attach_inet(struct ifnet
*ifp
, u_long proto_family
);
109 int ether_detach_inet(struct ifnet
*ifp
, u_long proto_family
);
111 extern void * kdp_get_interface(void);
112 extern void ipintr(void);
113 extern void arp_input(struct mbuf
* m
);
116 inet_ether_arp_input(
119 struct ether_arp
*ea
;
120 struct sockaddr_dl sender_hw
;
121 struct sockaddr_in sender_ip
;
122 struct sockaddr_in target_ip
;
124 if (mbuf_len(m
) < sizeof(*ea
) &&
125 mbuf_pullup(&m
, sizeof(*ea
)) != 0)
130 /* Verify this is an ethernet/ip arp and address lengths are correct */
131 if (ntohs(ea
->arp_hrd
) != ARPHRD_ETHER
||
132 ntohs(ea
->arp_pro
) != ETHERTYPE_IP
||
133 ea
->arp_pln
!= sizeof(struct in_addr
) ||
134 ea
->arp_hln
!= ETHER_ADDR_LEN
) {
139 /* Verify the sender is not broadcast */
140 if (bcmp(ea
->arp_sha
, etherbroadcastaddr
, ETHER_ADDR_LEN
) == 0) {
145 bzero(&sender_ip
, sizeof(sender_ip
));
146 sender_ip
.sin_len
= sizeof(sender_ip
);
147 sender_ip
.sin_family
= AF_INET
;
148 sender_ip
.sin_addr
= *(struct in_addr
*)ea
->arp_spa
;
149 target_ip
= sender_ip
;
150 target_ip
.sin_addr
= *(struct in_addr
*)ea
->arp_tpa
;
152 bzero(&sender_hw
, sizeof(sender_hw
));
153 sender_hw
.sdl_len
= sizeof(sender_hw
);
154 sender_hw
.sdl_family
= AF_LINK
;
155 sender_hw
.sdl_type
= IFT_ETHER
;
156 sender_hw
.sdl_alen
= ETHER_ADDR_LEN
;
157 bcopy(ea
->arp_sha
, LLADDR(&sender_hw
), ETHER_ADDR_LEN
);
159 arp_ip_handle_input(mbuf_pkthdr_rcvif(m
), ntohs(ea
->arp_op
), &sender_hw
, &sender_ip
, &target_ip
);
164 * Process a received Ethernet packet;
165 * the packet is in the mbuf chain m without
166 * the ether header, which is provided separately.
170 __unused ifnet_t ifp
,
171 __unused protocol_family_t protocol_family
,
175 register struct ether_header
*eh
= (struct ether_header
*) frame_header
;
178 ether_type
= ntohs(eh
->ether_type
);
180 switch (ether_type
) {
183 proto_input(PF_INET
, m
);
186 case ETHERTYPE_ARP
: {
187 inet_ether_arp_input(m
);
200 inet_ether_pre_output(
202 __unused protocol_family_t protocol_family
,
204 const struct sockaddr
*dst_netaddr
,
209 register struct mbuf
*m
= *m0
;
210 register struct ether_header
*eh
;
214 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
218 * Tell ether_frameout it's ok to loop packet unless negated below.
220 m
->m_flags
|= M_LOOP
;
222 switch (dst_netaddr
->sa_family
) {
225 struct sockaddr_dl ll_dest
;
226 result
= arp_lookup_ip(ifp
, (const struct sockaddr_in
*)dst_netaddr
,
227 &ll_dest
, sizeof(ll_dest
), (route_t
)route
, *m0
);
229 bcopy(LLADDR(&ll_dest
), edst
, ETHER_ADDR_LEN
);
230 *(u_int16_t
*)type
= htons(ETHERTYPE_IP
);
235 case pseudo_AF_HDRCMPLT
:
237 m
->m_flags
&= ~M_LOOP
;
238 eh
= (struct ether_header
*)dst_netaddr
->sa_data
;
239 (void)memcpy(edst
, eh
->ether_dhost
, 6);
240 *(u_short
*)type
= eh
->ether_type
;
244 printf("%s%d: can't handle af%d\n", ifp
->if_name
, ifp
->if_unit
,
245 dst_netaddr
->sa_family
);
247 result
= EAFNOSUPPORT
;
254 ether_inet_resolve_multi(
256 const struct sockaddr
*proto_addr
,
257 struct sockaddr_dl
*out_ll
,
260 static const size_t minsize
= offsetof(struct sockaddr_dl
, sdl_data
[0]) + ETHER_ADDR_LEN
;
261 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)proto_addr
;
263 if (proto_addr
->sa_family
!= AF_INET
)
266 if (proto_addr
->sa_len
< sizeof(struct sockaddr_in
))
269 if (ll_len
< minsize
)
272 bzero(out_ll
, minsize
);
273 out_ll
->sdl_len
= minsize
;
274 out_ll
->sdl_family
= AF_LINK
;
275 out_ll
->sdl_index
= ifp
->if_index
;
276 out_ll
->sdl_type
= IFT_ETHER
;
277 out_ll
->sdl_nlen
= 0;
278 out_ll
->sdl_alen
= ETHER_ADDR_LEN
;
279 out_ll
->sdl_slen
= 0;
280 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, LLADDR(out_ll
));
287 ether_inet_prmod_ioctl(
289 __unused protocol_family_t protocol_family
,
294 struct ifreq
*ifr
= data
;
300 if ((ifnet_flags(ifp
) & IFF_RUNNING
) == 0) {
301 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
302 ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
305 switch (ifaddr_address_family(ifa
)) {
309 inet_arp_init_ifaddr(ifp
, ifa
);
311 * Register new IP and MAC addresses with the kernel debugger
312 * if the interface is the same as was registered by IOKernelDebugger. If
313 * no interface was registered, fall back and just match against en0 interface.
315 if ((kdp_get_interface() != 0 && kdp_get_interface() == ifp
->if_softc
)
316 || (kdp_get_interface() == 0 && ifp
->if_unit
== 0))
317 kdp_set_ip_and_mac_addresses(&(IA_SIN(ifa
)->sin_addr
), ifnet_lladdr(ifp
));
328 ifnet_lladdr_copy_bytes(ifp
, ifr
->ifr_addr
.sa_data
, ETHER_ADDR_LEN
);
333 * IOKit IONetworkFamily will set the right MTU according to the driver
348 __unused protocol_family_t protocol
,
349 const struct kev_msg
*event
)
353 if (event
->vendor_code
!= KEV_VENDOR_APPLE
||
354 event
->kev_class
!= KEV_NETWORK_CLASS
||
355 event
->kev_subclass
!= KEV_DL_SUBCLASS
||
356 event
->event_code
!= KEV_DL_LINK_ADDRESS_CHANGED
) {
360 if (ifnet_get_address_list_family(ifp
, &addresses
, AF_INET
) == 0) {
363 for (i
= 0; addresses
[i
] != NULL
; i
++) {
364 inet_arp_init_ifaddr(ifp
, addresses
[i
]);
367 ifnet_free_address_list(addresses
);
375 const struct sockaddr_dl
* sender_hw
,
376 const struct sockaddr
* sender_proto
,
377 const struct sockaddr_dl
* target_hw
,
378 const struct sockaddr
* target_proto
)
382 struct ether_header
*eh
;
383 struct ether_arp
*ea
;
384 const struct sockaddr_in
* sender_ip
= (const struct sockaddr_in
*)sender_proto
;
385 const struct sockaddr_in
* target_ip
= (const struct sockaddr_in
*)target_proto
;
388 if (target_ip
== NULL
)
391 if ((sender_ip
&& sender_ip
->sin_family
!= AF_INET
) ||
392 (target_ip
&& target_ip
->sin_family
!= AF_INET
))
395 result
= mbuf_gethdr(MBUF_WAITOK
, MBUF_TYPE_DATA
, &m
);
399 mbuf_setlen(m
, sizeof(*ea
));
400 mbuf_pkthdr_setlen(m
, sizeof(*ea
));
402 /* Move the data pointer in the mbuf to the end, aligned to 4 bytes */
403 datap
= mbuf_datastart(m
);
404 datap
+= mbuf_trailingspace(m
);
405 datap
-= (((u_long
)datap
) & 0x3);
406 mbuf_setdata(m
, datap
, sizeof(*ea
));
409 /* Prepend the ethernet header, we will send the raw frame */
410 mbuf_prepend(&m
, sizeof(*eh
), MBUF_WAITOK
);
412 eh
->ether_type
= htons(ETHERTYPE_ARP
);
414 /* Fill out the arp header */
415 ea
->arp_pro
= htons(ETHERTYPE_IP
);
416 ea
->arp_hln
= sizeof(ea
->arp_sha
);
417 ea
->arp_pln
= sizeof(ea
->arp_spa
);
418 ea
->arp_hrd
= htons(ARPHRD_ETHER
);
419 ea
->arp_op
= htons(arpop
);
421 /* Sender Hardware */
422 if (sender_hw
!= NULL
) {
423 bcopy(CONST_LLADDR(sender_hw
), ea
->arp_sha
, sizeof(ea
->arp_sha
));
426 ifnet_lladdr_copy_bytes(ifp
, ea
->arp_sha
, ETHER_ADDR_LEN
);
428 ifnet_lladdr_copy_bytes(ifp
, eh
->ether_shost
, sizeof(eh
->ether_shost
));
431 if (sender_ip
!= NULL
) {
432 bcopy(&sender_ip
->sin_addr
, ea
->arp_spa
, sizeof(ea
->arp_spa
));
437 /* Look for an IP address to use as our source */
438 ifnet_lock_shared(ifp
);
439 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
440 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET
)
444 bcopy(&((struct sockaddr_in
*)ifa
->ifa_addr
)->sin_addr
, ea
->arp_spa
,
445 sizeof(ea
->arp_spa
));
447 ifnet_lock_done(ifp
);
455 /* Target Hardware */
456 if (target_hw
== 0) {
457 bzero(ea
->arp_tha
, sizeof(ea
->arp_tha
));
458 bcopy(etherbroadcastaddr
, eh
->ether_dhost
, sizeof(eh
->ether_dhost
));
461 bcopy(CONST_LLADDR(target_hw
), ea
->arp_tha
, sizeof(ea
->arp_tha
));
462 bcopy(CONST_LLADDR(target_hw
), eh
->ether_dhost
, sizeof(eh
->ether_dhost
));
466 bcopy(&target_ip
->sin_addr
, ea
->arp_tpa
, sizeof(ea
->arp_tpa
));
468 ifnet_output_raw(ifp
, PF_INET
, m
);
476 __unused u_long proto_family
)
478 struct ifnet_attach_proto_param proto
;
479 struct ifnet_demux_desc demux
[2];
480 u_short en_native
=htons(ETHERTYPE_IP
);
481 u_short arp_native
=htons(ETHERTYPE_ARP
);
484 bzero(&demux
[0], sizeof(demux
));
485 demux
[0].type
= DLIL_DESC_ETYPE2
;
486 demux
[0].data
= &en_native
;
487 demux
[0].datalen
= sizeof(en_native
);
488 demux
[1].type
= DLIL_DESC_ETYPE2
;
489 demux
[1].data
= &arp_native
;
490 demux
[1].datalen
= sizeof(arp_native
);
492 bzero(&proto
, sizeof(proto
));
493 proto
.demux_list
= demux
;
494 proto
.demux_count
= sizeof(demux
) / sizeof(demux
[0]);
495 proto
.input
= inet_ether_input
;
496 proto
.pre_output
= inet_ether_pre_output
;
497 proto
.ioctl
= ether_inet_prmod_ioctl
;
498 proto
.event
= ether_inet_event
;
499 proto
.resolve
= ether_inet_resolve_multi
;
500 proto
.send_arp
= ether_inet_arp
;
502 error
= ifnet_attach_protocol(ifp
, proto_family
, &proto
);
503 if (error
&& error
!= EEXIST
) {
504 printf("WARNING: ether_attach_inet can't attach ip to %s%d\n",
505 ifp
->if_name
, ifp
->if_unit
);
517 stat
= dlil_detach_protocol(ifp
, proto_family
);