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