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 " [-w<write pcap file>] [-f<MSK/PMK file>]\n");
36 static void passphrase_deinit(struct wlantest_passphrase *p)
38 dl_list_del(&p->list);
43 static void secret_deinit(struct wlantest_radius_secret *r)
45 dl_list_del(&r->list);
50 static void wlantest_init(struct wlantest *wt)
53 os_memset(wt, 0, sizeof(*wt));
54 wt->monitor_sock = -1;
56 for (i = 0; i < MAX_CTRL_CONNECTIONS; i++)
57 wt->ctrl_socks[i] = -1;
58 dl_list_init(&wt->passphrase);
59 dl_list_init(&wt->bss);
60 dl_list_init(&wt->secret);
61 dl_list_init(&wt->radius);
62 dl_list_init(&wt->pmk);
63 dl_list_init(&wt->wep);
67 void radius_deinit(struct wlantest_radius *r)
69 dl_list_del(&r->list);
74 static void wlantest_deinit(struct wlantest *wt)
76 struct wlantest_passphrase *p, *pn;
77 struct wlantest_radius_secret *s, *sn;
78 struct wlantest_radius *r, *rn;
79 struct wlantest_pmk *pmk, *np;
80 struct wlantest_wep *wep, *nw;
82 if (wt->ctrl_sock >= 0)
84 if (wt->monitor_sock >= 0)
87 dl_list_for_each_safe(p, pn, &wt->passphrase,
88 struct wlantest_passphrase, list)
90 dl_list_for_each_safe(s, sn, &wt->secret,
91 struct wlantest_radius_secret, list)
93 dl_list_for_each_safe(r, rn, &wt->radius, struct wlantest_radius, list)
95 dl_list_for_each_safe(pmk, np, &wt->pmk, struct wlantest_pmk, list)
97 dl_list_for_each_safe(wep, nw, &wt->wep, struct wlantest_wep, list)
99 write_pcap_deinit(wt);
103 static void add_passphrase(struct wlantest *wt, const char *passphrase)
105 struct wlantest_passphrase *p;
106 size_t len = os_strlen(passphrase);
108 if (len < 8 || len > 63)
110 p = os_zalloc(sizeof(*p));
113 os_memcpy(p->passphrase, passphrase, len);
114 dl_list_add(&wt->passphrase, &p->list);
118 static void add_secret(struct wlantest *wt, const char *secret)
120 struct wlantest_radius_secret *s;
121 size_t len = os_strlen(secret);
123 if (len >= MAX_RADIUS_SECRET_LEN)
125 s = os_zalloc(sizeof(*s));
128 os_memcpy(s->secret, secret, len);
129 dl_list_add(&wt->secret, &s->list);
133 static int add_pmk_file(struct wlantest *wt, const char *pmk_file)
138 struct wlantest_pmk *p;
140 f = fopen(pmk_file, "r");
142 wpa_printf(MSG_ERROR, "Could not open '%s'", pmk_file);
146 while (fgets(buf, sizeof(buf), f)) {
148 while (*pos && *pos != '\r' && *pos != '\n')
151 if (pos - buf < 2 * 32)
153 if (hexstr2bin(buf, pmk, 32) < 0)
155 p = os_zalloc(sizeof(*p));
158 os_memcpy(p->pmk, pmk, 32);
159 dl_list_add(&wt->pmk, &p->list);
160 wpa_hexdump(MSG_DEBUG, "Added PMK from file", pmk, 32);
168 int add_wep(struct wlantest *wt, const char *key)
170 struct wlantest_wep *w;
171 size_t len = os_strlen(key);
173 if (len != 2 * 5 && len != 2 * 13) {
174 wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
177 w = os_zalloc(sizeof(*w));
180 if (hexstr2bin(key, w->key, len / 2) < 0) {
182 wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
185 w->key_len = len / 2;
186 dl_list_add(&wt->wep, &w->list);
191 int main(int argc, char *argv[])
194 const char *read_file = NULL;
195 const char *read_wired_file = NULL;
196 const char *write_file = NULL;
197 const char *ifname = NULL;
198 const char *ifname_wired = NULL;
202 wpa_debug_level = MSG_INFO;
203 wpa_debug_show_keys = 1;
205 if (os_program_init())
211 c = getopt(argc, argv, "cdf:Fhi:I:p:P:qr:R:w:W:");
219 if (wpa_debug_level > 0)
223 if (add_pmk_file(&wt, optarg) < 0)
236 ifname_wired = optarg;
239 add_passphrase(&wt, optarg);
242 add_secret(&wt, optarg);
251 read_wired_file = optarg;
257 if (add_wep(&wt, optarg) < 0)
266 if (ifname == NULL && ifname_wired == NULL &&
267 read_file == NULL && read_wired_file == NULL) {
275 if (write_file && write_pcap_init(&wt, write_file) < 0)
278 if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
281 if (read_file && read_cap_file(&wt, read_file) < 0)
284 if (ifname && monitor_init(&wt, ifname) < 0)
287 if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
290 if (ctrl_iface && ctrl_init(&wt) < 0)
293 eloop_register_signal_terminate(wlantest_terminate, &wt);
297 wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
299 wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
301 wlantest_deinit(&wt);