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