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