xref: /xnu-12377.41.6/bsd/net/ethernet.h (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions  * Copyright (c) 2000-2017 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions  *
4*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions  *
6*bbb1b6f9SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions  *
15*bbb1b6f9SApple OSS Distributions  * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions  *
18*bbb1b6f9SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions  * limitations under the License.
25*bbb1b6f9SApple OSS Distributions  *
26*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions  */
28*bbb1b6f9SApple OSS Distributions /*
29*bbb1b6f9SApple OSS Distributions  * Fundamental constants relating to ethernet.
30*bbb1b6f9SApple OSS Distributions  *
31*bbb1b6f9SApple OSS Distributions  */
32*bbb1b6f9SApple OSS Distributions 
33*bbb1b6f9SApple OSS Distributions #ifndef _NET_ETHERNET_H_
34*bbb1b6f9SApple OSS Distributions #define _NET_ETHERNET_H_
35*bbb1b6f9SApple OSS Distributions #ifndef DRIVERKIT
36*bbb1b6f9SApple OSS Distributions #include <sys/appleapiopts.h>
37*bbb1b6f9SApple OSS Distributions #include <sys/types.h>          /* u_ types */
38*bbb1b6f9SApple OSS Distributions #else
39*bbb1b6f9SApple OSS Distributions #include <sys/_types.h>
40*bbb1b6f9SApple OSS Distributions #include <sys/_types/_u_char.h>
41*bbb1b6f9SApple OSS Distributions #include <sys/_types/_u_short.h>
42*bbb1b6f9SApple OSS Distributions #endif /* DRIVERKIT */
43*bbb1b6f9SApple OSS Distributions 
44*bbb1b6f9SApple OSS Distributions /*
45*bbb1b6f9SApple OSS Distributions  * The number of bytes in an ethernet (MAC) address.
46*bbb1b6f9SApple OSS Distributions  */
47*bbb1b6f9SApple OSS Distributions #define ETHER_ADDR_LEN          6
48*bbb1b6f9SApple OSS Distributions 
49*bbb1b6f9SApple OSS Distributions /*
50*bbb1b6f9SApple OSS Distributions  * The number of bytes in the type field.
51*bbb1b6f9SApple OSS Distributions  */
52*bbb1b6f9SApple OSS Distributions #define ETHER_TYPE_LEN          2
53*bbb1b6f9SApple OSS Distributions 
54*bbb1b6f9SApple OSS Distributions /*
55*bbb1b6f9SApple OSS Distributions  * The number of bytes in the trailing CRC field.
56*bbb1b6f9SApple OSS Distributions  */
57*bbb1b6f9SApple OSS Distributions #define ETHER_CRC_LEN           4
58*bbb1b6f9SApple OSS Distributions 
59*bbb1b6f9SApple OSS Distributions /*
60*bbb1b6f9SApple OSS Distributions  * The length of the combined header.
61*bbb1b6f9SApple OSS Distributions  */
62*bbb1b6f9SApple OSS Distributions #define ETHER_HDR_LEN           (ETHER_ADDR_LEN*2+ETHER_TYPE_LEN)
63*bbb1b6f9SApple OSS Distributions 
64*bbb1b6f9SApple OSS Distributions /*
65*bbb1b6f9SApple OSS Distributions  * The minimum packet length.
66*bbb1b6f9SApple OSS Distributions  */
67*bbb1b6f9SApple OSS Distributions #define ETHER_MIN_LEN           64
68*bbb1b6f9SApple OSS Distributions 
69*bbb1b6f9SApple OSS Distributions /*
70*bbb1b6f9SApple OSS Distributions  * The maximum packet length.
71*bbb1b6f9SApple OSS Distributions  */
72*bbb1b6f9SApple OSS Distributions #define ETHER_MAX_LEN           1518
73*bbb1b6f9SApple OSS Distributions 
74*bbb1b6f9SApple OSS Distributions /*
75*bbb1b6f9SApple OSS Distributions  * Mbuf adjust factor to force 32-bit alignment of IP header.
76*bbb1b6f9SApple OSS Distributions  * Drivers should do m_adj(m, ETHER_ALIGN) when setting up a
77*bbb1b6f9SApple OSS Distributions  * receive so the upper layers get the IP header properly aligned
78*bbb1b6f9SApple OSS Distributions  * past the 14-byte Ethernet header.
79*bbb1b6f9SApple OSS Distributions  */
80*bbb1b6f9SApple OSS Distributions #define ETHER_ALIGN             2       /* driver adjust for IP hdr alignment */
81*bbb1b6f9SApple OSS Distributions 
82*bbb1b6f9SApple OSS Distributions /*
83*bbb1b6f9SApple OSS Distributions  * A macro to validate a length with
84*bbb1b6f9SApple OSS Distributions  */
85*bbb1b6f9SApple OSS Distributions #define ETHER_IS_VALID_LEN(foo) \
86*bbb1b6f9SApple OSS Distributions 	((foo) >= ETHER_MIN_LEN && (foo) <= ETHER_MAX_LEN)
87*bbb1b6f9SApple OSS Distributions 
88*bbb1b6f9SApple OSS Distributions /*
89*bbb1b6f9SApple OSS Distributions  * Structure of a 10Mb/s Ethernet header.
90*bbb1b6f9SApple OSS Distributions  */
91*bbb1b6f9SApple OSS Distributions typedef struct  ether_header {
92*bbb1b6f9SApple OSS Distributions 	u_char  ether_dhost[ETHER_ADDR_LEN];
93*bbb1b6f9SApple OSS Distributions 	u_char  ether_shost[ETHER_ADDR_LEN];
94*bbb1b6f9SApple OSS Distributions 	u_short ether_type;
95*bbb1b6f9SApple OSS Distributions } ether_header_t;
96*bbb1b6f9SApple OSS Distributions 
97*bbb1b6f9SApple OSS Distributions /*
98*bbb1b6f9SApple OSS Distributions  * Structure of a 48-bit Ethernet address.
99*bbb1b6f9SApple OSS Distributions  */
100*bbb1b6f9SApple OSS Distributions typedef struct  ether_addr {
101*bbb1b6f9SApple OSS Distributions 	u_char octet[ETHER_ADDR_LEN];
102*bbb1b6f9SApple OSS Distributions } ether_addr_t;
103*bbb1b6f9SApple OSS Distributions 
104*bbb1b6f9SApple OSS Distributions #define ether_addr_octet octet
105*bbb1b6f9SApple OSS Distributions 
106*bbb1b6f9SApple OSS Distributions #define ETHERTYPE_PUP           0x0200  /* PUP protocol */
107*bbb1b6f9SApple OSS Distributions #define ETHERTYPE_IP            0x0800  /* IP protocol */
108*bbb1b6f9SApple OSS Distributions #define ETHERTYPE_ARP           0x0806  /* Addr. resolution protocol */
109*bbb1b6f9SApple OSS Distributions #define ETHERTYPE_REVARP        0x8035  /* reverse Addr. resolution protocol */
110*bbb1b6f9SApple OSS Distributions #define ETHERTYPE_VLAN          0x8100  /* IEEE 802.1Q VLAN tagging */
111*bbb1b6f9SApple OSS Distributions #define ETHERTYPE_IPV6          0x86dd  /* IPv6 */
112*bbb1b6f9SApple OSS Distributions #define ETHERTYPE_PAE           0x888e  /* EAPOL PAE/802.1x */
113*bbb1b6f9SApple OSS Distributions #define ETHERTYPE_WAI           0x88b4  /* WAI Authentication Protocol */
114*bbb1b6f9SApple OSS Distributions #define ETHERTYPE_RSN_PREAUTH   0x88c7  /* 802.11i / RSN Pre-Authentication */
115*bbb1b6f9SApple OSS Distributions #define ETHERTYPE_PTP           0x88f7  /* IEEE 1588 Precision Time Protocol */
116*bbb1b6f9SApple OSS Distributions #define ETHERTYPE_LOOPBACK      0x9000  /* used to test interfaces */
117*bbb1b6f9SApple OSS Distributions /* XXX - add more useful types here */
118*bbb1b6f9SApple OSS Distributions 
119*bbb1b6f9SApple OSS Distributions /*
120*bbb1b6f9SApple OSS Distributions  * The ETHERTYPE_NTRAILER packet types starting at ETHERTYPE_TRAIL have
121*bbb1b6f9SApple OSS Distributions  * (type-ETHERTYPE_TRAIL)*512 bytes of data followed
122*bbb1b6f9SApple OSS Distributions  * by an ETHER type (as given above) and then the (variable-length) header.
123*bbb1b6f9SApple OSS Distributions  */
124*bbb1b6f9SApple OSS Distributions #define ETHERTYPE_TRAIL         0x1000          /* Trailer packet */
125*bbb1b6f9SApple OSS Distributions #define ETHERTYPE_NTRAILER      16
126*bbb1b6f9SApple OSS Distributions 
127*bbb1b6f9SApple OSS Distributions #define ETHERMTU        (ETHER_MAX_LEN-ETHER_HDR_LEN-ETHER_CRC_LEN)
128*bbb1b6f9SApple OSS Distributions #define ETHERMIN        (ETHER_MIN_LEN-ETHER_HDR_LEN-ETHER_CRC_LEN)
129*bbb1b6f9SApple OSS Distributions 
130*bbb1b6f9SApple OSS Distributions #ifndef DRIVERKIT
131*bbb1b6f9SApple OSS Distributions #ifdef KERNEL_PRIVATE
132*bbb1b6f9SApple OSS Distributions /*
133*bbb1b6f9SApple OSS Distributions  * The following are used by ethernet interfaces.
134*bbb1b6f9SApple OSS Distributions  */
135*bbb1b6f9SApple OSS Distributions 
136*bbb1b6f9SApple OSS Distributions struct  ether_addr *ether_aton(const char *);
137*bbb1b6f9SApple OSS Distributions 
138*bbb1b6f9SApple OSS Distributions #ifdef BSD_KERNEL_PRIVATE
139*bbb1b6f9SApple OSS Distributions extern u_char   etherbroadcastaddr[ETHER_ADDR_LEN];
140*bbb1b6f9SApple OSS Distributions 
141*bbb1b6f9SApple OSS Distributions 
142*bbb1b6f9SApple OSS Distributions // TODO: This should really be `__sized_by(ETHER_ADDR_LEN)` rather than
143*bbb1b6f9SApple OSS Distributions // `__unsafe_indexable` on the parameters but it is being omitted until the perf
144*bbb1b6f9SApple OSS Distributions // impact of adding bounds checks is analyzed (rdar://117166943)
145*bbb1b6f9SApple OSS Distributions static __inline__ int
_ether_cmp(const void * __unsafe_indexable a,const void * __unsafe_indexable b)146*bbb1b6f9SApple OSS Distributions _ether_cmp(const void *__unsafe_indexable a, const void *__unsafe_indexable b)
147*bbb1b6f9SApple OSS Distributions {
148*bbb1b6f9SApple OSS Distributions 	// Given that `ETHER_ADDR_LEN` is a constant one might expect all
149*bbb1b6f9SApple OSS Distributions 	// bounds checks to be removed in optimized code. Unfortunately a bug
150*bbb1b6f9SApple OSS Distributions 	// means not all bounds checks are removed (rdar://117279245).
151*bbb1b6f9SApple OSS Distributions 	const u_int16_t * __unsafe_indexable a_s = __unsafe_forge_bidi_indexable(
152*bbb1b6f9SApple OSS Distributions 		const u_int16_t *, a, ETHER_ADDR_LEN);
153*bbb1b6f9SApple OSS Distributions 	const u_int16_t * __unsafe_indexable b_s = __unsafe_forge_bidi_indexable(
154*bbb1b6f9SApple OSS Distributions 		const u_int16_t *, b, ETHER_ADDR_LEN);
155*bbb1b6f9SApple OSS Distributions 
156*bbb1b6f9SApple OSS Distributions 	if (a_s[0] != b_s[0]
157*bbb1b6f9SApple OSS Distributions 	    || a_s[1] != b_s[1]
158*bbb1b6f9SApple OSS Distributions 	    || a_s[2] != b_s[2]) {
159*bbb1b6f9SApple OSS Distributions 		return 1;
160*bbb1b6f9SApple OSS Distributions 	}
161*bbb1b6f9SApple OSS Distributions 	return 0;
162*bbb1b6f9SApple OSS Distributions }
163*bbb1b6f9SApple OSS Distributions 
164*bbb1b6f9SApple OSS Distributions #endif /* BSD_KERNEL_PRIVATE */
165*bbb1b6f9SApple OSS Distributions 
166*bbb1b6f9SApple OSS Distributions #define ETHER_IS_MULTICAST(addr) (*(addr) & 0x01) /* is address mcast/bcast? */
167*bbb1b6f9SApple OSS Distributions 
168*bbb1b6f9SApple OSS Distributions #endif /* KERNEL_PRIVATE */
169*bbb1b6f9SApple OSS Distributions 
170*bbb1b6f9SApple OSS Distributions #ifndef KERNEL
171*bbb1b6f9SApple OSS Distributions #include <sys/cdefs.h>
172*bbb1b6f9SApple OSS Distributions 
173*bbb1b6f9SApple OSS Distributions /*
174*bbb1b6f9SApple OSS Distributions  * Ethernet address conversion/parsing routines.
175*bbb1b6f9SApple OSS Distributions  */
176*bbb1b6f9SApple OSS Distributions __BEGIN_DECLS
177*bbb1b6f9SApple OSS Distributions 
178*bbb1b6f9SApple OSS Distributions int     ether_hostton(const char *, struct ether_addr *);
179*bbb1b6f9SApple OSS Distributions int     ether_line(const char *, struct ether_addr *, char *);
180*bbb1b6f9SApple OSS Distributions char    *ether_ntoa(const struct ether_addr *);
181*bbb1b6f9SApple OSS Distributions struct  ether_addr *ether_aton(const char *);
182*bbb1b6f9SApple OSS Distributions int     ether_ntohost(char *, const struct ether_addr *);
183*bbb1b6f9SApple OSS Distributions __END_DECLS
184*bbb1b6f9SApple OSS Distributions #endif /* !KERNEL */
185*bbb1b6f9SApple OSS Distributions #endif /* DRIVERKIT */
186*bbb1b6f9SApple OSS Distributions 
187*bbb1b6f9SApple OSS Distributions #endif /* !_NET_ETHERNET_H_ */
188