bcmip.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Fundamental constants relating to IP Protocol
  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: bcmip.h 715513 2019-05-24 18:38:24Z $
  30. */
  31. #ifndef _bcmip_h_
  32. #define _bcmip_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. /* IPV4 and IPV6 common */
  39. #define IP_VER_OFFSET 0x0 /* offset to version field */
  40. #define IP_VER_MASK 0xf0 /* version mask */
  41. #define IP_VER_SHIFT 4 /* version shift */
  42. #define IP_VER_4 4 /* version number for IPV4 */
  43. #define IP_VER_6 6 /* version number for IPV6 */
  44. #define IP_VER(ip_body) \
  45. ((((uint8 *)(ip_body))[IP_VER_OFFSET] & IP_VER_MASK) >> IP_VER_SHIFT)
  46. #define IP_PROT_ICMP 0x1 /* ICMP protocol */
  47. #define IP_PROT_IGMP 0x2 /* IGMP protocol */
  48. #define IP_PROT_TCP 0x6 /* TCP protocol */
  49. #define IP_PROT_UDP 0x11 /* UDP protocol type */
  50. #define IP_PROT_GRE 0x2f /* GRE protocol type */
  51. #define IP_PROT_ICMP6 0x3a /* ICMPv6 protocol type */
  52. /* IPV4 field offsets */
  53. #define IPV4_VER_HL_OFFSET 0 /* version and ihl byte offset */
  54. #define IPV4_TOS_OFFSET 1 /* type of service offset */
  55. #define IPV4_PKTLEN_OFFSET 2 /* packet length offset */
  56. #define IPV4_PKTFLAG_OFFSET 6 /* more-frag,dont-frag flag offset */
  57. #define IPV4_PROT_OFFSET 9 /* protocol type offset */
  58. #define IPV4_CHKSUM_OFFSET 10 /* IP header checksum offset */
  59. #define IPV4_SRC_IP_OFFSET 12 /* src IP addr offset */
  60. #define IPV4_DEST_IP_OFFSET 16 /* dest IP addr offset */
  61. #define IPV4_OPTIONS_OFFSET 20 /* IP options offset */
  62. #define IPV4_MIN_HEADER_LEN 20 /* Minimum size for an IP header (no options) */
  63. /* IPV4 field decodes */
  64. #define IPV4_VER_MASK 0xf0 /* IPV4 version mask */
  65. #define IPV4_VER_SHIFT 4 /* IPV4 version shift */
  66. #define IPV4_HLEN_MASK 0x0f /* IPV4 header length mask */
  67. #define IPV4_HLEN(ipv4_body) (4 * (((uint8 *)(ipv4_body))[IPV4_VER_HL_OFFSET] & IPV4_HLEN_MASK))
  68. #define IPV4_HLEN_MIN (4 * 5) /* IPV4 header minimum length */
  69. #define IPV4_ADDR_LEN 4 /* IPV4 address length */
  70. #define IPV4_ADDR_NULL(a) ((((uint8 *)(a))[0] | ((uint8 *)(a))[1] | \
  71. ((uint8 *)(a))[2] | ((uint8 *)(a))[3]) == 0)
  72. #define IPV4_ADDR_BCAST(a) ((((uint8 *)(a))[0] & ((uint8 *)(a))[1] & \
  73. ((uint8 *)(a))[2] & ((uint8 *)(a))[3]) == 0xff)
  74. #define IPV4_TOS_DSCP_MASK 0xfc /* DiffServ codepoint mask */
  75. #define IPV4_TOS_DSCP_SHIFT 2 /* DiffServ codepoint shift */
  76. #define IPV4_TOS(ipv4_body) (((uint8 *)(ipv4_body))[IPV4_TOS_OFFSET])
  77. #define IPV4_TOS_PREC_MASK 0xe0 /* Historical precedence mask */
  78. #define IPV4_TOS_PREC_SHIFT 5 /* Historical precedence shift */
  79. #define IPV4_TOS_LOWDELAY 0x10 /* Lowest delay requested */
  80. #define IPV4_TOS_THROUGHPUT 0x8 /* Best throughput requested */
  81. #define IPV4_TOS_RELIABILITY 0x4 /* Most reliable delivery requested */
  82. #define IPV4_TOS_ROUTINE 0
  83. #define IPV4_TOS_PRIORITY 1
  84. #define IPV4_TOS_IMMEDIATE 2
  85. #define IPV4_TOS_FLASH 3
  86. #define IPV4_TOS_FLASHOVERRIDE 4
  87. #define IPV4_TOS_CRITICAL 5
  88. #define IPV4_TOS_INETWORK_CTRL 6
  89. #define IPV4_TOS_NETWORK_CTRL 7
  90. #define IPV4_PROT(ipv4_body) (((uint8 *)(ipv4_body))[IPV4_PROT_OFFSET])
  91. #define IPV4_FRAG_RESV 0x8000 /* Reserved */
  92. #define IPV4_FRAG_DONT 0x4000 /* Don't fragment */
  93. #define IPV4_FRAG_MORE 0x2000 /* More fragments */
  94. #define IPV4_FRAG_OFFSET_MASK 0x1fff /* Fragment offset */
  95. #define IPV4_ADDR_STR_LEN 16 /* Max IP address length in string format */
  96. /* IPV4 packet formats */
  97. BWL_PRE_PACKED_STRUCT struct ipv4_addr {
  98. uint8 addr[IPV4_ADDR_LEN];
  99. } BWL_POST_PACKED_STRUCT;
  100. BWL_PRE_PACKED_STRUCT struct ipv4_hdr {
  101. uint8 version_ihl; /* Version and Internet Header Length */
  102. uint8 tos; /* Type Of Service */
  103. uint16 tot_len; /* Number of bytes in packet (max 65535) */
  104. uint16 id;
  105. uint16 frag; /* 3 flag bits and fragment offset */
  106. uint8 ttl; /* Time To Live */
  107. uint8 prot; /* Protocol */
  108. uint16 hdr_chksum; /* IP header checksum */
  109. uint8 src_ip[IPV4_ADDR_LEN]; /* Source IP Address */
  110. uint8 dst_ip[IPV4_ADDR_LEN]; /* Destination IP Address */
  111. } BWL_POST_PACKED_STRUCT;
  112. /* IPV6 field offsets */
  113. #define IPV6_PAYLOAD_LEN_OFFSET 4 /* payload length offset */
  114. #define IPV6_NEXT_HDR_OFFSET 6 /* next header/protocol offset */
  115. #define IPV6_HOP_LIMIT_OFFSET 7 /* hop limit offset */
  116. #define IPV6_SRC_IP_OFFSET 8 /* src IP addr offset */
  117. #define IPV6_DEST_IP_OFFSET 24 /* dst IP addr offset */
  118. /* IPV6 field decodes */
  119. #define IPV6_TRAFFIC_CLASS(ipv6_body) \
  120. (((((uint8 *)(ipv6_body))[0] & 0x0f) << 4) | \
  121. ((((uint8 *)(ipv6_body))[1] & 0xf0) >> 4))
  122. #define IPV6_FLOW_LABEL(ipv6_body) \
  123. (((((uint8 *)(ipv6_body))[1] & 0x0f) << 16) | \
  124. (((uint8 *)(ipv6_body))[2] << 8) | \
  125. (((uint8 *)(ipv6_body))[3]))
  126. #define IPV6_PAYLOAD_LEN(ipv6_body) \
  127. ((((uint8 *)(ipv6_body))[IPV6_PAYLOAD_LEN_OFFSET + 0] << 8) | \
  128. ((uint8 *)(ipv6_body))[IPV6_PAYLOAD_LEN_OFFSET + 1])
  129. #define IPV6_NEXT_HDR(ipv6_body) \
  130. (((uint8 *)(ipv6_body))[IPV6_NEXT_HDR_OFFSET])
  131. #define IPV6_PROT(ipv6_body) IPV6_NEXT_HDR(ipv6_body)
  132. #define IPV6_ADDR_LEN 16 /* IPV6 address length */
  133. /* IPV4 TOS or IPV6 Traffic Classifier or 0 */
  134. #define IP_TOS46(ip_body) \
  135. (IP_VER(ip_body) == IP_VER_4 ? IPV4_TOS(ip_body) : \
  136. IP_VER(ip_body) == IP_VER_6 ? IPV6_TRAFFIC_CLASS(ip_body) : 0)
  137. #define IP_DSCP46(ip_body) (IP_TOS46(ip_body) >> IPV4_TOS_DSCP_SHIFT);
  138. /* IPV4 or IPV6 Protocol Classifier or 0 */
  139. #define IP_PROT46(ip_body) \
  140. (IP_VER(ip_body) == IP_VER_4 ? IPV4_PROT(ip_body) : \
  141. IP_VER(ip_body) == IP_VER_6 ? IPV6_PROT(ip_body) : 0)
  142. /* IPV6 extension headers (options) */
  143. #define IPV6_EXTHDR_HOP 0
  144. #define IPV6_EXTHDR_ROUTING 43
  145. #define IPV6_EXTHDR_FRAGMENT 44
  146. #define IPV6_EXTHDR_AUTH 51
  147. #define IPV6_EXTHDR_NONE 59
  148. #define IPV6_EXTHDR_DEST 60
  149. #define IPV6_EXTHDR(prot) (((prot) == IPV6_EXTHDR_HOP) || \
  150. ((prot) == IPV6_EXTHDR_ROUTING) || \
  151. ((prot) == IPV6_EXTHDR_FRAGMENT) || \
  152. ((prot) == IPV6_EXTHDR_AUTH) || \
  153. ((prot) == IPV6_EXTHDR_NONE) || \
  154. ((prot) == IPV6_EXTHDR_DEST))
  155. #define IPV6_MIN_HLEN 40
  156. #define IPV6_EXTHDR_LEN(eh) ((((struct ipv6_exthdr *)(eh))->hdrlen + 1) << 3)
  157. BWL_PRE_PACKED_STRUCT struct ipv6_exthdr {
  158. uint8 nexthdr;
  159. uint8 hdrlen;
  160. } BWL_POST_PACKED_STRUCT;
  161. BWL_PRE_PACKED_STRUCT struct ipv6_exthdr_frag {
  162. uint8 nexthdr;
  163. uint8 rsvd;
  164. uint16 frag_off;
  165. uint32 ident;
  166. } BWL_POST_PACKED_STRUCT;
  167. static INLINE int32
  168. ipv6_exthdr_len(uint8 *h, uint8 *proto)
  169. {
  170. uint16 len = 0, hlen;
  171. struct ipv6_exthdr *eh = (struct ipv6_exthdr *)h;
  172. while (IPV6_EXTHDR(eh->nexthdr)) {
  173. if (eh->nexthdr == IPV6_EXTHDR_NONE)
  174. return -1;
  175. else if (eh->nexthdr == IPV6_EXTHDR_FRAGMENT)
  176. hlen = 8U;
  177. else if (eh->nexthdr == IPV6_EXTHDR_AUTH)
  178. hlen = (uint16)((eh->hdrlen + 2U) << 2U);
  179. else
  180. hlen = (uint16)IPV6_EXTHDR_LEN(eh);
  181. len += hlen;
  182. eh = (struct ipv6_exthdr *)(h + len);
  183. }
  184. *proto = eh->nexthdr;
  185. return len;
  186. }
  187. #define IPV4_ISMULTI(a) (((a) & 0xf0000000) == 0xe0000000)
  188. #define IPV4_MCAST_TO_ETHER_MCAST(ipv4, ether) \
  189. { \
  190. ether[0] = 0x01; \
  191. ether[1] = 0x00; \
  192. ether[2] = 0x5E; \
  193. ether[3] = (ipv4 & 0x7f0000) >> 16; \
  194. ether[4] = (ipv4 & 0xff00) >> 8; \
  195. ether[5] = (ipv4 & 0xff); \
  196. }
  197. /* This marks the end of a packed structure section. */
  198. #include <packed_section_end.h>
  199. #define IPV4_ADDR_STR "%d.%d.%d.%d"
  200. #define IPV4_ADDR_TO_STR(addr) ((uint32)addr & 0xff000000) >> 24, \
  201. ((uint32)addr & 0x00ff0000) >> 16, \
  202. ((uint32)addr & 0x0000ff00) >> 8, \
  203. ((uint32)addr & 0x000000ff)
  204. #endif /* _bcmip_h_ */