]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/mesh_mpm.c
Add attribute for dwell time in QCA vendor scan
[thirdparty/hostap.git] / wpa_supplicant / mesh_mpm.c
CommitLineData
5f92659d
BC
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"
adc6a5d8 14#include "common/hw_features_common.h"
034388ac 15#include "common/ocv.h"
5f92659d
BC
16#include "ap/hostapd.h"
17#include "ap/sta_info.h"
18#include "ap/ieee802_11.h"
9f2cf23e 19#include "ap/wpa_auth.h"
5f92659d
BC
20#include "wpa_supplicant_i.h"
21#include "driver_i.h"
22#include "mesh_mpm.h"
0f950df0 23#include "mesh_rsn.h"
9b0701fb 24#include "notify.h"
5f92659d 25
5f92659d 26struct mesh_peer_mgmt_ie {
c2ed7797
JM
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) */
5f92659d
BC
32};
33
34static void plink_timer(void *eloop_ctx, void *user_data);
35
36
37enum plink_event {
38 PLINK_UNDEFINED,
39 OPN_ACPT,
40 OPN_RJCT,
5f92659d
BC
41 CNF_ACPT,
42 CNF_RJCT,
5f92659d 43 CLS_ACPT,
02022564 44 REQ_RJCT
5f92659d
BC
45};
46
47static const char * const mplstate[] = {
b5f5c324 48 [0] = "UNINITIALIZED",
d02e5498
JM
49 [PLINK_IDLE] = "IDLE",
50 [PLINK_OPN_SNT] = "OPN_SNT",
51 [PLINK_OPN_RCVD] = "OPN_RCVD",
5f92659d
BC
52 [PLINK_CNF_RCVD] = "CNF_RCVD",
53 [PLINK_ESTAB] = "ESTAB",
54 [PLINK_HOLDING] = "HOLDING",
55 [PLINK_BLOCKED] = "BLOCKED"
56};
57
58static const char * const mplevent[] = {
59 [PLINK_UNDEFINED] = "UNDEFINED",
60 [OPN_ACPT] = "OPN_ACPT",
61 [OPN_RJCT] = "OPN_RJCT",
5f92659d
BC
62 [CNF_ACPT] = "CNF_ACPT",
63 [CNF_RJCT] = "CNF_RJCT",
5f92659d 64 [CLS_ACPT] = "CLS_ACPT",
02022564 65 [REQ_RJCT] = "REQ_RJCT",
5f92659d
BC
66};
67
68
69static 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
c2ed7797
JM
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;
5f92659d
BC
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
c2ed7797
JM
105 /* Peer Link ID, present for confirm, and possibly close */
106 if (len >= 2)
5f92659d
BC
107 mpm_ie->plid = ie;
108
109 return 0;
110}
111
112
113static 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
121static 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 */
150static Boolean 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 */
170static Boolean 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 */
185static 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)
984d5b7e 192 llid = 0; /* continue */
5f92659d
BC
193 } while (!llid || llid_in_use(wpa_s, llid));
194
195 sta->my_lid = llid;
196 sta->peer_lid = 0;
e347cafe 197 sta->peer_aid = 0;
871ff0b7
MH
198
199 /*
200 * We do not use wpa_mesh_set_plink_state() here because there is no
201 * entry in kernel yet.
202 */
d02e5498 203 sta->plink_state = PLINK_IDLE;
5f92659d
BC
204}
205
206
207static 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];
0f950df0 217 u8 *pos, *cat;
5f92659d
BC
218 u8 ie_len, add_plid = 0;
219 int ret;
220 int ampe = conf->security & MESH_CONF_SEC_AMPE;
798b3182 221 size_t buf_len;
5f92659d
BC
222
223 if (!sta)
224 return;
225
4929d556
JM
226 buf_len = 2 + /* Category and Action */
227 2 + /* capability info */
798b3182
MH
228 2 + /* AID */
229 2 + 8 + /* supported rates */
230 2 + (32 - 8) +
231 2 + 32 + /* mesh ID */
232 2 + 7 + /* mesh config */
25778502 233 2 + 24 + /* peering management */
65d0b67c 234 2 + 96 + 32 + 32 + /* AMPE (96 + max GTKlen + max IGTKlen) */
798b3182 235 2 + 16; /* MIC */
6334330e 236 if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
798b3182
MH
237 buf_len += 2 + 26 + /* HT capabilities */
238 2 + 22; /* HT operation */
239 }
a65efbfb
PO
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 */
3459c54a
SE
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 */
f029c44c
MH
256 if (type != PLINK_CLOSE)
257 buf_len += conf->rsn_ie_len; /* RSN IE */
034388ac
MV
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 */
f029c44c 263
798b3182 264 buf = wpabuf_alloc(buf_len);
5f92659d
BC
265 if (!buf)
266 return;
267
0f950df0 268 cat = wpabuf_mhead_u8(buf);
5f92659d
BC
269 wpabuf_put_u8(buf, WLAN_ACTION_SELF_PROTECTED);
270 wpabuf_put_u8(buf, type);
271
272 if (type != PLINK_CLOSE) {
d5b95325
MH
273 u8 info;
274
5f92659d
BC
275 /* capability info */
276 wpabuf_put_le16(buf, ampe ? IEEE80211_CAP_PRIVACY : 0);
277
5cfb672d 278 /* aid */
5f92659d 279 if (type == PLINK_CONFIRM)
681753f2 280 wpabuf_put_le16(buf, sta->aid);
5f92659d
BC
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
f029c44c
MH
287 /* IE: RSN IE */
288 wpabuf_put_data(buf, conf->rsn_ie, conf->rsn_ie_len);
289
5f92659d
BC
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);
d5b95325
MH
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);
5f92659d 306 /* always forwarding & accepting plinks for now */
5a8e48fd
MH
307 wpabuf_put_u8(buf, MESH_CAP_ACCEPT_ADDITIONAL_PEER |
308 MESH_CAP_FORWARDING);
5f92659d
BC
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);
608b0ff5
JM
346 if (ampe) {
347 if (sta->sae == NULL) {
348 wpa_msg(wpa_s, MSG_INFO, "Mesh MPM: no SAE session");
349 goto fail;
350 }
0f950df0
TP
351 mesh_rsn_get_pmkid(wpa_s->mesh_rsn, sta,
352 wpabuf_put(buf, PMKID_LEN));
608b0ff5 353 }
5f92659d 354
6334330e 355 if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
a65efbfb
PO
356 u8 ht_capa_oper[2 + 26 + 2 + 22];
357
5cfb672d
JM
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 }
a65efbfb
PO
362#ifdef CONFIG_IEEE80211AC
363 if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
364 u8 vht_capa_oper[2 + 12 + 2 + 5];
365
fc72a48a 366 pos = hostapd_eid_vht_capabilities(bss, vht_capa_oper, 0);
a65efbfb
PO
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 */
3459c54a
SE
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 */
5f92659d 386
034388ac
MV
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
0f950df0
TP
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
b5f5c324
JM
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);
5f92659d
BC
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
0f950df0 419fail:
5f92659d
BC
420 wpabuf_free(buf);
421}
422
423
424/* configure peering state in ours and driver's station entry */
871ff0b7
MH
425void wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s,
426 struct sta_info *sta,
427 enum mesh_plink_state state)
5f92659d
BC
428{
429 struct hostapd_sta_add_params params;
430 int ret;
431
b5f5c324
JM
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]);
5f92659d
BC
435 sta->plink_state = state;
436
437 os_memset(&params, 0, sizeof(params));
438 params.addr = sta->addr;
439 params.plink_state = state;
e347cafe 440 params.peer_aid = sta->peer_aid;
5f92659d
BC
441 params.set = 1;
442
5f92659d
BC
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
451static 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
41bff862 458 ap_free_sta(hapd, sta);
5f92659d
BC
459}
460
461
462static 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;
e6096799 467 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
9f2cf23e 468 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
5f92659d
BC
469
470 switch (sta->plink_state) {
d02e5498
JM
471 case PLINK_OPN_RCVD:
472 case PLINK_OPN_SNT:
5f92659d 473 /* retry timer */
e6096799
MH
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);
5f92659d
BC
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;
d267bdf9 484 /* fall through */
5f92659d
BC
485
486 case PLINK_CNF_RCVD:
487 /* confirm timer */
488 if (!reason)
489 reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
871ff0b7 490 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
e6096799
MH
491 eloop_register_timeout(conf->dot11MeshHoldingTimeout / 1000,
492 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
493 plink_timer, wpa_s, sta);
5f92659d
BC
494 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
495 break;
496 case PLINK_HOLDING:
497 /* holding timer */
9f2cf23e
MH
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 }
5f92659d
BC
505 mesh_mpm_fsm_restart(wpa_s, sta);
506 break;
507 default:
508 break;
509 }
510}
511
512
513/* initiate peering with station */
514static void
515mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
516 enum mesh_plink_state next_state)
517{
e6096799
MH
518 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
519
5f92659d 520 eloop_cancel_timeout(plink_timer, wpa_s, sta);
e6096799
MH
521 eloop_register_timeout(conf->dot11MeshRetryTimeout / 1000,
522 (conf->dot11MeshRetryTimeout % 1000) * 1000,
523 plink_timer, wpa_s, sta);
5f92659d
BC
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
e174ef34
MH
529static int mesh_mpm_plink_close(struct hostapd_data *hapd, struct sta_info *sta,
530 void *ctx)
5f92659d
BC
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
e174ef34
MH
548int 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
9f2cf23e
MH
569static 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
578int mesh_mpm_connect_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
579 int duration)
2604edbf
MH
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
d02e5498 606 if ((PLINK_OPN_SNT <= sta->plink_state &&
2604edbf
MH
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
9f2cf23e 614 if (conf->security == MESH_CONF_SEC_NONE) {
d02e5498 615 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_SNT);
9f2cf23e 616 } else {
2604edbf 617 mesh_rsn_auth_sae_sta(wpa_s, sta);
9f2cf23e
MH
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 }
2604edbf
MH
622
623 return 0;
624}
625
626
5f92659d
BC
627void 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);
9f2cf23e 636 eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
5f92659d
BC
637}
638
639
640/* for mesh_rsn to indicate this peer has completed authentication, and we're
641 * ready to start AMPE */
642void 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
0f950df0
TP
659 mesh_rsn_init_ampe_sta(wpa_s, sta);
660
5f92659d
BC
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
d02e5498 678 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_SNT);
5f92659d
BC
679}
680
745d9369
BC
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 */
687static struct sta_info * mesh_mpm_add_peer(struct wpa_supplicant *wpa_s,
688 const u8 *addr,
689 struct ieee802_11_elems *elems)
5f92659d
BC
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;
adc6a5d8 695 struct ieee80211_ht_operation *oper;
745d9369 696 int ret;
5f92659d 697
d70a8ab1
MH
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
5f92659d 706 sta = ap_get_sta(data, addr);
41b06b06
FF
707 if (sta)
708 return NULL;
709
710 sta = ap_sta_add(data, addr);
711 if (!sta)
712 return NULL;
5f92659d 713
a73c9842
PO
714 /* Set WMM by default since Mesh STAs are QoS STAs */
715 sta->flags |= WLAN_STA_WMM;
716
5f92659d 717 /* initialize sta */
745d9369
BC
718 if (copy_supp_rates(wpa_s, sta, elems)) {
719 ap_free_sta(data, sta);
720 return NULL;
721 }
5f92659d 722
cc64fe7b
JM
723 if (!sta->my_lid)
724 mesh_mpm_init_link(wpa_s, sta);
5f92659d 725
baae4cb9 726 copy_sta_ht_capab(data, sta, elems->ht_capabilities);
adc6a5d8
MH
727
728 oper = (struct ieee80211_ht_operation *) elems->ht_operation;
729 if (oper &&
84ea61cf
MH
730 !(oper->ht_param & HT_INFO_HT_PARAM_STA_CHNL_WIDTH) &&
731 sta->ht_capabilities) {
adc6a5d8
MH
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
5cfb672d 738 update_ht_state(data, sta);
5cfb672d 739
a65efbfb
PO
740#ifdef CONFIG_IEEE80211AC
741 copy_sta_vht_capab(data, sta, elems->vht_capabilities);
ad20a136 742 copy_sta_vht_oper(data, sta, elems->vht_operation);
a65efbfb
PO
743 set_sta_vht_opmode(data, sta, elems->vht_opmode_notif);
744#endif /* CONFIG_IEEE80211AC */
745
3459c54a
SE
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
681753f2
BC
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
5f92659d
BC
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;
681753f2 763 params.aid = sta->aid;
e347cafe 764 params.peer_aid = sta->peer_aid;
5f92659d 765 params.listen_interval = 100;
5cfb672d 766 params.ht_capabilities = sta->ht_capabilities;
a65efbfb 767 params.vht_capabilities = sta->vht_capabilities;
3459c54a
SE
768 params.he_capab = sta->he_capab;
769 params.he_capab_len = sta->he_capab_len;
5f92659d
BC
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);
745d9369
BC
785 ap_free_sta(data, sta);
786 return NULL;
5f92659d
BC
787 }
788
745d9369
BC
789 return sta;
790}
791
792
793void 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
9f2cf23e
MH
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)) {
07cb45cc
TP
808 wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
809 MACSTR " because of no_auto_peer", MAC2STR(addr));
3a322496
JM
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 }
07cb45cc
TP
833 return;
834 }
835
27446e47 836 if (conf->security == MESH_CONF_SEC_NONE) {
d02e5498 837 if (sta->plink_state < PLINK_OPN_SNT ||
27446e47 838 sta->plink_state > PLINK_ESTAB)
d02e5498 839 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_SNT);
27446e47 840 } else {
0f950df0 841 mesh_rsn_auth_sae_sta(wpa_s, sta);
27446e47 842 }
5f92659d
BC
843}
844
845
846void 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
858static 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];
0f950df0
TP
862 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
863 u8 seq[6] = {};
5f92659d
BC
864
865 wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
866 MAC2STR(sta->addr));
867
0f950df0 868 if (conf->security & MESH_CONF_SEC_AMPE) {
b02f4d05 869 wpa_hexdump_key(MSG_DEBUG, "mesh: MTK", sta->mtk, sta->mtk_len);
3b6deac0
JM
870 wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->pairwise_cipher),
871 sta->addr, 0, 0, seq, sizeof(seq),
a919a260
AW
872 sta->mtk, sta->mtk_len,
873 KEY_FLAG_PAIRWISE_RX_TX);
a4eec3c2 874
f868d560
JM
875 wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK Key RSC",
876 sta->mgtk_rsc, sizeof(sta->mgtk_rsc));
a4eec3c2
JM
877 wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK",
878 sta->mgtk, sta->mgtk_len);
3b6deac0
JM
879 wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->group_cipher),
880 sta->addr, sta->mgtk_key_id, 0,
f868d560 881 sta->mgtk_rsc, sizeof(sta->mgtk_rsc),
a919a260
AW
882 sta->mgtk, sta->mgtk_len,
883 KEY_FLAG_GROUP_RX);
4367eec4
JM
884
885 if (sta->igtk_len) {
f868d560
JM
886 wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK Key RSC",
887 sta->igtk_rsc, sizeof(sta->igtk_rsc));
3b6deac0 888 wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK",
4367eec4 889 sta->igtk, sta->igtk_len);
3b6deac0
JM
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),
a919a260
AW
895 sta->igtk, sta->igtk_len,
896 KEY_FLAG_GROUP_RX);
4367eec4 897 }
0f950df0
TP
898 }
899
5f92659d
BC
900 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
901 hapd->num_plinks++;
902
903 sta->flags |= WLAN_STA_ASSOC;
9f2cf23e 904 sta->mesh_sae_pmksa_caching = 0;
5f92659d 905
9f2cf23e
MH
906 eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
907 peer_add_timer(wpa_s, NULL);
5f92659d
BC
908 eloop_cancel_timeout(plink_timer, wpa_s, sta);
909
910 /* Send ctrl event */
6174de66
JM
911 wpa_msg(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
912 MAC2STR(sta->addr));
9b0701fb
SB
913
914 /* Send D-Bus event */
915 wpas_notify_mesh_peer_connected(wpa_s, sta->addr);
5f92659d
BC
916}
917
918
919static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
02022564 920 enum plink_event event, u16 reason)
5f92659d
BC
921{
922 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
0f950df0 923 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
5f92659d
BC
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) {
d02e5498 930 case PLINK_IDLE:
5f92659d
BC
931 switch (event) {
932 case CLS_ACPT:
933 mesh_mpm_fsm_restart(wpa_s, sta);
934 break;
935 case OPN_ACPT:
d02e5498 936 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_RCVD);
5f92659d
BC
937 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
938 0);
939 break;
02022564
JM
940 case REQ_RJCT:
941 mesh_mpm_send_plink_action(wpa_s, sta,
942 PLINK_CLOSE, reason);
943 break;
5f92659d
BC
944 default:
945 break;
946 }
947 break;
d02e5498 948 case PLINK_OPN_SNT:
5f92659d
BC
949 switch (event) {
950 case OPN_RJCT:
951 case CNF_RJCT:
02022564
JM
952 if (!reason)
953 reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
5f92659d
BC
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;
e6096799
MH
959 eloop_register_timeout(
960 conf->dot11MeshHoldingTimeout / 1000,
961 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
962 plink_timer, wpa_s, sta);
5f92659d
BC
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 */
d02e5498 968 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPN_RCVD);
5f92659d
BC
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);
baa12136 974 eloop_cancel_timeout(plink_timer, wpa_s, sta);
e6096799
MH
975 eloop_register_timeout(
976 conf->dot11MeshConfirmTimeout / 1000,
977 (conf->dot11MeshConfirmTimeout % 1000) * 1000,
978 plink_timer, wpa_s, sta);
5f92659d
BC
979 break;
980 default:
981 break;
982 }
983 break;
d02e5498 984 case PLINK_OPN_RCVD:
5f92659d
BC
985 switch (event) {
986 case OPN_RJCT:
987 case CNF_RJCT:
02022564
JM
988 if (!reason)
989 reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
5f92659d
BC
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;
e6096799
MH
995 eloop_register_timeout(
996 conf->dot11MeshHoldingTimeout / 1000,
997 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
998 plink_timer, wpa_s, sta);
5f92659d
BC
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:
0f950df0
TP
1008 if (conf->security & MESH_CONF_SEC_AMPE)
1009 mesh_rsn_derive_mtk(wpa_s, sta);
5f92659d
BC
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:
02022564
JM
1020 if (!reason)
1021 reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
5f92659d
BC
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;
e6096799
MH
1027 eloop_register_timeout(
1028 conf->dot11MeshHoldingTimeout / 1000,
1029 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
1030 plink_timer, wpa_s, sta);
5f92659d
BC
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:
f42c3ceb
PO
1036 if (conf->security & MESH_CONF_SEC_AMPE)
1037 mesh_rsn_derive_mtk(wpa_s, sta);
5f92659d
BC
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) {
78ac3685
JM
1048 case OPN_RJCT:
1049 case CNF_RJCT:
5f92659d
BC
1050 case CLS_ACPT:
1051 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
02022564
JM
1052 if (!reason)
1053 reason = WLAN_REASON_MESH_CLOSE_RCVD;
5f92659d 1054
e6096799
MH
1055 eloop_register_timeout(
1056 conf->dot11MeshHoldingTimeout / 1000,
1057 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
1058 plink_timer, wpa_s, sta);
5f92659d
BC
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
6174de66
JM
1065 wpa_msg(wpa_s, MSG_INFO, MESH_PEER_DISCONNECTED MACSTR,
1066 MAC2STR(sta->addr));
5f92659d 1067
a9de99b1
SB
1068 /* Send D-Bus event */
1069 wpas_notify_mesh_peer_disconnected(wpa_s, sta->addr,
1070 reason);
1071
5f92659d
BC
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
1111void 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];
0f950df0 1116 struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
5f92659d 1117 struct sta_info *sta;
e347cafe 1118 u16 plid = 0, llid = 0, aid = 0;
5f92659d
BC
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;
02022564 1125 u16 reason = 0;
5f92659d
BC
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;
c932b43e
JM
1131 if (action_field != PLINK_OPEN &&
1132 action_field != PLINK_CONFIRM &&
1133 action_field != PLINK_CLOSE)
1134 return;
5f92659d
BC
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 */
c932b43e
JM
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);
5f92659d 1145 return;
c932b43e
JM
1146 }
1147 wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
5f92659d
BC
1148
1149 if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
c932b43e
JM
1150 wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
1151 WPA_GET_LE16(ies));
5f92659d
BC
1152 ies += 2; /* capability */
1153 ie_len -= 2;
1154 }
1155 if (action_field == PLINK_CONFIRM) {
e347cafe
MH
1156 aid = WPA_GET_LE16(ies);
1157 wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", aid);
5f92659d
BC
1158 ies += 2; /* aid */
1159 ie_len -= 2;
1160 }
1161
1162 /* check for mesh peering, mesh id and mesh config IEs */
c932b43e
JM
1163 if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
1164 wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
5f92659d 1165 return;
c932b43e
JM
1166 }
1167 if (!elems.peer_mgmt) {
1168 wpa_printf(MSG_DEBUG,
1169 "MPM: No Mesh Peering Management element");
5f92659d 1170 return;
c932b43e
JM
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 }
5f92659d 1178
c932b43e
JM
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 }
5f92659d
BC
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);
c932b43e
JM
1190 if (ret) {
1191 wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
5f92659d 1192 return;
c932b43e 1193 }
5f92659d
BC
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);
c932b43e 1199 wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
5f92659d 1200
e8afaad7
JM
1201 if (action_field == PLINK_CLOSE)
1202 wpa_printf(MSG_DEBUG, "MPM: close reason=%u",
1203 WPA_GET_LE16(peer_mgmt_ie.reason));
1204
5f92659d 1205 sta = ap_get_sta(hapd, mgmt->sa);
745d9369
BC
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 &&
9f2cf23e 1212 (!(mconf->security & MESH_CONF_SEC_AMPE) ||
c1bd4bac 1213 wpa_auth_pmksa_get(hapd->wpa_auth, mgmt->sa, NULL)))
745d9369
BC
1214 sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
1215
c932b43e
JM
1216 if (!sta) {
1217 wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
5f92659d 1218 return;
c932b43e 1219 }
5f92659d 1220
0f950df0
TP
1221#ifdef CONFIG_SAE
1222 /* peer is in sae_accepted? */
c932b43e
JM
1223 if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
1224 wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
0f950df0 1225 return;
c932b43e 1226 }
0f950df0
TP
1227#endif /* CONFIG_SAE */
1228
5f92659d
BC
1229 if (!sta->my_lid)
1230 mesh_mpm_init_link(wpa_s, sta);
1231
1f2f3f1c
JM
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 */
02022564
JM
1245 mesh_mpm_fsm(wpa_s, sta, OPN_RJCT,
1246 WLAN_REASON_MESH_INVALID_GTK);
1f2f3f1c
JM
1247 }
1248 return;
1249 }
034388ac
MV
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 */
c932b43e 1300 }
0f950df0 1301
c932b43e
JM
1302 if (sta->plink_state == PLINK_BLOCKED) {
1303 wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
5f92659d 1304 return;
c932b43e 1305 }
5f92659d
BC
1306
1307 /* Now we will figure out the appropriate event... */
1308 switch (action_field) {
1309 case PLINK_OPEN:
46e8d90f 1310 if (plink_free_count(hapd) == 0) {
02022564
JM
1311 event = REQ_RJCT;
1312 reason = WLAN_REASON_MESH_MAX_PEERS;
46e8d90f
MH
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) {
8bd90996
JM
1317 wpa_printf(MSG_DEBUG,
1318 "MPM: peer_lid mismatch: 0x%x != 0x%x",
1319 sta->peer_lid, plid);
02022564 1320 return; /* no FSM event */
5f92659d
BC
1321 } else {
1322 sta->peer_lid = plid;
1323 event = OPN_ACPT;
1324 }
1325 break;
1326 case PLINK_CONFIRM:
46e8d90f 1327 if (plink_free_count(hapd) == 0) {
02022564
JM
1328 event = REQ_RJCT;
1329 reason = WLAN_REASON_MESH_MAX_PEERS;
46e8d90f
MH
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)) {
8bd90996
JM
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);
02022564 1338 return; /* no FSM event */
5f92659d
BC
1339 } else {
1340 if (!sta->peer_lid)
1341 sta->peer_lid = plid;
e347cafe 1342 sta->peer_aid = aid;
5f92659d
BC
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;
8bd90996
JM
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);
02022564 1362 return; /* no FSM event */
8bd90996
JM
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);
02022564 1367 return; /* no FSM event */
8bd90996 1368 } else {
5f92659d 1369 event = CLS_ACPT;
8bd90996 1370 }
5f92659d 1371 break;
763041b2
JM
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;
5f92659d 1379 }
02022564 1380 mesh_mpm_fsm(wpa_s, sta, event, reason);
5f92659d 1381}
c596f3f0
CYY
1382
1383
1384/* called by ap_free_sta */
9684c756 1385void mesh_mpm_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
c596f3f0 1386{
9684c756
SD
1387 if (sta->plink_state == PLINK_ESTAB)
1388 hapd->num_plinks--;
c596f3f0
CYY
1389 eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
1390 eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
1391}