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