blob: c085466b1e71d69c21eb14a0c3638e02eefbbe63 [file] [log] [blame]
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -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 "ap/sta_info.h"
17#include "ap/hostapd.h"
18#include "ap/ieee802_11.h"
19#include "config_ssid.h"
20#include "config.h"
21#include "wpa_supplicant_i.h"
22#include "driver_i.h"
23#include "notify.h"
24#include "ap.h"
25#include "mesh_mpm.h"
26#include "mesh_rsn.h"
27#include "mesh.h"
28
29
30static void wpa_supplicant_mesh_deinit(struct wpa_supplicant *wpa_s)
31{
32 wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh);
33 wpa_s->ifmsh = NULL;
34 wpa_s->current_ssid = NULL;
35 os_free(wpa_s->mesh_rsn);
36 wpa_s->mesh_rsn = NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -080037 os_free(wpa_s->mesh_params);
38 wpa_s->mesh_params = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080039 /* TODO: leave mesh (stop beacon). This will happen on link down
40 * anyway, so it's not urgent */
41}
42
43
44void wpa_supplicant_mesh_iface_deinit(struct wpa_supplicant *wpa_s,
45 struct hostapd_iface *ifmsh)
46{
47 if (!ifmsh)
48 return;
49
50 if (ifmsh->mconf) {
51 mesh_mpm_deinit(wpa_s, ifmsh);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080052 if (ifmsh->mconf->rsn_ie) {
53 ifmsh->mconf->rsn_ie = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080054 /* We cannot free this struct
55 * because wpa_authenticator on
56 * hostapd side is also using it
57 * for now just set to NULL and
58 * let hostapd code free it.
59 */
60 }
61 os_free(ifmsh->mconf);
62 ifmsh->mconf = NULL;
63 }
64
65 /* take care of shared data */
66 hostapd_interface_deinit(ifmsh);
67 hostapd_interface_free(ifmsh);
68}
69
70
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070071static struct mesh_conf * mesh_config_create(struct wpa_supplicant *wpa_s,
72 struct wpa_ssid *ssid)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080073{
74 struct mesh_conf *conf;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070075 int cipher;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080076
77 conf = os_zalloc(sizeof(struct mesh_conf));
78 if (!conf)
79 return NULL;
80
81 os_memcpy(conf->meshid, ssid->ssid, ssid->ssid_len);
82 conf->meshid_len = ssid->ssid_len;
83
84 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE)
85 conf->security |= MESH_CONF_SEC_AUTH |
86 MESH_CONF_SEC_AMPE;
87 else
88 conf->security |= MESH_CONF_SEC_NONE;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070089 conf->ieee80211w = ssid->ieee80211w;
90 if (conf->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT) {
91 if (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_BIP)
92 conf->ieee80211w = wpa_s->conf->pmf;
93 else
94 conf->ieee80211w = NO_MGMT_FRAME_PROTECTION;
95 }
Hai Shalom74f70d42019-02-11 14:42:39 -080096#ifdef CONFIG_OCV
97 conf->ocv = ssid->ocv;
98#endif /* CONFIG_OCV */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070099
100 cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher, 0);
101 if (cipher < 0 || cipher == WPA_CIPHER_TKIP) {
102 wpa_msg(wpa_s, MSG_INFO, "mesh: Invalid pairwise cipher");
103 os_free(conf);
104 return NULL;
105 }
106 conf->pairwise_cipher = cipher;
107
108 cipher = wpa_pick_group_cipher(ssid->group_cipher);
109 if (cipher < 0 || cipher == WPA_CIPHER_TKIP ||
110 cipher == WPA_CIPHER_GTK_NOT_USED) {
111 wpa_msg(wpa_s, MSG_INFO, "mesh: Invalid group cipher");
112 os_free(conf);
113 return NULL;
114 }
115
116 conf->group_cipher = cipher;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800117 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
118 if (ssid->group_mgmt_cipher == WPA_CIPHER_BIP_GMAC_128 ||
119 ssid->group_mgmt_cipher == WPA_CIPHER_BIP_GMAC_256 ||
120 ssid->group_mgmt_cipher == WPA_CIPHER_BIP_CMAC_256)
121 conf->mgmt_group_cipher = ssid->group_mgmt_cipher;
122 else
123 conf->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
124 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800125
126 /* defaults */
127 conf->mesh_pp_id = MESH_PATH_PROTOCOL_HWMP;
128 conf->mesh_pm_id = MESH_PATH_METRIC_AIRTIME;
129 conf->mesh_cc_id = 0;
130 conf->mesh_sp_id = MESH_SYNC_METHOD_NEIGHBOR_OFFSET;
131 conf->mesh_auth_id = (conf->security & MESH_CONF_SEC_AUTH) ? 1 : 0;
132 conf->dot11MeshMaxRetries = ssid->dot11MeshMaxRetries;
133 conf->dot11MeshRetryTimeout = ssid->dot11MeshRetryTimeout;
134 conf->dot11MeshConfirmTimeout = ssid->dot11MeshConfirmTimeout;
135 conf->dot11MeshHoldingTimeout = ssid->dot11MeshHoldingTimeout;
136
137 return conf;
138}
139
140
141static void wpas_mesh_copy_groups(struct hostapd_data *bss,
142 struct wpa_supplicant *wpa_s)
143{
144 int num_groups;
145 size_t groups_size;
146
147 for (num_groups = 0; wpa_s->conf->sae_groups[num_groups] > 0;
148 num_groups++)
149 ;
150
151 groups_size = (num_groups + 1) * sizeof(wpa_s->conf->sae_groups[0]);
152 bss->conf->sae_groups = os_malloc(groups_size);
153 if (bss->conf->sae_groups)
154 os_memcpy(bss->conf->sae_groups, wpa_s->conf->sae_groups,
155 groups_size);
156}
157
158
Hai Shalom74f70d42019-02-11 14:42:39 -0800159static int wpas_mesh_init_rsn(struct wpa_supplicant *wpa_s)
160{
161 struct hostapd_iface *ifmsh = wpa_s->ifmsh;
162 struct wpa_ssid *ssid = wpa_s->current_ssid;
163 struct hostapd_data *bss = ifmsh->bss[0];
164 static int default_groups[] = { 19, 20, 21, 25, 26, -1 };
165 const char *password;
166 size_t len;
167
168 password = ssid->sae_password;
169 if (!password)
170 password = ssid->passphrase;
171 if (!password) {
172 wpa_printf(MSG_ERROR,
173 "mesh: Passphrase for SAE not configured");
174 return -1;
175 }
176
177 bss->conf->wpa = ssid->proto;
178 bss->conf->wpa_key_mgmt = ssid->key_mgmt;
179
180 if (wpa_s->conf->sae_groups && wpa_s->conf->sae_groups[0] > 0) {
181 wpas_mesh_copy_groups(bss, wpa_s);
182 } else {
183 bss->conf->sae_groups = os_memdup(default_groups,
184 sizeof(default_groups));
185 if (!bss->conf->sae_groups)
186 return -1;
187 }
188
189 len = os_strlen(password);
190 bss->conf->ssid.wpa_passphrase = dup_binstr(password, len);
191
192 wpa_s->mesh_rsn = mesh_rsn_auth_init(wpa_s, ifmsh->mconf);
193 return !wpa_s->mesh_rsn ? -1 : 0;
194}
195
196
197static int wpas_mesh_complete(struct wpa_supplicant *wpa_s)
198{
199 struct hostapd_iface *ifmsh = wpa_s->ifmsh;
200 struct wpa_driver_mesh_join_params *params = wpa_s->mesh_params;
201 struct wpa_ssid *ssid = wpa_s->current_ssid;
202 int ret;
203
Hai Shalom021b0b52019-04-10 11:17:58 -0700204 if (!params || !ssid || !ifmsh) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800205 wpa_printf(MSG_ERROR, "mesh: %s called without active mesh",
206 __func__);
207 return -1;
208 }
209
210 if (ifmsh->mconf->security != MESH_CONF_SEC_NONE &&
211 wpas_mesh_init_rsn(wpa_s)) {
212 wpa_printf(MSG_ERROR,
213 "mesh: RSN initialization failed - deinit mesh");
214 wpa_supplicant_mesh_deinit(wpa_s);
Hai Shalom81f62d82019-07-22 12:10:00 -0700215 wpa_drv_leave_mesh(wpa_s);
Hai Shalom74f70d42019-02-11 14:42:39 -0800216 return -1;
217 }
218
219 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
220 wpa_s->pairwise_cipher = wpa_s->mesh_rsn->pairwise_cipher;
221 wpa_s->group_cipher = wpa_s->mesh_rsn->group_cipher;
222 wpa_s->mgmt_group_cipher = wpa_s->mesh_rsn->mgmt_group_cipher;
223 }
224
Hai Shalom021b0b52019-04-10 11:17:58 -0700225 params->ies = ifmsh->mconf->rsn_ie;
226 params->ie_len = ifmsh->mconf->rsn_ie_len;
227 params->basic_rates = ifmsh->basic_rates;
228 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
229 params->conf.ht_opmode = ifmsh->bss[0]->iface->ht_op_mode;
Hai Shalom74f70d42019-02-11 14:42:39 -0800230
231 wpa_msg(wpa_s, MSG_INFO, "joining mesh %s",
232 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
233 ret = wpa_drv_join_mesh(wpa_s, params);
234 if (ret)
235 wpa_msg(wpa_s, MSG_ERROR, "mesh join error=%d", ret);
236
237 /* hostapd sets the interface down until we associate */
238 wpa_drv_set_operstate(wpa_s, 1);
239
240 if (!ret)
241 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
242
243 return ret;
244}
245
246
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800247static int wpa_supplicant_mesh_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt29333592017-01-09 12:27:11 -0800248 struct wpa_ssid *ssid,
249 struct hostapd_freq_params *freq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800250{
251 struct hostapd_iface *ifmsh;
252 struct hostapd_data *bss;
253 struct hostapd_config *conf;
254 struct mesh_conf *mconf;
255 int basic_rates_erp[] = { 10, 20, 55, 60, 110, 120, 240, -1 };
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800256 int rate_len;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800257 int frequency;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800258
259 if (!wpa_s->conf->user_mpm) {
260 /* not much for us to do here */
261 wpa_msg(wpa_s, MSG_WARNING,
262 "user_mpm is not enabled in configuration");
263 return 0;
264 }
265
Roshan Pius3a1667e2018-07-03 15:17:14 -0700266 wpa_s->ifmsh = ifmsh = hostapd_alloc_iface();
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800267 if (!ifmsh)
268 return -ENOMEM;
269
270 ifmsh->drv_flags = wpa_s->drv_flags;
Hai Shalomb755a2a2020-04-23 21:49:02 -0700271 ifmsh->drv_flags2 = wpa_s->drv_flags2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800272 ifmsh->num_bss = 1;
273 ifmsh->bss = os_calloc(wpa_s->ifmsh->num_bss,
274 sizeof(struct hostapd_data *));
275 if (!ifmsh->bss)
276 goto out_free;
277
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800278 ifmsh->bss[0] = bss = hostapd_alloc_bss_data(NULL, NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800279 if (!bss)
280 goto out_free;
281
Roshan Pius3a1667e2018-07-03 15:17:14 -0700282 ifmsh->bss[0]->msg_ctx = wpa_s;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800283 os_memcpy(bss->own_addr, wpa_s->own_addr, ETH_ALEN);
284 bss->driver = wpa_s->driver;
285 bss->drv_priv = wpa_s->drv_priv;
286 bss->iface = ifmsh;
287 bss->mesh_sta_free_cb = mesh_mpm_free_sta;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800288 frequency = ssid->frequency;
289 if (frequency != freq->freq &&
290 frequency == freq->freq + freq->sec_channel_offset * 20) {
291 wpa_printf(MSG_DEBUG, "mesh: pri/sec channels switched");
292 frequency = freq->freq;
293 }
294 wpa_s->assoc_freq = frequency;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800295 wpa_s->current_ssid = ssid;
296
297 /* setup an AP config for auth processing */
298 conf = hostapd_config_defaults();
299 if (!conf)
300 goto out_free;
301
302 bss->conf = *conf->bss;
303 bss->conf->start_disabled = 1;
304 bss->conf->mesh = MESH_ENABLED;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800305 bss->conf->ap_max_inactivity = wpa_s->conf->mesh_max_inactivity;
Hai Shalom74f70d42019-02-11 14:42:39 -0800306
307 if (ieee80211_is_dfs(ssid->frequency, wpa_s->hw.modes,
308 wpa_s->hw.num_modes) && wpa_s->conf->country[0]) {
309 conf->ieee80211h = 1;
310 conf->ieee80211d = 1;
311 conf->country[0] = wpa_s->conf->country[0];
312 conf->country[1] = wpa_s->conf->country[1];
313 conf->country[2] = ' ';
314 }
315
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800316 bss->iconf = conf;
317 ifmsh->conf = conf;
318
319 ifmsh->bss[0]->max_plinks = wpa_s->conf->max_peer_links;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800320 ifmsh->bss[0]->dot11RSNASAERetransPeriod =
321 wpa_s->conf->dot11RSNASAERetransPeriod;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800322 os_strlcpy(bss->conf->iface, wpa_s->ifname, sizeof(bss->conf->iface));
323
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700324 mconf = mesh_config_create(wpa_s, ssid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800325 if (!mconf)
326 goto out_free;
327 ifmsh->mconf = mconf;
328
329 /* need conf->hw_mode for supported rates. */
Dmitry Shmidt29333592017-01-09 12:27:11 -0800330 conf->hw_mode = ieee80211_freq_to_chan(frequency, &conf->channel);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800331 if (conf->hw_mode == NUM_HOSTAPD_MODES) {
332 wpa_printf(MSG_ERROR, "Unsupported mesh mode frequency: %d MHz",
Dmitry Shmidt29333592017-01-09 12:27:11 -0800333 frequency);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800334 goto out_free;
335 }
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -0800336 if (ssid->ht40)
337 conf->secondary_channel = ssid->ht40;
338 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A && ssid->vht) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800339 if (ssid->max_oper_chwidth != DEFAULT_MAX_OPER_CHWIDTH)
340 conf->vht_oper_chwidth = ssid->max_oper_chwidth;
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -0800341 switch (conf->vht_oper_chwidth) {
Hai Shalom81f62d82019-07-22 12:10:00 -0700342 case CHANWIDTH_80MHZ:
343 case CHANWIDTH_80P80MHZ:
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -0800344 ieee80211_freq_to_chan(
Dmitry Shmidt29333592017-01-09 12:27:11 -0800345 frequency,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -0800346 &conf->vht_oper_centr_freq_seg0_idx);
347 conf->vht_oper_centr_freq_seg0_idx += ssid->ht40 * 2;
348 break;
Hai Shalom81f62d82019-07-22 12:10:00 -0700349 case CHANWIDTH_160MHZ:
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -0800350 ieee80211_freq_to_chan(
Dmitry Shmidt29333592017-01-09 12:27:11 -0800351 frequency,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -0800352 &conf->vht_oper_centr_freq_seg0_idx);
353 conf->vht_oper_centr_freq_seg0_idx += ssid->ht40 * 2;
354 conf->vht_oper_centr_freq_seg0_idx += 40 / 5;
355 break;
356 }
357 ieee80211_freq_to_chan(ssid->vht_center_freq2,
358 &conf->vht_oper_centr_freq_seg1_idx);
359 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800360
361 if (ssid->mesh_basic_rates == NULL) {
362 /*
363 * XXX: Hack! This is so an MPM which correctly sets the ERP
364 * mandatory rates as BSSBasicRateSet doesn't reject us. We
365 * could add a new hw_mode HOSTAPD_MODE_IEEE80211G_ERP, but
366 * this is way easier. This also makes our BSSBasicRateSet
367 * advertised in beacons match the one in peering frames, sigh.
368 */
369 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700370 conf->basic_rates = os_memdup(basic_rates_erp,
371 sizeof(basic_rates_erp));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800372 if (!conf->basic_rates)
373 goto out_free;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800374 }
375 } else {
376 rate_len = 0;
377 while (1) {
378 if (ssid->mesh_basic_rates[rate_len] < 1)
379 break;
380 rate_len++;
381 }
382 conf->basic_rates = os_calloc(rate_len + 1, sizeof(int));
383 if (conf->basic_rates == NULL)
384 goto out_free;
385 os_memcpy(conf->basic_rates, ssid->mesh_basic_rates,
386 rate_len * sizeof(int));
387 conf->basic_rates[rate_len] = -1;
388 }
389
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800390 if (wpa_drv_init_mesh(wpa_s)) {
391 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh in driver");
392 return -1;
393 }
394
Hai Shalom74f70d42019-02-11 14:42:39 -0800395 if (hostapd_setup_interface(ifmsh)) {
396 wpa_printf(MSG_ERROR,
397 "Failed to initialize hostapd interface for mesh");
398 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800399 }
400
401 wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf);
402
403 return 0;
404out_free:
405 wpa_supplicant_mesh_deinit(wpa_s);
406 return -ENOMEM;
407}
408
409
410void wpa_mesh_notify_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
411 const u8 *ies, size_t ie_len)
412{
413 struct ieee802_11_elems elems;
414
415 wpa_msg(wpa_s, MSG_INFO,
416 "new peer notification for " MACSTR, MAC2STR(addr));
417
418 if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
419 wpa_msg(wpa_s, MSG_INFO, "Could not parse beacon from " MACSTR,
420 MAC2STR(addr));
421 return;
422 }
423 wpa_mesh_new_mesh_peer(wpa_s, addr, &elems);
424}
425
426
427void wpa_supplicant_mesh_add_scan_ie(struct wpa_supplicant *wpa_s,
428 struct wpabuf **extra_ie)
429{
430 /* EID + 0-length (wildcard) mesh-id */
431 size_t ielen = 2;
432
433 if (wpabuf_resize(extra_ie, ielen) == 0) {
434 wpabuf_put_u8(*extra_ie, WLAN_EID_MESH_ID);
435 wpabuf_put_u8(*extra_ie, 0);
436 }
437}
438
439
440int wpa_supplicant_join_mesh(struct wpa_supplicant *wpa_s,
441 struct wpa_ssid *ssid)
442{
Hai Shalom74f70d42019-02-11 14:42:39 -0800443 struct wpa_driver_mesh_join_params *params = os_zalloc(sizeof(*params));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800444 int ret = 0;
445
Hai Shalom74f70d42019-02-11 14:42:39 -0800446 if (!ssid || !ssid->ssid || !ssid->ssid_len || !ssid->frequency ||
447 !params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800448 ret = -ENOENT;
Hai Shalom74f70d42019-02-11 14:42:39 -0800449 os_free(params);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800450 goto out;
451 }
452
453 wpa_supplicant_mesh_deinit(wpa_s);
454
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700455 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
456 wpa_s->group_cipher = WPA_CIPHER_NONE;
457 wpa_s->mgmt_group_cipher = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800458
Hai Shalom74f70d42019-02-11 14:42:39 -0800459 params->meshid = ssid->ssid;
460 params->meshid_len = ssid->ssid_len;
461 ibss_mesh_setup_freq(wpa_s, ssid, &params->freq);
462 wpa_s->mesh_ht_enabled = !!params->freq.ht_enabled;
463 wpa_s->mesh_vht_enabled = !!params->freq.vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -0700464 wpa_s->mesh_he_enabled = !!params->freq.he_enabled;
Hai Shalom74f70d42019-02-11 14:42:39 -0800465 if (params->freq.ht_enabled && params->freq.sec_channel_offset)
466 ssid->ht40 = params->freq.sec_channel_offset;
467
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -0800468 if (wpa_s->mesh_vht_enabled) {
469 ssid->vht = 1;
Hai Shalom74f70d42019-02-11 14:42:39 -0800470 ssid->vht_center_freq1 = params->freq.center_freq1;
471 switch (params->freq.bandwidth) {
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -0800472 case 80:
Hai Shalom74f70d42019-02-11 14:42:39 -0800473 if (params->freq.center_freq2) {
Hai Shalom81f62d82019-07-22 12:10:00 -0700474 ssid->max_oper_chwidth = CHANWIDTH_80P80MHZ;
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -0800475 ssid->vht_center_freq2 =
Hai Shalom74f70d42019-02-11 14:42:39 -0800476 params->freq.center_freq2;
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -0800477 } else {
Hai Shalom81f62d82019-07-22 12:10:00 -0700478 ssid->max_oper_chwidth = CHANWIDTH_80MHZ;
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -0800479 }
480 break;
481 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -0700482 ssid->max_oper_chwidth = CHANWIDTH_160MHZ;
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -0800483 break;
484 default:
Hai Shalom81f62d82019-07-22 12:10:00 -0700485 ssid->max_oper_chwidth = CHANWIDTH_USE_HT;
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -0800486 break;
487 }
488 }
Hai Shalom81f62d82019-07-22 12:10:00 -0700489 if (wpa_s->mesh_he_enabled)
490 ssid->he = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800491 if (ssid->beacon_int > 0)
Hai Shalom74f70d42019-02-11 14:42:39 -0800492 params->beacon_int = ssid->beacon_int;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800493 else if (wpa_s->conf->beacon_int > 0)
Hai Shalom74f70d42019-02-11 14:42:39 -0800494 params->beacon_int = wpa_s->conf->beacon_int;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700495 if (ssid->dtim_period > 0)
Hai Shalom74f70d42019-02-11 14:42:39 -0800496 params->dtim_period = ssid->dtim_period;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700497 else if (wpa_s->conf->dtim_period > 0)
Hai Shalom74f70d42019-02-11 14:42:39 -0800498 params->dtim_period = wpa_s->conf->dtim_period;
499 params->conf.max_peer_links = wpa_s->conf->max_peer_links;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700500 if (ssid->mesh_rssi_threshold < DEFAULT_MESH_RSSI_THRESHOLD) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800501 params->conf.rssi_threshold = ssid->mesh_rssi_threshold;
502 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700503 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800504
505 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800506 params->flags |= WPA_DRIVER_MESH_FLAG_SAE_AUTH;
507 params->flags |= WPA_DRIVER_MESH_FLAG_AMPE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800508 wpa_s->conf->user_mpm = 1;
509 }
510
511 if (wpa_s->conf->user_mpm) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800512 params->flags |= WPA_DRIVER_MESH_FLAG_USER_MPM;
513 params->conf.auto_plinks = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800514 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -0800515 params->flags |= WPA_DRIVER_MESH_FLAG_DRIVER_MPM;
516 params->conf.auto_plinks = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800517 }
Hai Shalom74f70d42019-02-11 14:42:39 -0800518 params->conf.peer_link_timeout = wpa_s->conf->mesh_max_inactivity;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800519
Hai Shalom74f70d42019-02-11 14:42:39 -0800520 os_free(wpa_s->mesh_params);
521 wpa_s->mesh_params = params;
522 if (wpa_supplicant_mesh_init(wpa_s, ssid, &params->freq)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800523 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh");
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -0700524 wpa_drv_leave_mesh(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800525 ret = -1;
526 goto out;
527 }
528
Hai Shalom74f70d42019-02-11 14:42:39 -0800529 ret = wpas_mesh_complete(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800530out:
531 return ret;
532}
533
534
535int wpa_supplicant_leave_mesh(struct wpa_supplicant *wpa_s)
536{
537 int ret = 0;
538
539 wpa_msg(wpa_s, MSG_INFO, "leaving mesh");
540
541 /* Need to send peering close messages first */
542 wpa_supplicant_mesh_deinit(wpa_s);
543
544 ret = wpa_drv_leave_mesh(wpa_s);
545 if (ret)
546 wpa_msg(wpa_s, MSG_ERROR, "mesh leave error=%d", ret);
547
548 wpa_drv_set_operstate(wpa_s, 1);
549
550 return ret;
551}
552
553
554static int mesh_attr_text(const u8 *ies, size_t ies_len, char *buf, char *end)
555{
556 struct ieee802_11_elems elems;
557 char *mesh_id, *pos = buf;
558 u8 *bss_basic_rate_set;
559 int bss_basic_rate_set_len, ret, i;
560
561 if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) == ParseFailed)
562 return -1;
563
564 if (elems.mesh_id_len < 1)
565 return 0;
566
567 mesh_id = os_malloc(elems.mesh_id_len + 1);
568 if (mesh_id == NULL)
569 return -1;
570
571 os_memcpy(mesh_id, elems.mesh_id, elems.mesh_id_len);
572 mesh_id[elems.mesh_id_len] = '\0';
573 ret = os_snprintf(pos, end - pos, "mesh_id=%s\n", mesh_id);
574 os_free(mesh_id);
575 if (os_snprintf_error(end - pos, ret))
576 return pos - buf;
577 pos += ret;
578
579 if (elems.mesh_config_len > 6) {
580 ret = os_snprintf(pos, end - pos,
581 "active_path_selection_protocol_id=0x%02x\n"
582 "active_path_selection_metric_id=0x%02x\n"
583 "congestion_control_mode_id=0x%02x\n"
584 "synchronization_method_id=0x%02x\n"
585 "authentication_protocol_id=0x%02x\n"
586 "mesh_formation_info=0x%02x\n"
587 "mesh_capability=0x%02x\n",
588 elems.mesh_config[0], elems.mesh_config[1],
589 elems.mesh_config[2], elems.mesh_config[3],
590 elems.mesh_config[4], elems.mesh_config[5],
591 elems.mesh_config[6]);
592 if (os_snprintf_error(end - pos, ret))
593 return pos - buf;
594 pos += ret;
595 }
596
597 bss_basic_rate_set = os_malloc(elems.supp_rates_len +
598 elems.ext_supp_rates_len);
599 if (bss_basic_rate_set == NULL)
600 return -1;
601
602 bss_basic_rate_set_len = 0;
603 for (i = 0; i < elems.supp_rates_len; i++) {
604 if (elems.supp_rates[i] & 0x80) {
605 bss_basic_rate_set[bss_basic_rate_set_len++] =
606 (elems.supp_rates[i] & 0x7f) * 5;
607 }
608 }
609 for (i = 0; i < elems.ext_supp_rates_len; i++) {
610 if (elems.ext_supp_rates[i] & 0x80) {
611 bss_basic_rate_set[bss_basic_rate_set_len++] =
612 (elems.ext_supp_rates[i] & 0x7f) * 5;
613 }
614 }
615 if (bss_basic_rate_set_len > 0) {
616 ret = os_snprintf(pos, end - pos, "bss_basic_rate_set=%d",
617 bss_basic_rate_set[0]);
618 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700619 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800620 pos += ret;
621
622 for (i = 1; i < bss_basic_rate_set_len; i++) {
623 ret = os_snprintf(pos, end - pos, " %d",
624 bss_basic_rate_set[i]);
625 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700626 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800627 pos += ret;
628 }
629
630 ret = os_snprintf(pos, end - pos, "\n");
631 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700632 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800633 pos += ret;
634 }
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700635fail:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800636 os_free(bss_basic_rate_set);
637
638 return pos - buf;
639}
640
641
642int wpas_mesh_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
643 char *end)
644{
645 return mesh_attr_text(ies, ies_len, buf, end);
646}
647
648
649static int wpas_mesh_get_ifname(struct wpa_supplicant *wpa_s, char *ifname,
650 size_t len)
651{
652 char *ifname_ptr = wpa_s->ifname;
653 int res;
654
655 res = os_snprintf(ifname, len, "mesh-%s-%d", ifname_ptr,
656 wpa_s->mesh_if_idx);
657 if (os_snprintf_error(len, res) ||
658 (os_strlen(ifname) >= IFNAMSIZ &&
659 os_strlen(wpa_s->ifname) < IFNAMSIZ)) {
660 /* Try to avoid going over the IFNAMSIZ length limit */
661 res = os_snprintf(ifname, len, "mesh-%d", wpa_s->mesh_if_idx);
662 if (os_snprintf_error(len, res))
663 return -1;
664 }
665 wpa_s->mesh_if_idx++;
666 return 0;
667}
668
669
670int wpas_mesh_add_interface(struct wpa_supplicant *wpa_s, char *ifname,
671 size_t len)
672{
673 struct wpa_interface iface;
674 struct wpa_supplicant *mesh_wpa_s;
675 u8 addr[ETH_ALEN];
676
677 if (ifname[0] == '\0' && wpas_mesh_get_ifname(wpa_s, ifname, len) < 0)
678 return -1;
679
680 if (wpa_drv_if_add(wpa_s, WPA_IF_MESH, ifname, NULL, NULL, NULL, addr,
681 NULL) < 0) {
682 wpa_printf(MSG_ERROR,
683 "mesh: Failed to create new mesh interface");
684 return -1;
685 }
686 wpa_printf(MSG_INFO, "mesh: Created virtual interface %s addr "
687 MACSTR, ifname, MAC2STR(addr));
688
689 os_memset(&iface, 0, sizeof(iface));
690 iface.ifname = ifname;
691 iface.driver = wpa_s->driver->name;
692 iface.driver_param = wpa_s->conf->driver_param;
693 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
694
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800695 mesh_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800696 if (!mesh_wpa_s) {
697 wpa_printf(MSG_ERROR,
698 "mesh: Failed to create new wpa_supplicant interface");
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700699 wpa_drv_if_remove(wpa_s, WPA_IF_MESH, ifname);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800700 return -1;
701 }
702 mesh_wpa_s->mesh_if_created = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800703 return 0;
704}
Dmitry Shmidte4663042016-04-04 10:07:49 -0700705
706
707int wpas_mesh_peer_remove(struct wpa_supplicant *wpa_s, const u8 *addr)
708{
709 return mesh_mpm_close_peer(wpa_s, addr);
710}
711
712
713int wpas_mesh_peer_add(struct wpa_supplicant *wpa_s, const u8 *addr,
714 int duration)
715{
716 return mesh_mpm_connect_peer(wpa_s, addr, duration);
717}