mesh: Add mesh robust security 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 "mesh_mpm.h"
25 #include "mesh_rsn.h"
26 #include "mesh.h"
27
28
29 static void wpa_supplicant_mesh_deinit(struct wpa_supplicant *wpa_s)
30 {
31         wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh);
32         wpa_s->ifmsh = NULL;
33         wpa_s->current_ssid = NULL;
34         os_free(wpa_s->mesh_rsn);
35         wpa_s->mesh_rsn = NULL;
36         /* TODO: leave mesh (stop beacon). This will happen on link down
37          * anyway, so it's not urgent */
38 }
39
40
41 void wpa_supplicant_mesh_iface_deinit(struct wpa_supplicant *wpa_s,
42                                       struct hostapd_iface *ifmsh)
43 {
44         if (!ifmsh)
45                 return;
46
47         if (ifmsh->mconf) {
48                 mesh_mpm_deinit(wpa_s, ifmsh);
49                 if (ifmsh->mconf->ies) {
50                         ifmsh->mconf->ies = NULL;
51                         /* We cannot free this struct
52                          * because wpa_authenticator on
53                          * hostapd side is also using it
54                          * for now just set to NULL and
55                          * let hostapd code free it.
56                          */
57                 }
58                 os_free(ifmsh->mconf);
59                 ifmsh->mconf = NULL;
60         }
61
62         /* take care of shared data */
63         hostapd_interface_deinit(ifmsh);
64         hostapd_interface_free(ifmsh);
65 }
66
67
68 static struct mesh_conf * mesh_config_create(struct wpa_ssid *ssid)
69 {
70         struct mesh_conf *conf;
71
72         conf = os_zalloc(sizeof(struct mesh_conf));
73         if (!conf)
74                 return NULL;
75
76         os_memcpy(conf->meshid, ssid->ssid, ssid->ssid_len);
77         conf->meshid_len = ssid->ssid_len;
78
79         if (ssid->key_mgmt & WPA_KEY_MGMT_SAE)
80                 conf->security |= MESH_CONF_SEC_AUTH |
81                         MESH_CONF_SEC_AMPE;
82         else
83                 conf->security |= MESH_CONF_SEC_NONE;
84
85         /* defaults */
86         conf->mesh_pp_id = MESH_PATH_PROTOCOL_HWMP;
87         conf->mesh_pm_id = MESH_PATH_METRIC_AIRTIME;
88         conf->mesh_cc_id = 0;
89         conf->mesh_sp_id = MESH_SYNC_METHOD_NEIGHBOR_OFFSET;
90         conf->mesh_auth_id = (conf->security & MESH_CONF_SEC_AUTH) ? 1 : 0;
91
92         return conf;
93 }
94
95
96 static void wpas_mesh_copy_groups(struct hostapd_data *bss,
97                                   struct wpa_supplicant *wpa_s)
98 {
99         int num_groups;
100         size_t groups_size;
101
102         for (num_groups = 0; wpa_s->conf->sae_groups[num_groups] > 0;
103              num_groups++)
104                 ;
105
106         groups_size = (num_groups + 1) * sizeof(wpa_s->conf->sae_groups[0]);
107         bss->conf->sae_groups = os_malloc(groups_size);
108         if (bss->conf->sae_groups)
109                 os_memcpy(bss->conf->sae_groups, wpa_s->conf->sae_groups,
110                           groups_size);
111 }
112
113
114 static int wpa_supplicant_mesh_init(struct wpa_supplicant *wpa_s,
115                                     struct wpa_ssid *ssid)
116 {
117         struct hostapd_iface *ifmsh;
118         struct hostapd_data *bss;
119         struct hostapd_config *conf;
120         struct mesh_conf *mconf;
121         int basic_rates_erp[] = { 10, 20, 55, 60, 110, 120, 240, -1 };
122         static int default_groups[] = { 19, 20, 21, 25, 26, -1 };
123         size_t len;
124
125         if (!wpa_s->conf->user_mpm) {
126                 /* not much for us to do here */
127                 wpa_msg(wpa_s, MSG_WARNING,
128                         "user_mpm is not enabled in configuration");
129                 return 0;
130         }
131
132         wpa_s->ifmsh = ifmsh = os_zalloc(sizeof(*wpa_s->ifmsh));
133         if (!ifmsh)
134                 return -ENOMEM;
135
136         ifmsh->num_bss = 1;
137         ifmsh->bss = os_calloc(wpa_s->ifmsh->num_bss,
138                                sizeof(struct hostapd_data *));
139         if (!ifmsh->bss)
140                 goto out_free;
141
142         ifmsh->bss[0] = bss = os_zalloc(sizeof(struct hostapd_data));
143         if (!bss)
144                 goto out_free;
145
146         os_memcpy(bss->own_addr, wpa_s->own_addr, ETH_ALEN);
147         bss->driver = wpa_s->driver;
148         bss->drv_priv = wpa_s->drv_priv;
149         bss->iface = ifmsh;
150         wpa_s->assoc_freq = ssid->frequency;
151         wpa_s->current_ssid = ssid;
152
153         /* setup an AP config for auth processing */
154         conf = hostapd_config_defaults();
155         if (!conf)
156                 goto out_free;
157
158         bss->conf = *conf->bss;
159         bss->conf->start_disabled = 1;
160         bss->conf->mesh = MESH_ENABLED;
161         bss->iconf = conf;
162         ifmsh->conf = conf;
163
164         ifmsh->bss[0]->max_plinks = 99;
165         os_strlcpy(bss->conf->iface, wpa_s->ifname, sizeof(bss->conf->iface));
166
167         mconf = mesh_config_create(ssid);
168         if (!mconf)
169                 goto out_free;
170         ifmsh->mconf = mconf;
171
172         /* need conf->hw_mode for supported rates. */
173         if (ssid->frequency == 0) {
174                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
175                 conf->channel = 1;
176         } else {
177                 conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
178                                                        &conf->channel);
179         }
180         if (conf->hw_mode == NUM_HOSTAPD_MODES) {
181                 wpa_printf(MSG_ERROR, "Unsupported mesh mode frequency: %d MHz",
182                            ssid->frequency);
183                 goto out_free;
184         }
185
186         /*
187          * XXX: Hack! This is so an MPM which correctly sets the ERP mandatory
188          * rates as BSSBasicRateSet doesn't reject us. We could add a new
189          * hw_mode HOSTAPD_MODE_IEEE80211G_ERP, but this is way easier. This
190          * also makes our BSSBasicRateSet advertised in Beacon frames match the
191          * one in peering frames, sigh.
192          */
193         if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
194                 conf->basic_rates = os_malloc(sizeof(basic_rates_erp));
195                 if (!conf->basic_rates)
196                         goto out_free;
197                 os_memcpy(conf->basic_rates, basic_rates_erp,
198                           sizeof(basic_rates_erp));
199         }
200
201         if (hostapd_setup_interface(ifmsh)) {
202                 wpa_printf(MSG_ERROR,
203                            "Failed to initialize hostapd interface for mesh");
204                 return -1;
205         }
206
207         if (wpa_drv_init_mesh(wpa_s)) {
208                 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh in driver");
209                 return -1;
210         }
211
212         if (mconf->security != MESH_CONF_SEC_NONE) {
213                 if (ssid->passphrase == NULL) {
214                         wpa_printf(MSG_ERROR,
215                                    "mesh: Passphrase for SAE not configured");
216                         goto out_free;
217                 }
218
219                 bss->conf->wpa = ssid->proto;
220                 bss->conf->wpa_key_mgmt = ssid->key_mgmt;
221
222                 if (wpa_s->conf->sae_groups &&
223                     wpa_s->conf->sae_groups[0] > 0) {
224                         wpas_mesh_copy_groups(bss, wpa_s);
225                 } else {
226                         bss->conf->sae_groups =
227                                 os_malloc(sizeof(default_groups));
228                         if (!bss->conf->sae_groups)
229                                 goto out_free;
230                         os_memcpy(bss->conf->sae_groups, default_groups,
231                                   sizeof(default_groups));
232                 }
233
234                 len = os_strlen(ssid->passphrase);
235                 bss->conf->ssid.wpa_passphrase =
236                         dup_binstr(ssid->passphrase, len);
237
238                 wpa_s->mesh_rsn = mesh_rsn_auth_init(wpa_s, mconf);
239                 if (!wpa_s->mesh_rsn)
240                         goto out_free;
241         }
242
243         return 0;
244 out_free:
245         wpa_supplicant_mesh_deinit(wpa_s);
246         return -ENOMEM;
247 }
248
249
250 void wpa_mesh_notify_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
251                           const u8 *ies, size_t ie_len)
252 {
253         struct ieee802_11_elems elems;
254
255         wpa_msg(wpa_s, MSG_INFO,
256                 "new peer notification for " MACSTR, MAC2STR(addr));
257
258         if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
259                 wpa_msg(wpa_s, MSG_INFO, "Could not parse beacon from " MACSTR,
260                         MAC2STR(addr));
261                 return;
262         }
263         wpa_mesh_new_mesh_peer(wpa_s, addr, &elems);
264 }
265
266
267 void wpa_supplicant_mesh_add_scan_ie(struct wpa_supplicant *wpa_s,
268                                      struct wpabuf **extra_ie)
269 {
270         /* EID + 0-length (wildcard) mesh-id */
271         size_t ielen = 2;
272
273         if (wpabuf_resize(extra_ie, ielen) == 0) {
274                 wpabuf_put_u8(*extra_ie, WLAN_EID_MESH_ID);
275                 wpabuf_put_u8(*extra_ie, 0);
276         }
277 }
278
279
280 int wpa_supplicant_join_mesh(struct wpa_supplicant *wpa_s,
281                              struct wpa_ssid *ssid)
282 {
283         struct wpa_driver_mesh_join_params params;
284         int ret = 0;
285
286         if (!ssid || !ssid->ssid || !ssid->ssid_len || !ssid->frequency) {
287                 ret = -ENOENT;
288                 goto out;
289         }
290
291         wpa_supplicant_mesh_deinit(wpa_s);
292
293         os_memset(&params, 0, sizeof(params));
294         params.meshid = ssid->ssid;
295         params.meshid_len = ssid->ssid_len;
296         params.freq = ssid->frequency;
297
298         if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
299                 params.flags |= WPA_DRIVER_MESH_FLAG_SAE_AUTH;
300                 params.flags |= WPA_DRIVER_MESH_FLAG_AMPE;
301                 wpa_s->conf->user_mpm = 1;
302         }
303
304         if (wpa_s->conf->user_mpm) {
305                 params.flags |= WPA_DRIVER_MESH_FLAG_USER_MPM;
306                 params.conf.flags &= ~WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
307         } else {
308                 params.flags |= WPA_DRIVER_MESH_FLAG_DRIVER_MPM;
309                 params.conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
310         }
311
312         if (wpa_supplicant_mesh_init(wpa_s, ssid)) {
313                 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh");
314                 ret = -1;
315                 goto out;
316         }
317
318         if (wpa_s->ifmsh) {
319                 params.ies = wpa_s->ifmsh->mconf->ies;
320                 params.ie_len = wpa_s->ifmsh->mconf->ie_len;
321                 params.basic_rates = wpa_s->ifmsh->basic_rates;
322         }
323
324         wpa_msg(wpa_s, MSG_INFO, "joining mesh %s",
325                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
326         ret = wpa_drv_join_mesh(wpa_s, &params);
327         if (ret)
328                 wpa_msg(wpa_s, MSG_ERROR, "mesh join error=%d\n", ret);
329
330         /* hostapd sets the interface down until we associate */
331         wpa_drv_set_operstate(wpa_s, 1);
332
333 out:
334         return ret;
335 }
336
337
338 int wpa_supplicant_leave_mesh(struct wpa_supplicant *wpa_s)
339 {
340         int ret = 0;
341
342         wpa_msg(wpa_s, MSG_INFO, "leaving mesh");
343
344         ret = wpa_drv_leave_mesh(wpa_s);
345         if (ret)
346                 wpa_msg(wpa_s, MSG_ERROR, "mesh leave error=%d", ret);
347
348         wpa_drv_set_operstate(wpa_s, 1);
349
350         wpa_supplicant_mesh_deinit(wpa_s);
351
352         return ret;
353 }