blob: 36c0aff17374b196c888cec9f9577034bc01d68a [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
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080075 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070076}
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 */
Hai Shalomc3565922019-10-28 11:58:20 -0700114 wpa_sm_set_ap_rsnxe(peer->supp, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700115 return wpa_sm_set_ap_rsn_ie(peer->supp,
116 (u8 *) "\x30\x14\x01\x00"
117 "\x00\x0f\xac\x04"
118 "\x01\x00\x00\x0f\xac\x04"
119 "\x01\x00\x00\x0f\xac\x02"
120 "\x00\x00", 22);
121}
122
123
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700124static void ibss_check_rsn_completed(struct ibss_rsn_peer *peer)
125{
126 struct wpa_supplicant *wpa_s = peer->ibss_rsn->wpa_s;
127
128 if ((peer->authentication_status &
129 (IBSS_RSN_SET_PTK_SUPP | IBSS_RSN_SET_PTK_AUTH)) !=
130 (IBSS_RSN_SET_PTK_SUPP | IBSS_RSN_SET_PTK_AUTH))
131 return;
132 if (peer->authentication_status & IBSS_RSN_REPORTED_PTK)
133 return;
134 peer->authentication_status |= IBSS_RSN_REPORTED_PTK;
135 wpa_msg(wpa_s, MSG_INFO, IBSS_RSN_COMPLETED MACSTR,
136 MAC2STR(peer->addr));
137}
138
139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140static int supp_set_key(void *ctx, enum wpa_alg alg,
141 const u8 *addr, int key_idx, int set_tx,
142 const u8 *seq, size_t seq_len,
143 const u8 *key, size_t key_len)
144{
145 struct ibss_rsn_peer *peer = ctx;
146
147 wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
148 "set_tx=%d)",
149 __func__, alg, MAC2STR(addr), key_idx, set_tx);
150 wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
151 wpa_hexdump_key(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
152
153 if (key_idx == 0) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700154 peer->authentication_status |= IBSS_RSN_SET_PTK_SUPP;
155 ibss_check_rsn_completed(peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156 /*
157 * In IBSS RSN, the pairwise key from the 4-way handshake
158 * initiated by the peer with highest MAC address is used.
159 */
160 if (os_memcmp(peer->ibss_rsn->wpa_s->own_addr, peer->addr,
161 ETH_ALEN) > 0) {
162 wpa_printf(MSG_DEBUG, "SUPP: Do not use this PTK");
163 return 0;
164 }
165 }
166
167 if (is_broadcast_ether_addr(addr))
168 addr = peer->addr;
169 return wpa_drv_set_key(peer->ibss_rsn->wpa_s, alg, addr, key_idx,
170 set_tx, seq, seq_len, key, key_len);
171}
172
173
174static void * supp_get_network_ctx(void *ctx)
175{
176 struct ibss_rsn_peer *peer = ctx;
177 return wpa_supplicant_get_ssid(peer->ibss_rsn->wpa_s);
178}
179
180
181static int supp_mlme_setprotection(void *ctx, const u8 *addr,
182 int protection_type, int key_type)
183{
184 wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
185 "key_type=%d)",
186 __func__, MAC2STR(addr), protection_type, key_type);
187 return 0;
188}
189
190
191static void supp_cancel_auth_timeout(void *ctx)
192{
193 wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
194}
195
196
Hai Shalom81f62d82019-07-22 12:10:00 -0700197static void supp_deauthenticate(void * ctx, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198{
199 wpa_printf(MSG_DEBUG, "SUPP: %s (TODO)", __func__);
200}
201
202
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800203static int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
204 const u8 *psk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205{
206 struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
207 if (ctx == NULL)
208 return -1;
209
210 ctx->ctx = peer;
211 ctx->msg_ctx = peer->ibss_rsn->wpa_s;
212 ctx->set_state = supp_set_state;
213 ctx->get_state = supp_get_state;
214 ctx->ether_send = supp_ether_send;
215 ctx->get_beacon_ie = supp_get_beacon_ie;
216 ctx->alloc_eapol = supp_alloc_eapol;
217 ctx->set_key = supp_set_key;
218 ctx->get_network_ctx = supp_get_network_ctx;
219 ctx->mlme_setprotection = supp_mlme_setprotection;
220 ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
221 ctx->deauthenticate = supp_deauthenticate;
222 peer->supp = wpa_sm_init(ctx);
223 if (peer->supp == NULL) {
224 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700225 os_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 return -1;
227 }
228
229 wpa_sm_set_own_addr(peer->supp, own_addr);
230 wpa_sm_set_param(peer->supp, WPA_PARAM_RSN_ENABLED, 1);
231 wpa_sm_set_param(peer->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
232 wpa_sm_set_param(peer->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
233 wpa_sm_set_param(peer->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
234 wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800235 wpa_sm_set_pmk(peer->supp, psk, PMK_LEN, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236
237 peer->supp_ie_len = sizeof(peer->supp_ie);
238 if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
239 &peer->supp_ie_len) < 0) {
240 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
241 " failed");
242 return -1;
243 }
244
245 wpa_sm_notify_assoc(peer->supp, peer->addr);
246
247 return 0;
248}
249
250
251static void auth_logger(void *ctx, const u8 *addr, logger_level level,
252 const char *txt)
253{
254 if (addr)
255 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
256 MAC2STR(addr), txt);
257 else
258 wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
259}
260
261
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700262static const u8 * auth_get_psk(void *ctx, const u8 *addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700263 const u8 *p2p_dev_addr, const u8 *prev_psk,
Hai Shalom021b0b52019-04-10 11:17:58 -0700264 size_t *psk_len, int *vlan_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265{
266 struct ibss_rsn *ibss_rsn = ctx;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700267
268 if (psk_len)
269 *psk_len = PMK_LEN;
Hai Shalom021b0b52019-04-10 11:17:58 -0700270 if (vlan_id)
271 *vlan_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272 wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
273 __func__, MAC2STR(addr), prev_psk);
274 if (prev_psk)
275 return NULL;
276 return ibss_rsn->psk;
277}
278
279
280static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
281 size_t data_len, int encrypt)
282{
283 struct ibss_rsn *ibss_rsn = ctx;
284 struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
285
286 wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
287 "encrypt=%d)",
288 __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
289
290 if (wpa_s->l2)
291 return l2_packet_send(wpa_s->l2, addr, ETH_P_EAPOL, data,
292 data_len);
293
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800294 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295}
296
297
298static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
299 const u8 *addr, int idx, u8 *key, size_t key_len)
300{
301 struct ibss_rsn *ibss_rsn = ctx;
302 u8 seq[6];
303
304 os_memset(seq, 0, sizeof(seq));
305
306 if (addr) {
307 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
308 " key_idx=%d)",
309 __func__, alg, MAC2STR(addr), idx);
310 } else {
311 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
312 __func__, alg, idx);
313 }
314 wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
315
316 if (idx == 0) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700317 if (addr) {
318 struct ibss_rsn_peer *peer;
319 peer = ibss_rsn_get_peer(ibss_rsn, addr);
320 if (peer) {
321 peer->authentication_status |=
322 IBSS_RSN_SET_PTK_AUTH;
323 ibss_check_rsn_completed(peer);
324 }
325 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326 /*
327 * In IBSS RSN, the pairwise key from the 4-way handshake
328 * initiated by the peer with highest MAC address is used.
329 */
330 if (addr == NULL ||
331 os_memcmp(ibss_rsn->wpa_s->own_addr, addr, ETH_ALEN) < 0) {
332 wpa_printf(MSG_DEBUG, "AUTH: Do not use this PTK");
333 return 0;
334 }
335 }
336
337 return wpa_drv_set_key(ibss_rsn->wpa_s, alg, addr, idx,
338 1, seq, 6, key, key_len);
339}
340
341
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700342static void ibss_rsn_disconnect(void *ctx, const u8 *addr, u16 reason)
343{
344 struct ibss_rsn *ibss_rsn = ctx;
345 wpa_drv_sta_deauth(ibss_rsn->wpa_s, addr, reason);
346}
347
348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349static int auth_for_each_sta(void *ctx, int (*cb)(struct wpa_state_machine *sm,
350 void *ctx),
351 void *cb_ctx)
352{
353 struct ibss_rsn *ibss_rsn = ctx;
354 struct ibss_rsn_peer *peer;
355
356 wpa_printf(MSG_DEBUG, "AUTH: for_each_sta");
357
358 for (peer = ibss_rsn->peers; peer; peer = peer->next) {
359 if (peer->auth && cb(peer->auth, cb_ctx))
360 return 1;
361 }
362
363 return 0;
364}
365
366
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800367static void ibss_set_sta_authorized(struct ibss_rsn *ibss_rsn,
368 struct ibss_rsn_peer *peer, int authorized)
369{
370 int res;
371
372 if (authorized) {
373 res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
374 WPA_STA_AUTHORIZED,
375 WPA_STA_AUTHORIZED, ~0);
376 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " authorizing port",
377 MAC2STR(peer->addr));
378 } else {
379 res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
380 0, 0, ~WPA_STA_AUTHORIZED);
381 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " unauthorizing port",
382 MAC2STR(peer->addr));
383 }
384
385 if (res && errno != ENOENT) {
386 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR " flags "
387 "for kernel driver (errno=%d)",
388 MAC2STR(peer->addr), errno);
389 }
390}
391
392
393static void auth_set_eapol(void *ctx, const u8 *addr,
394 wpa_eapol_variable var, int value)
395{
396 struct ibss_rsn *ibss_rsn = ctx;
397 struct ibss_rsn_peer *peer = ibss_rsn_get_peer(ibss_rsn, addr);
398
399 if (peer == NULL)
400 return;
401
402 switch (var) {
403 case WPA_EAPOL_authorized:
404 ibss_set_sta_authorized(ibss_rsn, peer, value);
405 break;
406 default:
407 /* do not handle any other event */
408 wpa_printf(MSG_DEBUG, "AUTH: eapol event not handled %d", var);
409 break;
410 }
411}
412
413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700415 const u8 *own_addr, struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416{
417 struct wpa_auth_config conf;
Paul Stewart092955c2017-02-06 09:13:09 -0800418 static const struct wpa_auth_callbacks cb = {
419 .logger = auth_logger,
420 .set_eapol = auth_set_eapol,
421 .send_eapol = auth_send_eapol,
422 .get_psk = auth_get_psk,
423 .set_key = auth_set_key,
424 .for_each_sta = auth_for_each_sta,
425 .disconnect = ibss_rsn_disconnect,
426 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427
428 wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
429
430 os_memset(&conf, 0, sizeof(conf));
431 conf.wpa = 2;
432 conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
433 conf.wpa_pairwise = WPA_CIPHER_CCMP;
434 conf.rsn_pairwise = WPA_CIPHER_CCMP;
435 conf.wpa_group = WPA_CIPHER_CCMP;
436 conf.eapol_version = 2;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700437 conf.wpa_group_rekey = ssid->group_rekey ? ssid->group_rekey : 600;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800438 conf.wpa_group_update_count = 4;
439 conf.wpa_pairwise_update_count = 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440
Paul Stewart092955c2017-02-06 09:13:09 -0800441 ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb, ibss_rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442 if (ibss_rsn->auth_group == NULL) {
443 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
444 return -1;
445 }
446
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800447 wpa_init_keys(ibss_rsn->auth_group);
448
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449 return 0;
450}
451
452
453static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
454 struct ibss_rsn_peer *peer)
455{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700456 peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 if (peer->auth == NULL) {
458 wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
459 return -1;
460 }
461
462 /* TODO: get peer RSN IE with Probe Request */
Hai Shalom021b0b52019-04-10 11:17:58 -0700463 if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 (u8 *) "\x30\x14\x01\x00"
465 "\x00\x0f\xac\x04"
466 "\x01\x00\x00\x0f\xac\x04"
467 "\x01\x00\x00\x0f\xac\x02"
Hai Shalomc3565922019-10-28 11:58:20 -0700468 "\x00\x00", 22, NULL, 0, NULL, 0, NULL, 0) !=
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700469 WPA_IE_OK) {
470 wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
471 return -1;
472 }
473
474 if (wpa_auth_sm_event(peer->auth, WPA_ASSOC))
475 return -1;
476
477 if (wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth))
478 return -1;
479
480 return 0;
481}
482
483
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700484static int ibss_rsn_send_auth(struct ibss_rsn *ibss_rsn, const u8 *da, int seq)
485{
486 struct ieee80211_mgmt auth;
487 const size_t auth_length = IEEE80211_HDRLEN + sizeof(auth.u.auth);
488 struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
489
490 if (wpa_s->driver->send_frame == NULL)
491 return -1;
492
493 os_memset(&auth, 0, sizeof(auth));
494
495 auth.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
496 WLAN_FC_STYPE_AUTH);
497 os_memcpy(auth.da, da, ETH_ALEN);
498 os_memcpy(auth.sa, wpa_s->own_addr, ETH_ALEN);
499 os_memcpy(auth.bssid, wpa_s->bssid, ETH_ALEN);
500
501 auth.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
502 auth.u.auth.auth_transaction = host_to_le16(seq);
503 auth.u.auth.status_code = host_to_le16(WLAN_STATUS_SUCCESS);
504
505 wpa_printf(MSG_DEBUG, "RSN: IBSS TX Auth frame (SEQ %d) to " MACSTR,
506 seq, MAC2STR(da));
507
508 return wpa_s->driver->send_frame(wpa_s->drv_priv, (u8 *) &auth,
509 auth_length, 0);
510}
511
512
513static int ibss_rsn_is_auth_started(struct ibss_rsn_peer * peer)
514{
515 return peer->authentication_status &
516 (IBSS_RSN_AUTH_BY_US | IBSS_RSN_AUTH_EAPOL_BY_US);
517}
518
519
520static struct ibss_rsn_peer *
521ibss_rsn_peer_init(struct ibss_rsn *ibss_rsn, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700522{
523 struct ibss_rsn_peer *peer;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524 if (ibss_rsn == NULL)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700525 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700526
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700527 peer = ibss_rsn_get_peer(ibss_rsn, addr);
528 if (peer) {
529 wpa_printf(MSG_DEBUG, "RSN: IBSS Supplicant for peer "MACSTR
530 " already running", MAC2STR(addr));
531 return peer;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 }
533
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700534 wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Supplicant for peer "MACSTR,
535 MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536
537 peer = os_zalloc(sizeof(*peer));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700538 if (peer == NULL) {
539 wpa_printf(MSG_DEBUG, "RSN: Could not allocate memory.");
540 return NULL;
541 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542
543 peer->ibss_rsn = ibss_rsn;
544 os_memcpy(peer->addr, addr, ETH_ALEN);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700545 peer->authentication_status = IBSS_RSN_AUTH_NOT_AUTHENTICATED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700547 if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr,
548 ibss_rsn->psk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 ibss_rsn_free(peer);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700550 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700551 }
552
553 peer->next = ibss_rsn->peers;
554 ibss_rsn->peers = peer;
555
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700556 return peer;
557}
558
559
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700560static void ibss_rsn_auth_timeout(void *eloop_ctx, void *timeout_ctx)
561{
562 struct ibss_rsn_peer *peer = eloop_ctx;
563
564 /*
565 * Assume peer does not support Authentication exchange or the frame was
566 * lost somewhere - start EAPOL Authenticator.
567 */
568 wpa_printf(MSG_DEBUG,
569 "RSN: Timeout on waiting Authentication frame response from "
570 MACSTR " - start authenticator", MAC2STR(peer->addr));
571
572 peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
573 ibss_rsn_auth_init(peer->ibss_rsn, peer);
574}
575
576
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700577int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
578{
579 struct ibss_rsn_peer *peer;
580 int res;
581
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -0700582 if (!ibss_rsn)
583 return -1;
584
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700585 /* if the peer already exists, exit immediately */
586 peer = ibss_rsn_get_peer(ibss_rsn, addr);
587 if (peer)
588 return 0;
589
590 peer = ibss_rsn_peer_init(ibss_rsn, addr);
591 if (peer == NULL)
592 return -1;
593
594 /* Open Authentication: send first Authentication frame */
595 res = ibss_rsn_send_auth(ibss_rsn, addr, 1);
596 if (res) {
597 /*
598 * The driver may not support Authentication frame exchange in
599 * IBSS. Ignore authentication and go through EAPOL exchange.
600 */
601 peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
602 return ibss_rsn_auth_init(ibss_rsn, peer);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700603 } else {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800604 os_get_reltime(&peer->own_auth_tx);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700605 eloop_register_timeout(1, 0, ibss_rsn_auth_timeout, peer, NULL);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700606 }
607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608 return 0;
609}
610
611
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700612static int ibss_rsn_peer_authenticated(struct ibss_rsn *ibss_rsn,
613 struct ibss_rsn_peer *peer, int reason)
614{
615 int already_started;
616
617 if (ibss_rsn == NULL || peer == NULL)
618 return -1;
619
620 already_started = ibss_rsn_is_auth_started(peer);
621 peer->authentication_status |= reason;
622
623 if (already_started) {
624 wpa_printf(MSG_DEBUG, "RSN: IBSS Authenticator already "
625 "started for peer " MACSTR, MAC2STR(peer->addr));
626 return 0;
627 }
628
629 wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator "
630 "for now-authenticated peer " MACSTR, MAC2STR(peer->addr));
631
632 return ibss_rsn_auth_init(ibss_rsn, peer);
633}
634
635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636void ibss_rsn_stop(struct ibss_rsn *ibss_rsn, const u8 *peermac)
637{
638 struct ibss_rsn_peer *peer, *prev;
639
640 if (ibss_rsn == NULL)
641 return;
642
643 if (peermac == NULL) {
644 /* remove all peers */
645 wpa_printf(MSG_DEBUG, "%s: Remove all peers", __func__);
646 peer = ibss_rsn->peers;
647 while (peer) {
648 prev = peer;
649 peer = peer->next;
650 ibss_rsn_free(prev);
651 ibss_rsn->peers = peer;
652 }
653 } else {
654 /* remove specific peer */
655 wpa_printf(MSG_DEBUG, "%s: Remove specific peer " MACSTR,
656 __func__, MAC2STR(peermac));
657
658 for (prev = NULL, peer = ibss_rsn->peers; peer != NULL;
659 prev = peer, peer = peer->next) {
660 if (os_memcmp(peermac, peer->addr, ETH_ALEN) == 0) {
661 if (prev == NULL)
662 ibss_rsn->peers = peer->next;
663 else
664 prev->next = peer->next;
665 ibss_rsn_free(peer);
666 wpa_printf(MSG_DEBUG, "%s: Successfully "
667 "removed a specific peer",
668 __func__);
669 break;
670 }
671 }
672 }
673}
674
675
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700676struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s,
677 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678{
679 struct ibss_rsn *ibss_rsn;
680
681 ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
682 if (ibss_rsn == NULL)
683 return NULL;
684 ibss_rsn->wpa_s = wpa_s;
685
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700686 if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr, ssid) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 ibss_rsn_deinit(ibss_rsn);
688 return NULL;
689 }
690
691 return ibss_rsn;
692}
693
694
695void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
696{
697 struct ibss_rsn_peer *peer, *prev;
698
699 if (ibss_rsn == NULL)
700 return;
701
702 peer = ibss_rsn->peers;
703 while (peer) {
704 prev = peer;
705 peer = peer->next;
706 ibss_rsn_free(prev);
707 }
708
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800709 if (ibss_rsn->auth_group)
710 wpa_deinit(ibss_rsn->auth_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 os_free(ibss_rsn);
712
713}
714
715
716static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
717{
718 const struct ieee802_1x_hdr *hdr;
719 const struct wpa_eapol_key *key;
720 u16 key_info;
721 size_t plen;
722
723 /* TODO: Support other EAPOL packets than just EAPOL-Key */
724
725 if (len < sizeof(*hdr) + sizeof(*key))
726 return -1;
727
728 hdr = (const struct ieee802_1x_hdr *) buf;
729 key = (const struct wpa_eapol_key *) (hdr + 1);
730 plen = be_to_host16(hdr->length);
731
732 if (hdr->version < EAPOL_VERSION) {
733 /* TODO: backwards compatibility */
734 }
735 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
736 wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
737 "not a Key frame", hdr->type);
738 return -1;
739 }
740 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
741 wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
742 "invalid (frame size %lu)",
743 (unsigned long) plen, (unsigned long) len);
744 return -1;
745 }
746
747 if (key->type != EAPOL_KEY_TYPE_RSN) {
748 wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
749 "discarded", key->type);
750 return -1;
751 }
752
753 key_info = WPA_GET_BE16(key->key_info);
754
755 return !!(key_info & WPA_KEY_INFO_ACK);
756}
757
758
759static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
760 struct ibss_rsn_peer *peer,
761 const u8 *buf, size_t len)
762{
763 int supp;
764 u8 *tmp;
765
766 supp = ibss_rsn_eapol_dst_supp(buf, len);
767 if (supp < 0)
768 return -1;
769
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700770 tmp = os_memdup(buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771 if (tmp == NULL)
772 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773 if (supp) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700774 peer->authentication_status |= IBSS_RSN_AUTH_EAPOL_BY_PEER;
775 wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant from "
776 MACSTR, MAC2STR(peer->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777 wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
778 } else {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700779 if (ibss_rsn_is_auth_started(peer) == 0) {
780 wpa_printf(MSG_DEBUG, "RSN: IBSS EAPOL for "
781 "Authenticator dropped as " MACSTR " is not "
782 "authenticated", MAC2STR(peer->addr));
783 os_free(tmp);
784 return -1;
785 }
786
787 wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator "
788 "from "MACSTR, MAC2STR(peer->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700789 wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
790 }
791 os_free(tmp);
792
793 return 1;
794}
795
796
797int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
798 const u8 *buf, size_t len)
799{
800 struct ibss_rsn_peer *peer;
801
802 if (ibss_rsn == NULL)
803 return -1;
804
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800805 peer = ibss_rsn_get_peer(ibss_rsn, src_addr);
806 if (peer)
807 return ibss_rsn_process_rx_eapol(ibss_rsn, peer, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808
809 if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
810 /*
811 * Create new IBSS peer based on an EAPOL message from the peer
812 * Authenticator.
813 */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700814 peer = ibss_rsn_peer_init(ibss_rsn, src_addr);
815 if (peer == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700816 return -1;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700817
818 /* assume the peer is authenticated already */
819 wpa_printf(MSG_DEBUG, "RSN: IBSS Not using IBSS Auth for peer "
820 MACSTR, MAC2STR(src_addr));
821 ibss_rsn_peer_authenticated(ibss_rsn, peer,
822 IBSS_RSN_AUTH_EAPOL_BY_US);
823
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824 return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
825 buf, len);
826 }
827
828 return 0;
829}
830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
832{
833 if (ibss_rsn == NULL)
834 return;
835 os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
836}
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700837
838
839static void ibss_rsn_handle_auth_1_of_2(struct ibss_rsn *ibss_rsn,
840 struct ibss_rsn_peer *peer,
841 const u8* addr)
842{
843 wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 1) from " MACSTR,
844 MAC2STR(addr));
845
846 if (peer &&
Paul Stewart092955c2017-02-06 09:13:09 -0800847 peer->authentication_status & (IBSS_RSN_SET_PTK_SUPP |
848 IBSS_RSN_SET_PTK_AUTH)) {
849 /* Clear the TK for this pair to allow recovery from the case
850 * where the peer STA has restarted and lost its key while we
851 * still have a pairwise key configured. */
852 wpa_printf(MSG_DEBUG, "RSN: Clear pairwise key for peer "
853 MACSTR, MAC2STR(addr));
854 wpa_drv_set_key(ibss_rsn->wpa_s, WPA_ALG_NONE, addr, 0, 0,
855 NULL, 0, NULL, 0);
856 }
857
858 if (peer &&
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700859 peer->authentication_status & IBSS_RSN_AUTH_EAPOL_BY_PEER) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700860 if (peer->own_auth_tx.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800861 struct os_reltime now, diff;
862 os_get_reltime(&now);
863 os_reltime_sub(&now, &peer->own_auth_tx, &diff);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700864 if (diff.sec == 0 && diff.usec < 500000) {
865 wpa_printf(MSG_DEBUG, "RSN: Skip IBSS reinit since only %u usec from own Auth frame TX",
866 (int) diff.usec);
867 goto skip_reinit;
868 }
869 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700870 /*
871 * A peer sent us an Authentication frame even though it already
872 * started an EAPOL session. We should reinit state machines
873 * here, but it's much more complicated than just deleting and
874 * recreating the state machine
875 */
876 wpa_printf(MSG_DEBUG, "RSN: IBSS Reinitializing station "
877 MACSTR, MAC2STR(addr));
878
879 ibss_rsn_stop(ibss_rsn, addr);
880 peer = NULL;
881 }
882
883 if (!peer) {
884 peer = ibss_rsn_peer_init(ibss_rsn, addr);
885 if (!peer)
886 return;
887
888 wpa_printf(MSG_DEBUG, "RSN: IBSS Auth started by peer " MACSTR,
889 MAC2STR(addr));
890 }
891
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700892skip_reinit:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700893 /* reply with an Authentication frame now, before sending an EAPOL */
894 ibss_rsn_send_auth(ibss_rsn, addr, 2);
895 /* no need to start another AUTH challenge in the other way.. */
896 ibss_rsn_peer_authenticated(ibss_rsn, peer, IBSS_RSN_AUTH_EAPOL_BY_US);
897}
898
899
900void ibss_rsn_handle_auth(struct ibss_rsn *ibss_rsn, const u8 *auth_frame,
901 size_t len)
902{
903 const struct ieee80211_mgmt *header;
904 struct ibss_rsn_peer *peer;
905 size_t auth_length;
906
907 header = (const struct ieee80211_mgmt *) auth_frame;
908 auth_length = IEEE80211_HDRLEN + sizeof(header->u.auth);
909
910 if (ibss_rsn == NULL || len < auth_length)
911 return;
912
913 if (le_to_host16(header->u.auth.auth_alg) != WLAN_AUTH_OPEN ||
914 le_to_host16(header->u.auth.status_code) != WLAN_STATUS_SUCCESS)
915 return;
916
917 peer = ibss_rsn_get_peer(ibss_rsn, header->sa);
918
919 switch (le_to_host16(header->u.auth.auth_transaction)) {
920 case 1:
921 ibss_rsn_handle_auth_1_of_2(ibss_rsn, peer, header->sa);
922 break;
923 case 2:
924 wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 2) from "
925 MACSTR, MAC2STR(header->sa));
926 if (!peer) {
927 wpa_printf(MSG_DEBUG, "RSN: Received Auth seq 2 from "
928 "unknown STA " MACSTR, MAC2STR(header->sa));
929 break;
930 }
931
932 /* authentication has been completed */
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700933 eloop_cancel_timeout(ibss_rsn_auth_timeout, peer, NULL);
934 wpa_printf(MSG_DEBUG, "RSN: IBSS Auth completed with " MACSTR,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700935 MAC2STR(header->sa));
936 ibss_rsn_peer_authenticated(ibss_rsn, peer,
937 IBSS_RSN_AUTH_BY_US);
938 break;
939 }
940}