]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/ibss_rsn.c
IBSS RSN: Do not start multiple Auth/Supp for same peer
[thirdparty/hostap.git] / wpa_supplicant / ibss_rsn.c
1 /*
2 * wpa_supplicant - IBSS RSN
3 * Copyright (c) 2009, 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 "includes.h"
16
17 #include "common.h"
18 #include "l2_packet/l2_packet.h"
19 #include "rsn_supp/wpa.h"
20 #include "rsn_supp/wpa_ie.h"
21 #include "ap/wpa_auth.h"
22 #include "wpa_supplicant_i.h"
23 #include "driver_i.h"
24 #include "ibss_rsn.h"
25
26
27 static void ibss_rsn_free(struct ibss_rsn_peer *peer)
28 {
29 wpa_auth_sta_deinit(peer->auth);
30 wpa_sm_deinit(peer->supp);
31 os_free(peer);
32 }
33
34
35 static void supp_set_state(void *ctx, enum wpa_states state)
36 {
37 struct ibss_rsn_peer *peer = ctx;
38 peer->supp_state = state;
39 }
40
41
42 static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
43 size_t len)
44 {
45 struct ibss_rsn_peer *peer = ctx;
46 struct wpa_supplicant *wpa_s = peer->ibss_rsn->wpa_s;
47
48 wpa_printf(MSG_DEBUG, "SUPP: %s(dest=" MACSTR " proto=0x%04x "
49 "len=%lu)",
50 __func__, MAC2STR(dest), proto, (unsigned long) len);
51
52 if (wpa_s->l2)
53 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
54
55 return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
56 }
57
58
59 static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
60 u16 data_len, size_t *msg_len, void **data_pos)
61 {
62 struct ieee802_1x_hdr *hdr;
63
64 wpa_printf(MSG_DEBUG, "SUPP: %s(type=%d data_len=%d)",
65 __func__, type, data_len);
66
67 *msg_len = sizeof(*hdr) + data_len;
68 hdr = os_malloc(*msg_len);
69 if (hdr == NULL)
70 return NULL;
71
72 hdr->version = 2;
73 hdr->type = type;
74 hdr->length = host_to_be16(data_len);
75
76 if (data)
77 os_memcpy(hdr + 1, data, data_len);
78 else
79 os_memset(hdr + 1, 0, data_len);
80
81 if (data_pos)
82 *data_pos = hdr + 1;
83
84 return (u8 *) hdr;
85 }
86
87
88 static int supp_get_beacon_ie(void *ctx)
89 {
90 struct ibss_rsn_peer *peer = ctx;
91
92 wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
93 /* TODO: get correct RSN IE */
94 return wpa_sm_set_ap_rsn_ie(peer->supp,
95 (u8 *) "\x30\x14\x01\x00"
96 "\x00\x0f\xac\x04"
97 "\x01\x00\x00\x0f\xac\x04"
98 "\x01\x00\x00\x0f\xac\x02"
99 "\x00\x00", 22);
100 }
101
102
103 static int supp_set_key(void *ctx, enum wpa_alg alg,
104 const u8 *addr, int key_idx, int set_tx,
105 const u8 *seq, size_t seq_len,
106 const u8 *key, size_t key_len)
107 {
108 struct ibss_rsn_peer *peer = ctx;
109
110 wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
111 "set_tx=%d)",
112 __func__, alg, MAC2STR(addr), key_idx, set_tx);
113 wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
114 wpa_hexdump_key(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
115
116 if (key_idx == 0) {
117 /*
118 * In IBSS RSN, the pairwise key from the 4-way handshake
119 * initiated by the peer with highest MAC address is used.
120 */
121 if (os_memcmp(peer->ibss_rsn->wpa_s->own_addr, peer->addr,
122 ETH_ALEN) > 0) {
123 wpa_printf(MSG_DEBUG, "SUPP: Do not use this PTK");
124 return 0;
125 }
126 }
127
128 if (is_broadcast_ether_addr(addr) && peer->addr)
129 addr = peer->addr;
130 return wpa_drv_set_key(peer->ibss_rsn->wpa_s, alg, addr, key_idx,
131 set_tx, seq, seq_len, key, key_len);
132 }
133
134
135 static void * supp_get_network_ctx(void *ctx)
136 {
137 struct ibss_rsn_peer *peer = ctx;
138 return wpa_supplicant_get_ssid(peer->ibss_rsn->wpa_s);
139 }
140
141
142 static int supp_mlme_setprotection(void *ctx, const u8 *addr,
143 int protection_type, int key_type)
144 {
145 wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
146 "key_type=%d)",
147 __func__, MAC2STR(addr), protection_type, key_type);
148 return 0;
149 }
150
151
152 static void supp_cancel_auth_timeout(void *ctx)
153 {
154 wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
155 }
156
157
158 static void supp_deauthenticate(void * ctx, int reason_code)
159 {
160 wpa_printf(MSG_DEBUG, "SUPP: %s (TODO)", __func__);
161 }
162
163
164 int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
165 const u8 *psk)
166 {
167 struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
168 if (ctx == NULL)
169 return -1;
170
171 ctx->ctx = peer;
172 ctx->msg_ctx = peer->ibss_rsn->wpa_s;
173 ctx->set_state = supp_set_state;
174 ctx->ether_send = supp_ether_send;
175 ctx->get_beacon_ie = supp_get_beacon_ie;
176 ctx->alloc_eapol = supp_alloc_eapol;
177 ctx->set_key = supp_set_key;
178 ctx->get_network_ctx = supp_get_network_ctx;
179 ctx->mlme_setprotection = supp_mlme_setprotection;
180 ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
181 ctx->deauthenticate = supp_deauthenticate;
182 peer->supp = wpa_sm_init(ctx);
183 if (peer->supp == NULL) {
184 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
185 return -1;
186 }
187
188 wpa_sm_set_own_addr(peer->supp, own_addr);
189 wpa_sm_set_param(peer->supp, WPA_PARAM_RSN_ENABLED, 1);
190 wpa_sm_set_param(peer->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
191 wpa_sm_set_param(peer->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
192 wpa_sm_set_param(peer->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
193 wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
194 wpa_sm_set_pmk(peer->supp, psk, PMK_LEN);
195
196 peer->supp_ie_len = sizeof(peer->supp_ie);
197 if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
198 &peer->supp_ie_len) < 0) {
199 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
200 " failed");
201 return -1;
202 }
203
204 wpa_sm_notify_assoc(peer->supp, peer->addr);
205
206 return 0;
207 }
208
209
210 static void auth_logger(void *ctx, const u8 *addr, logger_level level,
211 const char *txt)
212 {
213 if (addr)
214 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
215 MAC2STR(addr), txt);
216 else
217 wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
218 }
219
220
221 static const u8 * auth_get_psk(void *ctx, const u8 *addr, const u8 *prev_psk)
222 {
223 struct ibss_rsn *ibss_rsn = ctx;
224 wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
225 __func__, MAC2STR(addr), prev_psk);
226 if (prev_psk)
227 return NULL;
228 return ibss_rsn->psk;
229 }
230
231
232 static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
233 size_t data_len, int encrypt)
234 {
235 struct ibss_rsn *ibss_rsn = ctx;
236 struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
237
238 wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
239 "encrypt=%d)",
240 __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
241
242 if (wpa_s->l2)
243 return l2_packet_send(wpa_s->l2, addr, ETH_P_EAPOL, data,
244 data_len);
245
246 return wpa_drv_send_eapol(wpa_s, addr, ETH_P_EAPOL, data, data_len);
247 }
248
249
250 static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
251 const u8 *addr, int idx, u8 *key, size_t key_len)
252 {
253 struct ibss_rsn *ibss_rsn = ctx;
254 u8 seq[6];
255
256 os_memset(seq, 0, sizeof(seq));
257
258 if (addr) {
259 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
260 " key_idx=%d)",
261 __func__, alg, MAC2STR(addr), idx);
262 } else {
263 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
264 __func__, alg, idx);
265 }
266 wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
267
268 if (idx == 0) {
269 /*
270 * In IBSS RSN, the pairwise key from the 4-way handshake
271 * initiated by the peer with highest MAC address is used.
272 */
273 if (addr == NULL ||
274 os_memcmp(ibss_rsn->wpa_s->own_addr, addr, ETH_ALEN) < 0) {
275 wpa_printf(MSG_DEBUG, "AUTH: Do not use this PTK");
276 return 0;
277 }
278 }
279
280 if (ibss_rsn->init_in_progress && key_len <=
281 sizeof(ibss_rsn->init_gtk)) {
282 wpa_printf(MSG_DEBUG, "AUTH: Delay setting of initial TX GTK "
283 "until RSN IBSS is connected");
284 ibss_rsn->init_gtk_idx = idx;
285 os_memcpy(ibss_rsn->init_gtk, key, key_len);
286 return 0;
287 }
288
289 return wpa_drv_set_key(ibss_rsn->wpa_s, alg, addr, idx,
290 1, seq, 6, key, key_len);
291 }
292
293
294 static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
295 const u8 *own_addr)
296 {
297 struct wpa_auth_config conf;
298 struct wpa_auth_callbacks cb;
299
300 wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
301
302 os_memset(&conf, 0, sizeof(conf));
303 conf.wpa = 2;
304 conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
305 conf.wpa_pairwise = WPA_CIPHER_CCMP;
306 conf.rsn_pairwise = WPA_CIPHER_CCMP;
307 conf.wpa_group = WPA_CIPHER_CCMP;
308 conf.eapol_version = 2;
309
310 os_memset(&cb, 0, sizeof(cb));
311 cb.ctx = ibss_rsn;
312 cb.logger = auth_logger;
313 cb.send_eapol = auth_send_eapol;
314 cb.get_psk = auth_get_psk;
315 cb.set_key = auth_set_key;
316
317 ibss_rsn->init_in_progress = 1;
318 ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb);
319 ibss_rsn->init_in_progress = 0;
320 if (ibss_rsn->auth_group == NULL) {
321 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
322 return -1;
323 }
324
325 return 0;
326 }
327
328
329 static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
330 struct ibss_rsn_peer *peer)
331 {
332 peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr);
333 if (peer->auth == NULL) {
334 wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
335 return -1;
336 }
337
338 /* TODO: get peer RSN IE with Probe Request */
339 if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth,
340 (u8 *) "\x30\x14\x01\x00"
341 "\x00\x0f\xac\x04"
342 "\x01\x00\x00\x0f\xac\x04"
343 "\x01\x00\x00\x0f\xac\x02"
344 "\x00\x00", 22, NULL, 0) !=
345 WPA_IE_OK) {
346 wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
347 return -1;
348 }
349
350 if (wpa_auth_sm_event(peer->auth, WPA_ASSOC))
351 return -1;
352
353 if (wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth))
354 return -1;
355
356 return 0;
357 }
358
359
360 int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
361 {
362 struct ibss_rsn_peer *peer;
363
364 for (peer = ibss_rsn->peers; peer; peer = peer->next) {
365 if (os_memcmp(addr, peer->addr, ETH_ALEN) == 0) {
366 wpa_printf(MSG_DEBUG, "RSN: IBSS Authenticator and "
367 "Supplicant for peer " MACSTR " already "
368 "running", MAC2STR(addr));
369 return 0;
370 }
371 }
372
373 wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator and "
374 "Supplicant for peer " MACSTR, MAC2STR(addr));
375
376 peer = os_zalloc(sizeof(*peer));
377 if (peer == NULL)
378 return -1;
379
380 peer->ibss_rsn = ibss_rsn;
381 os_memcpy(peer->addr, addr, ETH_ALEN);
382
383 if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr, ibss_rsn->psk)
384 < 0) {
385 ibss_rsn_free(peer);
386 return -1;
387 }
388
389 if (ibss_rsn_auth_init(ibss_rsn, peer) < 0) {
390 ibss_rsn_free(peer);
391 return -1;
392 }
393
394 peer->next = ibss_rsn->peers;
395 ibss_rsn->peers = peer;
396
397 return 0;
398 }
399
400
401 struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s)
402 {
403 struct ibss_rsn *ibss_rsn;
404
405 ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
406 if (ibss_rsn == NULL)
407 return NULL;
408 ibss_rsn->wpa_s = wpa_s;
409
410 if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr) < 0) {
411 ibss_rsn_deinit(ibss_rsn);
412 return NULL;
413 }
414
415 return ibss_rsn;
416 }
417
418
419 void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
420 {
421 struct ibss_rsn_peer *peer, *prev;
422
423 if (ibss_rsn == NULL)
424 return;
425
426 peer = ibss_rsn->peers;
427 while (peer) {
428 prev = peer;
429 peer = peer->next;
430 ibss_rsn_free(prev);
431 }
432
433 wpa_deinit(ibss_rsn->auth_group);
434 os_free(ibss_rsn);
435
436 }
437
438
439 static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
440 {
441 const struct ieee802_1x_hdr *hdr;
442 const struct wpa_eapol_key *key;
443 u16 key_info;
444 size_t plen;
445
446 /* TODO: Support other EAPOL packets than just EAPOL-Key */
447
448 if (len < sizeof(*hdr) + sizeof(*key))
449 return -1;
450
451 hdr = (const struct ieee802_1x_hdr *) buf;
452 key = (const struct wpa_eapol_key *) (hdr + 1);
453 plen = be_to_host16(hdr->length);
454
455 if (hdr->version < EAPOL_VERSION) {
456 /* TODO: backwards compatibility */
457 }
458 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
459 wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
460 "not a Key frame", hdr->type);
461 return -1;
462 }
463 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
464 wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
465 "invalid (frame size %lu)",
466 (unsigned long) plen, (unsigned long) len);
467 return -1;
468 }
469
470 if (key->type != EAPOL_KEY_TYPE_RSN) {
471 wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
472 "discarded", key->type);
473 return -1;
474 }
475
476 key_info = WPA_GET_BE16(key->key_info);
477
478 return !!(key_info & WPA_KEY_INFO_ACK);
479 }
480
481
482 static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
483 struct ibss_rsn_peer *peer,
484 const u8 *buf, size_t len)
485 {
486 int supp;
487 u8 *tmp;
488
489 supp = ibss_rsn_eapol_dst_supp(buf, len);
490 if (supp < 0)
491 return -1;
492
493 tmp = os_malloc(len);
494 if (tmp == NULL)
495 return -1;
496 os_memcpy(tmp, buf, len);
497 if (supp) {
498 wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant");
499 wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
500 } else {
501 wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator");
502 wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
503 }
504 os_free(tmp);
505
506 return 1;
507 }
508
509
510 int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
511 const u8 *buf, size_t len)
512 {
513 struct ibss_rsn_peer *peer;
514
515 for (peer = ibss_rsn->peers; peer; peer = peer->next) {
516 if (os_memcmp(src_addr, peer->addr, ETH_ALEN) == 0)
517 return ibss_rsn_process_rx_eapol(ibss_rsn, peer,
518 buf, len);
519 }
520
521 if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
522 /*
523 * Create new IBSS peer based on an EAPOL message from the peer
524 * Authenticator.
525 */
526 if (ibss_rsn_start(ibss_rsn, src_addr) < 0)
527 return -1;
528 return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
529 buf, len);
530 }
531
532 return 0;
533 }
534
535
536 void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
537 {
538 os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
539 }
540
541
542 void ibss_rsn_connected(struct ibss_rsn *ibss_rsn)
543 {
544 u8 seq[6];
545 wpa_printf(MSG_DEBUG, "RSN: IBSS connected notification");
546 if (ibss_rsn->init_gtk_idx) {
547 wpa_printf(MSG_DEBUG, "RSN: Set initial IBSS TX GTK");
548 os_memset(seq, 0, sizeof(seq));
549 if (wpa_drv_set_key(ibss_rsn->wpa_s, WPA_ALG_CCMP,
550 broadcast_ether_addr,
551 ibss_rsn->init_gtk_idx,
552 1, seq, sizeof(seq), ibss_rsn->init_gtk,
553 16) < 0)
554 wpa_printf(MSG_INFO, "RSN: Failed to set IBSS TX GTK");
555 ibss_rsn->init_gtk_idx = 0;
556 os_memset(ibss_rsn->init_gtk, 0, sizeof(ibss_rsn->init_gtk));
557 }
558 }