1/*
2 * From FreeBSD 2.2.7: Fundamental constants relating to ethernet.
3 *
4 * Copyright (C) 2013, Broadcom Corporation. All Rights Reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * $Id: ethernet.h 401759 2013-05-13 16:08:08Z $
19 */
20
21#ifndef _NET_ETHERNET_H_	/* use native BSD ethernet.h when available */
22#define _NET_ETHERNET_H_
23
24#ifndef _TYPEDEFS_H_
25#include "typedefs.h"
26#endif
27
28/* This marks the start of a packed structure section. */
29#include <packed_section_start.h>
30
31
32/*
33 * The number of bytes in an ethernet (MAC) address.
34 */
35#define	ETHER_ADDR_LEN		6
36
37/*
38 * The number of bytes in the type field.
39 */
40#define	ETHER_TYPE_LEN		2
41
42/*
43 * The number of bytes in the trailing CRC field.
44 */
45#define	ETHER_CRC_LEN		4
46
47/*
48 * The length of the combined header.
49 */
50#define	ETHER_HDR_LEN		(ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN)
51
52/*
53 * The minimum packet length.
54 */
55#define	ETHER_MIN_LEN		64
56
57/*
58 * The minimum packet user data length.
59 */
60#define	ETHER_MIN_DATA		46
61
62/*
63 * The maximum packet length.
64 */
65#define	ETHER_MAX_LEN		1518
66
67/*
68 * The maximum packet user data length.
69 */
70#define	ETHER_MAX_DATA		1500
71
72/* ether types */
73#define ETHER_TYPE_MIN		0x0600		/* Anything less than MIN is a length */
74#define	ETHER_TYPE_IP		0x0800		/* IP */
75#define ETHER_TYPE_ARP		0x0806		/* ARP */
76#define ETHER_TYPE_8021Q	0x8100		/* 802.1Q */
77#define	ETHER_TYPE_IPV6		0x86dd		/* IPv6 */
78#define	ETHER_TYPE_BRCM		0x886c		/* Broadcom Corp. */
79#define	ETHER_TYPE_802_1X	0x888e		/* 802.1x */
80#ifdef PLC
81#define	ETHER_TYPE_88E1		0x88e1		/* GIGLE */
82#define	ETHER_TYPE_8912		0x8912		/* GIGLE */
83#define ETHER_TYPE_GIGLED	0xffff		/* GIGLE */
84#endif /* PLC */
85#define	ETHER_TYPE_802_1X_PREAUTH 0x88c7	/* 802.1x preauthentication */
86#define ETHER_TYPE_WAI		0x88b4		/* WAI */
87#define ETHER_TYPE_89_0D	0x890d		/* 89-0d frame for TDLS */
88
89#define ETHER_TYPE_PPP_SES	0x8864		/* PPPoE Session */
90
91#define ETHER_TYPE_IAPP_L2_UPDATE	0x6	/* IAPP L2 update frame */
92
93/* Broadcom subtype follows ethertype;  First 2 bytes are reserved; Next 2 are subtype; */
94#define	ETHER_BRCM_SUBTYPE_LEN	4	/* Broadcom 4 byte subtype */
95
96/* ether header */
97#define ETHER_DEST_OFFSET	(0 * ETHER_ADDR_LEN)	/* dest address offset */
98#define ETHER_SRC_OFFSET	(1 * ETHER_ADDR_LEN)	/* src address offset */
99#define ETHER_TYPE_OFFSET	(2 * ETHER_ADDR_LEN)	/* ether type offset */
100
101/*
102 * A macro to validate a length with
103 */
104#define	ETHER_IS_VALID_LEN(foo)	\
105	((foo) >= ETHER_MIN_LEN && (foo) <= ETHER_MAX_LEN)
106
107#define ETHER_FILL_MCAST_ADDR_FROM_IP(ea, mgrp_ip) {		\
108		((uint8 *)ea)[0] = 0x01;			\
109		((uint8 *)ea)[1] = 0x00;			\
110		((uint8 *)ea)[2] = 0x5e;			\
111		((uint8 *)ea)[3] = ((mgrp_ip) >> 16) & 0x7f;	\
112		((uint8 *)ea)[4] = ((mgrp_ip) >>  8) & 0xff;	\
113		((uint8 *)ea)[5] = ((mgrp_ip) >>  0) & 0xff;	\
114}
115
116#ifndef __INCif_etherh /* Quick and ugly hack for VxWorks */
117/*
118 * Structure of a 10Mb/s Ethernet header.
119 */
120BWL_PRE_PACKED_STRUCT struct ether_header {
121	uint8	ether_dhost[ETHER_ADDR_LEN];
122	uint8	ether_shost[ETHER_ADDR_LEN];
123	uint16	ether_type;
124} BWL_POST_PACKED_STRUCT;
125
126/*
127 * Structure of a 48-bit Ethernet address.
128 */
129BWL_PRE_PACKED_STRUCT struct	ether_addr {
130	uint8 octet[ETHER_ADDR_LEN];
131} BWL_POST_PACKED_STRUCT;
132#endif	/* !__INCif_etherh Quick and ugly hack for VxWorks */
133
134/*
135 * Takes a pointer, set, test, clear, toggle locally admininistered
136 * address bit in the 48-bit Ethernet address.
137 */
138#define ETHER_SET_LOCALADDR(ea)	(((uint8 *)(ea))[0] = (((uint8 *)(ea))[0] | 2))
139#define ETHER_IS_LOCALADDR(ea) 	(((uint8 *)(ea))[0] & 2)
140#define ETHER_CLR_LOCALADDR(ea)	(((uint8 *)(ea))[0] = (((uint8 *)(ea))[0] & 0xfd))
141#define ETHER_TOGGLE_LOCALADDR(ea)	(((uint8 *)(ea))[0] = (((uint8 *)(ea))[0] ^ 2))
142
143/* Takes a pointer, marks unicast address bit in the MAC address */
144#define ETHER_SET_UNICAST(ea)	(((uint8 *)(ea))[0] = (((uint8 *)(ea))[0] & ~1))
145
146/*
147 * Takes a pointer, returns true if a 48-bit multicast address
148 * (including broadcast, since it is all ones)
149 */
150#define ETHER_ISMULTI(ea) (((const uint8 *)(ea))[0] & 1)
151
152
153/* compare two ethernet addresses - assumes the pointers can be referenced as shorts */
154#define eacmp(a, b)	((((const uint16 *)(a))[0] ^ ((const uint16 *)(b))[0]) | \
155	                 (((const uint16 *)(a))[1] ^ ((const uint16 *)(b))[1]) | \
156	                 (((const uint16 *)(a))[2] ^ ((const uint16 *)(b))[2]))
157
158#define	ether_cmp(a, b)	eacmp(a, b)
159
160/* copy an ethernet address - assumes the pointers can be referenced as shorts */
161#define eacopy(s, d) \
162do { \
163	((uint16 *)(d))[0] = ((const uint16 *)(s))[0]; \
164	((uint16 *)(d))[1] = ((const uint16 *)(s))[1]; \
165	((uint16 *)(d))[2] = ((const uint16 *)(s))[2]; \
166} while (0)
167
168#define	ether_copy(s, d) eacopy(s, d)
169
170/* Copy an ethernet address in reverse order */
171#define	ether_rcopy(s, d) \
172do { \
173	((uint16 *)(d))[2] = ((uint16 *)(s))[2]; \
174	((uint16 *)(d))[1] = ((uint16 *)(s))[1]; \
175	((uint16 *)(d))[0] = ((uint16 *)(s))[0]; \
176} while (0)
177
178
179#ifdef _HNDRTE_
180
181/* Dongles use bcmutils functions instead of macros.
182 * Possibly slower but saves over 800 bytes off THUMB dongle image.
183 */
184
185extern const struct ether_addr ether_bcast;
186extern const struct ether_addr ether_null;
187extern const struct ether_addr ether_ipv6_mcast;
188
189extern int ether_isbcast(const void *ea);
190extern int ether_isnulladdr(const void *ea);
191
192#define ETHER_ISBCAST(ea)	ether_isbcast(ea)
193#define ETHER_ISNULLADDR(ea)	ether_isnulladdr(ea)
194
195#else /* !_HNDRTE_ */
196
197static const struct ether_addr ether_bcast = {{255, 255, 255, 255, 255, 255}};
198static const struct ether_addr ether_null = {{0, 0, 0, 0, 0, 0}};
199static const struct ether_addr ether_ipv6_mcast = {{0x33, 0x33, 0x00, 0x00, 0x00, 0x01}};
200
201#define ETHER_ISBCAST(ea)	((((const uint8 *)(ea))[0] &		\
202	                          ((const uint8 *)(ea))[1] &		\
203				  ((const uint8 *)(ea))[2] &		\
204				  ((const uint8 *)(ea))[3] &		\
205				  ((const uint8 *)(ea))[4] &		\
206				  ((const uint8 *)(ea))[5]) == 0xff)
207#define ETHER_ISNULLADDR(ea)	((((const uint8 *)(ea))[0] |		\
208				  ((const uint8 *)(ea))[1] |		\
209				  ((const uint8 *)(ea))[2] |		\
210				  ((const uint8 *)(ea))[3] |		\
211				  ((const uint8 *)(ea))[4] |		\
212				  ((const uint8 *)(ea))[5]) == 0)
213
214#endif /* !_HNDRTE_ */
215
216#define ETHER_ISNULLDEST(da)	((((const uint16 *)(da))[0] |           \
217				  ((const uint16 *)(da))[1] |           \
218				  ((const uint16 *)(da))[2]) == 0)
219#define ETHER_ISNULLSRC(sa)	ETHER_ISNULLDEST(sa)
220
221#define ETHER_MOVE_HDR(d, s) \
222do { \
223	struct ether_header t; \
224	t = *(struct ether_header *)(s); \
225	*(struct ether_header *)(d) = t; \
226} while (0)
227
228#define  ETHER_ISUCAST(ea) ((((uint8 *)(ea))[0] & 0x01) == 0)
229
230/* This marks the end of a packed structure section. */
231#include <packed_section_end.h>
232
233#endif /* _NET_ETHERNET_H_ */
234