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