mesh: Write close reason from Mesh Peering Close to debug log
[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->rsn_ie) {
51                         ifmsh->mconf->rsn_ie = 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_supplicant *wpa_s,
70                                              struct wpa_ssid *ssid)
71 {
72         struct mesh_conf *conf;
73         int cipher;
74
75         conf = os_zalloc(sizeof(struct mesh_conf));
76         if (!conf)
77                 return NULL;
78
79         os_memcpy(conf->meshid, ssid->ssid, ssid->ssid_len);
80         conf->meshid_len = ssid->ssid_len;
81
82         if (ssid->key_mgmt & WPA_KEY_MGMT_SAE)
83                 conf->security |= MESH_CONF_SEC_AUTH |
84                         MESH_CONF_SEC_AMPE;
85         else
86                 conf->security |= MESH_CONF_SEC_NONE;
87         conf->ieee80211w = ssid->ieee80211w;
88         if (conf->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT) {
89                 if (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_BIP)
90                         conf->ieee80211w = wpa_s->conf->pmf;
91                 else
92                         conf->ieee80211w = NO_MGMT_FRAME_PROTECTION;
93         }
94
95         cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher, 0);
96         if (cipher < 0 || cipher == WPA_CIPHER_TKIP) {
97                 wpa_msg(wpa_s, MSG_INFO, "mesh: Invalid pairwise cipher");
98                 os_free(conf);
99                 return NULL;
100         }
101         conf->pairwise_cipher = cipher;
102
103         cipher = wpa_pick_group_cipher(ssid->group_cipher);
104         if (cipher < 0 || cipher == WPA_CIPHER_TKIP ||
105             cipher == WPA_CIPHER_GTK_NOT_USED) {
106                 wpa_msg(wpa_s, MSG_INFO, "mesh: Invalid group cipher");
107                 os_free(conf);
108                 return NULL;
109         }
110
111         conf->group_cipher = cipher;
112         if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION)
113                 conf->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
114
115         /* defaults */
116         conf->mesh_pp_id = MESH_PATH_PROTOCOL_HWMP;
117         conf->mesh_pm_id = MESH_PATH_METRIC_AIRTIME;
118         conf->mesh_cc_id = 0;
119         conf->mesh_sp_id = MESH_SYNC_METHOD_NEIGHBOR_OFFSET;
120         conf->mesh_auth_id = (conf->security & MESH_CONF_SEC_AUTH) ? 1 : 0;
121         conf->dot11MeshMaxRetries = ssid->dot11MeshMaxRetries;
122         conf->dot11MeshRetryTimeout = ssid->dot11MeshRetryTimeout;
123         conf->dot11MeshConfirmTimeout = ssid->dot11MeshConfirmTimeout;
124         conf->dot11MeshHoldingTimeout = ssid->dot11MeshHoldingTimeout;
125
126         return conf;
127 }
128
129
130 static void wpas_mesh_copy_groups(struct hostapd_data *bss,
131                                   struct wpa_supplicant *wpa_s)
132 {
133         int num_groups;
134         size_t groups_size;
135
136         for (num_groups = 0; wpa_s->conf->sae_groups[num_groups] > 0;
137              num_groups++)
138                 ;
139
140         groups_size = (num_groups + 1) * sizeof(wpa_s->conf->sae_groups[0]);
141         bss->conf->sae_groups = os_malloc(groups_size);
142         if (bss->conf->sae_groups)
143                 os_memcpy(bss->conf->sae_groups, wpa_s->conf->sae_groups,
144                           groups_size);
145 }
146
147
148 static int wpa_supplicant_mesh_init(struct wpa_supplicant *wpa_s,
149                                     struct wpa_ssid *ssid)
150 {
151         struct hostapd_iface *ifmsh;
152         struct hostapd_data *bss;
153         struct hostapd_config *conf;
154         struct mesh_conf *mconf;
155         int basic_rates_erp[] = { 10, 20, 55, 60, 110, 120, 240, -1 };
156         static int default_groups[] = { 19, 20, 21, 25, 26, -1 };
157         size_t len;
158         int rate_len;
159
160         if (!wpa_s->conf->user_mpm) {
161                 /* not much for us to do here */
162                 wpa_msg(wpa_s, MSG_WARNING,
163                         "user_mpm is not enabled in configuration");
164                 return 0;
165         }
166
167         wpa_s->ifmsh = ifmsh = os_zalloc(sizeof(*wpa_s->ifmsh));
168         if (!ifmsh)
169                 return -ENOMEM;
170
171         ifmsh->drv_flags = wpa_s->drv_flags;
172         ifmsh->num_bss = 1;
173         ifmsh->bss = os_calloc(wpa_s->ifmsh->num_bss,
174                                sizeof(struct hostapd_data *));
175         if (!ifmsh->bss)
176                 goto out_free;
177
178         ifmsh->bss[0] = bss = os_zalloc(sizeof(struct hostapd_data));
179         if (!bss)
180                 goto out_free;
181
182         os_memcpy(bss->own_addr, wpa_s->own_addr, ETH_ALEN);
183         bss->driver = wpa_s->driver;
184         bss->drv_priv = wpa_s->drv_priv;
185         bss->iface = ifmsh;
186         bss->mesh_sta_free_cb = mesh_mpm_free_sta;
187         wpa_s->assoc_freq = ssid->frequency;
188         wpa_s->current_ssid = ssid;
189
190         /* setup an AP config for auth processing */
191         conf = hostapd_config_defaults();
192         if (!conf)
193                 goto out_free;
194
195         bss->conf = *conf->bss;
196         bss->conf->start_disabled = 1;
197         bss->conf->mesh = MESH_ENABLED;
198         bss->conf->ap_max_inactivity = wpa_s->conf->mesh_max_inactivity;
199         bss->iconf = conf;
200         ifmsh->conf = conf;
201
202         ifmsh->bss[0]->max_plinks = wpa_s->conf->max_peer_links;
203         ifmsh->bss[0]->dot11RSNASAERetransPeriod =
204                 wpa_s->conf->dot11RSNASAERetransPeriod;
205         os_strlcpy(bss->conf->iface, wpa_s->ifname, sizeof(bss->conf->iface));
206
207         mconf = mesh_config_create(wpa_s, ssid);
208         if (!mconf)
209                 goto out_free;
210         ifmsh->mconf = mconf;
211
212         /* need conf->hw_mode for supported rates. */
213         conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency, &conf->channel);
214         if (conf->hw_mode == NUM_HOSTAPD_MODES) {
215                 wpa_printf(MSG_ERROR, "Unsupported mesh mode frequency: %d MHz",
216                            ssid->frequency);
217                 goto out_free;
218         }
219         if (ssid->ht40)
220                 conf->secondary_channel = ssid->ht40;
221         if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A && ssid->vht) {
222                 conf->vht_oper_chwidth = ssid->max_oper_chwidth;
223                 switch (conf->vht_oper_chwidth) {
224                 case VHT_CHANWIDTH_80MHZ:
225                 case VHT_CHANWIDTH_80P80MHZ:
226                         ieee80211_freq_to_chan(
227                                 ssid->frequency,
228                                 &conf->vht_oper_centr_freq_seg0_idx);
229                         conf->vht_oper_centr_freq_seg0_idx += ssid->ht40 * 2;
230                         break;
231                 case VHT_CHANWIDTH_160MHZ:
232                         ieee80211_freq_to_chan(
233                                 ssid->frequency,
234                                 &conf->vht_oper_centr_freq_seg0_idx);
235                         conf->vht_oper_centr_freq_seg0_idx += ssid->ht40 * 2;
236                         conf->vht_oper_centr_freq_seg0_idx += 40 / 5;
237                         break;
238                 }
239                 ieee80211_freq_to_chan(ssid->vht_center_freq2,
240                                        &conf->vht_oper_centr_freq_seg1_idx);
241         }
242
243         if (ssid->mesh_basic_rates == NULL) {
244                 /*
245                  * XXX: Hack! This is so an MPM which correctly sets the ERP
246                  * mandatory rates as BSSBasicRateSet doesn't reject us. We
247                  * could add a new hw_mode HOSTAPD_MODE_IEEE80211G_ERP, but
248                  * this is way easier. This also makes our BSSBasicRateSet
249                  * advertised in beacons match the one in peering frames, sigh.
250                  */
251                 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
252                         conf->basic_rates = os_malloc(sizeof(basic_rates_erp));
253                         if (!conf->basic_rates)
254                                 goto out_free;
255                         os_memcpy(conf->basic_rates, basic_rates_erp,
256                                   sizeof(basic_rates_erp));
257                 }
258         } else {
259                 rate_len = 0;
260                 while (1) {
261                         if (ssid->mesh_basic_rates[rate_len] < 1)
262                                 break;
263                         rate_len++;
264                 }
265                 conf->basic_rates = os_calloc(rate_len + 1, sizeof(int));
266                 if (conf->basic_rates == NULL)
267                         goto out_free;
268                 os_memcpy(conf->basic_rates, ssid->mesh_basic_rates,
269                           rate_len * sizeof(int));
270                 conf->basic_rates[rate_len] = -1;
271         }
272
273         if (hostapd_setup_interface(ifmsh)) {
274                 wpa_printf(MSG_ERROR,
275                            "Failed to initialize hostapd interface for mesh");
276                 return -1;
277         }
278
279         if (wpa_drv_init_mesh(wpa_s)) {
280                 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh in driver");
281                 return -1;
282         }
283
284         if (mconf->security != MESH_CONF_SEC_NONE) {
285                 if (ssid->passphrase == NULL) {
286                         wpa_printf(MSG_ERROR,
287                                    "mesh: Passphrase for SAE not configured");
288                         goto out_free;
289                 }
290
291                 bss->conf->wpa = ssid->proto;
292                 bss->conf->wpa_key_mgmt = ssid->key_mgmt;
293
294                 if (wpa_s->conf->sae_groups &&
295                     wpa_s->conf->sae_groups[0] > 0) {
296                         wpas_mesh_copy_groups(bss, wpa_s);
297                 } else {
298                         bss->conf->sae_groups =
299                                 os_malloc(sizeof(default_groups));
300                         if (!bss->conf->sae_groups)
301                                 goto out_free;
302                         os_memcpy(bss->conf->sae_groups, default_groups,
303                                   sizeof(default_groups));
304                 }
305
306                 len = os_strlen(ssid->passphrase);
307                 bss->conf->ssid.wpa_passphrase =
308                         dup_binstr(ssid->passphrase, len);
309
310                 wpa_s->mesh_rsn = mesh_rsn_auth_init(wpa_s, mconf);
311                 if (!wpa_s->mesh_rsn)
312                         goto out_free;
313         }
314
315         wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf);
316
317         return 0;
318 out_free:
319         wpa_supplicant_mesh_deinit(wpa_s);
320         return -ENOMEM;
321 }
322
323
324 void wpa_mesh_notify_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
325                           const u8 *ies, size_t ie_len)
326 {
327         struct ieee802_11_elems elems;
328
329         wpa_msg(wpa_s, MSG_INFO,
330                 "new peer notification for " MACSTR, MAC2STR(addr));
331
332         if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
333                 wpa_msg(wpa_s, MSG_INFO, "Could not parse beacon from " MACSTR,
334                         MAC2STR(addr));
335                 return;
336         }
337         wpa_mesh_new_mesh_peer(wpa_s, addr, &elems);
338 }
339
340
341 void wpa_supplicant_mesh_add_scan_ie(struct wpa_supplicant *wpa_s,
342                                      struct wpabuf **extra_ie)
343 {
344         /* EID + 0-length (wildcard) mesh-id */
345         size_t ielen = 2;
346
347         if (wpabuf_resize(extra_ie, ielen) == 0) {
348                 wpabuf_put_u8(*extra_ie, WLAN_EID_MESH_ID);
349                 wpabuf_put_u8(*extra_ie, 0);
350         }
351 }
352
353
354 int wpa_supplicant_join_mesh(struct wpa_supplicant *wpa_s,
355                              struct wpa_ssid *ssid)
356 {
357         struct wpa_driver_mesh_join_params params;
358         int ret = 0;
359
360         if (!ssid || !ssid->ssid || !ssid->ssid_len || !ssid->frequency) {
361                 ret = -ENOENT;
362                 goto out;
363         }
364
365         wpa_supplicant_mesh_deinit(wpa_s);
366
367         wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
368         wpa_s->group_cipher = WPA_CIPHER_NONE;
369         wpa_s->mgmt_group_cipher = 0;
370
371         os_memset(&params, 0, sizeof(params));
372         params.meshid = ssid->ssid;
373         params.meshid_len = ssid->ssid_len;
374         ibss_mesh_setup_freq(wpa_s, ssid, &params.freq);
375         wpa_s->mesh_ht_enabled = !!params.freq.ht_enabled;
376         wpa_s->mesh_vht_enabled = !!params.freq.vht_enabled;
377         if (params.freq.ht_enabled && params.freq.sec_channel_offset)
378                 ssid->ht40 = params.freq.sec_channel_offset;
379         if (wpa_s->mesh_vht_enabled) {
380                 ssid->vht = 1;
381                 switch (params.freq.bandwidth) {
382                 case 80:
383                         if (params.freq.center_freq2) {
384                                 ssid->max_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
385                                 ssid->vht_center_freq2 =
386                                         params.freq.center_freq2;
387                         } else {
388                                 ssid->max_oper_chwidth = VHT_CHANWIDTH_80MHZ;
389                         }
390                         break;
391                 case 160:
392                         ssid->max_oper_chwidth = VHT_CHANWIDTH_160MHZ;
393                         break;
394                 default:
395                         ssid->max_oper_chwidth = VHT_CHANWIDTH_USE_HT;
396                         break;
397                 }
398         }
399         if (ssid->beacon_int > 0)
400                 params.beacon_int = ssid->beacon_int;
401         else if (wpa_s->conf->beacon_int > 0)
402                 params.beacon_int = wpa_s->conf->beacon_int;
403         params.max_peer_links = wpa_s->conf->max_peer_links;
404
405         if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
406                 params.flags |= WPA_DRIVER_MESH_FLAG_SAE_AUTH;
407                 params.flags |= WPA_DRIVER_MESH_FLAG_AMPE;
408                 wpa_s->conf->user_mpm = 1;
409         }
410
411         if (wpa_s->conf->user_mpm) {
412                 params.flags |= WPA_DRIVER_MESH_FLAG_USER_MPM;
413                 params.conf.flags &= ~WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
414         } else {
415                 params.flags |= WPA_DRIVER_MESH_FLAG_DRIVER_MPM;
416                 params.conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
417         }
418         params.conf.peer_link_timeout = wpa_s->conf->mesh_max_inactivity;
419
420         if (wpa_supplicant_mesh_init(wpa_s, ssid)) {
421                 wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh");
422                 wpa_drv_leave_mesh(wpa_s);
423                 ret = -1;
424                 goto out;
425         }
426
427         if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
428                 wpa_s->pairwise_cipher = wpa_s->mesh_rsn->pairwise_cipher;
429                 wpa_s->group_cipher = wpa_s->mesh_rsn->group_cipher;
430                 wpa_s->mgmt_group_cipher = wpa_s->mesh_rsn->mgmt_group_cipher;
431         }
432
433         if (wpa_s->ifmsh) {
434                 params.ies = wpa_s->ifmsh->mconf->rsn_ie;
435                 params.ie_len = wpa_s->ifmsh->mconf->rsn_ie_len;
436                 params.basic_rates = wpa_s->ifmsh->basic_rates;
437         }
438
439         wpa_msg(wpa_s, MSG_INFO, "joining mesh %s",
440                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
441         ret = wpa_drv_join_mesh(wpa_s, &params);
442         if (ret)
443                 wpa_msg(wpa_s, MSG_ERROR, "mesh join error=%d", ret);
444
445         /* hostapd sets the interface down until we associate */
446         wpa_drv_set_operstate(wpa_s, 1);
447
448         if (!ret)
449                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
450
451 out:
452         return ret;
453 }
454
455
456 int wpa_supplicant_leave_mesh(struct wpa_supplicant *wpa_s)
457 {
458         int ret = 0;
459
460         wpa_msg(wpa_s, MSG_INFO, "leaving mesh");
461
462         /* Need to send peering close messages first */
463         wpa_supplicant_mesh_deinit(wpa_s);
464
465         ret = wpa_drv_leave_mesh(wpa_s);
466         if (ret)
467                 wpa_msg(wpa_s, MSG_ERROR, "mesh leave error=%d", ret);
468
469         wpa_drv_set_operstate(wpa_s, 1);
470
471         return ret;
472 }
473
474
475 static int mesh_attr_text(const u8 *ies, size_t ies_len, char *buf, char *end)
476 {
477         struct ieee802_11_elems elems;
478         char *mesh_id, *pos = buf;
479         u8 *bss_basic_rate_set;
480         int bss_basic_rate_set_len, ret, i;
481
482         if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) == ParseFailed)
483                 return -1;
484
485         if (elems.mesh_id_len < 1)
486                 return 0;
487
488         mesh_id = os_malloc(elems.mesh_id_len + 1);
489         if (mesh_id == NULL)
490                 return -1;
491
492         os_memcpy(mesh_id, elems.mesh_id, elems.mesh_id_len);
493         mesh_id[elems.mesh_id_len] = '\0';
494         ret = os_snprintf(pos, end - pos, "mesh_id=%s\n", mesh_id);
495         os_free(mesh_id);
496         if (os_snprintf_error(end - pos, ret))
497                 return pos - buf;
498         pos += ret;
499
500         if (elems.mesh_config_len > 6) {
501                 ret = os_snprintf(pos, end - pos,
502                                   "active_path_selection_protocol_id=0x%02x\n"
503                                   "active_path_selection_metric_id=0x%02x\n"
504                                   "congestion_control_mode_id=0x%02x\n"
505                                   "synchronization_method_id=0x%02x\n"
506                                   "authentication_protocol_id=0x%02x\n"
507                                   "mesh_formation_info=0x%02x\n"
508                                   "mesh_capability=0x%02x\n",
509                                   elems.mesh_config[0], elems.mesh_config[1],
510                                   elems.mesh_config[2], elems.mesh_config[3],
511                                   elems.mesh_config[4], elems.mesh_config[5],
512                                   elems.mesh_config[6]);
513                 if (os_snprintf_error(end - pos, ret))
514                         return pos - buf;
515                 pos += ret;
516         }
517
518         bss_basic_rate_set = os_malloc(elems.supp_rates_len +
519                 elems.ext_supp_rates_len);
520         if (bss_basic_rate_set == NULL)
521                 return -1;
522
523         bss_basic_rate_set_len = 0;
524         for (i = 0; i < elems.supp_rates_len; i++) {
525                 if (elems.supp_rates[i] & 0x80) {
526                         bss_basic_rate_set[bss_basic_rate_set_len++] =
527                                 (elems.supp_rates[i] & 0x7f) * 5;
528                 }
529         }
530         for (i = 0; i < elems.ext_supp_rates_len; i++) {
531                 if (elems.ext_supp_rates[i] & 0x80) {
532                         bss_basic_rate_set[bss_basic_rate_set_len++] =
533                                 (elems.ext_supp_rates[i] & 0x7f) * 5;
534                 }
535         }
536         if (bss_basic_rate_set_len > 0) {
537                 ret = os_snprintf(pos, end - pos, "bss_basic_rate_set=%d",
538                                   bss_basic_rate_set[0]);
539                 if (os_snprintf_error(end - pos, ret))
540                         goto fail;
541                 pos += ret;
542
543                 for (i = 1; i < bss_basic_rate_set_len; i++) {
544                         ret = os_snprintf(pos, end - pos, " %d",
545                                           bss_basic_rate_set[i]);
546                         if (os_snprintf_error(end - pos, ret))
547                                 goto fail;
548                         pos += ret;
549                 }
550
551                 ret = os_snprintf(pos, end - pos, "\n");
552                 if (os_snprintf_error(end - pos, ret))
553                         goto fail;
554                 pos += ret;
555         }
556 fail:
557         os_free(bss_basic_rate_set);
558
559         return pos - buf;
560 }
561
562
563 int wpas_mesh_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
564                                char *end)
565 {
566         return mesh_attr_text(ies, ies_len, buf, end);
567 }
568
569
570 static int wpas_mesh_get_ifname(struct wpa_supplicant *wpa_s, char *ifname,
571                                 size_t len)
572 {
573         char *ifname_ptr = wpa_s->ifname;
574         int res;
575
576         res = os_snprintf(ifname, len, "mesh-%s-%d", ifname_ptr,
577                           wpa_s->mesh_if_idx);
578         if (os_snprintf_error(len, res) ||
579             (os_strlen(ifname) >= IFNAMSIZ &&
580              os_strlen(wpa_s->ifname) < IFNAMSIZ)) {
581                 /* Try to avoid going over the IFNAMSIZ length limit */
582                 res = os_snprintf(ifname, len, "mesh-%d", wpa_s->mesh_if_idx);
583                 if (os_snprintf_error(len, res))
584                         return -1;
585         }
586         wpa_s->mesh_if_idx++;
587         return 0;
588 }
589
590
591 int wpas_mesh_add_interface(struct wpa_supplicant *wpa_s, char *ifname,
592                             size_t len)
593 {
594         struct wpa_interface iface;
595         struct wpa_supplicant *mesh_wpa_s;
596         u8 addr[ETH_ALEN];
597
598         if (ifname[0] == '\0' && wpas_mesh_get_ifname(wpa_s, ifname, len) < 0)
599                 return -1;
600
601         if (wpa_drv_if_add(wpa_s, WPA_IF_MESH, ifname, NULL, NULL, NULL, addr,
602                            NULL) < 0) {
603                 wpa_printf(MSG_ERROR,
604                            "mesh: Failed to create new mesh interface");
605                 return -1;
606         }
607         wpa_printf(MSG_INFO, "mesh: Created virtual interface %s addr "
608                    MACSTR, ifname, MAC2STR(addr));
609
610         os_memset(&iface, 0, sizeof(iface));
611         iface.ifname = ifname;
612         iface.driver = wpa_s->driver->name;
613         iface.driver_param = wpa_s->conf->driver_param;
614         iface.ctrl_interface = wpa_s->conf->ctrl_interface;
615
616         mesh_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
617         if (!mesh_wpa_s) {
618                 wpa_printf(MSG_ERROR,
619                            "mesh: Failed to create new wpa_supplicant interface");
620                 wpa_drv_if_remove(wpa_s, WPA_IF_MESH, ifname);
621                 return -1;
622         }
623         mesh_wpa_s->mesh_if_created = 1;
624         return 0;
625 }
626
627
628 int wpas_mesh_peer_remove(struct wpa_supplicant *wpa_s, const u8 *addr)
629 {
630         return mesh_mpm_close_peer(wpa_s, addr);
631 }
632
633
634 int wpas_mesh_peer_add(struct wpa_supplicant *wpa_s, const u8 *addr,
635                        int duration)
636 {
637         return mesh_mpm_connect_peer(wpa_s, addr, duration);
638 }