common.h 14 KB

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