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