Make HOSTAPD_DUMP_STATE configurable with CONFIG_NO_DUMP_STATE
[libeap.git] / hostapd / dump_state.c
1 /*
2  * hostapd / State dump
3  * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15
16 #include "includes.h"
17
18 #include "common.h"
19 #include "hostapd.h"
20 #include "config.h"
21 #include "sta_flags.h"
22 #include "sta_info.h"
23 #include "radius/radius_client.h"
24 #include "radius/radius_server.h"
25 #include "eapol_auth/eapol_auth_sm.h"
26 #include "eap_server/eap.h"
27
28
29 static void fprint_char(FILE *f, char c)
30 {
31         if (c >= 32 && c < 127)
32                 fprintf(f, "%c", c);
33         else
34                 fprintf(f, "<%02x>", c);
35 }
36
37
38 static void ieee802_1x_dump_state(FILE *f, const char *prefix,
39                                   struct sta_info *sta)
40 {
41         struct eapol_state_machine *sm = sta->eapol_sm;
42         if (sm == NULL)
43                 return;
44
45         fprintf(f, "%sIEEE 802.1X:\n", prefix);
46
47         if (sm->identity) {
48                 size_t i;
49                 fprintf(f, "%sidentity=", prefix);
50                 for (i = 0; i < sm->identity_len; i++)
51                         fprint_char(f, sm->identity[i]);
52                 fprintf(f, "\n");
53         }
54
55         fprintf(f, "%slast EAP type: Authentication Server: %d (%s) "
56                 "Supplicant: %d (%s)\n", prefix,
57                 sm->eap_type_authsrv,
58                 eap_server_get_name(0, sm->eap_type_authsrv),
59                 sm->eap_type_supp, eap_server_get_name(0, sm->eap_type_supp));
60
61         fprintf(f, "%scached_packets=%s\n", prefix,
62                 sm->last_recv_radius ? "[RX RADIUS]" : "");
63
64         eapol_auth_dump_state(f, prefix, sm);
65 }
66
67
68 /**
69  * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
70  */
71 static void hostapd_dump_state(struct hostapd_data *hapd)
72 {
73         FILE *f;
74         time_t now;
75         struct sta_info *sta;
76         int i;
77         char *buf;
78
79         if (!hapd->conf->dump_log_name) {
80                 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
81                            "request");
82                 return;
83         }
84
85         wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
86                    hapd->conf->dump_log_name);
87         f = fopen(hapd->conf->dump_log_name, "w");
88         if (f == NULL) {
89                 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
90                            "writing.", hapd->conf->dump_log_name);
91                 return;
92         }
93
94         time(&now);
95         fprintf(f, "hostapd state dump - %s", ctime(&now));
96         fprintf(f, "num_sta=%d num_sta_non_erp=%d "
97                 "num_sta_no_short_slot_time=%d\n"
98                 "num_sta_no_short_preamble=%d\n",
99                 hapd->num_sta, hapd->iface->num_sta_non_erp,
100                 hapd->iface->num_sta_no_short_slot_time,
101                 hapd->iface->num_sta_no_short_preamble);
102
103         for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
104                 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
105
106                 fprintf(f,
107                         "  AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
108                         "  capability=0x%x listen_interval=%d\n",
109                         sta->aid,
110                         sta->flags,
111                         (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
112                         (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
113                         (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
114                         (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
115                         (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
116                         (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
117                          ""),
118                         (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
119                          ""),
120                         (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
121                          "[SHORT_PREAMBLE]" : ""),
122                         (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
123                         (sta->flags & WLAN_STA_WMM ? "[WMM]" : ""),
124                         (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
125                         (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
126                         (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
127                         (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
128                         sta->capability,
129                         sta->listen_interval);
130
131                 fprintf(f, "  supported_rates=");
132                 for (i = 0; i < sta->supported_rates_len; i++)
133                         fprintf(f, "%02x ", sta->supported_rates[i]);
134                 fprintf(f, "\n");
135
136                 fprintf(f,
137                         "  timeout_next=%s\n",
138                         (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
139                          (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
140                           "DEAUTH")));
141
142                 ieee802_1x_dump_state(f, "  ", sta);
143         }
144
145         buf = os_malloc(4096);
146         if (buf) {
147                 int count = radius_client_get_mib(hapd->radius, buf, 4096);
148                 if (count < 0)
149                         count = 0;
150                 else if (count > 4095)
151                         count = 4095;
152                 buf[count] = '\0';
153                 fprintf(f, "%s", buf);
154
155                 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
156                 if (count < 0)
157                         count = 0;
158                 else if (count > 4095)
159                         count = 4095;
160                 buf[count] = '\0';
161                 fprintf(f, "%s", buf);
162                 os_free(buf);
163         }
164         fclose(f);
165 }
166
167
168 int handle_dump_state_iface(struct hostapd_iface *iface, void *ctx)
169 {
170         size_t i;
171
172         for (i = 0; i < iface->num_bss; i++)
173                 hostapd_dump_state(iface->bss[i]);
174
175         return 0;
176 }