dcd21bb7603c5d8cc5639bec4ccbd67f8b22805f
[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 "radius/radius_client.h"
20 #include "radius/radius_server.h"
21 #include "eapol_auth/eapol_auth_sm.h"
22 #include "eapol_auth/eapol_auth_sm_i.h"
23 #include "eap_server/eap.h"
24 #include "hostapd.h"
25 #include "config.h"
26 #include "sta_flags.h"
27 #include "sta_info.h"
28
29
30 static void fprint_char(FILE *f, char c)
31 {
32         if (c >= 32 && c < 127)
33                 fprintf(f, "%c", c);
34         else
35                 fprintf(f, "<%02x>", c);
36 }
37
38
39 static void ieee802_1x_dump_state(FILE *f, const char *prefix,
40                                   struct sta_info *sta)
41 {
42         struct eapol_state_machine *sm = sta->eapol_sm;
43         if (sm == NULL)
44                 return;
45
46         fprintf(f, "%sIEEE 802.1X:\n", prefix);
47
48         if (sm->identity) {
49                 size_t i;
50                 fprintf(f, "%sidentity=", prefix);
51                 for (i = 0; i < sm->identity_len; i++)
52                         fprint_char(f, sm->identity[i]);
53                 fprintf(f, "\n");
54         }
55
56         fprintf(f, "%slast EAP type: Authentication Server: %d (%s) "
57                 "Supplicant: %d (%s)\n", prefix,
58                 sm->eap_type_authsrv,
59                 eap_server_get_name(0, sm->eap_type_authsrv),
60                 sm->eap_type_supp, eap_server_get_name(0, sm->eap_type_supp));
61
62         fprintf(f, "%scached_packets=%s\n", prefix,
63                 sm->last_recv_radius ? "[RX RADIUS]" : "");
64
65         eapol_auth_dump_state(f, prefix, sm);
66 }
67
68
69 /**
70  * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
71  */
72 static void hostapd_dump_state(struct hostapd_data *hapd)
73 {
74         FILE *f;
75         time_t now;
76         struct sta_info *sta;
77         int i;
78 #ifndef CONFIG_NO_RADIUS
79         char *buf;
80 #endif /* CONFIG_NO_RADIUS */
81
82         if (!hapd->conf->dump_log_name) {
83                 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
84                            "request");
85                 return;
86         }
87
88         wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
89                    hapd->conf->dump_log_name);
90         f = fopen(hapd->conf->dump_log_name, "w");
91         if (f == NULL) {
92                 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
93                            "writing.", hapd->conf->dump_log_name);
94                 return;
95         }
96
97         time(&now);
98         fprintf(f, "hostapd state dump - %s", ctime(&now));
99         fprintf(f, "num_sta=%d num_sta_non_erp=%d "
100                 "num_sta_no_short_slot_time=%d\n"
101                 "num_sta_no_short_preamble=%d\n",
102                 hapd->num_sta, hapd->iface->num_sta_non_erp,
103                 hapd->iface->num_sta_no_short_slot_time,
104                 hapd->iface->num_sta_no_short_preamble);
105
106         for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
107                 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
108
109                 fprintf(f,
110                         "  AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
111                         "  capability=0x%x listen_interval=%d\n",
112                         sta->aid,
113                         sta->flags,
114                         (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
115                         (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
116                         (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
117                         (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
118                         (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
119                         (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
120                          ""),
121                         (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
122                          ""),
123                         (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
124                          "[SHORT_PREAMBLE]" : ""),
125                         (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
126                         (sta->flags & WLAN_STA_WMM ? "[WMM]" : ""),
127                         (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
128                         (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
129                         (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
130                         (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
131                         sta->capability,
132                         sta->listen_interval);
133
134                 fprintf(f, "  supported_rates=");
135                 for (i = 0; i < sta->supported_rates_len; i++)
136                         fprintf(f, "%02x ", sta->supported_rates[i]);
137                 fprintf(f, "\n");
138
139                 fprintf(f,
140                         "  timeout_next=%s\n",
141                         (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
142                          (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
143                           "DEAUTH")));
144
145                 ieee802_1x_dump_state(f, "  ", sta);
146         }
147
148 #ifndef CONFIG_NO_RADIUS
149         buf = os_malloc(4096);
150         if (buf) {
151                 int count = radius_client_get_mib(hapd->radius, buf, 4096);
152                 if (count < 0)
153                         count = 0;
154                 else if (count > 4095)
155                         count = 4095;
156                 buf[count] = '\0';
157                 fprintf(f, "%s", buf);
158
159 #ifdef RADIUS_SERVER
160                 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
161                 if (count < 0)
162                         count = 0;
163                 else if (count > 4095)
164                         count = 4095;
165                 buf[count] = '\0';
166                 fprintf(f, "%s", buf);
167 #endif /* RADIUS_SERVER */
168
169                 os_free(buf);
170         }
171 #endif /* CONFIG_NO_RADIUS */
172         fclose(f);
173 }
174
175
176 int handle_dump_state_iface(struct hostapd_iface *iface, void *ctx)
177 {
178         size_t i;
179
180         for (i = 0; i < iface->num_bss; i++)
181                 hostapd_dump_state(iface->bss[i]);
182
183         return 0;
184 }