blob: bede21e5717715fd4389bbbd3236fd1215abc0a3 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant/hostapd / common helper functions, etc.
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
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
44static inline unsigned short bswap_16(unsigned short v)
45{
46 return ((v & 0xff) << 8) | (v >> 8);
47}
48
49static 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
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070056#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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070065#ifdef CONFIG_NATIVE_WINDOWS
66#include <winsock.h>
67
68typedef 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
89typedef UINT64 u64;
90typedef UINT32 u32;
91typedef UINT16 u16;
92typedef UINT8 u8;
93typedef INT64 s64;
94typedef INT32 s32;
95typedef INT16 s16;
96typedef INT8 s8;
97#define WPA_TYPES_DEFINED
98#endif /* _MSC_VER */
99
100#ifdef __vxworks
101typedef unsigned long long u64;
102typedef UINT32 u32;
103typedef UINT16 u16;
104typedef UINT8 u8;
105typedef long long s64;
106typedef INT32 s32;
107typedef INT16 s16;
108typedef INT8 s8;
109#define WPA_TYPES_DEFINED
110#endif /* __vxworks */
111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112#ifndef WPA_TYPES_DEFINED
113#ifdef CONFIG_USE_INTTYPES_H
114#include <inttypes.h>
115#else
116#include <stdint.h>
117#endif
118typedef uint64_t u64;
119typedef uint32_t u32;
120typedef uint16_t u16;
121typedef uint8_t u8;
122typedef int64_t s64;
123typedef int32_t s32;
124typedef int16_t s16;
125typedef 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
134static inline unsigned short wpa_swap_16(unsigned short v)
135{
136 return ((v & 0xff) << 8) | (v >> 8);
137}
138
139static 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)
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800150#define host_to_le32(n) (n)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700151#define be_to_host32(n) wpa_swap_32(n)
152#define host_to_be32(n) wpa_swap_32(n)
Roshan Pius3a1667e2018-07-03 15:17:14 -0700153#define host_to_le64(n) (n)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154
155#define WPA_BYTE_SWAP_DEFINED
156
157#endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
158
159
160#ifndef WPA_BYTE_SWAP_DEFINED
161
162#ifndef __BYTE_ORDER
163#ifndef __LITTLE_ENDIAN
164#ifndef __BIG_ENDIAN
165#define __LITTLE_ENDIAN 1234
166#define __BIG_ENDIAN 4321
167#if defined(sparc)
168#define __BYTE_ORDER __BIG_ENDIAN
169#endif
170#endif /* __BIG_ENDIAN */
171#endif /* __LITTLE_ENDIAN */
172#endif /* __BYTE_ORDER */
173
174#if __BYTE_ORDER == __LITTLE_ENDIAN
175#define le_to_host16(n) ((__force u16) (le16) (n))
176#define host_to_le16(n) ((__force le16) (u16) (n))
177#define be_to_host16(n) bswap_16((__force u16) (be16) (n))
178#define host_to_be16(n) ((__force be16) bswap_16((n)))
179#define le_to_host32(n) ((__force u32) (le32) (n))
180#define host_to_le32(n) ((__force le32) (u32) (n))
181#define be_to_host32(n) bswap_32((__force u32) (be32) (n))
182#define host_to_be32(n) ((__force be32) bswap_32((n)))
183#define le_to_host64(n) ((__force u64) (le64) (n))
184#define host_to_le64(n) ((__force le64) (u64) (n))
185#define be_to_host64(n) bswap_64((__force u64) (be64) (n))
186#define host_to_be64(n) ((__force be64) bswap_64((n)))
187#elif __BYTE_ORDER == __BIG_ENDIAN
188#define le_to_host16(n) bswap_16(n)
189#define host_to_le16(n) bswap_16(n)
190#define be_to_host16(n) (n)
191#define host_to_be16(n) (n)
192#define le_to_host32(n) bswap_32(n)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700193#define host_to_le32(n) bswap_32(n)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194#define be_to_host32(n) (n)
195#define host_to_be32(n) (n)
196#define le_to_host64(n) bswap_64(n)
197#define host_to_le64(n) bswap_64(n)
198#define be_to_host64(n) (n)
199#define host_to_be64(n) (n)
200#ifndef WORDS_BIGENDIAN
201#define WORDS_BIGENDIAN
202#endif
203#else
204#error Could not determine CPU byte order
205#endif
206
207#define WPA_BYTE_SWAP_DEFINED
208#endif /* !WPA_BYTE_SWAP_DEFINED */
209
210
211/* Macros for handling unaligned memory accesses */
212
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800213static inline u16 WPA_GET_BE16(const u8 *a)
214{
215 return (a[0] << 8) | a[1];
216}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800218static inline void WPA_PUT_BE16(u8 *a, u16 val)
219{
220 a[0] = val >> 8;
221 a[1] = val & 0xff;
222}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800224static inline u16 WPA_GET_LE16(const u8 *a)
225{
226 return (a[1] << 8) | a[0];
227}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800229static inline void WPA_PUT_LE16(u8 *a, u16 val)
230{
231 a[1] = val >> 8;
232 a[0] = val & 0xff;
233}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800235static inline u32 WPA_GET_BE24(const u8 *a)
236{
237 return (a[0] << 16) | (a[1] << 8) | a[2];
238}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800240static inline void WPA_PUT_BE24(u8 *a, u32 val)
241{
242 a[0] = (val >> 16) & 0xff;
243 a[1] = (val >> 8) & 0xff;
244 a[2] = val & 0xff;
245}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700246
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000247static inline u32 WPA_GET_LE24(const u8 *a)
248{
249 return (a[2] << 16) | (a[1] << 8) | a[0];
250}
251
252static inline void WPA_PUT_LE24(u8 *a, u32 val)
253{
254 a[2] = (val >> 16) & 0xff;
255 a[1] = (val >> 8) & 0xff;
256 a[0] = val & 0xff;
257}
258
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800259static inline u32 WPA_GET_BE32(const u8 *a)
260{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800261 return ((u32) a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800262}
263
264static inline void WPA_PUT_BE32(u8 *a, u32 val)
265{
266 a[0] = (val >> 24) & 0xff;
267 a[1] = (val >> 16) & 0xff;
268 a[2] = (val >> 8) & 0xff;
269 a[3] = val & 0xff;
270}
271
272static inline u32 WPA_GET_LE32(const u8 *a)
273{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800274 return ((u32) a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800275}
276
277static inline void WPA_PUT_LE32(u8 *a, u32 val)
278{
279 a[3] = (val >> 24) & 0xff;
280 a[2] = (val >> 16) & 0xff;
281 a[1] = (val >> 8) & 0xff;
282 a[0] = val & 0xff;
283}
284
285static inline u64 WPA_GET_BE64(const u8 *a)
286{
287 return (((u64) a[0]) << 56) | (((u64) a[1]) << 48) |
288 (((u64) a[2]) << 40) | (((u64) a[3]) << 32) |
289 (((u64) a[4]) << 24) | (((u64) a[5]) << 16) |
290 (((u64) a[6]) << 8) | ((u64) a[7]);
291}
292
293static inline void WPA_PUT_BE64(u8 *a, u64 val)
294{
295 a[0] = val >> 56;
296 a[1] = val >> 48;
297 a[2] = val >> 40;
298 a[3] = val >> 32;
299 a[4] = val >> 24;
300 a[5] = val >> 16;
301 a[6] = val >> 8;
302 a[7] = val & 0xff;
303}
304
305static inline u64 WPA_GET_LE64(const u8 *a)
306{
307 return (((u64) a[7]) << 56) | (((u64) a[6]) << 48) |
308 (((u64) a[5]) << 40) | (((u64) a[4]) << 32) |
309 (((u64) a[3]) << 24) | (((u64) a[2]) << 16) |
310 (((u64) a[1]) << 8) | ((u64) a[0]);
311}
312
313static inline void WPA_PUT_LE64(u8 *a, u64 val)
314{
315 a[7] = val >> 56;
316 a[6] = val >> 48;
317 a[5] = val >> 40;
318 a[4] = val >> 32;
319 a[3] = val >> 24;
320 a[2] = val >> 16;
321 a[1] = val >> 8;
322 a[0] = val & 0xff;
323}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324
325
326#ifndef ETH_ALEN
327#define ETH_ALEN 6
328#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800329#ifndef ETH_HLEN
330#define ETH_HLEN 14
331#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332#ifndef IFNAMSIZ
333#define IFNAMSIZ 16
334#endif
335#ifndef ETH_P_ALL
336#define ETH_P_ALL 0x0003
337#endif
Dmitry Shmidte4663042016-04-04 10:07:49 -0700338#ifndef ETH_P_IP
339#define ETH_P_IP 0x0800
340#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341#ifndef ETH_P_80211_ENCAP
342#define ETH_P_80211_ENCAP 0x890d /* TDLS comes under this category */
343#endif
344#ifndef ETH_P_PAE
345#define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
346#endif /* ETH_P_PAE */
347#ifndef ETH_P_EAPOL
348#define ETH_P_EAPOL ETH_P_PAE
349#endif /* ETH_P_EAPOL */
350#ifndef ETH_P_RSN_PREAUTH
351#define ETH_P_RSN_PREAUTH 0x88c7
352#endif /* ETH_P_RSN_PREAUTH */
353#ifndef ETH_P_RRB
354#define ETH_P_RRB 0x890D
355#endif /* ETH_P_RRB */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700356#ifndef ETH_P_OUI
357#define ETH_P_OUI 0x88B7
358#endif /* ETH_P_OUI */
Hai Shalomc3565922019-10-28 11:58:20 -0700359#ifndef ETH_P_8021Q
360#define ETH_P_8021Q 0x8100
361#endif /* ETH_P_8021Q */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362
363
364#ifdef __GNUC__
365#define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
366#define STRUCT_PACKED __attribute__ ((packed))
367#else
368#define PRINTF_FORMAT(a,b)
369#define STRUCT_PACKED
370#endif
371
372
373#ifdef CONFIG_ANSI_C_EXTRA
374
375#if !defined(_MSC_VER) || _MSC_VER < 1400
376/* snprintf - used in number of places; sprintf() is _not_ a good replacement
377 * due to possible buffer overflow; see, e.g.,
378 * http://www.ijs.si/software/snprintf/ for portable implementation of
379 * snprintf. */
380int snprintf(char *str, size_t size, const char *format, ...);
381
382/* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
383int vsnprintf(char *str, size_t size, const char *format, va_list ap);
384#endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
385
386/* getopt - only used in main.c */
387int getopt(int argc, char *const argv[], const char *optstring);
388extern char *optarg;
389extern int optind;
390
391#ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
392#ifndef __socklen_t_defined
393typedef int socklen_t;
394#endif
395#endif
396
397/* inline - define as __inline or just define it to be empty, if needed */
398#ifdef CONFIG_NO_INLINE
399#define inline
400#else
401#define inline __inline
402#endif
403
404#ifndef __func__
405#define __func__ "__func__ not defined"
406#endif
407
408#ifndef bswap_16
409#define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
410#endif
411
412#ifndef bswap_32
413#define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
414 (((u32) (a) << 8) & 0xff0000) | \
415 (((u32) (a) >> 8) & 0xff00) | \
416 (((u32) (a) >> 24) & 0xff))
417#endif
418
419#ifndef MSG_DONTWAIT
420#define MSG_DONTWAIT 0
421#endif
422
423#ifdef _WIN32_WCE
424void perror(const char *s);
425#endif /* _WIN32_WCE */
426
427#endif /* CONFIG_ANSI_C_EXTRA */
428
429#ifndef MAC2STR
430#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
431#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
432
433/*
434 * Compact form for string representation of MAC address
435 * To be used, e.g., for constructing dbus paths for P2P Devices
436 */
437#define COMPACT_MACSTR "%02x%02x%02x%02x%02x%02x"
438#endif
439
440#ifndef BIT
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800441#define BIT(x) (1U << (x))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442#endif
443
444/*
445 * Definitions for sparse validation
446 * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
447 */
448#ifdef __CHECKER__
449#define __force __attribute__((force))
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700450#undef __bitwise
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700451#define __bitwise __attribute__((bitwise))
452#else
453#define __force
Roshan Pius3a1667e2018-07-03 15:17:14 -0700454#undef __bitwise
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455#define __bitwise
456#endif
457
458typedef u16 __bitwise be16;
459typedef u16 __bitwise le16;
460typedef u32 __bitwise be32;
461typedef u32 __bitwise le32;
462typedef u64 __bitwise be64;
463typedef u64 __bitwise le64;
464
465#ifndef __must_check
466#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
467#define __must_check __attribute__((__warn_unused_result__))
468#else
469#define __must_check
470#endif /* __GNUC__ */
471#endif /* __must_check */
472
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800473#ifndef __maybe_unused
474#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
475#define __maybe_unused __attribute__((unused))
476#else
477#define __maybe_unused
478#endif /* __GNUC__ */
479#endif /* __must_check */
480
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700481#define SSID_MAX_LEN 32
482
483struct wpa_ssid_value {
484 u8 ssid[SSID_MAX_LEN];
485 size_t ssid_len;
486};
487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700488int hwaddr_aton(const char *txt, u8 *addr);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800489int hwaddr_masked_aton(const char *txt, u8 *addr, u8 *mask, u8 maskable);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490int hwaddr_compact_aton(const char *txt, u8 *addr);
491int hwaddr_aton2(const char *txt, u8 *addr);
Sunil Ravia04bd252022-05-02 22:54:18 -0700492int hex2num(char c);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493int hex2byte(const char *hex);
494int hexstr2bin(const char *hex, u8 *buf, size_t len);
495void inc_byte_array(u8 *counter, size_t len);
Hai Shalom81f62d82019-07-22 12:10:00 -0700496void buf_shift_right(u8 *buf, size_t len, size_t bits);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497void wpa_get_ntp_timestamp(u8 *buf);
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700498int wpa_scnprintf(char *buf, size_t size, const char *fmt, ...)
499 PRINTF_FORMAT(3, 4);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -0700500int wpa_snprintf_hex_sep(char *buf, size_t buf_size, const u8 *data, size_t len,
501 char sep);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
503int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
504 size_t len);
505
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800506int hwaddr_mask_txt(char *buf, size_t len, const u8 *addr, const u8 *mask);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700507int ssid_parse(const char *buf, struct wpa_ssid_value *ssid);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509#ifdef CONFIG_NATIVE_WINDOWS
510void wpa_unicode2ascii_inplace(TCHAR *str);
511TCHAR * wpa_strdup_tchar(const char *str);
512#else /* CONFIG_NATIVE_WINDOWS */
513#define wpa_unicode2ascii_inplace(s) do { } while (0)
514#define wpa_strdup_tchar(s) strdup((s))
515#endif /* CONFIG_NATIVE_WINDOWS */
516
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700517void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len);
518size_t printf_decode(u8 *buf, size_t maxlen, const char *str);
519
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700520const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len);
521
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700522char * wpa_config_parse_string(const char *value, size_t *len);
523int is_hex(const u8 *data, size_t len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700524int has_ctrl_char(const u8 *data, size_t len);
525int has_newline(const char *str);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700526size_t merge_byte_arrays(u8 *res, size_t res_len,
527 const u8 *src1, size_t src1_len,
528 const u8 *src2, size_t src2_len);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700529char * dup_binstr(const void *src, size_t len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531static inline int is_zero_ether_addr(const u8 *a)
532{
533 return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]);
534}
535
536static inline int is_broadcast_ether_addr(const u8 *a)
537{
538 return (a[0] & a[1] & a[2] & a[3] & a[4] & a[5]) == 0xff;
539}
540
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800541static inline int is_multicast_ether_addr(const u8 *a)
542{
543 return a[0] & 0x01;
544}
545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546#define broadcast_ether_addr (const u8 *) "\xff\xff\xff\xff\xff\xff"
547
548#include "wpa_debug.h"
549
550
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700551struct wpa_freq_range_list {
552 struct wpa_freq_range {
553 unsigned int min;
554 unsigned int max;
555 } *range;
556 unsigned int num;
557};
558
559int freq_range_list_parse(struct wpa_freq_range_list *res, const char *value);
560int freq_range_list_includes(const struct wpa_freq_range_list *list,
561 unsigned int freq);
562char * freq_range_list_str(const struct wpa_freq_range_list *list);
563
Hai Shalomfdcde762020-04-02 11:19:20 -0700564size_t int_array_len(const int *a);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800565void int_array_concat(int **res, const int *a);
566void int_array_sort_unique(int *a);
567void int_array_add_unique(int **res, int a);
568
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700569#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
570
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700571void str_clear_free(char *str);
572void bin_clear_free(void *bin, size_t len);
573
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700574int random_mac_addr(u8 *addr);
575int random_mac_addr_keep_oui(u8 *addr);
576
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800577const char * cstr_token(const char *str, const char *delim, const char **last);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800578char * str_token(char *str, const char *delim, char **context);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800579size_t utf8_escape(const char *inp, size_t in_size,
580 char *outp, size_t out_size);
581size_t utf8_unescape(const char *inp, size_t in_size,
582 char *outp, size_t out_size);
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700583int is_ctrl_char(char c);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800584
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700585int str_starts(const char *str, const char *start);
586
Dmitry Shmidt29333592017-01-09 12:27:11 -0800587u8 rssi_to_rcpi(int rssi);
Hai Shalom021b0b52019-04-10 11:17:58 -0700588char * get_param(const char *cmd, const char *param);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700589
Hai Shalom81f62d82019-07-22 12:10:00 -0700590void forced_memzero(void *ptr, size_t len);
591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592/*
593 * gcc 4.4 ends up generating strict-aliasing warnings about some very common
594 * networking socket uses that do not really result in a real problem and
595 * cannot be easily avoided with union-based type-punning due to struct
596 * definitions including another struct in system header files. To avoid having
597 * to fully disable strict-aliasing warnings, provide a mechanism to hide the
598 * typecast from aliasing for now. A cleaner solution will hopefully be found
599 * in the future to handle these cases.
600 */
601void * __hide_aliasing_typecast(void *foo);
602#define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
603
604#ifdef CONFIG_VALGRIND
605#include <valgrind/memcheck.h>
606#define WPA_MEM_DEFINED(ptr, len) VALGRIND_MAKE_MEM_DEFINED((ptr), (len))
607#else /* CONFIG_VALGRIND */
608#define WPA_MEM_DEFINED(ptr, len) do { } while (0)
609#endif /* CONFIG_VALGRIND */
610
611#endif /* COMMON_H */