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