Home | History | Annotate | Download | only in wpa_supplicant
      1 /*
      2  * WPA Supplicant - Basic mesh peer management
      3  * Copyright (c) 2013-2014, cozybit, Inc.  All rights reserved.
      4  *
      5  * This software may be distributed under the terms of the BSD license.
      6  * See README for more details.
      7  */
      8 
      9 #include "utils/includes.h"
     10 
     11 #include "utils/common.h"
     12 #include "utils/eloop.h"
     13 #include "common/ieee802_11_defs.h"
     14 #include "common/hw_features_common.h"
     15 #include "ap/hostapd.h"
     16 #include "ap/sta_info.h"
     17 #include "ap/ieee802_11.h"
     18 #include "ap/wpa_auth.h"
     19 #include "wpa_supplicant_i.h"
     20 #include "driver_i.h"
     21 #include "mesh_mpm.h"
     22 #include "mesh_rsn.h"
     23 
     24 struct mesh_peer_mgmt_ie {
     25 	const u8 *proto_id; /* Mesh Peering Protocol Identifier (2 octets) */
     26 	const u8 *llid; /* Local Link ID (2 octets) */
     27 	const u8 *plid; /* Peer Link ID (conditional, 2 octets) */
     28 	const u8 *reason; /* Reason Code (conditional, 2 octets) */
     29 	const u8 *chosen_pmk; /* Chosen PMK (optional, 16 octets) */
     30 };
     31 
     32 static void plink_timer(void *eloop_ctx, void *user_data);
     33 
     34 
     35 enum plink_event {
     36 	PLINK_UNDEFINED,
     37 	OPN_ACPT,
     38 	OPN_RJCT,
     39 	CNF_ACPT,
     40 	CNF_RJCT,
     41 	CLS_ACPT,
     42 	REQ_RJCT
     43 };
     44 
     45 static const char * const mplstate[] = {
     46 	[0] = "UNINITIALIZED",
     47 	[PLINK_IDLE] = "IDLE",
     48 	[PLINK_OPN_SNT] = "OPN_SNT",
     49 	[PLINK_OPN_RCVD] = "OPN_RCVD",
     50 	[PLINK_CNF_RCVD] = "CNF_RCVD",
     51 	[PLINK_ESTAB] = "ESTAB",
     52 	[PLINK_HOLDING] = "HOLDING",
     53 	[PLINK_BLOCKED] = "BLOCKED"
     54 };
     55 
     56 static const char * const mplevent[] = {
     57 	[PLINK_UNDEFINED] = "UNDEFINED",
     58 	[OPN_ACPT] = "OPN_ACPT",
     59 	[OPN_RJCT] = "OPN_RJCT",
     60 	[CNF_ACPT] = "CNF_ACPT",
     61 	[CNF_RJCT] = "CNF_RJCT",
     62 	[CLS_ACPT] = "CLS_ACPT",
     63 	[REQ_RJCT] = "REQ_RJCT",
     64 };
     65 
     66 
     67 static int mesh_mpm_parse_peer_mgmt(struct wpa_supplicant *wpa_s,
     68 				    u8 action_field,
     69 				    const u8 *ie, size_t len,
     70 				    struct mesh_peer_mgmt_ie *mpm_ie)
     71 {
     72 	os_memset(mpm_ie, 0, sizeof(*mpm_ie));
     73 
     74 	/* Remove optional Chosen PMK field at end */
     75 	if (len >= SAE_PMKID_LEN) {
     76 		mpm_ie->chosen_pmk = ie + len - SAE_PMKID_LEN;
     77 		len -= SAE_PMKID_LEN;
     78 	}
     79 
     80 	if ((action_field == PLINK_OPEN && len != 4) ||
     81 	    (action_field == PLINK_CONFIRM && len != 6) ||
     82 	    (action_field == PLINK_CLOSE && len != 6 && len != 8)) {
     83 		wpa_msg(wpa_s, MSG_DEBUG, "MPM: Invalid peer mgmt ie");
     84 		return -1;
     85 	}
     86 
     87 	/* required fields */
     88 	if (len < 4)
     89 		return -1;
     90 	mpm_ie->proto_id = ie;
     91 	mpm_ie->llid = ie + 2;
     92 	ie += 4;
     93 	len -= 4;
     94 
     95 	/* close reason is always present at end for close */
     96 	if (action_field == PLINK_CLOSE) {
     97 		if (len < 2)
     98 			return -1;
     99 		mpm_ie->reason = ie + len - 2;
    100 		len -= 2;
    101 	}
    102 
    103 	/* Peer Link ID, present for confirm, and possibly close */
    104 	if (len >= 2)
    105 		mpm_ie->plid = ie;
    106 
    107 	return 0;
    108 }
    109 
    110 
    111 static int plink_free_count(struct hostapd_data *hapd)
    112 {
    113 	if (hapd->max_plinks > hapd->num_plinks)
    114 		return hapd->max_plinks - hapd->num_plinks;
    115 	return 0;
    116 }
    117 
    118 
    119 static u16 copy_supp_rates(struct wpa_supplicant *wpa_s,
    120 			   struct sta_info *sta,
    121 			   struct ieee802_11_elems *elems)
    122 {
    123 	if (!elems->supp_rates) {
    124 		wpa_msg(wpa_s, MSG_ERROR, "no supported rates from " MACSTR,
    125 			MAC2STR(sta->addr));
    126 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
    127 	}
    128 
    129 	if (elems->supp_rates_len + elems->ext_supp_rates_len >
    130 	    sizeof(sta->supported_rates)) {
    131 		wpa_msg(wpa_s, MSG_ERROR,
    132 			"Invalid supported rates element length " MACSTR
    133 			" %d+%d", MAC2STR(sta->addr), elems->supp_rates_len,
    134 			elems->ext_supp_rates_len);
    135 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
    136 	}
    137 
    138 	sta->supported_rates_len = merge_byte_arrays(
    139 		sta->supported_rates, sizeof(sta->supported_rates),
    140 		elems->supp_rates, elems->supp_rates_len,
    141 		elems->ext_supp_rates, elems->ext_supp_rates_len);
    142 
    143 	return WLAN_STATUS_SUCCESS;
    144 }
    145 
    146 
    147 /* return true if elems from a neighbor match this MBSS */
    148 static Boolean matches_local(struct wpa_supplicant *wpa_s,
    149 			     struct ieee802_11_elems *elems)
    150 {
    151 	struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
    152 
    153 	if (elems->mesh_config_len < 5)
    154 		return FALSE;
    155 
    156 	return (mconf->meshid_len == elems->mesh_id_len &&
    157 		os_memcmp(mconf->meshid, elems->mesh_id,
    158 			  elems->mesh_id_len) == 0 &&
    159 		mconf->mesh_pp_id == elems->mesh_config[0] &&
    160 		mconf->mesh_pm_id == elems->mesh_config[1] &&
    161 		mconf->mesh_cc_id == elems->mesh_config[2] &&
    162 		mconf->mesh_sp_id == elems->mesh_config[3] &&
    163 		mconf->mesh_auth_id == elems->mesh_config[4]);
    164 }
    165 
    166 
    167 /* check if local link id is already used with another peer */
    168 static Boolean llid_in_use(struct wpa_supplicant *wpa_s, u16 llid)
    169 {
    170 	struct sta_info *sta;
    171 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
    172 
    173 	for (sta = hapd->sta_list; sta; sta = sta->next) {
    174 		if (sta->my_lid == llid)
    175 			return TRUE;
    176 	}
    177 
    178 	return FALSE;
    179 }
    180 
    181 
    182 /* generate an llid for a link and set to initial state */
    183 static void mesh_mpm_init_link(struct wpa_supplicant *wpa_s,
    184 			       struct sta_info *sta)
    185 {
    186 	u16 llid;
    187 
    188 	do {
    189 		if (os_get_random((u8 *) &llid, sizeof(llid)) < 0)
    190 			continue;
    191 	} while (!llid || llid_in_use(wpa_s, llid));
    192 
    193 	sta->my_lid = llid;
    194 	sta->peer_lid = 0;
    195 	sta->peer_aid = 0;
    196 
    197 	/*
    198 	 * We do not use wpa_mesh_set_plink_state() here because there is no
    199 	 * entry in kernel yet.
    200 	 */
    201 	sta->plink_state = PLINK_IDLE;
    202 }
    203 
    204 
    205 static void mesh_mpm_send_plink_action(struct wpa_supplicant *wpa_s,
    206 				       struct sta_info *sta,
    207 				       enum plink_action_field type,
    208 				       u16 close_reason)
    209 {
    210 	struct wpabuf *buf;
    211 	struct hostapd_iface *ifmsh = wpa_s->ifmsh;
    212 	struct hostapd_data *bss = ifmsh->bss[0];
    213 	struct mesh_conf *conf = ifmsh->mconf;
    214 	u8 supp_rates[2 + 2 + 32];
    215 	u8 *pos, *cat;
    216 	u8 ie_len, add_plid = 0;
    217 	int ret;
    218 	int ampe = conf->security & MESH_CONF_SEC_AMPE;
    219 	size_t buf_len;
    220 
    221 	if (!sta)
    222 		return;
    223 
    224 	buf_len = 2 +      /* capability info */
    225 		  2 +      /* AID */
    226 		  2 + 8 +  /* supported rates */
    227 		  2 + (32 - 8) +
    228 		  2 + 32 + /* mesh ID */
    229 		  2 + 7 +  /* mesh config */
    230 		  2 + 23 + /* peering management */
    231 		  2 + 96 + /* AMPE */
    232 		  2 + 16;  /* MIC */
    233 #ifdef CONFIG_IEEE80211N
    234 	if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
    235 		buf_len += 2 + 26 + /* HT capabilities */
    236 			   2 + 22;  /* HT operation */
    237 	}
    238 #endif /* CONFIG_IEEE80211N */
    239 #ifdef CONFIG_IEEE80211AC
    240 	if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
    241 		buf_len += 2 + 12 + /* VHT Capabilities */
    242 			   2 + 5;  /* VHT Operation */
    243 	}
    244 #endif /* CONFIG_IEEE80211AC */
    245 	if (type != PLINK_CLOSE)
    246 		buf_len += conf->rsn_ie_len; /* RSN IE */
    247 
    248 	buf = wpabuf_alloc(buf_len);
    249 	if (!buf)
    250 		return;
    251 
    252 	cat = wpabuf_mhead_u8(buf);
    253 	wpabuf_put_u8(buf, WLAN_ACTION_SELF_PROTECTED);
    254 	wpabuf_put_u8(buf, type);
    255 
    256 	if (type != PLINK_CLOSE) {
    257 		u8 info;
    258 
    259 		/* capability info */
    260 		wpabuf_put_le16(buf, ampe ? IEEE80211_CAP_PRIVACY : 0);
    261 
    262 		/* aid */
    263 		if (type == PLINK_CONFIRM)
    264 			wpabuf_put_le16(buf, sta->aid);
    265 
    266 		/* IE: supp + ext. supp rates */
    267 		pos = hostapd_eid_supp_rates(bss, supp_rates);
    268 		pos = hostapd_eid_ext_supp_rates(bss, pos);
    269 		wpabuf_put_data(buf, supp_rates, pos - supp_rates);
    270 
    271 		/* IE: RSN IE */
    272 		wpabuf_put_data(buf, conf->rsn_ie, conf->rsn_ie_len);
    273 
    274 		/* IE: Mesh ID */
    275 		wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
    276 		wpabuf_put_u8(buf, conf->meshid_len);
    277 		wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
    278 
    279 		/* IE: mesh conf */
    280 		wpabuf_put_u8(buf, WLAN_EID_MESH_CONFIG);
    281 		wpabuf_put_u8(buf, 7);
    282 		wpabuf_put_u8(buf, conf->mesh_pp_id);
    283 		wpabuf_put_u8(buf, conf->mesh_pm_id);
    284 		wpabuf_put_u8(buf, conf->mesh_cc_id);
    285 		wpabuf_put_u8(buf, conf->mesh_sp_id);
    286 		wpabuf_put_u8(buf, conf->mesh_auth_id);
    287 		info = (bss->num_plinks > 63 ? 63 : bss->num_plinks) << 1;
    288 		/* TODO: Add Connected to Mesh Gate/AS subfields */
    289 		wpabuf_put_u8(buf, info);
    290 		/* always forwarding & accepting plinks for now */
    291 		wpabuf_put_u8(buf, MESH_CAP_ACCEPT_ADDITIONAL_PEER |
    292 			      MESH_CAP_FORWARDING);
    293 	} else {	/* Peer closing frame */
    294 		/* IE: Mesh ID */
    295 		wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
    296 		wpabuf_put_u8(buf, conf->meshid_len);
    297 		wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
    298 	}
    299 
    300 	/* IE: Mesh Peering Management element */
    301 	ie_len = 4;
    302 	if (ampe)
    303 		ie_len += PMKID_LEN;
    304 	switch (type) {
    305 	case PLINK_OPEN:
    306 		break;
    307 	case PLINK_CONFIRM:
    308 		ie_len += 2;
    309 		add_plid = 1;
    310 		break;
    311 	case PLINK_CLOSE:
    312 		ie_len += 2;
    313 		add_plid = 1;
    314 		ie_len += 2; /* reason code */
    315 		break;
    316 	}
    317 
    318 	wpabuf_put_u8(buf, WLAN_EID_PEER_MGMT);
    319 	wpabuf_put_u8(buf, ie_len);
    320 	/* peering protocol */
    321 	if (ampe)
    322 		wpabuf_put_le16(buf, 1);
    323 	else
    324 		wpabuf_put_le16(buf, 0);
    325 	wpabuf_put_le16(buf, sta->my_lid);
    326 	if (add_plid)
    327 		wpabuf_put_le16(buf, sta->peer_lid);
    328 	if (type == PLINK_CLOSE)
    329 		wpabuf_put_le16(buf, close_reason);
    330 	if (ampe) {
    331 		if (sta->sae == NULL) {
    332 			wpa_msg(wpa_s, MSG_INFO, "Mesh MPM: no SAE session");
    333 			goto fail;
    334 		}
    335 		mesh_rsn_get_pmkid(wpa_s->mesh_rsn, sta,
    336 				   wpabuf_put(buf, PMKID_LEN));
    337 	}
    338 
    339 #ifdef CONFIG_IEEE80211N
    340 	if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
    341 		u8 ht_capa_oper[2 + 26 + 2 + 22];
    342 
    343 		pos = hostapd_eid_ht_capabilities(bss, ht_capa_oper);
    344 		pos = hostapd_eid_ht_operation(bss, pos);
    345 		wpabuf_put_data(buf, ht_capa_oper, pos - ht_capa_oper);
    346 	}
    347 #endif /* CONFIG_IEEE80211N */
    348 #ifdef CONFIG_IEEE80211AC
    349 	if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
    350 		u8 vht_capa_oper[2 + 12 + 2 + 5];
    351 
    352 		pos = hostapd_eid_vht_capabilities(bss, vht_capa_oper, 0);
    353 		pos = hostapd_eid_vht_operation(bss, pos);
    354 		wpabuf_put_data(buf, vht_capa_oper, pos - vht_capa_oper);
    355 	}
    356 #endif /* CONFIG_IEEE80211AC */
    357 
    358 	if (ampe && mesh_rsn_protect_frame(wpa_s->mesh_rsn, sta, cat, buf)) {
    359 		wpa_msg(wpa_s, MSG_INFO,
    360 			"Mesh MPM: failed to add AMPE and MIC IE");
    361 		goto fail;
    362 	}
    363 
    364 	wpa_msg(wpa_s, MSG_DEBUG, "Mesh MPM: Sending peering frame type %d to "
    365 		MACSTR " (my_lid=0x%x peer_lid=0x%x)",
    366 		type, MAC2STR(sta->addr), sta->my_lid, sta->peer_lid);
    367 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
    368 				  sta->addr, wpa_s->own_addr, wpa_s->own_addr,
    369 				  wpabuf_head(buf), wpabuf_len(buf), 0);
    370 	if (ret < 0)
    371 		wpa_msg(wpa_s, MSG_INFO,
    372 			"Mesh MPM: failed to send peering frame");
    373 
    374 fail:
    375 	wpabuf_free(buf);
    376 }
    377 
    378 
    379 /* configure peering state in ours and driver's station entry */
    380 void wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s,
    381 			      struct sta_info *sta,
    382 			      enum mesh_plink_state state)
    383 {
    384 	struct hostapd_sta_add_params params;
    385 	int ret;
    386 
    387 	wpa_msg(wpa_s, MSG_DEBUG, "MPM set " MACSTR " from %s into %s",
    388 		MAC2STR(sta->addr), mplstate[sta->plink_state],
    389 		mplstate[state]);
    390 	sta->plink_state = state;
    391 
    392 	os_memset(&params, 0, sizeof(params));
    393 	params.addr = sta->addr;
    394 	params.plink_state = state;
    395 	params.peer_aid = sta->peer_aid;
    396 	params.set = 1;
    397 
    398 	ret = wpa_drv_sta_add(wpa_s, &params);
    399 	if (ret) {
    400 		wpa_msg(wpa_s, MSG_ERROR, "Driver failed to set " MACSTR
    401 			": %d", MAC2STR(sta->addr), ret);
    402 	}
    403 }
    404 
    405 
    406 static void mesh_mpm_fsm_restart(struct wpa_supplicant *wpa_s,
    407 				 struct sta_info *sta)
    408 {
    409 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
    410 
    411 	eloop_cancel_timeout(plink_timer, wpa_s, sta);
    412 
    413 	ap_free_sta(hapd, sta);
    414 }
    415 
    416 
    417 static void plink_timer(void *eloop_ctx, void *user_data)
    418 {
    419 	struct wpa_supplicant *wpa_s = eloop_ctx;
    420 	struct sta_info *sta = user_data;
    421 	u16 reason = 0;
    422 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
    423 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
    424 
    425 	switch (sta->plink_state) {
    426 	case PLINK_OPN_RCVD:
    427 	case PLINK_OPN_SNT:
    428 		/* retry timer */
    429 		if (sta->mpm_retries < conf->dot11MeshMaxRetries) {
    430 			eloop_register_timeout(
    431 				conf->dot11MeshRetryTimeout / 1000,
    432 				(conf->dot11MeshRetryTimeout % 1000) * 1000,
    433 				plink_timer, wpa_s, sta);
    434 			mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
    435 			sta->mpm_retries++;
    436 			break;
    437 		}
    438 		reason = WLAN_REASON_MESH_MAX_RETRIES;
    439 		/* fall through on else */
    440 
    441 	case PLINK_CNF_RCVD:
    442 		/* confirm timer */
    443 		if (!reason)
    444 			reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
    445 		wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
    446 		eloop_register_timeout(conf->dot11MeshHoldingTimeout / 1000,
    447 			(conf->dot11MeshHoldingTimeout % 1000) * 1000,
    448 			plink_timer, wpa_s, sta);
    449 		mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
    450 		break;
    451 	case PLINK_HOLDING:
    452 		/* holding timer */
    453 
    454 		if (sta->mesh_sae_pmksa_caching) {
    455 			wpa_printf(MSG_DEBUG, "MPM: Peer " MACSTR
    456 				   " looks like it does not support mesh SAE PMKSA caching, so remove the cached entry for it",
    457 				   MAC2STR(sta->addr));
    458 			wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
    459 		}
    460 		mesh_mpm_fsm_restart(wpa_s, sta);
    461 		break;
    462 	default:
    463 		break;
    464 	}
    465 }
    466 
    467 
    468 /* initiate peering with station */
    469 static void
    470 mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
    471 		    enum mesh_plink_state next_state)
    472 {
    473 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
    474 
    475 	eloop_cancel_timeout(plink_timer, wpa_s, sta);
    476 	eloop_register_timeout(conf->dot11MeshRetryTimeout / 1000,
    477 			       (conf->dot11MeshRetryTimeout % 1000) * 1000,
    478 			       plink_timer, wpa_s, sta);
    479 	mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
    480 	wpa_mesh_set_plink_state(wpa_s, sta, next_state);
    481 }
    482 
    483 
    484 static int mesh_mpm_plink_close(struct hostapd_data *hapd, struct sta_info *sta,
    485 				void *ctx)
    486 {
    487 	struct wpa_supplicant *wpa_s = ctx;
    488 	int reason = WLAN_REASON_MESH_PEERING_CANCELLED;
    489 
    490 	if (sta) {
    491 		wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
    492 		mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
    493 		wpa_printf(MSG_DEBUG, "MPM closing plink sta=" MACSTR,
    494 			   MAC2STR(sta->addr));
    495 		eloop_cancel_timeout(plink_timer, wpa_s, sta);
    496 		return 0;
    497 	}
    498 
    499 	return 1;
    500 }
    501 
    502 
    503 int mesh_mpm_close_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
    504 {
    505 	struct hostapd_data *hapd;
    506 	struct sta_info *sta;
    507 
    508 	if (!wpa_s->ifmsh) {
    509 		wpa_msg(wpa_s, MSG_INFO, "Mesh is not prepared yet");
    510 		return -1;
    511 	}
    512 
    513 	hapd = wpa_s->ifmsh->bss[0];
    514 	sta = ap_get_sta(hapd, addr);
    515 	if (!sta) {
    516 		wpa_msg(wpa_s, MSG_INFO, "No such mesh peer");
    517 		return -1;
    518 	}
    519 
    520 	return mesh_mpm_plink_close(hapd, sta, wpa_s) == 0 ? 0 : -1;
    521 }
    522 
    523 
    524 static void peer_add_timer(void *eloop_ctx, void *user_data)
    525 {
    526 	struct wpa_supplicant *wpa_s = eloop_ctx;
    527 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
    528 
    529 	os_memset(hapd->mesh_required_peer, 0, ETH_ALEN);
    530 }
    531 
    532 
    533 int mesh_mpm_connect_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
    534 			  int duration)
    535 {
    536 	struct wpa_ssid *ssid = wpa_s->current_ssid;
    537 	struct hostapd_data *hapd;
    538 	struct sta_info *sta;
    539 	struct mesh_conf *conf;
    540 
    541 	if (!wpa_s->ifmsh) {
    542 		wpa_msg(wpa_s, MSG_INFO, "Mesh is not prepared yet");
    543 		return -1;
    544 	}
    545 
    546 	if (!ssid || !ssid->no_auto_peer) {
    547 		wpa_msg(wpa_s, MSG_INFO,
    548 			"This command is available only with no_auto_peer mesh network");
    549 		return -1;
    550 	}
    551 
    552 	hapd = wpa_s->ifmsh->bss[0];
    553 	conf = wpa_s->ifmsh->mconf;
    554 
    555 	sta = ap_get_sta(hapd, addr);
    556 	if (!sta) {
    557 		wpa_msg(wpa_s, MSG_INFO, "No such mesh peer");
    558 		return -1;
    559 	}
    560 
    561 	if ((PLINK_OPN_SNT <= sta->plink_state &&
    562 	    sta->plink_state <= PLINK_ESTAB) ||
    563 	    (sta->sae && sta->sae->state > SAE_NOTHING)) {
    564 		wpa_msg(wpa_s, MSG_INFO,
    565 			"Specified peer is connecting/connected");
    566 		return -1;
    567 	}
    568 
    569 	if (conf->security == MESH_CONF_SEC_NONE) {
    570 		mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_SNT);
    571 	} else {
    572 		mesh_rsn_auth_sae_sta(wpa_s, sta);
    573 		os_memcpy(hapd->mesh_required_peer, addr, ETH_ALEN);
    574 		eloop_register_timeout(duration == -1 ? 10 : duration, 0,
    575 				       peer_add_timer, wpa_s, NULL);
    576 	}
    577 
    578 	return 0;
    579 }
    580 
    581 
    582 void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
    583 {
    584 	struct hostapd_data *hapd = ifmsh->bss[0];
    585 
    586 	/* notify peers we're leaving */
    587 	ap_for_each_sta(hapd, mesh_mpm_plink_close, wpa_s);
    588 
    589 	hapd->num_plinks = 0;
    590 	hostapd_free_stas(hapd);
    591 	eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
    592 }
    593 
    594 
    595 /* for mesh_rsn to indicate this peer has completed authentication, and we're
    596  * ready to start AMPE */
    597 void mesh_mpm_auth_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
    598 {
    599 	struct hostapd_data *data = wpa_s->ifmsh->bss[0];
    600 	struct hostapd_sta_add_params params;
    601 	struct sta_info *sta;
    602 	int ret;
    603 
    604 	sta = ap_get_sta(data, addr);
    605 	if (!sta) {
    606 		wpa_msg(wpa_s, MSG_DEBUG, "no such mesh peer");
    607 		return;
    608 	}
    609 
    610 	/* TODO: Should do nothing if this STA is already authenticated, but
    611 	 * the AP code already sets this flag. */
    612 	sta->flags |= WLAN_STA_AUTH;
    613 
    614 	mesh_rsn_init_ampe_sta(wpa_s, sta);
    615 
    616 	os_memset(&params, 0, sizeof(params));
    617 	params.addr = sta->addr;
    618 	params.flags = WPA_STA_AUTHENTICATED | WPA_STA_AUTHORIZED;
    619 	params.set = 1;
    620 
    621 	wpa_msg(wpa_s, MSG_DEBUG, "MPM authenticating " MACSTR,
    622 		MAC2STR(sta->addr));
    623 	ret = wpa_drv_sta_add(wpa_s, &params);
    624 	if (ret) {
    625 		wpa_msg(wpa_s, MSG_ERROR,
    626 			"Driver failed to set " MACSTR ": %d",
    627 			MAC2STR(sta->addr), ret);
    628 	}
    629 
    630 	if (!sta->my_lid)
    631 		mesh_mpm_init_link(wpa_s, sta);
    632 
    633 	mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_SNT);
    634 }
    635 
    636 /*
    637  * Initialize a sta_info structure for a peer and upload it into the driver
    638  * in preparation for beginning authentication or peering. This is done when a
    639  * Beacon (secure or open mesh) or a peering open frame (for open mesh) is
    640  * received from the peer for the first time.
    641  */
    642 static struct sta_info * mesh_mpm_add_peer(struct wpa_supplicant *wpa_s,
    643 					   const u8 *addr,
    644 					   struct ieee802_11_elems *elems)
    645 {
    646 	struct hostapd_sta_add_params params;
    647 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
    648 	struct hostapd_data *data = wpa_s->ifmsh->bss[0];
    649 	struct sta_info *sta;
    650 #ifdef CONFIG_IEEE80211N
    651 	struct ieee80211_ht_operation *oper;
    652 #endif /* CONFIG_IEEE80211N */
    653 	int ret;
    654 
    655 	if (elems->mesh_config_len >= 7 &&
    656 	    !(elems->mesh_config[6] & MESH_CAP_ACCEPT_ADDITIONAL_PEER)) {
    657 		wpa_msg(wpa_s, MSG_DEBUG,
    658 			"mesh: Ignore a crowded peer " MACSTR,
    659 			MAC2STR(addr));
    660 		return NULL;
    661 	}
    662 
    663 	sta = ap_get_sta(data, addr);
    664 	if (!sta) {
    665 		sta = ap_sta_add(data, addr);
    666 		if (!sta)
    667 			return NULL;
    668 	}
    669 
    670 	/* Set WMM by default since Mesh STAs are QoS STAs */
    671 	sta->flags |= WLAN_STA_WMM;
    672 
    673 	/* initialize sta */
    674 	if (copy_supp_rates(wpa_s, sta, elems)) {
    675 		ap_free_sta(data, sta);
    676 		return NULL;
    677 	}
    678 
    679 	if (!sta->my_lid)
    680 		mesh_mpm_init_link(wpa_s, sta);
    681 
    682 #ifdef CONFIG_IEEE80211N
    683 	copy_sta_ht_capab(data, sta, elems->ht_capabilities);
    684 
    685 	oper = (struct ieee80211_ht_operation *) elems->ht_operation;
    686 	if (oper &&
    687 	    !(oper->ht_param & HT_INFO_HT_PARAM_STA_CHNL_WIDTH) &&
    688 	    sta->ht_capabilities) {
    689 		wpa_msg(wpa_s, MSG_DEBUG, MACSTR
    690 			" does not support 40 MHz bandwidth",
    691 			MAC2STR(sta->addr));
    692 		set_disable_ht40(sta->ht_capabilities, 1);
    693 	}
    694 
    695 	update_ht_state(data, sta);
    696 #endif /* CONFIG_IEEE80211N */
    697 
    698 #ifdef CONFIG_IEEE80211AC
    699 	copy_sta_vht_capab(data, sta, elems->vht_capabilities);
    700 	set_sta_vht_opmode(data, sta, elems->vht_opmode_notif);
    701 #endif /* CONFIG_IEEE80211AC */
    702 
    703 	if (hostapd_get_aid(data, sta) < 0) {
    704 		wpa_msg(wpa_s, MSG_ERROR, "No AIDs available");
    705 		ap_free_sta(data, sta);
    706 		return NULL;
    707 	}
    708 
    709 	/* insert into driver */
    710 	os_memset(&params, 0, sizeof(params));
    711 	params.supp_rates = sta->supported_rates;
    712 	params.supp_rates_len = sta->supported_rates_len;
    713 	params.addr = addr;
    714 	params.plink_state = sta->plink_state;
    715 	params.aid = sta->aid;
    716 	params.peer_aid = sta->peer_aid;
    717 	params.listen_interval = 100;
    718 	params.ht_capabilities = sta->ht_capabilities;
    719 	params.vht_capabilities = sta->vht_capabilities;
    720 	params.flags |= WPA_STA_WMM;
    721 	params.flags_mask |= WPA_STA_AUTHENTICATED;
    722 	if (conf->security == MESH_CONF_SEC_NONE) {
    723 		params.flags |= WPA_STA_AUTHORIZED;
    724 		params.flags |= WPA_STA_AUTHENTICATED;
    725 	} else {
    726 		sta->flags |= WLAN_STA_MFP;
    727 		params.flags |= WPA_STA_MFP;
    728 	}
    729 
    730 	ret = wpa_drv_sta_add(wpa_s, &params);
    731 	if (ret) {
    732 		wpa_msg(wpa_s, MSG_ERROR,
    733 			"Driver failed to insert " MACSTR ": %d",
    734 			MAC2STR(addr), ret);
    735 		ap_free_sta(data, sta);
    736 		return NULL;
    737 	}
    738 
    739 	return sta;
    740 }
    741 
    742 
    743 void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
    744 			    struct ieee802_11_elems *elems)
    745 {
    746 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
    747 	struct hostapd_data *data = wpa_s->ifmsh->bss[0];
    748 	struct sta_info *sta;
    749 	struct wpa_ssid *ssid = wpa_s->current_ssid;
    750 
    751 	sta = mesh_mpm_add_peer(wpa_s, addr, elems);
    752 	if (!sta)
    753 		return;
    754 
    755 	if (ssid && ssid->no_auto_peer &&
    756 	    (is_zero_ether_addr(data->mesh_required_peer) ||
    757 	     os_memcmp(data->mesh_required_peer, addr, ETH_ALEN) != 0)) {
    758 		wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
    759 			MACSTR " because of no_auto_peer", MAC2STR(addr));
    760 		if (data->mesh_pending_auth) {
    761 			struct os_reltime age;
    762 			const struct ieee80211_mgmt *mgmt;
    763 			struct hostapd_frame_info fi;
    764 
    765 			mgmt = wpabuf_head(data->mesh_pending_auth);
    766 			os_reltime_age(&data->mesh_pending_auth_time, &age);
    767 			if (age.sec < 2 &&
    768 			    os_memcmp(mgmt->sa, addr, ETH_ALEN) == 0) {
    769 				wpa_printf(MSG_DEBUG,
    770 					   "mesh: Process pending Authentication frame from %u.%06u seconds ago",
    771 					   (unsigned int) age.sec,
    772 					   (unsigned int) age.usec);
    773 				os_memset(&fi, 0, sizeof(fi));
    774 				ieee802_11_mgmt(
    775 					data,
    776 					wpabuf_head(data->mesh_pending_auth),
    777 					wpabuf_len(data->mesh_pending_auth),
    778 					&fi);
    779 			}
    780 			wpabuf_free(data->mesh_pending_auth);
    781 			data->mesh_pending_auth = NULL;
    782 		}
    783 		return;
    784 	}
    785 
    786 	if (conf->security == MESH_CONF_SEC_NONE) {
    787 		if (sta->plink_state < PLINK_OPN_SNT ||
    788 		    sta->plink_state > PLINK_ESTAB)
    789 			mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_SNT);
    790 	} else {
    791 		mesh_rsn_auth_sae_sta(wpa_s, sta);
    792 	}
    793 }
    794 
    795 
    796 void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
    797 {
    798 	struct hostapd_frame_info fi;
    799 
    800 	os_memset(&fi, 0, sizeof(fi));
    801 	fi.datarate = rx_mgmt->datarate;
    802 	fi.ssi_signal = rx_mgmt->ssi_signal;
    803 	ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
    804 			rx_mgmt->frame_len, &fi);
    805 }
    806 
    807 
    808 static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
    809 				 struct sta_info *sta)
    810 {
    811 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
    812 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
    813 	u8 seq[6] = {};
    814 
    815 	wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
    816 		MAC2STR(sta->addr));
    817 
    818 	if (conf->security & MESH_CONF_SEC_AMPE) {
    819 		wpa_hexdump_key(MSG_DEBUG, "mesh: MTK", sta->mtk, sta->mtk_len);
    820 		wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->pairwise_cipher),
    821 				sta->addr, 0, 0, seq, sizeof(seq),
    822 				sta->mtk, sta->mtk_len);
    823 
    824 		wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK Key RSC",
    825 				sta->mgtk_rsc, sizeof(sta->mgtk_rsc));
    826 		wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK",
    827 				sta->mgtk, sta->mgtk_len);
    828 		wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->group_cipher),
    829 				sta->addr, sta->mgtk_key_id, 0,
    830 				sta->mgtk_rsc, sizeof(sta->mgtk_rsc),
    831 				sta->mgtk, sta->mgtk_len);
    832 
    833 		if (sta->igtk_len) {
    834 			wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK Key RSC",
    835 					sta->igtk_rsc, sizeof(sta->igtk_rsc));
    836 			wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK",
    837 					sta->igtk, sta->igtk_len);
    838 			wpa_drv_set_key(
    839 				wpa_s,
    840 				wpa_cipher_to_alg(conf->mgmt_group_cipher),
    841 				sta->addr, sta->igtk_key_id, 0,
    842 				sta->igtk_rsc, sizeof(sta->igtk_rsc),
    843 				sta->igtk, sta->igtk_len);
    844 		}
    845 	}
    846 
    847 	wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
    848 	hapd->num_plinks++;
    849 
    850 	sta->flags |= WLAN_STA_ASSOC;
    851 	sta->mesh_sae_pmksa_caching = 0;
    852 
    853 	eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
    854 	peer_add_timer(wpa_s, NULL);
    855 	eloop_cancel_timeout(plink_timer, wpa_s, sta);
    856 
    857 	/* Send ctrl event */
    858 	wpa_msg(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
    859 		MAC2STR(sta->addr));
    860 }
    861 
    862 
    863 static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
    864 			 enum plink_event event, u16 reason)
    865 {
    866 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
    867 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
    868 
    869 	wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
    870 		MAC2STR(sta->addr), mplstate[sta->plink_state],
    871 		mplevent[event]);
    872 
    873 	switch (sta->plink_state) {
    874 	case PLINK_IDLE:
    875 		switch (event) {
    876 		case CLS_ACPT:
    877 			mesh_mpm_fsm_restart(wpa_s, sta);
    878 			break;
    879 		case OPN_ACPT:
    880 			mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_RCVD);
    881 			mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
    882 						   0);
    883 			break;
    884 		case REQ_RJCT:
    885 			mesh_mpm_send_plink_action(wpa_s, sta,
    886 						   PLINK_CLOSE, reason);
    887 			break;
    888 		default:
    889 			break;
    890 		}
    891 		break;
    892 	case PLINK_OPN_SNT:
    893 		switch (event) {
    894 		case OPN_RJCT:
    895 		case CNF_RJCT:
    896 			if (!reason)
    897 				reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
    898 			/* fall-through */
    899 		case CLS_ACPT:
    900 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
    901 			if (!reason)
    902 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
    903 			eloop_register_timeout(
    904 				conf->dot11MeshHoldingTimeout / 1000,
    905 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
    906 				plink_timer, wpa_s, sta);
    907 			mesh_mpm_send_plink_action(wpa_s, sta,
    908 						   PLINK_CLOSE, reason);
    909 			break;
    910 		case OPN_ACPT:
    911 			/* retry timer is left untouched */
    912 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPN_RCVD);
    913 			mesh_mpm_send_plink_action(wpa_s, sta,
    914 						   PLINK_CONFIRM, 0);
    915 			break;
    916 		case CNF_ACPT:
    917 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
    918 			eloop_cancel_timeout(plink_timer, wpa_s, sta);
    919 			eloop_register_timeout(
    920 				conf->dot11MeshConfirmTimeout / 1000,
    921 				(conf->dot11MeshConfirmTimeout % 1000) * 1000,
    922 				plink_timer, wpa_s, sta);
    923 			break;
    924 		default:
    925 			break;
    926 		}
    927 		break;
    928 	case PLINK_OPN_RCVD:
    929 		switch (event) {
    930 		case OPN_RJCT:
    931 		case CNF_RJCT:
    932 			if (!reason)
    933 				reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
    934 			/* fall-through */
    935 		case CLS_ACPT:
    936 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
    937 			if (!reason)
    938 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
    939 			eloop_register_timeout(
    940 				conf->dot11MeshHoldingTimeout / 1000,
    941 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
    942 				plink_timer, wpa_s, sta);
    943 			sta->mpm_close_reason = reason;
    944 			mesh_mpm_send_plink_action(wpa_s, sta,
    945 						   PLINK_CLOSE, reason);
    946 			break;
    947 		case OPN_ACPT:
    948 			mesh_mpm_send_plink_action(wpa_s, sta,
    949 						   PLINK_CONFIRM, 0);
    950 			break;
    951 		case CNF_ACPT:
    952 			if (conf->security & MESH_CONF_SEC_AMPE)
    953 				mesh_rsn_derive_mtk(wpa_s, sta);
    954 			mesh_mpm_plink_estab(wpa_s, sta);
    955 			break;
    956 		default:
    957 			break;
    958 		}
    959 		break;
    960 	case PLINK_CNF_RCVD:
    961 		switch (event) {
    962 		case OPN_RJCT:
    963 		case CNF_RJCT:
    964 			if (!reason)
    965 				reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
    966 			/* fall-through */
    967 		case CLS_ACPT:
    968 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
    969 			if (!reason)
    970 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
    971 			eloop_register_timeout(
    972 				conf->dot11MeshHoldingTimeout / 1000,
    973 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
    974 				plink_timer, wpa_s, sta);
    975 			sta->mpm_close_reason = reason;
    976 			mesh_mpm_send_plink_action(wpa_s, sta,
    977 						   PLINK_CLOSE, reason);
    978 			break;
    979 		case OPN_ACPT:
    980 			if (conf->security & MESH_CONF_SEC_AMPE)
    981 				mesh_rsn_derive_mtk(wpa_s, sta);
    982 			mesh_mpm_plink_estab(wpa_s, sta);
    983 			mesh_mpm_send_plink_action(wpa_s, sta,
    984 						   PLINK_CONFIRM, 0);
    985 			break;
    986 		default:
    987 			break;
    988 		}
    989 		break;
    990 	case PLINK_ESTAB:
    991 		switch (event) {
    992 		case OPN_RJCT:
    993 		case CNF_RJCT:
    994 		case CLS_ACPT:
    995 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
    996 			if (!reason)
    997 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
    998 
    999 			eloop_register_timeout(
   1000 				conf->dot11MeshHoldingTimeout / 1000,
   1001 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
   1002 				plink_timer, wpa_s, sta);
   1003 			sta->mpm_close_reason = reason;
   1004 
   1005 			wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
   1006 				" closed with reason %d",
   1007 				MAC2STR(sta->addr), reason);
   1008 
   1009 			wpa_msg(wpa_s, MSG_INFO, MESH_PEER_DISCONNECTED MACSTR,
   1010 				MAC2STR(sta->addr));
   1011 
   1012 			hapd->num_plinks--;
   1013 
   1014 			mesh_mpm_send_plink_action(wpa_s, sta,
   1015 						   PLINK_CLOSE, reason);
   1016 			break;
   1017 		case OPN_ACPT:
   1018 			mesh_mpm_send_plink_action(wpa_s, sta,
   1019 						   PLINK_CONFIRM, 0);
   1020 			break;
   1021 		default:
   1022 			break;
   1023 		}
   1024 		break;
   1025 	case PLINK_HOLDING:
   1026 		switch (event) {
   1027 		case CLS_ACPT:
   1028 			mesh_mpm_fsm_restart(wpa_s, sta);
   1029 			break;
   1030 		case OPN_ACPT:
   1031 		case CNF_ACPT:
   1032 		case OPN_RJCT:
   1033 		case CNF_RJCT:
   1034 			reason = sta->mpm_close_reason;
   1035 			mesh_mpm_send_plink_action(wpa_s, sta,
   1036 						   PLINK_CLOSE, reason);
   1037 			break;
   1038 		default:
   1039 			break;
   1040 		}
   1041 		break;
   1042 	default:
   1043 		wpa_msg(wpa_s, MSG_DEBUG,
   1044 			"Unsupported MPM event %s for state %s",
   1045 			mplevent[event], mplstate[sta->plink_state]);
   1046 		break;
   1047 	}
   1048 }
   1049 
   1050 
   1051 void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
   1052 			const struct ieee80211_mgmt *mgmt, size_t len)
   1053 {
   1054 	u8 action_field;
   1055 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
   1056 	struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
   1057 	struct sta_info *sta;
   1058 	u16 plid = 0, llid = 0, aid = 0;
   1059 	enum plink_event event;
   1060 	struct ieee802_11_elems elems;
   1061 	struct mesh_peer_mgmt_ie peer_mgmt_ie;
   1062 	const u8 *ies;
   1063 	size_t ie_len;
   1064 	int ret;
   1065 	u16 reason = 0;
   1066 
   1067 	if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
   1068 		return;
   1069 
   1070 	action_field = mgmt->u.action.u.slf_prot_action.action;
   1071 	if (action_field != PLINK_OPEN &&
   1072 	    action_field != PLINK_CONFIRM &&
   1073 	    action_field != PLINK_CLOSE)
   1074 		return;
   1075 
   1076 	ies = mgmt->u.action.u.slf_prot_action.variable;
   1077 	ie_len = (const u8 *) mgmt + len -
   1078 		mgmt->u.action.u.slf_prot_action.variable;
   1079 
   1080 	/* at least expect mesh id and peering mgmt */
   1081 	if (ie_len < 2 + 2) {
   1082 		wpa_printf(MSG_DEBUG,
   1083 			   "MPM: Ignore too short action frame %u ie_len %u",
   1084 			   action_field, (unsigned int) ie_len);
   1085 		return;
   1086 	}
   1087 	wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
   1088 
   1089 	if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
   1090 		wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
   1091 			   WPA_GET_LE16(ies));
   1092 		ies += 2;	/* capability */
   1093 		ie_len -= 2;
   1094 	}
   1095 	if (action_field == PLINK_CONFIRM) {
   1096 		aid = WPA_GET_LE16(ies);
   1097 		wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", aid);
   1098 		ies += 2;	/* aid */
   1099 		ie_len -= 2;
   1100 	}
   1101 
   1102 	/* check for mesh peering, mesh id and mesh config IEs */
   1103 	if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
   1104 		wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
   1105 		return;
   1106 	}
   1107 	if (!elems.peer_mgmt) {
   1108 		wpa_printf(MSG_DEBUG,
   1109 			   "MPM: No Mesh Peering Management element");
   1110 		return;
   1111 	}
   1112 	if (action_field != PLINK_CLOSE) {
   1113 		if (!elems.mesh_id || !elems.mesh_config) {
   1114 			wpa_printf(MSG_DEBUG,
   1115 				   "MPM: No Mesh ID or Mesh Configuration element");
   1116 			return;
   1117 		}
   1118 
   1119 		if (!matches_local(wpa_s, &elems)) {
   1120 			wpa_printf(MSG_DEBUG,
   1121 				   "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
   1122 			return;
   1123 		}
   1124 	}
   1125 
   1126 	ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
   1127 				       elems.peer_mgmt,
   1128 				       elems.peer_mgmt_len,
   1129 				       &peer_mgmt_ie);
   1130 	if (ret) {
   1131 		wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
   1132 		return;
   1133 	}
   1134 
   1135 	/* the sender's llid is our plid and vice-versa */
   1136 	plid = WPA_GET_LE16(peer_mgmt_ie.llid);
   1137 	if (peer_mgmt_ie.plid)
   1138 		llid = WPA_GET_LE16(peer_mgmt_ie.plid);
   1139 	wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
   1140 
   1141 	if (action_field == PLINK_CLOSE)
   1142 		wpa_printf(MSG_DEBUG, "MPM: close reason=%u",
   1143 			   WPA_GET_LE16(peer_mgmt_ie.reason));
   1144 
   1145 	sta = ap_get_sta(hapd, mgmt->sa);
   1146 
   1147 	/*
   1148 	 * If this is an open frame from an unknown STA, and this is an
   1149 	 * open mesh, then go ahead and add the peer before proceeding.
   1150 	 */
   1151 	if (!sta && action_field == PLINK_OPEN &&
   1152 	    (!(mconf->security & MESH_CONF_SEC_AMPE) ||
   1153 	     wpa_auth_pmksa_get(hapd->wpa_auth, mgmt->sa, NULL)))
   1154 		sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
   1155 
   1156 	if (!sta) {
   1157 		wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
   1158 		return;
   1159 	}
   1160 
   1161 #ifdef CONFIG_SAE
   1162 	/* peer is in sae_accepted? */
   1163 	if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
   1164 		wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
   1165 		return;
   1166 	}
   1167 #endif /* CONFIG_SAE */
   1168 
   1169 	if (!sta->my_lid)
   1170 		mesh_mpm_init_link(wpa_s, sta);
   1171 
   1172 	if (mconf->security & MESH_CONF_SEC_AMPE) {
   1173 		int res;
   1174 
   1175 		res = mesh_rsn_process_ampe(wpa_s, sta, &elems,
   1176 					    &mgmt->u.action.category,
   1177 					    peer_mgmt_ie.chosen_pmk,
   1178 					    ies, ie_len);
   1179 		if (res) {
   1180 			wpa_printf(MSG_DEBUG,
   1181 				   "MPM: RSN process rejected frame (res=%d)",
   1182 				   res);
   1183 			if (action_field == PLINK_OPEN && res == -2) {
   1184 				/* AES-SIV decryption failed */
   1185 				mesh_mpm_fsm(wpa_s, sta, OPN_RJCT,
   1186 					     WLAN_REASON_MESH_INVALID_GTK);
   1187 			}
   1188 			return;
   1189 		}
   1190 	}
   1191 
   1192 	if (sta->plink_state == PLINK_BLOCKED) {
   1193 		wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
   1194 		return;
   1195 	}
   1196 
   1197 	/* Now we will figure out the appropriate event... */
   1198 	switch (action_field) {
   1199 	case PLINK_OPEN:
   1200 		if (plink_free_count(hapd) == 0) {
   1201 			event = REQ_RJCT;
   1202 			reason = WLAN_REASON_MESH_MAX_PEERS;
   1203 			wpa_printf(MSG_INFO,
   1204 				   "MPM: Peer link num over quota(%d)",
   1205 				   hapd->max_plinks);
   1206 		} else if (sta->peer_lid && sta->peer_lid != plid) {
   1207 			wpa_printf(MSG_DEBUG,
   1208 				   "MPM: peer_lid mismatch: 0x%x != 0x%x",
   1209 				   sta->peer_lid, plid);
   1210 			return; /* no FSM event */
   1211 		} else {
   1212 			sta->peer_lid = plid;
   1213 			event = OPN_ACPT;
   1214 		}
   1215 		break;
   1216 	case PLINK_CONFIRM:
   1217 		if (plink_free_count(hapd) == 0) {
   1218 			event = REQ_RJCT;
   1219 			reason = WLAN_REASON_MESH_MAX_PEERS;
   1220 			wpa_printf(MSG_INFO,
   1221 				   "MPM: Peer link num over quota(%d)",
   1222 				   hapd->max_plinks);
   1223 		} else if (sta->my_lid != llid ||
   1224 			   (sta->peer_lid && sta->peer_lid != plid)) {
   1225 			wpa_printf(MSG_DEBUG,
   1226 				   "MPM: lid mismatch: my_lid: 0x%x != 0x%x or peer_lid: 0x%x != 0x%x",
   1227 				   sta->my_lid, llid, sta->peer_lid, plid);
   1228 			return; /* no FSM event */
   1229 		} else {
   1230 			if (!sta->peer_lid)
   1231 				sta->peer_lid = plid;
   1232 			sta->peer_aid = aid;
   1233 			event = CNF_ACPT;
   1234 		}
   1235 		break;
   1236 	case PLINK_CLOSE:
   1237 		if (sta->plink_state == PLINK_ESTAB)
   1238 			/* Do not check for llid or plid. This does not
   1239 			 * follow the standard but since multiple plinks
   1240 			 * per cand are not supported, it is necessary in
   1241 			 * order to avoid a livelock when MP A sees an
   1242 			 * establish peer link to MP B but MP B does not
   1243 			 * see it. This can be caused by a timeout in
   1244 			 * B's peer link establishment or B being
   1245 			 * restarted.
   1246 			 */
   1247 			event = CLS_ACPT;
   1248 		else if (sta->peer_lid != plid) {
   1249 			wpa_printf(MSG_DEBUG,
   1250 				   "MPM: peer_lid mismatch: 0x%x != 0x%x",
   1251 				   sta->peer_lid, plid);
   1252 			return; /* no FSM event */
   1253 		} else if (peer_mgmt_ie.plid && sta->my_lid != llid) {
   1254 			wpa_printf(MSG_DEBUG,
   1255 				   "MPM: my_lid mismatch: 0x%x != 0x%x",
   1256 				   sta->my_lid, llid);
   1257 			return; /* no FSM event */
   1258 		} else {
   1259 			event = CLS_ACPT;
   1260 		}
   1261 		break;
   1262 	default:
   1263 		/*
   1264 		 * This cannot be hit due to the action_field check above, but
   1265 		 * compilers may not be able to figure that out and can warn
   1266 		 * about uninitialized event below.
   1267 		 */
   1268 		return;
   1269 	}
   1270 	mesh_mpm_fsm(wpa_s, sta, event, reason);
   1271 }
   1272 
   1273 
   1274 /* called by ap_free_sta */
   1275 void mesh_mpm_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
   1276 {
   1277 	if (sta->plink_state == PLINK_ESTAB)
   1278 		hapd->num_plinks--;
   1279 	eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
   1280 	eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
   1281 }
   1282