wlantest: Derive PMK from RADIUS exchange
[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 [-ddhqq] [-i<ifname>] [-r<pcap file>] "
35                "[-p<passphrase>]\n"
36                 "         [-I<wired ifname>] [-R<wired pcap file>] "
37                "[-P<RADIUS shared secret>]\n");
38 }
39
40
41 static void passphrase_deinit(struct wlantest_passphrase *p)
42 {
43         dl_list_del(&p->list);
44         os_free(p);
45 }
46
47
48 static void secret_deinit(struct wlantest_radius_secret *r)
49 {
50         dl_list_del(&r->list);
51         os_free(r);
52 }
53
54
55 static void wlantest_init(struct wlantest *wt)
56 {
57         os_memset(wt, 0, sizeof(*wt));
58         wt->monitor_sock = -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 }
65
66
67 void radius_deinit(struct wlantest_radius *r)
68 {
69         dl_list_del(&r->list);
70         os_free(r);
71 }
72
73
74 static void wlantest_deinit(struct wlantest *wt)
75 {
76         struct wlantest_bss *bss, *n;
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
82         if (wt->monitor_sock >= 0)
83                 monitor_deinit(wt);
84         dl_list_for_each_safe(bss, n, &wt->bss, struct wlantest_bss, list)
85                 bss_deinit(bss);
86         dl_list_for_each_safe(p, pn, &wt->passphrase,
87                               struct wlantest_passphrase, list)
88                 passphrase_deinit(p);
89         dl_list_for_each_safe(s, sn, &wt->secret,
90                               struct wlantest_radius_secret, list)
91                 secret_deinit(s);
92         dl_list_for_each_safe(r, rn, &wt->radius, struct wlantest_radius, list)
93                 radius_deinit(r);
94         dl_list_for_each_safe(pmk, np, &wt->pmk, struct wlantest_pmk, list)
95                 pmk_deinit(pmk);
96 }
97
98
99 static void add_passphrase(struct wlantest *wt, const char *passphrase)
100 {
101         struct wlantest_passphrase *p;
102         size_t len = os_strlen(passphrase);
103
104         if (len < 8 || len > 63)
105                 return;
106         p = os_zalloc(sizeof(*p));
107         if (p == NULL)
108                 return;
109         os_memcpy(p->passphrase, passphrase, len);
110         dl_list_add(&wt->passphrase, &p->list);
111 }
112
113
114 static void add_secret(struct wlantest *wt, const char *secret)
115 {
116         struct wlantest_radius_secret *s;
117         size_t len = os_strlen(secret);
118
119         if (len >= MAX_RADIUS_SECRET_LEN)
120                 return;
121         s = os_zalloc(sizeof(*s));
122         if (s == NULL)
123                 return;
124         os_memcpy(s->secret, secret, len);
125         dl_list_add(&wt->secret, &s->list);
126 }
127
128
129 int main(int argc, char *argv[])
130 {
131         int c;
132         const char *read_file = NULL;
133         const char *read_wired_file = NULL;
134         const char *ifname = NULL;
135         const char *ifname_wired = NULL;
136         struct wlantest wt;
137
138         wpa_debug_level = MSG_INFO;
139         wpa_debug_show_keys = 1;
140
141         if (os_program_init())
142                 return -1;
143
144         wlantest_init(&wt);
145
146         for (;;) {
147                 c = getopt(argc, argv, "dhi:I:p:P:qr:R:");
148                 if (c < 0)
149                         break;
150                 switch (c) {
151                 case 'd':
152                         if (wpa_debug_level > 0)
153                                 wpa_debug_level--;
154                         break;
155                 case 'h':
156                         usage();
157                         return 0;
158                 case 'i':
159                         ifname = optarg;
160                         break;
161                 case 'I':
162                         ifname_wired = optarg;
163                         break;
164                 case 'p':
165                         add_passphrase(&wt, optarg);
166                         break;
167                 case 'P':
168                         add_secret(&wt, optarg);
169                         break;
170                 case 'q':
171                         wpa_debug_level++;
172                         break;
173                 case 'r':
174                         read_file = optarg;
175                         break;
176                 case 'R':
177                         read_wired_file = optarg;
178                         break;
179                 default:
180                         usage();
181                         return -1;
182                 }
183         }
184
185         if (ifname == NULL && ifname_wired == NULL &&
186             read_file == NULL && read_wired_file == NULL) {
187                 usage();
188                 return 0;
189         }
190
191         if (eloop_init())
192                 return -1;
193
194         if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
195                 return -1;
196
197         if (read_file && read_cap_file(&wt, read_file) < 0)
198                 return -1;
199
200         if (ifname && monitor_init(&wt, ifname) < 0)
201                 return -1;
202
203         if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
204                 return -1;
205
206         eloop_register_signal_terminate(wlantest_terminate, &wt);
207
208         eloop_run();
209
210         wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
211                    "fcs_error=%u",
212                    wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
213
214         wlantest_deinit(&wt);
215
216         eloop_destroy();
217         os_program_deinit();
218
219         return 0;
220 }