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