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