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