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