Coverage Report

Created: 2025-11-03 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hostap/src/utils/wpa_debug.c
Line
Count
Source
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
#include "includes.h"
10
11
#include "common.h"
12
13
#ifdef CONFIG_DEBUG_SYSLOG
14
#include <syslog.h>
15
#endif /* CONFIG_DEBUG_SYSLOG */
16
17
#ifdef CONFIG_DEBUG_LINUX_TRACING
18
#include <sys/types.h>
19
#include <sys/stat.h>
20
#include <fcntl.h>
21
#include <string.h>
22
#include <stdio.h>
23
24
static FILE *wpa_debug_tracing_file = NULL;
25
26
#define WPAS_TRACE_PFX "wpas <%d>: "
27
#endif /* CONFIG_DEBUG_LINUX_TRACING */
28
29
30
int wpa_debug_level = MSG_INFO;
31
int wpa_debug_show_keys = 0;
32
int wpa_debug_timestamp = 0;
33
int wpa_debug_syslog = 0;
34
#ifndef CONFIG_NO_STDOUT_DEBUG
35
static FILE *out_file = NULL;
36
#endif /* CONFIG_NO_STDOUT_DEBUG */
37
38
39
#ifdef CONFIG_ANDROID_LOG
40
41
#include <android/log.h>
42
43
#ifndef ANDROID_LOG_NAME
44
#define ANDROID_LOG_NAME  "wpa_supplicant"
45
#endif /* ANDROID_LOG_NAME */
46
47
static int wpa_to_android_level(int level)
48
{
49
  if (level == MSG_ERROR)
50
    return ANDROID_LOG_ERROR;
51
  if (level == MSG_WARNING)
52
    return ANDROID_LOG_WARN;
53
  if (level == MSG_INFO)
54
    return ANDROID_LOG_INFO;
55
  return ANDROID_LOG_DEBUG;
56
}
57
58
#endif /* CONFIG_ANDROID_LOG */
59
60
#ifndef CONFIG_NO_STDOUT_DEBUG
61
62
#ifdef CONFIG_DEBUG_FILE
63
#include <sys/types.h>
64
#include <sys/stat.h>
65
#include <fcntl.h>
66
#endif /* CONFIG_DEBUG_FILE */
67
68
69
void wpa_debug_print_timestamp(void)
70
0
{
71
0
#ifndef CONFIG_ANDROID_LOG
72
0
  struct os_time tv;
73
74
0
  if (!wpa_debug_timestamp)
75
0
    return;
76
77
0
  os_get_time(&tv);
78
0
#ifdef CONFIG_DEBUG_FILE
79
0
  if (out_file)
80
0
    fprintf(out_file, "%ld.%06u: ", (long) tv.sec,
81
0
      (unsigned int) tv.usec);
82
0
#endif /* CONFIG_DEBUG_FILE */
83
0
  if (!out_file && !wpa_debug_syslog)
84
0
    printf("%ld.%06u: ", (long) tv.sec, (unsigned int) tv.usec);
85
0
#endif /* CONFIG_ANDROID_LOG */
86
0
}
87
88
89
#ifdef CONFIG_DEBUG_SYSLOG
90
#ifndef LOG_HOSTAPD
91
#define LOG_HOSTAPD LOG_DAEMON
92
#endif /* LOG_HOSTAPD */
93
94
void wpa_debug_open_syslog(void)
95
{
96
  openlog("wpa_supplicant", LOG_PID | LOG_NDELAY, LOG_HOSTAPD);
97
  wpa_debug_syslog++;
98
}
99
100
101
void wpa_debug_close_syslog(void)
102
{
103
  if (wpa_debug_syslog)
104
    closelog();
105
}
106
107
108
static int syslog_priority(int level)
109
{
110
  switch (level) {
111
  case MSG_MSGDUMP:
112
  case MSG_DEBUG:
113
    return LOG_DEBUG;
114
  case MSG_INFO:
115
    return LOG_NOTICE;
116
  case MSG_WARNING:
117
    return LOG_WARNING;
118
  case MSG_ERROR:
119
    return LOG_ERR;
120
  }
121
  return LOG_INFO;
122
}
123
#endif /* CONFIG_DEBUG_SYSLOG */
124
125
126
#ifdef CONFIG_DEBUG_LINUX_TRACING
127
128
int wpa_debug_open_linux_tracing(void)
129
{
130
  int mounts, trace_fd;
131
  char buf[4096] = {};
132
  ssize_t buflen;
133
  char *line, *tmp1;
134
135
  mounts = open("/proc/mounts", O_RDONLY);
136
  if (mounts < 0) {
137
    printf("no /proc/mounts\n");
138
    return -1;
139
  }
140
141
  buflen = read(mounts, buf, sizeof(buf) - 1);
142
  close(mounts);
143
  if (buflen < 0) {
144
    printf("failed to read /proc/mounts\n");
145
    return -1;
146
  }
147
  buf[buflen] = '\0';
148
149
  line = strtok_r(buf, "\n", &tmp1);
150
  while (line) {
151
    char *tmp2, *tmp_path, *fstype;
152
    /* "<dev> <mountpoint> <fs type> ..." */
153
    strtok_r(line, " ", &tmp2);
154
    tmp_path = strtok_r(NULL, " ", &tmp2);
155
    fstype = strtok_r(NULL, " ", &tmp2);
156
157
    if (!buf[0] && fstype && os_strcmp(fstype, "debugfs") == 0) {
158
      os_snprintf(buf, sizeof(buf) - 1,
159
            "%s/tracing/trace_marker",
160
            tmp_path);
161
      /*
162
       * Don't break to prefer tracefs, which if present may
163
       * mean debugfs doesn't have tracing/ (depending on the
164
       * kernel version).
165
       */
166
    }
167
168
    if (fstype && os_strcmp(fstype, "tracefs") == 0) {
169
      os_snprintf(buf, sizeof(buf) - 1, "%s/trace_marker",
170
            tmp_path);
171
      break;
172
    }
173
174
    line = strtok_r(NULL, "\n", &tmp1);
175
  }
176
177
  if (!buf[0]) {
178
    printf("tracefs/debugfs not found\n");
179
    return -1;
180
  }
181
182
  trace_fd = open(buf, O_WRONLY);
183
  if (trace_fd < 0) {
184
    printf("failed to open trace_marker file\n");
185
    return -1;
186
  }
187
  wpa_debug_tracing_file = fdopen(trace_fd, "w");
188
  if (wpa_debug_tracing_file == NULL) {
189
    close(trace_fd);
190
    printf("failed to fdopen()\n");
191
    return -1;
192
  }
193
194
  return 0;
195
}
196
197
198
void wpa_debug_close_linux_tracing(void)
199
{
200
  if (wpa_debug_tracing_file == NULL)
201
    return;
202
  fclose(wpa_debug_tracing_file);
203
  wpa_debug_tracing_file = NULL;
204
}
205
206
#endif /* CONFIG_DEBUG_LINUX_TRACING */
207
208
209
/**
210
 * wpa_printf - conditional printf
211
 * @level: priority level (MSG_*) of the message
212
 * @fmt: printf format string, followed by optional arguments
213
 *
214
 * This function is used to print conditional debugging and error messages. The
215
 * output may be directed to stdout, stderr, and/or syslog based on
216
 * configuration.
217
 *
218
 * Note: New line '\n' is added to the end of the text when printing to stdout.
219
 */
220
void wpa_printf(int level, const char *fmt, ...)
221
3.23k
{
222
3.23k
  va_list ap;
223
224
3.23k
  if (level >= wpa_debug_level) {
225
#ifdef CONFIG_ANDROID_LOG
226
    va_start(ap, fmt);
227
    __android_log_vprint(wpa_to_android_level(level),
228
             ANDROID_LOG_NAME, fmt, ap);
229
    va_end(ap);
230
#else /* CONFIG_ANDROID_LOG */
231
#ifdef CONFIG_DEBUG_SYSLOG
232
    if (wpa_debug_syslog) {
233
      va_start(ap, fmt);
234
      vsyslog(syslog_priority(level), fmt, ap);
235
      va_end(ap);
236
    }
237
#endif /* CONFIG_DEBUG_SYSLOG */
238
0
    wpa_debug_print_timestamp();
239
0
#ifdef CONFIG_DEBUG_FILE
240
0
    if (out_file) {
241
0
      va_start(ap, fmt);
242
0
      vfprintf(out_file, fmt, ap);
243
0
      fprintf(out_file, "\n");
244
0
      va_end(ap);
245
0
    }
246
0
#endif /* CONFIG_DEBUG_FILE */
247
0
    if (!wpa_debug_syslog && !out_file) {
248
0
      va_start(ap, fmt);
249
0
      vprintf(fmt, ap);
250
0
      printf("\n");
251
0
      va_end(ap);
252
0
    }
253
0
#endif /* CONFIG_ANDROID_LOG */
254
0
  }
255
256
#ifdef CONFIG_DEBUG_LINUX_TRACING
257
  if (wpa_debug_tracing_file != NULL) {
258
    va_start(ap, fmt);
259
    fprintf(wpa_debug_tracing_file, WPAS_TRACE_PFX, level);
260
    vfprintf(wpa_debug_tracing_file, fmt, ap);
261
    fprintf(wpa_debug_tracing_file, "\n");
262
    fflush(wpa_debug_tracing_file);
263
    va_end(ap);
264
  }
265
#endif /* CONFIG_DEBUG_LINUX_TRACING */
266
3.23k
}
267
268
269
static void _wpa_hexdump(int level, const char *title, const u8 *buf,
270
       size_t len, int show, int only_syslog)
271
3.52k
{
272
3.52k
  size_t i;
273
274
#ifdef CONFIG_DEBUG_LINUX_TRACING
275
  if (wpa_debug_tracing_file != NULL) {
276
    fprintf(wpa_debug_tracing_file,
277
      WPAS_TRACE_PFX "%s - hexdump(len=%lu):",
278
      level, title, (unsigned long) len);
279
    if (buf == NULL) {
280
      fprintf(wpa_debug_tracing_file, " [NULL]\n");
281
    } else if (!show) {
282
      fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
283
    } else {
284
      for (i = 0; i < len; i++)
285
        fprintf(wpa_debug_tracing_file,
286
          " %02x", buf[i]);
287
    }
288
    fflush(wpa_debug_tracing_file);
289
  }
290
#endif /* CONFIG_DEBUG_LINUX_TRACING */
291
292
3.52k
  if (level < wpa_debug_level)
293
3.52k
    return;
294
#ifdef CONFIG_ANDROID_LOG
295
  {
296
    const char *display;
297
    char *strbuf = NULL;
298
    size_t slen = len;
299
    if (buf == NULL) {
300
      display = " [NULL]";
301
    } else if (len == 0) {
302
      display = "";
303
    } else if (show && len) {
304
      /* Limit debug message length for Android log */
305
      if (slen > 32)
306
        slen = 32;
307
      strbuf = os_malloc(1 + 3 * slen);
308
      if (strbuf == NULL) {
309
        wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
310
             "allocate message buffer");
311
        return;
312
      }
313
314
      for (i = 0; i < slen; i++)
315
        os_snprintf(&strbuf[i * 3], 4, " %02x",
316
              buf[i]);
317
318
      display = strbuf;
319
    } else {
320
      display = " [REMOVED]";
321
    }
322
323
    __android_log_print(wpa_to_android_level(level),
324
            ANDROID_LOG_NAME,
325
            "%s - hexdump(len=%lu):%s%s",
326
            title, (long unsigned int) len, display,
327
            len > slen ? " ..." : "");
328
    bin_clear_free(strbuf, 1 + 3 * slen);
329
    return;
330
  }
331
#else /* CONFIG_ANDROID_LOG */
332
#ifdef CONFIG_DEBUG_SYSLOG
333
  if (wpa_debug_syslog) {
334
    const char *display;
335
    char *strbuf = NULL;
336
337
    if (buf == NULL) {
338
      display = " [NULL]";
339
    } else if (len == 0) {
340
      display = "";
341
    } else if (show && len) {
342
      strbuf = os_malloc(1 + 3 * len);
343
      if (strbuf == NULL) {
344
        wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
345
             "allocate message buffer");
346
        return;
347
      }
348
349
      for (i = 0; i < len; i++)
350
        os_snprintf(&strbuf[i * 3], 4, " %02x",
351
              buf[i]);
352
353
      display = strbuf;
354
    } else {
355
      display = " [REMOVED]";
356
    }
357
358
    syslog(syslog_priority(level), "%s - hexdump(len=%lu):%s",
359
           title, (unsigned long) len, display);
360
    bin_clear_free(strbuf, 1 + 3 * len);
361
    if (only_syslog)
362
      return;
363
  }
364
#endif /* CONFIG_DEBUG_SYSLOG */
365
0
  wpa_debug_print_timestamp();
366
0
#ifdef CONFIG_DEBUG_FILE
367
0
  if (out_file) {
368
0
    fprintf(out_file, "%s - hexdump(len=%lu):",
369
0
      title, (unsigned long) len);
370
0
    if (buf == NULL) {
371
0
      fprintf(out_file, " [NULL]");
372
0
    } else if (show) {
373
0
      for (i = 0; i < len; i++)
374
0
        fprintf(out_file, " %02x", buf[i]);
375
0
    } else {
376
0
      fprintf(out_file, " [REMOVED]");
377
0
    }
378
0
    fprintf(out_file, "\n");
379
0
  }
380
0
#endif /* CONFIG_DEBUG_FILE */
381
0
  if (!wpa_debug_syslog && !out_file) {
382
0
    printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
383
0
    if (buf == NULL) {
384
0
      printf(" [NULL]");
385
0
    } else if (show) {
386
0
      for (i = 0; i < len; i++)
387
0
        printf(" %02x", buf[i]);
388
0
    } else {
389
0
      printf(" [REMOVED]");
390
0
    }
391
0
    printf("\n");
392
0
  }
393
0
#endif /* CONFIG_ANDROID_LOG */
394
0
}
395
396
void wpa_hexdump(int level, const char *title, const void *buf, size_t len)
397
3.52k
{
398
3.52k
  _wpa_hexdump(level, title, buf, len, 1, 0);
399
3.52k
}
400
401
402
void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len)
403
0
{
404
0
  _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys, 0);
405
0
}
406
407
408
static void _wpa_hexdump_ascii(int level, const char *title, const void *buf,
409
             size_t len, int show)
410
70
{
411
70
  size_t i, llen;
412
70
  const u8 *pos = buf;
413
70
  const size_t line_len = 16;
414
415
#ifdef CONFIG_DEBUG_LINUX_TRACING
416
  if (wpa_debug_tracing_file != NULL) {
417
    fprintf(wpa_debug_tracing_file,
418
      WPAS_TRACE_PFX "%s - hexdump_ascii(len=%lu):",
419
      level, title, (unsigned long) len);
420
    if (buf == NULL) {
421
      fprintf(wpa_debug_tracing_file, " [NULL]\n");
422
    } else if (!show) {
423
      fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
424
    } else {
425
      /* can do ascii processing in userspace */
426
      for (i = 0; i < len; i++)
427
        fprintf(wpa_debug_tracing_file,
428
          " %02x", pos[i]);
429
    }
430
    fflush(wpa_debug_tracing_file);
431
  }
432
#endif /* CONFIG_DEBUG_LINUX_TRACING */
433
434
70
  if (level < wpa_debug_level)
435
70
    return;
436
#ifdef CONFIG_ANDROID_LOG
437
  _wpa_hexdump(level, title, buf, len, show, 0);
438
#else /* CONFIG_ANDROID_LOG */
439
#ifdef CONFIG_DEBUG_SYSLOG
440
  if (wpa_debug_syslog)
441
    _wpa_hexdump(level, title, buf, len, show, 1);
442
#endif /* CONFIG_DEBUG_SYSLOG */
443
0
  wpa_debug_print_timestamp();
444
0
#ifdef CONFIG_DEBUG_FILE
445
0
  if (out_file) {
446
0
    if (!show) {
447
0
      fprintf(out_file,
448
0
        "%s - hexdump_ascii(len=%lu): [REMOVED]\n",
449
0
        title, (unsigned long) len);
450
0
      goto file_done;
451
0
    }
452
0
    if (buf == NULL) {
453
0
      fprintf(out_file,
454
0
        "%s - hexdump_ascii(len=%lu): [NULL]\n",
455
0
        title, (unsigned long) len);
456
0
      goto file_done;
457
0
    }
458
0
    fprintf(out_file, "%s - hexdump_ascii(len=%lu):\n",
459
0
      title, (unsigned long) len);
460
0
    while (len) {
461
0
      llen = len > line_len ? line_len : len;
462
0
      fprintf(out_file, "    ");
463
0
      for (i = 0; i < llen; i++)
464
0
        fprintf(out_file, " %02x", pos[i]);
465
0
      for (i = llen; i < line_len; i++)
466
0
        fprintf(out_file, "   ");
467
0
      fprintf(out_file, "   ");
468
0
      for (i = 0; i < llen; i++) {
469
0
        if (isprint(pos[i]))
470
0
          fprintf(out_file, "%c", pos[i]);
471
0
        else
472
0
          fprintf(out_file, "_");
473
0
      }
474
0
      for (i = llen; i < line_len; i++)
475
0
        fprintf(out_file, " ");
476
0
      fprintf(out_file, "\n");
477
0
      pos += llen;
478
0
      len -= llen;
479
0
    }
480
0
  }
481
0
file_done:
482
0
#endif /* CONFIG_DEBUG_FILE */
483
0
  if (!wpa_debug_syslog && !out_file) {
484
0
    if (!show) {
485
0
      printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
486
0
             title, (unsigned long) len);
487
0
      return;
488
0
    }
489
0
    if (buf == NULL) {
490
0
      printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
491
0
             title, (unsigned long) len);
492
0
      return;
493
0
    }
494
0
    printf("%s - hexdump_ascii(len=%lu):\n", title,
495
0
           (unsigned long) len);
496
0
    while (len) {
497
0
      llen = len > line_len ? line_len : len;
498
0
      printf("    ");
499
0
      for (i = 0; i < llen; i++)
500
0
        printf(" %02x", pos[i]);
501
0
      for (i = llen; i < line_len; i++)
502
0
        printf("   ");
503
0
      printf("   ");
504
0
      for (i = 0; i < llen; i++) {
505
0
        if (isprint(pos[i]))
506
0
          printf("%c", pos[i]);
507
0
        else
508
0
          printf("_");
509
0
      }
510
0
      for (i = llen; i < line_len; i++)
511
0
        printf(" ");
512
0
      printf("\n");
513
0
      pos += llen;
514
0
      len -= llen;
515
0
    }
516
0
  }
517
0
#endif /* CONFIG_ANDROID_LOG */
518
0
}
519
520
521
void wpa_hexdump_ascii(int level, const char *title, const void *buf,
522
           size_t len)
523
70
{
524
70
  _wpa_hexdump_ascii(level, title, buf, len, 1);
525
70
}
526
527
528
void wpa_hexdump_ascii_key(int level, const char *title, const void *buf,
529
         size_t len)
530
0
{
531
0
  _wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
532
0
}
533
534
535
#ifdef CONFIG_DEBUG_FILE
536
static char *last_path = NULL;
537
#endif /* CONFIG_DEBUG_FILE */
538
539
int wpa_debug_reopen_file(void)
540
0
{
541
0
#ifdef CONFIG_DEBUG_FILE
542
0
  int rv;
543
0
  char *tmp;
544
545
0
  if (!last_path)
546
0
    return 0; /* logfile not used */
547
548
0
  tmp = os_strdup(last_path);
549
0
  if (!tmp)
550
0
    return -1;
551
552
0
  wpa_debug_close_file();
553
0
  rv = wpa_debug_open_file(tmp);
554
0
  os_free(tmp);
555
0
  return rv;
556
#else /* CONFIG_DEBUG_FILE */
557
  return 0;
558
#endif /* CONFIG_DEBUG_FILE */
559
0
}
560
561
562
int wpa_debug_open_file(const char *path)
563
0
{
564
0
#ifdef CONFIG_DEBUG_FILE
565
0
  int out_fd;
566
567
0
  if (!path)
568
0
    return 0;
569
570
0
  if (last_path == NULL || os_strcmp(last_path, path) != 0) {
571
    /* Save our path to enable re-open */
572
0
    os_free(last_path);
573
0
    last_path = os_strdup(path);
574
0
  }
575
576
0
  out_fd = open(path, O_CREAT | O_APPEND | O_WRONLY,
577
0
          S_IRUSR | S_IWUSR | S_IRGRP);
578
0
  if (out_fd < 0) {
579
0
    wpa_printf(MSG_ERROR,
580
0
         "%s: Failed to open output file descriptor, using standard output",
581
0
         __func__);
582
0
    return -1;
583
0
  }
584
585
0
#ifdef __linux__
586
0
  if (fcntl(out_fd, F_SETFD, FD_CLOEXEC) < 0) {
587
0
    wpa_printf(MSG_DEBUG,
588
0
         "%s: Failed to set FD_CLOEXEC - continue without: %s",
589
0
         __func__, strerror(errno));
590
0
  }
591
0
#endif /* __linux__ */
592
593
0
  out_file = fdopen(out_fd, "a");
594
0
  if (out_file == NULL) {
595
0
    wpa_printf(MSG_ERROR, "wpa_debug_open_file: Failed to open "
596
0
         "output file, using standard output");
597
0
    close(out_fd);
598
0
    return -1;
599
0
  }
600
0
#ifndef _WIN32
601
0
  setvbuf(out_file, NULL, _IOLBF, 0);
602
0
#endif /* _WIN32 */
603
#else /* CONFIG_DEBUG_FILE */
604
  (void)path;
605
#endif /* CONFIG_DEBUG_FILE */
606
0
  return 0;
607
0
}
608
609
610
void wpa_debug_stop_log(void)
611
0
{
612
0
#ifdef CONFIG_DEBUG_FILE
613
0
  if (!out_file)
614
0
    return;
615
0
  fclose(out_file);
616
0
  out_file = NULL;
617
0
#endif /* CONFIG_DEBUG_FILE */
618
0
}
619
620
621
void wpa_debug_close_file(void)
622
0
{
623
0
#ifdef CONFIG_DEBUG_FILE
624
0
  wpa_debug_stop_log();
625
0
  os_free(last_path);
626
0
  last_path = NULL;
627
0
#endif /* CONFIG_DEBUG_FILE */
628
0
}
629
630
631
void wpa_debug_setup_stdout(void)
632
0
{
633
0
#ifndef _WIN32
634
0
  setvbuf(stdout, NULL, _IOLBF, 0);
635
0
#endif /* _WIN32 */
636
0
}
637
638
#endif /* CONFIG_NO_STDOUT_DEBUG */
639
640
641
#ifndef CONFIG_NO_WPA_MSG
642
static wpa_msg_cb_func wpa_msg_cb = NULL;
643
644
void wpa_msg_register_cb(wpa_msg_cb_func func)
645
0
{
646
0
  wpa_msg_cb = func;
647
0
}
648
649
650
static wpa_msg_get_ifname_func wpa_msg_ifname_cb = NULL;
651
652
void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)
653
0
{
654
0
  wpa_msg_ifname_cb = func;
655
0
}
656
657
658
void wpa_msg(void *ctx, int level, const char *fmt, ...)
659
0
{
660
0
  va_list ap;
661
0
  char *buf;
662
0
  int buflen;
663
0
  int len;
664
0
  char prefix[130];
665
666
0
  va_start(ap, fmt);
667
0
  buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
668
0
  va_end(ap);
669
670
0
  buf = os_malloc(buflen);
671
0
  if (buf == NULL) {
672
0
    wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
673
0
         "buffer");
674
0
    return;
675
0
  }
676
0
  va_start(ap, fmt);
677
0
  prefix[0] = '\0';
678
0
  if (wpa_msg_ifname_cb) {
679
0
    const char *ifname = wpa_msg_ifname_cb(ctx);
680
0
    if (ifname) {
681
0
      int res = os_snprintf(prefix, sizeof(prefix), "%s: ",
682
0
                ifname);
683
0
      if (os_snprintf_error(sizeof(prefix), res))
684
0
        prefix[0] = '\0';
685
0
    }
686
0
  }
687
0
  len = vsnprintf(buf, buflen, fmt, ap);
688
0
  va_end(ap);
689
0
  wpa_printf(level, "%s%s", prefix, buf);
690
0
  if (wpa_msg_cb)
691
0
    wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
692
0
  bin_clear_free(buf, buflen);
693
0
}
694
695
696
void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
697
0
{
698
0
  va_list ap;
699
0
  char *buf;
700
0
  int buflen;
701
0
  int len;
702
703
0
  if (!wpa_msg_cb)
704
0
    return;
705
706
0
  va_start(ap, fmt);
707
0
  buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
708
0
  va_end(ap);
709
710
0
  buf = os_malloc(buflen);
711
0
  if (buf == NULL) {
712
0
    wpa_printf(MSG_ERROR, "wpa_msg_ctrl: Failed to allocate "
713
0
         "message buffer");
714
0
    return;
715
0
  }
716
0
  va_start(ap, fmt);
717
0
  len = vsnprintf(buf, buflen, fmt, ap);
718
0
  va_end(ap);
719
0
  wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
720
0
  bin_clear_free(buf, buflen);
721
0
}
722
723
724
void wpa_msg_global(void *ctx, int level, const char *fmt, ...)
725
0
{
726
0
  va_list ap;
727
0
  char *buf;
728
0
  int buflen;
729
0
  int len;
730
731
0
  va_start(ap, fmt);
732
0
  buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
733
0
  va_end(ap);
734
735
0
  buf = os_malloc(buflen);
736
0
  if (buf == NULL) {
737
0
    wpa_printf(MSG_ERROR, "wpa_msg_global: Failed to allocate "
738
0
         "message buffer");
739
0
    return;
740
0
  }
741
0
  va_start(ap, fmt);
742
0
  len = vsnprintf(buf, buflen, fmt, ap);
743
0
  va_end(ap);
744
0
  wpa_printf(level, "%s", buf);
745
0
  if (wpa_msg_cb)
746
0
    wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
747
0
  bin_clear_free(buf, buflen);
748
0
}
749
750
751
void wpa_msg_global_ctrl(void *ctx, int level, const char *fmt, ...)
752
0
{
753
0
  va_list ap;
754
0
  char *buf;
755
0
  int buflen;
756
0
  int len;
757
758
0
  if (!wpa_msg_cb)
759
0
    return;
760
761
0
  va_start(ap, fmt);
762
0
  buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
763
0
  va_end(ap);
764
765
0
  buf = os_malloc(buflen);
766
0
  if (buf == NULL) {
767
0
    wpa_printf(MSG_ERROR,
768
0
         "wpa_msg_global_ctrl: Failed to allocate message buffer");
769
0
    return;
770
0
  }
771
0
  va_start(ap, fmt);
772
0
  len = vsnprintf(buf, buflen, fmt, ap);
773
0
  va_end(ap);
774
0
  wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
775
0
  bin_clear_free(buf, buflen);
776
0
}
777
778
779
void wpa_msg_no_global(void *ctx, int level, const char *fmt, ...)
780
0
{
781
0
  va_list ap;
782
0
  char *buf;
783
0
  int buflen;
784
0
  int len;
785
786
0
  va_start(ap, fmt);
787
0
  buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
788
0
  va_end(ap);
789
790
0
  buf = os_malloc(buflen);
791
0
  if (buf == NULL) {
792
0
    wpa_printf(MSG_ERROR, "wpa_msg_no_global: Failed to allocate "
793
0
         "message buffer");
794
0
    return;
795
0
  }
796
0
  va_start(ap, fmt);
797
0
  len = vsnprintf(buf, buflen, fmt, ap);
798
0
  va_end(ap);
799
0
  wpa_printf(level, "%s", buf);
800
0
  if (wpa_msg_cb)
801
0
    wpa_msg_cb(ctx, level, WPA_MSG_NO_GLOBAL, buf, len);
802
0
  bin_clear_free(buf, buflen);
803
0
}
804
805
806
void wpa_msg_global_only(void *ctx, int level, const char *fmt, ...)
807
0
{
808
0
  va_list ap;
809
0
  char *buf;
810
0
  int buflen;
811
0
  int len;
812
813
0
  va_start(ap, fmt);
814
0
  buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
815
0
  va_end(ap);
816
817
0
  buf = os_malloc(buflen);
818
0
  if (buf == NULL) {
819
0
    wpa_printf(MSG_ERROR, "%s: Failed to allocate message buffer",
820
0
         __func__);
821
0
    return;
822
0
  }
823
0
  va_start(ap, fmt);
824
0
  len = vsnprintf(buf, buflen, fmt, ap);
825
0
  va_end(ap);
826
0
  wpa_printf(level, "%s", buf);
827
0
  if (wpa_msg_cb)
828
0
    wpa_msg_cb(ctx, level, WPA_MSG_ONLY_GLOBAL, buf, len);
829
0
  os_free(buf);
830
0
}
831
832
#endif /* CONFIG_NO_WPA_MSG */
833
834
835
#ifndef CONFIG_NO_HOSTAPD_LOGGER
836
static hostapd_logger_cb_func hostapd_logger_cb = NULL;
837
838
void hostapd_logger_register_cb(hostapd_logger_cb_func func)
839
0
{
840
0
  hostapd_logger_cb = func;
841
0
}
842
843
844
void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
845
        const char *fmt, ...)
846
0
{
847
0
  va_list ap;
848
0
  char *buf;
849
0
  int buflen;
850
0
  int len;
851
852
0
  va_start(ap, fmt);
853
0
  buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
854
0
  va_end(ap);
855
856
0
  buf = os_malloc(buflen);
857
0
  if (buf == NULL) {
858
0
    wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
859
0
         "message buffer");
860
0
    return;
861
0
  }
862
0
  va_start(ap, fmt);
863
0
  len = vsnprintf(buf, buflen, fmt, ap);
864
0
  va_end(ap);
865
0
  if (hostapd_logger_cb)
866
0
    hostapd_logger_cb(ctx, addr, module, level, buf, len);
867
0
  else if (addr)
868
0
    wpa_printf(MSG_DEBUG, "hostapd_logger: STA " MACSTR " - %s",
869
0
         MAC2STR(addr), buf);
870
0
  else
871
0
    wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
872
0
  bin_clear_free(buf, buflen);
873
0
}
874
#endif /* CONFIG_NO_HOSTAPD_LOGGER */
875
876
877
const char * debug_level_str(int level)
878
0
{
879
0
  switch (level) {
880
0
  case MSG_EXCESSIVE:
881
0
    return "EXCESSIVE";
882
0
  case MSG_MSGDUMP:
883
0
    return "MSGDUMP";
884
0
  case MSG_DEBUG:
885
0
    return "DEBUG";
886
0
  case MSG_INFO:
887
0
    return "INFO";
888
0
  case MSG_WARNING:
889
0
    return "WARNING";
890
0
  case MSG_ERROR:
891
0
    return "ERROR";
892
0
  default:
893
0
    return "?";
894
0
  }
895
0
}
896
897
898
int str_to_debug_level(const char *s)
899
0
{
900
0
  if (os_strcasecmp(s, "EXCESSIVE") == 0)
901
0
    return MSG_EXCESSIVE;
902
0
  if (os_strcasecmp(s, "MSGDUMP") == 0)
903
0
    return MSG_MSGDUMP;
904
0
  if (os_strcasecmp(s, "DEBUG") == 0)
905
0
    return MSG_DEBUG;
906
0
  if (os_strcasecmp(s, "INFO") == 0)
907
0
    return MSG_INFO;
908
0
  if (os_strcasecmp(s, "WARNING") == 0)
909
0
    return MSG_WARNING;
910
0
  if (os_strcasecmp(s, "ERROR") == 0)
911
0
    return MSG_ERROR;
912
0
  return -1;
913
0
}