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