wlantest: Add command for adding WEP keys during run time
[mech_eap.git] / wlantest / wlantest.c
1 /*
2  * wlantest - IEEE 802.11 protocol monitoring and testing tool
3  * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "wlantest.h"
20
21
22 extern int wpa_debug_level;
23 extern int wpa_debug_show_keys;
24
25
26 static void wlantest_terminate(int sig, void *signal_ctx)
27 {
28         eloop_terminate();
29 }
30
31
32 static void usage(void)
33 {
34         printf("wlantest [-cddhqq] [-i<ifname>] [-r<pcap file>] "
35                "[-p<passphrase>]\n"
36                 "         [-I<wired ifname>] [-R<wired pcap file>] "
37                "[-P<RADIUS shared secret>]\n"
38                 "         [-w<write pcap file>]\n");
39 }
40
41
42 static void passphrase_deinit(struct wlantest_passphrase *p)
43 {
44         dl_list_del(&p->list);
45         os_free(p);
46 }
47
48
49 static void secret_deinit(struct wlantest_radius_secret *r)
50 {
51         dl_list_del(&r->list);
52         os_free(r);
53 }
54
55
56 static void wlantest_init(struct wlantest *wt)
57 {
58         int i;
59         os_memset(wt, 0, sizeof(*wt));
60         wt->monitor_sock = -1;
61         wt->ctrl_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);
70 }
71
72
73 void radius_deinit(struct wlantest_radius *r)
74 {
75         dl_list_del(&r->list);
76         os_free(r);
77 }
78
79
80 static void wlantest_deinit(struct wlantest *wt)
81 {
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;
87
88         if (wt->ctrl_sock >= 0)
89                 ctrl_deinit(wt);
90         if (wt->monitor_sock >= 0)
91                 monitor_deinit(wt);
92         bss_flush(wt);
93         dl_list_for_each_safe(p, pn, &wt->passphrase,
94                               struct wlantest_passphrase, list)
95                 passphrase_deinit(p);
96         dl_list_for_each_safe(s, sn, &wt->secret,
97                               struct wlantest_radius_secret, list)
98                 secret_deinit(s);
99         dl_list_for_each_safe(r, rn, &wt->radius, struct wlantest_radius, list)
100                 radius_deinit(r);
101         dl_list_for_each_safe(pmk, np, &wt->pmk, struct wlantest_pmk, list)
102                 pmk_deinit(pmk);
103         dl_list_for_each_safe(wep, nw, &wt->wep, struct wlantest_wep, list)
104                 os_free(wep);
105         write_pcap_deinit(wt);
106 }
107
108
109 static void add_passphrase(struct wlantest *wt, const char *passphrase)
110 {
111         struct wlantest_passphrase *p;
112         size_t len = os_strlen(passphrase);
113
114         if (len < 8 || len > 63)
115                 return;
116         p = os_zalloc(sizeof(*p));
117         if (p == NULL)
118                 return;
119         os_memcpy(p->passphrase, passphrase, len);
120         dl_list_add(&wt->passphrase, &p->list);
121 }
122
123
124 static void add_secret(struct wlantest *wt, const char *secret)
125 {
126         struct wlantest_radius_secret *s;
127         size_t len = os_strlen(secret);
128
129         if (len >= MAX_RADIUS_SECRET_LEN)
130                 return;
131         s = os_zalloc(sizeof(*s));
132         if (s == NULL)
133                 return;
134         os_memcpy(s->secret, secret, len);
135         dl_list_add(&wt->secret, &s->list);
136 }
137
138
139 int add_wep(struct wlantest *wt, const char *key)
140 {
141         struct wlantest_wep *w;
142         size_t len = os_strlen(key);
143
144         if (len != 2 * 5 && len != 2 * 13) {
145                 wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
146                 return -1;
147         }
148         w = os_zalloc(sizeof(*w));
149         if (w == NULL)
150                 return -1;
151         if (hexstr2bin(key, w->key, len / 2) < 0) {
152                 os_free(w);
153                 wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
154                 return -1;
155         }
156         w->key_len = len / 2;
157         dl_list_add(&wt->wep, &w->list);
158         return 0;
159 }
160
161
162 int main(int argc, char *argv[])
163 {
164         int c;
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;
170         struct wlantest wt;
171         int ctrl_iface = 0;
172
173         wpa_debug_level = MSG_INFO;
174         wpa_debug_show_keys = 1;
175
176         if (os_program_init())
177                 return -1;
178
179         wlantest_init(&wt);
180
181         for (;;) {
182                 c = getopt(argc, argv, "cdhi:I:p:P:qr:R:w:W:");
183                 if (c < 0)
184                         break;
185                 switch (c) {
186                 case 'c':
187                         ctrl_iface = 1;
188                         break;
189                 case 'd':
190                         if (wpa_debug_level > 0)
191                                 wpa_debug_level--;
192                         break;
193                 case 'h':
194                         usage();
195                         return 0;
196                 case 'i':
197                         ifname = optarg;
198                         break;
199                 case 'I':
200                         ifname_wired = optarg;
201                         break;
202                 case 'p':
203                         add_passphrase(&wt, optarg);
204                         break;
205                 case 'P':
206                         add_secret(&wt, optarg);
207                         break;
208                 case 'q':
209                         wpa_debug_level++;
210                         break;
211                 case 'r':
212                         read_file = optarg;
213                         break;
214                 case 'R':
215                         read_wired_file = optarg;
216                         break;
217                 case 'w':
218                         write_file = optarg;
219                         break;
220                 case 'W':
221                         if (add_wep(&wt, optarg) < 0)
222                                 return -1;
223                         break;
224                 default:
225                         usage();
226                         return -1;
227                 }
228         }
229
230         if (ifname == NULL && ifname_wired == NULL &&
231             read_file == NULL && read_wired_file == NULL) {
232                 usage();
233                 return 0;
234         }
235
236         if (eloop_init())
237                 return -1;
238
239         if (write_file && write_pcap_init(&wt, write_file) < 0)
240                 return -1;
241
242         if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
243                 return -1;
244
245         if (read_file && read_cap_file(&wt, read_file) < 0)
246                 return -1;
247
248         if (ifname && monitor_init(&wt, ifname) < 0)
249                 return -1;
250
251         if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
252                 return -1;
253
254         if (ctrl_iface && ctrl_init(&wt) < 0)
255                 return -1;
256
257         eloop_register_signal_terminate(wlantest_terminate, &wt);
258
259         eloop_run();
260
261         wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
262                    "fcs_error=%u",
263                    wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
264
265         wlantest_deinit(&wt);
266
267         eloop_destroy();
268         os_program_deinit();
269
270         return 0;
271 }