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