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 #if defined(__ets__)
13 #endif /* ets */
14 #include "os.h"
15 #include "esp_bit_defs.h"
16 #include "list.h"
17
18 /* Define platform specific variable type macros */
19 #if defined(ESP_PLATFORM)
20 #include <stdint.h>
21 typedef uint64_t u64;
22 typedef uint32_t u32;
23 typedef uint16_t u16;
24 typedef uint8_t u8;
25 typedef int64_t s64;
26 typedef int32_t s32;
27 typedef int16_t s16;
28 typedef int8_t s8;
29 #endif /*ESP_PLATFORM*/
30
31 #if defined(__linux__) || defined(__GLIBC__)
32 #include <endian.h>
33 #include <byteswap.h>
34 #else
35 #include <machine/endian.h>
36 #define __BYTE_ORDER BYTE_ORDER
37 #define __LITTLE_ENDIAN LITTLE_ENDIAN
38 #define __BIG_ENDIAN BIG_ENDIAN
39 #endif /* __linux__ */
40
41 /* Define platform specific byte swapping macros */
42
43 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
44
wpa_swap_16(unsigned short v)45 static inline unsigned short wpa_swap_16(unsigned short v)
46 {
47 return ((v & 0xff) << 8) | (v >> 8);
48 }
49
wpa_swap_32(unsigned int v)50 static inline unsigned int wpa_swap_32(unsigned int v)
51 {
52 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
53 ((v & 0xff0000) >> 8) | (v >> 24);
54 }
55
56 #define le_to_host16(n) (n)
57 #define host_to_le16(n) (n)
58 #define be_to_host16(n) wpa_swap_16(n)
59 #define host_to_be16(n) wpa_swap_16(n)
60 #define le_to_host32(n) (n)
61 #define host_to_le32(n) (n)
62 #define be_to_host32(n) wpa_swap_32(n)
63 #define host_to_be32(n) wpa_swap_32(n)
64
65 #define WPA_BYTE_SWAP_DEFINED
66
67 #endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
68
69
70 #ifndef WPA_BYTE_SWAP_DEFINED
71
72 #ifndef __BYTE_ORDER
73 #ifndef __LITTLE_ENDIAN
74 #ifndef __BIG_ENDIAN
75 #define __LITTLE_ENDIAN 1234
76 #define __BIG_ENDIAN 4321
77 #if defined(sparc)
78 #define __BYTE_ORDER __BIG_ENDIAN
79 #endif
80 #endif /* __BIG_ENDIAN */
81 #endif /* __LITTLE_ENDIAN */
82 #endif /* __BYTE_ORDER */
83
84 #if __BYTE_ORDER == __LITTLE_ENDIAN
85 #define le_to_host16(n) ((__force u16) (le16) (n))
86 #define host_to_le16(n) ((__force le16) (u16) (n))
87 #define be_to_host16(n) __builtin_bswap16((__force u16) (be16) (n))
88 #define host_to_be16(n) ((__force be16) __builtin_bswap16((n)))
89 #define le_to_host32(n) ((__force u32) (le32) (n))
90 #define host_to_le32(n) ((__force le32) (u32) (n))
91 #define be_to_host32(n) __builtin_bswap32((__force u32) (be32) (n))
92 #define host_to_be32(n) ((__force be32) __builtin_bswap32((n)))
93 #define le_to_host64(n) ((__force u64) (le64) (n))
94 #define host_to_le64(n) ((__force le64) (u64) (n))
95 #define be_to_host64(n) __builtin_bswap64((__force u64) (be64) (n))
96 #define host_to_be64(n) ((__force be64) __builtin_bswap64((n)))
97 #elif __BYTE_ORDER == __BIG_ENDIAN
98 #define le_to_host16(n) __bswap_16(n)
99 #define host_to_le16(n) __bswap_16(n)
100 #define be_to_host16(n) (n)
101 #define host_to_be16(n) (n)
102 #define le_to_host32(n) __bswap_32(n)
103 #define be_to_host32(n) (n)
104 #define host_to_be32(n) (n)
105 #define le_to_host64(n) __bswap_64(n)
106 #define host_to_le64(n) __bswap_64(n)
107 #define be_to_host64(n) (n)
108 #define host_to_be64(n) (n)
109 #ifndef WORDS_BIGENDIAN
110 #define WORDS_BIGENDIAN
111 #endif
112 #else
113 #error Could not determine CPU byte order
114 #endif
115
116 #define WPA_BYTE_SWAP_DEFINED
117 #endif /* !WPA_BYTE_SWAP_DEFINED */
118
119 #define SSID_MAX_LEN 32
120
121 struct wpa_ssid_value {
122 u8 ssid[SSID_MAX_LEN];
123 size_t ssid_len;
124 };
125
126 /* Macros for handling unaligned memory accesses */
127
WPA_GET_BE16(const u8 * a)128 static inline u16 WPA_GET_BE16(const u8 *a)
129 {
130 return (a[0] << 8) | a[1];
131 }
132
WPA_PUT_BE16(u8 * a,u16 val)133 static inline void WPA_PUT_BE16(u8 *a, u16 val)
134 {
135 a[0] = val >> 8;
136 a[1] = val & 0xff;
137 }
138
WPA_GET_LE16(const u8 * a)139 static inline u16 WPA_GET_LE16(const u8 *a)
140 {
141 return (a[1] << 8) | a[0];
142 }
143
WPA_PUT_LE16(u8 * a,u16 val)144 static inline void WPA_PUT_LE16(u8 *a, u16 val)
145 {
146 a[1] = val >> 8;
147 a[0] = val & 0xff;
148 }
149
WPA_GET_BE24(const u8 * a)150 static inline u32 WPA_GET_BE24(const u8 *a)
151 {
152 return (a[0] << 16) | (a[1] << 8) | a[2];
153 }
154
WPA_PUT_BE24(u8 * a,u32 val)155 static inline void WPA_PUT_BE24(u8 *a, u32 val)
156 {
157 a[0] = (val >> 16) & 0xff;
158 a[1] = (val >> 8) & 0xff;
159 a[2] = val & 0xff;
160 }
161
WPA_GET_BE32(const u8 * a)162 static inline u32 WPA_GET_BE32(const u8 *a)
163 {
164 return ((u32) a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3];
165 }
166
WPA_PUT_BE32(u8 * a,u32 val)167 static inline void WPA_PUT_BE32(u8 *a, u32 val)
168 {
169 a[0] = (val >> 24) & 0xff;
170 a[1] = (val >> 16) & 0xff;
171 a[2] = (val >> 8) & 0xff;
172 a[3] = val & 0xff;
173 }
174
WPA_GET_LE32(const u8 * a)175 static inline u32 WPA_GET_LE32(const u8 *a)
176 {
177 return ((u32) a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0];
178 }
179
WPA_PUT_LE32(u8 * a,u32 val)180 static inline void WPA_PUT_LE32(u8 *a, u32 val)
181 {
182 a[3] = (val >> 24) & 0xff;
183 a[2] = (val >> 16) & 0xff;
184 a[1] = (val >> 8) & 0xff;
185 a[0] = val & 0xff;
186 }
187
WPA_GET_BE64(const u8 * a)188 static inline u64 WPA_GET_BE64(const u8 *a)
189 {
190 return (((u64) a[0]) << 56) | (((u64) a[1]) << 48) |
191 (((u64) a[2]) << 40) | (((u64) a[3]) << 32) |
192 (((u64) a[4]) << 24) | (((u64) a[5]) << 16) |
193 (((u64) a[6]) << 8) | ((u64) a[7]);
194 }
195
WPA_PUT_BE64(u8 * a,u64 val)196 static inline void WPA_PUT_BE64(u8 *a, u64 val)
197 {
198 a[0] = val >> 56;
199 a[1] = val >> 48;
200 a[2] = val >> 40;
201 a[3] = val >> 32;
202 a[4] = val >> 24;
203 a[5] = val >> 16;
204 a[6] = val >> 8;
205 a[7] = val & 0xff;
206 }
207
WPA_GET_LE64(const u8 * a)208 static inline u64 WPA_GET_LE64(const u8 *a)
209 {
210 return (((u64) a[7]) << 56) | (((u64) a[6]) << 48) |
211 (((u64) a[5]) << 40) | (((u64) a[4]) << 32) |
212 (((u64) a[3]) << 24) | (((u64) a[2]) << 16) |
213 (((u64) a[1]) << 8) | ((u64) a[0]);
214 }
215
WPA_PUT_LE64(u8 * a,u64 val)216 static inline void WPA_PUT_LE64(u8 *a, u64 val)
217 {
218 a[7] = val >> 56;
219 a[6] = val >> 48;
220 a[5] = val >> 40;
221 a[4] = val >> 32;
222 a[3] = val >> 24;
223 a[2] = val >> 16;
224 a[1] = val >> 8;
225 a[0] = val & 0xff;
226 }
227
228
229 #ifndef ETH_ALEN
230 #define ETH_ALEN 6
231 #endif
232 #ifndef ETH_HLEN
233 #define ETH_HLEN 14
234 #endif
235 #ifndef IFNAMSIZ
236 #define IFNAMSIZ 16
237 #endif
238 #ifndef ETH_P_ALL
239 #define ETH_P_ALL 0x0003
240 #endif
241 #ifndef ETH_P_80211_ENCAP
242 #define ETH_P_80211_ENCAP 0x890d /* TDLS comes under this category */
243 #endif
244 #ifndef ETH_P_PAE
245 #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
246 #endif /* ETH_P_PAE */
247 #ifndef ETH_P_EAPOL
248 #define ETH_P_EAPOL ETH_P_PAE
249 #endif /* ETH_P_EAPOL */
250 #ifndef ETH_P_RSN_PREAUTH
251 #define ETH_P_RSN_PREAUTH 0x88c7
252 #endif /* ETH_P_RSN_PREAUTH */
253 #ifndef ETH_P_RRB
254 #define ETH_P_RRB 0x890D
255 #endif /* ETH_P_RRB */
256
257
258 #ifdef __GNUC__
259 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
260 #define STRUCT_PACKED __attribute__ ((packed))
261 #else
262 #define PRINTF_FORMAT(a,b)
263 #define STRUCT_PACKED
264 #endif
265
266
267 #ifdef CONFIG_ANSI_C_EXTRA
268
269 #if !defined(_MSC_VER) || _MSC_VER < 1400
270 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
271 * due to possible buffer overflow; see, e.g.,
272 * http://www.ijs.si/software/snprintf/ for portable implementation of
273 * snprintf. */
274 int snprintf(char *str, size_t size, const char *format, ...);
275
276 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
277 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
278 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
279
280 /* getopt - only used in main.c */
281 int getopt(int argc, char *const argv[], const char *optstring);
282 extern char *optarg;
283 extern int optind;
284
285 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
286 #ifndef __socklen_t_defined
287 typedef int socklen_t;
288 #endif
289 #endif
290
291 /* inline - define as __inline or just define it to be empty, if needed */
292 #ifdef CONFIG_NO_INLINE
293 #define inline
294 #else
295 #define inline __inline
296 #endif
297
298 #ifndef __func__
299 #define __func__ "__func__ not defined"
300 #endif
301
302 #ifndef bswap_16
303 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
304 #endif
305
306 #ifndef bswap_32
307 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
308 (((u32) (a) << 8) & 0xff0000) | \
309 (((u32) (a) >> 8) & 0xff00) | \
310 (((u32) (a) >> 24) & 0xff))
311 #endif
312
313 #ifndef MSG_DONTWAIT
314 #define MSG_DONTWAIT 0
315 #endif
316
317 #ifdef _WIN32_WCE
318 void perror(const char *s);
319 #endif /* _WIN32_WCE */
320
321 #endif /* CONFIG_ANSI_C_EXTRA */
322
323 #ifndef MAC2STR
324 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
325 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
326
327 /*
328 * Compact form for string representation of MAC address
329 * To be used, e.g., for constructing dbus paths for P2P Devices
330 */
331 #define COMPACT_MACSTR "%02x%02x%02x%02x%02x%02x"
332 #endif
333
334 #ifndef BIT
335 #define BIT(x) (1U << (x))
336 #endif
337
338 /*
339 * Definitions for sparse validation
340 * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
341 */
342 #ifdef __CHECKER__
343 #define __force __attribute__((force))
344 #define __bitwise __attribute__((bitwise))
345 #else
346 #define __force
347 #define __bitwise
348 #endif
349
350 typedef u16 __bitwise be16;
351 typedef u16 __bitwise le16;
352 typedef u32 __bitwise be32;
353 typedef u32 __bitwise le32;
354 typedef u64 __bitwise be64;
355 typedef u64 __bitwise le64;
356
357 #ifndef __must_check
358 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
359 #define __must_check __attribute__((__warn_unused_result__))
360 #else
361 #define __must_check
362 #endif /* __GNUC__ */
363 #endif /* __must_check */
364
365 #ifndef __maybe_unused
366 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
367 #define __maybe_unused __attribute__((unused))
368 #else
369 #define __maybe_unused
370 #endif /* __GNUC__ */
371 #endif /* __must_check */
372
373 int hwaddr_aton(const char *txt, u8 *addr);
374 int hwaddr_masked_aton(const char *txt, u8 *addr, u8 *mask, u8 maskable);
375 int hwaddr_compact_aton(const char *txt, u8 *addr);
376 int hwaddr_aton2(const char *txt, u8 *addr);
377 int hex2byte(const char *hex);
378 int hexstr2bin(const char *hex, u8 *buf, size_t len);
379 void inc_byte_array(u8 *counter, size_t len);
380 void wpa_get_ntp_timestamp(u8 *buf);
381 int wpa_scnprintf(char *buf, size_t size, const char *fmt, ...);
382 int wpa_snprintf_hex_sep(char *buf, size_t buf_size, const u8 *data, size_t len,
383 char sep);
384 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
385 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
386 size_t len);
387
388 int hwaddr_mask_txt(char *buf, size_t len, const u8 *addr, const u8 *mask);
389 u8 rssi_to_rcpi(int rssi);
390 int os_time_expired(struct os_time *now,
391 struct os_time *ts,
392 os_time_t timeout_secs);
393
394 #ifdef CONFIG_NATIVE_WINDOWS
395 void wpa_unicode2ascii_inplace(TCHAR *str);
396 TCHAR * wpa_strdup_tchar(const char *str);
397 #else /* CONFIG_NATIVE_WINDOWS */
398 #define wpa_unicode2ascii_inplace(s) do { } while (0)
399 #define wpa_strdup_tchar(s) strdup((s))
400 #endif /* CONFIG_NATIVE_WINDOWS */
401
402 void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len);
403 size_t printf_decode(u8 *buf, size_t maxlen, const char *str);
404
405 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len);
406
407 char * wpa_config_parse_string(const char *value, size_t *len);
408 int wpa_is_hex(const u8 *data, size_t len);
409 size_t wpa_merge_byte_arrays(u8 *res, size_t res_len,
410 const u8 *src1, size_t src1_len,
411 const u8 *src2, size_t src2_len);
412 char * dup_binstr(const void *src, size_t len);
413
is_zero_ether_addr(const u8 * a)414 static inline int is_zero_ether_addr(const u8 *a)
415 {
416 return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]);
417 }
418
is_broadcast_ether_addr(const u8 * a)419 static inline int is_broadcast_ether_addr(const u8 *a)
420 {
421 return (a[0] & a[1] & a[2] & a[3] & a[4] & a[5]) == 0xff;
422 }
423
is_multicast_ether_addr(const u8 * a)424 static inline int is_multicast_ether_addr(const u8 *a)
425 {
426 return a[0] & 0x01;
427 }
428
429 #define broadcast_ether_addr (const u8 *) "\xff\xff\xff\xff\xff\xff"
430
431
432 #include "utils/wpa_debug.h"
433
434
435 struct wpa_freq_range_list {
436 struct wpa_freq_range {
437 unsigned int min;
438 unsigned int max;
439 } *range;
440 unsigned int num;
441 };
442
443 #ifndef ARRAY_SIZE
444 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
445 #endif
446 #ifndef TEST_FAIL
447 #define TEST_FAIL() 0
448 #endif
449
450 void wpa_bin_clear_free(void *bin, size_t len);
451 int int_array_len(const int *a);
452 void bin_clear_free(void *bin, size_t len);
453 void str_clear_free(char *str);
454 char * get_param(const char *cmd, const char *param);
455 void * os_memdup(const void *src, size_t len);
456
457 /*
458 * gcc 4.4 ends up generating strict-aliasing warnings about some very common
459 * networking socket uses that do not really result in a real problem and
460 * cannot be easily avoided with union-based type-punning due to struct
461 * definitions including another struct in system header files. To avoid having
462 * to fully disable strict-aliasing warnings, provide a mechanism to hide the
463 * typecast from aliasing for now. A cleaner solution will hopefully be found
464 * in the future to handle these cases.
465 */
466 void * __hide_aliasing_typecast(void *foo);
467 #define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
468
469 #ifdef CONFIG_VALGRIND
470 #include <valgrind/memcheck.h>
471 #define WPA_MEM_DEFINED(ptr, len) VALGRIND_MAKE_MEM_DEFINED((ptr), (len))
472 #else /* CONFIG_VALGRIND */
473 #define WPA_MEM_DEFINED(ptr, len) do { } while (0)
474 #endif /* CONFIG_VALGRIND */
475
476 #define IANA_SECP256R1 19
477
478 #endif /* COMMON_H */
479