2 * wpa_supplicant - IBSS RSN
3 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
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"
27 static void ibss_rsn_free(struct ibss_rsn_peer
*peer
)
29 wpa_auth_sta_deinit(peer
->auth
);
30 wpa_sm_deinit(peer
->supp
);
35 static void supp_set_state(void *ctx
, enum wpa_states state
)
37 struct ibss_rsn_peer
*peer
= ctx
;
38 peer
->supp_state
= state
;
42 static enum wpa_states
supp_get_state(void *ctx
)
44 struct ibss_rsn_peer
*peer
= ctx
;
45 return peer
->supp_state
;
49 static int supp_ether_send(void *ctx
, const u8
*dest
, u16 proto
, const u8
*buf
,
52 struct ibss_rsn_peer
*peer
= ctx
;
53 struct wpa_supplicant
*wpa_s
= peer
->ibss_rsn
->wpa_s
;
55 wpa_printf(MSG_DEBUG
, "SUPP: %s(dest=" MACSTR
" proto=0x%04x "
57 __func__
, MAC2STR(dest
), proto
, (unsigned long) len
);
60 return l2_packet_send(wpa_s
->l2
, dest
, proto
, buf
, len
);
62 return wpa_drv_send_eapol(wpa_s
, dest
, proto
, buf
, len
);
66 static u8
* supp_alloc_eapol(void *ctx
, u8 type
, const void *data
,
67 u16 data_len
, size_t *msg_len
, void **data_pos
)
69 struct ieee802_1x_hdr
*hdr
;
71 wpa_printf(MSG_DEBUG
, "SUPP: %s(type=%d data_len=%d)",
72 __func__
, type
, data_len
);
74 *msg_len
= sizeof(*hdr
) + data_len
;
75 hdr
= os_malloc(*msg_len
);
81 hdr
->length
= host_to_be16(data_len
);
84 os_memcpy(hdr
+ 1, data
, data_len
);
86 os_memset(hdr
+ 1, 0, data_len
);
95 static int supp_get_beacon_ie(void *ctx
)
97 struct ibss_rsn_peer
*peer
= ctx
;
99 wpa_printf(MSG_DEBUG
, "SUPP: %s", __func__
);
100 /* TODO: get correct RSN IE */
101 return wpa_sm_set_ap_rsn_ie(peer
->supp
,
102 (u8
*) "\x30\x14\x01\x00"
104 "\x01\x00\x00\x0f\xac\x04"
105 "\x01\x00\x00\x0f\xac\x02"
110 static int supp_set_key(void *ctx
, enum wpa_alg alg
,
111 const u8
*addr
, int key_idx
, int set_tx
,
112 const u8
*seq
, size_t seq_len
,
113 const u8
*key
, size_t key_len
)
115 struct ibss_rsn_peer
*peer
= ctx
;
117 wpa_printf(MSG_DEBUG
, "SUPP: %s(alg=%d addr=" MACSTR
" key_idx=%d "
119 __func__
, alg
, MAC2STR(addr
), key_idx
, set_tx
);
120 wpa_hexdump(MSG_DEBUG
, "SUPP: set_key - seq", seq
, seq_len
);
121 wpa_hexdump_key(MSG_DEBUG
, "SUPP: set_key - key", key
, key_len
);
125 * In IBSS RSN, the pairwise key from the 4-way handshake
126 * initiated by the peer with highest MAC address is used.
128 if (os_memcmp(peer
->ibss_rsn
->wpa_s
->own_addr
, peer
->addr
,
130 wpa_printf(MSG_DEBUG
, "SUPP: Do not use this PTK");
135 if (is_broadcast_ether_addr(addr
))
137 return wpa_drv_set_key(peer
->ibss_rsn
->wpa_s
, alg
, addr
, key_idx
,
138 set_tx
, seq
, seq_len
, key
, key_len
);
142 static void * supp_get_network_ctx(void *ctx
)
144 struct ibss_rsn_peer
*peer
= ctx
;
145 return wpa_supplicant_get_ssid(peer
->ibss_rsn
->wpa_s
);
149 static int supp_mlme_setprotection(void *ctx
, const u8
*addr
,
150 int protection_type
, int key_type
)
152 wpa_printf(MSG_DEBUG
, "SUPP: %s(addr=" MACSTR
" protection_type=%d "
154 __func__
, MAC2STR(addr
), protection_type
, key_type
);
159 static void supp_cancel_auth_timeout(void *ctx
)
161 wpa_printf(MSG_DEBUG
, "SUPP: %s", __func__
);
165 static void supp_deauthenticate(void * ctx
, int reason_code
)
167 wpa_printf(MSG_DEBUG
, "SUPP: %s (TODO)", __func__
);
171 int ibss_rsn_supp_init(struct ibss_rsn_peer
*peer
, const u8
*own_addr
,
174 struct wpa_sm_ctx
*ctx
= os_zalloc(sizeof(*ctx
));
179 ctx
->msg_ctx
= peer
->ibss_rsn
->wpa_s
;
180 ctx
->set_state
= supp_set_state
;
181 ctx
->get_state
= supp_get_state
;
182 ctx
->ether_send
= supp_ether_send
;
183 ctx
->get_beacon_ie
= supp_get_beacon_ie
;
184 ctx
->alloc_eapol
= supp_alloc_eapol
;
185 ctx
->set_key
= supp_set_key
;
186 ctx
->get_network_ctx
= supp_get_network_ctx
;
187 ctx
->mlme_setprotection
= supp_mlme_setprotection
;
188 ctx
->cancel_auth_timeout
= supp_cancel_auth_timeout
;
189 ctx
->deauthenticate
= supp_deauthenticate
;
190 peer
->supp
= wpa_sm_init(ctx
);
191 if (peer
->supp
== NULL
) {
192 wpa_printf(MSG_DEBUG
, "SUPP: wpa_sm_init() failed");
196 wpa_sm_set_own_addr(peer
->supp
, own_addr
);
197 wpa_sm_set_param(peer
->supp
, WPA_PARAM_RSN_ENABLED
, 1);
198 wpa_sm_set_param(peer
->supp
, WPA_PARAM_PROTO
, WPA_PROTO_RSN
);
199 wpa_sm_set_param(peer
->supp
, WPA_PARAM_PAIRWISE
, WPA_CIPHER_CCMP
);
200 wpa_sm_set_param(peer
->supp
, WPA_PARAM_GROUP
, WPA_CIPHER_CCMP
);
201 wpa_sm_set_param(peer
->supp
, WPA_PARAM_KEY_MGMT
, WPA_KEY_MGMT_PSK
);
202 wpa_sm_set_pmk(peer
->supp
, psk
, PMK_LEN
);
204 peer
->supp_ie_len
= sizeof(peer
->supp_ie
);
205 if (wpa_sm_set_assoc_wpa_ie_default(peer
->supp
, peer
->supp_ie
,
206 &peer
->supp_ie_len
) < 0) {
207 wpa_printf(MSG_DEBUG
, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
212 wpa_sm_notify_assoc(peer
->supp
, peer
->addr
);
218 static void auth_logger(void *ctx
, const u8
*addr
, logger_level level
,
222 wpa_printf(MSG_DEBUG
, "AUTH: " MACSTR
" - %s",
225 wpa_printf(MSG_DEBUG
, "AUTH: %s", txt
);
229 static const u8
* auth_get_psk(void *ctx
, const u8
*addr
, const u8
*prev_psk
)
231 struct ibss_rsn
*ibss_rsn
= ctx
;
232 wpa_printf(MSG_DEBUG
, "AUTH: %s (addr=" MACSTR
" prev_psk=%p)",
233 __func__
, MAC2STR(addr
), prev_psk
);
236 return ibss_rsn
->psk
;
240 static int auth_send_eapol(void *ctx
, const u8
*addr
, const u8
*data
,
241 size_t data_len
, int encrypt
)
243 struct ibss_rsn
*ibss_rsn
= ctx
;
244 struct wpa_supplicant
*wpa_s
= ibss_rsn
->wpa_s
;
246 wpa_printf(MSG_DEBUG
, "AUTH: %s(addr=" MACSTR
" data_len=%lu "
248 __func__
, MAC2STR(addr
), (unsigned long) data_len
, encrypt
);
251 return l2_packet_send(wpa_s
->l2
, addr
, ETH_P_EAPOL
, data
,
254 return wpa_drv_send_eapol(wpa_s
, addr
, ETH_P_EAPOL
, data
, data_len
);
258 static int auth_set_key(void *ctx
, int vlan_id
, enum wpa_alg alg
,
259 const u8
*addr
, int idx
, u8
*key
, size_t key_len
)
261 struct ibss_rsn
*ibss_rsn
= ctx
;
264 os_memset(seq
, 0, sizeof(seq
));
267 wpa_printf(MSG_DEBUG
, "AUTH: %s(alg=%d addr=" MACSTR
269 __func__
, alg
, MAC2STR(addr
), idx
);
271 wpa_printf(MSG_DEBUG
, "AUTH: %s(alg=%d key_idx=%d)",
274 wpa_hexdump_key(MSG_DEBUG
, "AUTH: set_key - key", key
, key_len
);
278 * In IBSS RSN, the pairwise key from the 4-way handshake
279 * initiated by the peer with highest MAC address is used.
282 os_memcmp(ibss_rsn
->wpa_s
->own_addr
, addr
, ETH_ALEN
) < 0) {
283 wpa_printf(MSG_DEBUG
, "AUTH: Do not use this PTK");
288 return wpa_drv_set_key(ibss_rsn
->wpa_s
, alg
, addr
, idx
,
289 1, seq
, 6, key
, key_len
);
293 static int auth_for_each_sta(void *ctx
, int (*cb
)(struct wpa_state_machine
*sm
,
297 struct ibss_rsn
*ibss_rsn
= ctx
;
298 struct ibss_rsn_peer
*peer
;
300 wpa_printf(MSG_DEBUG
, "AUTH: for_each_sta");
302 for (peer
= ibss_rsn
->peers
; peer
; peer
= peer
->next
) {
303 if (peer
->auth
&& cb(peer
->auth
, cb_ctx
))
311 static int ibss_rsn_auth_init_group(struct ibss_rsn
*ibss_rsn
,
314 struct wpa_auth_config conf
;
315 struct wpa_auth_callbacks cb
;
317 wpa_printf(MSG_DEBUG
, "AUTH: Initializing group state machine");
319 os_memset(&conf
, 0, sizeof(conf
));
321 conf
.wpa_key_mgmt
= WPA_KEY_MGMT_PSK
;
322 conf
.wpa_pairwise
= WPA_CIPHER_CCMP
;
323 conf
.rsn_pairwise
= WPA_CIPHER_CCMP
;
324 conf
.wpa_group
= WPA_CIPHER_CCMP
;
325 conf
.eapol_version
= 2;
326 conf
.wpa_group_rekey
= 600;
328 os_memset(&cb
, 0, sizeof(cb
));
330 cb
.logger
= auth_logger
;
331 cb
.send_eapol
= auth_send_eapol
;
332 cb
.get_psk
= auth_get_psk
;
333 cb
.set_key
= auth_set_key
;
334 cb
.for_each_sta
= auth_for_each_sta
;
336 ibss_rsn
->auth_group
= wpa_init(own_addr
, &conf
, &cb
);
337 if (ibss_rsn
->auth_group
== NULL
) {
338 wpa_printf(MSG_DEBUG
, "AUTH: wpa_init() failed");
346 static int ibss_rsn_auth_init(struct ibss_rsn
*ibss_rsn
,
347 struct ibss_rsn_peer
*peer
)
349 peer
->auth
= wpa_auth_sta_init(ibss_rsn
->auth_group
, peer
->addr
);
350 if (peer
->auth
== NULL
) {
351 wpa_printf(MSG_DEBUG
, "AUTH: wpa_auth_sta_init() failed");
355 /* TODO: get peer RSN IE with Probe Request */
356 if (wpa_validate_wpa_ie(ibss_rsn
->auth_group
, peer
->auth
,
357 (u8
*) "\x30\x14\x01\x00"
359 "\x01\x00\x00\x0f\xac\x04"
360 "\x01\x00\x00\x0f\xac\x02"
361 "\x00\x00", 22, NULL
, 0) !=
363 wpa_printf(MSG_DEBUG
, "AUTH: wpa_validate_wpa_ie() failed");
367 if (wpa_auth_sm_event(peer
->auth
, WPA_ASSOC
))
370 if (wpa_auth_sta_associated(ibss_rsn
->auth_group
, peer
->auth
))
377 int ibss_rsn_start(struct ibss_rsn
*ibss_rsn
, const u8
*addr
)
379 struct ibss_rsn_peer
*peer
;
381 if (ibss_rsn
== NULL
)
384 for (peer
= ibss_rsn
->peers
; peer
; peer
= peer
->next
) {
385 if (os_memcmp(addr
, peer
->addr
, ETH_ALEN
) == 0) {
386 wpa_printf(MSG_DEBUG
, "RSN: IBSS Authenticator and "
387 "Supplicant for peer " MACSTR
" already "
388 "running", MAC2STR(addr
));
393 wpa_printf(MSG_DEBUG
, "RSN: Starting IBSS Authenticator and "
394 "Supplicant for peer " MACSTR
, MAC2STR(addr
));
396 peer
= os_zalloc(sizeof(*peer
));
400 peer
->ibss_rsn
= ibss_rsn
;
401 os_memcpy(peer
->addr
, addr
, ETH_ALEN
);
403 if (ibss_rsn_supp_init(peer
, ibss_rsn
->wpa_s
->own_addr
, ibss_rsn
->psk
)
409 if (ibss_rsn_auth_init(ibss_rsn
, peer
) < 0) {
414 peer
->next
= ibss_rsn
->peers
;
415 ibss_rsn
->peers
= peer
;
421 void ibss_rsn_stop(struct ibss_rsn
*ibss_rsn
, const u8
*peermac
)
423 struct ibss_rsn_peer
*peer
, *prev
;
425 if (ibss_rsn
== NULL
)
428 if (peermac
== NULL
) {
429 /* remove all peers */
430 wpa_printf(MSG_DEBUG
, "%s: Remove all peers", __func__
);
431 peer
= ibss_rsn
->peers
;
436 ibss_rsn
->peers
= peer
;
439 /* remove specific peer */
440 wpa_printf(MSG_DEBUG
, "%s: Remove specific peer " MACSTR
,
441 __func__
, MAC2STR(peermac
));
443 for (prev
= NULL
, peer
= ibss_rsn
->peers
; peer
!= NULL
;
444 prev
= peer
, peer
= peer
->next
) {
445 if (os_memcmp(peermac
, peer
->addr
, ETH_ALEN
) == 0) {
447 ibss_rsn
->peers
= peer
->next
;
449 prev
->next
= peer
->next
;
451 wpa_printf(MSG_DEBUG
, "%s: Successfully "
452 "removed a specific peer",
461 struct ibss_rsn
* ibss_rsn_init(struct wpa_supplicant
*wpa_s
)
463 struct ibss_rsn
*ibss_rsn
;
465 ibss_rsn
= os_zalloc(sizeof(*ibss_rsn
));
466 if (ibss_rsn
== NULL
)
468 ibss_rsn
->wpa_s
= wpa_s
;
470 if (ibss_rsn_auth_init_group(ibss_rsn
, wpa_s
->own_addr
) < 0) {
471 ibss_rsn_deinit(ibss_rsn
);
479 void ibss_rsn_deinit(struct ibss_rsn
*ibss_rsn
)
481 struct ibss_rsn_peer
*peer
, *prev
;
483 if (ibss_rsn
== NULL
)
486 peer
= ibss_rsn
->peers
;
493 wpa_deinit(ibss_rsn
->auth_group
);
499 static int ibss_rsn_eapol_dst_supp(const u8
*buf
, size_t len
)
501 const struct ieee802_1x_hdr
*hdr
;
502 const struct wpa_eapol_key
*key
;
506 /* TODO: Support other EAPOL packets than just EAPOL-Key */
508 if (len
< sizeof(*hdr
) + sizeof(*key
))
511 hdr
= (const struct ieee802_1x_hdr
*) buf
;
512 key
= (const struct wpa_eapol_key
*) (hdr
+ 1);
513 plen
= be_to_host16(hdr
->length
);
515 if (hdr
->version
< EAPOL_VERSION
) {
516 /* TODO: backwards compatibility */
518 if (hdr
->type
!= IEEE802_1X_TYPE_EAPOL_KEY
) {
519 wpa_printf(MSG_DEBUG
, "RSN: EAPOL frame (type %u) discarded, "
520 "not a Key frame", hdr
->type
);
523 if (plen
> len
- sizeof(*hdr
) || plen
< sizeof(*key
)) {
524 wpa_printf(MSG_DEBUG
, "RSN: EAPOL frame payload size %lu "
525 "invalid (frame size %lu)",
526 (unsigned long) plen
, (unsigned long) len
);
530 if (key
->type
!= EAPOL_KEY_TYPE_RSN
) {
531 wpa_printf(MSG_DEBUG
, "RSN: EAPOL-Key type (%d) unknown, "
532 "discarded", key
->type
);
536 key_info
= WPA_GET_BE16(key
->key_info
);
538 return !!(key_info
& WPA_KEY_INFO_ACK
);
542 static int ibss_rsn_process_rx_eapol(struct ibss_rsn
*ibss_rsn
,
543 struct ibss_rsn_peer
*peer
,
544 const u8
*buf
, size_t len
)
549 supp
= ibss_rsn_eapol_dst_supp(buf
, len
);
553 tmp
= os_malloc(len
);
556 os_memcpy(tmp
, buf
, len
);
558 wpa_printf(MSG_DEBUG
, "RSN: IBSS RX EAPOL for Supplicant");
559 wpa_sm_rx_eapol(peer
->supp
, peer
->addr
, tmp
, len
);
561 wpa_printf(MSG_DEBUG
, "RSN: IBSS RX EAPOL for Authenticator");
562 wpa_receive(ibss_rsn
->auth_group
, peer
->auth
, tmp
, len
);
570 int ibss_rsn_rx_eapol(struct ibss_rsn
*ibss_rsn
, const u8
*src_addr
,
571 const u8
*buf
, size_t len
)
573 struct ibss_rsn_peer
*peer
;
575 if (ibss_rsn
== NULL
)
578 for (peer
= ibss_rsn
->peers
; peer
; peer
= peer
->next
) {
579 if (os_memcmp(src_addr
, peer
->addr
, ETH_ALEN
) == 0)
580 return ibss_rsn_process_rx_eapol(ibss_rsn
, peer
,
584 if (ibss_rsn_eapol_dst_supp(buf
, len
) > 0) {
586 * Create new IBSS peer based on an EAPOL message from the peer
589 if (ibss_rsn_start(ibss_rsn
, src_addr
) < 0)
591 return ibss_rsn_process_rx_eapol(ibss_rsn
, ibss_rsn
->peers
,
599 void ibss_rsn_set_psk(struct ibss_rsn
*ibss_rsn
, const u8
*psk
)
601 if (ibss_rsn
== NULL
)
603 os_memcpy(ibss_rsn
->psk
, psk
, PMK_LEN
);