xref: /freebsd/contrib/wpa/wpa_supplicant/mesh_mpm.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
15b9c547cSRui Paulo /*
25b9c547cSRui Paulo  * WPA Supplicant - Basic mesh peer management
35b9c547cSRui Paulo  * Copyright (c) 2013-2014, cozybit, Inc.  All rights reserved.
45b9c547cSRui Paulo  *
55b9c547cSRui Paulo  * This software may be distributed under the terms of the BSD license.
65b9c547cSRui Paulo  * See README for more details.
75b9c547cSRui Paulo  */
85b9c547cSRui Paulo 
95b9c547cSRui Paulo #include "utils/includes.h"
105b9c547cSRui Paulo 
115b9c547cSRui Paulo #include "utils/common.h"
125b9c547cSRui Paulo #include "utils/eloop.h"
135b9c547cSRui Paulo #include "common/ieee802_11_defs.h"
1485732ac8SCy Schubert #include "common/hw_features_common.h"
154bc52338SCy Schubert #include "common/ocv.h"
165b9c547cSRui Paulo #include "ap/hostapd.h"
175b9c547cSRui Paulo #include "ap/sta_info.h"
185b9c547cSRui Paulo #include "ap/ieee802_11.h"
19*a90b9d01SCy Schubert #include "ap/beacon.h"
20780fb4a2SCy Schubert #include "ap/wpa_auth.h"
215b9c547cSRui Paulo #include "wpa_supplicant_i.h"
225b9c547cSRui Paulo #include "driver_i.h"
235b9c547cSRui Paulo #include "mesh_mpm.h"
245b9c547cSRui Paulo #include "mesh_rsn.h"
2585732ac8SCy Schubert #include "notify.h"
265b9c547cSRui Paulo 
275b9c547cSRui Paulo struct mesh_peer_mgmt_ie {
28780fb4a2SCy Schubert 	const u8 *proto_id; /* Mesh Peering Protocol Identifier (2 octets) */
29780fb4a2SCy Schubert 	const u8 *llid; /* Local Link ID (2 octets) */
30780fb4a2SCy Schubert 	const u8 *plid; /* Peer Link ID (conditional, 2 octets) */
31780fb4a2SCy Schubert 	const u8 *reason; /* Reason Code (conditional, 2 octets) */
32780fb4a2SCy Schubert 	const u8 *chosen_pmk; /* Chosen PMK (optional, 16 octets) */
335b9c547cSRui Paulo };
345b9c547cSRui Paulo 
355b9c547cSRui Paulo static void plink_timer(void *eloop_ctx, void *user_data);
365b9c547cSRui Paulo 
375b9c547cSRui Paulo 
385b9c547cSRui Paulo enum plink_event {
395b9c547cSRui Paulo 	PLINK_UNDEFINED,
405b9c547cSRui Paulo 	OPN_ACPT,
415b9c547cSRui Paulo 	OPN_RJCT,
425b9c547cSRui Paulo 	CNF_ACPT,
435b9c547cSRui Paulo 	CNF_RJCT,
445b9c547cSRui Paulo 	CLS_ACPT,
45780fb4a2SCy Schubert 	REQ_RJCT
465b9c547cSRui Paulo };
475b9c547cSRui Paulo 
485b9c547cSRui Paulo static const char * const mplstate[] = {
49780fb4a2SCy Schubert 	[0] = "UNINITIALIZED",
50780fb4a2SCy Schubert 	[PLINK_IDLE] = "IDLE",
51780fb4a2SCy Schubert 	[PLINK_OPN_SNT] = "OPN_SNT",
52780fb4a2SCy Schubert 	[PLINK_OPN_RCVD] = "OPN_RCVD",
535b9c547cSRui Paulo 	[PLINK_CNF_RCVD] = "CNF_RCVD",
545b9c547cSRui Paulo 	[PLINK_ESTAB] = "ESTAB",
555b9c547cSRui Paulo 	[PLINK_HOLDING] = "HOLDING",
565b9c547cSRui Paulo 	[PLINK_BLOCKED] = "BLOCKED"
575b9c547cSRui Paulo };
585b9c547cSRui Paulo 
595b9c547cSRui Paulo static const char * const mplevent[] = {
605b9c547cSRui Paulo 	[PLINK_UNDEFINED] = "UNDEFINED",
615b9c547cSRui Paulo 	[OPN_ACPT] = "OPN_ACPT",
625b9c547cSRui Paulo 	[OPN_RJCT] = "OPN_RJCT",
635b9c547cSRui Paulo 	[CNF_ACPT] = "CNF_ACPT",
645b9c547cSRui Paulo 	[CNF_RJCT] = "CNF_RJCT",
655b9c547cSRui Paulo 	[CLS_ACPT] = "CLS_ACPT",
66780fb4a2SCy Schubert 	[REQ_RJCT] = "REQ_RJCT",
675b9c547cSRui Paulo };
685b9c547cSRui Paulo 
695b9c547cSRui Paulo 
mesh_mpm_parse_peer_mgmt(struct wpa_supplicant * wpa_s,u8 action_field,const u8 * ie,size_t len,struct mesh_peer_mgmt_ie * mpm_ie)705b9c547cSRui Paulo static int mesh_mpm_parse_peer_mgmt(struct wpa_supplicant *wpa_s,
715b9c547cSRui Paulo 				    u8 action_field,
725b9c547cSRui Paulo 				    const u8 *ie, size_t len,
735b9c547cSRui Paulo 				    struct mesh_peer_mgmt_ie *mpm_ie)
745b9c547cSRui Paulo {
755b9c547cSRui Paulo 	os_memset(mpm_ie, 0, sizeof(*mpm_ie));
765b9c547cSRui Paulo 
77780fb4a2SCy Schubert 	/* Remove optional Chosen PMK field at end */
78780fb4a2SCy Schubert 	if (len >= SAE_PMKID_LEN) {
79780fb4a2SCy Schubert 		mpm_ie->chosen_pmk = ie + len - SAE_PMKID_LEN;
80780fb4a2SCy Schubert 		len -= SAE_PMKID_LEN;
815b9c547cSRui Paulo 	}
825b9c547cSRui Paulo 
835b9c547cSRui Paulo 	if ((action_field == PLINK_OPEN && len != 4) ||
845b9c547cSRui Paulo 	    (action_field == PLINK_CONFIRM && len != 6) ||
855b9c547cSRui Paulo 	    (action_field == PLINK_CLOSE && len != 6 && len != 8)) {
865b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "MPM: Invalid peer mgmt ie");
875b9c547cSRui Paulo 		return -1;
885b9c547cSRui Paulo 	}
895b9c547cSRui Paulo 
905b9c547cSRui Paulo 	/* required fields */
915b9c547cSRui Paulo 	if (len < 4)
925b9c547cSRui Paulo 		return -1;
935b9c547cSRui Paulo 	mpm_ie->proto_id = ie;
945b9c547cSRui Paulo 	mpm_ie->llid = ie + 2;
955b9c547cSRui Paulo 	ie += 4;
965b9c547cSRui Paulo 	len -= 4;
975b9c547cSRui Paulo 
985b9c547cSRui Paulo 	/* close reason is always present at end for close */
995b9c547cSRui Paulo 	if (action_field == PLINK_CLOSE) {
1005b9c547cSRui Paulo 		if (len < 2)
1015b9c547cSRui Paulo 			return -1;
1025b9c547cSRui Paulo 		mpm_ie->reason = ie + len - 2;
1035b9c547cSRui Paulo 		len -= 2;
1045b9c547cSRui Paulo 	}
1055b9c547cSRui Paulo 
106780fb4a2SCy Schubert 	/* Peer Link ID, present for confirm, and possibly close */
107780fb4a2SCy Schubert 	if (len >= 2)
1085b9c547cSRui Paulo 		mpm_ie->plid = ie;
1095b9c547cSRui Paulo 
1105b9c547cSRui Paulo 	return 0;
1115b9c547cSRui Paulo }
1125b9c547cSRui Paulo 
1135b9c547cSRui Paulo 
plink_free_count(struct hostapd_data * hapd)1145b9c547cSRui Paulo static int plink_free_count(struct hostapd_data *hapd)
1155b9c547cSRui Paulo {
1165b9c547cSRui Paulo 	if (hapd->max_plinks > hapd->num_plinks)
1175b9c547cSRui Paulo 		return hapd->max_plinks - hapd->num_plinks;
1185b9c547cSRui Paulo 	return 0;
1195b9c547cSRui Paulo }
1205b9c547cSRui Paulo 
1215b9c547cSRui Paulo 
copy_supp_rates(struct wpa_supplicant * wpa_s,struct sta_info * sta,struct ieee802_11_elems * elems)1225b9c547cSRui Paulo static u16 copy_supp_rates(struct wpa_supplicant *wpa_s,
1235b9c547cSRui Paulo 			   struct sta_info *sta,
1245b9c547cSRui Paulo 			   struct ieee802_11_elems *elems)
1255b9c547cSRui Paulo {
1265b9c547cSRui Paulo 	if (!elems->supp_rates) {
1275b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_ERROR, "no supported rates from " MACSTR,
1285b9c547cSRui Paulo 			MAC2STR(sta->addr));
1295b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
1305b9c547cSRui Paulo 	}
1315b9c547cSRui Paulo 
1325b9c547cSRui Paulo 	if (elems->supp_rates_len + elems->ext_supp_rates_len >
1335b9c547cSRui Paulo 	    sizeof(sta->supported_rates)) {
1345b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_ERROR,
1355b9c547cSRui Paulo 			"Invalid supported rates element length " MACSTR
1365b9c547cSRui Paulo 			" %d+%d", MAC2STR(sta->addr), elems->supp_rates_len,
1375b9c547cSRui Paulo 			elems->ext_supp_rates_len);
1385b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
1395b9c547cSRui Paulo 	}
1405b9c547cSRui Paulo 
1415b9c547cSRui Paulo 	sta->supported_rates_len = merge_byte_arrays(
1425b9c547cSRui Paulo 		sta->supported_rates, sizeof(sta->supported_rates),
1435b9c547cSRui Paulo 		elems->supp_rates, elems->supp_rates_len,
1445b9c547cSRui Paulo 		elems->ext_supp_rates, elems->ext_supp_rates_len);
1455b9c547cSRui Paulo 
1465b9c547cSRui Paulo 	return WLAN_STATUS_SUCCESS;
1475b9c547cSRui Paulo }
1485b9c547cSRui Paulo 
1495b9c547cSRui Paulo 
1505b9c547cSRui Paulo /* return true if elems from a neighbor match this MBSS */
matches_local(struct wpa_supplicant * wpa_s,struct ieee802_11_elems * elems)151c1d255d3SCy Schubert static bool matches_local(struct wpa_supplicant *wpa_s,
1525b9c547cSRui Paulo 			  struct ieee802_11_elems *elems)
1535b9c547cSRui Paulo {
1545b9c547cSRui Paulo 	struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
1555b9c547cSRui Paulo 
1565b9c547cSRui Paulo 	if (elems->mesh_config_len < 5)
157c1d255d3SCy Schubert 		return false;
1585b9c547cSRui Paulo 
1595b9c547cSRui Paulo 	return (mconf->meshid_len == elems->mesh_id_len &&
1605b9c547cSRui Paulo 		os_memcmp(mconf->meshid, elems->mesh_id,
1615b9c547cSRui Paulo 			  elems->mesh_id_len) == 0 &&
1625b9c547cSRui Paulo 		mconf->mesh_pp_id == elems->mesh_config[0] &&
1635b9c547cSRui Paulo 		mconf->mesh_pm_id == elems->mesh_config[1] &&
1645b9c547cSRui Paulo 		mconf->mesh_cc_id == elems->mesh_config[2] &&
1655b9c547cSRui Paulo 		mconf->mesh_sp_id == elems->mesh_config[3] &&
1665b9c547cSRui Paulo 		mconf->mesh_auth_id == elems->mesh_config[4]);
1675b9c547cSRui Paulo }
1685b9c547cSRui Paulo 
1695b9c547cSRui Paulo 
1705b9c547cSRui Paulo /* check if local link id is already used with another peer */
llid_in_use(struct wpa_supplicant * wpa_s,u16 llid)171c1d255d3SCy Schubert static bool llid_in_use(struct wpa_supplicant *wpa_s, u16 llid)
1725b9c547cSRui Paulo {
1735b9c547cSRui Paulo 	struct sta_info *sta;
1745b9c547cSRui Paulo 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
1755b9c547cSRui Paulo 
1765b9c547cSRui Paulo 	for (sta = hapd->sta_list; sta; sta = sta->next) {
1775b9c547cSRui Paulo 		if (sta->my_lid == llid)
178c1d255d3SCy Schubert 			return true;
1795b9c547cSRui Paulo 	}
1805b9c547cSRui Paulo 
181c1d255d3SCy Schubert 	return false;
1825b9c547cSRui Paulo }
1835b9c547cSRui Paulo 
1845b9c547cSRui Paulo 
1855b9c547cSRui Paulo /* generate an llid for a link and set to initial state */
mesh_mpm_init_link(struct wpa_supplicant * wpa_s,struct sta_info * sta)1865b9c547cSRui Paulo static void mesh_mpm_init_link(struct wpa_supplicant *wpa_s,
1875b9c547cSRui Paulo 			       struct sta_info *sta)
1885b9c547cSRui Paulo {
1895b9c547cSRui Paulo 	u16 llid;
1905b9c547cSRui Paulo 
1915b9c547cSRui Paulo 	do {
1925b9c547cSRui Paulo 		if (os_get_random((u8 *) &llid, sizeof(llid)) < 0)
1934bc52338SCy Schubert 			llid = 0; /* continue */
1945b9c547cSRui Paulo 	} while (!llid || llid_in_use(wpa_s, llid));
1955b9c547cSRui Paulo 
1965b9c547cSRui Paulo 	sta->my_lid = llid;
1975b9c547cSRui Paulo 	sta->peer_lid = 0;
198780fb4a2SCy Schubert 	sta->peer_aid = 0;
1995b9c547cSRui Paulo 
2005b9c547cSRui Paulo 	/*
2015b9c547cSRui Paulo 	 * We do not use wpa_mesh_set_plink_state() here because there is no
2025b9c547cSRui Paulo 	 * entry in kernel yet.
2035b9c547cSRui Paulo 	 */
204780fb4a2SCy Schubert 	sta->plink_state = PLINK_IDLE;
2055b9c547cSRui Paulo }
2065b9c547cSRui Paulo 
2075b9c547cSRui Paulo 
mesh_mpm_send_plink_action(struct wpa_supplicant * wpa_s,struct sta_info * sta,enum plink_action_field type,u16 close_reason)2085b9c547cSRui Paulo static void mesh_mpm_send_plink_action(struct wpa_supplicant *wpa_s,
2095b9c547cSRui Paulo 				       struct sta_info *sta,
2105b9c547cSRui Paulo 				       enum plink_action_field type,
2115b9c547cSRui Paulo 				       u16 close_reason)
2125b9c547cSRui Paulo {
2135b9c547cSRui Paulo 	struct wpabuf *buf;
2145b9c547cSRui Paulo 	struct hostapd_iface *ifmsh = wpa_s->ifmsh;
2155b9c547cSRui Paulo 	struct hostapd_data *bss = ifmsh->bss[0];
2165b9c547cSRui Paulo 	struct mesh_conf *conf = ifmsh->mconf;
2175b9c547cSRui Paulo 	u8 supp_rates[2 + 2 + 32];
2185b9c547cSRui Paulo 	u8 *pos, *cat;
2195b9c547cSRui Paulo 	u8 ie_len, add_plid = 0;
2205b9c547cSRui Paulo 	int ret;
2215b9c547cSRui Paulo 	int ampe = conf->security & MESH_CONF_SEC_AMPE;
2225b9c547cSRui Paulo 	size_t buf_len;
2235b9c547cSRui Paulo 
2245b9c547cSRui Paulo 	if (!sta)
2255b9c547cSRui Paulo 		return;
2265b9c547cSRui Paulo 
22785732ac8SCy Schubert 	buf_len = 2 +      /* Category and Action */
22885732ac8SCy Schubert 		  2 +      /* capability info */
2295b9c547cSRui Paulo 		  2 +      /* AID */
2305b9c547cSRui Paulo 		  2 + 8 +  /* supported rates */
2315b9c547cSRui Paulo 		  2 + (32 - 8) +
2325b9c547cSRui Paulo 		  2 + 32 + /* mesh ID */
2335b9c547cSRui Paulo 		  2 + 7 +  /* mesh config */
23485732ac8SCy Schubert 		  2 + 24 + /* peering management */
235c1d255d3SCy Schubert 		  2 + 96 + 32 + 32 + /* AMPE (96 + max GTKlen + max IGTKlen) */
2365b9c547cSRui Paulo 		  2 + 16;  /* MIC */
2375b9c547cSRui Paulo 	if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
2385b9c547cSRui Paulo 		buf_len += 2 + 26 + /* HT capabilities */
2395b9c547cSRui Paulo 			   2 + 22;  /* HT operation */
2405b9c547cSRui Paulo 	}
241780fb4a2SCy Schubert #ifdef CONFIG_IEEE80211AC
242780fb4a2SCy Schubert 	if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
243780fb4a2SCy Schubert 		buf_len += 2 + 12 + /* VHT Capabilities */
244780fb4a2SCy Schubert 			   2 + 5;  /* VHT Operation */
245780fb4a2SCy Schubert 	}
246780fb4a2SCy Schubert #endif /* CONFIG_IEEE80211AC */
247206b73d0SCy Schubert #ifdef CONFIG_IEEE80211AX
248206b73d0SCy Schubert 	if (type != PLINK_CLOSE && wpa_s->mesh_he_enabled) {
249206b73d0SCy Schubert 		buf_len += 3 +
250206b73d0SCy Schubert 			   HE_MAX_MAC_CAPAB_SIZE +
251206b73d0SCy Schubert 			   HE_MAX_PHY_CAPAB_SIZE +
252206b73d0SCy Schubert 			   HE_MAX_MCS_CAPAB_SIZE +
253206b73d0SCy Schubert 			   HE_MAX_PPET_CAPAB_SIZE;
254206b73d0SCy Schubert 		buf_len += 3 + sizeof(struct ieee80211_he_operation);
2554b72b91aSCy Schubert 		if (is_6ghz_op_class(bss->iconf->op_class))
2564b72b91aSCy Schubert 			buf_len += sizeof(struct ieee80211_he_6ghz_oper_info) +
2574b72b91aSCy Schubert 				3 + sizeof(struct ieee80211_he_6ghz_band_cap);
258206b73d0SCy Schubert 	}
259206b73d0SCy Schubert #endif /* CONFIG_IEEE80211AX */
260325151a3SRui Paulo 	if (type != PLINK_CLOSE)
261325151a3SRui Paulo 		buf_len += conf->rsn_ie_len; /* RSN IE */
2624bc52338SCy Schubert #ifdef CONFIG_OCV
2634bc52338SCy Schubert 	/* OCI is included even when the other STA doesn't support OCV */
2644bc52338SCy Schubert 	if (type != PLINK_CLOSE && conf->ocv)
2654bc52338SCy Schubert 		buf_len += OCV_OCI_EXTENDED_LEN;
2664bc52338SCy Schubert #endif /* CONFIG_OCV */
267*a90b9d01SCy Schubert #ifdef CONFIG_IEEE80211BE
268*a90b9d01SCy Schubert 	if (type != PLINK_CLOSE && wpa_s->mesh_eht_enabled) {
269*a90b9d01SCy Schubert 		buf_len += 3 + 2 + EHT_PHY_CAPAB_LEN + EHT_MCS_NSS_CAPAB_LEN +
270*a90b9d01SCy Schubert 			EHT_PPE_THRESH_CAPAB_LEN;
271*a90b9d01SCy Schubert 		buf_len += 3 + sizeof(struct ieee80211_eht_operation);
272*a90b9d01SCy Schubert }
273*a90b9d01SCy Schubert #endif /* CONFIG_IEEE80211BE */
274325151a3SRui Paulo 
2755b9c547cSRui Paulo 	buf = wpabuf_alloc(buf_len);
2765b9c547cSRui Paulo 	if (!buf)
2775b9c547cSRui Paulo 		return;
2785b9c547cSRui Paulo 
2795b9c547cSRui Paulo 	cat = wpabuf_mhead_u8(buf);
2805b9c547cSRui Paulo 	wpabuf_put_u8(buf, WLAN_ACTION_SELF_PROTECTED);
2815b9c547cSRui Paulo 	wpabuf_put_u8(buf, type);
2825b9c547cSRui Paulo 
2835b9c547cSRui Paulo 	if (type != PLINK_CLOSE) {
2845b9c547cSRui Paulo 		u8 info;
2855b9c547cSRui Paulo 
2865b9c547cSRui Paulo 		/* capability info */
2875b9c547cSRui Paulo 		wpabuf_put_le16(buf, ampe ? IEEE80211_CAP_PRIVACY : 0);
2885b9c547cSRui Paulo 
2895b9c547cSRui Paulo 		/* aid */
2905b9c547cSRui Paulo 		if (type == PLINK_CONFIRM)
291780fb4a2SCy Schubert 			wpabuf_put_le16(buf, sta->aid);
2925b9c547cSRui Paulo 
2935b9c547cSRui Paulo 		/* IE: supp + ext. supp rates */
2945b9c547cSRui Paulo 		pos = hostapd_eid_supp_rates(bss, supp_rates);
2955b9c547cSRui Paulo 		pos = hostapd_eid_ext_supp_rates(bss, pos);
2965b9c547cSRui Paulo 		wpabuf_put_data(buf, supp_rates, pos - supp_rates);
2975b9c547cSRui Paulo 
298325151a3SRui Paulo 		/* IE: RSN IE */
299325151a3SRui Paulo 		wpabuf_put_data(buf, conf->rsn_ie, conf->rsn_ie_len);
300325151a3SRui Paulo 
3015b9c547cSRui Paulo 		/* IE: Mesh ID */
3025b9c547cSRui Paulo 		wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
3035b9c547cSRui Paulo 		wpabuf_put_u8(buf, conf->meshid_len);
3045b9c547cSRui Paulo 		wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
3055b9c547cSRui Paulo 
3065b9c547cSRui Paulo 		/* IE: mesh conf */
3075b9c547cSRui Paulo 		wpabuf_put_u8(buf, WLAN_EID_MESH_CONFIG);
3085b9c547cSRui Paulo 		wpabuf_put_u8(buf, 7);
3095b9c547cSRui Paulo 		wpabuf_put_u8(buf, conf->mesh_pp_id);
3105b9c547cSRui Paulo 		wpabuf_put_u8(buf, conf->mesh_pm_id);
3115b9c547cSRui Paulo 		wpabuf_put_u8(buf, conf->mesh_cc_id);
3125b9c547cSRui Paulo 		wpabuf_put_u8(buf, conf->mesh_sp_id);
3135b9c547cSRui Paulo 		wpabuf_put_u8(buf, conf->mesh_auth_id);
3145b9c547cSRui Paulo 		info = (bss->num_plinks > 63 ? 63 : bss->num_plinks) << 1;
3155b9c547cSRui Paulo 		/* TODO: Add Connected to Mesh Gate/AS subfields */
3165b9c547cSRui Paulo 		wpabuf_put_u8(buf, info);
31732a95656SCy Schubert 		/* Set forwarding based on configuration and always accept
31832a95656SCy Schubert 		 * plinks for now */
319780fb4a2SCy Schubert 		wpabuf_put_u8(buf, MESH_CAP_ACCEPT_ADDITIONAL_PEER |
32032a95656SCy Schubert 			      (conf->mesh_fwding ? MESH_CAP_FORWARDING : 0));
3215b9c547cSRui Paulo 	} else {	/* Peer closing frame */
3225b9c547cSRui Paulo 		/* IE: Mesh ID */
3235b9c547cSRui Paulo 		wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
3245b9c547cSRui Paulo 		wpabuf_put_u8(buf, conf->meshid_len);
3255b9c547cSRui Paulo 		wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
3265b9c547cSRui Paulo 	}
3275b9c547cSRui Paulo 
3285b9c547cSRui Paulo 	/* IE: Mesh Peering Management element */
3295b9c547cSRui Paulo 	ie_len = 4;
3305b9c547cSRui Paulo 	if (ampe)
3315b9c547cSRui Paulo 		ie_len += PMKID_LEN;
3325b9c547cSRui Paulo 	switch (type) {
3335b9c547cSRui Paulo 	case PLINK_OPEN:
3345b9c547cSRui Paulo 		break;
3355b9c547cSRui Paulo 	case PLINK_CONFIRM:
3365b9c547cSRui Paulo 		ie_len += 2;
3375b9c547cSRui Paulo 		add_plid = 1;
3385b9c547cSRui Paulo 		break;
3395b9c547cSRui Paulo 	case PLINK_CLOSE:
3405b9c547cSRui Paulo 		ie_len += 2;
3415b9c547cSRui Paulo 		add_plid = 1;
3425b9c547cSRui Paulo 		ie_len += 2; /* reason code */
3435b9c547cSRui Paulo 		break;
3445b9c547cSRui Paulo 	}
3455b9c547cSRui Paulo 
3465b9c547cSRui Paulo 	wpabuf_put_u8(buf, WLAN_EID_PEER_MGMT);
3475b9c547cSRui Paulo 	wpabuf_put_u8(buf, ie_len);
3485b9c547cSRui Paulo 	/* peering protocol */
3495b9c547cSRui Paulo 	if (ampe)
3505b9c547cSRui Paulo 		wpabuf_put_le16(buf, 1);
3515b9c547cSRui Paulo 	else
3525b9c547cSRui Paulo 		wpabuf_put_le16(buf, 0);
3535b9c547cSRui Paulo 	wpabuf_put_le16(buf, sta->my_lid);
3545b9c547cSRui Paulo 	if (add_plid)
3555b9c547cSRui Paulo 		wpabuf_put_le16(buf, sta->peer_lid);
3565b9c547cSRui Paulo 	if (type == PLINK_CLOSE)
3575b9c547cSRui Paulo 		wpabuf_put_le16(buf, close_reason);
3585b9c547cSRui Paulo 	if (ampe) {
3595b9c547cSRui Paulo 		if (sta->sae == NULL) {
3605b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_INFO, "Mesh MPM: no SAE session");
3615b9c547cSRui Paulo 			goto fail;
3625b9c547cSRui Paulo 		}
3635b9c547cSRui Paulo 		mesh_rsn_get_pmkid(wpa_s->mesh_rsn, sta,
3645b9c547cSRui Paulo 				   wpabuf_put(buf, PMKID_LEN));
3655b9c547cSRui Paulo 	}
3665b9c547cSRui Paulo 
3675b9c547cSRui Paulo 	if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
368780fb4a2SCy Schubert 		u8 ht_capa_oper[2 + 26 + 2 + 22];
369780fb4a2SCy Schubert 
3705b9c547cSRui Paulo 		pos = hostapd_eid_ht_capabilities(bss, ht_capa_oper);
3715b9c547cSRui Paulo 		pos = hostapd_eid_ht_operation(bss, pos);
3725b9c547cSRui Paulo 		wpabuf_put_data(buf, ht_capa_oper, pos - ht_capa_oper);
3735b9c547cSRui Paulo 	}
374780fb4a2SCy Schubert #ifdef CONFIG_IEEE80211AC
375780fb4a2SCy Schubert 	if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
376780fb4a2SCy Schubert 		u8 vht_capa_oper[2 + 12 + 2 + 5];
377780fb4a2SCy Schubert 
378780fb4a2SCy Schubert 		pos = hostapd_eid_vht_capabilities(bss, vht_capa_oper, 0);
379780fb4a2SCy Schubert 		pos = hostapd_eid_vht_operation(bss, pos);
380780fb4a2SCy Schubert 		wpabuf_put_data(buf, vht_capa_oper, pos - vht_capa_oper);
381780fb4a2SCy Schubert 	}
382780fb4a2SCy Schubert #endif /* CONFIG_IEEE80211AC */
383206b73d0SCy Schubert #ifdef CONFIG_IEEE80211AX
384206b73d0SCy Schubert 	if (type != PLINK_CLOSE && wpa_s->mesh_he_enabled) {
385206b73d0SCy Schubert 		u8 he_capa_oper[3 +
386206b73d0SCy Schubert 				HE_MAX_MAC_CAPAB_SIZE +
387206b73d0SCy Schubert 				HE_MAX_PHY_CAPAB_SIZE +
388206b73d0SCy Schubert 				HE_MAX_MCS_CAPAB_SIZE +
389206b73d0SCy Schubert 				HE_MAX_PPET_CAPAB_SIZE +
3904b72b91aSCy Schubert 				3 + sizeof(struct ieee80211_he_operation) +
3914b72b91aSCy Schubert 				sizeof(struct ieee80211_he_6ghz_oper_info) +
3924b72b91aSCy Schubert 				3 + sizeof(struct ieee80211_he_6ghz_band_cap)];
393206b73d0SCy Schubert 
394206b73d0SCy Schubert 		pos = hostapd_eid_he_capab(bss, he_capa_oper,
395206b73d0SCy Schubert 					   IEEE80211_MODE_MESH);
396206b73d0SCy Schubert 		pos = hostapd_eid_he_operation(bss, pos);
3974b72b91aSCy Schubert 		pos = hostapd_eid_he_6ghz_band_cap(bss, pos);
398206b73d0SCy Schubert 		wpabuf_put_data(buf, he_capa_oper, pos - he_capa_oper);
399206b73d0SCy Schubert 	}
400206b73d0SCy Schubert #endif /* CONFIG_IEEE80211AX */
4014bc52338SCy Schubert #ifdef CONFIG_OCV
4024bc52338SCy Schubert 	if (type != PLINK_CLOSE && conf->ocv) {
4034bc52338SCy Schubert 		struct wpa_channel_info ci;
4044bc52338SCy Schubert 
4054bc52338SCy Schubert 		if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
4064bc52338SCy Schubert 			wpa_printf(MSG_WARNING,
4074bc52338SCy Schubert 				   "Mesh MPM: Failed to get channel info for OCI element");
4084bc52338SCy Schubert 			goto fail;
4094bc52338SCy Schubert 		}
4104bc52338SCy Schubert 
4114bc52338SCy Schubert 		pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
4124bc52338SCy Schubert 		if (ocv_insert_extended_oci(&ci, pos) < 0)
4134bc52338SCy Schubert 			goto fail;
4144bc52338SCy Schubert 	}
4154bc52338SCy Schubert #endif /* CONFIG_OCV */
4164bc52338SCy Schubert 
417*a90b9d01SCy Schubert #ifdef CONFIG_IEEE80211BE
418*a90b9d01SCy Schubert 	if (type != PLINK_CLOSE && wpa_s->mesh_eht_enabled) {
419*a90b9d01SCy Schubert 		u8 eht_capa_oper[3 +
420*a90b9d01SCy Schubert 				 2 +
421*a90b9d01SCy Schubert 				 EHT_PHY_CAPAB_LEN +
422*a90b9d01SCy Schubert 				 EHT_MCS_NSS_CAPAB_LEN +
423*a90b9d01SCy Schubert 				 EHT_PPE_THRESH_CAPAB_LEN +
424*a90b9d01SCy Schubert 				 3 + sizeof(struct ieee80211_eht_operation)];
425*a90b9d01SCy Schubert 		pos = hostapd_eid_eht_capab(bss, eht_capa_oper,
426*a90b9d01SCy Schubert 					    IEEE80211_MODE_MESH);
427*a90b9d01SCy Schubert 		pos = hostapd_eid_eht_operation(bss, pos);
428*a90b9d01SCy Schubert 		wpabuf_put_data(buf, eht_capa_oper, pos - eht_capa_oper);
429*a90b9d01SCy Schubert 	}
430*a90b9d01SCy Schubert #endif /* CONFIG_IEEE80211BE */
431*a90b9d01SCy Schubert 
4325b9c547cSRui Paulo 	if (ampe && mesh_rsn_protect_frame(wpa_s->mesh_rsn, sta, cat, buf)) {
4335b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO,
4345b9c547cSRui Paulo 			"Mesh MPM: failed to add AMPE and MIC IE");
4355b9c547cSRui Paulo 		goto fail;
4365b9c547cSRui Paulo 	}
4375b9c547cSRui Paulo 
438780fb4a2SCy Schubert 	wpa_msg(wpa_s, MSG_DEBUG, "Mesh MPM: Sending peering frame type %d to "
439780fb4a2SCy Schubert 		MACSTR " (my_lid=0x%x peer_lid=0x%x)",
440780fb4a2SCy Schubert 		type, MAC2STR(sta->addr), sta->my_lid, sta->peer_lid);
4415b9c547cSRui Paulo 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
4425b9c547cSRui Paulo 				  sta->addr, wpa_s->own_addr, wpa_s->own_addr,
4435b9c547cSRui Paulo 				  wpabuf_head(buf), wpabuf_len(buf), 0);
4445b9c547cSRui Paulo 	if (ret < 0)
4455b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO,
4465b9c547cSRui Paulo 			"Mesh MPM: failed to send peering frame");
4475b9c547cSRui Paulo 
4485b9c547cSRui Paulo fail:
4495b9c547cSRui Paulo 	wpabuf_free(buf);
4505b9c547cSRui Paulo }
4515b9c547cSRui Paulo 
4525b9c547cSRui Paulo 
4535b9c547cSRui Paulo /* configure peering state in ours and driver's station entry */
wpa_mesh_set_plink_state(struct wpa_supplicant * wpa_s,struct sta_info * sta,enum mesh_plink_state state)4545b9c547cSRui Paulo void wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s,
4555b9c547cSRui Paulo 			      struct sta_info *sta,
4565b9c547cSRui Paulo 			      enum mesh_plink_state state)
4575b9c547cSRui Paulo {
4585b9c547cSRui Paulo 	struct hostapd_sta_add_params params;
4595b9c547cSRui Paulo 	int ret;
4605b9c547cSRui Paulo 
461780fb4a2SCy Schubert 	wpa_msg(wpa_s, MSG_DEBUG, "MPM set " MACSTR " from %s into %s",
462780fb4a2SCy Schubert 		MAC2STR(sta->addr), mplstate[sta->plink_state],
463780fb4a2SCy Schubert 		mplstate[state]);
4645b9c547cSRui Paulo 	sta->plink_state = state;
4655b9c547cSRui Paulo 
4665b9c547cSRui Paulo 	os_memset(&params, 0, sizeof(params));
4675b9c547cSRui Paulo 	params.addr = sta->addr;
4685b9c547cSRui Paulo 	params.plink_state = state;
469780fb4a2SCy Schubert 	params.peer_aid = sta->peer_aid;
4705b9c547cSRui Paulo 	params.set = 1;
471*a90b9d01SCy Schubert 	params.mld_link_id = -1;
4725b9c547cSRui Paulo 
4735b9c547cSRui Paulo 	ret = wpa_drv_sta_add(wpa_s, &params);
4745b9c547cSRui Paulo 	if (ret) {
4755b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_ERROR, "Driver failed to set " MACSTR
4765b9c547cSRui Paulo 			": %d", MAC2STR(sta->addr), ret);
4775b9c547cSRui Paulo 	}
4785b9c547cSRui Paulo }
4795b9c547cSRui Paulo 
4805b9c547cSRui Paulo 
mesh_mpm_fsm_restart(struct wpa_supplicant * wpa_s,struct sta_info * sta)4815b9c547cSRui Paulo static void mesh_mpm_fsm_restart(struct wpa_supplicant *wpa_s,
4825b9c547cSRui Paulo 				 struct sta_info *sta)
4835b9c547cSRui Paulo {
4845b9c547cSRui Paulo 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
4855b9c547cSRui Paulo 
4865b9c547cSRui Paulo 	eloop_cancel_timeout(plink_timer, wpa_s, sta);
4875b9c547cSRui Paulo 
4885b9c547cSRui Paulo 	ap_free_sta(hapd, sta);
4895b9c547cSRui Paulo }
4905b9c547cSRui Paulo 
4915b9c547cSRui Paulo 
plink_timer(void * eloop_ctx,void * user_data)4925b9c547cSRui Paulo static void plink_timer(void *eloop_ctx, void *user_data)
4935b9c547cSRui Paulo {
4945b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s = eloop_ctx;
4955b9c547cSRui Paulo 	struct sta_info *sta = user_data;
4965b9c547cSRui Paulo 	u16 reason = 0;
4975b9c547cSRui Paulo 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
498780fb4a2SCy Schubert 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
4995b9c547cSRui Paulo 
5005b9c547cSRui Paulo 	switch (sta->plink_state) {
501780fb4a2SCy Schubert 	case PLINK_OPN_RCVD:
502780fb4a2SCy Schubert 	case PLINK_OPN_SNT:
5035b9c547cSRui Paulo 		/* retry timer */
5045b9c547cSRui Paulo 		if (sta->mpm_retries < conf->dot11MeshMaxRetries) {
5055b9c547cSRui Paulo 			eloop_register_timeout(
5065b9c547cSRui Paulo 				conf->dot11MeshRetryTimeout / 1000,
5075b9c547cSRui Paulo 				(conf->dot11MeshRetryTimeout % 1000) * 1000,
5085b9c547cSRui Paulo 				plink_timer, wpa_s, sta);
5095b9c547cSRui Paulo 			mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
5105b9c547cSRui Paulo 			sta->mpm_retries++;
5115b9c547cSRui Paulo 			break;
5125b9c547cSRui Paulo 		}
5135b9c547cSRui Paulo 		reason = WLAN_REASON_MESH_MAX_RETRIES;
51485732ac8SCy Schubert 		/* fall through */
5155b9c547cSRui Paulo 
5165b9c547cSRui Paulo 	case PLINK_CNF_RCVD:
5175b9c547cSRui Paulo 		/* confirm timer */
5185b9c547cSRui Paulo 		if (!reason)
5195b9c547cSRui Paulo 			reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
5205b9c547cSRui Paulo 		wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
5215b9c547cSRui Paulo 		eloop_register_timeout(conf->dot11MeshHoldingTimeout / 1000,
5225b9c547cSRui Paulo 			(conf->dot11MeshHoldingTimeout % 1000) * 1000,
5235b9c547cSRui Paulo 			plink_timer, wpa_s, sta);
5245b9c547cSRui Paulo 		mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
5255b9c547cSRui Paulo 		break;
5265b9c547cSRui Paulo 	case PLINK_HOLDING:
5275b9c547cSRui Paulo 		/* holding timer */
528780fb4a2SCy Schubert 
529780fb4a2SCy Schubert 		if (sta->mesh_sae_pmksa_caching) {
530780fb4a2SCy Schubert 			wpa_printf(MSG_DEBUG, "MPM: Peer " MACSTR
531780fb4a2SCy Schubert 				   " looks like it does not support mesh SAE PMKSA caching, so remove the cached entry for it",
532780fb4a2SCy Schubert 				   MAC2STR(sta->addr));
533780fb4a2SCy Schubert 			wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
534780fb4a2SCy Schubert 		}
5355b9c547cSRui Paulo 		mesh_mpm_fsm_restart(wpa_s, sta);
5365b9c547cSRui Paulo 		break;
5375b9c547cSRui Paulo 	default:
5385b9c547cSRui Paulo 		break;
5395b9c547cSRui Paulo 	}
5405b9c547cSRui Paulo }
5415b9c547cSRui Paulo 
5425b9c547cSRui Paulo 
5435b9c547cSRui Paulo /* initiate peering with station */
5445b9c547cSRui Paulo static void
mesh_mpm_plink_open(struct wpa_supplicant * wpa_s,struct sta_info * sta,enum mesh_plink_state next_state)5455b9c547cSRui Paulo mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
5465b9c547cSRui Paulo 		    enum mesh_plink_state next_state)
5475b9c547cSRui Paulo {
5485b9c547cSRui Paulo 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
5495b9c547cSRui Paulo 
5505b9c547cSRui Paulo 	eloop_cancel_timeout(plink_timer, wpa_s, sta);
5515b9c547cSRui Paulo 	eloop_register_timeout(conf->dot11MeshRetryTimeout / 1000,
5525b9c547cSRui Paulo 			       (conf->dot11MeshRetryTimeout % 1000) * 1000,
5535b9c547cSRui Paulo 			       plink_timer, wpa_s, sta);
5545b9c547cSRui Paulo 	mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
5555b9c547cSRui Paulo 	wpa_mesh_set_plink_state(wpa_s, sta, next_state);
5565b9c547cSRui Paulo }
5575b9c547cSRui Paulo 
5585b9c547cSRui Paulo 
mesh_mpm_plink_close(struct hostapd_data * hapd,struct sta_info * sta,void * ctx)559780fb4a2SCy Schubert static int mesh_mpm_plink_close(struct hostapd_data *hapd, struct sta_info *sta,
560780fb4a2SCy Schubert 				void *ctx)
5615b9c547cSRui Paulo {
5625b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s = ctx;
5635b9c547cSRui Paulo 	int reason = WLAN_REASON_MESH_PEERING_CANCELLED;
5645b9c547cSRui Paulo 
5655b9c547cSRui Paulo 	if (sta) {
566*a90b9d01SCy Schubert 		if (sta->plink_state == PLINK_ESTAB) {
567c1d255d3SCy Schubert 			hapd->num_plinks--;
568*a90b9d01SCy Schubert 			wpas_notify_mesh_peer_disconnected(
569*a90b9d01SCy Schubert 				wpa_s, sta->addr, WLAN_REASON_UNSPECIFIED);
570*a90b9d01SCy Schubert 		}
5715b9c547cSRui Paulo 		wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
5725b9c547cSRui Paulo 		mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
5735b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "MPM closing plink sta=" MACSTR,
5745b9c547cSRui Paulo 			   MAC2STR(sta->addr));
5755b9c547cSRui Paulo 		eloop_cancel_timeout(plink_timer, wpa_s, sta);
576c1d255d3SCy Schubert 		eloop_cancel_timeout(mesh_auth_timer, wpa_s, sta);
5775b9c547cSRui Paulo 		return 0;
5785b9c547cSRui Paulo 	}
5795b9c547cSRui Paulo 
5805b9c547cSRui Paulo 	return 1;
5815b9c547cSRui Paulo }
5825b9c547cSRui Paulo 
5835b9c547cSRui Paulo 
mesh_mpm_close_peer(struct wpa_supplicant * wpa_s,const u8 * addr)584780fb4a2SCy Schubert int mesh_mpm_close_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
585780fb4a2SCy Schubert {
586780fb4a2SCy Schubert 	struct hostapd_data *hapd;
587780fb4a2SCy Schubert 	struct sta_info *sta;
588780fb4a2SCy Schubert 
589780fb4a2SCy Schubert 	if (!wpa_s->ifmsh) {
590780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, "Mesh is not prepared yet");
591780fb4a2SCy Schubert 		return -1;
592780fb4a2SCy Schubert 	}
593780fb4a2SCy Schubert 
594780fb4a2SCy Schubert 	hapd = wpa_s->ifmsh->bss[0];
595780fb4a2SCy Schubert 	sta = ap_get_sta(hapd, addr);
596780fb4a2SCy Schubert 	if (!sta) {
597780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, "No such mesh peer");
598780fb4a2SCy Schubert 		return -1;
599780fb4a2SCy Schubert 	}
600780fb4a2SCy Schubert 
601780fb4a2SCy Schubert 	return mesh_mpm_plink_close(hapd, sta, wpa_s) == 0 ? 0 : -1;
602780fb4a2SCy Schubert }
603780fb4a2SCy Schubert 
604780fb4a2SCy Schubert 
peer_add_timer(void * eloop_ctx,void * user_data)605780fb4a2SCy Schubert static void peer_add_timer(void *eloop_ctx, void *user_data)
606780fb4a2SCy Schubert {
607780fb4a2SCy Schubert 	struct wpa_supplicant *wpa_s = eloop_ctx;
608780fb4a2SCy Schubert 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
609780fb4a2SCy Schubert 
610780fb4a2SCy Schubert 	os_memset(hapd->mesh_required_peer, 0, ETH_ALEN);
611780fb4a2SCy Schubert }
612780fb4a2SCy Schubert 
613780fb4a2SCy Schubert 
mesh_mpm_connect_peer(struct wpa_supplicant * wpa_s,const u8 * addr,int duration)614780fb4a2SCy Schubert int mesh_mpm_connect_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
615780fb4a2SCy Schubert 			  int duration)
616780fb4a2SCy Schubert {
617780fb4a2SCy Schubert 	struct wpa_ssid *ssid = wpa_s->current_ssid;
618780fb4a2SCy Schubert 	struct hostapd_data *hapd;
619780fb4a2SCy Schubert 	struct sta_info *sta;
620780fb4a2SCy Schubert 	struct mesh_conf *conf;
621780fb4a2SCy Schubert 
622780fb4a2SCy Schubert 	if (!wpa_s->ifmsh) {
623780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, "Mesh is not prepared yet");
624780fb4a2SCy Schubert 		return -1;
625780fb4a2SCy Schubert 	}
626780fb4a2SCy Schubert 
627780fb4a2SCy Schubert 	if (!ssid || !ssid->no_auto_peer) {
628780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO,
629780fb4a2SCy Schubert 			"This command is available only with no_auto_peer mesh network");
630780fb4a2SCy Schubert 		return -1;
631780fb4a2SCy Schubert 	}
632780fb4a2SCy Schubert 
633780fb4a2SCy Schubert 	hapd = wpa_s->ifmsh->bss[0];
634780fb4a2SCy Schubert 	conf = wpa_s->ifmsh->mconf;
635780fb4a2SCy Schubert 
636780fb4a2SCy Schubert 	sta = ap_get_sta(hapd, addr);
637780fb4a2SCy Schubert 	if (!sta) {
638780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, "No such mesh peer");
639780fb4a2SCy Schubert 		return -1;
640780fb4a2SCy Schubert 	}
641780fb4a2SCy Schubert 
642780fb4a2SCy Schubert 	if ((PLINK_OPN_SNT <= sta->plink_state &&
643780fb4a2SCy Schubert 	    sta->plink_state <= PLINK_ESTAB) ||
644780fb4a2SCy Schubert 	    (sta->sae && sta->sae->state > SAE_NOTHING)) {
645780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO,
646780fb4a2SCy Schubert 			"Specified peer is connecting/connected");
647780fb4a2SCy Schubert 		return -1;
648780fb4a2SCy Schubert 	}
649780fb4a2SCy Schubert 
650780fb4a2SCy Schubert 	if (conf->security == MESH_CONF_SEC_NONE) {
651780fb4a2SCy Schubert 		mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_SNT);
652780fb4a2SCy Schubert 	} else {
653780fb4a2SCy Schubert 		mesh_rsn_auth_sae_sta(wpa_s, sta);
654780fb4a2SCy Schubert 		os_memcpy(hapd->mesh_required_peer, addr, ETH_ALEN);
655780fb4a2SCy Schubert 		eloop_register_timeout(duration == -1 ? 10 : duration, 0,
656780fb4a2SCy Schubert 				       peer_add_timer, wpa_s, NULL);
657780fb4a2SCy Schubert 	}
658780fb4a2SCy Schubert 
659780fb4a2SCy Schubert 	return 0;
660780fb4a2SCy Schubert }
661780fb4a2SCy Schubert 
662780fb4a2SCy Schubert 
mesh_mpm_deinit(struct wpa_supplicant * wpa_s,struct hostapd_iface * ifmsh)6635b9c547cSRui Paulo void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
6645b9c547cSRui Paulo {
6655b9c547cSRui Paulo 	struct hostapd_data *hapd = ifmsh->bss[0];
6665b9c547cSRui Paulo 
6675b9c547cSRui Paulo 	/* notify peers we're leaving */
6685b9c547cSRui Paulo 	ap_for_each_sta(hapd, mesh_mpm_plink_close, wpa_s);
6695b9c547cSRui Paulo 
6705b9c547cSRui Paulo 	hapd->num_plinks = 0;
6715b9c547cSRui Paulo 	hostapd_free_stas(hapd);
672780fb4a2SCy Schubert 	eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
6735b9c547cSRui Paulo }
6745b9c547cSRui Paulo 
6755b9c547cSRui Paulo 
6765b9c547cSRui Paulo /* for mesh_rsn to indicate this peer has completed authentication, and we're
6775b9c547cSRui Paulo  * ready to start AMPE */
mesh_mpm_auth_peer(struct wpa_supplicant * wpa_s,const u8 * addr)6785b9c547cSRui Paulo void mesh_mpm_auth_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
6795b9c547cSRui Paulo {
6805b9c547cSRui Paulo 	struct hostapd_data *data = wpa_s->ifmsh->bss[0];
6815b9c547cSRui Paulo 	struct hostapd_sta_add_params params;
6825b9c547cSRui Paulo 	struct sta_info *sta;
6835b9c547cSRui Paulo 	int ret;
6845b9c547cSRui Paulo 
6855b9c547cSRui Paulo 	sta = ap_get_sta(data, addr);
6865b9c547cSRui Paulo 	if (!sta) {
6875b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "no such mesh peer");
6885b9c547cSRui Paulo 		return;
6895b9c547cSRui Paulo 	}
6905b9c547cSRui Paulo 
6915b9c547cSRui Paulo 	/* TODO: Should do nothing if this STA is already authenticated, but
6925b9c547cSRui Paulo 	 * the AP code already sets this flag. */
6935b9c547cSRui Paulo 	sta->flags |= WLAN_STA_AUTH;
6945b9c547cSRui Paulo 
6955b9c547cSRui Paulo 	mesh_rsn_init_ampe_sta(wpa_s, sta);
6965b9c547cSRui Paulo 
6975b9c547cSRui Paulo 	os_memset(&params, 0, sizeof(params));
6985b9c547cSRui Paulo 	params.addr = sta->addr;
6995b9c547cSRui Paulo 	params.flags = WPA_STA_AUTHENTICATED | WPA_STA_AUTHORIZED;
7005b9c547cSRui Paulo 	params.set = 1;
701*a90b9d01SCy Schubert 	params.mld_link_id = -1;
7025b9c547cSRui Paulo 
7035b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "MPM authenticating " MACSTR,
7045b9c547cSRui Paulo 		MAC2STR(sta->addr));
7055b9c547cSRui Paulo 	ret = wpa_drv_sta_add(wpa_s, &params);
7065b9c547cSRui Paulo 	if (ret) {
7075b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_ERROR,
7085b9c547cSRui Paulo 			"Driver failed to set " MACSTR ": %d",
7095b9c547cSRui Paulo 			MAC2STR(sta->addr), ret);
7105b9c547cSRui Paulo 	}
7115b9c547cSRui Paulo 
7125b9c547cSRui Paulo 	if (!sta->my_lid)
7135b9c547cSRui Paulo 		mesh_mpm_init_link(wpa_s, sta);
7145b9c547cSRui Paulo 
715780fb4a2SCy Schubert 	mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_SNT);
7165b9c547cSRui Paulo }
7175b9c547cSRui Paulo 
7185b9c547cSRui Paulo /*
7195b9c547cSRui Paulo  * Initialize a sta_info structure for a peer and upload it into the driver
7205b9c547cSRui Paulo  * in preparation for beginning authentication or peering. This is done when a
7215b9c547cSRui Paulo  * Beacon (secure or open mesh) or a peering open frame (for open mesh) is
7225b9c547cSRui Paulo  * received from the peer for the first time.
7235b9c547cSRui Paulo  */
mesh_mpm_add_peer(struct wpa_supplicant * wpa_s,const u8 * addr,struct ieee802_11_elems * elems)7245b9c547cSRui Paulo static struct sta_info * mesh_mpm_add_peer(struct wpa_supplicant *wpa_s,
7255b9c547cSRui Paulo 					   const u8 *addr,
7265b9c547cSRui Paulo 					   struct ieee802_11_elems *elems)
7275b9c547cSRui Paulo {
7285b9c547cSRui Paulo 	struct hostapd_sta_add_params params;
7295b9c547cSRui Paulo 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
7305b9c547cSRui Paulo 	struct hostapd_data *data = wpa_s->ifmsh->bss[0];
7315b9c547cSRui Paulo 	struct sta_info *sta;
73285732ac8SCy Schubert 	struct ieee80211_ht_operation *oper;
7335b9c547cSRui Paulo 	int ret;
7345b9c547cSRui Paulo 
735780fb4a2SCy Schubert 	if (elems->mesh_config_len >= 7 &&
736780fb4a2SCy Schubert 	    !(elems->mesh_config[6] & MESH_CAP_ACCEPT_ADDITIONAL_PEER)) {
737780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_DEBUG,
738780fb4a2SCy Schubert 			"mesh: Ignore a crowded peer " MACSTR,
739780fb4a2SCy Schubert 			MAC2STR(addr));
740780fb4a2SCy Schubert 		return NULL;
741780fb4a2SCy Schubert 	}
742780fb4a2SCy Schubert 
7435b9c547cSRui Paulo 	sta = ap_get_sta(data, addr);
744c1d255d3SCy Schubert 	if (sta)
745c1d255d3SCy Schubert 		return NULL;
746c1d255d3SCy Schubert 
7475b9c547cSRui Paulo 	sta = ap_sta_add(data, addr);
7485b9c547cSRui Paulo 	if (!sta)
7495b9c547cSRui Paulo 		return NULL;
7505b9c547cSRui Paulo 
751780fb4a2SCy Schubert 	/* Set WMM by default since Mesh STAs are QoS STAs */
752780fb4a2SCy Schubert 	sta->flags |= WLAN_STA_WMM;
753780fb4a2SCy Schubert 
7545b9c547cSRui Paulo 	/* initialize sta */
7555b9c547cSRui Paulo 	if (copy_supp_rates(wpa_s, sta, elems)) {
7565b9c547cSRui Paulo 		ap_free_sta(data, sta);
7575b9c547cSRui Paulo 		return NULL;
7585b9c547cSRui Paulo 	}
7595b9c547cSRui Paulo 
760780fb4a2SCy Schubert 	if (!sta->my_lid)
7615b9c547cSRui Paulo 		mesh_mpm_init_link(wpa_s, sta);
7625b9c547cSRui Paulo 
763325151a3SRui Paulo 	copy_sta_ht_capab(data, sta, elems->ht_capabilities);
76485732ac8SCy Schubert 
76585732ac8SCy Schubert 	oper = (struct ieee80211_ht_operation *) elems->ht_operation;
76685732ac8SCy Schubert 	if (oper &&
76785732ac8SCy Schubert 	    !(oper->ht_param & HT_INFO_HT_PARAM_STA_CHNL_WIDTH) &&
76885732ac8SCy Schubert 	    sta->ht_capabilities) {
76985732ac8SCy Schubert 		wpa_msg(wpa_s, MSG_DEBUG, MACSTR
77085732ac8SCy Schubert 			" does not support 40 MHz bandwidth",
77185732ac8SCy Schubert 			MAC2STR(sta->addr));
77285732ac8SCy Schubert 		set_disable_ht40(sta->ht_capabilities, 1);
77385732ac8SCy Schubert 	}
77485732ac8SCy Schubert 
775*a90b9d01SCy Schubert 	if (update_ht_state(data, sta) > 0)
776*a90b9d01SCy Schubert 		ieee802_11_update_beacons(data->iface);
7775b9c547cSRui Paulo 
778780fb4a2SCy Schubert #ifdef CONFIG_IEEE80211AC
779780fb4a2SCy Schubert 	copy_sta_vht_capab(data, sta, elems->vht_capabilities);
7804bc52338SCy Schubert 	copy_sta_vht_oper(data, sta, elems->vht_operation);
781*a90b9d01SCy Schubert 	set_sta_vht_opmode(data, sta, elems->opmode_notif);
782780fb4a2SCy Schubert #endif /* CONFIG_IEEE80211AC */
783780fb4a2SCy Schubert 
784206b73d0SCy Schubert #ifdef CONFIG_IEEE80211AX
785206b73d0SCy Schubert 	copy_sta_he_capab(data, sta, IEEE80211_MODE_MESH,
786206b73d0SCy Schubert 			  elems->he_capabilities, elems->he_capabilities_len);
7874b72b91aSCy Schubert 	copy_sta_he_6ghz_capab(data, sta, elems->he_6ghz_band_cap);
788206b73d0SCy Schubert #endif /* CONFIG_IEEE80211AX */
789*a90b9d01SCy Schubert #ifdef CONFIG_IEEE80211BE
790*a90b9d01SCy Schubert 	copy_sta_eht_capab(data, sta, IEEE80211_MODE_MESH,
791*a90b9d01SCy Schubert 			   elems->he_capabilities,
792*a90b9d01SCy Schubert 			   elems->he_capabilities_len,
793*a90b9d01SCy Schubert 			   elems->eht_capabilities,
794*a90b9d01SCy Schubert 			   elems->eht_capabilities_len);
795*a90b9d01SCy Schubert #endif /*CONFIG_IEEE80211BE */
796206b73d0SCy Schubert 
797780fb4a2SCy Schubert 	if (hostapd_get_aid(data, sta) < 0) {
798780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_ERROR, "No AIDs available");
799780fb4a2SCy Schubert 		ap_free_sta(data, sta);
800780fb4a2SCy Schubert 		return NULL;
801780fb4a2SCy Schubert 	}
802780fb4a2SCy Schubert 
8035b9c547cSRui Paulo 	/* insert into driver */
8045b9c547cSRui Paulo 	os_memset(&params, 0, sizeof(params));
8055b9c547cSRui Paulo 	params.supp_rates = sta->supported_rates;
8065b9c547cSRui Paulo 	params.supp_rates_len = sta->supported_rates_len;
8075b9c547cSRui Paulo 	params.addr = addr;
8085b9c547cSRui Paulo 	params.plink_state = sta->plink_state;
809780fb4a2SCy Schubert 	params.aid = sta->aid;
810780fb4a2SCy Schubert 	params.peer_aid = sta->peer_aid;
8115b9c547cSRui Paulo 	params.listen_interval = 100;
8125b9c547cSRui Paulo 	params.ht_capabilities = sta->ht_capabilities;
813780fb4a2SCy Schubert 	params.vht_capabilities = sta->vht_capabilities;
814206b73d0SCy Schubert 	params.he_capab = sta->he_capab;
815206b73d0SCy Schubert 	params.he_capab_len = sta->he_capab_len;
8164b72b91aSCy Schubert 	params.he_6ghz_capab = sta->he_6ghz_capab;
817*a90b9d01SCy Schubert 	params.eht_capab = sta->eht_capab;
818*a90b9d01SCy Schubert 	params.eht_capab_len = sta->eht_capab_len;
8195b9c547cSRui Paulo 	params.flags |= WPA_STA_WMM;
8205b9c547cSRui Paulo 	params.flags_mask |= WPA_STA_AUTHENTICATED;
821*a90b9d01SCy Schubert 	params.mld_link_id = -1;
8225b9c547cSRui Paulo 	if (conf->security == MESH_CONF_SEC_NONE) {
8235b9c547cSRui Paulo 		params.flags |= WPA_STA_AUTHORIZED;
8245b9c547cSRui Paulo 		params.flags |= WPA_STA_AUTHENTICATED;
8255b9c547cSRui Paulo 	} else {
8265b9c547cSRui Paulo 		sta->flags |= WLAN_STA_MFP;
8275b9c547cSRui Paulo 		params.flags |= WPA_STA_MFP;
8285b9c547cSRui Paulo 	}
8295b9c547cSRui Paulo 
8305b9c547cSRui Paulo 	ret = wpa_drv_sta_add(wpa_s, &params);
8315b9c547cSRui Paulo 	if (ret) {
8325b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_ERROR,
8335b9c547cSRui Paulo 			"Driver failed to insert " MACSTR ": %d",
8345b9c547cSRui Paulo 			MAC2STR(addr), ret);
8355b9c547cSRui Paulo 		ap_free_sta(data, sta);
8365b9c547cSRui Paulo 		return NULL;
8375b9c547cSRui Paulo 	}
8385b9c547cSRui Paulo 
8395b9c547cSRui Paulo 	return sta;
8405b9c547cSRui Paulo }
8415b9c547cSRui Paulo 
8425b9c547cSRui Paulo 
wpa_mesh_new_mesh_peer(struct wpa_supplicant * wpa_s,const u8 * addr,struct ieee802_11_elems * elems)8435b9c547cSRui Paulo void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
8445b9c547cSRui Paulo 			    struct ieee802_11_elems *elems)
8455b9c547cSRui Paulo {
8465b9c547cSRui Paulo 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
8475b9c547cSRui Paulo 	struct hostapd_data *data = wpa_s->ifmsh->bss[0];
8485b9c547cSRui Paulo 	struct sta_info *sta;
8495b9c547cSRui Paulo 	struct wpa_ssid *ssid = wpa_s->current_ssid;
8505b9c547cSRui Paulo 
8515b9c547cSRui Paulo 	sta = mesh_mpm_add_peer(wpa_s, addr, elems);
8525b9c547cSRui Paulo 	if (!sta)
8535b9c547cSRui Paulo 		return;
8545b9c547cSRui Paulo 
855780fb4a2SCy Schubert 	if (ssid && ssid->no_auto_peer &&
856780fb4a2SCy Schubert 	    (is_zero_ether_addr(data->mesh_required_peer) ||
857*a90b9d01SCy Schubert 	     !ether_addr_equal(data->mesh_required_peer, addr))) {
8585b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
8595b9c547cSRui Paulo 			MACSTR " because of no_auto_peer", MAC2STR(addr));
8605b9c547cSRui Paulo 		if (data->mesh_pending_auth) {
8615b9c547cSRui Paulo 			struct os_reltime age;
8625b9c547cSRui Paulo 			const struct ieee80211_mgmt *mgmt;
8635b9c547cSRui Paulo 			struct hostapd_frame_info fi;
8645b9c547cSRui Paulo 
8655b9c547cSRui Paulo 			mgmt = wpabuf_head(data->mesh_pending_auth);
8665b9c547cSRui Paulo 			os_reltime_age(&data->mesh_pending_auth_time, &age);
8675b9c547cSRui Paulo 			if (age.sec < 2 &&
868*a90b9d01SCy Schubert 			    ether_addr_equal(mgmt->sa, addr)) {
8695b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG,
8705b9c547cSRui Paulo 					   "mesh: Process pending Authentication frame from %u.%06u seconds ago",
8715b9c547cSRui Paulo 					   (unsigned int) age.sec,
8725b9c547cSRui Paulo 					   (unsigned int) age.usec);
8735b9c547cSRui Paulo 				os_memset(&fi, 0, sizeof(fi));
8745b9c547cSRui Paulo 				ieee802_11_mgmt(
8755b9c547cSRui Paulo 					data,
8765b9c547cSRui Paulo 					wpabuf_head(data->mesh_pending_auth),
8775b9c547cSRui Paulo 					wpabuf_len(data->mesh_pending_auth),
8785b9c547cSRui Paulo 					&fi);
8795b9c547cSRui Paulo 			}
8805b9c547cSRui Paulo 			wpabuf_free(data->mesh_pending_auth);
8815b9c547cSRui Paulo 			data->mesh_pending_auth = NULL;
8825b9c547cSRui Paulo 		}
8835b9c547cSRui Paulo 		return;
8845b9c547cSRui Paulo 	}
8855b9c547cSRui Paulo 
886780fb4a2SCy Schubert 	if (conf->security == MESH_CONF_SEC_NONE) {
887780fb4a2SCy Schubert 		if (sta->plink_state < PLINK_OPN_SNT ||
888780fb4a2SCy Schubert 		    sta->plink_state > PLINK_ESTAB)
889780fb4a2SCy Schubert 			mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_SNT);
890780fb4a2SCy Schubert 	} else {
8915b9c547cSRui Paulo 		mesh_rsn_auth_sae_sta(wpa_s, sta);
8925b9c547cSRui Paulo 	}
893780fb4a2SCy Schubert }
8945b9c547cSRui Paulo 
8955b9c547cSRui Paulo 
mesh_mpm_mgmt_rx(struct wpa_supplicant * wpa_s,struct rx_mgmt * rx_mgmt)8965b9c547cSRui Paulo void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
8975b9c547cSRui Paulo {
8985b9c547cSRui Paulo 	struct hostapd_frame_info fi;
8995b9c547cSRui Paulo 
9005b9c547cSRui Paulo 	os_memset(&fi, 0, sizeof(fi));
9015b9c547cSRui Paulo 	fi.datarate = rx_mgmt->datarate;
9025b9c547cSRui Paulo 	fi.ssi_signal = rx_mgmt->ssi_signal;
9035b9c547cSRui Paulo 	ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
9045b9c547cSRui Paulo 			rx_mgmt->frame_len, &fi);
9055b9c547cSRui Paulo }
9065b9c547cSRui Paulo 
9075b9c547cSRui Paulo 
mesh_mpm_plink_estab(struct wpa_supplicant * wpa_s,struct sta_info * sta)9085b9c547cSRui Paulo static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
9095b9c547cSRui Paulo 				 struct sta_info *sta)
9105b9c547cSRui Paulo {
9115b9c547cSRui Paulo 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
9125b9c547cSRui Paulo 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
9135b9c547cSRui Paulo 	u8 seq[6] = {};
9145b9c547cSRui Paulo 
9155b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
9165b9c547cSRui Paulo 		MAC2STR(sta->addr));
9175b9c547cSRui Paulo 
9185b9c547cSRui Paulo 	if (conf->security & MESH_CONF_SEC_AMPE) {
919780fb4a2SCy Schubert 		wpa_hexdump_key(MSG_DEBUG, "mesh: MTK", sta->mtk, sta->mtk_len);
920*a90b9d01SCy Schubert 		wpa_drv_set_key(wpa_s, -1,
921*a90b9d01SCy Schubert 				wpa_cipher_to_alg(conf->pairwise_cipher),
922780fb4a2SCy Schubert 				sta->addr, 0, 0, seq, sizeof(seq),
923c1d255d3SCy Schubert 				sta->mtk, sta->mtk_len,
924c1d255d3SCy Schubert 				KEY_FLAG_PAIRWISE_RX_TX);
9255b9c547cSRui Paulo 
926780fb4a2SCy Schubert 		wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK Key RSC",
927780fb4a2SCy Schubert 				sta->mgtk_rsc, sizeof(sta->mgtk_rsc));
928780fb4a2SCy Schubert 		wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK",
929780fb4a2SCy Schubert 				sta->mgtk, sta->mgtk_len);
930*a90b9d01SCy Schubert 		wpa_drv_set_key(wpa_s, -1,
931*a90b9d01SCy Schubert 				wpa_cipher_to_alg(conf->group_cipher),
932780fb4a2SCy Schubert 				sta->addr, sta->mgtk_key_id, 0,
933780fb4a2SCy Schubert 				sta->mgtk_rsc, sizeof(sta->mgtk_rsc),
934c1d255d3SCy Schubert 				sta->mgtk, sta->mgtk_len,
935c1d255d3SCy Schubert 				KEY_FLAG_GROUP_RX);
936780fb4a2SCy Schubert 
937780fb4a2SCy Schubert 		if (sta->igtk_len) {
938780fb4a2SCy Schubert 			wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK Key RSC",
939780fb4a2SCy Schubert 					sta->igtk_rsc, sizeof(sta->igtk_rsc));
940780fb4a2SCy Schubert 			wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK",
941780fb4a2SCy Schubert 					sta->igtk, sta->igtk_len);
942780fb4a2SCy Schubert 			wpa_drv_set_key(
943*a90b9d01SCy Schubert 				wpa_s, -1,
944780fb4a2SCy Schubert 				wpa_cipher_to_alg(conf->mgmt_group_cipher),
945780fb4a2SCy Schubert 				sta->addr, sta->igtk_key_id, 0,
946780fb4a2SCy Schubert 				sta->igtk_rsc, sizeof(sta->igtk_rsc),
947c1d255d3SCy Schubert 				sta->igtk, sta->igtk_len,
948c1d255d3SCy Schubert 				KEY_FLAG_GROUP_RX);
949780fb4a2SCy Schubert 		}
9505b9c547cSRui Paulo 	}
9515b9c547cSRui Paulo 
9525b9c547cSRui Paulo 	wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
9535b9c547cSRui Paulo 	hapd->num_plinks++;
9545b9c547cSRui Paulo 
9555b9c547cSRui Paulo 	sta->flags |= WLAN_STA_ASSOC;
956780fb4a2SCy Schubert 	sta->mesh_sae_pmksa_caching = 0;
9575b9c547cSRui Paulo 
958780fb4a2SCy Schubert 	eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
959780fb4a2SCy Schubert 	peer_add_timer(wpa_s, NULL);
9605b9c547cSRui Paulo 	eloop_cancel_timeout(plink_timer, wpa_s, sta);
9615b9c547cSRui Paulo 
96285732ac8SCy Schubert 	wpas_notify_mesh_peer_connected(wpa_s, sta->addr);
9635b9c547cSRui Paulo }
9645b9c547cSRui Paulo 
9655b9c547cSRui Paulo 
mesh_mpm_fsm(struct wpa_supplicant * wpa_s,struct sta_info * sta,enum plink_event event,u16 reason)9665b9c547cSRui Paulo static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
967780fb4a2SCy Schubert 			 enum plink_event event, u16 reason)
9685b9c547cSRui Paulo {
9695b9c547cSRui Paulo 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
9705b9c547cSRui Paulo 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
9715b9c547cSRui Paulo 
9725b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
9735b9c547cSRui Paulo 		MAC2STR(sta->addr), mplstate[sta->plink_state],
9745b9c547cSRui Paulo 		mplevent[event]);
9755b9c547cSRui Paulo 
9765b9c547cSRui Paulo 	switch (sta->plink_state) {
977780fb4a2SCy Schubert 	case PLINK_IDLE:
9785b9c547cSRui Paulo 		switch (event) {
9795b9c547cSRui Paulo 		case CLS_ACPT:
9805b9c547cSRui Paulo 			mesh_mpm_fsm_restart(wpa_s, sta);
9815b9c547cSRui Paulo 			break;
9825b9c547cSRui Paulo 		case OPN_ACPT:
983780fb4a2SCy Schubert 			mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_RCVD);
9845b9c547cSRui Paulo 			mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
9855b9c547cSRui Paulo 						   0);
9865b9c547cSRui Paulo 			break;
987780fb4a2SCy Schubert 		case REQ_RJCT:
988780fb4a2SCy Schubert 			mesh_mpm_send_plink_action(wpa_s, sta,
989780fb4a2SCy Schubert 						   PLINK_CLOSE, reason);
990780fb4a2SCy Schubert 			break;
9915b9c547cSRui Paulo 		default:
9925b9c547cSRui Paulo 			break;
9935b9c547cSRui Paulo 		}
9945b9c547cSRui Paulo 		break;
995780fb4a2SCy Schubert 	case PLINK_OPN_SNT:
9965b9c547cSRui Paulo 		switch (event) {
9975b9c547cSRui Paulo 		case OPN_RJCT:
9985b9c547cSRui Paulo 		case CNF_RJCT:
999780fb4a2SCy Schubert 			if (!reason)
10005b9c547cSRui Paulo 				reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
10015b9c547cSRui Paulo 			/* fall-through */
10025b9c547cSRui Paulo 		case CLS_ACPT:
10035b9c547cSRui Paulo 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
10045b9c547cSRui Paulo 			if (!reason)
10055b9c547cSRui Paulo 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
10065b9c547cSRui Paulo 			eloop_register_timeout(
10075b9c547cSRui Paulo 				conf->dot11MeshHoldingTimeout / 1000,
10085b9c547cSRui Paulo 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
10095b9c547cSRui Paulo 				plink_timer, wpa_s, sta);
10105b9c547cSRui Paulo 			mesh_mpm_send_plink_action(wpa_s, sta,
10115b9c547cSRui Paulo 						   PLINK_CLOSE, reason);
10125b9c547cSRui Paulo 			break;
10135b9c547cSRui Paulo 		case OPN_ACPT:
10145b9c547cSRui Paulo 			/* retry timer is left untouched */
1015780fb4a2SCy Schubert 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPN_RCVD);
10165b9c547cSRui Paulo 			mesh_mpm_send_plink_action(wpa_s, sta,
10175b9c547cSRui Paulo 						   PLINK_CONFIRM, 0);
10185b9c547cSRui Paulo 			break;
10195b9c547cSRui Paulo 		case CNF_ACPT:
10205b9c547cSRui Paulo 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
1021780fb4a2SCy Schubert 			eloop_cancel_timeout(plink_timer, wpa_s, sta);
10225b9c547cSRui Paulo 			eloop_register_timeout(
10235b9c547cSRui Paulo 				conf->dot11MeshConfirmTimeout / 1000,
10245b9c547cSRui Paulo 				(conf->dot11MeshConfirmTimeout % 1000) * 1000,
10255b9c547cSRui Paulo 				plink_timer, wpa_s, sta);
10265b9c547cSRui Paulo 			break;
10275b9c547cSRui Paulo 		default:
10285b9c547cSRui Paulo 			break;
10295b9c547cSRui Paulo 		}
10305b9c547cSRui Paulo 		break;
1031780fb4a2SCy Schubert 	case PLINK_OPN_RCVD:
10325b9c547cSRui Paulo 		switch (event) {
10335b9c547cSRui Paulo 		case OPN_RJCT:
10345b9c547cSRui Paulo 		case CNF_RJCT:
1035780fb4a2SCy Schubert 			if (!reason)
10365b9c547cSRui Paulo 				reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
10375b9c547cSRui Paulo 			/* fall-through */
10385b9c547cSRui Paulo 		case CLS_ACPT:
10395b9c547cSRui Paulo 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
10405b9c547cSRui Paulo 			if (!reason)
10415b9c547cSRui Paulo 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
10425b9c547cSRui Paulo 			eloop_register_timeout(
10435b9c547cSRui Paulo 				conf->dot11MeshHoldingTimeout / 1000,
10445b9c547cSRui Paulo 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
10455b9c547cSRui Paulo 				plink_timer, wpa_s, sta);
10465b9c547cSRui Paulo 			sta->mpm_close_reason = reason;
10475b9c547cSRui Paulo 			mesh_mpm_send_plink_action(wpa_s, sta,
10485b9c547cSRui Paulo 						   PLINK_CLOSE, reason);
10495b9c547cSRui Paulo 			break;
10505b9c547cSRui Paulo 		case OPN_ACPT:
10515b9c547cSRui Paulo 			mesh_mpm_send_plink_action(wpa_s, sta,
10525b9c547cSRui Paulo 						   PLINK_CONFIRM, 0);
10535b9c547cSRui Paulo 			break;
10545b9c547cSRui Paulo 		case CNF_ACPT:
10555b9c547cSRui Paulo 			if (conf->security & MESH_CONF_SEC_AMPE)
10565b9c547cSRui Paulo 				mesh_rsn_derive_mtk(wpa_s, sta);
10575b9c547cSRui Paulo 			mesh_mpm_plink_estab(wpa_s, sta);
10585b9c547cSRui Paulo 			break;
10595b9c547cSRui Paulo 		default:
10605b9c547cSRui Paulo 			break;
10615b9c547cSRui Paulo 		}
10625b9c547cSRui Paulo 		break;
10635b9c547cSRui Paulo 	case PLINK_CNF_RCVD:
10645b9c547cSRui Paulo 		switch (event) {
10655b9c547cSRui Paulo 		case OPN_RJCT:
10665b9c547cSRui Paulo 		case CNF_RJCT:
1067780fb4a2SCy Schubert 			if (!reason)
10685b9c547cSRui Paulo 				reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
10695b9c547cSRui Paulo 			/* fall-through */
10705b9c547cSRui Paulo 		case CLS_ACPT:
10715b9c547cSRui Paulo 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
10725b9c547cSRui Paulo 			if (!reason)
10735b9c547cSRui Paulo 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
10745b9c547cSRui Paulo 			eloop_register_timeout(
10755b9c547cSRui Paulo 				conf->dot11MeshHoldingTimeout / 1000,
10765b9c547cSRui Paulo 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
10775b9c547cSRui Paulo 				plink_timer, wpa_s, sta);
10785b9c547cSRui Paulo 			sta->mpm_close_reason = reason;
10795b9c547cSRui Paulo 			mesh_mpm_send_plink_action(wpa_s, sta,
10805b9c547cSRui Paulo 						   PLINK_CLOSE, reason);
10815b9c547cSRui Paulo 			break;
10825b9c547cSRui Paulo 		case OPN_ACPT:
1083780fb4a2SCy Schubert 			if (conf->security & MESH_CONF_SEC_AMPE)
1084780fb4a2SCy Schubert 				mesh_rsn_derive_mtk(wpa_s, sta);
10855b9c547cSRui Paulo 			mesh_mpm_plink_estab(wpa_s, sta);
10865b9c547cSRui Paulo 			mesh_mpm_send_plink_action(wpa_s, sta,
10875b9c547cSRui Paulo 						   PLINK_CONFIRM, 0);
10885b9c547cSRui Paulo 			break;
10895b9c547cSRui Paulo 		default:
10905b9c547cSRui Paulo 			break;
10915b9c547cSRui Paulo 		}
10925b9c547cSRui Paulo 		break;
10935b9c547cSRui Paulo 	case PLINK_ESTAB:
10945b9c547cSRui Paulo 		switch (event) {
1095780fb4a2SCy Schubert 		case OPN_RJCT:
1096780fb4a2SCy Schubert 		case CNF_RJCT:
10975b9c547cSRui Paulo 		case CLS_ACPT:
10985b9c547cSRui Paulo 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
1099780fb4a2SCy Schubert 			if (!reason)
11005b9c547cSRui Paulo 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
11015b9c547cSRui Paulo 
11025b9c547cSRui Paulo 			eloop_register_timeout(
11035b9c547cSRui Paulo 				conf->dot11MeshHoldingTimeout / 1000,
11045b9c547cSRui Paulo 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
11055b9c547cSRui Paulo 				plink_timer, wpa_s, sta);
11065b9c547cSRui Paulo 			sta->mpm_close_reason = reason;
11075b9c547cSRui Paulo 
11085b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
11095b9c547cSRui Paulo 				" closed with reason %d",
11105b9c547cSRui Paulo 				MAC2STR(sta->addr), reason);
11115b9c547cSRui Paulo 
111285732ac8SCy Schubert 			wpas_notify_mesh_peer_disconnected(wpa_s, sta->addr,
111385732ac8SCy Schubert 							   reason);
111485732ac8SCy Schubert 
11155b9c547cSRui Paulo 			hapd->num_plinks--;
11165b9c547cSRui Paulo 
11175b9c547cSRui Paulo 			mesh_mpm_send_plink_action(wpa_s, sta,
11185b9c547cSRui Paulo 						   PLINK_CLOSE, reason);
11195b9c547cSRui Paulo 			break;
11205b9c547cSRui Paulo 		case OPN_ACPT:
11215b9c547cSRui Paulo 			mesh_mpm_send_plink_action(wpa_s, sta,
11225b9c547cSRui Paulo 						   PLINK_CONFIRM, 0);
11235b9c547cSRui Paulo 			break;
11245b9c547cSRui Paulo 		default:
11255b9c547cSRui Paulo 			break;
11265b9c547cSRui Paulo 		}
11275b9c547cSRui Paulo 		break;
11285b9c547cSRui Paulo 	case PLINK_HOLDING:
11295b9c547cSRui Paulo 		switch (event) {
11305b9c547cSRui Paulo 		case CLS_ACPT:
11315b9c547cSRui Paulo 			mesh_mpm_fsm_restart(wpa_s, sta);
11325b9c547cSRui Paulo 			break;
11335b9c547cSRui Paulo 		case OPN_ACPT:
11345b9c547cSRui Paulo 		case CNF_ACPT:
11355b9c547cSRui Paulo 		case OPN_RJCT:
11365b9c547cSRui Paulo 		case CNF_RJCT:
11375b9c547cSRui Paulo 			reason = sta->mpm_close_reason;
11385b9c547cSRui Paulo 			mesh_mpm_send_plink_action(wpa_s, sta,
11395b9c547cSRui Paulo 						   PLINK_CLOSE, reason);
11405b9c547cSRui Paulo 			break;
11415b9c547cSRui Paulo 		default:
11425b9c547cSRui Paulo 			break;
11435b9c547cSRui Paulo 		}
11445b9c547cSRui Paulo 		break;
11455b9c547cSRui Paulo 	default:
11465b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
11475b9c547cSRui Paulo 			"Unsupported MPM event %s for state %s",
11485b9c547cSRui Paulo 			mplevent[event], mplstate[sta->plink_state]);
11495b9c547cSRui Paulo 		break;
11505b9c547cSRui Paulo 	}
11515b9c547cSRui Paulo }
11525b9c547cSRui Paulo 
11535b9c547cSRui Paulo 
mesh_mpm_action_rx(struct wpa_supplicant * wpa_s,const struct ieee80211_mgmt * mgmt,size_t len)11545b9c547cSRui Paulo void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
11555b9c547cSRui Paulo 			const struct ieee80211_mgmt *mgmt, size_t len)
11565b9c547cSRui Paulo {
11575b9c547cSRui Paulo 	u8 action_field;
11585b9c547cSRui Paulo 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
11595b9c547cSRui Paulo 	struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
11605b9c547cSRui Paulo 	struct sta_info *sta;
1161780fb4a2SCy Schubert 	u16 plid = 0, llid = 0, aid = 0;
11625b9c547cSRui Paulo 	enum plink_event event;
11635b9c547cSRui Paulo 	struct ieee802_11_elems elems;
11645b9c547cSRui Paulo 	struct mesh_peer_mgmt_ie peer_mgmt_ie;
11655b9c547cSRui Paulo 	const u8 *ies;
11665b9c547cSRui Paulo 	size_t ie_len;
11675b9c547cSRui Paulo 	int ret;
1168780fb4a2SCy Schubert 	u16 reason = 0;
11695b9c547cSRui Paulo 
11705b9c547cSRui Paulo 	if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
11715b9c547cSRui Paulo 		return;
11725b9c547cSRui Paulo 
11735b9c547cSRui Paulo 	action_field = mgmt->u.action.u.slf_prot_action.action;
11745b9c547cSRui Paulo 	if (action_field != PLINK_OPEN &&
11755b9c547cSRui Paulo 	    action_field != PLINK_CONFIRM &&
11765b9c547cSRui Paulo 	    action_field != PLINK_CLOSE)
11775b9c547cSRui Paulo 		return;
11785b9c547cSRui Paulo 
11795b9c547cSRui Paulo 	ies = mgmt->u.action.u.slf_prot_action.variable;
11805b9c547cSRui Paulo 	ie_len = (const u8 *) mgmt + len -
11815b9c547cSRui Paulo 		mgmt->u.action.u.slf_prot_action.variable;
11825b9c547cSRui Paulo 
11835b9c547cSRui Paulo 	/* at least expect mesh id and peering mgmt */
11845b9c547cSRui Paulo 	if (ie_len < 2 + 2) {
11855b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
11865b9c547cSRui Paulo 			   "MPM: Ignore too short action frame %u ie_len %u",
11875b9c547cSRui Paulo 			   action_field, (unsigned int) ie_len);
11885b9c547cSRui Paulo 		return;
11895b9c547cSRui Paulo 	}
11905b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
11915b9c547cSRui Paulo 
11925b9c547cSRui Paulo 	if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
11935b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
11945b9c547cSRui Paulo 			   WPA_GET_LE16(ies));
11955b9c547cSRui Paulo 		ies += 2;	/* capability */
11965b9c547cSRui Paulo 		ie_len -= 2;
11975b9c547cSRui Paulo 	}
11985b9c547cSRui Paulo 	if (action_field == PLINK_CONFIRM) {
1199780fb4a2SCy Schubert 		aid = WPA_GET_LE16(ies);
1200780fb4a2SCy Schubert 		wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", aid);
12015b9c547cSRui Paulo 		ies += 2;	/* aid */
12025b9c547cSRui Paulo 		ie_len -= 2;
12035b9c547cSRui Paulo 	}
12045b9c547cSRui Paulo 
12055b9c547cSRui Paulo 	/* check for mesh peering, mesh id and mesh config IEs */
12065b9c547cSRui Paulo 	if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
12075b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
12085b9c547cSRui Paulo 		return;
12095b9c547cSRui Paulo 	}
12105b9c547cSRui Paulo 	if (!elems.peer_mgmt) {
12115b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
12125b9c547cSRui Paulo 			   "MPM: No Mesh Peering Management element");
12135b9c547cSRui Paulo 		return;
12145b9c547cSRui Paulo 	}
12155b9c547cSRui Paulo 	if (action_field != PLINK_CLOSE) {
12165b9c547cSRui Paulo 		if (!elems.mesh_id || !elems.mesh_config) {
12175b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
12185b9c547cSRui Paulo 				   "MPM: No Mesh ID or Mesh Configuration element");
12195b9c547cSRui Paulo 			return;
12205b9c547cSRui Paulo 		}
12215b9c547cSRui Paulo 
12225b9c547cSRui Paulo 		if (!matches_local(wpa_s, &elems)) {
12235b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
12245b9c547cSRui Paulo 				   "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
12255b9c547cSRui Paulo 			return;
12265b9c547cSRui Paulo 		}
12275b9c547cSRui Paulo 	}
12285b9c547cSRui Paulo 
12295b9c547cSRui Paulo 	ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
12305b9c547cSRui Paulo 				       elems.peer_mgmt,
12315b9c547cSRui Paulo 				       elems.peer_mgmt_len,
12325b9c547cSRui Paulo 				       &peer_mgmt_ie);
12335b9c547cSRui Paulo 	if (ret) {
12345b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
12355b9c547cSRui Paulo 		return;
12365b9c547cSRui Paulo 	}
12375b9c547cSRui Paulo 
12385b9c547cSRui Paulo 	/* the sender's llid is our plid and vice-versa */
12395b9c547cSRui Paulo 	plid = WPA_GET_LE16(peer_mgmt_ie.llid);
12405b9c547cSRui Paulo 	if (peer_mgmt_ie.plid)
12415b9c547cSRui Paulo 		llid = WPA_GET_LE16(peer_mgmt_ie.plid);
12425b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
12435b9c547cSRui Paulo 
1244780fb4a2SCy Schubert 	if (action_field == PLINK_CLOSE)
1245780fb4a2SCy Schubert 		wpa_printf(MSG_DEBUG, "MPM: close reason=%u",
1246780fb4a2SCy Schubert 			   WPA_GET_LE16(peer_mgmt_ie.reason));
1247780fb4a2SCy Schubert 
12485b9c547cSRui Paulo 	sta = ap_get_sta(hapd, mgmt->sa);
12495b9c547cSRui Paulo 
12505b9c547cSRui Paulo 	/*
12515b9c547cSRui Paulo 	 * If this is an open frame from an unknown STA, and this is an
12525b9c547cSRui Paulo 	 * open mesh, then go ahead and add the peer before proceeding.
12535b9c547cSRui Paulo 	 */
12545b9c547cSRui Paulo 	if (!sta && action_field == PLINK_OPEN &&
1255780fb4a2SCy Schubert 	    (!(mconf->security & MESH_CONF_SEC_AMPE) ||
125685732ac8SCy Schubert 	     wpa_auth_pmksa_get(hapd->wpa_auth, mgmt->sa, NULL)))
12575b9c547cSRui Paulo 		sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
12585b9c547cSRui Paulo 
12595b9c547cSRui Paulo 	if (!sta) {
12605b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
12615b9c547cSRui Paulo 		return;
12625b9c547cSRui Paulo 	}
12635b9c547cSRui Paulo 
12645b9c547cSRui Paulo #ifdef CONFIG_SAE
12655b9c547cSRui Paulo 	/* peer is in sae_accepted? */
12665b9c547cSRui Paulo 	if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
12675b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
12685b9c547cSRui Paulo 		return;
12695b9c547cSRui Paulo 	}
12705b9c547cSRui Paulo #endif /* CONFIG_SAE */
12715b9c547cSRui Paulo 
12725b9c547cSRui Paulo 	if (!sta->my_lid)
12735b9c547cSRui Paulo 		mesh_mpm_init_link(wpa_s, sta);
12745b9c547cSRui Paulo 
1275780fb4a2SCy Schubert 	if (mconf->security & MESH_CONF_SEC_AMPE) {
1276780fb4a2SCy Schubert 		int res;
1277780fb4a2SCy Schubert 
1278780fb4a2SCy Schubert 		res = mesh_rsn_process_ampe(wpa_s, sta, &elems,
12795b9c547cSRui Paulo 					    &mgmt->u.action.category,
1280780fb4a2SCy Schubert 					    peer_mgmt_ie.chosen_pmk,
1281780fb4a2SCy Schubert 					    ies, ie_len);
1282780fb4a2SCy Schubert 		if (res) {
1283780fb4a2SCy Schubert 			wpa_printf(MSG_DEBUG,
1284780fb4a2SCy Schubert 				   "MPM: RSN process rejected frame (res=%d)",
1285780fb4a2SCy Schubert 				   res);
1286780fb4a2SCy Schubert 			if (action_field == PLINK_OPEN && res == -2) {
1287780fb4a2SCy Schubert 				/* AES-SIV decryption failed */
1288780fb4a2SCy Schubert 				mesh_mpm_fsm(wpa_s, sta, OPN_RJCT,
1289780fb4a2SCy Schubert 					     WLAN_REASON_MESH_INVALID_GTK);
1290780fb4a2SCy Schubert 			}
12915b9c547cSRui Paulo 			return;
12925b9c547cSRui Paulo 		}
12934bc52338SCy Schubert 
12944bc52338SCy Schubert #ifdef CONFIG_OCV
12954bc52338SCy Schubert 		if (action_field == PLINK_OPEN && elems.rsn_ie) {
12964bc52338SCy Schubert 			struct wpa_state_machine *sm = sta->wpa_sm;
12974bc52338SCy Schubert 			struct wpa_ie_data data;
12984bc52338SCy Schubert 
12994bc52338SCy Schubert 			res = wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2,
13004bc52338SCy Schubert 						   elems.rsn_ie_len + 2,
13014bc52338SCy Schubert 						   &data);
13024bc52338SCy Schubert 			if (res) {
13034bc52338SCy Schubert 				wpa_printf(MSG_DEBUG,
13044bc52338SCy Schubert 					   "Failed to parse RSN IE (res=%d)",
13054bc52338SCy Schubert 					   res);
13064bc52338SCy Schubert 				wpa_hexdump(MSG_DEBUG, "RSN IE", elems.rsn_ie,
13074bc52338SCy Schubert 					    elems.rsn_ie_len);
13084bc52338SCy Schubert 				return;
13094bc52338SCy Schubert 			}
13104bc52338SCy Schubert 
13114bc52338SCy Schubert 			wpa_auth_set_ocv(sm, mconf->ocv &&
13124bc52338SCy Schubert 					 (data.capabilities &
13134bc52338SCy Schubert 					  WPA_CAPABILITY_OCVC));
13144bc52338SCy Schubert 		}
13154bc52338SCy Schubert 
13164bc52338SCy Schubert 		if (action_field != PLINK_CLOSE &&
13174bc52338SCy Schubert 		    wpa_auth_uses_ocv(sta->wpa_sm)) {
13184bc52338SCy Schubert 			struct wpa_channel_info ci;
13194bc52338SCy Schubert 			int tx_chanwidth;
13204bc52338SCy Schubert 			int tx_seg1_idx;
13214bc52338SCy Schubert 
13224bc52338SCy Schubert 			if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
13234bc52338SCy Schubert 				wpa_printf(MSG_WARNING,
13244bc52338SCy Schubert 					   "MPM: Failed to get channel info to validate received OCI in MPM Confirm");
13254bc52338SCy Schubert 				return;
13264bc52338SCy Schubert 			}
13274bc52338SCy Schubert 
13284bc52338SCy Schubert 			if (get_tx_parameters(
13294bc52338SCy Schubert 				    sta, channel_width_to_int(ci.chanwidth),
13304bc52338SCy Schubert 				    ci.seg1_idx, &tx_chanwidth,
13314bc52338SCy Schubert 				    &tx_seg1_idx) < 0)
13324bc52338SCy Schubert 				return;
13334bc52338SCy Schubert 
13344bc52338SCy Schubert 			if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
13354bc52338SCy Schubert 						 tx_chanwidth, tx_seg1_idx) !=
1336c1d255d3SCy Schubert 			    OCI_SUCCESS) {
1337c1d255d3SCy Schubert 				wpa_printf(MSG_WARNING, "MPM: OCV failed: %s",
13384bc52338SCy Schubert 					   ocv_errorstr);
13394bc52338SCy Schubert 				return;
13404bc52338SCy Schubert 			}
13414bc52338SCy Schubert 		}
13424bc52338SCy Schubert #endif /* CONFIG_OCV */
1343780fb4a2SCy Schubert 	}
13445b9c547cSRui Paulo 
13455b9c547cSRui Paulo 	if (sta->plink_state == PLINK_BLOCKED) {
13465b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
13475b9c547cSRui Paulo 		return;
13485b9c547cSRui Paulo 	}
13495b9c547cSRui Paulo 
13505b9c547cSRui Paulo 	/* Now we will figure out the appropriate event... */
13515b9c547cSRui Paulo 	switch (action_field) {
13525b9c547cSRui Paulo 	case PLINK_OPEN:
13535b9c547cSRui Paulo 		if (plink_free_count(hapd) == 0) {
1354780fb4a2SCy Schubert 			event = REQ_RJCT;
1355780fb4a2SCy Schubert 			reason = WLAN_REASON_MESH_MAX_PEERS;
13565b9c547cSRui Paulo 			wpa_printf(MSG_INFO,
13575b9c547cSRui Paulo 				   "MPM: Peer link num over quota(%d)",
13585b9c547cSRui Paulo 				   hapd->max_plinks);
13595b9c547cSRui Paulo 		} else if (sta->peer_lid && sta->peer_lid != plid) {
1360780fb4a2SCy Schubert 			wpa_printf(MSG_DEBUG,
1361780fb4a2SCy Schubert 				   "MPM: peer_lid mismatch: 0x%x != 0x%x",
1362780fb4a2SCy Schubert 				   sta->peer_lid, plid);
1363780fb4a2SCy Schubert 			return; /* no FSM event */
13645b9c547cSRui Paulo 		} else {
13655b9c547cSRui Paulo 			sta->peer_lid = plid;
13665b9c547cSRui Paulo 			event = OPN_ACPT;
13675b9c547cSRui Paulo 		}
13685b9c547cSRui Paulo 		break;
13695b9c547cSRui Paulo 	case PLINK_CONFIRM:
13705b9c547cSRui Paulo 		if (plink_free_count(hapd) == 0) {
1371780fb4a2SCy Schubert 			event = REQ_RJCT;
1372780fb4a2SCy Schubert 			reason = WLAN_REASON_MESH_MAX_PEERS;
13735b9c547cSRui Paulo 			wpa_printf(MSG_INFO,
13745b9c547cSRui Paulo 				   "MPM: Peer link num over quota(%d)",
13755b9c547cSRui Paulo 				   hapd->max_plinks);
13765b9c547cSRui Paulo 		} else if (sta->my_lid != llid ||
13775b9c547cSRui Paulo 			   (sta->peer_lid && sta->peer_lid != plid)) {
1378780fb4a2SCy Schubert 			wpa_printf(MSG_DEBUG,
1379780fb4a2SCy Schubert 				   "MPM: lid mismatch: my_lid: 0x%x != 0x%x or peer_lid: 0x%x != 0x%x",
1380780fb4a2SCy Schubert 				   sta->my_lid, llid, sta->peer_lid, plid);
1381780fb4a2SCy Schubert 			return; /* no FSM event */
13825b9c547cSRui Paulo 		} else {
13835b9c547cSRui Paulo 			if (!sta->peer_lid)
13845b9c547cSRui Paulo 				sta->peer_lid = plid;
1385780fb4a2SCy Schubert 			sta->peer_aid = aid;
13865b9c547cSRui Paulo 			event = CNF_ACPT;
13875b9c547cSRui Paulo 		}
13885b9c547cSRui Paulo 		break;
13895b9c547cSRui Paulo 	case PLINK_CLOSE:
13905b9c547cSRui Paulo 		if (sta->plink_state == PLINK_ESTAB)
13915b9c547cSRui Paulo 			/* Do not check for llid or plid. This does not
13925b9c547cSRui Paulo 			 * follow the standard but since multiple plinks
13935b9c547cSRui Paulo 			 * per cand are not supported, it is necessary in
13945b9c547cSRui Paulo 			 * order to avoid a livelock when MP A sees an
13955b9c547cSRui Paulo 			 * establish peer link to MP B but MP B does not
13965b9c547cSRui Paulo 			 * see it. This can be caused by a timeout in
13975b9c547cSRui Paulo 			 * B's peer link establishment or B being
13985b9c547cSRui Paulo 			 * restarted.
13995b9c547cSRui Paulo 			 */
14005b9c547cSRui Paulo 			event = CLS_ACPT;
1401780fb4a2SCy Schubert 		else if (sta->peer_lid != plid) {
1402780fb4a2SCy Schubert 			wpa_printf(MSG_DEBUG,
1403780fb4a2SCy Schubert 				   "MPM: peer_lid mismatch: 0x%x != 0x%x",
1404780fb4a2SCy Schubert 				   sta->peer_lid, plid);
1405780fb4a2SCy Schubert 			return; /* no FSM event */
1406780fb4a2SCy Schubert 		} else if (peer_mgmt_ie.plid && sta->my_lid != llid) {
1407780fb4a2SCy Schubert 			wpa_printf(MSG_DEBUG,
1408780fb4a2SCy Schubert 				   "MPM: my_lid mismatch: 0x%x != 0x%x",
1409780fb4a2SCy Schubert 				   sta->my_lid, llid);
1410780fb4a2SCy Schubert 			return; /* no FSM event */
1411780fb4a2SCy Schubert 		} else {
14125b9c547cSRui Paulo 			event = CLS_ACPT;
1413780fb4a2SCy Schubert 		}
14145b9c547cSRui Paulo 		break;
14155b9c547cSRui Paulo 	default:
14165b9c547cSRui Paulo 		/*
14175b9c547cSRui Paulo 		 * This cannot be hit due to the action_field check above, but
14185b9c547cSRui Paulo 		 * compilers may not be able to figure that out and can warn
14195b9c547cSRui Paulo 		 * about uninitialized event below.
14205b9c547cSRui Paulo 		 */
14215b9c547cSRui Paulo 		return;
14225b9c547cSRui Paulo 	}
1423780fb4a2SCy Schubert 	mesh_mpm_fsm(wpa_s, sta, event, reason);
14245b9c547cSRui Paulo }
14255b9c547cSRui Paulo 
14265b9c547cSRui Paulo 
14275b9c547cSRui Paulo /* called by ap_free_sta */
mesh_mpm_free_sta(struct hostapd_data * hapd,struct sta_info * sta)1428780fb4a2SCy Schubert void mesh_mpm_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
14295b9c547cSRui Paulo {
1430*a90b9d01SCy Schubert 	struct wpa_supplicant *wpa_s = hapd->iface->owner;
1431*a90b9d01SCy Schubert 
1432*a90b9d01SCy Schubert 	if (sta->plink_state == PLINK_ESTAB) {
1433780fb4a2SCy Schubert 		hapd->num_plinks--;
1434*a90b9d01SCy Schubert 		wpas_notify_mesh_peer_disconnected(
1435*a90b9d01SCy Schubert 			wpa_s, sta->addr, WLAN_REASON_UNSPECIFIED);
1436*a90b9d01SCy Schubert 	}
14375b9c547cSRui Paulo 	eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
14385b9c547cSRui Paulo 	eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
14395b9c547cSRui Paulo }
1440