]> git.ipfire.org Git - thirdparty/hostap.git/blob - wlantest/wlantest.c
wlantest: Add control interface and wlantest_cli
[thirdparty/hostap.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 }
70
71
72 void radius_deinit(struct wlantest_radius *r)
73 {
74 dl_list_del(&r->list);
75 os_free(r);
76 }
77
78
79 static void wlantest_deinit(struct wlantest *wt)
80 {
81 struct wlantest_bss *bss, *n;
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
87 if (wt->ctrl_sock >= 0)
88 ctrl_deinit(wt);
89 if (wt->monitor_sock >= 0)
90 monitor_deinit(wt);
91 dl_list_for_each_safe(bss, n, &wt->bss, struct wlantest_bss, list)
92 bss_deinit(bss);
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 write_pcap_deinit(wt);
104 }
105
106
107 static void add_passphrase(struct wlantest *wt, const char *passphrase)
108 {
109 struct wlantest_passphrase *p;
110 size_t len = os_strlen(passphrase);
111
112 if (len < 8 || len > 63)
113 return;
114 p = os_zalloc(sizeof(*p));
115 if (p == NULL)
116 return;
117 os_memcpy(p->passphrase, passphrase, len);
118 dl_list_add(&wt->passphrase, &p->list);
119 }
120
121
122 static void add_secret(struct wlantest *wt, const char *secret)
123 {
124 struct wlantest_radius_secret *s;
125 size_t len = os_strlen(secret);
126
127 if (len >= MAX_RADIUS_SECRET_LEN)
128 return;
129 s = os_zalloc(sizeof(*s));
130 if (s == NULL)
131 return;
132 os_memcpy(s->secret, secret, len);
133 dl_list_add(&wt->secret, &s->list);
134 }
135
136
137 int main(int argc, char *argv[])
138 {
139 int c;
140 const char *read_file = NULL;
141 const char *read_wired_file = NULL;
142 const char *write_file = NULL;
143 const char *ifname = NULL;
144 const char *ifname_wired = NULL;
145 struct wlantest wt;
146 int ctrl_iface = 0;
147
148 wpa_debug_level = MSG_INFO;
149 wpa_debug_show_keys = 1;
150
151 if (os_program_init())
152 return -1;
153
154 wlantest_init(&wt);
155
156 for (;;) {
157 c = getopt(argc, argv, "cdhi:I:p:P:qr:R:w:");
158 if (c < 0)
159 break;
160 switch (c) {
161 case 'c':
162 ctrl_iface = 1;
163 break;
164 case 'd':
165 if (wpa_debug_level > 0)
166 wpa_debug_level--;
167 break;
168 case 'h':
169 usage();
170 return 0;
171 case 'i':
172 ifname = optarg;
173 break;
174 case 'I':
175 ifname_wired = optarg;
176 break;
177 case 'p':
178 add_passphrase(&wt, optarg);
179 break;
180 case 'P':
181 add_secret(&wt, optarg);
182 break;
183 case 'q':
184 wpa_debug_level++;
185 break;
186 case 'r':
187 read_file = optarg;
188 break;
189 case 'R':
190 read_wired_file = optarg;
191 break;
192 case 'w':
193 write_file = optarg;
194 break;
195 default:
196 usage();
197 return -1;
198 }
199 }
200
201 if (ifname == NULL && ifname_wired == NULL &&
202 read_file == NULL && read_wired_file == NULL) {
203 usage();
204 return 0;
205 }
206
207 if (eloop_init())
208 return -1;
209
210 if (write_file && write_pcap_init(&wt, write_file) < 0)
211 return -1;
212
213 if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
214 return -1;
215
216 if (read_file && read_cap_file(&wt, read_file) < 0)
217 return -1;
218
219 if (ifname && monitor_init(&wt, ifname) < 0)
220 return -1;
221
222 if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
223 return -1;
224
225 if (ctrl_iface && ctrl_init(&wt) < 0)
226 return -1;
227
228 eloop_register_signal_terminate(wlantest_terminate, &wt);
229
230 eloop_run();
231
232 wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
233 "fcs_error=%u",
234 wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
235
236 wlantest_deinit(&wt);
237
238 eloop_destroy();
239 os_program_deinit();
240
241 return 0;
242 }