2 * WPA Supplicant - Glue code to setup EAPOL and RSN modules
3 * Copyright (c) 2003-2007, 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"
22 #include "l2_packet/l2_packet.h"
23 #include "wpa_common.h"
24 #include "wpa_supplicant_i.h"
25 #include "pmksa_cache.h"
27 #include "ieee802_11_defs.h"
29 #include "wpas_glue.h"
32 #ifndef CONFIG_NO_CONFIG_BLOBS
33 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
34 static void wpa_supplicant_set_config_blob(void *ctx
,
35 struct wpa_config_blob
*blob
)
37 struct wpa_supplicant
*wpa_s
= ctx
;
38 wpa_config_set_blob(wpa_s
->conf
, blob
);
42 static const struct wpa_config_blob
*
43 wpa_supplicant_get_config_blob(void *ctx
, const char *name
)
45 struct wpa_supplicant
*wpa_s
= ctx
;
46 return wpa_config_get_blob(wpa_s
->conf
, name
);
48 #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
49 #endif /* CONFIG_NO_CONFIG_BLOBS */
52 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
53 static u8
* wpa_alloc_eapol(const struct wpa_supplicant
*wpa_s
, u8 type
,
54 const void *data
, u16 data_len
,
55 size_t *msg_len
, void **data_pos
)
57 struct ieee802_1x_hdr
*hdr
;
59 *msg_len
= sizeof(*hdr
) + data_len
;
60 hdr
= os_malloc(*msg_len
);
64 hdr
->version
= wpa_s
->conf
->eapol_version
;
66 hdr
->length
= host_to_be16(data_len
);
69 os_memcpy(hdr
+ 1, data
, data_len
);
71 os_memset(hdr
+ 1, 0, data_len
);
81 * wpa_ether_send - Send Ethernet frame
82 * @wpa_s: Pointer to wpa_supplicant data
83 * @dest: Destination MAC address
84 * @proto: Ethertype in host byte order
85 * @buf: Frame payload starting from IEEE 802.1X header
86 * @len: Frame payload length
87 * Returns: >=0 on success, <0 on failure
89 static int wpa_ether_send(struct wpa_supplicant
*wpa_s
, const u8
*dest
,
90 u16 proto
, const u8
*buf
, size_t len
)
93 return l2_packet_send(wpa_s
->l2
, dest
, proto
, buf
, len
);
96 return wpa_drv_send_eapol(wpa_s
, dest
, proto
, buf
, len
);
98 #endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
101 #ifdef IEEE8021X_EAPOL
104 * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
105 * @ctx: Pointer to wpa_supplicant data (wpa_s)
106 * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
107 * @buf: EAPOL payload (after IEEE 802.1X header)
108 * @len: EAPOL payload length
109 * Returns: >=0 on success, <0 on failure
111 * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
112 * to the current Authenticator.
114 static int wpa_supplicant_eapol_send(void *ctx
, int type
, const u8
*buf
,
117 struct wpa_supplicant
*wpa_s
= ctx
;
118 u8
*msg
, *dst
, bssid
[ETH_ALEN
];
122 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
125 if (wpa_s
->key_mgmt
== WPA_KEY_MGMT_PSK
||
126 wpa_s
->key_mgmt
== WPA_KEY_MGMT_FT_PSK
||
127 wpa_s
->key_mgmt
== WPA_KEY_MGMT_NONE
) {
128 /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
129 * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
131 wpa_printf(MSG_DEBUG
, "WPA: drop TX EAPOL in non-IEEE 802.1X "
132 "mode (type=%d len=%lu)", type
,
133 (unsigned long) len
);
137 if (pmksa_cache_get_current(wpa_s
->wpa
) &&
138 type
== IEEE802_1X_TYPE_EAPOL_START
) {
139 /* Trying to use PMKSA caching - do not send EAPOL-Start frames
140 * since they will trigger full EAPOL authentication. */
141 wpa_printf(MSG_DEBUG
, "RSN: PMKSA caching - do not send "
146 if (is_zero_ether_addr(wpa_s
->bssid
)) {
147 wpa_printf(MSG_DEBUG
, "BSSID not set when trying to send an "
149 if (wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
150 !is_zero_ether_addr(bssid
)) {
152 wpa_printf(MSG_DEBUG
, "Using current BSSID " MACSTR
153 " from the driver as the EAPOL destination",
156 dst
= wpa_s
->last_eapol_src
;
157 wpa_printf(MSG_DEBUG
, "Using the source address of the"
158 " last received EAPOL frame " MACSTR
" as "
159 "the EAPOL destination",
163 /* BSSID was already set (from (Re)Assoc event, so use it as
164 * the EAPOL destination. */
168 msg
= wpa_alloc_eapol(wpa_s
, type
, buf
, len
, &msglen
, NULL
);
172 wpa_printf(MSG_DEBUG
, "TX EAPOL: dst=" MACSTR
, MAC2STR(dst
));
173 wpa_hexdump(MSG_MSGDUMP
, "TX EAPOL", msg
, msglen
);
174 res
= wpa_ether_send(wpa_s
, dst
, ETH_P_EAPOL
, msg
, msglen
);
181 * wpa_eapol_set_wep_key - set WEP key for the driver
182 * @ctx: Pointer to wpa_supplicant data (wpa_s)
183 * @unicast: 1 = individual unicast key, 0 = broadcast key
184 * @keyidx: WEP key index (0..3)
185 * @key: Pointer to key data
186 * @keylen: Key length in bytes
187 * Returns: 0 on success or < 0 on error.
189 static int wpa_eapol_set_wep_key(void *ctx
, int unicast
, int keyidx
,
190 const u8
*key
, size_t keylen
)
192 struct wpa_supplicant
*wpa_s
= ctx
;
193 if (wpa_s
->key_mgmt
== WPA_KEY_MGMT_IEEE8021X_NO_WPA
) {
194 int cipher
= (keylen
== 5) ? WPA_CIPHER_WEP40
:
197 wpa_s
->pairwise_cipher
= cipher
;
199 wpa_s
->group_cipher
= cipher
;
201 return wpa_drv_set_key(wpa_s
, WPA_ALG_WEP
,
202 unicast
? wpa_s
->bssid
:
203 (u8
*) "\xff\xff\xff\xff\xff\xff",
204 keyidx
, unicast
, (u8
*) "", 0, key
, keylen
);
208 static void wpa_supplicant_aborted_cached(void *ctx
)
210 struct wpa_supplicant
*wpa_s
= ctx
;
211 wpa_sm_aborted_cached(wpa_s
->wpa
);
215 static void wpa_supplicant_eapol_cb(struct eapol_sm
*eapol
, int success
,
218 struct wpa_supplicant
*wpa_s
= ctx
;
222 wpa_printf(MSG_DEBUG
, "EAPOL authentication completed %ssuccessfully",
223 success
? "" : "un");
225 if (!success
|| !wpa_s
->driver_4way_handshake
)
228 if (wpa_s
->key_mgmt
!= WPA_KEY_MGMT_IEEE8021X
&&
229 wpa_s
->key_mgmt
!= WPA_KEY_MGMT_FT_IEEE8021X
)
232 wpa_printf(MSG_DEBUG
, "Configure PMK for driver-based RSN 4-way "
236 res
= eapol_sm_get_key(eapol
, pmk
, PMK_LEN
);
239 * EAP-LEAP is an exception from other EAP methods: it
240 * uses only 16-byte PMK.
242 res
= eapol_sm_get_key(eapol
, pmk
, 16);
247 wpa_printf(MSG_DEBUG
, "Failed to get PMK from EAPOL state "
252 if (wpa_drv_set_key(wpa_s
, WPA_ALG_PMK
, NULL
, 0, 0, NULL
, 0, pmk
,
254 wpa_printf(MSG_DEBUG
, "Failed to set PMK to the driver");
257 wpa_supplicant_cancel_scan(wpa_s
);
258 wpa_supplicant_cancel_auth_timeout(wpa_s
);
259 wpa_supplicant_set_state(wpa_s
, WPA_COMPLETED
);
264 static void wpa_supplicant_notify_eapol_done(void *ctx
)
266 struct wpa_supplicant
*wpa_s
= ctx
;
267 wpa_msg(wpa_s
, MSG_DEBUG
, "WPA: EAPOL processing complete");
268 if (wpa_s
->key_mgmt
== WPA_KEY_MGMT_IEEE8021X
||
269 wpa_s
->key_mgmt
== WPA_KEY_MGMT_FT_IEEE8021X
) {
270 wpa_supplicant_set_state(wpa_s
, WPA_4WAY_HANDSHAKE
);
272 wpa_supplicant_cancel_auth_timeout(wpa_s
);
273 wpa_supplicant_set_state(wpa_s
, WPA_COMPLETED
);
277 #endif /* IEEE8021X_EAPOL */
280 #ifndef CONFIG_NO_WPA
282 static int wpa_get_beacon_ie(struct wpa_supplicant
*wpa_s
)
286 struct wpa_scan_res
*curr
= NULL
;
287 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
290 if (wpa_s
->scan_res
== NULL
)
293 for (i
= 0; i
< wpa_s
->scan_res
->num
; i
++) {
294 struct wpa_scan_res
*r
= wpa_s
->scan_res
->res
[i
];
295 if (os_memcmp(r
->bssid
, wpa_s
->bssid
, ETH_ALEN
) != 0)
297 ie
= wpa_scan_get_ie(r
, WLAN_EID_SSID
);
299 ((ie
&& ie
[1] == ssid
->ssid_len
&&
300 os_memcmp(ie
+ 2, ssid
->ssid
, ssid
->ssid_len
) == 0) ||
301 ssid
->ssid_len
== 0)) {
308 ie
= wpa_scan_get_vendor_ie(curr
, WPA_IE_VENDOR_TYPE
);
309 if (wpa_sm_set_ap_wpa_ie(wpa_s
->wpa
, ie
, ie
? 2 + ie
[1] : 0))
312 ie
= wpa_scan_get_ie(curr
, WLAN_EID_RSN
);
313 if (wpa_sm_set_ap_rsn_ie(wpa_s
->wpa
, ie
, ie
? 2 + ie
[1] : 0))
323 static int wpa_supplicant_get_beacon_ie(void *ctx
)
325 struct wpa_supplicant
*wpa_s
= ctx
;
326 if (wpa_get_beacon_ie(wpa_s
) == 0) {
330 /* No WPA/RSN IE found in the cached scan results. Try to get updated
331 * scan results from the driver. */
332 if (wpa_supplicant_get_scan_results(wpa_s
) < 0) {
336 return wpa_get_beacon_ie(wpa_s
);
340 static u8
* _wpa_alloc_eapol(void *wpa_s
, u8 type
,
341 const void *data
, u16 data_len
,
342 size_t *msg_len
, void **data_pos
)
344 return wpa_alloc_eapol(wpa_s
, type
, data
, data_len
, msg_len
, data_pos
);
348 static int _wpa_ether_send(void *wpa_s
, const u8
*dest
, u16 proto
,
349 const u8
*buf
, size_t len
)
351 return wpa_ether_send(wpa_s
, dest
, proto
, buf
, len
);
355 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s
)
357 wpa_supplicant_cancel_auth_timeout(wpa_s
);
361 static void _wpa_supplicant_set_state(void *wpa_s
, wpa_states state
)
363 wpa_supplicant_set_state(wpa_s
, state
);
368 * wpa_supplicant_get_state - Get the connection state
369 * @wpa_s: Pointer to wpa_supplicant data
370 * Returns: The current connection state (WPA_*)
372 static wpa_states
wpa_supplicant_get_state(struct wpa_supplicant
*wpa_s
)
374 return wpa_s
->wpa_state
;
378 static wpa_states
_wpa_supplicant_get_state(void *wpa_s
)
380 return wpa_supplicant_get_state(wpa_s
);
384 static void _wpa_supplicant_disassociate(void *wpa_s
, int reason_code
)
386 wpa_supplicant_disassociate(wpa_s
, reason_code
);
387 /* Schedule a scan to make sure we continue looking for networks */
388 wpa_supplicant_req_scan(wpa_s
, 0, 0);
392 static void _wpa_supplicant_deauthenticate(void *wpa_s
, int reason_code
)
394 wpa_supplicant_deauthenticate(wpa_s
, reason_code
);
395 /* Schedule a scan to make sure we continue looking for networks */
396 wpa_supplicant_req_scan(wpa_s
, 0, 0);
400 static void * wpa_supplicant_get_network_ctx(void *wpa_s
)
402 return wpa_supplicant_get_ssid(wpa_s
);
406 static int wpa_supplicant_get_bssid(void *ctx
, u8
*bssid
)
408 struct wpa_supplicant
*wpa_s
= ctx
;
409 if (wpa_s
->use_client_mlme
) {
410 os_memcpy(bssid
, wpa_s
->bssid
, ETH_ALEN
);
413 return wpa_drv_get_bssid(wpa_s
, bssid
);
417 static int wpa_supplicant_set_key(void *wpa_s
, wpa_alg alg
,
418 const u8
*addr
, int key_idx
, int set_tx
,
419 const u8
*seq
, size_t seq_len
,
420 const u8
*key
, size_t key_len
)
422 return wpa_drv_set_key(wpa_s
, alg
, addr
, key_idx
, set_tx
, seq
, seq_len
,
427 static int wpa_supplicant_mlme_setprotection(void *wpa_s
, const u8
*addr
,
431 return wpa_drv_mlme_setprotection(wpa_s
, addr
, protection_type
,
436 static int wpa_supplicant_add_pmkid(void *wpa_s
,
437 const u8
*bssid
, const u8
*pmkid
)
439 return wpa_drv_add_pmkid(wpa_s
, bssid
, pmkid
);
443 static int wpa_supplicant_remove_pmkid(void *wpa_s
,
444 const u8
*bssid
, const u8
*pmkid
)
446 return wpa_drv_remove_pmkid(wpa_s
, bssid
, pmkid
);
450 #ifdef CONFIG_IEEE80211R
451 static int wpa_supplicant_update_ft_ies(void *ctx
, const u8
*md
,
452 const u8
*ies
, size_t ies_len
)
454 struct wpa_supplicant
*wpa_s
= ctx
;
455 if (wpa_s
->use_client_mlme
)
456 return ieee80211_sta_update_ft_ies(wpa_s
, md
, ies
, ies_len
);
457 return wpa_drv_update_ft_ies(wpa_s
, md
, ies
, ies_len
);
461 static int wpa_supplicant_send_ft_action(void *ctx
, u8 action
,
463 const u8
*ies
, size_t ies_len
)
465 struct wpa_supplicant
*wpa_s
= ctx
;
466 if (wpa_s
->use_client_mlme
)
467 return ieee80211_sta_send_ft_action(wpa_s
, action
, target_ap
,
469 return wpa_drv_send_ft_action(wpa_s
, action
, target_ap
, ies
, ies_len
);
471 #endif /* CONFIG_IEEE80211R */
473 #endif /* CONFIG_NO_WPA */
476 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
477 static void wpa_supplicant_eap_param_needed(void *ctx
, const char *field
,
480 struct wpa_supplicant
*wpa_s
= ctx
;
481 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
489 buflen
= 100 + os_strlen(txt
) + ssid
->ssid_len
;
490 buf
= os_malloc(buflen
);
493 len
= os_snprintf(buf
, buflen
,
494 WPA_CTRL_REQ
"%s-%d:%s needed for SSID ",
495 field
, ssid
->id
, txt
);
496 if (len
< 0 || (size_t) len
>= buflen
) {
500 if (ssid
->ssid
&& buflen
> len
+ ssid
->ssid_len
) {
501 os_memcpy(buf
+ len
, ssid
->ssid
, ssid
->ssid_len
);
502 len
+= ssid
->ssid_len
;
505 buf
[buflen
- 1] = '\0';
506 wpa_msg(wpa_s
, MSG_INFO
, "%s", buf
);
509 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
510 #define wpa_supplicant_eap_param_needed NULL
511 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
514 int wpa_supplicant_init_eapol(struct wpa_supplicant
*wpa_s
)
516 #ifdef IEEE8021X_EAPOL
517 struct eapol_ctx
*ctx
;
518 ctx
= os_zalloc(sizeof(*ctx
));
520 wpa_printf(MSG_ERROR
, "Failed to allocate EAPOL context.");
525 ctx
->msg_ctx
= wpa_s
;
526 ctx
->eapol_send_ctx
= wpa_s
;
528 ctx
->eapol_done_cb
= wpa_supplicant_notify_eapol_done
;
529 ctx
->eapol_send
= wpa_supplicant_eapol_send
;
530 ctx
->set_wep_key
= wpa_eapol_set_wep_key
;
531 ctx
->set_config_blob
= wpa_supplicant_set_config_blob
;
532 ctx
->get_config_blob
= wpa_supplicant_get_config_blob
;
533 ctx
->aborted_cached
= wpa_supplicant_aborted_cached
;
534 #ifdef EAP_TLS_OPENSSL
535 ctx
->opensc_engine_path
= wpa_s
->conf
->opensc_engine_path
;
536 ctx
->pkcs11_engine_path
= wpa_s
->conf
->pkcs11_engine_path
;
537 ctx
->pkcs11_module_path
= wpa_s
->conf
->pkcs11_module_path
;
538 #endif /* EAP_TLS_OPENSSL */
539 ctx
->eap_param_needed
= wpa_supplicant_eap_param_needed
;
540 ctx
->cb
= wpa_supplicant_eapol_cb
;
542 wpa_s
->eapol
= eapol_sm_init(ctx
);
543 if (wpa_s
->eapol
== NULL
) {
545 wpa_printf(MSG_ERROR
, "Failed to initialize EAPOL state "
549 #endif /* IEEE8021X_EAPOL */
555 int wpa_supplicant_init_wpa(struct wpa_supplicant
*wpa_s
)
557 #ifndef CONFIG_NO_WPA
558 struct wpa_sm_ctx
*ctx
;
559 ctx
= os_zalloc(sizeof(*ctx
));
561 wpa_printf(MSG_ERROR
, "Failed to allocate WPA context.");
566 ctx
->set_state
= _wpa_supplicant_set_state
;
567 ctx
->get_state
= _wpa_supplicant_get_state
;
568 ctx
->deauthenticate
= _wpa_supplicant_deauthenticate
;
569 ctx
->disassociate
= _wpa_supplicant_disassociate
;
570 ctx
->set_key
= wpa_supplicant_set_key
;
571 ctx
->get_network_ctx
= wpa_supplicant_get_network_ctx
;
572 ctx
->get_bssid
= wpa_supplicant_get_bssid
;
573 ctx
->ether_send
= _wpa_ether_send
;
574 ctx
->get_beacon_ie
= wpa_supplicant_get_beacon_ie
;
575 ctx
->alloc_eapol
= _wpa_alloc_eapol
;
576 ctx
->cancel_auth_timeout
= _wpa_supplicant_cancel_auth_timeout
;
577 ctx
->add_pmkid
= wpa_supplicant_add_pmkid
;
578 ctx
->remove_pmkid
= wpa_supplicant_remove_pmkid
;
579 #ifndef CONFIG_NO_CONFIG_BLOBS
580 ctx
->set_config_blob
= wpa_supplicant_set_config_blob
;
581 ctx
->get_config_blob
= wpa_supplicant_get_config_blob
;
582 #endif /* CONFIG_NO_CONFIG_BLOBS */
583 ctx
->mlme_setprotection
= wpa_supplicant_mlme_setprotection
;
584 #ifdef CONFIG_IEEE80211R
585 ctx
->update_ft_ies
= wpa_supplicant_update_ft_ies
;
586 ctx
->send_ft_action
= wpa_supplicant_send_ft_action
;
587 #endif /* CONFIG_IEEE80211R */
589 wpa_s
->wpa
= wpa_sm_init(ctx
);
590 if (wpa_s
->wpa
== NULL
) {
591 wpa_printf(MSG_ERROR
, "Failed to initialize WPA state "
595 #endif /* CONFIG_NO_WPA */
601 void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant
*wpa_s
,
602 struct wpa_ssid
*ssid
)
604 struct rsn_supp_config conf
;
606 os_memset(&conf
, 0, sizeof(conf
));
607 conf
.network_ctx
= ssid
;
608 conf
.peerkey_enabled
= ssid
->peerkey
;
609 conf
.allowed_pairwise_cipher
= ssid
->pairwise_cipher
;
610 #ifdef IEEE8021X_EAPOL
611 conf
.eap_workaround
= ssid
->eap_workaround
;
612 conf
.eap_conf_ctx
= &ssid
->eap
;
613 #endif /* IEEE8021X_EAPOL */
614 conf
.ssid
= ssid
->ssid
;
615 conf
.ssid_len
= ssid
->ssid_len
;
617 wpa_sm_set_config(wpa_s
->wpa
, ssid
? &conf
: NULL
);