2 * wlantest - IEEE 802.11 protocol monitoring and testing tool
3 * Copyright (c) 2010-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
16 extern int wpa_debug_level;
17 extern int wpa_debug_show_keys;
18 extern int wpa_debug_timestamp;
21 static void wlantest_terminate(int sig, void *signal_ctx)
27 static void usage(void)
29 printf("wlantest [-cddhqqFt] [-i<ifname>] [-r<pcap file>] "
31 " [-I<wired ifname>] [-R<wired pcap file>] "
32 "[-P<RADIUS shared secret>]\n"
33 " [-n<write pcapng file>]\n"
34 " [-w<write pcap file>] [-f<MSK/PMK file>]\n"
39 static void passphrase_deinit(struct wlantest_passphrase *p)
41 dl_list_del(&p->list);
46 static void secret_deinit(struct wlantest_radius_secret *r)
48 dl_list_del(&r->list);
53 static void wlantest_init(struct wlantest *wt)
56 os_memset(wt, 0, sizeof(*wt));
57 wt->monitor_sock = -1;
59 for (i = 0; i < MAX_CTRL_CONNECTIONS; i++)
60 wt->ctrl_socks[i] = -1;
61 dl_list_init(&wt->passphrase);
62 dl_list_init(&wt->bss);
63 dl_list_init(&wt->secret);
64 dl_list_init(&wt->radius);
65 dl_list_init(&wt->pmk);
66 dl_list_init(&wt->wep);
70 void radius_deinit(struct wlantest_radius *r)
72 dl_list_del(&r->list);
77 static void wlantest_deinit(struct wlantest *wt)
79 struct wlantest_passphrase *p, *pn;
80 struct wlantest_radius_secret *s, *sn;
81 struct wlantest_radius *r, *rn;
82 struct wlantest_pmk *pmk, *np;
83 struct wlantest_wep *wep, *nw;
85 if (wt->ctrl_sock >= 0)
87 if (wt->monitor_sock >= 0)
90 dl_list_for_each_safe(p, pn, &wt->passphrase,
91 struct wlantest_passphrase, list)
93 dl_list_for_each_safe(s, sn, &wt->secret,
94 struct wlantest_radius_secret, list)
96 dl_list_for_each_safe(r, rn, &wt->radius, struct wlantest_radius, list)
98 dl_list_for_each_safe(pmk, np, &wt->pmk, struct wlantest_pmk, list)
100 dl_list_for_each_safe(wep, nw, &wt->wep, struct wlantest_wep, list)
102 write_pcap_deinit(wt);
103 write_pcapng_deinit(wt);
105 os_free(wt->decrypted);
106 wt->decrypted = NULL;
110 static void add_passphrase(struct wlantest *wt, const char *passphrase)
112 struct wlantest_passphrase *p;
113 size_t len = os_strlen(passphrase);
115 if (len < 8 || len > 63)
117 p = os_zalloc(sizeof(*p));
120 os_memcpy(p->passphrase, passphrase, len);
121 dl_list_add(&wt->passphrase, &p->list);
125 static void add_secret(struct wlantest *wt, const char *secret)
127 struct wlantest_radius_secret *s;
128 size_t len = os_strlen(secret);
130 if (len >= MAX_RADIUS_SECRET_LEN)
132 s = os_zalloc(sizeof(*s));
135 os_memcpy(s->secret, secret, len);
136 dl_list_add(&wt->secret, &s->list);
140 static int add_pmk_file(struct wlantest *wt, const char *pmk_file)
145 struct wlantest_pmk *p;
147 f = fopen(pmk_file, "r");
149 wpa_printf(MSG_ERROR, "Could not open '%s'", pmk_file);
153 while (fgets(buf, sizeof(buf), f)) {
155 while (*pos && *pos != '\r' && *pos != '\n')
158 if (pos - buf < 2 * 32)
160 if (hexstr2bin(buf, pmk, 32) < 0)
162 p = os_zalloc(sizeof(*p));
165 os_memcpy(p->pmk, pmk, 32);
166 dl_list_add(&wt->pmk, &p->list);
167 wpa_hexdump(MSG_DEBUG, "Added PMK from file", pmk, 32);
175 int add_wep(struct wlantest *wt, const char *key)
177 struct wlantest_wep *w;
178 size_t len = os_strlen(key);
180 if (len != 2 * 5 && len != 2 * 13) {
181 wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
184 w = os_zalloc(sizeof(*w));
187 if (hexstr2bin(key, w->key, len / 2) < 0) {
189 wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
192 w->key_len = len / 2;
193 dl_list_add(&wt->wep, &w->list);
198 void add_note(struct wlantest *wt, int level, const char *fmt, ...)
204 if (wt->num_notes == MAX_NOTES)
207 wt->notes[wt->num_notes] = os_malloc(len);
208 if (wt->notes[wt->num_notes] == NULL)
211 wlen = vsnprintf(wt->notes[wt->num_notes], len, fmt, ap);
214 os_free(wt->notes[wt->num_notes]);
215 wt->notes[wt->num_notes] = NULL;
219 wt->notes[wt->num_notes][len - 1] = '\0';
220 wpa_printf(level, "%s", wt->notes[wt->num_notes]);
225 void clear_notes(struct wlantest *wt)
229 for (i = 0; i < wt->num_notes; i++) {
230 os_free(wt->notes[i]);
238 size_t notes_len(struct wlantest *wt, size_t hdrlen)
241 size_t len = wt->num_notes * hdrlen;
243 for (i = 0; i < wt->num_notes; i++)
244 len += os_strlen(wt->notes[i]);
250 int wlantest_relog(struct wlantest *wt)
254 wpa_printf(MSG_INFO, "Re-open log/capture files");
255 if (wpa_debug_reopen_file())
258 if (wt->write_file) {
259 write_pcap_deinit(wt);
260 if (write_pcap_init(wt, wt->write_file) < 0)
264 if (wt->pcapng_file) {
265 write_pcapng_deinit(wt);
266 if (write_pcapng_init(wt, wt->pcapng_file) < 0)
274 int main(int argc, char *argv[])
277 const char *read_file = NULL;
278 const char *read_wired_file = NULL;
279 const char *ifname = NULL;
280 const char *ifname_wired = NULL;
281 const char *logfile = NULL;
285 wpa_debug_level = MSG_INFO;
286 wpa_debug_show_keys = 1;
288 if (os_program_init())
294 c = getopt(argc, argv, "cdf:Fhi:I:L:n:p:P:qr:R:tw:W:");
302 if (wpa_debug_level > 0)
306 if (add_pmk_file(&wt, optarg) < 0)
319 ifname_wired = optarg;
325 wt.pcapng_file = optarg;
328 add_passphrase(&wt, optarg);
331 add_secret(&wt, optarg);
340 read_wired_file = optarg;
343 wpa_debug_timestamp = 1;
346 wt.write_file = optarg;
349 if (add_wep(&wt, optarg) < 0)
358 if (ifname == NULL && ifname_wired == NULL &&
359 read_file == NULL && read_wired_file == NULL) {
368 wpa_debug_open_file(logfile);
370 if (wt.write_file && write_pcap_init(&wt, wt.write_file) < 0)
373 if (wt.pcapng_file && write_pcapng_init(&wt, wt.pcapng_file) < 0)
376 if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
379 if (read_file && read_cap_file(&wt, read_file) < 0)
382 if (ifname && monitor_init(&wt, ifname) < 0)
385 if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
388 if (ctrl_iface && ctrl_init(&wt) < 0)
391 eloop_register_signal_terminate(wlantest_terminate, &wt);
395 wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
397 wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
399 wlantest_deinit(&wt);
401 wpa_debug_close_file();