common.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * wpa_supplicant/hostapd / common helper functions, etc.
  3. * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #ifndef COMMON_H
  9. #define COMMON_H
  10. #include "os.h"
  11. #if defined(__linux__) || defined(__GLIBC__)
  12. #include <endian.h>
  13. #include <byteswap.h>
  14. #endif /* __linux__ */
  15. #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
  16. defined(__OpenBSD__)
  17. #include <sys/types.h>
  18. #include <sys/endian.h>
  19. #define __BYTE_ORDER _BYTE_ORDER
  20. #define __LITTLE_ENDIAN _LITTLE_ENDIAN
  21. #define __BIG_ENDIAN _BIG_ENDIAN
  22. #ifdef __OpenBSD__
  23. #define bswap_16 swap16
  24. #define bswap_32 swap32
  25. #define bswap_64 swap64
  26. #else /* __OpenBSD__ */
  27. #define bswap_16 bswap16
  28. #define bswap_32 bswap32
  29. #define bswap_64 bswap64
  30. #endif /* __OpenBSD__ */
  31. #endif /* defined(__FreeBSD__) || defined(__NetBSD__) ||
  32. * defined(__DragonFly__) || defined(__OpenBSD__) */
  33. #ifdef __APPLE__
  34. #include <sys/types.h>
  35. #include <machine/endian.h>
  36. #define __BYTE_ORDER _BYTE_ORDER
  37. #define __LITTLE_ENDIAN _LITTLE_ENDIAN
  38. #define __BIG_ENDIAN _BIG_ENDIAN
  39. static inline unsigned short bswap_16(unsigned short v)
  40. {
  41. return ((v & 0xff) << 8) | (v >> 8);
  42. }
  43. static inline unsigned int bswap_32(unsigned int v)
  44. {
  45. return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
  46. ((v & 0xff0000) >> 8) | (v >> 24);
  47. }
  48. #endif /* __APPLE__ */
  49. #ifdef CONFIG_TI_COMPILER
  50. #define __BIG_ENDIAN 4321
  51. #define __LITTLE_ENDIAN 1234
  52. #ifdef __big_endian__
  53. #define __BYTE_ORDER __BIG_ENDIAN
  54. #else
  55. #define __BYTE_ORDER __LITTLE_ENDIAN
  56. #endif
  57. #endif /* CONFIG_TI_COMPILER */
  58. #ifdef CONFIG_NATIVE_WINDOWS
  59. #include <winsock.h>
  60. typedef int socklen_t;
  61. #ifndef MSG_DONTWAIT
  62. #define MSG_DONTWAIT 0 /* not supported */
  63. #endif
  64. #endif /* CONFIG_NATIVE_WINDOWS */
  65. #ifdef _MSC_VER
  66. #define inline __inline
  67. #undef vsnprintf
  68. #define vsnprintf _vsnprintf
  69. #undef close
  70. #define close closesocket
  71. #endif /* _MSC_VER */
  72. /* Define platform specific integer types */
  73. #ifdef _MSC_VER
  74. typedef UINT64 u64;
  75. typedef UINT32 u32;
  76. typedef UINT16 u16;
  77. typedef UINT8 u8;
  78. typedef INT64 s64;
  79. typedef INT32 s32;
  80. typedef INT16 s16;
  81. typedef INT8 s8;
  82. #define WPA_TYPES_DEFINED
  83. #endif /* _MSC_VER */
  84. #ifdef __vxworks
  85. typedef unsigned long long u64;
  86. typedef UINT32 u32;
  87. typedef UINT16 u16;
  88. typedef UINT8 u8;
  89. typedef long long s64;
  90. typedef INT32 s32;
  91. typedef INT16 s16;
  92. typedef INT8 s8;
  93. #define WPA_TYPES_DEFINED
  94. #endif /* __vxworks */
  95. #ifdef CONFIG_TI_COMPILER
  96. #ifdef _LLONG_AVAILABLE
  97. typedef unsigned long long u64;
  98. #else
  99. /*
  100. * TODO: 64-bit variable not available. Using long as a workaround to test the
  101. * build, but this will likely not work for all operations.
  102. */
  103. typedef unsigned long u64;
  104. #endif
  105. typedef unsigned int u32;
  106. typedef unsigned short u16;
  107. typedef unsigned char u8;
  108. #define WPA_TYPES_DEFINED
  109. #endif /* CONFIG_TI_COMPILER */
  110. #ifndef WPA_TYPES_DEFINED
  111. #ifdef CONFIG_USE_INTTYPES_H
  112. #include <inttypes.h>
  113. #else
  114. #include <stdint.h>
  115. #endif
  116. typedef uint64_t u64;
  117. typedef uint32_t u32;
  118. typedef uint16_t u16;
  119. typedef uint8_t u8;
  120. typedef int64_t s64;
  121. typedef int32_t s32;
  122. typedef int16_t s16;
  123. typedef int8_t s8;
  124. #define WPA_TYPES_DEFINED
  125. #endif /* !WPA_TYPES_DEFINED */
  126. /* Define platform specific byte swapping macros */
  127. #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
  128. static inline unsigned short wpa_swap_16(unsigned short v)
  129. {
  130. return ((v & 0xff) << 8) | (v >> 8);
  131. }
  132. static inline unsigned int wpa_swap_32(unsigned int v)
  133. {
  134. return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
  135. ((v & 0xff0000) >> 8) | (v >> 24);
  136. }
  137. #define le_to_host16(n) (n)
  138. #define host_to_le16(n) (n)
  139. #define be_to_host16(n) wpa_swap_16(n)
  140. #define host_to_be16(n) wpa_swap_16(n)
  141. #define le_to_host32(n) (n)
  142. #define be_to_host32(n) wpa_swap_32(n)
  143. #define host_to_be32(n) wpa_swap_32(n)
  144. #define WPA_BYTE_SWAP_DEFINED
  145. #endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
  146. #ifndef WPA_BYTE_SWAP_DEFINED
  147. #ifndef __BYTE_ORDER
  148. #ifndef __LITTLE_ENDIAN
  149. #ifndef __BIG_ENDIAN
  150. #define __LITTLE_ENDIAN 1234
  151. #define __BIG_ENDIAN 4321
  152. #if defined(sparc)
  153. #define __BYTE_ORDER __BIG_ENDIAN
  154. #endif
  155. #endif /* __BIG_ENDIAN */
  156. #endif /* __LITTLE_ENDIAN */
  157. #endif /* __BYTE_ORDER */
  158. #if __BYTE_ORDER == __LITTLE_ENDIAN
  159. #define le_to_host16(n) ((__force u16) (le16) (n))
  160. #define host_to_le16(n) ((__force le16) (u16) (n))
  161. #define be_to_host16(n) bswap_16((__force u16) (be16) (n))
  162. #define host_to_be16(n) ((__force be16) bswap_16((n)))
  163. #define le_to_host32(n) ((__force u32) (le32) (n))
  164. #define host_to_le32(n) ((__force le32) (u32) (n))
  165. #define be_to_host32(n) bswap_32((__force u32) (be32) (n))
  166. #define host_to_be32(n) ((__force be32) bswap_32((n)))
  167. #define le_to_host64(n) ((__force u64) (le64) (n))
  168. #define host_to_le64(n) ((__force le64) (u64) (n))
  169. #define be_to_host64(n) bswap_64((__force u64) (be64) (n))
  170. #define host_to_be64(n) ((__force be64) bswap_64((n)))
  171. #elif __BYTE_ORDER == __BIG_ENDIAN
  172. #define le_to_host16(n) bswap_16(n)
  173. #define host_to_le16(n) bswap_16(n)
  174. #define be_to_host16(n) (n)
  175. #define host_to_be16(n) (n)
  176. #define le_to_host32(n) bswap_32(n)
  177. #define host_to_le32(n) bswap_32(n)
  178. #define be_to_host32(n) (n)
  179. #define host_to_be32(n) (n)
  180. #define le_to_host64(n) bswap_64(n)
  181. #define host_to_le64(n) bswap_64(n)
  182. #define be_to_host64(n) (n)
  183. #define host_to_be64(n) (n)
  184. #ifndef WORDS_BIGENDIAN
  185. #define WORDS_BIGENDIAN
  186. #endif
  187. #else
  188. #error Could not determine CPU byte order
  189. #endif
  190. #define WPA_BYTE_SWAP_DEFINED
  191. #endif /* !WPA_BYTE_SWAP_DEFINED */
  192. /* Macros for handling unaligned memory accesses */
  193. static inline u16 WPA_GET_BE16(const u8 *a)
  194. {
  195. return (a[0] << 8) | a[1];
  196. }
  197. static inline void WPA_PUT_BE16(u8 *a, u16 val)
  198. {
  199. a[0] = val >> 8;
  200. a[1] = val & 0xff;
  201. }
  202. static inline u16 WPA_GET_LE16(const u8 *a)
  203. {
  204. return (a[1] << 8) | a[0];
  205. }
  206. static inline void WPA_PUT_LE16(u8 *a, u16 val)
  207. {
  208. a[1] = val >> 8;
  209. a[0] = val & 0xff;
  210. }
  211. static inline u32 WPA_GET_BE24(const u8 *a)
  212. {
  213. return (a[0] << 16) | (a[1] << 8) | a[2];
  214. }
  215. static inline void WPA_PUT_BE24(u8 *a, u32 val)
  216. {
  217. a[0] = (val >> 16) & 0xff;
  218. a[1] = (val >> 8) & 0xff;
  219. a[2] = val & 0xff;
  220. }
  221. static inline u32 WPA_GET_BE32(const u8 *a)
  222. {
  223. return (a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3];
  224. }
  225. static inline void WPA_PUT_BE32(u8 *a, u32 val)
  226. {
  227. a[0] = (val >> 24) & 0xff;
  228. a[1] = (val >> 16) & 0xff;
  229. a[2] = (val >> 8) & 0xff;
  230. a[3] = val & 0xff;
  231. }
  232. static inline u32 WPA_GET_LE32(const u8 *a)
  233. {
  234. return (a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0];
  235. }
  236. static inline void WPA_PUT_LE32(u8 *a, u32 val)
  237. {
  238. a[3] = (val >> 24) & 0xff;
  239. a[2] = (val >> 16) & 0xff;
  240. a[1] = (val >> 8) & 0xff;
  241. a[0] = val & 0xff;
  242. }
  243. static inline u64 WPA_GET_BE64(const u8 *a)
  244. {
  245. return (((u64) a[0]) << 56) | (((u64) a[1]) << 48) |
  246. (((u64) a[2]) << 40) | (((u64) a[3]) << 32) |
  247. (((u64) a[4]) << 24) | (((u64) a[5]) << 16) |
  248. (((u64) a[6]) << 8) | ((u64) a[7]);
  249. }
  250. static inline void WPA_PUT_BE64(u8 *a, u64 val)
  251. {
  252. a[0] = val >> 56;
  253. a[1] = val >> 48;
  254. a[2] = val >> 40;
  255. a[3] = val >> 32;
  256. a[4] = val >> 24;
  257. a[5] = val >> 16;
  258. a[6] = val >> 8;
  259. a[7] = val & 0xff;
  260. }
  261. static inline u64 WPA_GET_LE64(const u8 *a)
  262. {
  263. return (((u64) a[7]) << 56) | (((u64) a[6]) << 48) |
  264. (((u64) a[5]) << 40) | (((u64) a[4]) << 32) |
  265. (((u64) a[3]) << 24) | (((u64) a[2]) << 16) |
  266. (((u64) a[1]) << 8) | ((u64) a[0]);
  267. }
  268. static inline void WPA_PUT_LE64(u8 *a, u64 val)
  269. {
  270. a[7] = val >> 56;
  271. a[6] = val >> 48;
  272. a[5] = val >> 40;
  273. a[4] = val >> 32;
  274. a[3] = val >> 24;
  275. a[2] = val >> 16;
  276. a[1] = val >> 8;
  277. a[0] = val & 0xff;
  278. }
  279. #ifndef ETH_ALEN
  280. #define ETH_ALEN 6
  281. #endif
  282. #ifndef ETH_HLEN
  283. #define ETH_HLEN 14
  284. #endif
  285. #ifndef IFNAMSIZ
  286. #define IFNAMSIZ 16
  287. #endif
  288. #ifndef ETH_P_ALL
  289. #define ETH_P_ALL 0x0003
  290. #endif
  291. #ifndef ETH_P_80211_ENCAP
  292. #define ETH_P_80211_ENCAP 0x890d /* TDLS comes under this category */
  293. #endif
  294. #ifndef ETH_P_PAE
  295. #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
  296. #endif /* ETH_P_PAE */
  297. #ifndef ETH_P_EAPOL
  298. #define ETH_P_EAPOL ETH_P_PAE
  299. #endif /* ETH_P_EAPOL */
  300. #ifndef ETH_P_RSN_PREAUTH
  301. #define ETH_P_RSN_PREAUTH 0x88c7
  302. #endif /* ETH_P_RSN_PREAUTH */
  303. #ifndef ETH_P_RRB
  304. #define ETH_P_RRB 0x890D
  305. #endif /* ETH_P_RRB */
  306. #ifdef __GNUC__
  307. #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
  308. #define STRUCT_PACKED __attribute__ ((packed))
  309. #else
  310. #define PRINTF_FORMAT(a,b)
  311. #define STRUCT_PACKED
  312. #endif
  313. #ifdef CONFIG_ANSI_C_EXTRA
  314. #if !defined(_MSC_VER) || _MSC_VER < 1400
  315. /* snprintf - used in number of places; sprintf() is _not_ a good replacement
  316. * due to possible buffer overflow; see, e.g.,
  317. * http://www.ijs.si/software/snprintf/ for portable implementation of
  318. * snprintf. */
  319. int snprintf(char *str, size_t size, const char *format, ...);
  320. /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
  321. int vsnprintf(char *str, size_t size, const char *format, va_list ap);
  322. #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
  323. /* getopt - only used in main.c */
  324. int getopt(int argc, char *const argv[], const char *optstring);
  325. extern char *optarg;
  326. extern int optind;
  327. #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
  328. #ifndef __socklen_t_defined
  329. typedef int socklen_t;
  330. #endif
  331. #endif
  332. /* inline - define as __inline or just define it to be empty, if needed */
  333. #ifdef CONFIG_NO_INLINE
  334. #define inline
  335. #else
  336. #define inline __inline
  337. #endif
  338. #ifndef __func__
  339. #define __func__ "__func__ not defined"
  340. #endif
  341. #ifndef bswap_16
  342. #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
  343. #endif
  344. #ifndef bswap_32
  345. #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
  346. (((u32) (a) << 8) & 0xff0000) | \
  347. (((u32) (a) >> 8) & 0xff00) | \
  348. (((u32) (a) >> 24) & 0xff))
  349. #endif
  350. #ifndef MSG_DONTWAIT
  351. #define MSG_DONTWAIT 0
  352. #endif
  353. #ifdef _WIN32_WCE
  354. void perror(const char *s);
  355. #endif /* _WIN32_WCE */
  356. #endif /* CONFIG_ANSI_C_EXTRA */
  357. #ifndef MAC2STR
  358. #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
  359. #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
  360. /*
  361. * Compact form for string representation of MAC address
  362. * To be used, e.g., for constructing dbus paths for P2P Devices
  363. */
  364. #define COMPACT_MACSTR "%02x%02x%02x%02x%02x%02x"
  365. #endif
  366. #ifndef BIT
  367. #define BIT(x) (1 << (x))
  368. #endif
  369. /*
  370. * Definitions for sparse validation
  371. * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
  372. */
  373. #ifdef __CHECKER__
  374. #define __force __attribute__((force))
  375. #define __bitwise __attribute__((bitwise))
  376. #else
  377. #define __force
  378. #define __bitwise
  379. #endif
  380. typedef u16 __bitwise be16;
  381. typedef u16 __bitwise le16;
  382. typedef u32 __bitwise be32;
  383. typedef u32 __bitwise le32;
  384. typedef u64 __bitwise be64;
  385. typedef u64 __bitwise le64;
  386. #ifndef __must_check
  387. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
  388. #define __must_check __attribute__((__warn_unused_result__))
  389. #else
  390. #define __must_check
  391. #endif /* __GNUC__ */
  392. #endif /* __must_check */
  393. #ifndef __maybe_unused
  394. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
  395. #define __maybe_unused __attribute__((unused))
  396. #else
  397. #define __maybe_unused
  398. #endif /* __GNUC__ */
  399. #endif /* __must_check */
  400. int hwaddr_aton(const char *txt, u8 *addr);
  401. int hwaddr_compact_aton(const char *txt, u8 *addr);
  402. int hwaddr_aton2(const char *txt, u8 *addr);
  403. int hex2byte(const char *hex);
  404. int hexstr2bin(const char *hex, u8 *buf, size_t len);
  405. void inc_byte_array(u8 *counter, size_t len);
  406. void wpa_get_ntp_timestamp(u8 *buf);
  407. int wpa_scnprintf(char *buf, size_t size, const char *fmt, ...);
  408. int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
  409. int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
  410. size_t len);
  411. #ifdef CONFIG_NATIVE_WINDOWS
  412. void wpa_unicode2ascii_inplace(TCHAR *str);
  413. TCHAR * wpa_strdup_tchar(const char *str);
  414. #else /* CONFIG_NATIVE_WINDOWS */
  415. #define wpa_unicode2ascii_inplace(s) do { } while (0)
  416. #define wpa_strdup_tchar(s) strdup((s))
  417. #endif /* CONFIG_NATIVE_WINDOWS */
  418. void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len);
  419. size_t printf_decode(u8 *buf, size_t maxlen, const char *str);
  420. const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len);
  421. char * wpa_config_parse_string(const char *value, size_t *len);
  422. int is_hex(const u8 *data, size_t len);
  423. int find_first_bit(u32 value);
  424. size_t merge_byte_arrays(u8 *res, size_t res_len,
  425. const u8 *src1, size_t src1_len,
  426. const u8 *src2, size_t src2_len);
  427. char * dup_binstr(const void *src, size_t len);
  428. static inline int is_zero_ether_addr(const u8 *a)
  429. {
  430. return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]);
  431. }
  432. static inline int is_broadcast_ether_addr(const u8 *a)
  433. {
  434. return (a[0] & a[1] & a[2] & a[3] & a[4] & a[5]) == 0xff;
  435. }
  436. #define broadcast_ether_addr (const u8 *) "\xff\xff\xff\xff\xff\xff"
  437. #include "wpa_debug.h"
  438. struct wpa_freq_range_list {
  439. struct wpa_freq_range {
  440. unsigned int min;
  441. unsigned int max;
  442. } *range;
  443. unsigned int num;
  444. };
  445. int freq_range_list_parse(struct wpa_freq_range_list *res, const char *value);
  446. int freq_range_list_includes(const struct wpa_freq_range_list *list,
  447. unsigned int freq);
  448. char * freq_range_list_str(const struct wpa_freq_range_list *list);
  449. int int_array_len(const int *a);
  450. void int_array_concat(int **res, const int *a);
  451. void int_array_sort_unique(int *a);
  452. void int_array_add_unique(int **res, int a);
  453. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  454. void str_clear_free(char *str);
  455. void bin_clear_free(void *bin, size_t len);
  456. int random_mac_addr(u8 *addr);
  457. int random_mac_addr_keep_oui(u8 *addr);
  458. char * str_token(char *str, const char *delim, char **context);
  459. /*
  460. * gcc 4.4 ends up generating strict-aliasing warnings about some very common
  461. * networking socket uses that do not really result in a real problem and
  462. * cannot be easily avoided with union-based type-punning due to struct
  463. * definitions including another struct in system header files. To avoid having
  464. * to fully disable strict-aliasing warnings, provide a mechanism to hide the
  465. * typecast from aliasing for now. A cleaner solution will hopefully be found
  466. * in the future to handle these cases.
  467. */
  468. void * __hide_aliasing_typecast(void *foo);
  469. #define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
  470. #ifdef CONFIG_VALGRIND
  471. #include <valgrind/memcheck.h>
  472. #define WPA_MEM_DEFINED(ptr, len) VALGRIND_MAKE_MEM_DEFINED((ptr), (len))
  473. #else /* CONFIG_VALGRIND */
  474. #define WPA_MEM_DEFINED(ptr, len) do { } while (0)
  475. #endif /* CONFIG_VALGRIND */
  476. #endif /* COMMON_H */