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