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