LCOV - code coverage report
Current view: top level - src/ap - wpa_auth.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1443382998 Lines: 1495 1722 86.8 %
Date: 2015-09-27 Functions: 109 112 97.3 %

          Line data    Source code
       1             : /*
       2             :  * IEEE 802.11 RSN / WPA Authenticator
       3             :  * Copyright (c) 2004-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 "utils/includes.h"
      10             : 
      11             : #include "utils/common.h"
      12             : #include "utils/eloop.h"
      13             : #include "utils/state_machine.h"
      14             : #include "utils/bitfield.h"
      15             : #include "common/ieee802_11_defs.h"
      16             : #include "crypto/aes_wrap.h"
      17             : #include "crypto/crypto.h"
      18             : #include "crypto/sha1.h"
      19             : #include "crypto/sha256.h"
      20             : #include "crypto/random.h"
      21             : #include "eapol_auth/eapol_auth_sm.h"
      22             : #include "ap_config.h"
      23             : #include "ieee802_11.h"
      24             : #include "wpa_auth.h"
      25             : #include "pmksa_cache_auth.h"
      26             : #include "wpa_auth_i.h"
      27             : #include "wpa_auth_ie.h"
      28             : 
      29             : #define STATE_MACHINE_DATA struct wpa_state_machine
      30             : #define STATE_MACHINE_DEBUG_PREFIX "WPA"
      31             : #define STATE_MACHINE_ADDR sm->addr
      32             : 
      33             : 
      34             : static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
      35             : static int wpa_sm_step(struct wpa_state_machine *sm);
      36             : static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
      37             :                               size_t data_len);
      38             : static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
      39             : static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
      40             :                               struct wpa_group *group);
      41             : static void wpa_request_new_ptk(struct wpa_state_machine *sm);
      42             : static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
      43             :                           struct wpa_group *group);
      44             : static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
      45             :                                        struct wpa_group *group);
      46             : static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
      47             :                           const u8 *pmk, struct wpa_ptk *ptk);
      48             : static void wpa_group_free(struct wpa_authenticator *wpa_auth,
      49             :                            struct wpa_group *group);
      50             : static void wpa_group_get(struct wpa_authenticator *wpa_auth,
      51             :                           struct wpa_group *group);
      52             : static void wpa_group_put(struct wpa_authenticator *wpa_auth,
      53             :                           struct wpa_group *group);
      54             : 
      55             : static const u32 dot11RSNAConfigGroupUpdateCount = 4;
      56             : static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
      57             : static const u32 eapol_key_timeout_first = 100; /* ms */
      58             : static const u32 eapol_key_timeout_subseq = 1000; /* ms */
      59             : static const u32 eapol_key_timeout_first_group = 500; /* ms */
      60             : 
      61             : /* TODO: make these configurable */
      62             : static const int dot11RSNAConfigPMKLifetime = 43200;
      63             : static const int dot11RSNAConfigPMKReauthThreshold = 70;
      64             : static const int dot11RSNAConfigSATimeout = 60;
      65             : 
      66             : 
      67           4 : static inline int wpa_auth_mic_failure_report(
      68             :         struct wpa_authenticator *wpa_auth, const u8 *addr)
      69             : {
      70           4 :         if (wpa_auth->cb.mic_failure_report)
      71           4 :                 return wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
      72           0 :         return 0;
      73             : }
      74             : 
      75             : 
      76          17 : static inline void wpa_auth_psk_failure_report(
      77             :         struct wpa_authenticator *wpa_auth, const u8 *addr)
      78             : {
      79          17 :         if (wpa_auth->cb.psk_failure_report)
      80          17 :                 wpa_auth->cb.psk_failure_report(wpa_auth->cb.ctx, addr);
      81          17 : }
      82             : 
      83             : 
      84       21799 : static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
      85             :                                       const u8 *addr, wpa_eapol_variable var,
      86             :                                       int value)
      87             : {
      88       21799 :         if (wpa_auth->cb.set_eapol)
      89       21799 :                 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
      90       21799 : }
      91             : 
      92             : 
      93       15428 : static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
      94             :                                      const u8 *addr, wpa_eapol_variable var)
      95             : {
      96       15428 :         if (wpa_auth->cb.get_eapol == NULL)
      97           0 :                 return -1;
      98       15428 :         return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
      99             : }
     100             : 
     101             : 
     102        2523 : static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
     103             :                                           const u8 *addr,
     104             :                                           const u8 *p2p_dev_addr,
     105             :                                           const u8 *prev_psk)
     106             : {
     107        2523 :         if (wpa_auth->cb.get_psk == NULL)
     108           0 :                 return NULL;
     109        2523 :         return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, p2p_dev_addr,
     110             :                                     prev_psk);
     111             : }
     112             : 
     113             : 
     114         734 : static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
     115             :                                    const u8 *addr, u8 *msk, size_t *len)
     116             : {
     117         734 :         if (wpa_auth->cb.get_msk == NULL)
     118           0 :                 return -1;
     119         734 :         return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
     120             : }
     121             : 
     122             : 
     123       15452 : static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
     124             :                                    int vlan_id,
     125             :                                    enum wpa_alg alg, const u8 *addr, int idx,
     126             :                                    u8 *key, size_t key_len)
     127             : {
     128       15452 :         if (wpa_auth->cb.set_key == NULL)
     129           0 :                 return -1;
     130       15452 :         return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
     131             :                                     key, key_len);
     132             : }
     133             : 
     134             : 
     135        1655 : static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
     136             :                                       const u8 *addr, int idx, u8 *seq)
     137             : {
     138        1655 :         if (wpa_auth->cb.get_seqnum == NULL)
     139          12 :                 return -1;
     140        1643 :         return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
     141             : }
     142             : 
     143             : 
     144             : static inline int
     145        3228 : wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
     146             :                     const u8 *data, size_t data_len, int encrypt)
     147             : {
     148        3228 :         if (wpa_auth->cb.send_eapol == NULL)
     149           0 :                 return -1;
     150        3228 :         return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
     151             :                                        encrypt);
     152             : }
     153             : 
     154             : 
     155             : #ifdef CONFIG_MESH
     156          14 : static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
     157             :                                       const u8 *addr)
     158             : {
     159          14 :         if (wpa_auth->cb.start_ampe == NULL)
     160           6 :                 return -1;
     161           8 :         return wpa_auth->cb.start_ampe(wpa_auth->cb.ctx, addr);
     162             : }
     163             : #endif /* CONFIG_MESH */
     164             : 
     165             : 
     166         408 : int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
     167             :                           int (*cb)(struct wpa_state_machine *sm, void *ctx),
     168             :                           void *cb_ctx)
     169             : {
     170         408 :         if (wpa_auth->cb.for_each_sta == NULL)
     171           0 :                 return 0;
     172         408 :         return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
     173             : }
     174             : 
     175             : 
     176           6 : int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
     177             :                            int (*cb)(struct wpa_authenticator *a, void *ctx),
     178             :                            void *cb_ctx)
     179             : {
     180           6 :         if (wpa_auth->cb.for_each_auth == NULL)
     181           0 :                 return 0;
     182           6 :         return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
     183             : }
     184             : 
     185             : 
     186       14508 : void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
     187             :                      logger_level level, const char *txt)
     188             : {
     189       14508 :         if (wpa_auth->cb.logger == NULL)
     190       14508 :                 return;
     191       14508 :         wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
     192             : }
     193             : 
     194             : 
     195        9117 : void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
     196             :                       logger_level level, const char *fmt, ...)
     197             : {
     198             :         char *format;
     199             :         int maxlen;
     200             :         va_list ap;
     201             : 
     202        9117 :         if (wpa_auth->cb.logger == NULL)
     203           4 :                 return;
     204             : 
     205        9117 :         maxlen = os_strlen(fmt) + 100;
     206        9117 :         format = os_malloc(maxlen);
     207        9117 :         if (!format)
     208           4 :                 return;
     209             : 
     210        9113 :         va_start(ap, fmt);
     211        9113 :         vsnprintf(format, maxlen, fmt, ap);
     212        9113 :         va_end(ap);
     213             : 
     214        9113 :         wpa_auth_logger(wpa_auth, addr, level, format);
     215             : 
     216        9113 :         os_free(format);
     217             : }
     218             : 
     219             : 
     220          23 : static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
     221             :                                const u8 *addr)
     222             : {
     223          23 :         if (wpa_auth->cb.disconnect == NULL)
     224          23 :                 return;
     225          23 :         wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
     226          23 :         wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
     227             :                                 WLAN_REASON_PREV_AUTH_NOT_VALID);
     228             : }
     229             : 
     230             : 
     231        9419 : static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
     232             : {
     233        9419 :         int ret = 0;
     234             : #ifdef CONFIG_IEEE80211R
     235        9419 :         if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
     236         274 :                 ret = 1;
     237             : #endif /* CONFIG_IEEE80211R */
     238             : #ifdef CONFIG_IEEE80211W
     239        9419 :         if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
     240         190 :                 ret = 1;
     241             : #endif /* CONFIG_IEEE80211W */
     242        9419 :         if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
     243           8 :                 ret = 1;
     244        9419 :         return ret;
     245             : }
     246             : 
     247             : 
     248           1 : static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
     249             : {
     250           1 :         struct wpa_authenticator *wpa_auth = eloop_ctx;
     251             : 
     252           1 :         if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
     253           0 :                 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
     254             :                            "initialization.");
     255             :         } else {
     256           1 :                 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
     257           1 :                 wpa_hexdump_key(MSG_DEBUG, "GMK",
     258           1 :                                 wpa_auth->group->GMK, WPA_GMK_LEN);
     259             :         }
     260             : 
     261           1 :         if (wpa_auth->conf.wpa_gmk_rekey) {
     262           1 :                 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
     263             :                                        wpa_rekey_gmk, wpa_auth, NULL);
     264             :         }
     265           1 : }
     266             : 
     267             : 
     268          10 : static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
     269             : {
     270          10 :         struct wpa_authenticator *wpa_auth = eloop_ctx;
     271             :         struct wpa_group *group, *next;
     272             : 
     273          10 :         wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
     274          10 :         group = wpa_auth->group;
     275          30 :         while (group) {
     276          10 :                 wpa_group_get(wpa_auth, group);
     277             : 
     278          10 :                 group->GTKReKey = TRUE;
     279             :                 do {
     280          12 :                         group->changed = FALSE;
     281          12 :                         wpa_group_sm_step(wpa_auth, group);
     282          12 :                 } while (group->changed);
     283             : 
     284          10 :                 next = group->next;
     285          10 :                 wpa_group_put(wpa_auth, group);
     286          10 :                 group = next;
     287             :         }
     288             : 
     289          10 :         if (wpa_auth->conf.wpa_group_rekey) {
     290          10 :                 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
     291             :                                        0, wpa_rekey_gtk, wpa_auth, NULL);
     292             :         }
     293          10 : }
     294             : 
     295             : 
     296           3 : static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
     297             : {
     298           3 :         struct wpa_authenticator *wpa_auth = eloop_ctx;
     299           3 :         struct wpa_state_machine *sm = timeout_ctx;
     300             : 
     301           3 :         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
     302           3 :         wpa_request_new_ptk(sm);
     303           3 :         wpa_sm_step(sm);
     304           3 : }
     305             : 
     306             : 
     307         367 : static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
     308             : {
     309         367 :         if (sm->pmksa == ctx)
     310           2 :                 sm->pmksa = NULL;
     311         367 :         return 0;
     312             : }
     313             : 
     314             : 
     315         388 : static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
     316             :                                    void *ctx)
     317             : {
     318         388 :         struct wpa_authenticator *wpa_auth = ctx;
     319         388 :         wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
     320         388 : }
     321             : 
     322             : 
     323        2160 : static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
     324             :                                           struct wpa_group *group)
     325             : {
     326             :         u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
     327             :         u8 rkey[32];
     328             :         unsigned long ptr;
     329             : 
     330        2160 :         if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
     331           0 :                 return -1;
     332        2160 :         wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
     333             : 
     334             :         /*
     335             :          * Counter = PRF-256(Random number, "Init Counter",
     336             :          *                   Local MAC Address || Time)
     337             :          */
     338        2160 :         os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
     339        2160 :         wpa_get_ntp_timestamp(buf + ETH_ALEN);
     340        2160 :         ptr = (unsigned long) group;
     341        2160 :         os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
     342        2160 :         if (random_get_bytes(rkey, sizeof(rkey)) < 0)
     343           0 :                 return -1;
     344             : 
     345        2160 :         if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
     346        2160 :                      group->Counter, WPA_NONCE_LEN) < 0)
     347           0 :                 return -1;
     348        2160 :         wpa_hexdump_key(MSG_DEBUG, "Key Counter",
     349        2160 :                         group->Counter, WPA_NONCE_LEN);
     350             : 
     351        2160 :         return 0;
     352             : }
     353             : 
     354             : 
     355        1308 : static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
     356             :                                          int vlan_id, int delay_init)
     357             : {
     358             :         struct wpa_group *group;
     359             : 
     360        1308 :         group = os_zalloc(sizeof(struct wpa_group));
     361        1308 :         if (group == NULL)
     362           0 :                 return NULL;
     363             : 
     364        1308 :         group->GTKAuthenticator = TRUE;
     365        1308 :         group->vlan_id = vlan_id;
     366        1308 :         group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
     367             : 
     368             :         if (random_pool_ready() != 1) {
     369             :                 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
     370             :                            "for secure operations - update keys later when "
     371             :                            "the first station connects");
     372             :         }
     373             : 
     374             :         /*
     375             :          * Set initial GMK/Counter value here. The actual values that will be
     376             :          * used in negotiations will be set once the first station tries to
     377             :          * connect. This allows more time for collecting additional randomness
     378             :          * on embedded devices.
     379             :          */
     380        1308 :         if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
     381           0 :                 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
     382             :                            "initialization.");
     383           0 :                 os_free(group);
     384           0 :                 return NULL;
     385             :         }
     386             : 
     387        1308 :         group->GInit = TRUE;
     388        1308 :         if (delay_init) {
     389        1293 :                 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
     390             :                            "until Beacon frames have been configured");
     391             :                 /* Initialization is completed in wpa_init_keys(). */
     392             :         } else {
     393          15 :                 wpa_group_sm_step(wpa_auth, group);
     394          15 :                 group->GInit = FALSE;
     395          15 :                 wpa_group_sm_step(wpa_auth, group);
     396             :         }
     397             : 
     398        1308 :         return group;
     399             : }
     400             : 
     401             : 
     402             : /**
     403             :  * wpa_init - Initialize WPA authenticator
     404             :  * @addr: Authenticator address
     405             :  * @conf: Configuration for WPA authenticator
     406             :  * @cb: Callback functions for WPA authenticator
     407             :  * Returns: Pointer to WPA authenticator data or %NULL on failure
     408             :  */
     409        1295 : struct wpa_authenticator * wpa_init(const u8 *addr,
     410             :                                     struct wpa_auth_config *conf,
     411             :                                     struct wpa_auth_callbacks *cb)
     412             : {
     413             :         struct wpa_authenticator *wpa_auth;
     414             : 
     415        1295 :         wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
     416        1295 :         if (wpa_auth == NULL)
     417           2 :                 return NULL;
     418        1293 :         os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
     419        1293 :         os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
     420        1293 :         os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
     421             : 
     422        1293 :         if (wpa_auth_gen_wpa_ie(wpa_auth)) {
     423           0 :                 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
     424           0 :                 os_free(wpa_auth);
     425           0 :                 return NULL;
     426             :         }
     427             : 
     428        1293 :         wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
     429        1293 :         if (wpa_auth->group == NULL) {
     430           0 :                 os_free(wpa_auth->wpa_ie);
     431           0 :                 os_free(wpa_auth);
     432           0 :                 return NULL;
     433             :         }
     434             : 
     435        1293 :         wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
     436             :                                                 wpa_auth);
     437        1293 :         if (wpa_auth->pmksa == NULL) {
     438           0 :                 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
     439           0 :                 os_free(wpa_auth->group);
     440           0 :                 os_free(wpa_auth->wpa_ie);
     441           0 :                 os_free(wpa_auth);
     442           0 :                 return NULL;
     443             :         }
     444             : 
     445             : #ifdef CONFIG_IEEE80211R
     446        1293 :         wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
     447        1293 :         if (wpa_auth->ft_pmk_cache == NULL) {
     448           0 :                 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
     449           0 :                 os_free(wpa_auth->group);
     450           0 :                 os_free(wpa_auth->wpa_ie);
     451           0 :                 pmksa_cache_auth_deinit(wpa_auth->pmksa);
     452           0 :                 os_free(wpa_auth);
     453           0 :                 return NULL;
     454             :         }
     455             : #endif /* CONFIG_IEEE80211R */
     456             : 
     457        1293 :         if (wpa_auth->conf.wpa_gmk_rekey) {
     458        1275 :                 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
     459             :                                        wpa_rekey_gmk, wpa_auth, NULL);
     460             :         }
     461             : 
     462        1293 :         if (wpa_auth->conf.wpa_group_rekey) {
     463        1293 :                 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
     464             :                                        wpa_rekey_gtk, wpa_auth, NULL);
     465             :         }
     466             : 
     467             : #ifdef CONFIG_P2P
     468         347 :         if (WPA_GET_BE32(conf->ip_addr_start)) {
     469         333 :                 int count = WPA_GET_BE32(conf->ip_addr_end) -
     470         222 :                         WPA_GET_BE32(conf->ip_addr_start) + 1;
     471         111 :                 if (count > 1000)
     472           0 :                         count = 1000;
     473         111 :                 if (count > 0)
     474         111 :                         wpa_auth->ip_pool = bitfield_alloc(count);
     475             :         }
     476             : #endif /* CONFIG_P2P */
     477             : 
     478        1293 :         return wpa_auth;
     479             : }
     480             : 
     481             : 
     482        1278 : int wpa_init_keys(struct wpa_authenticator *wpa_auth)
     483             : {
     484        1278 :         struct wpa_group *group = wpa_auth->group;
     485             : 
     486        1278 :         wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
     487             :                    "keys");
     488        1278 :         wpa_group_sm_step(wpa_auth, group);
     489        1278 :         group->GInit = FALSE;
     490        1278 :         wpa_group_sm_step(wpa_auth, group);
     491        1278 :         if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
     492           0 :                 return -1;
     493        1278 :         return 0;
     494             : }
     495             : 
     496             : 
     497             : /**
     498             :  * wpa_deinit - Deinitialize WPA authenticator
     499             :  * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
     500             :  */
     501        1293 : void wpa_deinit(struct wpa_authenticator *wpa_auth)
     502             : {
     503             :         struct wpa_group *group, *prev;
     504             : 
     505        1293 :         eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
     506        1293 :         eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
     507             : 
     508             : #ifdef CONFIG_PEERKEY
     509        2586 :         while (wpa_auth->stsl_negotiations)
     510           0 :                 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
     511             : #endif /* CONFIG_PEERKEY */
     512             : 
     513        1293 :         pmksa_cache_auth_deinit(wpa_auth->pmksa);
     514             : 
     515             : #ifdef CONFIG_IEEE80211R
     516        1293 :         wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
     517        1293 :         wpa_auth->ft_pmk_cache = NULL;
     518             : #endif /* CONFIG_IEEE80211R */
     519             : 
     520             : #ifdef CONFIG_P2P
     521         347 :         bitfield_free(wpa_auth->ip_pool);
     522             : #endif /* CONFIG_P2P */
     523             : 
     524             : 
     525        1293 :         os_free(wpa_auth->wpa_ie);
     526             : 
     527        1293 :         group = wpa_auth->group;
     528        3879 :         while (group) {
     529        1293 :                 prev = group;
     530        1293 :                 group = group->next;
     531        1293 :                 os_free(prev);
     532             :         }
     533             : 
     534        1293 :         os_free(wpa_auth);
     535        1293 : }
     536             : 
     537             : 
     538             : /**
     539             :  * wpa_reconfig - Update WPA authenticator configuration
     540             :  * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
     541             :  * @conf: Configuration for WPA authenticator
     542             :  */
     543           2 : int wpa_reconfig(struct wpa_authenticator *wpa_auth,
     544             :                  struct wpa_auth_config *conf)
     545             : {
     546             :         struct wpa_group *group;
     547           2 :         if (wpa_auth == NULL)
     548           0 :                 return 0;
     549             : 
     550           2 :         os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
     551           2 :         if (wpa_auth_gen_wpa_ie(wpa_auth)) {
     552           0 :                 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
     553           0 :                 return -1;
     554             :         }
     555             : 
     556             :         /*
     557             :          * Reinitialize GTK to make sure it is suitable for the new
     558             :          * configuration.
     559             :          */
     560           2 :         group = wpa_auth->group;
     561           2 :         group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
     562           2 :         group->GInit = TRUE;
     563           2 :         wpa_group_sm_step(wpa_auth, group);
     564           2 :         group->GInit = FALSE;
     565           2 :         wpa_group_sm_step(wpa_auth, group);
     566             : 
     567           2 :         return 0;
     568             : }
     569             : 
     570             : 
     571             : struct wpa_state_machine *
     572        1885 : wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
     573             :                   const u8 *p2p_dev_addr)
     574             : {
     575             :         struct wpa_state_machine *sm;
     576             : 
     577        1885 :         if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
     578           0 :                 return NULL;
     579             : 
     580        1885 :         sm = os_zalloc(sizeof(struct wpa_state_machine));
     581        1885 :         if (sm == NULL)
     582           0 :                 return NULL;
     583        1885 :         os_memcpy(sm->addr, addr, ETH_ALEN);
     584        1885 :         if (p2p_dev_addr)
     585         204 :                 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
     586             : 
     587        1885 :         sm->wpa_auth = wpa_auth;
     588        1885 :         sm->group = wpa_auth->group;
     589        1885 :         wpa_group_get(sm->wpa_auth, sm->group);
     590             : 
     591        1885 :         return sm;
     592             : }
     593             : 
     594             : 
     595        3305 : int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
     596             :                             struct wpa_state_machine *sm)
     597             : {
     598        3305 :         if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
     599        1106 :                 return -1;
     600             : 
     601             : #ifdef CONFIG_IEEE80211R
     602        2199 :         if (sm->ft_completed) {
     603         223 :                 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
     604             :                                 "FT authentication already completed - do not "
     605             :                                 "start 4-way handshake");
     606             :                 /* Go to PTKINITDONE state to allow GTK rekeying */
     607         223 :                 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
     608         223 :                 return 0;
     609             :         }
     610             : #endif /* CONFIG_IEEE80211R */
     611             : 
     612        1976 :         if (sm->started) {
     613         129 :                 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
     614         129 :                 sm->ReAuthenticationRequest = TRUE;
     615         129 :                 return wpa_sm_step(sm);
     616             :         }
     617             : 
     618        1847 :         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
     619             :                         "start authentication");
     620        1847 :         sm->started = 1;
     621             : 
     622        1847 :         sm->Init = TRUE;
     623        1847 :         if (wpa_sm_step(sm) == 1)
     624           0 :                 return 1; /* should not really happen */
     625        1847 :         sm->Init = FALSE;
     626        1847 :         sm->AuthenticationRequest = TRUE;
     627        1847 :         return wpa_sm_step(sm);
     628             : }
     629             : 
     630             : 
     631        1107 : void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
     632             : {
     633             :         /* WPA/RSN was not used - clear WPA state. This is needed if the STA
     634             :          * reassociates back to the same AP while the previous entry for the
     635             :          * STA has not yet been removed. */
     636        1107 :         if (sm == NULL)
     637        2213 :                 return;
     638             : 
     639           1 :         sm->wpa_key_mgmt = 0;
     640             : }
     641             : 
     642             : 
     643        1885 : static void wpa_free_sta_sm(struct wpa_state_machine *sm)
     644             : {
     645             : #ifdef CONFIG_P2P
     646         253 :         if (WPA_GET_BE32(sm->ip_addr)) {
     647             :                 u32 start;
     648         950 :                 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
     649             :                            "address %u.%u.%u.%u from " MACSTR,
     650         190 :                            sm->ip_addr[0], sm->ip_addr[1],
     651         190 :                            sm->ip_addr[2], sm->ip_addr[3],
     652         570 :                            MAC2STR(sm->addr));
     653          95 :                 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
     654          95 :                 bitfield_clear(sm->wpa_auth->ip_pool,
     655          95 :                                WPA_GET_BE32(sm->ip_addr) - start);
     656             :         }
     657             : #endif /* CONFIG_P2P */
     658        1885 :         if (sm->GUpdateStationKeys) {
     659           1 :                 sm->group->GKeyDoneStations--;
     660           1 :                 sm->GUpdateStationKeys = FALSE;
     661             :         }
     662             : #ifdef CONFIG_IEEE80211R
     663        1885 :         os_free(sm->assoc_resp_ftie);
     664        1885 :         wpabuf_free(sm->ft_pending_req_ies);
     665             : #endif /* CONFIG_IEEE80211R */
     666        1885 :         os_free(sm->last_rx_eapol_key);
     667        1885 :         os_free(sm->wpa_ie);
     668        1885 :         wpa_group_put(sm->wpa_auth, sm->group);
     669        1885 :         os_free(sm);
     670        1885 : }
     671             : 
     672             : 
     673        3024 : void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
     674             : {
     675        3024 :         if (sm == NULL)
     676        4163 :                 return;
     677             : 
     678        1885 :         if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
     679           2 :                 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
     680             :                                 "strict rekeying - force GTK rekey since STA "
     681             :                                 "is leaving");
     682           2 :                 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
     683           2 :                 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
     684             :                                        NULL);
     685             :         }
     686             : 
     687        1885 :         eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
     688        1885 :         sm->pending_1_of_4_timeout = 0;
     689        1885 :         eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
     690        1885 :         eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
     691        1885 :         if (sm->in_step_loop) {
     692             :                 /* Must not free state machine while wpa_sm_step() is running.
     693             :                  * Freeing will be completed in the end of wpa_sm_step(). */
     694           0 :                 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
     695           0 :                            "machine deinit for " MACSTR, MAC2STR(sm->addr));
     696           0 :                 sm->pending_deinit = 1;
     697             :         } else
     698        1885 :                 wpa_free_sta_sm(sm);
     699             : }
     700             : 
     701             : 
     702           8 : static void wpa_request_new_ptk(struct wpa_state_machine *sm)
     703             : {
     704           8 :         if (sm == NULL)
     705           8 :                 return;
     706             : 
     707           8 :         sm->PTKRequest = TRUE;
     708           8 :         sm->PTK_valid = 0;
     709             : }
     710             : 
     711             : 
     712        3195 : static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
     713             :                                     const u8 *replay_counter)
     714             : {
     715             :         int i;
     716        3203 :         for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
     717        3203 :                 if (!ctr[i].valid)
     718           5 :                         break;
     719        3198 :                 if (os_memcmp(replay_counter, ctr[i].counter,
     720             :                               WPA_REPLAY_COUNTER_LEN) == 0)
     721        3190 :                         return 1;
     722             :         }
     723           5 :         return 0;
     724             : }
     725             : 
     726             : 
     727        6366 : static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
     728             :                                             const u8 *replay_counter)
     729             : {
     730             :         int i;
     731       31830 :         for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
     732       25464 :                 if (ctr[i].valid &&
     733        3188 :                     (replay_counter == NULL ||
     734        3188 :                      os_memcmp(replay_counter, ctr[i].counter,
     735             :                                WPA_REPLAY_COUNTER_LEN) == 0))
     736        3188 :                         ctr[i].valid = FALSE;
     737             :         }
     738        6366 : }
     739             : 
     740             : 
     741             : #ifdef CONFIG_IEEE80211R
     742          46 : static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
     743             :                                struct wpa_state_machine *sm,
     744             :                                struct wpa_eapol_ie_parse *kde)
     745             : {
     746             :         struct wpa_ie_data ie;
     747             :         struct rsn_mdie *mdie;
     748             : 
     749          92 :         if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
     750          92 :             ie.num_pmkid != 1 || ie.pmkid == NULL) {
     751           0 :                 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
     752             :                            "FT 4-way handshake message 2/4");
     753           0 :                 return -1;
     754             :         }
     755             : 
     756          46 :         os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
     757          46 :         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
     758          46 :                     sm->sup_pmk_r1_name, PMKID_LEN);
     759             : 
     760          46 :         if (!kde->mdie || !kde->ftie) {
     761           0 :                 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
     762           0 :                            "message 2/4", kde->mdie ? "FTIE" : "MDIE");
     763           0 :                 return -1;
     764             :         }
     765             : 
     766          46 :         mdie = (struct rsn_mdie *) (kde->mdie + 2);
     767          92 :         if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
     768          46 :             os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
     769             :                       MOBILITY_DOMAIN_ID_LEN) != 0) {
     770           0 :                 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
     771           0 :                 return -1;
     772             :         }
     773             : 
     774          92 :         if (sm->assoc_resp_ftie &&
     775          92 :             (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
     776          46 :              os_memcmp(kde->ftie, sm->assoc_resp_ftie,
     777             :                        2 + sm->assoc_resp_ftie[1]) != 0)) {
     778           0 :                 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
     779           0 :                 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
     780           0 :                             kde->ftie, kde->ftie_len);
     781           0 :                 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
     782           0 :                             sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
     783           0 :                 return -1;
     784             :         }
     785             : 
     786          46 :         return 0;
     787             : }
     788             : #endif /* CONFIG_IEEE80211R */
     789             : 
     790             : 
     791           4 : static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
     792             :                                     struct wpa_state_machine *sm, int group)
     793             : {
     794             :         /* Supplicant reported a Michael MIC error */
     795           4 :         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
     796             :                          "received EAPOL-Key Error Request "
     797             :                          "(STA detected Michael MIC failure (group=%d))",
     798             :                          group);
     799             : 
     800           4 :         if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
     801           0 :                 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
     802             :                                 "ignore Michael MIC failure report since "
     803             :                                 "group cipher is not TKIP");
     804           4 :         } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
     805           0 :                 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
     806             :                                 "ignore Michael MIC failure report since "
     807             :                                 "pairwise cipher is not TKIP");
     808             :         } else {
     809           4 :                 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
     810           2 :                         return 1; /* STA entry was removed */
     811           2 :                 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
     812           2 :                 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
     813             :         }
     814             : 
     815             :         /*
     816             :          * Error report is not a request for a new key handshake, but since
     817             :          * Authenticator may do it, let's change the keys now anyway.
     818             :          */
     819           2 :         wpa_request_new_ptk(sm);
     820           2 :         return 0;
     821             : }
     822             : 
     823             : 
     824           1 : static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
     825             :                               size_t data_len)
     826             : {
     827             :         struct wpa_ptk PTK;
     828           1 :         int ok = 0;
     829           1 :         const u8 *pmk = NULL;
     830             : 
     831             :         for (;;) {
     832           1 :                 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
     833           1 :                         pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
     834           1 :                                                sm->p2p_dev_addr, pmk);
     835           1 :                         if (pmk == NULL)
     836           0 :                                 break;
     837             :                 } else
     838           0 :                         pmk = sm->PMK;
     839             : 
     840           1 :                 wpa_derive_ptk(sm, sm->alt_SNonce, pmk, &PTK);
     841             : 
     842           1 :                 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK, data, data_len)
     843             :                     == 0) {
     844           1 :                         ok = 1;
     845           1 :                         break;
     846             :                 }
     847             : 
     848           0 :                 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
     849           0 :                         break;
     850           0 :         }
     851             : 
     852           1 :         if (!ok) {
     853           0 :                 wpa_printf(MSG_DEBUG,
     854             :                            "WPA: Earlier SNonce did not result in matching MIC");
     855           0 :                 return -1;
     856             :         }
     857             : 
     858           1 :         wpa_printf(MSG_DEBUG,
     859             :                    "WPA: Earlier SNonce resulted in matching MIC");
     860           1 :         sm->alt_snonce_valid = 0;
     861           1 :         os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
     862           1 :         os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
     863           1 :         sm->PTK_valid = TRUE;
     864             : 
     865           1 :         return 0;
     866             : }
     867             : 
     868             : 
     869        3215 : void wpa_receive(struct wpa_authenticator *wpa_auth,
     870             :                  struct wpa_state_machine *sm,
     871             :                  u8 *data, size_t data_len)
     872             : {
     873             :         struct ieee802_1x_hdr *hdr;
     874             :         struct wpa_eapol_key *key;
     875             :         struct wpa_eapol_key_192 *key192;
     876             :         u16 key_info, key_data_length;
     877             :         enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
     878             :                SMK_M1, SMK_M3, SMK_ERROR } msg;
     879             :         char *msgtxt;
     880             :         struct wpa_eapol_ie_parse kde;
     881             :         int ft;
     882             :         const u8 *eapol_key_ie, *key_data;
     883             :         size_t eapol_key_ie_len, keyhdrlen, mic_len;
     884             : 
     885        3215 :         if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
     886          29 :                 return;
     887             : 
     888        3213 :         mic_len = wpa_mic_len(sm->wpa_key_mgmt);
     889        3213 :         keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
     890             : 
     891        3213 :         if (data_len < sizeof(*hdr) + keyhdrlen)
     892           0 :                 return;
     893             : 
     894        3213 :         hdr = (struct ieee802_1x_hdr *) data;
     895        3213 :         key = (struct wpa_eapol_key *) (hdr + 1);
     896        3213 :         key192 = (struct wpa_eapol_key_192 *) (hdr + 1);
     897        3213 :         key_info = WPA_GET_BE16(key->key_info);
     898        3213 :         if (mic_len == 24) {
     899           6 :                 key_data = (const u8 *) (key192 + 1);
     900           6 :                 key_data_length = WPA_GET_BE16(key192->key_data_length);
     901             :         } else {
     902        3207 :                 key_data = (const u8 *) (key + 1);
     903        3207 :                 key_data_length = WPA_GET_BE16(key->key_data_length);
     904             :         }
     905       25704 :         wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
     906             :                    " key_info=0x%x type=%u key_data_length=%u",
     907       22491 :                    MAC2STR(sm->addr), key_info, key->type, key_data_length);
     908        3213 :         if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
     909           1 :                 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
     910             :                            "key_data overflow (%d > %lu)",
     911             :                            key_data_length,
     912           1 :                            (unsigned long) (data_len - sizeof(*hdr) -
     913             :                                             keyhdrlen));
     914           1 :                 return;
     915             :         }
     916             : 
     917        3212 :         if (sm->wpa == WPA_VERSION_WPA2) {
     918        3126 :                 if (key->type == EAPOL_KEY_TYPE_WPA) {
     919             :                         /*
     920             :                          * Some deployed station implementations seem to send
     921             :                          * msg 4/4 with incorrect type value in WPA2 mode.
     922             :                          */
     923           1 :                         wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
     924             :                                    "with unexpected WPA type in RSN mode");
     925        3125 :                 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
     926           0 :                         wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
     927             :                                    "unexpected type %d in RSN mode",
     928           0 :                                    key->type);
     929           0 :                         return;
     930             :                 }
     931             :         } else {
     932          86 :                 if (key->type != EAPOL_KEY_TYPE_WPA) {
     933           1 :                         wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
     934             :                                    "unexpected type %d in WPA mode",
     935           1 :                                    key->type);
     936           1 :                         return;
     937             :                 }
     938             :         }
     939             : 
     940        3211 :         wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
     941             :                     WPA_NONCE_LEN);
     942        3211 :         wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
     943        3211 :                     key->replay_counter, WPA_REPLAY_COUNTER_LEN);
     944             : 
     945             :         /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
     946             :          * are set */
     947             : 
     948        3211 :         if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
     949             :             (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
     950           8 :                 if (key_info & WPA_KEY_INFO_ERROR) {
     951           3 :                         msg = SMK_ERROR;
     952           3 :                         msgtxt = "SMK Error";
     953             :                 } else {
     954           5 :                         msg = SMK_M1;
     955           5 :                         msgtxt = "SMK M1";
     956             :                 }
     957        3203 :         } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
     958           2 :                 msg = SMK_M3;
     959           2 :                 msgtxt = "SMK M3";
     960        3201 :         } else if (key_info & WPA_KEY_INFO_REQUEST) {
     961          11 :                 msg = REQUEST;
     962          11 :                 msgtxt = "Request";
     963        3190 :         } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
     964          35 :                 msg = GROUP_2;
     965          35 :                 msgtxt = "2/2 Group";
     966        3155 :         } else if (key_data_length == 0) {
     967        1563 :                 msg = PAIRWISE_4;
     968        1563 :                 msgtxt = "4/4 Pairwise";
     969             :         } else {
     970        1592 :                 msg = PAIRWISE_2;
     971        1592 :                 msgtxt = "2/4 Pairwise";
     972             :         }
     973             : 
     974             :         /* TODO: key_info type validation for PeerKey */
     975        3211 :         if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
     976             :             msg == GROUP_2) {
     977        3201 :                 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
     978        3307 :                 if (sm->pairwise == WPA_CIPHER_CCMP ||
     979         106 :                     sm->pairwise == WPA_CIPHER_GCMP) {
     980        3260 :                         if (wpa_use_aes_cmac(sm) &&
     981         310 :                             sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
     982         300 :                             !wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
     983             :                             ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
     984           0 :                                 wpa_auth_logger(wpa_auth, sm->addr,
     985             :                                                 LOGGER_WARNING,
     986             :                                                 "advertised support for "
     987             :                                                 "AES-128-CMAC, but did not "
     988             :                                                 "use it");
     989           0 :                                 return;
     990             :                         }
     991             : 
     992        3103 :                         if (!wpa_use_aes_cmac(sm) &&
     993             :                             ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
     994           1 :                                 wpa_auth_logger(wpa_auth, sm->addr,
     995             :                                                 LOGGER_WARNING,
     996             :                                                 "did not use HMAC-SHA1-AES "
     997             :                                                 "with CCMP/GCMP");
     998           1 :                                 return;
     999             :                         }
    1000             :                 }
    1001             : 
    1002        3200 :                 if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
    1003             :                     ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
    1004           0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
    1005             :                                         "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
    1006           0 :                         return;
    1007             :                 }
    1008             :         }
    1009             : 
    1010        3210 :         if (key_info & WPA_KEY_INFO_REQUEST) {
    1011          24 :                 if (sm->req_replay_counter_used &&
    1012           5 :                     os_memcmp(key->replay_counter, sm->req_replay_counter,
    1013             :                               WPA_REPLAY_COUNTER_LEN) <= 0) {
    1014           1 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
    1015             :                                         "received EAPOL-Key request with "
    1016             :                                         "replayed counter");
    1017           1 :                         return;
    1018             :                 }
    1019             :         }
    1020             : 
    1021        6400 :         if (!(key_info & WPA_KEY_INFO_REQUEST) &&
    1022        3191 :             !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
    1023             :                 int i;
    1024             : 
    1025           8 :                 if (msg == PAIRWISE_2 &&
    1026           3 :                     wpa_replay_counter_valid(sm->prev_key_replay,
    1027           6 :                                              key->replay_counter) &&
    1028           6 :                     sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
    1029           3 :                     os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
    1030             :                 {
    1031             :                         /*
    1032             :                          * Some supplicant implementations (e.g., Windows XP
    1033             :                          * WZC) update SNonce for each EAPOL-Key 2/4. This
    1034             :                          * breaks the workaround on accepting any of the
    1035             :                          * pending requests, so allow the SNonce to be updated
    1036             :                          * even if we have already sent out EAPOL-Key 3/4.
    1037             :                          */
    1038           2 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
    1039             :                                          "Process SNonce update from STA "
    1040             :                                          "based on retransmitted EAPOL-Key "
    1041             :                                          "1/4");
    1042           2 :                         sm->update_snonce = 1;
    1043           2 :                         os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
    1044           2 :                         sm->alt_snonce_valid = TRUE;
    1045           2 :                         os_memcpy(sm->alt_replay_counter,
    1046             :                                   sm->key_replay[0].counter,
    1047             :                                   WPA_REPLAY_COUNTER_LEN);
    1048           2 :                         goto continue_processing;
    1049             :                 }
    1050             : 
    1051           5 :                 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
    1052           4 :                     sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
    1053           2 :                     os_memcmp(key->replay_counter, sm->alt_replay_counter,
    1054             :                               WPA_REPLAY_COUNTER_LEN) == 0) {
    1055             :                         /*
    1056             :                          * Supplicant may still be using the old SNonce since
    1057             :                          * there was two EAPOL-Key 2/4 messages and they had
    1058             :                          * different SNonce values.
    1059             :                          */
    1060           2 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
    1061             :                                          "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
    1062           2 :                         goto continue_processing;
    1063             :                 }
    1064             : 
    1065           2 :                 if (msg == PAIRWISE_2 &&
    1066           1 :                     wpa_replay_counter_valid(sm->prev_key_replay,
    1067           2 :                                              key->replay_counter) &&
    1068           1 :                     sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
    1069           1 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
    1070             :                                          "ignore retransmitted EAPOL-Key %s - "
    1071             :                                          "SNonce did not change", msgtxt);
    1072             :                 } else {
    1073           0 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
    1074             :                                          "received EAPOL-Key %s with "
    1075             :                                          "unexpected replay counter", msgtxt);
    1076             :                 }
    1077           2 :                 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
    1078           2 :                         if (!sm->key_replay[i].valid)
    1079           1 :                                 break;
    1080           1 :                         wpa_hexdump(MSG_DEBUG, "pending replay counter",
    1081           1 :                                     sm->key_replay[i].counter,
    1082             :                                     WPA_REPLAY_COUNTER_LEN);
    1083             :                 }
    1084           1 :                 wpa_hexdump(MSG_DEBUG, "received replay counter",
    1085           1 :                             key->replay_counter, WPA_REPLAY_COUNTER_LEN);
    1086           1 :                 return;
    1087             :         }
    1088             : 
    1089             : continue_processing:
    1090        3208 :         switch (msg) {
    1091             :         case PAIRWISE_2:
    1092        1596 :                 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
    1093          10 :                     sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
    1094           7 :                     (!sm->update_snonce ||
    1095           2 :                      sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
    1096           3 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
    1097             :                                          "received EAPOL-Key msg 2/4 in "
    1098             :                                          "invalid state (%d) - dropped",
    1099           3 :                                          sm->wpa_ptk_state);
    1100           3 :                         return;
    1101             :                 }
    1102             :                 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
    1103        1588 :                 if (sm->group->reject_4way_hs_for_entropy) {
    1104             :                         /*
    1105             :                          * The system did not have enough entropy to generate
    1106             :                          * strong random numbers. Reject the first 4-way
    1107             :                          * handshake(s) and collect some entropy based on the
    1108             :                          * information from it. Once enough entropy is
    1109             :                          * available, the next atempt will trigger GMK/Key
    1110             :                          * Counter update and the station will be allowed to
    1111             :                          * continue.
    1112             :                          */
    1113           1 :                         wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
    1114             :                                    "collect more entropy for random number "
    1115             :                                    "generation");
    1116             :                         random_mark_pool_ready();
    1117           1 :                         wpa_sta_disconnect(wpa_auth, sm->addr);
    1118           1 :                         return;
    1119             :                 }
    1120        1587 :                 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
    1121           0 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
    1122             :                                          "received EAPOL-Key msg 2/4 with "
    1123             :                                          "invalid Key Data contents");
    1124           0 :                         return;
    1125             :                 }
    1126        1587 :                 if (kde.rsn_ie) {
    1127        1559 :                         eapol_key_ie = kde.rsn_ie;
    1128        1559 :                         eapol_key_ie_len = kde.rsn_ie_len;
    1129          28 :                 } else if (kde.osen) {
    1130           2 :                         eapol_key_ie = kde.osen;
    1131           2 :                         eapol_key_ie_len = kde.osen_len;
    1132             :                 } else {
    1133          26 :                         eapol_key_ie = kde.wpa_ie;
    1134          26 :                         eapol_key_ie_len = kde.wpa_ie_len;
    1135             :                 }
    1136        3148 :                 ft = sm->wpa == WPA_VERSION_WPA2 &&
    1137        1561 :                         wpa_key_mgmt_ft(sm->wpa_key_mgmt);
    1138        3174 :                 if (sm->wpa_ie == NULL ||
    1139        3174 :                     wpa_compare_rsn_ie(ft,
    1140        1587 :                                        sm->wpa_ie, sm->wpa_ie_len,
    1141             :                                        eapol_key_ie, eapol_key_ie_len)) {
    1142           0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1143             :                                         "WPA IE from (Re)AssocReq did not "
    1144             :                                         "match with msg 2/4");
    1145           0 :                         if (sm->wpa_ie) {
    1146           0 :                                 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
    1147           0 :                                             sm->wpa_ie, sm->wpa_ie_len);
    1148             :                         }
    1149           0 :                         wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
    1150             :                                     eapol_key_ie, eapol_key_ie_len);
    1151             :                         /* MLME-DEAUTHENTICATE.request */
    1152           0 :                         wpa_sta_disconnect(wpa_auth, sm->addr);
    1153           0 :                         return;
    1154             :                 }
    1155             : #ifdef CONFIG_IEEE80211R
    1156        1587 :                 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
    1157           0 :                         wpa_sta_disconnect(wpa_auth, sm->addr);
    1158           0 :                         return;
    1159             :                 }
    1160             : #endif /* CONFIG_IEEE80211R */
    1161             : #ifdef CONFIG_P2P
    1162         469 :                 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
    1163         314 :                     wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
    1164             :                         int idx;
    1165          95 :                         wpa_printf(MSG_DEBUG, "P2P: IP address requested in "
    1166             :                                    "EAPOL-Key exchange");
    1167          95 :                         idx = bitfield_get_first_zero(wpa_auth->ip_pool);
    1168          95 :                         if (idx >= 0) {
    1169          95 :                                 u32 start = WPA_GET_BE32(wpa_auth->conf.
    1170             :                                                          ip_addr_start);
    1171          95 :                                 bitfield_set(wpa_auth->ip_pool, idx);
    1172          95 :                                 WPA_PUT_BE32(sm->ip_addr, start + idx);
    1173         950 :                                 wpa_printf(MSG_DEBUG, "P2P: Assigned IP "
    1174             :                                            "address %u.%u.%u.%u to " MACSTR,
    1175         190 :                                            sm->ip_addr[0], sm->ip_addr[1],
    1176         190 :                                            sm->ip_addr[2], sm->ip_addr[3],
    1177         570 :                                            MAC2STR(sm->addr));
    1178             :                         }
    1179             :                 }
    1180             : #endif /* CONFIG_P2P */
    1181        1587 :                 break;
    1182             :         case PAIRWISE_4:
    1183        3124 :                 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
    1184        1561 :                     !sm->PTK_valid) {
    1185           2 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
    1186             :                                          "received EAPOL-Key msg 4/4 in "
    1187             :                                          "invalid state (%d) - dropped",
    1188           2 :                                          sm->wpa_ptk_state);
    1189           2 :                         return;
    1190             :                 }
    1191        1561 :                 break;
    1192             :         case GROUP_2:
    1193          34 :                 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
    1194          34 :                     || !sm->PTK_valid) {
    1195           0 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
    1196             :                                          "received EAPOL-Key msg 2/2 in "
    1197             :                                          "invalid state (%d) - dropped",
    1198           0 :                                          sm->wpa_ptk_group_state);
    1199           0 :                         return;
    1200             :                 }
    1201          34 :                 break;
    1202             : #ifdef CONFIG_PEERKEY
    1203             :         case SMK_M1:
    1204             :         case SMK_M3:
    1205             :         case SMK_ERROR:
    1206          10 :                 if (!wpa_auth->conf.peerkey) {
    1207           3 :                         wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
    1208             :                                    "PeerKey use disabled - ignoring message");
    1209           3 :                         return;
    1210             :                 }
    1211           7 :                 if (!sm->PTK_valid) {
    1212           0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1213             :                                         "received EAPOL-Key msg SMK in "
    1214             :                                         "invalid state - dropped");
    1215           0 :                         return;
    1216             :                 }
    1217           7 :                 break;
    1218             : #else /* CONFIG_PEERKEY */
    1219             :         case SMK_M1:
    1220             :         case SMK_M3:
    1221             :         case SMK_ERROR:
    1222             :                 return; /* STSL disabled - ignore SMK messages */
    1223             : #endif /* CONFIG_PEERKEY */
    1224             :         case REQUEST:
    1225          10 :                 break;
    1226             :         }
    1227             : 
    1228        3199 :         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
    1229             :                          "received EAPOL-Key frame (%s)", msgtxt);
    1230             : 
    1231        3199 :         if (key_info & WPA_KEY_INFO_ACK) {
    1232           0 :                 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1233             :                                 "received invalid EAPOL-Key: Key Ack set");
    1234           0 :                 return;
    1235             :         }
    1236             : 
    1237        3199 :         if (!(key_info & WPA_KEY_INFO_MIC)) {
    1238           0 :                 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1239             :                                 "received invalid EAPOL-Key: Key MIC not set");
    1240           0 :                 return;
    1241             :         }
    1242             : 
    1243        3199 :         sm->MICVerified = FALSE;
    1244        3199 :         if (sm->PTK_valid && !sm->update_snonce) {
    1245        1610 :                 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &sm->PTK, data,
    1246           1 :                                        data_len) &&
    1247           2 :                     (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
    1248           1 :                      wpa_try_alt_snonce(sm, data, data_len))) {
    1249           0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1250             :                                         "received EAPOL-Key with invalid MIC");
    1251           0 :                         return;
    1252             :                 }
    1253        1610 :                 sm->MICVerified = TRUE;
    1254        1610 :                 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
    1255        1610 :                 sm->pending_1_of_4_timeout = 0;
    1256             :         }
    1257             : 
    1258        3199 :         if (key_info & WPA_KEY_INFO_REQUEST) {
    1259          16 :                 if (sm->MICVerified) {
    1260          14 :                         sm->req_replay_counter_used = 1;
    1261          14 :                         os_memcpy(sm->req_replay_counter, key->replay_counter,
    1262             :                                   WPA_REPLAY_COUNTER_LEN);
    1263             :                 } else {
    1264           2 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1265             :                                         "received EAPOL-Key request with "
    1266             :                                         "invalid MIC");
    1267           2 :                         return;
    1268             :                 }
    1269             : 
    1270             :                 /*
    1271             :                  * TODO: should decrypt key data field if encryption was used;
    1272             :                  * even though MAC address KDE is not normally encrypted,
    1273             :                  * supplicant is allowed to encrypt it.
    1274             :                  */
    1275          14 :                 if (msg == SMK_ERROR) {
    1276             : #ifdef CONFIG_PEERKEY
    1277           2 :                         wpa_smk_error(wpa_auth, sm, key_data, key_data_length);
    1278             : #endif /* CONFIG_PEERKEY */
    1279           2 :                         return;
    1280          12 :                 } else if (key_info & WPA_KEY_INFO_ERROR) {
    1281           4 :                         if (wpa_receive_error_report(
    1282             :                                     wpa_auth, sm,
    1283           4 :                                     !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
    1284           2 :                                 return; /* STA entry was removed */
    1285           8 :                 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
    1286           3 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1287             :                                         "received EAPOL-Key Request for new "
    1288             :                                         "4-Way Handshake");
    1289           3 :                         wpa_request_new_ptk(sm);
    1290             : #ifdef CONFIG_PEERKEY
    1291           5 :                 } else if (msg == SMK_M1) {
    1292           4 :                         wpa_smk_m1(wpa_auth, sm, key, key_data,
    1293             :                                    key_data_length);
    1294             : #endif /* CONFIG_PEERKEY */
    1295           2 :                 } else if (key_data_length > 0 &&
    1296           1 :                            wpa_parse_kde_ies(key_data, key_data_length,
    1297           1 :                                              &kde) == 0 &&
    1298           1 :                            kde.mac_addr) {
    1299             :                 } else {
    1300           1 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1301             :                                         "received EAPOL-Key Request for GTK "
    1302             :                                         "rekeying");
    1303           1 :                         eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
    1304           1 :                         wpa_rekey_gtk(wpa_auth, NULL);
    1305             :                 }
    1306             :         } else {
    1307             :                 /* Do not allow the same key replay counter to be reused. */
    1308        3183 :                 wpa_replay_counter_mark_invalid(sm->key_replay,
    1309        3183 :                                                 key->replay_counter);
    1310             : 
    1311        3183 :                 if (msg == PAIRWISE_2) {
    1312             :                         /*
    1313             :                          * Maintain a copy of the pending EAPOL-Key frames in
    1314             :                          * case the EAPOL-Key frame was retransmitted. This is
    1315             :                          * needed to allow EAPOL-Key msg 2/4 reply to another
    1316             :                          * pending msg 1/4 to update the SNonce to work around
    1317             :                          * unexpected supplicant behavior.
    1318             :                          */
    1319        1587 :                         os_memcpy(sm->prev_key_replay, sm->key_replay,
    1320             :                                   sizeof(sm->key_replay));
    1321             :                 } else {
    1322        1596 :                         os_memset(sm->prev_key_replay, 0,
    1323             :                                   sizeof(sm->prev_key_replay));
    1324             :                 }
    1325             : 
    1326             :                 /*
    1327             :                  * Make sure old valid counters are not accepted anymore and
    1328             :                  * do not get copied again.
    1329             :                  */
    1330        3183 :                 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
    1331             :         }
    1332             : 
    1333             : #ifdef CONFIG_PEERKEY
    1334        3193 :         if (msg == SMK_M3) {
    1335           1 :                 wpa_smk_m3(wpa_auth, sm, key, key_data, key_data_length);
    1336           1 :                 return;
    1337             :         }
    1338             : #endif /* CONFIG_PEERKEY */
    1339             : 
    1340        3192 :         os_free(sm->last_rx_eapol_key);
    1341        3192 :         sm->last_rx_eapol_key = os_malloc(data_len);
    1342        3192 :         if (sm->last_rx_eapol_key == NULL)
    1343           4 :                 return;
    1344        3188 :         os_memcpy(sm->last_rx_eapol_key, data, data_len);
    1345        3188 :         sm->last_rx_eapol_key_len = data_len;
    1346             : 
    1347        3188 :         sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
    1348        3188 :         sm->EAPOLKeyReceived = TRUE;
    1349        3188 :         sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
    1350        3188 :         sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
    1351        3188 :         os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
    1352        3188 :         wpa_sm_step(sm);
    1353             : }
    1354             : 
    1355             : 
    1356        2449 : static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
    1357             :                           const u8 *gnonce, u8 *gtk, size_t gtk_len)
    1358             : {
    1359             :         u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
    1360             :         u8 *pos;
    1361        2449 :         int ret = 0;
    1362             : 
    1363             :         /* GTK = PRF-X(GMK, "Group key expansion",
    1364             :          *      AA || GNonce || Time || random data)
    1365             :          * The example described in the IEEE 802.11 standard uses only AA and
    1366             :          * GNonce as inputs here. Add some more entropy since this derivation
    1367             :          * is done only at the Authenticator and as such, does not need to be
    1368             :          * exactly same.
    1369             :          */
    1370        2449 :         os_memcpy(data, addr, ETH_ALEN);
    1371        2449 :         os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
    1372        2449 :         pos = data + ETH_ALEN + WPA_NONCE_LEN;
    1373        2449 :         wpa_get_ntp_timestamp(pos);
    1374        2449 :         pos += 8;
    1375        2449 :         if (random_get_bytes(pos, 16) < 0)
    1376           1 :                 ret = -1;
    1377             : 
    1378             : #ifdef CONFIG_IEEE80211W
    1379        2449 :         sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
    1380             : #else /* CONFIG_IEEE80211W */
    1381             :         if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
    1382             :             < 0)
    1383             :                 ret = -1;
    1384             : #endif /* CONFIG_IEEE80211W */
    1385             : 
    1386        2449 :         return ret;
    1387             : }
    1388             : 
    1389             : 
    1390          34 : static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
    1391             : {
    1392          34 :         struct wpa_authenticator *wpa_auth = eloop_ctx;
    1393          34 :         struct wpa_state_machine *sm = timeout_ctx;
    1394             : 
    1395          34 :         sm->pending_1_of_4_timeout = 0;
    1396          34 :         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
    1397          34 :         sm->TimeoutEvt = TRUE;
    1398          34 :         wpa_sm_step(sm);
    1399          34 : }
    1400             : 
    1401             : 
    1402        3229 : void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
    1403             :                       struct wpa_state_machine *sm, int key_info,
    1404             :                       const u8 *key_rsc, const u8 *nonce,
    1405             :                       const u8 *kde, size_t kde_len,
    1406             :                       int keyidx, int encr, int force_version)
    1407             : {
    1408             :         struct ieee802_1x_hdr *hdr;
    1409             :         struct wpa_eapol_key *key;
    1410             :         struct wpa_eapol_key_192 *key192;
    1411             :         size_t len, mic_len, keyhdrlen;
    1412             :         int alg;
    1413        3229 :         int key_data_len, pad_len = 0;
    1414             :         u8 *buf, *pos;
    1415             :         int version, pairwise;
    1416             :         int i;
    1417             :         u8 *key_data;
    1418             : 
    1419        3229 :         mic_len = wpa_mic_len(sm->wpa_key_mgmt);
    1420        3229 :         keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
    1421             : 
    1422        3229 :         len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
    1423             : 
    1424        3229 :         if (force_version)
    1425           0 :                 version = force_version;
    1426        6454 :         else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
    1427        3225 :                  wpa_key_mgmt_suite_b(sm->wpa_key_mgmt))
    1428          16 :                 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
    1429        3213 :         else if (wpa_use_aes_cmac(sm))
    1430         150 :                 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
    1431        3063 :         else if (sm->pairwise != WPA_CIPHER_TKIP)
    1432        2981 :                 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
    1433             :         else
    1434          82 :                 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
    1435             : 
    1436        3229 :         pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
    1437             : 
    1438       12916 :         wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
    1439             :                    "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
    1440             :                    "encr=%d)",
    1441             :                    version,
    1442        3229 :                    (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
    1443        3229 :                    (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
    1444        3229 :                    (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
    1445        3229 :                    (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
    1446             :                    pairwise, (unsigned long) kde_len, keyidx, encr);
    1447             : 
    1448        3229 :         key_data_len = kde_len;
    1449             : 
    1450        3477 :         if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
    1451         492 :              sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
    1452         476 :              wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
    1453        3147 :              version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
    1454        1550 :                 pad_len = key_data_len % 8;
    1455        1550 :                 if (pad_len)
    1456        1439 :                         pad_len = 8 - pad_len;
    1457        1550 :                 key_data_len += pad_len + 8;
    1458             :         }
    1459             : 
    1460        3229 :         len += key_data_len;
    1461             : 
    1462        3229 :         hdr = os_zalloc(len);
    1463        3229 :         if (hdr == NULL)
    1464           1 :                 return;
    1465        3228 :         hdr->version = wpa_auth->conf.eapol_version;
    1466        3228 :         hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
    1467        3228 :         hdr->length = host_to_be16(len  - sizeof(*hdr));
    1468        3228 :         key = (struct wpa_eapol_key *) (hdr + 1);
    1469        3228 :         key192 = (struct wpa_eapol_key_192 *) (hdr + 1);
    1470        3228 :         key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
    1471             : 
    1472        3228 :         key->type = sm->wpa == WPA_VERSION_WPA2 ?
    1473             :                 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
    1474        3228 :         key_info |= version;
    1475        3228 :         if (encr && sm->wpa == WPA_VERSION_WPA2)
    1476        1551 :                 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
    1477        3228 :         if (sm->wpa != WPA_VERSION_WPA2)
    1478          79 :                 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
    1479        3228 :         WPA_PUT_BE16(key->key_info, key_info);
    1480             : 
    1481        3228 :         alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
    1482        3228 :         WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
    1483        3228 :         if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
    1484           8 :                 WPA_PUT_BE16(key->key_length, 0);
    1485             : 
    1486             :         /* FIX: STSL: what to use as key_replay_counter? */
    1487       12912 :         for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
    1488        9684 :                 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
    1489        9684 :                 os_memcpy(sm->key_replay[i].counter,
    1490             :                           sm->key_replay[i - 1].counter,
    1491             :                           WPA_REPLAY_COUNTER_LEN);
    1492             :         }
    1493        3228 :         inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
    1494        3228 :         os_memcpy(key->replay_counter, sm->key_replay[0].counter,
    1495             :                   WPA_REPLAY_COUNTER_LEN);
    1496        3228 :         wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
    1497        3228 :                     key->replay_counter, WPA_REPLAY_COUNTER_LEN);
    1498        3228 :         sm->key_replay[0].valid = TRUE;
    1499             : 
    1500        3228 :         if (nonce)
    1501        3225 :                 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
    1502             : 
    1503        3228 :         if (key_rsc)
    1504        1576 :                 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
    1505             : 
    1506        3228 :         if (kde && !encr) {
    1507         775 :                 os_memcpy(key_data, kde, kde_len);
    1508        1550 :                 if (mic_len == 24)
    1509           1 :                         WPA_PUT_BE16(key192->key_data_length, kde_len);
    1510             :                 else
    1511         774 :                         WPA_PUT_BE16(key->key_data_length, kde_len);
    1512        2453 :         } else if (encr && kde) {
    1513        1578 :                 buf = os_zalloc(key_data_len);
    1514        1578 :                 if (buf == NULL) {
    1515           0 :                         os_free(hdr);
    1516           0 :                         return;
    1517             :                 }
    1518        1578 :                 pos = buf;
    1519        1578 :                 os_memcpy(pos, kde, kde_len);
    1520        1578 :                 pos += kde_len;
    1521             : 
    1522        1578 :                 if (pad_len)
    1523        1439 :                         *pos++ = 0xdd;
    1524             : 
    1525        1578 :                 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
    1526             :                                 buf, key_data_len);
    1527        1690 :                 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
    1528         222 :                     sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
    1529         214 :                     wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
    1530             :                     version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
    1531        1550 :                         if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
    1532        1550 :                                      (key_data_len - 8) / 8, buf, key_data)) {
    1533           0 :                                 os_free(hdr);
    1534           0 :                                 os_free(buf);
    1535           0 :                                 return;
    1536             :                         }
    1537        3100 :                         if (mic_len == 24)
    1538           3 :                                 WPA_PUT_BE16(key192->key_data_length,
    1539             :                                              key_data_len);
    1540             :                         else
    1541        1547 :                                 WPA_PUT_BE16(key->key_data_length,
    1542             :                                              key_data_len);
    1543             : #ifndef CONFIG_NO_RC4
    1544          28 :                 } else if (sm->PTK.kek_len == 16) {
    1545             :                         u8 ek[32];
    1546          28 :                         os_memcpy(key->key_iv,
    1547             :                                   sm->group->Counter + WPA_NONCE_LEN - 16, 16);
    1548          28 :                         inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
    1549          28 :                         os_memcpy(ek, key->key_iv, 16);
    1550          28 :                         os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
    1551          28 :                         os_memcpy(key_data, buf, key_data_len);
    1552          28 :                         rc4_skip(ek, 32, 256, key_data, key_data_len);
    1553          28 :                         if (mic_len == 24)
    1554           0 :                                 WPA_PUT_BE16(key192->key_data_length,
    1555             :                                              key_data_len);
    1556             :                         else
    1557          28 :                                 WPA_PUT_BE16(key->key_data_length,
    1558             :                                              key_data_len);
    1559             : #endif /* CONFIG_NO_RC4 */
    1560             :                 } else {
    1561           0 :                         os_free(hdr);
    1562           0 :                         os_free(buf);
    1563           0 :                         return;
    1564             :                 }
    1565        1578 :                 os_free(buf);
    1566             :         }
    1567             : 
    1568        3228 :         if (key_info & WPA_KEY_INFO_MIC) {
    1569             :                 u8 *key_mic;
    1570             : 
    1571        1610 :                 if (!sm->PTK_valid) {
    1572           0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
    1573             :                                         "PTK not valid when sending EAPOL-Key "
    1574             :                                         "frame");
    1575           0 :                         os_free(hdr);
    1576           0 :                         return;
    1577             :                 }
    1578             : 
    1579        1610 :                 key_mic = key192->key_mic; /* same offset for key and key192 */
    1580        1610 :                 wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
    1581             :                                   sm->wpa_key_mgmt, version,
    1582             :                                   (u8 *) hdr, len, key_mic);
    1583             : #ifdef CONFIG_TESTING_OPTIONS
    1584        1655 :                 if (!pairwise &&
    1585          45 :                     wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
    1586           0 :                     drand48() <
    1587           0 :                     wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
    1588           0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1589             :                                         "Corrupting group EAPOL-Key Key MIC");
    1590           0 :                         key_mic[0]++;
    1591             :                 }
    1592             : #endif /* CONFIG_TESTING_OPTIONS */
    1593             :         }
    1594             : 
    1595        3228 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
    1596             :                            1);
    1597        3228 :         wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
    1598        3228 :                             sm->pairwise_set);
    1599        3228 :         os_free(hdr);
    1600             : }
    1601             : 
    1602             : 
    1603        3221 : static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
    1604             :                            struct wpa_state_machine *sm, int key_info,
    1605             :                            const u8 *key_rsc, const u8 *nonce,
    1606             :                            const u8 *kde, size_t kde_len,
    1607             :                            int keyidx, int encr)
    1608             : {
    1609             :         int timeout_ms;
    1610        3221 :         int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
    1611             :         int ctr;
    1612             : 
    1613        3221 :         if (sm == NULL)
    1614        3221 :                 return;
    1615             : 
    1616        3221 :         __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
    1617             :                          keyidx, encr, 0);
    1618             : 
    1619        3221 :         ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
    1620        3221 :         if (ctr == 1 && wpa_auth->conf.tx_status)
    1621        3155 :                 timeout_ms = pairwise ? eapol_key_timeout_first :
    1622             :                         eapol_key_timeout_first_group;
    1623             :         else
    1624          66 :                 timeout_ms = eapol_key_timeout_subseq;
    1625        3221 :         if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
    1626        1585 :                 sm->pending_1_of_4_timeout = 1;
    1627        3221 :         wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
    1628             :                    "counter %d)", timeout_ms, ctr);
    1629        3221 :         eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
    1630             :                                wpa_send_eapol_timeout, wpa_auth, sm);
    1631             : }
    1632             : 
    1633             : 
    1634        3196 : static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
    1635             :                               size_t data_len)
    1636             : {
    1637             :         struct ieee802_1x_hdr *hdr;
    1638             :         struct wpa_eapol_key *key;
    1639             :         struct wpa_eapol_key_192 *key192;
    1640             :         u16 key_info;
    1641        3196 :         int ret = 0;
    1642             :         u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
    1643        3196 :         size_t mic_len = wpa_mic_len(akmp);
    1644             : 
    1645        3196 :         if (data_len < sizeof(*hdr) + sizeof(*key))
    1646           0 :                 return -1;
    1647             : 
    1648        3196 :         hdr = (struct ieee802_1x_hdr *) data;
    1649        3196 :         key = (struct wpa_eapol_key *) (hdr + 1);
    1650        3196 :         key192 = (struct wpa_eapol_key_192 *) (hdr + 1);
    1651        3196 :         key_info = WPA_GET_BE16(key->key_info);
    1652        3196 :         os_memcpy(mic, key192->key_mic, mic_len);
    1653        3196 :         os_memset(key192->key_mic, 0, mic_len);
    1654        3196 :         if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
    1655             :                               key_info & WPA_KEY_INFO_TYPE_MASK,
    1656        6392 :                               data, data_len, key192->key_mic) ||
    1657        3196 :             os_memcmp_const(mic, key192->key_mic, mic_len) != 0)
    1658          18 :                 ret = -1;
    1659        3196 :         os_memcpy(key192->key_mic, mic, mic_len);
    1660        3196 :         return ret;
    1661             : }
    1662             : 
    1663             : 
    1664       11445 : void wpa_remove_ptk(struct wpa_state_machine *sm)
    1665             : {
    1666       11445 :         sm->PTK_valid = FALSE;
    1667       11445 :         os_memset(&sm->PTK, 0, sizeof(sm->PTK));
    1668       11445 :         wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0);
    1669       11445 :         sm->pairwise_set = FALSE;
    1670       11445 :         eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
    1671       11445 : }
    1672             : 
    1673             : 
    1674        9754 : int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
    1675             : {
    1676        9754 :         int remove_ptk = 1;
    1677             : 
    1678        9754 :         if (sm == NULL)
    1679        5491 :                 return -1;
    1680             : 
    1681        4263 :         wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    1682             :                          "event %d notification", event);
    1683             : 
    1684        4263 :         switch (event) {
    1685             :         case WPA_AUTH:
    1686             : #ifdef CONFIG_MESH
    1687             :                 /* PTKs are derived through AMPE */
    1688          14 :                 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
    1689             :                         /* not mesh */
    1690           6 :                         break;
    1691             :                 }
    1692           8 :                 return 0;
    1693             : #endif /* CONFIG_MESH */
    1694             :         case WPA_ASSOC:
    1695        2152 :                 break;
    1696             :         case WPA_DEAUTH:
    1697             :         case WPA_DISASSOC:
    1698        1751 :                 sm->DeauthenticationRequest = TRUE;
    1699        1751 :                 break;
    1700             :         case WPA_REAUTH:
    1701             :         case WPA_REAUTH_EAPOL:
    1702         123 :                 if (!sm->started) {
    1703             :                         /*
    1704             :                          * When using WPS, we may end up here if the STA
    1705             :                          * manages to re-associate without the previous STA
    1706             :                          * entry getting removed. Consequently, we need to make
    1707             :                          * sure that the WPA state machines gets initialized
    1708             :                          * properly at this point.
    1709             :                          */
    1710           0 :                         wpa_printf(MSG_DEBUG, "WPA state machine had not been "
    1711             :                                    "started - initialize now");
    1712           0 :                         sm->started = 1;
    1713           0 :                         sm->Init = TRUE;
    1714           0 :                         if (wpa_sm_step(sm) == 1)
    1715           0 :                                 return 1; /* should not really happen */
    1716           0 :                         sm->Init = FALSE;
    1717           0 :                         sm->AuthenticationRequest = TRUE;
    1718           0 :                         break;
    1719             :                 }
    1720         123 :                 if (sm->GUpdateStationKeys) {
    1721             :                         /*
    1722             :                          * Reauthentication cancels the pending group key
    1723             :                          * update for this STA.
    1724             :                          */
    1725           0 :                         sm->group->GKeyDoneStations--;
    1726           0 :                         sm->GUpdateStationKeys = FALSE;
    1727           0 :                         sm->PtkGroupInit = TRUE;
    1728             :                 }
    1729         123 :                 sm->ReAuthenticationRequest = TRUE;
    1730         123 :                 break;
    1731             :         case WPA_ASSOC_FT:
    1732             : #ifdef CONFIG_IEEE80211R
    1733         223 :                 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
    1734             :                            "after association");
    1735         223 :                 wpa_ft_install_ptk(sm);
    1736             : 
    1737             :                 /* Using FT protocol, not WPA auth state machine */
    1738         223 :                 sm->ft_completed = 1;
    1739         223 :                 return 0;
    1740             : #else /* CONFIG_IEEE80211R */
    1741             :                 break;
    1742             : #endif /* CONFIG_IEEE80211R */
    1743             :         }
    1744             : 
    1745             : #ifdef CONFIG_IEEE80211R
    1746        4032 :         sm->ft_completed = 0;
    1747             : #endif /* CONFIG_IEEE80211R */
    1748             : 
    1749             : #ifdef CONFIG_IEEE80211W
    1750        4032 :         if (sm->mgmt_frame_prot && event == WPA_AUTH)
    1751           4 :                 remove_ptk = 0;
    1752             : #endif /* CONFIG_IEEE80211W */
    1753             : 
    1754        4032 :         if (remove_ptk) {
    1755        4028 :                 sm->PTK_valid = FALSE;
    1756        4028 :                 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
    1757             : 
    1758        4028 :                 if (event != WPA_REAUTH_EAPOL)
    1759        3920 :                         wpa_remove_ptk(sm);
    1760             :         }
    1761             : 
    1762        4032 :         if (sm->in_step_loop) {
    1763             :                 /*
    1764             :                  * wpa_sm_step() is already running - avoid recursive call to
    1765             :                  * it by making the existing loop process the new update.
    1766             :                  */
    1767          20 :                 sm->changed = TRUE;
    1768          20 :                 return 0;
    1769             :         }
    1770        4012 :         return wpa_sm_step(sm);
    1771             : }
    1772             : 
    1773             : 
    1774        3600 : SM_STATE(WPA_PTK, INITIALIZE)
    1775             : {
    1776        3600 :         SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
    1777        3600 :         if (sm->Init) {
    1778             :                 /* Init flag is not cleared here, so avoid busy
    1779             :                  * loop by claiming nothing changed. */
    1780        1847 :                 sm->changed = FALSE;
    1781             :         }
    1782             : 
    1783        3600 :         sm->keycount = 0;
    1784        3600 :         if (sm->GUpdateStationKeys)
    1785           0 :                 sm->group->GKeyDoneStations--;
    1786        3600 :         sm->GUpdateStationKeys = FALSE;
    1787        3600 :         if (sm->wpa == WPA_VERSION_WPA)
    1788          39 :                 sm->PInitAKeys = FALSE;
    1789             :         if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
    1790             :                * Local AA > Remote AA)) */) {
    1791        3600 :                 sm->Pair = TRUE;
    1792             :         }
    1793        3600 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
    1794        3600 :         wpa_remove_ptk(sm);
    1795        3600 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
    1796        3600 :         sm->TimeoutCtr = 0;
    1797        3600 :         if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
    1798        1436 :                 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
    1799             :                                    WPA_EAPOL_authorized, 0);
    1800             :         }
    1801        3600 : }
    1802             : 
    1803             : 
    1804          22 : SM_STATE(WPA_PTK, DISCONNECT)
    1805             : {
    1806          22 :         SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
    1807          22 :         sm->Disconnect = FALSE;
    1808          22 :         wpa_sta_disconnect(sm->wpa_auth, sm->addr);
    1809          22 : }
    1810             : 
    1811             : 
    1812        1753 : SM_STATE(WPA_PTK, DISCONNECTED)
    1813             : {
    1814        1753 :         SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
    1815        1753 :         sm->DeauthenticationRequest = FALSE;
    1816        1753 : }
    1817             : 
    1818             : 
    1819        1847 : SM_STATE(WPA_PTK, AUTHENTICATION)
    1820             : {
    1821        1847 :         SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
    1822        1847 :         os_memset(&sm->PTK, 0, sizeof(sm->PTK));
    1823        1847 :         sm->PTK_valid = FALSE;
    1824        1847 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
    1825             :                            1);
    1826        1847 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
    1827        1847 :         sm->AuthenticationRequest = FALSE;
    1828        1847 : }
    1829             : 
    1830             : 
    1831        2099 : static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
    1832             :                                   struct wpa_group *group)
    1833             : {
    1834        2099 :         if (group->first_sta_seen)
    1835        3346 :                 return;
    1836             :         /*
    1837             :          * System has run bit further than at the time hostapd was started
    1838             :          * potentially very early during boot up. This provides better chances
    1839             :          * of collecting more randomness on embedded systems. Re-initialize the
    1840             :          * GMK and Counter here to improve their strength if there was not
    1841             :          * enough entropy available immediately after system startup.
    1842             :          */
    1843         852 :         wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
    1844             :                    "station");
    1845             :         if (random_pool_ready() != 1) {
    1846             :                 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
    1847             :                            "to proceed - reject first 4-way handshake");
    1848             :                 group->reject_4way_hs_for_entropy = TRUE;
    1849             :         } else {
    1850         852 :                 group->first_sta_seen = TRUE;
    1851         852 :                 group->reject_4way_hs_for_entropy = FALSE;
    1852             :         }
    1853             : 
    1854        1704 :         if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
    1855        1703 :             wpa_gtk_update(wpa_auth, group) < 0 ||
    1856         851 :             wpa_group_config_group_keys(wpa_auth, group) < 0) {
    1857           1 :                 wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
    1858           1 :                 group->first_sta_seen = FALSE;
    1859           1 :                 group->reject_4way_hs_for_entropy = TRUE;
    1860             :         }
    1861             : }
    1862             : 
    1863             : 
    1864        2099 : SM_STATE(WPA_PTK, AUTHENTICATION2)
    1865             : {
    1866        2099 :         SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
    1867             : 
    1868        2099 :         wpa_group_ensure_init(sm->wpa_auth, sm->group);
    1869        2099 :         sm->ReAuthenticationRequest = FALSE;
    1870             : 
    1871             :         /*
    1872             :          * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
    1873             :          * ambiguous. The Authenticator state machine uses a counter that is
    1874             :          * incremented by one for each 4-way handshake. However, the security
    1875             :          * analysis of 4-way handshake points out that unpredictable nonces
    1876             :          * help in preventing precomputation attacks. Instead of the state
    1877             :          * machine definition, use an unpredictable nonce value here to provide
    1878             :          * stronger protection against potential precomputation attacks.
    1879             :          */
    1880        2099 :         if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
    1881           0 :                 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
    1882             :                            "ANonce.");
    1883           0 :                 sm->Disconnect = TRUE;
    1884        2099 :                 return;
    1885             :         }
    1886        2099 :         wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
    1887             :                     WPA_NONCE_LEN);
    1888             :         /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
    1889             :          * logical place than INITIALIZE since AUTHENTICATION2 can be
    1890             :          * re-entered on ReAuthenticationRequest without going through
    1891             :          * INITIALIZE. */
    1892        2099 :         sm->TimeoutCtr = 0;
    1893             : }
    1894             : 
    1895             : 
    1896         767 : SM_STATE(WPA_PTK, INITPMK)
    1897             : {
    1898             :         u8 msk[2 * PMK_LEN];
    1899         767 :         size_t len = 2 * PMK_LEN;
    1900             : 
    1901         767 :         SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
    1902             : #ifdef CONFIG_IEEE80211R
    1903         767 :         sm->xxkey_len = 0;
    1904             : #endif /* CONFIG_IEEE80211R */
    1905         767 :         if (sm->pmksa) {
    1906          33 :                 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
    1907          33 :                 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
    1908         734 :         } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
    1909         718 :                 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
    1910             :                            "(len=%lu)", (unsigned long) len);
    1911         718 :                 os_memcpy(sm->PMK, msk, PMK_LEN);
    1912             : #ifdef CONFIG_IEEE80211R
    1913         718 :                 if (len >= 2 * PMK_LEN) {
    1914         718 :                         os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
    1915         718 :                         sm->xxkey_len = PMK_LEN;
    1916             :                 }
    1917             : #endif /* CONFIG_IEEE80211R */
    1918             :         } else {
    1919          16 :                 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
    1920          16 :                            sm->wpa_auth->cb.get_msk);
    1921          16 :                 sm->Disconnect = TRUE;
    1922         783 :                 return;
    1923             :         }
    1924         751 :         os_memset(msk, 0, sizeof(msk));
    1925             : 
    1926         751 :         sm->req_replay_counter_used = 0;
    1927             :         /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
    1928             :          * will break reauthentication since EAPOL state machines may not be
    1929             :          * get into AUTHENTICATING state that clears keyRun before WPA state
    1930             :          * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
    1931             :          * state and takes PMK from the previously used AAA Key. This will
    1932             :          * eventually fail in 4-Way Handshake because Supplicant uses PMK
    1933             :          * derived from the new AAA Key. Setting keyRun = FALSE here seems to
    1934             :          * be good workaround for this issue. */
    1935         751 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
    1936             : }
    1937             : 
    1938             : 
    1939         834 : SM_STATE(WPA_PTK, INITPSK)
    1940             : {
    1941             :         const u8 *psk;
    1942         834 :         SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
    1943         834 :         psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
    1944         834 :         if (psk) {
    1945         834 :                 os_memcpy(sm->PMK, psk, PMK_LEN);
    1946             : #ifdef CONFIG_IEEE80211R
    1947         834 :                 os_memcpy(sm->xxkey, psk, PMK_LEN);
    1948         834 :                 sm->xxkey_len = PMK_LEN;
    1949             : #endif /* CONFIG_IEEE80211R */
    1950             :         }
    1951         834 :         sm->req_replay_counter_used = 0;
    1952         834 : }
    1953             : 
    1954             : 
    1955        1625 : SM_STATE(WPA_PTK, PTKSTART)
    1956             : {
    1957        1625 :         u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
    1958        1625 :         size_t pmkid_len = 0;
    1959             : 
    1960        1625 :         SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
    1961        1625 :         sm->PTKRequest = FALSE;
    1962        1625 :         sm->TimeoutEvt = FALSE;
    1963        1625 :         sm->alt_snonce_valid = FALSE;
    1964             : 
    1965        1625 :         sm->TimeoutCtr++;
    1966        1625 :         if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
    1967             :                 /* No point in sending the EAPOL-Key - we will disconnect
    1968             :                  * immediately following this. */
    1969        1631 :                 return;
    1970             :         }
    1971             : 
    1972        1619 :         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    1973             :                         "sending 1/4 msg of 4-Way Handshake");
    1974             :         /*
    1975             :          * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
    1976             :          * one possible PSK for this STA.
    1977             :          */
    1978        3212 :         if (sm->wpa == WPA_VERSION_WPA2 &&
    1979        2343 :             wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
    1980         750 :             sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
    1981         748 :                 pmkid = buf;
    1982         748 :                 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
    1983         748 :                 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
    1984         748 :                 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
    1985         748 :                 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
    1986         748 :                 if (sm->pmksa) {
    1987          33 :                         os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
    1988             :                                   sm->pmksa->pmkid, PMKID_LEN);
    1989         715 :                 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
    1990             :                         /* No KCK available to derive PMKID */
    1991           4 :                         pmkid = NULL;
    1992             :                 } else {
    1993             :                         /*
    1994             :                          * Calculate PMKID since no PMKSA cache entry was
    1995             :                          * available with pre-calculated PMKID.
    1996             :                          */
    1997        1422 :                         rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
    1998         711 :                                   sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
    1999             :                                   wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
    2000             :                 }
    2001             :         }
    2002        1619 :         wpa_send_eapol(sm->wpa_auth, sm,
    2003             :                        WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
    2004        1619 :                        sm->ANonce, pmkid, pmkid_len, 0, 0);
    2005             : }
    2006             : 
    2007             : 
    2008        1586 : static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
    2009             :                           const u8 *pmk, struct wpa_ptk *ptk)
    2010             : {
    2011             : #ifdef CONFIG_IEEE80211R
    2012        1586 :         if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
    2013          46 :                 return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
    2014             : #endif /* CONFIG_IEEE80211R */
    2015             : 
    2016        3080 :         return wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
    2017        1540 :                               sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
    2018             :                               ptk, sm->wpa_key_mgmt, sm->pairwise);
    2019             : }
    2020             : 
    2021             : 
    2022        1585 : SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
    2023             : {
    2024             :         struct wpa_ptk PTK;
    2025        1585 :         int ok = 0, psk_found = 0;
    2026        1585 :         const u8 *pmk = NULL;
    2027             : 
    2028        1585 :         SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
    2029        1585 :         sm->EAPOLKeyReceived = FALSE;
    2030        1585 :         sm->update_snonce = FALSE;
    2031             : 
    2032             :         /* WPA with IEEE 802.1X: use the derived PMK from EAP
    2033             :          * WPA-PSK: iterate through possible PSKs and select the one matching
    2034             :          * the packet */
    2035             :         for (;;) {
    2036        1602 :                 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
    2037         854 :                         pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
    2038         854 :                                                sm->p2p_dev_addr, pmk);
    2039         854 :                         if (pmk == NULL)
    2040          17 :                                 break;
    2041         837 :                         psk_found = 1;
    2042             :                 } else
    2043         748 :                         pmk = sm->PMK;
    2044             : 
    2045        1585 :                 wpa_derive_ptk(sm, sm->SNonce, pmk, &PTK);
    2046             : 
    2047        1585 :                 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK,
    2048             :                                        sm->last_rx_eapol_key,
    2049             :                                        sm->last_rx_eapol_key_len) == 0) {
    2050        1568 :                         ok = 1;
    2051        1568 :                         break;
    2052             :                 }
    2053             : 
    2054          17 :                 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
    2055           0 :                         break;
    2056          17 :         }
    2057             : 
    2058        1585 :         if (!ok) {
    2059          17 :                 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    2060             :                                 "invalid MIC in msg 2/4 of 4-Way Handshake");
    2061          17 :                 if (psk_found)
    2062          17 :                         wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
    2063          34 :                 return;
    2064             :         }
    2065             : 
    2066             : #ifdef CONFIG_IEEE80211R
    2067        1568 :         if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
    2068             :                 /*
    2069             :                  * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
    2070             :                  * with the value we derived.
    2071             :                  */
    2072          46 :                 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
    2073             :                                     WPA_PMK_NAME_LEN) != 0) {
    2074           0 :                         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    2075             :                                         "PMKR1Name mismatch in FT 4-way "
    2076             :                                         "handshake");
    2077           0 :                         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
    2078             :                                     "Supplicant",
    2079           0 :                                     sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
    2080           0 :                         wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
    2081           0 :                                     sm->pmk_r1_name, WPA_PMK_NAME_LEN);
    2082           0 :                         return;
    2083             :                 }
    2084             :         }
    2085             : #endif /* CONFIG_IEEE80211R */
    2086             : 
    2087        1568 :         sm->pending_1_of_4_timeout = 0;
    2088        1568 :         eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
    2089             : 
    2090        1568 :         if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
    2091             :                 /* PSK may have changed from the previous choice, so update
    2092             :                  * state machine data based on whatever PSK was selected here.
    2093             :                  */
    2094         820 :                 os_memcpy(sm->PMK, pmk, PMK_LEN);
    2095             :         }
    2096             : 
    2097        1568 :         sm->MICVerified = TRUE;
    2098             : 
    2099        1568 :         os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
    2100        1568 :         sm->PTK_valid = TRUE;
    2101             : }
    2102             : 
    2103             : 
    2104        1568 : SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
    2105             : {
    2106        1568 :         SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
    2107        1568 :         sm->TimeoutCtr = 0;
    2108        1568 : }
    2109             : 
    2110             : 
    2111             : #ifdef CONFIG_IEEE80211W
    2112             : 
    2113        1579 : static int ieee80211w_kde_len(struct wpa_state_machine *sm)
    2114             : {
    2115        1579 :         if (sm->mgmt_frame_prot) {
    2116             :                 size_t len;
    2117          59 :                 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
    2118          59 :                 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
    2119             :         }
    2120             : 
    2121        1520 :         return 0;
    2122             : }
    2123             : 
    2124             : 
    2125        1575 : static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
    2126             : {
    2127             :         struct wpa_igtk_kde igtk;
    2128        1575 :         struct wpa_group *gsm = sm->group;
    2129             :         u8 rsc[WPA_KEY_RSC_LEN];
    2130        1575 :         size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
    2131             : 
    2132        1575 :         if (!sm->mgmt_frame_prot)
    2133        1518 :                 return pos;
    2134             : 
    2135          57 :         igtk.keyid[0] = gsm->GN_igtk;
    2136          57 :         igtk.keyid[1] = 0;
    2137         114 :         if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
    2138          57 :             wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
    2139           0 :                 os_memset(igtk.pn, 0, sizeof(igtk.pn));
    2140             :         else
    2141          57 :                 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
    2142          57 :         os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
    2143          57 :         if (sm->wpa_auth->conf.disable_gtk) {
    2144             :                 /*
    2145             :                  * Provide unique random IGTK to each STA to prevent use of
    2146             :                  * IGTK in the BSS.
    2147             :                  */
    2148           2 :                 if (random_get_bytes(igtk.igtk, len) < 0)
    2149           0 :                         return pos;
    2150             :         }
    2151          57 :         pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
    2152             :                           (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
    2153             :                           NULL, 0);
    2154             : 
    2155          57 :         return pos;
    2156             : }
    2157             : 
    2158             : #else /* CONFIG_IEEE80211W */
    2159             : 
    2160             : static int ieee80211w_kde_len(struct wpa_state_machine *sm)
    2161             : {
    2162             :         return 0;
    2163             : }
    2164             : 
    2165             : 
    2166             : static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
    2167             : {
    2168             :         return pos;
    2169             : }
    2170             : 
    2171             : #endif /* CONFIG_IEEE80211W */
    2172             : 
    2173             : 
    2174        1569 : SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
    2175             : {
    2176             :         u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
    2177             :         size_t gtk_len, kde_len;
    2178        1569 :         struct wpa_group *gsm = sm->group;
    2179             :         u8 *wpa_ie;
    2180        1569 :         int wpa_ie_len, secure, keyidx, encr = 0;
    2181             : 
    2182        1569 :         SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
    2183        1569 :         sm->TimeoutEvt = FALSE;
    2184             : 
    2185        1569 :         sm->TimeoutCtr++;
    2186        1569 :         if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
    2187             :                 /* No point in sending the EAPOL-Key - we will disconnect
    2188             :                  * immediately following this. */
    2189           4 :                 return;
    2190             :         }
    2191             : 
    2192             :         /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
    2193             :            GTK[GN], IGTK, [FTIE], [TIE * 2])
    2194             :          */
    2195        1569 :         os_memset(rsc, 0, WPA_KEY_RSC_LEN);
    2196        1569 :         wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
    2197             :         /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
    2198        1569 :         wpa_ie = sm->wpa_auth->wpa_ie;
    2199        1569 :         wpa_ie_len = sm->wpa_auth->wpa_ie_len;
    2200        1595 :         if (sm->wpa == WPA_VERSION_WPA &&
    2201          31 :             (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
    2202          10 :             wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
    2203             :                 /* WPA-only STA, remove RSN IE and possible MDIE */
    2204           5 :                 wpa_ie = wpa_ie + wpa_ie[1] + 2;
    2205           5 :                 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
    2206           2 :                         wpa_ie = wpa_ie + wpa_ie[1] + 2;
    2207           5 :                 wpa_ie_len = wpa_ie[1] + 2;
    2208             :         }
    2209        1569 :         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    2210             :                         "sending 3/4 msg of 4-Way Handshake");
    2211        1569 :         if (sm->wpa == WPA_VERSION_WPA2) {
    2212             :                 /* WPA2 send GTK in the 4-way handshake */
    2213        1543 :                 secure = 1;
    2214        1543 :                 gtk = gsm->GTK[gsm->GN - 1];
    2215        1543 :                 gtk_len = gsm->GTK_len;
    2216        1543 :                 if (sm->wpa_auth->conf.disable_gtk) {
    2217             :                         /*
    2218             :                          * Provide unique random GTK to each STA to prevent use
    2219             :                          * of GTK in the BSS.
    2220             :                          */
    2221          10 :                         if (random_get_bytes(dummy_gtk, gtk_len) < 0)
    2222           0 :                                 return;
    2223          10 :                         gtk = dummy_gtk;
    2224             :                 }
    2225        1543 :                 keyidx = gsm->GN;
    2226        1543 :                 _rsc = rsc;
    2227        1543 :                 encr = 1;
    2228             :         } else {
    2229             :                 /* WPA does not include GTK in msg 3/4 */
    2230          26 :                 secure = 0;
    2231          26 :                 gtk = NULL;
    2232          26 :                 gtk_len = 0;
    2233          26 :                 keyidx = 0;
    2234          26 :                 _rsc = NULL;
    2235          26 :                 if (sm->rx_eapol_key_secure) {
    2236             :                         /*
    2237             :                          * It looks like Windows 7 supplicant tries to use
    2238             :                          * Secure bit in msg 2/4 after having reported Michael
    2239             :                          * MIC failure and it then rejects the 4-way handshake
    2240             :                          * if msg 3/4 does not set Secure bit. Work around this
    2241             :                          * by setting the Secure bit here even in the case of
    2242             :                          * WPA if the supplicant used it first.
    2243             :                          */
    2244           0 :                         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    2245             :                                         "STA used Secure bit in WPA msg 2/4 - "
    2246             :                                         "set Secure for 3/4 as workaround");
    2247           0 :                         secure = 1;
    2248             :                 }
    2249             :         }
    2250             : 
    2251        1569 :         kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
    2252        1569 :         if (gtk)
    2253        1543 :                 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
    2254             : #ifdef CONFIG_IEEE80211R
    2255        1569 :         if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
    2256          46 :                 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
    2257          46 :                 kde_len += 300; /* FTIE + 2 * TIE */
    2258             :         }
    2259             : #endif /* CONFIG_IEEE80211R */
    2260             : #ifdef CONFIG_P2P
    2261         234 :         if (WPA_GET_BE32(sm->ip_addr) > 0)
    2262          95 :                 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
    2263             : #endif /* CONFIG_P2P */
    2264        1569 :         kde = os_malloc(kde_len);
    2265        1569 :         if (kde == NULL)
    2266           2 :                 return;
    2267             : 
    2268        1567 :         pos = kde;
    2269        1567 :         os_memcpy(pos, wpa_ie, wpa_ie_len);
    2270        1567 :         pos += wpa_ie_len;
    2271             : #ifdef CONFIG_IEEE80211R
    2272        1567 :         if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
    2273          46 :                 int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
    2274          46 :                 if (res < 0) {
    2275           2 :                         wpa_printf(MSG_ERROR, "FT: Failed to insert "
    2276             :                                    "PMKR1Name into RSN IE in EAPOL-Key data");
    2277           2 :                         os_free(kde);
    2278           2 :                         return;
    2279             :                 }
    2280          44 :                 pos += res;
    2281             :         }
    2282             : #endif /* CONFIG_IEEE80211R */
    2283        1565 :         if (gtk) {
    2284             :                 u8 hdr[2];
    2285        1539 :                 hdr[0] = keyidx & 0x03;
    2286        1539 :                 hdr[1] = 0;
    2287        1539 :                 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
    2288             :                                   gtk, gtk_len);
    2289             :         }
    2290        1565 :         pos = ieee80211w_kde_add(sm, pos);
    2291             : 
    2292             : #ifdef CONFIG_IEEE80211R
    2293        1565 :         if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
    2294             :                 int res;
    2295             :                 struct wpa_auth_config *conf;
    2296             : 
    2297          44 :                 conf = &sm->wpa_auth->conf;
    2298          44 :                 res = wpa_write_ftie(conf, conf->r0_key_holder,
    2299             :                                      conf->r0_key_holder_len,
    2300          44 :                                      NULL, NULL, pos, kde + kde_len - pos,
    2301             :                                      NULL, 0);
    2302          44 :                 if (res < 0) {
    2303           0 :                         wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
    2304             :                                    "into EAPOL-Key Key Data");
    2305           0 :                         os_free(kde);
    2306           0 :                         return;
    2307             :                 }
    2308          44 :                 pos += res;
    2309             : 
    2310             :                 /* TIE[ReassociationDeadline] (TU) */
    2311          44 :                 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
    2312          44 :                 *pos++ = 5;
    2313          44 :                 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
    2314          44 :                 WPA_PUT_LE32(pos, conf->reassociation_deadline);
    2315          44 :                 pos += 4;
    2316             : 
    2317             :                 /* TIE[KeyLifetime] (seconds) */
    2318          44 :                 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
    2319          44 :                 *pos++ = 5;
    2320          44 :                 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
    2321          44 :                 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
    2322          44 :                 pos += 4;
    2323             :         }
    2324             : #endif /* CONFIG_IEEE80211R */
    2325             : #ifdef CONFIG_P2P
    2326         234 :         if (WPA_GET_BE32(sm->ip_addr) > 0) {
    2327             :                 u8 addr[3 * 4];
    2328          95 :                 os_memcpy(addr, sm->ip_addr, 4);
    2329          95 :                 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
    2330          95 :                 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
    2331          95 :                 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
    2332             :                                   addr, sizeof(addr), NULL, 0);
    2333             :         }
    2334             : #endif /* CONFIG_P2P */
    2335             : 
    2336        3130 :         wpa_send_eapol(sm->wpa_auth, sm,
    2337             :                        (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
    2338             :                        WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
    2339             :                        WPA_KEY_INFO_KEY_TYPE,
    2340        3130 :                        _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
    2341        1565 :         os_free(kde);
    2342             : }
    2343             : 
    2344             : 
    2345        1559 : SM_STATE(WPA_PTK, PTKINITDONE)
    2346             : {
    2347        1559 :         SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
    2348        1559 :         sm->EAPOLKeyReceived = FALSE;
    2349        1559 :         if (sm->Pair) {
    2350        1559 :                 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
    2351        1559 :                 int klen = wpa_cipher_key_len(sm->pairwise);
    2352        3118 :                 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
    2353        1559 :                                      sm->PTK.tk, klen)) {
    2354           0 :                         wpa_sta_disconnect(sm->wpa_auth, sm->addr);
    2355        1559 :                         return;
    2356             :                 }
    2357             :                 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
    2358        1559 :                 sm->pairwise_set = TRUE;
    2359             : 
    2360        1559 :                 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
    2361           6 :                         eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
    2362           6 :                         eloop_register_timeout(sm->wpa_auth->conf.
    2363             :                                                wpa_ptk_rekey, 0, wpa_rekey_ptk,
    2364           6 :                                                sm->wpa_auth, sm);
    2365             :                 }
    2366             : 
    2367        1559 :                 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
    2368         813 :                         wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
    2369             :                                            WPA_EAPOL_authorized, 1);
    2370             :                 }
    2371             :         }
    2372             : 
    2373             :         if (0 /* IBSS == TRUE */) {
    2374             :                 sm->keycount++;
    2375             :                 if (sm->keycount == 2) {
    2376             :                         wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
    2377             :                                            WPA_EAPOL_portValid, 1);
    2378             :                 }
    2379             :         } else {
    2380        1559 :                 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
    2381             :                                    1);
    2382             :         }
    2383        1559 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
    2384        1559 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
    2385        1559 :         if (sm->wpa == WPA_VERSION_WPA)
    2386          26 :                 sm->PInitAKeys = TRUE;
    2387             :         else
    2388        1533 :                 sm->has_GTK = TRUE;
    2389        1559 :         wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
    2390             :                          "pairwise key handshake completed (%s)",
    2391        1559 :                          sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
    2392             : 
    2393             : #ifdef CONFIG_IEEE80211R
    2394        1559 :         wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
    2395             : #endif /* CONFIG_IEEE80211R */
    2396             : }
    2397             : 
    2398             : 
    2399       43404 : SM_STEP(WPA_PTK)
    2400             : {
    2401       43404 :         struct wpa_authenticator *wpa_auth = sm->wpa_auth;
    2402             : 
    2403       43404 :         if (sm->Init)
    2404        1847 :                 SM_ENTER(WPA_PTK, INITIALIZE);
    2405       41557 :         else if (sm->Disconnect
    2406             :                  /* || FIX: dot11RSNAConfigSALifetime timeout */) {
    2407          16 :                 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
    2408             :                                 "WPA_PTK: sm->Disconnect");
    2409          16 :                 SM_ENTER(WPA_PTK, DISCONNECT);
    2410             :         }
    2411       41541 :         else if (sm->DeauthenticationRequest)
    2412        1751 :                 SM_ENTER(WPA_PTK, DISCONNECTED);
    2413       39790 :         else if (sm->AuthenticationRequest)
    2414        1847 :                 SM_ENTER(WPA_PTK, AUTHENTICATION);
    2415       37943 :         else if (sm->ReAuthenticationRequest)
    2416         252 :                 SM_ENTER(WPA_PTK, AUTHENTICATION2);
    2417       37691 :         else if (sm->PTKRequest)
    2418           8 :                 SM_ENTER(WPA_PTK, PTKSTART);
    2419       37683 :         else switch (sm->wpa_ptk_state) {
    2420             :         case WPA_PTK_INITIALIZE:
    2421        4672 :                 break;
    2422             :         case WPA_PTK_DISCONNECT:
    2423           2 :                 SM_ENTER(WPA_PTK, DISCONNECTED);
    2424           2 :                 break;
    2425             :         case WPA_PTK_DISCONNECTED:
    2426        1753 :                 SM_ENTER(WPA_PTK, INITIALIZE);
    2427        1753 :                 break;
    2428             :         case WPA_PTK_AUTHENTICATION:
    2429        1847 :                 SM_ENTER(WPA_PTK, AUTHENTICATION2);
    2430        1847 :                 break;
    2431             :         case WPA_PTK_AUTHENTICATION2:
    2432       30196 :                 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
    2433       14677 :                     wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
    2434             :                                        WPA_EAPOL_keyRun) > 0)
    2435         767 :                         SM_ENTER(WPA_PTK, INITPMK);
    2436       14752 :                 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
    2437             :                          /* FIX: && 802.1X::keyRun */)
    2438         834 :                         SM_ENTER(WPA_PTK, INITPSK);
    2439       15519 :                 break;
    2440             :         case WPA_PTK_INITPMK:
    2441         751 :                 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
    2442             :                                        WPA_EAPOL_keyAvailable) > 0)
    2443         751 :                         SM_ENTER(WPA_PTK, PTKSTART);
    2444             :                 else {
    2445           0 :                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
    2446           0 :                         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
    2447             :                                         "INITPMK - keyAvailable = false");
    2448           0 :                         SM_ENTER(WPA_PTK, DISCONNECT);
    2449             :                 }
    2450         751 :                 break;
    2451             :         case WPA_PTK_INITPSK:
    2452         834 :                 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
    2453             :                                      NULL))
    2454         834 :                         SM_ENTER(WPA_PTK, PTKSTART);
    2455             :                 else {
    2456           0 :                         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
    2457             :                                         "no PSK configured for the STA");
    2458           0 :                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
    2459           0 :                         SM_ENTER(WPA_PTK, DISCONNECT);
    2460             :                 }
    2461         834 :                 break;
    2462             :         case WPA_PTK_PTKSTART:
    2463        4901 :                 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
    2464        1583 :                     sm->EAPOLKeyPairwise)
    2465        1583 :                         SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
    2466        3470 :                 else if (sm->TimeoutCtr >
    2467        1735 :                          (int) dot11RSNAConfigPairwiseUpdateCount) {
    2468           6 :                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
    2469           6 :                         wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    2470             :                                          "PTKSTART: Retry limit %d reached",
    2471             :                                          dot11RSNAConfigPairwiseUpdateCount);
    2472           6 :                         SM_ENTER(WPA_PTK, DISCONNECT);
    2473        1729 :                 } else if (sm->TimeoutEvt)
    2474          16 :                         SM_ENTER(WPA_PTK, PTKSTART);
    2475        3318 :                 break;
    2476             :         case WPA_PTK_PTKCALCNEGOTIATING:
    2477        1601 :                 if (sm->MICVerified)
    2478        1568 :                         SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
    2479          33 :                 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
    2480           0 :                          sm->EAPOLKeyPairwise)
    2481           0 :                         SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
    2482          33 :                 else if (sm->TimeoutEvt)
    2483          16 :                         SM_ENTER(WPA_PTK, PTKSTART);
    2484        1601 :                 break;
    2485             :         case WPA_PTK_PTKCALCNEGOTIATING2:
    2486        1568 :                 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
    2487        1568 :                 break;
    2488             :         case WPA_PTK_PTKINITNEGOTIATING:
    2489        3139 :                 if (sm->update_snonce)
    2490           2 :                         SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
    2491        4696 :                 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
    2492        3118 :                          sm->EAPOLKeyPairwise && sm->MICVerified)
    2493        1559 :                         SM_ENTER(WPA_PTK, PTKINITDONE);
    2494        3156 :                 else if (sm->TimeoutCtr >
    2495        1578 :                          (int) dot11RSNAConfigPairwiseUpdateCount) {
    2496           0 :                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
    2497           0 :                         wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    2498             :                                          "PTKINITNEGOTIATING: Retry limit %d "
    2499             :                                          "reached",
    2500             :                                          dot11RSNAConfigPairwiseUpdateCount);
    2501           0 :                         SM_ENTER(WPA_PTK, DISCONNECT);
    2502        1578 :                 } else if (sm->TimeoutEvt)
    2503           1 :                         SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
    2504        3139 :                 break;
    2505             :         case WPA_PTK_PTKINITDONE:
    2506        2679 :                 break;
    2507             :         }
    2508       43404 : }
    2509             : 
    2510             : 
    2511        1881 : SM_STATE(WPA_PTK_GROUP, IDLE)
    2512             : {
    2513        1881 :         SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
    2514        1881 :         if (sm->Init) {
    2515             :                 /* Init flag is not cleared here, so avoid busy
    2516             :                  * loop by claiming nothing changed. */
    2517        1847 :                 sm->changed = FALSE;
    2518             :         }
    2519        1881 :         sm->GTimeoutCtr = 0;
    2520        1881 : }
    2521             : 
    2522             : 
    2523          37 : SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
    2524             : {
    2525             :         u8 rsc[WPA_KEY_RSC_LEN];
    2526          37 :         struct wpa_group *gsm = sm->group;
    2527             :         const u8 *kde;
    2528          37 :         u8 *kde_buf = NULL, *pos, hdr[2];
    2529             :         size_t kde_len;
    2530             :         u8 *gtk, dummy_gtk[32];
    2531             : 
    2532          37 :         SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
    2533             : 
    2534          37 :         sm->GTimeoutCtr++;
    2535          37 :         if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
    2536             :                 /* No point in sending the EAPOL-Key - we will disconnect
    2537             :                  * immediately following this. */
    2538           0 :                 return;
    2539             :         }
    2540             : 
    2541          37 :         if (sm->wpa == WPA_VERSION_WPA)
    2542          27 :                 sm->PInitAKeys = FALSE;
    2543          37 :         sm->TimeoutEvt = FALSE;
    2544             :         /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
    2545          37 :         os_memset(rsc, 0, WPA_KEY_RSC_LEN);
    2546          37 :         if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
    2547          27 :                 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
    2548          37 :         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    2549             :                         "sending 1/2 msg of Group Key Handshake");
    2550             : 
    2551          37 :         gtk = gsm->GTK[gsm->GN - 1];
    2552          37 :         if (sm->wpa_auth->conf.disable_gtk) {
    2553             :                 /*
    2554             :                  * Provide unique random GTK to each STA to prevent use
    2555             :                  * of GTK in the BSS.
    2556             :                  */
    2557           0 :                 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
    2558           0 :                         return;
    2559           0 :                 gtk = dummy_gtk;
    2560             :         }
    2561          37 :         if (sm->wpa == WPA_VERSION_WPA2) {
    2562          20 :                 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
    2563          10 :                         ieee80211w_kde_len(sm);
    2564          10 :                 kde_buf = os_malloc(kde_len);
    2565          10 :                 if (kde_buf == NULL)
    2566           0 :                         return;
    2567             : 
    2568          10 :                 kde = pos = kde_buf;
    2569          10 :                 hdr[0] = gsm->GN & 0x03;
    2570          10 :                 hdr[1] = 0;
    2571          10 :                 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
    2572          10 :                                   gtk, gsm->GTK_len);
    2573          10 :                 pos = ieee80211w_kde_add(sm, pos);
    2574          10 :                 kde_len = pos - kde;
    2575             :         } else {
    2576          27 :                 kde = gtk;
    2577          27 :                 kde_len = gsm->GTK_len;
    2578             :         }
    2579             : 
    2580          74 :         wpa_send_eapol(sm->wpa_auth, sm,
    2581             :                        WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
    2582             :                        WPA_KEY_INFO_ACK |
    2583          37 :                        (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
    2584          37 :                        rsc, gsm->GNonce, kde, kde_len, gsm->GN, 1);
    2585             : 
    2586          37 :         os_free(kde_buf);
    2587             : }
    2588             : 
    2589             : 
    2590          34 : SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
    2591             : {
    2592          34 :         SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
    2593          34 :         sm->EAPOLKeyReceived = FALSE;
    2594          34 :         if (sm->GUpdateStationKeys)
    2595           9 :                 sm->group->GKeyDoneStations--;
    2596          34 :         sm->GUpdateStationKeys = FALSE;
    2597          34 :         sm->GTimeoutCtr = 0;
    2598             :         /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
    2599          34 :         wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
    2600             :                          "group key handshake completed (%s)",
    2601          34 :                          sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
    2602          34 :         sm->has_GTK = TRUE;
    2603          34 : }
    2604             : 
    2605             : 
    2606           0 : SM_STATE(WPA_PTK_GROUP, KEYERROR)
    2607             : {
    2608           0 :         SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
    2609           0 :         if (sm->GUpdateStationKeys)
    2610           0 :                 sm->group->GKeyDoneStations--;
    2611           0 :         sm->GUpdateStationKeys = FALSE;
    2612           0 :         sm->Disconnect = TRUE;
    2613           0 : }
    2614             : 
    2615             : 
    2616       43404 : SM_STEP(WPA_PTK_GROUP)
    2617             : {
    2618       43404 :         if (sm->Init || sm->PtkGroupInit) {
    2619        1847 :                 SM_ENTER(WPA_PTK_GROUP, IDLE);
    2620        1847 :                 sm->PtkGroupInit = FALSE;
    2621       41557 :         } else switch (sm->wpa_ptk_group_state) {
    2622             :         case WPA_PTK_GROUP_IDLE:
    2623       82880 :                 if (sm->GUpdateStationKeys ||
    2624       41834 :                     (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
    2625          36 :                         SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
    2626       41445 :                 break;
    2627             :         case WPA_PTK_GROUP_REKEYNEGOTIATING:
    2628         112 :                 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
    2629          68 :                     !sm->EAPOLKeyPairwise && sm->MICVerified)
    2630          34 :                         SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
    2631          88 :                 else if (sm->GTimeoutCtr >
    2632          44 :                          (int) dot11RSNAConfigGroupUpdateCount)
    2633           0 :                         SM_ENTER(WPA_PTK_GROUP, KEYERROR);
    2634          44 :                 else if (sm->TimeoutEvt)
    2635           1 :                         SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
    2636          78 :                 break;
    2637             :         case WPA_PTK_GROUP_KEYERROR:
    2638           0 :                 SM_ENTER(WPA_PTK_GROUP, IDLE);
    2639           0 :                 break;
    2640             :         case WPA_PTK_GROUP_REKEYESTABLISHED:
    2641          34 :                 SM_ENTER(WPA_PTK_GROUP, IDLE);
    2642          34 :                 break;
    2643             :         }
    2644       43404 : }
    2645             : 
    2646             : 
    2647        2163 : static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
    2648             :                           struct wpa_group *group)
    2649             : {
    2650        2163 :         int ret = 0;
    2651             : 
    2652        2163 :         os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
    2653        2163 :         inc_byte_array(group->Counter, WPA_NONCE_LEN);
    2654        4326 :         if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
    2655        2163 :                            wpa_auth->addr, group->GNonce,
    2656        4326 :                            group->GTK[group->GN - 1], group->GTK_len) < 0)
    2657           1 :                 ret = -1;
    2658        4326 :         wpa_hexdump_key(MSG_DEBUG, "GTK",
    2659        4326 :                         group->GTK[group->GN - 1], group->GTK_len);
    2660             : 
    2661             : #ifdef CONFIG_IEEE80211W
    2662        2163 :         if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
    2663             :                 size_t len;
    2664         286 :                 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
    2665         286 :                 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
    2666         286 :                 inc_byte_array(group->Counter, WPA_NONCE_LEN);
    2667         286 :                 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
    2668         286 :                                    wpa_auth->addr, group->GNonce,
    2669         286 :                                    group->IGTK[group->GN_igtk - 4], len) < 0)
    2670           0 :                         ret = -1;
    2671         286 :                 wpa_hexdump_key(MSG_DEBUG, "IGTK",
    2672         286 :                                 group->IGTK[group->GN_igtk - 4], len);
    2673             :         }
    2674             : #endif /* CONFIG_IEEE80211W */
    2675             : 
    2676        2163 :         return ret;
    2677             : }
    2678             : 
    2679             : 
    2680        1295 : static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
    2681             :                                struct wpa_group *group)
    2682             : {
    2683        1295 :         wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
    2684             :                    "GTK_INIT (VLAN-ID %d)", group->vlan_id);
    2685        1295 :         group->changed = FALSE; /* GInit is not cleared here; avoid loop */
    2686        1295 :         group->wpa_group_state = WPA_GROUP_GTK_INIT;
    2687             : 
    2688             :         /* GTK[0..N] = 0 */
    2689        1295 :         os_memset(group->GTK, 0, sizeof(group->GTK));
    2690        1295 :         group->GN = 1;
    2691        1295 :         group->GM = 2;
    2692             : #ifdef CONFIG_IEEE80211W
    2693        1295 :         group->GN_igtk = 4;
    2694        1295 :         group->GM_igtk = 5;
    2695             : #endif /* CONFIG_IEEE80211W */
    2696             :         /* GTK[GN] = CalcGTK() */
    2697        1295 :         wpa_gtk_update(wpa_auth, group);
    2698        1295 : }
    2699             : 
    2700             : 
    2701          11 : static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
    2702             : {
    2703          11 :         if (ctx != NULL && ctx != sm->group)
    2704           0 :                 return 0;
    2705             : 
    2706          11 :         if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
    2707           1 :                 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    2708             :                                 "Not in PTKINITDONE; skip Group Key update");
    2709           1 :                 sm->GUpdateStationKeys = FALSE;
    2710           1 :                 return 0;
    2711             :         }
    2712          10 :         if (sm->GUpdateStationKeys) {
    2713             :                 /*
    2714             :                  * This should not really happen, so add a debug log entry.
    2715             :                  * Since we clear the GKeyDoneStations before the loop, the
    2716             :                  * station needs to be counted here anyway.
    2717             :                  */
    2718           0 :                 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    2719             :                                 "GUpdateStationKeys was already set when "
    2720             :                                 "marking station for GTK rekeying");
    2721             :         }
    2722             : 
    2723             :         /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
    2724          10 :         if (sm->is_wnmsleep)
    2725           0 :                 return 0;
    2726             : 
    2727          10 :         sm->group->GKeyDoneStations++;
    2728          10 :         sm->GUpdateStationKeys = TRUE;
    2729             : 
    2730          10 :         wpa_sm_step(sm);
    2731          10 :         return 0;
    2732             : }
    2733             : 
    2734             : 
    2735             : #ifdef CONFIG_WNM
    2736             : /* update GTK when exiting WNM-Sleep Mode */
    2737           4 : void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
    2738             : {
    2739           4 :         if (sm == NULL || sm->is_wnmsleep)
    2740           7 :                 return;
    2741             : 
    2742           1 :         wpa_group_update_sta(sm, NULL);
    2743             : }
    2744             : 
    2745             : 
    2746          10 : void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
    2747             : {
    2748          10 :         if (sm)
    2749           4 :                 sm->is_wnmsleep = !!flag;
    2750          10 : }
    2751             : 
    2752             : 
    2753           1 : int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
    2754             : {
    2755           1 :         struct wpa_group *gsm = sm->group;
    2756           1 :         u8 *start = pos;
    2757             : 
    2758             :         /*
    2759             :          * GTK subelement:
    2760             :          * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
    2761             :          * Key[5..32]
    2762             :          */
    2763           1 :         *pos++ = WNM_SLEEP_SUBELEM_GTK;
    2764           1 :         *pos++ = 11 + gsm->GTK_len;
    2765             :         /* Key ID in B0-B1 of Key Info */
    2766           1 :         WPA_PUT_LE16(pos, gsm->GN & 0x03);
    2767           1 :         pos += 2;
    2768           1 :         *pos++ = gsm->GTK_len;
    2769           1 :         if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
    2770           0 :                 return 0;
    2771           1 :         pos += 8;
    2772           1 :         os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
    2773           1 :         pos += gsm->GTK_len;
    2774             : 
    2775           1 :         wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
    2776             :                    gsm->GN);
    2777           2 :         wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
    2778           2 :                         gsm->GTK[gsm->GN - 1], gsm->GTK_len);
    2779             : 
    2780           1 :         return pos - start;
    2781             : }
    2782             : 
    2783             : 
    2784             : #ifdef CONFIG_IEEE80211W
    2785           1 : int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
    2786             : {
    2787           1 :         struct wpa_group *gsm = sm->group;
    2788           1 :         u8 *start = pos;
    2789           1 :         size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
    2790             : 
    2791             :         /*
    2792             :          * IGTK subelement:
    2793             :          * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
    2794             :          */
    2795           1 :         *pos++ = WNM_SLEEP_SUBELEM_IGTK;
    2796           1 :         *pos++ = 2 + 6 + len;
    2797           1 :         WPA_PUT_LE16(pos, gsm->GN_igtk);
    2798           1 :         pos += 2;
    2799           1 :         if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
    2800           0 :                 return 0;
    2801           1 :         pos += 6;
    2802             : 
    2803           1 :         os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
    2804           1 :         pos += len;
    2805             : 
    2806           1 :         wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
    2807             :                    gsm->GN_igtk);
    2808           1 :         wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
    2809           1 :                         gsm->IGTK[gsm->GN_igtk - 4], len);
    2810             : 
    2811           1 :         return pos - start;
    2812             : }
    2813             : #endif /* CONFIG_IEEE80211W */
    2814             : #endif /* CONFIG_WNM */
    2815             : 
    2816             : 
    2817          10 : static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
    2818             :                               struct wpa_group *group)
    2819             : {
    2820             :         int tmp;
    2821             : 
    2822          10 :         wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
    2823             :                    "SETKEYS (VLAN-ID %d)", group->vlan_id);
    2824          10 :         group->changed = TRUE;
    2825          10 :         group->wpa_group_state = WPA_GROUP_SETKEYS;
    2826          10 :         group->GTKReKey = FALSE;
    2827          10 :         tmp = group->GM;
    2828          10 :         group->GM = group->GN;
    2829          10 :         group->GN = tmp;
    2830             : #ifdef CONFIG_IEEE80211W
    2831          10 :         tmp = group->GM_igtk;
    2832          10 :         group->GM_igtk = group->GN_igtk;
    2833          10 :         group->GN_igtk = tmp;
    2834             : #endif /* CONFIG_IEEE80211W */
    2835             :         /* "GKeyDoneStations = GNoStations" is done in more robust way by
    2836             :          * counting the STAs that are marked with GUpdateStationKeys instead of
    2837             :          * including all STAs that could be in not-yet-completed state. */
    2838          10 :         wpa_gtk_update(wpa_auth, group);
    2839             : 
    2840          10 :         if (group->GKeyDoneStations) {
    2841           0 :                 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
    2842             :                            "GKeyDoneStations=%d when starting new GTK rekey",
    2843             :                            group->GKeyDoneStations);
    2844           0 :                 group->GKeyDoneStations = 0;
    2845             :         }
    2846          10 :         wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
    2847          10 :         wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
    2848             :                    group->GKeyDoneStations);
    2849          10 : }
    2850             : 
    2851             : 
    2852        2162 : static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
    2853             :                                        struct wpa_group *group)
    2854             : {
    2855        2162 :         int ret = 0;
    2856             : 
    2857        6486 :         if (wpa_auth_set_key(wpa_auth, group->vlan_id,
    2858        2162 :                              wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
    2859             :                              broadcast_ether_addr, group->GN,
    2860        4324 :                              group->GTK[group->GN - 1], group->GTK_len) < 0)
    2861           0 :                 ret = -1;
    2862             : 
    2863             : #ifdef CONFIG_IEEE80211W
    2864        2162 :         if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
    2865             :                 enum wpa_alg alg;
    2866             :                 size_t len;
    2867             : 
    2868         286 :                 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
    2869         286 :                 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
    2870             : 
    2871         572 :                 if (ret == 0 &&
    2872         286 :                     wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
    2873             :                                      broadcast_ether_addr, group->GN_igtk,
    2874         286 :                                      group->IGTK[group->GN_igtk - 4], len) < 0)
    2875           0 :                         ret = -1;
    2876             :         }
    2877             : #endif /* CONFIG_IEEE80211W */
    2878             : 
    2879        2162 :         return ret;
    2880             : }
    2881             : 
    2882             : 
    2883           0 : static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
    2884             : {
    2885           0 :         if (sm->group == ctx) {
    2886           0 :                 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
    2887             :                            " for discconnection due to fatal failure",
    2888           0 :                            MAC2STR(sm->addr));
    2889           0 :                 sm->Disconnect = TRUE;
    2890             :         }
    2891             : 
    2892           0 :         return 0;
    2893             : }
    2894             : 
    2895             : 
    2896           0 : static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
    2897             :                                     struct wpa_group *group)
    2898             : {
    2899           0 :         wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
    2900           0 :         group->changed = TRUE;
    2901           0 :         group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
    2902           0 :         wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
    2903           0 : }
    2904             : 
    2905             : 
    2906        1304 : static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
    2907             :                                  struct wpa_group *group)
    2908             : {
    2909        1304 :         wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
    2910             :                    "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
    2911        1304 :         group->changed = TRUE;
    2912        1304 :         group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
    2913             : 
    2914        1304 :         if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
    2915           0 :                 wpa_group_fatal_failure(wpa_auth, group);
    2916           0 :                 return -1;
    2917             :         }
    2918             : 
    2919        1304 :         return 0;
    2920             : }
    2921             : 
    2922             : 
    2923       46006 : static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
    2924             :                               struct wpa_group *group)
    2925             : {
    2926       46006 :         if (group->GInit) {
    2927        1295 :                 wpa_group_gtk_init(wpa_auth, group);
    2928       44711 :         } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
    2929             :                 /* Do not allow group operations */
    2930       46006 :         } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
    2931        1295 :                    group->GTKAuthenticator) {
    2932        1295 :                 wpa_group_setkeysdone(wpa_auth, group);
    2933       86803 :         } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
    2934       43387 :                    group->GTKReKey) {
    2935          10 :                 wpa_group_setkeys(wpa_auth, group);
    2936       43406 :         } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
    2937          29 :                 if (group->GKeyDoneStations == 0)
    2938           9 :                         wpa_group_setkeysdone(wpa_auth, group);
    2939          20 :                 else if (group->GTKReKey)
    2940           0 :                         wpa_group_setkeys(wpa_auth, group);
    2941             :         }
    2942       46006 : }
    2943             : 
    2944             : 
    2945       26344 : static int wpa_sm_step(struct wpa_state_machine *sm)
    2946             : {
    2947       26344 :         if (sm == NULL)
    2948           0 :                 return 0;
    2949             : 
    2950       26344 :         if (sm->in_step_loop) {
    2951             :                 /* This should not happen, but if it does, make sure we do not
    2952             :                  * end up freeing the state machine too early by exiting the
    2953             :                  * recursive call. */
    2954           0 :                 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
    2955           0 :                 return 0;
    2956             :         }
    2957             : 
    2958       26344 :         sm->in_step_loop = 1;
    2959             :         do {
    2960       43404 :                 if (sm->pending_deinit)
    2961           0 :                         break;
    2962             : 
    2963       43404 :                 sm->changed = FALSE;
    2964       43404 :                 sm->wpa_auth->group->changed = FALSE;
    2965             : 
    2966       43404 :                 SM_STEP_RUN(WPA_PTK);
    2967       43404 :                 if (sm->pending_deinit)
    2968           0 :                         break;
    2969       43404 :                 SM_STEP_RUN(WPA_PTK_GROUP);
    2970       43404 :                 if (sm->pending_deinit)
    2971           0 :                         break;
    2972       43404 :                 wpa_group_sm_step(sm->wpa_auth, sm->group);
    2973       43404 :         } while (sm->changed || sm->wpa_auth->group->changed);
    2974       26344 :         sm->in_step_loop = 0;
    2975             : 
    2976       26344 :         if (sm->pending_deinit) {
    2977           0 :                 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
    2978           0 :                            "machine deinit for " MACSTR, MAC2STR(sm->addr));
    2979           0 :                 wpa_free_sta_sm(sm);
    2980           0 :                 return 1;
    2981             :         }
    2982       26344 :         return 0;
    2983             : }
    2984             : 
    2985             : 
    2986       15274 : static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
    2987             : {
    2988       15274 :         struct wpa_state_machine *sm = eloop_ctx;
    2989       15274 :         wpa_sm_step(sm);
    2990       15274 : }
    2991             : 
    2992             : 
    2993       19156 : void wpa_auth_sm_notify(struct wpa_state_machine *sm)
    2994             : {
    2995       19156 :         if (sm == NULL)
    2996       22985 :                 return;
    2997       15327 :         eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
    2998             : }
    2999             : 
    3000             : 
    3001           3 : void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
    3002             : {
    3003             :         int tmp, i;
    3004             :         struct wpa_group *group;
    3005             : 
    3006           3 :         if (wpa_auth == NULL)
    3007           3 :                 return;
    3008             : 
    3009           3 :         group = wpa_auth->group;
    3010             : 
    3011           9 :         for (i = 0; i < 2; i++) {
    3012           6 :                 tmp = group->GM;
    3013           6 :                 group->GM = group->GN;
    3014           6 :                 group->GN = tmp;
    3015             : #ifdef CONFIG_IEEE80211W
    3016           6 :                 tmp = group->GM_igtk;
    3017           6 :                 group->GM_igtk = group->GN_igtk;
    3018           6 :                 group->GN_igtk = tmp;
    3019             : #endif /* CONFIG_IEEE80211W */
    3020           6 :                 wpa_gtk_update(wpa_auth, group);
    3021           6 :                 wpa_group_config_group_keys(wpa_auth, group);
    3022             :         }
    3023             : }
    3024             : 
    3025             : 
    3026          24 : static const char * wpa_bool_txt(int val)
    3027             : {
    3028          24 :         return val ? "TRUE" : "FALSE";
    3029             : }
    3030             : 
    3031             : 
    3032             : #define RSN_SUITE "%02x-%02x-%02x-%d"
    3033             : #define RSN_SUITE_ARG(s) \
    3034             : ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
    3035             : 
    3036           8 : int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
    3037             : {
    3038           8 :         int len = 0, ret;
    3039             :         char pmkid_txt[PMKID_LEN * 2 + 1];
    3040             : #ifdef CONFIG_RSN_PREAUTH
    3041           8 :         const int preauth = 1;
    3042             : #else /* CONFIG_RSN_PREAUTH */
    3043           0 :         const int preauth = 0;
    3044             : #endif /* CONFIG_RSN_PREAUTH */
    3045             : 
    3046           8 :         if (wpa_auth == NULL)
    3047           0 :                 return len;
    3048             : 
    3049          16 :         ret = os_snprintf(buf + len, buflen - len,
    3050             :                           "dot11RSNAOptionImplemented=TRUE\n"
    3051             :                           "dot11RSNAPreauthenticationImplemented=%s\n"
    3052             :                           "dot11RSNAEnabled=%s\n"
    3053             :                           "dot11RSNAPreauthenticationEnabled=%s\n",
    3054             :                           wpa_bool_txt(preauth),
    3055           8 :                           wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
    3056             :                           wpa_bool_txt(wpa_auth->conf.rsn_preauth));
    3057           8 :         if (os_snprintf_error(buflen - len, ret))
    3058           0 :                 return len;
    3059           8 :         len += ret;
    3060             : 
    3061           8 :         wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
    3062           8 :                          wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
    3063             : 
    3064         216 :         ret = os_snprintf(
    3065             :                 buf + len, buflen - len,
    3066             :                 "dot11RSNAConfigVersion=%u\n"
    3067             :                 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
    3068             :                 /* FIX: dot11RSNAConfigGroupCipher */
    3069             :                 /* FIX: dot11RSNAConfigGroupRekeyMethod */
    3070             :                 /* FIX: dot11RSNAConfigGroupRekeyTime */
    3071             :                 /* FIX: dot11RSNAConfigGroupRekeyPackets */
    3072             :                 "dot11RSNAConfigGroupRekeyStrict=%u\n"
    3073             :                 "dot11RSNAConfigGroupUpdateCount=%u\n"
    3074             :                 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
    3075             :                 "dot11RSNAConfigGroupCipherSize=%u\n"
    3076             :                 "dot11RSNAConfigPMKLifetime=%u\n"
    3077             :                 "dot11RSNAConfigPMKReauthThreshold=%u\n"
    3078             :                 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
    3079             :                 "dot11RSNAConfigSATimeout=%u\n"
    3080             :                 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
    3081             :                 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
    3082             :                 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
    3083             :                 "dot11RSNAPMKIDUsed=%s\n"
    3084             :                 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
    3085             :                 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
    3086             :                 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
    3087             :                 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
    3088             :                 "dot11RSNA4WayHandshakeFailures=%u\n"
    3089             :                 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
    3090             :                 RSN_VERSION,
    3091           8 :                 !!wpa_auth->conf.wpa_strict_rekey,
    3092             :                 dot11RSNAConfigGroupUpdateCount,
    3093             :                 dot11RSNAConfigPairwiseUpdateCount,
    3094           8 :                 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
    3095             :                 dot11RSNAConfigPMKLifetime,
    3096             :                 dot11RSNAConfigPMKReauthThreshold,
    3097             :                 dot11RSNAConfigSATimeout,
    3098          32 :                 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
    3099          32 :                 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
    3100          32 :                 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
    3101             :                 pmkid_txt,
    3102          32 :                 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
    3103          32 :                 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
    3104          32 :                 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
    3105             :                 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
    3106             :                 wpa_auth->dot11RSNA4WayHandshakeFailures);
    3107           8 :         if (os_snprintf_error(buflen - len, ret))
    3108           0 :                 return len;
    3109           8 :         len += ret;
    3110             : 
    3111             :         /* TODO: dot11RSNAConfigPairwiseCiphersTable */
    3112             :         /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
    3113             : 
    3114             :         /* Private MIB */
    3115           8 :         ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
    3116           8 :                           wpa_auth->group->wpa_group_state);
    3117           8 :         if (os_snprintf_error(buflen - len, ret))
    3118           0 :                 return len;
    3119           8 :         len += ret;
    3120             : 
    3121           8 :         return len;
    3122             : }
    3123             : 
    3124             : 
    3125         202 : int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
    3126             : {
    3127         202 :         int len = 0, ret;
    3128         202 :         u32 pairwise = 0;
    3129             : 
    3130         202 :         if (sm == NULL)
    3131         172 :                 return 0;
    3132             : 
    3133             :         /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
    3134             : 
    3135             :         /* dot11RSNAStatsEntry */
    3136             : 
    3137          30 :         pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
    3138             :                                        WPA_PROTO_RSN : WPA_PROTO_WPA,
    3139             :                                        sm->pairwise);
    3140          30 :         if (pairwise == 0)
    3141           0 :                 return 0;
    3142             : 
    3143         270 :         ret = os_snprintf(
    3144             :                 buf + len, buflen - len,
    3145             :                 /* TODO: dot11RSNAStatsIndex */
    3146             :                 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
    3147             :                 "dot11RSNAStatsVersion=1\n"
    3148             :                 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
    3149             :                 /* TODO: dot11RSNAStatsTKIPICVErrors */
    3150             :                 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
    3151             :                 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
    3152             :                 /* TODO: dot11RSNAStatsCCMPReplays */
    3153             :                 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
    3154             :                 /* TODO: dot11RSNAStatsTKIPReplays */,
    3155         180 :                 MAC2STR(sm->addr),
    3156          60 :                 RSN_SUITE_ARG(pairwise),
    3157             :                 sm->dot11RSNAStatsTKIPLocalMICFailures,
    3158             :                 sm->dot11RSNAStatsTKIPRemoteMICFailures);
    3159          30 :         if (os_snprintf_error(buflen - len, ret))
    3160           0 :                 return len;
    3161          30 :         len += ret;
    3162             : 
    3163             :         /* Private MIB */
    3164          30 :         ret = os_snprintf(buf + len, buflen - len,
    3165             :                           "hostapdWPAPTKState=%d\n"
    3166             :                           "hostapdWPAPTKGroupState=%d\n",
    3167          30 :                           sm->wpa_ptk_state,
    3168          30 :                           sm->wpa_ptk_group_state);
    3169          30 :         if (os_snprintf_error(buflen - len, ret))
    3170           0 :                 return len;
    3171          30 :         len += ret;
    3172             : 
    3173          30 :         return len;
    3174             : }
    3175             : 
    3176             : 
    3177           3 : void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
    3178             : {
    3179           3 :         if (wpa_auth)
    3180           3 :                 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
    3181           3 : }
    3182             : 
    3183             : 
    3184        8377 : int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
    3185             : {
    3186        8377 :         return sm && sm->pairwise_set;
    3187             : }
    3188             : 
    3189             : 
    3190        6303 : int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
    3191             : {
    3192        6303 :         return sm->pairwise;
    3193             : }
    3194             : 
    3195             : 
    3196       12648 : int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
    3197             : {
    3198       12648 :         if (sm == NULL)
    3199        2781 :                 return -1;
    3200        9867 :         return sm->wpa_key_mgmt;
    3201             : }
    3202             : 
    3203             : 
    3204        4130 : int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
    3205             : {
    3206        4130 :         if (sm == NULL)
    3207           0 :                 return 0;
    3208        4130 :         return sm->wpa;
    3209             : }
    3210             : 
    3211             : 
    3212           1 : int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
    3213             :                              struct rsn_pmksa_cache_entry *entry)
    3214             : {
    3215           1 :         if (sm == NULL || sm->pmksa != entry)
    3216           0 :                 return -1;
    3217           1 :         sm->pmksa = NULL;
    3218           1 :         return 0;
    3219             : }
    3220             : 
    3221             : 
    3222             : struct rsn_pmksa_cache_entry *
    3223        2818 : wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
    3224             : {
    3225        2818 :         return sm ? sm->pmksa : NULL;
    3226             : }
    3227             : 
    3228             : 
    3229           2 : void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
    3230             : {
    3231           2 :         if (sm)
    3232           2 :                 sm->dot11RSNAStatsTKIPLocalMICFailures++;
    3233           2 : }
    3234             : 
    3235             : 
    3236        9841 : const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
    3237             : {
    3238        9841 :         if (wpa_auth == NULL)
    3239        2662 :                 return NULL;
    3240        7179 :         *len = wpa_auth->wpa_ie_len;
    3241        7179 :         return wpa_auth->wpa_ie;
    3242             : }
    3243             : 
    3244             : 
    3245         725 : int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
    3246             :                        int session_timeout, struct eapol_state_machine *eapol)
    3247             : {
    3248        1432 :         if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
    3249         707 :             sm->wpa_auth->conf.disable_pmksa_caching)
    3250          61 :                 return -1;
    3251             : 
    3252        1992 :         if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
    3253         664 :                                  sm->PTK.kck, sm->PTK.kck_len,
    3254         664 :                                  sm->wpa_auth->addr, sm->addr, session_timeout,
    3255             :                                  eapol, sm->wpa_key_mgmt))
    3256         663 :                 return 0;
    3257             : 
    3258           1 :         return -1;
    3259             : }
    3260             : 
    3261             : 
    3262           4 : int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
    3263             :                                const u8 *pmk, size_t len, const u8 *sta_addr,
    3264             :                                int session_timeout,
    3265             :                                struct eapol_state_machine *eapol)
    3266             : {
    3267           4 :         if (wpa_auth == NULL)
    3268           0 :                 return -1;
    3269             : 
    3270           4 :         if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len,
    3271             :                                  NULL, 0,
    3272           4 :                                  wpa_auth->addr,
    3273             :                                  sta_addr, session_timeout, eapol,
    3274             :                                  WPA_KEY_MGMT_IEEE8021X))
    3275           4 :                 return 0;
    3276             : 
    3277           0 :         return -1;
    3278             : }
    3279             : 
    3280             : 
    3281          68 : int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
    3282             :                            const u8 *pmk)
    3283             : {
    3284          68 :         if (wpa_auth->conf.disable_pmksa_caching)
    3285           2 :                 return -1;
    3286             : 
    3287          66 :         if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN,
    3288             :                                  NULL, 0,
    3289          66 :                                  wpa_auth->addr, addr, 0, NULL,
    3290             :                                  WPA_KEY_MGMT_SAE))
    3291          66 :                 return 0;
    3292             : 
    3293           0 :         return -1;
    3294             : }
    3295             : 
    3296             : 
    3297          10 : void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
    3298             :                            const u8 *sta_addr)
    3299             : {
    3300             :         struct rsn_pmksa_cache_entry *pmksa;
    3301             : 
    3302          10 :         if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
    3303          12 :                 return;
    3304           8 :         pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
    3305           8 :         if (pmksa) {
    3306          42 :                 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
    3307          42 :                            MACSTR " based on request", MAC2STR(sta_addr));
    3308           7 :                 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
    3309             :         }
    3310             : }
    3311             : 
    3312             : 
    3313             : /*
    3314             :  * Remove and free the group from wpa_authenticator. This is triggered by a
    3315             :  * callback to make sure nobody is currently iterating the group list while it
    3316             :  * gets modified.
    3317             :  */
    3318          15 : static void wpa_group_free(struct wpa_authenticator *wpa_auth,
    3319             :                            struct wpa_group *group)
    3320             : {
    3321          15 :         struct wpa_group *prev = wpa_auth->group;
    3322             : 
    3323          15 :         wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
    3324             :                    group->vlan_id);
    3325             : 
    3326          36 :         while (prev) {
    3327          21 :                 if (prev->next == group) {
    3328             :                         /* This never frees the special first group as needed */
    3329          15 :                         prev->next = group->next;
    3330          15 :                         os_free(group);
    3331          15 :                         break;
    3332             :                 }
    3333           6 :                 prev = prev->next;
    3334             :         }
    3335             : 
    3336          15 : }
    3337             : 
    3338             : 
    3339             : /* Increase the reference counter for group */
    3340        1910 : static void wpa_group_get(struct wpa_authenticator *wpa_auth,
    3341             :                           struct wpa_group *group)
    3342             : {
    3343             :         /* Skip the special first group */
    3344        1910 :         if (wpa_auth->group == group)
    3345        3805 :                 return;
    3346             : 
    3347          15 :         group->references++;
    3348             : }
    3349             : 
    3350             : 
    3351             : /* Decrease the reference counter and maybe free the group */
    3352        1910 : static void wpa_group_put(struct wpa_authenticator *wpa_auth,
    3353             :                           struct wpa_group *group)
    3354             : {
    3355             :         /* Skip the special first group */
    3356        1910 :         if (wpa_auth->group == group)
    3357        1895 :                 return;
    3358             : 
    3359          15 :         group->references--;
    3360          15 :         if (group->references)
    3361           0 :                 return;
    3362          15 :         wpa_group_free(wpa_auth, group);
    3363             : }
    3364             : 
    3365             : 
    3366             : /*
    3367             :  * Add a group that has its references counter set to zero. Caller needs to
    3368             :  * call wpa_group_get() on the return value to mark the entry in use.
    3369             :  */
    3370             : static struct wpa_group *
    3371          15 : wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
    3372             : {
    3373             :         struct wpa_group *group;
    3374             : 
    3375          15 :         if (wpa_auth == NULL || wpa_auth->group == NULL)
    3376           0 :                 return NULL;
    3377             : 
    3378          15 :         wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
    3379             :                    vlan_id);
    3380          15 :         group = wpa_group_init(wpa_auth, vlan_id, 0);
    3381          15 :         if (group == NULL)
    3382           0 :                 return NULL;
    3383             : 
    3384          15 :         group->next = wpa_auth->group->next;
    3385          15 :         wpa_auth->group->next = group;
    3386             : 
    3387          15 :         return group;
    3388             : }
    3389             : 
    3390             : 
    3391        3463 : int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
    3392             : {
    3393             :         struct wpa_group *group;
    3394             : 
    3395        3463 :         if (sm == NULL || sm->wpa_auth == NULL)
    3396         911 :                 return 0;
    3397             : 
    3398        2552 :         group = sm->wpa_auth->group;
    3399        5125 :         while (group) {
    3400        2558 :                 if (group->vlan_id == vlan_id)
    3401        2537 :                         break;
    3402          21 :                 group = group->next;
    3403             :         }
    3404             : 
    3405        2552 :         if (group == NULL) {
    3406          15 :                 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
    3407          15 :                 if (group == NULL)
    3408           0 :                         return -1;
    3409             :         }
    3410             : 
    3411        2552 :         if (sm->group == group)
    3412        2537 :                 return 0;
    3413             : 
    3414          15 :         if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
    3415           0 :                 return -1;
    3416             : 
    3417          90 :         wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
    3418          90 :                    "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
    3419             : 
    3420          15 :         wpa_group_get(sm->wpa_auth, group);
    3421          15 :         wpa_group_put(sm->wpa_auth, sm->group);
    3422          15 :         sm->group = group;
    3423             : 
    3424          15 :         return 0;
    3425             : }
    3426             : 
    3427             : 
    3428        2687 : void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
    3429             :                                   struct wpa_state_machine *sm, int ack)
    3430             : {
    3431        2687 :         if (wpa_auth == NULL || sm == NULL)
    3432        2687 :                 return;
    3433       16122 :         wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
    3434       16122 :                    " ack=%d", MAC2STR(sm->addr), ack);
    3435        2687 :         if (sm->pending_1_of_4_timeout && ack) {
    3436             :                 /*
    3437             :                  * Some deployed supplicant implementations update their SNonce
    3438             :                  * for each EAPOL-Key 2/4 message even within the same 4-way
    3439             :                  * handshake and then fail to use the first SNonce when
    3440             :                  * deriving the PTK. This results in unsuccessful 4-way
    3441             :                  * handshake whenever the relatively short initial timeout is
    3442             :                  * reached and EAPOL-Key 1/4 is retransmitted. Try to work
    3443             :                  * around this by increasing the timeout now that we know that
    3444             :                  * the station has received the frame.
    3445             :                  */
    3446        1317 :                 int timeout_ms = eapol_key_timeout_subseq;
    3447        1317 :                 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
    3448             :                            "timeout by %u ms because of acknowledged frame",
    3449             :                            timeout_ms);
    3450        1317 :                 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
    3451        1317 :                 eloop_register_timeout(timeout_ms / 1000,
    3452        1317 :                                        (timeout_ms % 1000) * 1000,
    3453             :                                        wpa_send_eapol_timeout, wpa_auth, sm);
    3454             :         }
    3455             : }
    3456             : 
    3457             : 
    3458        4394 : int wpa_auth_uses_sae(struct wpa_state_machine *sm)
    3459             : {
    3460        4394 :         if (sm == NULL)
    3461           0 :                 return 0;
    3462        4394 :         return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
    3463             : }
    3464             : 
    3465             : 
    3466           4 : int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
    3467             : {
    3468           4 :         if (sm == NULL)
    3469           0 :                 return 0;
    3470           4 :         return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
    3471             : }
    3472             : 
    3473             : 
    3474             : #ifdef CONFIG_P2P
    3475         231 : int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
    3476             : {
    3477         231 :         if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
    3478         136 :                 return -1;
    3479          95 :         os_memcpy(addr, sm->ip_addr, 4);
    3480          95 :         return 0;
    3481             : }
    3482             : #endif /* CONFIG_P2P */
    3483             : 
    3484             : 
    3485          13 : int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
    3486             :                                          struct radius_das_attrs *attr)
    3487             : {
    3488          13 :         return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
    3489             : }
    3490             : 
    3491             : 
    3492           3 : void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
    3493             : {
    3494             :         struct wpa_group *group;
    3495             : 
    3496           3 :         if (!wpa_auth)
    3497           5 :                 return;
    3498           2 :         for (group = wpa_auth->group; group; group = group->next)
    3499           1 :                 wpa_group_config_group_keys(wpa_auth, group);
    3500             : }

Generated by: LCOV version 1.10