1 /* 2 * wpa_supplicant/hostapd / Debug prints 3 * Copyright (c) 2002-2013, Jouni Malinen <j (at) 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 18 /* Debugging function - conditional printf and hex dump. Driver wrappers can 19 * use these for debugging purposes. */ 20 21 enum { 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 40 static inline int wpa_debug_reopen_file(void) 41 { 42 return 0; 43 } 44 45 #else /* CONFIG_NO_STDOUT_DEBUG */ 46 47 int wpa_debug_open_file(const char *path); 48 int wpa_debug_reopen_file(void); 49 void wpa_debug_close_file(void); 50 void 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 */ 59 void 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 */ 72 void wpa_printf(int level, const char *fmt, ...) 73 PRINTF_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 */ 86 void wpa_hexdump(int level, const char *title, const void *buf, size_t len); 87 88 static 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 */ 108 void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len); 109 110 static 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 */ 130 void 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 */ 147 void 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_global_only(args...) do { } while (0) 168 #define wpa_msg_register_cb(f) do { } while (0) 169 #define wpa_msg_register_ifname_cb(f) do { } while (0) 170 #else /* CONFIG_NO_WPA_MSG */ 171 /** 172 * wpa_msg - Conditional printf for default target and ctrl_iface monitors 173 * @ctx: Pointer to context data; this is the ctx variable registered 174 * with struct wpa_driver_ops::init() 175 * @level: priority level (MSG_*) of the message 176 * @fmt: printf format string, followed by optional arguments 177 * 178 * This function is used to print conditional debugging and error messages. The 179 * output may be directed to stdout, stderr, and/or syslog based on 180 * configuration. This function is like wpa_printf(), but it also sends the 181 * same message to all attached ctrl_iface monitors. 182 * 183 * Note: New line '\n' is added to the end of the text when printing to stdout. 184 */ 185 void wpa_msg(void *ctx, int level, const char *fmt, ...) PRINTF_FORMAT(3, 4); 186 187 /** 188 * wpa_msg_ctrl - Conditional printf for ctrl_iface monitors 189 * @ctx: Pointer to context data; this is the ctx variable registered 190 * with struct wpa_driver_ops::init() 191 * @level: priority level (MSG_*) of the message 192 * @fmt: printf format string, followed by optional arguments 193 * 194 * This function is used to print conditional debugging and error messages. 195 * This function is like wpa_msg(), but it sends the output only to the 196 * attached ctrl_iface monitors. In other words, it can be used for frequent 197 * events that do not need to be sent to syslog. 198 */ 199 void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...) 200 PRINTF_FORMAT(3, 4); 201 202 /** 203 * wpa_msg_global - Global printf for ctrl_iface monitors 204 * @ctx: Pointer to context data; this is the ctx variable registered 205 * with struct wpa_driver_ops::init() 206 * @level: priority level (MSG_*) of the message 207 * @fmt: printf format string, followed by optional arguments 208 * 209 * This function is used to print conditional debugging and error messages. 210 * This function is like wpa_msg(), but it sends the output as a global event, 211 * i.e., without being specific to an interface. For backwards compatibility, 212 * an old style event is also delivered on one of the interfaces (the one 213 * specified by the context data). 214 */ 215 void wpa_msg_global(void *ctx, int level, const char *fmt, ...) 216 PRINTF_FORMAT(3, 4); 217 218 /** 219 * wpa_msg_global_ctrl - Conditional global printf for ctrl_iface monitors 220 * @ctx: Pointer to context data; this is the ctx variable registered 221 * with struct wpa_driver_ops::init() 222 * @level: priority level (MSG_*) of the message 223 * @fmt: printf format string, followed by optional arguments 224 * 225 * This function is used to print conditional debugging and error messages. 226 * This function is like wpa_msg_global(), but it sends the output only to the 227 * attached global ctrl_iface monitors. In other words, it can be used for 228 * frequent events that do not need to be sent to syslog. 229 */ 230 void wpa_msg_global_ctrl(void *ctx, int level, const char *fmt, ...) 231 PRINTF_FORMAT(3, 4); 232 233 /** 234 * wpa_msg_no_global - Conditional printf for ctrl_iface monitors 235 * @ctx: Pointer to context data; this is the ctx variable registered 236 * with struct wpa_driver_ops::init() 237 * @level: priority level (MSG_*) of the message 238 * @fmt: printf format string, followed by optional arguments 239 * 240 * This function is used to print conditional debugging and error messages. 241 * This function is like wpa_msg(), but it does not send the output as a global 242 * event. 243 */ 244 void wpa_msg_no_global(void *ctx, int level, const char *fmt, ...) 245 PRINTF_FORMAT(3, 4); 246 247 /** 248 * wpa_msg_global_only - Conditional printf for ctrl_iface monitors 249 * @ctx: Pointer to context data; this is the ctx variable registered 250 * with struct wpa_driver_ops::init() 251 * @level: priority level (MSG_*) of the message 252 * @fmt: printf format string, followed by optional arguments 253 * 254 * This function is used to print conditional debugging and error messages. 255 * This function is like wpa_msg_global(), but it sends the output only as a 256 * global event. 257 */ 258 void wpa_msg_global_only(void *ctx, int level, const char *fmt, ...) 259 PRINTF_FORMAT(3, 4); 260 261 enum wpa_msg_type { 262 WPA_MSG_PER_INTERFACE, 263 WPA_MSG_GLOBAL, 264 WPA_MSG_NO_GLOBAL, 265 WPA_MSG_ONLY_GLOBAL, 266 }; 267 268 typedef void (*wpa_msg_cb_func)(void *ctx, int level, enum wpa_msg_type type, 269 const char *txt, size_t len); 270 271 /** 272 * wpa_msg_register_cb - Register callback function for wpa_msg() messages 273 * @func: Callback function (%NULL to unregister) 274 */ 275 void wpa_msg_register_cb(wpa_msg_cb_func func); 276 277 typedef const char * (*wpa_msg_get_ifname_func)(void *ctx); 278 void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func); 279 280 #endif /* CONFIG_NO_WPA_MSG */ 281 282 #ifdef CONFIG_NO_HOSTAPD_LOGGER 283 #define hostapd_logger(args...) do { } while (0) 284 #define hostapd_logger_register_cb(f) do { } while (0) 285 #else /* CONFIG_NO_HOSTAPD_LOGGER */ 286 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level, 287 const char *fmt, ...) PRINTF_FORMAT(5, 6); 288 289 typedef void (*hostapd_logger_cb_func)(void *ctx, const u8 *addr, 290 unsigned int module, int level, 291 const char *txt, size_t len); 292 293 /** 294 * hostapd_logger_register_cb - Register callback function for hostapd_logger() 295 * @func: Callback function (%NULL to unregister) 296 */ 297 void hostapd_logger_register_cb(hostapd_logger_cb_func func); 298 #endif /* CONFIG_NO_HOSTAPD_LOGGER */ 299 300 #define HOSTAPD_MODULE_IEEE80211 0x00000001 301 #define HOSTAPD_MODULE_IEEE8021X 0x00000002 302 #define HOSTAPD_MODULE_RADIUS 0x00000004 303 #define HOSTAPD_MODULE_WPA 0x00000008 304 #define HOSTAPD_MODULE_DRIVER 0x00000010 305 #define HOSTAPD_MODULE_IAPP 0x00000020 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