blob: 3e5d412eeb8c607721f03459d07ea328152e84db [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - IBSS RSN
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003 * Copyright (c) 2009-2013, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070012#include "common/wpa_ctrl.h"
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070013#include "utils/eloop.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "l2_packet/l2_packet.h"
15#include "rsn_supp/wpa.h"
16#include "rsn_supp/wpa_ie.h"
17#include "ap/wpa_auth.h"
18#include "wpa_supplicant_i.h"
19#include "driver_i.h"
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070020#include "common/ieee802_11_defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "ibss_rsn.h"
22
23
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070024static void ibss_rsn_auth_timeout(void *eloop_ctx, void *timeout_ctx);
25
26
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080027static struct ibss_rsn_peer * ibss_rsn_get_peer(struct ibss_rsn *ibss_rsn,
28 const u8 *addr)
29{
30 struct ibss_rsn_peer *peer;
31
32 for (peer = ibss_rsn->peers; peer; peer = peer->next)
33 if (os_memcmp(addr, peer->addr, ETH_ALEN) == 0)
34 break;
35 return peer;
36}
37
38
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039static void ibss_rsn_free(struct ibss_rsn_peer *peer)
40{
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070041 eloop_cancel_timeout(ibss_rsn_auth_timeout, peer, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042 wpa_auth_sta_deinit(peer->auth);
43 wpa_sm_deinit(peer->supp);
44 os_free(peer);
45}
46
47
48static void supp_set_state(void *ctx, enum wpa_states state)
49{
50 struct ibss_rsn_peer *peer = ctx;
51 peer->supp_state = state;
52}
53
54
55static enum wpa_states supp_get_state(void *ctx)
56{
57 struct ibss_rsn_peer *peer = ctx;
58 return peer->supp_state;
59}
60
61
62static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
63 size_t len)
64{
65 struct ibss_rsn_peer *peer = ctx;
66 struct wpa_supplicant *wpa_s = peer->ibss_rsn->wpa_s;
67
68 wpa_printf(MSG_DEBUG, "SUPP: %s(dest=" MACSTR " proto=0x%04x "
69 "len=%lu)",
70 __func__, MAC2STR(dest), proto, (unsigned long) len);
71
72 if (wpa_s->l2)
73 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
74
75 return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
76}
77
78
79static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
80 u16 data_len, size_t *msg_len, void **data_pos)
81{
82 struct ieee802_1x_hdr *hdr;
83
84 wpa_printf(MSG_DEBUG, "SUPP: %s(type=%d data_len=%d)",
85 __func__, type, data_len);
86
87 *msg_len = sizeof(*hdr) + data_len;
88 hdr = os_malloc(*msg_len);
89 if (hdr == NULL)
90 return NULL;
91
92 hdr->version = 2;
93 hdr->type = type;
94 hdr->length = host_to_be16(data_len);
95
96 if (data)
97 os_memcpy(hdr + 1, data, data_len);
98 else
99 os_memset(hdr + 1, 0, data_len);
100
101 if (data_pos)
102 *data_pos = hdr + 1;
103
104 return (u8 *) hdr;
105}
106
107
108static int supp_get_beacon_ie(void *ctx)
109{
110 struct ibss_rsn_peer *peer = ctx;
111
112 wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
113 /* TODO: get correct RSN IE */
114 return wpa_sm_set_ap_rsn_ie(peer->supp,
115 (u8 *) "\x30\x14\x01\x00"
116 "\x00\x0f\xac\x04"
117 "\x01\x00\x00\x0f\xac\x04"
118 "\x01\x00\x00\x0f\xac\x02"
119 "\x00\x00", 22);
120}
121
122
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700123static void ibss_check_rsn_completed(struct ibss_rsn_peer *peer)
124{
125 struct wpa_supplicant *wpa_s = peer->ibss_rsn->wpa_s;
126
127 if ((peer->authentication_status &
128 (IBSS_RSN_SET_PTK_SUPP | IBSS_RSN_SET_PTK_AUTH)) !=
129 (IBSS_RSN_SET_PTK_SUPP | IBSS_RSN_SET_PTK_AUTH))
130 return;
131 if (peer->authentication_status & IBSS_RSN_REPORTED_PTK)
132 return;
133 peer->authentication_status |= IBSS_RSN_REPORTED_PTK;
134 wpa_msg(wpa_s, MSG_INFO, IBSS_RSN_COMPLETED MACSTR,
135 MAC2STR(peer->addr));
136}
137
138
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700139static int supp_set_key(void *ctx, enum wpa_alg alg,
140 const u8 *addr, int key_idx, int set_tx,
141 const u8 *seq, size_t seq_len,
142 const u8 *key, size_t key_len)
143{
144 struct ibss_rsn_peer *peer = ctx;
145
146 wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
147 "set_tx=%d)",
148 __func__, alg, MAC2STR(addr), key_idx, set_tx);
149 wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
150 wpa_hexdump_key(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
151
152 if (key_idx == 0) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700153 peer->authentication_status |= IBSS_RSN_SET_PTK_SUPP;
154 ibss_check_rsn_completed(peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155 /*
156 * In IBSS RSN, the pairwise key from the 4-way handshake
157 * initiated by the peer with highest MAC address is used.
158 */
159 if (os_memcmp(peer->ibss_rsn->wpa_s->own_addr, peer->addr,
160 ETH_ALEN) > 0) {
161 wpa_printf(MSG_DEBUG, "SUPP: Do not use this PTK");
162 return 0;
163 }
164 }
165
166 if (is_broadcast_ether_addr(addr))
167 addr = peer->addr;
168 return wpa_drv_set_key(peer->ibss_rsn->wpa_s, alg, addr, key_idx,
169 set_tx, seq, seq_len, key, key_len);
170}
171
172
173static void * supp_get_network_ctx(void *ctx)
174{
175 struct ibss_rsn_peer *peer = ctx;
176 return wpa_supplicant_get_ssid(peer->ibss_rsn->wpa_s);
177}
178
179
180static int supp_mlme_setprotection(void *ctx, const u8 *addr,
181 int protection_type, int key_type)
182{
183 wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
184 "key_type=%d)",
185 __func__, MAC2STR(addr), protection_type, key_type);
186 return 0;
187}
188
189
190static void supp_cancel_auth_timeout(void *ctx)
191{
192 wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
193}
194
195
196static void supp_deauthenticate(void * ctx, int reason_code)
197{
198 wpa_printf(MSG_DEBUG, "SUPP: %s (TODO)", __func__);
199}
200
201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800202static int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
203 const u8 *psk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204{
205 struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
206 if (ctx == NULL)
207 return -1;
208
209 ctx->ctx = peer;
210 ctx->msg_ctx = peer->ibss_rsn->wpa_s;
211 ctx->set_state = supp_set_state;
212 ctx->get_state = supp_get_state;
213 ctx->ether_send = supp_ether_send;
214 ctx->get_beacon_ie = supp_get_beacon_ie;
215 ctx->alloc_eapol = supp_alloc_eapol;
216 ctx->set_key = supp_set_key;
217 ctx->get_network_ctx = supp_get_network_ctx;
218 ctx->mlme_setprotection = supp_mlme_setprotection;
219 ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
220 ctx->deauthenticate = supp_deauthenticate;
221 peer->supp = wpa_sm_init(ctx);
222 if (peer->supp == NULL) {
223 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
224 return -1;
225 }
226
227 wpa_sm_set_own_addr(peer->supp, own_addr);
228 wpa_sm_set_param(peer->supp, WPA_PARAM_RSN_ENABLED, 1);
229 wpa_sm_set_param(peer->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
230 wpa_sm_set_param(peer->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
231 wpa_sm_set_param(peer->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
232 wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
233 wpa_sm_set_pmk(peer->supp, psk, PMK_LEN);
234
235 peer->supp_ie_len = sizeof(peer->supp_ie);
236 if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
237 &peer->supp_ie_len) < 0) {
238 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
239 " failed");
240 return -1;
241 }
242
243 wpa_sm_notify_assoc(peer->supp, peer->addr);
244
245 return 0;
246}
247
248
249static void auth_logger(void *ctx, const u8 *addr, logger_level level,
250 const char *txt)
251{
252 if (addr)
253 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
254 MAC2STR(addr), txt);
255 else
256 wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
257}
258
259
260static const u8 * auth_get_psk(void *ctx, const u8 *addr, const u8 *prev_psk)
261{
262 struct ibss_rsn *ibss_rsn = ctx;
263 wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
264 __func__, MAC2STR(addr), prev_psk);
265 if (prev_psk)
266 return NULL;
267 return ibss_rsn->psk;
268}
269
270
271static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
272 size_t data_len, int encrypt)
273{
274 struct ibss_rsn *ibss_rsn = ctx;
275 struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
276
277 wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
278 "encrypt=%d)",
279 __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
280
281 if (wpa_s->l2)
282 return l2_packet_send(wpa_s->l2, addr, ETH_P_EAPOL, data,
283 data_len);
284
285 return wpa_drv_send_eapol(wpa_s, addr, ETH_P_EAPOL, data, data_len);
286}
287
288
289static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
290 const u8 *addr, int idx, u8 *key, size_t key_len)
291{
292 struct ibss_rsn *ibss_rsn = ctx;
293 u8 seq[6];
294
295 os_memset(seq, 0, sizeof(seq));
296
297 if (addr) {
298 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
299 " key_idx=%d)",
300 __func__, alg, MAC2STR(addr), idx);
301 } else {
302 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
303 __func__, alg, idx);
304 }
305 wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
306
307 if (idx == 0) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700308 if (addr) {
309 struct ibss_rsn_peer *peer;
310 peer = ibss_rsn_get_peer(ibss_rsn, addr);
311 if (peer) {
312 peer->authentication_status |=
313 IBSS_RSN_SET_PTK_AUTH;
314 ibss_check_rsn_completed(peer);
315 }
316 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 /*
318 * In IBSS RSN, the pairwise key from the 4-way handshake
319 * initiated by the peer with highest MAC address is used.
320 */
321 if (addr == NULL ||
322 os_memcmp(ibss_rsn->wpa_s->own_addr, addr, ETH_ALEN) < 0) {
323 wpa_printf(MSG_DEBUG, "AUTH: Do not use this PTK");
324 return 0;
325 }
326 }
327
328 return wpa_drv_set_key(ibss_rsn->wpa_s, alg, addr, idx,
329 1, seq, 6, key, key_len);
330}
331
332
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700333static void ibss_rsn_disconnect(void *ctx, const u8 *addr, u16 reason)
334{
335 struct ibss_rsn *ibss_rsn = ctx;
336 wpa_drv_sta_deauth(ibss_rsn->wpa_s, addr, reason);
337}
338
339
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340static int auth_for_each_sta(void *ctx, int (*cb)(struct wpa_state_machine *sm,
341 void *ctx),
342 void *cb_ctx)
343{
344 struct ibss_rsn *ibss_rsn = ctx;
345 struct ibss_rsn_peer *peer;
346
347 wpa_printf(MSG_DEBUG, "AUTH: for_each_sta");
348
349 for (peer = ibss_rsn->peers; peer; peer = peer->next) {
350 if (peer->auth && cb(peer->auth, cb_ctx))
351 return 1;
352 }
353
354 return 0;
355}
356
357
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800358static void ibss_set_sta_authorized(struct ibss_rsn *ibss_rsn,
359 struct ibss_rsn_peer *peer, int authorized)
360{
361 int res;
362
363 if (authorized) {
364 res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
365 WPA_STA_AUTHORIZED,
366 WPA_STA_AUTHORIZED, ~0);
367 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " authorizing port",
368 MAC2STR(peer->addr));
369 } else {
370 res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
371 0, 0, ~WPA_STA_AUTHORIZED);
372 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " unauthorizing port",
373 MAC2STR(peer->addr));
374 }
375
376 if (res && errno != ENOENT) {
377 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR " flags "
378 "for kernel driver (errno=%d)",
379 MAC2STR(peer->addr), errno);
380 }
381}
382
383
384static void auth_set_eapol(void *ctx, const u8 *addr,
385 wpa_eapol_variable var, int value)
386{
387 struct ibss_rsn *ibss_rsn = ctx;
388 struct ibss_rsn_peer *peer = ibss_rsn_get_peer(ibss_rsn, addr);
389
390 if (peer == NULL)
391 return;
392
393 switch (var) {
394 case WPA_EAPOL_authorized:
395 ibss_set_sta_authorized(ibss_rsn, peer, value);
396 break;
397 default:
398 /* do not handle any other event */
399 wpa_printf(MSG_DEBUG, "AUTH: eapol event not handled %d", var);
400 break;
401 }
402}
403
404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
406 const u8 *own_addr)
407{
408 struct wpa_auth_config conf;
409 struct wpa_auth_callbacks cb;
410
411 wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
412
413 os_memset(&conf, 0, sizeof(conf));
414 conf.wpa = 2;
415 conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
416 conf.wpa_pairwise = WPA_CIPHER_CCMP;
417 conf.rsn_pairwise = WPA_CIPHER_CCMP;
418 conf.wpa_group = WPA_CIPHER_CCMP;
419 conf.eapol_version = 2;
420 conf.wpa_group_rekey = 600;
421
422 os_memset(&cb, 0, sizeof(cb));
423 cb.ctx = ibss_rsn;
424 cb.logger = auth_logger;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800425 cb.set_eapol = auth_set_eapol;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426 cb.send_eapol = auth_send_eapol;
427 cb.get_psk = auth_get_psk;
428 cb.set_key = auth_set_key;
429 cb.for_each_sta = auth_for_each_sta;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700430 cb.disconnect = ibss_rsn_disconnect;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431
432 ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb);
433 if (ibss_rsn->auth_group == NULL) {
434 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
435 return -1;
436 }
437
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800438 wpa_init_keys(ibss_rsn->auth_group);
439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440 return 0;
441}
442
443
444static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
445 struct ibss_rsn_peer *peer)
446{
447 peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr);
448 if (peer->auth == NULL) {
449 wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
450 return -1;
451 }
452
453 /* TODO: get peer RSN IE with Probe Request */
454 if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth,
455 (u8 *) "\x30\x14\x01\x00"
456 "\x00\x0f\xac\x04"
457 "\x01\x00\x00\x0f\xac\x04"
458 "\x01\x00\x00\x0f\xac\x02"
459 "\x00\x00", 22, NULL, 0) !=
460 WPA_IE_OK) {
461 wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
462 return -1;
463 }
464
465 if (wpa_auth_sm_event(peer->auth, WPA_ASSOC))
466 return -1;
467
468 if (wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth))
469 return -1;
470
471 return 0;
472}
473
474
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700475static int ibss_rsn_send_auth(struct ibss_rsn *ibss_rsn, const u8 *da, int seq)
476{
477 struct ieee80211_mgmt auth;
478 const size_t auth_length = IEEE80211_HDRLEN + sizeof(auth.u.auth);
479 struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
480
481 if (wpa_s->driver->send_frame == NULL)
482 return -1;
483
484 os_memset(&auth, 0, sizeof(auth));
485
486 auth.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
487 WLAN_FC_STYPE_AUTH);
488 os_memcpy(auth.da, da, ETH_ALEN);
489 os_memcpy(auth.sa, wpa_s->own_addr, ETH_ALEN);
490 os_memcpy(auth.bssid, wpa_s->bssid, ETH_ALEN);
491
492 auth.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
493 auth.u.auth.auth_transaction = host_to_le16(seq);
494 auth.u.auth.status_code = host_to_le16(WLAN_STATUS_SUCCESS);
495
496 wpa_printf(MSG_DEBUG, "RSN: IBSS TX Auth frame (SEQ %d) to " MACSTR,
497 seq, MAC2STR(da));
498
499 return wpa_s->driver->send_frame(wpa_s->drv_priv, (u8 *) &auth,
500 auth_length, 0);
501}
502
503
504static int ibss_rsn_is_auth_started(struct ibss_rsn_peer * peer)
505{
506 return peer->authentication_status &
507 (IBSS_RSN_AUTH_BY_US | IBSS_RSN_AUTH_EAPOL_BY_US);
508}
509
510
511static struct ibss_rsn_peer *
512ibss_rsn_peer_init(struct ibss_rsn *ibss_rsn, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513{
514 struct ibss_rsn_peer *peer;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700515 if (ibss_rsn == NULL)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700516 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700517
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700518 peer = ibss_rsn_get_peer(ibss_rsn, addr);
519 if (peer) {
520 wpa_printf(MSG_DEBUG, "RSN: IBSS Supplicant for peer "MACSTR
521 " already running", MAC2STR(addr));
522 return peer;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 }
524
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700525 wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Supplicant for peer "MACSTR,
526 MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527
528 peer = os_zalloc(sizeof(*peer));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700529 if (peer == NULL) {
530 wpa_printf(MSG_DEBUG, "RSN: Could not allocate memory.");
531 return NULL;
532 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533
534 peer->ibss_rsn = ibss_rsn;
535 os_memcpy(peer->addr, addr, ETH_ALEN);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700536 peer->authentication_status = IBSS_RSN_AUTH_NOT_AUTHENTICATED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700538 if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr,
539 ibss_rsn->psk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540 ibss_rsn_free(peer);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700541 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542 }
543
544 peer->next = ibss_rsn->peers;
545 ibss_rsn->peers = peer;
546
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700547 return peer;
548}
549
550
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700551static void ibss_rsn_auth_timeout(void *eloop_ctx, void *timeout_ctx)
552{
553 struct ibss_rsn_peer *peer = eloop_ctx;
554
555 /*
556 * Assume peer does not support Authentication exchange or the frame was
557 * lost somewhere - start EAPOL Authenticator.
558 */
559 wpa_printf(MSG_DEBUG,
560 "RSN: Timeout on waiting Authentication frame response from "
561 MACSTR " - start authenticator", MAC2STR(peer->addr));
562
563 peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
564 ibss_rsn_auth_init(peer->ibss_rsn, peer);
565}
566
567
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700568int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
569{
570 struct ibss_rsn_peer *peer;
571 int res;
572
573 /* if the peer already exists, exit immediately */
574 peer = ibss_rsn_get_peer(ibss_rsn, addr);
575 if (peer)
576 return 0;
577
578 peer = ibss_rsn_peer_init(ibss_rsn, addr);
579 if (peer == NULL)
580 return -1;
581
582 /* Open Authentication: send first Authentication frame */
583 res = ibss_rsn_send_auth(ibss_rsn, addr, 1);
584 if (res) {
585 /*
586 * The driver may not support Authentication frame exchange in
587 * IBSS. Ignore authentication and go through EAPOL exchange.
588 */
589 peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
590 return ibss_rsn_auth_init(ibss_rsn, peer);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700591 } else {
592 os_get_time(&peer->own_auth_tx);
593 eloop_register_timeout(1, 0, ibss_rsn_auth_timeout, peer, NULL);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700594 }
595
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596 return 0;
597}
598
599
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700600static int ibss_rsn_peer_authenticated(struct ibss_rsn *ibss_rsn,
601 struct ibss_rsn_peer *peer, int reason)
602{
603 int already_started;
604
605 if (ibss_rsn == NULL || peer == NULL)
606 return -1;
607
608 already_started = ibss_rsn_is_auth_started(peer);
609 peer->authentication_status |= reason;
610
611 if (already_started) {
612 wpa_printf(MSG_DEBUG, "RSN: IBSS Authenticator already "
613 "started for peer " MACSTR, MAC2STR(peer->addr));
614 return 0;
615 }
616
617 wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator "
618 "for now-authenticated peer " MACSTR, MAC2STR(peer->addr));
619
620 return ibss_rsn_auth_init(ibss_rsn, peer);
621}
622
623
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700624void ibss_rsn_stop(struct ibss_rsn *ibss_rsn, const u8 *peermac)
625{
626 struct ibss_rsn_peer *peer, *prev;
627
628 if (ibss_rsn == NULL)
629 return;
630
631 if (peermac == NULL) {
632 /* remove all peers */
633 wpa_printf(MSG_DEBUG, "%s: Remove all peers", __func__);
634 peer = ibss_rsn->peers;
635 while (peer) {
636 prev = peer;
637 peer = peer->next;
638 ibss_rsn_free(prev);
639 ibss_rsn->peers = peer;
640 }
641 } else {
642 /* remove specific peer */
643 wpa_printf(MSG_DEBUG, "%s: Remove specific peer " MACSTR,
644 __func__, MAC2STR(peermac));
645
646 for (prev = NULL, peer = ibss_rsn->peers; peer != NULL;
647 prev = peer, peer = peer->next) {
648 if (os_memcmp(peermac, peer->addr, ETH_ALEN) == 0) {
649 if (prev == NULL)
650 ibss_rsn->peers = peer->next;
651 else
652 prev->next = peer->next;
653 ibss_rsn_free(peer);
654 wpa_printf(MSG_DEBUG, "%s: Successfully "
655 "removed a specific peer",
656 __func__);
657 break;
658 }
659 }
660 }
661}
662
663
664struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s)
665{
666 struct ibss_rsn *ibss_rsn;
667
668 ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
669 if (ibss_rsn == NULL)
670 return NULL;
671 ibss_rsn->wpa_s = wpa_s;
672
673 if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr) < 0) {
674 ibss_rsn_deinit(ibss_rsn);
675 return NULL;
676 }
677
678 return ibss_rsn;
679}
680
681
682void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
683{
684 struct ibss_rsn_peer *peer, *prev;
685
686 if (ibss_rsn == NULL)
687 return;
688
689 peer = ibss_rsn->peers;
690 while (peer) {
691 prev = peer;
692 peer = peer->next;
693 ibss_rsn_free(prev);
694 }
695
696 wpa_deinit(ibss_rsn->auth_group);
697 os_free(ibss_rsn);
698
699}
700
701
702static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
703{
704 const struct ieee802_1x_hdr *hdr;
705 const struct wpa_eapol_key *key;
706 u16 key_info;
707 size_t plen;
708
709 /* TODO: Support other EAPOL packets than just EAPOL-Key */
710
711 if (len < sizeof(*hdr) + sizeof(*key))
712 return -1;
713
714 hdr = (const struct ieee802_1x_hdr *) buf;
715 key = (const struct wpa_eapol_key *) (hdr + 1);
716 plen = be_to_host16(hdr->length);
717
718 if (hdr->version < EAPOL_VERSION) {
719 /* TODO: backwards compatibility */
720 }
721 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
722 wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
723 "not a Key frame", hdr->type);
724 return -1;
725 }
726 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
727 wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
728 "invalid (frame size %lu)",
729 (unsigned long) plen, (unsigned long) len);
730 return -1;
731 }
732
733 if (key->type != EAPOL_KEY_TYPE_RSN) {
734 wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
735 "discarded", key->type);
736 return -1;
737 }
738
739 key_info = WPA_GET_BE16(key->key_info);
740
741 return !!(key_info & WPA_KEY_INFO_ACK);
742}
743
744
745static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
746 struct ibss_rsn_peer *peer,
747 const u8 *buf, size_t len)
748{
749 int supp;
750 u8 *tmp;
751
752 supp = ibss_rsn_eapol_dst_supp(buf, len);
753 if (supp < 0)
754 return -1;
755
756 tmp = os_malloc(len);
757 if (tmp == NULL)
758 return -1;
759 os_memcpy(tmp, buf, len);
760 if (supp) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700761 peer->authentication_status |= IBSS_RSN_AUTH_EAPOL_BY_PEER;
762 wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant from "
763 MACSTR, MAC2STR(peer->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764 wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
765 } else {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700766 if (ibss_rsn_is_auth_started(peer) == 0) {
767 wpa_printf(MSG_DEBUG, "RSN: IBSS EAPOL for "
768 "Authenticator dropped as " MACSTR " is not "
769 "authenticated", MAC2STR(peer->addr));
770 os_free(tmp);
771 return -1;
772 }
773
774 wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator "
775 "from "MACSTR, MAC2STR(peer->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776 wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
777 }
778 os_free(tmp);
779
780 return 1;
781}
782
783
784int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
785 const u8 *buf, size_t len)
786{
787 struct ibss_rsn_peer *peer;
788
789 if (ibss_rsn == NULL)
790 return -1;
791
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800792 peer = ibss_rsn_get_peer(ibss_rsn, src_addr);
793 if (peer)
794 return ibss_rsn_process_rx_eapol(ibss_rsn, peer, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795
796 if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
797 /*
798 * Create new IBSS peer based on an EAPOL message from the peer
799 * Authenticator.
800 */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700801 peer = ibss_rsn_peer_init(ibss_rsn, src_addr);
802 if (peer == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803 return -1;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700804
805 /* assume the peer is authenticated already */
806 wpa_printf(MSG_DEBUG, "RSN: IBSS Not using IBSS Auth for peer "
807 MACSTR, MAC2STR(src_addr));
808 ibss_rsn_peer_authenticated(ibss_rsn, peer,
809 IBSS_RSN_AUTH_EAPOL_BY_US);
810
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811 return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
812 buf, len);
813 }
814
815 return 0;
816}
817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700818void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
819{
820 if (ibss_rsn == NULL)
821 return;
822 os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
823}
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700824
825
826static void ibss_rsn_handle_auth_1_of_2(struct ibss_rsn *ibss_rsn,
827 struct ibss_rsn_peer *peer,
828 const u8* addr)
829{
830 wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 1) from " MACSTR,
831 MAC2STR(addr));
832
833 if (peer &&
834 peer->authentication_status & IBSS_RSN_AUTH_EAPOL_BY_PEER) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700835 if (peer->own_auth_tx.sec) {
836 struct os_time now, diff;
837 os_get_time(&now);
838 os_time_sub(&now, &peer->own_auth_tx, &diff);
839 if (diff.sec == 0 && diff.usec < 500000) {
840 wpa_printf(MSG_DEBUG, "RSN: Skip IBSS reinit since only %u usec from own Auth frame TX",
841 (int) diff.usec);
842 goto skip_reinit;
843 }
844 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700845 /*
846 * A peer sent us an Authentication frame even though it already
847 * started an EAPOL session. We should reinit state machines
848 * here, but it's much more complicated than just deleting and
849 * recreating the state machine
850 */
851 wpa_printf(MSG_DEBUG, "RSN: IBSS Reinitializing station "
852 MACSTR, MAC2STR(addr));
853
854 ibss_rsn_stop(ibss_rsn, addr);
855 peer = NULL;
856 }
857
858 if (!peer) {
859 peer = ibss_rsn_peer_init(ibss_rsn, addr);
860 if (!peer)
861 return;
862
863 wpa_printf(MSG_DEBUG, "RSN: IBSS Auth started by peer " MACSTR,
864 MAC2STR(addr));
865 }
866
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700867skip_reinit:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700868 /* reply with an Authentication frame now, before sending an EAPOL */
869 ibss_rsn_send_auth(ibss_rsn, addr, 2);
870 /* no need to start another AUTH challenge in the other way.. */
871 ibss_rsn_peer_authenticated(ibss_rsn, peer, IBSS_RSN_AUTH_EAPOL_BY_US);
872}
873
874
875void ibss_rsn_handle_auth(struct ibss_rsn *ibss_rsn, const u8 *auth_frame,
876 size_t len)
877{
878 const struct ieee80211_mgmt *header;
879 struct ibss_rsn_peer *peer;
880 size_t auth_length;
881
882 header = (const struct ieee80211_mgmt *) auth_frame;
883 auth_length = IEEE80211_HDRLEN + sizeof(header->u.auth);
884
885 if (ibss_rsn == NULL || len < auth_length)
886 return;
887
888 if (le_to_host16(header->u.auth.auth_alg) != WLAN_AUTH_OPEN ||
889 le_to_host16(header->u.auth.status_code) != WLAN_STATUS_SUCCESS)
890 return;
891
892 peer = ibss_rsn_get_peer(ibss_rsn, header->sa);
893
894 switch (le_to_host16(header->u.auth.auth_transaction)) {
895 case 1:
896 ibss_rsn_handle_auth_1_of_2(ibss_rsn, peer, header->sa);
897 break;
898 case 2:
899 wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 2) from "
900 MACSTR, MAC2STR(header->sa));
901 if (!peer) {
902 wpa_printf(MSG_DEBUG, "RSN: Received Auth seq 2 from "
903 "unknown STA " MACSTR, MAC2STR(header->sa));
904 break;
905 }
906
907 /* authentication has been completed */
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700908 eloop_cancel_timeout(ibss_rsn_auth_timeout, peer, NULL);
909 wpa_printf(MSG_DEBUG, "RSN: IBSS Auth completed with " MACSTR,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700910 MAC2STR(header->sa));
911 ibss_rsn_peer_authenticated(ibss_rsn, peer,
912 IBSS_RSN_AUTH_BY_US);
913 break;
914 }
915}