Home | History | Annotate | Line # | Download | only in wpa_supplicant
mesh_rsn.c revision 1.1.1.2.20.1
      1           1.1  christos /*
      2           1.1  christos  * WPA Supplicant - Mesh RSN routines
      3           1.1  christos  * Copyright (c) 2013-2014, cozybit, Inc.  All rights reserved.
      4           1.1  christos  *
      5           1.1  christos  * This software may be distributed under the terms of the BSD license.
      6           1.1  christos  * See README for more details.
      7           1.1  christos  */
      8           1.1  christos 
      9           1.1  christos #include "utils/includes.h"
     10           1.1  christos 
     11           1.1  christos #include "utils/common.h"
     12           1.1  christos #include "utils/eloop.h"
     13           1.1  christos #include "crypto/sha256.h"
     14           1.1  christos #include "crypto/random.h"
     15           1.1  christos #include "crypto/aes.h"
     16           1.1  christos #include "crypto/aes_siv.h"
     17           1.1  christos #include "rsn_supp/wpa.h"
     18           1.1  christos #include "ap/hostapd.h"
     19           1.1  christos #include "ap/wpa_auth.h"
     20           1.1  christos #include "ap/sta_info.h"
     21           1.1  christos #include "ap/ieee802_11.h"
     22           1.1  christos #include "wpa_supplicant_i.h"
     23           1.1  christos #include "driver_i.h"
     24           1.1  christos #include "wpas_glue.h"
     25           1.1  christos #include "mesh_mpm.h"
     26           1.1  christos #include "mesh_rsn.h"
     27           1.1  christos 
     28           1.1  christos #define MESH_AUTH_TIMEOUT 10
     29           1.1  christos #define MESH_AUTH_RETRY 3
     30           1.1  christos 
     31           1.1  christos void mesh_auth_timer(void *eloop_ctx, void *user_data)
     32           1.1  christos {
     33           1.1  christos 	struct wpa_supplicant *wpa_s = eloop_ctx;
     34           1.1  christos 	struct sta_info *sta = user_data;
     35       1.1.1.2  christos 	struct hostapd_data *hapd;
     36           1.1  christos 
     37           1.1  christos 	if (sta->sae->state != SAE_ACCEPTED) {
     38           1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: Re-authenticate with " MACSTR
     39           1.1  christos 			   " (attempt %d) ",
     40           1.1  christos 			   MAC2STR(sta->addr), sta->sae_auth_retry);
     41           1.1  christos 		wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_FAILURE "addr=" MACSTR,
     42           1.1  christos 			MAC2STR(sta->addr));
     43           1.1  christos 		if (sta->sae_auth_retry < MESH_AUTH_RETRY) {
     44           1.1  christos 			mesh_rsn_auth_sae_sta(wpa_s, sta);
     45           1.1  christos 		} else {
     46       1.1.1.2  christos 			hapd = wpa_s->ifmsh->bss[0];
     47       1.1.1.2  christos 
     48           1.1  christos 			if (sta->sae_auth_retry > MESH_AUTH_RETRY) {
     49       1.1.1.2  christos 				ap_free_sta(hapd, sta);
     50           1.1  christos 				return;
     51           1.1  christos 			}
     52           1.1  christos 
     53           1.1  christos 			/* block the STA if exceeded the number of attempts */
     54           1.1  christos 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_BLOCKED);
     55           1.1  christos 			sta->sae->state = SAE_NOTHING;
     56           1.1  christos 			wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_BLOCKED "addr="
     57           1.1  christos 				MACSTR " duration=%d",
     58           1.1  christos 				MAC2STR(sta->addr),
     59       1.1.1.2  christos 				hapd->conf->ap_max_inactivity);
     60           1.1  christos 		}
     61           1.1  christos 		sta->sae_auth_retry++;
     62           1.1  christos 	}
     63           1.1  christos }
     64           1.1  christos 
     65           1.1  christos 
     66           1.1  christos static void auth_logger(void *ctx, const u8 *addr, logger_level level,
     67           1.1  christos 			const char *txt)
     68           1.1  christos {
     69           1.1  christos 	if (addr)
     70           1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
     71           1.1  christos 			   MAC2STR(addr), txt);
     72           1.1  christos 	else
     73           1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
     74           1.1  christos }
     75           1.1  christos 
     76           1.1  christos 
     77           1.1  christos static const u8 *auth_get_psk(void *ctx, const u8 *addr,
     78  1.1.1.2.20.1  christos 			      const u8 *p2p_dev_addr, const u8 *prev_psk,
     79  1.1.1.2.20.1  christos 			      size_t *psk_len)
     80           1.1  christos {
     81           1.1  christos 	struct mesh_rsn *mesh_rsn = ctx;
     82           1.1  christos 	struct hostapd_data *hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
     83           1.1  christos 	struct sta_info *sta = ap_get_sta(hapd, addr);
     84           1.1  christos 
     85  1.1.1.2.20.1  christos 	if (psk_len)
     86  1.1.1.2.20.1  christos 		*psk_len = PMK_LEN;
     87           1.1  christos 	wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
     88           1.1  christos 		   __func__, MAC2STR(addr), prev_psk);
     89           1.1  christos 
     90           1.1  christos 	if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
     91           1.1  christos 		if (!sta->sae || prev_psk)
     92           1.1  christos 			return NULL;
     93           1.1  christos 		return sta->sae->pmk;
     94           1.1  christos 	}
     95           1.1  christos 
     96           1.1  christos 	return NULL;
     97           1.1  christos }
     98           1.1  christos 
     99           1.1  christos 
    100           1.1  christos static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
    101           1.1  christos 			const u8 *addr, int idx, u8 *key, size_t key_len)
    102           1.1  christos {
    103           1.1  christos 	struct mesh_rsn *mesh_rsn = ctx;
    104           1.1  christos 	u8 seq[6];
    105           1.1  christos 
    106           1.1  christos 	os_memset(seq, 0, sizeof(seq));
    107           1.1  christos 
    108           1.1  christos 	if (addr) {
    109           1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
    110           1.1  christos 			   " key_idx=%d)",
    111           1.1  christos 			   __func__, alg, MAC2STR(addr), idx);
    112           1.1  christos 	} else {
    113           1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
    114           1.1  christos 			   __func__, alg, idx);
    115           1.1  christos 	}
    116           1.1  christos 	wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
    117           1.1  christos 
    118           1.1  christos 	return wpa_drv_set_key(mesh_rsn->wpa_s, alg, addr, idx,
    119           1.1  christos 			       1, seq, 6, key, key_len);
    120           1.1  christos }
    121           1.1  christos 
    122           1.1  christos 
    123           1.1  christos static int auth_start_ampe(void *ctx, const u8 *addr)
    124           1.1  christos {
    125           1.1  christos 	struct mesh_rsn *mesh_rsn = ctx;
    126           1.1  christos 	struct hostapd_data *hapd;
    127           1.1  christos 	struct sta_info *sta;
    128           1.1  christos 
    129           1.1  christos 	if (mesh_rsn->wpa_s->current_ssid->mode != WPAS_MODE_MESH)
    130           1.1  christos 		return -1;
    131           1.1  christos 
    132           1.1  christos 	hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
    133           1.1  christos 	sta = ap_get_sta(hapd, addr);
    134           1.1  christos 	if (sta)
    135           1.1  christos 		eloop_cancel_timeout(mesh_auth_timer, mesh_rsn->wpa_s, sta);
    136           1.1  christos 
    137           1.1  christos 	mesh_mpm_auth_peer(mesh_rsn->wpa_s, addr);
    138           1.1  christos 	return 0;
    139           1.1  christos }
    140           1.1  christos 
    141           1.1  christos 
    142       1.1.1.2  christos static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr,
    143       1.1.1.2  christos 				enum mfp_options ieee80211w)
    144           1.1  christos {
    145           1.1  christos 	struct wpa_auth_config conf;
    146  1.1.1.2.20.1  christos 	static const struct wpa_auth_callbacks cb = {
    147  1.1.1.2.20.1  christos 		.logger = auth_logger,
    148  1.1.1.2.20.1  christos 		.get_psk = auth_get_psk,
    149  1.1.1.2.20.1  christos 		.set_key = auth_set_key,
    150  1.1.1.2.20.1  christos 		.start_ampe = auth_start_ampe,
    151  1.1.1.2.20.1  christos 	};
    152           1.1  christos 	u8 seq[6] = {};
    153           1.1  christos 
    154           1.1  christos 	wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
    155           1.1  christos 
    156           1.1  christos 	os_memset(&conf, 0, sizeof(conf));
    157       1.1.1.2  christos 	conf.wpa = WPA_PROTO_RSN;
    158           1.1  christos 	conf.wpa_key_mgmt = WPA_KEY_MGMT_SAE;
    159       1.1.1.2  christos 	conf.wpa_pairwise = rsn->pairwise_cipher;
    160       1.1.1.2  christos 	conf.rsn_pairwise = rsn->pairwise_cipher;
    161       1.1.1.2  christos 	conf.wpa_group = rsn->group_cipher;
    162           1.1  christos 	conf.eapol_version = 0;
    163           1.1  christos 	conf.wpa_group_rekey = -1;
    164  1.1.1.2.20.1  christos 	conf.wpa_group_update_count = 4;
    165  1.1.1.2.20.1  christos 	conf.wpa_pairwise_update_count = 4;
    166       1.1.1.2  christos #ifdef CONFIG_IEEE80211W
    167       1.1.1.2  christos 	conf.ieee80211w = ieee80211w;
    168       1.1.1.2  christos 	if (ieee80211w != NO_MGMT_FRAME_PROTECTION)
    169       1.1.1.2  christos 		conf.group_mgmt_cipher = rsn->mgmt_group_cipher;
    170       1.1.1.2  christos #endif /* CONFIG_IEEE80211W */
    171           1.1  christos 
    172  1.1.1.2.20.1  christos 	rsn->auth = wpa_init(addr, &conf, &cb, rsn);
    173           1.1  christos 	if (rsn->auth == NULL) {
    174           1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
    175           1.1  christos 		return -1;
    176           1.1  christos 	}
    177           1.1  christos 
    178           1.1  christos 	/* TODO: support rekeying */
    179       1.1.1.2  christos 	rsn->mgtk_len = wpa_cipher_key_len(conf.wpa_group);
    180       1.1.1.2  christos 	if (random_get_bytes(rsn->mgtk, rsn->mgtk_len) < 0)
    181           1.1  christos 		return -1;
    182       1.1.1.2  christos 	rsn->mgtk_key_id = 1;
    183       1.1.1.2  christos 
    184       1.1.1.2  christos #ifdef CONFIG_IEEE80211W
    185       1.1.1.2  christos 	if (ieee80211w != NO_MGMT_FRAME_PROTECTION) {
    186       1.1.1.2  christos 		rsn->igtk_len = wpa_cipher_key_len(conf.group_mgmt_cipher);
    187       1.1.1.2  christos 		if (random_get_bytes(rsn->igtk, rsn->igtk_len) < 0)
    188       1.1.1.2  christos 			return -1;
    189       1.1.1.2  christos 		rsn->igtk_key_id = 4;
    190           1.1  christos 
    191       1.1.1.2  christos 		/* group mgmt */
    192       1.1.1.2  christos 		wpa_hexdump_key(MSG_DEBUG, "mesh: Own TX IGTK",
    193       1.1.1.2  christos 				rsn->igtk, rsn->igtk_len);
    194       1.1.1.2  christos 		wpa_drv_set_key(rsn->wpa_s,
    195       1.1.1.2  christos 				wpa_cipher_to_alg(rsn->mgmt_group_cipher), NULL,
    196       1.1.1.2  christos 				rsn->igtk_key_id, 1,
    197       1.1.1.2  christos 				seq, sizeof(seq), rsn->igtk, rsn->igtk_len);
    198       1.1.1.2  christos 	}
    199       1.1.1.2  christos #endif /* CONFIG_IEEE80211W */
    200           1.1  christos 
    201           1.1  christos 	/* group privacy / data frames */
    202       1.1.1.2  christos 	wpa_hexdump_key(MSG_DEBUG, "mesh: Own TX MGTK",
    203       1.1.1.2  christos 			rsn->mgtk, rsn->mgtk_len);
    204       1.1.1.2  christos 	wpa_drv_set_key(rsn->wpa_s, wpa_cipher_to_alg(rsn->group_cipher), NULL,
    205       1.1.1.2  christos 			rsn->mgtk_key_id, 1, seq, sizeof(seq),
    206       1.1.1.2  christos 			rsn->mgtk, rsn->mgtk_len);
    207           1.1  christos 
    208           1.1  christos 	return 0;
    209           1.1  christos }
    210           1.1  christos 
    211           1.1  christos 
    212           1.1  christos static void mesh_rsn_deinit(struct mesh_rsn *rsn)
    213           1.1  christos {
    214           1.1  christos 	os_memset(rsn->mgtk, 0, sizeof(rsn->mgtk));
    215       1.1.1.2  christos 	rsn->mgtk_len = 0;
    216       1.1.1.2  christos 	os_memset(rsn->igtk, 0, sizeof(rsn->igtk));
    217       1.1.1.2  christos 	rsn->igtk_len = 0;
    218       1.1.1.2  christos 	if (rsn->auth)
    219       1.1.1.2  christos 		wpa_deinit(rsn->auth);
    220           1.1  christos }
    221           1.1  christos 
    222           1.1  christos 
    223           1.1  christos struct mesh_rsn *mesh_rsn_auth_init(struct wpa_supplicant *wpa_s,
    224           1.1  christos 				    struct mesh_conf *conf)
    225           1.1  christos {
    226           1.1  christos 	struct mesh_rsn *mesh_rsn;
    227           1.1  christos 	struct hostapd_data *bss = wpa_s->ifmsh->bss[0];
    228           1.1  christos 	const u8 *ie;
    229           1.1  christos 	size_t ie_len;
    230  1.1.1.2.20.1  christos #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
    231  1.1.1.2.20.1  christos 	struct external_pmksa_cache *entry;
    232  1.1.1.2.20.1  christos #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
    233           1.1  christos 
    234           1.1  christos 	mesh_rsn = os_zalloc(sizeof(*mesh_rsn));
    235           1.1  christos 	if (mesh_rsn == NULL)
    236           1.1  christos 		return NULL;
    237           1.1  christos 	mesh_rsn->wpa_s = wpa_s;
    238       1.1.1.2  christos 	mesh_rsn->pairwise_cipher = conf->pairwise_cipher;
    239       1.1.1.2  christos 	mesh_rsn->group_cipher = conf->group_cipher;
    240       1.1.1.2  christos 	mesh_rsn->mgmt_group_cipher = conf->mgmt_group_cipher;
    241           1.1  christos 
    242       1.1.1.2  christos 	if (__mesh_rsn_auth_init(mesh_rsn, wpa_s->own_addr,
    243       1.1.1.2  christos 				 conf->ieee80211w) < 0) {
    244           1.1  christos 		mesh_rsn_deinit(mesh_rsn);
    245       1.1.1.2  christos 		os_free(mesh_rsn);
    246           1.1  christos 		return NULL;
    247           1.1  christos 	}
    248           1.1  christos 
    249           1.1  christos 	bss->wpa_auth = mesh_rsn->auth;
    250           1.1  christos 
    251  1.1.1.2.20.1  christos #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
    252  1.1.1.2.20.1  christos 	while ((entry = dl_list_last(&wpa_s->mesh_external_pmksa_cache,
    253  1.1.1.2.20.1  christos 				     struct external_pmksa_cache,
    254  1.1.1.2.20.1  christos 				     list)) != NULL) {
    255  1.1.1.2.20.1  christos 		int ret;
    256  1.1.1.2.20.1  christos 
    257  1.1.1.2.20.1  christos 		ret = wpa_auth_pmksa_add_entry(bss->wpa_auth,
    258  1.1.1.2.20.1  christos 					       entry->pmksa_cache);
    259  1.1.1.2.20.1  christos 		dl_list_del(&entry->list);
    260  1.1.1.2.20.1  christos 		os_free(entry);
    261  1.1.1.2.20.1  christos 
    262  1.1.1.2.20.1  christos 		if (ret < 0)
    263  1.1.1.2.20.1  christos 			return NULL;
    264  1.1.1.2.20.1  christos 	}
    265  1.1.1.2.20.1  christos #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
    266  1.1.1.2.20.1  christos 
    267           1.1  christos 	ie = wpa_auth_get_wpa_ie(mesh_rsn->auth, &ie_len);
    268       1.1.1.2  christos 	conf->rsn_ie = (u8 *) ie;
    269       1.1.1.2  christos 	conf->rsn_ie_len = ie_len;
    270           1.1  christos 
    271           1.1  christos 	wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
    272           1.1  christos 
    273           1.1  christos 	return mesh_rsn;
    274           1.1  christos }
    275           1.1  christos 
    276           1.1  christos 
    277           1.1  christos static int index_within_array(const int *array, int idx)
    278           1.1  christos {
    279           1.1  christos 	int i;
    280           1.1  christos 
    281           1.1  christos 	for (i = 0; i < idx; i++) {
    282           1.1  christos 		if (array[i] == -1)
    283           1.1  christos 			return 0;
    284           1.1  christos 	}
    285           1.1  christos 
    286           1.1  christos 	return 1;
    287           1.1  christos }
    288           1.1  christos 
    289           1.1  christos 
    290           1.1  christos static int mesh_rsn_sae_group(struct wpa_supplicant *wpa_s,
    291           1.1  christos 			      struct sae_data *sae)
    292           1.1  christos {
    293           1.1  christos 	int *groups = wpa_s->ifmsh->bss[0]->conf->sae_groups;
    294           1.1  christos 
    295           1.1  christos 	/* Configuration may have changed, so validate current index */
    296           1.1  christos 	if (!index_within_array(groups, wpa_s->mesh_rsn->sae_group_index))
    297           1.1  christos 		return -1;
    298           1.1  christos 
    299           1.1  christos 	for (;;) {
    300           1.1  christos 		int group = groups[wpa_s->mesh_rsn->sae_group_index];
    301           1.1  christos 
    302           1.1  christos 		if (group <= 0)
    303           1.1  christos 			break;
    304           1.1  christos 		if (sae_set_group(sae, group) == 0) {
    305           1.1  christos 			wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected SAE group %d",
    306           1.1  christos 				sae->group);
    307           1.1  christos 			return 0;
    308           1.1  christos 		}
    309           1.1  christos 		wpa_s->mesh_rsn->sae_group_index++;
    310           1.1  christos 	}
    311           1.1  christos 
    312           1.1  christos 	return -1;
    313           1.1  christos }
    314           1.1  christos 
    315           1.1  christos 
    316           1.1  christos static int mesh_rsn_build_sae_commit(struct wpa_supplicant *wpa_s,
    317           1.1  christos 				     struct wpa_ssid *ssid,
    318           1.1  christos 				     struct sta_info *sta)
    319           1.1  christos {
    320  1.1.1.2.20.1  christos 	const char *password;
    321  1.1.1.2.20.1  christos 
    322  1.1.1.2.20.1  christos 	password = ssid->sae_password;
    323  1.1.1.2.20.1  christos 	if (!password)
    324  1.1.1.2.20.1  christos 		password = ssid->passphrase;
    325  1.1.1.2.20.1  christos 	if (!password) {
    326           1.1  christos 		wpa_msg(wpa_s, MSG_DEBUG, "SAE: No password available");
    327           1.1  christos 		return -1;
    328           1.1  christos 	}
    329           1.1  christos 
    330           1.1  christos 	if (mesh_rsn_sae_group(wpa_s, sta->sae) < 0) {
    331           1.1  christos 		wpa_msg(wpa_s, MSG_DEBUG, "SAE: Failed to select group");
    332           1.1  christos 		return -1;
    333           1.1  christos 	}
    334           1.1  christos 
    335  1.1.1.2.20.1  christos 	if (sta->sae->tmp && !sta->sae->tmp->pw_id && ssid->sae_password_id) {
    336  1.1.1.2.20.1  christos 		sta->sae->tmp->pw_id = os_strdup(ssid->sae_password_id);
    337  1.1.1.2.20.1  christos 		if (!sta->sae->tmp->pw_id)
    338  1.1.1.2.20.1  christos 			return -1;
    339  1.1.1.2.20.1  christos 	}
    340           1.1  christos 	return sae_prepare_commit(wpa_s->own_addr, sta->addr,
    341  1.1.1.2.20.1  christos 				  (u8 *) password, os_strlen(password),
    342  1.1.1.2.20.1  christos 				  ssid->sae_password_id,
    343  1.1.1.2.20.1  christos 				  sta->sae);
    344           1.1  christos }
    345           1.1  christos 
    346           1.1  christos 
    347           1.1  christos /* initiate new SAE authentication with sta */
    348           1.1  christos int mesh_rsn_auth_sae_sta(struct wpa_supplicant *wpa_s,
    349           1.1  christos 			  struct sta_info *sta)
    350           1.1  christos {
    351           1.1  christos 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
    352           1.1  christos 	struct wpa_ssid *ssid = wpa_s->current_ssid;
    353       1.1.1.2  christos 	struct rsn_pmksa_cache_entry *pmksa;
    354           1.1  christos 	unsigned int rnd;
    355           1.1  christos 	int ret;
    356           1.1  christos 
    357           1.1  christos 	if (!ssid) {
    358           1.1  christos 		wpa_msg(wpa_s, MSG_DEBUG,
    359           1.1  christos 			"AUTH: No current_ssid known to initiate new SAE");
    360           1.1  christos 		return -1;
    361           1.1  christos 	}
    362           1.1  christos 
    363           1.1  christos 	if (!sta->sae) {
    364           1.1  christos 		sta->sae = os_zalloc(sizeof(*sta->sae));
    365           1.1  christos 		if (sta->sae == NULL)
    366           1.1  christos 			return -1;
    367           1.1  christos 	}
    368           1.1  christos 
    369  1.1.1.2.20.1  christos 	pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr, NULL);
    370       1.1.1.2  christos 	if (pmksa) {
    371       1.1.1.2  christos 		if (!sta->wpa_sm)
    372       1.1.1.2  christos 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
    373       1.1.1.2  christos 							sta->addr, NULL);
    374       1.1.1.2  christos 		if (!sta->wpa_sm) {
    375       1.1.1.2  christos 			wpa_printf(MSG_ERROR,
    376       1.1.1.2  christos 				   "mesh: Failed to initialize RSN state machine");
    377       1.1.1.2  christos 			return -1;
    378       1.1.1.2  christos 		}
    379       1.1.1.2  christos 
    380       1.1.1.2  christos 		wpa_printf(MSG_DEBUG,
    381       1.1.1.2  christos 			   "AUTH: Mesh PMKSA cache entry found for " MACSTR
    382       1.1.1.2  christos 			   " - try to use PMKSA caching instead of new SAE authentication",
    383       1.1.1.2  christos 			   MAC2STR(sta->addr));
    384       1.1.1.2  christos 		wpa_auth_pmksa_set_to_sm(pmksa, sta->wpa_sm, hapd->wpa_auth,
    385       1.1.1.2  christos 					 sta->sae->pmkid, sta->sae->pmk);
    386       1.1.1.2  christos 		sae_accept_sta(hapd, sta);
    387       1.1.1.2  christos 		sta->mesh_sae_pmksa_caching = 1;
    388       1.1.1.2  christos 		return 0;
    389       1.1.1.2  christos 	}
    390       1.1.1.2  christos 	sta->mesh_sae_pmksa_caching = 0;
    391       1.1.1.2  christos 
    392           1.1  christos 	if (mesh_rsn_build_sae_commit(wpa_s, ssid, sta))
    393           1.1  christos 		return -1;
    394           1.1  christos 
    395           1.1  christos 	wpa_msg(wpa_s, MSG_DEBUG,
    396           1.1  christos 		"AUTH: started authentication with SAE peer: " MACSTR,
    397           1.1  christos 		MAC2STR(sta->addr));
    398           1.1  christos 
    399           1.1  christos 	ret = auth_sae_init_committed(hapd, sta);
    400           1.1  christos 	if (ret)
    401           1.1  christos 		return ret;
    402           1.1  christos 
    403           1.1  christos 	eloop_cancel_timeout(mesh_auth_timer, wpa_s, sta);
    404           1.1  christos 	rnd = rand() % MESH_AUTH_TIMEOUT;
    405           1.1  christos 	eloop_register_timeout(MESH_AUTH_TIMEOUT + rnd, 0, mesh_auth_timer,
    406           1.1  christos 			       wpa_s, sta);
    407           1.1  christos 	return 0;
    408           1.1  christos }
    409           1.1  christos 
    410           1.1  christos 
    411           1.1  christos void mesh_rsn_get_pmkid(struct mesh_rsn *rsn, struct sta_info *sta, u8 *pmkid)
    412           1.1  christos {
    413       1.1.1.2  christos 	os_memcpy(pmkid, sta->sae->pmkid, SAE_PMKID_LEN);
    414           1.1  christos }
    415           1.1  christos 
    416           1.1  christos 
    417           1.1  christos static void
    418           1.1  christos mesh_rsn_derive_aek(struct mesh_rsn *rsn, struct sta_info *sta)
    419           1.1  christos {
    420           1.1  christos 	u8 *myaddr = rsn->wpa_s->own_addr;
    421           1.1  christos 	u8 *peer = sta->addr;
    422       1.1.1.2  christos 	u8 *addr1, *addr2;
    423       1.1.1.2  christos 	u8 context[RSN_SELECTOR_LEN + 2 * ETH_ALEN], *ptr = context;
    424           1.1  christos 
    425       1.1.1.2  christos 	/*
    426       1.1.1.2  christos 	 * AEK = KDF-Hash-256(PMK, "AEK Derivation", Selected AKM Suite ||
    427       1.1.1.2  christos 	 *       min(localMAC, peerMAC) || max(localMAC, peerMAC))
    428       1.1.1.2  christos 	 */
    429       1.1.1.2  christos 	/* Selected AKM Suite: SAE */
    430       1.1.1.2  christos 	RSN_SELECTOR_PUT(ptr, RSN_AUTH_KEY_MGMT_SAE);
    431       1.1.1.2  christos 	ptr += RSN_SELECTOR_LEN;
    432           1.1  christos 
    433           1.1  christos 	if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
    434           1.1  christos 		addr1 = myaddr;
    435           1.1  christos 		addr2 = peer;
    436       1.1.1.2  christos 	} else {
    437       1.1.1.2  christos 		addr1 = peer;
    438       1.1.1.2  christos 		addr2 = myaddr;
    439           1.1  christos 	}
    440       1.1.1.2  christos 	os_memcpy(ptr, addr1, ETH_ALEN);
    441       1.1.1.2  christos 	ptr += ETH_ALEN;
    442       1.1.1.2  christos 	os_memcpy(ptr, addr2, ETH_ALEN);
    443           1.1  christos 
    444           1.1  christos 	sha256_prf(sta->sae->pmk, sizeof(sta->sae->pmk), "AEK Derivation",
    445           1.1  christos 		   context, sizeof(context), sta->aek, sizeof(sta->aek));
    446           1.1  christos }
    447           1.1  christos 
    448           1.1  christos 
    449           1.1  christos /* derive mesh temporal key from pmk */
    450           1.1  christos int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
    451           1.1  christos {
    452           1.1  christos 	u8 *ptr;
    453           1.1  christos 	u8 *min, *max;
    454           1.1  christos 	u8 *myaddr = wpa_s->own_addr;
    455           1.1  christos 	u8 *peer = sta->addr;
    456       1.1.1.2  christos 	u8 context[2 * WPA_NONCE_LEN + 2 * 2 + RSN_SELECTOR_LEN + 2 * ETH_ALEN];
    457           1.1  christos 
    458       1.1.1.2  christos 	/*
    459       1.1.1.2  christos 	 * MTK = KDF-Hash-Length(PMK, "Temporal Key Derivation", min(localNonce,
    460       1.1.1.2  christos 	 *  peerNonce) || max(localNonce, peerNonce) || min(localLinkID,
    461       1.1.1.2  christos 	 *  peerLinkID) || max(localLinkID, peerLinkID) || Selected AKM Suite ||
    462       1.1.1.2  christos 	 *  min(localMAC, peerMAC) || max(localMAC, peerMAC))
    463       1.1.1.2  christos 	 */
    464           1.1  christos 	ptr = context;
    465       1.1.1.2  christos 	if (os_memcmp(sta->my_nonce, sta->peer_nonce, WPA_NONCE_LEN) < 0) {
    466           1.1  christos 		min = sta->my_nonce;
    467           1.1  christos 		max = sta->peer_nonce;
    468           1.1  christos 	} else {
    469           1.1  christos 		min = sta->peer_nonce;
    470           1.1  christos 		max = sta->my_nonce;
    471           1.1  christos 	}
    472       1.1.1.2  christos 	os_memcpy(ptr, min, WPA_NONCE_LEN);
    473       1.1.1.2  christos 	ptr += WPA_NONCE_LEN;
    474       1.1.1.2  christos 	os_memcpy(ptr, max, WPA_NONCE_LEN);
    475       1.1.1.2  christos 	ptr += WPA_NONCE_LEN;
    476           1.1  christos 
    477           1.1  christos 	if (sta->my_lid < sta->peer_lid) {
    478       1.1.1.2  christos 		WPA_PUT_LE16(ptr, sta->my_lid);
    479       1.1.1.2  christos 		ptr += 2;
    480       1.1.1.2  christos 		WPA_PUT_LE16(ptr, sta->peer_lid);
    481       1.1.1.2  christos 		ptr += 2;
    482           1.1  christos 	} else {
    483       1.1.1.2  christos 		WPA_PUT_LE16(ptr, sta->peer_lid);
    484       1.1.1.2  christos 		ptr += 2;
    485       1.1.1.2  christos 		WPA_PUT_LE16(ptr, sta->my_lid);
    486       1.1.1.2  christos 		ptr += 2;
    487           1.1  christos 	}
    488       1.1.1.2  christos 
    489       1.1.1.2  christos 	/* Selected AKM Suite: SAE */
    490       1.1.1.2  christos 	RSN_SELECTOR_PUT(ptr, RSN_AUTH_KEY_MGMT_SAE);
    491       1.1.1.2  christos 	ptr += RSN_SELECTOR_LEN;
    492           1.1  christos 
    493           1.1  christos 	if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
    494           1.1  christos 		min = myaddr;
    495           1.1  christos 		max = peer;
    496           1.1  christos 	} else {
    497           1.1  christos 		min = peer;
    498           1.1  christos 		max = myaddr;
    499           1.1  christos 	}
    500           1.1  christos 	os_memcpy(ptr, min, ETH_ALEN);
    501       1.1.1.2  christos 	ptr += ETH_ALEN;
    502       1.1.1.2  christos 	os_memcpy(ptr, max, ETH_ALEN);
    503           1.1  christos 
    504       1.1.1.2  christos 	sta->mtk_len = wpa_cipher_key_len(wpa_s->mesh_rsn->pairwise_cipher);
    505       1.1.1.2  christos 	sha256_prf(sta->sae->pmk, SAE_PMK_LEN,
    506           1.1  christos 		   "Temporal Key Derivation", context, sizeof(context),
    507       1.1.1.2  christos 		   sta->mtk, sta->mtk_len);
    508           1.1  christos 	return 0;
    509           1.1  christos }
    510           1.1  christos 
    511           1.1  christos 
    512           1.1  christos void mesh_rsn_init_ampe_sta(struct wpa_supplicant *wpa_s, struct sta_info *sta)
    513           1.1  christos {
    514       1.1.1.2  christos 	if (random_get_bytes(sta->my_nonce, WPA_NONCE_LEN) < 0) {
    515           1.1  christos 		wpa_printf(MSG_INFO, "mesh: Failed to derive random nonce");
    516           1.1  christos 		/* TODO: How to handle this more cleanly? */
    517           1.1  christos 	}
    518       1.1.1.2  christos 	os_memset(sta->peer_nonce, 0, WPA_NONCE_LEN);
    519           1.1  christos 	mesh_rsn_derive_aek(wpa_s->mesh_rsn, sta);
    520           1.1  christos }
    521           1.1  christos 
    522           1.1  christos 
    523           1.1  christos /* insert AMPE and encrypted MIC at @ie.
    524           1.1  christos  * @mesh_rsn: mesh RSN context
    525           1.1  christos  * @sta: STA we're sending to
    526           1.1  christos  * @cat: pointer to category code in frame header.
    527           1.1  christos  * @buf: wpabuf to add encrypted AMPE and MIC to.
    528           1.1  christos  * */
    529           1.1  christos int mesh_rsn_protect_frame(struct mesh_rsn *rsn, struct sta_info *sta,
    530           1.1  christos 			   const u8 *cat, struct wpabuf *buf)
    531           1.1  christos {
    532           1.1  christos 	struct ieee80211_ampe_ie *ampe;
    533           1.1  christos 	u8 const *ie = wpabuf_head_u8(buf) + wpabuf_len(buf);
    534       1.1.1.2  christos 	u8 *ampe_ie, *pos, *mic_payload;
    535           1.1  christos 	const u8 *aad[] = { rsn->wpa_s->own_addr, sta->addr, cat };
    536           1.1  christos 	const size_t aad_len[] = { ETH_ALEN, ETH_ALEN, ie - cat };
    537           1.1  christos 	int ret = 0;
    538       1.1.1.2  christos 	size_t len;
    539           1.1  christos 
    540       1.1.1.2  christos 	len = sizeof(*ampe);
    541       1.1.1.2  christos 	if (cat[1] == PLINK_OPEN)
    542       1.1.1.2  christos 		len += rsn->mgtk_len + WPA_KEY_RSC_LEN + 4;
    543       1.1.1.2  christos #ifdef CONFIG_IEEE80211W
    544       1.1.1.2  christos 	if (cat[1] == PLINK_OPEN && rsn->igtk_len)
    545       1.1.1.2  christos 		len += 2 + 6 + rsn->igtk_len;
    546       1.1.1.2  christos #endif /* CONFIG_IEEE80211W */
    547       1.1.1.2  christos 
    548       1.1.1.2  christos 	if (2 + AES_BLOCK_SIZE + 2 + len > wpabuf_tailroom(buf)) {
    549           1.1  christos 		wpa_printf(MSG_ERROR, "protect frame: buffer too small");
    550           1.1  christos 		return -EINVAL;
    551           1.1  christos 	}
    552           1.1  christos 
    553       1.1.1.2  christos 	ampe_ie = os_zalloc(2 + len);
    554           1.1  christos 	if (!ampe_ie) {
    555           1.1  christos 		wpa_printf(MSG_ERROR, "protect frame: out of memory");
    556           1.1  christos 		return -ENOMEM;
    557           1.1  christos 	}
    558           1.1  christos 
    559           1.1  christos 	/*  IE: AMPE */
    560           1.1  christos 	ampe_ie[0] = WLAN_EID_AMPE;
    561       1.1.1.2  christos 	ampe_ie[1] = len;
    562           1.1  christos 	ampe = (struct ieee80211_ampe_ie *) (ampe_ie + 2);
    563           1.1  christos 
    564           1.1  christos 	RSN_SELECTOR_PUT(ampe->selected_pairwise_suite,
    565       1.1.1.2  christos 			 RSN_CIPHER_SUITE_CCMP);
    566       1.1.1.2  christos 	os_memcpy(ampe->local_nonce, sta->my_nonce, WPA_NONCE_LEN);
    567       1.1.1.2  christos 	os_memcpy(ampe->peer_nonce, sta->peer_nonce, WPA_NONCE_LEN);
    568       1.1.1.2  christos 
    569       1.1.1.2  christos 	pos = (u8 *) (ampe + 1);
    570       1.1.1.2  christos 	if (cat[1] != PLINK_OPEN)
    571       1.1.1.2  christos 		goto skip_keys;
    572       1.1.1.2  christos 
    573       1.1.1.2  christos 	/* TODO: Key Replay Counter[8] optionally for
    574       1.1.1.2  christos 	 * Mesh Group Key Inform/Acknowledge frames */
    575       1.1.1.2  christos 
    576           1.1  christos 	/* TODO: static mgtk for now since we don't support rekeying! */
    577       1.1.1.2  christos 	/*
    578       1.1.1.2  christos 	 * GTKdata[variable]:
    579       1.1.1.2  christos 	 * MGTK[variable] || Key RSC[8] || GTKExpirationTime[4]
    580       1.1.1.2  christos 	 */
    581       1.1.1.2  christos 	os_memcpy(pos, rsn->mgtk, rsn->mgtk_len);
    582       1.1.1.2  christos 	pos += rsn->mgtk_len;
    583       1.1.1.2  christos 	wpa_drv_get_seqnum(rsn->wpa_s, NULL, rsn->mgtk_key_id, pos);
    584       1.1.1.2  christos 	pos += WPA_KEY_RSC_LEN;
    585       1.1.1.2  christos 	/* Use fixed GTKExpirationTime for now */
    586       1.1.1.2  christos 	WPA_PUT_LE32(pos, 0xffffffff);
    587       1.1.1.2  christos 	pos += 4;
    588       1.1.1.2  christos 
    589       1.1.1.2  christos #ifdef CONFIG_IEEE80211W
    590       1.1.1.2  christos 	/*
    591       1.1.1.2  christos 	 * IGTKdata[variable]:
    592       1.1.1.2  christos 	 * Key ID[2], IPN[6], IGTK[variable]
    593       1.1.1.2  christos 	 */
    594       1.1.1.2  christos 	if (rsn->igtk_len) {
    595       1.1.1.2  christos 		WPA_PUT_LE16(pos, rsn->igtk_key_id);
    596       1.1.1.2  christos 		pos += 2;
    597       1.1.1.2  christos 		wpa_drv_get_seqnum(rsn->wpa_s, NULL, rsn->igtk_key_id, pos);
    598       1.1.1.2  christos 		pos += 6;
    599       1.1.1.2  christos 		os_memcpy(pos, rsn->igtk, rsn->igtk_len);
    600       1.1.1.2  christos 	}
    601       1.1.1.2  christos #endif /* CONFIG_IEEE80211W */
    602       1.1.1.2  christos 
    603       1.1.1.2  christos skip_keys:
    604       1.1.1.2  christos 	wpa_hexdump_key(MSG_DEBUG, "mesh: Plaintext AMPE element",
    605       1.1.1.2  christos 			ampe_ie, 2 + len);
    606           1.1  christos 
    607           1.1  christos 	/* IE: MIC */
    608       1.1.1.2  christos 	wpabuf_put_u8(buf, WLAN_EID_MIC);
    609       1.1.1.2  christos 	wpabuf_put_u8(buf, AES_BLOCK_SIZE);
    610           1.1  christos 	/* MIC field is output ciphertext */
    611           1.1  christos 
    612           1.1  christos 	/* encrypt after MIC */
    613       1.1.1.2  christos 	mic_payload = wpabuf_put(buf, 2 + len + AES_BLOCK_SIZE);
    614           1.1  christos 
    615  1.1.1.2.20.1  christos 	if (aes_siv_encrypt(sta->aek, sizeof(sta->aek), ampe_ie, 2 + len, 3,
    616           1.1  christos 			    aad, aad_len, mic_payload)) {
    617           1.1  christos 		wpa_printf(MSG_ERROR, "protect frame: failed to encrypt");
    618           1.1  christos 		ret = -ENOMEM;
    619           1.1  christos 	}
    620           1.1  christos 
    621           1.1  christos 	os_free(ampe_ie);
    622           1.1  christos 
    623           1.1  christos 	return ret;
    624           1.1  christos }
    625           1.1  christos 
    626           1.1  christos 
    627           1.1  christos int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
    628           1.1  christos 			  struct ieee802_11_elems *elems, const u8 *cat,
    629       1.1.1.2  christos 			  const u8 *chosen_pmk,
    630           1.1  christos 			  const u8 *start, size_t elems_len)
    631           1.1  christos {
    632           1.1  christos 	int ret = 0;
    633           1.1  christos 	struct ieee80211_ampe_ie *ampe;
    634       1.1.1.2  christos 	u8 null_nonce[WPA_NONCE_LEN] = {};
    635           1.1  christos 	u8 ampe_eid;
    636           1.1  christos 	u8 ampe_ie_len;
    637       1.1.1.2  christos 	u8 *ampe_buf, *crypt = NULL, *pos, *end;
    638           1.1  christos 	size_t crypt_len;
    639           1.1  christos 	const u8 *aad[] = { sta->addr, wpa_s->own_addr, cat };
    640           1.1  christos 	const size_t aad_len[] = { ETH_ALEN, ETH_ALEN,
    641           1.1  christos 				   (elems->mic - 2) - cat };
    642       1.1.1.2  christos 	size_t key_len;
    643       1.1.1.2  christos 
    644       1.1.1.2  christos 	if (!sta->sae) {
    645       1.1.1.2  christos 		struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
    646       1.1.1.2  christos 
    647  1.1.1.2.20.1  christos 		if (!wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr, NULL)) {
    648       1.1.1.2  christos 			wpa_printf(MSG_INFO,
    649       1.1.1.2  christos 				   "Mesh RSN: SAE is not prepared yet");
    650       1.1.1.2  christos 			return -1;
    651       1.1.1.2  christos 		}
    652       1.1.1.2  christos 		mesh_rsn_auth_sae_sta(wpa_s, sta);
    653       1.1.1.2  christos 	}
    654       1.1.1.2  christos 
    655       1.1.1.2  christos 	if (chosen_pmk && os_memcmp(chosen_pmk, sta->sae->pmkid, PMKID_LEN)) {
    656       1.1.1.2  christos 		wpa_msg(wpa_s, MSG_DEBUG,
    657       1.1.1.2  christos 			"Mesh RSN: Invalid PMKID (Chosen PMK did not match calculated PMKID)");
    658       1.1.1.2  christos 		return -1;
    659       1.1.1.2  christos 	}
    660           1.1  christos 
    661           1.1  christos 	if (!elems->mic || elems->mic_len < AES_BLOCK_SIZE) {
    662           1.1  christos 		wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing mic ie");
    663           1.1  christos 		return -1;
    664           1.1  christos 	}
    665           1.1  christos 
    666           1.1  christos 	ampe_buf = (u8 *) elems->mic + elems->mic_len;
    667           1.1  christos 	if ((int) elems_len < ampe_buf - start)
    668           1.1  christos 		return -1;
    669           1.1  christos 
    670           1.1  christos 	crypt_len = elems_len - (elems->mic - start);
    671       1.1.1.2  christos 	if (crypt_len < 2 + AES_BLOCK_SIZE) {
    672           1.1  christos 		wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing ampe ie");
    673           1.1  christos 		return -1;
    674           1.1  christos 	}
    675           1.1  christos 
    676           1.1  christos 	/* crypt is modified by siv_decrypt */
    677           1.1  christos 	crypt = os_zalloc(crypt_len);
    678           1.1  christos 	if (!crypt) {
    679           1.1  christos 		wpa_printf(MSG_ERROR, "Mesh RSN: out of memory");
    680           1.1  christos 		ret = -ENOMEM;
    681           1.1  christos 		goto free;
    682           1.1  christos 	}
    683           1.1  christos 
    684           1.1  christos 	os_memcpy(crypt, elems->mic, crypt_len);
    685           1.1  christos 
    686  1.1.1.2.20.1  christos 	if (aes_siv_decrypt(sta->aek, sizeof(sta->aek), crypt, crypt_len, 3,
    687           1.1  christos 			    aad, aad_len, ampe_buf)) {
    688           1.1  christos 		wpa_printf(MSG_ERROR, "Mesh RSN: frame verification failed!");
    689       1.1.1.2  christos 		ret = -2;
    690           1.1  christos 		goto free;
    691           1.1  christos 	}
    692           1.1  christos 
    693       1.1.1.2  christos 	crypt_len -= AES_BLOCK_SIZE;
    694       1.1.1.2  christos 	wpa_hexdump_key(MSG_DEBUG, "mesh: Decrypted AMPE element",
    695       1.1.1.2  christos 			ampe_buf, crypt_len);
    696       1.1.1.2  christos 
    697           1.1  christos 	ampe_eid = *ampe_buf++;
    698           1.1  christos 	ampe_ie_len = *ampe_buf++;
    699           1.1  christos 
    700           1.1  christos 	if (ampe_eid != WLAN_EID_AMPE ||
    701       1.1.1.2  christos 	    (size_t) 2 + ampe_ie_len > crypt_len ||
    702           1.1  christos 	    ampe_ie_len < sizeof(struct ieee80211_ampe_ie)) {
    703           1.1  christos 		wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid ampe ie");
    704           1.1  christos 		ret = -1;
    705           1.1  christos 		goto free;
    706           1.1  christos 	}
    707           1.1  christos 
    708           1.1  christos 	ampe = (struct ieee80211_ampe_ie *) ampe_buf;
    709       1.1.1.2  christos 	pos = (u8 *) (ampe + 1);
    710       1.1.1.2  christos 	end = ampe_buf + ampe_ie_len;
    711       1.1.1.2  christos 	if (os_memcmp(ampe->peer_nonce, null_nonce, WPA_NONCE_LEN) != 0 &&
    712       1.1.1.2  christos 	    os_memcmp(ampe->peer_nonce, sta->my_nonce, WPA_NONCE_LEN) != 0) {
    713           1.1  christos 		wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid peer nonce");
    714           1.1  christos 		ret = -1;
    715           1.1  christos 		goto free;
    716           1.1  christos 	}
    717           1.1  christos 	os_memcpy(sta->peer_nonce, ampe->local_nonce,
    718           1.1  christos 		  sizeof(ampe->local_nonce));
    719           1.1  christos 
    720       1.1.1.2  christos 	/* TODO: Key Replay Counter[8] in Mesh Group Key Inform/Acknowledge
    721       1.1.1.2  christos 	 * frames */
    722       1.1.1.2  christos 
    723       1.1.1.2  christos 	/*
    724       1.1.1.2  christos 	 * GTKdata shall not be included in Mesh Peering Confirm. While the
    725       1.1.1.2  christos 	 * standard does not state the same about IGTKdata, that same constraint
    726       1.1.1.2  christos 	 * needs to apply for it. It makes no sense to include the keys in Mesh
    727       1.1.1.2  christos 	 * Peering Close frames either, so while the standard does not seem to
    728       1.1.1.2  christos 	 * have a shall statement for these, they are described without
    729       1.1.1.2  christos 	 * mentioning GTKdata.
    730       1.1.1.2  christos 	 *
    731       1.1.1.2  christos 	 * An earlier implementation used to add GTKdata to both Mesh Peering
    732       1.1.1.2  christos 	 * Open and Mesh Peering Confirm frames, so ignore the possibly present
    733       1.1.1.2  christos 	 * GTKdata frame without rejecting the frame as a backwards
    734       1.1.1.2  christos 	 * compatibility mechanism.
    735       1.1.1.2  christos 	 */
    736       1.1.1.2  christos 	if (cat[1] != PLINK_OPEN) {
    737       1.1.1.2  christos 		if (end > pos) {
    738       1.1.1.2  christos 			wpa_hexdump_key(MSG_DEBUG,
    739       1.1.1.2  christos 					"mesh: Ignore unexpected GTKdata(etc.) fields in the end of AMPE element in Mesh Peering Confirm/Close",
    740       1.1.1.2  christos 					pos, end - pos);
    741       1.1.1.2  christos 		}
    742       1.1.1.2  christos 		goto free;
    743       1.1.1.2  christos 	}
    744       1.1.1.2  christos 
    745       1.1.1.2  christos 	/*
    746       1.1.1.2  christos 	 * GTKdata[variable]:
    747       1.1.1.2  christos 	 * MGTK[variable] || Key RSC[8] || GTKExpirationTime[4]
    748       1.1.1.2  christos 	 */
    749       1.1.1.2  christos 	sta->mgtk_key_id = 1; /* FIX: Where to get Key ID? */
    750       1.1.1.2  christos 	key_len = wpa_cipher_key_len(wpa_s->mesh_rsn->group_cipher);
    751       1.1.1.2  christos 	if ((int) key_len + WPA_KEY_RSC_LEN + 4 > end - pos) {
    752       1.1.1.2  christos 		wpa_dbg(wpa_s, MSG_DEBUG, "mesh: Truncated AMPE element");
    753       1.1.1.2  christos 		ret = -1;
    754       1.1.1.2  christos 		goto free;
    755       1.1.1.2  christos 	}
    756       1.1.1.2  christos 	sta->mgtk_len = key_len;
    757       1.1.1.2  christos 	os_memcpy(sta->mgtk, pos, sta->mgtk_len);
    758       1.1.1.2  christos 	wpa_hexdump_key(MSG_DEBUG, "mesh: GTKdata - MGTK",
    759       1.1.1.2  christos 			sta->mgtk, sta->mgtk_len);
    760       1.1.1.2  christos 	pos += sta->mgtk_len;
    761       1.1.1.2  christos 	wpa_hexdump(MSG_DEBUG, "mesh: GTKdata - MGTK - Key RSC",
    762       1.1.1.2  christos 		    pos, WPA_KEY_RSC_LEN);
    763       1.1.1.2  christos 	os_memcpy(sta->mgtk_rsc, pos, sizeof(sta->mgtk_rsc));
    764       1.1.1.2  christos 	pos += WPA_KEY_RSC_LEN;
    765       1.1.1.2  christos 	wpa_printf(MSG_DEBUG,
    766       1.1.1.2  christos 		   "mesh: GTKdata - MGTK - GTKExpirationTime: %u seconds",
    767       1.1.1.2  christos 		   WPA_GET_LE32(pos));
    768       1.1.1.2  christos 	pos += 4;
    769       1.1.1.2  christos 
    770       1.1.1.2  christos #ifdef CONFIG_IEEE80211W
    771       1.1.1.2  christos 	/*
    772       1.1.1.2  christos 	 * IGTKdata[variable]:
    773       1.1.1.2  christos 	 * Key ID[2], IPN[6], IGTK[variable]
    774       1.1.1.2  christos 	 */
    775       1.1.1.2  christos 	key_len = wpa_cipher_key_len(wpa_s->mesh_rsn->mgmt_group_cipher);
    776       1.1.1.2  christos 	if (end - pos >= (int) (2 + 6 + key_len)) {
    777       1.1.1.2  christos 		sta->igtk_key_id = WPA_GET_LE16(pos);
    778       1.1.1.2  christos 		wpa_printf(MSG_DEBUG, "mesh: IGTKdata - Key ID %u",
    779       1.1.1.2  christos 			   sta->igtk_key_id);
    780       1.1.1.2  christos 		pos += 2;
    781       1.1.1.2  christos 		os_memcpy(sta->igtk_rsc, pos, sizeof(sta->igtk_rsc));
    782       1.1.1.2  christos 		wpa_hexdump(MSG_DEBUG, "mesh: IGTKdata - IPN",
    783       1.1.1.2  christos 			    sta->igtk_rsc, sizeof(sta->igtk_rsc));
    784       1.1.1.2  christos 		pos += 6;
    785       1.1.1.2  christos 		os_memcpy(sta->igtk, pos, key_len);
    786       1.1.1.2  christos 		sta->igtk_len = key_len;
    787       1.1.1.2  christos 		wpa_hexdump_key(MSG_DEBUG, "mesh: IGTKdata - IGTK",
    788       1.1.1.2  christos 				sta->igtk, sta->igtk_len);
    789       1.1.1.2  christos 	}
    790       1.1.1.2  christos #endif /* CONFIG_IEEE80211W */
    791       1.1.1.2  christos 
    792           1.1  christos free:
    793           1.1  christos 	os_free(crypt);
    794           1.1  christos 	return ret;
    795           1.1  christos }
    796