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