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