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