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