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