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