• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * wpa_supplicant/hostapd / Debug prints
3  * Copyright (c) 2002-2013, 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 #include "includes.h"
10 #include <time.h>
11 
12 #include "common.h"
13 
14 #ifdef CONFIG_DEBUG_SYSLOG
15 #include <syslog.h>
16 #endif /* CONFIG_DEBUG_SYSLOG */
17 
18 #ifdef CONFIG_DEBUG_LINUX_TRACING
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 
26 static FILE *wpa_debug_tracing_file = NULL;
27 
28 #define WPAS_TRACE_PFX "wpas <%d>: "
29 #endif /* CONFIG_DEBUG_LINUX_TRACING */
30 
31 #define HIDDEN_CHAR '*'
32 int wpa_debug_level = MSG_DEBUG;
33 int wpa_debug_show_keys = 1;
34 int wpa_debug_timestamp = 0;
35 int wpa_debug_syslog = 0;
36 #ifndef CONFIG_NO_STDOUT_DEBUG
37 static FILE *out_file = NULL;
38 #endif /* CONFIG_NO_STDOUT_DEBUG */
39 
40 
41 #ifdef CONFIG_ANDROID_LOG
42 
43 #include <android/log.h>
44 
45 #ifndef ANDROID_LOG_NAME
46 #define ANDROID_LOG_NAME	"wpa_supplicant"
47 #endif /* ANDROID_LOG_NAME */
48 
wpa_to_android_level(int level)49 static int wpa_to_android_level(int level)
50 {
51 	if (level == MSG_ERROR)
52 		return ANDROID_LOG_ERROR;
53 	if (level == MSG_WARNING)
54 		return ANDROID_LOG_WARN;
55 	if (level == MSG_INFO)
56 		return ANDROID_LOG_INFO;
57 	return ANDROID_LOG_DEBUG;
58 }
59 
60 #endif /* CONFIG_ANDROID_LOG */
61 
62 #ifndef CONFIG_NO_STDOUT_DEBUG
63 
64 #ifdef CONFIG_DEBUG_FILE
65 #include <sys/types.h>
66 #include <sys/stat.h>
67 #include <fcntl.h>
68 #endif /* CONFIG_DEBUG_FILE */
69 #define WPA_MAX_ANONYMIZE_LENGTH 256
70 
71 #ifndef WPA_MAX_TOKEN_LEN
72 #define WPA_MAX_TOKEN_LEN 5
73 #endif /* WPA_MAX_TOKEN_LEN */
74 
75 
wpa_debug_print_timestamp(void)76 void wpa_debug_print_timestamp(void)
77 {
78 #ifndef CONFIG_ANDROID_LOG
79 	struct os_time tv;
80 
81 	if (!wpa_debug_timestamp)
82 		return;
83 
84 	os_get_time(&tv);
85 #ifdef CONFIG_DEBUG_FILE
86 	if (out_file)
87 		fprintf(out_file, "%ld.%06u: ", (long) tv.sec,
88 			(unsigned int) tv.usec);
89 #endif /* CONFIG_DEBUG_FILE */
90 	if (!out_file && !wpa_debug_syslog)
91 		printf("%ld.%06u: ", (long) tv.sec, (unsigned int) tv.usec);
92 #endif /* CONFIG_ANDROID_LOG */
93 }
94 
95 
96 #ifdef CONFIG_DEBUG_SYSLOG
97 #ifndef LOG_HOSTAPD
98 #define LOG_HOSTAPD LOG_DAEMON
99 #endif /* LOG_HOSTAPD */
100 
wpa_debug_open_syslog(void)101 void wpa_debug_open_syslog(void)
102 {
103 	openlog("wpa_supplicant", LOG_PID | LOG_NDELAY, LOG_HOSTAPD);
104 	wpa_debug_syslog++;
105 }
106 
107 
wpa_debug_close_syslog(void)108 void wpa_debug_close_syslog(void)
109 {
110 	if (wpa_debug_syslog)
111 		closelog();
112 }
113 
114 
syslog_priority(int level)115 static int syslog_priority(int level)
116 {
117 	switch (level) {
118 	case MSG_MSGDUMP:
119 	case MSG_DEBUG:
120 		return LOG_DEBUG;
121 	case MSG_INFO:
122 		return LOG_NOTICE;
123 	case MSG_WARNING:
124 		return LOG_WARNING;
125 	case MSG_ERROR:
126 		return LOG_ERR;
127 	}
128 	return LOG_INFO;
129 }
130 #endif /* CONFIG_DEBUG_SYSLOG */
131 
132 
133 #ifdef CONFIG_DEBUG_LINUX_TRACING
134 
wpa_debug_open_linux_tracing(void)135 int wpa_debug_open_linux_tracing(void)
136 {
137 	int mounts, trace_fd;
138 	char buf[4096] = {};
139 	ssize_t buflen;
140 	char *line, *tmp1, *path = NULL;
141 
142 	mounts = open("/proc/mounts", O_RDONLY);
143 	if (mounts < 0) {
144 		printf("no /proc/mounts\n");
145 		return -1;
146 	}
147 
148 	buflen = read(mounts, buf, sizeof(buf) - 1);
149 	close(mounts);
150 	if (buflen < 0) {
151 		printf("failed to read /proc/mounts\n");
152 		return -1;
153 	}
154 	buf[buflen] = '\0';
155 
156 	line = strtok_r(buf, "\n", &tmp1);
157 	while (line) {
158 		char *tmp2, *tmp_path, *fstype;
159 		/* "<dev> <mountpoint> <fs type> ..." */
160 		strtok_r(line, " ", &tmp2);
161 		tmp_path = strtok_r(NULL, " ", &tmp2);
162 		fstype = strtok_r(NULL, " ", &tmp2);
163 		if (fstype && strcmp(fstype, "debugfs") == 0) {
164 			path = tmp_path;
165 			break;
166 		}
167 
168 		line = strtok_r(NULL, "\n", &tmp1);
169 	}
170 
171 	if (path == NULL) {
172 		printf("debugfs mountpoint not found\n");
173 		return -1;
174 	}
175 
176 	snprintf(buf, sizeof(buf) - 1, "%s/tracing/trace_marker", path);
177 
178 	trace_fd = open(buf, O_WRONLY);
179 	if (trace_fd < 0) {
180 		printf("failed to open trace_marker file\n");
181 		return -1;
182 	}
183 	wpa_debug_tracing_file = fdopen(trace_fd, "w");
184 	if (wpa_debug_tracing_file == NULL) {
185 		close(trace_fd);
186 		printf("failed to fdopen()\n");
187 		return -1;
188 	}
189 
190 	return 0;
191 }
192 
193 
wpa_debug_close_linux_tracing(void)194 void wpa_debug_close_linux_tracing(void)
195 {
196 	if (wpa_debug_tracing_file == NULL)
197 		return;
198 	fclose(wpa_debug_tracing_file);
199 	wpa_debug_tracing_file = NULL;
200 }
201 
202 #endif /* CONFIG_DEBUG_LINUX_TRACING */
203 
204 #ifdef CONFIG_OPEN_HARMONY_PATCH
205 #include "hilog/log.h"
206 #include "parameter.h"
207 
208 #ifdef LOG_DOMAIN
209 #undef LOG_DOMAIN
210 #endif // LOG_DOMAIN
211 #ifdef LOG_TAG
212 #undef LOG_TAG
213 #endif // LOG_TAG
214 #define LOG_DOMAIN 0xD005200
215 #define LOG_TAG "wpa_supplicant"
216 #define WPA_MAX_LOG_CHAR 8196
217 #define WPA_PROP_KEY_DEBUG_ON "persist.sys.wpa_debug_on"
218 #define PARAM_VALUE_MAX_LEN 10
219 
220 enum {
221 	WPA_HILOG_UNKNOWN, WPA_HILOG_UNSET, WPA_HILOG_SET
222 };
223 
224 int32_t wpa_debug_hilog_switch = WPA_HILOG_UNKNOWN;
225 
wpa_can_hilog()226 static bool wpa_can_hilog()
227 {
228 	switch (wpa_debug_hilog_switch) {
229 		case WPA_HILOG_UNSET:
230 			return false;
231 		case WPA_HILOG_SET:
232 			return true;
233 		default:
234 			break;
235 	}
236 
237 	wpa_debug_hilog_switch = WPA_HILOG_SET;
238 	return true;
239 }
240 #endif // CONFIG_OPEN_HARMONY_PATCH
241 
is_char_hexadecimal(char * for_check)242 static int is_char_hexadecimal(char *for_check)
243 {
244 	if ((('0' <= *for_check) && ('9' >= *for_check)) || (('a' <= *for_check) && ('f' >= *for_check))
245 		|| (('A' <= *for_check) && ('F' >= *for_check))) {
246 		return 1;
247 	}
248 	return 0;
249 }
250 
is_symbol_logical(char * for_check,size_t i)251 static int is_symbol_logical(char *for_check, size_t i)
252 {
253 	const int macIndexThr = 3;
254 	const int macIndexSix = 6;
255 	const int macIndexNine = 9;
256 	const int macIndexTwelve = 12;
257 	if ((':' == *(for_check + i)) && (':' == *(for_check + i + macIndexThr))
258 		&& (':' == *(for_check + i + macIndexSix)) && (':' == *(for_check + i + macIndexNine))
259 	    && (':' == *(for_check + i + macIndexTwelve))) {
260 		return 1;
261 	}
262 	return 0;
263 }
264 
change_mac_address(char * input)265 static void change_mac_address(char *input)
266 {
267 	const int operandsThree = 3;
268 	const int operandsNine = 9;
269 	const int operandsFourteen = 14;
270 	const int operandsFifteen = 15;
271 	const int macMaxLen = 17;
272 	if (input == NULL) {
273 		return;
274 	}
275 	size_t len = strlen(input);
276 	if (len < macMaxLen) {
277 		return;
278 	}
279 	size_t i = 2;
280 	while ('\0' != *(input + i + operandsFourteen)) {
281 		int is_mac_address = 1;
282 		if (is_symbol_logical(input, i)) {
283 			int j = -2;
284 			while (operandsFifteen != j) {
285 				if (0 == j % operandsThree) {
286 					++j;
287 					continue;
288 				}
289 				if (is_char_hexadecimal(input + i + j)) {
290 					++j;
291 					continue;
292 				} else {
293 					++j;
294 					is_mac_address = 0;
295 					break;
296 				}
297 			}
298 		} else {
299 			is_mac_address = 0;
300 		}
301 		if (!is_mac_address) {
302 			++i;
303 		} else {
304 			int m =1;
305 			while (operandsNine != m) {
306 				if (0 == m % operandsThree) {
307 					++m;
308 					continue;
309 				} else {
310 					*(input + i + m) = '*';
311 					++m;
312 				}
313 			}
314 			if (i + macMaxLen + operandsFifteen > len) {
315 				break;
316 			} else {
317 				i += macMaxLen;
318 			}
319 		}
320 	}
321 	return;
322 }
323 
324 /**
325  * wpa_printf - conditional printf
326  * @level: priority level (MSG_*) of the message
327  * @fmt: printf format string, followed by optional arguments
328  *
329  * This function is used to print conditional debugging and error messages. The
330  * output may be directed to stdout, stderr, and/or syslog based on
331  * configuration.
332  *
333  * Note: New line '\n' is added to the end of the text when printing to stdout.
334  */
wpa_printf(int level,const char * fmt,...)335 void wpa_printf(int level, const char *fmt, ...)
336 {
337 #ifdef CONFIG_OPEN_HARMONY_PATCH
338 	if (wpa_can_hilog()) {
339 		int32_t ulPos = 0;
340 		char szStr[WPA_MAX_LOG_CHAR] = {0};
341 		va_list arg = {0};
342 		int32_t ret;
343 
344 		va_start(arg, fmt);
345 		ret = vsprintf(&szStr[ulPos], fmt, arg);
346 		if (!disable_anonymized_print()) {
347 			change_mac_address(szStr);
348 		}
349 		va_end(arg);
350 		if (ret > 0) {
351 			switch (level) {
352 				case MSG_ERROR:
353 					HILOG_ERROR(LOG_CORE, "%{public}s", szStr);
354 					break;
355 				case MSG_WARNING:
356 					HILOG_WARN(LOG_CORE, "%{public}s", szStr);
357 					break;
358 				case MSG_INFO:
359 					HILOG_INFO(LOG_CORE, "%{public}s", szStr);
360 					break;
361 				default:
362 					HILOG_DEBUG(LOG_CORE, "%{public}s", szStr);
363 					break;
364 			}
365 		}
366 		return;
367 	}
368 #endif
369 
370 #ifdef CONFIG_WPA_NO_LOG
371     return;
372 #else
373 	va_list ap;
374 
375 	if (level >= wpa_debug_level) {
376 #ifdef CONFIG_ANDROID_LOG
377 		va_start(ap, fmt);
378 		__android_log_vprint(wpa_to_android_level(level),
379 				     ANDROID_LOG_NAME, fmt, ap);
380 		va_end(ap);
381 #else /* CONFIG_ANDROID_LOG */
382 #ifdef CONFIG_DEBUG_SYSLOG
383 		if (wpa_debug_syslog) {
384 			va_start(ap, fmt);
385 			vsyslog(syslog_priority(level), fmt, ap);
386 			va_end(ap);
387 		}
388 #endif /* CONFIG_DEBUG_SYSLOG */
389 		wpa_debug_print_timestamp();
390 #ifdef CONFIG_DEBUG_FILE
391 		if (out_file) {
392 			va_start(ap, fmt);
393 			vfprintf(out_file, fmt, ap);
394 			fprintf(out_file, "\n");
395 			va_end(ap);
396 		}
397 #endif /* CONFIG_DEBUG_FILE */
398 		if (!wpa_debug_syslog && !out_file) {
399 			va_start(ap, fmt);
400 			vprintf(fmt, ap);
401 			printf("\n");
402 			va_end(ap);
403 		}
404 #endif /* CONFIG_ANDROID_LOG */
405 	}
406 
407 #ifdef CONFIG_DEBUG_LINUX_TRACING
408 	if (wpa_debug_tracing_file != NULL) {
409 		va_start(ap, fmt);
410 		fprintf(wpa_debug_tracing_file, WPAS_TRACE_PFX, level);
411 		vfprintf(wpa_debug_tracing_file, fmt, ap);
412 		fprintf(wpa_debug_tracing_file, "\n");
413 		fflush(wpa_debug_tracing_file);
414 		va_end(ap);
415 	}
416 #endif /* CONFIG_DEBUG_LINUX_TRACING */
417 #endif /* CONFIG_WPA_NO_LOG */
418 }
419 
420 
_wpa_hexdump(int level,const char * title,const u8 * buf,size_t len,int show,int only_syslog)421 static void _wpa_hexdump(int level, const char *title, const u8 *buf,
422 			 size_t len, int show, int only_syslog)
423 {
424 #ifdef CONFIG_WPA_NO_LOG
425     return;
426 #else
427 	size_t i;
428 #ifdef CONFIG_OPEN_HARMONY_PATCH
429 	if (wpa_can_hilog()) {
430 		const char *display;
431 		char *strbuf = NULL;
432 		size_t slen = len;
433 		if (buf == NULL) {
434 			display = " [NULL]";
435 		} else if (len == 0) {
436 			display = "";
437 		} else if (show && len) {
438 			if (slen > 32)
439 				slen = 32;
440 			strbuf = os_malloc(1 + 3 * slen);
441 			if (strbuf == NULL) {
442 				wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
443 				                      "allocate message buffer");
444 				return;
445 			}
446 
447 			for (i = 0; i < slen; i++)
448 				os_snprintf(&strbuf[i * 3], 4, " %02x",
449 				            buf[i]);
450 
451 			display = strbuf;
452 		} else {
453 			display = " [REMOVED]";
454 		}
455 		switch (level) {
456 			case MSG_ERROR:
457 				HILOG_ERROR(LOG_CORE, "%{public}s - hexdump(len=%{public}lu):%{public}s%{public}s",
458 					title, (long unsigned int) len, display, len > slen ? " ..." : "");
459 				break;
460 			case MSG_WARNING:
461 				HILOG_WARN(LOG_CORE, "%{public}s - hexdump(len=%{public}lu):%{public}s%{public}s",
462 					title, (long unsigned int) len, display, len > slen ? " ..." : "");
463 				break;
464 			case MSG_INFO:
465 				HILOG_INFO(LOG_CORE, "%{public}s - hexdump(len=%{public}lu):%{public}s%{public}s",
466 					title, (long unsigned int) len, display, len > slen ? " ..." : "");
467 				break;
468 			default:
469 				HILOG_DEBUG(LOG_CORE, "%{public}s - hexdump(len=%{public}lu):%{public}s%{public}s",
470 					title, (long unsigned int) len, display, len > slen ? " ..." : "");
471 				break;
472 		}
473 		bin_clear_free(strbuf, 1 + 3 * slen);
474 		return;
475 	}
476 #endif
477 
478 #ifdef CONFIG_DEBUG_LINUX_TRACING
479 	if (wpa_debug_tracing_file != NULL) {
480 		fprintf(wpa_debug_tracing_file,
481 			WPAS_TRACE_PFX "%s - hexdump(len=%lu):",
482 			level, title, (unsigned long) len);
483 		if (buf == NULL) {
484 			fprintf(wpa_debug_tracing_file, " [NULL]\n");
485 		} else if (!show) {
486 			fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
487 		} else {
488 			for (i = 0; i < len; i++)
489 				fprintf(wpa_debug_tracing_file,
490 					" %02x", buf[i]);
491 		}
492 		fflush(wpa_debug_tracing_file);
493 	}
494 #endif /* CONFIG_DEBUG_LINUX_TRACING */
495 
496 	if (level < wpa_debug_level)
497 		return;
498 #ifdef CONFIG_ANDROID_LOG
499 	{
500 		const char *display;
501 		char *strbuf = NULL;
502 		size_t slen = len;
503 		if (buf == NULL) {
504 			display = " [NULL]";
505 		} else if (len == 0) {
506 			display = "";
507 		} else if (show && len) {
508 			/* Limit debug message length for Android log */
509 			if (slen > 32)
510 				slen = 32;
511 			strbuf = os_malloc(1 + 3 * slen);
512 			if (strbuf == NULL) {
513 				wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
514 					   "allocate message buffer");
515 				return;
516 			}
517 
518 			for (i = 0; i < slen; i++)
519 				os_snprintf(&strbuf[i * 3], 4, " %02x",
520 					    buf[i]);
521 
522 			display = strbuf;
523 		} else {
524 			display = " [REMOVED]";
525 		}
526 
527 		__android_log_print(wpa_to_android_level(level),
528 				    ANDROID_LOG_NAME,
529 				    "%s - hexdump(len=%lu):%s%s",
530 				    title, (long unsigned int) len, display,
531 				    len > slen ? " ..." : "");
532 		bin_clear_free(strbuf, 1 + 3 * slen);
533 		return;
534 	}
535 #else /* CONFIG_ANDROID_LOG */
536 #ifdef CONFIG_DEBUG_SYSLOG
537 	if (wpa_debug_syslog) {
538 		const char *display;
539 		char *strbuf = NULL;
540 
541 		if (buf == NULL) {
542 			display = " [NULL]";
543 		} else if (len == 0) {
544 			display = "";
545 		} else if (show && len) {
546 			strbuf = os_malloc(1 + 3 * len);
547 			if (strbuf == NULL) {
548 				wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
549 					   "allocate message buffer");
550 				return;
551 			}
552 
553 			for (i = 0; i < len; i++)
554 				os_snprintf(&strbuf[i * 3], 4, " %02x",
555 					    buf[i]);
556 
557 			display = strbuf;
558 		} else {
559 			display = " [REMOVED]";
560 		}
561 
562 		syslog(syslog_priority(level), "%s - hexdump(len=%lu):%s",
563 		       title, (unsigned long) len, display);
564 		bin_clear_free(strbuf, 1 + 3 * len);
565 		if (only_syslog)
566 			return;
567 	}
568 #endif /* CONFIG_DEBUG_SYSLOG */
569 	wpa_debug_print_timestamp();
570 #ifdef CONFIG_DEBUG_FILE
571 	if (out_file) {
572 		fprintf(out_file, "%s - hexdump(len=%lu):",
573 			title, (unsigned long) len);
574 		if (buf == NULL) {
575 			fprintf(out_file, " [NULL]");
576 		} else if (show) {
577 			for (i = 0; i < len; i++)
578 				fprintf(out_file, " %02x", buf[i]);
579 		} else {
580 			fprintf(out_file, " [REMOVED]");
581 		}
582 		fprintf(out_file, "\n");
583 	}
584 #endif /* CONFIG_DEBUG_FILE */
585 	if (!wpa_debug_syslog && !out_file) {
586 		printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
587 		if (buf == NULL) {
588 			printf(" [NULL]");
589 		} else if (show) {
590 			for (i = 0; i < len; i++)
591 				printf(" %02x", buf[i]);
592 		} else {
593 			printf(" [REMOVED]");
594 		}
595 		printf("\n");
596 	}
597 #endif /* CONFIG_ANDROID_LOG */
598 #endif /* CONFIG_WPA_NO_LOG */
599 }
600 
disable_anonymized_print()601 int disable_anonymized_print()
602 {
603 	char prop[PARAM_VALUE_MAX_LEN] = { 0 };
604 	if (GetParameter(WPA_PROP_KEY_DEBUG_ON, "0", prop, sizeof(prop)) > 0) {
605 		if (atoi(prop) > 0) {
606 			return 1;
607 		}
608 	}
609 	return 0;
610 }
611 
get_realtime_microsecond()612 long get_realtime_microsecond()
613 {
614 	struct timespec ts = {0};
615 
616 	clock_gettime(CLOCK_REALTIME, &ts);
617 	long microseconds = ts.tv_nsec / 1000;
618 	return microseconds;
619 }
620 
anonymize_ssid(const char * str)621 const char *anonymize_ssid(const char *str)
622 {
623 	if (str == NULL || *str == '\0') {
624 		return str;
625 	}
626 
627 	static char s[WPA_MAX_ANONYMIZE_LENGTH];
628 	int strLen = os_strlen(str);
629 	os_strlcpy(s, str, sizeof(s));
630 
631 	if (disable_anonymized_print()) {
632 		return s;
633 	}
634 	const char hiddenChar = HIDDEN_CHAR;
635 	const int minHiddenSize = 3;
636 	const int headKeepSize = 3;
637 	const int tailKeepSize = 3;
638 
639 	if (strLen < minHiddenSize) {
640 		os_memset(s, hiddenChar, strLen);
641 		return s;
642 	}
643 
644 	if (strLen < minHiddenSize + headKeepSize + tailKeepSize) {
645 		int beginIndex = 1;
646 		int hiddenSize = strLen - minHiddenSize + 1;
647 		hiddenSize = hiddenSize > minHiddenSize ? minHiddenSize : hiddenSize;
648 		os_memset(s + beginIndex, hiddenChar, hiddenSize);
649 		return s;
650 	}
651 	os_memset(s + headKeepSize, hiddenChar, strLen - headKeepSize - tailKeepSize);
652 	return s;
653 }
654 
anonymize_token(const u8 num)655 const char *anonymize_token(const u8 num)
656 {
657     int res;
658     static char str[WPA_MAX_TOKEN_LEN] = { 0 };
659     res = os_snprintf(str, WPA_MAX_TOKEN_LEN, "%u", num);
660     if (os_snprintf_error(WPA_MAX_TOKEN_LEN, res)) {
661         wpa_printf(MSG_ERROR, "anonymize_token: Failed %d", res);
662         return str;
663     }
664     return anonymize_common(str);
665 }
666 
anonymize_common(const char * str)667 const char *anonymize_common(const char *str)
668 {
669 	static char temp[WPA_MAX_ANONYMIZE_LENGTH] = { 0 };
670 
671 	if (str == NULL || *str == '\0') {
672 		return temp;
673 	}
674 	int strLen = os_strlen(str);
675 	os_strlcpy(temp, str, sizeof(temp));
676 
677 	if (disable_anonymized_print()) {
678 		return temp;
679 	}
680 	const char hiddenChar = HIDDEN_CHAR;
681 	const int minHiddenSize = 3;
682 	const int headKeepSize = 3;
683 	const int tailKeepSize = 3;
684 
685 	if (strLen < minHiddenSize) {
686 		os_memset(temp, hiddenChar, strLen);
687 		return temp;
688 	}
689 
690 	if (strLen < minHiddenSize + headKeepSize + tailKeepSize) {
691 		int beginIndex = 1;
692 		int hiddenSize = strLen - minHiddenSize + 1;
693 		hiddenSize = hiddenSize > minHiddenSize ? minHiddenSize : hiddenSize;
694 		os_memset(temp + beginIndex, hiddenChar, hiddenSize);
695 		return temp;
696 	}
697 	os_memset(temp + headKeepSize, hiddenChar, strLen - headKeepSize - tailKeepSize);
698 	return temp;
699 }
700 
anonymize_ip(const char * str)701 const char *anonymize_ip(const char *str)
702 {
703 	if (str == NULL || *str == '\0') {
704 		return str;
705 	}
706 	int colonCount = 0;
707 	const int maxDisplayNum = 2;
708 	static char s[40];
709 	int start = 0;
710 	os_strlcpy(s, str, sizeof(s));
711 	if (disable_anonymized_print()) {
712 		return s;
713 	}
714 	// ipv4 or ipv6 anonymize
715 	for (int i = 0; i < os_strlen(s); i++) {
716 		if (s[i] == ':' || s[i] == '.') {
717 			colonCount++;
718 			if (colonCount == maxDisplayNum) {
719 				start = i + 1;
720 			}
721 		}
722 	}
723 	for (int j = start; j < os_strlen(s); j++) {
724 		if (s[j] != ':' && s[j] != '.') {
725 			s[j] = HIDDEN_CHAR;
726 		}
727 	}
728 	return s;
729 }
730 
731 
get_anonymized_result_setnetwork(const char * str)732 const char *get_anonymized_result_setnetwork(const char *str)
733 {
734 	if (str == NULL || *str == '\0') {
735 		return str;
736 	}
737 	static char cmd[WPA_MAX_ANONYMIZE_LENGTH];
738 	os_strlcpy(cmd, str, sizeof(cmd));
739 	if (disable_anonymized_print()) {
740 		return cmd;
741 	}
742 	// cmd include ssid or identity
743 	if (os_strchr(cmd, '\"') && (os_strstr(cmd, "ssid") || os_strstr(cmd, "identity"))) {
744 		char tempssid[WPA_MAX_ANONYMIZE_LENGTH];
745 		os_strlcpy(tempssid, os_strchr(cmd, '\"') + 1, sizeof(tempssid));
746 		tempssid[os_strrchr(cmd, '\"') - os_strchr(cmd, '\"') - 1] = '\0';
747 		static char tempStr[WPA_MAX_ANONYMIZE_LENGTH];
748 		char *strOfStrtok = strtok(cmd, "\"");
749 		if (strOfStrtok == NULL) {
750 			return cmd;
751 		}
752 		os_strlcpy(tempStr, strOfStrtok, sizeof(tempStr));
753 		os_snprintf(cmd, sizeof(cmd), "%s\"%s\"", tempStr, anonymize_ssid(tempssid));
754 		return cmd;
755 	}
756 	//cmd include password or psk
757 	if (os_strchr(cmd, '\"') && (os_strstr(cmd, "password") || os_strstr(cmd, "psk"))) {
758 		char tempNumbel[WPA_MAX_ANONYMIZE_LENGTH];
759 		os_strlcpy(tempNumbel, os_strchr(cmd, '\"') + 1, sizeof(tempNumbel));
760 		tempNumbel[os_strrchr(cmd, '\"') - os_strchr(cmd, '\"') - 1] = '\0';
761 		for (int i = 0; i < os_strlen(tempNumbel); i++) {
762 			tempNumbel[i] = HIDDEN_CHAR;
763 		}
764 		static char tempStr[WPA_MAX_ANONYMIZE_LENGTH];
765 		char *strOfStrtok = strtok(cmd, "\"");
766 		if (strOfStrtok == NULL) {
767 			return cmd;
768 		}
769 		os_strlcpy(tempStr, strOfStrtok, sizeof(tempStr));
770 		os_snprintf(cmd, sizeof(cmd), "%s\"%s\"", tempStr, tempNumbel);
771 		os_memset(tempNumbel, 0, sizeof(tempNumbel));
772 		return cmd;
773 	}
774 	return cmd;
775 }
776 
get_anonymized_result_setnetwork_for_bssid(const char * str)777 const char *get_anonymized_result_setnetwork_for_bssid(const char *str)
778 {
779 	if (str == NULL || *str == '\0') {
780 		return str;
781 	}
782 	static const int colonCountNum = 2;
783 	static const int maxHiddenNum = 9;
784 	static char cmd[WPA_MAX_ANONYMIZE_LENGTH];
785 	os_strlcpy(cmd, str, sizeof(cmd));
786 	if (disable_anonymized_print()) {
787 		return cmd;
788 	}
789 	//cmd include bssid
790 	if (os_strchr(cmd, ':')) {
791 		int colonCount = 0;
792 		int start = 0;
793 		for (int j = 0; j < os_strlen(cmd); j++) {
794 			if (cmd[j] == ':') {
795 				colonCount++;
796 			}
797 			if (colonCount == colonCountNum) {
798 				start = j + 1;
799 				break;
800 			}
801 		}
802 		if (colonCount != colonCountNum) {
803 			return cmd;
804 		}
805 		for (int k = start; k < start + maxHiddenNum; k++) {
806 			if (cmd[k] != ':') {
807 				cmd[k] = HIDDEN_CHAR;
808 			}
809 		}
810 		return cmd;
811 	}
812 	return cmd;
813 }
814 
get_anonymized_result_for_set(const char * str)815 const char *get_anonymized_result_for_set(const char *str)
816 {
817 	if (str == NULL || *str == '\0') {
818 		return str;
819 	}
820 	static char cmd[WPA_MAX_ANONYMIZE_LENGTH];
821 	os_strlcpy(cmd, str, sizeof(cmd));
822 	if (disable_anonymized_print()) {
823 		return cmd;
824 	}
825 	if (os_strstr(cmd, "wpa_passphrase")) {
826 		char *value = os_strchr(cmd, ' ') + 1;
827 		if (value == NULL) {
828 			return cmd;
829 		}
830 		os_memset(value, HIDDEN_CHAR, os_strlen(value));
831 		return cmd;
832 	} else if (os_strstr(cmd, "ssid")) {
833 		char *value = os_strchr(cmd, ' ') + 1;
834 		os_snprintf(cmd, sizeof(cmd), "ssid=%s", anonymize_ssid(value));
835 		return cmd;
836 	} else if (os_strstr(cmd, "P2P_CONNECT")) {
837 		char *value = os_strchr(cmd, ' ') + 1;
838 		if (value == NULL) {
839 			return cmd;
840 		}
841 		os_snprintf(cmd, sizeof(cmd), "P2P_CONNECT=%s", get_anonymized_result_setnetwork_for_bssid(value));
842 		return cmd;
843 	}
844 	return cmd;
845 }
846 
wpa_hexdump(int level,const char * title,const void * buf,size_t len)847 void wpa_hexdump(int level, const char *title, const void *buf, size_t len)
848 {
849 	_wpa_hexdump(level, title, buf, len, 1, 0);
850 }
851 
852 
wpa_hexdump_key(int level,const char * title,const void * buf,size_t len)853 void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len)
854 {
855 	_wpa_hexdump(level, title, buf, len, wpa_debug_show_keys, 0);
856 }
857 
858 
_wpa_hexdump_ascii(int level,const char * title,const void * buf,size_t len,int show)859 static void _wpa_hexdump_ascii(int level, const char *title, const void *buf,
860 			       size_t len, int show)
861 {
862 #ifdef CONFIG_WPA_NO_LOG
863     return;
864 #else
865 	size_t i, llen;
866 	const u8 *pos = buf;
867 	const size_t line_len = 16;
868 
869 #ifdef CONFIG_DEBUG_LINUX_TRACING
870 	if (wpa_debug_tracing_file != NULL) {
871 		fprintf(wpa_debug_tracing_file,
872 			WPAS_TRACE_PFX "%s - hexdump_ascii(len=%lu):",
873 			level, title, (unsigned long) len);
874 		if (buf == NULL) {
875 			fprintf(wpa_debug_tracing_file, " [NULL]\n");
876 		} else if (!show) {
877 			fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
878 		} else {
879 			/* can do ascii processing in userspace */
880 			for (i = 0; i < len; i++)
881 				fprintf(wpa_debug_tracing_file,
882 					" %02x", pos[i]);
883 		}
884 		fflush(wpa_debug_tracing_file);
885 	}
886 #endif /* CONFIG_DEBUG_LINUX_TRACING */
887 
888 	if (level < wpa_debug_level)
889 		return;
890 #ifdef CONFIG_ANDROID_LOG
891 	_wpa_hexdump(level, title, buf, len, show, 0);
892 #else /* CONFIG_ANDROID_LOG */
893 #ifdef CONFIG_DEBUG_SYSLOG
894 	if (wpa_debug_syslog)
895 		_wpa_hexdump(level, title, buf, len, show, 1);
896 #endif /* CONFIG_DEBUG_SYSLOG */
897 	wpa_debug_print_timestamp();
898 #ifdef CONFIG_DEBUG_FILE
899 	if (out_file) {
900 		if (!show) {
901 			fprintf(out_file,
902 				"%s - hexdump_ascii(len=%lu): [REMOVED]\n",
903 				title, (unsigned long) len);
904 			goto file_done;
905 		}
906 		if (buf == NULL) {
907 			fprintf(out_file,
908 				"%s - hexdump_ascii(len=%lu): [NULL]\n",
909 				title, (unsigned long) len);
910 			goto file_done;
911 		}
912 		fprintf(out_file, "%s - hexdump_ascii(len=%lu):\n",
913 			title, (unsigned long) len);
914 		while (len) {
915 			llen = len > line_len ? line_len : len;
916 			fprintf(out_file, "    ");
917 			for (i = 0; i < llen; i++)
918 				fprintf(out_file, " %02x", pos[i]);
919 			for (i = llen; i < line_len; i++)
920 				fprintf(out_file, "   ");
921 			fprintf(out_file, "   ");
922 			for (i = 0; i < llen; i++) {
923 				if (isprint(pos[i]))
924 					fprintf(out_file, "%c", pos[i]);
925 				else
926 					fprintf(out_file, "_");
927 			}
928 			for (i = llen; i < line_len; i++)
929 				fprintf(out_file, " ");
930 			fprintf(out_file, "\n");
931 			pos += llen;
932 			len -= llen;
933 		}
934 	}
935 file_done:
936 #endif /* CONFIG_DEBUG_FILE */
937 	if (!wpa_debug_syslog && !out_file) {
938 		if (!show) {
939 			printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
940 			       title, (unsigned long) len);
941 			return;
942 		}
943 		if (buf == NULL) {
944 			printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
945 			       title, (unsigned long) len);
946 			return;
947 		}
948 		printf("%s - hexdump_ascii(len=%lu):\n", title,
949 		       (unsigned long) len);
950 		while (len) {
951 			llen = len > line_len ? line_len : len;
952 			printf("    ");
953 			for (i = 0; i < llen; i++)
954 				printf(" %02x", pos[i]);
955 			for (i = llen; i < line_len; i++)
956 				printf("   ");
957 			printf("   ");
958 			for (i = 0; i < llen; i++) {
959 				if (isprint(pos[i]))
960 					printf("%c", pos[i]);
961 				else
962 					printf("_");
963 			}
964 			for (i = llen; i < line_len; i++)
965 				printf(" ");
966 			printf("\n");
967 			pos += llen;
968 			len -= llen;
969 		}
970 	}
971 #endif /* CONFIG_ANDROID_LOG */
972 #endif /* CONFIG_WPA_NO_LOG */
973 }
974 
975 
wpa_hexdump_ascii(int level,const char * title,const void * buf,size_t len)976 void wpa_hexdump_ascii(int level, const char *title, const void *buf,
977 		       size_t len)
978 {
979 	_wpa_hexdump_ascii(level, title, buf, len, 1);
980 }
981 
982 
wpa_hexdump_ascii_key(int level,const char * title,const void * buf,size_t len)983 void wpa_hexdump_ascii_key(int level, const char *title, const void *buf,
984 			   size_t len)
985 {
986 	_wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
987 }
988 
989 
990 #ifdef CONFIG_DEBUG_FILE
991 static char *last_path = NULL;
992 #endif /* CONFIG_DEBUG_FILE */
993 
wpa_debug_reopen_file(void)994 int wpa_debug_reopen_file(void)
995 {
996 #ifdef CONFIG_DEBUG_FILE
997 	int rv;
998 	char *tmp;
999 
1000 	if (!last_path)
1001 		return 0; /* logfile not used */
1002 
1003 	tmp = os_strdup(last_path);
1004 	if (!tmp)
1005 		return -1;
1006 
1007 	wpa_debug_close_file();
1008 	rv = wpa_debug_open_file(tmp);
1009 	os_free(tmp);
1010 	return rv;
1011 #else /* CONFIG_DEBUG_FILE */
1012 	return 0;
1013 #endif /* CONFIG_DEBUG_FILE */
1014 }
1015 
1016 
wpa_debug_open_file(const char * path)1017 int wpa_debug_open_file(const char *path)
1018 {
1019 #ifdef CONFIG_DEBUG_FILE
1020 	int out_fd;
1021 
1022 	if (!path)
1023 		return 0;
1024 
1025 	if (last_path == NULL || os_strcmp(last_path, path) != 0) {
1026 		/* Save our path to enable re-open */
1027 		os_free(last_path);
1028 		last_path = os_strdup(path);
1029 	}
1030 
1031 	out_fd = open(path, O_CREAT | O_APPEND | O_WRONLY,
1032 		      S_IRUSR | S_IWUSR | S_IRGRP);
1033 	if (out_fd < 0) {
1034 		wpa_printf(MSG_ERROR,
1035 			   "%s: Failed to open output file descriptor, using standard output",
1036 			   __func__);
1037 		return -1;
1038 	}
1039 
1040 #ifdef __linux__
1041 	if (fcntl(out_fd, F_SETFD, FD_CLOEXEC) < 0) {
1042 		wpa_printf(MSG_DEBUG,
1043 			   "%s: Failed to set FD_CLOEXEC - continue without: %s",
1044 			   __func__, strerror(errno));
1045 	}
1046 #endif /* __linux__ */
1047 
1048 	out_file = fdopen(out_fd, "a");
1049 	if (out_file == NULL) {
1050 		wpa_printf(MSG_ERROR, "wpa_debug_open_file: Failed to open "
1051 			   "output file, using standard output");
1052 		close(out_fd);
1053 		return -1;
1054 	}
1055 #ifndef _WIN32
1056 	setvbuf(out_file, NULL, _IOLBF, 0);
1057 #endif /* _WIN32 */
1058 #else /* CONFIG_DEBUG_FILE */
1059 	(void)path;
1060 #endif /* CONFIG_DEBUG_FILE */
1061 	return 0;
1062 }
1063 
1064 
wpa_debug_stop_log(void)1065 void wpa_debug_stop_log(void)
1066 {
1067 #ifdef CONFIG_DEBUG_FILE
1068 	if (!out_file)
1069 		return;
1070 	fclose(out_file);
1071 	out_file = NULL;
1072 #endif /* CONFIG_DEBUG_FILE */
1073 }
1074 
1075 
wpa_debug_close_file(void)1076 void wpa_debug_close_file(void)
1077 {
1078 #ifdef CONFIG_DEBUG_FILE
1079 	wpa_debug_stop_log();
1080 	os_free(last_path);
1081 	last_path = NULL;
1082 #endif /* CONFIG_DEBUG_FILE */
1083 }
1084 
1085 
wpa_debug_setup_stdout(void)1086 void wpa_debug_setup_stdout(void) __attribute__((no_sanitize("cfi")))
1087 {
1088 #ifndef _WIN32
1089 	setvbuf(stdout, NULL, _IOLBF, 0);
1090 #endif /* _WIN32 */
1091 }
1092 
1093 #endif /* CONFIG_NO_STDOUT_DEBUG */
1094 
1095 
1096 #ifndef CONFIG_NO_WPA_MSG
1097 static wpa_msg_cb_func wpa_msg_cb = NULL;
1098 
wpa_msg_register_cb(wpa_msg_cb_func func)1099 void wpa_msg_register_cb(wpa_msg_cb_func func)
1100 {
1101 	wpa_msg_cb = func;
1102 }
1103 
1104 
1105 static wpa_msg_get_ifname_func wpa_msg_ifname_cb = NULL;
1106 
wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)1107 void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)
1108 {
1109 	wpa_msg_ifname_cb = func;
1110 }
1111 
1112 
wpa_msg(void * ctx,int level,const char * fmt,...)1113 void wpa_msg(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
1114 {
1115 	va_list ap;
1116 	char *buf;
1117 	int buflen;
1118 	int len;
1119 	char prefix[130];
1120 
1121 	va_start(ap, fmt);
1122 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1123 	va_end(ap);
1124 
1125 	buf = os_malloc(buflen);
1126 	if (buf == NULL) {
1127 		wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
1128 			   "buffer");
1129 		return;
1130 	}
1131 	va_start(ap, fmt);
1132 	prefix[0] = '\0';
1133 	if (wpa_msg_ifname_cb) {
1134 		const char *ifname = wpa_msg_ifname_cb(ctx);
1135 		if (ifname) {
1136 			int res = os_snprintf(prefix, sizeof(prefix), "%s: ",
1137 					      ifname);
1138 			if (os_snprintf_error(sizeof(prefix), res))
1139 				prefix[0] = '\0';
1140 		}
1141 	}
1142 	len = vsnprintf(buf, buflen, fmt, ap);
1143 	va_end(ap);
1144 	wpa_printf(level, "%s%s", prefix, get_anonymized_result_setnetwork_for_bssid(buf));
1145 	if (wpa_msg_cb)
1146 		wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
1147 	bin_clear_free(buf, buflen);
1148 }
1149 
wpa_msg_only_for_cb(void * ctx,int level,const char * fmt,...)1150 void wpa_msg_only_for_cb(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
1151 {
1152 	va_list ap;
1153 	char *buf;
1154 	int buflen;
1155 	int len;
1156 	char prefix[130];
1157 
1158 	va_start(ap, fmt);
1159 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1160 	va_end(ap);
1161 
1162 	buf = os_malloc(buflen);
1163 	if (buf == NULL) {
1164 		wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
1165 			"buffer");
1166 		return;
1167 	}
1168 	va_start(ap, fmt);
1169 	prefix[0] = '\0';
1170 	if (wpa_msg_ifname_cb) {
1171 		const char *ifname = wpa_msg_ifname_cb(ctx);
1172 		if (ifname) {
1173 			int res = os_snprintf(prefix, sizeof(prefix), "%s: ",
1174 				ifname);
1175 			if (os_snprintf_error(sizeof(prefix), res))
1176 				prefix[0] = '\0';
1177 		}
1178 	}
1179 	len = vsnprintf(buf, buflen, fmt, ap);
1180 	va_end(ap);
1181 	if (wpa_msg_cb)
1182 		wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
1183 	bin_clear_free(buf, buflen);
1184 }
1185 
1186 
wpa_msg_ctrl(void * ctx,int level,const char * fmt,...)1187 void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
1188 {
1189 	va_list ap;
1190 	char *buf;
1191 	int buflen;
1192 	int len;
1193 
1194 	if (!wpa_msg_cb)
1195 		return;
1196 
1197 	va_start(ap, fmt);
1198 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1199 	va_end(ap);
1200 
1201 	buf = os_malloc(buflen);
1202 	if (buf == NULL) {
1203 		wpa_printf(MSG_ERROR, "wpa_msg_ctrl: Failed to allocate "
1204 			   "message buffer");
1205 		return;
1206 	}
1207 	va_start(ap, fmt);
1208 	len = vsnprintf(buf, buflen, fmt, ap);
1209 	va_end(ap);
1210 	wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
1211 	bin_clear_free(buf, buflen);
1212 }
1213 
1214 
wpa_msg_global(void * ctx,int level,const char * fmt,...)1215 void wpa_msg_global(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
1216 {
1217 	va_list ap;
1218 	char *buf;
1219 	int buflen;
1220 	int len;
1221 
1222 	va_start(ap, fmt);
1223 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1224 	va_end(ap);
1225 
1226 	buf = os_malloc(buflen);
1227 	if (buf == NULL) {
1228 		wpa_printf(MSG_ERROR, "wpa_msg_global: Failed to allocate "
1229 			   "message buffer");
1230 		return;
1231 	}
1232 	va_start(ap, fmt);
1233 	len = vsnprintf(buf, buflen, fmt, ap);
1234 	va_end(ap);
1235 	if (wpa_msg_cb)
1236 		wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
1237 	bin_clear_free(buf, buflen);
1238 }
1239 
1240 
wpa_msg_global_ctrl(void * ctx,int level,const char * fmt,...)1241 void wpa_msg_global_ctrl(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
1242 {
1243 	va_list ap;
1244 	char *buf;
1245 	int buflen;
1246 	int len;
1247 
1248 	if (!wpa_msg_cb)
1249 		return;
1250 
1251 	va_start(ap, fmt);
1252 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1253 	va_end(ap);
1254 
1255 	buf = os_malloc(buflen);
1256 	if (buf == NULL) {
1257 		wpa_printf(MSG_ERROR,
1258 			   "wpa_msg_global_ctrl: Failed to allocate message buffer");
1259 		return;
1260 	}
1261 	va_start(ap, fmt);
1262 	len = vsnprintf(buf, buflen, fmt, ap);
1263 	va_end(ap);
1264 	wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
1265 	bin_clear_free(buf, buflen);
1266 }
1267 
1268 
wpa_msg_no_global(void * ctx,int level,const char * fmt,...)1269 void wpa_msg_no_global(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
1270 {
1271 	va_list ap;
1272 	char *buf;
1273 	int buflen;
1274 	int len;
1275 
1276 	va_start(ap, fmt);
1277 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1278 	va_end(ap);
1279 
1280 	buf = os_malloc(buflen);
1281 	if (buf == NULL) {
1282 		wpa_printf(MSG_ERROR, "wpa_msg_no_global: Failed to allocate "
1283 			   "message buffer");
1284 		return;
1285 	}
1286 	va_start(ap, fmt);
1287 	len = vsnprintf(buf, buflen, fmt, ap);
1288 	va_end(ap);
1289 	if (wpa_msg_cb)
1290 		wpa_msg_cb(ctx, level, WPA_MSG_NO_GLOBAL, buf, len);
1291 	bin_clear_free(buf, buflen);
1292 }
1293 
1294 
wpa_msg_global_only(void * ctx,int level,const char * fmt,...)1295 void wpa_msg_global_only(void *ctx, int level, const char *fmt, ...)
1296 {
1297 	va_list ap;
1298 	char *buf;
1299 	int buflen;
1300 	int len;
1301 
1302 	va_start(ap, fmt);
1303 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1304 	va_end(ap);
1305 
1306 	buf = os_malloc(buflen);
1307 	if (buf == NULL) {
1308 		wpa_printf(MSG_ERROR, "%s: Failed to allocate message buffer",
1309 			   __func__);
1310 		return;
1311 	}
1312 	va_start(ap, fmt);
1313 	len = vsnprintf(buf, buflen, fmt, ap);
1314 	va_end(ap);
1315 	wpa_printf(level, "%s", get_anonymized_result_setnetwork_for_bssid(buf));
1316 	if (wpa_msg_cb)
1317 		wpa_msg_cb(ctx, level, WPA_MSG_ONLY_GLOBAL, buf, len);
1318 	os_free(buf);
1319 }
1320 
1321 #endif /* CONFIG_NO_WPA_MSG */
1322 
1323 
1324 #ifndef CONFIG_NO_HOSTAPD_LOGGER
1325 static hostapd_logger_cb_func hostapd_logger_cb = NULL;
1326 
hostapd_logger_register_cb(hostapd_logger_cb_func func)1327 void hostapd_logger_register_cb(hostapd_logger_cb_func func)
1328 {
1329 	hostapd_logger_cb = func;
1330 }
1331 
1332 
hostapd_logger(void * ctx,const u8 * addr,unsigned int module,int level,const char * fmt,...)1333 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
1334 		    const char *fmt, ...)
1335 {
1336 	va_list ap;
1337 	char *buf;
1338 	int buflen;
1339 	int len;
1340 
1341 	va_start(ap, fmt);
1342 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1343 	va_end(ap);
1344 
1345 	buf = os_malloc(buflen);
1346 	if (buf == NULL) {
1347 		wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
1348 			   "message buffer");
1349 		return;
1350 	}
1351 	va_start(ap, fmt);
1352 	len = vsnprintf(buf, buflen, fmt, ap);
1353 	va_end(ap);
1354 	if (hostapd_logger_cb)
1355 		hostapd_logger_cb(ctx, addr, module, level, buf, len);
1356 	else if (addr)
1357 		wpa_printf(MSG_DEBUG, "hostapd_logger: STA " MACSTR_SEC " - %s",
1358 			   MAC2STR_SEC(addr), get_anonymized_result_setnetwork_for_bssid(buf));
1359 	else
1360 		wpa_printf(MSG_DEBUG, "hostapd_logger: %s", get_anonymized_result_setnetwork_for_bssid(buf));
1361 	bin_clear_free(buf, buflen);
1362 }
1363 
hostapd_logger_only_for_cb(void * ctx,const u8 * addr,unsigned int module,int level,const char * fmt,...)1364 void hostapd_logger_only_for_cb(void *ctx, const u8 *addr, unsigned int module, int level,
1365 			const char *fmt, ...)
1366 {
1367 	va_list ap;
1368 	char *buf;
1369 	int buflen;
1370 	int len;
1371 
1372 	va_start(ap, fmt);
1373 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1374 	va_end(ap);
1375 
1376 	buf = os_malloc(buflen);
1377 	if (buf == NULL) {
1378 		wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
1379 			"message buffer");
1380 		return;
1381 	}
1382 	va_start(ap, fmt);
1383 	len = vsnprintf(buf, buflen, fmt, ap);
1384 	va_end(ap);
1385 	if (hostapd_logger_cb) {
1386 		hostapd_logger_cb(ctx, addr, module, level, buf, len);
1387 	}
1388 	bin_clear_free(buf, buflen);
1389 }
1390 
1391 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
1392 
1393 
debug_level_str(int level)1394 const char * debug_level_str(int level)
1395 {
1396 	switch (level) {
1397 	case MSG_EXCESSIVE:
1398 		return "EXCESSIVE";
1399 	case MSG_MSGDUMP:
1400 		return "MSGDUMP";
1401 	case MSG_DEBUG:
1402 		return "DEBUG";
1403 	case MSG_INFO:
1404 		return "INFO";
1405 	case MSG_WARNING:
1406 		return "WARNING";
1407 	case MSG_ERROR:
1408 		return "ERROR";
1409 	default:
1410 		return "?";
1411 	}
1412 }
1413 
1414 
str_to_debug_level(const char * s)1415 int str_to_debug_level(const char *s)
1416 {
1417 	if (os_strcasecmp(s, "EXCESSIVE") == 0)
1418 		return MSG_EXCESSIVE;
1419 	if (os_strcasecmp(s, "MSGDUMP") == 0)
1420 		return MSG_MSGDUMP;
1421 	if (os_strcasecmp(s, "DEBUG") == 0)
1422 		return MSG_DEBUG;
1423 	if (os_strcasecmp(s, "INFO") == 0)
1424 		return MSG_INFO;
1425 	if (os_strcasecmp(s, "WARNING") == 0)
1426 		return MSG_WARNING;
1427 	if (os_strcasecmp(s, "ERROR") == 0)
1428 		return MSG_ERROR;
1429 	return -1;
1430 }
1431