]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/ether_if_module.c
xnu-4570.1.46.tar.gz
[apple/xnu.git] / bsd / net / ether_if_module.c
CommitLineData
1c79356b 1/*
39236c6e 2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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
1c79356b
A
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
2d21ac55
A
71#include <pexpert/pexpert.h>
72
b0d623f7 73#define etherbroadcastaddr fugly
1c79356b 74#include <net/if.h>
1c79356b
A
75#include <net/route.h>
76#include <net/if_llc.h>
77#include <net/if_dl.h>
78#include <net/if_types.h>
91447636 79#include <net/if_ether.h>
39236c6e 80#include <net/if_gif.h>
1c79356b 81#include <netinet/if_ether.h>
9bccf70c 82#include <netinet/in.h> /* For M_LOOP */
2d21ac55
A
83#include <net/kpi_interface.h>
84#include <net/kpi_protocol.h>
b0d623f7 85#undef etherbroadcastaddr
1c79356b
A
86
87/*
88#if INET
89#include <netinet/in.h>
90#include <netinet/in_var.h>
91
92#include <netinet/in_systm.h>
93#include <netinet/ip.h>
94#endif
95*/
2d21ac55 96#include <net/ether_if_module.h>
1c79356b 97#include <sys/socketvar.h>
4a249263 98#include <net/if_vlan_var.h>
316670eb
A
99#if BOND
100#include <net/if_bond_internal.h>
101#endif /* BOND */
6d2010ae
A
102#if IF_BRIDGE
103#include <net/if_bridgevar.h>
104#endif /* IF_BRIDGE */
5ba3f43e
A
105#if IF_FAKE
106#include <net/if_fake_var.h>
107#endif /* IF_FAKE */
1c79356b
A
108
109#include <net/dlil.h>
110
91447636 111SYSCTL_DECL(_net_link);
39236c6e
A
112SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW|CTLFLAG_LOCKED, 0,
113 "Ethernet");
1c79356b 114
1c79356b 115struct en_desc {
39236c6e
A
116 u_int16_t type; /* Type of protocol stored in data */
117 u_int32_t protocol_family; /* Protocol family */
118 u_int32_t data[2]; /* Protocol data */
1c79356b 119};
2d21ac55 120
91447636 121/* descriptors are allocated in blocks of ETHER_DESC_BLK_SIZE */
5ba3f43e
A
122#if CONFIG_EMBEDDED
123#define ETHER_DESC_BLK_SIZE (2) /* IP, ARP */
124#else
2d21ac55 125#define ETHER_DESC_BLK_SIZE (10)
5ba3f43e 126#endif
1c79356b
A
127
128/*
6d2010ae 129 * Header for the demux list, hangs off of IFP at if_family_cookie
1c79356b 130 */
1c79356b 131struct ether_desc_blk_str {
b0d623f7
A
132 u_int32_t n_max_used;
133 u_int32_t n_count;
134 u_int32_t n_used;
91447636 135 struct en_desc block_ptr[1];
1c79356b 136};
39236c6e 137
91447636 138/* Size of the above struct before the array of struct en_desc */
39236c6e
A
139#define ETHER_DESC_HEADER_SIZE \
140 ((size_t) offsetof(struct ether_desc_blk_str, block_ptr))
1c79356b 141
39236c6e
A
142__private_extern__ u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
143 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
55e303ae 144
1c79356b 145/*
91447636 146 * Release all descriptor entries owned by this protocol (there may be several).
7b1edb79 147 * Setting the type to 0 releases the entry. Eventually we should compact-out
1c79356b
A
148 * the unused entries.
149 */
91447636 150int
39236c6e 151ether_del_proto(ifnet_t ifp, protocol_family_t protocol_family)
1c79356b 152{
39236c6e
A
153 struct ether_desc_blk_str *desc_blk =
154 (struct ether_desc_blk_str *)ifp->if_family_cookie;
155 u_int32_t current = 0;
91447636 156 int found = 0;
39236c6e 157
91447636 158 if (desc_blk == NULL)
39236c6e
A
159 return (0);
160
91447636 161 for (current = desc_blk->n_max_used; current > 0; current--) {
39236c6e
A
162 if (desc_blk->block_ptr[current - 1].protocol_family ==
163 protocol_family) {
91447636
A
164 found = 1;
165 desc_blk->block_ptr[current - 1].type = 0;
166 desc_blk->n_used--;
167 }
168 }
39236c6e 169
91447636 170 if (desc_blk->n_used == 0) {
6d2010ae
A
171 FREE(ifp->if_family_cookie, M_IFADDR);
172 ifp->if_family_cookie = 0;
39236c6e 173 } else {
91447636 174 /* Decrement n_max_used */
39236c6e
A
175 for (; desc_blk->n_max_used > 0 &&
176 desc_blk->block_ptr[desc_blk->n_max_used - 1].type == 0;
177 desc_blk->n_max_used--)
91447636
A
178 ;
179 }
1c79356b 180
39236c6e
A
181 return (0);
182}
1c79356b 183
91447636 184static int
39236c6e
A
185ether_add_proto_internal(struct ifnet *ifp, protocol_family_t protocol,
186 const struct ifnet_demux_desc *demux)
91447636
A
187{
188 struct en_desc *ed;
39236c6e
A
189 struct ether_desc_blk_str *desc_blk =
190 (struct ether_desc_blk_str *)ifp->if_family_cookie;
91447636 191 u_int32_t i;
39236c6e 192
91447636 193 switch (demux->type) {
39236c6e
A
194 /* These types are supported */
195 /* Top three are preferred */
196 case DLIL_DESC_ETYPE2:
197 if (demux->datalen != 2)
198 return (EINVAL);
199 break;
200
201 case DLIL_DESC_SAP:
202 if (demux->datalen != 3)
203 return (EINVAL);
204 break;
205
206 case DLIL_DESC_SNAP:
207 if (demux->datalen != 5)
208 return (EINVAL);
209 break;
210
211 default:
212 return (ENOTSUP);
213 }
214
215 /* Verify a matching descriptor does not exist */
216 if (desc_blk != NULL) {
217 switch (demux->type) {
91447636 218 case DLIL_DESC_ETYPE2:
39236c6e
A
219 for (i = 0; i < desc_blk->n_max_used; i++) {
220 if (desc_blk->block_ptr[i].type ==
221 DLIL_DESC_ETYPE2 &&
222 desc_blk->block_ptr[i].data[0] ==
223 *(u_int16_t*)demux->data) {
224 return (EADDRINUSE);
225 }
91447636
A
226 }
227 break;
91447636 228 case DLIL_DESC_SAP:
91447636 229 case DLIL_DESC_SNAP:
39236c6e
A
230 for (i = 0; i < desc_blk->n_max_used; i++) {
231 if (desc_blk->block_ptr[i].type ==
232 demux->type &&
233 bcmp(desc_blk->block_ptr[i].data,
234 demux->data, demux->datalen) == 0) {
235 return (EADDRINUSE);
236 }
91447636
A
237 }
238 break;
91447636
A
239 }
240 }
39236c6e
A
241
242 /* Check for case where all of the descriptor blocks are in use */
91447636
A
243 if (desc_blk == NULL || desc_blk->n_used == desc_blk->n_count) {
244 struct ether_desc_blk_str *tmp;
39236c6e
A
245 u_int32_t new_count = ETHER_DESC_BLK_SIZE;
246 u_int32_t new_size;
247 u_int32_t old_size = 0;
248
91447636 249 i = 0;
39236c6e 250
91447636
A
251 if (desc_blk) {
252 new_count += desc_blk->n_count;
39236c6e
A
253 old_size = desc_blk->n_count * sizeof (struct en_desc) +
254 ETHER_DESC_HEADER_SIZE;
91447636
A
255 i = desc_blk->n_used;
256 }
39236c6e
A
257
258 new_size = new_count * sizeof (struct en_desc) +
259 ETHER_DESC_HEADER_SIZE;
260
91447636 261 tmp = _MALLOC(new_size, M_IFADDR, M_WAITOK);
39236c6e 262 if (tmp == NULL) {
91447636
A
263 /*
264 * Remove any previous descriptors set in the call.
265 */
39236c6e 266 return (ENOMEM);
91447636 267 }
2d21ac55
A
268
269 bzero(((char *)tmp) + old_size, new_size - old_size);
91447636
A
270 if (desc_blk) {
271 bcopy(desc_blk, tmp, old_size);
272 FREE(desc_blk, M_IFADDR);
273 }
274 desc_blk = tmp;
6d2010ae 275 ifp->if_family_cookie = (uintptr_t)desc_blk;
91447636 276 desc_blk->n_count = new_count;
39236c6e 277 } else {
91447636
A
278 /* Find a free entry */
279 for (i = 0; i < desc_blk->n_count; i++) {
280 if (desc_blk->block_ptr[i].type == 0) {
281 break;
282 }
283 }
284 }
39236c6e 285
91447636
A
286 /* Bump n_max_used if appropriate */
287 if (i + 1 > desc_blk->n_max_used) {
288 desc_blk->n_max_used = i + 1;
289 }
39236c6e 290
91447636
A
291 ed = &desc_blk->block_ptr[i];
292 ed->protocol_family = protocol;
293 ed->data[0] = 0;
294 ed->data[1] = 0;
39236c6e 295
91447636 296 switch (demux->type) {
39236c6e
A
297 case DLIL_DESC_ETYPE2:
298 /* 2 byte ethernet raw protocol type is at native_type */
299 /* prtocol must be in network byte order */
300 ed->type = DLIL_DESC_ETYPE2;
301 ed->data[0] = *(u_int16_t*)demux->data;
302 break;
303
304 case DLIL_DESC_SAP:
305 ed->type = DLIL_DESC_SAP;
306 bcopy(demux->data, &ed->data[0], 3);
307 break;
308
309 case DLIL_DESC_SNAP: {
310 u_int8_t* pDest = ((u_int8_t*)&ed->data[0]) + 3;
311 ed->type = DLIL_DESC_SNAP;
312 bcopy(demux->data, pDest, 5);
313 break;
91447636 314 }
39236c6e
A
315 }
316
91447636 317 desc_blk->n_used++;
39236c6e
A
318
319 return (0);
91447636 320}
1c79356b 321
91447636 322int
39236c6e
A
323ether_add_proto(ifnet_t ifp, protocol_family_t protocol,
324 const struct ifnet_demux_desc *demux_list, u_int32_t demux_count)
91447636 325{
39236c6e
A
326 int error = 0;
327 u_int32_t i;
328
91447636
A
329 for (i = 0; i < demux_count; i++) {
330 error = ether_add_proto_internal(ifp, protocol, &demux_list[i]);
331 if (error) {
332 ether_del_proto(ifp, protocol);
333 break;
334 }
335 }
39236c6e
A
336
337 return (error);
91447636 338}
4a249263 339
91447636 340int
39236c6e
A
341ether_demux(ifnet_t ifp, mbuf_t m, char *frame_header,
342 protocol_family_t *protocol_family)
1c79356b 343{
316670eb 344 struct ether_header *eh = (struct ether_header *)(void *)frame_header;
39236c6e
A
345 u_short ether_type = eh->ether_type;
346 u_int16_t type;
347 u_int8_t *data;
348 u_int32_t i = 0;
349 struct ether_desc_blk_str *desc_blk =
350 (struct ether_desc_blk_str *)ifp->if_family_cookie;
351 u_int32_t maxd = desc_blk ? desc_blk->n_max_used : 0;
91447636 352 struct en_desc *ed = desc_blk ? desc_blk->block_ptr : NULL;
39236c6e
A
353 u_int32_t extProto1 = 0;
354 u_int32_t extProto2 = 0;
91447636
A
355
356 if (eh->ether_dhost[0] & 1) {
357 /* Check for broadcast */
2d21ac55 358 if (_ether_cmp(etherbroadcastaddr, eh->ether_dhost) == 0)
91447636
A
359 m->m_flags |= M_BCAST;
360 else
361 m->m_flags |= M_MCAST;
362 }
1c79356b 363
6d2010ae
A
364 if (m->m_flags & M_HASFCS) {
365 /*
366 * If the M_HASFCS is set by the driver we want to make sure
367 * that we strip off the trailing FCS data before handing it
368 * up the stack.
369 */
370 m_adj(m, -ETHER_CRC_LEN);
371 m->m_flags &= ~M_HASFCS;
372 }
373
91447636
A
374 if (ifp->if_eflags & IFEF_BOND) {
375 /* if we're bonded, bond "protocol" gets all the packets */
376 *protocol_family = PF_BOND;
377 return (0);
378 }
1c79356b 379
91447636
A
380 if ((eh->ether_dhost[0] & 1) == 0) {
381 /*
39236c6e
A
382 * When the driver is put into promiscuous mode we may receive
383 * unicast frames that are not intended for our interfaces.
384 * They are marked here as being promiscuous so the caller may
385 * dispose of them after passing the packets to any interface
386 * filters.
2d21ac55 387 */
39236c6e 388 if (_ether_cmp(eh->ether_dhost, IF_LLADDR(ifp))) {
91447636
A
389 m->m_flags |= M_PROMISC;
390 }
391 }
39236c6e 392
593a1d5f
A
393 /* check for VLAN */
394 if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) != 0) {
395 if (EVL_VLANOFTAG(m->m_pkthdr.vlan_tag) != 0) {
396 *protocol_family = PF_VLAN;
397 return (0);
398 }
399 /* the packet is just priority-tagged, clear the bit */
400 m->m_pkthdr.csum_flags &= ~CSUM_VLAN_TAG_VALID;
39236c6e 401 } else if (ether_type == htons(ETHERTYPE_VLAN)) {
593a1d5f
A
402 struct ether_vlan_header * evl;
403
316670eb 404 evl = (struct ether_vlan_header *)(void *)frame_header;
39236c6e
A
405 if (m->m_len < ETHER_VLAN_ENCAP_LEN ||
406 ntohs(evl->evl_proto) == ETHERTYPE_VLAN ||
407 EVL_VLANOFTAG(ntohs(evl->evl_tag)) != 0) {
593a1d5f 408 *protocol_family = PF_VLAN;
39236c6e 409 return (0);
593a1d5f
A
410 }
411 /* the packet is just priority-tagged */
412
413 /* make the encapsulated ethertype the actual ethertype */
414 ether_type = evl->evl_encap_proto = evl->evl_proto;
415
416 /* remove the encapsulation header */
417 m->m_len -= ETHER_VLAN_ENCAP_LEN;
418 m->m_data += ETHER_VLAN_ENCAP_LEN;
419 m->m_pkthdr.len -= ETHER_VLAN_ENCAP_LEN;
420 m->m_pkthdr.csum_flags = 0; /* can't trust hardware checksum */
39236c6e
A
421 } else if (ether_type == htons(ETHERTYPE_ARP))
422 m->m_pkthdr.pkt_flags |= PKTF_INET_RESOLVE; /* ARP packet */
423
91447636 424 data = mtod(m, u_int8_t*);
39236c6e 425
91447636
A
426 /*
427 * Determine the packet's protocol type and stuff the protocol into
428 * longs for quick compares.
429 */
91447636 430 if (ntohs(ether_type) <= 1500) {
316670eb 431 bcopy(data, &extProto1, sizeof (u_int32_t));
39236c6e
A
432
433 /* SAP or SNAP */
91447636 434 if ((extProto1 & htonl(0xFFFFFF00)) == htonl(0xAAAA0300)) {
39236c6e 435 /* SNAP */
91447636 436 type = DLIL_DESC_SNAP;
39236c6e
A
437 bcopy(data + sizeof (u_int32_t), &extProto2,
438 sizeof (u_int32_t));
91447636
A
439 extProto1 &= htonl(0x000000FF);
440 } else {
441 type = DLIL_DESC_SAP;
442 extProto1 &= htonl(0xFFFFFF00);
443 }
444 } else {
445 type = DLIL_DESC_ETYPE2;
446 }
39236c6e
A
447
448 /*
449 * Search through the connected protocols for a match.
91447636 450 */
91447636 451 switch (type) {
39236c6e
A
452 case DLIL_DESC_ETYPE2:
453 for (i = 0; i < maxd; i++) {
454 if ((ed[i].type == type) &&
455 (ed[i].data[0] == ether_type)) {
456 *protocol_family = ed[i].protocol_family;
457 return (0);
91447636 458 }
39236c6e
A
459 }
460 break;
461
462 case DLIL_DESC_SAP:
463 for (i = 0; i < maxd; i++) {
464 if ((ed[i].type == type) &&
465 (ed[i].data[0] == extProto1)) {
466 *protocol_family = ed[i].protocol_family;
467 return (0);
91447636 468 }
39236c6e 469 }
91447636 470 break;
39236c6e
A
471
472 case DLIL_DESC_SNAP:
473 for (i = 0; i < maxd; i++) {
474 if ((ed[i].type == type) &&
475 (ed[i].data[0] == extProto1) &&
476 (ed[i].data[1] == extProto2)) {
477 *protocol_family = ed[i].protocol_family;
478 return (0);
479 }
480 }
481 break;
91447636 482 }
39236c6e
A
483
484 return (ENOENT);
485}
486
487/*
488 * On embedded, ether_frameout is practicaly ether_frameout_extended.
489 * On non-embedded, ether_frameout has long been exposed as a public KPI,
490 * and therefore its signature must remain the same (without the pre- and
491 * postpend length parameters.)
492 */
493#if KPI_INTERFACE_EMBEDDED
494int
495ether_frameout(struct ifnet *ifp, struct mbuf **m,
496 const struct sockaddr *ndest, const char *edst,
497 const char *ether_type, u_int32_t *prepend_len, u_int32_t *postpend_len)
498#else /* !KPI_INTERFACE_EMBEDDED */
499int
500ether_frameout(struct ifnet *ifp, struct mbuf **m,
501 const struct sockaddr *ndest, const char *edst,
502 const char *ether_type)
503#endif /* KPI_INTERFACE_EMBEDDED */
504{
505#if KPI_INTERFACE_EMBEDDED
506 return (ether_frameout_extended(ifp, m, ndest, edst, ether_type,
507 prepend_len, postpend_len));
508#else /* !KPI_INTERFACE_EMBEDDED */
509 return (ether_frameout_extended(ifp, m, ndest, edst, ether_type,
510 NULL, NULL));
511#endif /* !KPI_INTERFACE_EMBEDDED */
512}
1c79356b
A
513
514/*
515 * Ethernet output routine.
516 * Encapsulate a packet of type family for the local net.
517 * Use trailer local net encapsulation if enough data in first
518 * packet leaves a multiple of 512 bytes of data in remainder.
1c79356b
A
519 */
520int
39236c6e
A
521ether_frameout_extended(struct ifnet *ifp, struct mbuf **m,
522 const struct sockaddr *ndest, const char *edst,
523 const char *ether_type, u_int32_t *prepend_len, u_int32_t *postpend_len)
1c79356b 524{
91447636 525 struct ether_header *eh;
4a249263 526 int hlen; /* link layer header length */
1c79356b
A
527
528 hlen = ETHER_HDR_LEN;
529
530 /*
531 * If a simplex interface, and the packet is being sent to our
532 * Ethernet address or a broadcast address, loopback a copy.
533 * XXX To make a simplex device behave exactly like a duplex
534 * device, we should copy in the case of sending to our own
535 * ethernet address (thus letting the original actually appear
536 * on the wire). However, we don't do that here for security
537 * reasons and compatibility with the original behavior.
538 */
539 if ((ifp->if_flags & IFF_SIMPLEX) &&
39236c6e
A
540 ((*m)->m_flags & M_LOOP) && lo_ifp != NULL) {
541 if ((*m)->m_flags & M_BCAST) {
542 struct mbuf *n = m_copy(*m, 0, (int)M_COPYALL);
543 if (n != NULL) {
544 dlil_output(lo_ifp, ndest->sa_family,
545 n, NULL, ndest, 0, NULL);
546 }
547 } else if (_ether_cmp(edst, IF_LLADDR(ifp)) == 0) {
548 dlil_output(lo_ifp, ndest->sa_family, *m,
549 NULL, ndest, 0, NULL);
550 return (EJUSTRETURN);
551 }
1c79356b 552 }
39236c6e 553
1c79356b
A
554 /*
555 * Add local net header. If no space in first mbuf,
556 * allocate another.
557 */
3e170ce0 558 M_PREPEND(*m, sizeof (struct ether_header), M_DONTWAIT, 0);
39236c6e
A
559 if (*m == NULL)
560 return (EJUSTRETURN);
561
562 if (prepend_len != NULL)
563 *prepend_len = sizeof (struct ether_header);
564 if (postpend_len != NULL)
565 *postpend_len = 0;
1c79356b 566
1c79356b 567 eh = mtod(*m, struct ether_header *);
39236c6e
A
568 (void) memcpy(&eh->ether_type, ether_type, sizeof(eh->ether_type));
569 (void) memcpy(eh->ether_dhost, edst, ETHER_ADDR_LEN);
570 ifnet_lladdr_copy_bytes(ifp, eh->ether_shost, ETHER_ADDR_LEN);
1c79356b 571
39236c6e 572 return (0);
1c79356b
A
573}
574
91447636 575errno_t
39236c6e 576ether_check_multi(ifnet_t ifp, const struct sockaddr *proto_addr)
91447636 577{
39236c6e 578#pragma unused(ifp)
91447636
A
579 errno_t result = EAFNOSUPPORT;
580 const u_char *e_addr;
39236c6e 581
91447636
A
582 /*
583 * AF_SPEC and AF_LINK don't require translation. We do
584 * want to verify that they specify a valid multicast.
585 */
586 switch(proto_addr->sa_family) {
39236c6e
A
587 case AF_UNSPEC:
588 e_addr = (const u_char*)&proto_addr->sa_data[0];
589 if ((e_addr[0] & 0x01) != 0x01)
590 result = EADDRNOTAVAIL;
591 else
592 result = 0;
593 break;
594
595 case AF_LINK:
596 e_addr = CONST_LLADDR((const struct sockaddr_dl*)
597 (uintptr_t)(size_t)proto_addr);
598 if ((e_addr[0] & 0x01) != 0x01)
599 result = EADDRNOTAVAIL;
600 else
601 result = 0;
602 break;
91447636 603 }
39236c6e
A
604
605 return (result);
91447636
A
606}
607
1c79356b 608int
39236c6e 609ether_ioctl(ifnet_t ifp, u_int32_t command, void *data)
1c79356b 610{
39236c6e
A
611#pragma unused(ifp, command, data)
612 return (EOPNOTSUPP);
1c79356b
A
613}
614
39236c6e
A
615__private_extern__ int
616ether_family_init(void)
1c79356b 617{
2d21ac55 618 errno_t error = 0;
39236c6e 619
91447636 620 /* Register protocol registration functions */
2d21ac55 621 if ((error = proto_register_plumber(PF_INET, APPLE_IF_FAM_ETHERNET,
39236c6e
A
622 ether_attach_inet, ether_detach_inet)) != 0) {
623 printf("proto_register_plumber failed for PF_INET error=%d\n",
624 error);
91447636
A
625 goto done;
626 }
2d21ac55
A
627#if INET6
628 if ((error = proto_register_plumber(PF_INET6, APPLE_IF_FAM_ETHERNET,
39236c6e
A
629 ether_attach_inet6, ether_detach_inet6)) != 0) {
630 printf("proto_register_plumber failed for PF_INET6 error=%d\n",
631 error);
2d21ac55
A
632 goto done;
633 }
634#endif /* INET6 */
2d21ac55 635#if VLAN
91447636 636 vlan_family_init();
2d21ac55
A
637#endif /* VLAN */
638#if BOND
91447636 639 bond_family_init();
2d21ac55 640#endif /* BOND */
6d2010ae
A
641#if IF_BRIDGE
642 bridgeattach(0);
643#endif /* IF_BRIDGE */
5ba3f43e
A
644#if IF_FAKE
645 if_fake_init();
646#endif /* IF_FAKE */
647
39236c6e 648done:
55e303ae 649
39236c6e 650 return (error);
1c79356b 651}