2 * WPA Supplicant - Basic mesh mode routines
3 * Copyright (c) 2013-2014, cozybit, Inc. All rights reserved.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
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"
21 #include "wpa_supplicant_i.h"
29 static void wpa_supplicant_mesh_deinit(struct wpa_supplicant *wpa_s)
31 wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh);
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 */
41 void wpa_supplicant_mesh_iface_deinit(struct wpa_supplicant *wpa_s,
42 struct hostapd_iface *ifmsh)
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.
58 os_free(ifmsh->mconf);
62 /* take care of shared data */
63 hostapd_interface_deinit(ifmsh);
64 hostapd_interface_free(ifmsh);
68 static struct mesh_conf * mesh_config_create(struct wpa_ssid *ssid)
70 struct mesh_conf *conf;
72 conf = os_zalloc(sizeof(struct mesh_conf));
76 os_memcpy(conf->meshid, ssid->ssid, ssid->ssid_len);
77 conf->meshid_len = ssid->ssid_len;
79 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE)
80 conf->security |= MESH_CONF_SEC_AUTH |
83 conf->security |= MESH_CONF_SEC_NONE;
86 conf->mesh_pp_id = MESH_PATH_PROTOCOL_HWMP;
87 conf->mesh_pm_id = MESH_PATH_METRIC_AIRTIME;
89 conf->mesh_sp_id = MESH_SYNC_METHOD_NEIGHBOR_OFFSET;
90 conf->mesh_auth_id = (conf->security & MESH_CONF_SEC_AUTH) ? 1 : 0;
96 static void wpas_mesh_copy_groups(struct hostapd_data *bss,
97 struct wpa_supplicant *wpa_s)
102 for (num_groups = 0; wpa_s->conf->sae_groups[num_groups] > 0;
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,
114 static int wpa_supplicant_mesh_init(struct wpa_supplicant *wpa_s,
115 struct wpa_ssid *ssid)
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 };
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");
132 wpa_s->ifmsh = ifmsh = os_zalloc(sizeof(*wpa_s->ifmsh));
137 ifmsh->bss = os_calloc(wpa_s->ifmsh->num_bss,
138 sizeof(struct hostapd_data *));
142 ifmsh->bss[0] = bss = os_zalloc(sizeof(struct hostapd_data));
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;
150 bss->mesh_sta_free_cb = mesh_mpm_free_sta;
151 wpa_s->assoc_freq = ssid->frequency;
152 wpa_s->current_ssid = ssid;
154 /* setup an AP config for auth processing */
155 conf = hostapd_config_defaults();
159 bss->conf = *conf->bss;
160 bss->conf->start_disabled = 1;
161 bss->conf->mesh = MESH_ENABLED;
165 ifmsh->bss[0]->max_plinks = 99;
166 os_strlcpy(bss->conf->iface, wpa_s->ifname, sizeof(bss->conf->iface));
168 mconf = mesh_config_create(ssid);
171 ifmsh->mconf = mconf;
173 /* need conf->hw_mode for supported rates. */
174 if (ssid->frequency == 0) {
175 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
178 conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
181 if (conf->hw_mode == NUM_HOSTAPD_MODES) {
182 wpa_printf(MSG_ERROR, "Unsupported mesh mode frequency: %d MHz",
188 * XXX: Hack! This is so an MPM which correctly sets the ERP mandatory
189 * rates as BSSBasicRateSet doesn't reject us. We could add a new
190 * hw_mode HOSTAPD_MODE_IEEE80211G_ERP, but this is way easier. This
191 * also makes our BSSBasicRateSet advertised in Beacon frames match the
192 * one in peering frames, sigh.
194 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
195 conf->basic_rates = os_malloc(sizeof(basic_rates_erp));
196 if (!conf->basic_rates)
198 os_memcpy(conf->basic_rates, basic_rates_erp,
199 sizeof(basic_rates_erp));
202 if (hostapd_setup_interface(ifmsh)) {
203 wpa_printf(MSG_ERROR,
204 "Failed to initialize hostapd interface for mesh");
208 if (wpa_drv_init_mesh(wpa_s)) {
209 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh in driver");
213 if (mconf->security != MESH_CONF_SEC_NONE) {
214 if (ssid->passphrase == NULL) {
215 wpa_printf(MSG_ERROR,
216 "mesh: Passphrase for SAE not configured");
220 bss->conf->wpa = ssid->proto;
221 bss->conf->wpa_key_mgmt = ssid->key_mgmt;
223 if (wpa_s->conf->sae_groups &&
224 wpa_s->conf->sae_groups[0] > 0) {
225 wpas_mesh_copy_groups(bss, wpa_s);
227 bss->conf->sae_groups =
228 os_malloc(sizeof(default_groups));
229 if (!bss->conf->sae_groups)
231 os_memcpy(bss->conf->sae_groups, default_groups,
232 sizeof(default_groups));
235 len = os_strlen(ssid->passphrase);
236 bss->conf->ssid.wpa_passphrase =
237 dup_binstr(ssid->passphrase, len);
239 wpa_s->mesh_rsn = mesh_rsn_auth_init(wpa_s, mconf);
240 if (!wpa_s->mesh_rsn)
246 wpa_supplicant_mesh_deinit(wpa_s);
251 void wpa_mesh_notify_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
252 const u8 *ies, size_t ie_len)
254 struct ieee802_11_elems elems;
256 wpa_msg(wpa_s, MSG_INFO,
257 "new peer notification for " MACSTR, MAC2STR(addr));
259 if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
260 wpa_msg(wpa_s, MSG_INFO, "Could not parse beacon from " MACSTR,
264 wpa_mesh_new_mesh_peer(wpa_s, addr, &elems);
268 void wpa_supplicant_mesh_add_scan_ie(struct wpa_supplicant *wpa_s,
269 struct wpabuf **extra_ie)
271 /* EID + 0-length (wildcard) mesh-id */
274 if (wpabuf_resize(extra_ie, ielen) == 0) {
275 wpabuf_put_u8(*extra_ie, WLAN_EID_MESH_ID);
276 wpabuf_put_u8(*extra_ie, 0);
281 int wpa_supplicant_join_mesh(struct wpa_supplicant *wpa_s,
282 struct wpa_ssid *ssid)
284 struct wpa_driver_mesh_join_params params;
287 if (!ssid || !ssid->ssid || !ssid->ssid_len || !ssid->frequency) {
292 wpa_supplicant_mesh_deinit(wpa_s);
294 os_memset(¶ms, 0, sizeof(params));
295 params.meshid = ssid->ssid;
296 params.meshid_len = ssid->ssid_len;
297 params.freq = ssid->frequency;
299 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
300 params.flags |= WPA_DRIVER_MESH_FLAG_SAE_AUTH;
301 params.flags |= WPA_DRIVER_MESH_FLAG_AMPE;
302 wpa_s->conf->user_mpm = 1;
305 if (wpa_s->conf->user_mpm) {
306 params.flags |= WPA_DRIVER_MESH_FLAG_USER_MPM;
307 params.conf.flags &= ~WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
309 params.flags |= WPA_DRIVER_MESH_FLAG_DRIVER_MPM;
310 params.conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
313 if (wpa_supplicant_mesh_init(wpa_s, ssid)) {
314 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh");
320 params.ies = wpa_s->ifmsh->mconf->ies;
321 params.ie_len = wpa_s->ifmsh->mconf->ie_len;
322 params.basic_rates = wpa_s->ifmsh->basic_rates;
325 wpa_msg(wpa_s, MSG_INFO, "joining mesh %s",
326 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
327 ret = wpa_drv_join_mesh(wpa_s, ¶ms);
329 wpa_msg(wpa_s, MSG_ERROR, "mesh join error=%d\n", ret);
331 /* hostapd sets the interface down until we associate */
332 wpa_drv_set_operstate(wpa_s, 1);
339 int wpa_supplicant_leave_mesh(struct wpa_supplicant *wpa_s)
343 wpa_msg(wpa_s, MSG_INFO, "leaving mesh");
345 ret = wpa_drv_leave_mesh(wpa_s);
347 wpa_msg(wpa_s, MSG_ERROR, "mesh leave error=%d", ret);
349 wpa_drv_set_operstate(wpa_s, 1);
351 wpa_supplicant_mesh_deinit(wpa_s);