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#ifndef WPA_DEBUG_H
10#define WPA_DEBUG_H
11
12#include "wpabuf.h"
13
14extern int wpa_debug_level;
15extern int wpa_debug_show_keys;
16extern int wpa_debug_timestamp;
17
18/* Debugging function - conditional printf and hex dump. Driver wrappers can
19 * use these for debugging purposes. */
20
21enum {
22	MSG_EXCESSIVE, MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, MSG_ERROR
23};
24
25#ifdef CONFIG_NO_STDOUT_DEBUG
26
27#define wpa_debug_print_timestamp() do { } while (0)
28#define wpa_printf(args...) do { } while (0)
29#define wpa_hexdump(l,t,b,le) do { } while (0)
30#define wpa_hexdump_buf(l,t,b) do { } while (0)
31#define wpa_hexdump_key(l,t,b,le) do { } while (0)
32#define wpa_hexdump_buf_key(l,t,b) do { } while (0)
33#define wpa_hexdump_ascii(l,t,b,le) do { } while (0)
34#define wpa_hexdump_ascii_key(l,t,b,le) do { } while (0)
35#define wpa_debug_open_file(p) do { } while (0)
36#define wpa_debug_close_file() do { } while (0)
37#define wpa_debug_setup_stdout() do { } while (0)
38#define wpa_dbg(args...) do { } while (0)
39
40static inline int wpa_debug_reopen_file(void)
41{
42	return 0;
43}
44
45#else /* CONFIG_NO_STDOUT_DEBUG */
46
47int wpa_debug_open_file(const char *path);
48int wpa_debug_reopen_file(void);
49void wpa_debug_close_file(void);
50void wpa_debug_setup_stdout(void);
51
52/**
53 * wpa_debug_printf_timestamp - Print timestamp for debug output
54 *
55 * This function prints a timestamp in seconds_from_1970.microsoconds
56 * format if debug output has been configured to include timestamps in debug
57 * messages.
58 */
59void wpa_debug_print_timestamp(void);
60
61/**
62 * wpa_printf - conditional printf
63 * @level: priority level (MSG_*) of the message
64 * @fmt: printf format string, followed by optional arguments
65 *
66 * This function is used to print conditional debugging and error messages. The
67 * output may be directed to stdout, stderr, and/or syslog based on
68 * configuration.
69 *
70 * Note: New line '\n' is added to the end of the text when printing to stdout.
71 */
72void wpa_printf(int level, const char *fmt, ...)
73PRINTF_FORMAT(2, 3);
74
75/**
76 * wpa_hexdump - conditional hex dump
77 * @level: priority level (MSG_*) of the message
78 * @title: title of for the message
79 * @buf: data buffer to be dumped
80 * @len: length of the buf
81 *
82 * This function is used to print conditional debugging and error messages. The
83 * output may be directed to stdout, stderr, and/or syslog based on
84 * configuration. The contents of buf is printed out has hex dump.
85 */
86void wpa_hexdump(int level, const char *title, const void *buf, size_t len);
87
88static inline void wpa_hexdump_buf(int level, const char *title,
89				   const struct wpabuf *buf)
90{
91	wpa_hexdump(level, title, buf ? wpabuf_head(buf) : NULL,
92		    buf ? wpabuf_len(buf) : 0);
93}
94
95/**
96 * wpa_hexdump_key - conditional hex dump, hide keys
97 * @level: priority level (MSG_*) of the message
98 * @title: title of for the message
99 * @buf: data buffer to be dumped
100 * @len: length of the buf
101 *
102 * This function is used to print conditional debugging and error messages. The
103 * output may be directed to stdout, stderr, and/or syslog based on
104 * configuration. The contents of buf is printed out has hex dump. This works
105 * like wpa_hexdump(), but by default, does not include secret keys (passwords,
106 * etc.) in debug output.
107 */
108void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len);
109
110static inline void wpa_hexdump_buf_key(int level, const char *title,
111				       const struct wpabuf *buf)
112{
113	wpa_hexdump_key(level, title, buf ? wpabuf_head(buf) : NULL,
114			buf ? wpabuf_len(buf) : 0);
115}
116
117/**
118 * wpa_hexdump_ascii - conditional hex dump
119 * @level: priority level (MSG_*) of the message
120 * @title: title of for the message
121 * @buf: data buffer to be dumped
122 * @len: length of the buf
123 *
124 * This function is used to print conditional debugging and error messages. The
125 * output may be directed to stdout, stderr, and/or syslog based on
126 * configuration. The contents of buf is printed out has hex dump with both
127 * the hex numbers and ASCII characters (for printable range) are shown. 16
128 * bytes per line will be shown.
129 */
130void wpa_hexdump_ascii(int level, const char *title, const void *buf,
131		       size_t len);
132
133/**
134 * wpa_hexdump_ascii_key - conditional hex dump, hide keys
135 * @level: priority level (MSG_*) of the message
136 * @title: title of for the message
137 * @buf: data buffer to be dumped
138 * @len: length of the buf
139 *
140 * This function is used to print conditional debugging and error messages. The
141 * output may be directed to stdout, stderr, and/or syslog based on
142 * configuration. The contents of buf is printed out has hex dump with both
143 * the hex numbers and ASCII characters (for printable range) are shown. 16
144 * bytes per line will be shown. This works like wpa_hexdump_ascii(), but by
145 * default, does not include secret keys (passwords, etc.) in debug output.
146 */
147void wpa_hexdump_ascii_key(int level, const char *title, const void *buf,
148			   size_t len);
149
150/*
151 * wpa_dbg() behaves like wpa_msg(), but it can be removed from build to reduce
152 * binary size. As such, it should be used with debugging messages that are not
153 * needed in the control interface while wpa_msg() has to be used for anything
154 * that needs to shown to control interface monitors.
155 */
156#define wpa_dbg(args...) wpa_msg(args)
157
158#endif /* CONFIG_NO_STDOUT_DEBUG */
159
160
161#ifdef CONFIG_NO_WPA_MSG
162#define wpa_msg(args...) do { } while (0)
163#define wpa_msg_ctrl(args...) do { } while (0)
164#define wpa_msg_global(args...) do { } while (0)
165#define wpa_msg_global_ctrl(args...) do { } while (0)
166#define wpa_msg_no_global(args...) do { } while (0)
167#define wpa_msg_register_cb(f) do { } while (0)
168#define wpa_msg_register_ifname_cb(f) do { } while (0)
169#else /* CONFIG_NO_WPA_MSG */
170/**
171 * wpa_msg - Conditional printf for default target and ctrl_iface monitors
172 * @ctx: Pointer to context data; this is the ctx variable registered
173 *	with struct wpa_driver_ops::init()
174 * @level: priority level (MSG_*) of the message
175 * @fmt: printf format string, followed by optional arguments
176 *
177 * This function is used to print conditional debugging and error messages. The
178 * output may be directed to stdout, stderr, and/or syslog based on
179 * configuration. This function is like wpa_printf(), but it also sends the
180 * same message to all attached ctrl_iface monitors.
181 *
182 * Note: New line '\n' is added to the end of the text when printing to stdout.
183 */
184void wpa_msg(void *ctx, int level, const char *fmt, ...) PRINTF_FORMAT(3, 4);
185
186/**
187 * wpa_msg_ctrl - Conditional printf for ctrl_iface monitors
188 * @ctx: Pointer to context data; this is the ctx variable registered
189 *	with struct wpa_driver_ops::init()
190 * @level: priority level (MSG_*) of the message
191 * @fmt: printf format string, followed by optional arguments
192 *
193 * This function is used to print conditional debugging and error messages.
194 * This function is like wpa_msg(), but it sends the output only to the
195 * attached ctrl_iface monitors. In other words, it can be used for frequent
196 * events that do not need to be sent to syslog.
197 */
198void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
199PRINTF_FORMAT(3, 4);
200
201/**
202 * wpa_msg_global - Global printf for ctrl_iface monitors
203 * @ctx: Pointer to context data; this is the ctx variable registered
204 *	with struct wpa_driver_ops::init()
205 * @level: priority level (MSG_*) of the message
206 * @fmt: printf format string, followed by optional arguments
207 *
208 * This function is used to print conditional debugging and error messages.
209 * This function is like wpa_msg(), but it sends the output as a global event,
210 * i.e., without being specific to an interface. For backwards compatibility,
211 * an old style event is also delivered on one of the interfaces (the one
212 * specified by the context data).
213 */
214void wpa_msg_global(void *ctx, int level, const char *fmt, ...)
215PRINTF_FORMAT(3, 4);
216
217/**
218 * wpa_msg_global_ctrl - Conditional global printf for ctrl_iface monitors
219 * @ctx: Pointer to context data; this is the ctx variable registered
220 *	with struct wpa_driver_ops::init()
221 * @level: priority level (MSG_*) of the message
222 * @fmt: printf format string, followed by optional arguments
223 *
224 * This function is used to print conditional debugging and error messages.
225 * This function is like wpa_msg_global(), but it sends the output only to the
226 * attached global ctrl_iface monitors. In other words, it can be used for
227 * frequent events that do not need to be sent to syslog.
228 */
229void wpa_msg_global_ctrl(void *ctx, int level, const char *fmt, ...)
230PRINTF_FORMAT(3, 4);
231
232/**
233 * wpa_msg_no_global - Conditional printf for ctrl_iface monitors
234 * @ctx: Pointer to context data; this is the ctx variable registered
235 *	with struct wpa_driver_ops::init()
236 * @level: priority level (MSG_*) of the message
237 * @fmt: printf format string, followed by optional arguments
238 *
239 * This function is used to print conditional debugging and error messages.
240 * This function is like wpa_msg(), but it does not send the output as a global
241 * event.
242 */
243void wpa_msg_no_global(void *ctx, int level, const char *fmt, ...)
244PRINTF_FORMAT(3, 4);
245
246enum wpa_msg_type {
247	WPA_MSG_PER_INTERFACE,
248	WPA_MSG_GLOBAL,
249	WPA_MSG_NO_GLOBAL,
250};
251
252typedef void (*wpa_msg_cb_func)(void *ctx, int level, enum wpa_msg_type type,
253				const char *txt, size_t len);
254
255/**
256 * wpa_msg_register_cb - Register callback function for wpa_msg() messages
257 * @func: Callback function (%NULL to unregister)
258 */
259void wpa_msg_register_cb(wpa_msg_cb_func func);
260
261typedef const char * (*wpa_msg_get_ifname_func)(void *ctx);
262void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func);
263
264#endif /* CONFIG_NO_WPA_MSG */
265
266#ifdef CONFIG_NO_HOSTAPD_LOGGER
267#define hostapd_logger(args...) do { } while (0)
268#define hostapd_logger_register_cb(f) do { } while (0)
269#else /* CONFIG_NO_HOSTAPD_LOGGER */
270void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
271		    const char *fmt, ...) PRINTF_FORMAT(5, 6);
272
273typedef void (*hostapd_logger_cb_func)(void *ctx, const u8 *addr,
274				       unsigned int module, int level,
275				       const char *txt, size_t len);
276
277/**
278 * hostapd_logger_register_cb - Register callback function for hostapd_logger()
279 * @func: Callback function (%NULL to unregister)
280 */
281void hostapd_logger_register_cb(hostapd_logger_cb_func func);
282#endif /* CONFIG_NO_HOSTAPD_LOGGER */
283
284#define HOSTAPD_MODULE_IEEE80211	0x00000001
285#define HOSTAPD_MODULE_IEEE8021X	0x00000002
286#define HOSTAPD_MODULE_RADIUS		0x00000004
287#define HOSTAPD_MODULE_WPA		0x00000008
288#define HOSTAPD_MODULE_DRIVER		0x00000010
289#define HOSTAPD_MODULE_IAPP		0x00000020
290#define HOSTAPD_MODULE_MLME		0x00000040
291
292enum hostapd_logger_level {
293	HOSTAPD_LEVEL_DEBUG_VERBOSE = 0,
294	HOSTAPD_LEVEL_DEBUG = 1,
295	HOSTAPD_LEVEL_INFO = 2,
296	HOSTAPD_LEVEL_NOTICE = 3,
297	HOSTAPD_LEVEL_WARNING = 4
298};
299
300
301#ifdef CONFIG_DEBUG_SYSLOG
302
303void wpa_debug_open_syslog(void);
304void wpa_debug_close_syslog(void);
305
306#else /* CONFIG_DEBUG_SYSLOG */
307
308static inline void wpa_debug_open_syslog(void)
309{
310}
311
312static inline void wpa_debug_close_syslog(void)
313{
314}
315
316#endif /* CONFIG_DEBUG_SYSLOG */
317
318#ifdef CONFIG_DEBUG_LINUX_TRACING
319
320int wpa_debug_open_linux_tracing(void);
321void wpa_debug_close_linux_tracing(void);
322
323#else /* CONFIG_DEBUG_LINUX_TRACING */
324
325static inline int wpa_debug_open_linux_tracing(void)
326{
327	return 0;
328}
329
330static inline void wpa_debug_close_linux_tracing(void)
331{
332}
333
334#endif /* CONFIG_DEBUG_LINUX_TRACING */
335
336
337#ifdef EAPOL_TEST
338#define WPA_ASSERT(a)						       \
339	do {							       \
340		if (!(a)) {					       \
341			printf("WPA_ASSERT FAILED '" #a "' "	       \
342			       "%s %s:%d\n",			       \
343			       __FUNCTION__, __FILE__, __LINE__);      \
344			exit(1);				       \
345		}						       \
346	} while (0)
347#else
348#define WPA_ASSERT(a) do { } while (0)
349#endif
350
351#endif /* WPA_DEBUG_H */
352