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