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