1 /* 2 * wpa_supplicant/hostapd / common helper functions, etc. 3 * Copyright (c) 2002-2007, Jouni Malinen <j (at) 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_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 __SYMBIAN32__ 67 #define __BIG_ENDIAN 4321 68 #define __LITTLE_ENDIAN 1234 69 #define __BYTE_ORDER __LITTLE_ENDIAN 70 #endif /* __SYMBIAN32__ */ 71 72 #ifdef CONFIG_NATIVE_WINDOWS 73 #include <winsock.h> 74 75 typedef int socklen_t; 76 77 #ifndef MSG_DONTWAIT 78 #define MSG_DONTWAIT 0 /* not supported */ 79 #endif 80 81 #endif /* CONFIG_NATIVE_WINDOWS */ 82 83 #ifdef _MSC_VER 84 #define inline __inline 85 86 #undef vsnprintf 87 #define vsnprintf _vsnprintf 88 #undef close 89 #define close closesocket 90 #endif /* _MSC_VER */ 91 92 93 /* Define platform specific integer types */ 94 95 #ifdef _MSC_VER 96 typedef UINT64 u64; 97 typedef UINT32 u32; 98 typedef UINT16 u16; 99 typedef UINT8 u8; 100 typedef INT64 s64; 101 typedef INT32 s32; 102 typedef INT16 s16; 103 typedef INT8 s8; 104 #define WPA_TYPES_DEFINED 105 #endif /* _MSC_VER */ 106 107 #ifdef __vxworks 108 typedef unsigned long long u64; 109 typedef UINT32 u32; 110 typedef UINT16 u16; 111 typedef UINT8 u8; 112 typedef long long s64; 113 typedef INT32 s32; 114 typedef INT16 s16; 115 typedef INT8 s8; 116 #define WPA_TYPES_DEFINED 117 #endif /* __vxworks */ 118 119 #ifdef CONFIG_TI_COMPILER 120 #ifdef _LLONG_AVAILABLE 121 typedef unsigned long long u64; 122 #else 123 /* 124 * TODO: 64-bit variable not available. Using long as a workaround to test the 125 * build, but this will likely not work for all operations. 126 */ 127 typedef unsigned long u64; 128 #endif 129 typedef unsigned int u32; 130 typedef unsigned short u16; 131 typedef unsigned char u8; 132 #define WPA_TYPES_DEFINED 133 #endif /* CONFIG_TI_COMPILER */ 134 135 #ifdef __SYMBIAN32__ 136 #define __REMOVE_PLATSEC_DIAGNOSTICS__ 137 #include <e32def.h> 138 typedef TUint64 u64; 139 typedef TUint32 u32; 140 typedef TUint16 u16; 141 typedef TUint8 u8; 142 #define WPA_TYPES_DEFINED 143 #endif /* __SYMBIAN32__ */ 144 145 #ifndef WPA_TYPES_DEFINED 146 #ifdef CONFIG_USE_INTTYPES_H 147 #include <inttypes.h> 148 #else 149 #include <stdint.h> 150 #endif 151 typedef uint64_t u64; 152 typedef uint32_t u32; 153 typedef uint16_t u16; 154 typedef uint8_t u8; 155 typedef int64_t s64; 156 typedef int32_t s32; 157 typedef int16_t s16; 158 typedef int8_t s8; 159 #define WPA_TYPES_DEFINED 160 #endif /* !WPA_TYPES_DEFINED */ 161 162 163 /* Define platform specific byte swapping macros */ 164 165 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS) 166 167 static inline unsigned short wpa_swap_16(unsigned short v) 168 { 169 return ((v & 0xff) << 8) | (v >> 8); 170 } 171 172 static inline unsigned int wpa_swap_32(unsigned int v) 173 { 174 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | 175 ((v & 0xff0000) >> 8) | (v >> 24); 176 } 177 178 #define le_to_host16(n) (n) 179 #define host_to_le16(n) (n) 180 #define be_to_host16(n) wpa_swap_16(n) 181 #define host_to_be16(n) wpa_swap_16(n) 182 #define le_to_host32(n) (n) 183 #define be_to_host32(n) wpa_swap_32(n) 184 #define host_to_be32(n) wpa_swap_32(n) 185 186 #define WPA_BYTE_SWAP_DEFINED 187 188 #endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */ 189 190 191 #ifndef WPA_BYTE_SWAP_DEFINED 192 193 #ifndef __BYTE_ORDER 194 #ifndef __LITTLE_ENDIAN 195 #ifndef __BIG_ENDIAN 196 #define __LITTLE_ENDIAN 1234 197 #define __BIG_ENDIAN 4321 198 #if defined(sparc) 199 #define __BYTE_ORDER __BIG_ENDIAN 200 #endif 201 #endif /* __BIG_ENDIAN */ 202 #endif /* __LITTLE_ENDIAN */ 203 #endif /* __BYTE_ORDER */ 204 205 #if __BYTE_ORDER == __LITTLE_ENDIAN 206 #define le_to_host16(n) ((__force u16) (le16) (n)) 207 #define host_to_le16(n) ((__force le16) (u16) (n)) 208 #define be_to_host16(n) bswap_16((__force u16) (be16) (n)) 209 #define host_to_be16(n) ((__force be16) bswap_16((n))) 210 #define le_to_host32(n) ((__force u32) (le32) (n)) 211 #define host_to_le32(n) ((__force le32) (u32) (n)) 212 #define be_to_host32(n) bswap_32((__force u32) (be32) (n)) 213 #define host_to_be32(n) ((__force be32) bswap_32((n))) 214 #define le_to_host64(n) ((__force u64) (le64) (n)) 215 #define host_to_le64(n) ((__force le64) (u64) (n)) 216 #define be_to_host64(n) bswap_64((__force u64) (be64) (n)) 217 #define host_to_be64(n) ((__force be64) bswap_64((n))) 218 #elif __BYTE_ORDER == __BIG_ENDIAN 219 #define le_to_host16(n) bswap_16(n) 220 #define host_to_le16(n) bswap_16(n) 221 #define be_to_host16(n) (n) 222 #define host_to_be16(n) (n) 223 #define le_to_host32(n) bswap_32(n) 224 #define be_to_host32(n) (n) 225 #define host_to_be32(n) (n) 226 #define le_to_host64(n) bswap_64(n) 227 #define host_to_le64(n) bswap_64(n) 228 #define be_to_host64(n) (n) 229 #define host_to_be64(n) (n) 230 #ifndef WORDS_BIGENDIAN 231 #define WORDS_BIGENDIAN 232 #endif 233 #else 234 #error Could not determine CPU byte order 235 #endif 236 237 #define WPA_BYTE_SWAP_DEFINED 238 #endif /* !WPA_BYTE_SWAP_DEFINED */ 239 240 241 /* Macros for handling unaligned memory accesses */ 242 243 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1])) 244 #define WPA_PUT_BE16(a, val) \ 245 do { \ 246 (a)[0] = ((u16) (val)) >> 8; \ 247 (a)[1] = ((u16) (val)) & 0xff; \ 248 } while (0) 249 250 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0])) 251 #define WPA_PUT_LE16(a, val) \ 252 do { \ 253 (a)[1] = ((u16) (val)) >> 8; \ 254 (a)[0] = ((u16) (val)) & 0xff; \ 255 } while (0) 256 257 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \ 258 ((u32) (a)[2])) 259 #define WPA_PUT_BE24(a, val) \ 260 do { \ 261 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff); \ 262 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \ 263 (a)[2] = (u8) (((u32) (val)) & 0xff); \ 264 } while (0) 265 266 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \ 267 (((u32) (a)[2]) << 8) | ((u32) (a)[3])) 268 #define WPA_PUT_BE32(a, val) \ 269 do { \ 270 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff); \ 271 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff); \ 272 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff); \ 273 (a)[3] = (u8) (((u32) (val)) & 0xff); \ 274 } while (0) 275 276 #define WPA_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \ 277 (((u32) (a)[1]) << 8) | ((u32) (a)[0])) 278 #define WPA_PUT_LE32(a, val) \ 279 do { \ 280 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff); \ 281 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff); \ 282 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \ 283 (a)[0] = (u8) (((u32) (val)) & 0xff); \ 284 } while (0) 285 286 #define WPA_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \ 287 (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \ 288 (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \ 289 (((u64) (a)[6]) << 8) | ((u64) (a)[7])) 290 #define WPA_PUT_BE64(a, val) \ 291 do { \ 292 (a)[0] = (u8) (((u64) (val)) >> 56); \ 293 (a)[1] = (u8) (((u64) (val)) >> 48); \ 294 (a)[2] = (u8) (((u64) (val)) >> 40); \ 295 (a)[3] = (u8) (((u64) (val)) >> 32); \ 296 (a)[4] = (u8) (((u64) (val)) >> 24); \ 297 (a)[5] = (u8) (((u64) (val)) >> 16); \ 298 (a)[6] = (u8) (((u64) (val)) >> 8); \ 299 (a)[7] = (u8) (((u64) (val)) & 0xff); \ 300 } while (0) 301 302 #define WPA_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \ 303 (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \ 304 (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \ 305 (((u64) (a)[1]) << 8) | ((u64) (a)[0])) 306 307 308 #ifndef ETH_ALEN 309 #define ETH_ALEN 6 310 #endif 311 #ifndef IFNAMSIZ 312 #define IFNAMSIZ 16 313 #endif 314 #ifndef ETH_P_ALL 315 #define ETH_P_ALL 0x0003 316 #endif 317 #ifndef ETH_P_80211_ENCAP 318 #define ETH_P_80211_ENCAP 0x890d /* TDLS comes under this category */ 319 #endif 320 #ifndef ETH_P_PAE 321 #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */ 322 #endif /* ETH_P_PAE */ 323 #ifndef ETH_P_EAPOL 324 #define ETH_P_EAPOL ETH_P_PAE 325 #endif /* ETH_P_EAPOL */ 326 #ifndef ETH_P_RSN_PREAUTH 327 #define ETH_P_RSN_PREAUTH 0x88c7 328 #endif /* ETH_P_RSN_PREAUTH */ 329 #ifndef ETH_P_RRB 330 #define ETH_P_RRB 0x890D 331 #endif /* ETH_P_RRB */ 332 333 334 #ifdef __GNUC__ 335 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b)))) 336 #define STRUCT_PACKED __attribute__ ((packed)) 337 #else 338 #define PRINTF_FORMAT(a,b) 339 #define STRUCT_PACKED 340 #endif 341 342 343 #ifdef CONFIG_ANSI_C_EXTRA 344 345 #if !defined(_MSC_VER) || _MSC_VER < 1400 346 /* snprintf - used in number of places; sprintf() is _not_ a good replacement 347 * due to possible buffer overflow; see, e.g., 348 * http://www.ijs.si/software/snprintf/ for portable implementation of 349 * snprintf. */ 350 int snprintf(char *str, size_t size, const char *format, ...); 351 352 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */ 353 int vsnprintf(char *str, size_t size, const char *format, va_list ap); 354 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */ 355 356 /* getopt - only used in main.c */ 357 int getopt(int argc, char *const argv[], const char *optstring); 358 extern char *optarg; 359 extern int optind; 360 361 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF 362 #ifndef __socklen_t_defined 363 typedef int socklen_t; 364 #endif 365 #endif 366 367 /* inline - define as __inline or just define it to be empty, if needed */ 368 #ifdef CONFIG_NO_INLINE 369 #define inline 370 #else 371 #define inline __inline 372 #endif 373 374 #ifndef __func__ 375 #define __func__ "__func__ not defined" 376 #endif 377 378 #ifndef bswap_16 379 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff)) 380 #endif 381 382 #ifndef bswap_32 383 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \ 384 (((u32) (a) << 8) & 0xff0000) | \ 385 (((u32) (a) >> 8) & 0xff00) | \ 386 (((u32) (a) >> 24) & 0xff)) 387 #endif 388 389 #ifndef MSG_DONTWAIT 390 #define MSG_DONTWAIT 0 391 #endif 392 393 #ifdef _WIN32_WCE 394 void perror(const char *s); 395 #endif /* _WIN32_WCE */ 396 397 #endif /* CONFIG_ANSI_C_EXTRA */ 398 399 #ifndef MAC2STR 400 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] 401 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x" 402 403 /* 404 * Compact form for string representation of MAC address 405 * To be used, e.g., for constructing dbus paths for P2P Devices 406 */ 407 #define COMPACT_MACSTR "%02x%02x%02x%02x%02x%02x" 408 #endif 409 410 #ifndef BIT 411 #define BIT(x) (1 << (x)) 412 #endif 413 414 /* 415 * Definitions for sparse validation 416 * (http://kernel.org/pub/linux/kernel/people/josh/sparse/) 417 */ 418 #ifdef __CHECKER__ 419 #define __force __attribute__((force)) 420 #define __bitwise __attribute__((bitwise)) 421 #else 422 #define __force 423 #define __bitwise 424 #endif 425 426 typedef u16 __bitwise be16; 427 typedef u16 __bitwise le16; 428 typedef u32 __bitwise be32; 429 typedef u32 __bitwise le32; 430 typedef u64 __bitwise be64; 431 typedef u64 __bitwise le64; 432 433 #ifndef __must_check 434 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) 435 #define __must_check __attribute__((__warn_unused_result__)) 436 #else 437 #define __must_check 438 #endif /* __GNUC__ */ 439 #endif /* __must_check */ 440 441 int hwaddr_aton(const char *txt, u8 *addr); 442 int hwaddr_compact_aton(const char *txt, u8 *addr); 443 int hwaddr_aton2(const char *txt, u8 *addr); 444 int hex2byte(const char *hex); 445 int hexstr2bin(const char *hex, u8 *buf, size_t len); 446 void inc_byte_array(u8 *counter, size_t len); 447 void wpa_get_ntp_timestamp(u8 *buf); 448 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len); 449 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data, 450 size_t len); 451 452 #ifdef CONFIG_NATIVE_WINDOWS 453 void wpa_unicode2ascii_inplace(TCHAR *str); 454 TCHAR * wpa_strdup_tchar(const char *str); 455 #else /* CONFIG_NATIVE_WINDOWS */ 456 #define wpa_unicode2ascii_inplace(s) do { } while (0) 457 #define wpa_strdup_tchar(s) strdup((s)) 458 #endif /* CONFIG_NATIVE_WINDOWS */ 459 460 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len); 461 462 static inline int is_zero_ether_addr(const u8 *a) 463 { 464 return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]); 465 } 466 467 static inline int is_broadcast_ether_addr(const u8 *a) 468 { 469 return (a[0] & a[1] & a[2] & a[3] & a[4] & a[5]) == 0xff; 470 } 471 472 #define broadcast_ether_addr (const u8 *) "\xff\xff\xff\xff\xff\xff" 473 474 #include "wpa_debug.h" 475 476 477 /* 478 * gcc 4.4 ends up generating strict-aliasing warnings about some very common 479 * networking socket uses that do not really result in a real problem and 480 * cannot be easily avoided with union-based type-punning due to struct 481 * definitions including another struct in system header files. To avoid having 482 * to fully disable strict-aliasing warnings, provide a mechanism to hide the 483 * typecast from aliasing for now. A cleaner solution will hopefully be found 484 * in the future to handle these cases. 485 */ 486 void * __hide_aliasing_typecast(void *foo); 487 #define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a)) 488 489 #ifdef CONFIG_VALGRIND 490 #include <valgrind/memcheck.h> 491 #define WPA_MEM_DEFINED(ptr, len) VALGRIND_MAKE_MEM_DEFINED((ptr), (len)) 492 #else /* CONFIG_VALGRIND */ 493 #define WPA_MEM_DEFINED(ptr, len) do { } while (0) 494 #endif /* CONFIG_VALGRIND */ 495 496 #endif /* COMMON_H */ 497