]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/mesh_mpm.c
mesh: Add MESH_PEER_ADD command
[thirdparty/hostap.git] / wpa_supplicant / mesh_mpm.c
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 "ap/hostapd.h"
15 #include "ap/sta_info.h"
16 #include "ap/ieee802_11.h"
17 #include "wpa_supplicant_i.h"
18 #include "driver_i.h"
19 #include "mesh_mpm.h"
20 #include "mesh_rsn.h"
21
22 struct mesh_peer_mgmt_ie {
23 const u8 *proto_id; /* Mesh Peering Protocol Identifier (2 octets) */
24 const u8 *llid; /* Local Link ID (2 octets) */
25 const u8 *plid; /* Peer Link ID (conditional, 2 octets) */
26 const u8 *reason; /* Reason Code (conditional, 2 octets) */
27 const u8 *chosen_pmk; /* Chosen PMK (optional, 16 octets) */
28 };
29
30 static void plink_timer(void *eloop_ctx, void *user_data);
31
32
33 enum plink_event {
34 PLINK_UNDEFINED,
35 OPN_ACPT,
36 OPN_RJCT,
37 OPN_IGNR,
38 CNF_ACPT,
39 CNF_RJCT,
40 CNF_IGNR,
41 CLS_ACPT,
42 CLS_IGNR
43 };
44
45 static const char * const mplstate[] = {
46 [0] = "UNINITIALIZED",
47 [PLINK_LISTEN] = "LISTEN",
48 [PLINK_OPEN_SENT] = "OPEN_SENT",
49 [PLINK_OPEN_RCVD] = "OPEN_RCVD",
50 [PLINK_CNF_RCVD] = "CNF_RCVD",
51 [PLINK_ESTAB] = "ESTAB",
52 [PLINK_HOLDING] = "HOLDING",
53 [PLINK_BLOCKED] = "BLOCKED"
54 };
55
56 static const char * const mplevent[] = {
57 [PLINK_UNDEFINED] = "UNDEFINED",
58 [OPN_ACPT] = "OPN_ACPT",
59 [OPN_RJCT] = "OPN_RJCT",
60 [OPN_IGNR] = "OPN_IGNR",
61 [CNF_ACPT] = "CNF_ACPT",
62 [CNF_RJCT] = "CNF_RJCT",
63 [CNF_IGNR] = "CNF_IGNR",
64 [CLS_ACPT] = "CLS_ACPT",
65 [CLS_IGNR] = "CLS_IGNR"
66 };
67
68
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
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
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 */
150 static 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 */
170 static 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 */
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 continue;
193 } while (!llid || llid_in_use(wpa_s, llid));
194
195 sta->my_lid = llid;
196 sta->peer_lid = 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_LISTEN;
203 }
204
205
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, 0x1 | 0x8);
293 } else { /* Peer closing frame */
294 /* IE: Mesh ID */
295 wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
296 wpabuf_put_u8(buf, conf->meshid_len);
297 wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
298 }
299
300 /* IE: Mesh Peering Management element */
301 ie_len = 4;
302 if (ampe)
303 ie_len += PMKID_LEN;
304 switch (type) {
305 case PLINK_OPEN:
306 break;
307 case PLINK_CONFIRM:
308 ie_len += 2;
309 add_plid = 1;
310 break;
311 case PLINK_CLOSE:
312 ie_len += 2;
313 add_plid = 1;
314 ie_len += 2; /* reason code */
315 break;
316 }
317
318 wpabuf_put_u8(buf, WLAN_EID_PEER_MGMT);
319 wpabuf_put_u8(buf, ie_len);
320 /* peering protocol */
321 if (ampe)
322 wpabuf_put_le16(buf, 1);
323 else
324 wpabuf_put_le16(buf, 0);
325 wpabuf_put_le16(buf, sta->my_lid);
326 if (add_plid)
327 wpabuf_put_le16(buf, sta->peer_lid);
328 if (type == PLINK_CLOSE)
329 wpabuf_put_le16(buf, close_reason);
330 if (ampe) {
331 if (sta->sae == NULL) {
332 wpa_msg(wpa_s, MSG_INFO, "Mesh MPM: no SAE session");
333 goto fail;
334 }
335 mesh_rsn_get_pmkid(wpa_s->mesh_rsn, sta,
336 wpabuf_put(buf, PMKID_LEN));
337 }
338
339 #ifdef CONFIG_IEEE80211N
340 if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
341 u8 ht_capa_oper[2 + 26 + 2 + 22];
342
343 pos = hostapd_eid_ht_capabilities(bss, ht_capa_oper);
344 pos = hostapd_eid_ht_operation(bss, pos);
345 wpabuf_put_data(buf, ht_capa_oper, pos - ht_capa_oper);
346 }
347 #endif /* CONFIG_IEEE80211N */
348 #ifdef CONFIG_IEEE80211AC
349 if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
350 u8 vht_capa_oper[2 + 12 + 2 + 5];
351
352 pos = hostapd_eid_vht_capabilities(bss, vht_capa_oper);
353 pos = hostapd_eid_vht_operation(bss, pos);
354 wpabuf_put_data(buf, vht_capa_oper, pos - vht_capa_oper);
355 }
356 #endif /* CONFIG_IEEE80211AC */
357
358 if (ampe && mesh_rsn_protect_frame(wpa_s->mesh_rsn, sta, cat, buf)) {
359 wpa_msg(wpa_s, MSG_INFO,
360 "Mesh MPM: failed to add AMPE and MIC IE");
361 goto fail;
362 }
363
364 wpa_msg(wpa_s, MSG_DEBUG, "Mesh MPM: Sending peering frame type %d to "
365 MACSTR " (my_lid=0x%x peer_lid=0x%x)",
366 type, MAC2STR(sta->addr), sta->my_lid, sta->peer_lid);
367 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
368 sta->addr, wpa_s->own_addr, wpa_s->own_addr,
369 wpabuf_head(buf), wpabuf_len(buf), 0);
370 if (ret < 0)
371 wpa_msg(wpa_s, MSG_INFO,
372 "Mesh MPM: failed to send peering frame");
373
374 fail:
375 wpabuf_free(buf);
376 }
377
378
379 /* configure peering state in ours and driver's station entry */
380 void wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s,
381 struct sta_info *sta,
382 enum mesh_plink_state state)
383 {
384 struct hostapd_sta_add_params params;
385 int ret;
386
387 wpa_msg(wpa_s, MSG_DEBUG, "MPM set " MACSTR " from %s into %s",
388 MAC2STR(sta->addr), mplstate[sta->plink_state],
389 mplstate[state]);
390 sta->plink_state = state;
391
392 os_memset(&params, 0, sizeof(params));
393 params.addr = sta->addr;
394 params.plink_state = state;
395 params.set = 1;
396
397 ret = wpa_drv_sta_add(wpa_s, &params);
398 if (ret) {
399 wpa_msg(wpa_s, MSG_ERROR, "Driver failed to set " MACSTR
400 ": %d", MAC2STR(sta->addr), ret);
401 }
402 }
403
404
405 static void mesh_mpm_fsm_restart(struct wpa_supplicant *wpa_s,
406 struct sta_info *sta)
407 {
408 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
409
410 eloop_cancel_timeout(plink_timer, wpa_s, sta);
411
412 ap_free_sta(hapd, sta);
413 }
414
415
416 static void plink_timer(void *eloop_ctx, void *user_data)
417 {
418 struct wpa_supplicant *wpa_s = eloop_ctx;
419 struct sta_info *sta = user_data;
420 u16 reason = 0;
421 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
422
423 switch (sta->plink_state) {
424 case PLINK_OPEN_RCVD:
425 case PLINK_OPEN_SENT:
426 /* retry timer */
427 if (sta->mpm_retries < conf->dot11MeshMaxRetries) {
428 eloop_register_timeout(
429 conf->dot11MeshRetryTimeout / 1000,
430 (conf->dot11MeshRetryTimeout % 1000) * 1000,
431 plink_timer, wpa_s, sta);
432 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
433 sta->mpm_retries++;
434 break;
435 }
436 reason = WLAN_REASON_MESH_MAX_RETRIES;
437 /* fall through on else */
438
439 case PLINK_CNF_RCVD:
440 /* confirm timer */
441 if (!reason)
442 reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
443 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
444 eloop_register_timeout(conf->dot11MeshHoldingTimeout / 1000,
445 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
446 plink_timer, wpa_s, sta);
447 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
448 break;
449 case PLINK_HOLDING:
450 /* holding timer */
451 mesh_mpm_fsm_restart(wpa_s, sta);
452 break;
453 default:
454 break;
455 }
456 }
457
458
459 /* initiate peering with station */
460 static void
461 mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
462 enum mesh_plink_state next_state)
463 {
464 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
465
466 eloop_cancel_timeout(plink_timer, wpa_s, sta);
467 eloop_register_timeout(conf->dot11MeshRetryTimeout / 1000,
468 (conf->dot11MeshRetryTimeout % 1000) * 1000,
469 plink_timer, wpa_s, sta);
470 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
471 wpa_mesh_set_plink_state(wpa_s, sta, next_state);
472 }
473
474
475 static int mesh_mpm_plink_close(struct hostapd_data *hapd, struct sta_info *sta,
476 void *ctx)
477 {
478 struct wpa_supplicant *wpa_s = ctx;
479 int reason = WLAN_REASON_MESH_PEERING_CANCELLED;
480
481 if (sta) {
482 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
483 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
484 wpa_printf(MSG_DEBUG, "MPM closing plink sta=" MACSTR,
485 MAC2STR(sta->addr));
486 eloop_cancel_timeout(plink_timer, wpa_s, sta);
487 return 0;
488 }
489
490 return 1;
491 }
492
493
494 int mesh_mpm_close_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
495 {
496 struct hostapd_data *hapd;
497 struct sta_info *sta;
498
499 if (!wpa_s->ifmsh) {
500 wpa_msg(wpa_s, MSG_INFO, "Mesh is not prepared yet");
501 return -1;
502 }
503
504 hapd = wpa_s->ifmsh->bss[0];
505 sta = ap_get_sta(hapd, addr);
506 if (!sta) {
507 wpa_msg(wpa_s, MSG_INFO, "No such mesh peer");
508 return -1;
509 }
510
511 return mesh_mpm_plink_close(hapd, sta, wpa_s) == 0 ? 0 : -1;
512 }
513
514
515 int mesh_mpm_connect_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
516 {
517 struct wpa_ssid *ssid = wpa_s->current_ssid;
518 struct hostapd_data *hapd;
519 struct sta_info *sta;
520 struct mesh_conf *conf;
521
522 if (!wpa_s->ifmsh) {
523 wpa_msg(wpa_s, MSG_INFO, "Mesh is not prepared yet");
524 return -1;
525 }
526
527 if (!ssid || !ssid->no_auto_peer) {
528 wpa_msg(wpa_s, MSG_INFO,
529 "This command is available only with no_auto_peer mesh network");
530 return -1;
531 }
532
533 hapd = wpa_s->ifmsh->bss[0];
534 conf = wpa_s->ifmsh->mconf;
535
536 sta = ap_get_sta(hapd, addr);
537 if (!sta) {
538 wpa_msg(wpa_s, MSG_INFO, "No such mesh peer");
539 return -1;
540 }
541
542 if ((PLINK_OPEN_SENT <= sta->plink_state &&
543 sta->plink_state <= PLINK_ESTAB) ||
544 (sta->sae && sta->sae->state > SAE_NOTHING)) {
545 wpa_msg(wpa_s, MSG_INFO,
546 "Specified peer is connecting/connected");
547 return -1;
548 }
549
550 if (conf->security == MESH_CONF_SEC_NONE)
551 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
552 else
553 mesh_rsn_auth_sae_sta(wpa_s, sta);
554
555 return 0;
556 }
557
558
559 void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
560 {
561 struct hostapd_data *hapd = ifmsh->bss[0];
562
563 /* notify peers we're leaving */
564 ap_for_each_sta(hapd, mesh_mpm_plink_close, wpa_s);
565
566 hapd->num_plinks = 0;
567 hostapd_free_stas(hapd);
568 }
569
570
571 /* for mesh_rsn to indicate this peer has completed authentication, and we're
572 * ready to start AMPE */
573 void mesh_mpm_auth_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
574 {
575 struct hostapd_data *data = wpa_s->ifmsh->bss[0];
576 struct hostapd_sta_add_params params;
577 struct sta_info *sta;
578 int ret;
579
580 sta = ap_get_sta(data, addr);
581 if (!sta) {
582 wpa_msg(wpa_s, MSG_DEBUG, "no such mesh peer");
583 return;
584 }
585
586 /* TODO: Should do nothing if this STA is already authenticated, but
587 * the AP code already sets this flag. */
588 sta->flags |= WLAN_STA_AUTH;
589
590 mesh_rsn_init_ampe_sta(wpa_s, sta);
591
592 os_memset(&params, 0, sizeof(params));
593 params.addr = sta->addr;
594 params.flags = WPA_STA_AUTHENTICATED | WPA_STA_AUTHORIZED;
595 params.set = 1;
596
597 wpa_msg(wpa_s, MSG_DEBUG, "MPM authenticating " MACSTR,
598 MAC2STR(sta->addr));
599 ret = wpa_drv_sta_add(wpa_s, &params);
600 if (ret) {
601 wpa_msg(wpa_s, MSG_ERROR,
602 "Driver failed to set " MACSTR ": %d",
603 MAC2STR(sta->addr), ret);
604 }
605
606 if (!sta->my_lid)
607 mesh_mpm_init_link(wpa_s, sta);
608
609 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
610 }
611
612 /*
613 * Initialize a sta_info structure for a peer and upload it into the driver
614 * in preparation for beginning authentication or peering. This is done when a
615 * Beacon (secure or open mesh) or a peering open frame (for open mesh) is
616 * received from the peer for the first time.
617 */
618 static struct sta_info * mesh_mpm_add_peer(struct wpa_supplicant *wpa_s,
619 const u8 *addr,
620 struct ieee802_11_elems *elems)
621 {
622 struct hostapd_sta_add_params params;
623 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
624 struct hostapd_data *data = wpa_s->ifmsh->bss[0];
625 struct sta_info *sta;
626 int ret;
627
628 sta = ap_get_sta(data, addr);
629 if (!sta) {
630 sta = ap_sta_add(data, addr);
631 if (!sta)
632 return NULL;
633 }
634
635 /* Set WMM by default since Mesh STAs are QoS STAs */
636 sta->flags |= WLAN_STA_WMM;
637
638 /* initialize sta */
639 if (copy_supp_rates(wpa_s, sta, elems)) {
640 ap_free_sta(data, sta);
641 return NULL;
642 }
643
644 if (!sta->my_lid)
645 mesh_mpm_init_link(wpa_s, sta);
646
647 #ifdef CONFIG_IEEE80211N
648 copy_sta_ht_capab(data, sta, elems->ht_capabilities);
649 update_ht_state(data, sta);
650 #endif /* CONFIG_IEEE80211N */
651
652 #ifdef CONFIG_IEEE80211AC
653 copy_sta_vht_capab(data, sta, elems->vht_capabilities);
654 set_sta_vht_opmode(data, sta, elems->vht_opmode_notif);
655 #endif /* CONFIG_IEEE80211AC */
656
657 if (hostapd_get_aid(data, sta) < 0) {
658 wpa_msg(wpa_s, MSG_ERROR, "No AIDs available");
659 ap_free_sta(data, sta);
660 return NULL;
661 }
662
663 /* insert into driver */
664 os_memset(&params, 0, sizeof(params));
665 params.supp_rates = sta->supported_rates;
666 params.supp_rates_len = sta->supported_rates_len;
667 params.addr = addr;
668 params.plink_state = sta->plink_state;
669 params.aid = sta->aid;
670 params.listen_interval = 100;
671 params.ht_capabilities = sta->ht_capabilities;
672 params.vht_capabilities = sta->vht_capabilities;
673 params.flags |= WPA_STA_WMM;
674 params.flags_mask |= WPA_STA_AUTHENTICATED;
675 if (conf->security == MESH_CONF_SEC_NONE) {
676 params.flags |= WPA_STA_AUTHORIZED;
677 params.flags |= WPA_STA_AUTHENTICATED;
678 } else {
679 sta->flags |= WLAN_STA_MFP;
680 params.flags |= WPA_STA_MFP;
681 }
682
683 ret = wpa_drv_sta_add(wpa_s, &params);
684 if (ret) {
685 wpa_msg(wpa_s, MSG_ERROR,
686 "Driver failed to insert " MACSTR ": %d",
687 MAC2STR(addr), ret);
688 ap_free_sta(data, sta);
689 return NULL;
690 }
691
692 return sta;
693 }
694
695
696 void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
697 struct ieee802_11_elems *elems)
698 {
699 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
700 struct hostapd_data *data = wpa_s->ifmsh->bss[0];
701 struct sta_info *sta;
702 struct wpa_ssid *ssid = wpa_s->current_ssid;
703
704 sta = mesh_mpm_add_peer(wpa_s, addr, elems);
705 if (!sta)
706 return;
707
708 if (ssid && ssid->no_auto_peer) {
709 wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
710 MACSTR " because of no_auto_peer", MAC2STR(addr));
711 if (data->mesh_pending_auth) {
712 struct os_reltime age;
713 const struct ieee80211_mgmt *mgmt;
714 struct hostapd_frame_info fi;
715
716 mgmt = wpabuf_head(data->mesh_pending_auth);
717 os_reltime_age(&data->mesh_pending_auth_time, &age);
718 if (age.sec < 2 &&
719 os_memcmp(mgmt->sa, addr, ETH_ALEN) == 0) {
720 wpa_printf(MSG_DEBUG,
721 "mesh: Process pending Authentication frame from %u.%06u seconds ago",
722 (unsigned int) age.sec,
723 (unsigned int) age.usec);
724 os_memset(&fi, 0, sizeof(fi));
725 ieee802_11_mgmt(
726 data,
727 wpabuf_head(data->mesh_pending_auth),
728 wpabuf_len(data->mesh_pending_auth),
729 &fi);
730 }
731 wpabuf_free(data->mesh_pending_auth);
732 data->mesh_pending_auth = NULL;
733 }
734 return;
735 }
736
737 if (conf->security == MESH_CONF_SEC_NONE) {
738 if (sta->plink_state < PLINK_OPEN_SENT ||
739 sta->plink_state > PLINK_ESTAB)
740 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
741 } else {
742 mesh_rsn_auth_sae_sta(wpa_s, sta);
743 }
744 }
745
746
747 void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
748 {
749 struct hostapd_frame_info fi;
750
751 os_memset(&fi, 0, sizeof(fi));
752 fi.datarate = rx_mgmt->datarate;
753 fi.ssi_signal = rx_mgmt->ssi_signal;
754 ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
755 rx_mgmt->frame_len, &fi);
756 }
757
758
759 static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
760 struct sta_info *sta)
761 {
762 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
763 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
764 u8 seq[6] = {};
765
766 wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
767 MAC2STR(sta->addr));
768
769 if (conf->security & MESH_CONF_SEC_AMPE) {
770 wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 0, 0,
771 seq, sizeof(seq), sta->mtk, sizeof(sta->mtk));
772 wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 1, 0,
773 seq, sizeof(seq),
774 sta->mgtk, sizeof(sta->mgtk));
775 wpa_drv_set_key(wpa_s, WPA_ALG_IGTK, sta->addr, 4, 0,
776 seq, sizeof(seq),
777 sta->mgtk, sizeof(sta->mgtk));
778
779 wpa_hexdump_key(MSG_DEBUG, "mtk:", sta->mtk, sizeof(sta->mtk));
780 wpa_hexdump_key(MSG_DEBUG, "mgtk:",
781 sta->mgtk, sizeof(sta->mgtk));
782 }
783
784 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
785 hapd->num_plinks++;
786
787 sta->flags |= WLAN_STA_ASSOC;
788
789 eloop_cancel_timeout(plink_timer, wpa_s, sta);
790
791 /* Send ctrl event */
792 wpa_msg(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
793 MAC2STR(sta->addr));
794 }
795
796
797 static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
798 enum plink_event event)
799 {
800 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
801 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
802 u16 reason = 0;
803
804 wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
805 MAC2STR(sta->addr), mplstate[sta->plink_state],
806 mplevent[event]);
807
808 switch (sta->plink_state) {
809 case PLINK_LISTEN:
810 switch (event) {
811 case CLS_ACPT:
812 mesh_mpm_fsm_restart(wpa_s, sta);
813 break;
814 case OPN_ACPT:
815 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
816 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
817 0);
818 break;
819 default:
820 break;
821 }
822 break;
823 case PLINK_OPEN_SENT:
824 switch (event) {
825 case OPN_RJCT:
826 case CNF_RJCT:
827 reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
828 /* fall-through */
829 case CLS_ACPT:
830 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
831 if (!reason)
832 reason = WLAN_REASON_MESH_CLOSE_RCVD;
833 eloop_register_timeout(
834 conf->dot11MeshHoldingTimeout / 1000,
835 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
836 plink_timer, wpa_s, sta);
837 mesh_mpm_send_plink_action(wpa_s, sta,
838 PLINK_CLOSE, reason);
839 break;
840 case OPN_ACPT:
841 /* retry timer is left untouched */
842 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
843 mesh_mpm_send_plink_action(wpa_s, sta,
844 PLINK_CONFIRM, 0);
845 break;
846 case CNF_ACPT:
847 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
848 eloop_register_timeout(
849 conf->dot11MeshConfirmTimeout / 1000,
850 (conf->dot11MeshConfirmTimeout % 1000) * 1000,
851 plink_timer, wpa_s, sta);
852 break;
853 default:
854 break;
855 }
856 break;
857 case PLINK_OPEN_RCVD:
858 switch (event) {
859 case OPN_RJCT:
860 case CNF_RJCT:
861 reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
862 /* fall-through */
863 case CLS_ACPT:
864 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
865 if (!reason)
866 reason = WLAN_REASON_MESH_CLOSE_RCVD;
867 eloop_register_timeout(
868 conf->dot11MeshHoldingTimeout / 1000,
869 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
870 plink_timer, wpa_s, sta);
871 sta->mpm_close_reason = reason;
872 mesh_mpm_send_plink_action(wpa_s, sta,
873 PLINK_CLOSE, reason);
874 break;
875 case OPN_ACPT:
876 mesh_mpm_send_plink_action(wpa_s, sta,
877 PLINK_CONFIRM, 0);
878 break;
879 case CNF_ACPT:
880 if (conf->security & MESH_CONF_SEC_AMPE)
881 mesh_rsn_derive_mtk(wpa_s, sta);
882 mesh_mpm_plink_estab(wpa_s, sta);
883 break;
884 default:
885 break;
886 }
887 break;
888 case PLINK_CNF_RCVD:
889 switch (event) {
890 case OPN_RJCT:
891 case CNF_RJCT:
892 reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
893 /* fall-through */
894 case CLS_ACPT:
895 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
896 if (!reason)
897 reason = WLAN_REASON_MESH_CLOSE_RCVD;
898 eloop_register_timeout(
899 conf->dot11MeshHoldingTimeout / 1000,
900 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
901 plink_timer, wpa_s, sta);
902 sta->mpm_close_reason = reason;
903 mesh_mpm_send_plink_action(wpa_s, sta,
904 PLINK_CLOSE, reason);
905 break;
906 case OPN_ACPT:
907 mesh_mpm_plink_estab(wpa_s, sta);
908 mesh_mpm_send_plink_action(wpa_s, sta,
909 PLINK_CONFIRM, 0);
910 break;
911 default:
912 break;
913 }
914 break;
915 case PLINK_ESTAB:
916 switch (event) {
917 case CLS_ACPT:
918 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
919 reason = WLAN_REASON_MESH_CLOSE_RCVD;
920
921 eloop_register_timeout(
922 conf->dot11MeshHoldingTimeout / 1000,
923 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
924 plink_timer, wpa_s, sta);
925 sta->mpm_close_reason = reason;
926
927 wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
928 " closed with reason %d",
929 MAC2STR(sta->addr), reason);
930
931 wpa_msg(wpa_s, MSG_INFO, MESH_PEER_DISCONNECTED MACSTR,
932 MAC2STR(sta->addr));
933
934 hapd->num_plinks--;
935
936 mesh_mpm_send_plink_action(wpa_s, sta,
937 PLINK_CLOSE, reason);
938 break;
939 case OPN_ACPT:
940 mesh_mpm_send_plink_action(wpa_s, sta,
941 PLINK_CONFIRM, 0);
942 break;
943 default:
944 break;
945 }
946 break;
947 case PLINK_HOLDING:
948 switch (event) {
949 case CLS_ACPT:
950 mesh_mpm_fsm_restart(wpa_s, sta);
951 break;
952 case OPN_ACPT:
953 case CNF_ACPT:
954 case OPN_RJCT:
955 case CNF_RJCT:
956 reason = sta->mpm_close_reason;
957 mesh_mpm_send_plink_action(wpa_s, sta,
958 PLINK_CLOSE, reason);
959 break;
960 default:
961 break;
962 }
963 break;
964 default:
965 wpa_msg(wpa_s, MSG_DEBUG,
966 "Unsupported MPM event %s for state %s",
967 mplevent[event], mplstate[sta->plink_state]);
968 break;
969 }
970 }
971
972
973 void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
974 const struct ieee80211_mgmt *mgmt, size_t len)
975 {
976 u8 action_field;
977 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
978 struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
979 struct sta_info *sta;
980 u16 plid = 0, llid = 0;
981 enum plink_event event;
982 struct ieee802_11_elems elems;
983 struct mesh_peer_mgmt_ie peer_mgmt_ie;
984 const u8 *ies;
985 size_t ie_len;
986 int ret;
987
988 if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
989 return;
990
991 action_field = mgmt->u.action.u.slf_prot_action.action;
992 if (action_field != PLINK_OPEN &&
993 action_field != PLINK_CONFIRM &&
994 action_field != PLINK_CLOSE)
995 return;
996
997 ies = mgmt->u.action.u.slf_prot_action.variable;
998 ie_len = (const u8 *) mgmt + len -
999 mgmt->u.action.u.slf_prot_action.variable;
1000
1001 /* at least expect mesh id and peering mgmt */
1002 if (ie_len < 2 + 2) {
1003 wpa_printf(MSG_DEBUG,
1004 "MPM: Ignore too short action frame %u ie_len %u",
1005 action_field, (unsigned int) ie_len);
1006 return;
1007 }
1008 wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
1009
1010 if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
1011 wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
1012 WPA_GET_LE16(ies));
1013 ies += 2; /* capability */
1014 ie_len -= 2;
1015 }
1016 if (action_field == PLINK_CONFIRM) {
1017 wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", WPA_GET_LE16(ies));
1018 ies += 2; /* aid */
1019 ie_len -= 2;
1020 }
1021
1022 /* check for mesh peering, mesh id and mesh config IEs */
1023 if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
1024 wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
1025 return;
1026 }
1027 if (!elems.peer_mgmt) {
1028 wpa_printf(MSG_DEBUG,
1029 "MPM: No Mesh Peering Management element");
1030 return;
1031 }
1032 if (action_field != PLINK_CLOSE) {
1033 if (!elems.mesh_id || !elems.mesh_config) {
1034 wpa_printf(MSG_DEBUG,
1035 "MPM: No Mesh ID or Mesh Configuration element");
1036 return;
1037 }
1038
1039 if (!matches_local(wpa_s, &elems)) {
1040 wpa_printf(MSG_DEBUG,
1041 "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
1042 return;
1043 }
1044 }
1045
1046 ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
1047 elems.peer_mgmt,
1048 elems.peer_mgmt_len,
1049 &peer_mgmt_ie);
1050 if (ret) {
1051 wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
1052 return;
1053 }
1054
1055 /* the sender's llid is our plid and vice-versa */
1056 plid = WPA_GET_LE16(peer_mgmt_ie.llid);
1057 if (peer_mgmt_ie.plid)
1058 llid = WPA_GET_LE16(peer_mgmt_ie.plid);
1059 wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
1060
1061 sta = ap_get_sta(hapd, mgmt->sa);
1062
1063 /*
1064 * If this is an open frame from an unknown STA, and this is an
1065 * open mesh, then go ahead and add the peer before proceeding.
1066 */
1067 if (!sta && action_field == PLINK_OPEN &&
1068 !(mconf->security & MESH_CONF_SEC_AMPE))
1069 sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
1070
1071 if (!sta) {
1072 wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
1073 return;
1074 }
1075
1076 #ifdef CONFIG_SAE
1077 /* peer is in sae_accepted? */
1078 if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
1079 wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
1080 return;
1081 }
1082 #endif /* CONFIG_SAE */
1083
1084 if (!sta->my_lid)
1085 mesh_mpm_init_link(wpa_s, sta);
1086
1087 if ((mconf->security & MESH_CONF_SEC_AMPE) &&
1088 mesh_rsn_process_ampe(wpa_s, sta, &elems,
1089 &mgmt->u.action.category,
1090 peer_mgmt_ie.chosen_pmk,
1091 ies, ie_len)) {
1092 wpa_printf(MSG_DEBUG, "MPM: RSN process rejected frame");
1093 return;
1094 }
1095
1096 if (sta->plink_state == PLINK_BLOCKED) {
1097 wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
1098 return;
1099 }
1100
1101 /* Now we will figure out the appropriate event... */
1102 switch (action_field) {
1103 case PLINK_OPEN:
1104 if (plink_free_count(hapd) == 0) {
1105 event = OPN_IGNR;
1106 wpa_printf(MSG_INFO,
1107 "MPM: Peer link num over quota(%d)",
1108 hapd->max_plinks);
1109 } else if (sta->peer_lid && sta->peer_lid != plid) {
1110 event = OPN_IGNR;
1111 } else {
1112 sta->peer_lid = plid;
1113 event = OPN_ACPT;
1114 }
1115 break;
1116 case PLINK_CONFIRM:
1117 if (plink_free_count(hapd) == 0) {
1118 event = CNF_IGNR;
1119 wpa_printf(MSG_INFO,
1120 "MPM: Peer link num over quota(%d)",
1121 hapd->max_plinks);
1122 } else if (sta->my_lid != llid ||
1123 (sta->peer_lid && sta->peer_lid != plid)) {
1124 event = CNF_IGNR;
1125 } else {
1126 if (!sta->peer_lid)
1127 sta->peer_lid = plid;
1128 event = CNF_ACPT;
1129 }
1130 break;
1131 case PLINK_CLOSE:
1132 if (sta->plink_state == PLINK_ESTAB)
1133 /* Do not check for llid or plid. This does not
1134 * follow the standard but since multiple plinks
1135 * per cand are not supported, it is necessary in
1136 * order to avoid a livelock when MP A sees an
1137 * establish peer link to MP B but MP B does not
1138 * see it. This can be caused by a timeout in
1139 * B's peer link establishment or B being
1140 * restarted.
1141 */
1142 event = CLS_ACPT;
1143 else if (sta->peer_lid != plid)
1144 event = CLS_IGNR;
1145 else if (peer_mgmt_ie.plid && sta->my_lid != llid)
1146 event = CLS_IGNR;
1147 else
1148 event = CLS_ACPT;
1149 break;
1150 default:
1151 /*
1152 * This cannot be hit due to the action_field check above, but
1153 * compilers may not be able to figure that out and can warn
1154 * about uninitialized event below.
1155 */
1156 return;
1157 }
1158 mesh_mpm_fsm(wpa_s, sta, event);
1159 }
1160
1161
1162 /* called by ap_free_sta */
1163 void mesh_mpm_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
1164 {
1165 if (sta->plink_state == PLINK_ESTAB)
1166 hapd->num_plinks--;
1167 eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
1168 eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
1169 }