2 * wlantest - IEEE 802.11 protocol monitoring and testing tool
3 * Copyright (c) 2010-2011, 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;
20 static void wlantest_terminate(int sig, void *signal_ctx)
26 static void usage(void)
28 printf("wlantest [-cddhqqF] [-i<ifname>] [-r<pcap file>] "
30 " [-I<wired ifname>] [-R<wired pcap file>] "
31 "[-P<RADIUS shared secret>]\n"
32 " [-n<write pcapng file>]\n"
33 " [-w<write pcap file>] [-f<MSK/PMK file>]\n");
37 static void passphrase_deinit(struct wlantest_passphrase *p)
39 dl_list_del(&p->list);
44 static void secret_deinit(struct wlantest_radius_secret *r)
46 dl_list_del(&r->list);
51 static void wlantest_init(struct wlantest *wt)
54 os_memset(wt, 0, sizeof(*wt));
55 wt->monitor_sock = -1;
57 for (i = 0; i < MAX_CTRL_CONNECTIONS; i++)
58 wt->ctrl_socks[i] = -1;
59 dl_list_init(&wt->passphrase);
60 dl_list_init(&wt->bss);
61 dl_list_init(&wt->secret);
62 dl_list_init(&wt->radius);
63 dl_list_init(&wt->pmk);
64 dl_list_init(&wt->wep);
68 void radius_deinit(struct wlantest_radius *r)
70 dl_list_del(&r->list);
75 static void wlantest_deinit(struct wlantest *wt)
77 struct wlantest_passphrase *p, *pn;
78 struct wlantest_radius_secret *s, *sn;
79 struct wlantest_radius *r, *rn;
80 struct wlantest_pmk *pmk, *np;
81 struct wlantest_wep *wep, *nw;
83 if (wt->ctrl_sock >= 0)
85 if (wt->monitor_sock >= 0)
88 dl_list_for_each_safe(p, pn, &wt->passphrase,
89 struct wlantest_passphrase, list)
91 dl_list_for_each_safe(s, sn, &wt->secret,
92 struct wlantest_radius_secret, list)
94 dl_list_for_each_safe(r, rn, &wt->radius, struct wlantest_radius, list)
96 dl_list_for_each_safe(pmk, np, &wt->pmk, struct wlantest_pmk, list)
98 dl_list_for_each_safe(wep, nw, &wt->wep, struct wlantest_wep, list)
100 write_pcap_deinit(wt);
101 write_pcapng_deinit(wt);
103 os_free(wt->decrypted);
104 wt->decrypted = NULL;
108 static void add_passphrase(struct wlantest *wt, const char *passphrase)
110 struct wlantest_passphrase *p;
111 size_t len = os_strlen(passphrase);
113 if (len < 8 || len > 63)
115 p = os_zalloc(sizeof(*p));
118 os_memcpy(p->passphrase, passphrase, len);
119 dl_list_add(&wt->passphrase, &p->list);
123 static void add_secret(struct wlantest *wt, const char *secret)
125 struct wlantest_radius_secret *s;
126 size_t len = os_strlen(secret);
128 if (len >= MAX_RADIUS_SECRET_LEN)
130 s = os_zalloc(sizeof(*s));
133 os_memcpy(s->secret, secret, len);
134 dl_list_add(&wt->secret, &s->list);
138 static int add_pmk_file(struct wlantest *wt, const char *pmk_file)
143 struct wlantest_pmk *p;
145 f = fopen(pmk_file, "r");
147 wpa_printf(MSG_ERROR, "Could not open '%s'", pmk_file);
151 while (fgets(buf, sizeof(buf), f)) {
153 while (*pos && *pos != '\r' && *pos != '\n')
156 if (pos - buf < 2 * 32)
158 if (hexstr2bin(buf, pmk, 32) < 0)
160 p = os_zalloc(sizeof(*p));
163 os_memcpy(p->pmk, pmk, 32);
164 dl_list_add(&wt->pmk, &p->list);
165 wpa_hexdump(MSG_DEBUG, "Added PMK from file", pmk, 32);
173 int add_wep(struct wlantest *wt, const char *key)
175 struct wlantest_wep *w;
176 size_t len = os_strlen(key);
178 if (len != 2 * 5 && len != 2 * 13) {
179 wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
182 w = os_zalloc(sizeof(*w));
185 if (hexstr2bin(key, w->key, len / 2) < 0) {
187 wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
190 w->key_len = len / 2;
191 dl_list_add(&wt->wep, &w->list);
196 void add_note(struct wlantest *wt, int level, const char *fmt, ...)
202 if (wt->num_notes == MAX_NOTES)
205 wt->notes[wt->num_notes] = os_malloc(len);
206 if (wt->notes[wt->num_notes] == NULL)
209 wlen = vsnprintf(wt->notes[wt->num_notes], len, fmt, ap);
212 os_free(wt->notes[wt->num_notes]);
213 wt->notes[wt->num_notes] = NULL;
217 wt->notes[wt->num_notes][len - 1] = '\0';
218 wpa_printf(level, "%s", wt->notes[wt->num_notes]);
223 void clear_notes(struct wlantest *wt)
227 for (i = 0; i < wt->num_notes; i++) {
228 os_free(wt->notes[i]);
236 size_t notes_len(struct wlantest *wt, size_t hdrlen)
239 size_t len = wt->num_notes * hdrlen;
241 for (i = 0; i < wt->num_notes; i++)
242 len += os_strlen(wt->notes[i]);
248 int main(int argc, char *argv[])
251 const char *read_file = NULL;
252 const char *read_wired_file = NULL;
253 const char *write_file = NULL;
254 const char *ifname = NULL;
255 const char *ifname_wired = NULL;
256 const char *pcapng_file = NULL;
260 wpa_debug_level = MSG_INFO;
261 wpa_debug_show_keys = 1;
263 if (os_program_init())
269 c = getopt(argc, argv, "cdf:Fhi:I:n:p:P:qr:R:w:W:");
277 if (wpa_debug_level > 0)
281 if (add_pmk_file(&wt, optarg) < 0)
294 ifname_wired = optarg;
297 pcapng_file = optarg;
300 add_passphrase(&wt, optarg);
303 add_secret(&wt, optarg);
312 read_wired_file = optarg;
318 if (add_wep(&wt, optarg) < 0)
327 if (ifname == NULL && ifname_wired == NULL &&
328 read_file == NULL && read_wired_file == NULL) {
336 if (write_file && write_pcap_init(&wt, write_file) < 0)
339 if (pcapng_file && write_pcapng_init(&wt, pcapng_file) < 0)
342 if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
345 if (read_file && read_cap_file(&wt, read_file) < 0)
348 if (ifname && monitor_init(&wt, ifname) < 0)
351 if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
354 if (ctrl_iface && ctrl_init(&wt) < 0)
357 eloop_register_signal_terminate(wlantest_terminate, &wt);
361 wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
363 wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
365 wlantest_deinit(&wt);