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