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 "ap/hostapd.h"
     15 #include "ap/sta_info.h"
     16 #include "ap/ieee802_11.h"
     17 #include "ap/wpa_auth.h"
     18 #include "wpa_supplicant_i.h"
     19 #include "driver_i.h"
     20 #include "mesh_mpm.h"
     21 #include "mesh_rsn.h"
     22 
     23 struct mesh_peer_mgmt_ie {
     24 	const u8 *proto_id; /* Mesh Peering Protocol Identifier (2 octets) */
     25 	const u8 *llid; /* Local Link ID (2 octets) */
     26 	const u8 *plid; /* Peer Link ID (conditional, 2 octets) */
     27 	const u8 *reason; /* Reason Code (conditional, 2 octets) */
     28 	const u8 *chosen_pmk; /* Chosen PMK (optional, 16 octets) */
     29 };
     30 
     31 static void plink_timer(void *eloop_ctx, void *user_data);
     32 
     33 
     34 enum plink_event {
     35 	PLINK_UNDEFINED,
     36 	OPN_ACPT,
     37 	OPN_RJCT,
     38 	OPN_IGNR,
     39 	CNF_ACPT,
     40 	CNF_RJCT,
     41 	CNF_IGNR,
     42 	CLS_ACPT,
     43 	CLS_IGNR
     44 };
     45 
     46 static const char * const mplstate[] = {
     47 	[0] = "UNINITIALIZED",
     48 	[PLINK_LISTEN] = "LISTEN",
     49 	[PLINK_OPEN_SENT] = "OPEN_SENT",
     50 	[PLINK_OPEN_RCVD] = "OPEN_RCVD",
     51 	[PLINK_CNF_RCVD] = "CNF_RCVD",
     52 	[PLINK_ESTAB] = "ESTAB",
     53 	[PLINK_HOLDING] = "HOLDING",
     54 	[PLINK_BLOCKED] = "BLOCKED"
     55 };
     56 
     57 static const char * const mplevent[] = {
     58 	[PLINK_UNDEFINED] = "UNDEFINED",
     59 	[OPN_ACPT] = "OPN_ACPT",
     60 	[OPN_RJCT] = "OPN_RJCT",
     61 	[OPN_IGNR] = "OPN_IGNR",
     62 	[CNF_ACPT] = "CNF_ACPT",
     63 	[CNF_RJCT] = "CNF_RJCT",
     64 	[CNF_IGNR] = "CNF_IGNR",
     65 	[CLS_ACPT] = "CLS_ACPT",
     66 	[CLS_IGNR] = "CLS_IGNR"
     67 };
     68 
     69 
     70 static int mesh_mpm_parse_peer_mgmt(struct wpa_supplicant *wpa_s,
     71 				    u8 action_field,
     72 				    const u8 *ie, size_t len,
     73 				    struct mesh_peer_mgmt_ie *mpm_ie)
     74 {
     75 	os_memset(mpm_ie, 0, sizeof(*mpm_ie));
     76 
     77 	/* Remove optional Chosen PMK field at end */
     78 	if (len >= SAE_PMKID_LEN) {
     79 		mpm_ie->chosen_pmk = ie + len - SAE_PMKID_LEN;
     80 		len -= SAE_PMKID_LEN;
     81 	}
     82 
     83 	if ((action_field == PLINK_OPEN && len != 4) ||
     84 	    (action_field == PLINK_CONFIRM && len != 6) ||
     85 	    (action_field == PLINK_CLOSE && len != 6 && len != 8)) {
     86 		wpa_msg(wpa_s, MSG_DEBUG, "MPM: Invalid peer mgmt ie");
     87 		return -1;
     88 	}
     89 
     90 	/* required fields */
     91 	if (len < 4)
     92 		return -1;
     93 	mpm_ie->proto_id = ie;
     94 	mpm_ie->llid = ie + 2;
     95 	ie += 4;
     96 	len -= 4;
     97 
     98 	/* close reason is always present at end for close */
     99 	if (action_field == PLINK_CLOSE) {
    100 		if (len < 2)
    101 			return -1;
    102 		mpm_ie->reason = ie + len - 2;
    103 		len -= 2;
    104 	}
    105 
    106 	/* Peer Link ID, present for confirm, and possibly close */
    107 	if (len >= 2)
    108 		mpm_ie->plid = ie;
    109 
    110 	return 0;
    111 }
    112 
    113 
    114 static int plink_free_count(struct hostapd_data *hapd)
    115 {
    116 	if (hapd->max_plinks > hapd->num_plinks)
    117 		return hapd->max_plinks - hapd->num_plinks;
    118 	return 0;
    119 }
    120 
    121 
    122 static u16 copy_supp_rates(struct wpa_supplicant *wpa_s,
    123 			   struct sta_info *sta,
    124 			   struct ieee802_11_elems *elems)
    125 {
    126 	if (!elems->supp_rates) {
    127 		wpa_msg(wpa_s, MSG_ERROR, "no supported rates from " MACSTR,
    128 			MAC2STR(sta->addr));
    129 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
    130 	}
    131 
    132 	if (elems->supp_rates_len + elems->ext_supp_rates_len >
    133 	    sizeof(sta->supported_rates)) {
    134 		wpa_msg(wpa_s, MSG_ERROR,
    135 			"Invalid supported rates element length " MACSTR
    136 			" %d+%d", MAC2STR(sta->addr), elems->supp_rates_len,
    137 			elems->ext_supp_rates_len);
    138 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
    139 	}
    140 
    141 	sta->supported_rates_len = merge_byte_arrays(
    142 		sta->supported_rates, sizeof(sta->supported_rates),
    143 		elems->supp_rates, elems->supp_rates_len,
    144 		elems->ext_supp_rates, elems->ext_supp_rates_len);
    145 
    146 	return WLAN_STATUS_SUCCESS;
    147 }
    148 
    149 
    150 /* return true if elems from a neighbor match this MBSS */
    151 static Boolean matches_local(struct wpa_supplicant *wpa_s,
    152 			     struct ieee802_11_elems *elems)
    153 {
    154 	struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
    155 
    156 	if (elems->mesh_config_len < 5)
    157 		return FALSE;
    158 
    159 	return (mconf->meshid_len == elems->mesh_id_len &&
    160 		os_memcmp(mconf->meshid, elems->mesh_id,
    161 			  elems->mesh_id_len) == 0 &&
    162 		mconf->mesh_pp_id == elems->mesh_config[0] &&
    163 		mconf->mesh_pm_id == elems->mesh_config[1] &&
    164 		mconf->mesh_cc_id == elems->mesh_config[2] &&
    165 		mconf->mesh_sp_id == elems->mesh_config[3] &&
    166 		mconf->mesh_auth_id == elems->mesh_config[4]);
    167 }
    168 
    169 
    170 /* check if local link id is already used with another peer */
    171 static Boolean llid_in_use(struct wpa_supplicant *wpa_s, u16 llid)
    172 {
    173 	struct sta_info *sta;
    174 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
    175 
    176 	for (sta = hapd->sta_list; sta; sta = sta->next) {
    177 		if (sta->my_lid == llid)
    178 			return TRUE;
    179 	}
    180 
    181 	return FALSE;
    182 }
    183 
    184 
    185 /* generate an llid for a link and set to initial state */
    186 static void mesh_mpm_init_link(struct wpa_supplicant *wpa_s,
    187 			       struct sta_info *sta)
    188 {
    189 	u16 llid;
    190 
    191 	do {
    192 		if (os_get_random((u8 *) &llid, sizeof(llid)) < 0)
    193 			continue;
    194 	} while (!llid || llid_in_use(wpa_s, llid));
    195 
    196 	sta->my_lid = llid;
    197 	sta->peer_lid = 0;
    198 
    199 	/*
    200 	 * We do not use wpa_mesh_set_plink_state() here because there is no
    201 	 * entry in kernel yet.
    202 	 */
    203 	sta->plink_state = PLINK_LISTEN;
    204 }
    205 
    206 
    207 static void mesh_mpm_send_plink_action(struct wpa_supplicant *wpa_s,
    208 				       struct sta_info *sta,
    209 				       enum plink_action_field type,
    210 				       u16 close_reason)
    211 {
    212 	struct wpabuf *buf;
    213 	struct hostapd_iface *ifmsh = wpa_s->ifmsh;
    214 	struct hostapd_data *bss = ifmsh->bss[0];
    215 	struct mesh_conf *conf = ifmsh->mconf;
    216 	u8 supp_rates[2 + 2 + 32];
    217 	u8 *pos, *cat;
    218 	u8 ie_len, add_plid = 0;
    219 	int ret;
    220 	int ampe = conf->security & MESH_CONF_SEC_AMPE;
    221 	size_t buf_len;
    222 
    223 	if (!sta)
    224 		return;
    225 
    226 	buf_len = 2 +      /* capability info */
    227 		  2 +      /* AID */
    228 		  2 + 8 +  /* supported rates */
    229 		  2 + (32 - 8) +
    230 		  2 + 32 + /* mesh ID */
    231 		  2 + 7 +  /* mesh config */
    232 		  2 + 23 + /* peering management */
    233 		  2 + 96 + /* AMPE */
    234 		  2 + 16;  /* MIC */
    235 #ifdef CONFIG_IEEE80211N
    236 	if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
    237 		buf_len += 2 + 26 + /* HT capabilities */
    238 			   2 + 22;  /* HT operation */
    239 	}
    240 #endif /* CONFIG_IEEE80211N */
    241 #ifdef CONFIG_IEEE80211AC
    242 	if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
    243 		buf_len += 2 + 12 + /* VHT Capabilities */
    244 			   2 + 5;  /* VHT Operation */
    245 	}
    246 #endif /* CONFIG_IEEE80211AC */
    247 	if (type != PLINK_CLOSE)
    248 		buf_len += conf->rsn_ie_len; /* RSN IE */
    249 
    250 	buf = wpabuf_alloc(buf_len);
    251 	if (!buf)
    252 		return;
    253 
    254 	cat = wpabuf_mhead_u8(buf);
    255 	wpabuf_put_u8(buf, WLAN_ACTION_SELF_PROTECTED);
    256 	wpabuf_put_u8(buf, type);
    257 
    258 	if (type != PLINK_CLOSE) {
    259 		u8 info;
    260 
    261 		/* capability info */
    262 		wpabuf_put_le16(buf, ampe ? IEEE80211_CAP_PRIVACY : 0);
    263 
    264 		/* aid */
    265 		if (type == PLINK_CONFIRM)
    266 			wpabuf_put_le16(buf, sta->aid);
    267 
    268 		/* IE: supp + ext. supp rates */
    269 		pos = hostapd_eid_supp_rates(bss, supp_rates);
    270 		pos = hostapd_eid_ext_supp_rates(bss, pos);
    271 		wpabuf_put_data(buf, supp_rates, pos - supp_rates);
    272 
    273 		/* IE: RSN IE */
    274 		wpabuf_put_data(buf, conf->rsn_ie, conf->rsn_ie_len);
    275 
    276 		/* IE: Mesh ID */
    277 		wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
    278 		wpabuf_put_u8(buf, conf->meshid_len);
    279 		wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
    280 
    281 		/* IE: mesh conf */
    282 		wpabuf_put_u8(buf, WLAN_EID_MESH_CONFIG);
    283 		wpabuf_put_u8(buf, 7);
    284 		wpabuf_put_u8(buf, conf->mesh_pp_id);
    285 		wpabuf_put_u8(buf, conf->mesh_pm_id);
    286 		wpabuf_put_u8(buf, conf->mesh_cc_id);
    287 		wpabuf_put_u8(buf, conf->mesh_sp_id);
    288 		wpabuf_put_u8(buf, conf->mesh_auth_id);
    289 		info = (bss->num_plinks > 63 ? 63 : bss->num_plinks) << 1;
    290 		/* TODO: Add Connected to Mesh Gate/AS subfields */
    291 		wpabuf_put_u8(buf, info);
    292 		/* always forwarding & accepting plinks for now */
    293 		wpabuf_put_u8(buf, 0x1 | 0x8);
    294 	} else {	/* Peer closing frame */
    295 		/* IE: Mesh ID */
    296 		wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
    297 		wpabuf_put_u8(buf, conf->meshid_len);
    298 		wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
    299 	}
    300 
    301 	/* IE: Mesh Peering Management element */
    302 	ie_len = 4;
    303 	if (ampe)
    304 		ie_len += PMKID_LEN;
    305 	switch (type) {
    306 	case PLINK_OPEN:
    307 		break;
    308 	case PLINK_CONFIRM:
    309 		ie_len += 2;
    310 		add_plid = 1;
    311 		break;
    312 	case PLINK_CLOSE:
    313 		ie_len += 2;
    314 		add_plid = 1;
    315 		ie_len += 2; /* reason code */
    316 		break;
    317 	}
    318 
    319 	wpabuf_put_u8(buf, WLAN_EID_PEER_MGMT);
    320 	wpabuf_put_u8(buf, ie_len);
    321 	/* peering protocol */
    322 	if (ampe)
    323 		wpabuf_put_le16(buf, 1);
    324 	else
    325 		wpabuf_put_le16(buf, 0);
    326 	wpabuf_put_le16(buf, sta->my_lid);
    327 	if (add_plid)
    328 		wpabuf_put_le16(buf, sta->peer_lid);
    329 	if (type == PLINK_CLOSE)
    330 		wpabuf_put_le16(buf, close_reason);
    331 	if (ampe) {
    332 		if (sta->sae == NULL) {
    333 			wpa_msg(wpa_s, MSG_INFO, "Mesh MPM: no SAE session");
    334 			goto fail;
    335 		}
    336 		mesh_rsn_get_pmkid(wpa_s->mesh_rsn, sta,
    337 				   wpabuf_put(buf, PMKID_LEN));
    338 	}
    339 
    340 #ifdef CONFIG_IEEE80211N
    341 	if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
    342 		u8 ht_capa_oper[2 + 26 + 2 + 22];
    343 
    344 		pos = hostapd_eid_ht_capabilities(bss, ht_capa_oper);
    345 		pos = hostapd_eid_ht_operation(bss, pos);
    346 		wpabuf_put_data(buf, ht_capa_oper, pos - ht_capa_oper);
    347 	}
    348 #endif /* CONFIG_IEEE80211N */
    349 #ifdef CONFIG_IEEE80211AC
    350 	if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
    351 		u8 vht_capa_oper[2 + 12 + 2 + 5];
    352 
    353 		pos = hostapd_eid_vht_capabilities(bss, vht_capa_oper);
    354 		pos = hostapd_eid_vht_operation(bss, pos);
    355 		wpabuf_put_data(buf, vht_capa_oper, pos - vht_capa_oper);
    356 	}
    357 #endif /* CONFIG_IEEE80211AC */
    358 
    359 	if (ampe && mesh_rsn_protect_frame(wpa_s->mesh_rsn, sta, cat, buf)) {
    360 		wpa_msg(wpa_s, MSG_INFO,
    361 			"Mesh MPM: failed to add AMPE and MIC IE");
    362 		goto fail;
    363 	}
    364 
    365 	wpa_msg(wpa_s, MSG_DEBUG, "Mesh MPM: Sending peering frame type %d to "
    366 		MACSTR " (my_lid=0x%x peer_lid=0x%x)",
    367 		type, MAC2STR(sta->addr), sta->my_lid, sta->peer_lid);
    368 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
    369 				  sta->addr, wpa_s->own_addr, wpa_s->own_addr,
    370 				  wpabuf_head(buf), wpabuf_len(buf), 0);
    371 	if (ret < 0)
    372 		wpa_msg(wpa_s, MSG_INFO,
    373 			"Mesh MPM: failed to send peering frame");
    374 
    375 fail:
    376 	wpabuf_free(buf);
    377 }
    378 
    379 
    380 /* configure peering state in ours and driver's station entry */
    381 void wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s,
    382 			      struct sta_info *sta,
    383 			      enum mesh_plink_state state)
    384 {
    385 	struct hostapd_sta_add_params params;
    386 	int ret;
    387 
    388 	wpa_msg(wpa_s, MSG_DEBUG, "MPM set " MACSTR " from %s into %s",
    389 		MAC2STR(sta->addr), mplstate[sta->plink_state],
    390 		mplstate[state]);
    391 	sta->plink_state = state;
    392 
    393 	os_memset(&params, 0, sizeof(params));
    394 	params.addr = sta->addr;
    395 	params.plink_state = state;
    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_OPEN_RCVD:
    427 	case PLINK_OPEN_SENT:
    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_OPEN_SENT <= 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_OPEN_SENT);
    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_OPEN_SENT);
    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 	int ret;
    651 
    652 	sta = ap_get_sta(data, addr);
    653 	if (!sta) {
    654 		sta = ap_sta_add(data, addr);
    655 		if (!sta)
    656 			return NULL;
    657 	}
    658 
    659 	/* Set WMM by default since Mesh STAs are QoS STAs */
    660 	sta->flags |= WLAN_STA_WMM;
    661 
    662 	/* initialize sta */
    663 	if (copy_supp_rates(wpa_s, sta, elems)) {
    664 		ap_free_sta(data, sta);
    665 		return NULL;
    666 	}
    667 
    668 	if (!sta->my_lid)
    669 		mesh_mpm_init_link(wpa_s, sta);
    670 
    671 #ifdef CONFIG_IEEE80211N
    672 	copy_sta_ht_capab(data, sta, elems->ht_capabilities);
    673 	update_ht_state(data, sta);
    674 #endif /* CONFIG_IEEE80211N */
    675 
    676 #ifdef CONFIG_IEEE80211AC
    677 	copy_sta_vht_capab(data, sta, elems->vht_capabilities);
    678 	set_sta_vht_opmode(data, sta, elems->vht_opmode_notif);
    679 #endif /* CONFIG_IEEE80211AC */
    680 
    681 	if (hostapd_get_aid(data, sta) < 0) {
    682 		wpa_msg(wpa_s, MSG_ERROR, "No AIDs available");
    683 		ap_free_sta(data, sta);
    684 		return NULL;
    685 	}
    686 
    687 	/* insert into driver */
    688 	os_memset(&params, 0, sizeof(params));
    689 	params.supp_rates = sta->supported_rates;
    690 	params.supp_rates_len = sta->supported_rates_len;
    691 	params.addr = addr;
    692 	params.plink_state = sta->plink_state;
    693 	params.aid = sta->aid;
    694 	params.listen_interval = 100;
    695 	params.ht_capabilities = sta->ht_capabilities;
    696 	params.vht_capabilities = sta->vht_capabilities;
    697 	params.flags |= WPA_STA_WMM;
    698 	params.flags_mask |= WPA_STA_AUTHENTICATED;
    699 	if (conf->security == MESH_CONF_SEC_NONE) {
    700 		params.flags |= WPA_STA_AUTHORIZED;
    701 		params.flags |= WPA_STA_AUTHENTICATED;
    702 	} else {
    703 		sta->flags |= WLAN_STA_MFP;
    704 		params.flags |= WPA_STA_MFP;
    705 	}
    706 
    707 	ret = wpa_drv_sta_add(wpa_s, &params);
    708 	if (ret) {
    709 		wpa_msg(wpa_s, MSG_ERROR,
    710 			"Driver failed to insert " MACSTR ": %d",
    711 			MAC2STR(addr), ret);
    712 		ap_free_sta(data, sta);
    713 		return NULL;
    714 	}
    715 
    716 	return sta;
    717 }
    718 
    719 
    720 void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
    721 			    struct ieee802_11_elems *elems)
    722 {
    723 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
    724 	struct hostapd_data *data = wpa_s->ifmsh->bss[0];
    725 	struct sta_info *sta;
    726 	struct wpa_ssid *ssid = wpa_s->current_ssid;
    727 
    728 	sta = mesh_mpm_add_peer(wpa_s, addr, elems);
    729 	if (!sta)
    730 		return;
    731 
    732 	if (ssid && ssid->no_auto_peer &&
    733 	    (is_zero_ether_addr(data->mesh_required_peer) ||
    734 	     os_memcmp(data->mesh_required_peer, addr, ETH_ALEN) != 0)) {
    735 		wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
    736 			MACSTR " because of no_auto_peer", MAC2STR(addr));
    737 		if (data->mesh_pending_auth) {
    738 			struct os_reltime age;
    739 			const struct ieee80211_mgmt *mgmt;
    740 			struct hostapd_frame_info fi;
    741 
    742 			mgmt = wpabuf_head(data->mesh_pending_auth);
    743 			os_reltime_age(&data->mesh_pending_auth_time, &age);
    744 			if (age.sec < 2 &&
    745 			    os_memcmp(mgmt->sa, addr, ETH_ALEN) == 0) {
    746 				wpa_printf(MSG_DEBUG,
    747 					   "mesh: Process pending Authentication frame from %u.%06u seconds ago",
    748 					   (unsigned int) age.sec,
    749 					   (unsigned int) age.usec);
    750 				os_memset(&fi, 0, sizeof(fi));
    751 				ieee802_11_mgmt(
    752 					data,
    753 					wpabuf_head(data->mesh_pending_auth),
    754 					wpabuf_len(data->mesh_pending_auth),
    755 					&fi);
    756 			}
    757 			wpabuf_free(data->mesh_pending_auth);
    758 			data->mesh_pending_auth = NULL;
    759 		}
    760 		return;
    761 	}
    762 
    763 	if (conf->security == MESH_CONF_SEC_NONE) {
    764 		if (sta->plink_state < PLINK_OPEN_SENT ||
    765 		    sta->plink_state > PLINK_ESTAB)
    766 			mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
    767 	} else {
    768 		mesh_rsn_auth_sae_sta(wpa_s, sta);
    769 	}
    770 }
    771 
    772 
    773 void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
    774 {
    775 	struct hostapd_frame_info fi;
    776 
    777 	os_memset(&fi, 0, sizeof(fi));
    778 	fi.datarate = rx_mgmt->datarate;
    779 	fi.ssi_signal = rx_mgmt->ssi_signal;
    780 	ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
    781 			rx_mgmt->frame_len, &fi);
    782 }
    783 
    784 
    785 static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
    786 				 struct sta_info *sta)
    787 {
    788 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
    789 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
    790 	u8 seq[6] = {};
    791 
    792 	wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
    793 		MAC2STR(sta->addr));
    794 
    795 	if (conf->security & MESH_CONF_SEC_AMPE) {
    796 		wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 0, 0,
    797 				seq, sizeof(seq), sta->mtk, sizeof(sta->mtk));
    798 		wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 1, 0,
    799 				seq, sizeof(seq),
    800 				sta->mgtk, sizeof(sta->mgtk));
    801 		wpa_drv_set_key(wpa_s, WPA_ALG_IGTK, sta->addr, 4, 0,
    802 				seq, sizeof(seq),
    803 				sta->mgtk, sizeof(sta->mgtk));
    804 
    805 		wpa_hexdump_key(MSG_DEBUG, "mtk:", sta->mtk, sizeof(sta->mtk));
    806 		wpa_hexdump_key(MSG_DEBUG, "mgtk:",
    807 				sta->mgtk, sizeof(sta->mgtk));
    808 	}
    809 
    810 	wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
    811 	hapd->num_plinks++;
    812 
    813 	sta->flags |= WLAN_STA_ASSOC;
    814 	sta->mesh_sae_pmksa_caching = 0;
    815 
    816 	eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
    817 	peer_add_timer(wpa_s, NULL);
    818 	eloop_cancel_timeout(plink_timer, wpa_s, sta);
    819 
    820 	/* Send ctrl event */
    821 	wpa_msg(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
    822 		MAC2STR(sta->addr));
    823 }
    824 
    825 
    826 static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
    827 			 enum plink_event event)
    828 {
    829 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
    830 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
    831 	u16 reason = 0;
    832 
    833 	wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
    834 		MAC2STR(sta->addr), mplstate[sta->plink_state],
    835 		mplevent[event]);
    836 
    837 	switch (sta->plink_state) {
    838 	case PLINK_LISTEN:
    839 		switch (event) {
    840 		case CLS_ACPT:
    841 			mesh_mpm_fsm_restart(wpa_s, sta);
    842 			break;
    843 		case OPN_ACPT:
    844 			mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
    845 			mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
    846 						   0);
    847 			break;
    848 		default:
    849 			break;
    850 		}
    851 		break;
    852 	case PLINK_OPEN_SENT:
    853 		switch (event) {
    854 		case OPN_RJCT:
    855 		case CNF_RJCT:
    856 			reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
    857 			/* fall-through */
    858 		case CLS_ACPT:
    859 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
    860 			if (!reason)
    861 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
    862 			eloop_register_timeout(
    863 				conf->dot11MeshHoldingTimeout / 1000,
    864 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
    865 				plink_timer, wpa_s, sta);
    866 			mesh_mpm_send_plink_action(wpa_s, sta,
    867 						   PLINK_CLOSE, reason);
    868 			break;
    869 		case OPN_ACPT:
    870 			/* retry timer is left untouched */
    871 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
    872 			mesh_mpm_send_plink_action(wpa_s, sta,
    873 						   PLINK_CONFIRM, 0);
    874 			break;
    875 		case CNF_ACPT:
    876 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
    877 			eloop_register_timeout(
    878 				conf->dot11MeshConfirmTimeout / 1000,
    879 				(conf->dot11MeshConfirmTimeout % 1000) * 1000,
    880 				plink_timer, wpa_s, sta);
    881 			break;
    882 		default:
    883 			break;
    884 		}
    885 		break;
    886 	case PLINK_OPEN_RCVD:
    887 		switch (event) {
    888 		case OPN_RJCT:
    889 		case CNF_RJCT:
    890 			reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
    891 			/* fall-through */
    892 		case CLS_ACPT:
    893 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
    894 			if (!reason)
    895 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
    896 			eloop_register_timeout(
    897 				conf->dot11MeshHoldingTimeout / 1000,
    898 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
    899 				plink_timer, wpa_s, sta);
    900 			sta->mpm_close_reason = reason;
    901 			mesh_mpm_send_plink_action(wpa_s, sta,
    902 						   PLINK_CLOSE, reason);
    903 			break;
    904 		case OPN_ACPT:
    905 			mesh_mpm_send_plink_action(wpa_s, sta,
    906 						   PLINK_CONFIRM, 0);
    907 			break;
    908 		case CNF_ACPT:
    909 			if (conf->security & MESH_CONF_SEC_AMPE)
    910 				mesh_rsn_derive_mtk(wpa_s, sta);
    911 			mesh_mpm_plink_estab(wpa_s, sta);
    912 			break;
    913 		default:
    914 			break;
    915 		}
    916 		break;
    917 	case PLINK_CNF_RCVD:
    918 		switch (event) {
    919 		case OPN_RJCT:
    920 		case CNF_RJCT:
    921 			reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
    922 			/* fall-through */
    923 		case CLS_ACPT:
    924 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
    925 			if (!reason)
    926 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
    927 			eloop_register_timeout(
    928 				conf->dot11MeshHoldingTimeout / 1000,
    929 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
    930 				plink_timer, wpa_s, sta);
    931 			sta->mpm_close_reason = reason;
    932 			mesh_mpm_send_plink_action(wpa_s, sta,
    933 						   PLINK_CLOSE, reason);
    934 			break;
    935 		case OPN_ACPT:
    936 			mesh_mpm_plink_estab(wpa_s, sta);
    937 			mesh_mpm_send_plink_action(wpa_s, sta,
    938 						   PLINK_CONFIRM, 0);
    939 			break;
    940 		default:
    941 			break;
    942 		}
    943 		break;
    944 	case PLINK_ESTAB:
    945 		switch (event) {
    946 		case CLS_ACPT:
    947 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
    948 			reason = WLAN_REASON_MESH_CLOSE_RCVD;
    949 
    950 			eloop_register_timeout(
    951 				conf->dot11MeshHoldingTimeout / 1000,
    952 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
    953 				plink_timer, wpa_s, sta);
    954 			sta->mpm_close_reason = reason;
    955 
    956 			wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
    957 				" closed with reason %d",
    958 				MAC2STR(sta->addr), reason);
    959 
    960 			wpa_msg(wpa_s, MSG_INFO, MESH_PEER_DISCONNECTED MACSTR,
    961 				MAC2STR(sta->addr));
    962 
    963 			hapd->num_plinks--;
    964 
    965 			mesh_mpm_send_plink_action(wpa_s, sta,
    966 						   PLINK_CLOSE, reason);
    967 			break;
    968 		case OPN_ACPT:
    969 			mesh_mpm_send_plink_action(wpa_s, sta,
    970 						   PLINK_CONFIRM, 0);
    971 			break;
    972 		default:
    973 			break;
    974 		}
    975 		break;
    976 	case PLINK_HOLDING:
    977 		switch (event) {
    978 		case CLS_ACPT:
    979 			mesh_mpm_fsm_restart(wpa_s, sta);
    980 			break;
    981 		case OPN_ACPT:
    982 		case CNF_ACPT:
    983 		case OPN_RJCT:
    984 		case CNF_RJCT:
    985 			reason = sta->mpm_close_reason;
    986 			mesh_mpm_send_plink_action(wpa_s, sta,
    987 						   PLINK_CLOSE, reason);
    988 			break;
    989 		default:
    990 			break;
    991 		}
    992 		break;
    993 	default:
    994 		wpa_msg(wpa_s, MSG_DEBUG,
    995 			"Unsupported MPM event %s for state %s",
    996 			mplevent[event], mplstate[sta->plink_state]);
    997 		break;
    998 	}
    999 }
   1000 
   1001 
   1002 void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
   1003 			const struct ieee80211_mgmt *mgmt, size_t len)
   1004 {
   1005 	u8 action_field;
   1006 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
   1007 	struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
   1008 	struct sta_info *sta;
   1009 	u16 plid = 0, llid = 0;
   1010 	enum plink_event event;
   1011 	struct ieee802_11_elems elems;
   1012 	struct mesh_peer_mgmt_ie peer_mgmt_ie;
   1013 	const u8 *ies;
   1014 	size_t ie_len;
   1015 	int ret;
   1016 
   1017 	if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
   1018 		return;
   1019 
   1020 	action_field = mgmt->u.action.u.slf_prot_action.action;
   1021 	if (action_field != PLINK_OPEN &&
   1022 	    action_field != PLINK_CONFIRM &&
   1023 	    action_field != PLINK_CLOSE)
   1024 		return;
   1025 
   1026 	ies = mgmt->u.action.u.slf_prot_action.variable;
   1027 	ie_len = (const u8 *) mgmt + len -
   1028 		mgmt->u.action.u.slf_prot_action.variable;
   1029 
   1030 	/* at least expect mesh id and peering mgmt */
   1031 	if (ie_len < 2 + 2) {
   1032 		wpa_printf(MSG_DEBUG,
   1033 			   "MPM: Ignore too short action frame %u ie_len %u",
   1034 			   action_field, (unsigned int) ie_len);
   1035 		return;
   1036 	}
   1037 	wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
   1038 
   1039 	if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
   1040 		wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
   1041 			   WPA_GET_LE16(ies));
   1042 		ies += 2;	/* capability */
   1043 		ie_len -= 2;
   1044 	}
   1045 	if (action_field == PLINK_CONFIRM) {
   1046 		wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", WPA_GET_LE16(ies));
   1047 		ies += 2;	/* aid */
   1048 		ie_len -= 2;
   1049 	}
   1050 
   1051 	/* check for mesh peering, mesh id and mesh config IEs */
   1052 	if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
   1053 		wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
   1054 		return;
   1055 	}
   1056 	if (!elems.peer_mgmt) {
   1057 		wpa_printf(MSG_DEBUG,
   1058 			   "MPM: No Mesh Peering Management element");
   1059 		return;
   1060 	}
   1061 	if (action_field != PLINK_CLOSE) {
   1062 		if (!elems.mesh_id || !elems.mesh_config) {
   1063 			wpa_printf(MSG_DEBUG,
   1064 				   "MPM: No Mesh ID or Mesh Configuration element");
   1065 			return;
   1066 		}
   1067 
   1068 		if (!matches_local(wpa_s, &elems)) {
   1069 			wpa_printf(MSG_DEBUG,
   1070 				   "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
   1071 			return;
   1072 		}
   1073 	}
   1074 
   1075 	ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
   1076 				       elems.peer_mgmt,
   1077 				       elems.peer_mgmt_len,
   1078 				       &peer_mgmt_ie);
   1079 	if (ret) {
   1080 		wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
   1081 		return;
   1082 	}
   1083 
   1084 	/* the sender's llid is our plid and vice-versa */
   1085 	plid = WPA_GET_LE16(peer_mgmt_ie.llid);
   1086 	if (peer_mgmt_ie.plid)
   1087 		llid = WPA_GET_LE16(peer_mgmt_ie.plid);
   1088 	wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
   1089 
   1090 	sta = ap_get_sta(hapd, mgmt->sa);
   1091 
   1092 	/*
   1093 	 * If this is an open frame from an unknown STA, and this is an
   1094 	 * open mesh, then go ahead and add the peer before proceeding.
   1095 	 */
   1096 	if (!sta && action_field == PLINK_OPEN &&
   1097 	    (!(mconf->security & MESH_CONF_SEC_AMPE) ||
   1098 	     wpa_auth_pmksa_get(hapd->wpa_auth, mgmt->sa)))
   1099 		sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
   1100 
   1101 	if (!sta) {
   1102 		wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
   1103 		return;
   1104 	}
   1105 
   1106 #ifdef CONFIG_SAE
   1107 	/* peer is in sae_accepted? */
   1108 	if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
   1109 		wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
   1110 		return;
   1111 	}
   1112 #endif /* CONFIG_SAE */
   1113 
   1114 	if (!sta->my_lid)
   1115 		mesh_mpm_init_link(wpa_s, sta);
   1116 
   1117 	if ((mconf->security & MESH_CONF_SEC_AMPE) &&
   1118 	    mesh_rsn_process_ampe(wpa_s, sta, &elems,
   1119 				  &mgmt->u.action.category,
   1120 				  peer_mgmt_ie.chosen_pmk,
   1121 				  ies, ie_len)) {
   1122 		wpa_printf(MSG_DEBUG, "MPM: RSN process rejected frame");
   1123 		return;
   1124 	}
   1125 
   1126 	if (sta->plink_state == PLINK_BLOCKED) {
   1127 		wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
   1128 		return;
   1129 	}
   1130 
   1131 	/* Now we will figure out the appropriate event... */
   1132 	switch (action_field) {
   1133 	case PLINK_OPEN:
   1134 		if (plink_free_count(hapd) == 0) {
   1135 			event = OPN_IGNR;
   1136 			wpa_printf(MSG_INFO,
   1137 				   "MPM: Peer link num over quota(%d)",
   1138 				   hapd->max_plinks);
   1139 		} else if (sta->peer_lid && sta->peer_lid != plid) {
   1140 			event = OPN_IGNR;
   1141 		} else {
   1142 			sta->peer_lid = plid;
   1143 			event = OPN_ACPT;
   1144 		}
   1145 		break;
   1146 	case PLINK_CONFIRM:
   1147 		if (plink_free_count(hapd) == 0) {
   1148 			event = CNF_IGNR;
   1149 			wpa_printf(MSG_INFO,
   1150 				   "MPM: Peer link num over quota(%d)",
   1151 				   hapd->max_plinks);
   1152 		} else if (sta->my_lid != llid ||
   1153 			   (sta->peer_lid && sta->peer_lid != plid)) {
   1154 			event = CNF_IGNR;
   1155 		} else {
   1156 			if (!sta->peer_lid)
   1157 				sta->peer_lid = plid;
   1158 			event = CNF_ACPT;
   1159 		}
   1160 		break;
   1161 	case PLINK_CLOSE:
   1162 		if (sta->plink_state == PLINK_ESTAB)
   1163 			/* Do not check for llid or plid. This does not
   1164 			 * follow the standard but since multiple plinks
   1165 			 * per cand are not supported, it is necessary in
   1166 			 * order to avoid a livelock when MP A sees an
   1167 			 * establish peer link to MP B but MP B does not
   1168 			 * see it. This can be caused by a timeout in
   1169 			 * B's peer link establishment or B being
   1170 			 * restarted.
   1171 			 */
   1172 			event = CLS_ACPT;
   1173 		else if (sta->peer_lid != plid)
   1174 			event = CLS_IGNR;
   1175 		else if (peer_mgmt_ie.plid && sta->my_lid != llid)
   1176 			event = CLS_IGNR;
   1177 		else
   1178 			event = CLS_ACPT;
   1179 		break;
   1180 	default:
   1181 		/*
   1182 		 * This cannot be hit due to the action_field check above, but
   1183 		 * compilers may not be able to figure that out and can warn
   1184 		 * about uninitialized event below.
   1185 		 */
   1186 		return;
   1187 	}
   1188 	mesh_mpm_fsm(wpa_s, sta, event);
   1189 }
   1190 
   1191 
   1192 /* called by ap_free_sta */
   1193 void mesh_mpm_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
   1194 {
   1195 	if (sta->plink_state == PLINK_ESTAB)
   1196 		hapd->num_plinks--;
   1197 	eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
   1198 	eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
   1199 }
   1200