2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1982, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/malloc.h>
64 #include <sys/socket.h>
65 #include <sys/sockio.h>
66 #include <sys/sysctl.h>
69 #include <net/route.h>
70 #include <net/if_llc.h>
71 #include <net/if_dl.h>
72 #include <net/if_types.h>
73 #include <net/if_ether.h>
74 #include <netinet/if_ether.h>
75 #include <netinet/in.h> /* For M_LOOP */
79 #include <netinet/in.h>
80 #include <netinet/in_var.h>
82 #include <netinet/in_systm.h>
83 #include <netinet/ip.h>
87 #include <sys/socketvar.h>
88 #include <net/if_vlan_var.h>
89 #include <net/if_bond_var.h>
94 extern struct ifqueue pkintrq
;
97 /* General stuff from if_ethersubr.c - may not need some of it */
99 #include <netat/at_pat.h>
101 extern struct ifqueue atalkintrq
;
106 #include <net/bridge.h>
109 #define memcpy(x,y,z) bcopy(y, x, z)
112 SYSCTL_DECL(_net_link
);
113 SYSCTL_NODE(_net_link
, IFT_ETHER
, ether
, CTLFLAG_RW
, 0, "Ethernet");
116 u_int16_t type
; /* Type of protocol stored in data */
117 u_long protocol_family
; /* Protocol family */
118 u_long data
[2]; /* Protocol data */
120 /* descriptors are allocated in blocks of ETHER_DESC_BLK_SIZE */
121 #define ETHER_DESC_BLK_SIZE (10)
124 * Header for the demux list, hangs off of IFP at family_cookie
127 struct ether_desc_blk_str
{
131 struct en_desc block_ptr
[1];
133 /* Size of the above struct before the array of struct en_desc */
134 #define ETHER_DESC_HEADER_SIZE ((size_t)&(((struct ether_desc_blk_str*)0)->block_ptr[0]))
135 __private_extern__ u_char etherbroadcastaddr
[ETHER_ADDR_LEN
] =
136 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
138 int ether_add_proto_old(struct ifnet
*ifp
, u_long protocol_family
, struct ddesc_head_str
*desc_head
);
139 int ether_add_if(struct ifnet
*ifp
);
140 int ether_del_if(struct ifnet
*ifp
);
141 int ether_init_if(struct ifnet
*ifp
);
142 int ether_family_init(void);
145 * Release all descriptor entries owned by this protocol (there may be several).
146 * Setting the type to 0 releases the entry. Eventually we should compact-out
147 * the unused entries.
152 protocol_family_t protocol_family
)
154 struct ether_desc_blk_str
*desc_blk
= (struct ether_desc_blk_str
*)ifp
->family_cookie
;
158 if (desc_blk
== NULL
)
161 for (current
= desc_blk
->n_max_used
; current
> 0; current
--) {
162 if (desc_blk
->block_ptr
[current
- 1].protocol_family
== protocol_family
) {
164 desc_blk
->block_ptr
[current
- 1].type
= 0;
169 if (desc_blk
->n_used
== 0) {
170 FREE(ifp
->family_cookie
, M_IFADDR
);
171 ifp
->family_cookie
= 0;
174 /* Decrement n_max_used */
175 for (; desc_blk
->n_max_used
> 0 && desc_blk
->block_ptr
[desc_blk
->n_max_used
- 1].type
== 0; desc_blk
->n_max_used
--)
184 ether_add_proto_internal(
186 protocol_family_t protocol
,
187 const struct ifnet_demux_desc
*demux
)
190 struct ether_desc_blk_str
*desc_blk
= (struct ether_desc_blk_str
*)ifp
->family_cookie
;
193 switch (demux
->type
) {
194 /* These types are supported */
195 /* Top three are preferred */
196 case DLIL_DESC_ETYPE2
:
197 if (demux
->datalen
!= 2) {
203 if (demux
->datalen
!= 3) {
209 if (demux
->datalen
!= 5) {
218 // Verify a matching descriptor does not exist.
219 if (desc_blk
!= NULL
) {
220 switch (demux
->type
) {
221 case DLIL_DESC_ETYPE2
:
222 for (i
= 0; i
< desc_blk
->n_max_used
; i
++) {
223 if (desc_blk
->block_ptr
[i
].type
== DLIL_DESC_ETYPE2
&&
224 desc_blk
->block_ptr
[i
].data
[0] ==
225 *(u_int16_t
*)demux
->data
) {
232 for (i
= 0; i
< desc_blk
->n_max_used
; i
++) {
233 if (desc_blk
->block_ptr
[i
].type
== demux
->type
&&
234 bcmp(desc_blk
->block_ptr
[i
].data
, demux
->data
,
235 demux
->datalen
) == 0) {
243 // Check for case where all of the descriptor blocks are in use
244 if (desc_blk
== NULL
|| desc_blk
->n_used
== desc_blk
->n_count
) {
245 struct ether_desc_blk_str
*tmp
;
246 u_long new_count
= ETHER_DESC_BLK_SIZE
;
253 new_count
+= desc_blk
->n_count
;
254 old_size
= desc_blk
->n_count
* sizeof(struct en_desc
) + ETHER_DESC_HEADER_SIZE
;
255 i
= desc_blk
->n_used
;
258 new_size
= new_count
* sizeof(struct en_desc
) + ETHER_DESC_HEADER_SIZE
;
260 tmp
= _MALLOC(new_size
, M_IFADDR
, M_WAITOK
);
263 * Remove any previous descriptors set in the call.
268 bzero(tmp
+ old_size
, new_size
- old_size
);
270 bcopy(desc_blk
, tmp
, old_size
);
271 FREE(desc_blk
, M_IFADDR
);
274 ifp
->family_cookie
= (u_long
)desc_blk
;
275 desc_blk
->n_count
= new_count
;
278 /* Find a free entry */
279 for (i
= 0; i
< desc_blk
->n_count
; i
++) {
280 if (desc_blk
->block_ptr
[i
].type
== 0) {
286 /* Bump n_max_used if appropriate */
287 if (i
+ 1 > desc_blk
->n_max_used
) {
288 desc_blk
->n_max_used
= i
+ 1;
291 ed
= &desc_blk
->block_ptr
[i
];
292 ed
->protocol_family
= protocol
;
296 switch (demux
->type
) {
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
;
305 ed
->type
= DLIL_DESC_SAP
;
306 bcopy(demux
->data
, &ed
->data
[0], 3);
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);
325 protocol_family_t protocol
,
326 const struct ifnet_demux_desc
*demux_list
,
327 u_int32_t demux_count
)
332 for (i
= 0; i
< demux_count
; i
++) {
333 error
= ether_add_proto_internal(ifp
, protocol
, &demux_list
[i
]);
335 ether_del_proto(ifp
, protocol
);
343 __private_extern__
int
346 u_long protocol_family
,
347 struct ddesc_head_str
*desc_head
)
349 struct dlil_demux_desc
*desc
;
352 TAILQ_FOREACH(desc
, desc_head
, next
) {
353 struct ifnet_demux_desc dmx
;
356 // Convert dlil_demux_desc to ifnet_demux_desc
357 dmx
.type
= desc
->type
;
358 dmx
.datalen
= desc
->variants
.native_type_length
;
359 dmx
.data
= desc
->native_type
;
362 if (dmx
.type
== DLIL_DESC_RAW
) {
364 dmx
.type
= DLIL_DESC_ETYPE2
;
366 *(u_int16_t
*)dmx
.data
= htons(*(u_int16_t
*)dmx
.data
);
370 error
= ether_add_proto_internal(ifp
, protocol_family
, &dmx
);
372 *(u_int16_t
*)dmx
.data
= ntohs(*(u_int16_t
*)dmx
.data
);
376 ether_del_proto(ifp
, protocol_family
);
397 protocol_family_t
*protocol_family
)
399 struct ether_header
*eh
= (struct ether_header
*)frame_header
;
400 u_short ether_type
= eh
->ether_type
;
404 struct ether_desc_blk_str
*desc_blk
= (struct ether_desc_blk_str
*)ifp
->family_cookie
;
405 u_long maxd
= desc_blk
? desc_blk
->n_max_used
: 0;
406 struct en_desc
*ed
= desc_blk
? desc_blk
->block_ptr
: NULL
;
407 u_int32_t extProto1
= 0;
408 u_int32_t extProto2
= 0;
410 if (eh
->ether_dhost
[0] & 1) {
411 /* Check for broadcast */
412 if (*(u_int32_t
*)eh
->ether_dhost
== 0xFFFFFFFF &&
413 *(u_int16_t
*)(eh
->ether_dhost
+ sizeof(u_int32_t
)) == 0xFFFF)
414 m
->m_flags
|= M_BCAST
;
416 m
->m_flags
|= M_MCAST
;
419 if (ifp
->if_eflags
& IFEF_BOND
) {
420 /* if we're bonded, bond "protocol" gets all the packets */
421 *protocol_family
= PF_BOND
;
425 if ((eh
->ether_dhost
[0] & 1) == 0) {
427 * When the driver is put into promiscuous mode we may receive unicast
428 * frames that are not intended for our interfaces. They are marked here
429 * as being promiscuous so the caller may dispose of them after passing
430 * the packets to any interface filters.
432 #define ETHER_CMP(x, y) ( ((u_int16_t *) x)[0] != ((u_int16_t *) y)[0] || \
433 ((u_int16_t *) x)[1] != ((u_int16_t *) y)[1] || \
434 ((u_int16_t *) x)[2] != ((u_int16_t *) y)[2] )
436 if (ETHER_CMP(eh
->ether_dhost
, ifnet_lladdr(ifp
))) {
437 m
->m_flags
|= M_PROMISC
;
441 /* Quick check for VLAN */
442 if ((m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) != 0 ||
443 ether_type
== htons(ETHERTYPE_VLAN
)) {
444 *protocol_family
= PF_VLAN
;
448 data
= mtod(m
, u_int8_t
*);
451 * Determine the packet's protocol type and stuff the protocol into
452 * longs for quick compares.
455 if (ntohs(ether_type
) <= 1500) {
456 extProto1
= *(u_int32_t
*)data
;
459 if ((extProto1
& htonl(0xFFFFFF00)) == htonl(0xAAAA0300)) {
461 type
= DLIL_DESC_SNAP
;
462 extProto2
= *(u_int32_t
*)(data
+ sizeof(u_int32_t
));
463 extProto1
&= htonl(0x000000FF);
465 type
= DLIL_DESC_SAP
;
466 extProto1
&= htonl(0xFFFFFF00);
469 type
= DLIL_DESC_ETYPE2
;
473 * Search through the connected protocols for a match.
477 case DLIL_DESC_ETYPE2
:
478 for (i
= 0; i
< maxd
; i
++) {
479 if ((ed
[i
].type
== type
) && (ed
[i
].data
[0] == ether_type
)) {
480 *protocol_family
= ed
[i
].protocol_family
;
487 for (i
= 0; i
< maxd
; i
++) {
488 if ((ed
[i
].type
== type
) && (ed
[i
].data
[0] == extProto1
)) {
489 *protocol_family
= ed
[i
].protocol_family
;
496 for (i
= 0; i
< maxd
; i
++) {
497 if ((ed
[i
].type
== type
) && (ed
[i
].data
[0] == extProto1
) &&
498 (ed
[i
].data
[1] == extProto2
)) {
499 *protocol_family
= ed
[i
].protocol_family
;
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.
519 const struct sockaddr
*ndest
,
521 const char *ether_type
)
523 struct ether_header
*eh
;
524 int hlen
; /* link layer header length */
526 hlen
= ETHER_HDR_LEN
;
529 * If a simplex interface, and the packet is being sent to our
530 * Ethernet address or a broadcast address, loopback a copy.
531 * XXX To make a simplex device behave exactly like a duplex
532 * device, we should copy in the case of sending to our own
533 * ethernet address (thus letting the original actually appear
534 * on the wire). However, we don't do that here for security
535 * reasons and compatibility with the original behavior.
537 if ((ifp
->if_flags
& IFF_SIMPLEX
) &&
538 ((*m
)->m_flags
& M_LOOP
)) {
540 if ((*m
)->m_flags
& M_BCAST
) {
541 struct mbuf
*n
= m_copy(*m
, 0, (int)M_COPYALL
);
543 dlil_output(lo_ifp
, ndest
->sa_family
, n
, 0, ndest
, 0);
546 if (bcmp(edst
, ifnet_lladdr(ifp
), ETHER_ADDR_LEN
) == 0) {
547 dlil_output(lo_ifp
, ndest
->sa_family
, *m
, 0, ndest
, 0);
555 * Add local net header. If no space in first mbuf,
558 M_PREPEND(*m
, sizeof (struct ether_header
), M_DONTWAIT
);
560 return (EJUSTRETURN
);
564 eh
= mtod(*m
, struct ether_header
*);
565 (void)memcpy(&eh
->ether_type
, ether_type
,
566 sizeof(eh
->ether_type
));
567 (void)memcpy(eh
->ether_dhost
, edst
, 6);
568 ifnet_lladdr_copy_bytes(ifp
, eh
->ether_shost
, ETHER_ADDR_LEN
);
574 __private_extern__
int
575 ether_add_if(struct ifnet
*ifp
)
577 ifp
->if_framer
= ether_frameout
;
578 ifp
->if_demux
= ether_demux
;
583 __private_extern__
int
584 ether_del_if(struct ifnet
*ifp
)
586 if (ifp
->family_cookie
) {
587 FREE(ifp
->family_cookie
, M_IFADDR
);
594 __private_extern__
int
595 ether_init_if(struct ifnet
*ifp
)
598 * Copy ethernet address out of old style arpcom. New
599 * interfaces created using the KPIs will not have an
600 * interface family. Those interfaces will have the
601 * lladdr passed in when the interface is created.
603 u_char
*enaddr
= ((u_char
*)ifp
) + sizeof(struct ifnet
);
604 ifnet_set_lladdr(ifp
, enaddr
, 6);
614 const struct sockaddr
*proto_addr
)
616 errno_t result
= EAFNOSUPPORT
;
617 const u_char
*e_addr
;
620 * AF_SPEC and AF_LINK don't require translation. We do
621 * want to verify that they specify a valid multicast.
623 switch(proto_addr
->sa_family
) {
625 e_addr
= (const u_char
*)&proto_addr
->sa_data
[0];
626 if ((e_addr
[0] & 0x01) != 0x01)
627 result
= EADDRNOTAVAIL
;
633 e_addr
= CONST_LLADDR((const struct sockaddr_dl
*)proto_addr
);
634 if ((e_addr
[0] & 0x01) != 0x01)
635 result
= EADDRNOTAVAIL
;
646 __unused ifnet_t ifp
,
647 __unused u_int32_t command
,
654 extern int ether_attach_inet(struct ifnet
*ifp
, u_long proto_family
);
655 extern int ether_detach_inet(struct ifnet
*ifp
, u_long proto_family
);
656 extern int ether_attach_inet6(struct ifnet
*ifp
, u_long proto_family
);
657 extern int ether_detach_inet6(struct ifnet
*ifp
, u_long proto_family
);
659 extern void kprintf(const char *, ...);
661 int ether_family_init(void)
664 struct dlil_ifmod_reg_str ifmod_reg
;
666 /* ethernet family is built-in, called from bsd_init */
668 bzero(&ifmod_reg
, sizeof(ifmod_reg
));
669 ifmod_reg
.add_if
= ether_add_if
;
670 ifmod_reg
.del_if
= ether_del_if
;
671 ifmod_reg
.init_if
= ether_init_if
;
672 ifmod_reg
.add_proto
= ether_add_proto_old
;
673 ifmod_reg
.del_proto
= ether_del_proto
;
674 ifmod_reg
.ifmod_ioctl
= ether_ioctl
;
675 ifmod_reg
.shutdown
= ether_shutdown
;
677 if (dlil_reg_if_modules(APPLE_IF_FAM_ETHERNET
, &ifmod_reg
)) {
678 printf("WARNING: ether_family_init -- Can't register if family modules\n");
683 /* Register protocol registration functions */
685 if ((error
= dlil_reg_proto_module(PF_INET
, APPLE_IF_FAM_ETHERNET
,
686 ether_attach_inet
, ether_detach_inet
)) != 0) {
687 kprintf("dlil_reg_proto_module failed for AF_INET6 error=%d\n", error
);
692 if ((error
= dlil_reg_proto_module(PF_INET6
, APPLE_IF_FAM_ETHERNET
,
693 ether_attach_inet6
, ether_detach_inet6
)) != 0) {
694 kprintf("dlil_reg_proto_module failed for AF_INET6 error=%d\n", error
);