ipv6_defs.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved.
  3. *
  4. * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
  5. *
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for
  8. * any purpose with or without fee is hereby granted, provided that the
  9. * above copyright notice and this permission notice appear in all
  10. * copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  13. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  15. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  16. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  17. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  18. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  19. * PERFORMANCE OF THIS SOFTWARE.
  20. */
  21. /*
  22. * This file was originally distributed by Qualcomm Atheros, Inc.
  23. * under proprietary terms before Copyright ownership was assigned
  24. * to the Linux Foundation.
  25. */
  26. #ifndef _IPV6__H_
  27. #define _IPV6__H_
  28. #if defined(ATH_TARGET)
  29. #include <osapi.h> /* A_UINT8 */
  30. #else
  31. #include <a_types.h> /* A_UINT8 */
  32. #endif
  33. /* utilities for converting between network byte order and native endianness */
  34. #ifndef BYTESWAP32
  35. #define BYTESWAP32(x) \
  36. ((((x) & 0x000000ff) << 24) /* byte 0 -> byte 3 */ | \
  37. (((x) & 0x0000ff00) << 8) /* byte 1 -> byte 2 */ | \
  38. (((x) & 0x00ff0000) >> 8) /* byte 2 -> byte 1 */ | \
  39. (((x) & 0xff000000) >> 24) /* byte 3 -> byte 0 */)
  40. #endif /* BYTESWAP32 */
  41. #ifndef BE_TO_CPU32
  42. #if defined(ATH_TARGET)
  43. /* assume target is little-endian */
  44. #define BE_TO_CPU32(x) BYTESWAP32(x)
  45. #else
  46. #ifdef BIG_ENDIAN_HOST
  47. #define BE_TO_CPU32(x) (x)
  48. #else
  49. #define BE_TO_CPU32(x) BYTESWAP32(x)
  50. #endif
  51. #endif
  52. #endif /* BE_TO_CPU32 */
  53. /* IPv6 header definition */
  54. #define IPV6_ADDR_LEN 4 /* bytes */
  55. struct ipv6_hdr_t {
  56. A_UINT32 ver_tclass_flowlabel; /* version, traffic class, and flow label */
  57. A_UINT8 pyld_len[2]; /* payload length */
  58. A_UINT8 next_hdr;
  59. A_UINT8 hop_limit;
  60. A_UINT8 src_addr[IPV6_ADDR_LEN];
  61. A_UINT8 dst_addr[IPV6_ADDR_LEN];
  62. };
  63. #define IPV6_HDR_LEN (sizeof(struct ipv6_hdr_t))
  64. #define IPV6_HDR_OFFSET_NEXT_HDR (offsetof(struct ipv6_hdr_t, next_hdr))
  65. #define IPV6_HDR_OFFSET_DST_ADDR (offsetof(struct ipv6_hdr_t, dst_addr[0]))
  66. /* IPv6 header field access macros */
  67. #define IPV6_HDR_VERSION_M 0xF0000000
  68. #define IPV6_HDR_VERSION_S 28
  69. #define IPV6_HDR_TRAFFIC_CLASS_M 0x0FF00000
  70. #define IPV6_HDR_TRAFFIC_CLASS_S 20
  71. #define IPV6_HDR_FLOW_LABEL_M 0x000FFFFF
  72. #define IPV6_HDR_FLOW_LABEL_S 0
  73. static inline A_UINT8 ipv6_version(struct ipv6_hdr_t *ipv6_hdr)
  74. {
  75. return
  76. (BE_TO_CPU32(ipv6_hdr->ver_tclass_flowlabel) &
  77. IPV6_HDR_VERSION_M) >> IPV6_HDR_VERSION_S;
  78. }
  79. static inline A_UINT8 ipv6_traffic_class(struct ipv6_hdr_t *ipv6_hdr)
  80. {
  81. return
  82. (A_UINT8) ((BE_TO_CPU32(ipv6_hdr->ver_tclass_flowlabel) &
  83. IPV6_HDR_TRAFFIC_CLASS_M) >> IPV6_HDR_TRAFFIC_CLASS_S);
  84. }
  85. static inline A_UINT32 ipv6_flow_label(struct ipv6_hdr_t *ipv6_hdr)
  86. {
  87. return
  88. (BE_TO_CPU32(ipv6_hdr->ver_tclass_flowlabel) &
  89. IPV6_HDR_FLOW_LABEL_M) >> IPV6_HDR_FLOW_LABEL_S;
  90. }
  91. #endif /* _IPV6__H_ */