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