dump_state.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * hostapd / State dump
  3. * Copyright (c) 2002-2009, 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. #include "utils/includes.h"
  9. #include <time.h>
  10. #include "utils/common.h"
  11. #include "radius/radius_client.h"
  12. #include "radius/radius_server.h"
  13. #include "eapol_auth/eapol_auth_sm.h"
  14. #include "eapol_auth/eapol_auth_sm_i.h"
  15. #include "eap_server/eap.h"
  16. #include "ap/hostapd.h"
  17. #include "ap/ap_config.h"
  18. #include "ap/sta_info.h"
  19. #include "dump_state.h"
  20. #include "ap/ap_drv_ops.h"
  21. static void fprint_char(FILE *f, char c)
  22. {
  23. if (c >= 32 && c < 127)
  24. fprintf(f, "%c", c);
  25. else
  26. fprintf(f, "<%02x>", c);
  27. }
  28. static void ieee802_1x_dump_state(FILE *f, const char *prefix,
  29. struct sta_info *sta)
  30. {
  31. struct eapol_state_machine *sm = sta->eapol_sm;
  32. if (sm == NULL)
  33. return;
  34. fprintf(f, "%sIEEE 802.1X:\n", prefix);
  35. if (sm->identity) {
  36. size_t i;
  37. fprintf(f, "%sidentity=", prefix);
  38. for (i = 0; i < sm->identity_len; i++)
  39. fprint_char(f, sm->identity[i]);
  40. fprintf(f, "\n");
  41. }
  42. fprintf(f, "%slast EAP type: Authentication Server: %d (%s) "
  43. "Supplicant: %d (%s)\n", prefix,
  44. sm->eap_type_authsrv,
  45. eap_server_get_name(0, sm->eap_type_authsrv),
  46. sm->eap_type_supp, eap_server_get_name(0, sm->eap_type_supp));
  47. fprintf(f, "%scached_packets=%s\n", prefix,
  48. sm->last_recv_radius ? "[RX RADIUS]" : "");
  49. eapol_auth_dump_state(f, prefix, sm);
  50. }
  51. /**
  52. * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
  53. */
  54. static void hostapd_dump_state(struct hostapd_data *hapd)
  55. {
  56. FILE *f;
  57. time_t now;
  58. struct sta_info *sta;
  59. int i;
  60. #ifndef CONFIG_NO_RADIUS
  61. char *buf;
  62. #endif /* CONFIG_NO_RADIUS */
  63. struct hostap_sta_driver_data data;
  64. if (!hapd->conf->dump_log_name) {
  65. wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
  66. "request");
  67. return;
  68. }
  69. wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
  70. hapd->conf->dump_log_name);
  71. f = fopen(hapd->conf->dump_log_name, "w");
  72. if (f == NULL) {
  73. wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
  74. "writing.", hapd->conf->dump_log_name);
  75. return;
  76. }
  77. time(&now);
  78. fprintf(f, "hostapd state dump - %s", ctime(&now));
  79. fprintf(f, "num_sta=%d num_sta_non_erp=%d "
  80. "num_sta_no_short_slot_time=%d\n"
  81. "num_sta_no_short_preamble=%d\n",
  82. hapd->num_sta, hapd->iface->num_sta_non_erp,
  83. hapd->iface->num_sta_no_short_slot_time,
  84. hapd->iface->num_sta_no_short_preamble);
  85. for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
  86. char flags[200];
  87. fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
  88. ap_sta_flags_txt(sta->flags, flags, sizeof(flags));
  89. fprintf(f,
  90. " AID=%d flags=0x%x %s\n"
  91. " capability=0x%x listen_interval=%d\n",
  92. sta->aid,
  93. sta->flags,
  94. flags,
  95. sta->capability,
  96. sta->listen_interval);
  97. fprintf(f, " supported_rates=");
  98. for (i = 0; i < sta->supported_rates_len; i++)
  99. fprintf(f, "%02x ", sta->supported_rates[i]);
  100. fprintf(f, "\n");
  101. fprintf(f,
  102. " timeout_next=%s\n",
  103. (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
  104. (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
  105. "DEAUTH")));
  106. ieee802_1x_dump_state(f, " ", sta);
  107. if (hostapd_drv_read_sta_data(hapd, &data, sta->addr) == 0) {
  108. fprintf(f, " rx_pkt=%lu tx_pkt=%lu\n"
  109. " rx_byte=%lu tx_byte=%lu\n",
  110. data.rx_packets, data.tx_packets,
  111. data.rx_bytes, data.tx_bytes);
  112. }
  113. }
  114. #ifndef CONFIG_NO_RADIUS
  115. buf = os_malloc(4096);
  116. if (buf) {
  117. int count = radius_client_get_mib(hapd->radius, buf, 4096);
  118. if (count < 0)
  119. count = 0;
  120. else if (count > 4095)
  121. count = 4095;
  122. buf[count] = '\0';
  123. fprintf(f, "%s", buf);
  124. #ifdef RADIUS_SERVER
  125. count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
  126. if (count < 0)
  127. count = 0;
  128. else if (count > 4095)
  129. count = 4095;
  130. buf[count] = '\0';
  131. fprintf(f, "%s", buf);
  132. #endif /* RADIUS_SERVER */
  133. os_free(buf);
  134. }
  135. #endif /* CONFIG_NO_RADIUS */
  136. fclose(f);
  137. }
  138. int handle_dump_state_iface(struct hostapd_iface *iface, void *ctx)
  139. {
  140. size_t i;
  141. for (i = 0; i < iface->num_bss; i++)
  142. hostapd_dump_state(iface->bss[i]);
  143. return 0;
  144. }