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