2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1982, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/malloc.h>
64 #include <sys/socket.h>
65 #include <sys/sockio.h>
66 #include <sys/sysctl.h>
67 #include <kern/lock.h>
70 #include <net/route.h>
71 #include <net/if_llc.h>
72 #include <net/if_dl.h>
73 #include <net/if_types.h>
74 #include <net/kpi_protocol.h>
76 #include <netinet/in.h>
77 #include <netinet/in_var.h>
78 #include <netinet/if_ether.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/in_arp.h>
83 #include <sys/socketvar.h>
88 extern struct ifqueue pkintrq
;
92 #include <net/bridge.h>
95 /* #include "vlan.h" */
97 #include <net/if_vlan_var.h>
98 #endif /* NVLAN > 0 */
100 /* Local function declerations */
101 int ether_attach_inet(struct ifnet
*ifp
, u_long proto_family
);
102 int ether_detach_inet(struct ifnet
*ifp
, u_long proto_family
);
104 extern void * kdp_get_interface(void);
105 extern void ipintr(void);
106 extern void arp_input(struct mbuf
* m
);
109 inet_ether_arp_input(
112 struct ether_arp
*ea
;
113 struct sockaddr_dl sender_hw
;
114 struct sockaddr_in sender_ip
;
115 struct sockaddr_in target_ip
;
117 if (mbuf_len(m
) < sizeof(*ea
) &&
118 mbuf_pullup(&m
, sizeof(*ea
)) != 0)
123 /* Verify this is an ethernet/ip arp and address lengths are correct */
124 if (ntohs(ea
->arp_hrd
) != ARPHRD_ETHER
||
125 ntohs(ea
->arp_pro
) != ETHERTYPE_IP
||
126 ea
->arp_pln
!= sizeof(struct in_addr
) ||
127 ea
->arp_hln
!= ETHER_ADDR_LEN
) {
132 /* Verify the sender is not broadcast or multicast */
133 if ((ea
->arp_sha
[0] & 0x01) != 0) {
138 bzero(&sender_ip
, sizeof(sender_ip
));
139 sender_ip
.sin_len
= sizeof(sender_ip
);
140 sender_ip
.sin_family
= AF_INET
;
141 sender_ip
.sin_addr
= *(struct in_addr
*)ea
->arp_spa
;
142 target_ip
= sender_ip
;
143 target_ip
.sin_addr
= *(struct in_addr
*)ea
->arp_tpa
;
145 bzero(&sender_hw
, sizeof(sender_hw
));
146 sender_hw
.sdl_len
= sizeof(sender_hw
);
147 sender_hw
.sdl_family
= AF_LINK
;
148 sender_hw
.sdl_type
= IFT_ETHER
;
149 sender_hw
.sdl_alen
= ETHER_ADDR_LEN
;
150 bcopy(ea
->arp_sha
, LLADDR(&sender_hw
), ETHER_ADDR_LEN
);
152 arp_ip_handle_input(mbuf_pkthdr_rcvif(m
), ntohs(ea
->arp_op
), &sender_hw
, &sender_ip
, &target_ip
);
157 * Process a received Ethernet packet;
158 * the packet is in the mbuf chain m without
159 * the ether header, which is provided separately.
163 __unused ifnet_t ifp
,
164 __unused protocol_family_t protocol_family
,
168 register struct ether_header
*eh
= (struct ether_header
*) frame_header
;
171 ether_type
= ntohs(eh
->ether_type
);
173 switch (ether_type
) {
176 proto_input(PF_INET
, m
);
179 case ETHERTYPE_ARP
: {
180 inet_ether_arp_input(m
);
193 inet_ether_pre_output(
195 __unused protocol_family_t protocol_family
,
197 const struct sockaddr
*dst_netaddr
,
202 register struct mbuf
*m
= *m0
;
203 register struct ether_header
*eh
;
207 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
211 * Tell ether_frameout it's ok to loop packet unless negated below.
213 m
->m_flags
|= M_LOOP
;
215 switch (dst_netaddr
->sa_family
) {
218 struct sockaddr_dl ll_dest
;
219 result
= arp_lookup_ip(ifp
, (const struct sockaddr_in
*)dst_netaddr
,
220 &ll_dest
, sizeof(ll_dest
), (route_t
)route
, *m0
);
222 bcopy(LLADDR(&ll_dest
), edst
, ETHER_ADDR_LEN
);
223 *(u_int16_t
*)type
= htons(ETHERTYPE_IP
);
228 case pseudo_AF_HDRCMPLT
:
230 m
->m_flags
&= ~M_LOOP
;
231 eh
= (struct ether_header
*)dst_netaddr
->sa_data
;
232 (void)memcpy(edst
, eh
->ether_dhost
, 6);
233 *(u_short
*)type
= eh
->ether_type
;
237 printf("%s%d: can't handle af%d\n", ifp
->if_name
, ifp
->if_unit
,
238 dst_netaddr
->sa_family
);
240 result
= EAFNOSUPPORT
;
247 ether_inet_resolve_multi(
249 const struct sockaddr
*proto_addr
,
250 struct sockaddr_dl
*out_ll
,
253 static const size_t minsize
= offsetof(struct sockaddr_dl
, sdl_data
[0]) + ETHER_ADDR_LEN
;
254 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)proto_addr
;
256 if (proto_addr
->sa_family
!= AF_INET
)
259 if (proto_addr
->sa_len
< sizeof(struct sockaddr_in
))
262 if (ll_len
< minsize
)
265 bzero(out_ll
, minsize
);
266 out_ll
->sdl_len
= minsize
;
267 out_ll
->sdl_family
= AF_LINK
;
268 out_ll
->sdl_index
= ifp
->if_index
;
269 out_ll
->sdl_type
= IFT_ETHER
;
270 out_ll
->sdl_nlen
= 0;
271 out_ll
->sdl_alen
= ETHER_ADDR_LEN
;
272 out_ll
->sdl_slen
= 0;
273 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, LLADDR(out_ll
));
280 ether_inet_prmod_ioctl(
282 __unused protocol_family_t protocol_family
,
287 struct ifreq
*ifr
= data
;
293 if ((ifnet_flags(ifp
) & IFF_RUNNING
) == 0) {
294 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
295 ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
298 switch (ifaddr_address_family(ifa
)) {
302 inet_arp_init_ifaddr(ifp
, ifa
);
304 * Register new IP and MAC addresses with the kernel debugger
305 * if the interface is the same as was registered by IOKernelDebugger. If
306 * no interface was registered, fall back and just match against en0 interface.
308 if ((kdp_get_interface() != 0 && kdp_get_interface() == ifp
->if_softc
)
309 || (kdp_get_interface() == 0 && ifp
->if_unit
== 0))
310 kdp_set_ip_and_mac_addresses(&(IA_SIN(ifa
)->sin_addr
), ifnet_lladdr(ifp
));
321 ifnet_lladdr_copy_bytes(ifp
, ifr
->ifr_addr
.sa_data
, ETHER_ADDR_LEN
);
326 * IOKit IONetworkFamily will set the right MTU according to the driver
341 __unused protocol_family_t protocol
,
342 const struct kev_msg
*event
)
346 if (event
->vendor_code
!= KEV_VENDOR_APPLE
||
347 event
->kev_class
!= KEV_NETWORK_CLASS
||
348 event
->kev_subclass
!= KEV_DL_SUBCLASS
||
349 event
->event_code
!= KEV_DL_LINK_ADDRESS_CHANGED
) {
353 if (ifnet_get_address_list_family(ifp
, &addresses
, AF_INET
) == 0) {
356 for (i
= 0; addresses
[i
] != NULL
; i
++) {
357 inet_arp_init_ifaddr(ifp
, addresses
[i
]);
360 ifnet_free_address_list(addresses
);
368 const struct sockaddr_dl
* sender_hw
,
369 const struct sockaddr
* sender_proto
,
370 const struct sockaddr_dl
* target_hw
,
371 const struct sockaddr
* target_proto
)
375 struct ether_header
*eh
;
376 struct ether_arp
*ea
;
377 const struct sockaddr_in
* sender_ip
= (const struct sockaddr_in
*)sender_proto
;
378 const struct sockaddr_in
* target_ip
= (const struct sockaddr_in
*)target_proto
;
381 if (target_ip
== NULL
)
384 if ((sender_ip
&& sender_ip
->sin_family
!= AF_INET
) ||
385 (target_ip
&& target_ip
->sin_family
!= AF_INET
))
388 result
= mbuf_gethdr(MBUF_WAITOK
, MBUF_TYPE_DATA
, &m
);
392 mbuf_setlen(m
, sizeof(*ea
));
393 mbuf_pkthdr_setlen(m
, sizeof(*ea
));
395 /* Move the data pointer in the mbuf to the end, aligned to 4 bytes */
396 datap
= mbuf_datastart(m
);
397 datap
+= mbuf_trailingspace(m
);
398 datap
-= (((u_long
)datap
) & 0x3);
399 mbuf_setdata(m
, datap
, sizeof(*ea
));
402 /* Prepend the ethernet header, we will send the raw frame */
403 mbuf_prepend(&m
, sizeof(*eh
), MBUF_WAITOK
);
405 eh
->ether_type
= htons(ETHERTYPE_ARP
);
407 /* Fill out the arp header */
408 ea
->arp_pro
= htons(ETHERTYPE_IP
);
409 ea
->arp_hln
= sizeof(ea
->arp_sha
);
410 ea
->arp_pln
= sizeof(ea
->arp_spa
);
411 ea
->arp_hrd
= htons(ARPHRD_ETHER
);
412 ea
->arp_op
= htons(arpop
);
414 /* Sender Hardware */
415 if (sender_hw
!= NULL
) {
416 bcopy(CONST_LLADDR(sender_hw
), ea
->arp_sha
, sizeof(ea
->arp_sha
));
419 ifnet_lladdr_copy_bytes(ifp
, ea
->arp_sha
, ETHER_ADDR_LEN
);
421 ifnet_lladdr_copy_bytes(ifp
, eh
->ether_shost
, sizeof(eh
->ether_shost
));
424 if (sender_ip
!= NULL
) {
425 bcopy(&sender_ip
->sin_addr
, ea
->arp_spa
, sizeof(ea
->arp_spa
));
430 /* Look for an IP address to use as our source */
431 ifnet_lock_shared(ifp
);
432 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
433 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET
)
437 bcopy(&((struct sockaddr_in
*)ifa
->ifa_addr
)->sin_addr
, ea
->arp_spa
,
438 sizeof(ea
->arp_spa
));
440 ifnet_lock_done(ifp
);
448 /* Target Hardware */
449 if (target_hw
== 0) {
450 bzero(ea
->arp_tha
, sizeof(ea
->arp_tha
));
451 bcopy(etherbroadcastaddr
, eh
->ether_dhost
, sizeof(eh
->ether_dhost
));
454 bcopy(CONST_LLADDR(target_hw
), ea
->arp_tha
, sizeof(ea
->arp_tha
));
455 bcopy(CONST_LLADDR(target_hw
), eh
->ether_dhost
, sizeof(eh
->ether_dhost
));
459 bcopy(&target_ip
->sin_addr
, ea
->arp_tpa
, sizeof(ea
->arp_tpa
));
461 ifnet_output_raw(ifp
, PF_INET
, m
);
469 __unused u_long proto_family
)
471 struct ifnet_attach_proto_param proto
;
472 struct ifnet_demux_desc demux
[2];
473 u_short en_native
=htons(ETHERTYPE_IP
);
474 u_short arp_native
=htons(ETHERTYPE_ARP
);
477 bzero(&demux
[0], sizeof(demux
));
478 demux
[0].type
= DLIL_DESC_ETYPE2
;
479 demux
[0].data
= &en_native
;
480 demux
[0].datalen
= sizeof(en_native
);
481 demux
[1].type
= DLIL_DESC_ETYPE2
;
482 demux
[1].data
= &arp_native
;
483 demux
[1].datalen
= sizeof(arp_native
);
485 bzero(&proto
, sizeof(proto
));
486 proto
.demux_list
= demux
;
487 proto
.demux_count
= sizeof(demux
) / sizeof(demux
[0]);
488 proto
.input
= inet_ether_input
;
489 proto
.pre_output
= inet_ether_pre_output
;
490 proto
.ioctl
= ether_inet_prmod_ioctl
;
491 proto
.event
= ether_inet_event
;
492 proto
.resolve
= ether_inet_resolve_multi
;
493 proto
.send_arp
= ether_inet_arp
;
495 error
= ifnet_attach_protocol(ifp
, proto_family
, &proto
);
496 if (error
&& error
!= EEXIST
) {
497 printf("WARNING: ether_attach_inet can't attach ip to %s%d\n",
498 ifp
->if_name
, ifp
->if_unit
);
510 stat
= dlil_detach_protocol(ifp
, proto_family
);