2 * wlantest - IEEE 802.11 protocol monitoring and testing tool
3 * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "utils/eloop.h"
22 extern int wpa_debug_level;
23 extern int wpa_debug_show_keys;
26 static void wlantest_terminate(int sig, void *signal_ctx)
32 static void usage(void)
34 printf("wlantest [-cddhqq] [-i<ifname>] [-r<pcap file>] "
36 " [-I<wired ifname>] [-R<wired pcap file>] "
37 "[-P<RADIUS shared secret>]\n"
38 " [-w<write pcap file>]\n");
42 static void passphrase_deinit(struct wlantest_passphrase *p)
44 dl_list_del(&p->list);
49 static void secret_deinit(struct wlantest_radius_secret *r)
51 dl_list_del(&r->list);
56 static void wlantest_init(struct wlantest *wt)
59 os_memset(wt, 0, sizeof(*wt));
60 wt->monitor_sock = -1;
62 for (i = 0; i < MAX_CTRL_CONNECTIONS; i++)
63 wt->ctrl_socks[i] = -1;
64 dl_list_init(&wt->passphrase);
65 dl_list_init(&wt->bss);
66 dl_list_init(&wt->secret);
67 dl_list_init(&wt->radius);
68 dl_list_init(&wt->pmk);
69 dl_list_init(&wt->wep);
73 void radius_deinit(struct wlantest_radius *r)
75 dl_list_del(&r->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_wep *wep, *nw;
88 if (wt->ctrl_sock >= 0)
90 if (wt->monitor_sock >= 0)
93 dl_list_for_each_safe(p, pn, &wt->passphrase,
94 struct wlantest_passphrase, list)
96 dl_list_for_each_safe(s, sn, &wt->secret,
97 struct wlantest_radius_secret, list)
99 dl_list_for_each_safe(r, rn, &wt->radius, struct wlantest_radius, list)
101 dl_list_for_each_safe(pmk, np, &wt->pmk, struct wlantest_pmk, list)
103 dl_list_for_each_safe(wep, nw, &wt->wep, struct wlantest_wep, list)
105 write_pcap_deinit(wt);
109 static void add_passphrase(struct wlantest *wt, const char *passphrase)
111 struct wlantest_passphrase *p;
112 size_t len = os_strlen(passphrase);
114 if (len < 8 || len > 63)
116 p = os_zalloc(sizeof(*p));
119 os_memcpy(p->passphrase, passphrase, len);
120 dl_list_add(&wt->passphrase, &p->list);
124 static void add_secret(struct wlantest *wt, const char *secret)
126 struct wlantest_radius_secret *s;
127 size_t len = os_strlen(secret);
129 if (len >= MAX_RADIUS_SECRET_LEN)
131 s = os_zalloc(sizeof(*s));
134 os_memcpy(s->secret, secret, len);
135 dl_list_add(&wt->secret, &s->list);
139 int add_wep(struct wlantest *wt, const char *key)
141 struct wlantest_wep *w;
142 size_t len = os_strlen(key);
144 if (len != 2 * 5 && len != 2 * 13) {
145 wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
148 w = os_zalloc(sizeof(*w));
151 if (hexstr2bin(key, w->key, len / 2) < 0) {
153 wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
156 w->key_len = len / 2;
157 dl_list_add(&wt->wep, &w->list);
162 int main(int argc, char *argv[])
165 const char *read_file = NULL;
166 const char *read_wired_file = NULL;
167 const char *write_file = NULL;
168 const char *ifname = NULL;
169 const char *ifname_wired = NULL;
173 wpa_debug_level = MSG_INFO;
174 wpa_debug_show_keys = 1;
176 if (os_program_init())
182 c = getopt(argc, argv, "cdhi:I:p:P:qr:R:w:W:");
190 if (wpa_debug_level > 0)
200 ifname_wired = optarg;
203 add_passphrase(&wt, optarg);
206 add_secret(&wt, optarg);
215 read_wired_file = optarg;
221 if (add_wep(&wt, optarg) < 0)
230 if (ifname == NULL && ifname_wired == NULL &&
231 read_file == NULL && read_wired_file == NULL) {
239 if (write_file && write_pcap_init(&wt, write_file) < 0)
242 if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
245 if (read_file && read_cap_file(&wt, read_file) < 0)
248 if (ifname && monitor_init(&wt, ifname) < 0)
251 if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
254 if (ctrl_iface && ctrl_init(&wt) < 0)
257 eloop_register_signal_terminate(wlantest_terminate, &wt);
261 wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
263 wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
265 wlantest_deinit(&wt);