1/*
2 * From FreeBSD 2.2.7: Fundamental constants relating to ethernet.
3 *
4 * Copyright (C) 1999-2013, Broadcom Corporation
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 384540 2013-02-12 04:28:58Z $
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/* Broadcom subtype follows ethertype;  First 2 bytes are reserved; Next 2 are subtype; */
92#define	ETHER_BRCM_SUBTYPE_LEN	4	/* Broadcom 4 byte subtype */
93
94/* ether header */
95#define ETHER_DEST_OFFSET	(0 * ETHER_ADDR_LEN)	/* dest address offset */
96#define ETHER_SRC_OFFSET	(1 * ETHER_ADDR_LEN)	/* src address offset */
97#define ETHER_TYPE_OFFSET	(2 * ETHER_ADDR_LEN)	/* ether type offset */
98
99/*
100 * A macro to validate a length with
101 */
102#define	ETHER_IS_VALID_LEN(foo)	\
103	((foo) >= ETHER_MIN_LEN && (foo) <= ETHER_MAX_LEN)
104
105#define ETHER_FILL_MCAST_ADDR_FROM_IP(ea, mgrp_ip) {		\
106		((uint8 *)ea)[0] = 0x01;			\
107		((uint8 *)ea)[1] = 0x00;			\
108		((uint8 *)ea)[2] = 0x5e;			\
109		((uint8 *)ea)[3] = ((mgrp_ip) >> 16) & 0x7f;	\
110		((uint8 *)ea)[4] = ((mgrp_ip) >>  8) & 0xff;	\
111		((uint8 *)ea)[5] = ((mgrp_ip) >>  0) & 0xff;	\
112}
113
114#ifndef __INCif_etherh     /* Quick and ugly hack for VxWorks */
115/*
116 * Structure of a 10Mb/s Ethernet header.
117 */
118BWL_PRE_PACKED_STRUCT struct ether_header {
119	uint8	ether_dhost[ETHER_ADDR_LEN];
120	uint8	ether_shost[ETHER_ADDR_LEN];
121	uint16	ether_type;
122} BWL_POST_PACKED_STRUCT;
123
124/*
125 * Structure of a 48-bit Ethernet address.
126 */
127BWL_PRE_PACKED_STRUCT struct	ether_addr {
128	uint8 octet[ETHER_ADDR_LEN];
129} BWL_POST_PACKED_STRUCT;
130#endif	/* !__INCif_etherh Quick and ugly hack for VxWorks */
131
132/*
133 * Takes a pointer, set, test, clear, toggle locally admininistered
134 * address bit in the 48-bit Ethernet address.
135 */
136#define ETHER_SET_LOCALADDR(ea)	(((uint8 *)(ea))[0] = (((uint8 *)(ea))[0] | 2))
137#define ETHER_IS_LOCALADDR(ea) 	(((uint8 *)(ea))[0] & 2)
138#define ETHER_CLR_LOCALADDR(ea)	(((uint8 *)(ea))[0] = (((uint8 *)(ea))[0] & 0xfd))
139#define ETHER_TOGGLE_LOCALADDR(ea)	(((uint8 *)(ea))[0] = (((uint8 *)(ea))[0] ^ 2))
140
141/* Takes a pointer, marks unicast address bit in the MAC address */
142#define ETHER_SET_UNICAST(ea)	(((uint8 *)(ea))[0] = (((uint8 *)(ea))[0] & ~1))
143
144/*
145 * Takes a pointer, returns true if a 48-bit multicast address
146 * (including broadcast, since it is all ones)
147 */
148#define ETHER_ISMULTI(ea) (((const uint8 *)(ea))[0] & 1)
149
150
151/* compare two ethernet addresses - assumes the pointers can be referenced as shorts */
152#define eacmp(a, b)	((((const uint16 *)(a))[0] ^ ((const uint16 *)(b))[0]) | \
153	                 (((const uint16 *)(a))[1] ^ ((const uint16 *)(b))[1]) | \
154	                 (((const uint16 *)(a))[2] ^ ((const uint16 *)(b))[2]))
155
156#define	ether_cmp(a, b)	eacmp(a, b)
157
158/* copy an ethernet address - assumes the pointers can be referenced as shorts */
159#define eacopy(s, d) \
160do { \
161	((uint16 *)(d))[0] = ((const uint16 *)(s))[0]; \
162	((uint16 *)(d))[1] = ((const uint16 *)(s))[1]; \
163	((uint16 *)(d))[2] = ((const uint16 *)(s))[2]; \
164} while (0)
165
166#define	ether_copy(s, d) eacopy(s, d)
167
168/* Copy an ethernet address in reverse order */
169#define	ether_rcopy(s, d) \
170do { \
171	((uint16 *)(d))[2] = ((uint16 *)(s))[2]; \
172	((uint16 *)(d))[1] = ((uint16 *)(s))[1]; \
173	((uint16 *)(d))[0] = ((uint16 *)(s))[0]; \
174} while (0)
175
176
177
178static const struct ether_addr ether_bcast = {{255, 255, 255, 255, 255, 255}};
179static const struct ether_addr ether_null = {{0, 0, 0, 0, 0, 0}};
180static const struct ether_addr ether_ipv6_mcast = {{0x33, 0x33, 0x00, 0x00, 0x00, 0x01}};
181
182#define ETHER_ISBCAST(ea)	((((const uint8 *)(ea))[0] &		\
183	                          ((const uint8 *)(ea))[1] &		\
184				  ((const uint8 *)(ea))[2] &		\
185				  ((const uint8 *)(ea))[3] &		\
186				  ((const uint8 *)(ea))[4] &		\
187				  ((const uint8 *)(ea))[5]) == 0xff)
188#define ETHER_ISNULLADDR(ea)	((((const uint8 *)(ea))[0] |		\
189				  ((const uint8 *)(ea))[1] |		\
190				  ((const uint8 *)(ea))[2] |		\
191				  ((const uint8 *)(ea))[3] |		\
192				  ((const uint8 *)(ea))[4] |		\
193				  ((const uint8 *)(ea))[5]) == 0)
194
195#define ETHER_ISNULLDEST(da)	((((const uint16 *)(da))[0] |           \
196				  ((const uint16 *)(da))[1] |           \
197				  ((const uint16 *)(da))[2]) == 0)
198#define ETHER_ISNULLSRC(sa)	ETHER_ISNULLDEST(sa)
199
200#define ETHER_MOVE_HDR(d, s) \
201do { \
202	struct ether_header t; \
203	t = *(struct ether_header *)(s); \
204	*(struct ether_header *)(d) = t; \
205} while (0)
206
207#define  ETHER_ISUCAST(ea) ((((uint8 *)(ea))[0] & 0x01) == 0)
208
209/* This marks the end of a packed structure section. */
210#include <packed_section_end.h>
211
212#endif /* _NET_ETHERNET_H_ */
213