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