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 = eh->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 ((eh->ether_dhost[0] & 1) != 0) {
463 /* Check for broadcast */
464 if (_ether_cmp(etherbroadcastaddr, eh->ether_dhost) == 0) {
465 m->m_flags |= M_BCAST;
466 } else {
467 m->m_flags |= M_MCAST;
468 }
469 } else {
470 /*
471 * When the driver is put into promiscuous mode we may receive
472 * unicast frames that are not intended for our interfaces.
473 * They are marked here as being promiscuous so the caller may
474 * dispose of them after passing the packets to any interface
475 * filters.
476 */
477 if (_ether_cmp(eh->ether_dhost, IF_LLADDR(ifp))) {
478 m->m_flags |= M_PROMISC;
479 }
480 }
481
482 if (m->m_flags & M_HASFCS) {
483 /*
484 * If the M_HASFCS is set by the driver we want to make sure
485 * that we strip off the trailing FCS data before handing it
486 * up the stack.
487 */
488 m_adj(m, -ETHER_CRC_LEN);
489 m->m_flags &= ~M_HASFCS;
490 }
491
492 /* check for BOND */
493 if ((ifnet_eflags(ifp) & IFEF_BOND) != 0) {
494 *protocol_family = PF_BOND;
495 return 0;
496 }
497 /* check for VLAN */
498 if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) != 0) {
499 if (EVL_VLANOFTAG(m->m_pkthdr.vlan_tag) != 0) {
500 *protocol_family = PF_VLAN;
501 return 0;
502 }
503 /* the packet is just priority-tagged, clear the bit */
504 m->m_pkthdr.csum_flags &= ~CSUM_VLAN_TAG_VALID;
505 m->m_pkthdr.vlan_tag = 0;
506 } else if (ether_type == htons(ETHERTYPE_VLAN)) {
507 uint16_t tag;
508
509 if (!ether_remove_vlan_encapsulation(ifp, m, frame_header, &tag,
510 ðer_type)) {
511 m_freem(m);
512 return EJUSTRETURN;
513 }
514 /* check whether a VLAN tag is set */
515 if (tag != 0) {
516 /* make it look like it was hardware tagged */
517 *protocol_family = PF_VLAN;
518 m->m_pkthdr.csum_flags |= CSUM_VLAN_TAG_VALID;
519 m->m_pkthdr.vlan_tag = tag;
520 return 0;
521 }
522 /* just priority-tagged, let packet continue */
523 eh = mbuf_pkthdr_header(m);
524 }
525 if (ether_type == htons(ETHERTYPE_ARP)) {
526 m->m_pkthdr.pkt_flags |= PKTF_INET_RESOLVE; /* ARP packet */
527 }
528 data = mtod(m, u_int8_t*);
529
530 /*
531 * Determine the packet's protocol type and stuff the protocol into
532 * longs for quick compares.
533 */
534 if (ntohs(ether_type) <= 1500) {
535 bcopy(data, &extProto1, sizeof(u_int32_t));
536
537 /* SAP or SNAP */
538 if ((extProto1 & htonl(0xFFFFFF00)) == htonl(0xAAAA0300)) {
539 /* SNAP */
540 type = DLIL_DESC_SNAP;
541 bcopy(data + sizeof(u_int32_t), &extProto2,
542 sizeof(u_int32_t));
543 extProto1 &= htonl(0x000000FF);
544 } else {
545 type = DLIL_DESC_SAP;
546 extProto1 &= htonl(0xFFFFFF00);
547 }
548 } else {
549 type = DLIL_DESC_ETYPE2;
550 }
551
552 /*
553 * Search through the connected protocols for a match.
554 */
555 switch (type) {
556 case DLIL_DESC_ETYPE2:
557 for (i = 0; i < maxd; i++) {
558 if ((ed[i].type == type) &&
559 (ed[i].data[0] == ether_type)) {
560 *protocol_family = ed[i].protocol_family;
561 return 0;
562 }
563 }
564 break;
565
566 case DLIL_DESC_SAP:
567 for (i = 0; i < maxd; i++) {
568 if ((ed[i].type == type) &&
569 (ed[i].data[0] == extProto1)) {
570 *protocol_family = ed[i].protocol_family;
571 return 0;
572 }
573 }
574 break;
575
576 case DLIL_DESC_SNAP:
577 for (i = 0; i < maxd; i++) {
578 if ((ed[i].type == type) &&
579 (ed[i].data[0] == extProto1) &&
580 (ed[i].data[1] == extProto2)) {
581 *protocol_family = ed[i].protocol_family;
582 return 0;
583 }
584 }
585 break;
586 }
587
588 return ENOENT;
589 }
590
591 /*
592 * On embedded, ether_frameout is practicaly ether_frameout_extended.
593 * On non-embedded, ether_frameout has long been exposed as a public KPI,
594 * and therefore its signature must remain the same (without the pre- and
595 * postpend length parameters.)
596 */
597 #if KPI_INTERFACE_EMBEDDED
598 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)599 ether_frameout(struct ifnet *ifp, struct mbuf **m,
600 const struct sockaddr *ndest, const char *edst,
601 const char *ether_type, u_int32_t *prepend_len, u_int32_t *postpend_len)
602 #else /* !KPI_INTERFACE_EMBEDDED */
603 int
604 ether_frameout(struct ifnet *ifp, struct mbuf **m,
605 const struct sockaddr *ndest, const char *edst,
606 const char *ether_type)
607 #endif /* KPI_INTERFACE_EMBEDDED */
608 {
609 #if KPI_INTERFACE_EMBEDDED
610 return ether_frameout_extended(ifp, m, ndest, edst, ether_type,
611 prepend_len, postpend_len);
612 #else /* !KPI_INTERFACE_EMBEDDED */
613 return ether_frameout_extended(ifp, m, ndest, edst, ether_type,
614 NULL, NULL);
615 #endif /* !KPI_INTERFACE_EMBEDDED */
616 }
617
618 /*
619 * Ethernet output routine.
620 * Encapsulate a packet of type family for the local net.
621 * Use trailer local net encapsulation if enough data in first
622 * packet leaves a multiple of 512 bytes of data in remainder.
623 */
624 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)625 ether_frameout_extended(struct ifnet *ifp, struct mbuf **m,
626 const struct sockaddr *ndest, const char *edst,
627 const char *ether_type, u_int32_t *prepend_len, u_int32_t *postpend_len)
628 {
629 struct ether_header *eh;
630 int hlen = ETHER_HDR_LEN; /* link layer header length */
631 const uint8_t *dst_laddr = dlil_link_addr(edst);
632 const uint8_t *frame_type = dlil_frame_type(ether_type);
633
634 /*
635 * If a simplex interface, and the packet is being sent to our
636 * Ethernet address or a broadcast address, loopback a copy.
637 * XXX To make a simplex device behave exactly like a duplex
638 * device, we should copy in the case of sending to our own
639 * ethernet address (thus letting the original actually appear
640 * on the wire). However, we don't do that here for security
641 * reasons and compatibility with the original behavior.
642 */
643 if ((ifp->if_flags & IFF_SIMPLEX) &&
644 ((*m)->m_flags & M_LOOP) && lo_ifp != NULL) {
645 if ((*m)->m_flags & M_BCAST) {
646 struct mbuf *n = m_copy(*m, 0, (int)M_COPYALL);
647 if (n != NULL) {
648 dlil_output(lo_ifp, ndest->sa_family,
649 n, NULL, ndest, DLIL_OUTPUT_FLAGS_NONE,
650 NULL);
651 }
652 } else if (_ether_cmp(edst, IF_LLADDR(ifp)) == 0) {
653 dlil_output(lo_ifp, ndest->sa_family, *m,
654 NULL, ndest, DLIL_OUTPUT_FLAGS_NONE, NULL);
655 return EJUSTRETURN;
656 }
657 }
658
659 /*
660 * Add local net header. If no space in first mbuf,
661 * allocate another.
662 */
663 if (ifp->if_type == IFT_L2VLAN) {
664 /* leave room for VLAN encapsulation */
665 hlen += ETHER_VLAN_ENCAP_LEN;
666 }
667 M_PREPEND(*m, hlen, M_DONTWAIT, 0);
668 if (*m == NULL) {
669 return EJUSTRETURN;
670 }
671
672 if (prepend_len != NULL) {
673 *prepend_len = ETHER_HDR_LEN;
674 }
675 if (postpend_len != NULL) {
676 *postpend_len = 0;
677 }
678 if (ifp->if_type == IFT_L2VLAN) {
679 m_adj(*m, ETHER_VLAN_ENCAP_LEN);
680 }
681 eh = mtod(*m, struct ether_header *);
682 (void) memcpy(&eh->ether_type, frame_type, sizeof(eh->ether_type));
683 (void) memcpy(eh->ether_dhost, dst_laddr, ETHER_ADDR_LEN);
684 (void) memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
685
686 return 0;
687 }
688
689 errno_t
ether_check_multi(ifnet_t ifp,const struct sockaddr * proto_addr)690 ether_check_multi(ifnet_t ifp, const struct sockaddr *proto_addr)
691 {
692 #pragma unused(ifp)
693 errno_t result = EAFNOSUPPORT;
694 const u_char *e_addr;
695
696 /*
697 * AF_SPEC and AF_LINK don't require translation. We do
698 * want to verify that they specify a valid multicast.
699 */
700 switch (proto_addr->sa_family) {
701 case AF_UNSPEC:
702 e_addr = (const u_char*)&proto_addr->sa_data[0];
703 if ((e_addr[0] & 0x01) != 0x01) {
704 result = EADDRNOTAVAIL;
705 } else {
706 result = 0;
707 }
708 break;
709
710 case AF_LINK:
711 e_addr = CONST_LLADDR(SDL(proto_addr));
712 if ((e_addr[0] & 0x01) != 0x01) {
713 result = EADDRNOTAVAIL;
714 } else {
715 result = 0;
716 }
717 break;
718 }
719
720 return result;
721 }
722
723 int
ether_ioctl(ifnet_t ifp,u_int32_t command,void * data)724 ether_ioctl(ifnet_t ifp, u_int32_t command, void *data)
725 {
726 #pragma unused(ifp, command, data)
727 return EOPNOTSUPP;
728 }
729
730 __private_extern__ int
ether_family_init(void)731 ether_family_init(void)
732 {
733 errno_t error = 0;
734
735 /* Register protocol registration functions */
736 if ((error = proto_register_plumber(PF_INET, APPLE_IF_FAM_ETHERNET,
737 ether_attach_inet, ether_detach_inet)) != 0) {
738 printf("proto_register_plumber failed for PF_INET error=%d\n",
739 error);
740 goto done;
741 }
742 if ((error = proto_register_plumber(PF_INET6, APPLE_IF_FAM_ETHERNET,
743 ether_attach_inet6, ether_detach_inet6)) != 0) {
744 printf("proto_register_plumber failed for PF_INET6 error=%d\n",
745 error);
746 goto done;
747 }
748 #if VLAN
749 vlan_family_init();
750 #endif /* VLAN */
751 #if BOND
752 bond_family_init();
753 #endif /* BOND */
754 #if IF_BRIDGE
755 bridgeattach(0);
756 #endif /* IF_BRIDGE */
757 #if IF_FAKE
758 if_fake_init();
759 #endif /* IF_FAKE */
760 #if IF_HEADLESS
761 if_headless_init();
762 #endif /* IF_HEADLESS */
763 done:
764
765 return error;
766 }
767