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