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 static void wlantest_terminate(int sig, void *signal_ctx)
22 static void usage(void)
24 printf("wlantest [-cddhqqFt] [-i<ifname>] [-r<pcap file>] "
26 " [-I<wired ifname>] [-R<wired pcap file>] "
27 "[-P<RADIUS shared secret>]\n"
28 " [-n<write pcapng file>]\n"
29 " [-w<write pcap file>] [-f<MSK/PMK file>]\n"
30 " [-L<log file>] [-T<PTK file>]\n");
34 static void passphrase_deinit(struct wlantest_passphrase *p)
36 dl_list_del(&p->list);
41 static void secret_deinit(struct wlantest_radius_secret *r)
43 dl_list_del(&r->list);
48 static void wlantest_init(struct wlantest *wt)
51 os_memset(wt, 0, sizeof(*wt));
52 wt->monitor_sock = -1;
54 for (i = 0; i < MAX_CTRL_CONNECTIONS; i++)
55 wt->ctrl_socks[i] = -1;
56 dl_list_init(&wt->passphrase);
57 dl_list_init(&wt->bss);
58 dl_list_init(&wt->secret);
59 dl_list_init(&wt->radius);
60 dl_list_init(&wt->pmk);
61 dl_list_init(&wt->ptk);
62 dl_list_init(&wt->wep);
66 void radius_deinit(struct wlantest_radius *r)
68 dl_list_del(&r->list);
73 static void ptk_deinit(struct wlantest_ptk *ptk)
75 dl_list_del(&ptk->list);
80 static void wlantest_deinit(struct wlantest *wt)
82 struct wlantest_passphrase *p, *pn;
83 struct wlantest_radius_secret *s, *sn;
84 struct wlantest_radius *r, *rn;
85 struct wlantest_pmk *pmk, *np;
86 struct wlantest_ptk *ptk, *npt;
87 struct wlantest_wep *wep, *nw;
89 if (wt->ctrl_sock >= 0)
91 if (wt->monitor_sock >= 0)
94 dl_list_for_each_safe(p, pn, &wt->passphrase,
95 struct wlantest_passphrase, list)
97 dl_list_for_each_safe(s, sn, &wt->secret,
98 struct wlantest_radius_secret, list)
100 dl_list_for_each_safe(r, rn, &wt->radius, struct wlantest_radius, list)
102 dl_list_for_each_safe(pmk, np, &wt->pmk, struct wlantest_pmk, list)
104 dl_list_for_each_safe(ptk, npt, &wt->ptk, struct wlantest_ptk, list)
106 dl_list_for_each_safe(wep, nw, &wt->wep, struct wlantest_wep, list)
108 write_pcap_deinit(wt);
109 write_pcapng_deinit(wt);
111 os_free(wt->decrypted);
112 wt->decrypted = NULL;
116 static void add_passphrase(struct wlantest *wt, const char *passphrase)
118 struct wlantest_passphrase *p;
119 size_t len = os_strlen(passphrase);
121 if (len < 8 || len > 63)
123 p = os_zalloc(sizeof(*p));
126 os_memcpy(p->passphrase, passphrase, len);
127 dl_list_add(&wt->passphrase, &p->list);
131 static void add_secret(struct wlantest *wt, const char *secret)
133 struct wlantest_radius_secret *s;
134 size_t len = os_strlen(secret);
136 if (len >= MAX_RADIUS_SECRET_LEN)
138 s = os_zalloc(sizeof(*s));
141 os_memcpy(s->secret, secret, len);
142 dl_list_add(&wt->secret, &s->list);
146 static int add_pmk_file(struct wlantest *wt, const char *pmk_file)
151 struct wlantest_pmk *p;
153 f = fopen(pmk_file, "r");
155 wpa_printf(MSG_ERROR, "Could not open '%s'", pmk_file);
159 while (fgets(buf, sizeof(buf), f)) {
161 while (*pos && *pos != '\r' && *pos != '\n')
164 if (pos - buf < 2 * 32)
166 if (hexstr2bin(buf, pmk, 32) < 0)
168 p = os_zalloc(sizeof(*p));
171 os_memcpy(p->pmk, pmk, 32);
172 dl_list_add(&wt->pmk, &p->list);
173 wpa_hexdump(MSG_DEBUG, "Added PMK from file", pmk, 32);
181 static int add_ptk_file(struct wlantest *wt, const char *ptk_file)
187 struct wlantest_ptk *p;
189 f = fopen(ptk_file, "r");
191 wpa_printf(MSG_ERROR, "Could not open '%s'", ptk_file);
195 while (fgets(buf, sizeof(buf), f)) {
197 while (*pos && *pos != '\r' && *pos != '\n')
204 if (ptk_len != 16 && ptk_len != 32 &&
205 ptk_len != 48 && ptk_len != 64)
207 if (hexstr2bin(buf, ptk, ptk_len) < 0)
209 p = os_zalloc(sizeof(*p));
213 os_memcpy(p->ptk.tk1, ptk, ptk_len);
214 p->ptk_len = 32 + ptk_len;
216 os_memcpy(&p->ptk, ptk, ptk_len);
217 p->ptk_len = ptk_len;
219 dl_list_add(&wt->ptk, &p->list);
220 wpa_hexdump(MSG_DEBUG, "Added PTK from file", ptk, ptk_len);
228 int add_wep(struct wlantest *wt, const char *key)
230 struct wlantest_wep *w;
231 size_t len = os_strlen(key);
233 if (len != 2 * 5 && len != 2 * 13) {
234 wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
237 w = os_zalloc(sizeof(*w));
240 if (hexstr2bin(key, w->key, len / 2) < 0) {
242 wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
245 w->key_len = len / 2;
246 dl_list_add(&wt->wep, &w->list);
251 void add_note(struct wlantest *wt, int level, const char *fmt, ...)
257 if (wt->num_notes == MAX_NOTES)
260 wt->notes[wt->num_notes] = os_malloc(len);
261 if (wt->notes[wt->num_notes] == NULL)
264 wlen = vsnprintf(wt->notes[wt->num_notes], len, fmt, ap);
267 os_free(wt->notes[wt->num_notes]);
268 wt->notes[wt->num_notes] = NULL;
272 wt->notes[wt->num_notes][len - 1] = '\0';
273 wpa_printf(level, "%s", wt->notes[wt->num_notes]);
278 void clear_notes(struct wlantest *wt)
282 for (i = 0; i < wt->num_notes; i++) {
283 os_free(wt->notes[i]);
291 size_t notes_len(struct wlantest *wt, size_t hdrlen)
294 size_t len = wt->num_notes * hdrlen;
296 for (i = 0; i < wt->num_notes; i++)
297 len += os_strlen(wt->notes[i]);
303 int wlantest_relog(struct wlantest *wt)
307 wpa_printf(MSG_INFO, "Re-open log/capture files");
308 if (wpa_debug_reopen_file())
311 if (wt->write_file) {
312 write_pcap_deinit(wt);
313 if (write_pcap_init(wt, wt->write_file) < 0)
317 if (wt->pcapng_file) {
318 write_pcapng_deinit(wt);
319 if (write_pcapng_init(wt, wt->pcapng_file) < 0)
327 int main(int argc, char *argv[])
330 const char *read_file = NULL;
331 const char *read_wired_file = NULL;
332 const char *ifname = NULL;
333 const char *ifname_wired = NULL;
334 const char *logfile = NULL;
338 wpa_debug_level = MSG_INFO;
339 wpa_debug_show_keys = 1;
341 if (os_program_init())
347 c = getopt(argc, argv, "cdf:Fhi:I:L:n:p:P:qr:R:tT:w:W:");
355 if (wpa_debug_level > 0)
359 if (add_pmk_file(&wt, optarg) < 0)
372 ifname_wired = optarg;
378 wt.pcapng_file = optarg;
381 add_passphrase(&wt, optarg);
384 add_secret(&wt, optarg);
393 read_wired_file = optarg;
396 wpa_debug_timestamp = 1;
399 if (add_ptk_file(&wt, optarg) < 0)
403 wt.write_file = optarg;
406 if (add_wep(&wt, optarg) < 0)
415 if (ifname == NULL && ifname_wired == NULL &&
416 read_file == NULL && read_wired_file == NULL) {
425 wpa_debug_open_file(logfile);
427 if (wt.write_file && write_pcap_init(&wt, wt.write_file) < 0)
430 if (wt.pcapng_file && write_pcapng_init(&wt, wt.pcapng_file) < 0)
433 if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
436 if (read_file && read_cap_file(&wt, read_file) < 0)
439 if (ifname && monitor_init(&wt, ifname) < 0)
442 if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
445 if (ctrl_iface && ctrl_init(&wt) < 0)
448 eloop_register_signal_terminate(wlantest_terminate, &wt);
452 wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
454 wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
456 wlantest_deinit(&wt);
458 wpa_debug_close_file();