blob: 20f1c6c3f8582eacfae9bf19970d229fbf2a0436 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant/hostapd / common helper functions, etc.
Hai Shalom021b0b52019-04-10 11:17:58 -07003 * Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
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#include "includes.h"
Hai Shalomfdcde762020-04-02 11:19:20 -070010#include <limits.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -070012#include "common/ieee802_11_defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070013#include "common.h"
14
15
16static int hex2num(char c)
17{
18 if (c >= '0' && c <= '9')
19 return c - '0';
20 if (c >= 'a' && c <= 'f')
21 return c - 'a' + 10;
22 if (c >= 'A' && c <= 'F')
23 return c - 'A' + 10;
24 return -1;
25}
26
27
28int hex2byte(const char *hex)
29{
30 int a, b;
31 a = hex2num(*hex++);
32 if (a < 0)
33 return -1;
34 b = hex2num(*hex++);
35 if (b < 0)
36 return -1;
37 return (a << 4) | b;
38}
39
40
Dmitry Shmidtff787d52015-01-12 13:01:47 -080041static const char * hwaddr_parse(const char *txt, u8 *addr)
42{
43 size_t i;
44
45 for (i = 0; i < ETH_ALEN; i++) {
46 int a;
47
48 a = hex2byte(txt);
49 if (a < 0)
50 return NULL;
51 txt += 2;
52 addr[i] = a;
53 if (i < ETH_ALEN - 1 && *txt++ != ':')
54 return NULL;
55 }
56 return txt;
57}
58
59
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060/**
61 * hwaddr_aton - Convert ASCII string to MAC address (colon-delimited format)
62 * @txt: MAC address as a string (e.g., "00:11:22:33:44:55")
63 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
64 * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
65 */
66int hwaddr_aton(const char *txt, u8 *addr)
67{
Dmitry Shmidtff787d52015-01-12 13:01:47 -080068 return hwaddr_parse(txt, addr) ? 0 : -1;
69}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070071
Dmitry Shmidtff787d52015-01-12 13:01:47 -080072/**
73 * hwaddr_masked_aton - Convert ASCII string with optional mask to MAC address (colon-delimited format)
74 * @txt: MAC address with optional mask as a string (e.g., "00:11:22:33:44:55/ff:ff:ff:ff:00:00")
75 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
76 * @mask: Buffer for the MAC address mask (ETH_ALEN = 6 bytes)
77 * @maskable: Flag to indicate whether a mask is allowed
78 * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
79 */
80int hwaddr_masked_aton(const char *txt, u8 *addr, u8 *mask, u8 maskable)
81{
82 const char *r;
83
84 /* parse address part */
85 r = hwaddr_parse(txt, addr);
86 if (!r)
87 return -1;
88
89 /* check for optional mask */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -080090 if (*r == '\0' || isspace((unsigned char) *r)) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -080091 /* no mask specified, assume default */
92 os_memset(mask, 0xff, ETH_ALEN);
93 } else if (maskable && *r == '/') {
94 /* mask specified and allowed */
95 r = hwaddr_parse(r + 1, mask);
96 /* parser error? */
97 if (!r)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070098 return -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -080099 } else {
100 /* mask specified but not allowed or trailing garbage */
101 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102 }
103
104 return 0;
105}
106
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800107
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108/**
109 * hwaddr_compact_aton - Convert ASCII string to MAC address (no colon delimitors format)
110 * @txt: MAC address as a string (e.g., "001122334455")
111 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
112 * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
113 */
114int hwaddr_compact_aton(const char *txt, u8 *addr)
115{
116 int i;
117
118 for (i = 0; i < 6; i++) {
119 int a, b;
120
121 a = hex2num(*txt++);
122 if (a < 0)
123 return -1;
124 b = hex2num(*txt++);
125 if (b < 0)
126 return -1;
127 *addr++ = (a << 4) | b;
128 }
129
130 return 0;
131}
132
133/**
134 * hwaddr_aton2 - Convert ASCII string to MAC address (in any known format)
135 * @txt: MAC address as a string (e.g., 00:11:22:33:44:55 or 0011.2233.4455)
136 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
137 * Returns: Characters used (> 0) on success, -1 on failure
138 */
139int hwaddr_aton2(const char *txt, u8 *addr)
140{
141 int i;
142 const char *pos = txt;
143
144 for (i = 0; i < 6; i++) {
145 int a, b;
146
147 while (*pos == ':' || *pos == '.' || *pos == '-')
148 pos++;
149
150 a = hex2num(*pos++);
151 if (a < 0)
152 return -1;
153 b = hex2num(*pos++);
154 if (b < 0)
155 return -1;
156 *addr++ = (a << 4) | b;
157 }
158
159 return pos - txt;
160}
161
162
163/**
164 * hexstr2bin - Convert ASCII hex string into binary data
165 * @hex: ASCII hex string (e.g., "01ab")
166 * @buf: Buffer for the binary data
167 * @len: Length of the text to convert in bytes (of buf); hex will be double
168 * this size
169 * Returns: 0 on success, -1 on failure (invalid hex string)
170 */
171int hexstr2bin(const char *hex, u8 *buf, size_t len)
172{
173 size_t i;
174 int a;
175 const char *ipos = hex;
176 u8 *opos = buf;
177
178 for (i = 0; i < len; i++) {
179 a = hex2byte(ipos);
180 if (a < 0)
181 return -1;
182 *opos++ = a;
183 ipos += 2;
184 }
185 return 0;
186}
187
188
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800189int hwaddr_mask_txt(char *buf, size_t len, const u8 *addr, const u8 *mask)
190{
191 size_t i;
192 int print_mask = 0;
193 int res;
194
195 for (i = 0; i < ETH_ALEN; i++) {
196 if (mask[i] != 0xff) {
197 print_mask = 1;
198 break;
199 }
200 }
201
202 if (print_mask)
203 res = os_snprintf(buf, len, MACSTR "/" MACSTR,
204 MAC2STR(addr), MAC2STR(mask));
205 else
206 res = os_snprintf(buf, len, MACSTR, MAC2STR(addr));
207 if (os_snprintf_error(len, res))
208 return -1;
209 return res;
210}
211
212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213/**
214 * inc_byte_array - Increment arbitrary length byte array by one
215 * @counter: Pointer to byte array
216 * @len: Length of the counter in bytes
217 *
218 * This function increments the last byte of the counter by one and continues
219 * rolling over to more significant bytes if the byte was incremented from
220 * 0xff to 0x00.
221 */
222void inc_byte_array(u8 *counter, size_t len)
223{
224 int pos = len - 1;
225 while (pos >= 0) {
226 counter[pos]++;
227 if (counter[pos] != 0)
228 break;
229 pos--;
230 }
231}
232
233
Hai Shalom81f62d82019-07-22 12:10:00 -0700234void buf_shift_right(u8 *buf, size_t len, size_t bits)
235{
236 size_t i;
237
238 for (i = len - 1; i > 0; i--)
239 buf[i] = (buf[i - 1] << (8 - bits)) | (buf[i] >> bits);
240 buf[0] >>= bits;
241}
242
243
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244void wpa_get_ntp_timestamp(u8 *buf)
245{
246 struct os_time now;
247 u32 sec, usec;
248 be32 tmp;
249
250 /* 64-bit NTP timestamp (time from 1900-01-01 00:00:00) */
251 os_get_time(&now);
252 sec = now.sec + 2208988800U; /* Epoch to 1900 */
253 /* Estimate 2^32/10^6 = 4295 - 1/32 - 1/512 */
254 usec = now.usec;
255 usec = 4295 * usec - (usec >> 5) - (usec >> 9);
256 tmp = host_to_be32(sec);
257 os_memcpy(buf, (u8 *) &tmp, 4);
258 tmp = host_to_be32(usec);
259 os_memcpy(buf + 4, (u8 *) &tmp, 4);
260}
261
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800262/**
263 * wpa_scnprintf - Simpler-to-use snprintf function
264 * @buf: Output buffer
265 * @size: Buffer size
266 * @fmt: format
267 *
268 * Simpler snprintf version that doesn't require further error checks - the
269 * return value only indicates how many bytes were actually written, excluding
270 * the NULL byte (i.e., 0 on error, size-1 if buffer is not big enough).
271 */
272int wpa_scnprintf(char *buf, size_t size, const char *fmt, ...)
273{
274 va_list ap;
275 int ret;
276
277 if (!size)
278 return 0;
279
280 va_start(ap, fmt);
281 ret = vsnprintf(buf, size, fmt, ap);
282 va_end(ap);
283
284 if (ret < 0)
285 return 0;
286 if ((size_t) ret >= size)
287 return size - 1;
288
289 return ret;
290}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291
Dmitry Shmidtaf9da312015-04-03 10:03:11 -0700292
293int wpa_snprintf_hex_sep(char *buf, size_t buf_size, const u8 *data, size_t len,
294 char sep)
295{
296 size_t i;
297 char *pos = buf, *end = buf + buf_size;
298 int ret;
299
300 if (buf_size == 0)
301 return 0;
302
303 for (i = 0; i < len; i++) {
304 ret = os_snprintf(pos, end - pos, "%02x%c",
305 data[i], sep);
306 if (os_snprintf_error(end - pos, ret)) {
307 end[-1] = '\0';
308 return pos - buf;
309 }
310 pos += ret;
311 }
312 pos[-1] = '\0';
313 return pos - buf;
314}
315
316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,
318 size_t len, int uppercase)
319{
320 size_t i;
321 char *pos = buf, *end = buf + buf_size;
322 int ret;
323 if (buf_size == 0)
324 return 0;
325 for (i = 0; i < len; i++) {
326 ret = os_snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
327 data[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800328 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 end[-1] = '\0';
330 return pos - buf;
331 }
332 pos += ret;
333 }
334 end[-1] = '\0';
335 return pos - buf;
336}
337
338/**
339 * wpa_snprintf_hex - Print data as a hex string into a buffer
340 * @buf: Memory area to use as the output buffer
341 * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
342 * @data: Data to be printed
343 * @len: Length of data in bytes
344 * Returns: Number of bytes written
345 */
346int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
347{
348 return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
349}
350
351
352/**
353 * wpa_snprintf_hex_uppercase - Print data as a upper case hex string into buf
354 * @buf: Memory area to use as the output buffer
355 * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
356 * @data: Data to be printed
357 * @len: Length of data in bytes
358 * Returns: Number of bytes written
359 */
360int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
361 size_t len)
362{
363 return _wpa_snprintf_hex(buf, buf_size, data, len, 1);
364}
365
366
367#ifdef CONFIG_ANSI_C_EXTRA
368
369#ifdef _WIN32_WCE
370void perror(const char *s)
371{
372 wpa_printf(MSG_ERROR, "%s: GetLastError: %d",
373 s, (int) GetLastError());
374}
375#endif /* _WIN32_WCE */
376
377
378int optind = 1;
379int optopt;
380char *optarg;
381
382int getopt(int argc, char *const argv[], const char *optstring)
383{
384 static int optchr = 1;
385 char *cp;
386
387 if (optchr == 1) {
388 if (optind >= argc) {
389 /* all arguments processed */
390 return EOF;
391 }
392
393 if (argv[optind][0] != '-' || argv[optind][1] == '\0') {
394 /* no option characters */
395 return EOF;
396 }
397 }
398
399 if (os_strcmp(argv[optind], "--") == 0) {
400 /* no more options */
401 optind++;
402 return EOF;
403 }
404
405 optopt = argv[optind][optchr];
406 cp = os_strchr(optstring, optopt);
407 if (cp == NULL || optopt == ':') {
408 if (argv[optind][++optchr] == '\0') {
409 optchr = 1;
410 optind++;
411 }
412 return '?';
413 }
414
415 if (cp[1] == ':') {
416 /* Argument required */
417 optchr = 1;
418 if (argv[optind][optchr + 1]) {
419 /* No space between option and argument */
420 optarg = &argv[optind++][optchr + 1];
421 } else if (++optind >= argc) {
422 /* option requires an argument */
423 return '?';
424 } else {
425 /* Argument in the next argv */
426 optarg = argv[optind++];
427 }
428 } else {
429 /* No argument */
430 if (argv[optind][++optchr] == '\0') {
431 optchr = 1;
432 optind++;
433 }
434 optarg = NULL;
435 }
436 return *cp;
437}
438#endif /* CONFIG_ANSI_C_EXTRA */
439
440
441#ifdef CONFIG_NATIVE_WINDOWS
442/**
443 * wpa_unicode2ascii_inplace - Convert unicode string into ASCII
444 * @str: Pointer to string to convert
445 *
446 * This function converts a unicode string to ASCII using the same
447 * buffer for output. If UNICODE is not set, the buffer is not
448 * modified.
449 */
450void wpa_unicode2ascii_inplace(TCHAR *str)
451{
452#ifdef UNICODE
453 char *dst = (char *) str;
454 while (*str)
455 *dst++ = (char) *str++;
456 *dst = '\0';
457#endif /* UNICODE */
458}
459
460
461TCHAR * wpa_strdup_tchar(const char *str)
462{
463#ifdef UNICODE
464 TCHAR *buf;
465 buf = os_malloc((strlen(str) + 1) * sizeof(TCHAR));
466 if (buf == NULL)
467 return NULL;
468 wsprintf(buf, L"%S", str);
469 return buf;
470#else /* UNICODE */
471 return os_strdup(str);
472#endif /* UNICODE */
473}
474#endif /* CONFIG_NATIVE_WINDOWS */
475
476
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700477void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len)
478{
479 char *end = txt + maxlen;
480 size_t i;
481
482 for (i = 0; i < len; i++) {
Dmitry Shmidtb5d893b2014-06-04 15:28:27 -0700483 if (txt + 4 >= end)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700484 break;
485
486 switch (data[i]) {
487 case '\"':
488 *txt++ = '\\';
489 *txt++ = '\"';
490 break;
491 case '\\':
492 *txt++ = '\\';
493 *txt++ = '\\';
494 break;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700495 case '\033':
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700496 *txt++ = '\\';
497 *txt++ = 'e';
498 break;
499 case '\n':
500 *txt++ = '\\';
501 *txt++ = 'n';
502 break;
503 case '\r':
504 *txt++ = '\\';
505 *txt++ = 'r';
506 break;
507 case '\t':
508 *txt++ = '\\';
509 *txt++ = 't';
510 break;
511 default:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800512 if (data[i] >= 32 && data[i] <= 126) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700513 *txt++ = data[i];
514 } else {
515 txt += os_snprintf(txt, end - txt, "\\x%02x",
516 data[i]);
517 }
518 break;
519 }
520 }
521
522 *txt = '\0';
523}
524
525
526size_t printf_decode(u8 *buf, size_t maxlen, const char *str)
527{
528 const char *pos = str;
529 size_t len = 0;
530 int val;
531
532 while (*pos) {
Dmitry Shmidt56052862013-10-04 10:23:25 -0700533 if (len + 1 >= maxlen)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700534 break;
535 switch (*pos) {
536 case '\\':
537 pos++;
538 switch (*pos) {
539 case '\\':
540 buf[len++] = '\\';
541 pos++;
542 break;
543 case '"':
544 buf[len++] = '"';
545 pos++;
546 break;
547 case 'n':
548 buf[len++] = '\n';
549 pos++;
550 break;
551 case 'r':
552 buf[len++] = '\r';
553 pos++;
554 break;
555 case 't':
556 buf[len++] = '\t';
557 pos++;
558 break;
559 case 'e':
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700560 buf[len++] = '\033';
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700561 pos++;
562 break;
563 case 'x':
564 pos++;
565 val = hex2byte(pos);
566 if (val < 0) {
567 val = hex2num(*pos);
568 if (val < 0)
569 break;
570 buf[len++] = val;
571 pos++;
572 } else {
573 buf[len++] = val;
574 pos += 2;
575 }
576 break;
577 case '0':
578 case '1':
579 case '2':
580 case '3':
581 case '4':
582 case '5':
583 case '6':
584 case '7':
585 val = *pos++ - '0';
586 if (*pos >= '0' && *pos <= '7')
587 val = val * 8 + (*pos++ - '0');
588 if (*pos >= '0' && *pos <= '7')
589 val = val * 8 + (*pos++ - '0');
590 buf[len++] = val;
591 break;
592 default:
593 break;
594 }
595 break;
596 default:
597 buf[len++] = *pos++;
598 break;
599 }
600 }
Dmitry Shmidt56052862013-10-04 10:23:25 -0700601 if (maxlen > len)
602 buf[len] = '\0';
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700603
604 return len;
605}
606
607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608/**
609 * wpa_ssid_txt - Convert SSID to a printable string
610 * @ssid: SSID (32-octet string)
611 * @ssid_len: Length of ssid in octets
612 * Returns: Pointer to a printable string
613 *
614 * This function can be used to convert SSIDs into printable form. In most
615 * cases, SSIDs do not use unprintable characters, but IEEE 802.11 standard
616 * does not limit the used character set, so anything could be used in an SSID.
617 *
618 * This function uses a static buffer, so only one call can be used at the
619 * time, i.e., this is not re-entrant and the returned buffer must be used
620 * before calling this again.
621 */
622const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len)
623{
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700624 static char ssid_txt[SSID_MAX_LEN * 4 + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700625
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700626 if (ssid == NULL) {
627 ssid_txt[0] = '\0';
628 return ssid_txt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700630
631 printf_encode(ssid_txt, sizeof(ssid_txt), ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700632 return ssid_txt;
633}
634
635
636void * __hide_aliasing_typecast(void *foo)
637{
638 return foo;
639}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700640
641
642char * wpa_config_parse_string(const char *value, size_t *len)
643{
644 if (*value == '"') {
645 const char *pos;
646 char *str;
647 value++;
648 pos = os_strrchr(value, '"');
649 if (pos == NULL || pos[1] != '\0')
650 return NULL;
651 *len = pos - value;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700652 str = dup_binstr(value, *len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700653 if (str == NULL)
654 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700655 return str;
656 } else if (*value == 'P' && value[1] == '"') {
657 const char *pos;
658 char *tstr, *str;
659 size_t tlen;
660 value += 2;
661 pos = os_strrchr(value, '"');
662 if (pos == NULL || pos[1] != '\0')
663 return NULL;
664 tlen = pos - value;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700665 tstr = dup_binstr(value, tlen);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700666 if (tstr == NULL)
667 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700668
669 str = os_malloc(tlen + 1);
670 if (str == NULL) {
671 os_free(tstr);
672 return NULL;
673 }
674
675 *len = printf_decode((u8 *) str, tlen + 1, tstr);
676 os_free(tstr);
677
678 return str;
679 } else {
680 u8 *str;
681 size_t tlen, hlen = os_strlen(value);
682 if (hlen & 1)
683 return NULL;
684 tlen = hlen / 2;
685 str = os_malloc(tlen + 1);
686 if (str == NULL)
687 return NULL;
688 if (hexstr2bin(value, str, tlen)) {
689 os_free(str);
690 return NULL;
691 }
692 str[tlen] = '\0';
693 *len = tlen;
694 return (char *) str;
695 }
696}
697
698
699int is_hex(const u8 *data, size_t len)
700{
701 size_t i;
702
703 for (i = 0; i < len; i++) {
704 if (data[i] < 32 || data[i] >= 127)
705 return 1;
706 }
707 return 0;
708}
709
710
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700711int has_ctrl_char(const u8 *data, size_t len)
712{
713 size_t i;
714
715 for (i = 0; i < len; i++) {
716 if (data[i] < 32 || data[i] == 127)
717 return 1;
718 }
719 return 0;
720}
721
722
Isaac Chiou8ba146c2021-03-04 22:29:40 +0800723int has_non_nvt_ascii_char(const u8 *data, size_t len)
724{
725 size_t i;
726
727 for (i = 0; i < len; i++) {
728 if (data[i] < 7
729 || (13 < data[i] && data[i] < 32)
730 || data[i] == 127)
731 return 1;
732 }
733 return 0;
734}
735
736
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700737int has_newline(const char *str)
738{
739 while (*str) {
740 if (*str == '\n' || *str == '\r')
741 return 1;
742 str++;
743 }
744 return 0;
745}
746
747
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700748size_t merge_byte_arrays(u8 *res, size_t res_len,
749 const u8 *src1, size_t src1_len,
750 const u8 *src2, size_t src2_len)
751{
752 size_t len = 0;
753
754 os_memset(res, 0, res_len);
755
756 if (src1) {
757 if (src1_len >= res_len) {
758 os_memcpy(res, src1, res_len);
759 return res_len;
760 }
761
762 os_memcpy(res, src1, src1_len);
763 len += src1_len;
764 }
765
766 if (src2) {
767 if (len + src2_len >= res_len) {
768 os_memcpy(res + len, src2, res_len - len);
769 return res_len;
770 }
771
772 os_memcpy(res + len, src2, src2_len);
773 len += src2_len;
774 }
775
776 return len;
777}
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700778
779
780char * dup_binstr(const void *src, size_t len)
781{
782 char *res;
783
784 if (src == NULL)
785 return NULL;
786 res = os_malloc(len + 1);
787 if (res == NULL)
788 return NULL;
789 os_memcpy(res, src, len);
790 res[len] = '\0';
791
792 return res;
793}
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700794
795
796int freq_range_list_parse(struct wpa_freq_range_list *res, const char *value)
797{
798 struct wpa_freq_range *freq = NULL, *n;
799 unsigned int count = 0;
800 const char *pos, *pos2, *pos3;
801
802 /*
803 * Comma separated list of frequency ranges.
804 * For example: 2412-2432,2462,5000-6000
805 */
806 pos = value;
807 while (pos && pos[0]) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700808 if (count == UINT_MAX) {
809 os_free(freq);
810 return -1;
811 }
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700812 n = os_realloc_array(freq, count + 1,
813 sizeof(struct wpa_freq_range));
814 if (n == NULL) {
815 os_free(freq);
816 return -1;
817 }
818 freq = n;
819 freq[count].min = atoi(pos);
820 pos2 = os_strchr(pos, '-');
821 pos3 = os_strchr(pos, ',');
822 if (pos2 && (!pos3 || pos2 < pos3)) {
823 pos2++;
824 freq[count].max = atoi(pos2);
825 } else
826 freq[count].max = freq[count].min;
827 pos = pos3;
828 if (pos)
829 pos++;
830 count++;
831 }
832
833 os_free(res->range);
834 res->range = freq;
835 res->num = count;
836
837 return 0;
838}
839
840
841int freq_range_list_includes(const struct wpa_freq_range_list *list,
842 unsigned int freq)
843{
844 unsigned int i;
845
846 if (list == NULL)
847 return 0;
848
849 for (i = 0; i < list->num; i++) {
850 if (freq >= list->range[i].min && freq <= list->range[i].max)
851 return 1;
852 }
853
854 return 0;
855}
856
857
858char * freq_range_list_str(const struct wpa_freq_range_list *list)
859{
860 char *buf, *pos, *end;
861 size_t maxlen;
862 unsigned int i;
863 int res;
864
865 if (list->num == 0)
866 return NULL;
867
868 maxlen = list->num * 30;
869 buf = os_malloc(maxlen);
870 if (buf == NULL)
871 return NULL;
872 pos = buf;
873 end = buf + maxlen;
874
875 for (i = 0; i < list->num; i++) {
876 struct wpa_freq_range *range = &list->range[i];
877
878 if (range->min == range->max)
879 res = os_snprintf(pos, end - pos, "%s%u",
880 i == 0 ? "" : ",", range->min);
881 else
882 res = os_snprintf(pos, end - pos, "%s%u-%u",
883 i == 0 ? "" : ",",
884 range->min, range->max);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800885 if (os_snprintf_error(end - pos, res)) {
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700886 os_free(buf);
887 return NULL;
888 }
889 pos += res;
890 }
891
892 return buf;
893}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800894
895
Hai Shalomfdcde762020-04-02 11:19:20 -0700896size_t int_array_len(const int *a)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800897{
Hai Shalomfdcde762020-04-02 11:19:20 -0700898 size_t i;
899
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800900 for (i = 0; a && a[i]; i++)
901 ;
902 return i;
903}
904
905
906void int_array_concat(int **res, const int *a)
907{
Hai Shalomfdcde762020-04-02 11:19:20 -0700908 size_t reslen, alen, i, max_size;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800909 int *n;
910
911 reslen = int_array_len(*res);
912 alen = int_array_len(a);
Hai Shalomfdcde762020-04-02 11:19:20 -0700913 max_size = (size_t) -1;
914 if (alen >= max_size - reslen) {
915 /* This should not really happen, but if it did, something
916 * would overflow. Do not try to merge the arrays; instead, make
917 * this behave like memory allocation failure to avoid messing
918 * up memory. */
919 os_free(*res);
920 *res = NULL;
921 return;
922 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800923 n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
924 if (n == NULL) {
925 os_free(*res);
926 *res = NULL;
927 return;
928 }
929 for (i = 0; i <= alen; i++)
930 n[reslen + i] = a[i];
931 *res = n;
932}
933
934
935static int freq_cmp(const void *a, const void *b)
936{
937 int _a = *(int *) a;
938 int _b = *(int *) b;
939
940 if (_a == 0)
941 return 1;
942 if (_b == 0)
943 return -1;
944 return _a - _b;
945}
946
947
948void int_array_sort_unique(int *a)
949{
Hai Shalomfdcde762020-04-02 11:19:20 -0700950 size_t alen, i, j;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800951
952 if (a == NULL)
953 return;
954
955 alen = int_array_len(a);
956 qsort(a, alen, sizeof(int), freq_cmp);
957
958 i = 0;
959 j = 1;
960 while (a[i] && a[j]) {
961 if (a[i] == a[j]) {
962 j++;
963 continue;
964 }
965 a[++i] = a[j++];
966 }
967 if (a[i])
968 i++;
969 a[i] = 0;
970}
971
972
973void int_array_add_unique(int **res, int a)
974{
Hai Shalomfdcde762020-04-02 11:19:20 -0700975 size_t reslen, max_size;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800976 int *n;
977
978 for (reslen = 0; *res && (*res)[reslen]; reslen++) {
979 if ((*res)[reslen] == a)
980 return; /* already in the list */
981 }
982
Hai Shalomfdcde762020-04-02 11:19:20 -0700983 max_size = (size_t) -1;
984 if (reslen > max_size - 2) {
985 /* This should not really happen in practice, but if it did,
986 * something would overflow. Do not try to add the new value;
987 * instead, make this behave like memory allocation failure to
988 * avoid messing up memory. */
989 os_free(*res);
990 *res = NULL;
991 return;
992 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800993 n = os_realloc_array(*res, reslen + 2, sizeof(int));
994 if (n == NULL) {
995 os_free(*res);
996 *res = NULL;
997 return;
998 }
999
1000 n[reslen] = a;
1001 n[reslen + 1] = 0;
1002
1003 *res = n;
1004}
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001005
1006
1007void str_clear_free(char *str)
1008{
1009 if (str) {
1010 size_t len = os_strlen(str);
Hai Shalom81f62d82019-07-22 12:10:00 -07001011 forced_memzero(str, len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001012 os_free(str);
1013 }
1014}
1015
1016
1017void bin_clear_free(void *bin, size_t len)
1018{
1019 if (bin) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001020 forced_memzero(bin, len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001021 os_free(bin);
1022 }
1023}
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001024
1025
1026int random_mac_addr(u8 *addr)
1027{
1028 if (os_get_random(addr, ETH_ALEN) < 0)
1029 return -1;
1030 addr[0] &= 0xfe; /* unicast */
1031 addr[0] |= 0x02; /* locally administered */
1032 return 0;
1033}
1034
1035
1036int random_mac_addr_keep_oui(u8 *addr)
1037{
1038 if (os_get_random(addr + 3, 3) < 0)
1039 return -1;
1040 addr[0] &= 0xfe; /* unicast */
1041 addr[0] |= 0x02; /* locally administered */
1042 return 0;
1043}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001044
1045
1046/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001047 * cstr_token - Get next token from const char string
1048 * @str: a constant string to tokenize
1049 * @delim: a string of delimiters
1050 * @last: a pointer to a character following the returned token
1051 * It has to be set to NULL for the first call and passed for any
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001052 * further call.
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001053 * Returns: a pointer to token position in str or NULL
1054 *
1055 * This function is similar to str_token, but it can be used with both
1056 * char and const char strings. Differences:
1057 * - The str buffer remains unmodified
1058 * - The returned token is not a NULL terminated string, but a token
1059 * position in str buffer. If a return value is not NULL a size
1060 * of the returned token could be calculated as (last - token).
1061 */
1062const char * cstr_token(const char *str, const char *delim, const char **last)
1063{
1064 const char *end, *token = str;
1065
1066 if (!str || !delim || !last)
1067 return NULL;
1068
1069 if (*last)
1070 token = *last;
1071
1072 while (*token && os_strchr(delim, *token))
1073 token++;
1074
1075 if (!*token)
1076 return NULL;
1077
1078 end = token + 1;
1079
1080 while (*end && !os_strchr(delim, *end))
1081 end++;
1082
1083 *last = end;
1084 return token;
1085}
1086
1087
1088/**
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001089 * str_token - Get next token from a string
1090 * @buf: String to tokenize. Note that the string might be modified.
1091 * @delim: String of delimiters
1092 * @context: Pointer to save our context. Should be initialized with
1093 * NULL on the first call, and passed for any further call.
1094 * Returns: The next token, NULL if there are no more valid tokens.
1095 */
1096char * str_token(char *str, const char *delim, char **context)
1097{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001098 char *token = (char *) cstr_token(str, delim, (const char **) context);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001099
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001100 if (token && **context)
1101 *(*context)++ = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001102
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001103 return token;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001104}
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001105
1106
1107size_t utf8_unescape(const char *inp, size_t in_size,
1108 char *outp, size_t out_size)
1109{
1110 size_t res_size = 0;
1111
1112 if (!inp || !outp)
1113 return 0;
1114
1115 if (!in_size)
1116 in_size = os_strlen(inp);
1117
1118 /* Advance past leading single quote */
1119 if (*inp == '\'' && in_size) {
1120 inp++;
1121 in_size--;
1122 }
1123
Hai Shalom021b0b52019-04-10 11:17:58 -07001124 while (in_size) {
1125 in_size--;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001126 if (res_size >= out_size)
1127 return 0;
1128
1129 switch (*inp) {
1130 case '\'':
1131 /* Terminate on bare single quote */
1132 *outp = '\0';
1133 return res_size;
1134
1135 case '\\':
Hai Shalom021b0b52019-04-10 11:17:58 -07001136 if (!in_size)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001137 return 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001138 in_size--;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001139 inp++;
1140 /* fall through */
1141
1142 default:
1143 *outp++ = *inp++;
1144 res_size++;
1145 }
1146 }
1147
1148 /* NUL terminate if space allows */
1149 if (res_size < out_size)
1150 *outp = '\0';
1151
1152 return res_size;
1153}
1154
1155
1156size_t utf8_escape(const char *inp, size_t in_size,
1157 char *outp, size_t out_size)
1158{
1159 size_t res_size = 0;
1160
1161 if (!inp || !outp)
1162 return 0;
1163
1164 /* inp may or may not be NUL terminated, but must be if 0 size
1165 * is specified */
1166 if (!in_size)
1167 in_size = os_strlen(inp);
1168
Hai Shalom021b0b52019-04-10 11:17:58 -07001169 while (in_size) {
1170 in_size--;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001171 if (res_size++ >= out_size)
1172 return 0;
1173
1174 switch (*inp) {
1175 case '\\':
1176 case '\'':
1177 if (res_size++ >= out_size)
1178 return 0;
1179 *outp++ = '\\';
1180 /* fall through */
1181
1182 default:
1183 *outp++ = *inp++;
1184 break;
1185 }
1186 }
1187
1188 /* NUL terminate if space allows */
1189 if (res_size < out_size)
1190 *outp = '\0';
1191
1192 return res_size;
1193}
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001194
1195
1196int is_ctrl_char(char c)
1197{
1198 return c > 0 && c < 32;
1199}
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001200
1201
1202/**
1203 * ssid_parse - Parse a string that contains SSID in hex or text format
1204 * @buf: Input NULL terminated string that contains the SSID
1205 * @ssid: Output SSID
1206 * Returns: 0 on success, -1 otherwise
1207 *
1208 * The SSID has to be enclosed in double quotes for the text format or space
1209 * or NULL terminated string of hex digits for the hex format. buf can include
1210 * additional arguments after the SSID.
1211 */
1212int ssid_parse(const char *buf, struct wpa_ssid_value *ssid)
1213{
1214 char *tmp, *res, *end;
1215 size_t len;
1216
1217 ssid->ssid_len = 0;
1218
1219 tmp = os_strdup(buf);
1220 if (!tmp)
1221 return -1;
1222
1223 if (*tmp != '"') {
1224 end = os_strchr(tmp, ' ');
1225 if (end)
1226 *end = '\0';
1227 } else {
1228 end = os_strchr(tmp + 1, '"');
1229 if (!end) {
1230 os_free(tmp);
1231 return -1;
1232 }
1233
1234 end[1] = '\0';
1235 }
1236
1237 res = wpa_config_parse_string(tmp, &len);
1238 if (res && len <= SSID_MAX_LEN) {
1239 ssid->ssid_len = len;
1240 os_memcpy(ssid->ssid, res, len);
1241 }
1242
1243 os_free(tmp);
1244 os_free(res);
1245
1246 return ssid->ssid_len ? 0 : -1;
1247}
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001248
1249
1250int str_starts(const char *str, const char *start)
1251{
1252 return os_strncmp(str, start, os_strlen(start)) == 0;
1253}
Dmitry Shmidt29333592017-01-09 12:27:11 -08001254
1255
1256/**
1257 * rssi_to_rcpi - Convert RSSI to RCPI
1258 * @rssi: RSSI to convert
1259 * Returns: RCPI corresponding to the given RSSI value, or 255 if not available.
1260 *
1261 * It's possible to estimate RCPI based on RSSI in dBm. This calculation will
1262 * not reflect the correct value for high rates, but it's good enough for Action
1263 * frames which are transmitted with up to 24 Mbps rates.
1264 */
1265u8 rssi_to_rcpi(int rssi)
1266{
1267 if (!rssi)
1268 return 255; /* not available */
1269 if (rssi < -110)
1270 return 0;
1271 if (rssi > 0)
1272 return 220;
1273 return (rssi + 110) * 2;
1274}
Hai Shalom021b0b52019-04-10 11:17:58 -07001275
1276
1277char * get_param(const char *cmd, const char *param)
1278{
1279 const char *pos, *end;
1280 char *val;
1281 size_t len;
1282
1283 pos = os_strstr(cmd, param);
1284 if (!pos)
1285 return NULL;
1286
1287 pos += os_strlen(param);
1288 end = os_strchr(pos, ' ');
1289 if (end)
1290 len = end - pos;
1291 else
1292 len = os_strlen(pos);
1293 val = os_malloc(len + 1);
1294 if (!val)
1295 return NULL;
1296 os_memcpy(val, pos, len);
1297 val[len] = '\0';
1298 return val;
1299}
Hai Shalom81f62d82019-07-22 12:10:00 -07001300
1301
1302/* Try to prevent most compilers from optimizing out clearing of memory that
1303 * becomes unaccessible after this function is called. This is mostly the case
1304 * for clearing local stack variables at the end of a function. This is not
1305 * exactly perfect, i.e., someone could come up with a compiler that figures out
1306 * the pointer is pointing to memset and then end up optimizing the call out, so
1307 * try go a bit further by storing the first octet (now zero) to make this even
1308 * a bit more difficult to optimize out. Once memset_s() is available, that
1309 * could be used here instead. */
1310static void * (* const volatile memset_func)(void *, int, size_t) = memset;
1311static u8 forced_memzero_val;
1312
1313void forced_memzero(void *ptr, size_t len)
1314{
1315 memset_func(ptr, 0, len);
1316 if (len)
1317 forced_memzero_val = ((u8 *) ptr)[0];
1318}