mesh: Set driver capability flags to mesh interface
[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
126         if (!wpa_s->conf->user_mpm) {
127                 /* not much for us to do here */
128                 wpa_msg(wpa_s, MSG_WARNING,
129                         "user_mpm is not enabled in configuration");
130                 return 0;
131         }
132
133         wpa_s->ifmsh = ifmsh = os_zalloc(sizeof(*wpa_s->ifmsh));
134         if (!ifmsh)
135                 return -ENOMEM;
136
137         ifmsh->drv_flags = wpa_s->drv_flags;
138         ifmsh->num_bss = 1;
139         ifmsh->bss = os_calloc(wpa_s->ifmsh->num_bss,
140                                sizeof(struct hostapd_data *));
141         if (!ifmsh->bss)
142                 goto out_free;
143
144         ifmsh->bss[0] = bss = os_zalloc(sizeof(struct hostapd_data));
145         if (!bss)
146                 goto out_free;
147
148         os_memcpy(bss->own_addr, wpa_s->own_addr, ETH_ALEN);
149         bss->driver = wpa_s->driver;
150         bss->drv_priv = wpa_s->drv_priv;
151         bss->iface = ifmsh;
152         bss->mesh_sta_free_cb = mesh_mpm_free_sta;
153         wpa_s->assoc_freq = ssid->frequency;
154         wpa_s->current_ssid = ssid;
155
156         /* setup an AP config for auth processing */
157         conf = hostapd_config_defaults();
158         if (!conf)
159                 goto out_free;
160
161         bss->conf = *conf->bss;
162         bss->conf->start_disabled = 1;
163         bss->conf->mesh = MESH_ENABLED;
164         bss->iconf = conf;
165         ifmsh->conf = conf;
166
167         ifmsh->bss[0]->max_plinks = 99;
168         os_strlcpy(bss->conf->iface, wpa_s->ifname, sizeof(bss->conf->iface));
169
170         mconf = mesh_config_create(ssid);
171         if (!mconf)
172                 goto out_free;
173         ifmsh->mconf = mconf;
174
175         /* need conf->hw_mode for supported rates. */
176         if (ssid->frequency == 0) {
177                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
178                 conf->channel = 1;
179         } else {
180                 conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
181                                                        &conf->channel);
182         }
183         if (conf->hw_mode == NUM_HOSTAPD_MODES) {
184                 wpa_printf(MSG_ERROR, "Unsupported mesh mode frequency: %d MHz",
185                            ssid->frequency);
186                 goto out_free;
187         }
188
189         /*
190          * XXX: Hack! This is so an MPM which correctly sets the ERP mandatory
191          * rates as BSSBasicRateSet doesn't reject us. We could add a new
192          * hw_mode HOSTAPD_MODE_IEEE80211G_ERP, but this is way easier. This
193          * also makes our BSSBasicRateSet advertised in Beacon frames match the
194          * one in peering frames, sigh.
195          */
196         if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
197                 conf->basic_rates = os_malloc(sizeof(basic_rates_erp));
198                 if (!conf->basic_rates)
199                         goto out_free;
200                 os_memcpy(conf->basic_rates, basic_rates_erp,
201                           sizeof(basic_rates_erp));
202         }
203
204         if (hostapd_setup_interface(ifmsh)) {
205                 wpa_printf(MSG_ERROR,
206                            "Failed to initialize hostapd interface for mesh");
207                 return -1;
208         }
209
210         if (wpa_drv_init_mesh(wpa_s)) {
211                 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh in driver");
212                 return -1;
213         }
214
215         if (mconf->security != MESH_CONF_SEC_NONE) {
216                 if (ssid->passphrase == NULL) {
217                         wpa_printf(MSG_ERROR,
218                                    "mesh: Passphrase for SAE not configured");
219                         goto out_free;
220                 }
221
222                 bss->conf->wpa = ssid->proto;
223                 bss->conf->wpa_key_mgmt = ssid->key_mgmt;
224
225                 if (wpa_s->conf->sae_groups &&
226                     wpa_s->conf->sae_groups[0] > 0) {
227                         wpas_mesh_copy_groups(bss, wpa_s);
228                 } else {
229                         bss->conf->sae_groups =
230                                 os_malloc(sizeof(default_groups));
231                         if (!bss->conf->sae_groups)
232                                 goto out_free;
233                         os_memcpy(bss->conf->sae_groups, default_groups,
234                                   sizeof(default_groups));
235                 }
236
237                 len = os_strlen(ssid->passphrase);
238                 bss->conf->ssid.wpa_passphrase =
239                         dup_binstr(ssid->passphrase, len);
240
241                 wpa_s->mesh_rsn = mesh_rsn_auth_init(wpa_s, mconf);
242                 if (!wpa_s->mesh_rsn)
243                         goto out_free;
244         }
245
246         wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf);
247
248         return 0;
249 out_free:
250         wpa_supplicant_mesh_deinit(wpa_s);
251         return -ENOMEM;
252 }
253
254
255 void wpa_mesh_notify_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
256                           const u8 *ies, size_t ie_len)
257 {
258         struct ieee802_11_elems elems;
259
260         wpa_msg(wpa_s, MSG_INFO,
261                 "new peer notification for " MACSTR, MAC2STR(addr));
262
263         if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
264                 wpa_msg(wpa_s, MSG_INFO, "Could not parse beacon from " MACSTR,
265                         MAC2STR(addr));
266                 return;
267         }
268         wpa_mesh_new_mesh_peer(wpa_s, addr, &elems);
269 }
270
271
272 void wpa_supplicant_mesh_add_scan_ie(struct wpa_supplicant *wpa_s,
273                                      struct wpabuf **extra_ie)
274 {
275         /* EID + 0-length (wildcard) mesh-id */
276         size_t ielen = 2;
277
278         if (wpabuf_resize(extra_ie, ielen) == 0) {
279                 wpabuf_put_u8(*extra_ie, WLAN_EID_MESH_ID);
280                 wpabuf_put_u8(*extra_ie, 0);
281         }
282 }
283
284
285 int wpa_supplicant_join_mesh(struct wpa_supplicant *wpa_s,
286                              struct wpa_ssid *ssid)
287 {
288         struct wpa_driver_mesh_join_params params;
289         int ret = 0;
290
291         if (!ssid || !ssid->ssid || !ssid->ssid_len || !ssid->frequency) {
292                 ret = -ENOENT;
293                 goto out;
294         }
295
296         wpa_supplicant_mesh_deinit(wpa_s);
297
298         os_memset(&params, 0, sizeof(params));
299         params.meshid = ssid->ssid;
300         params.meshid_len = ssid->ssid_len;
301         params.freq = ssid->frequency;
302 #ifdef CONFIG_IEEE80211N
303         params.ht_mode = ssid->mesh_ht_mode;
304 #endif /* CONFIG_IEEE80211N */
305
306         if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
307                 params.flags |= WPA_DRIVER_MESH_FLAG_SAE_AUTH;
308                 params.flags |= WPA_DRIVER_MESH_FLAG_AMPE;
309                 wpa_s->conf->user_mpm = 1;
310         }
311
312         if (wpa_s->conf->user_mpm) {
313                 params.flags |= WPA_DRIVER_MESH_FLAG_USER_MPM;
314                 params.conf.flags &= ~WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
315         } else {
316                 params.flags |= WPA_DRIVER_MESH_FLAG_DRIVER_MPM;
317                 params.conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
318         }
319
320         if (wpa_supplicant_mesh_init(wpa_s, ssid)) {
321                 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh");
322                 ret = -1;
323                 goto out;
324         }
325
326         if (wpa_s->ifmsh) {
327                 params.ies = wpa_s->ifmsh->mconf->ies;
328                 params.ie_len = wpa_s->ifmsh->mconf->ie_len;
329                 params.basic_rates = wpa_s->ifmsh->basic_rates;
330         }
331
332         wpa_msg(wpa_s, MSG_INFO, "joining mesh %s",
333                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
334         ret = wpa_drv_join_mesh(wpa_s, &params);
335         if (ret)
336                 wpa_msg(wpa_s, MSG_ERROR, "mesh join error=%d\n", ret);
337
338         /* hostapd sets the interface down until we associate */
339         wpa_drv_set_operstate(wpa_s, 1);
340
341 out:
342         return ret;
343 }
344
345
346 int wpa_supplicant_leave_mesh(struct wpa_supplicant *wpa_s)
347 {
348         int ret = 0;
349
350         wpa_msg(wpa_s, MSG_INFO, "leaving mesh");
351
352         ret = wpa_drv_leave_mesh(wpa_s);
353         if (ret)
354                 wpa_msg(wpa_s, MSG_ERROR, "mesh leave error=%d", ret);
355
356         wpa_drv_set_operstate(wpa_s, 1);
357
358         wpa_supplicant_mesh_deinit(wpa_s);
359
360         return ret;
361 }