2 * Test program for combined WPA authenticator/supplicant
3 * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #include "common/ieee802_11_defs.h"
14 #include "../config.h"
15 #include "rsn_supp/wpa.h"
16 #include "rsn_supp/wpa_ie.h"
17 #include "ap/wpa_auth.h"
21 u8 auth_addr[ETH_ALEN];
22 u8 supp_addr[ETH_ALEN];
25 /* from authenticator */
26 u8 auth_eapol_dst[ETH_ALEN];
28 size_t auth_eapol_len;
32 size_t supp_eapol_len;
35 struct wpa_authenticator *auth_group;
36 struct wpa_state_machine *auth;
44 static int supp_get_bssid(void *ctx, u8 *bssid)
46 struct wpa *wpa = ctx;
47 wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
48 os_memcpy(bssid, wpa->auth_addr, ETH_ALEN);
53 static void supp_set_state(void *ctx, enum wpa_states state)
55 wpa_printf(MSG_DEBUG, "SUPP: %s(state=%d)", __func__, state);
59 static void auth_eapol_rx(void *eloop_data, void *user_ctx)
61 struct wpa *wpa = eloop_data;
63 wpa_printf(MSG_DEBUG, "AUTH: RX EAPOL frame");
64 wpa_receive(wpa->auth_group, wpa->auth, wpa->supp_eapol,
69 static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
72 struct wpa *wpa = ctx;
74 wpa_printf(MSG_DEBUG, "SUPP: %s(dest=" MACSTR " proto=0x%04x "
76 __func__, MAC2STR(dest), proto, (unsigned long) len);
78 os_free(wpa->supp_eapol);
79 wpa->supp_eapol = os_malloc(len);
80 if (wpa->supp_eapol == NULL)
82 os_memcpy(wpa->supp_eapol, buf, len);
83 wpa->supp_eapol_len = len;
84 eloop_register_timeout(0, 0, auth_eapol_rx, wpa, NULL);
90 static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
91 u16 data_len, size_t *msg_len, void **data_pos)
93 struct ieee802_1x_hdr *hdr;
95 wpa_printf(MSG_DEBUG, "SUPP: %s(type=%d data_len=%d)",
96 __func__, type, data_len);
98 *msg_len = sizeof(*hdr) + data_len;
99 hdr = os_malloc(*msg_len);
105 hdr->length = host_to_be16(data_len);
108 os_memcpy(hdr + 1, data, data_len);
110 os_memset(hdr + 1, 0, data_len);
119 static int supp_get_beacon_ie(void *ctx)
121 struct wpa *wpa = ctx;
125 wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
127 ie = wpa_auth_get_wpa_ie(wpa->auth_group, &ielen);
128 if (ie == NULL || ielen < 1)
130 if (ie[0] == WLAN_EID_RSN)
131 return wpa_sm_set_ap_rsn_ie(wpa->supp, ie, 2 + ie[1]);
132 return wpa_sm_set_ap_wpa_ie(wpa->supp, ie, 2 + ie[1]);
136 static int supp_set_key(void *ctx, enum wpa_alg alg,
137 const u8 *addr, int key_idx, int set_tx,
138 const u8 *seq, size_t seq_len,
139 const u8 *key, size_t key_len)
141 wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
143 __func__, alg, MAC2STR(addr), key_idx, set_tx);
144 wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
145 wpa_hexdump(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
150 static int supp_mlme_setprotection(void *ctx, const u8 *addr,
151 int protection_type, int key_type)
153 wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
155 __func__, MAC2STR(addr), protection_type, key_type);
160 static void supp_cancel_auth_timeout(void *ctx)
162 wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
166 static int supp_init(struct wpa *wpa)
168 struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
174 ctx->set_state = supp_set_state;
175 ctx->get_bssid = supp_get_bssid;
176 ctx->ether_send = supp_ether_send;
177 ctx->get_beacon_ie = supp_get_beacon_ie;
178 ctx->alloc_eapol = supp_alloc_eapol;
179 ctx->set_key = supp_set_key;
180 ctx->mlme_setprotection = supp_mlme_setprotection;
181 ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
182 wpa->supp = wpa_sm_init(ctx);
183 if (wpa->supp == NULL) {
184 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
188 wpa_sm_set_own_addr(wpa->supp, wpa->supp_addr);
189 wpa_sm_set_param(wpa->supp, WPA_PARAM_RSN_ENABLED, 1);
190 wpa_sm_set_param(wpa->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
191 wpa_sm_set_param(wpa->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
192 wpa_sm_set_param(wpa->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
193 wpa_sm_set_param(wpa->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
194 wpa_sm_set_pmk(wpa->supp, wpa->psk, PMK_LEN);
196 wpa->supp_ie_len = sizeof(wpa->supp_ie);
197 if (wpa_sm_set_assoc_wpa_ie_default(wpa->supp, wpa->supp_ie,
198 &wpa->supp_ie_len) < 0) {
199 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
204 wpa_sm_notify_assoc(wpa->supp, wpa->auth_addr);
210 static void auth_logger(void *ctx, const u8 *addr, logger_level level,
214 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
217 wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
221 static void supp_eapol_rx(void *eloop_data, void *user_ctx)
223 struct wpa *wpa = eloop_data;
225 wpa_printf(MSG_DEBUG, "SUPP: RX EAPOL frame");
226 wpa_sm_rx_eapol(wpa->supp, wpa->auth_addr, wpa->auth_eapol,
227 wpa->auth_eapol_len);
231 static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
232 size_t data_len, int encrypt)
234 struct wpa *wpa = ctx;
236 wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
238 __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
240 os_free(wpa->auth_eapol);
241 wpa->auth_eapol = os_malloc(data_len);
242 if (wpa->auth_eapol == NULL)
244 os_memcpy(wpa->auth_eapol_dst, addr, ETH_ALEN);
245 os_memcpy(wpa->auth_eapol, data, data_len);
246 wpa->auth_eapol_len = data_len;
247 eloop_register_timeout(0, 0, supp_eapol_rx, wpa, NULL);
253 static const u8 * auth_get_psk(void *ctx, const u8 *addr, const u8 *prev_psk)
255 struct wpa *wpa = ctx;
256 wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
257 __func__, MAC2STR(addr), prev_psk);
264 static int auth_init_group(struct wpa *wpa)
266 struct wpa_auth_config conf;
267 struct wpa_auth_callbacks cb;
269 wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
271 os_memset(&conf, 0, sizeof(conf));
273 conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
274 conf.wpa_pairwise = WPA_CIPHER_CCMP;
275 conf.rsn_pairwise = WPA_CIPHER_CCMP;
276 conf.wpa_group = WPA_CIPHER_CCMP;
277 conf.eapol_version = 2;
279 os_memset(&cb, 0, sizeof(cb));
281 cb.logger = auth_logger;
282 cb.send_eapol = auth_send_eapol;
283 cb.get_psk = auth_get_psk;
285 wpa->auth_group = wpa_init(wpa->auth_addr, &conf, &cb);
286 if (wpa->auth_group == NULL) {
287 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
295 static int auth_init(struct wpa *wpa)
297 wpa->auth = wpa_auth_sta_init(wpa->auth_group, wpa->supp_addr, NULL);
298 if (wpa->auth == NULL) {
299 wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
303 if (wpa_validate_wpa_ie(wpa->auth_group, wpa->auth, wpa->supp_ie,
304 wpa->supp_ie_len, NULL, 0) != WPA_IE_OK) {
305 wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
309 wpa_auth_sm_event(wpa->auth, WPA_ASSOC);
311 wpa_auth_sta_associated(wpa->auth_group, wpa->auth);
317 static void deinit(struct wpa *wpa)
319 wpa_auth_sta_deinit(wpa->auth);
320 wpa_sm_deinit(wpa->supp);
321 wpa_deinit(wpa->auth_group);
322 os_free(wpa->auth_eapol);
323 wpa->auth_eapol = NULL;
324 os_free(wpa->supp_eapol);
325 wpa->supp_eapol = NULL;
329 int main(int argc, char *argv[])
333 if (os_program_init())
336 os_memset(&wpa, 0, sizeof(wpa));
337 os_memset(wpa.auth_addr, 0x12, ETH_ALEN);
338 os_memset(wpa.supp_addr, 0x32, ETH_ALEN);
339 os_memset(wpa.psk, 0x44, PMK_LEN);
342 wpa_debug_show_keys = 1;
345 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
349 if (auth_init_group(&wpa) < 0)
352 if (supp_init(&wpa) < 0)
355 if (auth_init(&wpa) < 0)
358 wpa_printf(MSG_DEBUG, "Starting eloop");
360 wpa_printf(MSG_DEBUG, "eloop done");