• 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 		wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
537 		mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
538 		wpa_printf(MSG_DEBUG, "MPM closing plink sta=" MACSTR,
539 			   MAC2STR(sta->addr));
540 		eloop_cancel_timeout(plink_timer, wpa_s, sta);
541 		return 0;
542 	}
543 
544 	return 1;
545 }
546 
547 
mesh_mpm_close_peer(struct wpa_supplicant * wpa_s,const u8 * addr)548 int mesh_mpm_close_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
549 {
550 	struct hostapd_data *hapd;
551 	struct sta_info *sta;
552 
553 	if (!wpa_s->ifmsh) {
554 		wpa_msg(wpa_s, MSG_INFO, "Mesh is not prepared yet");
555 		return -1;
556 	}
557 
558 	hapd = wpa_s->ifmsh->bss[0];
559 	sta = ap_get_sta(hapd, addr);
560 	if (!sta) {
561 		wpa_msg(wpa_s, MSG_INFO, "No such mesh peer");
562 		return -1;
563 	}
564 
565 	return mesh_mpm_plink_close(hapd, sta, wpa_s) == 0 ? 0 : -1;
566 }
567 
568 
peer_add_timer(void * eloop_ctx,void * user_data)569 static void peer_add_timer(void *eloop_ctx, void *user_data)
570 {
571 	struct wpa_supplicant *wpa_s = eloop_ctx;
572 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
573 
574 	os_memset(hapd->mesh_required_peer, 0, ETH_ALEN);
575 }
576 
577 
mesh_mpm_connect_peer(struct wpa_supplicant * wpa_s,const u8 * addr,int duration)578 int mesh_mpm_connect_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
579 			  int duration)
580 {
581 	struct wpa_ssid *ssid = wpa_s->current_ssid;
582 	struct hostapd_data *hapd;
583 	struct sta_info *sta;
584 	struct mesh_conf *conf;
585 
586 	if (!wpa_s->ifmsh) {
587 		wpa_msg(wpa_s, MSG_INFO, "Mesh is not prepared yet");
588 		return -1;
589 	}
590 
591 	if (!ssid || !ssid->no_auto_peer) {
592 		wpa_msg(wpa_s, MSG_INFO,
593 			"This command is available only with no_auto_peer mesh network");
594 		return -1;
595 	}
596 
597 	hapd = wpa_s->ifmsh->bss[0];
598 	conf = wpa_s->ifmsh->mconf;
599 
600 	sta = ap_get_sta(hapd, addr);
601 	if (!sta) {
602 		wpa_msg(wpa_s, MSG_INFO, "No such mesh peer");
603 		return -1;
604 	}
605 
606 	if ((PLINK_OPN_SNT <= sta->plink_state &&
607 	    sta->plink_state <= PLINK_ESTAB) ||
608 	    (sta->sae && sta->sae->state > SAE_NOTHING)) {
609 		wpa_msg(wpa_s, MSG_INFO,
610 			"Specified peer is connecting/connected");
611 		return -1;
612 	}
613 
614 	if (conf->security == MESH_CONF_SEC_NONE) {
615 		mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_SNT);
616 	} else {
617 		mesh_rsn_auth_sae_sta(wpa_s, sta);
618 		os_memcpy(hapd->mesh_required_peer, addr, ETH_ALEN);
619 		eloop_register_timeout(duration == -1 ? 10 : duration, 0,
620 				       peer_add_timer, wpa_s, NULL);
621 	}
622 
623 	return 0;
624 }
625 
626 
mesh_mpm_deinit(struct wpa_supplicant * wpa_s,struct hostapd_iface * ifmsh)627 void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
628 {
629 	struct hostapd_data *hapd = ifmsh->bss[0];
630 
631 	/* notify peers we're leaving */
632 	ap_for_each_sta(hapd, mesh_mpm_plink_close, wpa_s);
633 
634 	hapd->num_plinks = 0;
635 	hostapd_free_stas(hapd);
636 	eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
637 }
638 
639 
640 /* for mesh_rsn to indicate this peer has completed authentication, and we're
641  * ready to start AMPE */
mesh_mpm_auth_peer(struct wpa_supplicant * wpa_s,const u8 * addr)642 void mesh_mpm_auth_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
643 {
644 	struct hostapd_data *data = wpa_s->ifmsh->bss[0];
645 	struct hostapd_sta_add_params params;
646 	struct sta_info *sta;
647 	int ret;
648 
649 	sta = ap_get_sta(data, addr);
650 	if (!sta) {
651 		wpa_msg(wpa_s, MSG_DEBUG, "no such mesh peer");
652 		return;
653 	}
654 
655 	/* TODO: Should do nothing if this STA is already authenticated, but
656 	 * the AP code already sets this flag. */
657 	sta->flags |= WLAN_STA_AUTH;
658 
659 	mesh_rsn_init_ampe_sta(wpa_s, sta);
660 
661 	os_memset(&params, 0, sizeof(params));
662 	params.addr = sta->addr;
663 	params.flags = WPA_STA_AUTHENTICATED | WPA_STA_AUTHORIZED;
664 	params.set = 1;
665 
666 	wpa_msg(wpa_s, MSG_DEBUG, "MPM authenticating " MACSTR,
667 		MAC2STR(sta->addr));
668 	ret = wpa_drv_sta_add(wpa_s, &params);
669 	if (ret) {
670 		wpa_msg(wpa_s, MSG_ERROR,
671 			"Driver failed to set " MACSTR ": %d",
672 			MAC2STR(sta->addr), ret);
673 	}
674 
675 	if (!sta->my_lid)
676 		mesh_mpm_init_link(wpa_s, sta);
677 
678 	mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_SNT);
679 }
680 
681 /*
682  * Initialize a sta_info structure for a peer and upload it into the driver
683  * in preparation for beginning authentication or peering. This is done when a
684  * Beacon (secure or open mesh) or a peering open frame (for open mesh) is
685  * received from the peer for the first time.
686  */
mesh_mpm_add_peer(struct wpa_supplicant * wpa_s,const u8 * addr,struct ieee802_11_elems * elems)687 static struct sta_info * mesh_mpm_add_peer(struct wpa_supplicant *wpa_s,
688 					   const u8 *addr,
689 					   struct ieee802_11_elems *elems)
690 {
691 	struct hostapd_sta_add_params params;
692 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
693 	struct hostapd_data *data = wpa_s->ifmsh->bss[0];
694 	struct sta_info *sta;
695 	struct ieee80211_ht_operation *oper;
696 	int ret;
697 
698 	if (elems->mesh_config_len >= 7 &&
699 	    !(elems->mesh_config[6] & MESH_CAP_ACCEPT_ADDITIONAL_PEER)) {
700 		wpa_msg(wpa_s, MSG_DEBUG,
701 			"mesh: Ignore a crowded peer " MACSTR,
702 			MAC2STR(addr));
703 		return NULL;
704 	}
705 
706 	sta = ap_get_sta(data, addr);
707 	if (sta)
708 		return NULL;
709 
710 	sta = ap_sta_add(data, addr);
711 	if (!sta)
712 		return NULL;
713 
714 	/* Set WMM by default since Mesh STAs are QoS STAs */
715 	sta->flags |= WLAN_STA_WMM;
716 
717 	/* initialize sta */
718 	if (copy_supp_rates(wpa_s, sta, elems)) {
719 		ap_free_sta(data, sta);
720 		return NULL;
721 	}
722 
723 	if (!sta->my_lid)
724 		mesh_mpm_init_link(wpa_s, sta);
725 
726 	copy_sta_ht_capab(data, sta, elems->ht_capabilities);
727 
728 	oper = (struct ieee80211_ht_operation *) elems->ht_operation;
729 	if (oper &&
730 	    !(oper->ht_param & HT_INFO_HT_PARAM_STA_CHNL_WIDTH) &&
731 	    sta->ht_capabilities) {
732 		wpa_msg(wpa_s, MSG_DEBUG, MACSTR
733 			" does not support 40 MHz bandwidth",
734 			MAC2STR(sta->addr));
735 		set_disable_ht40(sta->ht_capabilities, 1);
736 	}
737 
738 	update_ht_state(data, sta);
739 
740 #ifdef CONFIG_IEEE80211AC
741 	copy_sta_vht_capab(data, sta, elems->vht_capabilities);
742 	copy_sta_vht_oper(data, sta, elems->vht_operation);
743 	set_sta_vht_opmode(data, sta, elems->vht_opmode_notif);
744 #endif /* CONFIG_IEEE80211AC */
745 
746 #ifdef CONFIG_IEEE80211AX
747 	copy_sta_he_capab(data, sta, IEEE80211_MODE_MESH,
748 			  elems->he_capabilities, elems->he_capabilities_len);
749 #endif /* CONFIG_IEEE80211AX */
750 
751 	if (hostapd_get_aid(data, sta) < 0) {
752 		wpa_msg(wpa_s, MSG_ERROR, "No AIDs available");
753 		ap_free_sta(data, sta);
754 		return NULL;
755 	}
756 
757 	/* insert into driver */
758 	os_memset(&params, 0, sizeof(params));
759 	params.supp_rates = sta->supported_rates;
760 	params.supp_rates_len = sta->supported_rates_len;
761 	params.addr = addr;
762 	params.plink_state = sta->plink_state;
763 	params.aid = sta->aid;
764 	params.peer_aid = sta->peer_aid;
765 	params.listen_interval = 100;
766 	params.ht_capabilities = sta->ht_capabilities;
767 	params.vht_capabilities = sta->vht_capabilities;
768 	params.he_capab = sta->he_capab;
769 	params.he_capab_len = sta->he_capab_len;
770 	params.flags |= WPA_STA_WMM;
771 	params.flags_mask |= WPA_STA_AUTHENTICATED;
772 	if (conf->security == MESH_CONF_SEC_NONE) {
773 		params.flags |= WPA_STA_AUTHORIZED;
774 		params.flags |= WPA_STA_AUTHENTICATED;
775 	} else {
776 		sta->flags |= WLAN_STA_MFP;
777 		params.flags |= WPA_STA_MFP;
778 	}
779 
780 	ret = wpa_drv_sta_add(wpa_s, &params);
781 	if (ret) {
782 		wpa_msg(wpa_s, MSG_ERROR,
783 			"Driver failed to insert " MACSTR ": %d",
784 			MAC2STR(addr), ret);
785 		ap_free_sta(data, sta);
786 		return NULL;
787 	}
788 
789 	return sta;
790 }
791 
792 
wpa_mesh_new_mesh_peer(struct wpa_supplicant * wpa_s,const u8 * addr,struct ieee802_11_elems * elems)793 void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
794 			    struct ieee802_11_elems *elems)
795 {
796 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
797 	struct hostapd_data *data = wpa_s->ifmsh->bss[0];
798 	struct sta_info *sta;
799 	struct wpa_ssid *ssid = wpa_s->current_ssid;
800 
801 	sta = mesh_mpm_add_peer(wpa_s, addr, elems);
802 	if (!sta)
803 		return;
804 
805 	if (ssid && ssid->no_auto_peer &&
806 	    (is_zero_ether_addr(data->mesh_required_peer) ||
807 	     os_memcmp(data->mesh_required_peer, addr, ETH_ALEN) != 0)) {
808 		wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
809 			MACSTR " because of no_auto_peer", MAC2STR(addr));
810 		if (data->mesh_pending_auth) {
811 			struct os_reltime age;
812 			const struct ieee80211_mgmt *mgmt;
813 			struct hostapd_frame_info fi;
814 
815 			mgmt = wpabuf_head(data->mesh_pending_auth);
816 			os_reltime_age(&data->mesh_pending_auth_time, &age);
817 			if (age.sec < 2 &&
818 			    os_memcmp(mgmt->sa, addr, ETH_ALEN) == 0) {
819 				wpa_printf(MSG_DEBUG,
820 					   "mesh: Process pending Authentication frame from %u.%06u seconds ago",
821 					   (unsigned int) age.sec,
822 					   (unsigned int) age.usec);
823 				os_memset(&fi, 0, sizeof(fi));
824 				ieee802_11_mgmt(
825 					data,
826 					wpabuf_head(data->mesh_pending_auth),
827 					wpabuf_len(data->mesh_pending_auth),
828 					&fi);
829 			}
830 			wpabuf_free(data->mesh_pending_auth);
831 			data->mesh_pending_auth = NULL;
832 		}
833 		return;
834 	}
835 
836 	if (conf->security == MESH_CONF_SEC_NONE) {
837 		if (sta->plink_state < PLINK_OPN_SNT ||
838 		    sta->plink_state > PLINK_ESTAB)
839 			mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_SNT);
840 	} else {
841 		mesh_rsn_auth_sae_sta(wpa_s, sta);
842 	}
843 }
844 
845 
mesh_mpm_mgmt_rx(struct wpa_supplicant * wpa_s,struct rx_mgmt * rx_mgmt)846 void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
847 {
848 	struct hostapd_frame_info fi;
849 
850 	os_memset(&fi, 0, sizeof(fi));
851 	fi.datarate = rx_mgmt->datarate;
852 	fi.ssi_signal = rx_mgmt->ssi_signal;
853 	ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
854 			rx_mgmt->frame_len, &fi);
855 }
856 
857 
mesh_mpm_plink_estab(struct wpa_supplicant * wpa_s,struct sta_info * sta)858 static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
859 				 struct sta_info *sta)
860 {
861 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
862 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
863 	u8 seq[6] = {};
864 
865 	wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
866 		MAC2STR(sta->addr));
867 
868 	if (conf->security & MESH_CONF_SEC_AMPE) {
869 		wpa_hexdump_key(MSG_DEBUG, "mesh: MTK", sta->mtk, sta->mtk_len);
870 		wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->pairwise_cipher),
871 				sta->addr, 0, 0, seq, sizeof(seq),
872 				sta->mtk, sta->mtk_len,
873 				KEY_FLAG_PAIRWISE_RX_TX);
874 
875 		wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK Key RSC",
876 				sta->mgtk_rsc, sizeof(sta->mgtk_rsc));
877 		wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK",
878 				sta->mgtk, sta->mgtk_len);
879 		wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->group_cipher),
880 				sta->addr, sta->mgtk_key_id, 0,
881 				sta->mgtk_rsc, sizeof(sta->mgtk_rsc),
882 				sta->mgtk, sta->mgtk_len,
883 				KEY_FLAG_GROUP_RX);
884 
885 		if (sta->igtk_len) {
886 			wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK Key RSC",
887 					sta->igtk_rsc, sizeof(sta->igtk_rsc));
888 			wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK",
889 					sta->igtk, sta->igtk_len);
890 			wpa_drv_set_key(
891 				wpa_s,
892 				wpa_cipher_to_alg(conf->mgmt_group_cipher),
893 				sta->addr, sta->igtk_key_id, 0,
894 				sta->igtk_rsc, sizeof(sta->igtk_rsc),
895 				sta->igtk, sta->igtk_len,
896 				KEY_FLAG_GROUP_RX);
897 		}
898 	}
899 
900 	wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
901 	hapd->num_plinks++;
902 
903 	sta->flags |= WLAN_STA_ASSOC;
904 	sta->mesh_sae_pmksa_caching = 0;
905 
906 	eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
907 	peer_add_timer(wpa_s, NULL);
908 	eloop_cancel_timeout(plink_timer, wpa_s, sta);
909 
910 	/* Send ctrl event */
911 	wpa_msg(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
912 		MAC2STR(sta->addr));
913 
914 	/* Send D-Bus event */
915 	wpas_notify_mesh_peer_connected(wpa_s, sta->addr);
916 }
917 
918 
mesh_mpm_fsm(struct wpa_supplicant * wpa_s,struct sta_info * sta,enum plink_event event,u16 reason)919 static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
920 			 enum plink_event event, u16 reason)
921 {
922 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
923 	struct mesh_conf *conf = wpa_s->ifmsh->mconf;
924 
925 	wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
926 		MAC2STR(sta->addr), mplstate[sta->plink_state],
927 		mplevent[event]);
928 
929 	switch (sta->plink_state) {
930 	case PLINK_IDLE:
931 		switch (event) {
932 		case CLS_ACPT:
933 			mesh_mpm_fsm_restart(wpa_s, sta);
934 			break;
935 		case OPN_ACPT:
936 			mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_RCVD);
937 			mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
938 						   0);
939 			break;
940 		case REQ_RJCT:
941 			mesh_mpm_send_plink_action(wpa_s, sta,
942 						   PLINK_CLOSE, reason);
943 			break;
944 		default:
945 			break;
946 		}
947 		break;
948 	case PLINK_OPN_SNT:
949 		switch (event) {
950 		case OPN_RJCT:
951 		case CNF_RJCT:
952 			if (!reason)
953 				reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
954 			/* fall-through */
955 		case CLS_ACPT:
956 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
957 			if (!reason)
958 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
959 			eloop_register_timeout(
960 				conf->dot11MeshHoldingTimeout / 1000,
961 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
962 				plink_timer, wpa_s, sta);
963 			mesh_mpm_send_plink_action(wpa_s, sta,
964 						   PLINK_CLOSE, reason);
965 			break;
966 		case OPN_ACPT:
967 			/* retry timer is left untouched */
968 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPN_RCVD);
969 			mesh_mpm_send_plink_action(wpa_s, sta,
970 						   PLINK_CONFIRM, 0);
971 			break;
972 		case CNF_ACPT:
973 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
974 			eloop_cancel_timeout(plink_timer, wpa_s, sta);
975 			eloop_register_timeout(
976 				conf->dot11MeshConfirmTimeout / 1000,
977 				(conf->dot11MeshConfirmTimeout % 1000) * 1000,
978 				plink_timer, wpa_s, sta);
979 			break;
980 		default:
981 			break;
982 		}
983 		break;
984 	case PLINK_OPN_RCVD:
985 		switch (event) {
986 		case OPN_RJCT:
987 		case CNF_RJCT:
988 			if (!reason)
989 				reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
990 			/* fall-through */
991 		case CLS_ACPT:
992 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
993 			if (!reason)
994 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
995 			eloop_register_timeout(
996 				conf->dot11MeshHoldingTimeout / 1000,
997 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
998 				plink_timer, wpa_s, sta);
999 			sta->mpm_close_reason = reason;
1000 			mesh_mpm_send_plink_action(wpa_s, sta,
1001 						   PLINK_CLOSE, reason);
1002 			break;
1003 		case OPN_ACPT:
1004 			mesh_mpm_send_plink_action(wpa_s, sta,
1005 						   PLINK_CONFIRM, 0);
1006 			break;
1007 		case CNF_ACPT:
1008 			if (conf->security & MESH_CONF_SEC_AMPE)
1009 				mesh_rsn_derive_mtk(wpa_s, sta);
1010 			mesh_mpm_plink_estab(wpa_s, sta);
1011 			break;
1012 		default:
1013 			break;
1014 		}
1015 		break;
1016 	case PLINK_CNF_RCVD:
1017 		switch (event) {
1018 		case OPN_RJCT:
1019 		case CNF_RJCT:
1020 			if (!reason)
1021 				reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
1022 			/* fall-through */
1023 		case CLS_ACPT:
1024 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
1025 			if (!reason)
1026 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
1027 			eloop_register_timeout(
1028 				conf->dot11MeshHoldingTimeout / 1000,
1029 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
1030 				plink_timer, wpa_s, sta);
1031 			sta->mpm_close_reason = reason;
1032 			mesh_mpm_send_plink_action(wpa_s, sta,
1033 						   PLINK_CLOSE, reason);
1034 			break;
1035 		case OPN_ACPT:
1036 			if (conf->security & MESH_CONF_SEC_AMPE)
1037 				mesh_rsn_derive_mtk(wpa_s, sta);
1038 			mesh_mpm_plink_estab(wpa_s, sta);
1039 			mesh_mpm_send_plink_action(wpa_s, sta,
1040 						   PLINK_CONFIRM, 0);
1041 			break;
1042 		default:
1043 			break;
1044 		}
1045 		break;
1046 	case PLINK_ESTAB:
1047 		switch (event) {
1048 		case OPN_RJCT:
1049 		case CNF_RJCT:
1050 		case CLS_ACPT:
1051 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
1052 			if (!reason)
1053 				reason = WLAN_REASON_MESH_CLOSE_RCVD;
1054 
1055 			eloop_register_timeout(
1056 				conf->dot11MeshHoldingTimeout / 1000,
1057 				(conf->dot11MeshHoldingTimeout % 1000) * 1000,
1058 				plink_timer, wpa_s, sta);
1059 			sta->mpm_close_reason = reason;
1060 
1061 			wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
1062 				" closed with reason %d",
1063 				MAC2STR(sta->addr), reason);
1064 
1065 			wpa_msg(wpa_s, MSG_INFO, MESH_PEER_DISCONNECTED MACSTR,
1066 				MAC2STR(sta->addr));
1067 
1068 			/* Send D-Bus event */
1069 			wpas_notify_mesh_peer_disconnected(wpa_s, sta->addr,
1070 							   reason);
1071 
1072 			hapd->num_plinks--;
1073 
1074 			mesh_mpm_send_plink_action(wpa_s, sta,
1075 						   PLINK_CLOSE, reason);
1076 			break;
1077 		case OPN_ACPT:
1078 			mesh_mpm_send_plink_action(wpa_s, sta,
1079 						   PLINK_CONFIRM, 0);
1080 			break;
1081 		default:
1082 			break;
1083 		}
1084 		break;
1085 	case PLINK_HOLDING:
1086 		switch (event) {
1087 		case CLS_ACPT:
1088 			mesh_mpm_fsm_restart(wpa_s, sta);
1089 			break;
1090 		case OPN_ACPT:
1091 		case CNF_ACPT:
1092 		case OPN_RJCT:
1093 		case CNF_RJCT:
1094 			reason = sta->mpm_close_reason;
1095 			mesh_mpm_send_plink_action(wpa_s, sta,
1096 						   PLINK_CLOSE, reason);
1097 			break;
1098 		default:
1099 			break;
1100 		}
1101 		break;
1102 	default:
1103 		wpa_msg(wpa_s, MSG_DEBUG,
1104 			"Unsupported MPM event %s for state %s",
1105 			mplevent[event], mplstate[sta->plink_state]);
1106 		break;
1107 	}
1108 }
1109 
1110 
mesh_mpm_action_rx(struct wpa_supplicant * wpa_s,const struct ieee80211_mgmt * mgmt,size_t len)1111 void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
1112 			const struct ieee80211_mgmt *mgmt, size_t len)
1113 {
1114 	u8 action_field;
1115 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
1116 	struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
1117 	struct sta_info *sta;
1118 	u16 plid = 0, llid = 0, aid = 0;
1119 	enum plink_event event;
1120 	struct ieee802_11_elems elems;
1121 	struct mesh_peer_mgmt_ie peer_mgmt_ie;
1122 	const u8 *ies;
1123 	size_t ie_len;
1124 	int ret;
1125 	u16 reason = 0;
1126 
1127 	if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
1128 		return;
1129 
1130 	action_field = mgmt->u.action.u.slf_prot_action.action;
1131 	if (action_field != PLINK_OPEN &&
1132 	    action_field != PLINK_CONFIRM &&
1133 	    action_field != PLINK_CLOSE)
1134 		return;
1135 
1136 	ies = mgmt->u.action.u.slf_prot_action.variable;
1137 	ie_len = (const u8 *) mgmt + len -
1138 		mgmt->u.action.u.slf_prot_action.variable;
1139 
1140 	/* at least expect mesh id and peering mgmt */
1141 	if (ie_len < 2 + 2) {
1142 		wpa_printf(MSG_DEBUG,
1143 			   "MPM: Ignore too short action frame %u ie_len %u",
1144 			   action_field, (unsigned int) ie_len);
1145 		return;
1146 	}
1147 	wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
1148 
1149 	if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
1150 		wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
1151 			   WPA_GET_LE16(ies));
1152 		ies += 2;	/* capability */
1153 		ie_len -= 2;
1154 	}
1155 	if (action_field == PLINK_CONFIRM) {
1156 		aid = WPA_GET_LE16(ies);
1157 		wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", aid);
1158 		ies += 2;	/* aid */
1159 		ie_len -= 2;
1160 	}
1161 
1162 	/* check for mesh peering, mesh id and mesh config IEs */
1163 	if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
1164 		wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
1165 		return;
1166 	}
1167 	if (!elems.peer_mgmt) {
1168 		wpa_printf(MSG_DEBUG,
1169 			   "MPM: No Mesh Peering Management element");
1170 		return;
1171 	}
1172 	if (action_field != PLINK_CLOSE) {
1173 		if (!elems.mesh_id || !elems.mesh_config) {
1174 			wpa_printf(MSG_DEBUG,
1175 				   "MPM: No Mesh ID or Mesh Configuration element");
1176 			return;
1177 		}
1178 
1179 		if (!matches_local(wpa_s, &elems)) {
1180 			wpa_printf(MSG_DEBUG,
1181 				   "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
1182 			return;
1183 		}
1184 	}
1185 
1186 	ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
1187 				       elems.peer_mgmt,
1188 				       elems.peer_mgmt_len,
1189 				       &peer_mgmt_ie);
1190 	if (ret) {
1191 		wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
1192 		return;
1193 	}
1194 
1195 	/* the sender's llid is our plid and vice-versa */
1196 	plid = WPA_GET_LE16(peer_mgmt_ie.llid);
1197 	if (peer_mgmt_ie.plid)
1198 		llid = WPA_GET_LE16(peer_mgmt_ie.plid);
1199 	wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
1200 
1201 	if (action_field == PLINK_CLOSE)
1202 		wpa_printf(MSG_DEBUG, "MPM: close reason=%u",
1203 			   WPA_GET_LE16(peer_mgmt_ie.reason));
1204 
1205 	sta = ap_get_sta(hapd, mgmt->sa);
1206 
1207 	/*
1208 	 * If this is an open frame from an unknown STA, and this is an
1209 	 * open mesh, then go ahead and add the peer before proceeding.
1210 	 */
1211 	if (!sta && action_field == PLINK_OPEN &&
1212 	    (!(mconf->security & MESH_CONF_SEC_AMPE) ||
1213 	     wpa_auth_pmksa_get(hapd->wpa_auth, mgmt->sa, NULL)))
1214 		sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
1215 
1216 	if (!sta) {
1217 		wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
1218 		return;
1219 	}
1220 
1221 #ifdef CONFIG_SAE
1222 	/* peer is in sae_accepted? */
1223 	if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
1224 		wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
1225 		return;
1226 	}
1227 #endif /* CONFIG_SAE */
1228 
1229 	if (!sta->my_lid)
1230 		mesh_mpm_init_link(wpa_s, sta);
1231 
1232 	if (mconf->security & MESH_CONF_SEC_AMPE) {
1233 		int res;
1234 
1235 		res = mesh_rsn_process_ampe(wpa_s, sta, &elems,
1236 					    &mgmt->u.action.category,
1237 					    peer_mgmt_ie.chosen_pmk,
1238 					    ies, ie_len);
1239 		if (res) {
1240 			wpa_printf(MSG_DEBUG,
1241 				   "MPM: RSN process rejected frame (res=%d)",
1242 				   res);
1243 			if (action_field == PLINK_OPEN && res == -2) {
1244 				/* AES-SIV decryption failed */
1245 				mesh_mpm_fsm(wpa_s, sta, OPN_RJCT,
1246 					     WLAN_REASON_MESH_INVALID_GTK);
1247 			}
1248 			return;
1249 		}
1250 
1251 #ifdef CONFIG_OCV
1252 		if (action_field == PLINK_OPEN && elems.rsn_ie) {
1253 			struct wpa_state_machine *sm = sta->wpa_sm;
1254 			struct wpa_ie_data data;
1255 
1256 			res = wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2,
1257 						   elems.rsn_ie_len + 2,
1258 						   &data);
1259 			if (res) {
1260 				wpa_printf(MSG_DEBUG,
1261 					   "Failed to parse RSN IE (res=%d)",
1262 					   res);
1263 				wpa_hexdump(MSG_DEBUG, "RSN IE", elems.rsn_ie,
1264 					    elems.rsn_ie_len);
1265 				return;
1266 			}
1267 
1268 			wpa_auth_set_ocv(sm, mconf->ocv &&
1269 					 (data.capabilities &
1270 					  WPA_CAPABILITY_OCVC));
1271 		}
1272 
1273 		if (action_field != PLINK_CLOSE &&
1274 		    wpa_auth_uses_ocv(sta->wpa_sm)) {
1275 			struct wpa_channel_info ci;
1276 			int tx_chanwidth;
1277 			int tx_seg1_idx;
1278 
1279 			if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
1280 				wpa_printf(MSG_WARNING,
1281 					   "MPM: Failed to get channel info to validate received OCI in MPM Confirm");
1282 				return;
1283 			}
1284 
1285 			if (get_tx_parameters(
1286 				    sta, channel_width_to_int(ci.chanwidth),
1287 				    ci.seg1_idx, &tx_chanwidth,
1288 				    &tx_seg1_idx) < 0)
1289 				return;
1290 
1291 			if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
1292 						 tx_chanwidth, tx_seg1_idx) !=
1293 			    0) {
1294 				wpa_printf(MSG_WARNING, "MPM: %s",
1295 					   ocv_errorstr);
1296 				return;
1297 			}
1298 		}
1299 #endif /* CONFIG_OCV */
1300 	}
1301 
1302 	if (sta->plink_state == PLINK_BLOCKED) {
1303 		wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
1304 		return;
1305 	}
1306 
1307 	/* Now we will figure out the appropriate event... */
1308 	switch (action_field) {
1309 	case PLINK_OPEN:
1310 		if (plink_free_count(hapd) == 0) {
1311 			event = REQ_RJCT;
1312 			reason = WLAN_REASON_MESH_MAX_PEERS;
1313 			wpa_printf(MSG_INFO,
1314 				   "MPM: Peer link num over quota(%d)",
1315 				   hapd->max_plinks);
1316 		} else if (sta->peer_lid && sta->peer_lid != plid) {
1317 			wpa_printf(MSG_DEBUG,
1318 				   "MPM: peer_lid mismatch: 0x%x != 0x%x",
1319 				   sta->peer_lid, plid);
1320 			return; /* no FSM event */
1321 		} else {
1322 			sta->peer_lid = plid;
1323 			event = OPN_ACPT;
1324 		}
1325 		break;
1326 	case PLINK_CONFIRM:
1327 		if (plink_free_count(hapd) == 0) {
1328 			event = REQ_RJCT;
1329 			reason = WLAN_REASON_MESH_MAX_PEERS;
1330 			wpa_printf(MSG_INFO,
1331 				   "MPM: Peer link num over quota(%d)",
1332 				   hapd->max_plinks);
1333 		} else if (sta->my_lid != llid ||
1334 			   (sta->peer_lid && sta->peer_lid != plid)) {
1335 			wpa_printf(MSG_DEBUG,
1336 				   "MPM: lid mismatch: my_lid: 0x%x != 0x%x or peer_lid: 0x%x != 0x%x",
1337 				   sta->my_lid, llid, sta->peer_lid, plid);
1338 			return; /* no FSM event */
1339 		} else {
1340 			if (!sta->peer_lid)
1341 				sta->peer_lid = plid;
1342 			sta->peer_aid = aid;
1343 			event = CNF_ACPT;
1344 		}
1345 		break;
1346 	case PLINK_CLOSE:
1347 		if (sta->plink_state == PLINK_ESTAB)
1348 			/* Do not check for llid or plid. This does not
1349 			 * follow the standard but since multiple plinks
1350 			 * per cand are not supported, it is necessary in
1351 			 * order to avoid a livelock when MP A sees an
1352 			 * establish peer link to MP B but MP B does not
1353 			 * see it. This can be caused by a timeout in
1354 			 * B's peer link establishment or B being
1355 			 * restarted.
1356 			 */
1357 			event = CLS_ACPT;
1358 		else if (sta->peer_lid != plid) {
1359 			wpa_printf(MSG_DEBUG,
1360 				   "MPM: peer_lid mismatch: 0x%x != 0x%x",
1361 				   sta->peer_lid, plid);
1362 			return; /* no FSM event */
1363 		} else if (peer_mgmt_ie.plid && sta->my_lid != llid) {
1364 			wpa_printf(MSG_DEBUG,
1365 				   "MPM: my_lid mismatch: 0x%x != 0x%x",
1366 				   sta->my_lid, llid);
1367 			return; /* no FSM event */
1368 		} else {
1369 			event = CLS_ACPT;
1370 		}
1371 		break;
1372 	default:
1373 		/*
1374 		 * This cannot be hit due to the action_field check above, but
1375 		 * compilers may not be able to figure that out and can warn
1376 		 * about uninitialized event below.
1377 		 */
1378 		return;
1379 	}
1380 	mesh_mpm_fsm(wpa_s, sta, event, reason);
1381 }
1382 
1383 
1384 /* called by ap_free_sta */
mesh_mpm_free_sta(struct hostapd_data * hapd,struct sta_info * sta)1385 void mesh_mpm_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
1386 {
1387 	if (sta->plink_state == PLINK_ESTAB)
1388 		hapd->num_plinks--;
1389 	eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
1390 	eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
1391 }
1392