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