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