LCOV - code coverage report
Current view: top level - wpa_supplicant - wpas_glue.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1443382998 Lines: 420 501 83.8 %
Date: 2015-09-27 Functions: 47 47 100.0 %

          Line data    Source code
       1             : /*
       2             :  * WPA Supplicant - Glue code to setup EAPOL and RSN modules
       3             :  * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  */
       8             : 
       9             : #include "includes.h"
      10             : 
      11             : #include "common.h"
      12             : #include "eapol_supp/eapol_supp_sm.h"
      13             : #include "rsn_supp/wpa.h"
      14             : #include "eloop.h"
      15             : #include "config.h"
      16             : #include "l2_packet/l2_packet.h"
      17             : #include "common/wpa_common.h"
      18             : #include "wpa_supplicant_i.h"
      19             : #include "driver_i.h"
      20             : #include "rsn_supp/pmksa_cache.h"
      21             : #include "sme.h"
      22             : #include "common/ieee802_11_defs.h"
      23             : #include "common/wpa_ctrl.h"
      24             : #include "wpas_glue.h"
      25             : #include "wps_supplicant.h"
      26             : #include "bss.h"
      27             : #include "scan.h"
      28             : #include "notify.h"
      29             : #include "wpas_kay.h"
      30             : 
      31             : 
      32             : #ifndef CONFIG_NO_CONFIG_BLOBS
      33             : #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
      34          10 : static void wpa_supplicant_set_config_blob(void *ctx,
      35             :                                            struct wpa_config_blob *blob)
      36             : {
      37          10 :         struct wpa_supplicant *wpa_s = ctx;
      38          10 :         wpa_config_set_blob(wpa_s->conf, blob);
      39          10 :         if (wpa_s->conf->update_config) {
      40           0 :                 int ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
      41           0 :                 if (ret) {
      42           0 :                         wpa_printf(MSG_DEBUG, "Failed to update config after "
      43             :                                    "blob set");
      44             :                 }
      45             :         }
      46          10 : }
      47             : 
      48             : 
      49             : static const struct wpa_config_blob *
      50          26 : wpa_supplicant_get_config_blob(void *ctx, const char *name)
      51             : {
      52          26 :         struct wpa_supplicant *wpa_s = ctx;
      53          26 :         return wpa_config_get_blob(wpa_s->conf, name);
      54             : }
      55             : #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
      56             : #endif /* CONFIG_NO_CONFIG_BLOBS */
      57             : 
      58             : 
      59             : #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
      60       10024 : static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
      61             :                             const void *data, u16 data_len,
      62             :                             size_t *msg_len, void **data_pos)
      63             : {
      64             :         struct ieee802_1x_hdr *hdr;
      65             : 
      66       10024 :         *msg_len = sizeof(*hdr) + data_len;
      67       10024 :         hdr = os_malloc(*msg_len);
      68       10024 :         if (hdr == NULL)
      69           0 :                 return NULL;
      70             : 
      71       10024 :         hdr->version = wpa_s->conf->eapol_version;
      72       10024 :         hdr->type = type;
      73       10024 :         hdr->length = host_to_be16(data_len);
      74             : 
      75       10024 :         if (data)
      76        6840 :                 os_memcpy(hdr + 1, data, data_len);
      77             :         else
      78        3184 :                 os_memset(hdr + 1, 0, data_len);
      79             : 
      80       10024 :         if (data_pos)
      81        3184 :                 *data_pos = hdr + 1;
      82             : 
      83       10024 :         return (u8 *) hdr;
      84             : }
      85             : 
      86             : 
      87             : /**
      88             :  * wpa_ether_send - Send Ethernet frame
      89             :  * @wpa_s: Pointer to wpa_supplicant data
      90             :  * @dest: Destination MAC address
      91             :  * @proto: Ethertype in host byte order
      92             :  * @buf: Frame payload starting from IEEE 802.1X header
      93             :  * @len: Frame payload length
      94             :  * Returns: >=0 on success, <0 on failure
      95             :  */
      96       10006 : static int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest,
      97             :                           u16 proto, const u8 *buf, size_t len)
      98             : {
      99             : #ifdef CONFIG_TESTING_OPTIONS
     100       10006 :         if (wpa_s->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
     101         342 :                 size_t hex_len = 2 * len + 1;
     102         342 :                 char *hex = os_malloc(hex_len);
     103             : 
     104         342 :                 if (hex == NULL)
     105           0 :                         return -1;
     106         342 :                 wpa_snprintf_hex(hex, hex_len, buf, len);
     107        2052 :                 wpa_msg(wpa_s, MSG_INFO, "EAPOL-TX " MACSTR " %s",
     108        2052 :                         MAC2STR(dest), hex);
     109         342 :                 os_free(hex);
     110         342 :                 return 0;
     111             :         }
     112             : #endif /* CONFIG_TESTING_OPTIONS */
     113             : 
     114        9664 :         if (wpa_s->l2) {
     115        9664 :                 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
     116             :         }
     117             : 
     118           0 :         return -1;
     119             : }
     120             : #endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
     121             : 
     122             : 
     123             : #ifdef IEEE8021X_EAPOL
     124             : 
     125             : /**
     126             :  * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
     127             :  * @ctx: Pointer to wpa_supplicant data (wpa_s)
     128             :  * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
     129             :  * @buf: EAPOL payload (after IEEE 802.1X header)
     130             :  * @len: EAPOL payload length
     131             :  * Returns: >=0 on success, <0 on failure
     132             :  *
     133             :  * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
     134             :  * to the current Authenticator.
     135             :  */
     136        6823 : static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
     137             :                                      size_t len)
     138             : {
     139        6823 :         struct wpa_supplicant *wpa_s = ctx;
     140             :         u8 *msg, *dst, bssid[ETH_ALEN];
     141             :         size_t msglen;
     142             :         int res;
     143             : 
     144             :         /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
     145             :          * extra copy here */
     146             : 
     147       13645 :         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
     148        6822 :             wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
     149             :                 /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
     150             :                  * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
     151             :                  * machines. */
     152           1 :                 wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
     153             :                            "mode (type=%d len=%lu)", type,
     154             :                            (unsigned long) len);
     155           1 :                 return -1;
     156             :         }
     157             : 
     158        6822 :         if (pmksa_cache_get_current(wpa_s->wpa) &&
     159             :             type == IEEE802_1X_TYPE_EAPOL_START) {
     160             :                 /*
     161             :                  * We were trying to use PMKSA caching and sending EAPOL-Start
     162             :                  * would abort that and trigger full EAPOL authentication.
     163             :                  * However, we've already waited for the AP/Authenticator to
     164             :                  * start 4-way handshake or EAP authentication, and apparently
     165             :                  * it has not done so since the startWhen timer has reached zero
     166             :                  * to get the state machine sending EAPOL-Start. This is not
     167             :                  * really supposed to happen, but an interoperability issue with
     168             :                  * a deployed AP has been identified where the connection fails
     169             :                  * due to that AP failing to operate correctly if PMKID is
     170             :                  * included in the Association Request frame. To work around
     171             :                  * this, assume PMKSA caching failed and try to initiate full
     172             :                  * EAP authentication.
     173             :                  */
     174           0 :                 if (!wpa_s->current_ssid ||
     175           0 :                     wpa_s->current_ssid->eap_workaround) {
     176           0 :                         wpa_printf(MSG_DEBUG,
     177             :                                    "RSN: Timeout on waiting for the AP to initiate 4-way handshake for PMKSA caching or EAP authentication - try to force it to start EAP authentication");
     178             :                 } else {
     179           0 :                         wpa_printf(MSG_DEBUG,
     180             :                                    "RSN: PMKSA caching - do not send EAPOL-Start");
     181           0 :                         return -1;
     182             :                 }
     183             :         }
     184             : 
     185        6822 :         if (is_zero_ether_addr(wpa_s->bssid)) {
     186           0 :                 wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
     187             :                            "EAPOL frame");
     188           0 :                 if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
     189           0 :                     !is_zero_ether_addr(bssid)) {
     190           0 :                         dst = bssid;
     191           0 :                         wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
     192             :                                    " from the driver as the EAPOL destination",
     193           0 :                                    MAC2STR(dst));
     194             :                 } else {
     195           0 :                         dst = wpa_s->last_eapol_src;
     196           0 :                         wpa_printf(MSG_DEBUG, "Using the source address of the"
     197             :                                    " last received EAPOL frame " MACSTR " as "
     198             :                                    "the EAPOL destination",
     199           0 :                                    MAC2STR(dst));
     200             :                 }
     201             :         } else {
     202             :                 /* BSSID was already set (from (Re)Assoc event, so use it as
     203             :                  * the EAPOL destination. */
     204        6822 :                 dst = wpa_s->bssid;
     205             :         }
     206             : 
     207        6822 :         msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
     208        6822 :         if (msg == NULL)
     209           0 :                 return -1;
     210             : 
     211        6822 :         wpa_printf(MSG_DEBUG, "TX EAPOL: dst=" MACSTR, MAC2STR(dst));
     212        6822 :         wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
     213        6822 :         res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
     214        6822 :         os_free(msg);
     215        6822 :         return res;
     216             : }
     217             : 
     218             : 
     219             : /**
     220             :  * wpa_eapol_set_wep_key - set WEP key for the driver
     221             :  * @ctx: Pointer to wpa_supplicant data (wpa_s)
     222             :  * @unicast: 1 = individual unicast key, 0 = broadcast key
     223             :  * @keyidx: WEP key index (0..3)
     224             :  * @key: Pointer to key data
     225             :  * @keylen: Key length in bytes
     226             :  * Returns: 0 on success or < 0 on error.
     227             :  */
     228           7 : static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
     229             :                                  const u8 *key, size_t keylen)
     230             : {
     231           7 :         struct wpa_supplicant *wpa_s = ctx;
     232           7 :         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
     233           7 :                 int cipher = (keylen == 5) ? WPA_CIPHER_WEP40 :
     234             :                         WPA_CIPHER_WEP104;
     235           7 :                 if (unicast)
     236           3 :                         wpa_s->pairwise_cipher = cipher;
     237             :                 else
     238           4 :                         wpa_s->group_cipher = cipher;
     239             :         }
     240           7 :         return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
     241             :                                unicast ? wpa_s->bssid : NULL,
     242             :                                keyidx, unicast, NULL, 0, key, keylen);
     243             : }
     244             : 
     245             : 
     246          60 : static void wpa_supplicant_aborted_cached(void *ctx)
     247             : {
     248          60 :         struct wpa_supplicant *wpa_s = ctx;
     249          60 :         wpa_sm_aborted_cached(wpa_s->wpa);
     250          60 : }
     251             : 
     252             : 
     253        2436 : static const char * result_str(enum eapol_supp_result result)
     254             : {
     255        2436 :         switch (result) {
     256             :         case EAPOL_SUPP_RESULT_FAILURE:
     257         651 :                 return "FAILURE";
     258             :         case EAPOL_SUPP_RESULT_SUCCESS:
     259        1780 :                 return "SUCCESS";
     260             :         case EAPOL_SUPP_RESULT_EXPECTED_FAILURE:
     261           5 :                 return "EXPECTED_FAILURE";
     262             :         }
     263           0 :         return "?";
     264             : }
     265             : 
     266             : 
     267        2436 : static void wpa_supplicant_eapol_cb(struct eapol_sm *eapol,
     268             :                                     enum eapol_supp_result result,
     269             :                                     void *ctx)
     270             : {
     271        2436 :         struct wpa_supplicant *wpa_s = ctx;
     272             :         int res, pmk_len;
     273             :         u8 pmk[PMK_LEN];
     274             : 
     275        2436 :         wpa_printf(MSG_DEBUG, "EAPOL authentication completed - result=%s",
     276             :                    result_str(result));
     277             : 
     278        2436 :         if (wpas_wps_eapol_cb(wpa_s) > 0)
     279        2803 :                 return;
     280             : 
     281        2069 :         wpa_s->eap_expected_failure = result ==
     282             :                 EAPOL_SUPP_RESULT_EXPECTED_FAILURE;
     283             : 
     284        2069 :         if (result != EAPOL_SUPP_RESULT_SUCCESS) {
     285             :                 /*
     286             :                  * Make sure we do not get stuck here waiting for long EAPOL
     287             :                  * timeout if the AP does not disconnect in case of
     288             :                  * authentication failure.
     289             :                  */
     290         289 :                 wpa_supplicant_req_auth_timeout(wpa_s, 2, 0);
     291             :         } else {
     292        1780 :                 ieee802_1x_notify_create_actor(wpa_s, wpa_s->last_eapol_src);
     293             :         }
     294             : 
     295        3849 :         if (result != EAPOL_SUPP_RESULT_SUCCESS ||
     296        1780 :             !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
     297        2069 :                 return;
     298             : 
     299           0 :         if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
     300           0 :                 return;
     301             : 
     302           0 :         wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
     303             :                    "handshake");
     304             : 
     305           0 :         pmk_len = PMK_LEN;
     306           0 :         if (wpa_key_mgmt_ft(wpa_s->key_mgmt)) {
     307             : #ifdef CONFIG_IEEE80211R
     308             :                 u8 buf[2 * PMK_LEN];
     309           0 :                 wpa_printf(MSG_DEBUG, "RSN: Use FT XXKey as PMK for "
     310             :                            "driver-based 4-way hs and FT");
     311           0 :                 res = eapol_sm_get_key(eapol, buf, 2 * PMK_LEN);
     312           0 :                 if (res == 0) {
     313           0 :                         os_memcpy(pmk, buf + PMK_LEN, PMK_LEN);
     314           0 :                         os_memset(buf, 0, sizeof(buf));
     315             :                 }
     316             : #else /* CONFIG_IEEE80211R */
     317             :                 res = -1;
     318             : #endif /* CONFIG_IEEE80211R */
     319             :         } else {
     320           0 :                 res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
     321           0 :                 if (res) {
     322             :                         /*
     323             :                          * EAP-LEAP is an exception from other EAP methods: it
     324             :                          * uses only 16-byte PMK.
     325             :                          */
     326           0 :                         res = eapol_sm_get_key(eapol, pmk, 16);
     327           0 :                         pmk_len = 16;
     328             :                 }
     329             :         }
     330             : 
     331           0 :         if (res) {
     332           0 :                 wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
     333             :                            "machines");
     334           0 :                 return;
     335             :         }
     336             : 
     337           0 :         wpa_hexdump_key(MSG_DEBUG, "RSN: Configure PMK for driver-based 4-way "
     338             :                         "handshake", pmk, pmk_len);
     339             : 
     340           0 :         if (wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0, NULL, 0, pmk,
     341             :                             pmk_len)) {
     342           0 :                 wpa_printf(MSG_DEBUG, "Failed to set PMK to the driver");
     343             :         }
     344             : 
     345           0 :         wpa_supplicant_cancel_scan(wpa_s);
     346           0 :         wpa_supplicant_cancel_auth_timeout(wpa_s);
     347           0 :         wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
     348             : 
     349             : }
     350             : 
     351             : 
     352          13 : static void wpa_supplicant_notify_eapol_done(void *ctx)
     353             : {
     354          13 :         struct wpa_supplicant *wpa_s = ctx;
     355          13 :         wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
     356          13 :         if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
     357           0 :                 wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
     358             :         } else {
     359          13 :                 wpa_supplicant_cancel_auth_timeout(wpa_s);
     360          13 :                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
     361             :         }
     362          13 : }
     363             : 
     364             : #endif /* IEEE8021X_EAPOL */
     365             : 
     366             : 
     367             : #ifndef CONFIG_NO_WPA
     368             : 
     369          10 : static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
     370             : {
     371          10 :         int ret = 0;
     372          10 :         struct wpa_bss *curr = NULL, *bss;
     373          10 :         struct wpa_ssid *ssid = wpa_s->current_ssid;
     374             :         const u8 *ie;
     375             : 
     376          13 :         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
     377          13 :                 if (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) != 0)
     378           2 :                         continue;
     379          22 :                 if (ssid == NULL ||
     380          21 :                     ((bss->ssid_len == ssid->ssid_len &&
     381          11 :                       os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) == 0) ||
     382           1 :                      ssid->ssid_len == 0)) {
     383          10 :                         curr = bss;
     384          10 :                         break;
     385             :                 }
     386             :         }
     387             : 
     388          10 :         if (curr) {
     389          10 :                 ie = wpa_bss_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
     390          10 :                 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
     391           0 :                         ret = -1;
     392             : 
     393          10 :                 ie = wpa_bss_get_ie(curr, WLAN_EID_RSN);
     394          10 :                 if (wpa_sm_set_ap_rsn_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
     395           0 :                         ret = -1;
     396             :         } else {
     397           0 :                 ret = -1;
     398             :         }
     399             : 
     400          10 :         return ret;
     401             : }
     402             : 
     403             : 
     404          10 : static int wpa_supplicant_get_beacon_ie(void *ctx)
     405             : {
     406          10 :         struct wpa_supplicant *wpa_s = ctx;
     407          10 :         if (wpa_get_beacon_ie(wpa_s) == 0) {
     408          10 :                 return 0;
     409             :         }
     410             : 
     411             :         /* No WPA/RSN IE found in the cached scan results. Try to get updated
     412             :          * scan results from the driver. */
     413           0 :         if (wpa_supplicant_update_scan_results(wpa_s) < 0)
     414           0 :                 return -1;
     415             : 
     416           0 :         return wpa_get_beacon_ie(wpa_s);
     417             : }
     418             : 
     419             : 
     420        3202 : static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
     421             :                              const void *data, u16 data_len,
     422             :                              size_t *msg_len, void **data_pos)
     423             : {
     424        3202 :         return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
     425             : }
     426             : 
     427             : 
     428        3184 : static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
     429             :                            const u8 *buf, size_t len)
     430             : {
     431        3184 :         return wpa_ether_send(wpa_s, dest, proto, buf, len);
     432             : }
     433             : 
     434             : 
     435        1780 : static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
     436             : {
     437        1780 :         wpa_supplicant_cancel_auth_timeout(wpa_s);
     438        1780 : }
     439             : 
     440             : 
     441        6509 : static void _wpa_supplicant_set_state(void *wpa_s, enum wpa_states state)
     442             : {
     443        6509 :         wpa_supplicant_set_state(wpa_s, state);
     444        6509 : }
     445             : 
     446             : 
     447             : /**
     448             :  * wpa_supplicant_get_state - Get the connection state
     449             :  * @wpa_s: Pointer to wpa_supplicant data
     450             :  * Returns: The current connection state (WPA_*)
     451             :  */
     452        3484 : static enum wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
     453             : {
     454        3484 :         return wpa_s->wpa_state;
     455             : }
     456             : 
     457             : 
     458        3484 : static enum wpa_states _wpa_supplicant_get_state(void *wpa_s)
     459             : {
     460        3484 :         return wpa_supplicant_get_state(wpa_s);
     461             : }
     462             : 
     463             : 
     464          53 : static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
     465             : {
     466          53 :         wpa_supplicant_deauthenticate(wpa_s, reason_code);
     467             :         /* Schedule a scan to make sure we continue looking for networks */
     468          53 :         wpa_supplicant_req_scan(wpa_s, 5, 0);
     469          53 : }
     470             : 
     471             : 
     472        1585 : static void * wpa_supplicant_get_network_ctx(void *wpa_s)
     473             : {
     474        1585 :         return wpa_supplicant_get_ssid(wpa_s);
     475             : }
     476             : 
     477             : 
     478          11 : static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
     479             : {
     480          11 :         struct wpa_supplicant *wpa_s = ctx;
     481          11 :         return wpa_drv_get_bssid(wpa_s, bssid);
     482             : }
     483             : 
     484             : 
     485        3919 : static int wpa_supplicant_set_key(void *_wpa_s, enum wpa_alg alg,
     486             :                                   const u8 *addr, int key_idx, int set_tx,
     487             :                                   const u8 *seq, size_t seq_len,
     488             :                                   const u8 *key, size_t key_len)
     489             : {
     490        3919 :         struct wpa_supplicant *wpa_s = _wpa_s;
     491        3919 :         if (alg == WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
     492             :                 /* Clear the MIC error counter when setting a new PTK. */
     493          26 :                 wpa_s->mic_errors_seen = 0;
     494             :         }
     495             : #ifdef CONFIG_TESTING_GET_GTK
     496             :         if (key_idx > 0 && addr && is_broadcast_ether_addr(addr) &&
     497             :             alg != WPA_ALG_NONE && key_len <= sizeof(wpa_s->last_gtk)) {
     498             :                 os_memcpy(wpa_s->last_gtk, key, key_len);
     499             :                 wpa_s->last_gtk_len = key_len;
     500             :         }
     501             : #endif /* CONFIG_TESTING_GET_GTK */
     502        3919 :         return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
     503             :                                key, key_len);
     504             : }
     505             : 
     506             : 
     507        3295 : static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
     508             :                                              int protection_type,
     509             :                                              int key_type)
     510             : {
     511        3295 :         return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
     512             :                                           key_type);
     513             : }
     514             : 
     515             : 
     516         781 : static int wpa_supplicant_add_pmkid(void *wpa_s,
     517             :                                     const u8 *bssid, const u8 *pmkid)
     518             : {
     519         781 :         return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
     520             : }
     521             : 
     522             : 
     523         781 : static int wpa_supplicant_remove_pmkid(void *wpa_s,
     524             :                                        const u8 *bssid, const u8 *pmkid)
     525             : {
     526         781 :         return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
     527             : }
     528             : 
     529             : 
     530             : #ifdef CONFIG_IEEE80211R
     531         889 : static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
     532             :                                         const u8 *ies, size_t ies_len)
     533             : {
     534         889 :         struct wpa_supplicant *wpa_s = ctx;
     535         889 :         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
     536         889 :                 return sme_update_ft_ies(wpa_s, md, ies, ies_len);
     537           0 :         return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
     538             : }
     539             : 
     540             : 
     541         116 : static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
     542             :                                          const u8 *target_ap,
     543             :                                          const u8 *ies, size_t ies_len)
     544             : {
     545         116 :         struct wpa_supplicant *wpa_s = ctx;
     546             :         int ret;
     547             :         u8 *data, *pos;
     548             :         size_t data_len;
     549             : 
     550         116 :         if (action != 1) {
     551           0 :                 wpa_printf(MSG_ERROR, "Unsupported send_ft_action action %d",
     552             :                            action);
     553           0 :                 return -1;
     554             :         }
     555             : 
     556             :         /*
     557             :          * Action frame payload:
     558             :          * Category[1] = 6 (Fast BSS Transition)
     559             :          * Action[1] = 1 (Fast BSS Transition Request)
     560             :          * STA Address
     561             :          * Target AP Address
     562             :          * FT IEs
     563             :          */
     564             : 
     565         116 :         data_len = 2 + 2 * ETH_ALEN + ies_len;
     566         116 :         data = os_malloc(data_len);
     567         116 :         if (data == NULL)
     568           0 :                 return -1;
     569         116 :         pos = data;
     570         116 :         *pos++ = 0x06; /* FT Action category */
     571         116 :         *pos++ = action;
     572         116 :         os_memcpy(pos, wpa_s->own_addr, ETH_ALEN);
     573         116 :         pos += ETH_ALEN;
     574         116 :         os_memcpy(pos, target_ap, ETH_ALEN);
     575         116 :         pos += ETH_ALEN;
     576         116 :         os_memcpy(pos, ies, ies_len);
     577             : 
     578         116 :         ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
     579         116 :                                   wpa_s->bssid, wpa_s->own_addr, wpa_s->bssid,
     580             :                                   data, data_len, 0);
     581         116 :         os_free(data);
     582             : 
     583         116 :         return ret;
     584             : }
     585             : 
     586             : 
     587         226 : static int wpa_supplicant_mark_authenticated(void *ctx, const u8 *target_ap)
     588             : {
     589         226 :         struct wpa_supplicant *wpa_s = ctx;
     590             :         struct wpa_driver_auth_params params;
     591             :         struct wpa_bss *bss;
     592             : 
     593         226 :         bss = wpa_bss_get_bssid(wpa_s, target_ap);
     594         226 :         if (bss == NULL)
     595           0 :                 return -1;
     596             : 
     597         226 :         os_memset(&params, 0, sizeof(params));
     598         226 :         params.bssid = target_ap;
     599         226 :         params.freq = bss->freq;
     600         226 :         params.ssid = bss->ssid;
     601         226 :         params.ssid_len = bss->ssid_len;
     602         226 :         params.auth_alg = WPA_AUTH_ALG_FT;
     603         226 :         params.local_state_change = 1;
     604         226 :         return wpa_drv_authenticate(wpa_s, &params);
     605             : }
     606             : #endif /* CONFIG_IEEE80211R */
     607             : 
     608             : 
     609             : #ifdef CONFIG_TDLS
     610             : 
     611         576 : static int wpa_supplicant_tdls_get_capa(void *ctx, int *tdls_supported,
     612             :                                         int *tdls_ext_setup,
     613             :                                         int *tdls_chan_switch)
     614             : {
     615         576 :         struct wpa_supplicant *wpa_s = ctx;
     616             : 
     617         576 :         *tdls_supported = 0;
     618         576 :         *tdls_ext_setup = 0;
     619         576 :         *tdls_chan_switch = 0;
     620             : 
     621         576 :         if (!wpa_s->drv_capa_known)
     622          44 :                 return -1;
     623             : 
     624         532 :         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)
     625         523 :                 *tdls_supported = 1;
     626             : 
     627         532 :         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP)
     628         523 :                 *tdls_ext_setup = 1;
     629             : 
     630         532 :         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH)
     631         523 :                 *tdls_chan_switch = 1;
     632             : 
     633         532 :         return 0;
     634             : }
     635             : 
     636             : 
     637         132 : static int wpa_supplicant_send_tdls_mgmt(void *ctx, const u8 *dst,
     638             :                                          u8 action_code, u8 dialog_token,
     639             :                                          u16 status_code, u32 peer_capab,
     640             :                                          int initiator, const u8 *buf,
     641             :                                          size_t len)
     642             : {
     643         132 :         struct wpa_supplicant *wpa_s = ctx;
     644         132 :         return wpa_drv_send_tdls_mgmt(wpa_s, dst, action_code, dialog_token,
     645             :                                       status_code, peer_capab, initiator, buf,
     646             :                                       len);
     647             : }
     648             : 
     649             : 
     650         112 : static int wpa_supplicant_tdls_oper(void *ctx, int oper, const u8 *peer)
     651             : {
     652         112 :         struct wpa_supplicant *wpa_s = ctx;
     653         112 :         return wpa_drv_tdls_oper(wpa_s, oper, peer);
     654             : }
     655             : 
     656             : 
     657         111 : static int wpa_supplicant_tdls_peer_addset(
     658             :         void *ctx, const u8 *peer, int add, u16 aid, u16 capability,
     659             :         const u8 *supp_rates, size_t supp_rates_len,
     660             :         const struct ieee80211_ht_capabilities *ht_capab,
     661             :         const struct ieee80211_vht_capabilities *vht_capab,
     662             :         u8 qosinfo, int wmm, const u8 *ext_capab, size_t ext_capab_len,
     663             :         const u8 *supp_channels, size_t supp_channels_len,
     664             :         const u8 *supp_oper_classes, size_t supp_oper_classes_len)
     665             : {
     666         111 :         struct wpa_supplicant *wpa_s = ctx;
     667             :         struct hostapd_sta_add_params params;
     668             : 
     669         111 :         os_memset(&params, 0, sizeof(params));
     670             : 
     671         111 :         params.addr = peer;
     672         111 :         params.aid = aid;
     673         111 :         params.capability = capability;
     674         111 :         params.flags = WPA_STA_TDLS_PEER | WPA_STA_AUTHORIZED;
     675             : 
     676             :         /*
     677             :          * Don't rely only on qosinfo for WMM capability. It may be 0 even when
     678             :          * present. Allow the WMM IE to also indicate QoS support.
     679             :          */
     680         111 :         if (wmm || qosinfo)
     681          79 :                 params.flags |= WPA_STA_WMM;
     682             : 
     683         111 :         params.ht_capabilities = ht_capab;
     684         111 :         params.vht_capabilities = vht_capab;
     685         111 :         params.qosinfo = qosinfo;
     686         111 :         params.listen_interval = 0;
     687         111 :         params.supp_rates = supp_rates;
     688         111 :         params.supp_rates_len = supp_rates_len;
     689         111 :         params.set = !add;
     690         111 :         params.ext_capab = ext_capab;
     691         111 :         params.ext_capab_len = ext_capab_len;
     692         111 :         params.supp_channels = supp_channels;
     693         111 :         params.supp_channels_len = supp_channels_len;
     694         111 :         params.supp_oper_classes = supp_oper_classes;
     695         111 :         params.supp_oper_classes_len = supp_oper_classes_len;
     696             : 
     697         111 :         return wpa_drv_sta_add(wpa_s, &params);
     698             : }
     699             : 
     700             : 
     701           1 : static int wpa_supplicant_tdls_enable_channel_switch(
     702             :         void *ctx, const u8 *addr, u8 oper_class,
     703             :         const struct hostapd_freq_params *params)
     704             : {
     705           1 :         struct wpa_supplicant *wpa_s = ctx;
     706             : 
     707           1 :         return wpa_drv_tdls_enable_channel_switch(wpa_s, addr, oper_class,
     708             :                                                   params);
     709             : }
     710             : 
     711             : 
     712           1 : static int wpa_supplicant_tdls_disable_channel_switch(void *ctx, const u8 *addr)
     713             : {
     714           1 :         struct wpa_supplicant *wpa_s = ctx;
     715             : 
     716           1 :         return wpa_drv_tdls_disable_channel_switch(wpa_s, addr);
     717             : }
     718             : 
     719             : #endif /* CONFIG_TDLS */
     720             : 
     721             : #endif /* CONFIG_NO_WPA */
     722             : 
     723             : 
     724          50 : enum wpa_ctrl_req_type wpa_supplicant_ctrl_req_from_string(const char *field)
     725             : {
     726          50 :         if (os_strcmp(field, "IDENTITY") == 0)
     727           4 :                 return WPA_CTRL_REQ_EAP_IDENTITY;
     728          46 :         else if (os_strcmp(field, "PASSWORD") == 0)
     729           8 :                 return WPA_CTRL_REQ_EAP_PASSWORD;
     730          38 :         else if (os_strcmp(field, "NEW_PASSWORD") == 0)
     731           2 :                 return WPA_CTRL_REQ_EAP_NEW_PASSWORD;
     732          36 :         else if (os_strcmp(field, "PIN") == 0)
     733           2 :                 return WPA_CTRL_REQ_EAP_PIN;
     734          34 :         else if (os_strcmp(field, "OTP") == 0)
     735           4 :                 return WPA_CTRL_REQ_EAP_OTP;
     736          30 :         else if (os_strcmp(field, "PASSPHRASE") == 0)
     737           3 :                 return WPA_CTRL_REQ_EAP_PASSPHRASE;
     738          27 :         else if (os_strcmp(field, "SIM") == 0)
     739          23 :                 return WPA_CTRL_REQ_SIM;
     740           4 :         else if (os_strcmp(field, "PSK_PASSPHRASE") == 0)
     741           3 :                 return WPA_CTRL_REQ_PSK_PASSPHRASE;
     742           1 :         return WPA_CTRL_REQ_UNKNOWN;
     743             : }
     744             : 
     745             : 
     746          79 : const char * wpa_supplicant_ctrl_req_to_string(enum wpa_ctrl_req_type field,
     747             :                                                const char *default_txt,
     748             :                                                const char **txt)
     749             : {
     750          79 :         const char *ret = NULL;
     751             : 
     752          79 :         *txt = default_txt;
     753             : 
     754          79 :         switch (field) {
     755             :         case WPA_CTRL_REQ_EAP_IDENTITY:
     756           4 :                 *txt = "Identity";
     757           4 :                 ret = "IDENTITY";
     758           4 :                 break;
     759             :         case WPA_CTRL_REQ_EAP_PASSWORD:
     760          14 :                 *txt = "Password";
     761          14 :                 ret = "PASSWORD";
     762          14 :                 break;
     763             :         case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
     764           0 :                 *txt = "New Password";
     765           0 :                 ret = "NEW_PASSWORD";
     766           0 :                 break;
     767             :         case WPA_CTRL_REQ_EAP_PIN:
     768           0 :                 *txt = "PIN";
     769           0 :                 ret = "PIN";
     770           0 :                 break;
     771             :         case WPA_CTRL_REQ_EAP_OTP:
     772           4 :                 ret = "OTP";
     773           4 :                 break;
     774             :         case WPA_CTRL_REQ_EAP_PASSPHRASE:
     775          10 :                 *txt = "Private key passphrase";
     776          10 :                 ret = "PASSPHRASE";
     777          10 :                 break;
     778             :         case WPA_CTRL_REQ_SIM:
     779          42 :                 ret = "SIM";
     780          42 :                 break;
     781             :         case WPA_CTRL_REQ_PSK_PASSPHRASE:
     782           5 :                 *txt = "PSK or passphrase";
     783           5 :                 ret = "PSK_PASSPHRASE";
     784           5 :                 break;
     785             :         default:
     786           0 :                 break;
     787             :         }
     788             : 
     789             :         /* txt needs to be something */
     790          79 :         if (*txt == NULL) {
     791           0 :                 wpa_printf(MSG_WARNING, "No message for request %d", field);
     792           0 :                 ret = NULL;
     793             :         }
     794             : 
     795          79 :         return ret;
     796             : }
     797             : 
     798             : 
     799          40 : void wpas_send_ctrl_req(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
     800             :                         const char *field_name, const char *txt)
     801             : {
     802             :         char *buf;
     803             :         size_t buflen;
     804             :         int len;
     805             : 
     806          40 :         buflen = 100 + os_strlen(txt) + ssid->ssid_len;
     807          40 :         buf = os_malloc(buflen);
     808          40 :         if (buf == NULL)
     809           0 :                 return;
     810          40 :         len = os_snprintf(buf, buflen, "%s-%d:%s needed for SSID ",
     811             :                           field_name, ssid->id, txt);
     812          40 :         if (os_snprintf_error(buflen, len)) {
     813           0 :                 os_free(buf);
     814           0 :                 return;
     815             :         }
     816          40 :         if (ssid->ssid && buflen > len + ssid->ssid_len) {
     817          40 :                 os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
     818          40 :                 len += ssid->ssid_len;
     819          40 :                 buf[len] = '\0';
     820             :         }
     821          40 :         buf[buflen - 1] = '\0';
     822          40 :         wpa_msg(wpa_s, MSG_INFO, WPA_CTRL_REQ "%s", buf);
     823          40 :         os_free(buf);
     824             : }
     825             : 
     826             : 
     827             : #ifdef IEEE8021X_EAPOL
     828             : #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
     829          37 : static void wpa_supplicant_eap_param_needed(void *ctx,
     830             :                                             enum wpa_ctrl_req_type field,
     831             :                                             const char *default_txt)
     832             : {
     833          37 :         struct wpa_supplicant *wpa_s = ctx;
     834          37 :         struct wpa_ssid *ssid = wpa_s->current_ssid;
     835          37 :         const char *field_name, *txt = NULL;
     836             : 
     837          37 :         if (ssid == NULL)
     838           0 :                 return;
     839             : 
     840          37 :         wpas_notify_network_request(wpa_s, ssid, field, default_txt);
     841             : 
     842          37 :         field_name = wpa_supplicant_ctrl_req_to_string(field, default_txt,
     843             :                                                        &txt);
     844          37 :         if (field_name == NULL) {
     845           0 :                 wpa_printf(MSG_WARNING, "Unhandled EAP param %d needed",
     846             :                            field);
     847           0 :                 return;
     848             :         }
     849             : 
     850          37 :         wpas_notify_eap_status(wpa_s, "eap parameter needed", field_name);
     851             : 
     852          37 :         wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
     853             : }
     854             : #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
     855             : #define wpa_supplicant_eap_param_needed NULL
     856             : #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
     857             : 
     858             : 
     859             : #ifdef CONFIG_EAP_PROXY
     860             : static void wpa_supplicant_eap_proxy_cb(void *ctx)
     861             : {
     862             :         struct wpa_supplicant *wpa_s = ctx;
     863             :         size_t len;
     864             : 
     865             :         wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
     866             :                                                      wpa_s->imsi, &len);
     867             :         if (wpa_s->mnc_len > 0) {
     868             :                 wpa_s->imsi[len] = '\0';
     869             :                 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
     870             :                            wpa_s->imsi, wpa_s->mnc_len);
     871             :         } else {
     872             :                 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
     873             :         }
     874             : }
     875             : #endif /* CONFIG_EAP_PROXY */
     876             : 
     877             : 
     878        6969 : static void wpa_supplicant_port_cb(void *ctx, int authorized)
     879             : {
     880        6969 :         struct wpa_supplicant *wpa_s = ctx;
     881             : #ifdef CONFIG_AP
     882        6969 :         if (wpa_s->ap_iface) {
     883           0 :                 wpa_printf(MSG_DEBUG, "AP mode active - skip EAPOL Supplicant "
     884             :                            "port status: %s",
     885             :                            authorized ? "Authorized" : "Unauthorized");
     886        6969 :                 return;
     887             :         }
     888             : #endif /* CONFIG_AP */
     889        6969 :         wpa_printf(MSG_DEBUG, "EAPOL: Supplicant port status: %s",
     890             :                    authorized ? "Authorized" : "Unauthorized");
     891        6969 :         wpa_drv_set_supp_port(wpa_s, authorized);
     892             : }
     893             : 
     894             : 
     895         582 : static void wpa_supplicant_cert_cb(void *ctx, int depth, const char *subject,
     896             :                                    const char *altsubject[], int num_altsubject,
     897             :                                    const char *cert_hash,
     898             :                                    const struct wpabuf *cert)
     899             : {
     900         582 :         struct wpa_supplicant *wpa_s = ctx;
     901             : 
     902         582 :         wpas_notify_certification(wpa_s, depth, subject, altsubject,
     903             :                                   num_altsubject, cert_hash, cert);
     904         582 : }
     905             : 
     906             : 
     907        5342 : static void wpa_supplicant_status_cb(void *ctx, const char *status,
     908             :                                      const char *parameter)
     909             : {
     910        5342 :         struct wpa_supplicant *wpa_s = ctx;
     911             : 
     912        5342 :         wpas_notify_eap_status(wpa_s, status, parameter);
     913        5342 : }
     914             : 
     915             : 
     916          52 : static void wpa_supplicant_set_anon_id(void *ctx, const u8 *id, size_t len)
     917             : {
     918          52 :         struct wpa_supplicant *wpa_s = ctx;
     919             :         char *str;
     920             :         int res;
     921             : 
     922          52 :         wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
     923             :                           id, len);
     924             : 
     925          52 :         if (wpa_s->current_ssid == NULL)
     926           0 :                 return;
     927             : 
     928          52 :         if (id == NULL) {
     929           5 :                 if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
     930             :                                    "NULL", 0) < 0)
     931           0 :                         return;
     932             :         } else {
     933          47 :                 str = os_malloc(len * 2 + 1);
     934          47 :                 if (str == NULL)
     935           0 :                         return;
     936          47 :                 wpa_snprintf_hex(str, len * 2 + 1, id, len);
     937          47 :                 res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
     938             :                                      str, 0);
     939          47 :                 os_free(str);
     940          47 :                 if (res < 0)
     941           0 :                         return;
     942             :         }
     943             : 
     944          52 :         if (wpa_s->conf->update_config) {
     945           0 :                 res = wpa_config_write(wpa_s->confname, wpa_s->conf);
     946           0 :                 if (res) {
     947           0 :                         wpa_printf(MSG_DEBUG, "Failed to update config after "
     948             :                                    "anonymous_id update");
     949             :                 }
     950             :         }
     951             : }
     952             : #endif /* IEEE8021X_EAPOL */
     953             : 
     954             : 
     955         587 : int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
     956             : {
     957             : #ifdef IEEE8021X_EAPOL
     958             :         struct eapol_ctx *ctx;
     959         587 :         ctx = os_zalloc(sizeof(*ctx));
     960         587 :         if (ctx == NULL) {
     961           1 :                 wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
     962           1 :                 return -1;
     963             :         }
     964             : 
     965         586 :         ctx->ctx = wpa_s;
     966         586 :         ctx->msg_ctx = wpa_s;
     967         586 :         ctx->eapol_send_ctx = wpa_s;
     968         586 :         ctx->preauth = 0;
     969         586 :         ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
     970         586 :         ctx->eapol_send = wpa_supplicant_eapol_send;
     971         586 :         ctx->set_wep_key = wpa_eapol_set_wep_key;
     972             : #ifndef CONFIG_NO_CONFIG_BLOBS
     973         586 :         ctx->set_config_blob = wpa_supplicant_set_config_blob;
     974         586 :         ctx->get_config_blob = wpa_supplicant_get_config_blob;
     975             : #endif /* CONFIG_NO_CONFIG_BLOBS */
     976         586 :         ctx->aborted_cached = wpa_supplicant_aborted_cached;
     977         586 :         ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
     978         586 :         ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
     979         586 :         ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
     980         586 :         ctx->openssl_ciphers = wpa_s->conf->openssl_ciphers;
     981         586 :         ctx->wps = wpa_s->wps;
     982         586 :         ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
     983             : #ifdef CONFIG_EAP_PROXY
     984             :         ctx->eap_proxy_cb = wpa_supplicant_eap_proxy_cb;
     985             : #endif /* CONFIG_EAP_PROXY */
     986         586 :         ctx->port_cb = wpa_supplicant_port_cb;
     987         586 :         ctx->cb = wpa_supplicant_eapol_cb;
     988         586 :         ctx->cert_cb = wpa_supplicant_cert_cb;
     989         586 :         ctx->cert_in_cb = wpa_s->conf->cert_in_cb;
     990         586 :         ctx->status_cb = wpa_supplicant_status_cb;
     991         586 :         ctx->set_anon_id = wpa_supplicant_set_anon_id;
     992         586 :         ctx->cb_ctx = wpa_s;
     993         586 :         wpa_s->eapol = eapol_sm_init(ctx);
     994         586 :         if (wpa_s->eapol == NULL) {
     995           4 :                 os_free(ctx);
     996           4 :                 wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
     997             :                            "machines.");
     998           4 :                 return -1;
     999             :         }
    1000             : #endif /* IEEE8021X_EAPOL */
    1001             : 
    1002         582 :         return 0;
    1003             : }
    1004             : 
    1005             : 
    1006             : #ifndef CONFIG_NO_WPA
    1007        1557 : static void wpa_supplicant_set_rekey_offload(void *ctx,
    1008             :                                              const u8 *kek, size_t kek_len,
    1009             :                                              const u8 *kck, size_t kck_len,
    1010             :                                              const u8 *replay_ctr)
    1011             : {
    1012        1557 :         struct wpa_supplicant *wpa_s = ctx;
    1013             : 
    1014        1557 :         wpa_drv_set_rekey_info(wpa_s, kek, kek_len, kck, kck_len, replay_ctr);
    1015        1557 : }
    1016             : #endif /* CONFIG_NO_WPA */
    1017             : 
    1018             : 
    1019           8 : static int wpa_supplicant_key_mgmt_set_pmk(void *ctx, const u8 *pmk,
    1020             :                                            size_t pmk_len)
    1021             : {
    1022           8 :         struct wpa_supplicant *wpa_s = ctx;
    1023             : 
    1024          16 :         if (wpa_s->conf->key_mgmt_offload &&
    1025           8 :             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
    1026           0 :                 return wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0,
    1027             :                                        NULL, 0, pmk, pmk_len);
    1028             :         else
    1029           8 :                 return 0;
    1030             : }
    1031             : 
    1032             : 
    1033         590 : int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
    1034             : {
    1035             : #ifndef CONFIG_NO_WPA
    1036             :         struct wpa_sm_ctx *ctx;
    1037         590 :         ctx = os_zalloc(sizeof(*ctx));
    1038         590 :         if (ctx == NULL) {
    1039           1 :                 wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
    1040           1 :                 return -1;
    1041             :         }
    1042             : 
    1043         589 :         ctx->ctx = wpa_s;
    1044         589 :         ctx->msg_ctx = wpa_s;
    1045         589 :         ctx->set_state = _wpa_supplicant_set_state;
    1046         589 :         ctx->get_state = _wpa_supplicant_get_state;
    1047         589 :         ctx->deauthenticate = _wpa_supplicant_deauthenticate;
    1048         589 :         ctx->set_key = wpa_supplicant_set_key;
    1049         589 :         ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
    1050         589 :         ctx->get_bssid = wpa_supplicant_get_bssid;
    1051         589 :         ctx->ether_send = _wpa_ether_send;
    1052         589 :         ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
    1053         589 :         ctx->alloc_eapol = _wpa_alloc_eapol;
    1054         589 :         ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
    1055         589 :         ctx->add_pmkid = wpa_supplicant_add_pmkid;
    1056         589 :         ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
    1057             : #ifndef CONFIG_NO_CONFIG_BLOBS
    1058         589 :         ctx->set_config_blob = wpa_supplicant_set_config_blob;
    1059         589 :         ctx->get_config_blob = wpa_supplicant_get_config_blob;
    1060             : #endif /* CONFIG_NO_CONFIG_BLOBS */
    1061         589 :         ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
    1062             : #ifdef CONFIG_IEEE80211R
    1063         589 :         ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
    1064         589 :         ctx->send_ft_action = wpa_supplicant_send_ft_action;
    1065         589 :         ctx->mark_authenticated = wpa_supplicant_mark_authenticated;
    1066             : #endif /* CONFIG_IEEE80211R */
    1067             : #ifdef CONFIG_TDLS
    1068         589 :         ctx->tdls_get_capa = wpa_supplicant_tdls_get_capa;
    1069         589 :         ctx->send_tdls_mgmt = wpa_supplicant_send_tdls_mgmt;
    1070         589 :         ctx->tdls_oper = wpa_supplicant_tdls_oper;
    1071         589 :         ctx->tdls_peer_addset = wpa_supplicant_tdls_peer_addset;
    1072         589 :         ctx->tdls_enable_channel_switch =
    1073             :                 wpa_supplicant_tdls_enable_channel_switch;
    1074         589 :         ctx->tdls_disable_channel_switch =
    1075             :                 wpa_supplicant_tdls_disable_channel_switch;
    1076             : #endif /* CONFIG_TDLS */
    1077         589 :         ctx->set_rekey_offload = wpa_supplicant_set_rekey_offload;
    1078         589 :         ctx->key_mgmt_set_pmk = wpa_supplicant_key_mgmt_set_pmk;
    1079             : 
    1080         589 :         wpa_s->wpa = wpa_sm_init(ctx);
    1081         589 :         if (wpa_s->wpa == NULL) {
    1082           2 :                 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
    1083             :                            "machine");
    1084           2 :                 os_free(ctx);
    1085           2 :                 return -1;
    1086             :         }
    1087             : #endif /* CONFIG_NO_WPA */
    1088             : 
    1089         587 :         return 0;
    1090             : }
    1091             : 
    1092             : 
    1093        3434 : void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
    1094             :                                         struct wpa_ssid *ssid)
    1095             : {
    1096             :         struct rsn_supp_config conf;
    1097        3434 :         if (ssid) {
    1098        3434 :                 os_memset(&conf, 0, sizeof(conf));
    1099        3434 :                 conf.network_ctx = ssid;
    1100        3434 :                 conf.peerkey_enabled = ssid->peerkey;
    1101        3434 :                 conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
    1102             : #ifdef IEEE8021X_EAPOL
    1103        6868 :                 conf.proactive_key_caching = ssid->proactive_key_caching < 0 ?
    1104        3434 :                         wpa_s->conf->okc : ssid->proactive_key_caching;
    1105        3434 :                 conf.eap_workaround = ssid->eap_workaround;
    1106        3434 :                 conf.eap_conf_ctx = &ssid->eap;
    1107             : #endif /* IEEE8021X_EAPOL */
    1108        3434 :                 conf.ssid = ssid->ssid;
    1109        3434 :                 conf.ssid_len = ssid->ssid_len;
    1110        3434 :                 conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
    1111             : #ifdef CONFIG_P2P
    1112        3830 :                 if (ssid->p2p_group && wpa_s->current_bss &&
    1113         396 :                     !wpa_s->p2p_disable_ip_addr_req) {
    1114             :                         struct wpabuf *p2p;
    1115         394 :                         p2p = wpa_bss_get_vendor_ie_multi(wpa_s->current_bss,
    1116             :                                                           P2P_IE_VENDOR_TYPE);
    1117         394 :                         if (p2p) {
    1118             :                                 u8 group_capab;
    1119         394 :                                 group_capab = p2p_get_group_capab(p2p);
    1120         394 :                                 if (group_capab &
    1121             :                                     P2P_GROUP_CAPAB_IP_ADDR_ALLOCATION)
    1122         394 :                                         conf.p2p = 1;
    1123         394 :                                 wpabuf_free(p2p);
    1124             :                         }
    1125             :                 }
    1126             : #endif /* CONFIG_P2P */
    1127             :         }
    1128        3434 :         wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);
    1129        3434 : }

Generated by: LCOV version 1.10