3d604f34b397b73b0536b0107e68df8e9016cae3
[libeap.git] / wpa_supplicant / ctrl_iface.c
1 /*
2  * WPA Supplicant / Control interface (shared code for all backends)
3  * Copyright (c) 2004-2010, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "common/ieee802_11_defs.h"
20 #include "common/wpa_ctrl.h"
21 #include "eap_peer/eap.h"
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "rsn_supp/wpa.h"
24 #include "rsn_supp/preauth.h"
25 #include "rsn_supp/pmksa_cache.h"
26 #include "l2_packet/l2_packet.h"
27 #include "wps/wps.h"
28 #include "config.h"
29 #include "wpa_supplicant_i.h"
30 #include "driver_i.h"
31 #include "wps_supplicant.h"
32 #include "ibss_rsn.h"
33 #include "ap.h"
34 #include "p2p_supplicant.h"
35 #include "p2p/p2p.h"
36 #include "notify.h"
37 #include "bss.h"
38 #include "scan.h"
39 #include "ctrl_iface.h"
40
41 extern struct wpa_driver_ops *wpa_drivers[];
42
43 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
44                                             char *buf, int len);
45 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
46                                                   char *buf, int len);
47
48
49 static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
50                                          char *cmd)
51 {
52         char *value;
53         int ret = 0;
54
55         value = os_strchr(cmd, ' ');
56         if (value == NULL)
57                 return -1;
58         *value++ = '\0';
59
60         wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
61         if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
62                 eapol_sm_configure(wpa_s->eapol,
63                                    atoi(value), -1, -1, -1);
64         } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
65                 eapol_sm_configure(wpa_s->eapol,
66                                    -1, atoi(value), -1, -1);
67         } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
68                 eapol_sm_configure(wpa_s->eapol,
69                                    -1, -1, atoi(value), -1);
70         } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
71                 eapol_sm_configure(wpa_s->eapol,
72                                    -1, -1, -1, atoi(value));
73         } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
74                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
75                                      atoi(value)))
76                         ret = -1;
77         } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
78                    0) {
79                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
80                                      atoi(value)))
81                         ret = -1;
82         } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
83                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
84                         ret = -1;
85         } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
86                 wpa_s->wps_fragment_size = atoi(value);
87         } else {
88                 value[-1] = '=';
89                 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
90                 if (ret == 0)
91                         wpa_supplicant_update_config(wpa_s);
92         }
93
94         return ret;
95 }
96
97
98 #ifdef IEEE8021X_EAPOL
99 static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
100                                              char *addr)
101 {
102         u8 bssid[ETH_ALEN];
103         struct wpa_ssid *ssid = wpa_s->current_ssid;
104
105         if (hwaddr_aton(addr, bssid)) {
106                 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
107                            "'%s'", addr);
108                 return -1;
109         }
110
111         wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
112         rsn_preauth_deinit(wpa_s->wpa);
113         if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
114                 return -1;
115
116         return 0;
117 }
118 #endif /* IEEE8021X_EAPOL */
119
120
121 #ifdef CONFIG_PEERKEY
122 /* MLME-STKSTART.request(peer) */
123 static int wpa_supplicant_ctrl_iface_stkstart(
124         struct wpa_supplicant *wpa_s, char *addr)
125 {
126         u8 peer[ETH_ALEN];
127
128         if (hwaddr_aton(addr, peer)) {
129                 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
130                            "address '%s'", addr);
131                 return -1;
132         }
133
134         wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
135                    MAC2STR(peer));
136
137         return wpa_sm_stkstart(wpa_s->wpa, peer);
138 }
139 #endif /* CONFIG_PEERKEY */
140
141
142 #ifdef CONFIG_IEEE80211R
143 static int wpa_supplicant_ctrl_iface_ft_ds(
144         struct wpa_supplicant *wpa_s, char *addr)
145 {
146         u8 target_ap[ETH_ALEN];
147         struct wpa_bss *bss;
148         const u8 *mdie;
149
150         if (hwaddr_aton(addr, target_ap)) {
151                 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
152                            "address '%s'", addr);
153                 return -1;
154         }
155
156         wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
157
158         bss = wpa_bss_get_bssid(wpa_s, target_ap);
159         if (bss)
160                 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
161         else
162                 mdie = NULL;
163
164         return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
165 }
166 #endif /* CONFIG_IEEE80211R */
167
168
169 #ifdef CONFIG_WPS
170 static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
171                                              char *cmd)
172 {
173         u8 bssid[ETH_ALEN], *_bssid = bssid;
174
175         if (cmd == NULL || os_strcmp(cmd, "any") == 0)
176                 _bssid = NULL;
177         else if (hwaddr_aton(cmd, bssid)) {
178                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
179                            cmd);
180                 return -1;
181         }
182
183 #ifdef CONFIG_AP
184         if (wpa_s->ap_iface)
185                 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid);
186 #endif /* CONFIG_AP */
187
188         return wpas_wps_start_pbc(wpa_s, _bssid, 0);
189 }
190
191
192 static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
193                                              char *cmd, char *buf,
194                                              size_t buflen)
195 {
196         u8 bssid[ETH_ALEN], *_bssid = bssid;
197         char *pin;
198         int ret;
199
200         pin = os_strchr(cmd, ' ');
201         if (pin)
202                 *pin++ = '\0';
203
204         if (os_strcmp(cmd, "any") == 0)
205                 _bssid = NULL;
206         else if (hwaddr_aton(cmd, bssid)) {
207                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
208                            cmd);
209                 return -1;
210         }
211
212 #ifdef CONFIG_AP
213         if (wpa_s->ap_iface)
214                 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
215                                                  buf, buflen);
216 #endif /* CONFIG_AP */
217
218         if (pin) {
219                 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
220                                          DEV_PW_DEFAULT);
221                 if (ret < 0)
222                         return -1;
223                 ret = os_snprintf(buf, buflen, "%s", pin);
224                 if (ret < 0 || (size_t) ret >= buflen)
225                         return -1;
226                 return ret;
227         }
228
229         ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
230         if (ret < 0)
231                 return -1;
232
233         /* Return the generated PIN */
234         ret = os_snprintf(buf, buflen, "%08d", ret);
235         if (ret < 0 || (size_t) ret >= buflen)
236                 return -1;
237         return ret;
238 }
239
240
241 #ifdef CONFIG_WPS_OOB
242 static int wpa_supplicant_ctrl_iface_wps_oob(struct wpa_supplicant *wpa_s,
243                                              char *cmd)
244 {
245         char *path, *method, *name;
246
247         path = os_strchr(cmd, ' ');
248         if (path == NULL)
249                 return -1;
250         *path++ = '\0';
251
252         method = os_strchr(path, ' ');
253         if (method == NULL)
254                 return -1;
255         *method++ = '\0';
256
257         name = os_strchr(method, ' ');
258         if (name != NULL)
259                 *name++ = '\0';
260
261         return wpas_wps_start_oob(wpa_s, cmd, path, method, name);
262 }
263 #endif /* CONFIG_WPS_OOB */
264
265
266 static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
267                                              char *cmd)
268 {
269         u8 bssid[ETH_ALEN], *_bssid = bssid;
270         char *pin;
271         char *new_ssid;
272         char *new_auth;
273         char *new_encr;
274         char *new_key;
275         struct wps_new_ap_settings ap;
276
277         pin = os_strchr(cmd, ' ');
278         if (pin == NULL)
279                 return -1;
280         *pin++ = '\0';
281
282         if (os_strcmp(cmd, "any") == 0)
283                 _bssid = NULL;
284         else if (hwaddr_aton(cmd, bssid)) {
285                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
286                            cmd);
287                 return -1;
288         }
289
290         new_ssid = os_strchr(pin, ' ');
291         if (new_ssid == NULL)
292                 return wpas_wps_start_reg(wpa_s, _bssid, pin, NULL);
293         *new_ssid++ = '\0';
294
295         new_auth = os_strchr(new_ssid, ' ');
296         if (new_auth == NULL)
297                 return -1;
298         *new_auth++ = '\0';
299
300         new_encr = os_strchr(new_auth, ' ');
301         if (new_encr == NULL)
302                 return -1;
303         *new_encr++ = '\0';
304
305         new_key = os_strchr(new_encr, ' ');
306         if (new_key == NULL)
307                 return -1;
308         *new_key++ = '\0';
309
310         os_memset(&ap, 0, sizeof(ap));
311         ap.ssid_hex = new_ssid;
312         ap.auth = new_auth;
313         ap.encr = new_encr;
314         ap.key_hex = new_key;
315         return wpas_wps_start_reg(wpa_s, _bssid, pin, &ap);
316 }
317
318
319 #ifdef CONFIG_WPS_ER
320 static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
321                                                 char *cmd)
322 {
323         char *uuid = cmd, *pin, *pos;
324         u8 addr_buf[ETH_ALEN], *addr = NULL;
325         pin = os_strchr(uuid, ' ');
326         if (pin == NULL)
327                 return -1;
328         *pin++ = '\0';
329         pos = os_strchr(pin, ' ');
330         if (pos) {
331                 *pos++ = '\0';
332                 if (hwaddr_aton(pos, addr_buf) == 0)
333                         addr = addr_buf;
334         }
335         return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
336 }
337
338
339 static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
340                                                   char *cmd)
341 {
342         char *uuid = cmd, *pin;
343         pin = os_strchr(uuid, ' ');
344         if (pin == NULL)
345                 return -1;
346         *pin++ = '\0';
347         return wpas_wps_er_learn(wpa_s, uuid, pin);
348 }
349
350
351 static int wpa_supplicant_ctrl_iface_wps_er_config(
352         struct wpa_supplicant *wpa_s, char *cmd)
353 {
354         char *pin;
355         char *new_ssid;
356         char *new_auth;
357         char *new_encr;
358         char *new_key;
359         struct wps_new_ap_settings ap;
360
361         pin = os_strchr(cmd, ' ');
362         if (pin == NULL)
363                 return -1;
364         *pin++ = '\0';
365
366         new_ssid = os_strchr(pin, ' ');
367         if (new_ssid == NULL)
368                 return -1;
369         *new_ssid++ = '\0';
370
371         new_auth = os_strchr(new_ssid, ' ');
372         if (new_auth == NULL)
373                 return -1;
374         *new_auth++ = '\0';
375
376         new_encr = os_strchr(new_auth, ' ');
377         if (new_encr == NULL)
378                 return -1;
379         *new_encr++ = '\0';
380
381         new_key = os_strchr(new_encr, ' ');
382         if (new_key == NULL)
383                 return -1;
384         *new_key++ = '\0';
385
386         os_memset(&ap, 0, sizeof(ap));
387         ap.ssid_hex = new_ssid;
388         ap.auth = new_auth;
389         ap.encr = new_encr;
390         ap.key_hex = new_key;
391         return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
392 }
393 #endif /* CONFIG_WPS_ER */
394
395 #endif /* CONFIG_WPS */
396
397
398 #ifdef CONFIG_IBSS_RSN
399 static int wpa_supplicant_ctrl_iface_ibss_rsn(
400         struct wpa_supplicant *wpa_s, char *addr)
401 {
402         u8 peer[ETH_ALEN];
403
404         if (hwaddr_aton(addr, peer)) {
405                 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
406                            "address '%s'", addr);
407                 return -1;
408         }
409
410         wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
411                    MAC2STR(peer));
412
413         return ibss_rsn_start(wpa_s->ibss_rsn, peer);
414 }
415 #endif /* CONFIG_IBSS_RSN */
416
417
418 static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
419                                               char *rsp)
420 {
421 #ifdef IEEE8021X_EAPOL
422         char *pos, *id_pos;
423         int id;
424         struct wpa_ssid *ssid;
425         struct eap_peer_config *eap;
426
427         pos = os_strchr(rsp, '-');
428         if (pos == NULL)
429                 return -1;
430         *pos++ = '\0';
431         id_pos = pos;
432         pos = os_strchr(pos, ':');
433         if (pos == NULL)
434                 return -1;
435         *pos++ = '\0';
436         id = atoi(id_pos);
437         wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
438         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
439                               (u8 *) pos, os_strlen(pos));
440
441         ssid = wpa_config_get_network(wpa_s->conf, id);
442         if (ssid == NULL) {
443                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
444                            "to update", id);
445                 return -1;
446         }
447         eap = &ssid->eap;
448
449         if (os_strcmp(rsp, "IDENTITY") == 0) {
450                 os_free(eap->identity);
451                 eap->identity = (u8 *) os_strdup(pos);
452                 eap->identity_len = os_strlen(pos);
453                 eap->pending_req_identity = 0;
454                 if (ssid == wpa_s->current_ssid)
455                         wpa_s->reassociate = 1;
456         } else if (os_strcmp(rsp, "PASSWORD") == 0) {
457                 os_free(eap->password);
458                 eap->password = (u8 *) os_strdup(pos);
459                 eap->password_len = os_strlen(pos);
460                 eap->pending_req_password = 0;
461                 if (ssid == wpa_s->current_ssid)
462                         wpa_s->reassociate = 1;
463         } else if (os_strcmp(rsp, "NEW_PASSWORD") == 0) {
464                 os_free(eap->new_password);
465                 eap->new_password = (u8 *) os_strdup(pos);
466                 eap->new_password_len = os_strlen(pos);
467                 eap->pending_req_new_password = 0;
468                 if (ssid == wpa_s->current_ssid)
469                         wpa_s->reassociate = 1;
470         } else if (os_strcmp(rsp, "PIN") == 0) {
471                 os_free(eap->pin);
472                 eap->pin = os_strdup(pos);
473                 eap->pending_req_pin = 0;
474                 if (ssid == wpa_s->current_ssid)
475                         wpa_s->reassociate = 1;
476         } else if (os_strcmp(rsp, "OTP") == 0) {
477                 os_free(eap->otp);
478                 eap->otp = (u8 *) os_strdup(pos);
479                 eap->otp_len = os_strlen(pos);
480                 os_free(eap->pending_req_otp);
481                 eap->pending_req_otp = NULL;
482                 eap->pending_req_otp_len = 0;
483         } else if (os_strcmp(rsp, "PASSPHRASE") == 0) {
484                 os_free(eap->private_key_passwd);
485                 eap->private_key_passwd = (u8 *) os_strdup(pos);
486                 eap->pending_req_passphrase = 0;
487                 if (ssid == wpa_s->current_ssid)
488                         wpa_s->reassociate = 1;
489         } else {
490                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", rsp);
491                 return -1;
492         }
493
494         return 0;
495 #else /* IEEE8021X_EAPOL */
496         wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
497         return -1;
498 #endif /* IEEE8021X_EAPOL */
499 }
500
501
502 static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
503                                             const char *params,
504                                             char *buf, size_t buflen)
505 {
506         char *pos, *end, tmp[30];
507         int res, verbose, ret;
508
509         verbose = os_strcmp(params, "-VERBOSE") == 0;
510         pos = buf;
511         end = buf + buflen;
512         if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
513                 struct wpa_ssid *ssid = wpa_s->current_ssid;
514                 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
515                                   MAC2STR(wpa_s->bssid));
516                 if (ret < 0 || ret >= end - pos)
517                         return pos - buf;
518                 pos += ret;
519                 if (ssid) {
520                         u8 *_ssid = ssid->ssid;
521                         size_t ssid_len = ssid->ssid_len;
522                         u8 ssid_buf[MAX_SSID_LEN];
523                         if (ssid_len == 0) {
524                                 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
525                                 if (_res < 0)
526                                         ssid_len = 0;
527                                 else
528                                         ssid_len = _res;
529                                 _ssid = ssid_buf;
530                         }
531                         ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
532                                           wpa_ssid_txt(_ssid, ssid_len),
533                                           ssid->id);
534                         if (ret < 0 || ret >= end - pos)
535                                 return pos - buf;
536                         pos += ret;
537
538                         if (ssid->id_str) {
539                                 ret = os_snprintf(pos, end - pos,
540                                                   "id_str=%s\n",
541                                                   ssid->id_str);
542                                 if (ret < 0 || ret >= end - pos)
543                                         return pos - buf;
544                                 pos += ret;
545                         }
546
547                         switch (ssid->mode) {
548                         case WPAS_MODE_INFRA:
549                                 ret = os_snprintf(pos, end - pos,
550                                                   "mode=station\n");
551                                 break;
552                         case WPAS_MODE_IBSS:
553                                 ret = os_snprintf(pos, end - pos,
554                                                   "mode=IBSS\n");
555                                 break;
556                         case WPAS_MODE_AP:
557                                 ret = os_snprintf(pos, end - pos,
558                                                   "mode=AP\n");
559                                 break;
560                         case WPAS_MODE_P2P_GO:
561                                 ret = os_snprintf(pos, end - pos,
562                                                   "mode=P2P GO\n");
563                                 break;
564                         case WPAS_MODE_P2P_GROUP_FORMATION:
565                                 ret = os_snprintf(pos, end - pos,
566                                                   "mode=P2P GO - group "
567                                                   "formation\n");
568                                 break;
569                         default:
570                                 ret = 0;
571                                 break;
572                         }
573                         if (ret < 0 || ret >= end - pos)
574                                 return pos - buf;
575                         pos += ret;
576                 }
577
578 #ifdef CONFIG_AP
579                 if (wpa_s->ap_iface) {
580                         pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
581                                                             end - pos,
582                                                             verbose);
583                 } else
584 #endif /* CONFIG_AP */
585                 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
586         }
587         ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
588                           wpa_supplicant_state_txt(wpa_s->wpa_state));
589         if (ret < 0 || ret >= end - pos)
590                 return pos - buf;
591         pos += ret;
592
593         if (wpa_s->l2 &&
594             l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
595                 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
596                 if (ret < 0 || ret >= end - pos)
597                         return pos - buf;
598                 pos += ret;
599         }
600
601 #ifdef CONFIG_P2P
602         if (wpa_s->global->p2p) {
603                 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
604                                   "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
605                 if (ret < 0 || ret >= end - pos)
606                         return pos - buf;
607                 pos += ret;
608         }
609 #endif /* CONFIG_P2P */
610
611         if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
612             wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
613                 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
614                                           verbose);
615                 if (res >= 0)
616                         pos += res;
617         }
618
619         res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
620         if (res >= 0)
621                 pos += res;
622
623         return pos - buf;
624 }
625
626
627 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
628                                            char *cmd)
629 {
630         char *pos;
631         int id;
632         struct wpa_ssid *ssid;
633         u8 bssid[ETH_ALEN];
634
635         /* cmd: "<network id> <BSSID>" */
636         pos = os_strchr(cmd, ' ');
637         if (pos == NULL)
638                 return -1;
639         *pos++ = '\0';
640         id = atoi(cmd);
641         wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
642         if (hwaddr_aton(pos, bssid)) {
643                 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
644                 return -1;
645         }
646
647         ssid = wpa_config_get_network(wpa_s->conf, id);
648         if (ssid == NULL) {
649                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
650                            "to update", id);
651                 return -1;
652         }
653
654         os_memcpy(ssid->bssid, bssid, ETH_ALEN);
655         ssid->bssid_set = !is_zero_ether_addr(bssid);
656
657         return 0;
658 }
659
660
661 static int wpa_supplicant_ctrl_iface_list_networks(
662         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
663 {
664         char *pos, *end;
665         struct wpa_ssid *ssid;
666         int ret;
667
668         pos = buf;
669         end = buf + buflen;
670         ret = os_snprintf(pos, end - pos,
671                           "network id / ssid / bssid / flags\n");
672         if (ret < 0 || ret >= end - pos)
673                 return pos - buf;
674         pos += ret;
675
676         ssid = wpa_s->conf->ssid;
677         while (ssid) {
678                 ret = os_snprintf(pos, end - pos, "%d\t%s",
679                                   ssid->id,
680                                   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
681                 if (ret < 0 || ret >= end - pos)
682                         return pos - buf;
683                 pos += ret;
684                 if (ssid->bssid_set) {
685                         ret = os_snprintf(pos, end - pos, "\t" MACSTR,
686                                           MAC2STR(ssid->bssid));
687                 } else {
688                         ret = os_snprintf(pos, end - pos, "\tany");
689                 }
690                 if (ret < 0 || ret >= end - pos)
691                         return pos - buf;
692                 pos += ret;
693                 ret = os_snprintf(pos, end - pos, "\t%s%s%s",
694                                   ssid == wpa_s->current_ssid ?
695                                   "[CURRENT]" : "",
696                                   ssid->disabled ? "[DISABLED]" : "",
697                                   ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
698                                   "");
699                 if (ret < 0 || ret >= end - pos)
700                         return pos - buf;
701                 pos += ret;
702                 ret = os_snprintf(pos, end - pos, "\n");
703                 if (ret < 0 || ret >= end - pos)
704                         return pos - buf;
705                 pos += ret;
706
707                 ssid = ssid->next;
708         }
709
710         return pos - buf;
711 }
712
713
714 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
715 {
716         int first = 1, ret;
717         ret = os_snprintf(pos, end - pos, "-");
718         if (ret < 0 || ret >= end - pos)
719                 return pos;
720         pos += ret;
721         if (cipher & WPA_CIPHER_NONE) {
722                 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
723                 if (ret < 0 || ret >= end - pos)
724                         return pos;
725                 pos += ret;
726                 first = 0;
727         }
728         if (cipher & WPA_CIPHER_WEP40) {
729                 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
730                 if (ret < 0 || ret >= end - pos)
731                         return pos;
732                 pos += ret;
733                 first = 0;
734         }
735         if (cipher & WPA_CIPHER_WEP104) {
736                 ret = os_snprintf(pos, end - pos, "%sWEP104",
737                                   first ? "" : "+");
738                 if (ret < 0 || ret >= end - pos)
739                         return pos;
740                 pos += ret;
741                 first = 0;
742         }
743         if (cipher & WPA_CIPHER_TKIP) {
744                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
745                 if (ret < 0 || ret >= end - pos)
746                         return pos;
747                 pos += ret;
748                 first = 0;
749         }
750         if (cipher & WPA_CIPHER_CCMP) {
751                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
752                 if (ret < 0 || ret >= end - pos)
753                         return pos;
754                 pos += ret;
755                 first = 0;
756         }
757         return pos;
758 }
759
760
761 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
762                                     const u8 *ie, size_t ie_len)
763 {
764         struct wpa_ie_data data;
765         int first, ret;
766
767         ret = os_snprintf(pos, end - pos, "[%s-", proto);
768         if (ret < 0 || ret >= end - pos)
769                 return pos;
770         pos += ret;
771
772         if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
773                 ret = os_snprintf(pos, end - pos, "?]");
774                 if (ret < 0 || ret >= end - pos)
775                         return pos;
776                 pos += ret;
777                 return pos;
778         }
779
780         first = 1;
781         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
782                 ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
783                 if (ret < 0 || ret >= end - pos)
784                         return pos;
785                 pos += ret;
786                 first = 0;
787         }
788         if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
789                 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
790                 if (ret < 0 || ret >= end - pos)
791                         return pos;
792                 pos += ret;
793                 first = 0;
794         }
795         if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
796                 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
797                 if (ret < 0 || ret >= end - pos)
798                         return pos;
799                 pos += ret;
800                 first = 0;
801         }
802 #ifdef CONFIG_IEEE80211R
803         if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
804                 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
805                                   first ? "" : "+");
806                 if (ret < 0 || ret >= end - pos)
807                         return pos;
808                 pos += ret;
809                 first = 0;
810         }
811         if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
812                 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
813                                   first ? "" : "+");
814                 if (ret < 0 || ret >= end - pos)
815                         return pos;
816                 pos += ret;
817                 first = 0;
818         }
819 #endif /* CONFIG_IEEE80211R */
820 #ifdef CONFIG_IEEE80211W
821         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
822                 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
823                                   first ? "" : "+");
824                 if (ret < 0 || ret >= end - pos)
825                         return pos;
826                 pos += ret;
827                 first = 0;
828         }
829         if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
830                 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
831                                   first ? "" : "+");
832                 if (ret < 0 || ret >= end - pos)
833                         return pos;
834                 pos += ret;
835                 first = 0;
836         }
837 #endif /* CONFIG_IEEE80211W */
838
839         pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
840
841         if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
842                 ret = os_snprintf(pos, end - pos, "-preauth");
843                 if (ret < 0 || ret >= end - pos)
844                         return pos;
845                 pos += ret;
846         }
847
848         ret = os_snprintf(pos, end - pos, "]");
849         if (ret < 0 || ret >= end - pos)
850                 return pos;
851         pos += ret;
852
853         return pos;
854 }
855
856
857 #ifdef CONFIG_WPS
858 static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
859                                             char *pos, char *end,
860                                             struct wpabuf *wps_ie)
861 {
862         int ret;
863         const char *txt;
864
865         if (wps_ie == NULL)
866                 return pos;
867         if (wps_is_selected_pbc_registrar(wps_ie))
868                 txt = "[WPS-PBC]";
869 #ifdef CONFIG_WPS2
870         else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
871                 txt = "[WPS-AUTH]";
872 #endif /* CONFIG_WPS2 */
873         else if (wps_is_selected_pin_registrar(wps_ie))
874                 txt = "[WPS-PIN]";
875         else
876                 txt = "[WPS]";
877
878         ret = os_snprintf(pos, end - pos, "%s", txt);
879         if (ret >= 0 && ret < end - pos)
880                 pos += ret;
881         wpabuf_free(wps_ie);
882         return pos;
883 }
884 #endif /* CONFIG_WPS */
885
886
887 static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
888                                         char *pos, char *end,
889                                         const struct wpa_bss *bss)
890 {
891 #ifdef CONFIG_WPS
892         struct wpabuf *wps_ie;
893         wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
894         return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
895 #else /* CONFIG_WPS */
896         return pos;
897 #endif /* CONFIG_WPS */
898 }
899
900
901 /* Format one result on one text line into a buffer. */
902 static int wpa_supplicant_ctrl_iface_scan_result(
903         struct wpa_supplicant *wpa_s,
904         const struct wpa_bss *bss, char *buf, size_t buflen)
905 {
906         char *pos, *end;
907         int ret;
908         const u8 *ie, *ie2, *p2p;
909
910         p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
911         if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
912             os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
913             0)
914                 return 0; /* Do not show P2P listen discovery results here */
915
916         pos = buf;
917         end = buf + buflen;
918
919         ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
920                           MAC2STR(bss->bssid), bss->freq, bss->level);
921         if (ret < 0 || ret >= end - pos)
922                 return pos - buf;
923         pos += ret;
924         ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
925         if (ie)
926                 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
927         ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
928         if (ie2)
929                 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
930         pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
931         if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
932                 ret = os_snprintf(pos, end - pos, "[WEP]");
933                 if (ret < 0 || ret >= end - pos)
934                         return pos - buf;
935                 pos += ret;
936         }
937         if (bss->caps & IEEE80211_CAP_IBSS) {
938                 ret = os_snprintf(pos, end - pos, "[IBSS]");
939                 if (ret < 0 || ret >= end - pos)
940                         return pos - buf;
941                 pos += ret;
942         }
943         if (bss->caps & IEEE80211_CAP_ESS) {
944                 ret = os_snprintf(pos, end - pos, "[ESS]");
945                 if (ret < 0 || ret >= end - pos)
946                         return pos - buf;
947                 pos += ret;
948         }
949         if (p2p) {
950                 ret = os_snprintf(pos, end - pos, "[P2P]");
951                 if (ret < 0 || ret >= end - pos)
952                         return pos - buf;
953                 pos += ret;
954         }
955
956         ret = os_snprintf(pos, end - pos, "\t%s",
957                           wpa_ssid_txt(bss->ssid, bss->ssid_len));
958         if (ret < 0 || ret >= end - pos)
959                 return pos - buf;
960         pos += ret;
961
962         ret = os_snprintf(pos, end - pos, "\n");
963         if (ret < 0 || ret >= end - pos)
964                 return pos - buf;
965         pos += ret;
966
967         return pos - buf;
968 }
969
970
971 static int wpa_supplicant_ctrl_iface_scan_results(
972         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
973 {
974         char *pos, *end;
975         struct wpa_bss *bss;
976         int ret;
977
978         pos = buf;
979         end = buf + buflen;
980         ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
981                           "flags / ssid\n");
982         if (ret < 0 || ret >= end - pos)
983                 return pos - buf;
984         pos += ret;
985
986         dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
987                 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
988                                                             end - pos);
989                 if (ret < 0 || ret >= end - pos)
990                         return pos - buf;
991                 pos += ret;
992         }
993
994         return pos - buf;
995 }
996
997
998 static int wpa_supplicant_ctrl_iface_select_network(
999         struct wpa_supplicant *wpa_s, char *cmd)
1000 {
1001         int id;
1002         struct wpa_ssid *ssid;
1003
1004         /* cmd: "<network id>" or "any" */
1005         if (os_strcmp(cmd, "any") == 0) {
1006                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
1007                 ssid = NULL;
1008         } else {
1009                 id = atoi(cmd);
1010                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
1011
1012                 ssid = wpa_config_get_network(wpa_s->conf, id);
1013                 if (ssid == NULL) {
1014                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1015                                    "network id=%d", id);
1016                         return -1;
1017                 }
1018                 if (ssid->disabled == 2) {
1019                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1020                                    "SELECT_NETWORK with persistent P2P group");
1021                         return -1;
1022                 }
1023         }
1024
1025         wpa_supplicant_select_network(wpa_s, ssid);
1026
1027         return 0;
1028 }
1029
1030
1031 static int wpa_supplicant_ctrl_iface_enable_network(
1032         struct wpa_supplicant *wpa_s, char *cmd)
1033 {
1034         int id;
1035         struct wpa_ssid *ssid;
1036
1037         /* cmd: "<network id>" or "all" */
1038         if (os_strcmp(cmd, "all") == 0) {
1039                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
1040                 ssid = NULL;
1041         } else {
1042                 id = atoi(cmd);
1043                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
1044
1045                 ssid = wpa_config_get_network(wpa_s->conf, id);
1046                 if (ssid == NULL) {
1047                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1048                                    "network id=%d", id);
1049                         return -1;
1050                 }
1051                 if (ssid->disabled == 2) {
1052                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1053                                    "ENABLE_NETWORK with persistent P2P group");
1054                         return -1;
1055                 }
1056         }
1057         wpa_supplicant_enable_network(wpa_s, ssid);
1058
1059         return 0;
1060 }
1061
1062
1063 static int wpa_supplicant_ctrl_iface_disable_network(
1064         struct wpa_supplicant *wpa_s, char *cmd)
1065 {
1066         int id;
1067         struct wpa_ssid *ssid;
1068
1069         /* cmd: "<network id>" or "all" */
1070         if (os_strcmp(cmd, "all") == 0) {
1071                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
1072                 ssid = NULL;
1073         } else {
1074                 id = atoi(cmd);
1075                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
1076
1077                 ssid = wpa_config_get_network(wpa_s->conf, id);
1078                 if (ssid == NULL) {
1079                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1080                                    "network id=%d", id);
1081                         return -1;
1082                 }
1083                 if (ssid->disabled == 2) {
1084                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1085                                    "DISABLE_NETWORK with persistent P2P "
1086                                    "group");
1087                         return -1;
1088                 }
1089         }
1090         wpa_supplicant_disable_network(wpa_s, ssid);
1091
1092         return 0;
1093 }
1094
1095
1096 static int wpa_supplicant_ctrl_iface_add_network(
1097         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1098 {
1099         struct wpa_ssid *ssid;
1100         int ret;
1101
1102         wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
1103
1104         ssid = wpa_config_add_network(wpa_s->conf);
1105         if (ssid == NULL)
1106                 return -1;
1107
1108         wpas_notify_network_added(wpa_s, ssid);
1109
1110         ssid->disabled = 1;
1111         wpa_config_set_network_defaults(ssid);
1112
1113         ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
1114         if (ret < 0 || (size_t) ret >= buflen)
1115                 return -1;
1116         return ret;
1117 }
1118
1119
1120 static int wpa_supplicant_ctrl_iface_remove_network(
1121         struct wpa_supplicant *wpa_s, char *cmd)
1122 {
1123         int id;
1124         struct wpa_ssid *ssid;
1125
1126         /* cmd: "<network id>" or "all" */
1127         if (os_strcmp(cmd, "all") == 0) {
1128                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
1129                 ssid = wpa_s->conf->ssid;
1130                 while (ssid) {
1131                         struct wpa_ssid *remove_ssid = ssid;
1132                         id = ssid->id;
1133                         ssid = ssid->next;
1134                         wpas_notify_network_removed(wpa_s, remove_ssid);
1135                         wpa_config_remove_network(wpa_s->conf, id);
1136                 }
1137                 if (wpa_s->current_ssid) {
1138                         eapol_sm_invalidate_cached_session(wpa_s->eapol);
1139                         wpa_supplicant_disassociate(wpa_s,
1140                                                     WLAN_REASON_DEAUTH_LEAVING);
1141                 }
1142                 return 0;
1143         }
1144
1145         id = atoi(cmd);
1146         wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
1147
1148         ssid = wpa_config_get_network(wpa_s->conf, id);
1149         if (ssid == NULL ||
1150             wpa_config_remove_network(wpa_s->conf, id) < 0) {
1151                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1152                            "id=%d", id);
1153                 return -1;
1154         }
1155
1156         if (ssid == wpa_s->current_ssid) {
1157                 /*
1158                  * Invalidate the EAP session cache if the current network is
1159                  * removed.
1160                  */
1161                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1162
1163                 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1164         }
1165
1166         return 0;
1167 }
1168
1169
1170 static int wpa_supplicant_ctrl_iface_set_network(
1171         struct wpa_supplicant *wpa_s, char *cmd)
1172 {
1173         int id;
1174         struct wpa_ssid *ssid;
1175         char *name, *value;
1176
1177         /* cmd: "<network id> <variable name> <value>" */
1178         name = os_strchr(cmd, ' ');
1179         if (name == NULL)
1180                 return -1;
1181         *name++ = '\0';
1182
1183         value = os_strchr(name, ' ');
1184         if (value == NULL)
1185                 return -1;
1186         *value++ = '\0';
1187
1188         id = atoi(cmd);
1189         wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
1190                    id, name);
1191         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1192                               (u8 *) value, os_strlen(value));
1193
1194         ssid = wpa_config_get_network(wpa_s->conf, id);
1195         if (ssid == NULL) {
1196                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1197                            "id=%d", id);
1198                 return -1;
1199         }
1200
1201         if (wpa_config_set(ssid, name, value, 0) < 0) {
1202                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
1203                            "variable '%s'", name);
1204                 return -1;
1205         }
1206
1207         if (wpa_s->current_ssid == ssid) {
1208                 /*
1209                  * Invalidate the EAP session cache if anything in the current
1210                  * configuration changes.
1211                  */
1212                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1213         }
1214
1215         if ((os_strcmp(name, "psk") == 0 &&
1216              value[0] == '"' && ssid->ssid_len) ||
1217             (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
1218                 wpa_config_update_psk(ssid);
1219         else if (os_strcmp(name, "priority") == 0)
1220                 wpa_config_update_prio_list(wpa_s->conf);
1221
1222         return 0;
1223 }
1224
1225
1226 static int wpa_supplicant_ctrl_iface_get_network(
1227         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1228 {
1229         int id;
1230         size_t res;
1231         struct wpa_ssid *ssid;
1232         char *name, *value;
1233
1234         /* cmd: "<network id> <variable name>" */
1235         name = os_strchr(cmd, ' ');
1236         if (name == NULL || buflen == 0)
1237                 return -1;
1238         *name++ = '\0';
1239
1240         id = atoi(cmd);
1241         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
1242                    id, name);
1243
1244         ssid = wpa_config_get_network(wpa_s->conf, id);
1245         if (ssid == NULL) {
1246                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1247                            "id=%d", id);
1248                 return -1;
1249         }
1250
1251         value = wpa_config_get_no_key(ssid, name);
1252         if (value == NULL) {
1253                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
1254                            "variable '%s'", name);
1255                 return -1;
1256         }
1257
1258         res = os_strlcpy(buf, value, buflen);
1259         if (res >= buflen) {
1260                 os_free(value);
1261                 return -1;
1262         }
1263
1264         os_free(value);
1265
1266         return res;
1267 }
1268
1269
1270 #ifndef CONFIG_NO_CONFIG_WRITE
1271 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
1272 {
1273         int ret;
1274
1275         if (!wpa_s->conf->update_config) {
1276                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
1277                            "to update configuration (update_config=0)");
1278                 return -1;
1279         }
1280
1281         ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
1282         if (ret) {
1283                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
1284                            "update configuration");
1285         } else {
1286                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
1287                            " updated");
1288         }
1289
1290         return ret;
1291 }
1292 #endif /* CONFIG_NO_CONFIG_WRITE */
1293
1294
1295 static int ctrl_iface_get_capability_pairwise(int res, char *strict,
1296                                               struct wpa_driver_capa *capa,
1297                                               char *buf, size_t buflen)
1298 {
1299         int ret, first = 1;
1300         char *pos, *end;
1301         size_t len;
1302
1303         pos = buf;
1304         end = pos + buflen;
1305
1306         if (res < 0) {
1307                 if (strict)
1308                         return 0;
1309                 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
1310                 if (len >= buflen)
1311                         return -1;
1312                 return len;
1313         }
1314
1315         if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
1316                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
1317                 if (ret < 0 || ret >= end - pos)
1318                         return pos - buf;
1319                 pos += ret;
1320                 first = 0;
1321         }
1322
1323         if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
1324                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
1325                 if (ret < 0 || ret >= end - pos)
1326                         return pos - buf;
1327                 pos += ret;
1328                 first = 0;
1329         }
1330
1331         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
1332                 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
1333                 if (ret < 0 || ret >= end - pos)
1334                         return pos - buf;
1335                 pos += ret;
1336                 first = 0;
1337         }
1338
1339         return pos - buf;
1340 }
1341
1342
1343 static int ctrl_iface_get_capability_group(int res, char *strict,
1344                                            struct wpa_driver_capa *capa,
1345                                            char *buf, size_t buflen)
1346 {
1347         int ret, first = 1;
1348         char *pos, *end;
1349         size_t len;
1350
1351         pos = buf;
1352         end = pos + buflen;
1353
1354         if (res < 0) {
1355                 if (strict)
1356                         return 0;
1357                 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
1358                 if (len >= buflen)
1359                         return -1;
1360                 return len;
1361         }
1362
1363         if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
1364                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
1365                 if (ret < 0 || ret >= end - pos)
1366                         return pos - buf;
1367                 pos += ret;
1368                 first = 0;
1369         }
1370
1371         if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
1372                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
1373                 if (ret < 0 || ret >= end - pos)
1374                         return pos - buf;
1375                 pos += ret;
1376                 first = 0;
1377         }
1378
1379         if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
1380                 ret = os_snprintf(pos, end - pos, "%sWEP104",
1381                                   first ? "" : " ");
1382                 if (ret < 0 || ret >= end - pos)
1383                         return pos - buf;
1384                 pos += ret;
1385                 first = 0;
1386         }
1387
1388         if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
1389                 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
1390                 if (ret < 0 || ret >= end - pos)
1391                         return pos - buf;
1392                 pos += ret;
1393                 first = 0;
1394         }
1395
1396         return pos - buf;
1397 }
1398
1399
1400 static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
1401                                               struct wpa_driver_capa *capa,
1402                                               char *buf, size_t buflen)
1403 {
1404         int ret;
1405         char *pos, *end;
1406         size_t len;
1407
1408         pos = buf;
1409         end = pos + buflen;
1410
1411         if (res < 0) {
1412                 if (strict)
1413                         return 0;
1414                 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
1415                                  "NONE", buflen);
1416                 if (len >= buflen)
1417                         return -1;
1418                 return len;
1419         }
1420
1421         ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
1422         if (ret < 0 || ret >= end - pos)
1423                 return pos - buf;
1424         pos += ret;
1425
1426         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1427                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
1428                 ret = os_snprintf(pos, end - pos, " WPA-EAP");
1429                 if (ret < 0 || ret >= end - pos)
1430                         return pos - buf;
1431                 pos += ret;
1432         }
1433
1434         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
1435                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
1436                 ret = os_snprintf(pos, end - pos, " WPA-PSK");
1437                 if (ret < 0 || ret >= end - pos)
1438                         return pos - buf;
1439                 pos += ret;
1440         }
1441
1442         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
1443                 ret = os_snprintf(pos, end - pos, " WPA-NONE");
1444                 if (ret < 0 || ret >= end - pos)
1445                         return pos - buf;
1446                 pos += ret;
1447         }
1448
1449         return pos - buf;
1450 }
1451
1452
1453 static int ctrl_iface_get_capability_proto(int res, char *strict,
1454                                            struct wpa_driver_capa *capa,
1455                                            char *buf, size_t buflen)
1456 {
1457         int ret, first = 1;
1458         char *pos, *end;
1459         size_t len;
1460
1461         pos = buf;
1462         end = pos + buflen;
1463
1464         if (res < 0) {
1465                 if (strict)
1466                         return 0;
1467                 len = os_strlcpy(buf, "RSN WPA", buflen);
1468                 if (len >= buflen)
1469                         return -1;
1470                 return len;
1471         }
1472
1473         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1474                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
1475                 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
1476                 if (ret < 0 || ret >= end - pos)
1477                         return pos - buf;
1478                 pos += ret;
1479                 first = 0;
1480         }
1481
1482         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1483                               WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
1484                 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
1485                 if (ret < 0 || ret >= end - pos)
1486                         return pos - buf;
1487                 pos += ret;
1488                 first = 0;
1489         }
1490
1491         return pos - buf;
1492 }
1493
1494
1495 static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
1496                                               struct wpa_driver_capa *capa,
1497                                               char *buf, size_t buflen)
1498 {
1499         int ret, first = 1;
1500         char *pos, *end;
1501         size_t len;
1502
1503         pos = buf;
1504         end = pos + buflen;
1505
1506         if (res < 0) {
1507                 if (strict)
1508                         return 0;
1509                 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
1510                 if (len >= buflen)
1511                         return -1;
1512                 return len;
1513         }
1514
1515         if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
1516                 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
1517                 if (ret < 0 || ret >= end - pos)
1518                         return pos - buf;
1519                 pos += ret;
1520                 first = 0;
1521         }
1522
1523         if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
1524                 ret = os_snprintf(pos, end - pos, "%sSHARED",
1525                                   first ? "" : " ");
1526                 if (ret < 0 || ret >= end - pos)
1527                         return pos - buf;
1528                 pos += ret;
1529                 first = 0;
1530         }
1531
1532         if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
1533                 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
1534                 if (ret < 0 || ret >= end - pos)
1535                         return pos - buf;
1536                 pos += ret;
1537                 first = 0;
1538         }
1539
1540         return pos - buf;
1541 }
1542
1543
1544 static int wpa_supplicant_ctrl_iface_get_capability(
1545         struct wpa_supplicant *wpa_s, const char *_field, char *buf,
1546         size_t buflen)
1547 {
1548         struct wpa_driver_capa capa;
1549         int res;
1550         char *strict;
1551         char field[30];
1552         size_t len;
1553
1554         /* Determine whether or not strict checking was requested */
1555         len = os_strlcpy(field, _field, sizeof(field));
1556         if (len >= sizeof(field))
1557                 return -1;
1558         strict = os_strchr(field, ' ');
1559         if (strict != NULL) {
1560                 *strict++ = '\0';
1561                 if (os_strcmp(strict, "strict") != 0)
1562                         return -1;
1563         }
1564
1565         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
1566                 field, strict ? strict : "");
1567
1568         if (os_strcmp(field, "eap") == 0) {
1569                 return eap_get_names(buf, buflen);
1570         }
1571
1572         res = wpa_drv_get_capa(wpa_s, &capa);
1573
1574         if (os_strcmp(field, "pairwise") == 0)
1575                 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
1576                                                           buf, buflen);
1577
1578         if (os_strcmp(field, "group") == 0)
1579                 return ctrl_iface_get_capability_group(res, strict, &capa,
1580                                                        buf, buflen);
1581
1582         if (os_strcmp(field, "key_mgmt") == 0)
1583                 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
1584                                                           buf, buflen);
1585
1586         if (os_strcmp(field, "proto") == 0)
1587                 return ctrl_iface_get_capability_proto(res, strict, &capa,
1588                                                        buf, buflen);
1589
1590         if (os_strcmp(field, "auth_alg") == 0)
1591                 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
1592                                                           buf, buflen);
1593
1594         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
1595                    field);
1596
1597         return -1;
1598 }
1599
1600
1601 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
1602                                          const char *cmd, char *buf,
1603                                          size_t buflen)
1604 {
1605         u8 bssid[ETH_ALEN];
1606         size_t i;
1607         struct wpa_bss *bss;
1608         int ret;
1609         char *pos, *end;
1610         const u8 *ie, *ie2;
1611
1612         if (os_strcmp(cmd, "FIRST") == 0)
1613                 bss = dl_list_first(&wpa_s->bss, struct wpa_bss, list);
1614         else if (os_strncmp(cmd, "ID-", 3) == 0) {
1615                 i = atoi(cmd + 3);
1616                 bss = wpa_bss_get_id(wpa_s, i);
1617         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
1618                 i = atoi(cmd + 5);
1619                 bss = wpa_bss_get_id(wpa_s, i);
1620                 if (bss) {
1621                         struct dl_list *next = bss->list_id.next;
1622                         if (next == &wpa_s->bss_id)
1623                                 bss = NULL;
1624                         else
1625                                 bss = dl_list_entry(next, struct wpa_bss,
1626                                                     list_id);
1627                 }
1628         } else if (hwaddr_aton(cmd, bssid) == 0)
1629                 bss = wpa_bss_get_bssid(wpa_s, bssid);
1630         else {
1631                 struct wpa_bss *tmp;
1632                 i = atoi(cmd);
1633                 bss = NULL;
1634                 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
1635                 {
1636                         if (i-- == 0) {
1637                                 bss = tmp;
1638                                 break;
1639                         }
1640                 }
1641         }
1642
1643         if (bss == NULL)
1644                 return 0;
1645
1646         pos = buf;
1647         end = buf + buflen;
1648         ret = os_snprintf(pos, end - pos,
1649                           "id=%u\n"
1650                           "bssid=" MACSTR "\n"
1651                           "freq=%d\n"
1652                           "beacon_int=%d\n"
1653                           "capabilities=0x%04x\n"
1654                           "qual=%d\n"
1655                           "noise=%d\n"
1656                           "level=%d\n"
1657                           "tsf=%016llu\n"
1658                           "ie=",
1659                           bss->id,
1660                           MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
1661                           bss->caps, bss->qual, bss->noise, bss->level,
1662                           (unsigned long long) bss->tsf);
1663         if (ret < 0 || ret >= end - pos)
1664                 return pos - buf;
1665         pos += ret;
1666
1667         ie = (const u8 *) (bss + 1);
1668         for (i = 0; i < bss->ie_len; i++) {
1669                 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
1670                 if (ret < 0 || ret >= end - pos)
1671                         return pos - buf;
1672                 pos += ret;
1673         }
1674         if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
1675                 ret = os_snprintf(pos, end - pos, "[P2P]");
1676                 if (ret < 0 || ret >= end - pos)
1677                         return pos - buf;
1678                 pos += ret;
1679         }
1680
1681         ret = os_snprintf(pos, end - pos, "\n");
1682         if (ret < 0 || ret >= end - pos)
1683                 return pos - buf;
1684         pos += ret;
1685
1686         ret = os_snprintf(pos, end - pos, "flags=");
1687         if (ret < 0 || ret >= end - pos)
1688                 return pos - buf;
1689         pos += ret;
1690
1691         ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1692         if (ie)
1693                 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
1694         ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1695         if (ie2)
1696                 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
1697         pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
1698         if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
1699                 ret = os_snprintf(pos, end - pos, "[WEP]");
1700                 if (ret < 0 || ret >= end - pos)
1701                         return pos - buf;
1702                 pos += ret;
1703         }
1704         if (bss->caps & IEEE80211_CAP_IBSS) {
1705                 ret = os_snprintf(pos, end - pos, "[IBSS]");
1706                 if (ret < 0 || ret >= end - pos)
1707                         return pos - buf;
1708                 pos += ret;
1709         }
1710         if (bss->caps & IEEE80211_CAP_ESS) {
1711                 ret = os_snprintf(pos, end - pos, "[ESS]");
1712                 if (ret < 0 || ret >= end - pos)
1713                         return pos - buf;
1714                 pos += ret;
1715         }
1716
1717         ret = os_snprintf(pos, end - pos, "\n");
1718         if (ret < 0 || ret >= end - pos)
1719                 return pos - buf;
1720         pos += ret;
1721
1722         ret = os_snprintf(pos, end - pos, "ssid=%s\n",
1723                           wpa_ssid_txt(bss->ssid, bss->ssid_len));
1724         if (ret < 0 || ret >= end - pos)
1725                 return pos - buf;
1726         pos += ret;
1727
1728 #ifdef CONFIG_WPS
1729         ie = (const u8 *) (bss + 1);
1730         ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
1731         if (ret < 0 || ret >= end - pos)
1732                 return pos - buf;
1733         pos += ret;
1734 #endif /* CONFIG_WPS */
1735
1736 #ifdef CONFIG_P2P
1737         ie = (const u8 *) (bss + 1);
1738         ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
1739         if (ret < 0 || ret >= end - pos)
1740                 return pos - buf;
1741         pos += ret;
1742 #endif /* CONFIG_P2P */
1743
1744         return pos - buf;
1745 }
1746
1747
1748 static int wpa_supplicant_ctrl_iface_ap_scan(
1749         struct wpa_supplicant *wpa_s, char *cmd)
1750 {
1751         int ap_scan = atoi(cmd);
1752         return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
1753 }
1754
1755
1756 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
1757 {
1758         u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
1759
1760         wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
1761         /* MLME-DELETEKEYS.request */
1762         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
1763         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
1764         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
1765         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
1766 #ifdef CONFIG_IEEE80211W
1767         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
1768         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
1769 #endif /* CONFIG_IEEE80211W */
1770
1771         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
1772                         0);
1773         /* MLME-SETPROTECTION.request(None) */
1774         wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
1775                                    MLME_SETPROTECTION_PROTECT_TYPE_NONE,
1776                                    MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1777         wpa_sm_drop_sa(wpa_s->wpa);
1778 }
1779
1780
1781 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
1782                                           char *addr)
1783 {
1784         u8 bssid[ETH_ALEN];
1785         struct wpa_bss *bss;
1786         struct wpa_ssid *ssid = wpa_s->current_ssid;
1787
1788         if (hwaddr_aton(addr, bssid)) {
1789                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
1790                            "address '%s'", addr);
1791                 return -1;
1792         }
1793
1794         wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
1795
1796         bss = wpa_bss_get_bssid(wpa_s, bssid);
1797         if (!bss) {
1798                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
1799                            "from BSS table");
1800                 return -1;
1801         }
1802
1803         /*
1804          * TODO: Find best network configuration block from configuration to
1805          * allow roaming to other networks
1806          */
1807
1808         if (!ssid) {
1809                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
1810                            "configuration known for the target AP");
1811                 return -1;
1812         }
1813
1814         wpa_s->reassociate = 1;
1815         wpa_supplicant_connect(wpa_s, bss, ssid);
1816
1817         return 0;
1818 }
1819
1820
1821 #ifdef CONFIG_P2P
1822 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
1823 {
1824         unsigned int timeout = atoi(cmd);
1825         enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
1826
1827         if (os_strstr(cmd, "type=social"))
1828                 type = P2P_FIND_ONLY_SOCIAL;
1829         else if (os_strstr(cmd, "type=progressive"))
1830                 type = P2P_FIND_PROGRESSIVE;
1831
1832         wpas_p2p_find(wpa_s, timeout, type);
1833         return 0;
1834 }
1835
1836
1837 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
1838                             char *buf, size_t buflen)
1839 {
1840         u8 addr[ETH_ALEN];
1841         char *pos, *pos2;
1842         char *pin = NULL;
1843         enum p2p_wps_method wps_method;
1844         int new_pin;
1845         int ret;
1846         int persistent_group;
1847         int join;
1848         int auth;
1849         int go_intent = -1;
1850         int freq = 0;
1851
1852         /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad] [persistent]
1853          * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] */
1854
1855         if (hwaddr_aton(cmd, addr))
1856                 return -1;
1857
1858         pos = cmd + 17;
1859         if (*pos != ' ')
1860                 return -1;
1861         pos++;
1862
1863         persistent_group = os_strstr(pos, " persistent") != NULL;
1864         join = os_strstr(pos, " join") != NULL;
1865         auth = os_strstr(pos, " auth") != NULL;
1866
1867         pos2 = os_strstr(pos, " go_intent=");
1868         if (pos2) {
1869                 pos2 += 11;
1870                 go_intent = atoi(pos2);
1871                 if (go_intent < 0 || go_intent > 15)
1872                         return -1;
1873         }
1874
1875         pos2 = os_strstr(pos, " freq=");
1876         if (pos2) {
1877                 pos2 += 6;
1878                 freq = atoi(pos2);
1879                 if (freq <= 0)
1880                         return -1;
1881         }
1882
1883         if (os_strncmp(pos, "pin", 3) == 0) {
1884                 /* Request random PIN (to be displayed) and enable the PIN */
1885                 wps_method = WPS_PIN_DISPLAY;
1886         } else if (os_strncmp(pos, "pbc", 3) == 0) {
1887                 wps_method = WPS_PBC;
1888         } else {
1889                 pin = pos;
1890                 pos = os_strchr(pin, ' ');
1891                 wps_method = WPS_PIN_KEYPAD;
1892                 if (pos) {
1893                         *pos++ = '\0';
1894                         if (os_strncmp(pos, "label", 5) == 0)
1895                                 wps_method = WPS_PIN_LABEL;
1896                         else if (os_strncmp(pos, "display", 7) == 0)
1897                                 wps_method = WPS_PIN_DISPLAY;
1898                 }
1899         }
1900
1901         new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
1902                                    persistent_group, join, auth, go_intent,
1903                                    freq);
1904         if (new_pin < 0)
1905                 return -1;
1906         if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
1907                 ret = os_snprintf(buf, buflen, "%08d", new_pin);
1908                 if (ret < 0 || (size_t) ret >= buflen)
1909                         return -1;
1910                 return ret;
1911         }
1912
1913         os_memcpy(buf, "OK\n", 3);
1914         return 3;
1915 }
1916
1917
1918 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
1919 {
1920         unsigned int timeout = atoi(cmd);
1921         return wpas_p2p_listen(wpa_s, timeout);
1922 }
1923
1924
1925 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
1926 {
1927         u8 addr[ETH_ALEN];
1928         char *pos;
1929
1930         /* <addr> <config method> */
1931
1932         if (hwaddr_aton(cmd, addr))
1933                 return -1;
1934
1935         pos = cmd + 17;
1936         if (*pos != ' ')
1937                 return -1;
1938         pos++;
1939
1940         return wpas_p2p_prov_disc(wpa_s, addr, pos);
1941 }
1942
1943
1944 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
1945                               size_t buflen)
1946 {
1947         struct wpa_ssid *ssid = wpa_s->current_ssid;
1948
1949         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1950             ssid->passphrase == NULL)
1951                 return -1;
1952
1953         os_strlcpy(buf, ssid->passphrase, buflen);
1954         return os_strlen(buf);
1955 }
1956
1957
1958 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
1959                                   char *buf, size_t buflen)
1960 {
1961         u64 ref;
1962         int res;
1963         u8 dst_buf[ETH_ALEN], *dst;
1964         struct wpabuf *tlvs;
1965         char *pos;
1966         size_t len;
1967
1968         if (hwaddr_aton(cmd, dst_buf))
1969                 return -1;
1970         dst = dst_buf;
1971         if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
1972             dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
1973                 dst = NULL;
1974         pos = cmd + 17;
1975         if (*pos != ' ')
1976                 return -1;
1977         pos++;
1978
1979         if (os_strncmp(pos, "upnp ", 5) == 0) {
1980                 u8 version;
1981                 pos += 5;
1982                 if (hexstr2bin(pos, &version, 1) < 0)
1983                         return -1;
1984                 pos += 2;
1985                 if (*pos != ' ')
1986                         return -1;
1987                 pos++;
1988                 ref = (u64) wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
1989         } else {
1990                 len = os_strlen(pos);
1991                 if (len & 1)
1992                         return -1;
1993                 len /= 2;
1994                 tlvs = wpabuf_alloc(len);
1995                 if (tlvs == NULL)
1996                         return -1;
1997                 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
1998                         wpabuf_free(tlvs);
1999                         return -1;
2000                 }
2001
2002                 ref = (u64) wpas_p2p_sd_request(wpa_s, dst, tlvs);
2003                 wpabuf_free(tlvs);
2004         }
2005         res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
2006         if (res < 0 || (unsigned) res >= buflen)
2007                 return -1;
2008         return res;
2009 }
2010
2011
2012 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
2013                                          char *cmd)
2014 {
2015         long long unsigned val;
2016         u64 req;
2017         if (sscanf(cmd, "%llx", &val) != 1)
2018                 return -1;
2019         req = val;
2020         return wpas_p2p_sd_cancel_request(wpa_s, (void *) req);
2021 }
2022
2023
2024 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
2025 {
2026         int freq;
2027         u8 dst_buf[ETH_ALEN], *dst;
2028         u8 dialog_token;
2029         struct wpabuf *resp_tlvs;
2030         char *pos, *pos2;
2031         size_t len;
2032
2033         pos = os_strchr(cmd, ' ');
2034         if (pos == NULL)
2035                 return -1;
2036         *pos++ = '\0';
2037         freq = atoi(cmd);
2038         if (freq == 0)
2039                 return -1;
2040
2041         if (hwaddr_aton(pos, dst_buf))
2042                 return -1;
2043         dst = dst_buf;
2044         if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
2045             dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
2046                 dst = NULL;
2047         pos += 17;
2048         if (*pos != ' ')
2049                 return -1;
2050         pos++;
2051
2052         pos2 = os_strchr(pos, ' ');
2053         if (pos2 == NULL)
2054                 return -1;
2055         *pos2++ = '\0';
2056         dialog_token = atoi(pos);
2057
2058         len = os_strlen(pos2);
2059         if (len & 1)
2060                 return -1;
2061         len /= 2;
2062         resp_tlvs = wpabuf_alloc(len);
2063         if (resp_tlvs == NULL)
2064                 return -1;
2065         if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
2066                 wpabuf_free(resp_tlvs);
2067                 return -1;
2068         }
2069
2070         wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
2071         wpabuf_free(resp_tlvs);
2072         return 0;
2073 }
2074
2075
2076 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
2077                                        char *cmd)
2078 {
2079         wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
2080         return 0;
2081 }
2082
2083
2084 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
2085                                         char *cmd)
2086 {
2087         char *pos;
2088         size_t len;
2089         struct wpabuf *query, *resp;
2090
2091         pos = os_strchr(cmd, ' ');
2092         if (pos == NULL)
2093                 return -1;
2094         *pos++ = '\0';
2095
2096         len = os_strlen(cmd);
2097         if (len & 1)
2098                 return -1;
2099         len /= 2;
2100         query = wpabuf_alloc(len);
2101         if (query == NULL)
2102                 return -1;
2103         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
2104                 wpabuf_free(query);
2105                 return -1;
2106         }
2107
2108         len = os_strlen(pos);
2109         if (len & 1) {
2110                 wpabuf_free(query);
2111                 return -1;
2112         }
2113         len /= 2;
2114         resp = wpabuf_alloc(len);
2115         if (resp == NULL) {
2116                 wpabuf_free(query);
2117                 return -1;
2118         }
2119         if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
2120                 wpabuf_free(query);
2121                 wpabuf_free(resp);
2122                 return -1;
2123         }
2124
2125         if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
2126                 wpabuf_free(query);
2127                 wpabuf_free(resp);
2128                 return -1;
2129         }
2130         return 0;
2131 }
2132
2133
2134 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
2135 {
2136         char *pos;
2137         u8 version;
2138
2139         pos = os_strchr(cmd, ' ');
2140         if (pos == NULL)
2141                 return -1;
2142         *pos++ = '\0';
2143
2144         if (hexstr2bin(cmd, &version, 1) < 0)
2145                 return -1;
2146
2147         return wpas_p2p_service_add_upnp(wpa_s, version, pos);
2148 }
2149
2150
2151 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
2152 {
2153         char *pos;
2154
2155         pos = os_strchr(cmd, ' ');
2156         if (pos == NULL)
2157                 return -1;
2158         *pos++ = '\0';
2159
2160         if (os_strcmp(cmd, "bonjour") == 0)
2161                 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
2162         if (os_strcmp(cmd, "upnp") == 0)
2163                 return p2p_ctrl_service_add_upnp(wpa_s, pos);
2164         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
2165         return -1;
2166 }
2167
2168
2169 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
2170                                         char *cmd)
2171 {
2172         size_t len;
2173         struct wpabuf *query;
2174         int ret;
2175
2176         len = os_strlen(cmd);
2177         if (len & 1)
2178                 return -1;
2179         len /= 2;
2180         query = wpabuf_alloc(len);
2181         if (query == NULL)
2182                 return -1;
2183         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
2184                 wpabuf_free(query);
2185                 return -1;
2186         }
2187
2188         ret = wpas_p2p_service_del_bonjour(wpa_s, query);
2189         wpabuf_free(query);
2190         return ret;
2191 }
2192
2193
2194 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
2195 {
2196         char *pos;
2197         u8 version;
2198
2199         pos = os_strchr(cmd, ' ');
2200         if (pos == NULL)
2201                 return -1;
2202         *pos++ = '\0';
2203
2204         if (hexstr2bin(cmd, &version, 1) < 0)
2205                 return -1;
2206
2207         return wpas_p2p_service_del_upnp(wpa_s, version, pos);
2208 }
2209
2210
2211 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
2212 {
2213         char *pos;
2214
2215         pos = os_strchr(cmd, ' ');
2216         if (pos == NULL)
2217                 return -1;
2218         *pos++ = '\0';
2219
2220         if (os_strcmp(cmd, "bonjour") == 0)
2221                 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
2222         if (os_strcmp(cmd, "upnp") == 0)
2223                 return p2p_ctrl_service_del_upnp(wpa_s, pos);
2224         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
2225         return -1;
2226 }
2227
2228
2229 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
2230 {
2231         u8 addr[ETH_ALEN];
2232
2233         /* <addr> */
2234
2235         if (hwaddr_aton(cmd, addr))
2236                 return -1;
2237
2238         return wpas_p2p_reject(wpa_s, addr);
2239 }
2240
2241
2242 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
2243 {
2244         char *pos;
2245         int id;
2246         struct wpa_ssid *ssid;
2247         u8 peer[ETH_ALEN];
2248
2249         id = atoi(cmd);
2250         pos = os_strstr(cmd, " peer=");
2251         if (pos) {
2252                 pos += 6;
2253                 if (hwaddr_aton(pos, peer))
2254                         return -1;
2255         }
2256         ssid = wpa_config_get_network(wpa_s->conf, id);
2257         if (ssid == NULL || ssid->disabled != 2) {
2258                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2259                            "for persistent P2P group",
2260                            id);
2261                 return -1;
2262         }
2263
2264         return wpas_p2p_invite(wpa_s, pos ? peer : NULL, ssid, NULL);
2265 }
2266
2267
2268 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
2269 {
2270         char *pos;
2271         u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
2272
2273         pos = os_strstr(cmd, " peer=");
2274         if (!pos)
2275                 return -1;
2276
2277         *pos = '\0';
2278         pos += 6;
2279         if (hwaddr_aton(pos, peer)) {
2280                 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
2281                 return -1;
2282         }
2283
2284         pos = os_strstr(pos, " go_dev_addr=");
2285         if (pos) {
2286                 pos += 13;
2287                 if (hwaddr_aton(pos, go_dev_addr)) {
2288                         wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
2289                                    pos);
2290                         return -1;
2291                 }
2292                 go_dev = go_dev_addr;
2293         }
2294
2295         return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
2296 }
2297
2298
2299 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
2300 {
2301         if (os_strncmp(cmd, "persistent=", 11) == 0)
2302                 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
2303         if (os_strncmp(cmd, "group=", 6) == 0)
2304                 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
2305
2306         return -1;
2307 }
2308
2309
2310 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
2311                                          char *cmd, int freq)
2312 {
2313         int id;
2314         struct wpa_ssid *ssid;
2315
2316         id = atoi(cmd);
2317         ssid = wpa_config_get_network(wpa_s->conf, id);
2318         if (ssid == NULL || ssid->disabled != 2) {
2319                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2320                            "for persistent P2P group",
2321                            id);
2322                 return -1;
2323         }
2324
2325         return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq);
2326 }
2327
2328
2329 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
2330 {
2331         int freq = 0;
2332         char *pos;
2333
2334         pos = os_strstr(cmd, "freq=");
2335         if (pos)
2336                 freq = atoi(pos + 5);
2337
2338         if (os_strncmp(cmd, "persistent=", 11) == 0)
2339                 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq);
2340         if (os_strcmp(cmd, "persistent") == 0 ||
2341             os_strncmp(cmd, "persistent ", 11) == 0)
2342                 return wpas_p2p_group_add(wpa_s, 1, freq);
2343         if (os_strncmp(cmd, "freq=", 5) == 0)
2344                 return wpas_p2p_group_add(wpa_s, 0, freq);
2345
2346         wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
2347                    cmd);
2348         return -1;
2349 }
2350
2351
2352 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
2353                          char *buf, size_t buflen)
2354 {
2355         u8 addr[ETH_ALEN], *addr_ptr;
2356         int next;
2357
2358         if (!wpa_s->global->p2p)
2359                 return -1;
2360
2361         if (os_strcmp(cmd, "FIRST") == 0) {
2362                 addr_ptr = NULL;
2363                 next = 0;
2364         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
2365                 if (hwaddr_aton(cmd + 5, addr) < 0)
2366                         return -1;
2367                 addr_ptr = addr;
2368                 next = 1;
2369         } else {
2370                 if (hwaddr_aton(cmd, addr) < 0)
2371                         return -1;
2372                 addr_ptr = addr;
2373                 next = 0;
2374         }
2375
2376         return p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next,
2377                                  buf, buflen);
2378 }
2379
2380
2381 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
2382 {
2383         char *param;
2384
2385         if (wpa_s->global->p2p == NULL)
2386                 return -1;
2387
2388         param = os_strchr(cmd, ' ');
2389         if (param == NULL)
2390                 return -1;
2391         *param++ = '\0';
2392
2393         if (os_strcmp(cmd, "discoverability") == 0) {
2394                 p2p_set_client_discoverability(wpa_s->global->p2p,
2395                                                atoi(param));
2396                 return 0;
2397         }
2398
2399         if (os_strcmp(cmd, "managed") == 0) {
2400                 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
2401                 return 0;
2402         }
2403
2404         if (os_strcmp(cmd, "listen_channel") == 0) {
2405                 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
2406                                               atoi(param));
2407         }
2408
2409         if (os_strcmp(cmd, "ssid_postfix") == 0) {
2410                 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
2411                                             os_strlen(param));
2412         }
2413
2414         if (os_strcmp(cmd, "noa") == 0) {
2415                 char *pos;
2416                 int count, start, duration;
2417                 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
2418                 count = atoi(param);
2419                 pos = os_strchr(param, ',');
2420                 if (pos == NULL)
2421                         return -1;
2422                 pos++;
2423                 start = atoi(pos);
2424                 pos = os_strchr(pos, ',');
2425                 if (pos == NULL)
2426                         return -1;
2427                 pos++;
2428                 duration = atoi(pos);
2429                 if (count < 0 || count > 255 || start < 0 || duration < 0)
2430                         return -1;
2431                 if (count == 0 && duration > 0)
2432                         return -1;
2433                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
2434                            "start=%d duration=%d", count, start, duration);
2435                 return wpa_drv_set_noa(wpa_s, count, start, duration);
2436         }
2437
2438         if (os_strcmp(cmd, "ps") == 0)
2439                 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
2440
2441         if (os_strcmp(cmd, "oppps") == 0)
2442                 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
2443
2444         if (os_strcmp(cmd, "ctwindow") == 0)
2445                 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
2446
2447         if (os_strcmp(cmd, "disabled") == 0) {
2448                 wpa_s->global->p2p_disabled = atoi(param);
2449                 wpa_printf(MSG_DEBUG, "P2P functionality %s",
2450                            wpa_s->global->p2p_disabled ?
2451                            "disabled" : "enabled");
2452                 if (wpa_s->global->p2p_disabled) {
2453                         wpas_p2p_stop_find(wpa_s);
2454                         os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
2455                         p2p_flush(wpa_s->global->p2p);
2456                 }
2457                 return 0;
2458         }
2459
2460         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
2461                    cmd);
2462
2463         return -1;
2464 }
2465
2466
2467 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
2468 {
2469         char *pos, *pos2;
2470         unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
2471
2472         if (cmd[0]) {
2473                 pos = os_strchr(cmd, ' ');
2474                 if (pos == NULL)
2475                         return -1;
2476                 *pos++ = '\0';
2477                 dur1 = atoi(cmd);
2478
2479                 pos2 = os_strchr(pos, ' ');
2480                 if (pos2)
2481                         *pos2++ = '\0';
2482                 int1 = atoi(pos);
2483         } else
2484                 pos2 = NULL;
2485
2486         if (pos2) {
2487                 pos = os_strchr(pos2, ' ');
2488                 if (pos == NULL)
2489                         return -1;
2490                 *pos++ = '\0';
2491                 dur2 = atoi(pos2);
2492                 int2 = atoi(pos);
2493         }
2494
2495         return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
2496 }
2497
2498
2499 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
2500 {
2501         char *pos;
2502         unsigned int period = 0, interval = 0;
2503
2504         if (cmd[0]) {
2505                 pos = os_strchr(cmd, ' ');
2506                 if (pos == NULL)
2507                         return -1;
2508                 *pos++ = '\0';
2509                 period = atoi(cmd);
2510                 interval = atoi(pos);
2511         }
2512
2513         return wpas_p2p_ext_listen(wpa_s, period, interval);
2514 }
2515
2516 #endif /* CONFIG_P2P */
2517
2518
2519 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
2520                                          char *buf, size_t *resp_len)
2521 {
2522         char *reply;
2523         const int reply_size = 4096;
2524         int ctrl_rsp = 0;
2525         int reply_len;
2526
2527         if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
2528             os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
2529                 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
2530                                       (const u8 *) buf, os_strlen(buf));
2531         } else {
2532                 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface",
2533                                   (const u8 *) buf, os_strlen(buf));
2534         }
2535
2536         reply = os_malloc(reply_size);
2537         if (reply == NULL) {
2538                 *resp_len = 1;
2539                 return NULL;
2540         }
2541
2542         os_memcpy(reply, "OK\n", 3);
2543         reply_len = 3;
2544
2545         if (os_strcmp(buf, "PING") == 0) {
2546                 os_memcpy(reply, "PONG\n", 5);
2547                 reply_len = 5;
2548         } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
2549                 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
2550         } else if (os_strcmp(buf, "MIB") == 0) {
2551                 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
2552                 if (reply_len >= 0) {
2553                         int res;
2554                         res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
2555                                                reply_size - reply_len);
2556                         if (res < 0)
2557                                 reply_len = -1;
2558                         else
2559                                 reply_len += res;
2560                 }
2561         } else if (os_strncmp(buf, "STATUS", 6) == 0) {
2562                 reply_len = wpa_supplicant_ctrl_iface_status(
2563                         wpa_s, buf + 6, reply, reply_size);
2564         } else if (os_strcmp(buf, "PMKSA") == 0) {
2565                 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
2566                                                     reply_size);
2567         } else if (os_strncmp(buf, "SET ", 4) == 0) {
2568                 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
2569                         reply_len = -1;
2570         } else if (os_strcmp(buf, "LOGON") == 0) {
2571                 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
2572         } else if (os_strcmp(buf, "LOGOFF") == 0) {
2573                 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
2574         } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
2575                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2576                         reply_len = -1;
2577                 else {
2578                         wpa_s->disconnected = 0;
2579                         wpa_s->reassociate = 1;
2580                         wpa_supplicant_req_scan(wpa_s, 0, 0);
2581                 }
2582         } else if (os_strcmp(buf, "RECONNECT") == 0) {
2583                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2584                         reply_len = -1;
2585                 else if (wpa_s->disconnected) {
2586                         wpa_s->disconnected = 0;
2587                         wpa_s->reassociate = 1;
2588                         wpa_supplicant_req_scan(wpa_s, 0, 0);
2589                 }
2590 #ifdef IEEE8021X_EAPOL
2591         } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
2592                 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
2593                         reply_len = -1;
2594 #endif /* IEEE8021X_EAPOL */
2595 #ifdef CONFIG_PEERKEY
2596         } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
2597                 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
2598                         reply_len = -1;
2599 #endif /* CONFIG_PEERKEY */
2600 #ifdef CONFIG_IEEE80211R
2601         } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
2602                 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
2603                         reply_len = -1;
2604 #endif /* CONFIG_IEEE80211R */
2605 #ifdef CONFIG_WPS
2606         } else if (os_strcmp(buf, "WPS_PBC") == 0) {
2607                 if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL))
2608                         reply_len = -1;
2609         } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
2610                 if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8))
2611                         reply_len = -1;
2612         } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2613                 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
2614                                                               reply,
2615                                                               reply_size);
2616 #ifdef CONFIG_WPS_OOB
2617         } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
2618                 if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
2619                         reply_len = -1;
2620 #endif /* CONFIG_WPS_OOB */
2621         } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
2622                 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
2623                         reply_len = -1;
2624 #ifdef CONFIG_WPS_ER
2625         } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
2626                 if (wpas_wps_er_start(wpa_s, NULL))
2627                         reply_len = -1;
2628         } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
2629                 if (wpas_wps_er_start(wpa_s, buf + 13))
2630                         reply_len = -1;
2631         } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
2632                 if (wpas_wps_er_stop(wpa_s))
2633                         reply_len = -1;
2634         } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
2635                 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
2636                         reply_len = -1;
2637         } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
2638                 if (wpas_wps_er_pbc(wpa_s, buf + 11))
2639                         reply_len = -1;
2640         } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
2641                 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
2642                         reply_len = -1;
2643         } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
2644                 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
2645                         reply_len = -1;
2646 #endif /* CONFIG_WPS_ER */
2647 #endif /* CONFIG_WPS */
2648 #ifdef CONFIG_IBSS_RSN
2649         } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
2650                 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
2651                         reply_len = -1;
2652 #endif /* CONFIG_IBSS_RSN */
2653 #ifdef CONFIG_P2P
2654         } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
2655                 if (p2p_ctrl_find(wpa_s, buf + 9))
2656                         reply_len = -1;
2657         } else if (os_strcmp(buf, "P2P_FIND") == 0) {
2658                 if (p2p_ctrl_find(wpa_s, ""))
2659                         reply_len = -1;
2660         } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
2661                 wpas_p2p_stop_find(wpa_s);
2662         } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
2663                 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
2664                                              reply_size);
2665         } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
2666                 if (p2p_ctrl_listen(wpa_s, buf + 11))
2667                         reply_len = -1;
2668         } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
2669                 if (p2p_ctrl_listen(wpa_s, ""))
2670                         reply_len = -1;
2671         } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
2672                 if (wpas_p2p_group_remove(wpa_s, buf + 17))
2673                         reply_len = -1;
2674         } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
2675                 if (wpas_p2p_group_add(wpa_s, 0, 0))
2676                         reply_len = -1;
2677         } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
2678                 if (p2p_ctrl_group_add(wpa_s, buf + 14))
2679                         reply_len = -1;
2680         } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
2681                 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
2682                         reply_len = -1;
2683         } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
2684                 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
2685         } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
2686                 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
2687                                                    reply_size);
2688         } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
2689                 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
2690                         reply_len = -1;
2691         } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
2692                 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
2693                         reply_len = -1;
2694         } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
2695                 wpas_p2p_sd_service_update(wpa_s);
2696         } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
2697                 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
2698                         reply_len = -1;
2699         } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
2700                 wpas_p2p_service_flush(wpa_s);
2701         } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
2702                 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
2703                         reply_len = -1;
2704         } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
2705                 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
2706                         reply_len = -1;
2707         } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
2708                 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
2709                         reply_len = -1;
2710         } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
2711                 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
2712                         reply_len = -1;
2713         } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
2714                 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
2715                                               reply_size);
2716         } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
2717                 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
2718                         reply_len = -1;
2719         } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
2720                 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
2721                 p2p_flush(wpa_s->global->p2p);
2722         } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
2723                 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
2724                         reply_len = -1;
2725         } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
2726                 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
2727                         reply_len = -1;
2728         } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
2729                 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
2730                         reply_len = -1;
2731         } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
2732                 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
2733                         reply_len = -1;
2734 #endif /* CONFIG_P2P */
2735         } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
2736         {
2737                 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
2738                             wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
2739                         reply_len = -1;
2740                 else
2741                         ctrl_rsp = 1;
2742         } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
2743                 if (wpa_supplicant_reload_configuration(wpa_s))
2744                         reply_len = -1;
2745         } else if (os_strcmp(buf, "TERMINATE") == 0) {
2746                 wpa_supplicant_terminate_proc(wpa_s->global);
2747         } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
2748                 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
2749                         reply_len = -1;
2750         } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
2751                 reply_len = wpa_supplicant_ctrl_iface_list_networks(
2752                         wpa_s, reply, reply_size);
2753         } else if (os_strcmp(buf, "DISCONNECT") == 0) {
2754                 wpa_s->reassociate = 0;
2755                 wpa_s->disconnected = 1;
2756                 wpa_supplicant_deauthenticate(wpa_s,
2757                                               WLAN_REASON_DEAUTH_LEAVING);
2758         } else if (os_strcmp(buf, "SCAN") == 0) {
2759                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2760                         reply_len = -1;
2761                 else {
2762                         wpa_s->scan_req = 2;
2763                         wpa_supplicant_req_scan(wpa_s, 0, 0);
2764                 }
2765         } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
2766                 reply_len = wpa_supplicant_ctrl_iface_scan_results(
2767                         wpa_s, reply, reply_size);
2768         } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
2769                 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
2770                         reply_len = -1;
2771         } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
2772                 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
2773                         reply_len = -1;
2774         } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
2775                 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
2776                         reply_len = -1;
2777         } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
2778                 reply_len = wpa_supplicant_ctrl_iface_add_network(
2779                         wpa_s, reply, reply_size);
2780         } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
2781                 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
2782                         reply_len = -1;
2783         } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
2784                 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
2785                         reply_len = -1;
2786         } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
2787                 reply_len = wpa_supplicant_ctrl_iface_get_network(
2788                         wpa_s, buf + 12, reply, reply_size);
2789 #ifndef CONFIG_NO_CONFIG_WRITE
2790         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
2791                 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
2792                         reply_len = -1;
2793 #endif /* CONFIG_NO_CONFIG_WRITE */
2794         } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
2795                 reply_len = wpa_supplicant_ctrl_iface_get_capability(
2796                         wpa_s, buf + 15, reply, reply_size);
2797         } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
2798                 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
2799                         reply_len = -1;
2800         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
2801                 reply_len = wpa_supplicant_global_iface_list(
2802                         wpa_s->global, reply, reply_size);
2803         } else if (os_strcmp(buf, "INTERFACES") == 0) {
2804                 reply_len = wpa_supplicant_global_iface_interfaces(
2805                         wpa_s->global, reply, reply_size);
2806         } else if (os_strncmp(buf, "BSS ", 4) == 0) {
2807                 reply_len = wpa_supplicant_ctrl_iface_bss(
2808                         wpa_s, buf + 4, reply, reply_size);
2809 #ifdef CONFIG_AP
2810         } else if (os_strcmp(buf, "STA-FIRST") == 0) {
2811                 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
2812         } else if (os_strncmp(buf, "STA ", 4) == 0) {
2813                 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
2814                                               reply_size);
2815         } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
2816                 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
2817                                                    reply_size);
2818 #endif /* CONFIG_AP */
2819         } else if (os_strcmp(buf, "SUSPEND") == 0) {
2820                 wpas_notify_suspend(wpa_s->global);
2821         } else if (os_strcmp(buf, "RESUME") == 0) {
2822                 wpas_notify_resume(wpa_s->global);
2823         } else if (os_strcmp(buf, "DROP_SA") == 0) {
2824                 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
2825         } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
2826                 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
2827                         reply_len = -1;
2828         } else {
2829                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2830                 reply_len = 16;
2831         }
2832
2833         if (reply_len < 0) {
2834                 os_memcpy(reply, "FAIL\n", 5);
2835                 reply_len = 5;
2836         }
2837
2838         if (ctrl_rsp)
2839                 eapol_sm_notify_ctrl_response(wpa_s->eapol);
2840
2841         *resp_len = reply_len;
2842         return reply;
2843 }
2844
2845
2846 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
2847                                            char *cmd)
2848 {
2849         struct wpa_interface iface;
2850         char *pos;
2851
2852         /*
2853          * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
2854          * TAB<bridge_ifname>
2855          */
2856         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
2857
2858         os_memset(&iface, 0, sizeof(iface));
2859
2860         do {
2861                 iface.ifname = pos = cmd;
2862                 pos = os_strchr(pos, '\t');
2863                 if (pos)
2864                         *pos++ = '\0';
2865                 if (iface.ifname[0] == '\0')
2866                         return -1;
2867                 if (pos == NULL)
2868                         break;
2869
2870                 iface.confname = pos;
2871                 pos = os_strchr(pos, '\t');
2872                 if (pos)
2873                         *pos++ = '\0';
2874                 if (iface.confname[0] == '\0')
2875                         iface.confname = NULL;
2876                 if (pos == NULL)
2877                         break;
2878
2879                 iface.driver = pos;
2880                 pos = os_strchr(pos, '\t');
2881                 if (pos)
2882                         *pos++ = '\0';
2883                 if (iface.driver[0] == '\0')
2884                         iface.driver = NULL;
2885                 if (pos == NULL)
2886                         break;
2887
2888                 iface.ctrl_interface = pos;
2889                 pos = os_strchr(pos, '\t');
2890                 if (pos)
2891                         *pos++ = '\0';
2892                 if (iface.ctrl_interface[0] == '\0')
2893                         iface.ctrl_interface = NULL;
2894                 if (pos == NULL)
2895                         break;
2896
2897                 iface.driver_param = pos;
2898                 pos = os_strchr(pos, '\t');
2899                 if (pos)
2900                         *pos++ = '\0';
2901                 if (iface.driver_param[0] == '\0')
2902                         iface.driver_param = NULL;
2903                 if (pos == NULL)
2904                         break;
2905
2906                 iface.bridge_ifname = pos;
2907                 pos = os_strchr(pos, '\t');
2908                 if (pos)
2909                         *pos++ = '\0';
2910                 if (iface.bridge_ifname[0] == '\0')
2911                         iface.bridge_ifname = NULL;
2912                 if (pos == NULL)
2913                         break;
2914         } while (0);
2915
2916         if (wpa_supplicant_get_iface(global, iface.ifname))
2917                 return -1;
2918
2919         return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
2920 }
2921
2922
2923 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
2924                                               char *cmd)
2925 {
2926         struct wpa_supplicant *wpa_s;
2927
2928         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
2929
2930         wpa_s = wpa_supplicant_get_iface(global, cmd);
2931         if (wpa_s == NULL)
2932                 return -1;
2933         return wpa_supplicant_remove_iface(global, wpa_s);
2934 }
2935
2936
2937 static void wpa_free_iface_info(struct wpa_interface_info *iface)
2938 {
2939         struct wpa_interface_info *prev;
2940
2941         while (iface) {
2942                 prev = iface;
2943                 iface = iface->next;
2944
2945                 os_free(prev->ifname);
2946                 os_free(prev->desc);
2947                 os_free(prev);
2948         }
2949 }
2950
2951
2952 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
2953                                             char *buf, int len)
2954 {
2955         int i, res;
2956         struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
2957         char *pos, *end;
2958
2959         for (i = 0; wpa_drivers[i]; i++) {
2960                 struct wpa_driver_ops *drv = wpa_drivers[i];
2961                 if (drv->get_interfaces == NULL)
2962                         continue;
2963                 tmp = drv->get_interfaces(global->drv_priv[i]);
2964                 if (tmp == NULL)
2965                         continue;
2966
2967                 if (last == NULL)
2968                         iface = last = tmp;
2969                 else
2970                         last->next = tmp;
2971                 while (last->next)
2972                         last = last->next;
2973         }
2974
2975         pos = buf;
2976         end = buf + len;
2977         for (tmp = iface; tmp; tmp = tmp->next) {
2978                 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
2979                                   tmp->drv_name, tmp->ifname,
2980                                   tmp->desc ? tmp->desc : "");
2981                 if (res < 0 || res >= end - pos) {
2982                         *pos = '\0';
2983                         break;
2984                 }
2985                 pos += res;
2986         }
2987
2988         wpa_free_iface_info(iface);
2989
2990         return pos - buf;
2991 }
2992
2993
2994 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
2995                                                   char *buf, int len)
2996 {
2997         int res;
2998         char *pos, *end;
2999         struct wpa_supplicant *wpa_s;
3000
3001         wpa_s = global->ifaces;
3002         pos = buf;
3003         end = buf + len;
3004
3005         while (wpa_s) {
3006                 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
3007                 if (res < 0 || res >= end - pos) {
3008                         *pos = '\0';
3009                         break;
3010                 }
3011                 pos += res;
3012                 wpa_s = wpa_s->next;
3013         }
3014         return pos - buf;
3015 }
3016
3017
3018 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
3019                                                 char *buf, size_t *resp_len)
3020 {
3021         char *reply;
3022         const int reply_size = 2048;
3023         int reply_len;
3024
3025         wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
3026                           (const u8 *) buf, os_strlen(buf));
3027
3028         reply = os_malloc(reply_size);
3029         if (reply == NULL) {
3030                 *resp_len = 1;
3031                 return NULL;
3032         }
3033
3034         os_memcpy(reply, "OK\n", 3);
3035         reply_len = 3;
3036
3037         if (os_strcmp(buf, "PING") == 0) {
3038                 os_memcpy(reply, "PONG\n", 5);
3039                 reply_len = 5;
3040         } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
3041                 if (wpa_supplicant_global_iface_add(global, buf + 14))
3042                         reply_len = -1;
3043         } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
3044                 if (wpa_supplicant_global_iface_remove(global, buf + 17))
3045                         reply_len = -1;
3046         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
3047                 reply_len = wpa_supplicant_global_iface_list(
3048                         global, reply, reply_size);
3049         } else if (os_strcmp(buf, "INTERFACES") == 0) {
3050                 reply_len = wpa_supplicant_global_iface_interfaces(
3051                         global, reply, reply_size);
3052         } else if (os_strcmp(buf, "TERMINATE") == 0) {
3053                 wpa_supplicant_terminate_proc(global);
3054         } else if (os_strcmp(buf, "SUSPEND") == 0) {
3055                 wpas_notify_suspend(global);
3056         } else if (os_strcmp(buf, "RESUME") == 0) {
3057                 wpas_notify_resume(global);
3058         } else {
3059                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
3060                 reply_len = 16;
3061         }
3062
3063         if (reply_len < 0) {
3064                 os_memcpy(reply, "FAIL\n", 5);
3065                 reply_len = 5;
3066         }
3067
3068         *resp_len = reply_len;
3069         return reply;
3070 }