xref: /netbsd-src/external/bsd/wpa/dist/src/utils/wpa_debug.h (revision bb6183629cf165db498d8e1f4e2de129f7efb21c)
18dbcf02cSchristos /*
28dbcf02cSchristos  * wpa_supplicant/hostapd / Debug prints
33c260e60Schristos  * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
48dbcf02cSchristos  *
5e604d861Schristos  * This software may be distributed under the terms of the BSD license.
6e604d861Schristos  * See README for more details.
78dbcf02cSchristos  */
88dbcf02cSchristos 
98dbcf02cSchristos #ifndef WPA_DEBUG_H
108dbcf02cSchristos #define WPA_DEBUG_H
118dbcf02cSchristos 
128dbcf02cSchristos #include "wpabuf.h"
138dbcf02cSchristos 
143c260e60Schristos extern int wpa_debug_level;
153c260e60Schristos extern int wpa_debug_show_keys;
163c260e60Schristos extern int wpa_debug_timestamp;
170a73ee0aSchristos extern int wpa_debug_syslog;
183c260e60Schristos 
198dbcf02cSchristos /* Debugging function - conditional printf and hex dump. Driver wrappers can
208dbcf02cSchristos  * use these for debugging purposes. */
218dbcf02cSchristos 
22111b9fd8Schristos enum {
23111b9fd8Schristos 	MSG_EXCESSIVE, MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, MSG_ERROR
24111b9fd8Schristos };
25111b9fd8Schristos 
268dbcf02cSchristos #ifdef CONFIG_NO_STDOUT_DEBUG
278dbcf02cSchristos 
288dbcf02cSchristos #define wpa_debug_print_timestamp() do { } while (0)
298dbcf02cSchristos #define wpa_printf(args...) do { } while (0)
308dbcf02cSchristos #define wpa_hexdump(l,t,b,le) do { } while (0)
318dbcf02cSchristos #define wpa_hexdump_buf(l,t,b) do { } while (0)
328dbcf02cSchristos #define wpa_hexdump_key(l,t,b,le) do { } while (0)
338dbcf02cSchristos #define wpa_hexdump_buf_key(l,t,b) do { } while (0)
348dbcf02cSchristos #define wpa_hexdump_ascii(l,t,b,le) do { } while (0)
358dbcf02cSchristos #define wpa_hexdump_ascii_key(l,t,b,le) do { } while (0)
368dbcf02cSchristos #define wpa_debug_open_file(p) do { } while (0)
378dbcf02cSchristos #define wpa_debug_close_file() do { } while (0)
38bb610346Schristos #define wpa_debug_setup_stdout() do { } while (0)
39111b9fd8Schristos #define wpa_dbg(args...) do { } while (0)
40111b9fd8Schristos 
41111b9fd8Schristos static inline int wpa_debug_reopen_file(void)
42111b9fd8Schristos {
43111b9fd8Schristos 	return 0;
44111b9fd8Schristos }
458dbcf02cSchristos 
468dbcf02cSchristos #else /* CONFIG_NO_STDOUT_DEBUG */
478dbcf02cSchristos 
488dbcf02cSchristos int wpa_debug_open_file(const char *path);
49111b9fd8Schristos int wpa_debug_reopen_file(void);
508dbcf02cSchristos void wpa_debug_close_file(void);
51bb610346Schristos void wpa_debug_setup_stdout(void);
52*bb618362Schristos void wpa_debug_stop_log(void);
538dbcf02cSchristos 
548dbcf02cSchristos /**
558dbcf02cSchristos  * wpa_debug_printf_timestamp - Print timestamp for debug output
568dbcf02cSchristos  *
578dbcf02cSchristos  * This function prints a timestamp in seconds_from_1970.microsoconds
588dbcf02cSchristos  * format if debug output has been configured to include timestamps in debug
598dbcf02cSchristos  * messages.
608dbcf02cSchristos  */
618dbcf02cSchristos void wpa_debug_print_timestamp(void);
628dbcf02cSchristos 
638dbcf02cSchristos /**
648dbcf02cSchristos  * wpa_printf - conditional printf
658dbcf02cSchristos  * @level: priority level (MSG_*) of the message
668dbcf02cSchristos  * @fmt: printf format string, followed by optional arguments
678dbcf02cSchristos  *
688dbcf02cSchristos  * This function is used to print conditional debugging and error messages. The
698dbcf02cSchristos  * output may be directed to stdout, stderr, and/or syslog based on
708dbcf02cSchristos  * configuration.
718dbcf02cSchristos  *
728dbcf02cSchristos  * Note: New line '\n' is added to the end of the text when printing to stdout.
738dbcf02cSchristos  */
748dbcf02cSchristos void wpa_printf(int level, const char *fmt, ...)
758dbcf02cSchristos PRINTF_FORMAT(2, 3);
768dbcf02cSchristos 
778dbcf02cSchristos /**
788dbcf02cSchristos  * wpa_hexdump - conditional hex dump
798dbcf02cSchristos  * @level: priority level (MSG_*) of the message
808dbcf02cSchristos  * @title: title of for the message
818dbcf02cSchristos  * @buf: data buffer to be dumped
828dbcf02cSchristos  * @len: length of the buf
838dbcf02cSchristos  *
848dbcf02cSchristos  * This function is used to print conditional debugging and error messages. The
858dbcf02cSchristos  * output may be directed to stdout, stderr, and/or syslog based on
868dbcf02cSchristos  * configuration. The contents of buf is printed out has hex dump.
878dbcf02cSchristos  */
883c260e60Schristos void wpa_hexdump(int level, const char *title, const void *buf, size_t len);
898dbcf02cSchristos 
908dbcf02cSchristos static inline void wpa_hexdump_buf(int level, const char *title,
918dbcf02cSchristos 				   const struct wpabuf *buf)
928dbcf02cSchristos {
93111b9fd8Schristos 	wpa_hexdump(level, title, buf ? wpabuf_head(buf) : NULL,
94111b9fd8Schristos 		    buf ? wpabuf_len(buf) : 0);
958dbcf02cSchristos }
968dbcf02cSchristos 
978dbcf02cSchristos /**
988dbcf02cSchristos  * wpa_hexdump_key - conditional hex dump, hide keys
998dbcf02cSchristos  * @level: priority level (MSG_*) of the message
1008dbcf02cSchristos  * @title: title of for the message
1018dbcf02cSchristos  * @buf: data buffer to be dumped
1028dbcf02cSchristos  * @len: length of the buf
1038dbcf02cSchristos  *
1048dbcf02cSchristos  * This function is used to print conditional debugging and error messages. The
1058dbcf02cSchristos  * output may be directed to stdout, stderr, and/or syslog based on
1068dbcf02cSchristos  * configuration. The contents of buf is printed out has hex dump. This works
1078dbcf02cSchristos  * like wpa_hexdump(), but by default, does not include secret keys (passwords,
1088dbcf02cSchristos  * etc.) in debug output.
1098dbcf02cSchristos  */
1103c260e60Schristos void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len);
1118dbcf02cSchristos 
1128dbcf02cSchristos static inline void wpa_hexdump_buf_key(int level, const char *title,
1138dbcf02cSchristos 				       const struct wpabuf *buf)
1148dbcf02cSchristos {
115e604d861Schristos 	wpa_hexdump_key(level, title, buf ? wpabuf_head(buf) : NULL,
116111b9fd8Schristos 			buf ? wpabuf_len(buf) : 0);
1178dbcf02cSchristos }
1188dbcf02cSchristos 
1198dbcf02cSchristos /**
1208dbcf02cSchristos  * wpa_hexdump_ascii - conditional hex dump
1218dbcf02cSchristos  * @level: priority level (MSG_*) of the message
1228dbcf02cSchristos  * @title: title of for the message
1238dbcf02cSchristos  * @buf: data buffer to be dumped
1248dbcf02cSchristos  * @len: length of the buf
1258dbcf02cSchristos  *
1268dbcf02cSchristos  * This function is used to print conditional debugging and error messages. The
1278dbcf02cSchristos  * output may be directed to stdout, stderr, and/or syslog based on
1288dbcf02cSchristos  * configuration. The contents of buf is printed out has hex dump with both
1298dbcf02cSchristos  * the hex numbers and ASCII characters (for printable range) are shown. 16
1308dbcf02cSchristos  * bytes per line will be shown.
1318dbcf02cSchristos  */
1323c260e60Schristos void wpa_hexdump_ascii(int level, const char *title, const void *buf,
1338dbcf02cSchristos 		       size_t len);
1348dbcf02cSchristos 
1358dbcf02cSchristos /**
1368dbcf02cSchristos  * wpa_hexdump_ascii_key - conditional hex dump, hide keys
1378dbcf02cSchristos  * @level: priority level (MSG_*) of the message
1388dbcf02cSchristos  * @title: title of for the message
1398dbcf02cSchristos  * @buf: data buffer to be dumped
1408dbcf02cSchristos  * @len: length of the buf
1418dbcf02cSchristos  *
1428dbcf02cSchristos  * This function is used to print conditional debugging and error messages. The
1438dbcf02cSchristos  * output may be directed to stdout, stderr, and/or syslog based on
1448dbcf02cSchristos  * configuration. The contents of buf is printed out has hex dump with both
1458dbcf02cSchristos  * the hex numbers and ASCII characters (for printable range) are shown. 16
1468dbcf02cSchristos  * bytes per line will be shown. This works like wpa_hexdump_ascii(), but by
1478dbcf02cSchristos  * default, does not include secret keys (passwords, etc.) in debug output.
1488dbcf02cSchristos  */
1493c260e60Schristos void wpa_hexdump_ascii_key(int level, const char *title, const void *buf,
1508dbcf02cSchristos 			   size_t len);
1518dbcf02cSchristos 
152111b9fd8Schristos /*
153111b9fd8Schristos  * wpa_dbg() behaves like wpa_msg(), but it can be removed from build to reduce
154111b9fd8Schristos  * binary size. As such, it should be used with debugging messages that are not
155111b9fd8Schristos  * needed in the control interface while wpa_msg() has to be used for anything
156111b9fd8Schristos  * that needs to shown to control interface monitors.
157111b9fd8Schristos  */
158111b9fd8Schristos #define wpa_dbg(args...) wpa_msg(args)
159111b9fd8Schristos 
1608dbcf02cSchristos #endif /* CONFIG_NO_STDOUT_DEBUG */
1618dbcf02cSchristos 
1628dbcf02cSchristos 
1638dbcf02cSchristos #ifdef CONFIG_NO_WPA_MSG
1648dbcf02cSchristos #define wpa_msg(args...) do { } while (0)
1658dbcf02cSchristos #define wpa_msg_ctrl(args...) do { } while (0)
1663c260e60Schristos #define wpa_msg_global(args...) do { } while (0)
1673c260e60Schristos #define wpa_msg_global_ctrl(args...) do { } while (0)
1683c260e60Schristos #define wpa_msg_no_global(args...) do { } while (0)
16936ebd06eSchristos #define wpa_msg_global_only(args...) do { } while (0)
1708dbcf02cSchristos #define wpa_msg_register_cb(f) do { } while (0)
171111b9fd8Schristos #define wpa_msg_register_ifname_cb(f) do { } while (0)
1728dbcf02cSchristos #else /* CONFIG_NO_WPA_MSG */
1738dbcf02cSchristos /**
1748dbcf02cSchristos  * wpa_msg - Conditional printf for default target and ctrl_iface monitors
1758dbcf02cSchristos  * @ctx: Pointer to context data; this is the ctx variable registered
1768dbcf02cSchristos  *	with struct wpa_driver_ops::init()
1778dbcf02cSchristos  * @level: priority level (MSG_*) of the message
1788dbcf02cSchristos  * @fmt: printf format string, followed by optional arguments
1798dbcf02cSchristos  *
1808dbcf02cSchristos  * This function is used to print conditional debugging and error messages. The
1818dbcf02cSchristos  * output may be directed to stdout, stderr, and/or syslog based on
1828dbcf02cSchristos  * configuration. This function is like wpa_printf(), but it also sends the
1838dbcf02cSchristos  * same message to all attached ctrl_iface monitors.
1848dbcf02cSchristos  *
1858dbcf02cSchristos  * Note: New line '\n' is added to the end of the text when printing to stdout.
1868dbcf02cSchristos  */
1878dbcf02cSchristos void wpa_msg(void *ctx, int level, const char *fmt, ...) PRINTF_FORMAT(3, 4);
1888dbcf02cSchristos 
1898dbcf02cSchristos /**
1908dbcf02cSchristos  * wpa_msg_ctrl - Conditional printf for ctrl_iface monitors
1918dbcf02cSchristos  * @ctx: Pointer to context data; this is the ctx variable registered
1928dbcf02cSchristos  *	with struct wpa_driver_ops::init()
1938dbcf02cSchristos  * @level: priority level (MSG_*) of the message
1948dbcf02cSchristos  * @fmt: printf format string, followed by optional arguments
1958dbcf02cSchristos  *
1968dbcf02cSchristos  * This function is used to print conditional debugging and error messages.
1978dbcf02cSchristos  * This function is like wpa_msg(), but it sends the output only to the
1988dbcf02cSchristos  * attached ctrl_iface monitors. In other words, it can be used for frequent
1998dbcf02cSchristos  * events that do not need to be sent to syslog.
2008dbcf02cSchristos  */
2018dbcf02cSchristos void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
2028dbcf02cSchristos PRINTF_FORMAT(3, 4);
2038dbcf02cSchristos 
2043c260e60Schristos /**
2053c260e60Schristos  * wpa_msg_global - Global printf for ctrl_iface monitors
2063c260e60Schristos  * @ctx: Pointer to context data; this is the ctx variable registered
2073c260e60Schristos  *	with struct wpa_driver_ops::init()
2083c260e60Schristos  * @level: priority level (MSG_*) of the message
2093c260e60Schristos  * @fmt: printf format string, followed by optional arguments
2103c260e60Schristos  *
2113c260e60Schristos  * This function is used to print conditional debugging and error messages.
2123c260e60Schristos  * This function is like wpa_msg(), but it sends the output as a global event,
2133c260e60Schristos  * i.e., without being specific to an interface. For backwards compatibility,
2143c260e60Schristos  * an old style event is also delivered on one of the interfaces (the one
2153c260e60Schristos  * specified by the context data).
2163c260e60Schristos  */
2173c260e60Schristos void wpa_msg_global(void *ctx, int level, const char *fmt, ...)
2183c260e60Schristos PRINTF_FORMAT(3, 4);
2193c260e60Schristos 
2203c260e60Schristos /**
2213c260e60Schristos  * wpa_msg_global_ctrl - Conditional global printf for ctrl_iface monitors
2223c260e60Schristos  * @ctx: Pointer to context data; this is the ctx variable registered
2233c260e60Schristos  *	with struct wpa_driver_ops::init()
2243c260e60Schristos  * @level: priority level (MSG_*) of the message
2253c260e60Schristos  * @fmt: printf format string, followed by optional arguments
2263c260e60Schristos  *
2273c260e60Schristos  * This function is used to print conditional debugging and error messages.
2283c260e60Schristos  * This function is like wpa_msg_global(), but it sends the output only to the
2293c260e60Schristos  * attached global ctrl_iface monitors. In other words, it can be used for
2303c260e60Schristos  * frequent events that do not need to be sent to syslog.
2313c260e60Schristos  */
2323c260e60Schristos void wpa_msg_global_ctrl(void *ctx, int level, const char *fmt, ...)
2333c260e60Schristos PRINTF_FORMAT(3, 4);
2343c260e60Schristos 
2353c260e60Schristos /**
2363c260e60Schristos  * wpa_msg_no_global - Conditional printf for ctrl_iface monitors
2373c260e60Schristos  * @ctx: Pointer to context data; this is the ctx variable registered
2383c260e60Schristos  *	with struct wpa_driver_ops::init()
2393c260e60Schristos  * @level: priority level (MSG_*) of the message
2403c260e60Schristos  * @fmt: printf format string, followed by optional arguments
2413c260e60Schristos  *
2423c260e60Schristos  * This function is used to print conditional debugging and error messages.
2433c260e60Schristos  * This function is like wpa_msg(), but it does not send the output as a global
2443c260e60Schristos  * event.
2453c260e60Schristos  */
2463c260e60Schristos void wpa_msg_no_global(void *ctx, int level, const char *fmt, ...)
2473c260e60Schristos PRINTF_FORMAT(3, 4);
2483c260e60Schristos 
24936ebd06eSchristos /**
25036ebd06eSchristos  * wpa_msg_global_only - Conditional printf for ctrl_iface monitors
25136ebd06eSchristos  * @ctx: Pointer to context data; this is the ctx variable registered
25236ebd06eSchristos  *	with struct wpa_driver_ops::init()
25336ebd06eSchristos  * @level: priority level (MSG_*) of the message
25436ebd06eSchristos  * @fmt: printf format string, followed by optional arguments
25536ebd06eSchristos  *
25636ebd06eSchristos  * This function is used to print conditional debugging and error messages.
25736ebd06eSchristos  * This function is like wpa_msg_global(), but it sends the output only as a
25836ebd06eSchristos  * global event.
25936ebd06eSchristos  */
26036ebd06eSchristos void wpa_msg_global_only(void *ctx, int level, const char *fmt, ...)
26136ebd06eSchristos PRINTF_FORMAT(3, 4);
26236ebd06eSchristos 
26336ebd06eSchristos enum wpa_msg_type {
26436ebd06eSchristos 	WPA_MSG_PER_INTERFACE,
26536ebd06eSchristos 	WPA_MSG_GLOBAL,
26636ebd06eSchristos 	WPA_MSG_NO_GLOBAL,
26736ebd06eSchristos 	WPA_MSG_ONLY_GLOBAL,
26836ebd06eSchristos };
26936ebd06eSchristos 
27036ebd06eSchristos typedef void (*wpa_msg_cb_func)(void *ctx, int level, enum wpa_msg_type type,
2713c260e60Schristos 				const char *txt, size_t len);
2728dbcf02cSchristos 
2738dbcf02cSchristos /**
2748dbcf02cSchristos  * wpa_msg_register_cb - Register callback function for wpa_msg() messages
2758dbcf02cSchristos  * @func: Callback function (%NULL to unregister)
2768dbcf02cSchristos  */
2778dbcf02cSchristos void wpa_msg_register_cb(wpa_msg_cb_func func);
2788dbcf02cSchristos 
279111b9fd8Schristos typedef const char * (*wpa_msg_get_ifname_func)(void *ctx);
280111b9fd8Schristos void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func);
281111b9fd8Schristos 
282111b9fd8Schristos #endif /* CONFIG_NO_WPA_MSG */
2838dbcf02cSchristos 
2848dbcf02cSchristos #ifdef CONFIG_NO_HOSTAPD_LOGGER
2858dbcf02cSchristos #define hostapd_logger(args...) do { } while (0)
2868dbcf02cSchristos #define hostapd_logger_register_cb(f) do { } while (0)
2878dbcf02cSchristos #else /* CONFIG_NO_HOSTAPD_LOGGER */
2888dbcf02cSchristos void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
2898dbcf02cSchristos 		    const char *fmt, ...) PRINTF_FORMAT(5, 6);
2908dbcf02cSchristos 
2918dbcf02cSchristos typedef void (*hostapd_logger_cb_func)(void *ctx, const u8 *addr,
2928dbcf02cSchristos 				       unsigned int module, int level,
2938dbcf02cSchristos 				       const char *txt, size_t len);
2948dbcf02cSchristos 
2958dbcf02cSchristos /**
2968dbcf02cSchristos  * hostapd_logger_register_cb - Register callback function for hostapd_logger()
2978dbcf02cSchristos  * @func: Callback function (%NULL to unregister)
2988dbcf02cSchristos  */
2998dbcf02cSchristos void hostapd_logger_register_cb(hostapd_logger_cb_func func);
3008dbcf02cSchristos #endif /* CONFIG_NO_HOSTAPD_LOGGER */
3018dbcf02cSchristos 
3028dbcf02cSchristos #define HOSTAPD_MODULE_IEEE80211	0x00000001
3038dbcf02cSchristos #define HOSTAPD_MODULE_IEEE8021X	0x00000002
3048dbcf02cSchristos #define HOSTAPD_MODULE_RADIUS		0x00000004
3058dbcf02cSchristos #define HOSTAPD_MODULE_WPA		0x00000008
3068dbcf02cSchristos #define HOSTAPD_MODULE_DRIVER		0x00000010
3078dbcf02cSchristos #define HOSTAPD_MODULE_MLME		0x00000040
3088dbcf02cSchristos 
3098dbcf02cSchristos enum hostapd_logger_level {
3108dbcf02cSchristos 	HOSTAPD_LEVEL_DEBUG_VERBOSE = 0,
3118dbcf02cSchristos 	HOSTAPD_LEVEL_DEBUG = 1,
3128dbcf02cSchristos 	HOSTAPD_LEVEL_INFO = 2,
3138dbcf02cSchristos 	HOSTAPD_LEVEL_NOTICE = 3,
3148dbcf02cSchristos 	HOSTAPD_LEVEL_WARNING = 4
3158dbcf02cSchristos };
3168dbcf02cSchristos 
3178dbcf02cSchristos 
3188dbcf02cSchristos #ifdef CONFIG_DEBUG_SYSLOG
3198dbcf02cSchristos 
3208dbcf02cSchristos void wpa_debug_open_syslog(void);
3218dbcf02cSchristos void wpa_debug_close_syslog(void);
3228dbcf02cSchristos 
3238dbcf02cSchristos #else /* CONFIG_DEBUG_SYSLOG */
3248dbcf02cSchristos 
3258dbcf02cSchristos static inline void wpa_debug_open_syslog(void)
3268dbcf02cSchristos {
3278dbcf02cSchristos }
3288dbcf02cSchristos 
3298dbcf02cSchristos static inline void wpa_debug_close_syslog(void)
3308dbcf02cSchristos {
3318dbcf02cSchristos }
3328dbcf02cSchristos 
3338dbcf02cSchristos #endif /* CONFIG_DEBUG_SYSLOG */
3348dbcf02cSchristos 
335e604d861Schristos #ifdef CONFIG_DEBUG_LINUX_TRACING
336e604d861Schristos 
337e604d861Schristos int wpa_debug_open_linux_tracing(void);
338e604d861Schristos void wpa_debug_close_linux_tracing(void);
339e604d861Schristos 
340e604d861Schristos #else /* CONFIG_DEBUG_LINUX_TRACING */
341e604d861Schristos 
342e604d861Schristos static inline int wpa_debug_open_linux_tracing(void)
343e604d861Schristos {
344e604d861Schristos 	return 0;
345e604d861Schristos }
346e604d861Schristos 
347e604d861Schristos static inline void wpa_debug_close_linux_tracing(void)
348e604d861Schristos {
349e604d861Schristos }
350e604d861Schristos 
351e604d861Schristos #endif /* CONFIG_DEBUG_LINUX_TRACING */
352e604d861Schristos 
3538dbcf02cSchristos 
3548dbcf02cSchristos #ifdef EAPOL_TEST
3558dbcf02cSchristos #define WPA_ASSERT(a)						       \
3568dbcf02cSchristos 	do {							       \
3578dbcf02cSchristos 		if (!(a)) {					       \
3588dbcf02cSchristos 			printf("WPA_ASSERT FAILED '" #a "' "	       \
3598dbcf02cSchristos 			       "%s %s:%d\n",			       \
3608dbcf02cSchristos 			       __FUNCTION__, __FILE__, __LINE__);      \
3618dbcf02cSchristos 			exit(1);				       \
3628dbcf02cSchristos 		}						       \
3638dbcf02cSchristos 	} while (0)
3648dbcf02cSchristos #else
3658dbcf02cSchristos #define WPA_ASSERT(a) do { } while (0)
3668dbcf02cSchristos #endif
3678dbcf02cSchristos 
36836ebd06eSchristos const char * debug_level_str(int level);
36936ebd06eSchristos int str_to_debug_level(const char *s);
37036ebd06eSchristos 
3718dbcf02cSchristos #endif /* WPA_DEBUG_H */
372