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