mesh: Add scan result for mesh network
[mech_eap.git] / wpa_supplicant / mesh.c
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"
24 #include "ap.h"
25 #include "mesh_mpm.h"
26 #include "mesh_rsn.h"
27 #include "mesh.h"
28
29
30 static 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;
37         /* TODO: leave mesh (stop beacon). This will happen on link down
38          * anyway, so it's not urgent */
39 }
40
41
42 void 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) {
49                 mesh_mpm_deinit(wpa_s, ifmsh);
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
69 static 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;
92
93         return conf;
94 }
95
96
97 static void wpas_mesh_copy_groups(struct hostapd_data *bss,
98                                   struct wpa_supplicant *wpa_s)
99 {
100         int num_groups;
101         size_t groups_size;
102
103         for (num_groups = 0; wpa_s->conf->sae_groups[num_groups] > 0;
104              num_groups++)
105                 ;
106
107         groups_size = (num_groups + 1) * sizeof(wpa_s->conf->sae_groups[0]);
108         bss->conf->sae_groups = os_malloc(groups_size);
109         if (bss->conf->sae_groups)
110                 os_memcpy(bss->conf->sae_groups, wpa_s->conf->sae_groups,
111                           groups_size);
112 }
113
114
115 static int wpa_supplicant_mesh_init(struct wpa_supplicant *wpa_s,
116                                     struct wpa_ssid *ssid)
117 {
118         struct hostapd_iface *ifmsh;
119         struct hostapd_data *bss;
120         struct hostapd_config *conf;
121         struct mesh_conf *mconf;
122         int basic_rates_erp[] = { 10, 20, 55, 60, 110, 120, 240, -1 };
123         static int default_groups[] = { 19, 20, 21, 25, 26, -1 };
124         size_t len;
125         int rate_len;
126
127         if (!wpa_s->conf->user_mpm) {
128                 /* not much for us to do here */
129                 wpa_msg(wpa_s, MSG_WARNING,
130                         "user_mpm is not enabled in configuration");
131                 return 0;
132         }
133
134         wpa_s->ifmsh = ifmsh = os_zalloc(sizeof(*wpa_s->ifmsh));
135         if (!ifmsh)
136                 return -ENOMEM;
137
138         ifmsh->drv_flags = wpa_s->drv_flags;
139         ifmsh->num_bss = 1;
140         ifmsh->bss = os_calloc(wpa_s->ifmsh->num_bss,
141                                sizeof(struct hostapd_data *));
142         if (!ifmsh->bss)
143                 goto out_free;
144
145         ifmsh->bss[0] = bss = os_zalloc(sizeof(struct hostapd_data));
146         if (!bss)
147                 goto out_free;
148
149         os_memcpy(bss->own_addr, wpa_s->own_addr, ETH_ALEN);
150         bss->driver = wpa_s->driver;
151         bss->drv_priv = wpa_s->drv_priv;
152         bss->iface = ifmsh;
153         bss->mesh_sta_free_cb = mesh_mpm_free_sta;
154         wpa_s->assoc_freq = ssid->frequency;
155         wpa_s->current_ssid = ssid;
156
157         /* setup an AP config for auth processing */
158         conf = hostapd_config_defaults();
159         if (!conf)
160                 goto out_free;
161
162         bss->conf = *conf->bss;
163         bss->conf->start_disabled = 1;
164         bss->conf->mesh = MESH_ENABLED;
165         bss->iconf = conf;
166         ifmsh->conf = conf;
167
168         ifmsh->bss[0]->max_plinks = 99;
169         os_strlcpy(bss->conf->iface, wpa_s->ifname, sizeof(bss->conf->iface));
170
171         mconf = mesh_config_create(ssid);
172         if (!mconf)
173                 goto out_free;
174         ifmsh->mconf = mconf;
175
176         /* need conf->hw_mode for supported rates. */
177         if (ssid->frequency == 0) {
178                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
179                 conf->channel = 1;
180         } else {
181                 conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
182                                                        &conf->channel);
183         }
184         if (conf->hw_mode == NUM_HOSTAPD_MODES) {
185                 wpa_printf(MSG_ERROR, "Unsupported mesh mode frequency: %d MHz",
186                            ssid->frequency);
187                 goto out_free;
188         }
189
190         if (ssid->mesh_basic_rates == NULL) {
191                 /*
192                  * XXX: Hack! This is so an MPM which correctly sets the ERP
193                  * mandatory rates as BSSBasicRateSet doesn't reject us. We
194                  * could add a new hw_mode HOSTAPD_MODE_IEEE80211G_ERP, but
195                  * this is way easier. This also makes our BSSBasicRateSet
196                  * advertised in beacons match the one in peering frames, sigh.
197                  */
198                 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
199                         conf->basic_rates = os_malloc(sizeof(basic_rates_erp));
200                         if (!conf->basic_rates)
201                                 goto out_free;
202                         os_memcpy(conf->basic_rates, basic_rates_erp,
203                                   sizeof(basic_rates_erp));
204                 }
205         } else {
206                 rate_len = 0;
207                 while (1) {
208                         if (ssid->mesh_basic_rates[rate_len] < 1)
209                                 break;
210                         rate_len++;
211                 }
212                 conf->basic_rates = os_calloc(rate_len + 1, sizeof(int));
213                 if (conf->basic_rates == NULL)
214                         goto out_free;
215                 os_memcpy(conf->basic_rates, ssid->mesh_basic_rates,
216                           rate_len * sizeof(int));
217                 conf->basic_rates[rate_len] = -1;
218         }
219
220         if (hostapd_setup_interface(ifmsh)) {
221                 wpa_printf(MSG_ERROR,
222                            "Failed to initialize hostapd interface for mesh");
223                 return -1;
224         }
225
226         if (wpa_drv_init_mesh(wpa_s)) {
227                 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh in driver");
228                 return -1;
229         }
230
231         if (mconf->security != MESH_CONF_SEC_NONE) {
232                 if (ssid->passphrase == NULL) {
233                         wpa_printf(MSG_ERROR,
234                                    "mesh: Passphrase for SAE not configured");
235                         goto out_free;
236                 }
237
238                 bss->conf->wpa = ssid->proto;
239                 bss->conf->wpa_key_mgmt = ssid->key_mgmt;
240
241                 if (wpa_s->conf->sae_groups &&
242                     wpa_s->conf->sae_groups[0] > 0) {
243                         wpas_mesh_copy_groups(bss, wpa_s);
244                 } else {
245                         bss->conf->sae_groups =
246                                 os_malloc(sizeof(default_groups));
247                         if (!bss->conf->sae_groups)
248                                 goto out_free;
249                         os_memcpy(bss->conf->sae_groups, default_groups,
250                                   sizeof(default_groups));
251                 }
252
253                 len = os_strlen(ssid->passphrase);
254                 bss->conf->ssid.wpa_passphrase =
255                         dup_binstr(ssid->passphrase, len);
256
257                 wpa_s->mesh_rsn = mesh_rsn_auth_init(wpa_s, mconf);
258                 if (!wpa_s->mesh_rsn)
259                         goto out_free;
260         }
261
262         wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf);
263
264         return 0;
265 out_free:
266         wpa_supplicant_mesh_deinit(wpa_s);
267         return -ENOMEM;
268 }
269
270
271 void wpa_mesh_notify_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
272                           const u8 *ies, size_t ie_len)
273 {
274         struct ieee802_11_elems elems;
275
276         wpa_msg(wpa_s, MSG_INFO,
277                 "new peer notification for " MACSTR, MAC2STR(addr));
278
279         if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
280                 wpa_msg(wpa_s, MSG_INFO, "Could not parse beacon from " MACSTR,
281                         MAC2STR(addr));
282                 return;
283         }
284         wpa_mesh_new_mesh_peer(wpa_s, addr, &elems);
285 }
286
287
288 void wpa_supplicant_mesh_add_scan_ie(struct wpa_supplicant *wpa_s,
289                                      struct wpabuf **extra_ie)
290 {
291         /* EID + 0-length (wildcard) mesh-id */
292         size_t ielen = 2;
293
294         if (wpabuf_resize(extra_ie, ielen) == 0) {
295                 wpabuf_put_u8(*extra_ie, WLAN_EID_MESH_ID);
296                 wpabuf_put_u8(*extra_ie, 0);
297         }
298 }
299
300
301 int wpa_supplicant_join_mesh(struct wpa_supplicant *wpa_s,
302                              struct wpa_ssid *ssid)
303 {
304         struct wpa_driver_mesh_join_params params;
305         int ret = 0;
306
307         if (!ssid || !ssid->ssid || !ssid->ssid_len || !ssid->frequency) {
308                 ret = -ENOENT;
309                 goto out;
310         }
311
312         wpa_supplicant_mesh_deinit(wpa_s);
313
314         os_memset(&params, 0, sizeof(params));
315         params.meshid = ssid->ssid;
316         params.meshid_len = ssid->ssid_len;
317         params.freq = ssid->frequency;
318 #ifdef CONFIG_IEEE80211N
319         params.ht_mode = ssid->mesh_ht_mode;
320 #endif /* CONFIG_IEEE80211N */
321
322         if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
323                 params.flags |= WPA_DRIVER_MESH_FLAG_SAE_AUTH;
324                 params.flags |= WPA_DRIVER_MESH_FLAG_AMPE;
325                 wpa_s->conf->user_mpm = 1;
326         }
327
328         if (wpa_s->conf->user_mpm) {
329                 params.flags |= WPA_DRIVER_MESH_FLAG_USER_MPM;
330                 params.conf.flags &= ~WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
331         } else {
332                 params.flags |= WPA_DRIVER_MESH_FLAG_DRIVER_MPM;
333                 params.conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
334         }
335
336         if (wpa_supplicant_mesh_init(wpa_s, ssid)) {
337                 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh");
338                 ret = -1;
339                 goto out;
340         }
341
342         if (wpa_s->ifmsh) {
343                 params.ies = wpa_s->ifmsh->mconf->ies;
344                 params.ie_len = wpa_s->ifmsh->mconf->ie_len;
345                 params.basic_rates = wpa_s->ifmsh->basic_rates;
346         }
347
348         wpa_msg(wpa_s, MSG_INFO, "joining mesh %s",
349                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
350         ret = wpa_drv_join_mesh(wpa_s, &params);
351         if (ret)
352                 wpa_msg(wpa_s, MSG_ERROR, "mesh join error=%d\n", ret);
353
354         /* hostapd sets the interface down until we associate */
355         wpa_drv_set_operstate(wpa_s, 1);
356
357 out:
358         return ret;
359 }
360
361
362 int wpa_supplicant_leave_mesh(struct wpa_supplicant *wpa_s)
363 {
364         int ret = 0;
365
366         wpa_msg(wpa_s, MSG_INFO, "leaving mesh");
367
368         ret = wpa_drv_leave_mesh(wpa_s);
369         if (ret)
370                 wpa_msg(wpa_s, MSG_ERROR, "mesh leave error=%d", ret);
371
372         wpa_drv_set_operstate(wpa_s, 1);
373
374         wpa_supplicant_mesh_deinit(wpa_s);
375
376         return ret;
377 }
378
379
380 static int mesh_attr_text(const u8 *ies, size_t ies_len, char *buf, char *end)
381 {
382         struct ieee802_11_elems elems;
383         char *mesh_id, *pos = buf;
384         u8 *bss_basic_rate_set;
385         int bss_basic_rate_set_len, ret, i;
386
387         if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) == ParseFailed)
388                 return -1;
389
390         if (elems.mesh_id_len < 1)
391                 return 0;
392
393         mesh_id = os_malloc(elems.mesh_id_len + 1);
394         if (mesh_id == NULL)
395                 return -1;
396
397         os_memcpy(mesh_id, elems.mesh_id, elems.mesh_id_len);
398         mesh_id[elems.mesh_id_len] = '\0';
399         ret = os_snprintf(pos, end - pos, "mesh_id=%s\n", mesh_id);
400         os_free(mesh_id);
401         if (ret < 0 || ret >= end - pos)
402                 return pos - buf;
403         pos += ret;
404
405         if (elems.mesh_config_len > 6) {
406                 ret = os_snprintf(pos, end - pos,
407                                   "active_path_selection_protocol_id=0x%02x\n"
408                                   "active_path_selection_metric_id=0x%02x\n"
409                                   "congestion_control_mode_id=0x%02x\n"
410                                   "synchronization_method_id=0x%02x\n"
411                                   "authentication_protocol_id=0x%02x\n"
412                                   "mesh_formation_info=0x%02x\n"
413                                   "mesh_capability=0x%02x\n",
414                                   elems.mesh_config[0], elems.mesh_config[1],
415                                   elems.mesh_config[2], elems.mesh_config[3],
416                                   elems.mesh_config[4], elems.mesh_config[5],
417                                   elems.mesh_config[6]);
418                 if (ret < 0 || ret >= end - pos)
419                         return pos - buf;
420                 pos += ret;
421         }
422
423         bss_basic_rate_set = os_malloc(elems.supp_rates_len +
424                 elems.ext_supp_rates_len);
425         if (bss_basic_rate_set == NULL)
426                 return -1;
427
428         bss_basic_rate_set_len = 0;
429         for (i = 0; i < elems.supp_rates_len; i++) {
430                 if (elems.supp_rates[i] & 0x80) {
431                         bss_basic_rate_set[bss_basic_rate_set_len++] =
432                                 (elems.supp_rates[i] & 0x7f) * 5;
433                 }
434         }
435         for (i = 0; i < elems.ext_supp_rates_len; i++) {
436                 if (elems.ext_supp_rates[i] & 0x80) {
437                         bss_basic_rate_set[bss_basic_rate_set_len++] =
438                                 (elems.ext_supp_rates[i] & 0x7f) * 5;
439                 }
440         }
441         if (bss_basic_rate_set_len > 0) {
442                 ret = os_snprintf(pos, end - pos, "bss_basic_rate_set=%d",
443                                   bss_basic_rate_set[0]);
444                 if (ret < 0 || ret >= end - pos)
445                         return pos - buf;
446                 pos += ret;
447
448                 for (i = 1; i < bss_basic_rate_set_len; i++) {
449                         ret = os_snprintf(pos, end - pos, " %d",
450                                           bss_basic_rate_set[i]);
451                         if (ret < 0 || ret >= end - pos)
452                                 return pos - buf;
453                         pos += ret;
454                 }
455
456                 ret = os_snprintf(pos, end - pos, "\n");
457                 if (ret < 0 || ret >= end - pos)
458                         return pos - buf;
459                 pos += ret;
460         }
461         os_free(bss_basic_rate_set);
462
463         return pos - buf;
464 }
465
466
467 int wpas_mesh_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
468                                char *end)
469 {
470         return mesh_attr_text(ies, ies_len, buf, end);
471 }