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