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