2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1989, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
61 #include <sys/malloc.h>
63 #include <sys/socket.h>
64 #include <sys/sockio.h>
65 #include <sys/sysctl.h>
66 #include <kern/lock.h>
69 #include <net/route.h>
70 #include <net/if_llc.h>
71 #include <net/if_dl.h>
72 #include <net/if_types.h>
73 #include <net/kpi_protocol.h>
75 #include <netinet/in.h>
76 #include <netinet/in_var.h>
77 #include <netinet/if_ether.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
80 #include <netinet/in_arp.h>
82 #include <sys/socketvar.h>
87 extern struct ifqueue pkintrq
;
91 #include <net/bridge.h>
94 /* #include "vlan.h" */
96 #include <net/if_vlan_var.h>
97 #endif /* NVLAN > 0 */
99 /* Local function declerations */
100 int ether_attach_inet(struct ifnet
*ifp
, u_long proto_family
);
101 int ether_detach_inet(struct ifnet
*ifp
, u_long proto_family
);
103 extern void * kdp_get_interface(void);
104 extern void ipintr(void);
105 extern void arp_input(struct mbuf
* m
);
108 inet_ether_arp_input(
111 struct ether_arp
*ea
;
112 struct sockaddr_dl sender_hw
;
113 struct sockaddr_in sender_ip
;
114 struct sockaddr_in target_ip
;
116 if (mbuf_len(m
) < sizeof(*ea
) &&
117 mbuf_pullup(&m
, sizeof(*ea
)) != 0)
122 /* Verify this is an ethernet/ip arp and address lengths are correct */
123 if (ntohs(ea
->arp_hrd
) != ARPHRD_ETHER
||
124 ntohs(ea
->arp_pro
) != ETHERTYPE_IP
||
125 ea
->arp_pln
!= sizeof(struct in_addr
) ||
126 ea
->arp_hln
!= ETHER_ADDR_LEN
) {
131 /* Verify the sender is not broadcast */
132 if (bcmp(ea
->arp_sha
, etherbroadcastaddr
, ETHER_ADDR_LEN
) == 0) {
137 bzero(&sender_ip
, sizeof(sender_ip
));
138 sender_ip
.sin_len
= sizeof(sender_ip
);
139 sender_ip
.sin_family
= AF_INET
;
140 sender_ip
.sin_addr
= *(struct in_addr
*)ea
->arp_spa
;
141 target_ip
= sender_ip
;
142 target_ip
.sin_addr
= *(struct in_addr
*)ea
->arp_tpa
;
144 bzero(&sender_hw
, sizeof(sender_hw
));
145 sender_hw
.sdl_len
= sizeof(sender_hw
);
146 sender_hw
.sdl_family
= AF_LINK
;
147 sender_hw
.sdl_type
= IFT_ETHER
;
148 sender_hw
.sdl_alen
= ETHER_ADDR_LEN
;
149 bcopy(ea
->arp_sha
, LLADDR(&sender_hw
), ETHER_ADDR_LEN
);
151 arp_ip_handle_input(mbuf_pkthdr_rcvif(m
), ntohs(ea
->arp_op
), &sender_hw
, &sender_ip
, &target_ip
);
156 * Process a received Ethernet packet;
157 * the packet is in the mbuf chain m without
158 * the ether header, which is provided separately.
162 __unused ifnet_t ifp
,
163 __unused protocol_family_t protocol_family
,
167 register struct ether_header
*eh
= (struct ether_header
*) frame_header
;
170 ether_type
= ntohs(eh
->ether_type
);
172 switch (ether_type
) {
175 proto_input(PF_INET
, m
);
178 case ETHERTYPE_ARP
: {
179 inet_ether_arp_input(m
);
192 inet_ether_pre_output(
194 __unused protocol_family_t protocol_family
,
196 const struct sockaddr
*dst_netaddr
,
201 register struct mbuf
*m
= *m0
;
202 register struct ether_header
*eh
;
206 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
210 * Tell ether_frameout it's ok to loop packet unless negated below.
212 m
->m_flags
|= M_LOOP
;
214 switch (dst_netaddr
->sa_family
) {
217 struct sockaddr_dl ll_dest
;
218 result
= arp_lookup_ip(ifp
, (const struct sockaddr_in
*)dst_netaddr
,
219 &ll_dest
, sizeof(ll_dest
), (route_t
)route
, *m0
);
221 bcopy(LLADDR(&ll_dest
), edst
, ETHER_ADDR_LEN
);
222 *(u_int16_t
*)type
= htons(ETHERTYPE_IP
);
227 case pseudo_AF_HDRCMPLT
:
229 m
->m_flags
&= ~M_LOOP
;
230 eh
= (struct ether_header
*)dst_netaddr
->sa_data
;
231 (void)memcpy(edst
, eh
->ether_dhost
, 6);
232 *(u_short
*)type
= eh
->ether_type
;
236 printf("%s%d: can't handle af%d\n", ifp
->if_name
, ifp
->if_unit
,
237 dst_netaddr
->sa_family
);
239 result
= EAFNOSUPPORT
;
246 ether_inet_resolve_multi(
248 const struct sockaddr
*proto_addr
,
249 struct sockaddr_dl
*out_ll
,
252 static const size_t minsize
= offsetof(struct sockaddr_dl
, sdl_data
[0]) + ETHER_ADDR_LEN
;
253 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)proto_addr
;
255 if (proto_addr
->sa_family
!= AF_INET
)
258 if (proto_addr
->sa_len
< sizeof(struct sockaddr_in
))
261 if (ll_len
< minsize
)
264 bzero(out_ll
, minsize
);
265 out_ll
->sdl_len
= minsize
;
266 out_ll
->sdl_family
= AF_LINK
;
267 out_ll
->sdl_index
= ifp
->if_index
;
268 out_ll
->sdl_type
= IFT_ETHER
;
269 out_ll
->sdl_nlen
= 0;
270 out_ll
->sdl_alen
= ETHER_ADDR_LEN
;
271 out_ll
->sdl_slen
= 0;
272 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, LLADDR(out_ll
));
279 ether_inet_prmod_ioctl(
281 __unused protocol_family_t protocol_family
,
286 struct ifreq
*ifr
= data
;
292 if ((ifnet_flags(ifp
) & IFF_RUNNING
) == 0) {
293 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
294 ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
297 switch (ifaddr_address_family(ifa
)) {
301 inet_arp_init_ifaddr(ifp
, ifa
);
303 * Register new IP and MAC addresses with the kernel debugger
304 * if the interface is the same as was registered by IOKernelDebugger. If
305 * no interface was registered, fall back and just match against en0 interface.
307 if ((kdp_get_interface() != 0 && kdp_get_interface() == ifp
->if_softc
)
308 || (kdp_get_interface() == 0 && ifp
->if_unit
== 0))
309 kdp_set_ip_and_mac_addresses(&(IA_SIN(ifa
)->sin_addr
), ifnet_lladdr(ifp
));
320 ifnet_lladdr_copy_bytes(ifp
, ifr
->ifr_addr
.sa_data
, ETHER_ADDR_LEN
);
325 * IOKit IONetworkFamily will set the right MTU according to the driver
340 __unused protocol_family_t protocol
,
341 const struct kev_msg
*event
)
345 if (event
->vendor_code
!= KEV_VENDOR_APPLE
||
346 event
->kev_class
!= KEV_NETWORK_CLASS
||
347 event
->kev_subclass
!= KEV_DL_SUBCLASS
||
348 event
->event_code
!= KEV_DL_LINK_ADDRESS_CHANGED
) {
352 if (ifnet_get_address_list_family(ifp
, &addresses
, AF_INET
) == 0) {
355 for (i
= 0; addresses
[i
] != NULL
; i
++) {
356 inet_arp_init_ifaddr(ifp
, addresses
[i
]);
359 ifnet_free_address_list(addresses
);
367 const struct sockaddr_dl
* sender_hw
,
368 const struct sockaddr
* sender_proto
,
369 const struct sockaddr_dl
* target_hw
,
370 const struct sockaddr
* target_proto
)
374 struct ether_header
*eh
;
375 struct ether_arp
*ea
;
376 const struct sockaddr_in
* sender_ip
= (const struct sockaddr_in
*)sender_proto
;
377 const struct sockaddr_in
* target_ip
= (const struct sockaddr_in
*)target_proto
;
380 if (target_ip
== NULL
)
383 if ((sender_ip
&& sender_ip
->sin_family
!= AF_INET
) ||
384 (target_ip
&& target_ip
->sin_family
!= AF_INET
))
387 result
= mbuf_gethdr(MBUF_WAITOK
, MBUF_TYPE_DATA
, &m
);
391 mbuf_setlen(m
, sizeof(*ea
));
392 mbuf_pkthdr_setlen(m
, sizeof(*ea
));
394 /* Move the data pointer in the mbuf to the end, aligned to 4 bytes */
395 datap
= mbuf_datastart(m
);
396 datap
+= mbuf_trailingspace(m
);
397 datap
-= (((u_long
)datap
) & 0x3);
398 mbuf_setdata(m
, datap
, sizeof(*ea
));
401 /* Prepend the ethernet header, we will send the raw frame */
402 mbuf_prepend(&m
, sizeof(*eh
), MBUF_WAITOK
);
404 eh
->ether_type
= htons(ETHERTYPE_ARP
);
406 /* Fill out the arp header */
407 ea
->arp_pro
= htons(ETHERTYPE_IP
);
408 ea
->arp_hln
= sizeof(ea
->arp_sha
);
409 ea
->arp_pln
= sizeof(ea
->arp_spa
);
410 ea
->arp_hrd
= htons(ARPHRD_ETHER
);
411 ea
->arp_op
= htons(arpop
);
413 /* Sender Hardware */
414 if (sender_hw
!= NULL
) {
415 bcopy(CONST_LLADDR(sender_hw
), ea
->arp_sha
, sizeof(ea
->arp_sha
));
418 ifnet_lladdr_copy_bytes(ifp
, ea
->arp_sha
, ETHER_ADDR_LEN
);
420 ifnet_lladdr_copy_bytes(ifp
, eh
->ether_shost
, sizeof(eh
->ether_shost
));
423 if (sender_ip
!= NULL
) {
424 bcopy(&sender_ip
->sin_addr
, ea
->arp_spa
, sizeof(ea
->arp_spa
));
429 /* Look for an IP address to use as our source */
430 ifnet_lock_shared(ifp
);
431 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
432 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET
)
436 bcopy(&((struct sockaddr_in
*)ifa
->ifa_addr
)->sin_addr
, ea
->arp_spa
,
437 sizeof(ea
->arp_spa
));
439 ifnet_lock_done(ifp
);
447 /* Target Hardware */
448 if (target_hw
== 0) {
449 bzero(ea
->arp_tha
, sizeof(ea
->arp_tha
));
450 bcopy(etherbroadcastaddr
, eh
->ether_dhost
, sizeof(eh
->ether_dhost
));
453 bcopy(CONST_LLADDR(target_hw
), ea
->arp_tha
, sizeof(ea
->arp_tha
));
454 bcopy(CONST_LLADDR(target_hw
), eh
->ether_dhost
, sizeof(eh
->ether_dhost
));
458 bcopy(&target_ip
->sin_addr
, ea
->arp_tpa
, sizeof(ea
->arp_tpa
));
460 ifnet_output_raw(ifp
, PF_INET
, m
);
468 __unused u_long proto_family
)
470 struct ifnet_attach_proto_param proto
;
471 struct ifnet_demux_desc demux
[2];
472 u_short en_native
=htons(ETHERTYPE_IP
);
473 u_short arp_native
=htons(ETHERTYPE_ARP
);
476 bzero(&demux
[0], sizeof(demux
));
477 demux
[0].type
= DLIL_DESC_ETYPE2
;
478 demux
[0].data
= &en_native
;
479 demux
[0].datalen
= sizeof(en_native
);
480 demux
[1].type
= DLIL_DESC_ETYPE2
;
481 demux
[1].data
= &arp_native
;
482 demux
[1].datalen
= sizeof(arp_native
);
484 bzero(&proto
, sizeof(proto
));
485 proto
.demux_list
= demux
;
486 proto
.demux_count
= sizeof(demux
) / sizeof(demux
[0]);
487 proto
.input
= inet_ether_input
;
488 proto
.pre_output
= inet_ether_pre_output
;
489 proto
.ioctl
= ether_inet_prmod_ioctl
;
490 proto
.event
= ether_inet_event
;
491 proto
.resolve
= ether_inet_resolve_multi
;
492 proto
.send_arp
= ether_inet_arp
;
494 error
= ifnet_attach_protocol(ifp
, proto_family
, &proto
);
495 if (error
&& error
!= EEXIST
) {
496 printf("WARNING: ether_attach_inet can't attach ip to %s%d\n",
497 ifp
->if_name
, ifp
->if_unit
);
509 stat
= dlil_detach_protocol(ifp
, proto_family
);