]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/mesh.c
Fix signal_poll based roaming skip
[thirdparty/hostap.git] / wpa_supplicant / mesh.c
CommitLineData
8319e312
TP
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 "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"
5cfb672d 24#include "ap.h"
5f92659d 25#include "mesh_mpm.h"
0f950df0 26#include "mesh_rsn.h"
8319e312
TP
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;
0f950df0
TP
35 os_free(wpa_s->mesh_rsn);
36 wpa_s->mesh_rsn = NULL;
0daa7b75
PO
37 os_free(wpa_s->mesh_params);
38 wpa_s->mesh_params = NULL;
8319e312
TP
39 /* 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) {
5f92659d 51 mesh_mpm_deinit(wpa_s, ifmsh);
8a51dcbc
MH
52 if (ifmsh->mconf->rsn_ie) {
53 ifmsh->mconf->rsn_ie = NULL;
8319e312
TP
54 /* 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
18aca1a0
JM
71static struct mesh_conf * mesh_config_create(struct wpa_supplicant *wpa_s,
72 struct wpa_ssid *ssid)
8319e312
TP
73{
74 struct mesh_conf *conf;
a151b0e3 75 int cipher;
8319e312
TP
76
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;
18aca1a0
JM
89 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 }
716ed96e
MV
96#ifdef CONFIG_OCV
97 conf->ocv = ssid->ocv;
98#endif /* CONFIG_OCV */
a151b0e3
JM
99
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;
9b391715
JM
117 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 }
8319e312
TP
125
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;
e6096799
MH
132 conf->dot11MeshMaxRetries = ssid->dot11MeshMaxRetries;
133 conf->dot11MeshRetryTimeout = ssid->dot11MeshRetryTimeout;
134 conf->dot11MeshConfirmTimeout = ssid->dot11MeshConfirmTimeout;
135 conf->dot11MeshHoldingTimeout = ssid->dot11MeshHoldingTimeout;
8319e312
TP
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
18778429
PO
159static 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
0daa7b75
PO
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
0f9632ce 204 if (!params || !ssid || !ifmsh) {
0daa7b75
PO
205 wpa_printf(MSG_ERROR, "mesh: %s called without active mesh",
206 __func__);
207 return -1;
208 }
209
4b5453ce
PO
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);
0de46d8a 215 wpa_drv_leave_mesh(wpa_s);
4b5453ce
PO
216 return -1;
217 }
218
0daa7b75
PO
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
0f9632ce
JM
225 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;
0daa7b75
PO
230
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
8319e312 247static int wpa_supplicant_mesh_init(struct wpa_supplicant *wpa_s,
274e76f2
JM
248 struct wpa_ssid *ssid,
249 struct hostapd_freq_params *freq)
8319e312
TP
250{
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 };
2b2bb5a8 256 int rate_len;
3e949655 257 int frequency;
8319e312 258
5f92659d
BC
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
8fd29a04 266 wpa_s->ifmsh = ifmsh = hostapd_alloc_iface();
8319e312
TP
267 if (!ifmsh)
268 return -ENOMEM;
269
edfefaed 270 ifmsh->drv_flags = wpa_s->drv_flags;
8319e312
TP
271 ifmsh->num_bss = 1;
272 ifmsh->bss = os_calloc(wpa_s->ifmsh->num_bss,
273 sizeof(struct hostapd_data *));
274 if (!ifmsh->bss)
275 goto out_free;
276
db5e53cb 277 ifmsh->bss[0] = bss = hostapd_alloc_bss_data(NULL, NULL, NULL);
8319e312
TP
278 if (!bss)
279 goto out_free;
280
d6a65a83 281 ifmsh->bss[0]->msg_ctx = wpa_s;
8319e312
TP
282 os_memcpy(bss->own_addr, wpa_s->own_addr, ETH_ALEN);
283 bss->driver = wpa_s->driver;
284 bss->drv_priv = wpa_s->drv_priv;
285 bss->iface = ifmsh;
c596f3f0 286 bss->mesh_sta_free_cb = mesh_mpm_free_sta;
274e76f2
JM
287 frequency = ssid->frequency;
288 if (frequency != freq->freq &&
289 frequency == freq->freq + freq->sec_channel_offset * 20) {
290 wpa_printf(MSG_DEBUG, "mesh: pri/sec channels switched");
291 frequency = freq->freq;
292 }
293 wpa_s->assoc_freq = frequency;
8319e312
TP
294 wpa_s->current_ssid = ssid;
295
296 /* setup an AP config for auth processing */
297 conf = hostapd_config_defaults();
298 if (!conf)
299 goto out_free;
300
301 bss->conf = *conf->bss;
302 bss->conf->start_disabled = 1;
303 bss->conf->mesh = MESH_ENABLED;
5a2a6de6 304 bss->conf->ap_max_inactivity = wpa_s->conf->mesh_max_inactivity;
fed51174
PO
305
306 if (ieee80211_is_dfs(ssid->frequency, wpa_s->hw.modes,
307 wpa_s->hw.num_modes) && wpa_s->conf->country[0]) {
308 conf->ieee80211h = 1;
309 conf->ieee80211d = 1;
310 conf->country[0] = wpa_s->conf->country[0];
311 conf->country[1] = wpa_s->conf->country[1];
312 conf->country[2] = ' ';
313 }
314
8319e312
TP
315 bss->iconf = conf;
316 ifmsh->conf = conf;
317
4b409368 318 ifmsh->bss[0]->max_plinks = wpa_s->conf->max_peer_links;
ecd40fef
MH
319 ifmsh->bss[0]->dot11RSNASAERetransPeriod =
320 wpa_s->conf->dot11RSNASAERetransPeriod;
8319e312
TP
321 os_strlcpy(bss->conf->iface, wpa_s->ifname, sizeof(bss->conf->iface));
322
18aca1a0 323 mconf = mesh_config_create(wpa_s, ssid);
8319e312
TP
324 if (!mconf)
325 goto out_free;
326 ifmsh->mconf = mconf;
327
328 /* need conf->hw_mode for supported rates. */
274e76f2 329 conf->hw_mode = ieee80211_freq_to_chan(frequency, &conf->channel);
8319e312
TP
330 if (conf->hw_mode == NUM_HOSTAPD_MODES) {
331 wpa_printf(MSG_ERROR, "Unsupported mesh mode frequency: %d MHz",
274e76f2 332 frequency);
8319e312
TP
333 goto out_free;
334 }
d06a3505
JM
335 if (ssid->ht40)
336 conf->secondary_channel = ssid->ht40;
337 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A && ssid->vht) {
806db174
PO
338 if (ssid->max_oper_chwidth != DEFAULT_MAX_OPER_CHWIDTH)
339 conf->vht_oper_chwidth = ssid->max_oper_chwidth;
d06a3505 340 switch (conf->vht_oper_chwidth) {
464dcfd0
JC
341 case CHANWIDTH_80MHZ:
342 case CHANWIDTH_80P80MHZ:
d06a3505 343 ieee80211_freq_to_chan(
274e76f2 344 frequency,
d06a3505
JM
345 &conf->vht_oper_centr_freq_seg0_idx);
346 conf->vht_oper_centr_freq_seg0_idx += ssid->ht40 * 2;
347 break;
464dcfd0 348 case CHANWIDTH_160MHZ:
d06a3505 349 ieee80211_freq_to_chan(
274e76f2 350 frequency,
d06a3505
JM
351 &conf->vht_oper_centr_freq_seg0_idx);
352 conf->vht_oper_centr_freq_seg0_idx += ssid->ht40 * 2;
353 conf->vht_oper_centr_freq_seg0_idx += 40 / 5;
354 break;
355 }
356 ieee80211_freq_to_chan(ssid->vht_center_freq2,
357 &conf->vht_oper_centr_freq_seg1_idx);
358 }
8319e312 359
2b2bb5a8
MH
360 if (ssid->mesh_basic_rates == NULL) {
361 /*
362 * XXX: Hack! This is so an MPM which correctly sets the ERP
363 * mandatory rates as BSSBasicRateSet doesn't reject us. We
364 * could add a new hw_mode HOSTAPD_MODE_IEEE80211G_ERP, but
365 * this is way easier. This also makes our BSSBasicRateSet
366 * advertised in beacons match the one in peering frames, sigh.
367 */
368 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
a1f11e34
JB
369 conf->basic_rates = os_memdup(basic_rates_erp,
370 sizeof(basic_rates_erp));
2b2bb5a8
MH
371 if (!conf->basic_rates)
372 goto out_free;
2b2bb5a8
MH
373 }
374 } else {
375 rate_len = 0;
376 while (1) {
377 if (ssid->mesh_basic_rates[rate_len] < 1)
378 break;
379 rate_len++;
380 }
381 conf->basic_rates = os_calloc(rate_len + 1, sizeof(int));
382 if (conf->basic_rates == NULL)
8319e312 383 goto out_free;
2b2bb5a8
MH
384 os_memcpy(conf->basic_rates, ssid->mesh_basic_rates,
385 rate_len * sizeof(int));
386 conf->basic_rates[rate_len] = -1;
8319e312
TP
387 }
388
3ba4a25e
PO
389 if (wpa_drv_init_mesh(wpa_s)) {
390 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh in driver");
8319e312
TP
391 return -1;
392 }
393
3ba4a25e
PO
394 if (hostapd_setup_interface(ifmsh)) {
395 wpa_printf(MSG_ERROR,
396 "Failed to initialize hostapd interface for mesh");
8319e312
TP
397 return -1;
398 }
399
3e949655
MH
400 wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf);
401
8319e312
TP
402 return 0;
403out_free:
404 wpa_supplicant_mesh_deinit(wpa_s);
405 return -ENOMEM;
406}
407
408
409void wpa_mesh_notify_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
410 const u8 *ies, size_t ie_len)
411{
412 struct ieee802_11_elems elems;
413
414 wpa_msg(wpa_s, MSG_INFO,
415 "new peer notification for " MACSTR, MAC2STR(addr));
416
417 if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
418 wpa_msg(wpa_s, MSG_INFO, "Could not parse beacon from " MACSTR,
419 MAC2STR(addr));
5f92659d 420 return;
8319e312 421 }
5f92659d 422 wpa_mesh_new_mesh_peer(wpa_s, addr, &elems);
8319e312
TP
423}
424
425
fbca4c89
JA
426void wpa_supplicant_mesh_add_scan_ie(struct wpa_supplicant *wpa_s,
427 struct wpabuf **extra_ie)
428{
429 /* EID + 0-length (wildcard) mesh-id */
430 size_t ielen = 2;
431
432 if (wpabuf_resize(extra_ie, ielen) == 0) {
433 wpabuf_put_u8(*extra_ie, WLAN_EID_MESH_ID);
434 wpabuf_put_u8(*extra_ie, 0);
435 }
436}
437
438
8319e312
TP
439int wpa_supplicant_join_mesh(struct wpa_supplicant *wpa_s,
440 struct wpa_ssid *ssid)
441{
0daa7b75 442 struct wpa_driver_mesh_join_params *params = os_zalloc(sizeof(*params));
8319e312
TP
443 int ret = 0;
444
0daa7b75
PO
445 if (!ssid || !ssid->ssid || !ssid->ssid_len || !ssid->frequency ||
446 !params) {
8319e312 447 ret = -ENOENT;
0daa7b75 448 os_free(params);
8319e312
TP
449 goto out;
450 }
451
452 wpa_supplicant_mesh_deinit(wpa_s);
453
3b6deac0
JM
454 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
455 wpa_s->group_cipher = WPA_CIPHER_NONE;
456 wpa_s->mgmt_group_cipher = 0;
0530eb10 457
0daa7b75
PO
458 params->meshid = ssid->ssid;
459 params->meshid_len = ssid->ssid_len;
460 ibss_mesh_setup_freq(wpa_s, ssid, &params->freq);
461 wpa_s->mesh_ht_enabled = !!params->freq.ht_enabled;
462 wpa_s->mesh_vht_enabled = !!params->freq.vht_enabled;
3459c54a 463 wpa_s->mesh_he_enabled = !!params->freq.he_enabled;
0daa7b75
PO
464 if (params->freq.ht_enabled && params->freq.sec_channel_offset)
465 ssid->ht40 = params->freq.sec_channel_offset;
466
d06a3505
JM
467 if (wpa_s->mesh_vht_enabled) {
468 ssid->vht = 1;
c95619c2 469 ssid->vht_center_freq1 = params->freq.center_freq1;
0daa7b75 470 switch (params->freq.bandwidth) {
d06a3505 471 case 80:
0daa7b75 472 if (params->freq.center_freq2) {
464dcfd0 473 ssid->max_oper_chwidth = CHANWIDTH_80P80MHZ;
d06a3505 474 ssid->vht_center_freq2 =
0daa7b75 475 params->freq.center_freq2;
d06a3505 476 } else {
464dcfd0 477 ssid->max_oper_chwidth = CHANWIDTH_80MHZ;
d06a3505
JM
478 }
479 break;
480 case 160:
464dcfd0 481 ssid->max_oper_chwidth = CHANWIDTH_160MHZ;
d06a3505
JM
482 break;
483 default:
464dcfd0 484 ssid->max_oper_chwidth = CHANWIDTH_USE_HT;
d06a3505
JM
485 break;
486 }
487 }
3459c54a
SE
488 if (wpa_s->mesh_he_enabled)
489 ssid->he = 1;
9c58c5f7 490 if (ssid->beacon_int > 0)
0daa7b75 491 params->beacon_int = ssid->beacon_int;
9c58c5f7 492 else if (wpa_s->conf->beacon_int > 0)
0daa7b75 493 params->beacon_int = wpa_s->conf->beacon_int;
4ac2ea57 494 if (ssid->dtim_period > 0)
0daa7b75 495 params->dtim_period = ssid->dtim_period;
4ac2ea57 496 else if (wpa_s->conf->dtim_period > 0)
0daa7b75
PO
497 params->dtim_period = wpa_s->conf->dtim_period;
498 params->conf.max_peer_links = wpa_s->conf->max_peer_links;
31a856a1 499 if (ssid->mesh_rssi_threshold < DEFAULT_MESH_RSSI_THRESHOLD) {
0daa7b75
PO
500 params->conf.rssi_threshold = ssid->mesh_rssi_threshold;
501 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD;
31a856a1 502 }
8319e312
TP
503
504 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
0daa7b75
PO
505 params->flags |= WPA_DRIVER_MESH_FLAG_SAE_AUTH;
506 params->flags |= WPA_DRIVER_MESH_FLAG_AMPE;
5f92659d 507 wpa_s->conf->user_mpm = 1;
8319e312
TP
508 }
509
5f92659d 510 if (wpa_s->conf->user_mpm) {
0daa7b75
PO
511 params->flags |= WPA_DRIVER_MESH_FLAG_USER_MPM;
512 params->conf.auto_plinks = 0;
5f92659d 513 } else {
0daa7b75
PO
514 params->flags |= WPA_DRIVER_MESH_FLAG_DRIVER_MPM;
515 params->conf.auto_plinks = 1;
5f92659d 516 }
0daa7b75 517 params->conf.peer_link_timeout = wpa_s->conf->mesh_max_inactivity;
8319e312 518
0daa7b75
PO
519 os_free(wpa_s->mesh_params);
520 wpa_s->mesh_params = params;
521 if (wpa_supplicant_mesh_init(wpa_s, ssid, &params->freq)) {
8319e312 522 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh");
41312fc7 523 wpa_drv_leave_mesh(wpa_s);
8319e312
TP
524 ret = -1;
525 goto out;
526 }
527
0daa7b75 528 ret = wpas_mesh_complete(wpa_s);
8319e312
TP
529out:
530 return ret;
531}
532
533
534int wpa_supplicant_leave_mesh(struct wpa_supplicant *wpa_s)
535{
536 int ret = 0;
537
538 wpa_msg(wpa_s, MSG_INFO, "leaving mesh");
539
4e0990dc
JM
540 /* Need to send peering close messages first */
541 wpa_supplicant_mesh_deinit(wpa_s);
542
8319e312
TP
543 ret = wpa_drv_leave_mesh(wpa_s);
544 if (ret)
545 wpa_msg(wpa_s, MSG_ERROR, "mesh leave error=%d", ret);
546
547 wpa_drv_set_operstate(wpa_s, 1);
548
8319e312
TP
549 return ret;
550}
79070906
MH
551
552
553static int mesh_attr_text(const u8 *ies, size_t ies_len, char *buf, char *end)
554{
555 struct ieee802_11_elems elems;
556 char *mesh_id, *pos = buf;
557 u8 *bss_basic_rate_set;
558 int bss_basic_rate_set_len, ret, i;
559
560 if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) == ParseFailed)
561 return -1;
562
563 if (elems.mesh_id_len < 1)
564 return 0;
565
566 mesh_id = os_malloc(elems.mesh_id_len + 1);
567 if (mesh_id == NULL)
568 return -1;
569
570 os_memcpy(mesh_id, elems.mesh_id, elems.mesh_id_len);
571 mesh_id[elems.mesh_id_len] = '\0';
572 ret = os_snprintf(pos, end - pos, "mesh_id=%s\n", mesh_id);
573 os_free(mesh_id);
d85e1fc8 574 if (os_snprintf_error(end - pos, ret))
79070906
MH
575 return pos - buf;
576 pos += ret;
577
578 if (elems.mesh_config_len > 6) {
579 ret = os_snprintf(pos, end - pos,
580 "active_path_selection_protocol_id=0x%02x\n"
581 "active_path_selection_metric_id=0x%02x\n"
582 "congestion_control_mode_id=0x%02x\n"
583 "synchronization_method_id=0x%02x\n"
584 "authentication_protocol_id=0x%02x\n"
585 "mesh_formation_info=0x%02x\n"
586 "mesh_capability=0x%02x\n",
587 elems.mesh_config[0], elems.mesh_config[1],
588 elems.mesh_config[2], elems.mesh_config[3],
589 elems.mesh_config[4], elems.mesh_config[5],
590 elems.mesh_config[6]);
d85e1fc8 591 if (os_snprintf_error(end - pos, ret))
79070906
MH
592 return pos - buf;
593 pos += ret;
594 }
595
596 bss_basic_rate_set = os_malloc(elems.supp_rates_len +
597 elems.ext_supp_rates_len);
598 if (bss_basic_rate_set == NULL)
599 return -1;
600
601 bss_basic_rate_set_len = 0;
602 for (i = 0; i < elems.supp_rates_len; i++) {
603 if (elems.supp_rates[i] & 0x80) {
604 bss_basic_rate_set[bss_basic_rate_set_len++] =
605 (elems.supp_rates[i] & 0x7f) * 5;
606 }
607 }
608 for (i = 0; i < elems.ext_supp_rates_len; i++) {
609 if (elems.ext_supp_rates[i] & 0x80) {
610 bss_basic_rate_set[bss_basic_rate_set_len++] =
611 (elems.ext_supp_rates[i] & 0x7f) * 5;
612 }
613 }
614 if (bss_basic_rate_set_len > 0) {
615 ret = os_snprintf(pos, end - pos, "bss_basic_rate_set=%d",
616 bss_basic_rate_set[0]);
d85e1fc8 617 if (os_snprintf_error(end - pos, ret))
c9bf7b66 618 goto fail;
79070906
MH
619 pos += ret;
620
621 for (i = 1; i < bss_basic_rate_set_len; i++) {
622 ret = os_snprintf(pos, end - pos, " %d",
623 bss_basic_rate_set[i]);
d85e1fc8 624 if (os_snprintf_error(end - pos, ret))
c9bf7b66 625 goto fail;
79070906
MH
626 pos += ret;
627 }
628
629 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 630 if (os_snprintf_error(end - pos, ret))
c9bf7b66 631 goto fail;
79070906
MH
632 pos += ret;
633 }
c9bf7b66 634fail:
79070906
MH
635 os_free(bss_basic_rate_set);
636
637 return pos - buf;
638}
639
640
641int wpas_mesh_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
642 char *end)
643{
644 return mesh_attr_text(ies, ies_len, buf, end);
645}
5b78493f
MH
646
647
648static int wpas_mesh_get_ifname(struct wpa_supplicant *wpa_s, char *ifname,
649 size_t len)
650{
651 char *ifname_ptr = wpa_s->ifname;
652 int res;
653
654 res = os_snprintf(ifname, len, "mesh-%s-%d", ifname_ptr,
655 wpa_s->mesh_if_idx);
656 if (os_snprintf_error(len, res) ||
657 (os_strlen(ifname) >= IFNAMSIZ &&
658 os_strlen(wpa_s->ifname) < IFNAMSIZ)) {
659 /* Try to avoid going over the IFNAMSIZ length limit */
660 res = os_snprintf(ifname, len, "mesh-%d", wpa_s->mesh_if_idx);
661 if (os_snprintf_error(len, res))
662 return -1;
663 }
664 wpa_s->mesh_if_idx++;
665 return 0;
666}
667
668
669int wpas_mesh_add_interface(struct wpa_supplicant *wpa_s, char *ifname,
670 size_t len)
671{
672 struct wpa_interface iface;
673 struct wpa_supplicant *mesh_wpa_s;
674 u8 addr[ETH_ALEN];
675
676 if (ifname[0] == '\0' && wpas_mesh_get_ifname(wpa_s, ifname, len) < 0)
677 return -1;
678
679 if (wpa_drv_if_add(wpa_s, WPA_IF_MESH, ifname, NULL, NULL, NULL, addr,
680 NULL) < 0) {
681 wpa_printf(MSG_ERROR,
682 "mesh: Failed to create new mesh interface");
683 return -1;
684 }
685 wpa_printf(MSG_INFO, "mesh: Created virtual interface %s addr "
686 MACSTR, ifname, MAC2STR(addr));
687
688 os_memset(&iface, 0, sizeof(iface));
689 iface.ifname = ifname;
690 iface.driver = wpa_s->driver->name;
691 iface.driver_param = wpa_s->conf->driver_param;
692 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
693
1772d348 694 mesh_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
5b78493f
MH
695 if (!mesh_wpa_s) {
696 wpa_printf(MSG_ERROR,
697 "mesh: Failed to create new wpa_supplicant interface");
62fc8e6a 698 wpa_drv_if_remove(wpa_s, WPA_IF_MESH, ifname);
5b78493f
MH
699 return -1;
700 }
701 mesh_wpa_s->mesh_if_created = 1;
5b78493f
MH
702 return 0;
703}
e174ef34
MH
704
705
706int wpas_mesh_peer_remove(struct wpa_supplicant *wpa_s, const u8 *addr)
707{
708 return mesh_mpm_close_peer(wpa_s, addr);
709}
2604edbf
MH
710
711
9f2cf23e
MH
712int wpas_mesh_peer_add(struct wpa_supplicant *wpa_s, const u8 *addr,
713 int duration)
2604edbf 714{
9f2cf23e 715 return mesh_mpm_connect_peer(wpa_s, addr, duration);
2604edbf 716}