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 struct ether_header * eh_p;
371 struct ether_vlan_encap_header encap;
372 struct ether_header new_eh;
373 bool success = false;
374
375 if (m->m_pkthdr.len < ETHER_VLAN_ENCAP_LEN) {
376 os_log_debug(OS_LOG_DEFAULT,
377 "%s: dropping short VLAN packet %d < %d",
378 ifp->if_xname, m->m_pkthdr.len,
379 ETHER_VLAN_ENCAP_LEN);
380 goto done;
381 }
382 if (frame_header < (char *)mbuf_datastart(m) ||
383 frame_header > mtod(m, char *)) {
384 os_log_debug(OS_LOG_DEFAULT,
385 "%s: dropping VLAN non-contiguous header %p, %p",
386 ifp->if_xname, mbuf_data(m), frame_header);
387 goto done;
388 }
389 /*
390 * Remove the VLAN encapsulation header by shifting the
391 * ethernet destination and source addresses over by the
392 * encapsulation header length (4 bytes).
393 */
394
395 /* copy the VLAN encapsulation header (4 bytes) */
396 if (mbuf_copydata(m, 0, sizeof(encap), (caddr_t)&encap) != 0) {
397 os_log_debug(OS_LOG_DEFAULT,
398 "%s: mbuf_copydata VLAN encap failed",
399 ifp->if_xname);
400 goto done;
401 }
402
403 /* create the new ethernet header with encapsulated proto */
404 eh_p = (struct ether_header *)(void *)
405 (m_mtod_lower_bound(m) + (frame_header - m_mtod_lower_bound(m)));
406 new_eh = *eh_p;
407 *ether_type_p = new_eh.ether_type = encap.evle_proto;
408
409 /* rollback to new ethernet header start (backward 10 bytes) */
410 #define ETHER_VLAN_ADJUST_HEADER (ETHER_HDR_LEN - ETHER_VLAN_ENCAP_LEN)
411 _mbuf_adjust_pkthdr_and_data(m, -ETHER_VLAN_ADJUST_HEADER);
412
413 /* write new ether header */
414 if (mbuf_copyback(m, 0, sizeof(new_eh), &new_eh, MBUF_DONTWAIT) != 0) {
415 os_log_debug(OS_LOG_DEFAULT,
416 "%s: mbuf_copyback VLAN stripped header failed",
417 ifp->if_xname);
418 goto done;
419 }
420
421 /* set new frame header */
422 mbuf_pkthdr_setheader(m, mtod(m, void *));
423
424 /* skip ethernet header (forward 14 bytes) */
425 _mbuf_adjust_pkthdr_and_data(m, ETHER_HDR_LEN);
426
427 /* can't trust hardware checksum */
428 m->m_pkthdr.csum_flags = 0;
429
430 /* return just the VLAN ID */
431 *tag_p = EVL_VLANOFTAG(ntohs(encap.evle_tag));
432 success = true;
433
434 done:
435 return success;
436 }
437
438 int
ether_demux(ifnet_t ifp,mbuf_t m,char * frame_header,protocol_family_t * protocol_family)439 ether_demux(ifnet_t ifp, mbuf_t m, char *frame_header,
440 protocol_family_t *protocol_family)
441 {
442 struct ether_header * __single eh = (struct ether_header *)(void *)frame_header;
443 u_short ether_type = eh->ether_type;
444 u_int16_t type;
445 u_int8_t *data;
446 u_int32_t i = 0;
447 struct ether_desc_blk_str *desc_blk = ifnet_ether_blk_str(ifp);
448 u_int32_t maxd = desc_blk ? desc_blk->n_max_used : 0;
449 struct en_desc *ed = desc_blk ? desc_blk->block_ptr : NULL;
450 u_int32_t extProto1 = 0;
451 u_int32_t extProto2 = 0;
452
453 if ((eh->ether_dhost[0] & 1) != 0) {
454 /* Check for broadcast */
455 if (_ether_cmp(etherbroadcastaddr, eh->ether_dhost) == 0) {
456 m->m_flags |= M_BCAST;
457 } else {
458 m->m_flags |= M_MCAST;
459 }
460 } else {
461 /*
462 * When the driver is put into promiscuous mode we may receive
463 * unicast frames that are not intended for our interfaces.
464 * They are marked here as being promiscuous so the caller may
465 * dispose of them after passing the packets to any interface
466 * filters.
467 */
468 if (_ether_cmp(eh->ether_dhost, IF_LLADDR(ifp))) {
469 m->m_flags |= M_PROMISC;
470 }
471 }
472
473 if (m->m_flags & M_HASFCS) {
474 /*
475 * If the M_HASFCS is set by the driver we want to make sure
476 * that we strip off the trailing FCS data before handing it
477 * up the stack.
478 */
479 m_adj(m, -ETHER_CRC_LEN);
480 m->m_flags &= ~M_HASFCS;
481 }
482
483 /* check for BOND */
484 if ((ifnet_eflags(ifp) & IFEF_BOND) != 0) {
485 *protocol_family = PF_BOND;
486 return 0;
487 }
488 /* check for VLAN */
489 if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) != 0) {
490 if (EVL_VLANOFTAG(m->m_pkthdr.vlan_tag) != 0) {
491 *protocol_family = PF_VLAN;
492 return 0;
493 }
494 /* the packet is just priority-tagged, clear the bit */
495 m->m_pkthdr.csum_flags &= ~CSUM_VLAN_TAG_VALID;
496 m->m_pkthdr.vlan_tag = 0;
497 } else if (ether_type == htons(ETHERTYPE_VLAN)) {
498 uint16_t tag;
499
500 if (!ether_remove_vlan_encapsulation(ifp, m, frame_header, &tag,
501 ðer_type)) {
502 m_freem(m);
503 return EJUSTRETURN;
504 }
505 /* check whether a VLAN tag is set */
506 if (tag != 0) {
507 /* make it look like it was hardware tagged */
508 *protocol_family = PF_VLAN;
509 m->m_pkthdr.csum_flags |= CSUM_VLAN_TAG_VALID;
510 m->m_pkthdr.vlan_tag = tag;
511 return 0;
512 }
513 /* just priority-tagged, let packet continue */
514 eh = mbuf_pkthdr_header(m);
515 }
516 if (ether_type == htons(ETHERTYPE_ARP)) {
517 m->m_pkthdr.pkt_flags |= PKTF_INET_RESOLVE; /* ARP packet */
518 }
519 data = mtod(m, u_int8_t*);
520
521 /*
522 * Determine the packet's protocol type and stuff the protocol into
523 * longs for quick compares.
524 */
525 if (ntohs(ether_type) <= 1500) {
526 bcopy(data, &extProto1, sizeof(u_int32_t));
527
528 /* SAP or SNAP */
529 if ((extProto1 & htonl(0xFFFFFF00)) == htonl(0xAAAA0300)) {
530 /* SNAP */
531 type = DLIL_DESC_SNAP;
532 bcopy(data + sizeof(u_int32_t), &extProto2,
533 sizeof(u_int32_t));
534 extProto1 &= htonl(0x000000FF);
535 } else {
536 type = DLIL_DESC_SAP;
537 extProto1 &= htonl(0xFFFFFF00);
538 }
539 } else {
540 type = DLIL_DESC_ETYPE2;
541 }
542
543 /*
544 * Search through the connected protocols for a match.
545 */
546 switch (type) {
547 case DLIL_DESC_ETYPE2:
548 for (i = 0; i < maxd; i++) {
549 if ((ed[i].type == type) &&
550 (ed[i].data[0] == ether_type)) {
551 *protocol_family = ed[i].protocol_family;
552 return 0;
553 }
554 }
555 break;
556
557 case DLIL_DESC_SAP:
558 for (i = 0; i < maxd; i++) {
559 if ((ed[i].type == type) &&
560 (ed[i].data[0] == extProto1)) {
561 *protocol_family = ed[i].protocol_family;
562 return 0;
563 }
564 }
565 break;
566
567 case DLIL_DESC_SNAP:
568 for (i = 0; i < maxd; i++) {
569 if ((ed[i].type == type) &&
570 (ed[i].data[0] == extProto1) &&
571 (ed[i].data[1] == extProto2)) {
572 *protocol_family = ed[i].protocol_family;
573 return 0;
574 }
575 }
576 break;
577 }
578
579 return ENOENT;
580 }
581
582 /*
583 * On embedded, ether_frameout is practicaly ether_frameout_extended.
584 * On non-embedded, ether_frameout has long been exposed as a public KPI,
585 * and therefore its signature must remain the same (without the pre- and
586 * postpend length parameters.)
587 */
588 #if KPI_INTERFACE_EMBEDDED
589 int
ether_frameout(struct ifnet * ifp,struct mbuf ** m,const struct sockaddr * ndest,const char * edst,const char * ether_type,u_int32_t * prepend_len,u_int32_t * postpend_len)590 ether_frameout(struct ifnet *ifp, struct mbuf **m,
591 const struct sockaddr *ndest, const char *edst,
592 const char *ether_type, u_int32_t *prepend_len, u_int32_t *postpend_len)
593 #else /* !KPI_INTERFACE_EMBEDDED */
594 int
595 ether_frameout(struct ifnet *ifp, struct mbuf **m,
596 const struct sockaddr *ndest, const char *edst,
597 const char *ether_type)
598 #endif /* KPI_INTERFACE_EMBEDDED */
599 {
600 #if KPI_INTERFACE_EMBEDDED
601 return ether_frameout_extended(ifp, m, ndest, edst, ether_type,
602 prepend_len, postpend_len);
603 #else /* !KPI_INTERFACE_EMBEDDED */
604 return ether_frameout_extended(ifp, m, ndest, edst, ether_type,
605 NULL, NULL);
606 #endif /* !KPI_INTERFACE_EMBEDDED */
607 }
608
609 /*
610 * Ethernet output routine.
611 * Encapsulate a packet of type family for the local net.
612 * Use trailer local net encapsulation if enough data in first
613 * packet leaves a multiple of 512 bytes of data in remainder.
614 */
615 int
ether_frameout_extended(struct ifnet * ifp,struct mbuf ** m,const struct sockaddr * ndest,const char * edst,const char * ether_type,u_int32_t * prepend_len,u_int32_t * postpend_len)616 ether_frameout_extended(struct ifnet *ifp, struct mbuf **m,
617 const struct sockaddr *ndest, const char *edst,
618 const char *ether_type, u_int32_t *prepend_len, u_int32_t *postpend_len)
619 {
620 struct ether_header *eh;
621 int hlen = ETHER_HDR_LEN; /* link layer header length */
622 const uint8_t *dst_laddr = dlil_link_addr(edst);
623 const uint8_t *frame_type = dlil_frame_type(ether_type);
624
625 /*
626 * If a simplex interface, and the packet is being sent to our
627 * Ethernet address or a broadcast address, loopback a copy.
628 * XXX To make a simplex device behave exactly like a duplex
629 * device, we should copy in the case of sending to our own
630 * ethernet address (thus letting the original actually appear
631 * on the wire). However, we don't do that here for security
632 * reasons and compatibility with the original behavior.
633 */
634 if ((ifp->if_flags & IFF_SIMPLEX) &&
635 ((*m)->m_flags & M_LOOP) && lo_ifp != NULL) {
636 if ((*m)->m_flags & M_BCAST) {
637 struct mbuf *n = m_copy(*m, 0, (int)M_COPYALL);
638 if (n != NULL) {
639 dlil_output(lo_ifp, ndest->sa_family,
640 n, NULL, ndest, DLIL_OUTPUT_FLAGS_NONE,
641 NULL);
642 }
643 } else if (_ether_cmp(edst, IF_LLADDR(ifp)) == 0) {
644 dlil_output(lo_ifp, ndest->sa_family, *m,
645 NULL, ndest, DLIL_OUTPUT_FLAGS_NONE, NULL);
646 return EJUSTRETURN;
647 }
648 }
649
650 /*
651 * Add local net header. If no space in first mbuf,
652 * allocate another.
653 */
654 if (ifp->if_type == IFT_L2VLAN) {
655 /* leave room for VLAN encapsulation */
656 hlen += ETHER_VLAN_ENCAP_LEN;
657 }
658 M_PREPEND(*m, hlen, M_DONTWAIT, 0);
659 if (*m == NULL) {
660 return EJUSTRETURN;
661 }
662
663 if (prepend_len != NULL) {
664 *prepend_len = ETHER_HDR_LEN;
665 }
666 if (postpend_len != NULL) {
667 *postpend_len = 0;
668 }
669 if (ifp->if_type == IFT_L2VLAN) {
670 m_adj(*m, ETHER_VLAN_ENCAP_LEN);
671 }
672 eh = mtod(*m, struct ether_header *);
673 (void) memcpy(&eh->ether_type, frame_type, sizeof(eh->ether_type));
674 (void) memcpy(eh->ether_dhost, dst_laddr, ETHER_ADDR_LEN);
675 (void) memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
676
677 return 0;
678 }
679
680 errno_t
ether_check_multi(ifnet_t ifp,const struct sockaddr * proto_addr)681 ether_check_multi(ifnet_t ifp, const struct sockaddr *proto_addr)
682 {
683 #pragma unused(ifp)
684 errno_t result = EAFNOSUPPORT;
685 const u_char *e_addr;
686
687 /*
688 * AF_SPEC and AF_LINK don't require translation. We do
689 * want to verify that they specify a valid multicast.
690 */
691 switch (proto_addr->sa_family) {
692 case AF_UNSPEC:
693 e_addr = (const u_char*)&proto_addr->sa_data[0];
694 if ((e_addr[0] & 0x01) != 0x01) {
695 result = EADDRNOTAVAIL;
696 } else {
697 result = 0;
698 }
699 break;
700
701 case AF_LINK:
702 e_addr = CONST_LLADDR(SDL(proto_addr));
703 if ((e_addr[0] & 0x01) != 0x01) {
704 result = EADDRNOTAVAIL;
705 } else {
706 result = 0;
707 }
708 break;
709 }
710
711 return result;
712 }
713
714 int
ether_ioctl(ifnet_t ifp,u_int32_t command,void * data)715 ether_ioctl(ifnet_t ifp, u_int32_t command, void *data)
716 {
717 #pragma unused(ifp, command, data)
718 return EOPNOTSUPP;
719 }
720
721 __private_extern__ int
ether_family_init(void)722 ether_family_init(void)
723 {
724 errno_t error = 0;
725
726 /* Register protocol registration functions */
727 if ((error = proto_register_plumber(PF_INET, APPLE_IF_FAM_ETHERNET,
728 ether_attach_inet, ether_detach_inet)) != 0) {
729 printf("proto_register_plumber failed for PF_INET error=%d\n",
730 error);
731 goto done;
732 }
733 if ((error = proto_register_plumber(PF_INET6, APPLE_IF_FAM_ETHERNET,
734 ether_attach_inet6, ether_detach_inet6)) != 0) {
735 printf("proto_register_plumber failed for PF_INET6 error=%d\n",
736 error);
737 goto done;
738 }
739 #if VLAN
740 vlan_family_init();
741 #endif /* VLAN */
742 #if BOND
743 bond_family_init();
744 #endif /* BOND */
745 #if IF_BRIDGE
746 bridgeattach(0);
747 #endif /* IF_BRIDGE */
748 #if IF_FAKE
749 if_fake_init();
750 #endif /* IF_FAKE */
751 #if IF_HEADLESS
752 if_headless_init();
753 #endif /* IF_HEADLESS */
754 done:
755
756 return error;
757 }
758