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