tls_common.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #ifndef TLS_COMMON_H
  2. #define TLS_COMMON_H
  3. #include "wm_osal.h"
  4. #include "wm_config.h"
  5. #include "wm_regs.h"
  6. #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
  7. #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
  8. #define time_after(a, b) ((long)(b) - (long)(a) < 0)
  9. #define time_before(a,b) time_after(b,a)
  10. /*
  11. * These are the defined Ethernet Protocol ID's.
  12. */
  13. #define ETH_P_LOOP 0x0060 /* Ethernet Loopback packet */
  14. #define ETH_P_PUP 0x0200 /* Xerox PUP packet */
  15. #define ETH_P_PUPAT 0x0201 /* Xerox PUP Addr Trans packet */
  16. #define ETH_P_IP 0x0800 /* Internet Protocol packet */
  17. #define ETH_P_X25 0x0805 /* CCITT X.25 */
  18. #define ETH_P_ARP 0x0806 /* Address Resolution packet */
  19. #define ETH_P_BPQ 0x08FF /* G8BPQ AX.25 Ethernet Packet [ NOT AN OFFICIALLY REGISTERED ID ] */
  20. #define ETH_P_IEEEPUP 0x0a00 /* Xerox IEEE802.3 PUP packet */
  21. #define ETH_P_IEEEPUPAT 0x0a01 /* Xerox IEEE802.3 PUP Addr Trans packet */
  22. #define ETH_P_DEC 0x6000 /* DEC Assigned proto */
  23. #define ETH_P_DNA_DL 0x6001 /* DEC DNA Dump/Load */
  24. #define ETH_P_DNA_RC 0x6002 /* DEC DNA Remote Console */
  25. #define ETH_P_DNA_RT 0x6003 /* DEC DNA Routing */
  26. #define ETH_P_LAT 0x6004 /* DEC LAT */
  27. #define ETH_P_DIAG 0x6005 /* DEC Diagnostics */
  28. #define ETH_P_CUST 0x6006 /* DEC Customer use */
  29. #define ETH_P_SCA 0x6007 /* DEC Systems Comms Arch */
  30. #define ETH_P_TEB 0x6558 /* Trans Ether Bridging */
  31. #define ETH_P_RARP 0x8035 /* Reverse Addr Res packet */
  32. #define ETH_P_ATALK 0x809B /* Appletalk DDP */
  33. #define ETH_P_AARP 0x80F3 /* Appletalk AARP */
  34. #define ETH_P_8021Q 0x8100 /* 802.1Q VLAN Extended Header */
  35. #define ETH_P_IPX 0x8137 /* IPX over DIX */
  36. #define ETH_P_IPV6 0x86DD /* IPv6 over bluebook */
  37. #define ETH_P_PAUSE 0x8808 /* IEEE Pause frames. See 802.3 31B */
  38. #define ETH_P_SLOW 0x8809 /* Slow Protocol. See 802.3ad 43B */
  39. #define ETH_P_WCCP 0x883E /* Web-cache coordination protocol
  40. * defined in draft-wilson-wrec-wccp-v2-00.txt */
  41. #define ETH_P_PPP_DISC 0x8863 /* PPPoE discovery messages */
  42. #define ETH_P_PPP_SES 0x8864 /* PPPoE session messages */
  43. #define ETH_P_MPLS_UC 0x8847 /* MPLS Unicast traffic */
  44. #define ETH_P_MPLS_MC 0x8848 /* MPLS Multicast traffic */
  45. #define ETH_P_ATMMPOA 0x884c /* MultiProtocol Over ATM */
  46. #define ETH_P_ATMFATE 0x8884 /* Frame-based ATM Transport
  47. * over Ethernet
  48. */
  49. #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
  50. #define ETH_P_WAPI 0x88B4 /* WAPI authentication */
  51. #define ETH_P_AOE 0x88A2 /* ATA over Ethernet */
  52. #define ETH_P_TIPC 0x88CA /* TIPC */
  53. #define ETH_P_FCOE 0x8906 /* Fibre Channel over Ethernet */
  54. #define ETH_P_EDSA 0xDADA /* Ethertype DSA [ NOT AN OFFICIALLY REGISTERED ID ] */
  55. #define ETH_HLEN 14 /* Total octets in header. */
  56. #ifndef ETH_ALEN
  57. #define ETH_ALEN 6
  58. #endif
  59. #ifndef IFNAMSIZ
  60. #define IFNAMSIZ 16
  61. #endif
  62. #ifndef ETH_P_ALL
  63. #define ETH_P_ALL 0x0003
  64. #endif
  65. #ifndef ETH_P_80211_ENCAP
  66. #define ETH_P_80211_ENCAP 0x890d /* TDLS comes under this category */
  67. #endif
  68. #ifndef ETH_P_PAE
  69. #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
  70. #endif /* ETH_P_PAE */
  71. #ifndef ETH_P_EAPOL
  72. #define ETH_P_EAPOL ETH_P_PAE
  73. #endif /* ETH_P_EAPOL */
  74. #ifndef ETH_P_RSN_PREAUTH
  75. #define ETH_P_RSN_PREAUTH 0x88c7
  76. #endif /* ETH_P_RSN_PREAUTH */
  77. #ifndef ETH_P_RRB
  78. #define ETH_P_RRB 0x890D
  79. #endif /* ETH_P_RRB */
  80. #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
  81. #define STRUCT_PACKED __attribute__ ((packed))
  82. #define broadcast_ether_addr (const u8 *) "\xff\xff\xff\xff\xff\xff"
  83. /**
  84. * compare_ether_addr - Compare two Ethernet addresses
  85. * @addr1: Pointer to a six-byte array containing the Ethernet address
  86. * @addr2: Pointer other six-byte array containing the Ethernet address
  87. *
  88. * Compare two ethernet addresses, returns 0 if equal
  89. */
  90. static __inline unsigned compare_ether_addr(const u8 *addr1, const u8 *addr2)
  91. {
  92. return !((addr1[0] == addr2[0]) && (addr1[1] == addr2[1]) && (addr1[2] == addr2[2]) && \
  93. (addr1[3] == addr2[3]) && (addr1[4] == addr2[4]) && (addr1[5] == addr2[5]));
  94. }
  95. static __inline int is_zero_ether_addr(const u8 *a)
  96. {
  97. return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]);
  98. }
  99. static __inline int is_broadcast_ether_addr(const u8 *a)
  100. {
  101. return (a[0] & a[1] & a[2] & a[3] & a[4] & a[5]) == 0xff;
  102. }
  103. static __inline int is_multicast_ether_addr(const u8 *addr)
  104. {
  105. return (0x01 & addr[0]);
  106. }
  107. static __inline unsigned short swap_16(unsigned short v)
  108. {
  109. return ((v & 0xff) << 8) | (v >> 8);
  110. }
  111. static __inline unsigned int swap_32(unsigned int v)
  112. {
  113. return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
  114. ((v & 0xff0000) >> 8) | (v >> 24);
  115. }
  116. #define le_to_host16(n) (n)
  117. #define host_to_le16(n) (n)
  118. #define be_to_host16(n) swap_16(n)
  119. #define host_to_be16(n) swap_16(n)
  120. #define le_to_host32(n) (n)
  121. #define le_to_host64(n) (n)
  122. #define be_to_host32(n) swap_32(n)
  123. #define host_to_be32(n) swap_32(n)
  124. static __inline u16 get_unaligned_le16(const u8 *p)
  125. {
  126. return p[0] | p[1] << 8;
  127. }
  128. static __inline u32 get_unaligned_le32(const u8 *p)
  129. {
  130. return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
  131. }
  132. static __inline void put_unaligned_le16(u16 val, u8 *p)
  133. {
  134. *p++ = val;
  135. *p++ = val >> 8;
  136. }
  137. static __inline void put_unaligned_le32(u32 val, u8 *p)
  138. {
  139. put_unaligned_le16(val >> 16, p + 2);
  140. put_unaligned_le16(val, p);
  141. }
  142. static __inline u16 get_unaligned_be16(const u8 *p)
  143. {
  144. return be_to_host16(get_unaligned_le16(p));
  145. }
  146. static __inline u32 get_unaligned_be32(const u8 *p)
  147. {
  148. return be_to_host32(get_unaligned_le32(p));
  149. }
  150. static __inline void put_unaligned_be16(u16 val, u8 *p)
  151. {
  152. *p++ = val >> 8;
  153. *p++ = val;
  154. }
  155. static __inline void put_unaligned_be32(u32 val, u8 *p)
  156. {
  157. put_unaligned_be16(val >> 16, p);
  158. put_unaligned_be16(val, p + 2);
  159. }
  160. #ifdef __CHECKER__
  161. #define __force __attribute__((force))
  162. #define __bitwise __attribute__((bitwise))
  163. #else
  164. #define __force
  165. #define __bitwise
  166. #endif
  167. typedef u16 __bitwise be16;
  168. typedef u16 __bitwise le16;
  169. typedef u32 __bitwise be32;
  170. typedef u32 __bitwise le32;
  171. typedef u64 __bitwise be64;
  172. typedef u64 __bitwise le64;
  173. /*
  174. * This is an Ethernet frame header.
  175. */
  176. struct ethhdr {
  177. unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
  178. unsigned char h_source[ETH_ALEN]; /* source ether addr */
  179. unsigned short h_proto; /* packet type ID field */
  180. } __attribute__((packed));
  181. typedef struct {
  182. volatile int counter;
  183. } atomic_t;
  184. struct kref {
  185. atomic_t refcount;
  186. };
  187. static __inline int atomic_sub_return(int i, atomic_t *v)
  188. {
  189. u32 cpu_sr;
  190. int val;
  191. cpu_sr = tls_os_set_critical();
  192. val = v->counter;
  193. v->counter = val -= i;
  194. tls_os_release_critical(cpu_sr);
  195. return val;
  196. }
  197. static __inline int atomic_add_return(int i, atomic_t *v)
  198. {
  199. u32 cpu_sr;
  200. int val;
  201. cpu_sr = tls_os_set_critical();
  202. val = v->counter;
  203. v->counter = val += i;
  204. tls_os_release_critical(cpu_sr);
  205. return val;
  206. }
  207. #define atomic_set(v,i) (((v)->counter) = (i))
  208. #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
  209. #define atomic_inc(v) (void) atomic_add_return(1, v)
  210. #define atomic_dec(v) (void) atomic_sub_return(1, v)
  211. #endif /* end of TLS_COMMON_H */