Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * wpa_supplicant/hostapd / common helper functions, etc. |
| 3 | * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi> |
| 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #ifndef COMMON_H |
| 10 | #define COMMON_H |
| 11 | |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 12 | #include "includes.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 13 | #include "os.h" |
| 14 | |
| 15 | #if defined(__linux__) || defined(__GLIBC__) |
| 16 | #include <endian.h> |
| 17 | #include <byteswap.h> |
| 18 | #endif /* __linux__ */ |
| 19 | |
| 20 | #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \ |
| 21 | defined(__OpenBSD__) |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/endian.h> |
| 24 | #define __BYTE_ORDER _BYTE_ORDER |
| 25 | #define __LITTLE_ENDIAN _LITTLE_ENDIAN |
| 26 | #define __BIG_ENDIAN _BIG_ENDIAN |
| 27 | #ifdef __OpenBSD__ |
| 28 | #define bswap_16 swap16 |
| 29 | #define bswap_32 swap32 |
| 30 | #define bswap_64 swap64 |
| 31 | #else /* __OpenBSD__ */ |
| 32 | #define bswap_16 bswap16 |
| 33 | #define bswap_32 bswap32 |
| 34 | #define bswap_64 bswap64 |
| 35 | #endif /* __OpenBSD__ */ |
| 36 | #endif /* defined(__FreeBSD__) || defined(__NetBSD__) || |
| 37 | * defined(__DragonFly__) || defined(__OpenBSD__) */ |
| 38 | |
| 39 | #ifdef __APPLE__ |
| 40 | #include <sys/types.h> |
| 41 | #include <machine/endian.h> |
| 42 | #define __BYTE_ORDER _BYTE_ORDER |
| 43 | #define __LITTLE_ENDIAN _LITTLE_ENDIAN |
| 44 | #define __BIG_ENDIAN _BIG_ENDIAN |
| 45 | static inline unsigned short bswap_16(unsigned short v) |
| 46 | { |
| 47 | return ((v & 0xff) << 8) | (v >> 8); |
| 48 | } |
| 49 | |
| 50 | static inline unsigned int bswap_32(unsigned int v) |
| 51 | { |
| 52 | return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | |
| 53 | ((v & 0xff0000) >> 8) | (v >> 24); |
| 54 | } |
| 55 | #endif /* __APPLE__ */ |
| 56 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 57 | #ifdef __rtems__ |
| 58 | #include <rtems/endian.h> |
| 59 | #define __BYTE_ORDER BYTE_ORDER |
| 60 | #define __LITTLE_ENDIAN LITTLE_ENDIAN |
| 61 | #define __BIG_ENDIAN BIG_ENDIAN |
| 62 | #define bswap_16 CPU_swap_u16 |
| 63 | #define bswap_32 CPU_swap_u32 |
| 64 | #endif /* __rtems__ */ |
| 65 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 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 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 113 | #ifndef WPA_TYPES_DEFINED |
| 114 | #ifdef CONFIG_USE_INTTYPES_H |
| 115 | #include <inttypes.h> |
| 116 | #else |
| 117 | #include <stdint.h> |
| 118 | #endif |
| 119 | typedef uint64_t u64; |
| 120 | typedef uint32_t u32; |
| 121 | typedef uint16_t u16; |
| 122 | typedef uint8_t u8; |
| 123 | typedef int64_t s64; |
| 124 | typedef int32_t s32; |
| 125 | typedef int16_t s16; |
| 126 | typedef int8_t s8; |
| 127 | #define WPA_TYPES_DEFINED |
| 128 | #endif /* !WPA_TYPES_DEFINED */ |
| 129 | |
| 130 | |
| 131 | /* Define platform specific byte swapping macros */ |
| 132 | |
| 133 | #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS) |
| 134 | |
| 135 | static inline unsigned short wpa_swap_16(unsigned short v) |
| 136 | { |
| 137 | return ((v & 0xff) << 8) | (v >> 8); |
| 138 | } |
| 139 | |
| 140 | static inline unsigned int wpa_swap_32(unsigned int v) |
| 141 | { |
| 142 | return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | |
| 143 | ((v & 0xff0000) >> 8) | (v >> 24); |
| 144 | } |
| 145 | |
| 146 | #define le_to_host16(n) (n) |
| 147 | #define host_to_le16(n) (n) |
| 148 | #define be_to_host16(n) wpa_swap_16(n) |
| 149 | #define host_to_be16(n) wpa_swap_16(n) |
| 150 | #define le_to_host32(n) (n) |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 151 | #define host_to_le32(n) (n) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 152 | #define be_to_host32(n) wpa_swap_32(n) |
| 153 | #define host_to_be32(n) wpa_swap_32(n) |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 154 | #define host_to_le64(n) (n) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 155 | |
| 156 | #define WPA_BYTE_SWAP_DEFINED |
| 157 | |
| 158 | #endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */ |
| 159 | |
| 160 | |
| 161 | #ifndef WPA_BYTE_SWAP_DEFINED |
| 162 | |
| 163 | #ifndef __BYTE_ORDER |
| 164 | #ifndef __LITTLE_ENDIAN |
| 165 | #ifndef __BIG_ENDIAN |
| 166 | #define __LITTLE_ENDIAN 1234 |
| 167 | #define __BIG_ENDIAN 4321 |
| 168 | #if defined(sparc) |
| 169 | #define __BYTE_ORDER __BIG_ENDIAN |
| 170 | #endif |
| 171 | #endif /* __BIG_ENDIAN */ |
| 172 | #endif /* __LITTLE_ENDIAN */ |
| 173 | #endif /* __BYTE_ORDER */ |
| 174 | |
| 175 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 176 | #define le_to_host16(n) ((__force u16) (le16) (n)) |
| 177 | #define host_to_le16(n) ((__force le16) (u16) (n)) |
| 178 | #define be_to_host16(n) bswap_16((__force u16) (be16) (n)) |
| 179 | #define host_to_be16(n) ((__force be16) bswap_16((n))) |
| 180 | #define le_to_host32(n) ((__force u32) (le32) (n)) |
| 181 | #define host_to_le32(n) ((__force le32) (u32) (n)) |
| 182 | #define be_to_host32(n) bswap_32((__force u32) (be32) (n)) |
| 183 | #define host_to_be32(n) ((__force be32) bswap_32((n))) |
| 184 | #define le_to_host64(n) ((__force u64) (le64) (n)) |
| 185 | #define host_to_le64(n) ((__force le64) (u64) (n)) |
| 186 | #define be_to_host64(n) bswap_64((__force u64) (be64) (n)) |
| 187 | #define host_to_be64(n) ((__force be64) bswap_64((n))) |
| 188 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 189 | #define le_to_host16(n) bswap_16(n) |
| 190 | #define host_to_le16(n) bswap_16(n) |
| 191 | #define be_to_host16(n) (n) |
| 192 | #define host_to_be16(n) (n) |
| 193 | #define le_to_host32(n) bswap_32(n) |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 194 | #define host_to_le32(n) bswap_32(n) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 195 | #define be_to_host32(n) (n) |
| 196 | #define host_to_be32(n) (n) |
| 197 | #define le_to_host64(n) bswap_64(n) |
| 198 | #define host_to_le64(n) bswap_64(n) |
| 199 | #define be_to_host64(n) (n) |
| 200 | #define host_to_be64(n) (n) |
| 201 | #ifndef WORDS_BIGENDIAN |
| 202 | #define WORDS_BIGENDIAN |
| 203 | #endif |
| 204 | #else |
| 205 | #error Could not determine CPU byte order |
| 206 | #endif |
| 207 | |
| 208 | #define WPA_BYTE_SWAP_DEFINED |
| 209 | #endif /* !WPA_BYTE_SWAP_DEFINED */ |
| 210 | |
| 211 | |
| 212 | /* Macros for handling unaligned memory accesses */ |
| 213 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 214 | static inline u16 WPA_GET_BE16(const u8 *a) |
| 215 | { |
| 216 | return (a[0] << 8) | a[1]; |
| 217 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 218 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 219 | static inline void WPA_PUT_BE16(u8 *a, u16 val) |
| 220 | { |
| 221 | a[0] = val >> 8; |
| 222 | a[1] = val & 0xff; |
| 223 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 224 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 225 | static inline u16 WPA_GET_LE16(const u8 *a) |
| 226 | { |
| 227 | return (a[1] << 8) | a[0]; |
| 228 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 229 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 230 | static inline void WPA_PUT_LE16(u8 *a, u16 val) |
| 231 | { |
| 232 | a[1] = val >> 8; |
| 233 | a[0] = val & 0xff; |
| 234 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 235 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 236 | static inline u32 WPA_GET_BE24(const u8 *a) |
| 237 | { |
| 238 | return (a[0] << 16) | (a[1] << 8) | a[2]; |
| 239 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 240 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 241 | static inline void WPA_PUT_BE24(u8 *a, u32 val) |
| 242 | { |
| 243 | a[0] = (val >> 16) & 0xff; |
| 244 | a[1] = (val >> 8) & 0xff; |
| 245 | a[2] = val & 0xff; |
| 246 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 247 | |
Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 248 | static inline u32 WPA_GET_LE24(const u8 *a) |
| 249 | { |
| 250 | return (a[2] << 16) | (a[1] << 8) | a[0]; |
| 251 | } |
| 252 | |
| 253 | static inline void WPA_PUT_LE24(u8 *a, u32 val) |
| 254 | { |
| 255 | a[2] = (val >> 16) & 0xff; |
| 256 | a[1] = (val >> 8) & 0xff; |
| 257 | a[0] = val & 0xff; |
| 258 | } |
| 259 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 260 | static inline u32 WPA_GET_BE32(const u8 *a) |
| 261 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 262 | return ((u32) a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static inline void WPA_PUT_BE32(u8 *a, u32 val) |
| 266 | { |
| 267 | a[0] = (val >> 24) & 0xff; |
| 268 | a[1] = (val >> 16) & 0xff; |
| 269 | a[2] = (val >> 8) & 0xff; |
| 270 | a[3] = val & 0xff; |
| 271 | } |
| 272 | |
| 273 | static inline u32 WPA_GET_LE32(const u8 *a) |
| 274 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 275 | return ((u32) a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0]; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static inline void WPA_PUT_LE32(u8 *a, u32 val) |
| 279 | { |
| 280 | a[3] = (val >> 24) & 0xff; |
| 281 | a[2] = (val >> 16) & 0xff; |
| 282 | a[1] = (val >> 8) & 0xff; |
| 283 | a[0] = val & 0xff; |
| 284 | } |
| 285 | |
| 286 | static inline u64 WPA_GET_BE64(const u8 *a) |
| 287 | { |
| 288 | return (((u64) a[0]) << 56) | (((u64) a[1]) << 48) | |
| 289 | (((u64) a[2]) << 40) | (((u64) a[3]) << 32) | |
| 290 | (((u64) a[4]) << 24) | (((u64) a[5]) << 16) | |
| 291 | (((u64) a[6]) << 8) | ((u64) a[7]); |
| 292 | } |
| 293 | |
| 294 | static inline void WPA_PUT_BE64(u8 *a, u64 val) |
| 295 | { |
| 296 | a[0] = val >> 56; |
| 297 | a[1] = val >> 48; |
| 298 | a[2] = val >> 40; |
| 299 | a[3] = val >> 32; |
| 300 | a[4] = val >> 24; |
| 301 | a[5] = val >> 16; |
| 302 | a[6] = val >> 8; |
| 303 | a[7] = val & 0xff; |
| 304 | } |
| 305 | |
| 306 | static inline u64 WPA_GET_LE64(const u8 *a) |
| 307 | { |
| 308 | return (((u64) a[7]) << 56) | (((u64) a[6]) << 48) | |
| 309 | (((u64) a[5]) << 40) | (((u64) a[4]) << 32) | |
| 310 | (((u64) a[3]) << 24) | (((u64) a[2]) << 16) | |
| 311 | (((u64) a[1]) << 8) | ((u64) a[0]); |
| 312 | } |
| 313 | |
| 314 | static inline void WPA_PUT_LE64(u8 *a, u64 val) |
| 315 | { |
| 316 | a[7] = val >> 56; |
| 317 | a[6] = val >> 48; |
| 318 | a[5] = val >> 40; |
| 319 | a[4] = val >> 32; |
| 320 | a[3] = val >> 24; |
| 321 | a[2] = val >> 16; |
| 322 | a[1] = val >> 8; |
| 323 | a[0] = val & 0xff; |
| 324 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 325 | |
| 326 | |
| 327 | #ifndef ETH_ALEN |
| 328 | #define ETH_ALEN 6 |
| 329 | #endif |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 330 | #ifndef ETH_HLEN |
| 331 | #define ETH_HLEN 14 |
| 332 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 333 | #ifndef IFNAMSIZ |
| 334 | #define IFNAMSIZ 16 |
| 335 | #endif |
| 336 | #ifndef ETH_P_ALL |
| 337 | #define ETH_P_ALL 0x0003 |
| 338 | #endif |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 339 | #ifndef ETH_P_IP |
| 340 | #define ETH_P_IP 0x0800 |
| 341 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 342 | #ifndef ETH_P_80211_ENCAP |
| 343 | #define ETH_P_80211_ENCAP 0x890d /* TDLS comes under this category */ |
| 344 | #endif |
| 345 | #ifndef ETH_P_PAE |
| 346 | #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */ |
| 347 | #endif /* ETH_P_PAE */ |
| 348 | #ifndef ETH_P_EAPOL |
| 349 | #define ETH_P_EAPOL ETH_P_PAE |
| 350 | #endif /* ETH_P_EAPOL */ |
| 351 | #ifndef ETH_P_RSN_PREAUTH |
| 352 | #define ETH_P_RSN_PREAUTH 0x88c7 |
| 353 | #endif /* ETH_P_RSN_PREAUTH */ |
| 354 | #ifndef ETH_P_RRB |
| 355 | #define ETH_P_RRB 0x890D |
| 356 | #endif /* ETH_P_RRB */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 357 | #ifndef ETH_P_OUI |
| 358 | #define ETH_P_OUI 0x88B7 |
| 359 | #endif /* ETH_P_OUI */ |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 360 | #ifndef ETH_P_8021Q |
| 361 | #define ETH_P_8021Q 0x8100 |
| 362 | #endif /* ETH_P_8021Q */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 363 | |
| 364 | |
| 365 | #ifdef __GNUC__ |
| 366 | #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b)))) |
| 367 | #define STRUCT_PACKED __attribute__ ((packed)) |
| 368 | #else |
| 369 | #define PRINTF_FORMAT(a,b) |
| 370 | #define STRUCT_PACKED |
| 371 | #endif |
| 372 | |
| 373 | |
| 374 | #ifdef CONFIG_ANSI_C_EXTRA |
| 375 | |
| 376 | #if !defined(_MSC_VER) || _MSC_VER < 1400 |
| 377 | /* snprintf - used in number of places; sprintf() is _not_ a good replacement |
| 378 | * due to possible buffer overflow; see, e.g., |
| 379 | * http://www.ijs.si/software/snprintf/ for portable implementation of |
| 380 | * snprintf. */ |
| 381 | int snprintf(char *str, size_t size, const char *format, ...); |
| 382 | |
| 383 | /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */ |
| 384 | int vsnprintf(char *str, size_t size, const char *format, va_list ap); |
| 385 | #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */ |
| 386 | |
| 387 | /* getopt - only used in main.c */ |
| 388 | int getopt(int argc, char *const argv[], const char *optstring); |
| 389 | extern char *optarg; |
| 390 | extern int optind; |
| 391 | |
| 392 | #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF |
| 393 | #ifndef __socklen_t_defined |
| 394 | typedef int socklen_t; |
| 395 | #endif |
| 396 | #endif |
| 397 | |
| 398 | /* inline - define as __inline or just define it to be empty, if needed */ |
| 399 | #ifdef CONFIG_NO_INLINE |
| 400 | #define inline |
| 401 | #else |
| 402 | #define inline __inline |
| 403 | #endif |
| 404 | |
| 405 | #ifndef __func__ |
| 406 | #define __func__ "__func__ not defined" |
| 407 | #endif |
| 408 | |
| 409 | #ifndef bswap_16 |
| 410 | #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff)) |
| 411 | #endif |
| 412 | |
| 413 | #ifndef bswap_32 |
| 414 | #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \ |
| 415 | (((u32) (a) << 8) & 0xff0000) | \ |
| 416 | (((u32) (a) >> 8) & 0xff00) | \ |
| 417 | (((u32) (a) >> 24) & 0xff)) |
| 418 | #endif |
| 419 | |
| 420 | #ifndef MSG_DONTWAIT |
| 421 | #define MSG_DONTWAIT 0 |
| 422 | #endif |
| 423 | |
| 424 | #ifdef _WIN32_WCE |
| 425 | void perror(const char *s); |
| 426 | #endif /* _WIN32_WCE */ |
| 427 | |
| 428 | #endif /* CONFIG_ANSI_C_EXTRA */ |
| 429 | |
| 430 | #ifndef MAC2STR |
| 431 | #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] |
| 432 | #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x" |
| 433 | |
| 434 | /* |
| 435 | * Compact form for string representation of MAC address |
| 436 | * To be used, e.g., for constructing dbus paths for P2P Devices |
| 437 | */ |
| 438 | #define COMPACT_MACSTR "%02x%02x%02x%02x%02x%02x" |
| 439 | #endif |
| 440 | |
| 441 | #ifndef BIT |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 442 | #define BIT(x) (1U << (x)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 443 | #endif |
| 444 | |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 445 | #ifndef MIN |
| 446 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 447 | #endif |
| 448 | #ifndef MAX |
| 449 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) |
| 450 | #endif |
| 451 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 452 | /* |
| 453 | * Definitions for sparse validation |
| 454 | * (http://kernel.org/pub/linux/kernel/people/josh/sparse/) |
| 455 | */ |
| 456 | #ifdef __CHECKER__ |
| 457 | #define __force __attribute__((force)) |
Dmitry Shmidt | 4ae50e6 | 2016-06-27 13:48:39 -0700 | [diff] [blame] | 458 | #undef __bitwise |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 459 | #define __bitwise __attribute__((bitwise)) |
| 460 | #else |
| 461 | #define __force |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 462 | #undef __bitwise |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 463 | #define __bitwise |
| 464 | #endif |
| 465 | |
| 466 | typedef u16 __bitwise be16; |
| 467 | typedef u16 __bitwise le16; |
| 468 | typedef u32 __bitwise be32; |
| 469 | typedef u32 __bitwise le32; |
| 470 | typedef u64 __bitwise be64; |
| 471 | typedef u64 __bitwise le64; |
| 472 | |
| 473 | #ifndef __must_check |
| 474 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) |
| 475 | #define __must_check __attribute__((__warn_unused_result__)) |
| 476 | #else |
| 477 | #define __must_check |
| 478 | #endif /* __GNUC__ */ |
| 479 | #endif /* __must_check */ |
| 480 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 481 | #ifndef __maybe_unused |
| 482 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) |
| 483 | #define __maybe_unused __attribute__((unused)) |
| 484 | #else |
| 485 | #define __maybe_unused |
| 486 | #endif /* __GNUC__ */ |
| 487 | #endif /* __must_check */ |
| 488 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 489 | #define SSID_MAX_LEN 32 |
| 490 | |
| 491 | struct wpa_ssid_value { |
| 492 | u8 ssid[SSID_MAX_LEN]; |
| 493 | size_t ssid_len; |
| 494 | }; |
| 495 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 496 | int hwaddr_aton(const char *txt, u8 *addr); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 497 | int hwaddr_masked_aton(const char *txt, u8 *addr, u8 *mask, u8 maskable); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 498 | int hwaddr_compact_aton(const char *txt, u8 *addr); |
| 499 | int hwaddr_aton2(const char *txt, u8 *addr); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 500 | int hex2num(char c); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 501 | int hex2byte(const char *hex); |
| 502 | int hexstr2bin(const char *hex, u8 *buf, size_t len); |
| 503 | void inc_byte_array(u8 *counter, size_t len); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 504 | void buf_shift_right(u8 *buf, size_t len, size_t bits); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 505 | void wpa_get_ntp_timestamp(u8 *buf); |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 506 | int wpa_scnprintf(char *buf, size_t size, const char *fmt, ...) |
| 507 | PRINTF_FORMAT(3, 4); |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 508 | int wpa_snprintf_hex_sep(char *buf, size_t buf_size, const u8 *data, size_t len, |
| 509 | char sep); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 510 | int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len); |
| 511 | int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data, |
| 512 | size_t len); |
| 513 | |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 514 | int hwaddr_mask_txt(char *buf, size_t len, const u8 *addr, const u8 *mask); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 515 | int ssid_parse(const char *buf, struct wpa_ssid_value *ssid); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 516 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 517 | #ifdef CONFIG_NATIVE_WINDOWS |
| 518 | void wpa_unicode2ascii_inplace(TCHAR *str); |
| 519 | TCHAR * wpa_strdup_tchar(const char *str); |
| 520 | #else /* CONFIG_NATIVE_WINDOWS */ |
| 521 | #define wpa_unicode2ascii_inplace(s) do { } while (0) |
| 522 | #define wpa_strdup_tchar(s) strdup((s)) |
| 523 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 524 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 525 | void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len); |
| 526 | size_t printf_decode(u8 *buf, size_t maxlen, const char *str); |
| 527 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 528 | const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len); |
| 529 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 530 | char * wpa_config_parse_string(const char *value, size_t *len); |
| 531 | int is_hex(const u8 *data, size_t len); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 532 | int has_ctrl_char(const u8 *data, size_t len); |
| 533 | int has_newline(const char *str); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 534 | size_t merge_byte_arrays(u8 *res, size_t res_len, |
| 535 | const u8 *src1, size_t src1_len, |
| 536 | const u8 *src2, size_t src2_len); |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 537 | char * dup_binstr(const void *src, size_t len); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 538 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 539 | static inline int is_zero_ether_addr(const u8 *a) |
| 540 | { |
| 541 | return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]); |
| 542 | } |
| 543 | |
| 544 | static inline int is_broadcast_ether_addr(const u8 *a) |
| 545 | { |
| 546 | return (a[0] & a[1] & a[2] & a[3] & a[4] & a[5]) == 0xff; |
| 547 | } |
| 548 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 549 | static inline int is_multicast_ether_addr(const u8 *a) |
| 550 | { |
| 551 | return a[0] & 0x01; |
| 552 | } |
| 553 | |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 554 | static inline bool ether_addr_equal(const u8 *a, const u8 *b) |
| 555 | { |
| 556 | return os_memcmp(a, b, ETH_ALEN) == 0; |
| 557 | } |
| 558 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 559 | #define broadcast_ether_addr (const u8 *) "\xff\xff\xff\xff\xff\xff" |
| 560 | |
| 561 | #include "wpa_debug.h" |
| 562 | |
| 563 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 564 | struct wpa_freq_range_list { |
| 565 | struct wpa_freq_range { |
| 566 | unsigned int min; |
| 567 | unsigned int max; |
| 568 | } *range; |
| 569 | unsigned int num; |
| 570 | }; |
| 571 | |
| 572 | int freq_range_list_parse(struct wpa_freq_range_list *res, const char *value); |
| 573 | int freq_range_list_includes(const struct wpa_freq_range_list *list, |
| 574 | unsigned int freq); |
| 575 | char * freq_range_list_str(const struct wpa_freq_range_list *list); |
| 576 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 577 | size_t int_array_len(const int *a); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 578 | void int_array_concat(int **res, const int *a); |
| 579 | void int_array_sort_unique(int *a); |
| 580 | void int_array_add_unique(int **res, int a); |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 581 | bool int_array_includes(int *arr, int val); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 582 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 583 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 584 | |
Dmitry Shmidt | c281702 | 2014-07-02 10:32:10 -0700 | [diff] [blame] | 585 | void str_clear_free(char *str); |
| 586 | void bin_clear_free(void *bin, size_t len); |
| 587 | |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 588 | int random_mac_addr(u8 *addr); |
| 589 | int random_mac_addr_keep_oui(u8 *addr); |
| 590 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 591 | const char * cstr_token(const char *str, const char *delim, const char **last); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 592 | char * str_token(char *str, const char *delim, char **context); |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 593 | size_t utf8_escape(const char *inp, size_t in_size, |
| 594 | char *outp, size_t out_size); |
| 595 | size_t utf8_unescape(const char *inp, size_t in_size, |
| 596 | char *outp, size_t out_size); |
Dmitry Shmidt | 9d9e602 | 2015-04-23 10:34:55 -0700 | [diff] [blame] | 597 | int is_ctrl_char(char c); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 598 | |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 599 | int str_starts(const char *str, const char *start); |
| 600 | |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 601 | u8 rssi_to_rcpi(int rssi); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 602 | char * get_param(const char *cmd, const char *param); |
Dmitry Shmidt | c281702 | 2014-07-02 10:32:10 -0700 | [diff] [blame] | 603 | |
Sunil Ravi | 99c035e | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 604 | #define for_each_link(__links, __i) \ |
| 605 | for ((__i) = 0; (__i) < MAX_NUM_MLD_LINKS; (__i)++) \ |
| 606 | if ((__links) & BIT(__i)) |
| 607 | |
| 608 | /* Iterate all links, or, if no link is defined, iterate given index */ |
| 609 | #define for_each_link_default(_links, _i, _def_idx) \ |
| 610 | for ((_i) = (_links) ? 0 : (_def_idx); \ |
| 611 | (_i) < MAX_NUM_MLD_LINKS || \ |
| 612 | (!(_links) && (_i) == (_def_idx)); \ |
| 613 | (_i)++) \ |
| 614 | if (!(_links) || (_links) & BIT(_i)) |
| 615 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 616 | void forced_memzero(void *ptr, size_t len); |
| 617 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 618 | /* |
| 619 | * gcc 4.4 ends up generating strict-aliasing warnings about some very common |
| 620 | * networking socket uses that do not really result in a real problem and |
| 621 | * cannot be easily avoided with union-based type-punning due to struct |
| 622 | * definitions including another struct in system header files. To avoid having |
| 623 | * to fully disable strict-aliasing warnings, provide a mechanism to hide the |
| 624 | * typecast from aliasing for now. A cleaner solution will hopefully be found |
| 625 | * in the future to handle these cases. |
| 626 | */ |
| 627 | void * __hide_aliasing_typecast(void *foo); |
| 628 | #define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a)) |
| 629 | |
| 630 | #ifdef CONFIG_VALGRIND |
| 631 | #include <valgrind/memcheck.h> |
| 632 | #define WPA_MEM_DEFINED(ptr, len) VALGRIND_MAKE_MEM_DEFINED((ptr), (len)) |
| 633 | #else /* CONFIG_VALGRIND */ |
| 634 | #define WPA_MEM_DEFINED(ptr, len) do { } while (0) |
| 635 | #endif /* CONFIG_VALGRIND */ |
| 636 | |
| 637 | #endif /* COMMON_H */ |