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