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