2 * WPA Supplicant - Glue code to setup EAPOL and RSN modules
3 * Copyright (c) 2003-2008, 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 "eapol_supp/eapol_supp_sm.h"
19 #include "rsn_supp/wpa.h"
22 #include "l2_packet/l2_packet.h"
23 #include "common/wpa_common.h"
24 #include "wpa_supplicant_i.h"
26 #include "rsn_supp/pmksa_cache.h"
29 #include "common/ieee802_11_defs.h"
30 #include "common/wpa_ctrl.h"
31 #include "wpas_glue.h"
32 #include "wps_supplicant.h"
35 #ifndef CONFIG_NO_CONFIG_BLOBS
36 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
37 static void wpa_supplicant_set_config_blob(void *ctx
,
38 struct wpa_config_blob
*blob
)
40 struct wpa_supplicant
*wpa_s
= ctx
;
41 wpa_config_set_blob(wpa_s
->conf
, blob
);
42 if (wpa_s
->conf
->update_config
) {
43 int ret
= wpa_config_write(wpa_s
->confname
, wpa_s
->conf
);
45 wpa_printf(MSG_DEBUG
, "Failed to update config after "
52 static const struct wpa_config_blob
*
53 wpa_supplicant_get_config_blob(void *ctx
, const char *name
)
55 struct wpa_supplicant
*wpa_s
= ctx
;
56 return wpa_config_get_blob(wpa_s
->conf
, name
);
58 #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
59 #endif /* CONFIG_NO_CONFIG_BLOBS */
62 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
63 static u8
* wpa_alloc_eapol(const struct wpa_supplicant
*wpa_s
, u8 type
,
64 const void *data
, u16 data_len
,
65 size_t *msg_len
, void **data_pos
)
67 struct ieee802_1x_hdr
*hdr
;
69 *msg_len
= sizeof(*hdr
) + data_len
;
70 hdr
= os_malloc(*msg_len
);
74 hdr
->version
= wpa_s
->conf
->eapol_version
;
76 hdr
->length
= host_to_be16(data_len
);
79 os_memcpy(hdr
+ 1, data
, data_len
);
81 os_memset(hdr
+ 1, 0, data_len
);
91 * wpa_ether_send - Send Ethernet frame
92 * @wpa_s: Pointer to wpa_supplicant data
93 * @dest: Destination MAC address
94 * @proto: Ethertype in host byte order
95 * @buf: Frame payload starting from IEEE 802.1X header
96 * @len: Frame payload length
97 * Returns: >=0 on success, <0 on failure
99 static int wpa_ether_send(struct wpa_supplicant
*wpa_s
, const u8
*dest
,
100 u16 proto
, const u8
*buf
, size_t len
)
103 return l2_packet_send(wpa_s
->l2
, dest
, proto
, buf
, len
);
106 return wpa_drv_send_eapol(wpa_s
, dest
, proto
, buf
, len
);
108 #endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
111 #ifdef IEEE8021X_EAPOL
114 * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
115 * @ctx: Pointer to wpa_supplicant data (wpa_s)
116 * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
117 * @buf: EAPOL payload (after IEEE 802.1X header)
118 * @len: EAPOL payload length
119 * Returns: >=0 on success, <0 on failure
121 * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
122 * to the current Authenticator.
124 static int wpa_supplicant_eapol_send(void *ctx
, int type
, const u8
*buf
,
127 struct wpa_supplicant
*wpa_s
= ctx
;
128 u8
*msg
, *dst
, bssid
[ETH_ALEN
];
132 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
135 if (wpa_key_mgmt_wpa_psk(wpa_s
->key_mgmt
) ||
136 wpa_s
->key_mgmt
== WPA_KEY_MGMT_NONE
) {
137 /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
138 * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
140 wpa_printf(MSG_DEBUG
, "WPA: drop TX EAPOL in non-IEEE 802.1X "
141 "mode (type=%d len=%lu)", type
,
142 (unsigned long) len
);
146 if (pmksa_cache_get_current(wpa_s
->wpa
) &&
147 type
== IEEE802_1X_TYPE_EAPOL_START
) {
148 /* Trying to use PMKSA caching - do not send EAPOL-Start frames
149 * since they will trigger full EAPOL authentication. */
150 wpa_printf(MSG_DEBUG
, "RSN: PMKSA caching - do not send "
155 if (is_zero_ether_addr(wpa_s
->bssid
)) {
156 wpa_printf(MSG_DEBUG
, "BSSID not set when trying to send an "
158 if (wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
159 !is_zero_ether_addr(bssid
)) {
161 wpa_printf(MSG_DEBUG
, "Using current BSSID " MACSTR
162 " from the driver as the EAPOL destination",
165 dst
= wpa_s
->last_eapol_src
;
166 wpa_printf(MSG_DEBUG
, "Using the source address of the"
167 " last received EAPOL frame " MACSTR
" as "
168 "the EAPOL destination",
172 /* BSSID was already set (from (Re)Assoc event, so use it as
173 * the EAPOL destination. */
177 msg
= wpa_alloc_eapol(wpa_s
, type
, buf
, len
, &msglen
, NULL
);
181 wpa_printf(MSG_DEBUG
, "TX EAPOL: dst=" MACSTR
, MAC2STR(dst
));
182 wpa_hexdump(MSG_MSGDUMP
, "TX EAPOL", msg
, msglen
);
183 res
= wpa_ether_send(wpa_s
, dst
, ETH_P_EAPOL
, msg
, msglen
);
190 * wpa_eapol_set_wep_key - set WEP key for the driver
191 * @ctx: Pointer to wpa_supplicant data (wpa_s)
192 * @unicast: 1 = individual unicast key, 0 = broadcast key
193 * @keyidx: WEP key index (0..3)
194 * @key: Pointer to key data
195 * @keylen: Key length in bytes
196 * Returns: 0 on success or < 0 on error.
198 static int wpa_eapol_set_wep_key(void *ctx
, int unicast
, int keyidx
,
199 const u8
*key
, size_t keylen
)
201 struct wpa_supplicant
*wpa_s
= ctx
;
202 if (wpa_s
->key_mgmt
== WPA_KEY_MGMT_IEEE8021X_NO_WPA
) {
203 int cipher
= (keylen
== 5) ? WPA_CIPHER_WEP40
:
206 wpa_s
->pairwise_cipher
= cipher
;
208 wpa_s
->group_cipher
= cipher
;
210 return wpa_drv_set_key(wpa_s
, WPA_ALG_WEP
,
211 unicast
? wpa_s
->bssid
:
212 (u8
*) "\xff\xff\xff\xff\xff\xff",
213 keyidx
, unicast
, (u8
*) "", 0, key
, keylen
);
217 static void wpa_supplicant_aborted_cached(void *ctx
)
219 struct wpa_supplicant
*wpa_s
= ctx
;
220 wpa_sm_aborted_cached(wpa_s
->wpa
);
224 static void wpa_supplicant_eapol_cb(struct eapol_sm
*eapol
, int success
,
227 struct wpa_supplicant
*wpa_s
= ctx
;
231 wpa_printf(MSG_DEBUG
, "EAPOL authentication completed %ssuccessfully",
232 success
? "" : "un");
234 if (wpas_wps_eapol_cb(wpa_s
) > 0)
239 * Make sure we do not get stuck here waiting for long EAPOL
240 * timeout if the AP does not disconnect in case of
241 * authentication failure.
243 wpa_supplicant_req_auth_timeout(wpa_s
, 2, 0);
246 if (!success
|| !(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_4WAY_HANDSHAKE
))
249 if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s
->key_mgmt
))
252 wpa_printf(MSG_DEBUG
, "Configure PMK for driver-based RSN 4-way "
256 res
= eapol_sm_get_key(eapol
, pmk
, PMK_LEN
);
259 * EAP-LEAP is an exception from other EAP methods: it
260 * uses only 16-byte PMK.
262 res
= eapol_sm_get_key(eapol
, pmk
, 16);
267 wpa_printf(MSG_DEBUG
, "Failed to get PMK from EAPOL state "
272 if (wpa_drv_set_key(wpa_s
, WPA_ALG_PMK
, NULL
, 0, 0, NULL
, 0, pmk
,
274 wpa_printf(MSG_DEBUG
, "Failed to set PMK to the driver");
277 wpa_supplicant_cancel_scan(wpa_s
);
278 wpa_supplicant_cancel_auth_timeout(wpa_s
);
279 wpa_supplicant_set_state(wpa_s
, WPA_COMPLETED
);
284 static void wpa_supplicant_notify_eapol_done(void *ctx
)
286 struct wpa_supplicant
*wpa_s
= ctx
;
287 wpa_msg(wpa_s
, MSG_DEBUG
, "WPA: EAPOL processing complete");
288 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s
->key_mgmt
)) {
289 wpa_supplicant_set_state(wpa_s
, WPA_4WAY_HANDSHAKE
);
291 wpa_supplicant_cancel_auth_timeout(wpa_s
);
292 wpa_supplicant_set_state(wpa_s
, WPA_COMPLETED
);
296 #endif /* IEEE8021X_EAPOL */
299 #ifndef CONFIG_NO_WPA
301 static int wpa_get_beacon_ie(struct wpa_supplicant
*wpa_s
)
305 struct wpa_scan_res
*curr
= NULL
;
306 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
309 if (wpa_s
->scan_res
== NULL
)
312 for (i
= 0; i
< wpa_s
->scan_res
->num
; i
++) {
313 struct wpa_scan_res
*r
= wpa_s
->scan_res
->res
[i
];
314 if (os_memcmp(r
->bssid
, wpa_s
->bssid
, ETH_ALEN
) != 0)
316 ie
= wpa_scan_get_ie(r
, WLAN_EID_SSID
);
318 ((ie
&& ie
[1] == ssid
->ssid_len
&&
319 os_memcmp(ie
+ 2, ssid
->ssid
, ssid
->ssid_len
) == 0) ||
320 ssid
->ssid_len
== 0)) {
327 ie
= wpa_scan_get_vendor_ie(curr
, WPA_IE_VENDOR_TYPE
);
328 if (wpa_sm_set_ap_wpa_ie(wpa_s
->wpa
, ie
, ie
? 2 + ie
[1] : 0))
331 ie
= wpa_scan_get_ie(curr
, WLAN_EID_RSN
);
332 if (wpa_sm_set_ap_rsn_ie(wpa_s
->wpa
, ie
, ie
? 2 + ie
[1] : 0))
342 static int wpa_supplicant_get_beacon_ie(void *ctx
)
344 struct wpa_supplicant
*wpa_s
= ctx
;
345 if (wpa_get_beacon_ie(wpa_s
) == 0) {
349 /* No WPA/RSN IE found in the cached scan results. Try to get updated
350 * scan results from the driver. */
351 if (wpa_supplicant_get_scan_results(wpa_s
, NULL
, 0) < 0) {
355 return wpa_get_beacon_ie(wpa_s
);
359 static u8
* _wpa_alloc_eapol(void *wpa_s
, u8 type
,
360 const void *data
, u16 data_len
,
361 size_t *msg_len
, void **data_pos
)
363 return wpa_alloc_eapol(wpa_s
, type
, data
, data_len
, msg_len
, data_pos
);
367 static int _wpa_ether_send(void *wpa_s
, const u8
*dest
, u16 proto
,
368 const u8
*buf
, size_t len
)
370 return wpa_ether_send(wpa_s
, dest
, proto
, buf
, len
);
374 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s
)
376 wpa_supplicant_cancel_auth_timeout(wpa_s
);
380 static void _wpa_supplicant_set_state(void *wpa_s
, enum wpa_states state
)
382 wpa_supplicant_set_state(wpa_s
, state
);
387 * wpa_supplicant_get_state - Get the connection state
388 * @wpa_s: Pointer to wpa_supplicant data
389 * Returns: The current connection state (WPA_*)
391 static enum wpa_states
wpa_supplicant_get_state(struct wpa_supplicant
*wpa_s
)
393 return wpa_s
->wpa_state
;
397 static enum wpa_states
_wpa_supplicant_get_state(void *wpa_s
)
399 return wpa_supplicant_get_state(wpa_s
);
403 static void _wpa_supplicant_disassociate(void *wpa_s
, int reason_code
)
405 wpa_supplicant_disassociate(wpa_s
, reason_code
);
406 /* Schedule a scan to make sure we continue looking for networks */
407 wpa_supplicant_req_scan(wpa_s
, 5, 0);
411 static void _wpa_supplicant_deauthenticate(void *wpa_s
, int reason_code
)
413 wpa_supplicant_deauthenticate(wpa_s
, reason_code
);
414 /* Schedule a scan to make sure we continue looking for networks */
415 wpa_supplicant_req_scan(wpa_s
, 5, 0);
419 static void * wpa_supplicant_get_network_ctx(void *wpa_s
)
421 return wpa_supplicant_get_ssid(wpa_s
);
425 static int wpa_supplicant_get_bssid(void *ctx
, u8
*bssid
)
427 struct wpa_supplicant
*wpa_s
= ctx
;
428 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_USER_SPACE_MLME
) {
429 os_memcpy(bssid
, wpa_s
->bssid
, ETH_ALEN
);
432 return wpa_drv_get_bssid(wpa_s
, bssid
);
436 static int wpa_supplicant_set_key(void *_wpa_s
, enum wpa_alg alg
,
437 const u8
*addr
, int key_idx
, int set_tx
,
438 const u8
*seq
, size_t seq_len
,
439 const u8
*key
, size_t key_len
)
441 struct wpa_supplicant
*wpa_s
= _wpa_s
;
442 if (alg
== WPA_ALG_TKIP
&& key_idx
== 0 && key_len
== 32) {
443 /* Clear the MIC error counter when setting a new PTK. */
444 wpa_s
->mic_errors_seen
= 0;
446 return wpa_drv_set_key(wpa_s
, alg
, addr
, key_idx
, set_tx
, seq
, seq_len
,
451 static int wpa_supplicant_mlme_setprotection(void *wpa_s
, const u8
*addr
,
455 return wpa_drv_mlme_setprotection(wpa_s
, addr
, protection_type
,
460 static int wpa_supplicant_add_pmkid(void *wpa_s
,
461 const u8
*bssid
, const u8
*pmkid
)
463 return wpa_drv_add_pmkid(wpa_s
, bssid
, pmkid
);
467 static int wpa_supplicant_remove_pmkid(void *wpa_s
,
468 const u8
*bssid
, const u8
*pmkid
)
470 return wpa_drv_remove_pmkid(wpa_s
, bssid
, pmkid
);
474 #ifdef CONFIG_IEEE80211R
475 static int wpa_supplicant_update_ft_ies(void *ctx
, const u8
*md
,
476 const u8
*ies
, size_t ies_len
)
478 struct wpa_supplicant
*wpa_s
= ctx
;
479 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_USER_SPACE_MLME
)
480 return ieee80211_sta_update_ft_ies(wpa_s
, md
, ies
, ies_len
);
481 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_SME
)
482 return sme_update_ft_ies(wpa_s
, md
, ies
, ies_len
);
483 return wpa_drv_update_ft_ies(wpa_s
, md
, ies
, ies_len
);
487 static int wpa_supplicant_send_ft_action(void *ctx
, u8 action
,
489 const u8
*ies
, size_t ies_len
)
491 struct wpa_supplicant
*wpa_s
= ctx
;
492 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_USER_SPACE_MLME
)
493 return ieee80211_sta_send_ft_action(wpa_s
, action
, target_ap
,
495 return wpa_drv_send_ft_action(wpa_s
, action
, target_ap
, ies
, ies_len
);
497 #endif /* CONFIG_IEEE80211R */
499 #endif /* CONFIG_NO_WPA */
502 #ifdef IEEE8021X_EAPOL
503 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
504 static void wpa_supplicant_eap_param_needed(void *ctx
, const char *field
,
507 struct wpa_supplicant
*wpa_s
= ctx
;
508 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
516 buflen
= 100 + os_strlen(txt
) + ssid
->ssid_len
;
517 buf
= os_malloc(buflen
);
520 len
= os_snprintf(buf
, buflen
,
521 WPA_CTRL_REQ
"%s-%d:%s needed for SSID ",
522 field
, ssid
->id
, txt
);
523 if (len
< 0 || (size_t) len
>= buflen
) {
527 if (ssid
->ssid
&& buflen
> len
+ ssid
->ssid_len
) {
528 os_memcpy(buf
+ len
, ssid
->ssid
, ssid
->ssid_len
);
529 len
+= ssid
->ssid_len
;
532 buf
[buflen
- 1] = '\0';
533 wpa_msg(wpa_s
, MSG_INFO
, "%s", buf
);
536 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
537 #define wpa_supplicant_eap_param_needed NULL
538 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
541 static void wpa_supplicant_port_cb(void *ctx
, int authorized
)
543 struct wpa_supplicant
*wpa_s
= ctx
;
544 wpa_printf(MSG_DEBUG
, "EAPOL: Supplicant port status: %s",
545 authorized
? "Authorized" : "Unauthorized");
546 wpa_drv_set_supp_port(wpa_s
, authorized
);
548 #endif /* IEEE8021X_EAPOL */
551 int wpa_supplicant_init_eapol(struct wpa_supplicant
*wpa_s
)
553 #ifdef IEEE8021X_EAPOL
554 struct eapol_ctx
*ctx
;
555 ctx
= os_zalloc(sizeof(*ctx
));
557 wpa_printf(MSG_ERROR
, "Failed to allocate EAPOL context.");
562 ctx
->msg_ctx
= wpa_s
;
563 ctx
->eapol_send_ctx
= wpa_s
;
565 ctx
->eapol_done_cb
= wpa_supplicant_notify_eapol_done
;
566 ctx
->eapol_send
= wpa_supplicant_eapol_send
;
567 ctx
->set_wep_key
= wpa_eapol_set_wep_key
;
568 ctx
->set_config_blob
= wpa_supplicant_set_config_blob
;
569 ctx
->get_config_blob
= wpa_supplicant_get_config_blob
;
570 ctx
->aborted_cached
= wpa_supplicant_aborted_cached
;
571 ctx
->opensc_engine_path
= wpa_s
->conf
->opensc_engine_path
;
572 ctx
->pkcs11_engine_path
= wpa_s
->conf
->pkcs11_engine_path
;
573 ctx
->pkcs11_module_path
= wpa_s
->conf
->pkcs11_module_path
;
574 ctx
->wps
= wpa_s
->wps
;
575 ctx
->eap_param_needed
= wpa_supplicant_eap_param_needed
;
576 ctx
->port_cb
= wpa_supplicant_port_cb
;
577 ctx
->cb
= wpa_supplicant_eapol_cb
;
579 wpa_s
->eapol
= eapol_sm_init(ctx
);
580 if (wpa_s
->eapol
== NULL
) {
582 wpa_printf(MSG_ERROR
, "Failed to initialize EAPOL state "
586 #endif /* IEEE8021X_EAPOL */
592 int wpa_supplicant_init_wpa(struct wpa_supplicant
*wpa_s
)
594 #ifndef CONFIG_NO_WPA
595 struct wpa_sm_ctx
*ctx
;
596 ctx
= os_zalloc(sizeof(*ctx
));
598 wpa_printf(MSG_ERROR
, "Failed to allocate WPA context.");
603 ctx
->msg_ctx
= wpa_s
;
604 ctx
->set_state
= _wpa_supplicant_set_state
;
605 ctx
->get_state
= _wpa_supplicant_get_state
;
606 ctx
->deauthenticate
= _wpa_supplicant_deauthenticate
;
607 ctx
->disassociate
= _wpa_supplicant_disassociate
;
608 ctx
->set_key
= wpa_supplicant_set_key
;
609 ctx
->get_network_ctx
= wpa_supplicant_get_network_ctx
;
610 ctx
->get_bssid
= wpa_supplicant_get_bssid
;
611 ctx
->ether_send
= _wpa_ether_send
;
612 ctx
->get_beacon_ie
= wpa_supplicant_get_beacon_ie
;
613 ctx
->alloc_eapol
= _wpa_alloc_eapol
;
614 ctx
->cancel_auth_timeout
= _wpa_supplicant_cancel_auth_timeout
;
615 ctx
->add_pmkid
= wpa_supplicant_add_pmkid
;
616 ctx
->remove_pmkid
= wpa_supplicant_remove_pmkid
;
617 #ifndef CONFIG_NO_CONFIG_BLOBS
618 ctx
->set_config_blob
= wpa_supplicant_set_config_blob
;
619 ctx
->get_config_blob
= wpa_supplicant_get_config_blob
;
620 #endif /* CONFIG_NO_CONFIG_BLOBS */
621 ctx
->mlme_setprotection
= wpa_supplicant_mlme_setprotection
;
622 #ifdef CONFIG_IEEE80211R
623 ctx
->update_ft_ies
= wpa_supplicant_update_ft_ies
;
624 ctx
->send_ft_action
= wpa_supplicant_send_ft_action
;
625 #endif /* CONFIG_IEEE80211R */
627 wpa_s
->wpa
= wpa_sm_init(ctx
);
628 if (wpa_s
->wpa
== NULL
) {
629 wpa_printf(MSG_ERROR
, "Failed to initialize WPA state "
633 #endif /* CONFIG_NO_WPA */
639 void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant
*wpa_s
,
640 struct wpa_ssid
*ssid
)
642 struct rsn_supp_config conf
;
644 os_memset(&conf
, 0, sizeof(conf
));
645 conf
.network_ctx
= ssid
;
646 conf
.peerkey_enabled
= ssid
->peerkey
;
647 conf
.allowed_pairwise_cipher
= ssid
->pairwise_cipher
;
648 #ifdef IEEE8021X_EAPOL
649 conf
.eap_workaround
= ssid
->eap_workaround
;
650 conf
.eap_conf_ctx
= &ssid
->eap
;
651 #endif /* IEEE8021X_EAPOL */
652 conf
.ssid
= ssid
->ssid
;
653 conf
.ssid_len
= ssid
->ssid_len
;
654 conf
.wpa_ptk_rekey
= ssid
->wpa_ptk_rekey
;
656 wpa_sm_set_config(wpa_s
->wpa
, ssid
? &conf
: NULL
);