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