xref: /freebsd/contrib/wpa/wpa_supplicant/mesh.c (revision 10ff414c)
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 	bss->conf = *conf->bss;
441 	bss->conf->start_disabled = 1;
442 	bss->conf->mesh = MESH_ENABLED;
443 	bss->conf->ap_max_inactivity = wpa_s->conf->mesh_max_inactivity;
444 
445 	if (ieee80211_is_dfs(ssid->frequency, wpa_s->hw.modes,
446 			     wpa_s->hw.num_modes) && wpa_s->conf->country[0]) {
447 		conf->ieee80211h = 1;
448 		conf->ieee80211d = 1;
449 		conf->country[0] = wpa_s->conf->country[0];
450 		conf->country[1] = wpa_s->conf->country[1];
451 		conf->country[2] = ' ';
452 		wpa_s->mesh_params->handle_dfs = true;
453 	}
454 
455 	bss->iconf = conf;
456 	ifmsh->conf = conf;
457 
458 	ifmsh->bss[0]->max_plinks = wpa_s->conf->max_peer_links;
459 	ifmsh->bss[0]->dot11RSNASAERetransPeriod =
460 		wpa_s->conf->dot11RSNASAERetransPeriod;
461 	os_strlcpy(bss->conf->iface, wpa_s->ifname, sizeof(bss->conf->iface));
462 
463 	mconf = mesh_config_create(wpa_s, ssid);
464 	if (!mconf)
465 		goto out_free;
466 	ifmsh->mconf = mconf;
467 
468 	/* need conf->hw_mode for supported rates. */
469 	conf->hw_mode = ieee80211_freq_to_chan(frequency, &conf->channel);
470 	if (conf->hw_mode == NUM_HOSTAPD_MODES) {
471 		wpa_printf(MSG_ERROR, "Unsupported mesh mode frequency: %d MHz",
472 			   frequency);
473 		goto out_free;
474 	}
475 
476 	if (ssid->mesh_basic_rates == NULL) {
477 		/*
478 		 * XXX: Hack! This is so an MPM which correctly sets the ERP
479 		 * mandatory rates as BSSBasicRateSet doesn't reject us. We
480 		 * could add a new hw_mode HOSTAPD_MODE_IEEE80211G_ERP, but
481 		 * this is way easier. This also makes our BSSBasicRateSet
482 		 * advertised in beacons match the one in peering frames, sigh.
483 		 */
484 		if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
485 			conf->basic_rates = os_memdup(basic_rates_erp,
486 						      sizeof(basic_rates_erp));
487 			if (!conf->basic_rates)
488 				goto out_free;
489 		}
490 	} else {
491 		rate_len = 0;
492 		while (1) {
493 			if (ssid->mesh_basic_rates[rate_len] < 1)
494 				break;
495 			rate_len++;
496 		}
497 		conf->basic_rates = os_calloc(rate_len + 1, sizeof(int));
498 		if (conf->basic_rates == NULL)
499 			goto out_free;
500 		os_memcpy(conf->basic_rates, ssid->mesh_basic_rates,
501 			  rate_len * sizeof(int));
502 		conf->basic_rates[rate_len] = -1;
503 	}
504 
505 	/* While it can enhance performance to switch the primary channel, which
506 	 * is also the secondary channel of another network at the same time),
507 	 * to the other primary channel, problems exist with this in mesh
508 	 * networks.
509 	 *
510 	 * Example with problems:
511 	 *     - 3 mesh nodes M1-M3, freq (5200, 5180)
512 	 *     - other node O1, e.g. AP mode, freq (5180, 5200),
513 	 * Locations: O1 M1      M2      M3
514 	 *
515 	 * M3 can only send frames to M1 over M2, no direct connection is
516 	 * possible
517 	 * Start O1, M1 and M3 first, M1 or O1 will switch channels to align
518 	 * with* each other. M3 does not swap, because M1 or O1 cannot be
519 	 * reached. M2 is started afterwards and can either connect to M3 or M1
520 	 * because of this primary secondary channel switch.
521 	 *
522 	 * Solutions: (1) central coordination -> not always possible
523 	 *            (2) disable pri/sec channel switch in mesh networks
524 	 *
525 	 * In AP mode, when all nodes can work independently, this poses of
526 	 * course no problem, therefore disable it only in mesh mode. */
527 	conf->no_pri_sec_switch = 1;
528 	wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf);
529 
530 	if (wpa_drv_init_mesh(wpa_s)) {
531 		wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh in driver");
532 		return -1;
533 	}
534 
535 	if (hostapd_setup_interface(ifmsh)) {
536 		wpa_printf(MSG_ERROR,
537 			   "Failed to initialize hostapd interface for mesh");
538 		return -1;
539 	}
540 
541 	return 0;
542 out_free:
543 	wpa_supplicant_mesh_deinit(wpa_s, true);
544 	return -ENOMEM;
545 }
546 
547 
548 void wpa_mesh_notify_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
549 			  const u8 *ies, size_t ie_len)
550 {
551 	struct ieee802_11_elems elems;
552 
553 	wpa_msg(wpa_s, MSG_INFO,
554 		"new peer notification for " MACSTR, MAC2STR(addr));
555 
556 	if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
557 		wpa_msg(wpa_s, MSG_INFO, "Could not parse beacon from " MACSTR,
558 			MAC2STR(addr));
559 		return;
560 	}
561 	wpa_mesh_new_mesh_peer(wpa_s, addr, &elems);
562 }
563 
564 
565 void wpa_supplicant_mesh_add_scan_ie(struct wpa_supplicant *wpa_s,
566 				     struct wpabuf **extra_ie)
567 {
568 	/* EID + 0-length (wildcard) mesh-id */
569 	size_t ielen = 2;
570 
571 	if (wpabuf_resize(extra_ie, ielen) == 0) {
572 		wpabuf_put_u8(*extra_ie, WLAN_EID_MESH_ID);
573 		wpabuf_put_u8(*extra_ie, 0);
574 	}
575 }
576 
577 
578 int wpa_supplicant_join_mesh(struct wpa_supplicant *wpa_s,
579 			     struct wpa_ssid *ssid)
580 {
581 	struct wpa_driver_mesh_join_params *params = os_zalloc(sizeof(*params));
582 	int ret = 0;
583 
584 	if (!ssid || !ssid->ssid || !ssid->ssid_len || !ssid->frequency ||
585 	    !params) {
586 		ret = -ENOENT;
587 		os_free(params);
588 		goto out;
589 	}
590 
591 	wpa_supplicant_mesh_deinit(wpa_s, true);
592 
593 	wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
594 	wpa_s->group_cipher = WPA_CIPHER_NONE;
595 	wpa_s->mgmt_group_cipher = 0;
596 
597 	params->meshid = ssid->ssid;
598 	params->meshid_len = ssid->ssid_len;
599 	ibss_mesh_setup_freq(wpa_s, ssid, &params->freq);
600 	wpa_s->mesh_ht_enabled = !!params->freq.ht_enabled;
601 	wpa_s->mesh_vht_enabled = !!params->freq.vht_enabled;
602 	wpa_s->mesh_he_enabled = !!params->freq.he_enabled;
603 	if (params->freq.ht_enabled && params->freq.sec_channel_offset)
604 		ssid->ht40 = params->freq.sec_channel_offset;
605 
606 	if (wpa_s->mesh_vht_enabled) {
607 		ssid->vht = 1;
608 		ssid->vht_center_freq1 = params->freq.center_freq1;
609 		switch (params->freq.bandwidth) {
610 		case 80:
611 			if (params->freq.center_freq2) {
612 				ssid->max_oper_chwidth = CHANWIDTH_80P80MHZ;
613 				ssid->vht_center_freq2 =
614 					params->freq.center_freq2;
615 			} else {
616 				ssid->max_oper_chwidth = CHANWIDTH_80MHZ;
617 			}
618 			break;
619 		case 160:
620 			ssid->max_oper_chwidth = CHANWIDTH_160MHZ;
621 			break;
622 		default:
623 			ssid->max_oper_chwidth = CHANWIDTH_USE_HT;
624 			break;
625 		}
626 	}
627 	if (wpa_s->mesh_he_enabled)
628 		ssid->he = 1;
629 	if (ssid->beacon_int > 0)
630 		params->beacon_int = ssid->beacon_int;
631 	else if (wpa_s->conf->beacon_int > 0)
632 		params->beacon_int = wpa_s->conf->beacon_int;
633 	if (ssid->dtim_period > 0)
634 		params->dtim_period = ssid->dtim_period;
635 	else if (wpa_s->conf->dtim_period > 0)
636 		params->dtim_period = wpa_s->conf->dtim_period;
637 	params->conf.max_peer_links = wpa_s->conf->max_peer_links;
638 	if (ssid->mesh_rssi_threshold < DEFAULT_MESH_RSSI_THRESHOLD) {
639 		params->conf.rssi_threshold = ssid->mesh_rssi_threshold;
640 		params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD;
641 	}
642 
643 	if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
644 		params->flags |= WPA_DRIVER_MESH_FLAG_SAE_AUTH;
645 		params->flags |= WPA_DRIVER_MESH_FLAG_AMPE;
646 		wpa_s->conf->user_mpm = 1;
647 	}
648 
649 	if (wpa_s->conf->user_mpm) {
650 		params->flags |= WPA_DRIVER_MESH_FLAG_USER_MPM;
651 		params->conf.auto_plinks = 0;
652 	} else {
653 		params->flags |= WPA_DRIVER_MESH_FLAG_DRIVER_MPM;
654 		params->conf.auto_plinks = 1;
655 	}
656 	params->conf.peer_link_timeout = wpa_s->conf->mesh_max_inactivity;
657 
658 	os_free(wpa_s->mesh_params);
659 	wpa_s->mesh_params = params;
660 	if (wpa_supplicant_mesh_init(wpa_s, ssid, &params->freq)) {
661 		wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh");
662 		wpa_supplicant_leave_mesh(wpa_s, true);
663 		ret = -1;
664 		goto out;
665 	}
666 
667 out:
668 	return ret;
669 }
670 
671 
672 int wpa_supplicant_leave_mesh(struct wpa_supplicant *wpa_s, bool need_deinit)
673 {
674 	int ret = 0;
675 
676 	wpa_msg(wpa_s, MSG_INFO, "leaving mesh");
677 
678 	/* Need to send peering close messages first */
679 	if (need_deinit)
680 		wpa_supplicant_mesh_deinit(wpa_s, true);
681 
682 	ret = wpa_drv_leave_mesh(wpa_s);
683 	if (ret)
684 		wpa_msg(wpa_s, MSG_ERROR, "mesh leave error=%d", ret);
685 
686 	wpa_drv_set_operstate(wpa_s, 1);
687 
688 	return ret;
689 }
690 
691 
692 static int mesh_attr_text(const u8 *ies, size_t ies_len, char *buf, char *end)
693 {
694 	struct ieee802_11_elems elems;
695 	char *mesh_id, *pos = buf;
696 	u8 *bss_basic_rate_set;
697 	int bss_basic_rate_set_len, ret, i;
698 
699 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) == ParseFailed)
700 		return -1;
701 
702 	if (elems.mesh_id_len < 1)
703 		return 0;
704 
705 	mesh_id = os_malloc(elems.mesh_id_len + 1);
706 	if (mesh_id == NULL)
707 		return -1;
708 
709 	os_memcpy(mesh_id, elems.mesh_id, elems.mesh_id_len);
710 	mesh_id[elems.mesh_id_len] = '\0';
711 	ret = os_snprintf(pos, end - pos, "mesh_id=%s\n", mesh_id);
712 	os_free(mesh_id);
713 	if (os_snprintf_error(end - pos, ret))
714 		return pos - buf;
715 	pos += ret;
716 
717 	if (elems.mesh_config_len > 6) {
718 		ret = os_snprintf(pos, end - pos,
719 				  "active_path_selection_protocol_id=0x%02x\n"
720 				  "active_path_selection_metric_id=0x%02x\n"
721 				  "congestion_control_mode_id=0x%02x\n"
722 				  "synchronization_method_id=0x%02x\n"
723 				  "authentication_protocol_id=0x%02x\n"
724 				  "mesh_formation_info=0x%02x\n"
725 				  "mesh_capability=0x%02x\n",
726 				  elems.mesh_config[0], elems.mesh_config[1],
727 				  elems.mesh_config[2], elems.mesh_config[3],
728 				  elems.mesh_config[4], elems.mesh_config[5],
729 				  elems.mesh_config[6]);
730 		if (os_snprintf_error(end - pos, ret))
731 			return pos - buf;
732 		pos += ret;
733 	}
734 
735 	bss_basic_rate_set = os_malloc(elems.supp_rates_len +
736 		elems.ext_supp_rates_len);
737 	if (bss_basic_rate_set == NULL)
738 		return -1;
739 
740 	bss_basic_rate_set_len = 0;
741 	for (i = 0; i < elems.supp_rates_len; i++) {
742 		if (elems.supp_rates[i] & 0x80) {
743 			bss_basic_rate_set[bss_basic_rate_set_len++] =
744 				(elems.supp_rates[i] & 0x7f) * 5;
745 		}
746 	}
747 	for (i = 0; i < elems.ext_supp_rates_len; i++) {
748 		if (elems.ext_supp_rates[i] & 0x80) {
749 			bss_basic_rate_set[bss_basic_rate_set_len++] =
750 				(elems.ext_supp_rates[i] & 0x7f) * 5;
751 		}
752 	}
753 	if (bss_basic_rate_set_len > 0) {
754 		ret = os_snprintf(pos, end - pos, "bss_basic_rate_set=%d",
755 				  bss_basic_rate_set[0]);
756 		if (os_snprintf_error(end - pos, ret))
757 			goto fail;
758 		pos += ret;
759 
760 		for (i = 1; i < bss_basic_rate_set_len; i++) {
761 			ret = os_snprintf(pos, end - pos, " %d",
762 					  bss_basic_rate_set[i]);
763 			if (os_snprintf_error(end - pos, ret))
764 				goto fail;
765 			pos += ret;
766 		}
767 
768 		ret = os_snprintf(pos, end - pos, "\n");
769 		if (os_snprintf_error(end - pos, ret))
770 			goto fail;
771 		pos += ret;
772 	}
773 fail:
774 	os_free(bss_basic_rate_set);
775 
776 	return pos - buf;
777 }
778 
779 
780 int wpas_mesh_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
781 			       char *end)
782 {
783 	return mesh_attr_text(ies, ies_len, buf, end);
784 }
785 
786 
787 static int wpas_mesh_get_ifname(struct wpa_supplicant *wpa_s, char *ifname,
788 				size_t len)
789 {
790 	char *ifname_ptr = wpa_s->ifname;
791 	int res;
792 
793 	res = os_snprintf(ifname, len, "mesh-%s-%d", ifname_ptr,
794 			  wpa_s->mesh_if_idx);
795 	if (os_snprintf_error(len, res) ||
796 	    (os_strlen(ifname) >= IFNAMSIZ &&
797 	     os_strlen(wpa_s->ifname) < IFNAMSIZ)) {
798 		/* Try to avoid going over the IFNAMSIZ length limit */
799 		res = os_snprintf(ifname, len, "mesh-%d", wpa_s->mesh_if_idx);
800 		if (os_snprintf_error(len, res))
801 			return -1;
802 	}
803 	wpa_s->mesh_if_idx++;
804 	return 0;
805 }
806 
807 
808 int wpas_mesh_add_interface(struct wpa_supplicant *wpa_s, char *ifname,
809 			    size_t len)
810 {
811 	struct wpa_interface iface;
812 	struct wpa_supplicant *mesh_wpa_s;
813 	u8 addr[ETH_ALEN];
814 
815 	if (ifname[0] == '\0' && wpas_mesh_get_ifname(wpa_s, ifname, len) < 0)
816 		return -1;
817 
818 	if (wpa_drv_if_add(wpa_s, WPA_IF_MESH, ifname, NULL, NULL, NULL, addr,
819 			   NULL) < 0) {
820 		wpa_printf(MSG_ERROR,
821 			   "mesh: Failed to create new mesh interface");
822 		return -1;
823 	}
824 	wpa_printf(MSG_INFO, "mesh: Created virtual interface %s addr "
825 		   MACSTR, ifname, MAC2STR(addr));
826 
827 	os_memset(&iface, 0, sizeof(iface));
828 	iface.ifname = ifname;
829 	iface.driver = wpa_s->driver->name;
830 	iface.driver_param = wpa_s->conf->driver_param;
831 	iface.ctrl_interface = wpa_s->conf->ctrl_interface;
832 
833 	mesh_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
834 	if (!mesh_wpa_s) {
835 		wpa_printf(MSG_ERROR,
836 			   "mesh: Failed to create new wpa_supplicant interface");
837 		wpa_drv_if_remove(wpa_s, WPA_IF_MESH, ifname);
838 		return -1;
839 	}
840 	mesh_wpa_s->mesh_if_created = 1;
841 	return 0;
842 }
843 
844 
845 int wpas_mesh_peer_remove(struct wpa_supplicant *wpa_s, const u8 *addr)
846 {
847 	return mesh_mpm_close_peer(wpa_s, addr);
848 }
849 
850 
851 int wpas_mesh_peer_add(struct wpa_supplicant *wpa_s, const u8 *addr,
852 		       int duration)
853 {
854 	return mesh_mpm_connect_peer(wpa_s, addr, duration);
855 }
856