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 #endif /* CONFIG_DEBUG_SYSLOG */
16
17 #ifdef CONFIG_DEBUG_LINUX_TRACING
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <string.h>
22 #include <stdio.h>
23
24 static FILE *wpa_debug_tracing_file = NULL;
25
26 #define WPAS_TRACE_PFX "wpas <%d>: "
27 #endif /* CONFIG_DEBUG_LINUX_TRACING */
28
29
30 int wpa_debug_level = MSG_DEBUG;
31 int wpa_debug_show_keys = 1;
32 int wpa_debug_timestamp = 0;
33 int wpa_debug_syslog = 0;
34 #ifndef CONFIG_NO_STDOUT_DEBUG
35 static FILE *out_file = NULL;
36 #endif /* CONFIG_NO_STDOUT_DEBUG */
37
38
39 #ifdef CONFIG_ANDROID_LOG
40
41 #include <android/log.h>
42
43 #ifndef ANDROID_LOG_NAME
44 #define ANDROID_LOG_NAME "wpa_supplicant"
45 #endif /* ANDROID_LOG_NAME */
46
wpa_to_android_level(int level)47 static int wpa_to_android_level(int level)
48 {
49 if (level == MSG_ERROR)
50 return ANDROID_LOG_ERROR;
51 if (level == MSG_WARNING)
52 return ANDROID_LOG_WARN;
53 if (level == MSG_INFO)
54 return ANDROID_LOG_INFO;
55 return ANDROID_LOG_DEBUG;
56 }
57
58 #endif /* CONFIG_ANDROID_LOG */
59
60 #ifndef CONFIG_NO_STDOUT_DEBUG
61
62 #ifdef CONFIG_DEBUG_FILE
63 #include <sys/types.h>
64 #include <sys/stat.h>
65 #include <fcntl.h>
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 #endif /* CONFIG_DEBUG_FILE */
83 if (!out_file && !wpa_debug_syslog)
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 if (level >= wpa_debug_level) {
291 #ifdef CONFIG_ANDROID_LOG
292 va_start(ap, fmt);
293 __android_log_vprint(wpa_to_android_level(level),
294 ANDROID_LOG_NAME, fmt, ap);
295 va_end(ap);
296 #else /* CONFIG_ANDROID_LOG */
297 #ifdef CONFIG_DEBUG_SYSLOG
298 if (wpa_debug_syslog) {
299 va_start(ap, fmt);
300 vsyslog(syslog_priority(level), fmt, ap);
301 va_end(ap);
302 }
303 #endif /* CONFIG_DEBUG_SYSLOG */
304 wpa_debug_print_timestamp();
305 #ifdef CONFIG_DEBUG_FILE
306 if (out_file) {
307 va_start(ap, fmt);
308 vfprintf(out_file, fmt, ap);
309 fprintf(out_file, "\n");
310 va_end(ap);
311 }
312 #endif /* CONFIG_DEBUG_FILE */
313 if (!wpa_debug_syslog && !out_file) {
314 va_start(ap, fmt);
315 vprintf(fmt, ap);
316 printf("\n");
317 va_end(ap);
318 }
319 #endif /* CONFIG_ANDROID_LOG */
320 }
321
322 #ifdef CONFIG_DEBUG_LINUX_TRACING
323 if (wpa_debug_tracing_file != NULL) {
324 va_start(ap, fmt);
325 fprintf(wpa_debug_tracing_file, WPAS_TRACE_PFX, level);
326 vfprintf(wpa_debug_tracing_file, fmt, ap);
327 fprintf(wpa_debug_tracing_file, "\n");
328 fflush(wpa_debug_tracing_file);
329 va_end(ap);
330 }
331 #endif /* CONFIG_DEBUG_LINUX_TRACING */
332 #endif /* CONFIG_WPA_NO_LOG */
333 }
334
335
_wpa_hexdump(int level,const char * title,const u8 * buf,size_t len,int show,int only_syslog)336 static void _wpa_hexdump(int level, const char *title, const u8 *buf,
337 size_t len, int show, int only_syslog)
338 {
339 #ifdef CONFIG_WPA_NO_LOG
340 return;
341 #else
342 size_t i;
343 #ifdef CONFIG_OPEN_HARMONY_PATCH
344 if (wpa_can_hilog()) {
345 const char *display;
346 char *strbuf = NULL;
347 size_t slen = len;
348 if (buf == NULL) {
349 display = " [NULL]";
350 } else if (len == 0) {
351 display = "";
352 } else if (show && len) {
353 if (slen > 32)
354 slen = 32;
355 strbuf = os_malloc(1 + 3 * slen);
356 if (strbuf == NULL) {
357 wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
358 "allocate message buffer");
359 return;
360 }
361
362 for (i = 0; i < slen; i++)
363 os_snprintf(&strbuf[i * 3], 4, " %02x",
364 buf[i]);
365
366 display = strbuf;
367 } else {
368 display = " [REMOVED]";
369 }
370 HiLogPrint(LOG_CORE, wpa_get_log_level(level), LOG_DOMAIN,
371 LOG_TAG, "%{public}s - hexdump(len=%{public}lu):%{public}s%{public}s",
372 title, (long unsigned int) len, display,
373 len > slen ? " ..." : "");
374 bin_clear_free(strbuf, 1 + 3 * slen);
375 return;
376 }
377 #endif
378
379 #ifdef CONFIG_DEBUG_LINUX_TRACING
380 if (wpa_debug_tracing_file != NULL) {
381 fprintf(wpa_debug_tracing_file,
382 WPAS_TRACE_PFX "%s - hexdump(len=%lu):",
383 level, title, (unsigned long) len);
384 if (buf == NULL) {
385 fprintf(wpa_debug_tracing_file, " [NULL]\n");
386 } else if (!show) {
387 fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
388 } else {
389 for (i = 0; i < len; i++)
390 fprintf(wpa_debug_tracing_file,
391 " %02x", buf[i]);
392 }
393 fflush(wpa_debug_tracing_file);
394 }
395 #endif /* CONFIG_DEBUG_LINUX_TRACING */
396
397 if (level < wpa_debug_level)
398 return;
399 #ifdef CONFIG_ANDROID_LOG
400 {
401 const char *display;
402 char *strbuf = NULL;
403 size_t slen = len;
404 if (buf == NULL) {
405 display = " [NULL]";
406 } else if (len == 0) {
407 display = "";
408 } else if (show && len) {
409 /* Limit debug message length for Android log */
410 if (slen > 32)
411 slen = 32;
412 strbuf = os_malloc(1 + 3 * slen);
413 if (strbuf == NULL) {
414 wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
415 "allocate message buffer");
416 return;
417 }
418
419 for (i = 0; i < slen; i++)
420 os_snprintf(&strbuf[i * 3], 4, " %02x",
421 buf[i]);
422
423 display = strbuf;
424 } else {
425 display = " [REMOVED]";
426 }
427
428 __android_log_print(wpa_to_android_level(level),
429 ANDROID_LOG_NAME,
430 "%s - hexdump(len=%lu):%s%s",
431 title, (long unsigned int) len, display,
432 len > slen ? " ..." : "");
433 bin_clear_free(strbuf, 1 + 3 * slen);
434 return;
435 }
436 #else /* CONFIG_ANDROID_LOG */
437 #ifdef CONFIG_DEBUG_SYSLOG
438 if (wpa_debug_syslog) {
439 const char *display;
440 char *strbuf = NULL;
441
442 if (buf == NULL) {
443 display = " [NULL]";
444 } else if (len == 0) {
445 display = "";
446 } else if (show && len) {
447 strbuf = os_malloc(1 + 3 * len);
448 if (strbuf == NULL) {
449 wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
450 "allocate message buffer");
451 return;
452 }
453
454 for (i = 0; i < len; i++)
455 os_snprintf(&strbuf[i * 3], 4, " %02x",
456 buf[i]);
457
458 display = strbuf;
459 } else {
460 display = " [REMOVED]";
461 }
462
463 syslog(syslog_priority(level), "%s - hexdump(len=%lu):%s",
464 title, (unsigned long) len, display);
465 bin_clear_free(strbuf, 1 + 3 * len);
466 if (only_syslog)
467 return;
468 }
469 #endif /* CONFIG_DEBUG_SYSLOG */
470 wpa_debug_print_timestamp();
471 #ifdef CONFIG_DEBUG_FILE
472 if (out_file) {
473 fprintf(out_file, "%s - hexdump(len=%lu):",
474 title, (unsigned long) len);
475 if (buf == NULL) {
476 fprintf(out_file, " [NULL]");
477 } else if (show) {
478 for (i = 0; i < len; i++)
479 fprintf(out_file, " %02x", buf[i]);
480 } else {
481 fprintf(out_file, " [REMOVED]");
482 }
483 fprintf(out_file, "\n");
484 }
485 #endif /* CONFIG_DEBUG_FILE */
486 if (!wpa_debug_syslog && !out_file) {
487 printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
488 if (buf == NULL) {
489 printf(" [NULL]");
490 } else if (show) {
491 for (i = 0; i < len; i++)
492 printf(" %02x", buf[i]);
493 } else {
494 printf(" [REMOVED]");
495 }
496 printf("\n");
497 }
498 #endif /* CONFIG_ANDROID_LOG */
499 #endif /* CONFIG_WPA_NO_LOG */
500 }
501
wpa_hexdump(int level,const char * title,const void * buf,size_t len)502 void wpa_hexdump(int level, const char *title, const void *buf, size_t len)
503 {
504 _wpa_hexdump(level, title, buf, len, 1, 0);
505 }
506
507
wpa_hexdump_key(int level,const char * title,const void * buf,size_t len)508 void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len)
509 {
510 _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys, 0);
511 }
512
513
_wpa_hexdump_ascii(int level,const char * title,const void * buf,size_t len,int show)514 static void _wpa_hexdump_ascii(int level, const char *title, const void *buf,
515 size_t len, int show)
516 {
517 #ifdef CONFIG_WPA_NO_LOG
518 return;
519 #else
520 size_t i, llen;
521 const u8 *pos = buf;
522 const size_t line_len = 16;
523
524 #ifdef CONFIG_DEBUG_LINUX_TRACING
525 if (wpa_debug_tracing_file != NULL) {
526 fprintf(wpa_debug_tracing_file,
527 WPAS_TRACE_PFX "%s - hexdump_ascii(len=%lu):",
528 level, title, (unsigned long) len);
529 if (buf == NULL) {
530 fprintf(wpa_debug_tracing_file, " [NULL]\n");
531 } else if (!show) {
532 fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
533 } else {
534 /* can do ascii processing in userspace */
535 for (i = 0; i < len; i++)
536 fprintf(wpa_debug_tracing_file,
537 " %02x", pos[i]);
538 }
539 fflush(wpa_debug_tracing_file);
540 }
541 #endif /* CONFIG_DEBUG_LINUX_TRACING */
542
543 if (level < wpa_debug_level)
544 return;
545 #ifdef CONFIG_ANDROID_LOG
546 _wpa_hexdump(level, title, buf, len, show, 0);
547 #else /* CONFIG_ANDROID_LOG */
548 #ifdef CONFIG_DEBUG_SYSLOG
549 if (wpa_debug_syslog)
550 _wpa_hexdump(level, title, buf, len, show, 1);
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 goto file_done;
560 }
561 if (buf == NULL) {
562 fprintf(out_file,
563 "%s - hexdump_ascii(len=%lu): [NULL]\n",
564 title, (unsigned long) len);
565 goto file_done;
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 }
590 file_done:
591 #endif /* CONFIG_DEBUG_FILE */
592 if (!wpa_debug_syslog && !out_file) {
593 if (!show) {
594 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
595 title, (unsigned long) len);
596 return;
597 }
598 if (buf == NULL) {
599 printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
600 title, (unsigned long) len);
601 return;
602 }
603 printf("%s - hexdump_ascii(len=%lu):\n", title,
604 (unsigned long) len);
605 while (len) {
606 llen = len > line_len ? line_len : len;
607 printf(" ");
608 for (i = 0; i < llen; i++)
609 printf(" %02x", pos[i]);
610 for (i = llen; i < line_len; i++)
611 printf(" ");
612 printf(" ");
613 for (i = 0; i < llen; i++) {
614 if (isprint(pos[i]))
615 printf("%c", pos[i]);
616 else
617 printf("_");
618 }
619 for (i = llen; i < line_len; i++)
620 printf(" ");
621 printf("\n");
622 pos += llen;
623 len -= llen;
624 }
625 }
626 #endif /* CONFIG_ANDROID_LOG */
627 #endif /* CONFIG_WPA_NO_LOG */
628 }
629
630
wpa_hexdump_ascii(int level,const char * title,const void * buf,size_t len)631 void wpa_hexdump_ascii(int level, const char *title, const void *buf,
632 size_t len)
633 {
634 _wpa_hexdump_ascii(level, title, buf, len, 1);
635 }
636
637
wpa_hexdump_ascii_key(int level,const char * title,const void * buf,size_t len)638 void wpa_hexdump_ascii_key(int level, const char *title, const void *buf,
639 size_t len)
640 {
641 _wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
642 }
643
644
645 #ifdef CONFIG_DEBUG_FILE
646 static char *last_path = NULL;
647 #endif /* CONFIG_DEBUG_FILE */
648
wpa_debug_reopen_file(void)649 int wpa_debug_reopen_file(void)
650 {
651 #ifdef CONFIG_DEBUG_FILE
652 int rv;
653 char *tmp;
654
655 if (!last_path)
656 return 0; /* logfile not used */
657
658 tmp = os_strdup(last_path);
659 if (!tmp)
660 return -1;
661
662 wpa_debug_close_file();
663 rv = wpa_debug_open_file(tmp);
664 os_free(tmp);
665 return rv;
666 #else /* CONFIG_DEBUG_FILE */
667 return 0;
668 #endif /* CONFIG_DEBUG_FILE */
669 }
670
671
wpa_debug_open_file(const char * path)672 int wpa_debug_open_file(const char *path)
673 {
674 #ifdef CONFIG_DEBUG_FILE
675 int out_fd;
676
677 if (!path)
678 return 0;
679
680 if (last_path == NULL || os_strcmp(last_path, path) != 0) {
681 /* Save our path to enable re-open */
682 os_free(last_path);
683 last_path = os_strdup(path);
684 }
685
686 out_fd = open(path, O_CREAT | O_APPEND | O_WRONLY,
687 S_IRUSR | S_IWUSR | S_IRGRP);
688 if (out_fd < 0) {
689 wpa_printf(MSG_ERROR,
690 "%s: Failed to open output file descriptor, using standard output",
691 __func__);
692 return -1;
693 }
694
695 #ifdef __linux__
696 if (fcntl(out_fd, F_SETFD, FD_CLOEXEC) < 0) {
697 wpa_printf(MSG_DEBUG,
698 "%s: Failed to set FD_CLOEXEC - continue without: %s",
699 __func__, strerror(errno));
700 }
701 #endif /* __linux__ */
702
703 out_file = fdopen(out_fd, "a");
704 if (out_file == NULL) {
705 wpa_printf(MSG_ERROR, "wpa_debug_open_file: Failed to open "
706 "output file, using standard output");
707 close(out_fd);
708 return -1;
709 }
710 #ifndef _WIN32
711 setvbuf(out_file, NULL, _IOLBF, 0);
712 #endif /* _WIN32 */
713 #else /* CONFIG_DEBUG_FILE */
714 (void)path;
715 #endif /* CONFIG_DEBUG_FILE */
716 return 0;
717 }
718
719
wpa_debug_close_file(void)720 void wpa_debug_close_file(void)
721 {
722 #ifdef CONFIG_DEBUG_FILE
723 if (!out_file)
724 return;
725 fclose(out_file);
726 out_file = NULL;
727 os_free(last_path);
728 last_path = NULL;
729 #endif /* CONFIG_DEBUG_FILE */
730 }
731
732
wpa_debug_setup_stdout(void)733 void wpa_debug_setup_stdout(void) __attribute__((no_sanitize("cfi")))
734 {
735 #ifndef _WIN32
736 setvbuf(stdout, NULL, _IOLBF, 0);
737 #endif /* _WIN32 */
738 }
739
740 #endif /* CONFIG_NO_STDOUT_DEBUG */
741
742
743 #ifndef CONFIG_NO_WPA_MSG
744 static wpa_msg_cb_func wpa_msg_cb = NULL;
745
wpa_msg_register_cb(wpa_msg_cb_func func)746 void wpa_msg_register_cb(wpa_msg_cb_func func)
747 {
748 wpa_msg_cb = func;
749 }
750
751
752 static wpa_msg_get_ifname_func wpa_msg_ifname_cb = NULL;
753
wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)754 void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)
755 {
756 wpa_msg_ifname_cb = func;
757 }
758
759
wpa_msg(void * ctx,int level,const char * fmt,...)760 void wpa_msg(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
761 {
762 va_list ap;
763 char *buf;
764 int buflen;
765 int len;
766 char prefix[130];
767
768 va_start(ap, fmt);
769 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
770 va_end(ap);
771
772 buf = os_malloc(buflen);
773 if (buf == NULL) {
774 wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
775 "buffer");
776 return;
777 }
778 va_start(ap, fmt);
779 prefix[0] = '\0';
780 if (wpa_msg_ifname_cb) {
781 const char *ifname = wpa_msg_ifname_cb(ctx);
782 if (ifname) {
783 int res = os_snprintf(prefix, sizeof(prefix), "%s: ",
784 ifname);
785 if (os_snprintf_error(sizeof(prefix), res))
786 prefix[0] = '\0';
787 }
788 }
789 len = vsnprintf(buf, buflen, fmt, ap);
790 va_end(ap);
791 wpa_printf(level, "%s%s", prefix, buf);
792 if (wpa_msg_cb)
793 wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
794 bin_clear_free(buf, buflen);
795 }
796
797
wpa_msg_ctrl(void * ctx,int level,const char * fmt,...)798 void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
799 {
800 va_list ap;
801 char *buf;
802 int buflen;
803 int len;
804
805 if (!wpa_msg_cb)
806 return;
807
808 va_start(ap, fmt);
809 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
810 va_end(ap);
811
812 buf = os_malloc(buflen);
813 if (buf == NULL) {
814 wpa_printf(MSG_ERROR, "wpa_msg_ctrl: Failed to allocate "
815 "message buffer");
816 return;
817 }
818 va_start(ap, fmt);
819 len = vsnprintf(buf, buflen, fmt, ap);
820 va_end(ap);
821 wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
822 bin_clear_free(buf, buflen);
823 }
824
825
wpa_msg_global(void * ctx,int level,const char * fmt,...)826 void wpa_msg_global(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
827 {
828 va_list ap;
829 char *buf;
830 int buflen;
831 int len;
832
833 va_start(ap, fmt);
834 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
835 va_end(ap);
836
837 buf = os_malloc(buflen);
838 if (buf == NULL) {
839 wpa_printf(MSG_ERROR, "wpa_msg_global: Failed to allocate "
840 "message buffer");
841 return;
842 }
843 va_start(ap, fmt);
844 len = vsnprintf(buf, buflen, fmt, ap);
845 va_end(ap);
846 wpa_printf(level, "%s", buf);
847 if (wpa_msg_cb)
848 wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
849 bin_clear_free(buf, buflen);
850 }
851
852
wpa_msg_global_ctrl(void * ctx,int level,const char * fmt,...)853 void wpa_msg_global_ctrl(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
854 {
855 va_list ap;
856 char *buf;
857 int buflen;
858 int len;
859
860 if (!wpa_msg_cb)
861 return;
862
863 va_start(ap, fmt);
864 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
865 va_end(ap);
866
867 buf = os_malloc(buflen);
868 if (buf == NULL) {
869 wpa_printf(MSG_ERROR,
870 "wpa_msg_global_ctrl: Failed to allocate message buffer");
871 return;
872 }
873 va_start(ap, fmt);
874 len = vsnprintf(buf, buflen, fmt, ap);
875 va_end(ap);
876 wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
877 bin_clear_free(buf, buflen);
878 }
879
880
wpa_msg_no_global(void * ctx,int level,const char * fmt,...)881 void wpa_msg_no_global(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
882 {
883 va_list ap;
884 char *buf;
885 int buflen;
886 int len;
887
888 va_start(ap, fmt);
889 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
890 va_end(ap);
891
892 buf = os_malloc(buflen);
893 if (buf == NULL) {
894 wpa_printf(MSG_ERROR, "wpa_msg_no_global: Failed to allocate "
895 "message buffer");
896 return;
897 }
898 va_start(ap, fmt);
899 len = vsnprintf(buf, buflen, fmt, ap);
900 va_end(ap);
901 wpa_printf(level, "%s", buf);
902 if (wpa_msg_cb)
903 wpa_msg_cb(ctx, level, WPA_MSG_NO_GLOBAL, buf, len);
904 bin_clear_free(buf, buflen);
905 }
906
907
wpa_msg_global_only(void * ctx,int level,const char * fmt,...)908 void wpa_msg_global_only(void *ctx, int level, const char *fmt, ...)
909 {
910 va_list ap;
911 char *buf;
912 int buflen;
913 int len;
914
915 va_start(ap, fmt);
916 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
917 va_end(ap);
918
919 buf = os_malloc(buflen);
920 if (buf == NULL) {
921 wpa_printf(MSG_ERROR, "%s: Failed to allocate message buffer",
922 __func__);
923 return;
924 }
925 va_start(ap, fmt);
926 len = vsnprintf(buf, buflen, fmt, ap);
927 va_end(ap);
928 wpa_printf(level, "%s", buf);
929 if (wpa_msg_cb)
930 wpa_msg_cb(ctx, level, WPA_MSG_ONLY_GLOBAL, buf, len);
931 os_free(buf);
932 }
933
934 #endif /* CONFIG_NO_WPA_MSG */
935
936
937 #ifndef CONFIG_NO_HOSTAPD_LOGGER
938 static hostapd_logger_cb_func hostapd_logger_cb = NULL;
939
hostapd_logger_register_cb(hostapd_logger_cb_func func)940 void hostapd_logger_register_cb(hostapd_logger_cb_func func)
941 {
942 hostapd_logger_cb = func;
943 }
944
945
hostapd_logger(void * ctx,const u8 * addr,unsigned int module,int level,const char * fmt,...)946 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
947 const char *fmt, ...)
948 {
949 va_list ap;
950 char *buf;
951 int buflen;
952 int len;
953
954 va_start(ap, fmt);
955 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
956 va_end(ap);
957
958 buf = os_malloc(buflen);
959 if (buf == NULL) {
960 wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
961 "message buffer");
962 return;
963 }
964 va_start(ap, fmt);
965 len = vsnprintf(buf, buflen, fmt, ap);
966 va_end(ap);
967 if (hostapd_logger_cb)
968 hostapd_logger_cb(ctx, addr, module, level, buf, len);
969 else if (addr)
970 wpa_printf(MSG_DEBUG, "hostapd_logger: STA " MACSTR " - %s",
971 MAC2STR(addr), buf);
972 else
973 wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
974 bin_clear_free(buf, buflen);
975 }
976 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
977
978
debug_level_str(int level)979 const char * debug_level_str(int level)
980 {
981 switch (level) {
982 case MSG_EXCESSIVE:
983 return "EXCESSIVE";
984 case MSG_MSGDUMP:
985 return "MSGDUMP";
986 case MSG_DEBUG:
987 return "DEBUG";
988 case MSG_INFO:
989 return "INFO";
990 case MSG_WARNING:
991 return "WARNING";
992 case MSG_ERROR:
993 return "ERROR";
994 default:
995 return "?";
996 }
997 }
998
999
str_to_debug_level(const char * s)1000 int str_to_debug_level(const char *s)
1001 {
1002 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1003 return MSG_EXCESSIVE;
1004 if (os_strcasecmp(s, "MSGDUMP") == 0)
1005 return MSG_MSGDUMP;
1006 if (os_strcasecmp(s, "DEBUG") == 0)
1007 return MSG_DEBUG;
1008 if (os_strcasecmp(s, "INFO") == 0)
1009 return MSG_INFO;
1010 if (os_strcasecmp(s, "WARNING") == 0)
1011 return MSG_WARNING;
1012 if (os_strcasecmp(s, "ERROR") == 0)
1013 return MSG_ERROR;
1014 return -1;
1015 }
1016