ethernet.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * From FreeBSD 2.2.7: Fundamental constants relating to ethernet.
  3. *
  4. * Portions of this code are copyright (c) 2020 Cypress Semiconductor Corporation
  5. *
  6. * Copyright (C) 1999-2020, Broadcom Corporation
  7. *
  8. * Unless you and Broadcom execute a separate written software license
  9. * agreement governing use of this software, this software is licensed to you
  10. * under the terms of the GNU General Public License version 2 (the "GPL"),
  11. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  12. * following added to such license:
  13. *
  14. * As a special exception, the copyright holders of this software give you
  15. * permission to link this software with independent modules, and to copy and
  16. * distribute the resulting executable under terms of your choice, provided that
  17. * you also meet, for each linked independent module, the terms and conditions of
  18. * the license of that module. An independent module is a module which is not
  19. * derived from this software. The special exception does not apply to any
  20. * modifications of the software.
  21. *
  22. * Notwithstanding the above, under no circumstances may you combine this
  23. * software in any way with any other Broadcom software provided under a license
  24. * other than the GPL, without Broadcom's express prior written consent.
  25. *
  26. *
  27. * <<Broadcom-WL-IPTag/Open:>>
  28. *
  29. * $Id: ethernet.h 681862 2018-01-03 12:34:14Z $
  30. */
  31. #ifndef _NET_ETHERNET_H_ /* use native BSD ethernet.h when available */
  32. #define _NET_ETHERNET_H_
  33. #ifndef _TYPEDEFS_H_
  34. #include "typedefs.h"
  35. #endif // endif
  36. /* This marks the start of a packed structure section. */
  37. #include <packed_section_start.h>
  38. /*
  39. * The number of bytes in an ethernet (MAC) address.
  40. */
  41. #define ETHER_ADDR_LEN 6
  42. /*
  43. * The number of bytes in the type field.
  44. */
  45. #define ETHER_TYPE_LEN 2
  46. /*
  47. * The number of bytes in the trailing CRC field.
  48. */
  49. #define ETHER_CRC_LEN 4
  50. /*
  51. * The length of the combined header.
  52. */
  53. #define ETHER_HDR_LEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN)
  54. /*
  55. * The minimum packet length.
  56. */
  57. #define ETHER_MIN_LEN 64
  58. /*
  59. * The minimum packet user data length.
  60. */
  61. #define ETHER_MIN_DATA 46
  62. /*
  63. * The maximum packet length.
  64. */
  65. #define ETHER_MAX_LEN 1518
  66. /*
  67. * The maximum packet user data length.
  68. */
  69. #define ETHER_MAX_DATA 1500
  70. /* ether types */
  71. #define ETHER_TYPE_MIN 0x0600 /* Anything less than MIN is a length */
  72. #define ETHER_TYPE_IP 0x0800 /* IP */
  73. #define ETHER_TYPE_ARP 0x0806 /* ARP */
  74. #define ETHER_TYPE_8021Q 0x8100 /* 802.1Q */
  75. #define ETHER_TYPE_IPV6 0x86dd /* IPv6 */
  76. #define ETHER_TYPE_BRCM 0x886c /* Broadcom Corp. */
  77. #define ETHER_TYPE_802_1X 0x888e /* 802.1x */
  78. #define ETHER_TYPE_802_1X_PREAUTH 0x88c7 /* 802.1x preauthentication */
  79. #define ETHER_TYPE_WAI 0x88b4 /* WAI */
  80. #define ETHER_TYPE_89_0D 0x890d /* 89-0d frame for TDLS */
  81. #define ETHER_TYPE_RRB ETHER_TYPE_89_0D /* RRB 802.11r 2008 */
  82. #define ETHER_TYPE_PPP_SES 0x8864 /* PPPoE Session */
  83. #define ETHER_TYPE_IAPP_L2_UPDATE 0x6 /* IAPP L2 update frame */
  84. /* Broadcom subtype follows ethertype; First 2 bytes are reserved; Next 2 are subtype; */
  85. #define ETHER_BRCM_SUBTYPE_LEN 4 /* Broadcom 4 byte subtype */
  86. /* ether header */
  87. #define ETHER_DEST_OFFSET (0 * ETHER_ADDR_LEN) /* dest address offset */
  88. #define ETHER_SRC_OFFSET (1 * ETHER_ADDR_LEN) /* src address offset */
  89. #define ETHER_TYPE_OFFSET (2 * ETHER_ADDR_LEN) /* ether type offset */
  90. /*
  91. * A macro to validate a length with
  92. */
  93. #define ETHER_IS_VALID_LEN(foo) \
  94. ((foo) >= ETHER_MIN_LEN && (foo) <= ETHER_MAX_LEN)
  95. #define ETHER_FILL_MCAST_ADDR_FROM_IP(ea, mgrp_ip) { \
  96. ((uint8 *)ea)[0] = 0x01; \
  97. ((uint8 *)ea)[1] = 0x00; \
  98. ((uint8 *)ea)[2] = 0x5e; \
  99. ((uint8 *)ea)[3] = ((mgrp_ip) >> 16) & 0x7f; \
  100. ((uint8 *)ea)[4] = ((mgrp_ip) >> 8) & 0xff; \
  101. ((uint8 *)ea)[5] = ((mgrp_ip) >> 0) & 0xff; \
  102. }
  103. #ifndef __INCif_etherh /* Quick and ugly hack for VxWorks */
  104. /*
  105. * Structure of a 10Mb/s Ethernet header.
  106. */
  107. BWL_PRE_PACKED_STRUCT struct ether_header {
  108. uint8 ether_dhost[ETHER_ADDR_LEN];
  109. uint8 ether_shost[ETHER_ADDR_LEN];
  110. uint16 ether_type;
  111. } BWL_POST_PACKED_STRUCT;
  112. /*
  113. * Structure of a 48-bit Ethernet address.
  114. */
  115. BWL_PRE_PACKED_STRUCT struct ether_addr {
  116. uint8 octet[ETHER_ADDR_LEN];
  117. } BWL_POST_PACKED_STRUCT;
  118. #endif /* !__INCif_etherh Quick and ugly hack for VxWorks */
  119. /*
  120. * Takes a pointer, set, test, clear, toggle locally admininistered
  121. * address bit in the 48-bit Ethernet address.
  122. */
  123. #define ETHER_SET_LOCALADDR(ea) (((uint8 *)(ea))[0] = (((uint8 *)(ea))[0] | 2))
  124. #define ETHER_IS_LOCALADDR(ea) (((uint8 *)(ea))[0] & 2)
  125. #define ETHER_CLR_LOCALADDR(ea) (((uint8 *)(ea))[0] = (((uint8 *)(ea))[0] & 0xfd))
  126. #define ETHER_TOGGLE_LOCALADDR(ea) (((uint8 *)(ea))[0] = (((uint8 *)(ea))[0] ^ 2))
  127. /* Takes a pointer, marks unicast address bit in the MAC address */
  128. #define ETHER_SET_UNICAST(ea) (((uint8 *)(ea))[0] = (((uint8 *)(ea))[0] & ~1))
  129. /*
  130. * Takes a pointer, returns true if a 48-bit multicast address
  131. * (including broadcast, since it is all ones)
  132. */
  133. #define ETHER_ISMULTI(ea) (((const uint8 *)(ea))[0] & 1)
  134. /* compare two ethernet addresses - assumes the pointers can be referenced as shorts */
  135. #define eacmp(a, b) ((((const uint16 *)(a))[0] ^ ((const uint16 *)(b))[0]) | \
  136. (((const uint16 *)(a))[1] ^ ((const uint16 *)(b))[1]) | \
  137. (((const uint16 *)(a))[2] ^ ((const uint16 *)(b))[2]))
  138. #define ether_cmp(a, b) eacmp(a, b)
  139. /* copy an ethernet address - assumes the pointers can be referenced as shorts */
  140. #define eacopy(s, d) \
  141. do { \
  142. ((uint16 *)(d))[0] = ((const uint16 *)(s))[0]; \
  143. ((uint16 *)(d))[1] = ((const uint16 *)(s))[1]; \
  144. ((uint16 *)(d))[2] = ((const uint16 *)(s))[2]; \
  145. } while (0)
  146. #define ether_copy(s, d) eacopy(s, d)
  147. /* Copy an ethernet address in reverse order */
  148. #define ether_rcopy(s, d) \
  149. do { \
  150. ((uint16 *)(d))[2] = ((uint16 *)(s))[2]; \
  151. ((uint16 *)(d))[1] = ((uint16 *)(s))[1]; \
  152. ((uint16 *)(d))[0] = ((uint16 *)(s))[0]; \
  153. } while (0)
  154. /* Copy 14B ethernet header: 32bit aligned source and destination. */
  155. #define ehcopy32(s, d) \
  156. do { \
  157. ((uint32 *)(d))[0] = ((const uint32 *)(s))[0]; \
  158. ((uint32 *)(d))[1] = ((const uint32 *)(s))[1]; \
  159. ((uint32 *)(d))[2] = ((const uint32 *)(s))[2]; \
  160. ((uint16 *)(d))[6] = ((const uint16 *)(s))[6]; \
  161. } while (0)
  162. static const struct ether_addr ether_bcast = {{255, 255, 255, 255, 255, 255}};
  163. static const struct ether_addr ether_null = {{0, 0, 0, 0, 0, 0}};
  164. static const struct ether_addr ether_ipv6_mcast = {{0x33, 0x33, 0x00, 0x00, 0x00, 0x01}};
  165. #define ETHER_ISBCAST(ea) ((((const uint8 *)(ea))[0] & \
  166. ((const uint8 *)(ea))[1] & \
  167. ((const uint8 *)(ea))[2] & \
  168. ((const uint8 *)(ea))[3] & \
  169. ((const uint8 *)(ea))[4] & \
  170. ((const uint8 *)(ea))[5]) == 0xff)
  171. #define ETHER_ISNULLADDR(ea) ((((const uint8 *)(ea))[0] | \
  172. ((const uint8 *)(ea))[1] | \
  173. ((const uint8 *)(ea))[2] | \
  174. ((const uint8 *)(ea))[3] | \
  175. ((const uint8 *)(ea))[4] | \
  176. ((const uint8 *)(ea))[5]) == 0)
  177. #define ETHER_ISNULLDEST(da) ((((const uint16 *)(da))[0] | \
  178. ((const uint16 *)(da))[1] | \
  179. ((const uint16 *)(da))[2]) == 0)
  180. #define ETHER_ISNULLSRC(sa) ETHER_ISNULLDEST(sa)
  181. #define ETHER_MOVE_HDR(d, s) \
  182. do { \
  183. struct ether_header t; \
  184. t = *(struct ether_header *)(s); \
  185. *(struct ether_header *)(d) = t; \
  186. } while (0)
  187. #define ETHER_ISUCAST(ea) ((((uint8 *)(ea))[0] & 0x01) == 0)
  188. /* This marks the end of a packed structure section. */
  189. #include <packed_section_end.h>
  190. #endif /* _NET_ETHERNET_H_ */