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