2 * Copyright (c) 2000 Apple Computer, 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
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/malloc.h>
69 #include <sys/socket.h>
70 #include <sys/sockio.h>
71 #include <sys/sysctl.h>
72 #include <kern/lock.h>
75 #include <net/route.h>
76 #include <net/if_llc.h>
77 #include <net/if_dl.h>
78 #include <net/if_types.h>
79 #include <net/kpi_protocol.h>
81 #include <netinet/in.h>
82 #include <netinet/in_var.h>
83 #include <netinet/if_ether.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/in_arp.h>
88 #include <sys/socketvar.h>
93 extern struct ifqueue pkintrq
;
97 #include <net/bridge.h>
100 /* #include "vlan.h" */
102 #include <net/if_vlan_var.h>
103 #endif /* NVLAN > 0 */
105 /* Local function declerations */
106 int ether_attach_inet(struct ifnet
*ifp
, u_long proto_family
);
107 int ether_detach_inet(struct ifnet
*ifp
, u_long proto_family
);
109 extern void * kdp_get_interface(void);
110 extern void ipintr(void);
111 extern void arp_input(struct mbuf
* m
);
114 inet_ether_arp_input(
117 struct ether_arp
*ea
;
118 struct sockaddr_dl sender_hw
;
119 struct sockaddr_in sender_ip
;
120 struct sockaddr_in target_ip
;
122 if (mbuf_len(m
) < sizeof(*ea
) &&
123 mbuf_pullup(&m
, sizeof(*ea
)) != 0)
128 /* Verify this is an ethernet/ip arp and address lengths are correct */
129 if (ntohs(ea
->arp_hrd
) != ARPHRD_ETHER
||
130 ntohs(ea
->arp_pro
) != ETHERTYPE_IP
||
131 ea
->arp_pln
!= sizeof(struct in_addr
) ||
132 ea
->arp_hln
!= ETHER_ADDR_LEN
) {
137 /* Verify the sender is not broadcast */
138 if (bcmp(ea
->arp_sha
, etherbroadcastaddr
, ETHER_ADDR_LEN
) == 0) {
143 bzero(&sender_ip
, sizeof(sender_ip
));
144 sender_ip
.sin_len
= sizeof(sender_ip
);
145 sender_ip
.sin_family
= AF_INET
;
146 sender_ip
.sin_addr
= *(struct in_addr
*)ea
->arp_spa
;
147 target_ip
= sender_ip
;
148 target_ip
.sin_addr
= *(struct in_addr
*)ea
->arp_tpa
;
150 bzero(&sender_hw
, sizeof(sender_hw
));
151 sender_hw
.sdl_len
= sizeof(sender_hw
);
152 sender_hw
.sdl_family
= AF_LINK
;
153 sender_hw
.sdl_type
= IFT_ETHER
;
154 sender_hw
.sdl_alen
= ETHER_ADDR_LEN
;
155 bcopy(ea
->arp_sha
, LLADDR(&sender_hw
), ETHER_ADDR_LEN
);
157 arp_ip_handle_input(mbuf_pkthdr_rcvif(m
), ntohs(ea
->arp_op
), &sender_hw
, &sender_ip
, &target_ip
);
162 * Process a received Ethernet packet;
163 * the packet is in the mbuf chain m without
164 * the ether header, which is provided separately.
168 __unused ifnet_t ifp
,
169 __unused protocol_family_t protocol_family
,
173 register struct ether_header
*eh
= (struct ether_header
*) frame_header
;
176 ether_type
= ntohs(eh
->ether_type
);
178 switch (ether_type
) {
181 proto_input(PF_INET
, m
);
184 case ETHERTYPE_ARP
: {
185 inet_ether_arp_input(m
);
198 inet_ether_pre_output(
200 __unused protocol_family_t protocol_family
,
202 const struct sockaddr
*dst_netaddr
,
207 register struct mbuf
*m
= *m0
;
208 register struct ether_header
*eh
;
212 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
216 * Tell ether_frameout it's ok to loop packet unless negated below.
218 m
->m_flags
|= M_LOOP
;
220 switch (dst_netaddr
->sa_family
) {
223 struct sockaddr_dl ll_dest
;
224 result
= arp_lookup_ip(ifp
, (const struct sockaddr_in
*)dst_netaddr
,
225 &ll_dest
, sizeof(ll_dest
), (route_t
)route
, *m0
);
227 bcopy(LLADDR(&ll_dest
), edst
, ETHER_ADDR_LEN
);
228 *(u_int16_t
*)type
= htons(ETHERTYPE_IP
);
233 case pseudo_AF_HDRCMPLT
:
235 m
->m_flags
&= ~M_LOOP
;
236 eh
= (struct ether_header
*)dst_netaddr
->sa_data
;
237 (void)memcpy(edst
, eh
->ether_dhost
, 6);
238 *(u_short
*)type
= eh
->ether_type
;
242 printf("%s%d: can't handle af%d\n", ifp
->if_name
, ifp
->if_unit
,
243 dst_netaddr
->sa_family
);
245 result
= EAFNOSUPPORT
;
252 ether_inet_resolve_multi(
254 const struct sockaddr
*proto_addr
,
255 struct sockaddr_dl
*out_ll
,
258 static const size_t minsize
= offsetof(struct sockaddr_dl
, sdl_data
[0]) + ETHER_ADDR_LEN
;
259 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)proto_addr
;
261 if (proto_addr
->sa_family
!= AF_INET
)
264 if (proto_addr
->sa_len
< sizeof(struct sockaddr_in
))
267 if (ll_len
< minsize
)
270 bzero(out_ll
, minsize
);
271 out_ll
->sdl_len
= minsize
;
272 out_ll
->sdl_family
= AF_LINK
;
273 out_ll
->sdl_index
= ifp
->if_index
;
274 out_ll
->sdl_type
= IFT_ETHER
;
275 out_ll
->sdl_nlen
= 0;
276 out_ll
->sdl_alen
= ETHER_ADDR_LEN
;
277 out_ll
->sdl_slen
= 0;
278 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, LLADDR(out_ll
));
285 ether_inet_prmod_ioctl(
287 __unused protocol_family_t protocol_family
,
292 struct ifreq
*ifr
= data
;
298 if ((ifnet_flags(ifp
) & IFF_RUNNING
) == 0) {
299 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
300 ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
303 switch (ifaddr_address_family(ifa
)) {
307 inet_arp_init_ifaddr(ifp
, ifa
);
309 * Register new IP and MAC addresses with the kernel debugger
310 * if the interface is the same as was registered by IOKernelDebugger. If
311 * no interface was registered, fall back and just match against en0 interface.
313 if ((kdp_get_interface() != 0 && kdp_get_interface() == ifp
->if_softc
)
314 || (kdp_get_interface() == 0 && ifp
->if_unit
== 0))
315 kdp_set_ip_and_mac_addresses(&(IA_SIN(ifa
)->sin_addr
), ifnet_lladdr(ifp
));
326 ifnet_lladdr_copy_bytes(ifp
, ifr
->ifr_addr
.sa_data
, ETHER_ADDR_LEN
);
331 * IOKit IONetworkFamily will set the right MTU according to the driver
346 __unused protocol_family_t protocol
,
347 const struct kev_msg
*event
)
351 if (event
->vendor_code
!= KEV_VENDOR_APPLE
||
352 event
->kev_class
!= KEV_NETWORK_CLASS
||
353 event
->kev_subclass
!= KEV_DL_SUBCLASS
||
354 event
->event_code
!= KEV_DL_LINK_ADDRESS_CHANGED
) {
358 if (ifnet_get_address_list_family(ifp
, &addresses
, AF_INET
) == 0) {
361 for (i
= 0; addresses
[i
] != NULL
; i
++) {
362 inet_arp_init_ifaddr(ifp
, addresses
[i
]);
365 ifnet_free_address_list(addresses
);
373 const struct sockaddr_dl
* sender_hw
,
374 const struct sockaddr
* sender_proto
,
375 const struct sockaddr_dl
* target_hw
,
376 const struct sockaddr
* target_proto
)
380 struct ether_header
*eh
;
381 struct ether_arp
*ea
;
382 const struct sockaddr_in
* sender_ip
= (const struct sockaddr_in
*)sender_proto
;
383 const struct sockaddr_in
* target_ip
= (const struct sockaddr_in
*)target_proto
;
386 if (target_ip
== NULL
)
389 if ((sender_ip
&& sender_ip
->sin_family
!= AF_INET
) ||
390 (target_ip
&& target_ip
->sin_family
!= AF_INET
))
393 result
= mbuf_gethdr(MBUF_WAITOK
, MBUF_TYPE_DATA
, &m
);
397 mbuf_setlen(m
, sizeof(*ea
));
398 mbuf_pkthdr_setlen(m
, sizeof(*ea
));
400 /* Move the data pointer in the mbuf to the end, aligned to 4 bytes */
401 datap
= mbuf_datastart(m
);
402 datap
+= mbuf_trailingspace(m
);
403 datap
-= (((u_long
)datap
) & 0x3);
404 mbuf_setdata(m
, datap
, sizeof(*ea
));
407 /* Prepend the ethernet header, we will send the raw frame */
408 mbuf_prepend(&m
, sizeof(*eh
), MBUF_WAITOK
);
410 eh
->ether_type
= htons(ETHERTYPE_ARP
);
412 /* Fill out the arp header */
413 ea
->arp_pro
= htons(ETHERTYPE_IP
);
414 ea
->arp_hln
= sizeof(ea
->arp_sha
);
415 ea
->arp_pln
= sizeof(ea
->arp_spa
);
416 ea
->arp_hrd
= htons(ARPHRD_ETHER
);
417 ea
->arp_op
= htons(arpop
);
419 /* Sender Hardware */
420 if (sender_hw
!= NULL
) {
421 bcopy(CONST_LLADDR(sender_hw
), ea
->arp_sha
, sizeof(ea
->arp_sha
));
424 ifnet_lladdr_copy_bytes(ifp
, ea
->arp_sha
, ETHER_ADDR_LEN
);
426 ifnet_lladdr_copy_bytes(ifp
, eh
->ether_shost
, sizeof(eh
->ether_shost
));
429 if (sender_ip
!= NULL
) {
430 bcopy(&sender_ip
->sin_addr
, ea
->arp_spa
, sizeof(ea
->arp_spa
));
435 /* Look for an IP address to use as our source */
436 ifnet_lock_shared(ifp
);
437 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
438 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET
)
442 bcopy(&((struct sockaddr_in
*)ifa
->ifa_addr
)->sin_addr
, ea
->arp_spa
,
443 sizeof(ea
->arp_spa
));
445 ifnet_lock_done(ifp
);
453 /* Target Hardware */
454 if (target_hw
== 0) {
455 bzero(ea
->arp_tha
, sizeof(ea
->arp_tha
));
456 bcopy(etherbroadcastaddr
, eh
->ether_dhost
, sizeof(eh
->ether_dhost
));
459 bcopy(CONST_LLADDR(target_hw
), ea
->arp_tha
, sizeof(ea
->arp_tha
));
460 bcopy(CONST_LLADDR(target_hw
), eh
->ether_dhost
, sizeof(eh
->ether_dhost
));
464 bcopy(&target_ip
->sin_addr
, ea
->arp_tpa
, sizeof(ea
->arp_tpa
));
466 ifnet_output_raw(ifp
, PF_INET
, m
);
474 __unused u_long proto_family
)
476 struct ifnet_attach_proto_param proto
;
477 struct ifnet_demux_desc demux
[2];
478 u_short en_native
=htons(ETHERTYPE_IP
);
479 u_short arp_native
=htons(ETHERTYPE_ARP
);
482 bzero(&demux
[0], sizeof(demux
));
483 demux
[0].type
= DLIL_DESC_ETYPE2
;
484 demux
[0].data
= &en_native
;
485 demux
[0].datalen
= sizeof(en_native
);
486 demux
[1].type
= DLIL_DESC_ETYPE2
;
487 demux
[1].data
= &arp_native
;
488 demux
[1].datalen
= sizeof(arp_native
);
490 bzero(&proto
, sizeof(proto
));
491 proto
.demux_list
= demux
;
492 proto
.demux_count
= sizeof(demux
) / sizeof(demux
[0]);
493 proto
.input
= inet_ether_input
;
494 proto
.pre_output
= inet_ether_pre_output
;
495 proto
.ioctl
= ether_inet_prmod_ioctl
;
496 proto
.event
= ether_inet_event
;
497 proto
.resolve
= ether_inet_resolve_multi
;
498 proto
.send_arp
= ether_inet_arp
;
500 error
= ifnet_attach_protocol(ifp
, proto_family
, &proto
);
501 if (error
&& error
!= EEXIST
) {
502 printf("WARNING: ether_attach_inet can't attach ip to %s%d\n",
503 ifp
->if_name
, ifp
->if_unit
);
515 stat
= dlil_detach_protocol(ifp
, proto_family
);