blob: c0dd6d0b8830ec528fc14ebcb06b58a1b8fccb88 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * WPA Supplicant - Basic mesh mode routines
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 "utils/uuid.h"
14#include "common/ieee802_11_defs.h"
15#include "common/wpa_ctrl.h"
16#include "common/hw_features_common.h"
17#include "ap/sta_info.h"
18#include "ap/hostapd.h"
19#include "ap/ieee802_11.h"
20#include "config_ssid.h"
21#include "config.h"
22#include "wpa_supplicant_i.h"
23#include "driver_i.h"
24#include "notify.h"
25#include "ap.h"
26#include "mesh_mpm.h"
27#include "mesh_rsn.h"
28#include "mesh.h"
29
30
31static void wpa_supplicant_mesh_deinit(struct wpa_supplicant *wpa_s,
32 bool also_clear_hostapd)
33{
34 wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh,
35 also_clear_hostapd);
36
37 if (also_clear_hostapd) {
38 wpa_s->ifmsh = NULL;
39 wpa_s->current_ssid = NULL;
40 os_free(wpa_s->mesh_params);
41 wpa_s->mesh_params = NULL;
42 }
43
44 os_free(wpa_s->mesh_rsn);
45 wpa_s->mesh_rsn = NULL;
46
47 if (!also_clear_hostapd)
48 wpa_supplicant_leave_mesh(wpa_s, false);
49}
50
51
52void wpa_supplicant_mesh_iface_deinit(struct wpa_supplicant *wpa_s,
53 struct hostapd_iface *ifmsh,
54 bool also_clear_hostapd)
55{
56 if (!ifmsh)
57 return;
58
59 if (ifmsh->mconf) {
60 mesh_mpm_deinit(wpa_s, ifmsh);
61 if (ifmsh->mconf->rsn_ie) {
62 ifmsh->mconf->rsn_ie = NULL;
63 /* We cannot free this struct
64 * because wpa_authenticator on
65 * hostapd side is also using it
66 * for now just set to NULL and
67 * let hostapd code free it.
68 */
69 }
70 os_free(ifmsh->mconf);
71 ifmsh->mconf = NULL;
72 }
73
74 /* take care of shared data */
75 if (also_clear_hostapd) {
76 hostapd_interface_deinit(ifmsh);
77 hostapd_interface_free(ifmsh);
78 }
79}
80
81
82static struct mesh_conf * mesh_config_create(struct wpa_supplicant *wpa_s,
83 struct wpa_ssid *ssid)
84{
85 struct mesh_conf *conf;
86 int cipher;
87
88 conf = os_zalloc(sizeof(struct mesh_conf));
89 if (!conf)
90 return NULL;
91
92 os_memcpy(conf->meshid, ssid->ssid, ssid->ssid_len);
93 conf->meshid_len = ssid->ssid_len;
94
95 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE)
96 conf->security |= MESH_CONF_SEC_AUTH |
97 MESH_CONF_SEC_AMPE;
98 else
99 conf->security |= MESH_CONF_SEC_NONE;
100 conf->ieee80211w = ssid->ieee80211w;
101 if (conf->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT) {
102 if (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_BIP)
103 conf->ieee80211w = wpa_s->conf->pmf;
104 else
105 conf->ieee80211w = NO_MGMT_FRAME_PROTECTION;
106 }
107#ifdef CONFIG_OCV
108 conf->ocv = ssid->ocv;
109#endif /* CONFIG_OCV */
110
111 cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher, 0);
112 if (cipher < 0 || cipher == WPA_CIPHER_TKIP) {
113 wpa_msg(wpa_s, MSG_INFO, "mesh: Invalid pairwise cipher");
114 os_free(conf);
115 return NULL;
116 }
117 conf->pairwise_cipher = cipher;
118
119 cipher = wpa_pick_group_cipher(ssid->group_cipher);
120 if (cipher < 0 || cipher == WPA_CIPHER_TKIP ||
121 cipher == WPA_CIPHER_GTK_NOT_USED) {
122 wpa_msg(wpa_s, MSG_INFO, "mesh: Invalid group cipher");
123 os_free(conf);
124 return NULL;
125 }
126
127 conf->group_cipher = cipher;
128 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
129 if (ssid->group_mgmt_cipher == WPA_CIPHER_BIP_GMAC_128 ||
130 ssid->group_mgmt_cipher == WPA_CIPHER_BIP_GMAC_256 ||
131 ssid->group_mgmt_cipher == WPA_CIPHER_BIP_CMAC_256)
132 conf->mgmt_group_cipher = ssid->group_mgmt_cipher;
133 else
134 conf->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
135 }
136
137 /* defaults */
138 conf->mesh_pp_id = MESH_PATH_PROTOCOL_HWMP;
139 conf->mesh_pm_id = MESH_PATH_METRIC_AIRTIME;
140 conf->mesh_cc_id = 0;
141 conf->mesh_sp_id = MESH_SYNC_METHOD_NEIGHBOR_OFFSET;
142 conf->mesh_auth_id = (conf->security & MESH_CONF_SEC_AUTH) ? 1 : 0;
143 conf->mesh_fwding = ssid->mesh_fwding;
144 conf->dot11MeshMaxRetries = ssid->dot11MeshMaxRetries;
145 conf->dot11MeshRetryTimeout = ssid->dot11MeshRetryTimeout;
146 conf->dot11MeshConfirmTimeout = ssid->dot11MeshConfirmTimeout;
147 conf->dot11MeshHoldingTimeout = ssid->dot11MeshHoldingTimeout;
148
149 return conf;
150}
151
152
153static void wpas_mesh_copy_groups(struct hostapd_data *bss,
154 struct wpa_supplicant *wpa_s)
155{
156 int num_groups;
157 size_t groups_size;
158
159 for (num_groups = 0; wpa_s->conf->sae_groups[num_groups] > 0;
160 num_groups++)
161 ;
162
163 groups_size = (num_groups + 1) * sizeof(wpa_s->conf->sae_groups[0]);
164 bss->conf->sae_groups = os_malloc(groups_size);
165 if (bss->conf->sae_groups)
166 os_memcpy(bss->conf->sae_groups, wpa_s->conf->sae_groups,
167 groups_size);
168}
169
170
171static int wpas_mesh_init_rsn(struct wpa_supplicant *wpa_s)
172{
173 struct hostapd_iface *ifmsh = wpa_s->ifmsh;
174 struct wpa_ssid *ssid = wpa_s->current_ssid;
175 struct hostapd_data *bss = ifmsh->bss[0];
176 static int default_groups[] = { 19, 20, 21, 25, 26, -1 };
177 const char *password;
178 size_t len;
179
180 password = ssid->sae_password;
181 if (!password)
182 password = ssid->passphrase;
183 if (!password) {
184 wpa_printf(MSG_ERROR,
185 "mesh: Passphrase for SAE not configured");
186 return -1;
187 }
188
189 bss->conf->wpa = ssid->proto;
190 bss->conf->wpa_key_mgmt = ssid->key_mgmt;
191
192 if (wpa_s->conf->sae_groups && wpa_s->conf->sae_groups[0] > 0) {
193 wpas_mesh_copy_groups(bss, wpa_s);
194 } else {
195 bss->conf->sae_groups = os_memdup(default_groups,
196 sizeof(default_groups));
197 if (!bss->conf->sae_groups)
198 return -1;
199 }
200
201 len = os_strlen(password);
202 bss->conf->ssid.wpa_passphrase = dup_binstr(password, len);
203
204 wpa_s->mesh_rsn = mesh_rsn_auth_init(wpa_s, ifmsh->mconf);
205 return !wpa_s->mesh_rsn ? -1 : 0;
206}
207
208
209static int wpas_mesh_update_freq_params(struct wpa_supplicant *wpa_s)
210{
211 struct wpa_driver_mesh_join_params *params = wpa_s->mesh_params;
212 struct hostapd_iface *ifmsh = wpa_s->ifmsh;
213 struct he_capabilities *he_capab = NULL;
214
215 if (ifmsh->current_mode)
216 he_capab = &ifmsh->current_mode->he_capab[IEEE80211_MODE_MESH];
217
218 if (hostapd_set_freq_params(
219 &params->freq,
220 ifmsh->conf->hw_mode,
221 ifmsh->freq,
222 ifmsh->conf->channel,
223 ifmsh->conf->enable_edmg,
224 ifmsh->conf->edmg_channel,
225 ifmsh->conf->ieee80211n,
226 ifmsh->conf->ieee80211ac,
227 ifmsh->conf->ieee80211ax,
228 ifmsh->conf->ieee80211be,
229 ifmsh->conf->secondary_channel,
230 hostapd_get_oper_chwidth(ifmsh->conf),
231 hostapd_get_oper_centr_freq_seg0_idx(ifmsh->conf),
232 hostapd_get_oper_centr_freq_seg1_idx(ifmsh->conf),
233 ifmsh->conf->vht_capab,
234 he_capab, NULL)) {
235 wpa_printf(MSG_ERROR, "Error updating mesh frequency params");
236 wpa_supplicant_mesh_deinit(wpa_s, true);
237 return -1;
238 }
239
240 return 0;
241}
242
243
244static int wpas_mesh_complete(struct wpa_supplicant *wpa_s)
245{
246 struct hostapd_iface *ifmsh = wpa_s->ifmsh;
247 struct wpa_driver_mesh_join_params *params = wpa_s->mesh_params;
248 struct wpa_ssid *ssid = wpa_s->current_ssid;
249 int ret;
250
251 if (!params || !ssid || !ifmsh) {
252 wpa_printf(MSG_ERROR, "mesh: %s called without active mesh",
253 __func__);
254 return -1;
255 }
256
257 /*
258 * Update channel configuration if the channel has changed since the
259 * initial setting, i.e., due to DFS radar detection during CAC.
260 */
261 if (ifmsh->freq > 0 && ifmsh->freq != params->freq.freq) {
262 wpa_s->assoc_freq = ifmsh->freq;
263 ssid->frequency = ifmsh->freq;
264 if (wpas_mesh_update_freq_params(wpa_s) < 0)
265 return -1;
266 }
267
268 if (ifmsh->mconf->security != MESH_CONF_SEC_NONE &&
269 wpas_mesh_init_rsn(wpa_s)) {
270 wpa_printf(MSG_ERROR,
271 "mesh: RSN initialization failed - deinit mesh");
272 wpa_supplicant_mesh_deinit(wpa_s, false);
273 return -1;
274 }
275
276 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
277 wpa_s->pairwise_cipher = wpa_s->mesh_rsn->pairwise_cipher;
278 wpa_s->group_cipher = wpa_s->mesh_rsn->group_cipher;
279 wpa_s->mgmt_group_cipher = wpa_s->mesh_rsn->mgmt_group_cipher;
280 }
281
282 params->ies = ifmsh->mconf->rsn_ie;
283 params->ie_len = ifmsh->mconf->rsn_ie_len;
284 params->basic_rates = ifmsh->basic_rates;
285 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
286 params->conf.ht_opmode = ifmsh->bss[0]->iface->ht_op_mode;
287
288 wpa_msg(wpa_s, MSG_INFO, "joining mesh %s",
289 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
290 ret = wpa_drv_join_mesh(wpa_s, params);
291 if (ret)
292 wpa_msg(wpa_s, MSG_ERROR, "mesh join error=%d", ret);
293
294 /* hostapd sets the interface down until we associate */
295 wpa_drv_set_operstate(wpa_s, 1);
296
297 if (!ret) {
298 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
299
300 wpa_msg(wpa_s, MSG_INFO, MESH_GROUP_STARTED "ssid=\"%s\" id=%d",
301 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
302 ssid->id);
303 wpas_notify_mesh_group_started(wpa_s, ssid);
304 }
305
306 return ret;
307}
308
309
310static void wpas_mesh_complete_cb(void *arg)
311{
312 struct wpa_supplicant *wpa_s = arg;
313
314 wpas_mesh_complete(wpa_s);
315}
316
317
318static int wpa_supplicant_mesh_enable_iface_cb(struct hostapd_iface *ifmsh)
319{
320 struct wpa_supplicant *wpa_s = ifmsh->owner;
321 struct hostapd_data *bss;
322
323 ifmsh->mconf = mesh_config_create(wpa_s, wpa_s->current_ssid);
324
325 bss = ifmsh->bss[0];
326 bss->msg_ctx = wpa_s;
327 os_memcpy(bss->own_addr, wpa_s->own_addr, ETH_ALEN);
328 bss->driver = wpa_s->driver;
329 bss->drv_priv = wpa_s->drv_priv;
330 bss->iface = ifmsh;
331 bss->mesh_sta_free_cb = mesh_mpm_free_sta;
332 bss->setup_complete_cb = wpas_mesh_complete_cb;
333 bss->setup_complete_cb_ctx = wpa_s;
334
335 bss->conf->start_disabled = 1;
336 bss->conf->mesh = MESH_ENABLED;
337 bss->conf->ap_max_inactivity = wpa_s->conf->mesh_max_inactivity;
338
339 if (wpa_drv_init_mesh(wpa_s)) {
340 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh in driver");
341 return -1;
342 }
343
344 if (hostapd_setup_interface(ifmsh)) {
345 wpa_printf(MSG_ERROR,
346 "Failed to initialize hostapd interface for mesh");
347 return -1;
348 }
349
350 return 0;
351}
352
353
354static int wpa_supplicant_mesh_disable_iface_cb(struct hostapd_iface *ifmsh)
355{
356 struct wpa_supplicant *wpa_s = ifmsh->owner;
357 size_t j;
358
359 wpa_supplicant_mesh_deinit(wpa_s, false);
360
361#ifdef NEED_AP_MLME
362 for (j = 0; j < ifmsh->num_bss; j++)
363 hostapd_cleanup_cs_params(ifmsh->bss[j]);
364#endif /* NEED_AP_MLME */
365
366 /* Same as hostapd_interface_deinit() without deinitializing control
367 * interface */
368 for (j = 0; j < ifmsh->num_bss; j++) {
369 struct hostapd_data *hapd = ifmsh->bss[j];
370
371 hostapd_bss_deinit_no_free(hapd);
372 hostapd_free_hapd_data(hapd);
373 }
374
375 hostapd_cleanup_iface_partial(ifmsh);
376
377 return 0;
378}
379
380
381static int wpa_supplicant_mesh_init(struct wpa_supplicant *wpa_s,
382 struct wpa_ssid *ssid,
383 struct hostapd_freq_params *freq)
384{
385 struct hostapd_iface *ifmsh;
386 struct hostapd_data *bss;
387 struct hostapd_config *conf;
388 struct mesh_conf *mconf;
389 int basic_rates_erp[] = { 10, 20, 55, 60, 110, 120, 240, -1 };
390 int rate_len;
391 int frequency;
392
393 if (!wpa_s->conf->user_mpm) {
394 /* not much for us to do here */
395 wpa_msg(wpa_s, MSG_WARNING,
396 "user_mpm is not enabled in configuration");
397 return 0;
398 }
399
400 wpa_s->ifmsh = ifmsh = hostapd_alloc_iface();
401 if (!ifmsh)
402 return -ENOMEM;
403
404 ifmsh->owner = wpa_s;
405 ifmsh->drv_flags = wpa_s->drv_flags;
406 ifmsh->drv_flags2 = wpa_s->drv_flags2;
407 ifmsh->num_bss = 1;
408 ifmsh->enable_iface_cb = wpa_supplicant_mesh_enable_iface_cb;
409 ifmsh->disable_iface_cb = wpa_supplicant_mesh_disable_iface_cb;
410 ifmsh->bss = os_calloc(wpa_s->ifmsh->num_bss,
411 sizeof(struct hostapd_data *));
412 if (!ifmsh->bss)
413 goto out_free;
414
415 ifmsh->bss[0] = bss = hostapd_alloc_bss_data(NULL, NULL, NULL);
416 if (!bss)
417 goto out_free;
418
419 ifmsh->bss[0]->msg_ctx = wpa_s;
420 os_memcpy(bss->own_addr, wpa_s->own_addr, ETH_ALEN);
421 bss->driver = wpa_s->driver;
422 bss->drv_priv = wpa_s->drv_priv;
423 bss->iface = ifmsh;
424 bss->mesh_sta_free_cb = mesh_mpm_free_sta;
425 bss->setup_complete_cb = wpas_mesh_complete_cb;
426 bss->setup_complete_cb_ctx = wpa_s;
427 frequency = ssid->frequency;
428 if (frequency != freq->freq &&
429 frequency == freq->freq + freq->sec_channel_offset * 20) {
430 wpa_printf(MSG_DEBUG, "mesh: pri/sec channels switched");
431 frequency = freq->freq;
432 ssid->frequency = frequency;
433 }
434 wpa_s->assoc_freq = frequency;
435 wpa_s->current_ssid = ssid;
436
437 /* setup an AP config for auth processing */
438 conf = hostapd_config_defaults();
439 if (!conf)
440 goto out_free;
441
442 if (is_6ghz_freq(freq->freq)) {
443 /*
444 * IEEE Std 802.11ax-2021, 12.12.2:
445 * The STA shall use management frame protection (MFPR=1) when
446 * using RSN.
447 */
448 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
449
450 /* Set mandatory op_class parameter for setting up BSS */
451 switch (freq->bandwidth) {
452 case 20:
453 if (freq->freq == 5935)
454 conf->op_class = 136;
455 else
456 conf->op_class = 131;
457 break;
458 case 40:
459 conf->op_class = 132;
460 break;
461 case 80:
462 conf->op_class = 133;
463 break;
464 case 160:
465 conf->op_class = 134;
466 break;
467 default:
468 conf->op_class = 131;
469 break;
470 }
471 }
472
473 bss->conf = *conf->bss;
474 bss->conf->start_disabled = 1;
475 bss->conf->mesh = MESH_ENABLED;
476 bss->conf->ap_max_inactivity = wpa_s->conf->mesh_max_inactivity;
477 bss->conf->mesh_fwding = wpa_s->conf->mesh_fwding;
478
479 if (ieee80211_is_dfs(ssid->frequency, wpa_s->hw.modes,
480 wpa_s->hw.num_modes) && wpa_s->conf->country[0]) {
481 conf->ieee80211h = 1;
482 conf->ieee80211d = 1;
483 conf->country[0] = wpa_s->conf->country[0];
484 conf->country[1] = wpa_s->conf->country[1];
485 conf->country[2] = ' ';
486 wpa_s->mesh_params->handle_dfs = true;
487 }
488
489 bss->iconf = conf;
490 ifmsh->conf = conf;
491
492 ifmsh->bss[0]->max_plinks = wpa_s->conf->max_peer_links;
493 ifmsh->bss[0]->dot11RSNASAERetransPeriod =
494 wpa_s->conf->dot11RSNASAERetransPeriod;
495 os_strlcpy(bss->conf->iface, wpa_s->ifname, sizeof(bss->conf->iface));
496
497 mconf = mesh_config_create(wpa_s, ssid);
498 if (!mconf)
499 goto out_free;
500 ifmsh->mconf = mconf;
501
502 /* need conf->hw_mode for supported rates. */
503 conf->hw_mode = ieee80211_freq_to_chan(frequency, &conf->channel);
504 if (conf->hw_mode == NUM_HOSTAPD_MODES) {
505 wpa_printf(MSG_ERROR, "Unsupported mesh mode frequency: %d MHz",
506 frequency);
507 goto out_free;
508 }
509 if (ssid->noscan)
510 conf->noscan = 1;
511
512 if (ssid->mesh_basic_rates == NULL) {
513 /*
514 * XXX: Hack! This is so an MPM which correctly sets the ERP
515 * mandatory rates as BSSBasicRateSet doesn't reject us. We
516 * could add a new hw_mode HOSTAPD_MODE_IEEE80211G_ERP, but
517 * this is way easier. This also makes our BSSBasicRateSet
518 * advertised in beacons match the one in peering frames, sigh.
519 */
520 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
521 conf->basic_rates = os_memdup(basic_rates_erp,
522 sizeof(basic_rates_erp));
523 if (!conf->basic_rates)
524 goto out_free;
525 }
526 } else {
527 rate_len = 0;
528 while (1) {
529 if (ssid->mesh_basic_rates[rate_len] < 1)
530 break;
531 rate_len++;
532 }
533 conf->basic_rates = os_calloc(rate_len + 1, sizeof(int));
534 if (conf->basic_rates == NULL)
535 goto out_free;
536 os_memcpy(conf->basic_rates, ssid->mesh_basic_rates,
537 rate_len * sizeof(int));
538 conf->basic_rates[rate_len] = -1;
539 }
540
541 /* While it can enhance performance to switch the primary channel, which
542 * is also the secondary channel of another network at the same time),
543 * to the other primary channel, problems exist with this in mesh
544 * networks.
545 *
546 * Example with problems:
547 * - 3 mesh nodes M1-M3, freq (5200, 5180)
548 * - other node O1, e.g. AP mode, freq (5180, 5200),
549 * Locations: O1 M1 M2 M3
550 *
551 * M3 can only send frames to M1 over M2, no direct connection is
552 * possible
553 * Start O1, M1 and M3 first, M1 or O1 will switch channels to align
554 * with* each other. M3 does not swap, because M1 or O1 cannot be
555 * reached. M2 is started afterwards and can either connect to M3 or M1
556 * because of this primary secondary channel switch.
557 *
558 * Solutions: (1) central coordination -> not always possible
559 * (2) disable pri/sec channel switch in mesh networks
560 *
561 * In AP mode, when all nodes can work independently, this poses of
562 * course no problem, therefore disable it only in mesh mode. */
563 conf->no_pri_sec_switch = 1;
564 wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf);
565
566 if (wpa_drv_init_mesh(wpa_s)) {
567 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh in driver");
568 return -1;
569 }
570
571 if (hostapd_setup_interface(ifmsh)) {
572 wpa_printf(MSG_ERROR,
573 "Failed to initialize hostapd interface for mesh");
574 return -1;
575 }
576
577 return 0;
578out_free:
579 wpa_supplicant_mesh_deinit(wpa_s, true);
580 return -ENOMEM;
581}
582
583
584void wpa_mesh_notify_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
585 const u8 *ies, size_t ie_len)
586{
587 struct ieee802_11_elems elems;
588
589 wpa_msg(wpa_s, MSG_INFO,
590 "new peer notification for " MACSTR, MAC2STR(addr));
591
592 if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
593 wpa_msg(wpa_s, MSG_INFO, "Could not parse beacon from " MACSTR,
594 MAC2STR(addr));
595 return;
596 }
597 wpa_mesh_new_mesh_peer(wpa_s, addr, &elems);
598}
599
600
601void wpa_supplicant_mesh_add_scan_ie(struct wpa_supplicant *wpa_s,
602 struct wpabuf **extra_ie)
603{
604 /* EID + 0-length (wildcard) mesh-id */
605 size_t ielen = 2;
606
607 if (wpabuf_resize(extra_ie, ielen) == 0) {
608 wpabuf_put_u8(*extra_ie, WLAN_EID_MESH_ID);
609 wpabuf_put_u8(*extra_ie, 0);
610 }
611}
612
613
614int wpa_supplicant_join_mesh(struct wpa_supplicant *wpa_s,
615 struct wpa_ssid *ssid)
616{
617 struct wpa_driver_mesh_join_params *params = os_zalloc(sizeof(*params));
618 int ret = 0;
619
620 if (!ssid || !ssid->ssid || !ssid->ssid_len || !ssid->frequency ||
621 !params) {
622 ret = -ENOENT;
623 os_free(params);
624 goto out;
625 }
626
627 wpa_supplicant_mesh_deinit(wpa_s, true);
628
629 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
630 wpa_s->group_cipher = WPA_CIPHER_NONE;
631 wpa_s->mgmt_group_cipher = 0;
632
633 params->meshid = ssid->ssid;
634 params->meshid_len = ssid->ssid_len;
635 params->mcast_rate = ssid->mcast_rate;
636 ibss_mesh_setup_freq(wpa_s, ssid, &params->freq);
637 wpa_s->mesh_ht_enabled = !!params->freq.ht_enabled;
638 wpa_s->mesh_vht_enabled = !!params->freq.vht_enabled;
639 wpa_s->mesh_he_enabled = !!params->freq.he_enabled;
640 wpa_s->mesh_eht_enabled = !!params->freq.eht_enabled;
641 if (params->freq.ht_enabled && params->freq.sec_channel_offset)
642 ssid->ht40 = params->freq.sec_channel_offset;
643
644 if (wpa_s->mesh_vht_enabled) {
645 ssid->vht = 1;
646 ssid->vht_center_freq1 = params->freq.center_freq1;
647 switch (params->freq.bandwidth) {
648 case 80:
649 if (params->freq.center_freq2) {
650 ssid->max_oper_chwidth =
651 CONF_OPER_CHWIDTH_80P80MHZ;
652 ssid->vht_center_freq2 =
653 params->freq.center_freq2;
654 } else {
655 ssid->max_oper_chwidth =
656 CONF_OPER_CHWIDTH_80MHZ;
657 }
658 break;
659 case 160:
660 ssid->max_oper_chwidth = CONF_OPER_CHWIDTH_160MHZ;
661 break;
662 default:
663 ssid->max_oper_chwidth = CONF_OPER_CHWIDTH_USE_HT;
664 break;
665 }
666 }
667 if (wpa_s->mesh_he_enabled)
668 ssid->he = 1;
669 if (wpa_s->mesh_eht_enabled)
670 ssid->eht = 1;
671 if (ssid->beacon_int > 0)
672 params->beacon_int = ssid->beacon_int;
673 else if (wpa_s->conf->beacon_int > 0)
674 params->beacon_int = wpa_s->conf->beacon_int;
675 if (ssid->dtim_period > 0)
676 params->dtim_period = ssid->dtim_period;
677 else if (wpa_s->conf->dtim_period > 0)
678 params->dtim_period = wpa_s->conf->dtim_period;
679 params->conf.max_peer_links = wpa_s->conf->max_peer_links;
680 if (ssid->mesh_rssi_threshold < DEFAULT_MESH_RSSI_THRESHOLD) {
681 params->conf.rssi_threshold = ssid->mesh_rssi_threshold;
682 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD;
683 }
684
685 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
686 params->flags |= WPA_DRIVER_MESH_FLAG_SAE_AUTH;
687 params->flags |= WPA_DRIVER_MESH_FLAG_AMPE;
688 wpa_s->conf->user_mpm = 1;
689 }
690
691 if (wpa_s->conf->user_mpm) {
692 params->flags |= WPA_DRIVER_MESH_FLAG_USER_MPM;
693 params->conf.auto_plinks = 0;
694 } else {
695 params->flags |= WPA_DRIVER_MESH_FLAG_DRIVER_MPM;
696 params->conf.auto_plinks = 1;
697 }
698 params->conf.peer_link_timeout = wpa_s->conf->mesh_max_inactivity;
699
700 /* Always explicitely set forwarding to on or off for now */
701 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_FORWARDING;
702 params->conf.forwarding = ssid->mesh_fwding;
703
704 os_free(wpa_s->mesh_params);
705 wpa_s->mesh_params = params;
706 if (wpa_supplicant_mesh_init(wpa_s, ssid, &params->freq)) {
707 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh");
708 wpa_supplicant_leave_mesh(wpa_s, true);
709 ret = -1;
710 goto out;
711 }
712
713out:
714 return ret;
715}
716
717
718int wpa_supplicant_leave_mesh(struct wpa_supplicant *wpa_s, bool need_deinit)
719{
720 int ret = 0;
721
722 wpa_msg(wpa_s, MSG_INFO, "leaving mesh");
723
724 /* Need to send peering close messages first */
725 if (need_deinit)
726 wpa_supplicant_mesh_deinit(wpa_s, true);
727
728 ret = wpa_drv_leave_mesh(wpa_s);
729 if (ret)
730 wpa_msg(wpa_s, MSG_ERROR, "mesh leave error=%d", ret);
731
732 wpa_drv_set_operstate(wpa_s, 1);
733
734 return ret;
735}
736
737
738static int mesh_attr_text(const u8 *ies, size_t ies_len, char *buf, char *end)
739{
740 struct ieee802_11_elems elems;
741 char *mesh_id, *pos = buf;
742 u8 *bss_basic_rate_set;
743 int bss_basic_rate_set_len, ret, i;
744
745 if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) == ParseFailed)
746 return -1;
747
748 if (elems.mesh_id_len < 1)
749 return 0;
750
751 mesh_id = os_malloc(elems.mesh_id_len + 1);
752 if (mesh_id == NULL)
753 return -1;
754
755 os_memcpy(mesh_id, elems.mesh_id, elems.mesh_id_len);
756 mesh_id[elems.mesh_id_len] = '\0';
757 ret = os_snprintf(pos, end - pos, "mesh_id=%s\n", mesh_id);
758 os_free(mesh_id);
759 if (os_snprintf_error(end - pos, ret))
760 return pos - buf;
761 pos += ret;
762
763 if (elems.mesh_config_len > 6) {
764 ret = os_snprintf(pos, end - pos,
765 "active_path_selection_protocol_id=0x%02x\n"
766 "active_path_selection_metric_id=0x%02x\n"
767 "congestion_control_mode_id=0x%02x\n"
768 "synchronization_method_id=0x%02x\n"
769 "authentication_protocol_id=0x%02x\n"
770 "mesh_formation_info=0x%02x\n"
771 "mesh_capability=0x%02x\n",
772 elems.mesh_config[0], elems.mesh_config[1],
773 elems.mesh_config[2], elems.mesh_config[3],
774 elems.mesh_config[4], elems.mesh_config[5],
775 elems.mesh_config[6]);
776 if (os_snprintf_error(end - pos, ret))
777 return pos - buf;
778 pos += ret;
779 }
780
781 bss_basic_rate_set = os_malloc(elems.supp_rates_len +
782 elems.ext_supp_rates_len);
783 if (bss_basic_rate_set == NULL)
784 return -1;
785
786 bss_basic_rate_set_len = 0;
787 for (i = 0; i < elems.supp_rates_len; i++) {
788 if (elems.supp_rates[i] & 0x80) {
789 bss_basic_rate_set[bss_basic_rate_set_len++] =
790 (elems.supp_rates[i] & 0x7f) * 5;
791 }
792 }
793 for (i = 0; i < elems.ext_supp_rates_len; i++) {
794 if (elems.ext_supp_rates[i] & 0x80) {
795 bss_basic_rate_set[bss_basic_rate_set_len++] =
796 (elems.ext_supp_rates[i] & 0x7f) * 5;
797 }
798 }
799 if (bss_basic_rate_set_len > 0) {
800 ret = os_snprintf(pos, end - pos, "bss_basic_rate_set=%d",
801 bss_basic_rate_set[0]);
802 if (os_snprintf_error(end - pos, ret))
803 goto fail;
804 pos += ret;
805
806 for (i = 1; i < bss_basic_rate_set_len; i++) {
807 ret = os_snprintf(pos, end - pos, " %d",
808 bss_basic_rate_set[i]);
809 if (os_snprintf_error(end - pos, ret))
810 goto fail;
811 pos += ret;
812 }
813
814 ret = os_snprintf(pos, end - pos, "\n");
815 if (os_snprintf_error(end - pos, ret))
816 goto fail;
817 pos += ret;
818 }
819fail:
820 os_free(bss_basic_rate_set);
821
822 return pos - buf;
823}
824
825
826int wpas_mesh_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
827 char *end)
828{
829 return mesh_attr_text(ies, ies_len, buf, end);
830}
831
832
833static int wpas_mesh_get_ifname(struct wpa_supplicant *wpa_s, char *ifname,
834 size_t len)
835{
836 char *ifname_ptr = wpa_s->ifname;
837 int res;
838
839 res = os_snprintf(ifname, len, "mesh-%s-%d", ifname_ptr,
840 wpa_s->mesh_if_idx);
841 if (os_snprintf_error(len, res) ||
842 (os_strlen(ifname) >= IFNAMSIZ &&
843 os_strlen(wpa_s->ifname) < IFNAMSIZ)) {
844 /* Try to avoid going over the IFNAMSIZ length limit */
845 res = os_snprintf(ifname, len, "mesh-%d", wpa_s->mesh_if_idx);
846 if (os_snprintf_error(len, res))
847 return -1;
848 }
849 wpa_s->mesh_if_idx++;
850 return 0;
851}
852
853
854int wpas_mesh_add_interface(struct wpa_supplicant *wpa_s, char *ifname,
855 size_t len)
856{
857 struct wpa_interface iface;
858 struct wpa_supplicant *mesh_wpa_s;
859 u8 addr[ETH_ALEN];
860
861 if (ifname[0] == '\0' && wpas_mesh_get_ifname(wpa_s, ifname, len) < 0)
862 return -1;
863
864 if (wpa_drv_if_add(wpa_s, WPA_IF_MESH, ifname, NULL, NULL, NULL, addr,
865 NULL) < 0) {
866 wpa_printf(MSG_ERROR,
867 "mesh: Failed to create new mesh interface");
868 return -1;
869 }
870 wpa_printf(MSG_INFO, "mesh: Created virtual interface %s addr "
871 MACSTR, ifname, MAC2STR(addr));
872
873 os_memset(&iface, 0, sizeof(iface));
874 iface.ifname = ifname;
875 iface.driver = wpa_s->driver->name;
876 iface.driver_param = wpa_s->conf->driver_param;
877 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
878
879 mesh_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
880 if (!mesh_wpa_s) {
881 wpa_printf(MSG_ERROR,
882 "mesh: Failed to create new wpa_supplicant interface");
883 wpa_drv_if_remove(wpa_s, WPA_IF_MESH, ifname);
884 return -1;
885 }
886 mesh_wpa_s->mesh_if_created = 1;
887 return 0;
888}
889
890
891int wpas_mesh_peer_remove(struct wpa_supplicant *wpa_s, const u8 *addr)
892{
893 return mesh_mpm_close_peer(wpa_s, addr);
894}
895
896
897int wpas_mesh_peer_add(struct wpa_supplicant *wpa_s, const u8 *addr,
898 int duration)
899{
900 return mesh_mpm_connect_peer(wpa_s, addr, duration);
901}