2 * wpa_supplicant - IBSS RSN
3 * Copyright (c) 2009-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "common/wpa_ctrl.h"
13 #include "utils/eloop.h"
14 #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"
20 #include "common/ieee802_11_defs.h"
24 static void ibss_rsn_auth_timeout(void *eloop_ctx
, void *timeout_ctx
);
27 static struct ibss_rsn_peer
* ibss_rsn_get_peer(struct ibss_rsn
*ibss_rsn
,
30 struct ibss_rsn_peer
*peer
;
32 for (peer
= ibss_rsn
->peers
; peer
; peer
= peer
->next
)
33 if (os_memcmp(addr
, peer
->addr
, ETH_ALEN
) == 0)
39 static void ibss_rsn_free(struct ibss_rsn_peer
*peer
)
41 eloop_cancel_timeout(ibss_rsn_auth_timeout
, peer
, NULL
);
42 wpa_auth_sta_deinit(peer
->auth
);
43 wpa_sm_deinit(peer
->supp
);
48 static void supp_set_state(void *ctx
, enum wpa_states state
)
50 struct ibss_rsn_peer
*peer
= ctx
;
51 peer
->supp_state
= state
;
55 static enum wpa_states
supp_get_state(void *ctx
)
57 struct ibss_rsn_peer
*peer
= ctx
;
58 return peer
->supp_state
;
62 static int supp_ether_send(void *ctx
, const u8
*dest
, u16 proto
, const u8
*buf
,
65 struct ibss_rsn_peer
*peer
= ctx
;
66 struct wpa_supplicant
*wpa_s
= peer
->ibss_rsn
->wpa_s
;
68 wpa_printf(MSG_DEBUG
, "SUPP: %s(dest=" MACSTR
" proto=0x%04x "
70 __func__
, MAC2STR(dest
), proto
, (unsigned long) len
);
73 return l2_packet_send(wpa_s
->l2
, dest
, proto
, buf
, len
);
79 static u8
* supp_alloc_eapol(void *ctx
, u8 type
, const void *data
,
80 u16 data_len
, size_t *msg_len
, void **data_pos
)
82 struct ieee802_1x_hdr
*hdr
;
84 wpa_printf(MSG_DEBUG
, "SUPP: %s(type=%d data_len=%d)",
85 __func__
, type
, data_len
);
87 *msg_len
= sizeof(*hdr
) + data_len
;
88 hdr
= os_malloc(*msg_len
);
94 hdr
->length
= host_to_be16(data_len
);
97 os_memcpy(hdr
+ 1, data
, data_len
);
99 os_memset(hdr
+ 1, 0, data_len
);
108 static int supp_get_beacon_ie(void *ctx
)
110 struct ibss_rsn_peer
*peer
= ctx
;
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"
117 "\x01\x00\x00\x0f\xac\x04"
118 "\x01\x00\x00\x0f\xac\x02"
123 static void ibss_check_rsn_completed(struct ibss_rsn_peer
*peer
)
125 struct wpa_supplicant
*wpa_s
= peer
->ibss_rsn
->wpa_s
;
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
))
131 if (peer
->authentication_status
& IBSS_RSN_REPORTED_PTK
)
133 peer
->authentication_status
|= IBSS_RSN_REPORTED_PTK
;
134 wpa_msg(wpa_s
, MSG_INFO
, IBSS_RSN_COMPLETED MACSTR
,
135 MAC2STR(peer
->addr
));
139 static 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
)
144 struct ibss_rsn_peer
*peer
= ctx
;
146 wpa_printf(MSG_DEBUG
, "SUPP: %s(alg=%d addr=" MACSTR
" key_idx=%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
);
153 peer
->authentication_status
|= IBSS_RSN_SET_PTK_SUPP
;
154 ibss_check_rsn_completed(peer
);
156 * In IBSS RSN, the pairwise key from the 4-way handshake
157 * initiated by the peer with highest MAC address is used.
159 if (os_memcmp(peer
->ibss_rsn
->wpa_s
->own_addr
, peer
->addr
,
161 wpa_printf(MSG_DEBUG
, "SUPP: Do not use this PTK");
166 if (is_broadcast_ether_addr(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
);
173 static void * supp_get_network_ctx(void *ctx
)
175 struct ibss_rsn_peer
*peer
= ctx
;
176 return wpa_supplicant_get_ssid(peer
->ibss_rsn
->wpa_s
);
180 static int supp_mlme_setprotection(void *ctx
, const u8
*addr
,
181 int protection_type
, int key_type
)
183 wpa_printf(MSG_DEBUG
, "SUPP: %s(addr=" MACSTR
" protection_type=%d "
185 __func__
, MAC2STR(addr
), protection_type
, key_type
);
190 static void supp_cancel_auth_timeout(void *ctx
)
192 wpa_printf(MSG_DEBUG
, "SUPP: %s", __func__
);
196 static void supp_deauthenticate(void * ctx
, int reason_code
)
198 wpa_printf(MSG_DEBUG
, "SUPP: %s (TODO)", __func__
);
202 static int ibss_rsn_supp_init(struct ibss_rsn_peer
*peer
, const u8
*own_addr
,
205 struct wpa_sm_ctx
*ctx
= os_zalloc(sizeof(*ctx
));
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");
228 wpa_sm_set_own_addr(peer
->supp
, own_addr
);
229 wpa_sm_set_param(peer
->supp
, WPA_PARAM_RSN_ENABLED
, 1);
230 wpa_sm_set_param(peer
->supp
, WPA_PARAM_PROTO
, WPA_PROTO_RSN
);
231 wpa_sm_set_param(peer
->supp
, WPA_PARAM_PAIRWISE
, WPA_CIPHER_CCMP
);
232 wpa_sm_set_param(peer
->supp
, WPA_PARAM_GROUP
, WPA_CIPHER_CCMP
);
233 wpa_sm_set_param(peer
->supp
, WPA_PARAM_KEY_MGMT
, WPA_KEY_MGMT_PSK
);
234 wpa_sm_set_pmk(peer
->supp
, psk
, PMK_LEN
, NULL
, NULL
);
236 peer
->supp_ie_len
= sizeof(peer
->supp_ie
);
237 if (wpa_sm_set_assoc_wpa_ie_default(peer
->supp
, peer
->supp_ie
,
238 &peer
->supp_ie_len
) < 0) {
239 wpa_printf(MSG_DEBUG
, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
244 wpa_sm_notify_assoc(peer
->supp
, peer
->addr
);
250 static void auth_logger(void *ctx
, const u8
*addr
, logger_level level
,
254 wpa_printf(MSG_DEBUG
, "AUTH: " MACSTR
" - %s",
257 wpa_printf(MSG_DEBUG
, "AUTH: %s", txt
);
261 static const u8
* auth_get_psk(void *ctx
, const u8
*addr
,
262 const u8
*p2p_dev_addr
, const u8
*prev_psk
,
265 struct ibss_rsn
*ibss_rsn
= ctx
;
269 wpa_printf(MSG_DEBUG
, "AUTH: %s (addr=" MACSTR
" prev_psk=%p)",
270 __func__
, MAC2STR(addr
), prev_psk
);
273 return ibss_rsn
->psk
;
277 static int auth_send_eapol(void *ctx
, const u8
*addr
, const u8
*data
,
278 size_t data_len
, int encrypt
)
280 struct ibss_rsn
*ibss_rsn
= ctx
;
281 struct wpa_supplicant
*wpa_s
= ibss_rsn
->wpa_s
;
283 wpa_printf(MSG_DEBUG
, "AUTH: %s(addr=" MACSTR
" data_len=%lu "
285 __func__
, MAC2STR(addr
), (unsigned long) data_len
, encrypt
);
288 return l2_packet_send(wpa_s
->l2
, addr
, ETH_P_EAPOL
, data
,
295 static int auth_set_key(void *ctx
, int vlan_id
, enum wpa_alg alg
,
296 const u8
*addr
, int idx
, u8
*key
, size_t key_len
)
298 struct ibss_rsn
*ibss_rsn
= ctx
;
301 os_memset(seq
, 0, sizeof(seq
));
304 wpa_printf(MSG_DEBUG
, "AUTH: %s(alg=%d addr=" MACSTR
306 __func__
, alg
, MAC2STR(addr
), idx
);
308 wpa_printf(MSG_DEBUG
, "AUTH: %s(alg=%d key_idx=%d)",
311 wpa_hexdump_key(MSG_DEBUG
, "AUTH: set_key - key", key
, key_len
);
315 struct ibss_rsn_peer
*peer
;
316 peer
= ibss_rsn_get_peer(ibss_rsn
, addr
);
318 peer
->authentication_status
|=
319 IBSS_RSN_SET_PTK_AUTH
;
320 ibss_check_rsn_completed(peer
);
324 * In IBSS RSN, the pairwise key from the 4-way handshake
325 * initiated by the peer with highest MAC address is used.
328 os_memcmp(ibss_rsn
->wpa_s
->own_addr
, addr
, ETH_ALEN
) < 0) {
329 wpa_printf(MSG_DEBUG
, "AUTH: Do not use this PTK");
334 return wpa_drv_set_key(ibss_rsn
->wpa_s
, alg
, addr
, idx
,
335 1, seq
, 6, key
, key_len
);
339 static void ibss_rsn_disconnect(void *ctx
, const u8
*addr
, u16 reason
)
341 struct ibss_rsn
*ibss_rsn
= ctx
;
342 wpa_drv_sta_deauth(ibss_rsn
->wpa_s
, addr
, reason
);
346 static int auth_for_each_sta(void *ctx
, int (*cb
)(struct wpa_state_machine
*sm
,
350 struct ibss_rsn
*ibss_rsn
= ctx
;
351 struct ibss_rsn_peer
*peer
;
353 wpa_printf(MSG_DEBUG
, "AUTH: for_each_sta");
355 for (peer
= ibss_rsn
->peers
; peer
; peer
= peer
->next
) {
356 if (peer
->auth
&& cb(peer
->auth
, cb_ctx
))
364 static void ibss_set_sta_authorized(struct ibss_rsn
*ibss_rsn
,
365 struct ibss_rsn_peer
*peer
, int authorized
)
370 res
= wpa_drv_sta_set_flags(ibss_rsn
->wpa_s
, peer
->addr
,
372 WPA_STA_AUTHORIZED
, ~0);
373 wpa_printf(MSG_DEBUG
, "AUTH: " MACSTR
" authorizing port",
374 MAC2STR(peer
->addr
));
376 res
= wpa_drv_sta_set_flags(ibss_rsn
->wpa_s
, peer
->addr
,
377 0, 0, ~WPA_STA_AUTHORIZED
);
378 wpa_printf(MSG_DEBUG
, "AUTH: " MACSTR
" unauthorizing port",
379 MAC2STR(peer
->addr
));
382 if (res
&& errno
!= ENOENT
) {
383 wpa_printf(MSG_DEBUG
, "Could not set station " MACSTR
" flags "
384 "for kernel driver (errno=%d)",
385 MAC2STR(peer
->addr
), errno
);
390 static void auth_set_eapol(void *ctx
, const u8
*addr
,
391 wpa_eapol_variable var
, int value
)
393 struct ibss_rsn
*ibss_rsn
= ctx
;
394 struct ibss_rsn_peer
*peer
= ibss_rsn_get_peer(ibss_rsn
, addr
);
400 case WPA_EAPOL_authorized
:
401 ibss_set_sta_authorized(ibss_rsn
, peer
, value
);
404 /* do not handle any other event */
405 wpa_printf(MSG_DEBUG
, "AUTH: eapol event not handled %d", var
);
411 static int ibss_rsn_auth_init_group(struct ibss_rsn
*ibss_rsn
,
412 const u8
*own_addr
, struct wpa_ssid
*ssid
)
414 struct wpa_auth_config conf
;
415 static const struct wpa_auth_callbacks cb
= {
416 .logger
= auth_logger
,
417 .set_eapol
= auth_set_eapol
,
418 .send_eapol
= auth_send_eapol
,
419 .get_psk
= auth_get_psk
,
420 .set_key
= auth_set_key
,
421 .for_each_sta
= auth_for_each_sta
,
422 .disconnect
= ibss_rsn_disconnect
,
425 wpa_printf(MSG_DEBUG
, "AUTH: Initializing group state machine");
427 os_memset(&conf
, 0, sizeof(conf
));
429 conf
.wpa_key_mgmt
= WPA_KEY_MGMT_PSK
;
430 conf
.wpa_pairwise
= WPA_CIPHER_CCMP
;
431 conf
.rsn_pairwise
= WPA_CIPHER_CCMP
;
432 conf
.wpa_group
= WPA_CIPHER_CCMP
;
433 conf
.eapol_version
= 2;
434 conf
.wpa_group_rekey
= ssid
->group_rekey
? ssid
->group_rekey
: 600;
435 conf
.wpa_group_update_count
= 4;
436 conf
.wpa_pairwise_update_count
= 4;
438 ibss_rsn
->auth_group
= wpa_init(own_addr
, &conf
, &cb
, ibss_rsn
);
439 if (ibss_rsn
->auth_group
== NULL
) {
440 wpa_printf(MSG_DEBUG
, "AUTH: wpa_init() failed");
444 wpa_init_keys(ibss_rsn
->auth_group
);
450 static int ibss_rsn_auth_init(struct ibss_rsn
*ibss_rsn
,
451 struct ibss_rsn_peer
*peer
)
453 peer
->auth
= wpa_auth_sta_init(ibss_rsn
->auth_group
, peer
->addr
, NULL
);
454 if (peer
->auth
== NULL
) {
455 wpa_printf(MSG_DEBUG
, "AUTH: wpa_auth_sta_init() failed");
459 /* TODO: get peer RSN IE with Probe Request */
460 if (wpa_validate_wpa_ie(ibss_rsn
->auth_group
, peer
->auth
,
461 (u8
*) "\x30\x14\x01\x00"
463 "\x01\x00\x00\x0f\xac\x04"
464 "\x01\x00\x00\x0f\xac\x02"
465 "\x00\x00", 22, NULL
, 0, NULL
, 0) !=
467 wpa_printf(MSG_DEBUG
, "AUTH: wpa_validate_wpa_ie() failed");
471 if (wpa_auth_sm_event(peer
->auth
, WPA_ASSOC
))
474 if (wpa_auth_sta_associated(ibss_rsn
->auth_group
, peer
->auth
))
481 static int ibss_rsn_send_auth(struct ibss_rsn
*ibss_rsn
, const u8
*da
, int seq
)
483 struct ieee80211_mgmt auth
;
484 const size_t auth_length
= IEEE80211_HDRLEN
+ sizeof(auth
.u
.auth
);
485 struct wpa_supplicant
*wpa_s
= ibss_rsn
->wpa_s
;
487 if (wpa_s
->driver
->send_frame
== NULL
)
490 os_memset(&auth
, 0, sizeof(auth
));
492 auth
.frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
494 os_memcpy(auth
.da
, da
, ETH_ALEN
);
495 os_memcpy(auth
.sa
, wpa_s
->own_addr
, ETH_ALEN
);
496 os_memcpy(auth
.bssid
, wpa_s
->bssid
, ETH_ALEN
);
498 auth
.u
.auth
.auth_alg
= host_to_le16(WLAN_AUTH_OPEN
);
499 auth
.u
.auth
.auth_transaction
= host_to_le16(seq
);
500 auth
.u
.auth
.status_code
= host_to_le16(WLAN_STATUS_SUCCESS
);
502 wpa_printf(MSG_DEBUG
, "RSN: IBSS TX Auth frame (SEQ %d) to " MACSTR
,
505 return wpa_s
->driver
->send_frame(wpa_s
->drv_priv
, (u8
*) &auth
,
510 static int ibss_rsn_is_auth_started(struct ibss_rsn_peer
* peer
)
512 return peer
->authentication_status
&
513 (IBSS_RSN_AUTH_BY_US
| IBSS_RSN_AUTH_EAPOL_BY_US
);
517 static struct ibss_rsn_peer
*
518 ibss_rsn_peer_init(struct ibss_rsn
*ibss_rsn
, const u8
*addr
)
520 struct ibss_rsn_peer
*peer
;
521 if (ibss_rsn
== NULL
)
524 peer
= ibss_rsn_get_peer(ibss_rsn
, addr
);
526 wpa_printf(MSG_DEBUG
, "RSN: IBSS Supplicant for peer "MACSTR
527 " already running", MAC2STR(addr
));
531 wpa_printf(MSG_DEBUG
, "RSN: Starting IBSS Supplicant for peer "MACSTR
,
534 peer
= os_zalloc(sizeof(*peer
));
536 wpa_printf(MSG_DEBUG
, "RSN: Could not allocate memory.");
540 peer
->ibss_rsn
= ibss_rsn
;
541 os_memcpy(peer
->addr
, addr
, ETH_ALEN
);
542 peer
->authentication_status
= IBSS_RSN_AUTH_NOT_AUTHENTICATED
;
544 if (ibss_rsn_supp_init(peer
, ibss_rsn
->wpa_s
->own_addr
,
545 ibss_rsn
->psk
) < 0) {
550 peer
->next
= ibss_rsn
->peers
;
551 ibss_rsn
->peers
= peer
;
557 static void ibss_rsn_auth_timeout(void *eloop_ctx
, void *timeout_ctx
)
559 struct ibss_rsn_peer
*peer
= eloop_ctx
;
562 * Assume peer does not support Authentication exchange or the frame was
563 * lost somewhere - start EAPOL Authenticator.
565 wpa_printf(MSG_DEBUG
,
566 "RSN: Timeout on waiting Authentication frame response from "
567 MACSTR
" - start authenticator", MAC2STR(peer
->addr
));
569 peer
->authentication_status
|= IBSS_RSN_AUTH_BY_US
;
570 ibss_rsn_auth_init(peer
->ibss_rsn
, peer
);
574 int ibss_rsn_start(struct ibss_rsn
*ibss_rsn
, const u8
*addr
)
576 struct ibss_rsn_peer
*peer
;
582 /* if the peer already exists, exit immediately */
583 peer
= ibss_rsn_get_peer(ibss_rsn
, addr
);
587 peer
= ibss_rsn_peer_init(ibss_rsn
, addr
);
591 /* Open Authentication: send first Authentication frame */
592 res
= ibss_rsn_send_auth(ibss_rsn
, addr
, 1);
595 * The driver may not support Authentication frame exchange in
596 * IBSS. Ignore authentication and go through EAPOL exchange.
598 peer
->authentication_status
|= IBSS_RSN_AUTH_BY_US
;
599 return ibss_rsn_auth_init(ibss_rsn
, peer
);
601 os_get_reltime(&peer
->own_auth_tx
);
602 eloop_register_timeout(1, 0, ibss_rsn_auth_timeout
, peer
, NULL
);
609 static int ibss_rsn_peer_authenticated(struct ibss_rsn
*ibss_rsn
,
610 struct ibss_rsn_peer
*peer
, int reason
)
614 if (ibss_rsn
== NULL
|| peer
== NULL
)
617 already_started
= ibss_rsn_is_auth_started(peer
);
618 peer
->authentication_status
|= reason
;
620 if (already_started
) {
621 wpa_printf(MSG_DEBUG
, "RSN: IBSS Authenticator already "
622 "started for peer " MACSTR
, MAC2STR(peer
->addr
));
626 wpa_printf(MSG_DEBUG
, "RSN: Starting IBSS Authenticator "
627 "for now-authenticated peer " MACSTR
, MAC2STR(peer
->addr
));
629 return ibss_rsn_auth_init(ibss_rsn
, peer
);
633 void ibss_rsn_stop(struct ibss_rsn
*ibss_rsn
, const u8
*peermac
)
635 struct ibss_rsn_peer
*peer
, *prev
;
637 if (ibss_rsn
== NULL
)
640 if (peermac
== NULL
) {
641 /* remove all peers */
642 wpa_printf(MSG_DEBUG
, "%s: Remove all peers", __func__
);
643 peer
= ibss_rsn
->peers
;
648 ibss_rsn
->peers
= peer
;
651 /* remove specific peer */
652 wpa_printf(MSG_DEBUG
, "%s: Remove specific peer " MACSTR
,
653 __func__
, MAC2STR(peermac
));
655 for (prev
= NULL
, peer
= ibss_rsn
->peers
; peer
!= NULL
;
656 prev
= peer
, peer
= peer
->next
) {
657 if (os_memcmp(peermac
, peer
->addr
, ETH_ALEN
) == 0) {
659 ibss_rsn
->peers
= peer
->next
;
661 prev
->next
= peer
->next
;
663 wpa_printf(MSG_DEBUG
, "%s: Successfully "
664 "removed a specific peer",
673 struct ibss_rsn
* ibss_rsn_init(struct wpa_supplicant
*wpa_s
,
674 struct wpa_ssid
*ssid
)
676 struct ibss_rsn
*ibss_rsn
;
678 ibss_rsn
= os_zalloc(sizeof(*ibss_rsn
));
679 if (ibss_rsn
== NULL
)
681 ibss_rsn
->wpa_s
= wpa_s
;
683 if (ibss_rsn_auth_init_group(ibss_rsn
, wpa_s
->own_addr
, ssid
) < 0) {
684 ibss_rsn_deinit(ibss_rsn
);
692 void ibss_rsn_deinit(struct ibss_rsn
*ibss_rsn
)
694 struct ibss_rsn_peer
*peer
, *prev
;
696 if (ibss_rsn
== NULL
)
699 peer
= ibss_rsn
->peers
;
706 if (ibss_rsn
->auth_group
)
707 wpa_deinit(ibss_rsn
->auth_group
);
713 static int ibss_rsn_eapol_dst_supp(const u8
*buf
, size_t len
)
715 const struct ieee802_1x_hdr
*hdr
;
716 const struct wpa_eapol_key
*key
;
720 /* TODO: Support other EAPOL packets than just EAPOL-Key */
722 if (len
< sizeof(*hdr
) + sizeof(*key
))
725 hdr
= (const struct ieee802_1x_hdr
*) buf
;
726 key
= (const struct wpa_eapol_key
*) (hdr
+ 1);
727 plen
= be_to_host16(hdr
->length
);
729 if (hdr
->version
< EAPOL_VERSION
) {
730 /* TODO: backwards compatibility */
732 if (hdr
->type
!= IEEE802_1X_TYPE_EAPOL_KEY
) {
733 wpa_printf(MSG_DEBUG
, "RSN: EAPOL frame (type %u) discarded, "
734 "not a Key frame", hdr
->type
);
737 if (plen
> len
- sizeof(*hdr
) || plen
< sizeof(*key
)) {
738 wpa_printf(MSG_DEBUG
, "RSN: EAPOL frame payload size %lu "
739 "invalid (frame size %lu)",
740 (unsigned long) plen
, (unsigned long) len
);
744 if (key
->type
!= EAPOL_KEY_TYPE_RSN
) {
745 wpa_printf(MSG_DEBUG
, "RSN: EAPOL-Key type (%d) unknown, "
746 "discarded", key
->type
);
750 key_info
= WPA_GET_BE16(key
->key_info
);
752 return !!(key_info
& WPA_KEY_INFO_ACK
);
756 static int ibss_rsn_process_rx_eapol(struct ibss_rsn
*ibss_rsn
,
757 struct ibss_rsn_peer
*peer
,
758 const u8
*buf
, size_t len
)
763 supp
= ibss_rsn_eapol_dst_supp(buf
, len
);
767 tmp
= os_memdup(buf
, len
);
771 peer
->authentication_status
|= IBSS_RSN_AUTH_EAPOL_BY_PEER
;
772 wpa_printf(MSG_DEBUG
, "RSN: IBSS RX EAPOL for Supplicant from "
773 MACSTR
, MAC2STR(peer
->addr
));
774 wpa_sm_rx_eapol(peer
->supp
, peer
->addr
, tmp
, len
);
776 if (ibss_rsn_is_auth_started(peer
) == 0) {
777 wpa_printf(MSG_DEBUG
, "RSN: IBSS EAPOL for "
778 "Authenticator dropped as " MACSTR
" is not "
779 "authenticated", MAC2STR(peer
->addr
));
784 wpa_printf(MSG_DEBUG
, "RSN: IBSS RX EAPOL for Authenticator "
785 "from "MACSTR
, MAC2STR(peer
->addr
));
786 wpa_receive(ibss_rsn
->auth_group
, peer
->auth
, tmp
, len
);
794 int ibss_rsn_rx_eapol(struct ibss_rsn
*ibss_rsn
, const u8
*src_addr
,
795 const u8
*buf
, size_t len
)
797 struct ibss_rsn_peer
*peer
;
799 if (ibss_rsn
== NULL
)
802 peer
= ibss_rsn_get_peer(ibss_rsn
, src_addr
);
804 return ibss_rsn_process_rx_eapol(ibss_rsn
, peer
, buf
, len
);
806 if (ibss_rsn_eapol_dst_supp(buf
, len
) > 0) {
808 * Create new IBSS peer based on an EAPOL message from the peer
811 peer
= ibss_rsn_peer_init(ibss_rsn
, src_addr
);
815 /* assume the peer is authenticated already */
816 wpa_printf(MSG_DEBUG
, "RSN: IBSS Not using IBSS Auth for peer "
817 MACSTR
, MAC2STR(src_addr
));
818 ibss_rsn_peer_authenticated(ibss_rsn
, peer
,
819 IBSS_RSN_AUTH_EAPOL_BY_US
);
821 return ibss_rsn_process_rx_eapol(ibss_rsn
, ibss_rsn
->peers
,
828 void ibss_rsn_set_psk(struct ibss_rsn
*ibss_rsn
, const u8
*psk
)
830 if (ibss_rsn
== NULL
)
832 os_memcpy(ibss_rsn
->psk
, psk
, PMK_LEN
);
836 static void ibss_rsn_handle_auth_1_of_2(struct ibss_rsn
*ibss_rsn
,
837 struct ibss_rsn_peer
*peer
,
840 wpa_printf(MSG_DEBUG
, "RSN: IBSS RX Auth frame (SEQ 1) from " MACSTR
,
844 peer
->authentication_status
& (IBSS_RSN_SET_PTK_SUPP
|
845 IBSS_RSN_SET_PTK_AUTH
)) {
846 /* Clear the TK for this pair to allow recovery from the case
847 * where the peer STA has restarted and lost its key while we
848 * still have a pairwise key configured. */
849 wpa_printf(MSG_DEBUG
, "RSN: Clear pairwise key for peer "
850 MACSTR
, MAC2STR(addr
));
851 wpa_drv_set_key(ibss_rsn
->wpa_s
, WPA_ALG_NONE
, addr
, 0, 0,
856 peer
->authentication_status
& IBSS_RSN_AUTH_EAPOL_BY_PEER
) {
857 if (peer
->own_auth_tx
.sec
) {
858 struct os_reltime now
, diff
;
859 os_get_reltime(&now
);
860 os_reltime_sub(&now
, &peer
->own_auth_tx
, &diff
);
861 if (diff
.sec
== 0 && diff
.usec
< 500000) {
862 wpa_printf(MSG_DEBUG
, "RSN: Skip IBSS reinit since only %u usec from own Auth frame TX",
868 * A peer sent us an Authentication frame even though it already
869 * started an EAPOL session. We should reinit state machines
870 * here, but it's much more complicated than just deleting and
871 * recreating the state machine
873 wpa_printf(MSG_DEBUG
, "RSN: IBSS Reinitializing station "
874 MACSTR
, MAC2STR(addr
));
876 ibss_rsn_stop(ibss_rsn
, addr
);
881 peer
= ibss_rsn_peer_init(ibss_rsn
, addr
);
885 wpa_printf(MSG_DEBUG
, "RSN: IBSS Auth started by peer " MACSTR
,
890 /* reply with an Authentication frame now, before sending an EAPOL */
891 ibss_rsn_send_auth(ibss_rsn
, addr
, 2);
892 /* no need to start another AUTH challenge in the other way.. */
893 ibss_rsn_peer_authenticated(ibss_rsn
, peer
, IBSS_RSN_AUTH_EAPOL_BY_US
);
897 void ibss_rsn_handle_auth(struct ibss_rsn
*ibss_rsn
, const u8
*auth_frame
,
900 const struct ieee80211_mgmt
*header
;
901 struct ibss_rsn_peer
*peer
;
904 header
= (const struct ieee80211_mgmt
*) auth_frame
;
905 auth_length
= IEEE80211_HDRLEN
+ sizeof(header
->u
.auth
);
907 if (ibss_rsn
== NULL
|| len
< auth_length
)
910 if (le_to_host16(header
->u
.auth
.auth_alg
) != WLAN_AUTH_OPEN
||
911 le_to_host16(header
->u
.auth
.status_code
) != WLAN_STATUS_SUCCESS
)
914 peer
= ibss_rsn_get_peer(ibss_rsn
, header
->sa
);
916 switch (le_to_host16(header
->u
.auth
.auth_transaction
)) {
918 ibss_rsn_handle_auth_1_of_2(ibss_rsn
, peer
, header
->sa
);
921 wpa_printf(MSG_DEBUG
, "RSN: IBSS RX Auth frame (SEQ 2) from "
922 MACSTR
, MAC2STR(header
->sa
));
924 wpa_printf(MSG_DEBUG
, "RSN: Received Auth seq 2 from "
925 "unknown STA " MACSTR
, MAC2STR(header
->sa
));
929 /* authentication has been completed */
930 eloop_cancel_timeout(ibss_rsn_auth_timeout
, peer
, NULL
);
931 wpa_printf(MSG_DEBUG
, "RSN: IBSS Auth completed with " MACSTR
,
932 MAC2STR(header
->sa
));
933 ibss_rsn_peer_authenticated(ibss_rsn
, peer
,
934 IBSS_RSN_AUTH_BY_US
);