]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/mesh.c
tests: Mesh beacon_int parameter
[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;
8319e312
TP
37 /* TODO: leave mesh (stop beacon). This will happen on link down
38 * anyway, so it's not urgent */
39}
40
41
42void wpa_supplicant_mesh_iface_deinit(struct wpa_supplicant *wpa_s,
43 struct hostapd_iface *ifmsh)
44{
45 if (!ifmsh)
46 return;
47
48 if (ifmsh->mconf) {
5f92659d 49 mesh_mpm_deinit(wpa_s, ifmsh);
8319e312
TP
50 if (ifmsh->mconf->ies) {
51 ifmsh->mconf->ies = NULL;
52 /* We cannot free this struct
53 * because wpa_authenticator on
54 * hostapd side is also using it
55 * for now just set to NULL and
56 * let hostapd code free it.
57 */
58 }
59 os_free(ifmsh->mconf);
60 ifmsh->mconf = NULL;
61 }
62
63 /* take care of shared data */
64 hostapd_interface_deinit(ifmsh);
65 hostapd_interface_free(ifmsh);
66}
67
68
69static struct mesh_conf * mesh_config_create(struct wpa_ssid *ssid)
70{
71 struct mesh_conf *conf;
72
73 conf = os_zalloc(sizeof(struct mesh_conf));
74 if (!conf)
75 return NULL;
76
77 os_memcpy(conf->meshid, ssid->ssid, ssid->ssid_len);
78 conf->meshid_len = ssid->ssid_len;
79
80 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE)
81 conf->security |= MESH_CONF_SEC_AUTH |
82 MESH_CONF_SEC_AMPE;
83 else
84 conf->security |= MESH_CONF_SEC_NONE;
85
86 /* defaults */
87 conf->mesh_pp_id = MESH_PATH_PROTOCOL_HWMP;
88 conf->mesh_pm_id = MESH_PATH_METRIC_AIRTIME;
89 conf->mesh_cc_id = 0;
90 conf->mesh_sp_id = MESH_SYNC_METHOD_NEIGHBOR_OFFSET;
91 conf->mesh_auth_id = (conf->security & MESH_CONF_SEC_AUTH) ? 1 : 0;
e6096799
MH
92 conf->dot11MeshMaxRetries = ssid->dot11MeshMaxRetries;
93 conf->dot11MeshRetryTimeout = ssid->dot11MeshRetryTimeout;
94 conf->dot11MeshConfirmTimeout = ssid->dot11MeshConfirmTimeout;
95 conf->dot11MeshHoldingTimeout = ssid->dot11MeshHoldingTimeout;
8319e312
TP
96
97 return conf;
98}
99
100
101static void wpas_mesh_copy_groups(struct hostapd_data *bss,
102 struct wpa_supplicant *wpa_s)
103{
104 int num_groups;
105 size_t groups_size;
106
107 for (num_groups = 0; wpa_s->conf->sae_groups[num_groups] > 0;
108 num_groups++)
109 ;
110
111 groups_size = (num_groups + 1) * sizeof(wpa_s->conf->sae_groups[0]);
112 bss->conf->sae_groups = os_malloc(groups_size);
113 if (bss->conf->sae_groups)
114 os_memcpy(bss->conf->sae_groups, wpa_s->conf->sae_groups,
115 groups_size);
116}
117
118
119static int wpa_supplicant_mesh_init(struct wpa_supplicant *wpa_s,
120 struct wpa_ssid *ssid)
121{
122 struct hostapd_iface *ifmsh;
123 struct hostapd_data *bss;
124 struct hostapd_config *conf;
125 struct mesh_conf *mconf;
126 int basic_rates_erp[] = { 10, 20, 55, 60, 110, 120, 240, -1 };
127 static int default_groups[] = { 19, 20, 21, 25, 26, -1 };
128 size_t len;
2b2bb5a8 129 int rate_len;
8319e312 130
5f92659d
BC
131 if (!wpa_s->conf->user_mpm) {
132 /* not much for us to do here */
133 wpa_msg(wpa_s, MSG_WARNING,
134 "user_mpm is not enabled in configuration");
135 return 0;
136 }
137
8319e312
TP
138 wpa_s->ifmsh = ifmsh = os_zalloc(sizeof(*wpa_s->ifmsh));
139 if (!ifmsh)
140 return -ENOMEM;
141
edfefaed 142 ifmsh->drv_flags = wpa_s->drv_flags;
8319e312
TP
143 ifmsh->num_bss = 1;
144 ifmsh->bss = os_calloc(wpa_s->ifmsh->num_bss,
145 sizeof(struct hostapd_data *));
146 if (!ifmsh->bss)
147 goto out_free;
148
149 ifmsh->bss[0] = bss = os_zalloc(sizeof(struct hostapd_data));
150 if (!bss)
151 goto out_free;
152
153 os_memcpy(bss->own_addr, wpa_s->own_addr, ETH_ALEN);
154 bss->driver = wpa_s->driver;
155 bss->drv_priv = wpa_s->drv_priv;
156 bss->iface = ifmsh;
c596f3f0 157 bss->mesh_sta_free_cb = mesh_mpm_free_sta;
8319e312
TP
158 wpa_s->assoc_freq = ssid->frequency;
159 wpa_s->current_ssid = ssid;
160
161 /* setup an AP config for auth processing */
162 conf = hostapd_config_defaults();
163 if (!conf)
164 goto out_free;
165
166 bss->conf = *conf->bss;
167 bss->conf->start_disabled = 1;
168 bss->conf->mesh = MESH_ENABLED;
169 bss->iconf = conf;
170 ifmsh->conf = conf;
171
5f92659d 172 ifmsh->bss[0]->max_plinks = 99;
8319e312
TP
173 os_strlcpy(bss->conf->iface, wpa_s->ifname, sizeof(bss->conf->iface));
174
175 mconf = mesh_config_create(ssid);
176 if (!mconf)
177 goto out_free;
178 ifmsh->mconf = mconf;
179
180 /* need conf->hw_mode for supported rates. */
181 if (ssid->frequency == 0) {
182 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
183 conf->channel = 1;
184 } else {
185 conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
186 &conf->channel);
187 }
188 if (conf->hw_mode == NUM_HOSTAPD_MODES) {
189 wpa_printf(MSG_ERROR, "Unsupported mesh mode frequency: %d MHz",
190 ssid->frequency);
191 goto out_free;
192 }
193
2b2bb5a8
MH
194 if (ssid->mesh_basic_rates == NULL) {
195 /*
196 * XXX: Hack! This is so an MPM which correctly sets the ERP
197 * mandatory rates as BSSBasicRateSet doesn't reject us. We
198 * could add a new hw_mode HOSTAPD_MODE_IEEE80211G_ERP, but
199 * this is way easier. This also makes our BSSBasicRateSet
200 * advertised in beacons match the one in peering frames, sigh.
201 */
202 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
203 conf->basic_rates = os_malloc(sizeof(basic_rates_erp));
204 if (!conf->basic_rates)
205 goto out_free;
206 os_memcpy(conf->basic_rates, basic_rates_erp,
207 sizeof(basic_rates_erp));
208 }
209 } else {
210 rate_len = 0;
211 while (1) {
212 if (ssid->mesh_basic_rates[rate_len] < 1)
213 break;
214 rate_len++;
215 }
216 conf->basic_rates = os_calloc(rate_len + 1, sizeof(int));
217 if (conf->basic_rates == NULL)
8319e312 218 goto out_free;
2b2bb5a8
MH
219 os_memcpy(conf->basic_rates, ssid->mesh_basic_rates,
220 rate_len * sizeof(int));
221 conf->basic_rates[rate_len] = -1;
8319e312
TP
222 }
223
224 if (hostapd_setup_interface(ifmsh)) {
225 wpa_printf(MSG_ERROR,
226 "Failed to initialize hostapd interface for mesh");
227 return -1;
228 }
229
230 if (wpa_drv_init_mesh(wpa_s)) {
231 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh in driver");
232 return -1;
233 }
234
235 if (mconf->security != MESH_CONF_SEC_NONE) {
236 if (ssid->passphrase == NULL) {
237 wpa_printf(MSG_ERROR,
238 "mesh: Passphrase for SAE not configured");
239 goto out_free;
240 }
241
242 bss->conf->wpa = ssid->proto;
243 bss->conf->wpa_key_mgmt = ssid->key_mgmt;
244
245 if (wpa_s->conf->sae_groups &&
246 wpa_s->conf->sae_groups[0] > 0) {
247 wpas_mesh_copy_groups(bss, wpa_s);
248 } else {
249 bss->conf->sae_groups =
250 os_malloc(sizeof(default_groups));
251 if (!bss->conf->sae_groups)
252 goto out_free;
253 os_memcpy(bss->conf->sae_groups, default_groups,
254 sizeof(default_groups));
255 }
256
257 len = os_strlen(ssid->passphrase);
258 bss->conf->ssid.wpa_passphrase =
259 dup_binstr(ssid->passphrase, len);
0f950df0
TP
260
261 wpa_s->mesh_rsn = mesh_rsn_auth_init(wpa_s, mconf);
262 if (!wpa_s->mesh_rsn)
263 goto out_free;
8319e312
TP
264 }
265
5cfb672d
JM
266 wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf);
267
8319e312
TP
268 return 0;
269out_free:
270 wpa_supplicant_mesh_deinit(wpa_s);
271 return -ENOMEM;
272}
273
274
275void wpa_mesh_notify_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
276 const u8 *ies, size_t ie_len)
277{
278 struct ieee802_11_elems elems;
279
280 wpa_msg(wpa_s, MSG_INFO,
281 "new peer notification for " MACSTR, MAC2STR(addr));
282
283 if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
284 wpa_msg(wpa_s, MSG_INFO, "Could not parse beacon from " MACSTR,
285 MAC2STR(addr));
5f92659d 286 return;
8319e312 287 }
5f92659d 288 wpa_mesh_new_mesh_peer(wpa_s, addr, &elems);
8319e312
TP
289}
290
291
fbca4c89
JA
292void wpa_supplicant_mesh_add_scan_ie(struct wpa_supplicant *wpa_s,
293 struct wpabuf **extra_ie)
294{
295 /* EID + 0-length (wildcard) mesh-id */
296 size_t ielen = 2;
297
298 if (wpabuf_resize(extra_ie, ielen) == 0) {
299 wpabuf_put_u8(*extra_ie, WLAN_EID_MESH_ID);
300 wpabuf_put_u8(*extra_ie, 0);
301 }
302}
303
304
8319e312
TP
305int wpa_supplicant_join_mesh(struct wpa_supplicant *wpa_s,
306 struct wpa_ssid *ssid)
307{
308 struct wpa_driver_mesh_join_params params;
309 int ret = 0;
310
311 if (!ssid || !ssid->ssid || !ssid->ssid_len || !ssid->frequency) {
312 ret = -ENOENT;
313 goto out;
314 }
315
316 wpa_supplicant_mesh_deinit(wpa_s);
317
318 os_memset(&params, 0, sizeof(params));
319 params.meshid = ssid->ssid;
320 params.meshid_len = ssid->ssid_len;
321 params.freq = ssid->frequency;
9c58c5f7
MH
322 if (ssid->beacon_int > 0)
323 params.beacon_int = ssid->beacon_int;
324 else if (wpa_s->conf->beacon_int > 0)
325 params.beacon_int = wpa_s->conf->beacon_int;
5cfb672d
JM
326#ifdef CONFIG_IEEE80211N
327 params.ht_mode = ssid->mesh_ht_mode;
328#endif /* CONFIG_IEEE80211N */
8319e312
TP
329
330 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
331 params.flags |= WPA_DRIVER_MESH_FLAG_SAE_AUTH;
332 params.flags |= WPA_DRIVER_MESH_FLAG_AMPE;
5f92659d 333 wpa_s->conf->user_mpm = 1;
8319e312
TP
334 }
335
5f92659d
BC
336 if (wpa_s->conf->user_mpm) {
337 params.flags |= WPA_DRIVER_MESH_FLAG_USER_MPM;
338 params.conf.flags &= ~WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
339 } else {
340 params.flags |= WPA_DRIVER_MESH_FLAG_DRIVER_MPM;
341 params.conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
342 }
8319e312
TP
343
344 if (wpa_supplicant_mesh_init(wpa_s, ssid)) {
345 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh");
346 ret = -1;
347 goto out;
348 }
349
350 if (wpa_s->ifmsh) {
351 params.ies = wpa_s->ifmsh->mconf->ies;
352 params.ie_len = wpa_s->ifmsh->mconf->ie_len;
353 params.basic_rates = wpa_s->ifmsh->basic_rates;
354 }
355
356 wpa_msg(wpa_s, MSG_INFO, "joining mesh %s",
357 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
358 ret = wpa_drv_join_mesh(wpa_s, &params);
359 if (ret)
360 wpa_msg(wpa_s, MSG_ERROR, "mesh join error=%d\n", ret);
361
362 /* hostapd sets the interface down until we associate */
363 wpa_drv_set_operstate(wpa_s, 1);
364
365out:
366 return ret;
367}
368
369
370int wpa_supplicant_leave_mesh(struct wpa_supplicant *wpa_s)
371{
372 int ret = 0;
373
374 wpa_msg(wpa_s, MSG_INFO, "leaving mesh");
375
4e0990dc
JM
376 /* Need to send peering close messages first */
377 wpa_supplicant_mesh_deinit(wpa_s);
378
8319e312
TP
379 ret = wpa_drv_leave_mesh(wpa_s);
380 if (ret)
381 wpa_msg(wpa_s, MSG_ERROR, "mesh leave error=%d", ret);
382
383 wpa_drv_set_operstate(wpa_s, 1);
384
8319e312
TP
385 return ret;
386}
79070906
MH
387
388
389static int mesh_attr_text(const u8 *ies, size_t ies_len, char *buf, char *end)
390{
391 struct ieee802_11_elems elems;
392 char *mesh_id, *pos = buf;
393 u8 *bss_basic_rate_set;
394 int bss_basic_rate_set_len, ret, i;
395
396 if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) == ParseFailed)
397 return -1;
398
399 if (elems.mesh_id_len < 1)
400 return 0;
401
402 mesh_id = os_malloc(elems.mesh_id_len + 1);
403 if (mesh_id == NULL)
404 return -1;
405
406 os_memcpy(mesh_id, elems.mesh_id, elems.mesh_id_len);
407 mesh_id[elems.mesh_id_len] = '\0';
408 ret = os_snprintf(pos, end - pos, "mesh_id=%s\n", mesh_id);
409 os_free(mesh_id);
d85e1fc8 410 if (os_snprintf_error(end - pos, ret))
79070906
MH
411 return pos - buf;
412 pos += ret;
413
414 if (elems.mesh_config_len > 6) {
415 ret = os_snprintf(pos, end - pos,
416 "active_path_selection_protocol_id=0x%02x\n"
417 "active_path_selection_metric_id=0x%02x\n"
418 "congestion_control_mode_id=0x%02x\n"
419 "synchronization_method_id=0x%02x\n"
420 "authentication_protocol_id=0x%02x\n"
421 "mesh_formation_info=0x%02x\n"
422 "mesh_capability=0x%02x\n",
423 elems.mesh_config[0], elems.mesh_config[1],
424 elems.mesh_config[2], elems.mesh_config[3],
425 elems.mesh_config[4], elems.mesh_config[5],
426 elems.mesh_config[6]);
d85e1fc8 427 if (os_snprintf_error(end - pos, ret))
79070906
MH
428 return pos - buf;
429 pos += ret;
430 }
431
432 bss_basic_rate_set = os_malloc(elems.supp_rates_len +
433 elems.ext_supp_rates_len);
434 if (bss_basic_rate_set == NULL)
435 return -1;
436
437 bss_basic_rate_set_len = 0;
438 for (i = 0; i < elems.supp_rates_len; i++) {
439 if (elems.supp_rates[i] & 0x80) {
440 bss_basic_rate_set[bss_basic_rate_set_len++] =
441 (elems.supp_rates[i] & 0x7f) * 5;
442 }
443 }
444 for (i = 0; i < elems.ext_supp_rates_len; i++) {
445 if (elems.ext_supp_rates[i] & 0x80) {
446 bss_basic_rate_set[bss_basic_rate_set_len++] =
447 (elems.ext_supp_rates[i] & 0x7f) * 5;
448 }
449 }
450 if (bss_basic_rate_set_len > 0) {
451 ret = os_snprintf(pos, end - pos, "bss_basic_rate_set=%d",
452 bss_basic_rate_set[0]);
d85e1fc8 453 if (os_snprintf_error(end - pos, ret))
79070906
MH
454 return pos - buf;
455 pos += ret;
456
457 for (i = 1; i < bss_basic_rate_set_len; i++) {
458 ret = os_snprintf(pos, end - pos, " %d",
459 bss_basic_rate_set[i]);
d85e1fc8 460 if (os_snprintf_error(end - pos, ret))
79070906
MH
461 return pos - buf;
462 pos += ret;
463 }
464
465 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 466 if (os_snprintf_error(end - pos, ret))
79070906
MH
467 return pos - buf;
468 pos += ret;
469 }
470 os_free(bss_basic_rate_set);
471
472 return pos - buf;
473}
474
475
476int wpas_mesh_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
477 char *end)
478{
479 return mesh_attr_text(ies, ies_len, buf, end);
480}
5b78493f
MH
481
482
483static int wpas_mesh_get_ifname(struct wpa_supplicant *wpa_s, char *ifname,
484 size_t len)
485{
486 char *ifname_ptr = wpa_s->ifname;
487 int res;
488
489 res = os_snprintf(ifname, len, "mesh-%s-%d", ifname_ptr,
490 wpa_s->mesh_if_idx);
491 if (os_snprintf_error(len, res) ||
492 (os_strlen(ifname) >= IFNAMSIZ &&
493 os_strlen(wpa_s->ifname) < IFNAMSIZ)) {
494 /* Try to avoid going over the IFNAMSIZ length limit */
495 res = os_snprintf(ifname, len, "mesh-%d", wpa_s->mesh_if_idx);
496 if (os_snprintf_error(len, res))
497 return -1;
498 }
499 wpa_s->mesh_if_idx++;
500 return 0;
501}
502
503
504int wpas_mesh_add_interface(struct wpa_supplicant *wpa_s, char *ifname,
505 size_t len)
506{
507 struct wpa_interface iface;
508 struct wpa_supplicant *mesh_wpa_s;
509 u8 addr[ETH_ALEN];
510
511 if (ifname[0] == '\0' && wpas_mesh_get_ifname(wpa_s, ifname, len) < 0)
512 return -1;
513
514 if (wpa_drv_if_add(wpa_s, WPA_IF_MESH, ifname, NULL, NULL, NULL, addr,
515 NULL) < 0) {
516 wpa_printf(MSG_ERROR,
517 "mesh: Failed to create new mesh interface");
518 return -1;
519 }
520 wpa_printf(MSG_INFO, "mesh: Created virtual interface %s addr "
521 MACSTR, ifname, MAC2STR(addr));
522
523 os_memset(&iface, 0, sizeof(iface));
524 iface.ifname = ifname;
525 iface.driver = wpa_s->driver->name;
526 iface.driver_param = wpa_s->conf->driver_param;
527 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
528
529 mesh_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
530 if (!mesh_wpa_s) {
531 wpa_printf(MSG_ERROR,
532 "mesh: Failed to create new wpa_supplicant interface");
533 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
534 return -1;
535 }
536 mesh_wpa_s->mesh_if_created = 1;
537 mesh_wpa_s->parent = wpa_s;
538 return 0;
539}