xref: /xnu-10002.1.13/bsd/net/ether_inet_pr_module.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions  * Copyright (c) 2000-2022 Apple Inc. All rights reserved.
3*1031c584SApple OSS Distributions  *
4*1031c584SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions  *
6*1031c584SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1031c584SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1031c584SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1031c584SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1031c584SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1031c584SApple OSS Distributions  *
15*1031c584SApple OSS Distributions  * Please obtain a copy of the License at
16*1031c584SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1031c584SApple OSS Distributions  *
18*1031c584SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1031c584SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1031c584SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1031c584SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1031c584SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1031c584SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1031c584SApple OSS Distributions  * limitations under the License.
25*1031c584SApple OSS Distributions  *
26*1031c584SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1031c584SApple OSS Distributions  */
28*1031c584SApple OSS Distributions /*
29*1031c584SApple OSS Distributions  * Copyright (c) 1982, 1989, 1993
30*1031c584SApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
31*1031c584SApple OSS Distributions  *
32*1031c584SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
33*1031c584SApple OSS Distributions  * modification, are permitted provided that the following conditions
34*1031c584SApple OSS Distributions  * are met:
35*1031c584SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
36*1031c584SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
37*1031c584SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
38*1031c584SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
39*1031c584SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
40*1031c584SApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
41*1031c584SApple OSS Distributions  *    must display the following acknowledgement:
42*1031c584SApple OSS Distributions  *	This product includes software developed by the University of
43*1031c584SApple OSS Distributions  *	California, Berkeley and its contributors.
44*1031c584SApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
45*1031c584SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
46*1031c584SApple OSS Distributions  *    without specific prior written permission.
47*1031c584SApple OSS Distributions  *
48*1031c584SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49*1031c584SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50*1031c584SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51*1031c584SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52*1031c584SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53*1031c584SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54*1031c584SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55*1031c584SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56*1031c584SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57*1031c584SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58*1031c584SApple OSS Distributions  * SUCH DAMAGE.
59*1031c584SApple OSS Distributions  *
60*1031c584SApple OSS Distributions  */
61*1031c584SApple OSS Distributions /*
62*1031c584SApple OSS Distributions  * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
63*1031c584SApple OSS Distributions  * support for mandatory and extensible security protections.  This notice
64*1031c584SApple OSS Distributions  * is included in support of clause 2.2 (b) of the Apple Public License,
65*1031c584SApple OSS Distributions  * Version 2.0.
66*1031c584SApple OSS Distributions  */
67*1031c584SApple OSS Distributions 
68*1031c584SApple OSS Distributions 
69*1031c584SApple OSS Distributions #include <sys/param.h>
70*1031c584SApple OSS Distributions #include <sys/systm.h>
71*1031c584SApple OSS Distributions #include <sys/kernel.h>
72*1031c584SApple OSS Distributions #include <sys/malloc.h>
73*1031c584SApple OSS Distributions #include <sys/mbuf.h>
74*1031c584SApple OSS Distributions #include <sys/socket.h>
75*1031c584SApple OSS Distributions #include <sys/sockio.h>
76*1031c584SApple OSS Distributions #include <sys/sysctl.h>
77*1031c584SApple OSS Distributions 
78*1031c584SApple OSS Distributions #include <net/dlil.h>
79*1031c584SApple OSS Distributions #include <net/if.h>
80*1031c584SApple OSS Distributions #include <net/route.h>
81*1031c584SApple OSS Distributions #include <net/if_llc.h>
82*1031c584SApple OSS Distributions #include <net/if_dl.h>
83*1031c584SApple OSS Distributions #include <net/if_types.h>
84*1031c584SApple OSS Distributions #include <net/kpi_protocol.h>
85*1031c584SApple OSS Distributions #include <netinet/in.h>
86*1031c584SApple OSS Distributions #include <netinet/in_var.h>
87*1031c584SApple OSS Distributions #include <netinet/if_ether.h>
88*1031c584SApple OSS Distributions #include <netinet/in_systm.h>
89*1031c584SApple OSS Distributions #include <netinet/ip.h>
90*1031c584SApple OSS Distributions #include <netinet/in_arp.h>
91*1031c584SApple OSS Distributions 
92*1031c584SApple OSS Distributions #include <sys/socketvar.h>
93*1031c584SApple OSS Distributions 
94*1031c584SApple OSS Distributions #include <net/dlil.h>
95*1031c584SApple OSS Distributions 
96*1031c584SApple OSS Distributions /* #include "vlan.h" */
97*1031c584SApple OSS Distributions #if NVLAN > 0
98*1031c584SApple OSS Distributions #include <net/if_vlan_var.h>
99*1031c584SApple OSS Distributions #endif /* NVLAN > 0 */
100*1031c584SApple OSS Distributions #include <net/ether_if_module.h>
101*1031c584SApple OSS Distributions #if CONFIG_MACF
102*1031c584SApple OSS Distributions #include <security/mac_framework.h>
103*1031c584SApple OSS Distributions #endif
104*1031c584SApple OSS Distributions 
105*1031c584SApple OSS Distributions /* Local function declarations */
106*1031c584SApple OSS Distributions extern void *kdp_get_interface(void);
107*1031c584SApple OSS Distributions extern void kdp_set_ip_and_mac_addresses(struct in_addr *ipaddr,
108*1031c584SApple OSS Distributions     struct ether_addr *macaddr);
109*1031c584SApple OSS Distributions 
110*1031c584SApple OSS Distributions #define _ip_copy(dst, src)      \
111*1031c584SApple OSS Distributions 	bcopy(src, dst, sizeof (struct in_addr))
112*1031c584SApple OSS Distributions 
113*1031c584SApple OSS Distributions static void
ether_inet_arp_input(struct ifnet * ifp,struct mbuf * m)114*1031c584SApple OSS Distributions ether_inet_arp_input(struct ifnet *ifp, struct mbuf *m)
115*1031c584SApple OSS Distributions {
116*1031c584SApple OSS Distributions 	struct ether_arp *ea;
117*1031c584SApple OSS Distributions 	struct sockaddr_dl      sender_hw;
118*1031c584SApple OSS Distributions 	struct sockaddr_in      sender_ip;
119*1031c584SApple OSS Distributions 	struct sockaddr_in      target_ip;
120*1031c584SApple OSS Distributions 
121*1031c584SApple OSS Distributions 	if (mbuf_len(m) < sizeof(*ea) && mbuf_pullup(&m, sizeof(*ea)) != 0) {
122*1031c584SApple OSS Distributions 		return;
123*1031c584SApple OSS Distributions 	}
124*1031c584SApple OSS Distributions 
125*1031c584SApple OSS Distributions 	ea = mbuf_data(m);
126*1031c584SApple OSS Distributions 
127*1031c584SApple OSS Distributions 	/* Verify this is an ethernet/ip arp and address lengths are correct */
128*1031c584SApple OSS Distributions 	if (ntohs(ea->arp_hrd) != ARPHRD_ETHER ||
129*1031c584SApple OSS Distributions 	    ntohs(ea->arp_pro) != ETHERTYPE_IP ||
130*1031c584SApple OSS Distributions 	    ea->arp_pln != sizeof(struct in_addr) ||
131*1031c584SApple OSS Distributions 	    ea->arp_hln != ETHER_ADDR_LEN) {
132*1031c584SApple OSS Distributions 		mbuf_freem(m);
133*1031c584SApple OSS Distributions 		return;
134*1031c584SApple OSS Distributions 	}
135*1031c584SApple OSS Distributions 
136*1031c584SApple OSS Distributions 	/* Verify the sender is not broadcast */
137*1031c584SApple OSS Distributions 	if (bcmp(ea->arp_sha, etherbroadcastaddr, ETHER_ADDR_LEN) == 0) {
138*1031c584SApple OSS Distributions 		mbuf_freem(m);
139*1031c584SApple OSS Distributions 		return;
140*1031c584SApple OSS Distributions 	}
141*1031c584SApple OSS Distributions 
142*1031c584SApple OSS Distributions 	bzero(&sender_ip, sizeof(sender_ip));
143*1031c584SApple OSS Distributions 	sender_ip.sin_len = sizeof(sender_ip);
144*1031c584SApple OSS Distributions 	sender_ip.sin_family = AF_INET;
145*1031c584SApple OSS Distributions 	_ip_copy(&sender_ip.sin_addr, ea->arp_spa);
146*1031c584SApple OSS Distributions 	target_ip = sender_ip;
147*1031c584SApple OSS Distributions 	_ip_copy(&target_ip.sin_addr, ea->arp_tpa);
148*1031c584SApple OSS Distributions 
149*1031c584SApple OSS Distributions 	bzero(&sender_hw, sizeof(sender_hw));
150*1031c584SApple OSS Distributions 	sender_hw.sdl_len = sizeof(sender_hw);
151*1031c584SApple OSS Distributions 	sender_hw.sdl_family = AF_LINK;
152*1031c584SApple OSS Distributions 	sender_hw.sdl_type = IFT_ETHER;
153*1031c584SApple OSS Distributions 	sender_hw.sdl_alen = ETHER_ADDR_LEN;
154*1031c584SApple OSS Distributions 	bcopy(ea->arp_sha, LLADDR(&sender_hw), ETHER_ADDR_LEN);
155*1031c584SApple OSS Distributions 
156*1031c584SApple OSS Distributions 	/* update L2 reachability record, if present */
157*1031c584SApple OSS Distributions 	arp_llreach_set_reachable(ifp, LLADDR(&sender_hw), ETHER_ADDR_LEN);
158*1031c584SApple OSS Distributions 
159*1031c584SApple OSS Distributions 	arp_ip_handle_input(ifp, ntohs(ea->arp_op), &sender_hw, &sender_ip,
160*1031c584SApple OSS Distributions 	    &target_ip);
161*1031c584SApple OSS Distributions 	mbuf_freem(m);
162*1031c584SApple OSS Distributions }
163*1031c584SApple OSS Distributions 
164*1031c584SApple OSS Distributions /*
165*1031c584SApple OSS Distributions  * Process a received Ethernet packet;
166*1031c584SApple OSS Distributions  * the packet is in the mbuf chain m without
167*1031c584SApple OSS Distributions  * the ether header, which is provided separately.
168*1031c584SApple OSS Distributions  */
169*1031c584SApple OSS Distributions static errno_t
ether_inet_input(ifnet_t ifp,protocol_family_t protocol_family,mbuf_t m_list)170*1031c584SApple OSS Distributions ether_inet_input(ifnet_t ifp, protocol_family_t protocol_family,
171*1031c584SApple OSS Distributions     mbuf_t m_list)
172*1031c584SApple OSS Distributions {
173*1031c584SApple OSS Distributions #pragma unused(ifp, protocol_family)
174*1031c584SApple OSS Distributions 	mbuf_t  m;
175*1031c584SApple OSS Distributions 	mbuf_t  *tailptr = &m_list;
176*1031c584SApple OSS Distributions 	mbuf_t  nextpkt;
177*1031c584SApple OSS Distributions 
178*1031c584SApple OSS Distributions 	/* Strip ARP and non-IP packets out of the list */
179*1031c584SApple OSS Distributions 	for (m = m_list; m; m = nextpkt) {
180*1031c584SApple OSS Distributions 		struct ether_header *eh = mbuf_pkthdr_header(m);
181*1031c584SApple OSS Distributions 		struct ifnet *mifp;
182*1031c584SApple OSS Distributions 
183*1031c584SApple OSS Distributions 		/*
184*1031c584SApple OSS Distributions 		 * Trust the ifp in the mbuf, rather than ifproto's
185*1031c584SApple OSS Distributions 		 * since the packet could have been injected via
186*1031c584SApple OSS Distributions 		 * a dlil_input_packet_list() using an ifp that is
187*1031c584SApple OSS Distributions 		 * different than the one where the packet really
188*1031c584SApple OSS Distributions 		 * came from.
189*1031c584SApple OSS Distributions 		 */
190*1031c584SApple OSS Distributions 		mifp = mbuf_pkthdr_rcvif(m);
191*1031c584SApple OSS Distributions 
192*1031c584SApple OSS Distributions 		nextpkt = m->m_nextpkt;
193*1031c584SApple OSS Distributions 
194*1031c584SApple OSS Distributions 		if (eh->ether_type == htons(ETHERTYPE_IP)) {
195*1031c584SApple OSS Distributions 			/*
196*1031c584SApple OSS Distributions 			 * Update L2 reachability record, if present
197*1031c584SApple OSS Distributions 			 * (and if not a broadcast sender).
198*1031c584SApple OSS Distributions 			 */
199*1031c584SApple OSS Distributions 			if (bcmp(eh->ether_shost, etherbroadcastaddr,
200*1031c584SApple OSS Distributions 			    ETHER_ADDR_LEN) != 0) {
201*1031c584SApple OSS Distributions 				arp_llreach_set_reachable(mifp, eh->ether_shost,
202*1031c584SApple OSS Distributions 				    ETHER_ADDR_LEN);
203*1031c584SApple OSS Distributions 			}
204*1031c584SApple OSS Distributions 			/* put this packet in the list */
205*1031c584SApple OSS Distributions 			*tailptr = m;
206*1031c584SApple OSS Distributions 			tailptr = &m->m_nextpkt;
207*1031c584SApple OSS Distributions 		} else {
208*1031c584SApple OSS Distributions 			/* Pass ARP packets to arp input */
209*1031c584SApple OSS Distributions 			m->m_nextpkt = NULL;
210*1031c584SApple OSS Distributions 			if (eh->ether_type == htons(ETHERTYPE_ARP)) {
211*1031c584SApple OSS Distributions 				ether_inet_arp_input(mifp, m);
212*1031c584SApple OSS Distributions 			} else {
213*1031c584SApple OSS Distributions 				mbuf_freem(m);
214*1031c584SApple OSS Distributions 			}
215*1031c584SApple OSS Distributions 		}
216*1031c584SApple OSS Distributions 	}
217*1031c584SApple OSS Distributions 
218*1031c584SApple OSS Distributions 	*tailptr = NULL;
219*1031c584SApple OSS Distributions 
220*1031c584SApple OSS Distributions 	/* Pass IP list to ip input */
221*1031c584SApple OSS Distributions 	if (m_list != NULL && proto_input(PF_INET, m_list) != 0) {
222*1031c584SApple OSS Distributions 		mbuf_freem_list(m_list);
223*1031c584SApple OSS Distributions 	}
224*1031c584SApple OSS Distributions 
225*1031c584SApple OSS Distributions 	return EJUSTRETURN;
226*1031c584SApple OSS Distributions }
227*1031c584SApple OSS Distributions 
228*1031c584SApple OSS Distributions static errno_t
ether_inet_pre_output(ifnet_t ifp,protocol_family_t protocol_family,mbuf_t * m0,const struct sockaddr * dst_netaddr,void * route,char * type,char * edst)229*1031c584SApple OSS Distributions ether_inet_pre_output(ifnet_t ifp, protocol_family_t protocol_family,
230*1031c584SApple OSS Distributions     mbuf_t *m0, const struct sockaddr *dst_netaddr,
231*1031c584SApple OSS Distributions     void *route, char *type, char *edst)
232*1031c584SApple OSS Distributions {
233*1031c584SApple OSS Distributions #pragma unused(protocol_family)
234*1031c584SApple OSS Distributions 	struct mbuf *m = *m0;
235*1031c584SApple OSS Distributions 	const struct ether_header *eh;
236*1031c584SApple OSS Distributions 	errno_t result = 0;
237*1031c584SApple OSS Distributions 
238*1031c584SApple OSS Distributions 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING)) {
239*1031c584SApple OSS Distributions 		return ENETDOWN;
240*1031c584SApple OSS Distributions 	}
241*1031c584SApple OSS Distributions 
242*1031c584SApple OSS Distributions 	/*
243*1031c584SApple OSS Distributions 	 * Tell ether_frameout it's ok to loop packet unless negated below.
244*1031c584SApple OSS Distributions 	 */
245*1031c584SApple OSS Distributions 	m->m_flags |= M_LOOP;
246*1031c584SApple OSS Distributions 
247*1031c584SApple OSS Distributions 	switch (dst_netaddr->sa_family) {
248*1031c584SApple OSS Distributions 	case AF_INET: {
249*1031c584SApple OSS Distributions 		struct sockaddr_dl ll_dest = {};
250*1031c584SApple OSS Distributions 
251*1031c584SApple OSS Distributions 		result = arp_lookup_ip(ifp,
252*1031c584SApple OSS Distributions 		    (const struct sockaddr_in *)(uintptr_t)(size_t)dst_netaddr,
253*1031c584SApple OSS Distributions 		    &ll_dest, sizeof(ll_dest), (route_t)route, *m0);
254*1031c584SApple OSS Distributions 		if (result == 0) {
255*1031c584SApple OSS Distributions 			u_int16_t ethertype_ip = htons(ETHERTYPE_IP);
256*1031c584SApple OSS Distributions 
257*1031c584SApple OSS Distributions 			bcopy(LLADDR(&ll_dest), edst, ETHER_ADDR_LEN);
258*1031c584SApple OSS Distributions 			bcopy(&ethertype_ip, type, sizeof(ethertype_ip));
259*1031c584SApple OSS Distributions 		}
260*1031c584SApple OSS Distributions 		break;
261*1031c584SApple OSS Distributions 	}
262*1031c584SApple OSS Distributions 
263*1031c584SApple OSS Distributions 	case pseudo_AF_HDRCMPLT:
264*1031c584SApple OSS Distributions 	case AF_UNSPEC:
265*1031c584SApple OSS Distributions 		m->m_flags &= ~M_LOOP;
266*1031c584SApple OSS Distributions 		eh = (const struct ether_header *)(uintptr_t)(size_t)
267*1031c584SApple OSS Distributions 		    dst_netaddr->sa_data;
268*1031c584SApple OSS Distributions 		(void) memcpy(edst, eh->ether_dhost, 6);
269*1031c584SApple OSS Distributions 		bcopy(&eh->ether_type, type, sizeof(u_short));
270*1031c584SApple OSS Distributions 		break;
271*1031c584SApple OSS Distributions 
272*1031c584SApple OSS Distributions 	default:
273*1031c584SApple OSS Distributions 		printf("%s: can't handle af%d\n", if_name(ifp),
274*1031c584SApple OSS Distributions 		    dst_netaddr->sa_family);
275*1031c584SApple OSS Distributions 
276*1031c584SApple OSS Distributions 		result = EAFNOSUPPORT;
277*1031c584SApple OSS Distributions 		break;
278*1031c584SApple OSS Distributions 	}
279*1031c584SApple OSS Distributions 
280*1031c584SApple OSS Distributions 	return result;
281*1031c584SApple OSS Distributions }
282*1031c584SApple OSS Distributions 
283*1031c584SApple OSS Distributions static errno_t
ether_inet_resolve_multi(ifnet_t ifp,const struct sockaddr * proto_addr,struct sockaddr_dl * out_ll,size_t ll_len)284*1031c584SApple OSS Distributions ether_inet_resolve_multi(ifnet_t ifp, const struct sockaddr *proto_addr,
285*1031c584SApple OSS Distributions     struct sockaddr_dl *out_ll, size_t ll_len)
286*1031c584SApple OSS Distributions {
287*1031c584SApple OSS Distributions 	static const size_t minsize =
288*1031c584SApple OSS Distributions 	    offsetof(struct sockaddr_dl, sdl_data[0]) + ETHER_ADDR_LEN;
289*1031c584SApple OSS Distributions 	const struct sockaddr_in *sin =
290*1031c584SApple OSS Distributions 	    (const struct sockaddr_in *)(uintptr_t)(size_t)proto_addr;
291*1031c584SApple OSS Distributions 
292*1031c584SApple OSS Distributions 	if (proto_addr->sa_family != AF_INET) {
293*1031c584SApple OSS Distributions 		return EAFNOSUPPORT;
294*1031c584SApple OSS Distributions 	}
295*1031c584SApple OSS Distributions 
296*1031c584SApple OSS Distributions 	if (proto_addr->sa_len < sizeof(struct sockaddr_in)) {
297*1031c584SApple OSS Distributions 		return EINVAL;
298*1031c584SApple OSS Distributions 	}
299*1031c584SApple OSS Distributions 
300*1031c584SApple OSS Distributions 	if (ll_len < minsize) {
301*1031c584SApple OSS Distributions 		return EMSGSIZE;
302*1031c584SApple OSS Distributions 	}
303*1031c584SApple OSS Distributions 
304*1031c584SApple OSS Distributions 	bzero(out_ll, minsize);
305*1031c584SApple OSS Distributions 	out_ll->sdl_len = minsize;
306*1031c584SApple OSS Distributions 	out_ll->sdl_family = AF_LINK;
307*1031c584SApple OSS Distributions 	out_ll->sdl_index = ifp->if_index;
308*1031c584SApple OSS Distributions 	out_ll->sdl_type = IFT_ETHER;
309*1031c584SApple OSS Distributions 	out_ll->sdl_nlen = 0;
310*1031c584SApple OSS Distributions 	out_ll->sdl_alen = ETHER_ADDR_LEN;
311*1031c584SApple OSS Distributions 	out_ll->sdl_slen = 0;
312*1031c584SApple OSS Distributions 	ETHER_MAP_IP_MULTICAST(&sin->sin_addr, LLADDR(out_ll));
313*1031c584SApple OSS Distributions 
314*1031c584SApple OSS Distributions 	return 0;
315*1031c584SApple OSS Distributions }
316*1031c584SApple OSS Distributions 
317*1031c584SApple OSS Distributions static errno_t
ether_inet_prmod_ioctl(ifnet_t ifp,protocol_family_t protocol_family,u_long command,void * data)318*1031c584SApple OSS Distributions ether_inet_prmod_ioctl(ifnet_t ifp, protocol_family_t protocol_family,
319*1031c584SApple OSS Distributions     u_long command, void *data)
320*1031c584SApple OSS Distributions {
321*1031c584SApple OSS Distributions #pragma unused(protocol_family)
322*1031c584SApple OSS Distributions 	int error = 0;
323*1031c584SApple OSS Distributions 
324*1031c584SApple OSS Distributions 	switch (command) {
325*1031c584SApple OSS Distributions 	case SIOCSIFADDR:               /* struct ifaddr pointer */
326*1031c584SApple OSS Distributions 	case SIOCAIFADDR: {             /* struct ifaddr pointer */
327*1031c584SApple OSS Distributions 		/*
328*1031c584SApple OSS Distributions 		 * Note: caller of ifnet_ioctl() passes in pointer to
329*1031c584SApple OSS Distributions 		 * struct ifaddr as parameter to SIOC{A,S}IFADDR, for
330*1031c584SApple OSS Distributions 		 * legacy reasons.
331*1031c584SApple OSS Distributions 		 */
332*1031c584SApple OSS Distributions 		struct ifaddr *ifa = data;
333*1031c584SApple OSS Distributions 
334*1031c584SApple OSS Distributions 		if (!(ifnet_flags(ifp) & IFF_RUNNING)) {
335*1031c584SApple OSS Distributions 			ifnet_set_flags(ifp, IFF_UP, IFF_UP);
336*1031c584SApple OSS Distributions 			ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
337*1031c584SApple OSS Distributions 		}
338*1031c584SApple OSS Distributions 
339*1031c584SApple OSS Distributions 		if (ifaddr_address_family(ifa) != AF_INET) {
340*1031c584SApple OSS Distributions 			break;
341*1031c584SApple OSS Distributions 		}
342*1031c584SApple OSS Distributions 
343*1031c584SApple OSS Distributions 		inet_arp_init_ifaddr(ifp, ifa);
344*1031c584SApple OSS Distributions 
345*1031c584SApple OSS Distributions 		if (command != SIOCSIFADDR) {
346*1031c584SApple OSS Distributions 			break;
347*1031c584SApple OSS Distributions 		}
348*1031c584SApple OSS Distributions 
349*1031c584SApple OSS Distributions 		/*
350*1031c584SApple OSS Distributions 		 * Register new IP and MAC addresses with the kernel
351*1031c584SApple OSS Distributions 		 * debugger if the interface is the same as was registered
352*1031c584SApple OSS Distributions 		 * by IOKernelDebugger. If no interface was registered,
353*1031c584SApple OSS Distributions 		 * fall back and just match against en0 interface.
354*1031c584SApple OSS Distributions 		 * Do this only for the first address of the interface
355*1031c584SApple OSS Distributions 		 * and not for aliases.
356*1031c584SApple OSS Distributions 		 */
357*1031c584SApple OSS Distributions 		if ((kdp_get_interface() != 0 &&
358*1031c584SApple OSS Distributions 		    kdp_get_interface() == ifp->if_softc) ||
359*1031c584SApple OSS Distributions 		    (kdp_get_interface() == 0 && ifp->if_unit == 0)) {
360*1031c584SApple OSS Distributions 			kdp_set_ip_and_mac_addresses(&(IA_SIN(ifa)->sin_addr),
361*1031c584SApple OSS Distributions 			    (struct ether_addr *)IF_LLADDR(ifp));
362*1031c584SApple OSS Distributions 		}
363*1031c584SApple OSS Distributions 		break;
364*1031c584SApple OSS Distributions 	}
365*1031c584SApple OSS Distributions 
366*1031c584SApple OSS Distributions 	case SIOCGIFADDR: {             /* struct ifreq */
367*1031c584SApple OSS Distributions 		struct ifreq *ifr = data;
368*1031c584SApple OSS Distributions 		ifnet_guarded_lladdr_copy_bytes(ifp, ifr->ifr_addr.sa_data,
369*1031c584SApple OSS Distributions 		    ETHER_ADDR_LEN);
370*1031c584SApple OSS Distributions 		break;
371*1031c584SApple OSS Distributions 	}
372*1031c584SApple OSS Distributions 
373*1031c584SApple OSS Distributions 	default:
374*1031c584SApple OSS Distributions 		error = EOPNOTSUPP;
375*1031c584SApple OSS Distributions 		break;
376*1031c584SApple OSS Distributions 	}
377*1031c584SApple OSS Distributions 
378*1031c584SApple OSS Distributions 	return error;
379*1031c584SApple OSS Distributions }
380*1031c584SApple OSS Distributions 
381*1031c584SApple OSS Distributions static void
ether_inet_event(ifnet_t ifp,protocol_family_t protocol,const struct kev_msg * event)382*1031c584SApple OSS Distributions ether_inet_event(ifnet_t ifp, protocol_family_t protocol,
383*1031c584SApple OSS Distributions     const struct kev_msg *event)
384*1031c584SApple OSS Distributions {
385*1031c584SApple OSS Distributions #pragma unused(protocol)
386*1031c584SApple OSS Distributions 	ifaddr_t *addresses;
387*1031c584SApple OSS Distributions 
388*1031c584SApple OSS Distributions 	if (event->vendor_code != KEV_VENDOR_APPLE ||
389*1031c584SApple OSS Distributions 	    event->kev_class != KEV_NETWORK_CLASS ||
390*1031c584SApple OSS Distributions 	    event->kev_subclass != KEV_DL_SUBCLASS ||
391*1031c584SApple OSS Distributions 	    event->event_code != KEV_DL_LINK_ADDRESS_CHANGED) {
392*1031c584SApple OSS Distributions 		return;
393*1031c584SApple OSS Distributions 	}
394*1031c584SApple OSS Distributions 
395*1031c584SApple OSS Distributions 	if (ifnet_get_address_list_family(ifp, &addresses, AF_INET) == 0) {
396*1031c584SApple OSS Distributions 		int i;
397*1031c584SApple OSS Distributions 
398*1031c584SApple OSS Distributions 		for (i = 0; addresses[i] != NULL; i++) {
399*1031c584SApple OSS Distributions 			inet_arp_init_ifaddr(ifp, addresses[i]);
400*1031c584SApple OSS Distributions 		}
401*1031c584SApple OSS Distributions 
402*1031c584SApple OSS Distributions 		ifnet_free_address_list(addresses);
403*1031c584SApple OSS Distributions 	}
404*1031c584SApple OSS Distributions }
405*1031c584SApple OSS Distributions 
406*1031c584SApple OSS Distributions static errno_t
ether_inet_arp(ifnet_t ifp,u_short arpop,const struct sockaddr_dl * sender_hw,const struct sockaddr * sender_proto,const struct sockaddr_dl * target_hw,const struct sockaddr * target_proto)407*1031c584SApple OSS Distributions ether_inet_arp(ifnet_t ifp, u_short arpop, const struct sockaddr_dl *sender_hw,
408*1031c584SApple OSS Distributions     const struct sockaddr *sender_proto, const struct sockaddr_dl *target_hw,
409*1031c584SApple OSS Distributions     const struct sockaddr *target_proto)
410*1031c584SApple OSS Distributions {
411*1031c584SApple OSS Distributions 	mbuf_t  m;
412*1031c584SApple OSS Distributions 	errno_t result;
413*1031c584SApple OSS Distributions 	struct ether_header *eh;
414*1031c584SApple OSS Distributions 	struct ether_arp *ea;
415*1031c584SApple OSS Distributions 	const struct sockaddr_in *sender_ip =
416*1031c584SApple OSS Distributions 	    (const struct sockaddr_in *)(uintptr_t)(size_t)sender_proto;
417*1031c584SApple OSS Distributions 	const struct sockaddr_inarp *target_ip =
418*1031c584SApple OSS Distributions 	    (const struct sockaddr_inarp *)(uintptr_t)(size_t)target_proto;
419*1031c584SApple OSS Distributions 	char *datap;
420*1031c584SApple OSS Distributions 
421*1031c584SApple OSS Distributions 	if (target_ip == NULL) {
422*1031c584SApple OSS Distributions 		return EINVAL;
423*1031c584SApple OSS Distributions 	}
424*1031c584SApple OSS Distributions 
425*1031c584SApple OSS Distributions 	if ((sender_ip && sender_ip->sin_family != AF_INET) ||
426*1031c584SApple OSS Distributions 	    target_ip->sin_family != AF_INET) {
427*1031c584SApple OSS Distributions 		return EAFNOSUPPORT;
428*1031c584SApple OSS Distributions 	}
429*1031c584SApple OSS Distributions 
430*1031c584SApple OSS Distributions 	result = mbuf_gethdr(MBUF_DONTWAIT, MBUF_TYPE_DATA, &m);
431*1031c584SApple OSS Distributions 	if (result != 0) {
432*1031c584SApple OSS Distributions 		return result;
433*1031c584SApple OSS Distributions 	}
434*1031c584SApple OSS Distributions 
435*1031c584SApple OSS Distributions 	mbuf_setlen(m, sizeof(*ea));
436*1031c584SApple OSS Distributions 	mbuf_pkthdr_setlen(m, sizeof(*ea));
437*1031c584SApple OSS Distributions 
438*1031c584SApple OSS Distributions 	/* Move the data pointer in the mbuf to the end, aligned to 4 bytes */
439*1031c584SApple OSS Distributions 	datap = mbuf_datastart(m);
440*1031c584SApple OSS Distributions 	datap += mbuf_trailingspace(m);
441*1031c584SApple OSS Distributions 	datap -= (((uintptr_t)datap) & 0x3);
442*1031c584SApple OSS Distributions 	mbuf_setdata(m, datap, sizeof(*ea));
443*1031c584SApple OSS Distributions 	ea = mbuf_data(m);
444*1031c584SApple OSS Distributions 
445*1031c584SApple OSS Distributions 	/*
446*1031c584SApple OSS Distributions 	 * Prepend the ethernet header, we will send the raw frame;
447*1031c584SApple OSS Distributions 	 * callee frees the original mbuf when allocation fails.
448*1031c584SApple OSS Distributions 	 */
449*1031c584SApple OSS Distributions 	result = mbuf_prepend(&m, sizeof(*eh), MBUF_DONTWAIT);
450*1031c584SApple OSS Distributions 	if (result != 0) {
451*1031c584SApple OSS Distributions 		return result;
452*1031c584SApple OSS Distributions 	}
453*1031c584SApple OSS Distributions 
454*1031c584SApple OSS Distributions 	eh = mbuf_data(m);
455*1031c584SApple OSS Distributions 	eh->ether_type = htons(ETHERTYPE_ARP);
456*1031c584SApple OSS Distributions 
457*1031c584SApple OSS Distributions 	/* Fill out the arp header */
458*1031c584SApple OSS Distributions 	ea->arp_pro = htons(ETHERTYPE_IP);
459*1031c584SApple OSS Distributions 	ea->arp_hln = sizeof(ea->arp_sha);
460*1031c584SApple OSS Distributions 	ea->arp_pln = sizeof(ea->arp_spa);
461*1031c584SApple OSS Distributions 	ea->arp_hrd = htons(ARPHRD_ETHER);
462*1031c584SApple OSS Distributions 	ea->arp_op = htons(arpop);
463*1031c584SApple OSS Distributions 
464*1031c584SApple OSS Distributions 	/* Sender Hardware */
465*1031c584SApple OSS Distributions 	if (sender_hw != NULL) {
466*1031c584SApple OSS Distributions 		bcopy(CONST_LLADDR(sender_hw), ea->arp_sha,
467*1031c584SApple OSS Distributions 		    sizeof(ea->arp_sha));
468*1031c584SApple OSS Distributions 	} else {
469*1031c584SApple OSS Distributions 		ifnet_lladdr_copy_bytes(ifp, ea->arp_sha, ETHER_ADDR_LEN);
470*1031c584SApple OSS Distributions 	}
471*1031c584SApple OSS Distributions 	ifnet_lladdr_copy_bytes(ifp, eh->ether_shost, sizeof(eh->ether_shost));
472*1031c584SApple OSS Distributions 
473*1031c584SApple OSS Distributions 	/* Sender IP */
474*1031c584SApple OSS Distributions 	if (sender_ip != NULL) {
475*1031c584SApple OSS Distributions 		bcopy(&sender_ip->sin_addr, ea->arp_spa, sizeof(ea->arp_spa));
476*1031c584SApple OSS Distributions 	} else {
477*1031c584SApple OSS Distributions 		struct ifaddr *ifa;
478*1031c584SApple OSS Distributions 
479*1031c584SApple OSS Distributions 		/* Look for an IP address to use as our source */
480*1031c584SApple OSS Distributions 		ifnet_lock_shared(ifp);
481*1031c584SApple OSS Distributions 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
482*1031c584SApple OSS Distributions 			IFA_LOCK(ifa);
483*1031c584SApple OSS Distributions 			if (ifa->ifa_addr != NULL &&
484*1031c584SApple OSS Distributions 			    ifa->ifa_addr->sa_family == AF_INET) {
485*1031c584SApple OSS Distributions 				bcopy(&((struct sockaddr_in *)(void *)
486*1031c584SApple OSS Distributions 				    ifa->ifa_addr)->sin_addr, ea->arp_spa,
487*1031c584SApple OSS Distributions 				    sizeof(ea->arp_spa));
488*1031c584SApple OSS Distributions 				IFA_UNLOCK(ifa);
489*1031c584SApple OSS Distributions 				break;
490*1031c584SApple OSS Distributions 			}
491*1031c584SApple OSS Distributions 			IFA_UNLOCK(ifa);
492*1031c584SApple OSS Distributions 		}
493*1031c584SApple OSS Distributions 		ifnet_lock_done(ifp);
494*1031c584SApple OSS Distributions 
495*1031c584SApple OSS Distributions 		if (ifa == NULL) {
496*1031c584SApple OSS Distributions 			mbuf_freem(m);
497*1031c584SApple OSS Distributions 			return ENXIO;
498*1031c584SApple OSS Distributions 		}
499*1031c584SApple OSS Distributions 	}
500*1031c584SApple OSS Distributions 
501*1031c584SApple OSS Distributions 	/* Target Hardware */
502*1031c584SApple OSS Distributions 	if (target_hw == NULL) {
503*1031c584SApple OSS Distributions 		bzero(ea->arp_tha, sizeof(ea->arp_tha));
504*1031c584SApple OSS Distributions 		bcopy(etherbroadcastaddr, eh->ether_dhost,
505*1031c584SApple OSS Distributions 		    sizeof(eh->ether_dhost));
506*1031c584SApple OSS Distributions 		m->m_flags |= M_BCAST;
507*1031c584SApple OSS Distributions 	} else {
508*1031c584SApple OSS Distributions 		bcopy(CONST_LLADDR(target_hw), ea->arp_tha,
509*1031c584SApple OSS Distributions 		    sizeof(ea->arp_tha));
510*1031c584SApple OSS Distributions 		bcopy(CONST_LLADDR(target_hw), eh->ether_dhost,
511*1031c584SApple OSS Distributions 		    sizeof(eh->ether_dhost));
512*1031c584SApple OSS Distributions 
513*1031c584SApple OSS Distributions 		if (bcmp(eh->ether_dhost, etherbroadcastaddr,
514*1031c584SApple OSS Distributions 		    ETHER_ADDR_LEN) == 0) {
515*1031c584SApple OSS Distributions 			m->m_flags |= M_BCAST;
516*1031c584SApple OSS Distributions 		}
517*1031c584SApple OSS Distributions 	}
518*1031c584SApple OSS Distributions 
519*1031c584SApple OSS Distributions 	/* Target IP */
520*1031c584SApple OSS Distributions 	bcopy(&target_ip->sin_addr, ea->arp_tpa, sizeof(ea->arp_tpa));
521*1031c584SApple OSS Distributions 
522*1031c584SApple OSS Distributions 	/*
523*1031c584SApple OSS Distributions 	 * PKTF_{INET,INET6}_RESOLVE_RTR are mutually exclusive, so make
524*1031c584SApple OSS Distributions 	 * sure only one of them is set (just in case.)
525*1031c584SApple OSS Distributions 	 */
526*1031c584SApple OSS Distributions 	m->m_pkthdr.pkt_flags &= ~(PKTF_INET6_RESOLVE | PKTF_RESOLVE_RTR);
527*1031c584SApple OSS Distributions 	m->m_pkthdr.pkt_flags |= PKTF_INET_RESOLVE;
528*1031c584SApple OSS Distributions 	/*
529*1031c584SApple OSS Distributions 	 * If this is an ARP request for a (default) router, mark
530*1031c584SApple OSS Distributions 	 * the packet accordingly so that the driver can find out,
531*1031c584SApple OSS Distributions 	 * in case it needs to perform driver-specific action(s).
532*1031c584SApple OSS Distributions 	 */
533*1031c584SApple OSS Distributions 	if (arpop == ARPOP_REQUEST && (target_ip->sin_other & SIN_ROUTER)) {
534*1031c584SApple OSS Distributions 		m->m_pkthdr.pkt_flags |= PKTF_RESOLVE_RTR;
535*1031c584SApple OSS Distributions 	}
536*1031c584SApple OSS Distributions 
537*1031c584SApple OSS Distributions 	if (ifp->if_eflags & IFEF_TXSTART) {
538*1031c584SApple OSS Distributions 		/*
539*1031c584SApple OSS Distributions 		 * Use control service class if the interface
540*1031c584SApple OSS Distributions 		 * supports transmit-start model
541*1031c584SApple OSS Distributions 		 */
542*1031c584SApple OSS Distributions 		(void) m_set_service_class(m, MBUF_SC_CTL);
543*1031c584SApple OSS Distributions 	}
544*1031c584SApple OSS Distributions 
545*1031c584SApple OSS Distributions 	ifnet_output_raw(ifp, IS_INTF_CLAT46(ifp) ? 0 : AF_INET, m);
546*1031c584SApple OSS Distributions 
547*1031c584SApple OSS Distributions 	return 0;
548*1031c584SApple OSS Distributions }
549*1031c584SApple OSS Distributions 
550*1031c584SApple OSS Distributions errno_t
ether_attach_inet(struct ifnet * ifp,protocol_family_t proto_family)551*1031c584SApple OSS Distributions ether_attach_inet(struct ifnet *ifp, protocol_family_t proto_family)
552*1031c584SApple OSS Distributions {
553*1031c584SApple OSS Distributions #pragma unused(proto_family)
554*1031c584SApple OSS Distributions 	struct ifnet_attach_proto_param_v2 proto;
555*1031c584SApple OSS Distributions 	struct ifnet_demux_desc demux[2];
556*1031c584SApple OSS Distributions 	u_short en_native = htons(ETHERTYPE_IP);
557*1031c584SApple OSS Distributions 	u_short arp_native = htons(ETHERTYPE_ARP);
558*1031c584SApple OSS Distributions 	errno_t error;
559*1031c584SApple OSS Distributions 
560*1031c584SApple OSS Distributions 	bzero(&demux[0], sizeof(demux));
561*1031c584SApple OSS Distributions 	demux[0].type = DLIL_DESC_ETYPE2;
562*1031c584SApple OSS Distributions 	demux[0].data = &en_native;
563*1031c584SApple OSS Distributions 	demux[0].datalen = sizeof(en_native);
564*1031c584SApple OSS Distributions 	demux[1].type = DLIL_DESC_ETYPE2;
565*1031c584SApple OSS Distributions 	demux[1].data = &arp_native;
566*1031c584SApple OSS Distributions 	demux[1].datalen = sizeof(arp_native);
567*1031c584SApple OSS Distributions 
568*1031c584SApple OSS Distributions 	bzero(&proto, sizeof(proto));
569*1031c584SApple OSS Distributions 	proto.demux_list = demux;
570*1031c584SApple OSS Distributions 	proto.demux_count = sizeof(demux) / sizeof(demux[0]);
571*1031c584SApple OSS Distributions 	proto.input = ether_inet_input;
572*1031c584SApple OSS Distributions 	proto.pre_output = ether_inet_pre_output;
573*1031c584SApple OSS Distributions 	proto.ioctl = ether_inet_prmod_ioctl;
574*1031c584SApple OSS Distributions 	proto.event = ether_inet_event;
575*1031c584SApple OSS Distributions 	proto.resolve = ether_inet_resolve_multi;
576*1031c584SApple OSS Distributions 	proto.send_arp = ether_inet_arp;
577*1031c584SApple OSS Distributions 
578*1031c584SApple OSS Distributions 	error = ifnet_attach_protocol_v2(ifp, proto_family, &proto);
579*1031c584SApple OSS Distributions 	if (error && error != EEXIST) {
580*1031c584SApple OSS Distributions 		printf("WARNING: %s can't attach ip to %s\n", __func__,
581*1031c584SApple OSS Distributions 		    if_name(ifp));
582*1031c584SApple OSS Distributions 	}
583*1031c584SApple OSS Distributions 	return error;
584*1031c584SApple OSS Distributions }
585*1031c584SApple OSS Distributions 
586*1031c584SApple OSS Distributions void
ether_detach_inet(struct ifnet * ifp,protocol_family_t proto_family)587*1031c584SApple OSS Distributions ether_detach_inet(struct ifnet *ifp, protocol_family_t proto_family)
588*1031c584SApple OSS Distributions {
589*1031c584SApple OSS Distributions 	(void) ifnet_detach_protocol(ifp, proto_family);
590*1031c584SApple OSS Distributions }
591