1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions * Copyright (c) 2000-2021 Apple Inc. All rights reserved.
3*1031c584SApple OSS Distributions *
4*1031c584SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions *
6*1031c584SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*1031c584SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*1031c584SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*1031c584SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*1031c584SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*1031c584SApple OSS Distributions *
15*1031c584SApple OSS Distributions * Please obtain a copy of the License at
16*1031c584SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1031c584SApple OSS Distributions *
18*1031c584SApple OSS Distributions * The Original Code and all software distributed under the License are
19*1031c584SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1031c584SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1031c584SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1031c584SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1031c584SApple OSS Distributions * Please see the License for the specific language governing rights and
24*1031c584SApple OSS Distributions * limitations under the License.
25*1031c584SApple OSS Distributions *
26*1031c584SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1031c584SApple OSS Distributions */
28*1031c584SApple OSS Distributions /*
29*1031c584SApple OSS Distributions * Copyright (c) 1982, 1989, 1993
30*1031c584SApple OSS Distributions * The Regents of the University of California. All rights reserved.
31*1031c584SApple OSS Distributions *
32*1031c584SApple OSS Distributions * Redistribution and use in source and binary forms, with or without
33*1031c584SApple OSS Distributions * modification, are permitted provided that the following conditions
34*1031c584SApple OSS Distributions * are met:
35*1031c584SApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
36*1031c584SApple OSS Distributions * notice, this list of conditions and the following disclaimer.
37*1031c584SApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
38*1031c584SApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
39*1031c584SApple OSS Distributions * documentation and/or other materials provided with the distribution.
40*1031c584SApple OSS Distributions * 3. All advertising materials mentioning features or use of this software
41*1031c584SApple OSS Distributions * must display the following acknowledgement:
42*1031c584SApple OSS Distributions * This product includes software developed by the University of
43*1031c584SApple OSS Distributions * California, Berkeley and its contributors.
44*1031c584SApple OSS Distributions * 4. Neither the name of the University nor the names of its contributors
45*1031c584SApple OSS Distributions * may be used to endorse or promote products derived from this software
46*1031c584SApple OSS Distributions * without specific prior written permission.
47*1031c584SApple OSS Distributions *
48*1031c584SApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49*1031c584SApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50*1031c584SApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51*1031c584SApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52*1031c584SApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53*1031c584SApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54*1031c584SApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55*1031c584SApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56*1031c584SApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57*1031c584SApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58*1031c584SApple OSS Distributions * SUCH DAMAGE.
59*1031c584SApple OSS Distributions *
60*1031c584SApple OSS Distributions */
61*1031c584SApple OSS Distributions
62*1031c584SApple OSS Distributions #include <sys/param.h>
63*1031c584SApple OSS Distributions #include <sys/systm.h>
64*1031c584SApple OSS Distributions #include <sys/kernel.h>
65*1031c584SApple OSS Distributions #include <sys/malloc.h>
66*1031c584SApple OSS Distributions #include <sys/mbuf.h>
67*1031c584SApple OSS Distributions #include <sys/socket.h>
68*1031c584SApple OSS Distributions #include <sys/sockio.h>
69*1031c584SApple OSS Distributions #include <sys/sysctl.h>
70*1031c584SApple OSS Distributions
71*1031c584SApple OSS Distributions #include <pexpert/pexpert.h>
72*1031c584SApple OSS Distributions
73*1031c584SApple OSS Distributions #define etherbroadcastaddr fugly
74*1031c584SApple OSS Distributions #include <net/if.h>
75*1031c584SApple OSS Distributions #include <net/route.h>
76*1031c584SApple OSS Distributions #include <net/if_llc.h>
77*1031c584SApple OSS Distributions #include <net/if_dl.h>
78*1031c584SApple OSS Distributions #include <net/if_types.h>
79*1031c584SApple OSS Distributions #include <net/if_ether.h>
80*1031c584SApple OSS Distributions #include <net/if_gif.h>
81*1031c584SApple OSS Distributions #include <netinet/if_ether.h>
82*1031c584SApple OSS Distributions #include <netinet/in.h> /* For M_LOOP */
83*1031c584SApple OSS Distributions #include <net/kpi_interface.h>
84*1031c584SApple OSS Distributions #include <net/kpi_protocol.h>
85*1031c584SApple OSS Distributions #undef etherbroadcastaddr
86*1031c584SApple OSS Distributions
87*1031c584SApple OSS Distributions /*
88*1031c584SApple OSS Distributions #if INET
89*1031c584SApple OSS Distributions #include <netinet/in.h>
90*1031c584SApple OSS Distributions #include <netinet/in_var.h>
91*1031c584SApple OSS Distributions *
92*1031c584SApple OSS Distributions #include <netinet/in_systm.h>
93*1031c584SApple OSS Distributions #include <netinet/ip.h>
94*1031c584SApple OSS Distributions #endif
95*1031c584SApple OSS Distributions */
96*1031c584SApple OSS Distributions #include <net/ether_if_module.h>
97*1031c584SApple OSS Distributions #include <sys/socketvar.h>
98*1031c584SApple OSS Distributions #include <net/if_vlan_var.h>
99*1031c584SApple OSS Distributions #if BOND
100*1031c584SApple OSS Distributions #include <net/if_bond_internal.h>
101*1031c584SApple OSS Distributions #endif /* BOND */
102*1031c584SApple OSS Distributions #if IF_BRIDGE
103*1031c584SApple OSS Distributions #include <net/if_bridgevar.h>
104*1031c584SApple OSS Distributions #endif /* IF_BRIDGE */
105*1031c584SApple OSS Distributions #if IF_FAKE
106*1031c584SApple OSS Distributions #include <net/if_fake_var.h>
107*1031c584SApple OSS Distributions #endif /* IF_FAKE */
108*1031c584SApple OSS Distributions #if IF_HEADLESS
109*1031c584SApple OSS Distributions extern void if_headless_init(void);
110*1031c584SApple OSS Distributions #endif /* IF_HEADLESS */
111*1031c584SApple OSS Distributions
112*1031c584SApple OSS Distributions #include <net/dlil.h>
113*1031c584SApple OSS Distributions
114*1031c584SApple OSS Distributions SYSCTL_DECL(_net_link);
115*1031c584SApple OSS Distributions SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
116*1031c584SApple OSS Distributions "Ethernet");
117*1031c584SApple OSS Distributions
118*1031c584SApple OSS Distributions struct en_desc {
119*1031c584SApple OSS Distributions u_int16_t type; /* Type of protocol stored in data */
120*1031c584SApple OSS Distributions u_int32_t protocol_family; /* Protocol family */
121*1031c584SApple OSS Distributions u_int32_t data[2]; /* Protocol data */
122*1031c584SApple OSS Distributions };
123*1031c584SApple OSS Distributions
124*1031c584SApple OSS Distributions /* descriptors are allocated in blocks of ETHER_DESC_BLK_SIZE */
125*1031c584SApple OSS Distributions #if !XNU_TARGET_OS_OSX
126*1031c584SApple OSS Distributions #define ETHER_DESC_BLK_SIZE (2) /* IP, ARP */
127*1031c584SApple OSS Distributions #else /* XNU_TARGET_OS_OSX */
128*1031c584SApple OSS Distributions #define ETHER_DESC_BLK_SIZE (10)
129*1031c584SApple OSS Distributions #endif /* XNU_TARGET_OS_OSX */
130*1031c584SApple OSS Distributions
131*1031c584SApple OSS Distributions /*
132*1031c584SApple OSS Distributions * Header for the demux list, hangs off of IFP at if_family_cookie
133*1031c584SApple OSS Distributions */
134*1031c584SApple OSS Distributions struct ether_desc_blk_str {
135*1031c584SApple OSS Distributions u_int32_t n_max_used;
136*1031c584SApple OSS Distributions u_int32_t n_count;
137*1031c584SApple OSS Distributions u_int32_t n_used;
138*1031c584SApple OSS Distributions struct en_desc block_ptr[1];
139*1031c584SApple OSS Distributions };
140*1031c584SApple OSS Distributions
141*1031c584SApple OSS Distributions /* Size of the above struct before the array of struct en_desc */
142*1031c584SApple OSS Distributions #define ETHER_DESC_HEADER_SIZE \
143*1031c584SApple OSS Distributions ((size_t) offsetof(struct ether_desc_blk_str, block_ptr))
144*1031c584SApple OSS Distributions
145*1031c584SApple OSS Distributions __private_extern__ u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
146*1031c584SApple OSS Distributions { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
147*1031c584SApple OSS Distributions
148*1031c584SApple OSS Distributions /*
149*1031c584SApple OSS Distributions * Release all descriptor entries owned by this protocol (there may be several).
150*1031c584SApple OSS Distributions * Setting the type to 0 releases the entry. Eventually we should compact-out
151*1031c584SApple OSS Distributions * the unused entries.
152*1031c584SApple OSS Distributions */
153*1031c584SApple OSS Distributions int
ether_del_proto(ifnet_t ifp,protocol_family_t protocol_family)154*1031c584SApple OSS Distributions ether_del_proto(ifnet_t ifp, protocol_family_t protocol_family)
155*1031c584SApple OSS Distributions {
156*1031c584SApple OSS Distributions struct ether_desc_blk_str *desc_blk =
157*1031c584SApple OSS Distributions (struct ether_desc_blk_str *)ifp->if_family_cookie;
158*1031c584SApple OSS Distributions u_int32_t current = 0;
159*1031c584SApple OSS Distributions int found = 0;
160*1031c584SApple OSS Distributions
161*1031c584SApple OSS Distributions if (desc_blk == NULL) {
162*1031c584SApple OSS Distributions return 0;
163*1031c584SApple OSS Distributions }
164*1031c584SApple OSS Distributions
165*1031c584SApple OSS Distributions for (current = desc_blk->n_max_used; current > 0; current--) {
166*1031c584SApple OSS Distributions if (desc_blk->block_ptr[current - 1].protocol_family ==
167*1031c584SApple OSS Distributions protocol_family) {
168*1031c584SApple OSS Distributions found = 1;
169*1031c584SApple OSS Distributions desc_blk->block_ptr[current - 1].type = 0;
170*1031c584SApple OSS Distributions desc_blk->n_used--;
171*1031c584SApple OSS Distributions }
172*1031c584SApple OSS Distributions }
173*1031c584SApple OSS Distributions
174*1031c584SApple OSS Distributions if (desc_blk->n_used == 0) {
175*1031c584SApple OSS Distributions u_int32_t size = desc_blk->n_count * sizeof(struct en_desc) +
176*1031c584SApple OSS Distributions ETHER_DESC_HEADER_SIZE;
177*1031c584SApple OSS Distributions kfree_data(ifp->if_family_cookie, size);
178*1031c584SApple OSS Distributions ifp->if_family_cookie = 0;
179*1031c584SApple OSS Distributions } else {
180*1031c584SApple OSS Distributions /* Decrement n_max_used */
181*1031c584SApple OSS Distributions for (; desc_blk->n_max_used > 0 &&
182*1031c584SApple OSS Distributions desc_blk->block_ptr[desc_blk->n_max_used - 1].type == 0;
183*1031c584SApple OSS Distributions desc_blk->n_max_used--) {
184*1031c584SApple OSS Distributions ;
185*1031c584SApple OSS Distributions }
186*1031c584SApple OSS Distributions }
187*1031c584SApple OSS Distributions
188*1031c584SApple OSS Distributions return 0;
189*1031c584SApple OSS Distributions }
190*1031c584SApple OSS Distributions
191*1031c584SApple OSS Distributions static int
ether_add_proto_internal(struct ifnet * ifp,protocol_family_t protocol,const struct ifnet_demux_desc * demux)192*1031c584SApple OSS Distributions ether_add_proto_internal(struct ifnet *ifp, protocol_family_t protocol,
193*1031c584SApple OSS Distributions const struct ifnet_demux_desc *demux)
194*1031c584SApple OSS Distributions {
195*1031c584SApple OSS Distributions struct en_desc *ed;
196*1031c584SApple OSS Distributions struct ether_desc_blk_str *desc_blk =
197*1031c584SApple OSS Distributions (struct ether_desc_blk_str *)ifp->if_family_cookie;
198*1031c584SApple OSS Distributions u_int32_t i;
199*1031c584SApple OSS Distributions
200*1031c584SApple OSS Distributions switch (demux->type) {
201*1031c584SApple OSS Distributions /* These types are supported */
202*1031c584SApple OSS Distributions /* Top three are preferred */
203*1031c584SApple OSS Distributions case DLIL_DESC_ETYPE2:
204*1031c584SApple OSS Distributions if (demux->datalen != 2) {
205*1031c584SApple OSS Distributions return EINVAL;
206*1031c584SApple OSS Distributions }
207*1031c584SApple OSS Distributions break;
208*1031c584SApple OSS Distributions
209*1031c584SApple OSS Distributions case DLIL_DESC_SAP:
210*1031c584SApple OSS Distributions if (demux->datalen != 3) {
211*1031c584SApple OSS Distributions return EINVAL;
212*1031c584SApple OSS Distributions }
213*1031c584SApple OSS Distributions break;
214*1031c584SApple OSS Distributions
215*1031c584SApple OSS Distributions case DLIL_DESC_SNAP:
216*1031c584SApple OSS Distributions if (demux->datalen != 5) {
217*1031c584SApple OSS Distributions return EINVAL;
218*1031c584SApple OSS Distributions }
219*1031c584SApple OSS Distributions break;
220*1031c584SApple OSS Distributions
221*1031c584SApple OSS Distributions default:
222*1031c584SApple OSS Distributions return ENOTSUP;
223*1031c584SApple OSS Distributions }
224*1031c584SApple OSS Distributions
225*1031c584SApple OSS Distributions /* Verify a matching descriptor does not exist */
226*1031c584SApple OSS Distributions if (desc_blk != NULL) {
227*1031c584SApple OSS Distributions switch (demux->type) {
228*1031c584SApple OSS Distributions case DLIL_DESC_ETYPE2:
229*1031c584SApple OSS Distributions for (i = 0; i < desc_blk->n_max_used; i++) {
230*1031c584SApple OSS Distributions if (desc_blk->block_ptr[i].type ==
231*1031c584SApple OSS Distributions DLIL_DESC_ETYPE2 &&
232*1031c584SApple OSS Distributions desc_blk->block_ptr[i].data[0] ==
233*1031c584SApple OSS Distributions *(u_int16_t*)demux->data) {
234*1031c584SApple OSS Distributions return EADDRINUSE;
235*1031c584SApple OSS Distributions }
236*1031c584SApple OSS Distributions }
237*1031c584SApple OSS Distributions break;
238*1031c584SApple OSS Distributions case DLIL_DESC_SAP:
239*1031c584SApple OSS Distributions case DLIL_DESC_SNAP:
240*1031c584SApple OSS Distributions for (i = 0; i < desc_blk->n_max_used; i++) {
241*1031c584SApple OSS Distributions if (desc_blk->block_ptr[i].type ==
242*1031c584SApple OSS Distributions demux->type &&
243*1031c584SApple OSS Distributions bcmp(desc_blk->block_ptr[i].data,
244*1031c584SApple OSS Distributions demux->data, demux->datalen) == 0) {
245*1031c584SApple OSS Distributions return EADDRINUSE;
246*1031c584SApple OSS Distributions }
247*1031c584SApple OSS Distributions }
248*1031c584SApple OSS Distributions break;
249*1031c584SApple OSS Distributions }
250*1031c584SApple OSS Distributions }
251*1031c584SApple OSS Distributions
252*1031c584SApple OSS Distributions /* Check for case where all of the descriptor blocks are in use */
253*1031c584SApple OSS Distributions if (desc_blk == NULL || desc_blk->n_used == desc_blk->n_count) {
254*1031c584SApple OSS Distributions struct ether_desc_blk_str *tmp;
255*1031c584SApple OSS Distributions u_int32_t new_count = ETHER_DESC_BLK_SIZE;
256*1031c584SApple OSS Distributions u_int32_t new_size;
257*1031c584SApple OSS Distributions u_int32_t old_size = 0;
258*1031c584SApple OSS Distributions
259*1031c584SApple OSS Distributions i = 0;
260*1031c584SApple OSS Distributions
261*1031c584SApple OSS Distributions if (desc_blk) {
262*1031c584SApple OSS Distributions new_count += desc_blk->n_count;
263*1031c584SApple OSS Distributions old_size = desc_blk->n_count * sizeof(struct en_desc) +
264*1031c584SApple OSS Distributions ETHER_DESC_HEADER_SIZE;
265*1031c584SApple OSS Distributions i = desc_blk->n_used;
266*1031c584SApple OSS Distributions }
267*1031c584SApple OSS Distributions
268*1031c584SApple OSS Distributions new_size = new_count * sizeof(struct en_desc) +
269*1031c584SApple OSS Distributions ETHER_DESC_HEADER_SIZE;
270*1031c584SApple OSS Distributions
271*1031c584SApple OSS Distributions tmp = (struct ether_desc_blk_str *)kalloc_data(new_size, Z_WAITOK);
272*1031c584SApple OSS Distributions if (tmp == NULL) {
273*1031c584SApple OSS Distributions /*
274*1031c584SApple OSS Distributions * Remove any previous descriptors set in the call.
275*1031c584SApple OSS Distributions */
276*1031c584SApple OSS Distributions return ENOMEM;
277*1031c584SApple OSS Distributions }
278*1031c584SApple OSS Distributions
279*1031c584SApple OSS Distributions bzero(((char *)tmp) + old_size, new_size - old_size);
280*1031c584SApple OSS Distributions if (desc_blk) {
281*1031c584SApple OSS Distributions bcopy(desc_blk, tmp, old_size);
282*1031c584SApple OSS Distributions kfree_data(desc_blk, old_size);
283*1031c584SApple OSS Distributions }
284*1031c584SApple OSS Distributions desc_blk = tmp;
285*1031c584SApple OSS Distributions ifp->if_family_cookie = (uintptr_t)desc_blk;
286*1031c584SApple OSS Distributions desc_blk->n_count = new_count;
287*1031c584SApple OSS Distributions } else {
288*1031c584SApple OSS Distributions /* Find a free entry */
289*1031c584SApple OSS Distributions for (i = 0; i < desc_blk->n_count; i++) {
290*1031c584SApple OSS Distributions if (desc_blk->block_ptr[i].type == 0) {
291*1031c584SApple OSS Distributions break;
292*1031c584SApple OSS Distributions }
293*1031c584SApple OSS Distributions }
294*1031c584SApple OSS Distributions }
295*1031c584SApple OSS Distributions
296*1031c584SApple OSS Distributions /* Bump n_max_used if appropriate */
297*1031c584SApple OSS Distributions if (i + 1 > desc_blk->n_max_used) {
298*1031c584SApple OSS Distributions desc_blk->n_max_used = i + 1;
299*1031c584SApple OSS Distributions }
300*1031c584SApple OSS Distributions
301*1031c584SApple OSS Distributions ed = &desc_blk->block_ptr[i];
302*1031c584SApple OSS Distributions ed->protocol_family = protocol;
303*1031c584SApple OSS Distributions ed->data[0] = 0;
304*1031c584SApple OSS Distributions ed->data[1] = 0;
305*1031c584SApple OSS Distributions
306*1031c584SApple OSS Distributions switch (demux->type) {
307*1031c584SApple OSS Distributions case DLIL_DESC_ETYPE2:
308*1031c584SApple OSS Distributions /* 2 byte ethernet raw protocol type is at native_type */
309*1031c584SApple OSS Distributions /* prtocol must be in network byte order */
310*1031c584SApple OSS Distributions ed->type = DLIL_DESC_ETYPE2;
311*1031c584SApple OSS Distributions ed->data[0] = *(u_int16_t*)demux->data;
312*1031c584SApple OSS Distributions break;
313*1031c584SApple OSS Distributions
314*1031c584SApple OSS Distributions case DLIL_DESC_SAP:
315*1031c584SApple OSS Distributions ed->type = DLIL_DESC_SAP;
316*1031c584SApple OSS Distributions bcopy(demux->data, &ed->data[0], 3);
317*1031c584SApple OSS Distributions break;
318*1031c584SApple OSS Distributions
319*1031c584SApple OSS Distributions case DLIL_DESC_SNAP: {
320*1031c584SApple OSS Distributions u_int8_t* pDest = ((u_int8_t*)&ed->data[0]) + 3;
321*1031c584SApple OSS Distributions ed->type = DLIL_DESC_SNAP;
322*1031c584SApple OSS Distributions bcopy(demux->data, pDest, 5);
323*1031c584SApple OSS Distributions break;
324*1031c584SApple OSS Distributions }
325*1031c584SApple OSS Distributions }
326*1031c584SApple OSS Distributions
327*1031c584SApple OSS Distributions desc_blk->n_used++;
328*1031c584SApple OSS Distributions
329*1031c584SApple OSS Distributions return 0;
330*1031c584SApple OSS Distributions }
331*1031c584SApple OSS Distributions
332*1031c584SApple OSS Distributions int
ether_add_proto(ifnet_t ifp,protocol_family_t protocol,const struct ifnet_demux_desc * demux_list,u_int32_t demux_count)333*1031c584SApple OSS Distributions ether_add_proto(ifnet_t ifp, protocol_family_t protocol,
334*1031c584SApple OSS Distributions const struct ifnet_demux_desc *demux_list, u_int32_t demux_count)
335*1031c584SApple OSS Distributions {
336*1031c584SApple OSS Distributions int error = 0;
337*1031c584SApple OSS Distributions u_int32_t i;
338*1031c584SApple OSS Distributions
339*1031c584SApple OSS Distributions for (i = 0; i < demux_count; i++) {
340*1031c584SApple OSS Distributions error = ether_add_proto_internal(ifp, protocol, &demux_list[i]);
341*1031c584SApple OSS Distributions if (error) {
342*1031c584SApple OSS Distributions ether_del_proto(ifp, protocol);
343*1031c584SApple OSS Distributions break;
344*1031c584SApple OSS Distributions }
345*1031c584SApple OSS Distributions }
346*1031c584SApple OSS Distributions
347*1031c584SApple OSS Distributions return error;
348*1031c584SApple OSS Distributions }
349*1031c584SApple OSS Distributions
350*1031c584SApple OSS Distributions int
ether_demux(ifnet_t ifp,mbuf_t m,char * frame_header,protocol_family_t * protocol_family)351*1031c584SApple OSS Distributions ether_demux(ifnet_t ifp, mbuf_t m, char *frame_header,
352*1031c584SApple OSS Distributions protocol_family_t *protocol_family)
353*1031c584SApple OSS Distributions {
354*1031c584SApple OSS Distributions struct ether_header *eh = (struct ether_header *)(void *)frame_header;
355*1031c584SApple OSS Distributions u_short ether_type = eh->ether_type;
356*1031c584SApple OSS Distributions u_int16_t type;
357*1031c584SApple OSS Distributions u_int8_t *data;
358*1031c584SApple OSS Distributions u_int32_t i = 0;
359*1031c584SApple OSS Distributions struct ether_desc_blk_str *desc_blk =
360*1031c584SApple OSS Distributions (struct ether_desc_blk_str *)ifp->if_family_cookie;
361*1031c584SApple OSS Distributions u_int32_t maxd = desc_blk ? desc_blk->n_max_used : 0;
362*1031c584SApple OSS Distributions struct en_desc *ed = desc_blk ? desc_blk->block_ptr : NULL;
363*1031c584SApple OSS Distributions u_int32_t extProto1 = 0;
364*1031c584SApple OSS Distributions u_int32_t extProto2 = 0;
365*1031c584SApple OSS Distributions
366*1031c584SApple OSS Distributions if (eh->ether_dhost[0] & 1) {
367*1031c584SApple OSS Distributions /* Check for broadcast */
368*1031c584SApple OSS Distributions if (_ether_cmp(etherbroadcastaddr, eh->ether_dhost) == 0) {
369*1031c584SApple OSS Distributions m->m_flags |= M_BCAST;
370*1031c584SApple OSS Distributions } else {
371*1031c584SApple OSS Distributions m->m_flags |= M_MCAST;
372*1031c584SApple OSS Distributions }
373*1031c584SApple OSS Distributions }
374*1031c584SApple OSS Distributions
375*1031c584SApple OSS Distributions if (m->m_flags & M_HASFCS) {
376*1031c584SApple OSS Distributions /*
377*1031c584SApple OSS Distributions * If the M_HASFCS is set by the driver we want to make sure
378*1031c584SApple OSS Distributions * that we strip off the trailing FCS data before handing it
379*1031c584SApple OSS Distributions * up the stack.
380*1031c584SApple OSS Distributions */
381*1031c584SApple OSS Distributions m_adj(m, -ETHER_CRC_LEN);
382*1031c584SApple OSS Distributions m->m_flags &= ~M_HASFCS;
383*1031c584SApple OSS Distributions }
384*1031c584SApple OSS Distributions
385*1031c584SApple OSS Distributions if ((eh->ether_dhost[0] & 1) == 0) {
386*1031c584SApple OSS Distributions /*
387*1031c584SApple OSS Distributions * When the driver is put into promiscuous mode we may receive
388*1031c584SApple OSS Distributions * unicast frames that are not intended for our interfaces.
389*1031c584SApple OSS Distributions * They are marked here as being promiscuous so the caller may
390*1031c584SApple OSS Distributions * dispose of them after passing the packets to any interface
391*1031c584SApple OSS Distributions * filters.
392*1031c584SApple OSS Distributions */
393*1031c584SApple OSS Distributions if (_ether_cmp(eh->ether_dhost, IF_LLADDR(ifp))) {
394*1031c584SApple OSS Distributions m->m_flags |= M_PROMISC;
395*1031c584SApple OSS Distributions }
396*1031c584SApple OSS Distributions }
397*1031c584SApple OSS Distributions
398*1031c584SApple OSS Distributions /* check for VLAN */
399*1031c584SApple OSS Distributions if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) != 0) {
400*1031c584SApple OSS Distributions if (EVL_VLANOFTAG(m->m_pkthdr.vlan_tag) != 0) {
401*1031c584SApple OSS Distributions *protocol_family = PF_VLAN;
402*1031c584SApple OSS Distributions return 0;
403*1031c584SApple OSS Distributions }
404*1031c584SApple OSS Distributions /* the packet is just priority-tagged, clear the bit */
405*1031c584SApple OSS Distributions m->m_pkthdr.csum_flags &= ~CSUM_VLAN_TAG_VALID;
406*1031c584SApple OSS Distributions } else if (ether_type == htons(ETHERTYPE_VLAN)) {
407*1031c584SApple OSS Distributions struct ether_vlan_header * evl;
408*1031c584SApple OSS Distributions
409*1031c584SApple OSS Distributions evl = (struct ether_vlan_header *)(void *)frame_header;
410*1031c584SApple OSS Distributions if (m->m_len < ETHER_VLAN_ENCAP_LEN ||
411*1031c584SApple OSS Distributions ntohs(evl->evl_proto) == ETHERTYPE_VLAN ||
412*1031c584SApple OSS Distributions EVL_VLANOFTAG(ntohs(evl->evl_tag)) != 0) {
413*1031c584SApple OSS Distributions *protocol_family = PF_VLAN;
414*1031c584SApple OSS Distributions return 0;
415*1031c584SApple OSS Distributions }
416*1031c584SApple OSS Distributions /* the packet is just priority-tagged */
417*1031c584SApple OSS Distributions
418*1031c584SApple OSS Distributions /* make the encapsulated ethertype the actual ethertype */
419*1031c584SApple OSS Distributions ether_type = evl->evl_encap_proto = evl->evl_proto;
420*1031c584SApple OSS Distributions
421*1031c584SApple OSS Distributions /* remove the encapsulation header */
422*1031c584SApple OSS Distributions m->m_len -= ETHER_VLAN_ENCAP_LEN;
423*1031c584SApple OSS Distributions m->m_data += ETHER_VLAN_ENCAP_LEN;
424*1031c584SApple OSS Distributions m->m_pkthdr.len -= ETHER_VLAN_ENCAP_LEN;
425*1031c584SApple OSS Distributions m->m_pkthdr.csum_flags = 0; /* can't trust hardware checksum */
426*1031c584SApple OSS Distributions } else if (ether_type == htons(ETHERTYPE_ARP)) {
427*1031c584SApple OSS Distributions m->m_pkthdr.pkt_flags |= PKTF_INET_RESOLVE; /* ARP packet */
428*1031c584SApple OSS Distributions }
429*1031c584SApple OSS Distributions data = mtod(m, u_int8_t*);
430*1031c584SApple OSS Distributions
431*1031c584SApple OSS Distributions /*
432*1031c584SApple OSS Distributions * Determine the packet's protocol type and stuff the protocol into
433*1031c584SApple OSS Distributions * longs for quick compares.
434*1031c584SApple OSS Distributions */
435*1031c584SApple OSS Distributions if (ntohs(ether_type) <= 1500) {
436*1031c584SApple OSS Distributions bcopy(data, &extProto1, sizeof(u_int32_t));
437*1031c584SApple OSS Distributions
438*1031c584SApple OSS Distributions /* SAP or SNAP */
439*1031c584SApple OSS Distributions if ((extProto1 & htonl(0xFFFFFF00)) == htonl(0xAAAA0300)) {
440*1031c584SApple OSS Distributions /* SNAP */
441*1031c584SApple OSS Distributions type = DLIL_DESC_SNAP;
442*1031c584SApple OSS Distributions bcopy(data + sizeof(u_int32_t), &extProto2,
443*1031c584SApple OSS Distributions sizeof(u_int32_t));
444*1031c584SApple OSS Distributions extProto1 &= htonl(0x000000FF);
445*1031c584SApple OSS Distributions } else {
446*1031c584SApple OSS Distributions type = DLIL_DESC_SAP;
447*1031c584SApple OSS Distributions extProto1 &= htonl(0xFFFFFF00);
448*1031c584SApple OSS Distributions }
449*1031c584SApple OSS Distributions } else {
450*1031c584SApple OSS Distributions type = DLIL_DESC_ETYPE2;
451*1031c584SApple OSS Distributions }
452*1031c584SApple OSS Distributions
453*1031c584SApple OSS Distributions /*
454*1031c584SApple OSS Distributions * Search through the connected protocols for a match.
455*1031c584SApple OSS Distributions */
456*1031c584SApple OSS Distributions switch (type) {
457*1031c584SApple OSS Distributions case DLIL_DESC_ETYPE2:
458*1031c584SApple OSS Distributions for (i = 0; i < maxd; i++) {
459*1031c584SApple OSS Distributions if ((ed[i].type == type) &&
460*1031c584SApple OSS Distributions (ed[i].data[0] == ether_type)) {
461*1031c584SApple OSS Distributions *protocol_family = ed[i].protocol_family;
462*1031c584SApple OSS Distributions return 0;
463*1031c584SApple OSS Distributions }
464*1031c584SApple OSS Distributions }
465*1031c584SApple OSS Distributions break;
466*1031c584SApple OSS Distributions
467*1031c584SApple OSS Distributions case DLIL_DESC_SAP:
468*1031c584SApple OSS Distributions for (i = 0; i < maxd; i++) {
469*1031c584SApple OSS Distributions if ((ed[i].type == type) &&
470*1031c584SApple OSS Distributions (ed[i].data[0] == extProto1)) {
471*1031c584SApple OSS Distributions *protocol_family = ed[i].protocol_family;
472*1031c584SApple OSS Distributions return 0;
473*1031c584SApple OSS Distributions }
474*1031c584SApple OSS Distributions }
475*1031c584SApple OSS Distributions break;
476*1031c584SApple OSS Distributions
477*1031c584SApple OSS Distributions case DLIL_DESC_SNAP:
478*1031c584SApple OSS Distributions for (i = 0; i < maxd; i++) {
479*1031c584SApple OSS Distributions if ((ed[i].type == type) &&
480*1031c584SApple OSS Distributions (ed[i].data[0] == extProto1) &&
481*1031c584SApple OSS Distributions (ed[i].data[1] == extProto2)) {
482*1031c584SApple OSS Distributions *protocol_family = ed[i].protocol_family;
483*1031c584SApple OSS Distributions return 0;
484*1031c584SApple OSS Distributions }
485*1031c584SApple OSS Distributions }
486*1031c584SApple OSS Distributions break;
487*1031c584SApple OSS Distributions }
488*1031c584SApple OSS Distributions
489*1031c584SApple OSS Distributions return ENOENT;
490*1031c584SApple OSS Distributions }
491*1031c584SApple OSS Distributions
492*1031c584SApple OSS Distributions /*
493*1031c584SApple OSS Distributions * On embedded, ether_frameout is practicaly ether_frameout_extended.
494*1031c584SApple OSS Distributions * On non-embedded, ether_frameout has long been exposed as a public KPI,
495*1031c584SApple OSS Distributions * and therefore its signature must remain the same (without the pre- and
496*1031c584SApple OSS Distributions * postpend length parameters.)
497*1031c584SApple OSS Distributions */
498*1031c584SApple OSS Distributions #if KPI_INTERFACE_EMBEDDED
499*1031c584SApple OSS Distributions int
ether_frameout(struct ifnet * ifp,struct mbuf ** m,const struct sockaddr * ndest,const char * edst,const char * ether_type,u_int32_t * prepend_len,u_int32_t * postpend_len)500*1031c584SApple OSS Distributions ether_frameout(struct ifnet *ifp, struct mbuf **m,
501*1031c584SApple OSS Distributions const struct sockaddr *ndest, const char *edst,
502*1031c584SApple OSS Distributions const char *ether_type, u_int32_t *prepend_len, u_int32_t *postpend_len)
503*1031c584SApple OSS Distributions #else /* !KPI_INTERFACE_EMBEDDED */
504*1031c584SApple OSS Distributions int
505*1031c584SApple OSS Distributions ether_frameout(struct ifnet *ifp, struct mbuf **m,
506*1031c584SApple OSS Distributions const struct sockaddr *ndest, const char *edst,
507*1031c584SApple OSS Distributions const char *ether_type)
508*1031c584SApple OSS Distributions #endif /* KPI_INTERFACE_EMBEDDED */
509*1031c584SApple OSS Distributions {
510*1031c584SApple OSS Distributions #if KPI_INTERFACE_EMBEDDED
511*1031c584SApple OSS Distributions return ether_frameout_extended(ifp, m, ndest, edst, ether_type,
512*1031c584SApple OSS Distributions prepend_len, postpend_len);
513*1031c584SApple OSS Distributions #else /* !KPI_INTERFACE_EMBEDDED */
514*1031c584SApple OSS Distributions return ether_frameout_extended(ifp, m, ndest, edst, ether_type,
515*1031c584SApple OSS Distributions NULL, NULL);
516*1031c584SApple OSS Distributions #endif /* !KPI_INTERFACE_EMBEDDED */
517*1031c584SApple OSS Distributions }
518*1031c584SApple OSS Distributions
519*1031c584SApple OSS Distributions /*
520*1031c584SApple OSS Distributions * Ethernet output routine.
521*1031c584SApple OSS Distributions * Encapsulate a packet of type family for the local net.
522*1031c584SApple OSS Distributions * Use trailer local net encapsulation if enough data in first
523*1031c584SApple OSS Distributions * packet leaves a multiple of 512 bytes of data in remainder.
524*1031c584SApple OSS Distributions */
525*1031c584SApple OSS Distributions int
ether_frameout_extended(struct ifnet * ifp,struct mbuf ** m,const struct sockaddr * ndest,const char * edst,const char * ether_type,u_int32_t * prepend_len,u_int32_t * postpend_len)526*1031c584SApple OSS Distributions ether_frameout_extended(struct ifnet *ifp, struct mbuf **m,
527*1031c584SApple OSS Distributions const struct sockaddr *ndest, const char *edst,
528*1031c584SApple OSS Distributions const char *ether_type, u_int32_t *prepend_len, u_int32_t *postpend_len)
529*1031c584SApple OSS Distributions {
530*1031c584SApple OSS Distributions struct ether_header *eh;
531*1031c584SApple OSS Distributions int hlen; /* link layer header length */
532*1031c584SApple OSS Distributions
533*1031c584SApple OSS Distributions hlen = ETHER_HDR_LEN;
534*1031c584SApple OSS Distributions
535*1031c584SApple OSS Distributions /*
536*1031c584SApple OSS Distributions * If a simplex interface, and the packet is being sent to our
537*1031c584SApple OSS Distributions * Ethernet address or a broadcast address, loopback a copy.
538*1031c584SApple OSS Distributions * XXX To make a simplex device behave exactly like a duplex
539*1031c584SApple OSS Distributions * device, we should copy in the case of sending to our own
540*1031c584SApple OSS Distributions * ethernet address (thus letting the original actually appear
541*1031c584SApple OSS Distributions * on the wire). However, we don't do that here for security
542*1031c584SApple OSS Distributions * reasons and compatibility with the original behavior.
543*1031c584SApple OSS Distributions */
544*1031c584SApple OSS Distributions if ((ifp->if_flags & IFF_SIMPLEX) &&
545*1031c584SApple OSS Distributions ((*m)->m_flags & M_LOOP) && lo_ifp != NULL) {
546*1031c584SApple OSS Distributions if ((*m)->m_flags & M_BCAST) {
547*1031c584SApple OSS Distributions struct mbuf *n = m_copy(*m, 0, (int)M_COPYALL);
548*1031c584SApple OSS Distributions if (n != NULL) {
549*1031c584SApple OSS Distributions dlil_output(lo_ifp, ndest->sa_family,
550*1031c584SApple OSS Distributions n, NULL, ndest, 0, NULL);
551*1031c584SApple OSS Distributions }
552*1031c584SApple OSS Distributions } else if (_ether_cmp(edst, IF_LLADDR(ifp)) == 0) {
553*1031c584SApple OSS Distributions dlil_output(lo_ifp, ndest->sa_family, *m,
554*1031c584SApple OSS Distributions NULL, ndest, 0, NULL);
555*1031c584SApple OSS Distributions return EJUSTRETURN;
556*1031c584SApple OSS Distributions }
557*1031c584SApple OSS Distributions }
558*1031c584SApple OSS Distributions
559*1031c584SApple OSS Distributions /*
560*1031c584SApple OSS Distributions * Add local net header. If no space in first mbuf,
561*1031c584SApple OSS Distributions * allocate another.
562*1031c584SApple OSS Distributions */
563*1031c584SApple OSS Distributions M_PREPEND(*m, sizeof(struct ether_header), M_DONTWAIT, 0);
564*1031c584SApple OSS Distributions if (*m == NULL) {
565*1031c584SApple OSS Distributions return EJUSTRETURN;
566*1031c584SApple OSS Distributions }
567*1031c584SApple OSS Distributions
568*1031c584SApple OSS Distributions if (prepend_len != NULL) {
569*1031c584SApple OSS Distributions *prepend_len = sizeof(struct ether_header);
570*1031c584SApple OSS Distributions }
571*1031c584SApple OSS Distributions if (postpend_len != NULL) {
572*1031c584SApple OSS Distributions *postpend_len = 0;
573*1031c584SApple OSS Distributions }
574*1031c584SApple OSS Distributions
575*1031c584SApple OSS Distributions eh = mtod(*m, struct ether_header *);
576*1031c584SApple OSS Distributions (void) memcpy(&eh->ether_type, ether_type, sizeof(eh->ether_type));
577*1031c584SApple OSS Distributions (void) memcpy(eh->ether_dhost, edst, ETHER_ADDR_LEN);
578*1031c584SApple OSS Distributions ifnet_lladdr_copy_bytes(ifp, eh->ether_shost, ETHER_ADDR_LEN);
579*1031c584SApple OSS Distributions
580*1031c584SApple OSS Distributions return 0;
581*1031c584SApple OSS Distributions }
582*1031c584SApple OSS Distributions
583*1031c584SApple OSS Distributions errno_t
ether_check_multi(ifnet_t ifp,const struct sockaddr * proto_addr)584*1031c584SApple OSS Distributions ether_check_multi(ifnet_t ifp, const struct sockaddr *proto_addr)
585*1031c584SApple OSS Distributions {
586*1031c584SApple OSS Distributions #pragma unused(ifp)
587*1031c584SApple OSS Distributions errno_t result = EAFNOSUPPORT;
588*1031c584SApple OSS Distributions const u_char *e_addr;
589*1031c584SApple OSS Distributions
590*1031c584SApple OSS Distributions /*
591*1031c584SApple OSS Distributions * AF_SPEC and AF_LINK don't require translation. We do
592*1031c584SApple OSS Distributions * want to verify that they specify a valid multicast.
593*1031c584SApple OSS Distributions */
594*1031c584SApple OSS Distributions switch (proto_addr->sa_family) {
595*1031c584SApple OSS Distributions case AF_UNSPEC:
596*1031c584SApple OSS Distributions e_addr = (const u_char*)&proto_addr->sa_data[0];
597*1031c584SApple OSS Distributions if ((e_addr[0] & 0x01) != 0x01) {
598*1031c584SApple OSS Distributions result = EADDRNOTAVAIL;
599*1031c584SApple OSS Distributions } else {
600*1031c584SApple OSS Distributions result = 0;
601*1031c584SApple OSS Distributions }
602*1031c584SApple OSS Distributions break;
603*1031c584SApple OSS Distributions
604*1031c584SApple OSS Distributions case AF_LINK:
605*1031c584SApple OSS Distributions e_addr = CONST_LLADDR((const struct sockaddr_dl*)
606*1031c584SApple OSS Distributions (uintptr_t)(size_t)proto_addr);
607*1031c584SApple OSS Distributions if ((e_addr[0] & 0x01) != 0x01) {
608*1031c584SApple OSS Distributions result = EADDRNOTAVAIL;
609*1031c584SApple OSS Distributions } else {
610*1031c584SApple OSS Distributions result = 0;
611*1031c584SApple OSS Distributions }
612*1031c584SApple OSS Distributions break;
613*1031c584SApple OSS Distributions }
614*1031c584SApple OSS Distributions
615*1031c584SApple OSS Distributions return result;
616*1031c584SApple OSS Distributions }
617*1031c584SApple OSS Distributions
618*1031c584SApple OSS Distributions int
ether_ioctl(ifnet_t ifp,u_int32_t command,void * data)619*1031c584SApple OSS Distributions ether_ioctl(ifnet_t ifp, u_int32_t command, void *data)
620*1031c584SApple OSS Distributions {
621*1031c584SApple OSS Distributions #pragma unused(ifp, command, data)
622*1031c584SApple OSS Distributions return EOPNOTSUPP;
623*1031c584SApple OSS Distributions }
624*1031c584SApple OSS Distributions
625*1031c584SApple OSS Distributions __private_extern__ int
ether_family_init(void)626*1031c584SApple OSS Distributions ether_family_init(void)
627*1031c584SApple OSS Distributions {
628*1031c584SApple OSS Distributions errno_t error = 0;
629*1031c584SApple OSS Distributions
630*1031c584SApple OSS Distributions /* Register protocol registration functions */
631*1031c584SApple OSS Distributions if ((error = proto_register_plumber(PF_INET, APPLE_IF_FAM_ETHERNET,
632*1031c584SApple OSS Distributions ether_attach_inet, ether_detach_inet)) != 0) {
633*1031c584SApple OSS Distributions printf("proto_register_plumber failed for PF_INET error=%d\n",
634*1031c584SApple OSS Distributions error);
635*1031c584SApple OSS Distributions goto done;
636*1031c584SApple OSS Distributions }
637*1031c584SApple OSS Distributions if ((error = proto_register_plumber(PF_INET6, APPLE_IF_FAM_ETHERNET,
638*1031c584SApple OSS Distributions ether_attach_inet6, ether_detach_inet6)) != 0) {
639*1031c584SApple OSS Distributions printf("proto_register_plumber failed for PF_INET6 error=%d\n",
640*1031c584SApple OSS Distributions error);
641*1031c584SApple OSS Distributions goto done;
642*1031c584SApple OSS Distributions }
643*1031c584SApple OSS Distributions #if VLAN
644*1031c584SApple OSS Distributions vlan_family_init();
645*1031c584SApple OSS Distributions #endif /* VLAN */
646*1031c584SApple OSS Distributions #if BOND
647*1031c584SApple OSS Distributions bond_family_init();
648*1031c584SApple OSS Distributions #endif /* BOND */
649*1031c584SApple OSS Distributions #if IF_BRIDGE
650*1031c584SApple OSS Distributions bridgeattach(0);
651*1031c584SApple OSS Distributions #endif /* IF_BRIDGE */
652*1031c584SApple OSS Distributions #if IF_FAKE
653*1031c584SApple OSS Distributions if_fake_init();
654*1031c584SApple OSS Distributions #endif /* IF_FAKE */
655*1031c584SApple OSS Distributions #if IF_HEADLESS
656*1031c584SApple OSS Distributions if_headless_init();
657*1031c584SApple OSS Distributions #endif /* IF_HEADLESS */
658*1031c584SApple OSS Distributions done:
659*1031c584SApple OSS Distributions
660*1031c584SApple OSS Distributions return error;
661*1031c584SApple OSS Distributions }
662