• 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 
11 #include "common.h"
12 
13 #ifdef CONFIG_DEBUG_SYSLOG
14 #include <syslog.h>
15 
16 int wpa_debug_syslog = 0;
17 #endif /* CONFIG_DEBUG_SYSLOG */
18 
19 #ifdef CONFIG_DEBUG_LINUX_TRACING
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <stdio.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 
32 int wpa_debug_level = MSG_DEBUG;
33 int wpa_debug_show_keys = 1;
34 int wpa_debug_timestamp = 0;
35 
36 
37 #ifdef CONFIG_ANDROID_LOG
38 
39 #include <android/log.h>
40 
41 #ifndef ANDROID_LOG_NAME
42 #define ANDROID_LOG_NAME	"wpa_supplicant"
43 #endif /* ANDROID_LOG_NAME */
44 
wpa_to_android_level(int level)45 static int wpa_to_android_level(int level)
46 {
47 	if (level == MSG_ERROR)
48 		return ANDROID_LOG_ERROR;
49 	if (level == MSG_WARNING)
50 		return ANDROID_LOG_WARN;
51 	if (level == MSG_INFO)
52 		return ANDROID_LOG_INFO;
53 	return ANDROID_LOG_DEBUG;
54 }
55 
56 #endif /* CONFIG_ANDROID_LOG */
57 
58 #ifndef CONFIG_NO_STDOUT_DEBUG
59 
60 #ifdef CONFIG_DEBUG_FILE
61 #include <sys/types.h>
62 #include <sys/stat.h>
63 #include <fcntl.h>
64 
65 static FILE *out_file = NULL;
66 #endif /* CONFIG_DEBUG_FILE */
67 
68 
wpa_debug_print_timestamp(void)69 void wpa_debug_print_timestamp(void)
70 {
71 #ifndef CONFIG_ANDROID_LOG
72 	struct os_time tv;
73 
74 	if (!wpa_debug_timestamp)
75 		return;
76 
77 	os_get_time(&tv);
78 #ifdef CONFIG_DEBUG_FILE
79 	if (out_file) {
80 		fprintf(out_file, "%ld.%06u: ", (long) tv.sec,
81 			(unsigned int) tv.usec);
82 	} else
83 #endif /* CONFIG_DEBUG_FILE */
84 	printf("%ld.%06u: ", (long) tv.sec, (unsigned int) tv.usec);
85 #endif /* CONFIG_ANDROID_LOG */
86 }
87 
88 
89 #ifdef CONFIG_DEBUG_SYSLOG
90 #ifndef LOG_HOSTAPD
91 #define LOG_HOSTAPD LOG_DAEMON
92 #endif /* LOG_HOSTAPD */
93 
wpa_debug_open_syslog(void)94 void wpa_debug_open_syslog(void)
95 {
96 	openlog("wpa_supplicant", LOG_PID | LOG_NDELAY, LOG_HOSTAPD);
97 	wpa_debug_syslog++;
98 }
99 
100 
wpa_debug_close_syslog(void)101 void wpa_debug_close_syslog(void)
102 {
103 	if (wpa_debug_syslog)
104 		closelog();
105 }
106 
107 
syslog_priority(int level)108 static int syslog_priority(int level)
109 {
110 	switch (level) {
111 	case MSG_MSGDUMP:
112 	case MSG_DEBUG:
113 		return LOG_DEBUG;
114 	case MSG_INFO:
115 		return LOG_NOTICE;
116 	case MSG_WARNING:
117 		return LOG_WARNING;
118 	case MSG_ERROR:
119 		return LOG_ERR;
120 	}
121 	return LOG_INFO;
122 }
123 #endif /* CONFIG_DEBUG_SYSLOG */
124 
125 
126 #ifdef CONFIG_DEBUG_LINUX_TRACING
127 
wpa_debug_open_linux_tracing(void)128 int wpa_debug_open_linux_tracing(void)
129 {
130 	int mounts, trace_fd;
131 	char buf[4096] = {};
132 	ssize_t buflen;
133 	char *line, *tmp1, *path = NULL;
134 
135 	mounts = open("/proc/mounts", O_RDONLY);
136 	if (mounts < 0) {
137 		printf("no /proc/mounts\n");
138 		return -1;
139 	}
140 
141 	buflen = read(mounts, buf, sizeof(buf) - 1);
142 	close(mounts);
143 	if (buflen < 0) {
144 		printf("failed to read /proc/mounts\n");
145 		return -1;
146 	}
147 	buf[buflen] = '\0';
148 
149 	line = strtok_r(buf, "\n", &tmp1);
150 	while (line) {
151 		char *tmp2, *tmp_path, *fstype;
152 		/* "<dev> <mountpoint> <fs type> ..." */
153 		strtok_r(line, " ", &tmp2);
154 		tmp_path = strtok_r(NULL, " ", &tmp2);
155 		fstype = strtok_r(NULL, " ", &tmp2);
156 		if (fstype && strcmp(fstype, "debugfs") == 0) {
157 			path = tmp_path;
158 			break;
159 		}
160 
161 		line = strtok_r(NULL, "\n", &tmp1);
162 	}
163 
164 	if (path == NULL) {
165 		printf("debugfs mountpoint not found\n");
166 		return -1;
167 	}
168 
169 	snprintf(buf, sizeof(buf) - 1, "%s/tracing/trace_marker", path);
170 
171 	trace_fd = open(buf, O_WRONLY);
172 	if (trace_fd < 0) {
173 		printf("failed to open trace_marker file\n");
174 		return -1;
175 	}
176 	wpa_debug_tracing_file = fdopen(trace_fd, "w");
177 	if (wpa_debug_tracing_file == NULL) {
178 		close(trace_fd);
179 		printf("failed to fdopen()\n");
180 		return -1;
181 	}
182 
183 	return 0;
184 }
185 
186 
wpa_debug_close_linux_tracing(void)187 void wpa_debug_close_linux_tracing(void)
188 {
189 	if (wpa_debug_tracing_file == NULL)
190 		return;
191 	fclose(wpa_debug_tracing_file);
192 	wpa_debug_tracing_file = NULL;
193 }
194 
195 #endif /* CONFIG_DEBUG_LINUX_TRACING */
196 
197 #ifdef CONFIG_OPEN_HARMONY_PATCH
198 #include "hilog/log_c.h"
199 #include "parameter.h"
200 
201 #ifdef LOG_DOMAIN
202 #undef LOG_DOMAIN
203 #endif // LOG_DOMAIN
204 #ifdef LOG_TAG
205 #undef LOG_TAG
206 #endif // LOG_TAG
207 #define LOG_DOMAIN 0xD0015C0
208 #define LOG_TAG "wpa_supplicant"
209 #define WPA_MAX_LOG_CHAR 1024
210 #define WPA_PROP_KEY_DEBUG_ON "persist.sys.wpa_debug_on"
211 #define PARAM_VALUE_MAX_LEN 10
212 
213 enum {
214 	WPA_HILOG_UNKNOWN, WPA_HILOG_UNSET, WPA_HILOG_SET
215 };
216 
217 int32_t wpa_debug_hilog_switch = WPA_HILOG_UNKNOWN;
218 
wpa_get_log_level(int level)219 static int wpa_get_log_level(int level)
220 {
221 	switch (level) {
222 		case MSG_ERROR:
223 			return LOG_ERROR;
224 		case MSG_WARNING:
225 			return LOG_WARN;
226 		case MSG_INFO:
227 			return LOG_INFO;
228 		default:
229 			return LOG_DEBUG;
230 	}
231 }
232 
wpa_can_hilog()233 static bool wpa_can_hilog()
234 {
235 	switch (wpa_debug_hilog_switch) {
236 		case WPA_HILOG_UNSET:
237 			return false;
238 		case WPA_HILOG_SET:
239 			return true;
240 		default:
241 			break;
242 	}
243 	char prop[PARAM_VALUE_MAX_LEN] = { 0 };
244 	if (GetParameter(WPA_PROP_KEY_DEBUG_ON, "0", prop, sizeof(prop)) > 0) {
245 		if (atoi(prop) > 0) {
246 			wpa_debug_hilog_switch = WPA_HILOG_SET;
247 			return true;
248 		}
249 	}
250 	wpa_debug_hilog_switch = WPA_HILOG_UNSET;
251 	return false;
252 }
253 #endif // CONFIG_OPEN_HARMONY_PATCH
254 
255 /**
256  * wpa_printf - conditional printf
257  * @level: priority level (MSG_*) of the message
258  * @fmt: printf format string, followed by optional arguments
259  *
260  * This function is used to print conditional debugging and error messages. The
261  * output may be directed to stdout, stderr, and/or syslog based on
262  * configuration.
263  *
264  * Note: New line '\n' is added to the end of the text when printing to stdout.
265  */
wpa_printf(int level,const char * fmt,...)266 void wpa_printf(int level, const char *fmt, ...)
267 {
268 #ifdef CONFIG_OPEN_HARMONY_PATCH
269 	if (wpa_can_hilog()) {
270 		int32_t ulPos = 0;
271 		char szStr[WPA_MAX_LOG_CHAR] = {0};
272 		va_list arg = {0};
273 		int32_t ret;
274 
275 		va_start(arg, fmt);
276 		ret = vsprintf(&szStr[ulPos], fmt, arg);
277 		va_end(arg);
278 		if (ret > 0) {
279 			HiLogPrint(LOG_CORE, wpa_get_log_level(level), LOG_DOMAIN, LOG_TAG, "%{public}s", szStr);
280 		}
281 		return;
282 	}
283 #endif
284 
285 #ifdef CONFIG_WPA_NO_LOG
286     return;
287 #else
288 	va_list ap;
289 
290 	va_start(ap, fmt);
291 	if (level >= wpa_debug_level) {
292 #ifdef CONFIG_ANDROID_LOG
293 		__android_log_vprint(wpa_to_android_level(level),
294 				     ANDROID_LOG_NAME, fmt, ap);
295 #else /* CONFIG_ANDROID_LOG */
296 #ifdef CONFIG_DEBUG_SYSLOG
297 		if (wpa_debug_syslog) {
298 			vsyslog(syslog_priority(level), fmt, ap);
299 		} else {
300 #endif /* CONFIG_DEBUG_SYSLOG */
301 		wpa_debug_print_timestamp();
302 #ifdef CONFIG_DEBUG_FILE
303 		if (out_file) {
304 			vfprintf(out_file, fmt, ap);
305 			fprintf(out_file, "\n");
306 		} else {
307 #endif /* CONFIG_DEBUG_FILE */
308 		vprintf(fmt, ap);
309 		printf("\n");
310 #ifdef CONFIG_DEBUG_FILE
311 		}
312 #endif /* CONFIG_DEBUG_FILE */
313 #ifdef CONFIG_DEBUG_SYSLOG
314 		}
315 #endif /* CONFIG_DEBUG_SYSLOG */
316 #endif /* CONFIG_ANDROID_LOG */
317 	}
318 	va_end(ap);
319 
320 #ifdef CONFIG_DEBUG_LINUX_TRACING
321 	if (wpa_debug_tracing_file != NULL) {
322 		va_start(ap, fmt);
323 		fprintf(wpa_debug_tracing_file, WPAS_TRACE_PFX, level);
324 		vfprintf(wpa_debug_tracing_file, fmt, ap);
325 		fprintf(wpa_debug_tracing_file, "\n");
326 		fflush(wpa_debug_tracing_file);
327 		va_end(ap);
328 	}
329 #endif /* CONFIG_DEBUG_LINUX_TRACING */
330 #endif /* CONFIG_WPA_NO_LOG */
331 }
332 
333 
_wpa_hexdump(int level,const char * title,const u8 * buf,size_t len,int show)334 static void _wpa_hexdump(int level, const char *title, const u8 *buf,
335 			 size_t len, int show)
336 {
337 #ifdef CONFIG_WPA_NO_LOG
338     return;
339 #else
340 	size_t i;
341 #ifdef CONFIG_OPEN_HARMONY_PATCH
342 	if (wpa_can_hilog()) {
343 		const char *display;
344 		char *strbuf = NULL;
345 		size_t slen = len;
346 		if (buf == NULL) {
347 			display = " [NULL]";
348 		} else if (len == 0) {
349 			display = "";
350 		} else if (show && len) {
351 			if (slen > 32)
352 				slen = 32;
353 			strbuf = os_malloc(1 + 3 * slen);
354 			if (strbuf == NULL) {
355 				wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
356 				                      "allocate message buffer");
357 				return;
358 			}
359 
360 			for (i = 0; i < slen; i++)
361 				os_snprintf(&strbuf[i * 3], 4, " %02x",
362 				            buf[i]);
363 
364 			display = strbuf;
365 		} else {
366 			display = " [REMOVED]";
367 		}
368 		HiLogPrint(LOG_CORE, wpa_get_log_level(level), LOG_DOMAIN,
369 		           LOG_TAG, "%{public}s - hexdump(len=%{public}lu):%{public}s%{public}s",
370 		           title, (long unsigned int) len, display,
371 		           len > slen ? " ..." : "");
372 		bin_clear_free(strbuf, 1 + 3 * slen);
373 		return;
374 	}
375 #endif
376 
377 #ifdef CONFIG_DEBUG_LINUX_TRACING
378 	if (wpa_debug_tracing_file != NULL) {
379 		fprintf(wpa_debug_tracing_file,
380 			WPAS_TRACE_PFX "%s - hexdump(len=%lu):",
381 			level, title, (unsigned long) len);
382 		if (buf == NULL) {
383 			fprintf(wpa_debug_tracing_file, " [NULL]\n");
384 		} else if (!show) {
385 			fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
386 		} else {
387 			for (i = 0; i < len; i++)
388 				fprintf(wpa_debug_tracing_file,
389 					" %02x", buf[i]);
390 		}
391 		fflush(wpa_debug_tracing_file);
392 	}
393 #endif /* CONFIG_DEBUG_LINUX_TRACING */
394 
395 	if (level < wpa_debug_level)
396 		return;
397 #ifdef CONFIG_ANDROID_LOG
398 	{
399 		const char *display;
400 		char *strbuf = NULL;
401 		size_t slen = len;
402 		if (buf == NULL) {
403 			display = " [NULL]";
404 		} else if (len == 0) {
405 			display = "";
406 		} else if (show && len) {
407 			/* Limit debug message length for Android log */
408 			if (slen > 32)
409 				slen = 32;
410 			strbuf = os_malloc(1 + 3 * slen);
411 			if (strbuf == NULL) {
412 				wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
413 					   "allocate message buffer");
414 				return;
415 			}
416 
417 			for (i = 0; i < slen; i++)
418 				os_snprintf(&strbuf[i * 3], 4, " %02x",
419 					    buf[i]);
420 
421 			display = strbuf;
422 		} else {
423 			display = " [REMOVED]";
424 		}
425 
426 		__android_log_print(wpa_to_android_level(level),
427 				    ANDROID_LOG_NAME,
428 				    "%s - hexdump(len=%lu):%s%s",
429 				    title, (long unsigned int) len, display,
430 				    len > slen ? " ..." : "");
431 		bin_clear_free(strbuf, 1 + 3 * slen);
432 		return;
433 	}
434 #else /* CONFIG_ANDROID_LOG */
435 #ifdef CONFIG_DEBUG_SYSLOG
436 	if (wpa_debug_syslog) {
437 		const char *display;
438 		char *strbuf = NULL;
439 
440 		if (buf == NULL) {
441 			display = " [NULL]";
442 		} else if (len == 0) {
443 			display = "";
444 		} else if (show && len) {
445 			strbuf = os_malloc(1 + 3 * len);
446 			if (strbuf == NULL) {
447 				wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
448 					   "allocate message buffer");
449 				return;
450 			}
451 
452 			for (i = 0; i < len; i++)
453 				os_snprintf(&strbuf[i * 3], 4, " %02x",
454 					    buf[i]);
455 
456 			display = strbuf;
457 		} else {
458 			display = " [REMOVED]";
459 		}
460 
461 		syslog(syslog_priority(level), "%s - hexdump(len=%lu):%s",
462 		       title, (unsigned long) len, display);
463 		bin_clear_free(strbuf, 1 + 3 * len);
464 		return;
465 	}
466 #endif /* CONFIG_DEBUG_SYSLOG */
467 	wpa_debug_print_timestamp();
468 #ifdef CONFIG_DEBUG_FILE
469 	if (out_file) {
470 		fprintf(out_file, "%s - hexdump(len=%lu):",
471 			title, (unsigned long) len);
472 		if (buf == NULL) {
473 			fprintf(out_file, " [NULL]");
474 		} else if (show) {
475 			for (i = 0; i < len; i++)
476 				fprintf(out_file, " %02x", buf[i]);
477 		} else {
478 			fprintf(out_file, " [REMOVED]");
479 		}
480 		fprintf(out_file, "\n");
481 	} else {
482 #endif /* CONFIG_DEBUG_FILE */
483 	printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
484 	if (buf == NULL) {
485 		printf(" [NULL]");
486 	} else if (show) {
487 		for (i = 0; i < len; i++)
488 			printf(" %02x", buf[i]);
489 	} else {
490 		printf(" [REMOVED]");
491 	}
492 	printf("\n");
493 #ifdef CONFIG_DEBUG_FILE
494 	}
495 #endif /* CONFIG_DEBUG_FILE */
496 #endif /* CONFIG_ANDROID_LOG */
497 #endif /* CONFIG_WPA_NO_LOG */
498 }
499 
wpa_hexdump(int level,const char * title,const void * buf,size_t len)500 void wpa_hexdump(int level, const char *title, const void *buf, size_t len)
501 {
502 	_wpa_hexdump(level, title, buf, len, 1);
503 }
504 
505 
wpa_hexdump_key(int level,const char * title,const void * buf,size_t len)506 void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len)
507 {
508 	_wpa_hexdump(level, title, buf, len, wpa_debug_show_keys);
509 }
510 
511 
_wpa_hexdump_ascii(int level,const char * title,const void * buf,size_t len,int show)512 static void _wpa_hexdump_ascii(int level, const char *title, const void *buf,
513 			       size_t len, int show)
514 {
515 #ifdef CONFIG_WPA_NO_LOG
516     return;
517 #else
518 	size_t i, llen;
519 	const u8 *pos = buf;
520 	const size_t line_len = 16;
521 
522 #ifdef CONFIG_DEBUG_LINUX_TRACING
523 	if (wpa_debug_tracing_file != NULL) {
524 		fprintf(wpa_debug_tracing_file,
525 			WPAS_TRACE_PFX "%s - hexdump_ascii(len=%lu):",
526 			level, title, (unsigned long) len);
527 		if (buf == NULL) {
528 			fprintf(wpa_debug_tracing_file, " [NULL]\n");
529 		} else if (!show) {
530 			fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
531 		} else {
532 			/* can do ascii processing in userspace */
533 			for (i = 0; i < len; i++)
534 				fprintf(wpa_debug_tracing_file,
535 					" %02x", pos[i]);
536 		}
537 		fflush(wpa_debug_tracing_file);
538 	}
539 #endif /* CONFIG_DEBUG_LINUX_TRACING */
540 
541 	if (level < wpa_debug_level)
542 		return;
543 #ifdef CONFIG_ANDROID_LOG
544 	_wpa_hexdump(level, title, buf, len, show);
545 #else /* CONFIG_ANDROID_LOG */
546 #ifdef CONFIG_DEBUG_SYSLOG
547 	if (wpa_debug_syslog) {
548 		_wpa_hexdump(level, title, buf, len, show);
549 		return;
550 	}
551 #endif /* CONFIG_DEBUG_SYSLOG */
552 	wpa_debug_print_timestamp();
553 #ifdef CONFIG_DEBUG_FILE
554 	if (out_file) {
555 		if (!show) {
556 			fprintf(out_file,
557 				"%s - hexdump_ascii(len=%lu): [REMOVED]\n",
558 				title, (unsigned long) len);
559 			return;
560 		}
561 		if (buf == NULL) {
562 			fprintf(out_file,
563 				"%s - hexdump_ascii(len=%lu): [NULL]\n",
564 				title, (unsigned long) len);
565 			return;
566 		}
567 		fprintf(out_file, "%s - hexdump_ascii(len=%lu):\n",
568 			title, (unsigned long) len);
569 		while (len) {
570 			llen = len > line_len ? line_len : len;
571 			fprintf(out_file, "    ");
572 			for (i = 0; i < llen; i++)
573 				fprintf(out_file, " %02x", pos[i]);
574 			for (i = llen; i < line_len; i++)
575 				fprintf(out_file, "   ");
576 			fprintf(out_file, "   ");
577 			for (i = 0; i < llen; i++) {
578 				if (isprint(pos[i]))
579 					fprintf(out_file, "%c", pos[i]);
580 				else
581 					fprintf(out_file, "_");
582 			}
583 			for (i = llen; i < line_len; i++)
584 				fprintf(out_file, " ");
585 			fprintf(out_file, "\n");
586 			pos += llen;
587 			len -= llen;
588 		}
589 	} else {
590 #endif /* CONFIG_DEBUG_FILE */
591 	if (!show) {
592 		printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
593 		       title, (unsigned long) len);
594 		return;
595 	}
596 	if (buf == NULL) {
597 		printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
598 		       title, (unsigned long) len);
599 		return;
600 	}
601 	printf("%s - hexdump_ascii(len=%lu):\n", title, (unsigned long) len);
602 	while (len) {
603 		llen = len > line_len ? line_len : len;
604 		printf("    ");
605 		for (i = 0; i < llen; i++)
606 			printf(" %02x", pos[i]);
607 		for (i = llen; i < line_len; i++)
608 			printf("   ");
609 		printf("   ");
610 		for (i = 0; i < llen; i++) {
611 			if (isprint(pos[i]))
612 				printf("%c", pos[i]);
613 			else
614 				printf("_");
615 		}
616 		for (i = llen; i < line_len; i++)
617 			printf(" ");
618 		printf("\n");
619 		pos += llen;
620 		len -= llen;
621 	}
622 #ifdef CONFIG_DEBUG_FILE
623 	}
624 #endif /* CONFIG_DEBUG_FILE */
625 #endif /* CONFIG_ANDROID_LOG */
626 #endif /* CONFIG_WPA_NO_LOG */
627 }
628 
629 
wpa_hexdump_ascii(int level,const char * title,const void * buf,size_t len)630 void wpa_hexdump_ascii(int level, const char *title, const void *buf,
631 		       size_t len)
632 {
633 	_wpa_hexdump_ascii(level, title, buf, len, 1);
634 }
635 
636 
wpa_hexdump_ascii_key(int level,const char * title,const void * buf,size_t len)637 void wpa_hexdump_ascii_key(int level, const char *title, const void *buf,
638 			   size_t len)
639 {
640 	_wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
641 }
642 
643 
644 #ifdef CONFIG_DEBUG_FILE
645 static char *last_path = NULL;
646 #endif /* CONFIG_DEBUG_FILE */
647 
wpa_debug_reopen_file(void)648 int wpa_debug_reopen_file(void)
649 {
650 #ifdef CONFIG_DEBUG_FILE
651 	int rv;
652 	char *tmp;
653 
654 	if (!last_path)
655 		return 0; /* logfile not used */
656 
657 	tmp = os_strdup(last_path);
658 	if (!tmp)
659 		return -1;
660 
661 	wpa_debug_close_file();
662 	rv = wpa_debug_open_file(tmp);
663 	os_free(tmp);
664 	return rv;
665 #else /* CONFIG_DEBUG_FILE */
666 	return 0;
667 #endif /* CONFIG_DEBUG_FILE */
668 }
669 
670 
wpa_debug_open_file(const char * path)671 int wpa_debug_open_file(const char *path)
672 {
673 #ifdef CONFIG_DEBUG_FILE
674 	int out_fd;
675 
676 	if (!path)
677 		return 0;
678 
679 	if (last_path == NULL || os_strcmp(last_path, path) != 0) {
680 		/* Save our path to enable re-open */
681 		os_free(last_path);
682 		last_path = os_strdup(path);
683 	}
684 
685 	out_fd = open(path, O_CREAT | O_APPEND | O_WRONLY,
686 		      S_IRUSR | S_IWUSR | S_IRGRP);
687 	if (out_fd < 0) {
688 		wpa_printf(MSG_ERROR,
689 			   "%s: Failed to open output file descriptor, using standard output",
690 			   __func__);
691 		return -1;
692 	}
693 
694 #ifdef __linux__
695 	if (fcntl(out_fd, F_SETFD, FD_CLOEXEC) < 0) {
696 		wpa_printf(MSG_DEBUG,
697 			   "%s: Failed to set FD_CLOEXEC - continue without: %s",
698 			   __func__, strerror(errno));
699 	}
700 #endif /* __linux__ */
701 
702 	out_file = fdopen(out_fd, "a");
703 	if (out_file == NULL) {
704 		wpa_printf(MSG_ERROR, "wpa_debug_open_file: Failed to open "
705 			   "output file, using standard output");
706 		close(out_fd);
707 		return -1;
708 	}
709 #ifndef _WIN32
710 	setvbuf(out_file, NULL, _IOLBF, 0);
711 #endif /* _WIN32 */
712 #else /* CONFIG_DEBUG_FILE */
713 	(void)path;
714 #endif /* CONFIG_DEBUG_FILE */
715 	return 0;
716 }
717 
718 
wpa_debug_close_file(void)719 void wpa_debug_close_file(void)
720 {
721 #ifdef CONFIG_DEBUG_FILE
722 	if (!out_file)
723 		return;
724 	fclose(out_file);
725 	out_file = NULL;
726 	os_free(last_path);
727 	last_path = NULL;
728 #endif /* CONFIG_DEBUG_FILE */
729 }
730 
731 
wpa_debug_setup_stdout(void)732 void wpa_debug_setup_stdout(void) __attribute__((no_sanitize("cfi")))
733 {
734 #ifndef _WIN32
735 	setvbuf(stdout, NULL, _IOLBF, 0);
736 #endif /* _WIN32 */
737 }
738 
739 #endif /* CONFIG_NO_STDOUT_DEBUG */
740 
741 
742 #ifndef CONFIG_NO_WPA_MSG
743 static wpa_msg_cb_func wpa_msg_cb = NULL;
744 
wpa_msg_register_cb(wpa_msg_cb_func func)745 void wpa_msg_register_cb(wpa_msg_cb_func func)
746 {
747 	wpa_msg_cb = func;
748 }
749 
750 
751 static wpa_msg_get_ifname_func wpa_msg_ifname_cb = NULL;
752 
wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)753 void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)
754 {
755 	wpa_msg_ifname_cb = func;
756 }
757 
758 
wpa_msg(void * ctx,int level,const char * fmt,...)759 void wpa_msg(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
760 {
761 	va_list ap;
762 	char *buf;
763 	int buflen;
764 	int len;
765 	char prefix[130];
766 
767 	va_start(ap, fmt);
768 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
769 	va_end(ap);
770 
771 	buf = os_malloc(buflen);
772 	if (buf == NULL) {
773 		wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
774 			   "buffer");
775 		return;
776 	}
777 	va_start(ap, fmt);
778 	prefix[0] = '\0';
779 	if (wpa_msg_ifname_cb) {
780 		const char *ifname = wpa_msg_ifname_cb(ctx);
781 		if (ifname) {
782 			int res = os_snprintf(prefix, sizeof(prefix), "%s: ",
783 					      ifname);
784 			if (os_snprintf_error(sizeof(prefix), res))
785 				prefix[0] = '\0';
786 		}
787 	}
788 	len = vsnprintf(buf, buflen, fmt, ap);
789 	va_end(ap);
790 	wpa_printf(level, "%s%s", prefix, buf);
791 	if (wpa_msg_cb)
792 		wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
793 	bin_clear_free(buf, buflen);
794 }
795 
796 
wpa_msg_ctrl(void * ctx,int level,const char * fmt,...)797 void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
798 {
799 	va_list ap;
800 	char *buf;
801 	int buflen;
802 	int len;
803 
804 	if (!wpa_msg_cb)
805 		return;
806 
807 	va_start(ap, fmt);
808 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
809 	va_end(ap);
810 
811 	buf = os_malloc(buflen);
812 	if (buf == NULL) {
813 		wpa_printf(MSG_ERROR, "wpa_msg_ctrl: Failed to allocate "
814 			   "message buffer");
815 		return;
816 	}
817 	va_start(ap, fmt);
818 	len = vsnprintf(buf, buflen, fmt, ap);
819 	va_end(ap);
820 	wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
821 	bin_clear_free(buf, buflen);
822 }
823 
824 
wpa_msg_global(void * ctx,int level,const char * fmt,...)825 void wpa_msg_global(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
826 {
827 	va_list ap;
828 	char *buf;
829 	int buflen;
830 	int len;
831 
832 	va_start(ap, fmt);
833 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
834 	va_end(ap);
835 
836 	buf = os_malloc(buflen);
837 	if (buf == NULL) {
838 		wpa_printf(MSG_ERROR, "wpa_msg_global: Failed to allocate "
839 			   "message buffer");
840 		return;
841 	}
842 	va_start(ap, fmt);
843 	len = vsnprintf(buf, buflen, fmt, ap);
844 	va_end(ap);
845 	wpa_printf(level, "%s", buf);
846 	if (wpa_msg_cb)
847 		wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
848 	bin_clear_free(buf, buflen);
849 }
850 
851 
wpa_msg_global_ctrl(void * ctx,int level,const char * fmt,...)852 void wpa_msg_global_ctrl(void *ctx, int level, const char *fmt, ...)
853 {
854 	va_list ap;
855 	char *buf;
856 	int buflen;
857 	int len;
858 
859 	if (!wpa_msg_cb)
860 		return;
861 
862 	va_start(ap, fmt);
863 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
864 	va_end(ap);
865 
866 	buf = os_malloc(buflen);
867 	if (buf == NULL) {
868 		wpa_printf(MSG_ERROR,
869 			   "wpa_msg_global_ctrl: Failed to allocate message buffer");
870 		return;
871 	}
872 	va_start(ap, fmt);
873 	len = vsnprintf(buf, buflen, fmt, ap);
874 	va_end(ap);
875 	wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
876 	bin_clear_free(buf, buflen);
877 }
878 
879 
wpa_msg_no_global(void * ctx,int level,const char * fmt,...)880 void wpa_msg_no_global(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
881 {
882 	va_list ap;
883 	char *buf;
884 	int buflen;
885 	int len;
886 
887 	va_start(ap, fmt);
888 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
889 	va_end(ap);
890 
891 	buf = os_malloc(buflen);
892 	if (buf == NULL) {
893 		wpa_printf(MSG_ERROR, "wpa_msg_no_global: Failed to allocate "
894 			   "message buffer");
895 		return;
896 	}
897 	va_start(ap, fmt);
898 	len = vsnprintf(buf, buflen, fmt, ap);
899 	va_end(ap);
900 	wpa_printf(level, "%s", buf);
901 	if (wpa_msg_cb)
902 		wpa_msg_cb(ctx, level, WPA_MSG_NO_GLOBAL, buf, len);
903 	bin_clear_free(buf, buflen);
904 }
905 
906 
wpa_msg_global_only(void * ctx,int level,const char * fmt,...)907 void wpa_msg_global_only(void *ctx, int level, const char *fmt, ...)
908 {
909 	va_list ap;
910 	char *buf;
911 	int buflen;
912 	int len;
913 
914 	va_start(ap, fmt);
915 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
916 	va_end(ap);
917 
918 	buf = os_malloc(buflen);
919 	if (buf == NULL) {
920 		wpa_printf(MSG_ERROR, "%s: Failed to allocate message buffer",
921 			   __func__);
922 		return;
923 	}
924 	va_start(ap, fmt);
925 	len = vsnprintf(buf, buflen, fmt, ap);
926 	va_end(ap);
927 	wpa_printf(level, "%s", buf);
928 	if (wpa_msg_cb)
929 		wpa_msg_cb(ctx, level, WPA_MSG_ONLY_GLOBAL, buf, len);
930 	os_free(buf);
931 }
932 
933 #endif /* CONFIG_NO_WPA_MSG */
934 
935 
936 #ifndef CONFIG_NO_HOSTAPD_LOGGER
937 static hostapd_logger_cb_func hostapd_logger_cb = NULL;
938 
hostapd_logger_register_cb(hostapd_logger_cb_func func)939 void hostapd_logger_register_cb(hostapd_logger_cb_func func)
940 {
941 	hostapd_logger_cb = func;
942 }
943 
944 
hostapd_logger(void * ctx,const u8 * addr,unsigned int module,int level,const char * fmt,...)945 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
946 		    const char *fmt, ...)
947 {
948 	va_list ap;
949 	char *buf;
950 	int buflen;
951 	int len;
952 
953 	va_start(ap, fmt);
954 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
955 	va_end(ap);
956 
957 	buf = os_malloc(buflen);
958 	if (buf == NULL) {
959 		wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
960 			   "message buffer");
961 		return;
962 	}
963 	va_start(ap, fmt);
964 	len = vsnprintf(buf, buflen, fmt, ap);
965 	va_end(ap);
966 	if (hostapd_logger_cb)
967 		hostapd_logger_cb(ctx, addr, module, level, buf, len);
968 	else if (addr)
969 		wpa_printf(MSG_DEBUG, "hostapd_logger: STA " MACSTR " - %s",
970 			   MAC2STR(addr), buf);
971 	else
972 		wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
973 	bin_clear_free(buf, buflen);
974 }
975 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
976 
977 
debug_level_str(int level)978 const char * debug_level_str(int level)
979 {
980 	switch (level) {
981 	case MSG_EXCESSIVE:
982 		return "EXCESSIVE";
983 	case MSG_MSGDUMP:
984 		return "MSGDUMP";
985 	case MSG_DEBUG:
986 		return "DEBUG";
987 	case MSG_INFO:
988 		return "INFO";
989 	case MSG_WARNING:
990 		return "WARNING";
991 	case MSG_ERROR:
992 		return "ERROR";
993 	default:
994 		return "?";
995 	}
996 }
997 
998 
str_to_debug_level(const char * s)999 int str_to_debug_level(const char *s)
1000 {
1001 	if (os_strcasecmp(s, "EXCESSIVE") == 0)
1002 		return MSG_EXCESSIVE;
1003 	if (os_strcasecmp(s, "MSGDUMP") == 0)
1004 		return MSG_MSGDUMP;
1005 	if (os_strcasecmp(s, "DEBUG") == 0)
1006 		return MSG_DEBUG;
1007 	if (os_strcasecmp(s, "INFO") == 0)
1008 		return MSG_INFO;
1009 	if (os_strcasecmp(s, "WARNING") == 0)
1010 		return MSG_WARNING;
1011 	if (os_strcasecmp(s, "ERROR") == 0)
1012 		return MSG_ERROR;
1013 	return -1;
1014 }
1015