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