WPS: Add testing option to corrupt public key hash
[mech_eap.git] / wpa_supplicant / ctrl_iface.c
1 /*
2  * WPA Supplicant / Control interface (shared code for all backends)
3  * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/uuid.h"
14 #include "common/version.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ieee802_11_common.h"
17 #include "common/wpa_ctrl.h"
18 #include "eap_peer/eap.h"
19 #include "eapol_supp/eapol_supp_sm.h"
20 #include "rsn_supp/wpa.h"
21 #include "rsn_supp/preauth.h"
22 #include "rsn_supp/pmksa_cache.h"
23 #include "l2_packet/l2_packet.h"
24 #include "wps/wps.h"
25 #include "config.h"
26 #include "wpa_supplicant_i.h"
27 #include "driver_i.h"
28 #include "wps_supplicant.h"
29 #include "ibss_rsn.h"
30 #include "ap.h"
31 #include "p2p_supplicant.h"
32 #include "p2p/p2p.h"
33 #include "hs20_supplicant.h"
34 #include "wifi_display.h"
35 #include "notify.h"
36 #include "bss.h"
37 #include "scan.h"
38 #include "ctrl_iface.h"
39 #include "interworking.h"
40 #include "blacklist.h"
41 #include "autoscan.h"
42 #include "wnm_sta.h"
43
44 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
45                                             char *buf, int len);
46 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
47                                                   char *buf, int len);
48
49
50 static int pno_start(struct wpa_supplicant *wpa_s)
51 {
52         int ret, interval;
53         size_t i, num_ssid;
54         struct wpa_ssid *ssid;
55         struct wpa_driver_scan_params params;
56
57         if (wpa_s->pno || wpa_s->pno_sched_pending)
58                 return 0;
59
60         if ((wpa_s->wpa_state > WPA_SCANNING) &&
61             (wpa_s->wpa_state <= WPA_COMPLETED)) {
62                 wpa_printf(MSG_ERROR, "PNO: In assoc process");
63                 return -EAGAIN;
64         }
65
66         if (wpa_s->wpa_state == WPA_SCANNING) {
67                 wpa_supplicant_cancel_scan(wpa_s);
68                 if (wpa_s->sched_scanning) {
69                         wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
70                                    "ongoing sched scan");
71                         wpa_supplicant_cancel_sched_scan(wpa_s);
72                         wpa_s->pno_sched_pending = 1;
73                         return 0;
74                 }
75         }
76
77         os_memset(&params, 0, sizeof(params));
78
79         num_ssid = 0;
80         ssid = wpa_s->conf->ssid;
81         while (ssid) {
82                 if (!wpas_network_disabled(wpa_s, ssid))
83                         num_ssid++;
84                 ssid = ssid->next;
85         }
86         if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
87                 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
88                            "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
89                 num_ssid = WPAS_MAX_SCAN_SSIDS;
90         }
91
92         if (num_ssid == 0) {
93                 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
94                 return -1;
95         }
96
97         params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
98                                         num_ssid);
99         if (params.filter_ssids == NULL)
100                 return -1;
101         i = 0;
102         ssid = wpa_s->conf->ssid;
103         while (ssid) {
104                 if (!wpas_network_disabled(wpa_s, ssid)) {
105                         params.ssids[i].ssid = ssid->ssid;
106                         params.ssids[i].ssid_len = ssid->ssid_len;
107                         params.num_ssids++;
108                         os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
109                                   ssid->ssid_len);
110                         params.filter_ssids[i].ssid_len = ssid->ssid_len;
111                         params.num_filter_ssids++;
112                         i++;
113                         if (i == num_ssid)
114                                 break;
115                 }
116                 ssid = ssid->next;
117         }
118
119         if (wpa_s->conf->filter_rssi)
120                 params.filter_rssi = wpa_s->conf->filter_rssi;
121
122         interval = wpa_s->conf->sched_scan_interval ?
123                 wpa_s->conf->sched_scan_interval : 10;
124
125         ret = wpa_supplicant_start_sched_scan(wpa_s, &params, interval);
126         os_free(params.filter_ssids);
127         if (ret == 0)
128                 wpa_s->pno = 1;
129         return ret;
130 }
131
132
133 static int pno_stop(struct wpa_supplicant *wpa_s)
134 {
135         int ret = 0;
136
137         if (wpa_s->pno || wpa_s->sched_scanning) {
138                 wpa_s->pno = 0;
139                 ret = wpa_supplicant_stop_sched_scan(wpa_s);
140         }
141
142         wpa_s->pno_sched_pending = 0;
143
144         if (wpa_s->wpa_state == WPA_SCANNING)
145                 wpa_supplicant_req_scan(wpa_s, 0, 0);
146
147         return ret;
148 }
149
150
151 static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
152 {
153         char *pos;
154         u8 addr[ETH_ALEN], *filter = NULL, *n;
155         size_t count = 0;
156
157         pos = val;
158         while (pos) {
159                 if (*pos == '\0')
160                         break;
161                 if (hwaddr_aton(pos, addr)) {
162                         os_free(filter);
163                         return -1;
164                 }
165                 n = os_realloc_array(filter, count + 1, ETH_ALEN);
166                 if (n == NULL) {
167                         os_free(filter);
168                         return -1;
169                 }
170                 filter = n;
171                 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
172                 count++;
173
174                 pos = os_strchr(pos, ' ');
175                 if (pos)
176                         pos++;
177         }
178
179         wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
180         os_free(wpa_s->bssid_filter);
181         wpa_s->bssid_filter = filter;
182         wpa_s->bssid_filter_count = count;
183
184         return 0;
185 }
186
187
188 static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
189 {
190         char *pos;
191         u8 addr[ETH_ALEN], *bssid = NULL, *n;
192         struct wpa_ssid_value *ssid = NULL, *ns;
193         size_t count = 0, ssid_count = 0;
194         struct wpa_ssid *c;
195
196         /*
197          * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
198          * SSID_SPEC ::= ssid <SSID_HEX>
199          * BSSID_SPEC ::= bssid <BSSID_HEX>
200          */
201
202         pos = val;
203         while (pos) {
204                 if (*pos == '\0')
205                         break;
206                 if (os_strncmp(pos, "bssid ", 6) == 0) {
207                         int res;
208                         pos += 6;
209                         res = hwaddr_aton2(pos, addr);
210                         if (res < 0) {
211                                 os_free(ssid);
212                                 os_free(bssid);
213                                 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
214                                            "BSSID value '%s'", pos);
215                                 return -1;
216                         }
217                         pos += res;
218                         n = os_realloc_array(bssid, count + 1, ETH_ALEN);
219                         if (n == NULL) {
220                                 os_free(ssid);
221                                 os_free(bssid);
222                                 return -1;
223                         }
224                         bssid = n;
225                         os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
226                         count++;
227                 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
228                         char *end;
229                         pos += 5;
230
231                         end = pos;
232                         while (*end) {
233                                 if (*end == '\0' || *end == ' ')
234                                         break;
235                                 end++;
236                         }
237
238                         ns = os_realloc_array(ssid, ssid_count + 1,
239                                               sizeof(struct wpa_ssid_value));
240                         if (ns == NULL) {
241                                 os_free(ssid);
242                                 os_free(bssid);
243                                 return -1;
244                         }
245                         ssid = ns;
246
247                         if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
248                             hexstr2bin(pos, ssid[ssid_count].ssid,
249                                        (end - pos) / 2) < 0) {
250                                 os_free(ssid);
251                                 os_free(bssid);
252                                 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
253                                            "SSID value '%s'", pos);
254                                 return -1;
255                         }
256                         ssid[ssid_count].ssid_len = (end - pos) / 2;
257                         wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
258                                           ssid[ssid_count].ssid,
259                                           ssid[ssid_count].ssid_len);
260                         ssid_count++;
261                         pos = end;
262                 } else {
263                         wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
264                                    "'%s'", pos);
265                         os_free(ssid);
266                         os_free(bssid);
267                         return -1;
268                 }
269
270                 pos = os_strchr(pos, ' ');
271                 if (pos)
272                         pos++;
273         }
274
275         wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
276         os_free(wpa_s->disallow_aps_bssid);
277         wpa_s->disallow_aps_bssid = bssid;
278         wpa_s->disallow_aps_bssid_count = count;
279
280         wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
281         os_free(wpa_s->disallow_aps_ssid);
282         wpa_s->disallow_aps_ssid = ssid;
283         wpa_s->disallow_aps_ssid_count = ssid_count;
284
285         if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
286                 return 0;
287
288         c = wpa_s->current_ssid;
289         if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
290                 return 0;
291
292         if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
293             !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
294                 return 0;
295
296         wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
297                    "because current AP was marked disallowed");
298
299 #ifdef CONFIG_SME
300         wpa_s->sme.prev_bssid_set = 0;
301 #endif /* CONFIG_SME */
302         wpa_s->reassociate = 1;
303         wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
304         wpa_supplicant_req_scan(wpa_s, 0, 0);
305
306         return 0;
307 }
308
309
310 static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
311                                          char *cmd)
312 {
313         char *value;
314         int ret = 0;
315
316         value = os_strchr(cmd, ' ');
317         if (value == NULL)
318                 return -1;
319         *value++ = '\0';
320
321         wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
322         if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
323                 eapol_sm_configure(wpa_s->eapol,
324                                    atoi(value), -1, -1, -1);
325         } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
326                 eapol_sm_configure(wpa_s->eapol,
327                                    -1, atoi(value), -1, -1);
328         } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
329                 eapol_sm_configure(wpa_s->eapol,
330                                    -1, -1, atoi(value), -1);
331         } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
332                 eapol_sm_configure(wpa_s->eapol,
333                                    -1, -1, -1, atoi(value));
334         } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
335                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
336                                      atoi(value)))
337                         ret = -1;
338         } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
339                    0) {
340                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
341                                      atoi(value)))
342                         ret = -1;
343         } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
344                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
345                         ret = -1;
346         } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
347                 wpa_s->wps_fragment_size = atoi(value);
348 #ifdef CONFIG_WPS_TESTING
349         } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
350                 long int val;
351                 val = strtol(value, NULL, 0);
352                 if (val < 0 || val > 0xff) {
353                         ret = -1;
354                         wpa_printf(MSG_DEBUG, "WPS: Invalid "
355                                    "wps_version_number %ld", val);
356                 } else {
357                         wps_version_number = val;
358                         wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
359                                    "version %u.%u",
360                                    (wps_version_number & 0xf0) >> 4,
361                                    wps_version_number & 0x0f);
362                 }
363         } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
364                 wps_testing_dummy_cred = atoi(value);
365                 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
366                            wps_testing_dummy_cred);
367         } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
368                 wps_corrupt_pkhash = atoi(value);
369                 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
370                            wps_corrupt_pkhash);
371 #endif /* CONFIG_WPS_TESTING */
372         } else if (os_strcasecmp(cmd, "ampdu") == 0) {
373                 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
374                         ret = -1;
375 #ifdef CONFIG_TDLS
376 #ifdef CONFIG_TDLS_TESTING
377         } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
378                 extern unsigned int tdls_testing;
379                 tdls_testing = strtol(value, NULL, 0);
380                 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
381 #endif /* CONFIG_TDLS_TESTING */
382         } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
383                 int disabled = atoi(value);
384                 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
385                 if (disabled) {
386                         if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
387                                 ret = -1;
388                 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
389                         ret = -1;
390                 wpa_tdls_enable(wpa_s->wpa, !disabled);
391 #endif /* CONFIG_TDLS */
392         } else if (os_strcasecmp(cmd, "pno") == 0) {
393                 if (atoi(value))
394                         ret = pno_start(wpa_s);
395                 else
396                         ret = pno_stop(wpa_s);
397         } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
398                 int disabled = atoi(value);
399                 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
400                         ret = -1;
401                 else if (disabled)
402                         wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
403         } else if (os_strcasecmp(cmd, "uapsd") == 0) {
404                 if (os_strcmp(value, "disable") == 0)
405                         wpa_s->set_sta_uapsd = 0;
406                 else {
407                         int be, bk, vi, vo;
408                         char *pos;
409                         /* format: BE,BK,VI,VO;max SP Length */
410                         be = atoi(value);
411                         pos = os_strchr(value, ',');
412                         if (pos == NULL)
413                                 return -1;
414                         pos++;
415                         bk = atoi(pos);
416                         pos = os_strchr(pos, ',');
417                         if (pos == NULL)
418                                 return -1;
419                         pos++;
420                         vi = atoi(pos);
421                         pos = os_strchr(pos, ',');
422                         if (pos == NULL)
423                                 return -1;
424                         pos++;
425                         vo = atoi(pos);
426                         /* ignore max SP Length for now */
427
428                         wpa_s->set_sta_uapsd = 1;
429                         wpa_s->sta_uapsd = 0;
430                         if (be)
431                                 wpa_s->sta_uapsd |= BIT(0);
432                         if (bk)
433                                 wpa_s->sta_uapsd |= BIT(1);
434                         if (vi)
435                                 wpa_s->sta_uapsd |= BIT(2);
436                         if (vo)
437                                 wpa_s->sta_uapsd |= BIT(3);
438                 }
439         } else if (os_strcasecmp(cmd, "ps") == 0) {
440                 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
441 #ifdef CONFIG_WIFI_DISPLAY
442         } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
443                 wifi_display_enable(wpa_s->global, !!atoi(value));
444 #endif /* CONFIG_WIFI_DISPLAY */
445         } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
446                 ret = set_bssid_filter(wpa_s, value);
447         } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
448                 ret = set_disallow_aps(wpa_s, value);
449         } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
450                 wpa_s->no_keep_alive = !!atoi(value);
451         } else {
452                 value[-1] = '=';
453                 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
454                 if (ret == 0)
455                         wpa_supplicant_update_config(wpa_s);
456         }
457
458         return ret;
459 }
460
461
462 static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
463                                          char *cmd, char *buf, size_t buflen)
464 {
465         int res = -1;
466
467         wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
468
469         if (os_strcmp(cmd, "version") == 0) {
470                 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
471         } else if (os_strcasecmp(cmd, "country") == 0) {
472                 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
473                         res = os_snprintf(buf, buflen, "%c%c",
474                                           wpa_s->conf->country[0],
475                                           wpa_s->conf->country[1]);
476 #ifdef CONFIG_WIFI_DISPLAY
477         } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
478                 res = os_snprintf(buf, buflen, "%d",
479                                   wpa_s->global->wifi_display);
480                 if (res < 0 || (unsigned int) res >= buflen)
481                         return -1;
482                 return res;
483 #endif /* CONFIG_WIFI_DISPLAY */
484 #ifdef CONFIG_TESTING_GET_GTK
485         } else if (os_strcmp(cmd, "gtk") == 0) {
486                 if (wpa_s->last_gtk_len == 0)
487                         return -1;
488                 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
489                                        wpa_s->last_gtk_len);
490                 return res;
491 #endif /* CONFIG_TESTING_GET_GTK */
492         }
493
494         if (res < 0 || (unsigned int) res >= buflen)
495                 return -1;
496         return res;
497 }
498
499
500 #ifdef IEEE8021X_EAPOL
501 static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
502                                              char *addr)
503 {
504         u8 bssid[ETH_ALEN];
505         struct wpa_ssid *ssid = wpa_s->current_ssid;
506
507         if (hwaddr_aton(addr, bssid)) {
508                 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
509                            "'%s'", addr);
510                 return -1;
511         }
512
513         wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
514         rsn_preauth_deinit(wpa_s->wpa);
515         if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
516                 return -1;
517
518         return 0;
519 }
520 #endif /* IEEE8021X_EAPOL */
521
522
523 #ifdef CONFIG_PEERKEY
524 /* MLME-STKSTART.request(peer) */
525 static int wpa_supplicant_ctrl_iface_stkstart(
526         struct wpa_supplicant *wpa_s, char *addr)
527 {
528         u8 peer[ETH_ALEN];
529
530         if (hwaddr_aton(addr, peer)) {
531                 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
532                            "address '%s'", addr);
533                 return -1;
534         }
535
536         wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
537                    MAC2STR(peer));
538
539         return wpa_sm_stkstart(wpa_s->wpa, peer);
540 }
541 #endif /* CONFIG_PEERKEY */
542
543
544 #ifdef CONFIG_TDLS
545
546 static int wpa_supplicant_ctrl_iface_tdls_discover(
547         struct wpa_supplicant *wpa_s, char *addr)
548 {
549         u8 peer[ETH_ALEN];
550         int ret;
551
552         if (hwaddr_aton(addr, peer)) {
553                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
554                            "address '%s'", addr);
555                 return -1;
556         }
557
558         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
559                    MAC2STR(peer));
560
561         if (wpa_tdls_is_external_setup(wpa_s->wpa))
562                 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
563         else
564                 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
565
566         return ret;
567 }
568
569
570 static int wpa_supplicant_ctrl_iface_tdls_setup(
571         struct wpa_supplicant *wpa_s, char *addr)
572 {
573         u8 peer[ETH_ALEN];
574         int ret;
575
576         if (hwaddr_aton(addr, peer)) {
577                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
578                            "address '%s'", addr);
579                 return -1;
580         }
581
582         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
583                    MAC2STR(peer));
584
585         if ((wpa_s->conf->tdls_external_control) &&
586             wpa_tdls_is_external_setup(wpa_s->wpa))
587                 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
588
589         wpa_tdls_remove(wpa_s->wpa, peer);
590
591         if (wpa_tdls_is_external_setup(wpa_s->wpa))
592                 ret = wpa_tdls_start(wpa_s->wpa, peer);
593         else
594                 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
595
596         return ret;
597 }
598
599
600 static int wpa_supplicant_ctrl_iface_tdls_teardown(
601         struct wpa_supplicant *wpa_s, char *addr)
602 {
603         u8 peer[ETH_ALEN];
604         int ret;
605
606         if (hwaddr_aton(addr, peer)) {
607                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
608                            "address '%s'", addr);
609                 return -1;
610         }
611
612         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
613                    MAC2STR(peer));
614
615         if ((wpa_s->conf->tdls_external_control) &&
616             wpa_tdls_is_external_setup(wpa_s->wpa))
617                 return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
618
619         if (wpa_tdls_is_external_setup(wpa_s->wpa))
620                 ret = wpa_tdls_teardown_link(
621                         wpa_s->wpa, peer,
622                         WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
623         else
624                 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
625
626         return ret;
627 }
628
629 #endif /* CONFIG_TDLS */
630
631
632 #ifdef CONFIG_IEEE80211R
633 static int wpa_supplicant_ctrl_iface_ft_ds(
634         struct wpa_supplicant *wpa_s, char *addr)
635 {
636         u8 target_ap[ETH_ALEN];
637         struct wpa_bss *bss;
638         const u8 *mdie;
639
640         if (hwaddr_aton(addr, target_ap)) {
641                 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
642                            "address '%s'", addr);
643                 return -1;
644         }
645
646         wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
647
648         bss = wpa_bss_get_bssid(wpa_s, target_ap);
649         if (bss)
650                 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
651         else
652                 mdie = NULL;
653
654         return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
655 }
656 #endif /* CONFIG_IEEE80211R */
657
658
659 #ifdef CONFIG_WPS
660 static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
661                                              char *cmd)
662 {
663         u8 bssid[ETH_ALEN], *_bssid = bssid;
664 #ifdef CONFIG_P2P
665         u8 p2p_dev_addr[ETH_ALEN];
666 #endif /* CONFIG_P2P */
667 #ifdef CONFIG_AP
668         u8 *_p2p_dev_addr = NULL;
669 #endif /* CONFIG_AP */
670
671         if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
672                 _bssid = NULL;
673 #ifdef CONFIG_P2P
674         } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
675                 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
676                         wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
677                                    "P2P Device Address '%s'",
678                                    cmd + 13);
679                         return -1;
680                 }
681                 _p2p_dev_addr = p2p_dev_addr;
682 #endif /* CONFIG_P2P */
683         } else if (hwaddr_aton(cmd, bssid)) {
684                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
685                            cmd);
686                 return -1;
687         }
688
689 #ifdef CONFIG_AP
690         if (wpa_s->ap_iface)
691                 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
692 #endif /* CONFIG_AP */
693
694         return wpas_wps_start_pbc(wpa_s, _bssid, 0);
695 }
696
697
698 static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
699                                              char *cmd, char *buf,
700                                              size_t buflen)
701 {
702         u8 bssid[ETH_ALEN], *_bssid = bssid;
703         char *pin;
704         int ret;
705
706         pin = os_strchr(cmd, ' ');
707         if (pin)
708                 *pin++ = '\0';
709
710         if (os_strcmp(cmd, "any") == 0)
711                 _bssid = NULL;
712         else if (os_strcmp(cmd, "get") == 0) {
713                 ret = wps_generate_pin();
714                 goto done;
715         } else if (hwaddr_aton(cmd, bssid)) {
716                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
717                            cmd);
718                 return -1;
719         }
720
721 #ifdef CONFIG_AP
722         if (wpa_s->ap_iface) {
723                 int timeout = 0;
724                 char *pos;
725
726                 if (pin) {
727                         pos = os_strchr(pin, ' ');
728                         if (pos) {
729                                 *pos++ = '\0';
730                                 timeout = atoi(pos);
731                         }
732                 }
733
734                 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
735                                                  buf, buflen, timeout);
736         }
737 #endif /* CONFIG_AP */
738
739         if (pin) {
740                 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
741                                          DEV_PW_DEFAULT);
742                 if (ret < 0)
743                         return -1;
744                 ret = os_snprintf(buf, buflen, "%s", pin);
745                 if (ret < 0 || (size_t) ret >= buflen)
746                         return -1;
747                 return ret;
748         }
749
750         ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
751         if (ret < 0)
752                 return -1;
753
754 done:
755         /* Return the generated PIN */
756         ret = os_snprintf(buf, buflen, "%08d", ret);
757         if (ret < 0 || (size_t) ret >= buflen)
758                 return -1;
759         return ret;
760 }
761
762
763 static int wpa_supplicant_ctrl_iface_wps_check_pin(
764         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
765 {
766         char pin[9];
767         size_t len;
768         char *pos;
769         int ret;
770
771         wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
772                               (u8 *) cmd, os_strlen(cmd));
773         for (pos = cmd, len = 0; *pos != '\0'; pos++) {
774                 if (*pos < '0' || *pos > '9')
775                         continue;
776                 pin[len++] = *pos;
777                 if (len == 9) {
778                         wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
779                         return -1;
780                 }
781         }
782         if (len != 4 && len != 8) {
783                 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
784                 return -1;
785         }
786         pin[len] = '\0';
787
788         if (len == 8) {
789                 unsigned int pin_val;
790                 pin_val = atoi(pin);
791                 if (!wps_pin_valid(pin_val)) {
792                         wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
793                         ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
794                         if (ret < 0 || (size_t) ret >= buflen)
795                                 return -1;
796                         return ret;
797                 }
798         }
799
800         ret = os_snprintf(buf, buflen, "%s", pin);
801         if (ret < 0 || (size_t) ret >= buflen)
802                 return -1;
803
804         return ret;
805 }
806
807
808 #ifdef CONFIG_WPS_NFC
809
810 static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
811                                              char *cmd)
812 {
813         u8 bssid[ETH_ALEN], *_bssid = bssid;
814
815         if (cmd == NULL || cmd[0] == '\0')
816                 _bssid = NULL;
817         else if (hwaddr_aton(cmd, bssid))
818                 return -1;
819
820         return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
821                                   0);
822 }
823
824
825 static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
826         struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
827 {
828         int ndef;
829         struct wpabuf *buf;
830         int res;
831         char *pos;
832
833         pos = os_strchr(cmd, ' ');
834         if (pos)
835                 *pos++ = '\0';
836         if (os_strcmp(cmd, "WPS") == 0)
837                 ndef = 0;
838         else if (os_strcmp(cmd, "NDEF") == 0)
839                 ndef = 1;
840         else
841                 return -1;
842
843         buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
844         if (buf == NULL)
845                 return -1;
846
847         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
848                                          wpabuf_len(buf));
849         reply[res++] = '\n';
850         reply[res] = '\0';
851
852         wpabuf_free(buf);
853
854         return res;
855 }
856
857
858 static int wpa_supplicant_ctrl_iface_wps_nfc_token(
859         struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
860 {
861         int ndef;
862         struct wpabuf *buf;
863         int res;
864
865         if (os_strcmp(cmd, "WPS") == 0)
866                 ndef = 0;
867         else if (os_strcmp(cmd, "NDEF") == 0)
868                 ndef = 1;
869         else
870                 return -1;
871
872         buf = wpas_wps_nfc_token(wpa_s, ndef);
873         if (buf == NULL)
874                 return -1;
875
876         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
877                                          wpabuf_len(buf));
878         reply[res++] = '\n';
879         reply[res] = '\0';
880
881         wpabuf_free(buf);
882
883         return res;
884 }
885
886
887 static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
888         struct wpa_supplicant *wpa_s, char *pos)
889 {
890         size_t len;
891         struct wpabuf *buf;
892         int ret;
893
894         len = os_strlen(pos);
895         if (len & 0x01)
896                 return -1;
897         len /= 2;
898
899         buf = wpabuf_alloc(len);
900         if (buf == NULL)
901                 return -1;
902         if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
903                 wpabuf_free(buf);
904                 return -1;
905         }
906
907         ret = wpas_wps_nfc_tag_read(wpa_s, buf);
908         wpabuf_free(buf);
909
910         return ret;
911 }
912
913
914 static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
915                                               char *reply, size_t max_len,
916                                               int ndef)
917 {
918         struct wpabuf *buf;
919         int res;
920
921         buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
922         if (buf == NULL)
923                 return -1;
924
925         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
926                                          wpabuf_len(buf));
927         reply[res++] = '\n';
928         reply[res] = '\0';
929
930         wpabuf_free(buf);
931
932         return res;
933 }
934
935
936 static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
937                                               char *reply, size_t max_len,
938                                               int ndef)
939 {
940         struct wpabuf *buf;
941         int res;
942
943         buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
944         if (buf == NULL) {
945                 wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
946                 return -1;
947         }
948
949         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
950                                          wpabuf_len(buf));
951         reply[res++] = '\n';
952         reply[res] = '\0';
953
954         wpabuf_free(buf);
955
956         return res;
957 }
958
959
960 static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
961                                           char *cmd, char *reply,
962                                           size_t max_len)
963 {
964         char *pos;
965         int ndef;
966
967         pos = os_strchr(cmd, ' ');
968         if (pos == NULL)
969                 return -1;
970         *pos++ = '\0';
971
972         if (os_strcmp(cmd, "WPS") == 0)
973                 ndef = 0;
974         else if (os_strcmp(cmd, "NDEF") == 0)
975                 ndef = 1;
976         else
977                 return -1;
978
979         if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
980                 if (!ndef)
981                         return -1;
982                 return wpas_ctrl_nfc_get_handover_req_wps(
983                         wpa_s, reply, max_len, ndef);
984         }
985
986         if (os_strcmp(pos, "P2P-CR") == 0) {
987                 return wpas_ctrl_nfc_get_handover_req_p2p(
988                         wpa_s, reply, max_len, ndef);
989         }
990
991         return -1;
992 }
993
994
995 static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
996                                               char *reply, size_t max_len,
997                                               int ndef, int cr, char *uuid)
998 {
999         struct wpabuf *buf;
1000         int res;
1001
1002         buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
1003         if (buf == NULL)
1004                 return -1;
1005
1006         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1007                                          wpabuf_len(buf));
1008         reply[res++] = '\n';
1009         reply[res] = '\0';
1010
1011         wpabuf_free(buf);
1012
1013         return res;
1014 }
1015
1016
1017 static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1018                                               char *reply, size_t max_len,
1019                                               int ndef, int tag)
1020 {
1021         struct wpabuf *buf;
1022         int res;
1023
1024         buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1025         if (buf == NULL)
1026                 return -1;
1027
1028         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1029                                          wpabuf_len(buf));
1030         reply[res++] = '\n';
1031         reply[res] = '\0';
1032
1033         wpabuf_free(buf);
1034
1035         return res;
1036 }
1037
1038
1039 static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1040                                           char *cmd, char *reply,
1041                                           size_t max_len)
1042 {
1043         char *pos, *pos2;
1044         int ndef;
1045
1046         pos = os_strchr(cmd, ' ');
1047         if (pos == NULL)
1048                 return -1;
1049         *pos++ = '\0';
1050
1051         if (os_strcmp(cmd, "WPS") == 0)
1052                 ndef = 0;
1053         else if (os_strcmp(cmd, "NDEF") == 0)
1054                 ndef = 1;
1055         else
1056                 return -1;
1057
1058         pos2 = os_strchr(pos, ' ');
1059         if (pos2)
1060                 *pos2++ = '\0';
1061         if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1062                 if (!ndef)
1063                         return -1;
1064                 return wpas_ctrl_nfc_get_handover_sel_wps(
1065                         wpa_s, reply, max_len, ndef,
1066                         os_strcmp(pos, "WPS-CR") == 0, pos2);
1067         }
1068
1069         if (os_strcmp(pos, "P2P-CR") == 0) {
1070                 return wpas_ctrl_nfc_get_handover_sel_p2p(
1071                         wpa_s, reply, max_len, ndef, 0);
1072         }
1073
1074         if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1075                 return wpas_ctrl_nfc_get_handover_sel_p2p(
1076                         wpa_s, reply, max_len, ndef, 1);
1077         }
1078
1079         return -1;
1080 }
1081
1082
1083 static int wpas_ctrl_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
1084                                          char *cmd, char *reply,
1085                                          size_t max_len)
1086 {
1087         size_t len;
1088         struct wpabuf *buf;
1089         int ret;
1090
1091         len = os_strlen(cmd);
1092         if (len & 0x01)
1093                 return -1;
1094         len /= 2;
1095
1096         buf = wpabuf_alloc(len);
1097         if (buf == NULL)
1098                 return -1;
1099         if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
1100                 wpabuf_free(buf);
1101                 return -1;
1102         }
1103
1104         ret = wpas_wps_nfc_rx_handover_req(wpa_s, buf);
1105         wpabuf_free(buf);
1106
1107         return ret;
1108 }
1109
1110
1111 static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
1112                                          char *cmd)
1113 {
1114         size_t len;
1115         struct wpabuf *buf;
1116         int ret;
1117
1118         len = os_strlen(cmd);
1119         if (len & 0x01)
1120                 return -1;
1121         len /= 2;
1122
1123         buf = wpabuf_alloc(len);
1124         if (buf == NULL)
1125                 return -1;
1126         if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
1127                 wpabuf_free(buf);
1128                 return -1;
1129         }
1130
1131         ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
1132         wpabuf_free(buf);
1133
1134         return ret;
1135 }
1136
1137
1138 static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1139                                          char *cmd)
1140 {
1141         size_t len;
1142         struct wpabuf *req, *sel;
1143         int ret;
1144         char *pos, *role, *type, *pos2;
1145
1146         role = cmd;
1147         pos = os_strchr(role, ' ');
1148         if (pos == NULL) {
1149                 wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
1150                 return -1;
1151         }
1152         *pos++ = '\0';
1153
1154         type = pos;
1155         pos = os_strchr(type, ' ');
1156         if (pos == NULL) {
1157                 wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
1158                 return -1;
1159         }
1160         *pos++ = '\0';
1161
1162         pos2 = os_strchr(pos, ' ');
1163         if (pos2 == NULL) {
1164                 wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
1165                 return -1;
1166         }
1167         *pos2++ = '\0';
1168
1169         len = os_strlen(pos);
1170         if (len & 0x01) {
1171                 wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
1172                 return -1;
1173         }
1174         len /= 2;
1175
1176         req = wpabuf_alloc(len);
1177         if (req == NULL) {
1178                 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
1179                 return -1;
1180         }
1181         if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
1182                 wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
1183                 wpabuf_free(req);
1184                 return -1;
1185         }
1186
1187         len = os_strlen(pos2);
1188         if (len & 0x01) {
1189                 wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
1190                 wpabuf_free(req);
1191                 return -1;
1192         }
1193         len /= 2;
1194
1195         sel = wpabuf_alloc(len);
1196         if (sel == NULL) {
1197                 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
1198                 wpabuf_free(req);
1199                 return -1;
1200         }
1201         if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
1202                 wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
1203                 wpabuf_free(req);
1204                 wpabuf_free(sel);
1205                 return -1;
1206         }
1207
1208         wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1209                    role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1210
1211         if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1212                 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
1213         } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1214         {
1215                 ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1216                 if (ret < 0)
1217                         ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
1218         } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1219         {
1220                 ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel);
1221         } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1222         {
1223                 ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel);
1224         } else {
1225                 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1226                            "reported: role=%s type=%s", role, type);
1227                 ret = -1;
1228         }
1229         wpabuf_free(req);
1230         wpabuf_free(sel);
1231
1232         if (ret)
1233                 wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1234
1235         return ret;
1236 }
1237
1238 #endif /* CONFIG_WPS_NFC */
1239
1240
1241 static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1242                                              char *cmd)
1243 {
1244         u8 bssid[ETH_ALEN];
1245         char *pin;
1246         char *new_ssid;
1247         char *new_auth;
1248         char *new_encr;
1249         char *new_key;
1250         struct wps_new_ap_settings ap;
1251
1252         pin = os_strchr(cmd, ' ');
1253         if (pin == NULL)
1254                 return -1;
1255         *pin++ = '\0';
1256
1257         if (hwaddr_aton(cmd, bssid)) {
1258                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1259                            cmd);
1260                 return -1;
1261         }
1262
1263         new_ssid = os_strchr(pin, ' ');
1264         if (new_ssid == NULL)
1265                 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1266         *new_ssid++ = '\0';
1267
1268         new_auth = os_strchr(new_ssid, ' ');
1269         if (new_auth == NULL)
1270                 return -1;
1271         *new_auth++ = '\0';
1272
1273         new_encr = os_strchr(new_auth, ' ');
1274         if (new_encr == NULL)
1275                 return -1;
1276         *new_encr++ = '\0';
1277
1278         new_key = os_strchr(new_encr, ' ');
1279         if (new_key == NULL)
1280                 return -1;
1281         *new_key++ = '\0';
1282
1283         os_memset(&ap, 0, sizeof(ap));
1284         ap.ssid_hex = new_ssid;
1285         ap.auth = new_auth;
1286         ap.encr = new_encr;
1287         ap.key_hex = new_key;
1288         return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1289 }
1290
1291
1292 #ifdef CONFIG_AP
1293 static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1294                                                 char *cmd, char *buf,
1295                                                 size_t buflen)
1296 {
1297         int timeout = 300;
1298         char *pos;
1299         const char *pin_txt;
1300
1301         if (!wpa_s->ap_iface)
1302                 return -1;
1303
1304         pos = os_strchr(cmd, ' ');
1305         if (pos)
1306                 *pos++ = '\0';
1307
1308         if (os_strcmp(cmd, "disable") == 0) {
1309                 wpas_wps_ap_pin_disable(wpa_s);
1310                 return os_snprintf(buf, buflen, "OK\n");
1311         }
1312
1313         if (os_strcmp(cmd, "random") == 0) {
1314                 if (pos)
1315                         timeout = atoi(pos);
1316                 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1317                 if (pin_txt == NULL)
1318                         return -1;
1319                 return os_snprintf(buf, buflen, "%s", pin_txt);
1320         }
1321
1322         if (os_strcmp(cmd, "get") == 0) {
1323                 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1324                 if (pin_txt == NULL)
1325                         return -1;
1326                 return os_snprintf(buf, buflen, "%s", pin_txt);
1327         }
1328
1329         if (os_strcmp(cmd, "set") == 0) {
1330                 char *pin;
1331                 if (pos == NULL)
1332                         return -1;
1333                 pin = pos;
1334                 pos = os_strchr(pos, ' ');
1335                 if (pos) {
1336                         *pos++ = '\0';
1337                         timeout = atoi(pos);
1338                 }
1339                 if (os_strlen(pin) > buflen)
1340                         return -1;
1341                 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1342                         return -1;
1343                 return os_snprintf(buf, buflen, "%s", pin);
1344         }
1345
1346         return -1;
1347 }
1348 #endif /* CONFIG_AP */
1349
1350
1351 #ifdef CONFIG_WPS_ER
1352 static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1353                                                 char *cmd)
1354 {
1355         char *uuid = cmd, *pin, *pos;
1356         u8 addr_buf[ETH_ALEN], *addr = NULL;
1357         pin = os_strchr(uuid, ' ');
1358         if (pin == NULL)
1359                 return -1;
1360         *pin++ = '\0';
1361         pos = os_strchr(pin, ' ');
1362         if (pos) {
1363                 *pos++ = '\0';
1364                 if (hwaddr_aton(pos, addr_buf) == 0)
1365                         addr = addr_buf;
1366         }
1367         return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1368 }
1369
1370
1371 static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1372                                                   char *cmd)
1373 {
1374         char *uuid = cmd, *pin;
1375         pin = os_strchr(uuid, ' ');
1376         if (pin == NULL)
1377                 return -1;
1378         *pin++ = '\0';
1379         return wpas_wps_er_learn(wpa_s, uuid, pin);
1380 }
1381
1382
1383 static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1384         struct wpa_supplicant *wpa_s, char *cmd)
1385 {
1386         char *uuid = cmd, *id;
1387         id = os_strchr(uuid, ' ');
1388         if (id == NULL)
1389                 return -1;
1390         *id++ = '\0';
1391         return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1392 }
1393
1394
1395 static int wpa_supplicant_ctrl_iface_wps_er_config(
1396         struct wpa_supplicant *wpa_s, char *cmd)
1397 {
1398         char *pin;
1399         char *new_ssid;
1400         char *new_auth;
1401         char *new_encr;
1402         char *new_key;
1403         struct wps_new_ap_settings ap;
1404
1405         pin = os_strchr(cmd, ' ');
1406         if (pin == NULL)
1407                 return -1;
1408         *pin++ = '\0';
1409
1410         new_ssid = os_strchr(pin, ' ');
1411         if (new_ssid == NULL)
1412                 return -1;
1413         *new_ssid++ = '\0';
1414
1415         new_auth = os_strchr(new_ssid, ' ');
1416         if (new_auth == NULL)
1417                 return -1;
1418         *new_auth++ = '\0';
1419
1420         new_encr = os_strchr(new_auth, ' ');
1421         if (new_encr == NULL)
1422                 return -1;
1423         *new_encr++ = '\0';
1424
1425         new_key = os_strchr(new_encr, ' ');
1426         if (new_key == NULL)
1427                 return -1;
1428         *new_key++ = '\0';
1429
1430         os_memset(&ap, 0, sizeof(ap));
1431         ap.ssid_hex = new_ssid;
1432         ap.auth = new_auth;
1433         ap.encr = new_encr;
1434         ap.key_hex = new_key;
1435         return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1436 }
1437
1438
1439 #ifdef CONFIG_WPS_NFC
1440 static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1441         struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1442 {
1443         int ndef;
1444         struct wpabuf *buf;
1445         int res;
1446         char *uuid;
1447
1448         uuid = os_strchr(cmd, ' ');
1449         if (uuid == NULL)
1450                 return -1;
1451         *uuid++ = '\0';
1452
1453         if (os_strcmp(cmd, "WPS") == 0)
1454                 ndef = 0;
1455         else if (os_strcmp(cmd, "NDEF") == 0)
1456                 ndef = 1;
1457         else
1458                 return -1;
1459
1460         buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1461         if (buf == NULL)
1462                 return -1;
1463
1464         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1465                                          wpabuf_len(buf));
1466         reply[res++] = '\n';
1467         reply[res] = '\0';
1468
1469         wpabuf_free(buf);
1470
1471         return res;
1472 }
1473 #endif /* CONFIG_WPS_NFC */
1474 #endif /* CONFIG_WPS_ER */
1475
1476 #endif /* CONFIG_WPS */
1477
1478
1479 #ifdef CONFIG_IBSS_RSN
1480 static int wpa_supplicant_ctrl_iface_ibss_rsn(
1481         struct wpa_supplicant *wpa_s, char *addr)
1482 {
1483         u8 peer[ETH_ALEN];
1484
1485         if (hwaddr_aton(addr, peer)) {
1486                 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1487                            "address '%s'", addr);
1488                 return -1;
1489         }
1490
1491         wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1492                    MAC2STR(peer));
1493
1494         return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1495 }
1496 #endif /* CONFIG_IBSS_RSN */
1497
1498
1499 static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1500                                               char *rsp)
1501 {
1502 #ifdef IEEE8021X_EAPOL
1503         char *pos, *id_pos;
1504         int id;
1505         struct wpa_ssid *ssid;
1506
1507         pos = os_strchr(rsp, '-');
1508         if (pos == NULL)
1509                 return -1;
1510         *pos++ = '\0';
1511         id_pos = pos;
1512         pos = os_strchr(pos, ':');
1513         if (pos == NULL)
1514                 return -1;
1515         *pos++ = '\0';
1516         id = atoi(id_pos);
1517         wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1518         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1519                               (u8 *) pos, os_strlen(pos));
1520
1521         ssid = wpa_config_get_network(wpa_s->conf, id);
1522         if (ssid == NULL) {
1523                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1524                            "to update", id);
1525                 return -1;
1526         }
1527
1528         return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1529                                                          pos);
1530 #else /* IEEE8021X_EAPOL */
1531         wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1532         return -1;
1533 #endif /* IEEE8021X_EAPOL */
1534 }
1535
1536
1537 static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1538                                             const char *params,
1539                                             char *buf, size_t buflen)
1540 {
1541         char *pos, *end, tmp[30];
1542         int res, verbose, wps, ret;
1543
1544         if (os_strcmp(params, "-DRIVER") == 0)
1545                 return wpa_drv_status(wpa_s, buf, buflen);
1546         verbose = os_strcmp(params, "-VERBOSE") == 0;
1547         wps = os_strcmp(params, "-WPS") == 0;
1548         pos = buf;
1549         end = buf + buflen;
1550         if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1551                 struct wpa_ssid *ssid = wpa_s->current_ssid;
1552                 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1553                                   MAC2STR(wpa_s->bssid));
1554                 if (ret < 0 || ret >= end - pos)
1555                         return pos - buf;
1556                 pos += ret;
1557                 if (ssid) {
1558                         u8 *_ssid = ssid->ssid;
1559                         size_t ssid_len = ssid->ssid_len;
1560                         u8 ssid_buf[MAX_SSID_LEN];
1561                         if (ssid_len == 0) {
1562                                 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1563                                 if (_res < 0)
1564                                         ssid_len = 0;
1565                                 else
1566                                         ssid_len = _res;
1567                                 _ssid = ssid_buf;
1568                         }
1569                         ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1570                                           wpa_ssid_txt(_ssid, ssid_len),
1571                                           ssid->id);
1572                         if (ret < 0 || ret >= end - pos)
1573                                 return pos - buf;
1574                         pos += ret;
1575
1576                         if (wps && ssid->passphrase &&
1577                             wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1578                             (ssid->mode == WPAS_MODE_AP ||
1579                              ssid->mode == WPAS_MODE_P2P_GO)) {
1580                                 ret = os_snprintf(pos, end - pos,
1581                                                   "passphrase=%s\n",
1582                                                   ssid->passphrase);
1583                                 if (ret < 0 || ret >= end - pos)
1584                                         return pos - buf;
1585                                 pos += ret;
1586                         }
1587                         if (ssid->id_str) {
1588                                 ret = os_snprintf(pos, end - pos,
1589                                                   "id_str=%s\n",
1590                                                   ssid->id_str);
1591                                 if (ret < 0 || ret >= end - pos)
1592                                         return pos - buf;
1593                                 pos += ret;
1594                         }
1595
1596                         switch (ssid->mode) {
1597                         case WPAS_MODE_INFRA:
1598                                 ret = os_snprintf(pos, end - pos,
1599                                                   "mode=station\n");
1600                                 break;
1601                         case WPAS_MODE_IBSS:
1602                                 ret = os_snprintf(pos, end - pos,
1603                                                   "mode=IBSS\n");
1604                                 break;
1605                         case WPAS_MODE_AP:
1606                                 ret = os_snprintf(pos, end - pos,
1607                                                   "mode=AP\n");
1608                                 break;
1609                         case WPAS_MODE_P2P_GO:
1610                                 ret = os_snprintf(pos, end - pos,
1611                                                   "mode=P2P GO\n");
1612                                 break;
1613                         case WPAS_MODE_P2P_GROUP_FORMATION:
1614                                 ret = os_snprintf(pos, end - pos,
1615                                                   "mode=P2P GO - group "
1616                                                   "formation\n");
1617                                 break;
1618                         default:
1619                                 ret = 0;
1620                                 break;
1621                         }
1622                         if (ret < 0 || ret >= end - pos)
1623                                 return pos - buf;
1624                         pos += ret;
1625                 }
1626
1627 #ifdef CONFIG_AP
1628                 if (wpa_s->ap_iface) {
1629                         pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1630                                                             end - pos,
1631                                                             verbose);
1632                 } else
1633 #endif /* CONFIG_AP */
1634                 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1635         }
1636 #ifdef CONFIG_SAE
1637         if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
1638 #ifdef CONFIG_AP
1639             !wpa_s->ap_iface &&
1640 #endif /* CONFIG_AP */
1641             wpa_s->sme.sae.state == SAE_ACCEPTED) {
1642                 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1643                                   wpa_s->sme.sae.group);
1644                 if (ret < 0 || ret >= end - pos)
1645                         return pos - buf;
1646                 pos += ret;
1647         }
1648 #endif /* CONFIG_SAE */
1649         ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1650                           wpa_supplicant_state_txt(wpa_s->wpa_state));
1651         if (ret < 0 || ret >= end - pos)
1652                 return pos - buf;
1653         pos += ret;
1654
1655         if (wpa_s->l2 &&
1656             l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1657                 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
1658                 if (ret < 0 || ret >= end - pos)
1659                         return pos - buf;
1660                 pos += ret;
1661         }
1662
1663 #ifdef CONFIG_P2P
1664         if (wpa_s->global->p2p) {
1665                 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1666                                   "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
1667                 if (ret < 0 || ret >= end - pos)
1668                         return pos - buf;
1669                 pos += ret;
1670         }
1671 #endif /* CONFIG_P2P */
1672
1673         ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1674                           MAC2STR(wpa_s->own_addr));
1675         if (ret < 0 || ret >= end - pos)
1676                 return pos - buf;
1677         pos += ret;
1678
1679 #ifdef CONFIG_HS20
1680         if (wpa_s->current_bss &&
1681             wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
1682             wpa_s->wpa_proto == WPA_PROTO_RSN &&
1683             wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1684                 ret = os_snprintf(pos, end - pos, "hs20=1\n");
1685                 if (ret < 0 || ret >= end - pos)
1686                         return pos - buf;
1687                 pos += ret;
1688         }
1689
1690         if (wpa_s->current_ssid) {
1691                 struct wpa_cred *cred;
1692                 char *type;
1693
1694                 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1695                         size_t i;
1696
1697                         if (wpa_s->current_ssid->parent_cred != cred)
1698                                 continue;
1699
1700                         for (i = 0; cred->domain && i < cred->num_domain; i++) {
1701                                 ret = os_snprintf(pos, end - pos,
1702                                                   "home_sp=%s\n",
1703                                                   cred->domain[i]);
1704                                 if (ret < 0 || ret >= end - pos)
1705                                         return pos - buf;
1706                                 pos += ret;
1707                         }
1708
1709                         if (wpa_s->current_bss == NULL ||
1710                             wpa_s->current_bss->anqp == NULL)
1711                                 res = -1;
1712                         else
1713                                 res = interworking_home_sp_cred(
1714                                         wpa_s, cred,
1715                                         wpa_s->current_bss->anqp->domain_name);
1716                         if (res > 0)
1717                                 type = "home";
1718                         else if (res == 0)
1719                                 type = "roaming";
1720                         else
1721                                 type = "unknown";
1722
1723                         ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
1724                         if (ret < 0 || ret >= end - pos)
1725                                 return pos - buf;
1726                         pos += ret;
1727
1728                         break;
1729                 }
1730         }
1731 #endif /* CONFIG_HS20 */
1732
1733         if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1734             wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1735                 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1736                                           verbose);
1737                 if (res >= 0)
1738                         pos += res;
1739         }
1740
1741         res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1742         if (res >= 0)
1743                 pos += res;
1744
1745 #ifdef CONFIG_WPS
1746         {
1747                 char uuid_str[100];
1748                 uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
1749                 ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
1750                 if (ret < 0 || ret >= end - pos)
1751                         return pos - buf;
1752                 pos += ret;
1753         }
1754 #endif /* CONFIG_WPS */
1755
1756 #ifdef ANDROID
1757         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
1758                      "id=%d state=%d BSSID=" MACSTR " SSID=%s",
1759                      wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
1760                      wpa_s->wpa_state,
1761                      MAC2STR(wpa_s->bssid),
1762                      wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1763                      wpa_ssid_txt(wpa_s->current_ssid->ssid,
1764                                   wpa_s->current_ssid->ssid_len) : "");
1765         if (wpa_s->wpa_state == WPA_COMPLETED) {
1766                 struct wpa_ssid *ssid = wpa_s->current_ssid;
1767                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
1768                              "- connection to " MACSTR
1769                              " completed %s [id=%d id_str=%s]",
1770                              MAC2STR(wpa_s->bssid), "(auth)",
1771                              ssid ? ssid->id : -1,
1772                              ssid && ssid->id_str ? ssid->id_str : "");
1773         }
1774 #endif /* ANDROID */
1775
1776         return pos - buf;
1777 }
1778
1779
1780 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1781                                            char *cmd)
1782 {
1783         char *pos;
1784         int id;
1785         struct wpa_ssid *ssid;
1786         u8 bssid[ETH_ALEN];
1787
1788         /* cmd: "<network id> <BSSID>" */
1789         pos = os_strchr(cmd, ' ');
1790         if (pos == NULL)
1791                 return -1;
1792         *pos++ = '\0';
1793         id = atoi(cmd);
1794         wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1795         if (hwaddr_aton(pos, bssid)) {
1796                 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1797                 return -1;
1798         }
1799
1800         ssid = wpa_config_get_network(wpa_s->conf, id);
1801         if (ssid == NULL) {
1802                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1803                            "to update", id);
1804                 return -1;
1805         }
1806
1807         os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1808         ssid->bssid_set = !is_zero_ether_addr(bssid);
1809
1810         return 0;
1811 }
1812
1813
1814 static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
1815                                                char *cmd, char *buf,
1816                                                size_t buflen)
1817 {
1818         u8 bssid[ETH_ALEN];
1819         struct wpa_blacklist *e;
1820         char *pos, *end;
1821         int ret;
1822
1823         /* cmd: "BLACKLIST [<BSSID>]" */
1824         if (*cmd == '\0') {
1825                 pos = buf;
1826                 end = buf + buflen;
1827                 e = wpa_s->blacklist;
1828                 while (e) {
1829                         ret = os_snprintf(pos, end - pos, MACSTR "\n",
1830                                           MAC2STR(e->bssid));
1831                         if (ret < 0 || ret >= end - pos)
1832                                 return pos - buf;
1833                         pos += ret;
1834                         e = e->next;
1835                 }
1836                 return pos - buf;
1837         }
1838
1839         cmd++;
1840         if (os_strncmp(cmd, "clear", 5) == 0) {
1841                 wpa_blacklist_clear(wpa_s);
1842                 os_memcpy(buf, "OK\n", 3);
1843                 return 3;
1844         }
1845
1846         wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1847         if (hwaddr_aton(cmd, bssid)) {
1848                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
1849                 return -1;
1850         }
1851
1852         /*
1853          * Add the BSSID twice, so its count will be 2, causing it to be
1854          * skipped when processing scan results.
1855          */
1856         ret = wpa_blacklist_add(wpa_s, bssid);
1857         if (ret != 0)
1858                 return -1;
1859         ret = wpa_blacklist_add(wpa_s, bssid);
1860         if (ret != 0)
1861                 return -1;
1862         os_memcpy(buf, "OK\n", 3);
1863         return 3;
1864 }
1865
1866
1867 static const char * debug_level_str(int level)
1868 {
1869         switch (level) {
1870         case MSG_EXCESSIVE:
1871                 return "EXCESSIVE";
1872         case MSG_MSGDUMP:
1873                 return "MSGDUMP";
1874         case MSG_DEBUG:
1875                 return "DEBUG";
1876         case MSG_INFO:
1877                 return "INFO";
1878         case MSG_WARNING:
1879                 return "WARNING";
1880         case MSG_ERROR:
1881                 return "ERROR";
1882         default:
1883                 return "?";
1884         }
1885 }
1886
1887
1888 static int str_to_debug_level(const char *s)
1889 {
1890         if (os_strcasecmp(s, "EXCESSIVE") == 0)
1891                 return MSG_EXCESSIVE;
1892         if (os_strcasecmp(s, "MSGDUMP") == 0)
1893                 return MSG_MSGDUMP;
1894         if (os_strcasecmp(s, "DEBUG") == 0)
1895                 return MSG_DEBUG;
1896         if (os_strcasecmp(s, "INFO") == 0)
1897                 return MSG_INFO;
1898         if (os_strcasecmp(s, "WARNING") == 0)
1899                 return MSG_WARNING;
1900         if (os_strcasecmp(s, "ERROR") == 0)
1901                 return MSG_ERROR;
1902         return -1;
1903 }
1904
1905
1906 static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1907                                                char *cmd, char *buf,
1908                                                size_t buflen)
1909 {
1910         char *pos, *end, *stamp;
1911         int ret;
1912
1913         if (cmd == NULL) {
1914                 return -1;
1915         }
1916
1917         /* cmd: "LOG_LEVEL [<level>]" */
1918         if (*cmd == '\0') {
1919                 pos = buf;
1920                 end = buf + buflen;
1921                 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1922                                   "Timestamp: %d\n",
1923                                   debug_level_str(wpa_debug_level),
1924                                   wpa_debug_timestamp);
1925                 if (ret < 0 || ret >= end - pos)
1926                         ret = 0;
1927
1928                 return ret;
1929         }
1930
1931         while (*cmd == ' ')
1932                 cmd++;
1933
1934         stamp = os_strchr(cmd, ' ');
1935         if (stamp) {
1936                 *stamp++ = '\0';
1937                 while (*stamp == ' ') {
1938                         stamp++;
1939                 }
1940         }
1941
1942         if (cmd && os_strlen(cmd)) {
1943                 int level = str_to_debug_level(cmd);
1944                 if (level < 0)
1945                         return -1;
1946                 wpa_debug_level = level;
1947         }
1948
1949         if (stamp && os_strlen(stamp))
1950                 wpa_debug_timestamp = atoi(stamp);
1951
1952         os_memcpy(buf, "OK\n", 3);
1953         return 3;
1954 }
1955
1956
1957 static int wpa_supplicant_ctrl_iface_list_networks(
1958         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1959 {
1960         char *pos, *end;
1961         struct wpa_ssid *ssid;
1962         int ret;
1963
1964         pos = buf;
1965         end = buf + buflen;
1966         ret = os_snprintf(pos, end - pos,
1967                           "network id / ssid / bssid / flags\n");
1968         if (ret < 0 || ret >= end - pos)
1969                 return pos - buf;
1970         pos += ret;
1971
1972         ssid = wpa_s->conf->ssid;
1973         while (ssid) {
1974                 ret = os_snprintf(pos, end - pos, "%d\t%s",
1975                                   ssid->id,
1976                                   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1977                 if (ret < 0 || ret >= end - pos)
1978                         return pos - buf;
1979                 pos += ret;
1980                 if (ssid->bssid_set) {
1981                         ret = os_snprintf(pos, end - pos, "\t" MACSTR,
1982                                           MAC2STR(ssid->bssid));
1983                 } else {
1984                         ret = os_snprintf(pos, end - pos, "\tany");
1985                 }
1986                 if (ret < 0 || ret >= end - pos)
1987                         return pos - buf;
1988                 pos += ret;
1989                 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
1990                                   ssid == wpa_s->current_ssid ?
1991                                   "[CURRENT]" : "",
1992                                   ssid->disabled ? "[DISABLED]" : "",
1993                                   ssid->disabled_until.sec ?
1994                                   "[TEMP-DISABLED]" : "",
1995                                   ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
1996                                   "");
1997                 if (ret < 0 || ret >= end - pos)
1998                         return pos - buf;
1999                 pos += ret;
2000                 ret = os_snprintf(pos, end - pos, "\n");
2001                 if (ret < 0 || ret >= end - pos)
2002                         return pos - buf;
2003                 pos += ret;
2004
2005                 ssid = ssid->next;
2006         }
2007
2008         return pos - buf;
2009 }
2010
2011
2012 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2013 {
2014         int ret;
2015         ret = os_snprintf(pos, end - pos, "-");
2016         if (ret < 0 || ret >= end - pos)
2017                 return pos;
2018         pos += ret;
2019         ret = wpa_write_ciphers(pos, end, cipher, "+");
2020         if (ret < 0)
2021                 return pos;
2022         pos += ret;
2023         return pos;
2024 }
2025
2026
2027 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2028                                     const u8 *ie, size_t ie_len)
2029 {
2030         struct wpa_ie_data data;
2031         int first, ret;
2032
2033         ret = os_snprintf(pos, end - pos, "[%s-", proto);
2034         if (ret < 0 || ret >= end - pos)
2035                 return pos;
2036         pos += ret;
2037
2038         if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2039                 ret = os_snprintf(pos, end - pos, "?]");
2040                 if (ret < 0 || ret >= end - pos)
2041                         return pos;
2042                 pos += ret;
2043                 return pos;
2044         }
2045
2046         first = 1;
2047         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
2048                 ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
2049                 if (ret < 0 || ret >= end - pos)
2050                         return pos;
2051                 pos += ret;
2052                 first = 0;
2053         }
2054         if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
2055                 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
2056                 if (ret < 0 || ret >= end - pos)
2057                         return pos;
2058                 pos += ret;
2059                 first = 0;
2060         }
2061         if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
2062                 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
2063                 if (ret < 0 || ret >= end - pos)
2064                         return pos;
2065                 pos += ret;
2066                 first = 0;
2067         }
2068 #ifdef CONFIG_IEEE80211R
2069         if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2070                 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
2071                                   first ? "" : "+");
2072                 if (ret < 0 || ret >= end - pos)
2073                         return pos;
2074                 pos += ret;
2075                 first = 0;
2076         }
2077         if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2078                 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
2079                                   first ? "" : "+");
2080                 if (ret < 0 || ret >= end - pos)
2081                         return pos;
2082                 pos += ret;
2083                 first = 0;
2084         }
2085 #endif /* CONFIG_IEEE80211R */
2086 #ifdef CONFIG_IEEE80211W
2087         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2088                 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
2089                                   first ? "" : "+");
2090                 if (ret < 0 || ret >= end - pos)
2091                         return pos;
2092                 pos += ret;
2093                 first = 0;
2094         }
2095         if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2096                 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
2097                                   first ? "" : "+");
2098                 if (ret < 0 || ret >= end - pos)
2099                         return pos;
2100                 pos += ret;
2101                 first = 0;
2102         }
2103 #endif /* CONFIG_IEEE80211W */
2104
2105         pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2106
2107         if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2108                 ret = os_snprintf(pos, end - pos, "-preauth");
2109                 if (ret < 0 || ret >= end - pos)
2110                         return pos;
2111                 pos += ret;
2112         }
2113
2114         ret = os_snprintf(pos, end - pos, "]");
2115         if (ret < 0 || ret >= end - pos)
2116                 return pos;
2117         pos += ret;
2118
2119         return pos;
2120 }
2121
2122
2123 #ifdef CONFIG_WPS
2124 static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2125                                             char *pos, char *end,
2126                                             struct wpabuf *wps_ie)
2127 {
2128         int ret;
2129         const char *txt;
2130
2131         if (wps_ie == NULL)
2132                 return pos;
2133         if (wps_is_selected_pbc_registrar(wps_ie))
2134                 txt = "[WPS-PBC]";
2135 #ifdef CONFIG_WPS2
2136         else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2137                 txt = "[WPS-AUTH]";
2138 #endif /* CONFIG_WPS2 */
2139         else if (wps_is_selected_pin_registrar(wps_ie))
2140                 txt = "[WPS-PIN]";
2141         else
2142                 txt = "[WPS]";
2143
2144         ret = os_snprintf(pos, end - pos, "%s", txt);
2145         if (ret >= 0 && ret < end - pos)
2146                 pos += ret;
2147         wpabuf_free(wps_ie);
2148         return pos;
2149 }
2150 #endif /* CONFIG_WPS */
2151
2152
2153 static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2154                                         char *pos, char *end,
2155                                         const struct wpa_bss *bss)
2156 {
2157 #ifdef CONFIG_WPS
2158         struct wpabuf *wps_ie;
2159         wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2160         return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2161 #else /* CONFIG_WPS */
2162         return pos;
2163 #endif /* CONFIG_WPS */
2164 }
2165
2166
2167 /* Format one result on one text line into a buffer. */
2168 static int wpa_supplicant_ctrl_iface_scan_result(
2169         struct wpa_supplicant *wpa_s,
2170         const struct wpa_bss *bss, char *buf, size_t buflen)
2171 {
2172         char *pos, *end;
2173         int ret;
2174         const u8 *ie, *ie2, *p2p;
2175
2176         p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
2177         if (!p2p)
2178                 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
2179         if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2180             os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2181             0)
2182                 return 0; /* Do not show P2P listen discovery results here */
2183
2184         pos = buf;
2185         end = buf + buflen;
2186
2187         ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2188                           MAC2STR(bss->bssid), bss->freq, bss->level);
2189         if (ret < 0 || ret >= end - pos)
2190                 return -1;
2191         pos += ret;
2192         ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2193         if (ie)
2194                 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2195         ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2196         if (ie2)
2197                 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
2198         pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2199         if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2200                 ret = os_snprintf(pos, end - pos, "[WEP]");
2201                 if (ret < 0 || ret >= end - pos)
2202                         return -1;
2203                 pos += ret;
2204         }
2205         if (bss->caps & IEEE80211_CAP_IBSS) {
2206                 ret = os_snprintf(pos, end - pos, "[IBSS]");
2207                 if (ret < 0 || ret >= end - pos)
2208                         return -1;
2209                 pos += ret;
2210         }
2211         if (bss->caps & IEEE80211_CAP_ESS) {
2212                 ret = os_snprintf(pos, end - pos, "[ESS]");
2213                 if (ret < 0 || ret >= end - pos)
2214                         return -1;
2215                 pos += ret;
2216         }
2217         if (p2p) {
2218                 ret = os_snprintf(pos, end - pos, "[P2P]");
2219                 if (ret < 0 || ret >= end - pos)
2220                         return -1;
2221                 pos += ret;
2222         }
2223 #ifdef CONFIG_HS20
2224         if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
2225                 ret = os_snprintf(pos, end - pos, "[HS20]");
2226                 if (ret < 0 || ret >= end - pos)
2227                         return -1;
2228                 pos += ret;
2229         }
2230 #endif /* CONFIG_HS20 */
2231
2232         ret = os_snprintf(pos, end - pos, "\t%s",
2233                           wpa_ssid_txt(bss->ssid, bss->ssid_len));
2234         if (ret < 0 || ret >= end - pos)
2235                 return -1;
2236         pos += ret;
2237
2238         ret = os_snprintf(pos, end - pos, "\n");
2239         if (ret < 0 || ret >= end - pos)
2240                 return -1;
2241         pos += ret;
2242
2243         return pos - buf;
2244 }
2245
2246
2247 static int wpa_supplicant_ctrl_iface_scan_results(
2248         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2249 {
2250         char *pos, *end;
2251         struct wpa_bss *bss;
2252         int ret;
2253
2254         pos = buf;
2255         end = buf + buflen;
2256         ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2257                           "flags / ssid\n");
2258         if (ret < 0 || ret >= end - pos)
2259                 return pos - buf;
2260         pos += ret;
2261
2262         dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2263                 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2264                                                             end - pos);
2265                 if (ret < 0 || ret >= end - pos)
2266                         return pos - buf;
2267                 pos += ret;
2268         }
2269
2270         return pos - buf;
2271 }
2272
2273
2274 static int wpa_supplicant_ctrl_iface_select_network(
2275         struct wpa_supplicant *wpa_s, char *cmd)
2276 {
2277         int id;
2278         struct wpa_ssid *ssid;
2279
2280         /* cmd: "<network id>" or "any" */
2281         if (os_strcmp(cmd, "any") == 0) {
2282                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2283                 ssid = NULL;
2284         } else {
2285                 id = atoi(cmd);
2286                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2287
2288                 ssid = wpa_config_get_network(wpa_s->conf, id);
2289                 if (ssid == NULL) {
2290                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2291                                    "network id=%d", id);
2292                         return -1;
2293                 }
2294                 if (ssid->disabled == 2) {
2295                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2296                                    "SELECT_NETWORK with persistent P2P group");
2297                         return -1;
2298                 }
2299         }
2300
2301         wpa_supplicant_select_network(wpa_s, ssid);
2302
2303         return 0;
2304 }
2305
2306
2307 static int wpa_supplicant_ctrl_iface_enable_network(
2308         struct wpa_supplicant *wpa_s, char *cmd)
2309 {
2310         int id;
2311         struct wpa_ssid *ssid;
2312
2313         /* cmd: "<network id>" or "all" */
2314         if (os_strcmp(cmd, "all") == 0) {
2315                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2316                 ssid = NULL;
2317         } else {
2318                 id = atoi(cmd);
2319                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2320
2321                 ssid = wpa_config_get_network(wpa_s->conf, id);
2322                 if (ssid == NULL) {
2323                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2324                                    "network id=%d", id);
2325                         return -1;
2326                 }
2327                 if (ssid->disabled == 2) {
2328                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2329                                    "ENABLE_NETWORK with persistent P2P group");
2330                         return -1;
2331                 }
2332
2333                 if (os_strstr(cmd, " no-connect")) {
2334                         ssid->disabled = 0;
2335                         return 0;
2336                 }
2337         }
2338         wpa_supplicant_enable_network(wpa_s, ssid);
2339
2340         return 0;
2341 }
2342
2343
2344 static int wpa_supplicant_ctrl_iface_disable_network(
2345         struct wpa_supplicant *wpa_s, char *cmd)
2346 {
2347         int id;
2348         struct wpa_ssid *ssid;
2349
2350         /* cmd: "<network id>" or "all" */
2351         if (os_strcmp(cmd, "all") == 0) {
2352                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2353                 ssid = NULL;
2354         } else {
2355                 id = atoi(cmd);
2356                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2357
2358                 ssid = wpa_config_get_network(wpa_s->conf, id);
2359                 if (ssid == NULL) {
2360                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2361                                    "network id=%d", id);
2362                         return -1;
2363                 }
2364                 if (ssid->disabled == 2) {
2365                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2366                                    "DISABLE_NETWORK with persistent P2P "
2367                                    "group");
2368                         return -1;
2369                 }
2370         }
2371         wpa_supplicant_disable_network(wpa_s, ssid);
2372
2373         return 0;
2374 }
2375
2376
2377 static int wpa_supplicant_ctrl_iface_add_network(
2378         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2379 {
2380         struct wpa_ssid *ssid;
2381         int ret;
2382
2383         wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2384
2385         ssid = wpa_config_add_network(wpa_s->conf);
2386         if (ssid == NULL)
2387                 return -1;
2388
2389         wpas_notify_network_added(wpa_s, ssid);
2390
2391         ssid->disabled = 1;
2392         wpa_config_set_network_defaults(ssid);
2393
2394         ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
2395         if (ret < 0 || (size_t) ret >= buflen)
2396                 return -1;
2397         return ret;
2398 }
2399
2400
2401 static int wpa_supplicant_ctrl_iface_remove_network(
2402         struct wpa_supplicant *wpa_s, char *cmd)
2403 {
2404         int id;
2405         struct wpa_ssid *ssid;
2406         int was_disabled;
2407
2408         /* cmd: "<network id>" or "all" */
2409         if (os_strcmp(cmd, "all") == 0) {
2410                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
2411                 if (wpa_s->sched_scanning)
2412                         wpa_supplicant_cancel_sched_scan(wpa_s);
2413
2414                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2415                 if (wpa_s->current_ssid) {
2416 #ifdef CONFIG_SME
2417                         wpa_s->sme.prev_bssid_set = 0;
2418 #endif /* CONFIG_SME */
2419                         wpa_sm_set_config(wpa_s->wpa, NULL);
2420                         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2421                         wpa_supplicant_deauthenticate(
2422                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2423                 }
2424                 ssid = wpa_s->conf->ssid;
2425                 while (ssid) {
2426                         struct wpa_ssid *remove_ssid = ssid;
2427                         id = ssid->id;
2428                         ssid = ssid->next;
2429                         wpas_notify_network_removed(wpa_s, remove_ssid);
2430                         wpa_config_remove_network(wpa_s->conf, id);
2431                 }
2432                 return 0;
2433         }
2434
2435         id = atoi(cmd);
2436         wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2437
2438         ssid = wpa_config_get_network(wpa_s->conf, id);
2439         if (ssid)
2440                 wpas_notify_network_removed(wpa_s, ssid);
2441         if (ssid == NULL) {
2442                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2443                            "id=%d", id);
2444                 return -1;
2445         }
2446
2447         if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
2448 #ifdef CONFIG_SME
2449                 wpa_s->sme.prev_bssid_set = 0;
2450 #endif /* CONFIG_SME */
2451                 /*
2452                  * Invalidate the EAP session cache if the current or
2453                  * previously used network is removed.
2454                  */
2455                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2456         }
2457
2458         if (ssid == wpa_s->current_ssid) {
2459                 wpa_sm_set_config(wpa_s->wpa, NULL);
2460                 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2461
2462                 wpa_supplicant_deauthenticate(wpa_s,
2463                                               WLAN_REASON_DEAUTH_LEAVING);
2464         }
2465
2466         was_disabled = ssid->disabled;
2467
2468         if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2469                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2470                            "network id=%d", id);
2471                 return -1;
2472         }
2473
2474         if (!was_disabled && wpa_s->sched_scanning) {
2475                 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2476                            "network from filters");
2477                 wpa_supplicant_cancel_sched_scan(wpa_s);
2478                 wpa_supplicant_req_scan(wpa_s, 0, 0);
2479         }
2480
2481         return 0;
2482 }
2483
2484
2485 static int wpa_supplicant_ctrl_iface_set_network(
2486         struct wpa_supplicant *wpa_s, char *cmd)
2487 {
2488         int id;
2489         struct wpa_ssid *ssid;
2490         char *name, *value;
2491
2492         /* cmd: "<network id> <variable name> <value>" */
2493         name = os_strchr(cmd, ' ');
2494         if (name == NULL)
2495                 return -1;
2496         *name++ = '\0';
2497
2498         value = os_strchr(name, ' ');
2499         if (value == NULL)
2500                 return -1;
2501         *value++ = '\0';
2502
2503         id = atoi(cmd);
2504         wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2505                    id, name);
2506         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2507                               (u8 *) value, os_strlen(value));
2508
2509         ssid = wpa_config_get_network(wpa_s->conf, id);
2510         if (ssid == NULL) {
2511                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2512                            "id=%d", id);
2513                 return -1;
2514         }
2515
2516         if (wpa_config_set(ssid, name, value, 0) < 0) {
2517                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2518                            "variable '%s'", name);
2519                 return -1;
2520         }
2521
2522         if (os_strcmp(name, "bssid") != 0 &&
2523             os_strcmp(name, "priority") != 0)
2524                 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
2525
2526         if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
2527                 /*
2528                  * Invalidate the EAP session cache if anything in the current
2529                  * or previously used configuration changes.
2530                  */
2531                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2532         }
2533
2534         if ((os_strcmp(name, "psk") == 0 &&
2535              value[0] == '"' && ssid->ssid_len) ||
2536             (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2537                 wpa_config_update_psk(ssid);
2538         else if (os_strcmp(name, "priority") == 0)
2539                 wpa_config_update_prio_list(wpa_s->conf);
2540
2541         return 0;
2542 }
2543
2544
2545 static int wpa_supplicant_ctrl_iface_get_network(
2546         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2547 {
2548         int id;
2549         size_t res;
2550         struct wpa_ssid *ssid;
2551         char *name, *value;
2552
2553         /* cmd: "<network id> <variable name>" */
2554         name = os_strchr(cmd, ' ');
2555         if (name == NULL || buflen == 0)
2556                 return -1;
2557         *name++ = '\0';
2558
2559         id = atoi(cmd);
2560         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2561                    id, name);
2562
2563         ssid = wpa_config_get_network(wpa_s->conf, id);
2564         if (ssid == NULL) {
2565                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2566                            "id=%d", id);
2567                 return -1;
2568         }
2569
2570         value = wpa_config_get_no_key(ssid, name);
2571         if (value == NULL) {
2572                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2573                            "variable '%s'", name);
2574                 return -1;
2575         }
2576
2577         res = os_strlcpy(buf, value, buflen);
2578         if (res >= buflen) {
2579                 os_free(value);
2580                 return -1;
2581         }
2582
2583         os_free(value);
2584
2585         return res;
2586 }
2587
2588
2589 static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2590                                                 char *buf, size_t buflen)
2591 {
2592         char *pos, *end;
2593         struct wpa_cred *cred;
2594         int ret;
2595
2596         pos = buf;
2597         end = buf + buflen;
2598         ret = os_snprintf(pos, end - pos,
2599                           "cred id / realm / username / domain / imsi\n");
2600         if (ret < 0 || ret >= end - pos)
2601                 return pos - buf;
2602         pos += ret;
2603
2604         cred = wpa_s->conf->cred;
2605         while (cred) {
2606                 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2607                                   cred->id, cred->realm ? cred->realm : "",
2608                                   cred->username ? cred->username : "",
2609                                   cred->domain ? cred->domain[0] : "",
2610                                   cred->imsi ? cred->imsi : "");
2611                 if (ret < 0 || ret >= end - pos)
2612                         return pos - buf;
2613                 pos += ret;
2614
2615                 cred = cred->next;
2616         }
2617
2618         return pos - buf;
2619 }
2620
2621
2622 static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2623                                               char *buf, size_t buflen)
2624 {
2625         struct wpa_cred *cred;
2626         int ret;
2627
2628         wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2629
2630         cred = wpa_config_add_cred(wpa_s->conf);
2631         if (cred == NULL)
2632                 return -1;
2633
2634         ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2635         if (ret < 0 || (size_t) ret >= buflen)
2636                 return -1;
2637         return ret;
2638 }
2639
2640
2641 static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2642                                  struct wpa_cred *cred)
2643 {
2644         struct wpa_ssid *ssid;
2645         char str[20];
2646
2647         if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
2648                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2649                 return -1;
2650         }
2651
2652         /* Remove any network entry created based on the removed credential */
2653         ssid = wpa_s->conf->ssid;
2654         while (ssid) {
2655                 if (ssid->parent_cred == cred) {
2656                         wpa_printf(MSG_DEBUG, "Remove network id %d since it "
2657                                    "used the removed credential", ssid->id);
2658                         os_snprintf(str, sizeof(str), "%d", ssid->id);
2659                         ssid = ssid->next;
2660                         wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
2661                 } else
2662                         ssid = ssid->next;
2663         }
2664
2665         return 0;
2666 }
2667
2668
2669 static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2670                                                  char *cmd)
2671 {
2672         int id;
2673         struct wpa_cred *cred, *prev;
2674
2675         /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
2676         if (os_strcmp(cmd, "all") == 0) {
2677                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2678                 cred = wpa_s->conf->cred;
2679                 while (cred) {
2680                         prev = cred;
2681                         cred = cred->next;
2682                         wpas_ctrl_remove_cred(wpa_s, prev);
2683                 }
2684                 return 0;
2685         }
2686
2687         if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
2688                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
2689                            cmd + 8);
2690                 cred = wpa_s->conf->cred;
2691                 while (cred) {
2692                         prev = cred;
2693                         cred = cred->next;
2694                         if (prev->domain) {
2695                                 size_t i;
2696                                 for (i = 0; i < prev->num_domain; i++) {
2697                                         if (os_strcmp(prev->domain[i], cmd + 8)
2698                                             != 0)
2699                                                 continue;
2700                                         wpas_ctrl_remove_cred(wpa_s, prev);
2701                                         break;
2702                                 }
2703                         }
2704                 }
2705                 return 0;
2706         }
2707
2708         id = atoi(cmd);
2709         wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2710
2711         cred = wpa_config_get_cred(wpa_s->conf, id);
2712         return wpas_ctrl_remove_cred(wpa_s, cred);
2713 }
2714
2715
2716 static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2717                                               char *cmd)
2718 {
2719         int id;
2720         struct wpa_cred *cred;
2721         char *name, *value;
2722
2723         /* cmd: "<cred id> <variable name> <value>" */
2724         name = os_strchr(cmd, ' ');
2725         if (name == NULL)
2726                 return -1;
2727         *name++ = '\0';
2728
2729         value = os_strchr(name, ' ');
2730         if (value == NULL)
2731                 return -1;
2732         *value++ = '\0';
2733
2734         id = atoi(cmd);
2735         wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2736                    id, name);
2737         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2738                               (u8 *) value, os_strlen(value));
2739
2740         cred = wpa_config_get_cred(wpa_s->conf, id);
2741         if (cred == NULL) {
2742                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2743                            id);
2744                 return -1;
2745         }
2746
2747         if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2748                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2749                            "variable '%s'", name);
2750                 return -1;
2751         }
2752
2753         return 0;
2754 }
2755
2756
2757 #ifndef CONFIG_NO_CONFIG_WRITE
2758 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2759 {
2760         int ret;
2761
2762         if (!wpa_s->conf->update_config) {
2763                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2764                            "to update configuration (update_config=0)");
2765                 return -1;
2766         }
2767
2768         ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2769         if (ret) {
2770                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2771                            "update configuration");
2772         } else {
2773                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2774                            " updated");
2775         }
2776
2777         return ret;
2778 }
2779 #endif /* CONFIG_NO_CONFIG_WRITE */
2780
2781
2782 struct cipher_info {
2783         unsigned int capa;
2784         const char *name;
2785         int group_only;
2786 };
2787
2788 static const struct cipher_info ciphers[] = {
2789         { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
2790         { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
2791         { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
2792         { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
2793         { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
2794         { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
2795         { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
2796         { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
2797 };
2798
2799
2800 static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2801                                               struct wpa_driver_capa *capa,
2802                                               char *buf, size_t buflen)
2803 {
2804         int ret, first = 1;
2805         char *pos, *end;
2806         size_t len;
2807         unsigned int i;
2808
2809         pos = buf;
2810         end = pos + buflen;
2811
2812         if (res < 0) {
2813                 if (strict)
2814                         return 0;
2815                 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2816                 if (len >= buflen)
2817                         return -1;
2818                 return len;
2819         }
2820
2821         for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
2822                 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
2823                         ret = os_snprintf(pos, end - pos, "%s%s",
2824                                           first ? "" : " ", ciphers[i].name);
2825                         if (ret < 0 || ret >= end - pos)
2826                                 return pos - buf;
2827                         pos += ret;
2828                         first = 0;
2829                 }
2830         }
2831
2832         return pos - buf;
2833 }
2834
2835
2836 static int ctrl_iface_get_capability_group(int res, char *strict,
2837                                            struct wpa_driver_capa *capa,
2838                                            char *buf, size_t buflen)
2839 {
2840         int ret, first = 1;
2841         char *pos, *end;
2842         size_t len;
2843         unsigned int i;
2844
2845         pos = buf;
2846         end = pos + buflen;
2847
2848         if (res < 0) {
2849                 if (strict)
2850                         return 0;
2851                 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
2852                 if (len >= buflen)
2853                         return -1;
2854                 return len;
2855         }
2856
2857         for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
2858                 if (capa->enc & ciphers[i].capa) {
2859                         ret = os_snprintf(pos, end - pos, "%s%s",
2860                                           first ? "" : " ", ciphers[i].name);
2861                         if (ret < 0 || ret >= end - pos)
2862                                 return pos - buf;
2863                         pos += ret;
2864                         first = 0;
2865                 }
2866         }
2867
2868         return pos - buf;
2869 }
2870
2871
2872 static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
2873                                               struct wpa_driver_capa *capa,
2874                                               char *buf, size_t buflen)
2875 {
2876         int ret;
2877         char *pos, *end;
2878         size_t len;
2879
2880         pos = buf;
2881         end = pos + buflen;
2882
2883         if (res < 0) {
2884                 if (strict)
2885                         return 0;
2886                 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
2887                                  "NONE", buflen);
2888                 if (len >= buflen)
2889                         return -1;
2890                 return len;
2891         }
2892
2893         ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
2894         if (ret < 0 || ret >= end - pos)
2895                 return pos - buf;
2896         pos += ret;
2897
2898         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2899                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
2900                 ret = os_snprintf(pos, end - pos, " WPA-EAP");
2901                 if (ret < 0 || ret >= end - pos)
2902                         return pos - buf;
2903                 pos += ret;
2904         }
2905
2906         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2907                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2908                 ret = os_snprintf(pos, end - pos, " WPA-PSK");
2909                 if (ret < 0 || ret >= end - pos)
2910                         return pos - buf;
2911                 pos += ret;
2912         }
2913
2914         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2915                 ret = os_snprintf(pos, end - pos, " WPA-NONE");
2916                 if (ret < 0 || ret >= end - pos)
2917                         return pos - buf;
2918                 pos += ret;
2919         }
2920
2921         return pos - buf;
2922 }
2923
2924
2925 static int ctrl_iface_get_capability_proto(int res, char *strict,
2926                                            struct wpa_driver_capa *capa,
2927                                            char *buf, size_t buflen)
2928 {
2929         int ret, first = 1;
2930         char *pos, *end;
2931         size_t len;
2932
2933         pos = buf;
2934         end = pos + buflen;
2935
2936         if (res < 0) {
2937                 if (strict)
2938                         return 0;
2939                 len = os_strlcpy(buf, "RSN WPA", buflen);
2940                 if (len >= buflen)
2941                         return -1;
2942                 return len;
2943         }
2944
2945         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2946                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2947                 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
2948                 if (ret < 0 || ret >= end - pos)
2949                         return pos - buf;
2950                 pos += ret;
2951                 first = 0;
2952         }
2953
2954         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2955                               WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
2956                 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
2957                 if (ret < 0 || ret >= end - pos)
2958                         return pos - buf;
2959                 pos += ret;
2960                 first = 0;
2961         }
2962
2963         return pos - buf;
2964 }
2965
2966
2967 static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
2968                                               struct wpa_driver_capa *capa,
2969                                               char *buf, size_t buflen)
2970 {
2971         int ret, first = 1;
2972         char *pos, *end;
2973         size_t len;
2974
2975         pos = buf;
2976         end = pos + buflen;
2977
2978         if (res < 0) {
2979                 if (strict)
2980                         return 0;
2981                 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
2982                 if (len >= buflen)
2983                         return -1;
2984                 return len;
2985         }
2986
2987         if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
2988                 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
2989                 if (ret < 0 || ret >= end - pos)
2990                         return pos - buf;
2991                 pos += ret;
2992                 first = 0;
2993         }
2994
2995         if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
2996                 ret = os_snprintf(pos, end - pos, "%sSHARED",
2997                                   first ? "" : " ");
2998                 if (ret < 0 || ret >= end - pos)
2999                         return pos - buf;
3000                 pos += ret;
3001                 first = 0;
3002         }
3003
3004         if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
3005                 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
3006                 if (ret < 0 || ret >= end - pos)
3007                         return pos - buf;
3008                 pos += ret;
3009                 first = 0;
3010         }
3011
3012         return pos - buf;
3013 }
3014
3015
3016 static int ctrl_iface_get_capability_modes(int res, char *strict,
3017                                            struct wpa_driver_capa *capa,
3018                                            char *buf, size_t buflen)
3019 {
3020         int ret, first = 1;
3021         char *pos, *end;
3022         size_t len;
3023
3024         pos = buf;
3025         end = pos + buflen;
3026
3027         if (res < 0) {
3028                 if (strict)
3029                         return 0;
3030                 len = os_strlcpy(buf, "IBSS AP", buflen);
3031                 if (len >= buflen)
3032                         return -1;
3033                 return len;
3034         }
3035
3036         if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
3037                 ret = os_snprintf(pos, end - pos, "%sIBSS", first ? "" : " ");
3038                 if (ret < 0 || ret >= end - pos)
3039                         return pos - buf;
3040                 pos += ret;
3041                 first = 0;
3042         }
3043
3044         if (capa->flags & WPA_DRIVER_FLAGS_AP) {
3045                 ret = os_snprintf(pos, end - pos, "%sAP", first ? "" : " ");
3046                 if (ret < 0 || ret >= end - pos)
3047                         return pos - buf;
3048                 pos += ret;
3049                 first = 0;
3050         }
3051
3052         return pos - buf;
3053 }
3054
3055
3056 static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
3057                                               char *buf, size_t buflen)
3058 {
3059         struct hostapd_channel_data *chnl;
3060         int ret, i, j;
3061         char *pos, *end, *hmode;
3062
3063         pos = buf;
3064         end = pos + buflen;
3065
3066         for (j = 0; j < wpa_s->hw.num_modes; j++) {
3067                 switch (wpa_s->hw.modes[j].mode) {
3068                 case HOSTAPD_MODE_IEEE80211B:
3069                         hmode = "B";
3070                         break;
3071                 case HOSTAPD_MODE_IEEE80211G:
3072                         hmode = "G";
3073                         break;
3074                 case HOSTAPD_MODE_IEEE80211A:
3075                         hmode = "A";
3076                         break;
3077                 case HOSTAPD_MODE_IEEE80211AD:
3078                         hmode = "AD";
3079                         break;
3080                 default:
3081                         continue;
3082                 }
3083                 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
3084                 if (ret < 0 || ret >= end - pos)
3085                         return pos - buf;
3086                 pos += ret;
3087                 chnl = wpa_s->hw.modes[j].channels;
3088                 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3089                         if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3090                                 continue;
3091                         ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
3092                         if (ret < 0 || ret >= end - pos)
3093                                 return pos - buf;
3094                         pos += ret;
3095                 }
3096                 ret = os_snprintf(pos, end - pos, "\n");
3097                 if (ret < 0 || ret >= end - pos)
3098                         return pos - buf;
3099                 pos += ret;
3100         }
3101
3102         return pos - buf;
3103 }
3104
3105
3106 static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3107                                           char *buf, size_t buflen)
3108 {
3109         struct hostapd_channel_data *chnl;
3110         int ret, i, j;
3111         char *pos, *end, *hmode;
3112
3113         pos = buf;
3114         end = pos + buflen;
3115
3116         for (j = 0; j < wpa_s->hw.num_modes; j++) {
3117                 switch (wpa_s->hw.modes[j].mode) {
3118                 case HOSTAPD_MODE_IEEE80211B:
3119                         hmode = "B";
3120                         break;
3121                 case HOSTAPD_MODE_IEEE80211G:
3122                         hmode = "G";
3123                         break;
3124                 case HOSTAPD_MODE_IEEE80211A:
3125                         hmode = "A";
3126                         break;
3127                 case HOSTAPD_MODE_IEEE80211AD:
3128                         hmode = "AD";
3129                         break;
3130                 default:
3131                         continue;
3132                 }
3133                 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3134                                   hmode);
3135                 if (ret < 0 || ret >= end - pos)
3136                         return pos - buf;
3137                 pos += ret;
3138                 chnl = wpa_s->hw.modes[j].channels;
3139                 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3140                         if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3141                                 continue;
3142                         ret = os_snprintf(pos, end - pos, " %d = %d MHz%s\n",
3143                                           chnl[i].chan, chnl[i].freq,
3144                                           chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
3145                                           " (NO_IBSS)" : "");
3146                         if (ret < 0 || ret >= end - pos)
3147                                 return pos - buf;
3148                         pos += ret;
3149                 }
3150                 ret = os_snprintf(pos, end - pos, "\n");
3151                 if (ret < 0 || ret >= end - pos)
3152                         return pos - buf;
3153                 pos += ret;
3154         }
3155
3156         return pos - buf;
3157 }
3158
3159
3160 static int wpa_supplicant_ctrl_iface_get_capability(
3161         struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3162         size_t buflen)
3163 {
3164         struct wpa_driver_capa capa;
3165         int res;
3166         char *strict;
3167         char field[30];
3168         size_t len;
3169
3170         /* Determine whether or not strict checking was requested */
3171         len = os_strlcpy(field, _field, sizeof(field));
3172         if (len >= sizeof(field))
3173                 return -1;
3174         strict = os_strchr(field, ' ');
3175         if (strict != NULL) {
3176                 *strict++ = '\0';
3177                 if (os_strcmp(strict, "strict") != 0)
3178                         return -1;
3179         }
3180
3181         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3182                 field, strict ? strict : "");
3183
3184         if (os_strcmp(field, "eap") == 0) {
3185                 return eap_get_names(buf, buflen);
3186         }
3187
3188         res = wpa_drv_get_capa(wpa_s, &capa);
3189
3190         if (os_strcmp(field, "pairwise") == 0)
3191                 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3192                                                           buf, buflen);
3193
3194         if (os_strcmp(field, "group") == 0)
3195                 return ctrl_iface_get_capability_group(res, strict, &capa,
3196                                                        buf, buflen);
3197
3198         if (os_strcmp(field, "key_mgmt") == 0)
3199                 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3200                                                           buf, buflen);
3201
3202         if (os_strcmp(field, "proto") == 0)
3203                 return ctrl_iface_get_capability_proto(res, strict, &capa,
3204                                                        buf, buflen);
3205
3206         if (os_strcmp(field, "auth_alg") == 0)
3207                 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
3208                                                           buf, buflen);
3209
3210         if (os_strcmp(field, "modes") == 0)
3211                 return ctrl_iface_get_capability_modes(res, strict, &capa,
3212                                                        buf, buflen);
3213
3214         if (os_strcmp(field, "channels") == 0)
3215                 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3216
3217         if (os_strcmp(field, "freq") == 0)
3218                 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3219
3220         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3221                    field);
3222
3223         return -1;
3224 }
3225
3226
3227 #ifdef CONFIG_INTERWORKING
3228 static char * anqp_add_hex(char *pos, char *end, const char *title,
3229                            struct wpabuf *data)
3230 {
3231         char *start = pos;
3232         size_t i;
3233         int ret;
3234         const u8 *d;
3235
3236         if (data == NULL)
3237                 return start;
3238
3239         ret = os_snprintf(pos, end - pos, "%s=", title);
3240         if (ret < 0 || ret >= end - pos)
3241                 return start;
3242         pos += ret;
3243
3244         d = wpabuf_head_u8(data);
3245         for (i = 0; i < wpabuf_len(data); i++) {
3246                 ret = os_snprintf(pos, end - pos, "%02x", *d++);
3247                 if (ret < 0 || ret >= end - pos)
3248                         return start;
3249                 pos += ret;
3250         }
3251
3252         ret = os_snprintf(pos, end - pos, "\n");
3253         if (ret < 0 || ret >= end - pos)
3254                 return start;
3255         pos += ret;
3256
3257         return pos;
3258 }
3259 #endif /* CONFIG_INTERWORKING */
3260
3261
3262 static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3263                           unsigned long mask, char *buf, size_t buflen)
3264 {
3265         size_t i;
3266         int ret;
3267         char *pos, *end;
3268         const u8 *ie, *ie2;
3269
3270         pos = buf;
3271         end = buf + buflen;
3272
3273         if (mask & WPA_BSS_MASK_ID) {
3274                 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
3275                 if (ret < 0 || ret >= end - pos)
3276                         return 0;
3277                 pos += ret;
3278         }
3279
3280         if (mask & WPA_BSS_MASK_BSSID) {
3281                 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3282                                   MAC2STR(bss->bssid));
3283                 if (ret < 0 || ret >= end - pos)
3284                         return 0;
3285                 pos += ret;
3286         }
3287
3288         if (mask & WPA_BSS_MASK_FREQ) {
3289                 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
3290                 if (ret < 0 || ret >= end - pos)
3291                         return 0;
3292                 pos += ret;
3293         }
3294
3295         if (mask & WPA_BSS_MASK_BEACON_INT) {
3296                 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3297                                   bss->beacon_int);
3298                 if (ret < 0 || ret >= end - pos)
3299                         return 0;
3300                 pos += ret;
3301         }
3302
3303         if (mask & WPA_BSS_MASK_CAPABILITIES) {
3304                 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3305                                   bss->caps);
3306                 if (ret < 0 || ret >= end - pos)
3307                         return 0;
3308                 pos += ret;
3309         }
3310
3311         if (mask & WPA_BSS_MASK_QUAL) {
3312                 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
3313                 if (ret < 0 || ret >= end - pos)
3314                         return 0;
3315                 pos += ret;
3316         }
3317
3318         if (mask & WPA_BSS_MASK_NOISE) {
3319                 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
3320                 if (ret < 0 || ret >= end - pos)
3321                         return 0;
3322                 pos += ret;
3323         }
3324
3325         if (mask & WPA_BSS_MASK_LEVEL) {
3326                 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
3327                 if (ret < 0 || ret >= end - pos)
3328                         return 0;
3329                 pos += ret;
3330         }
3331
3332         if (mask & WPA_BSS_MASK_TSF) {
3333                 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3334                                   (unsigned long long) bss->tsf);
3335                 if (ret < 0 || ret >= end - pos)
3336                         return 0;
3337                 pos += ret;
3338         }
3339
3340         if (mask & WPA_BSS_MASK_AGE) {
3341                 struct os_reltime now;
3342
3343                 os_get_reltime(&now);
3344                 ret = os_snprintf(pos, end - pos, "age=%d\n",
3345                                   (int) (now.sec - bss->last_update.sec));
3346                 if (ret < 0 || ret >= end - pos)
3347                         return 0;
3348                 pos += ret;
3349         }
3350
3351         if (mask & WPA_BSS_MASK_IE) {
3352                 ret = os_snprintf(pos, end - pos, "ie=");
3353                 if (ret < 0 || ret >= end - pos)
3354                         return 0;
3355                 pos += ret;
3356
3357                 ie = (const u8 *) (bss + 1);
3358                 for (i = 0; i < bss->ie_len; i++) {
3359                         ret = os_snprintf(pos, end - pos, "%02x", *ie++);
3360                         if (ret < 0 || ret >= end - pos)
3361                                 return 0;
3362                         pos += ret;
3363                 }
3364
3365                 ret = os_snprintf(pos, end - pos, "\n");
3366                 if (ret < 0 || ret >= end - pos)
3367                         return 0;
3368                 pos += ret;
3369         }
3370
3371         if (mask & WPA_BSS_MASK_FLAGS) {
3372                 ret = os_snprintf(pos, end - pos, "flags=");
3373                 if (ret < 0 || ret >= end - pos)
3374                         return 0;
3375                 pos += ret;
3376
3377                 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3378                 if (ie)
3379                         pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3380                                                     2 + ie[1]);
3381                 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3382                 if (ie2)
3383                         pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3384                                                     2 + ie2[1]);
3385                 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3386                 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3387                         ret = os_snprintf(pos, end - pos, "[WEP]");
3388                         if (ret < 0 || ret >= end - pos)
3389                                 return 0;
3390                         pos += ret;
3391                 }
3392                 if (bss->caps & IEEE80211_CAP_IBSS) {
3393                         ret = os_snprintf(pos, end - pos, "[IBSS]");
3394                         if (ret < 0 || ret >= end - pos)
3395                                 return 0;
3396                         pos += ret;
3397                 }
3398                 if (bss->caps & IEEE80211_CAP_ESS) {
3399                         ret = os_snprintf(pos, end - pos, "[ESS]");
3400                         if (ret < 0 || ret >= end - pos)
3401                                 return 0;
3402                         pos += ret;
3403                 }
3404                 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
3405                     wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
3406                         ret = os_snprintf(pos, end - pos, "[P2P]");
3407                         if (ret < 0 || ret >= end - pos)
3408                                 return 0;
3409                         pos += ret;
3410                 }
3411 #ifdef CONFIG_HS20
3412                 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3413                         ret = os_snprintf(pos, end - pos, "[HS20]");
3414                         if (ret < 0 || ret >= end - pos)
3415                                 return 0;
3416                         pos += ret;
3417                 }
3418 #endif /* CONFIG_HS20 */
3419
3420                 ret = os_snprintf(pos, end - pos, "\n");
3421                 if (ret < 0 || ret >= end - pos)
3422                         return 0;
3423                 pos += ret;
3424         }
3425
3426         if (mask & WPA_BSS_MASK_SSID) {
3427                 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3428                                   wpa_ssid_txt(bss->ssid, bss->ssid_len));
3429                 if (ret < 0 || ret >= end - pos)
3430                         return 0;
3431                 pos += ret;
3432         }
3433
3434 #ifdef CONFIG_WPS
3435         if (mask & WPA_BSS_MASK_WPS_SCAN) {
3436                 ie = (const u8 *) (bss + 1);
3437                 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3438                 if (ret < 0 || ret >= end - pos)
3439                         return 0;
3440                 pos += ret;
3441         }
3442 #endif /* CONFIG_WPS */
3443
3444 #ifdef CONFIG_P2P
3445         if (mask & WPA_BSS_MASK_P2P_SCAN) {
3446                 ie = (const u8 *) (bss + 1);
3447                 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3448                 if (ret < 0 || ret >= end - pos)
3449                         return 0;
3450                 pos += ret;
3451         }
3452 #endif /* CONFIG_P2P */
3453
3454 #ifdef CONFIG_WIFI_DISPLAY
3455         if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3456                 struct wpabuf *wfd;
3457                 ie = (const u8 *) (bss + 1);
3458                 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3459                                                   WFD_IE_VENDOR_TYPE);
3460                 if (wfd) {
3461                         ret = os_snprintf(pos, end - pos, "wfd_subelems=");
3462                         if (ret < 0 || ret >= end - pos)
3463                                 return 0;
3464                         pos += ret;
3465
3466                         pos += wpa_snprintf_hex(pos, end - pos,
3467                                                 wpabuf_head(wfd),
3468                                                 wpabuf_len(wfd));
3469                         wpabuf_free(wfd);
3470
3471                         ret = os_snprintf(pos, end - pos, "\n");
3472                         if (ret < 0 || ret >= end - pos)
3473                                 return 0;
3474                         pos += ret;
3475                 }
3476         }
3477 #endif /* CONFIG_WIFI_DISPLAY */
3478
3479 #ifdef CONFIG_INTERWORKING
3480         if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3481                 struct wpa_bss_anqp *anqp = bss->anqp;
3482                 pos = anqp_add_hex(pos, end, "anqp_venue_name",
3483                                    anqp->venue_name);
3484                 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
3485                                    anqp->network_auth_type);
3486                 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
3487                                    anqp->roaming_consortium);
3488                 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
3489                                    anqp->ip_addr_type_availability);
3490                 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
3491                                    anqp->nai_realm);
3492                 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
3493                 pos = anqp_add_hex(pos, end, "anqp_domain_name",
3494                                    anqp->domain_name);
3495 #ifdef CONFIG_HS20
3496                 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
3497                                    anqp->hs20_operator_friendly_name);
3498                 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
3499                                    anqp->hs20_wan_metrics);
3500                 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
3501                                    anqp->hs20_connection_capability);
3502 #endif /* CONFIG_HS20 */
3503         }
3504 #endif /* CONFIG_INTERWORKING */
3505
3506         if (mask & WPA_BSS_MASK_DELIM) {
3507                 ret = os_snprintf(pos, end - pos, "====\n");
3508                 if (ret < 0 || ret >= end - pos)
3509                         return 0;
3510                 pos += ret;
3511         }
3512
3513         return pos - buf;
3514 }
3515
3516
3517 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
3518                                          const char *cmd, char *buf,
3519                                          size_t buflen)
3520 {
3521         u8 bssid[ETH_ALEN];
3522         size_t i;
3523         struct wpa_bss *bss;
3524         struct wpa_bss *bsslast = NULL;
3525         struct dl_list *next;
3526         int ret = 0;
3527         int len;
3528         char *ctmp;
3529         unsigned long mask = WPA_BSS_MASK_ALL;
3530
3531         if (os_strncmp(cmd, "RANGE=", 6) == 0) {
3532                 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
3533                         bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
3534                                             list_id);
3535                         bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
3536                                                list_id);
3537                 } else { /* N1-N2 */
3538                         unsigned int id1, id2;
3539
3540                         if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
3541                                 wpa_printf(MSG_INFO, "Wrong BSS range "
3542                                            "format");
3543                                 return 0;
3544                         }
3545
3546                         if (*(cmd + 6) == '-')
3547                                 id1 = 0;
3548                         else
3549                                 id1 = atoi(cmd + 6);
3550                         ctmp++;
3551                         if (*ctmp >= '0' && *ctmp <= '9')
3552                                 id2 = atoi(ctmp);
3553                         else
3554                                 id2 = (unsigned int) -1;
3555                         bss = wpa_bss_get_id_range(wpa_s, id1, id2);
3556                         if (id2 == (unsigned int) -1)
3557                                 bsslast = dl_list_last(&wpa_s->bss_id,
3558                                                        struct wpa_bss,
3559                                                        list_id);
3560                         else {
3561                                 bsslast = wpa_bss_get_id(wpa_s, id2);
3562                                 if (bsslast == NULL && bss && id2 > id1) {
3563                                         struct wpa_bss *tmp = bss;
3564                                         for (;;) {
3565                                                 next = tmp->list_id.next;
3566                                                 if (next == &wpa_s->bss_id)
3567                                                         break;
3568                                                 tmp = dl_list_entry(
3569                                                         next, struct wpa_bss,
3570                                                         list_id);
3571                                                 if (tmp->id > id2)
3572                                                         break;
3573                                                 bsslast = tmp;
3574                                         }
3575                                 }
3576                         }
3577                 }
3578         } else if (os_strncmp(cmd, "FIRST", 5) == 0)
3579                 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
3580         else if (os_strncmp(cmd, "LAST", 4) == 0)
3581                 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
3582         else if (os_strncmp(cmd, "ID-", 3) == 0) {
3583                 i = atoi(cmd + 3);
3584                 bss = wpa_bss_get_id(wpa_s, i);
3585         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3586                 i = atoi(cmd + 5);
3587                 bss = wpa_bss_get_id(wpa_s, i);
3588                 if (bss) {
3589                         next = bss->list_id.next;
3590                         if (next == &wpa_s->bss_id)
3591                                 bss = NULL;
3592                         else
3593                                 bss = dl_list_entry(next, struct wpa_bss,
3594                                                     list_id);
3595                 }
3596 #ifdef CONFIG_P2P
3597         } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
3598                 if (hwaddr_aton(cmd + 13, bssid) == 0)
3599                         bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
3600                 else
3601                         bss = NULL;
3602 #endif /* CONFIG_P2P */
3603         } else if (hwaddr_aton(cmd, bssid) == 0)
3604                 bss = wpa_bss_get_bssid(wpa_s, bssid);
3605         else {
3606                 struct wpa_bss *tmp;
3607                 i = atoi(cmd);
3608                 bss = NULL;
3609                 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
3610                 {
3611                         if (i-- == 0) {
3612                                 bss = tmp;
3613                                 break;
3614                         }
3615                 }
3616         }
3617
3618         if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
3619                 mask = strtoul(ctmp + 5, NULL, 0x10);
3620                 if (mask == 0)
3621                         mask = WPA_BSS_MASK_ALL;
3622         }
3623
3624         if (bss == NULL)
3625                 return 0;
3626
3627         if (bsslast == NULL)
3628                 bsslast = bss;
3629         do {
3630                 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
3631                 ret += len;
3632                 buf += len;
3633                 buflen -= len;
3634                 if (bss == bsslast) {
3635                         if ((mask & WPA_BSS_MASK_DELIM) && len &&
3636                             (bss == dl_list_last(&wpa_s->bss_id,
3637                                                  struct wpa_bss, list_id)))
3638                                 os_snprintf(buf - 5, 5, "####\n");
3639                         break;
3640                 }
3641                 next = bss->list_id.next;
3642                 if (next == &wpa_s->bss_id)
3643                         break;
3644                 bss = dl_list_entry(next, struct wpa_bss, list_id);
3645         } while (bss && len);
3646
3647         return ret;
3648 }
3649
3650
3651 static int wpa_supplicant_ctrl_iface_ap_scan(
3652         struct wpa_supplicant *wpa_s, char *cmd)
3653 {
3654         int ap_scan = atoi(cmd);
3655         return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
3656 }
3657
3658
3659 static int wpa_supplicant_ctrl_iface_scan_interval(
3660         struct wpa_supplicant *wpa_s, char *cmd)
3661 {
3662         int scan_int = atoi(cmd);
3663         return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
3664 }
3665
3666
3667 static int wpa_supplicant_ctrl_iface_bss_expire_age(
3668         struct wpa_supplicant *wpa_s, char *cmd)
3669 {
3670         int expire_age = atoi(cmd);
3671         return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
3672 }
3673
3674
3675 static int wpa_supplicant_ctrl_iface_bss_expire_count(
3676         struct wpa_supplicant *wpa_s, char *cmd)
3677 {
3678         int expire_count = atoi(cmd);
3679         return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
3680 }
3681
3682
3683 static int wpa_supplicant_ctrl_iface_bss_flush(
3684         struct wpa_supplicant *wpa_s, char *cmd)
3685 {
3686         int flush_age = atoi(cmd);
3687
3688         if (flush_age == 0)
3689                 wpa_bss_flush(wpa_s);
3690         else
3691                 wpa_bss_flush_by_age(wpa_s, flush_age);
3692         return 0;
3693 }
3694
3695
3696 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
3697 {
3698         wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
3699         /* MLME-DELETEKEYS.request */
3700         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
3701         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
3702         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
3703         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
3704 #ifdef CONFIG_IEEE80211W
3705         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
3706         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
3707 #endif /* CONFIG_IEEE80211W */
3708
3709         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
3710                         0);
3711         /* MLME-SETPROTECTION.request(None) */
3712         wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
3713                                    MLME_SETPROTECTION_PROTECT_TYPE_NONE,
3714                                    MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3715         wpa_sm_drop_sa(wpa_s->wpa);
3716 }
3717
3718
3719 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
3720                                           char *addr)
3721 {
3722 #ifdef CONFIG_NO_SCAN_PROCESSING
3723         return -1;
3724 #else /* CONFIG_NO_SCAN_PROCESSING */
3725         u8 bssid[ETH_ALEN];
3726         struct wpa_bss *bss;
3727         struct wpa_ssid *ssid = wpa_s->current_ssid;
3728
3729         if (hwaddr_aton(addr, bssid)) {
3730                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
3731                            "address '%s'", addr);
3732                 return -1;
3733         }
3734
3735         wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
3736
3737         if (!ssid) {
3738                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
3739                            "configuration known for the target AP");
3740                 return -1;
3741         }
3742
3743         bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
3744         if (!bss) {
3745                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
3746                            "from BSS table");
3747                 return -1;
3748         }
3749
3750         /*
3751          * TODO: Find best network configuration block from configuration to
3752          * allow roaming to other networks
3753          */
3754
3755         wpa_s->reassociate = 1;
3756         wpa_supplicant_connect(wpa_s, bss, ssid);
3757
3758         return 0;
3759 #endif /* CONFIG_NO_SCAN_PROCESSING */
3760 }
3761
3762
3763 #ifdef CONFIG_P2P
3764 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
3765 {
3766         unsigned int timeout = atoi(cmd);
3767         enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
3768         u8 dev_id[ETH_ALEN], *_dev_id = NULL;
3769         u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
3770         char *pos;
3771         unsigned int search_delay;
3772
3773         if (os_strstr(cmd, "type=social"))
3774                 type = P2P_FIND_ONLY_SOCIAL;
3775         else if (os_strstr(cmd, "type=progressive"))
3776                 type = P2P_FIND_PROGRESSIVE;
3777
3778         pos = os_strstr(cmd, "dev_id=");
3779         if (pos) {
3780                 pos += 7;
3781                 if (hwaddr_aton(pos, dev_id))
3782                         return -1;
3783                 _dev_id = dev_id;
3784         }
3785
3786         pos = os_strstr(cmd, "dev_type=");
3787         if (pos) {
3788                 pos += 9;
3789                 if (wps_dev_type_str2bin(pos, dev_type) < 0)
3790                         return -1;
3791                 _dev_type = dev_type;
3792         }
3793
3794         pos = os_strstr(cmd, "delay=");
3795         if (pos) {
3796                 pos += 6;
3797                 search_delay = atoi(pos);
3798         } else
3799                 search_delay = wpas_p2p_search_delay(wpa_s);
3800
3801         return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
3802                              _dev_id, search_delay);
3803 }
3804
3805
3806 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
3807                             char *buf, size_t buflen)
3808 {
3809         u8 addr[ETH_ALEN];
3810         char *pos, *pos2;
3811         char *pin = NULL;
3812         enum p2p_wps_method wps_method;
3813         int new_pin;
3814         int ret;
3815         int persistent_group, persistent_id = -1;
3816         int join;
3817         int auth;
3818         int automatic;
3819         int go_intent = -1;
3820         int freq = 0;
3821         int pd;
3822         int ht40, vht;
3823
3824         /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
3825          * [persistent|persistent=<network id>]
3826          * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
3827          * [ht40] [vht] */
3828
3829         if (hwaddr_aton(cmd, addr))
3830                 return -1;
3831
3832         pos = cmd + 17;
3833         if (*pos != ' ')
3834                 return -1;
3835         pos++;
3836
3837         persistent_group = os_strstr(pos, " persistent") != NULL;
3838         pos2 = os_strstr(pos, " persistent=");
3839         if (pos2) {
3840                 struct wpa_ssid *ssid;
3841                 persistent_id = atoi(pos2 + 12);
3842                 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3843                 if (ssid == NULL || ssid->disabled != 2 ||
3844                     ssid->mode != WPAS_MODE_P2P_GO) {
3845                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3846                                    "SSID id=%d for persistent P2P group (GO)",
3847                                    persistent_id);
3848                         return -1;
3849                 }
3850         }
3851         join = os_strstr(pos, " join") != NULL;
3852         auth = os_strstr(pos, " auth") != NULL;
3853         automatic = os_strstr(pos, " auto") != NULL;
3854         pd = os_strstr(pos, " provdisc") != NULL;
3855         vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
3856         ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
3857                 vht;
3858
3859         pos2 = os_strstr(pos, " go_intent=");
3860         if (pos2) {
3861                 pos2 += 11;
3862                 go_intent = atoi(pos2);
3863                 if (go_intent < 0 || go_intent > 15)
3864                         return -1;
3865         }
3866
3867         pos2 = os_strstr(pos, " freq=");
3868         if (pos2) {
3869                 pos2 += 6;
3870                 freq = atoi(pos2);
3871                 if (freq <= 0)
3872                         return -1;
3873         }
3874
3875         if (os_strncmp(pos, "pin", 3) == 0) {
3876                 /* Request random PIN (to be displayed) and enable the PIN */
3877                 wps_method = WPS_PIN_DISPLAY;
3878         } else if (os_strncmp(pos, "pbc", 3) == 0) {
3879                 wps_method = WPS_PBC;
3880         } else {
3881                 pin = pos;
3882                 pos = os_strchr(pin, ' ');
3883                 wps_method = WPS_PIN_KEYPAD;
3884                 if (pos) {
3885                         *pos++ = '\0';
3886                         if (os_strncmp(pos, "display", 7) == 0)
3887                                 wps_method = WPS_PIN_DISPLAY;
3888                 }
3889                 if (!wps_pin_str_valid(pin)) {
3890                         os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
3891                         return 17;
3892                 }
3893         }
3894
3895         new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
3896                                    persistent_group, automatic, join,
3897                                    auth, go_intent, freq, persistent_id, pd,
3898                                    ht40, vht);
3899         if (new_pin == -2) {
3900                 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
3901                 return 25;
3902         }
3903         if (new_pin == -3) {
3904                 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
3905                 return 25;
3906         }
3907         if (new_pin < 0)
3908                 return -1;
3909         if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
3910                 ret = os_snprintf(buf, buflen, "%08d", new_pin);
3911                 if (ret < 0 || (size_t) ret >= buflen)
3912                         return -1;
3913                 return ret;
3914         }
3915
3916         os_memcpy(buf, "OK\n", 3);
3917         return 3;
3918 }
3919
3920
3921 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
3922 {
3923         unsigned int timeout = atoi(cmd);
3924         return wpas_p2p_listen(wpa_s, timeout);
3925 }
3926
3927
3928 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
3929 {
3930         u8 addr[ETH_ALEN];
3931         char *pos;
3932         enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
3933
3934         /* <addr> <config method> [join|auto] */
3935
3936         if (hwaddr_aton(cmd, addr))
3937                 return -1;
3938
3939         pos = cmd + 17;
3940         if (*pos != ' ')
3941                 return -1;
3942         pos++;
3943
3944         if (os_strstr(pos, " join") != NULL)
3945                 use = WPAS_P2P_PD_FOR_JOIN;
3946         else if (os_strstr(pos, " auto") != NULL)
3947                 use = WPAS_P2P_PD_AUTO;
3948
3949         return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
3950 }
3951
3952
3953 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
3954                               size_t buflen)
3955 {
3956         struct wpa_ssid *ssid = wpa_s->current_ssid;
3957
3958         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3959             ssid->passphrase == NULL)
3960                 return -1;
3961
3962         os_strlcpy(buf, ssid->passphrase, buflen);
3963         return os_strlen(buf);
3964 }
3965
3966
3967 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
3968                                   char *buf, size_t buflen)
3969 {
3970         u64 ref;
3971         int res;
3972         u8 dst_buf[ETH_ALEN], *dst;
3973         struct wpabuf *tlvs;
3974         char *pos;
3975         size_t len;
3976
3977         if (hwaddr_aton(cmd, dst_buf))
3978                 return -1;
3979         dst = dst_buf;
3980         if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
3981             dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
3982                 dst = NULL;
3983         pos = cmd + 17;
3984         if (*pos != ' ')
3985                 return -1;
3986         pos++;
3987
3988         if (os_strncmp(pos, "upnp ", 5) == 0) {
3989                 u8 version;
3990                 pos += 5;
3991                 if (hexstr2bin(pos, &version, 1) < 0)
3992                         return -1;
3993                 pos += 2;
3994                 if (*pos != ' ')
3995                         return -1;
3996                 pos++;
3997                 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
3998 #ifdef CONFIG_WIFI_DISPLAY
3999         } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
4000                 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
4001 #endif /* CONFIG_WIFI_DISPLAY */
4002         } else {
4003                 len = os_strlen(pos);
4004                 if (len & 1)
4005                         return -1;
4006                 len /= 2;
4007                 tlvs = wpabuf_alloc(len);
4008                 if (tlvs == NULL)
4009                         return -1;
4010                 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
4011                         wpabuf_free(tlvs);
4012                         return -1;
4013                 }
4014
4015                 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
4016                 wpabuf_free(tlvs);
4017         }
4018         if (ref == 0)
4019                 return -1;
4020         res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
4021         if (res < 0 || (unsigned) res >= buflen)
4022                 return -1;
4023         return res;
4024 }
4025
4026
4027 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
4028                                          char *cmd)
4029 {
4030         long long unsigned val;
4031         u64 req;
4032         if (sscanf(cmd, "%llx", &val) != 1)
4033                 return -1;
4034         req = val;
4035         return wpas_p2p_sd_cancel_request(wpa_s, req);
4036 }
4037
4038
4039 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
4040 {
4041         int freq;
4042         u8 dst[ETH_ALEN];
4043         u8 dialog_token;
4044         struct wpabuf *resp_tlvs;
4045         char *pos, *pos2;
4046         size_t len;
4047
4048         pos = os_strchr(cmd, ' ');
4049         if (pos == NULL)
4050                 return -1;
4051         *pos++ = '\0';
4052         freq = atoi(cmd);
4053         if (freq == 0)
4054                 return -1;
4055
4056         if (hwaddr_aton(pos, dst))
4057                 return -1;
4058         pos += 17;
4059         if (*pos != ' ')
4060                 return -1;
4061         pos++;
4062
4063         pos2 = os_strchr(pos, ' ');
4064         if (pos2 == NULL)
4065                 return -1;
4066         *pos2++ = '\0';
4067         dialog_token = atoi(pos);
4068
4069         len = os_strlen(pos2);
4070         if (len & 1)
4071                 return -1;
4072         len /= 2;
4073         resp_tlvs = wpabuf_alloc(len);
4074         if (resp_tlvs == NULL)
4075                 return -1;
4076         if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
4077                 wpabuf_free(resp_tlvs);
4078                 return -1;
4079         }
4080
4081         wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
4082         wpabuf_free(resp_tlvs);
4083         return 0;
4084 }
4085
4086
4087 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
4088                                        char *cmd)
4089 {
4090         if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
4091                 return -1;
4092         wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
4093         return 0;
4094 }
4095
4096
4097 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
4098                                         char *cmd)
4099 {
4100         char *pos;
4101         size_t len;
4102         struct wpabuf *query, *resp;
4103
4104         pos = os_strchr(cmd, ' ');
4105         if (pos == NULL)
4106                 return -1;
4107         *pos++ = '\0';
4108
4109         len = os_strlen(cmd);
4110         if (len & 1)
4111                 return -1;
4112         len /= 2;
4113         query = wpabuf_alloc(len);
4114         if (query == NULL)
4115                 return -1;
4116         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4117                 wpabuf_free(query);
4118                 return -1;
4119         }
4120
4121         len = os_strlen(pos);
4122         if (len & 1) {
4123                 wpabuf_free(query);
4124                 return -1;
4125         }
4126         len /= 2;
4127         resp = wpabuf_alloc(len);
4128         if (resp == NULL) {
4129                 wpabuf_free(query);
4130                 return -1;
4131         }
4132         if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4133                 wpabuf_free(query);
4134                 wpabuf_free(resp);
4135                 return -1;
4136         }
4137
4138         if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4139                 wpabuf_free(query);
4140                 wpabuf_free(resp);
4141                 return -1;
4142         }
4143         return 0;
4144 }
4145
4146
4147 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4148 {
4149         char *pos;
4150         u8 version;
4151
4152         pos = os_strchr(cmd, ' ');
4153         if (pos == NULL)
4154                 return -1;
4155         *pos++ = '\0';
4156
4157         if (hexstr2bin(cmd, &version, 1) < 0)
4158                 return -1;
4159
4160         return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4161 }
4162
4163
4164 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4165 {
4166         char *pos;
4167
4168         pos = os_strchr(cmd, ' ');
4169         if (pos == NULL)
4170                 return -1;
4171         *pos++ = '\0';
4172
4173         if (os_strcmp(cmd, "bonjour") == 0)
4174                 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4175         if (os_strcmp(cmd, "upnp") == 0)
4176                 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4177         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4178         return -1;
4179 }
4180
4181
4182 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4183                                         char *cmd)
4184 {
4185         size_t len;
4186         struct wpabuf *query;
4187         int ret;
4188
4189         len = os_strlen(cmd);
4190         if (len & 1)
4191                 return -1;
4192         len /= 2;
4193         query = wpabuf_alloc(len);
4194         if (query == NULL)
4195                 return -1;
4196         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4197                 wpabuf_free(query);
4198                 return -1;
4199         }
4200
4201         ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4202         wpabuf_free(query);
4203         return ret;
4204 }
4205
4206
4207 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4208 {
4209         char *pos;
4210         u8 version;
4211
4212         pos = os_strchr(cmd, ' ');
4213         if (pos == NULL)
4214                 return -1;
4215         *pos++ = '\0';
4216
4217         if (hexstr2bin(cmd, &version, 1) < 0)
4218                 return -1;
4219
4220         return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4221 }
4222
4223
4224 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4225 {
4226         char *pos;
4227
4228         pos = os_strchr(cmd, ' ');
4229         if (pos == NULL)
4230                 return -1;
4231         *pos++ = '\0';
4232
4233         if (os_strcmp(cmd, "bonjour") == 0)
4234                 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4235         if (os_strcmp(cmd, "upnp") == 0)
4236                 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4237         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4238         return -1;
4239 }
4240
4241
4242 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4243 {
4244         u8 addr[ETH_ALEN];
4245
4246         /* <addr> */
4247
4248         if (hwaddr_aton(cmd, addr))
4249                 return -1;
4250
4251         return wpas_p2p_reject(wpa_s, addr);
4252 }
4253
4254
4255 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4256 {
4257         char *pos;
4258         int id;
4259         struct wpa_ssid *ssid;
4260         u8 *_peer = NULL, peer[ETH_ALEN];
4261         int freq = 0, pref_freq = 0;
4262         int ht40, vht;
4263
4264         id = atoi(cmd);
4265         pos = os_strstr(cmd, " peer=");
4266         if (pos) {
4267                 pos += 6;
4268                 if (hwaddr_aton(pos, peer))
4269                         return -1;
4270                 _peer = peer;
4271         }
4272         ssid = wpa_config_get_network(wpa_s->conf, id);
4273         if (ssid == NULL || ssid->disabled != 2) {
4274                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4275                            "for persistent P2P group",
4276                            id);
4277                 return -1;
4278         }
4279
4280         pos = os_strstr(cmd, " freq=");
4281         if (pos) {
4282                 pos += 6;
4283                 freq = atoi(pos);
4284                 if (freq <= 0)
4285                         return -1;
4286         }
4287
4288         pos = os_strstr(cmd, " pref=");
4289         if (pos) {
4290                 pos += 6;
4291                 pref_freq = atoi(pos);
4292                 if (pref_freq <= 0)
4293                         return -1;
4294         }
4295
4296         vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4297         ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4298                 vht;
4299
4300         return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
4301                                pref_freq);
4302 }
4303
4304
4305 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
4306 {
4307         char *pos;
4308         u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
4309
4310         pos = os_strstr(cmd, " peer=");
4311         if (!pos)
4312                 return -1;
4313
4314         *pos = '\0';
4315         pos += 6;
4316         if (hwaddr_aton(pos, peer)) {
4317                 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
4318                 return -1;
4319         }
4320
4321         pos = os_strstr(pos, " go_dev_addr=");
4322         if (pos) {
4323                 pos += 13;
4324                 if (hwaddr_aton(pos, go_dev_addr)) {
4325                         wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
4326                                    pos);
4327                         return -1;
4328                 }
4329                 go_dev = go_dev_addr;
4330         }
4331
4332         return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
4333 }
4334
4335
4336 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
4337 {
4338         if (os_strncmp(cmd, "persistent=", 11) == 0)
4339                 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
4340         if (os_strncmp(cmd, "group=", 6) == 0)
4341                 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
4342
4343         return -1;
4344 }
4345
4346
4347 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
4348                                          char *cmd, int freq, int ht40,
4349                                          int vht)
4350 {
4351         int id;
4352         struct wpa_ssid *ssid;
4353
4354         id = atoi(cmd);
4355         ssid = wpa_config_get_network(wpa_s->conf, id);
4356         if (ssid == NULL || ssid->disabled != 2) {
4357                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4358                            "for persistent P2P group",
4359                            id);
4360                 return -1;
4361         }
4362
4363         return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40, vht,
4364                                              NULL, 0);
4365 }
4366
4367
4368 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
4369 {
4370         int freq = 0, ht40, vht;
4371         char *pos;
4372
4373         pos = os_strstr(cmd, "freq=");
4374         if (pos)
4375                 freq = atoi(pos + 5);
4376
4377         vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
4378         ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4379                 vht;
4380
4381         if (os_strncmp(cmd, "persistent=", 11) == 0)
4382                 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
4383                                                      ht40, vht);
4384         if (os_strcmp(cmd, "persistent") == 0 ||
4385             os_strncmp(cmd, "persistent ", 11) == 0)
4386                 return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
4387         if (os_strncmp(cmd, "freq=", 5) == 0)
4388                 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
4389         if (ht40)
4390                 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
4391
4392         wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
4393                    cmd);
4394         return -1;
4395 }
4396
4397
4398 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
4399                          char *buf, size_t buflen)
4400 {
4401         u8 addr[ETH_ALEN], *addr_ptr;
4402         int next, res;
4403         const struct p2p_peer_info *info;
4404         char *pos, *end;
4405         char devtype[WPS_DEV_TYPE_BUFSIZE];
4406         struct wpa_ssid *ssid;
4407         size_t i;
4408
4409         if (!wpa_s->global->p2p)
4410                 return -1;
4411
4412         if (os_strcmp(cmd, "FIRST") == 0) {
4413                 addr_ptr = NULL;
4414                 next = 0;
4415         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4416                 if (hwaddr_aton(cmd + 5, addr) < 0)
4417                         return -1;
4418                 addr_ptr = addr;
4419                 next = 1;
4420         } else {
4421                 if (hwaddr_aton(cmd, addr) < 0)
4422                         return -1;
4423                 addr_ptr = addr;
4424                 next = 0;
4425         }
4426
4427         info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4428         if (info == NULL)
4429                 return -1;
4430
4431         pos = buf;
4432         end = buf + buflen;
4433
4434         res = os_snprintf(pos, end - pos, MACSTR "\n"
4435                           "pri_dev_type=%s\n"
4436                           "device_name=%s\n"
4437                           "manufacturer=%s\n"
4438                           "model_name=%s\n"
4439                           "model_number=%s\n"
4440                           "serial_number=%s\n"
4441                           "config_methods=0x%x\n"
4442                           "dev_capab=0x%x\n"
4443                           "group_capab=0x%x\n"
4444                           "level=%d\n",
4445                           MAC2STR(info->p2p_device_addr),
4446                           wps_dev_type_bin2str(info->pri_dev_type,
4447                                                devtype, sizeof(devtype)),
4448                           info->device_name,
4449                           info->manufacturer,
4450                           info->model_name,
4451                           info->model_number,
4452                           info->serial_number,
4453                           info->config_methods,
4454                           info->dev_capab,
4455                           info->group_capab,
4456                           info->level);
4457         if (res < 0 || res >= end - pos)
4458                 return pos - buf;
4459         pos += res;
4460
4461         for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4462         {
4463                 const u8 *t;
4464                 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4465                 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4466                                   wps_dev_type_bin2str(t, devtype,
4467                                                        sizeof(devtype)));
4468                 if (res < 0 || res >= end - pos)
4469                         return pos - buf;
4470                 pos += res;
4471         }
4472
4473         ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
4474         if (ssid) {
4475                 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
4476                 if (res < 0 || res >= end - pos)
4477                         return pos - buf;
4478                 pos += res;
4479         }
4480
4481         res = p2p_get_peer_info_txt(info, pos, end - pos);
4482         if (res < 0)
4483                 return pos - buf;
4484         pos += res;
4485
4486         return pos - buf;
4487 }
4488
4489
4490 static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
4491                                   const char *param)
4492 {
4493         unsigned int i;
4494
4495         if (wpa_s->global->p2p == NULL)
4496                 return -1;
4497
4498         if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
4499                 return -1;
4500
4501         for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
4502                 struct wpa_freq_range *freq;
4503                 freq = &wpa_s->global->p2p_disallow_freq.range[i];
4504                 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
4505                            freq->min, freq->max);
4506         }
4507
4508         wpas_p2p_update_channel_list(wpa_s);
4509         return 0;
4510 }
4511
4512
4513 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
4514 {
4515         char *param;
4516
4517         if (wpa_s->global->p2p == NULL)
4518                 return -1;
4519
4520         param = os_strchr(cmd, ' ');
4521         if (param == NULL)
4522                 return -1;
4523         *param++ = '\0';
4524
4525         if (os_strcmp(cmd, "discoverability") == 0) {
4526                 p2p_set_client_discoverability(wpa_s->global->p2p,
4527                                                atoi(param));
4528                 return 0;
4529         }
4530
4531         if (os_strcmp(cmd, "managed") == 0) {
4532                 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
4533                 return 0;
4534         }
4535
4536         if (os_strcmp(cmd, "listen_channel") == 0) {
4537                 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
4538                                               atoi(param));
4539         }
4540
4541         if (os_strcmp(cmd, "ssid_postfix") == 0) {
4542                 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
4543                                             os_strlen(param));
4544         }
4545
4546         if (os_strcmp(cmd, "noa") == 0) {
4547                 char *pos;
4548                 int count, start, duration;
4549                 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
4550                 count = atoi(param);
4551                 pos = os_strchr(param, ',');
4552                 if (pos == NULL)
4553                         return -1;
4554                 pos++;
4555                 start = atoi(pos);
4556                 pos = os_strchr(pos, ',');
4557                 if (pos == NULL)
4558                         return -1;
4559                 pos++;
4560                 duration = atoi(pos);
4561                 if (count < 0 || count > 255 || start < 0 || duration < 0)
4562                         return -1;
4563                 if (count == 0 && duration > 0)
4564                         return -1;
4565                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
4566                            "start=%d duration=%d", count, start, duration);
4567                 return wpas_p2p_set_noa(wpa_s, count, start, duration);
4568         }
4569
4570         if (os_strcmp(cmd, "ps") == 0)
4571                 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
4572
4573         if (os_strcmp(cmd, "oppps") == 0)
4574                 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
4575
4576         if (os_strcmp(cmd, "ctwindow") == 0)
4577                 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
4578
4579         if (os_strcmp(cmd, "disabled") == 0) {
4580                 wpa_s->global->p2p_disabled = atoi(param);
4581                 wpa_printf(MSG_DEBUG, "P2P functionality %s",
4582                            wpa_s->global->p2p_disabled ?
4583                            "disabled" : "enabled");
4584                 if (wpa_s->global->p2p_disabled) {
4585                         wpas_p2p_stop_find(wpa_s);
4586                         os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4587                         p2p_flush(wpa_s->global->p2p);
4588                 }
4589                 return 0;
4590         }
4591
4592         if (os_strcmp(cmd, "conc_pref") == 0) {
4593                 if (os_strcmp(param, "sta") == 0)
4594                         wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
4595                 else if (os_strcmp(param, "p2p") == 0)
4596                         wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
4597                 else {
4598                         wpa_printf(MSG_INFO, "Invalid conc_pref value");
4599                         return -1;
4600                 }
4601                 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
4602                            "%s", param);
4603                 return 0;
4604         }
4605
4606         if (os_strcmp(cmd, "force_long_sd") == 0) {
4607                 wpa_s->force_long_sd = atoi(param);
4608                 return 0;
4609         }
4610
4611         if (os_strcmp(cmd, "peer_filter") == 0) {
4612                 u8 addr[ETH_ALEN];
4613                 if (hwaddr_aton(param, addr))
4614                         return -1;
4615                 p2p_set_peer_filter(wpa_s->global->p2p, addr);
4616                 return 0;
4617         }
4618
4619         if (os_strcmp(cmd, "cross_connect") == 0)
4620                 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
4621
4622         if (os_strcmp(cmd, "go_apsd") == 0) {
4623                 if (os_strcmp(param, "disable") == 0)
4624                         wpa_s->set_ap_uapsd = 0;
4625                 else {
4626                         wpa_s->set_ap_uapsd = 1;
4627                         wpa_s->ap_uapsd = atoi(param);
4628                 }
4629                 return 0;
4630         }
4631
4632         if (os_strcmp(cmd, "client_apsd") == 0) {
4633                 if (os_strcmp(param, "disable") == 0)
4634                         wpa_s->set_sta_uapsd = 0;
4635                 else {
4636                         int be, bk, vi, vo;
4637                         char *pos;
4638                         /* format: BE,BK,VI,VO;max SP Length */
4639                         be = atoi(param);
4640                         pos = os_strchr(param, ',');
4641                         if (pos == NULL)
4642                                 return -1;
4643                         pos++;
4644                         bk = atoi(pos);
4645                         pos = os_strchr(pos, ',');
4646                         if (pos == NULL)
4647                                 return -1;
4648                         pos++;
4649                         vi = atoi(pos);
4650                         pos = os_strchr(pos, ',');
4651                         if (pos == NULL)
4652                                 return -1;
4653                         pos++;
4654                         vo = atoi(pos);
4655                         /* ignore max SP Length for now */
4656
4657                         wpa_s->set_sta_uapsd = 1;
4658                         wpa_s->sta_uapsd = 0;
4659                         if (be)
4660                                 wpa_s->sta_uapsd |= BIT(0);
4661                         if (bk)
4662                                 wpa_s->sta_uapsd |= BIT(1);
4663                         if (vi)
4664                                 wpa_s->sta_uapsd |= BIT(2);
4665                         if (vo)
4666                                 wpa_s->sta_uapsd |= BIT(3);
4667                 }
4668                 return 0;
4669         }
4670
4671         if (os_strcmp(cmd, "disallow_freq") == 0)
4672                 return p2p_ctrl_disallow_freq(wpa_s, param);
4673
4674         if (os_strcmp(cmd, "disc_int") == 0) {
4675                 int min_disc_int, max_disc_int, max_disc_tu;
4676                 char *pos;
4677
4678                 pos = param;
4679
4680                 min_disc_int = atoi(pos);
4681                 pos = os_strchr(pos, ' ');
4682                 if (pos == NULL)
4683                         return -1;
4684                 *pos++ = '\0';
4685
4686                 max_disc_int = atoi(pos);
4687                 pos = os_strchr(pos, ' ');
4688                 if (pos == NULL)
4689                         return -1;
4690                 *pos++ = '\0';
4691
4692                 max_disc_tu = atoi(pos);
4693
4694                 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
4695                                         max_disc_int, max_disc_tu);
4696         }
4697
4698         if (os_strcmp(cmd, "per_sta_psk") == 0) {
4699                 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
4700                 return 0;
4701         }
4702
4703 #ifdef CONFIG_WPS_NFC
4704         if (os_strcmp(cmd, "nfc_tag") == 0)
4705                 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
4706 #endif /* CONFIG_WPS_NFC */
4707
4708         if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
4709                 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
4710                 return 0;
4711         }
4712
4713         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
4714                    cmd);
4715
4716         return -1;
4717 }
4718
4719
4720 static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
4721 {
4722         os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4723         wpa_s->force_long_sd = 0;
4724         if (wpa_s->global->p2p)
4725                 p2p_flush(wpa_s->global->p2p);
4726 }
4727
4728
4729 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
4730 {
4731         char *pos, *pos2;
4732         unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
4733
4734         if (cmd[0]) {
4735                 pos = os_strchr(cmd, ' ');
4736                 if (pos == NULL)
4737                         return -1;
4738                 *pos++ = '\0';
4739                 dur1 = atoi(cmd);
4740
4741                 pos2 = os_strchr(pos, ' ');
4742                 if (pos2)
4743                         *pos2++ = '\0';
4744                 int1 = atoi(pos);
4745         } else
4746                 pos2 = NULL;
4747
4748         if (pos2) {
4749                 pos = os_strchr(pos2, ' ');
4750                 if (pos == NULL)
4751                         return -1;
4752                 *pos++ = '\0';
4753                 dur2 = atoi(pos2);
4754                 int2 = atoi(pos);
4755         }
4756
4757         return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
4758 }
4759
4760
4761 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
4762 {
4763         char *pos;
4764         unsigned int period = 0, interval = 0;
4765
4766         if (cmd[0]) {
4767                 pos = os_strchr(cmd, ' ');
4768                 if (pos == NULL)
4769                         return -1;
4770                 *pos++ = '\0';
4771                 period = atoi(cmd);
4772                 interval = atoi(pos);
4773         }
4774
4775         return wpas_p2p_ext_listen(wpa_s, period, interval);
4776 }
4777
4778
4779 static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
4780 {
4781         const char *pos;
4782         u8 peer[ETH_ALEN];
4783         int iface_addr = 0;
4784
4785         pos = cmd;
4786         if (os_strncmp(pos, "iface=", 6) == 0) {
4787                 iface_addr = 1;
4788                 pos += 6;
4789         }
4790         if (hwaddr_aton(pos, peer))
4791                 return -1;
4792
4793         wpas_p2p_remove_client(wpa_s, peer, iface_addr);
4794         return 0;
4795 }
4796
4797 #endif /* CONFIG_P2P */
4798
4799
4800 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
4801 {
4802         struct wpa_freq_range_list ranges;
4803         int *freqs = NULL;
4804         struct hostapd_hw_modes *mode;
4805         u16 i;
4806
4807         if (wpa_s->hw.modes == NULL)
4808                 return NULL;
4809
4810         os_memset(&ranges, 0, sizeof(ranges));
4811         if (freq_range_list_parse(&ranges, val) < 0)
4812                 return NULL;
4813
4814         for (i = 0; i < wpa_s->hw.num_modes; i++) {
4815                 int j;
4816
4817                 mode = &wpa_s->hw.modes[i];
4818                 for (j = 0; j < mode->num_channels; j++) {
4819                         unsigned int freq;
4820
4821                         if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
4822                                 continue;
4823
4824                         freq = mode->channels[j].freq;
4825                         if (!freq_range_list_includes(&ranges, freq))
4826                                 continue;
4827
4828                         int_array_add_unique(&freqs, freq);
4829                 }
4830         }
4831
4832         os_free(ranges.range);
4833         return freqs;
4834 }
4835
4836
4837 #ifdef CONFIG_INTERWORKING
4838
4839 static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
4840 {
4841         int auto_sel = 0;
4842         int *freqs = NULL;
4843
4844         if (param) {
4845                 char *pos;
4846
4847                 auto_sel = os_strstr(param, "auto") != NULL;
4848
4849                 pos = os_strstr(param, "freq=");
4850                 if (pos) {
4851                         freqs = freq_range_to_channel_list(wpa_s, pos + 5);
4852                         if (freqs == NULL)
4853                                 return -1;
4854                 }
4855
4856         }
4857
4858         return interworking_select(wpa_s, auto_sel, freqs);
4859 }
4860
4861
4862 static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
4863 {
4864         u8 bssid[ETH_ALEN];
4865         struct wpa_bss *bss;
4866
4867         if (hwaddr_aton(dst, bssid)) {
4868                 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
4869                 return -1;
4870         }
4871
4872         bss = wpa_bss_get_bssid(wpa_s, bssid);
4873         if (bss == NULL) {
4874                 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
4875                            MAC2STR(bssid));
4876                 return -1;
4877         }
4878
4879         return interworking_connect(wpa_s, bss);
4880 }
4881
4882
4883 static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
4884 {
4885         u8 dst_addr[ETH_ALEN];
4886         int used;
4887         char *pos;
4888 #define MAX_ANQP_INFO_ID 100
4889         u16 id[MAX_ANQP_INFO_ID];
4890         size_t num_id = 0;
4891
4892         used = hwaddr_aton2(dst, dst_addr);
4893         if (used < 0)
4894                 return -1;
4895         pos = dst + used;
4896         while (num_id < MAX_ANQP_INFO_ID) {
4897                 id[num_id] = atoi(pos);
4898                 if (id[num_id])
4899                         num_id++;
4900                 pos = os_strchr(pos + 1, ',');
4901                 if (pos == NULL)
4902                         break;
4903                 pos++;
4904         }
4905
4906         if (num_id == 0)
4907                 return -1;
4908
4909         return anqp_send_req(wpa_s, dst_addr, id, num_id);
4910 }
4911
4912
4913 static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
4914 {
4915         u8 dst_addr[ETH_ALEN];
4916         struct wpabuf *advproto, *query = NULL;
4917         int used, ret = -1;
4918         char *pos, *end;
4919         size_t len;
4920
4921         used = hwaddr_aton2(cmd, dst_addr);
4922         if (used < 0)
4923                 return -1;
4924
4925         pos = cmd + used;
4926         while (*pos == ' ')
4927                 pos++;
4928
4929         /* Advertisement Protocol ID */
4930         end = os_strchr(pos, ' ');
4931         if (end)
4932                 len = end - pos;
4933         else
4934                 len = os_strlen(pos);
4935         if (len & 0x01)
4936                 return -1;
4937         len /= 2;
4938         if (len == 0)
4939                 return -1;
4940         advproto = wpabuf_alloc(len);
4941         if (advproto == NULL)
4942                 return -1;
4943         if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
4944                 goto fail;
4945
4946         if (end) {
4947                 /* Optional Query Request */
4948                 pos = end + 1;
4949                 while (*pos == ' ')
4950                         pos++;
4951
4952                 len = os_strlen(pos);
4953                 if (len) {
4954                         if (len & 0x01)
4955                                 goto fail;
4956                         len /= 2;
4957                         if (len == 0)
4958                                 goto fail;
4959                         query = wpabuf_alloc(len);
4960                         if (query == NULL)
4961                                 goto fail;
4962                         if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
4963                                 goto fail;
4964                 }
4965         }
4966
4967         ret = gas_send_request(wpa_s, dst_addr, advproto, query);
4968
4969 fail:
4970         wpabuf_free(advproto);
4971         wpabuf_free(query);
4972
4973         return ret;
4974 }
4975
4976
4977 static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
4978                             size_t buflen)
4979 {
4980         u8 addr[ETH_ALEN];
4981         int dialog_token;
4982         int used;
4983         char *pos;
4984         size_t resp_len, start, requested_len;
4985         struct wpabuf *resp;
4986         int ret;
4987
4988         used = hwaddr_aton2(cmd, addr);
4989         if (used < 0)
4990                 return -1;
4991
4992         pos = cmd + used;
4993         while (*pos == ' ')
4994                 pos++;
4995         dialog_token = atoi(pos);
4996
4997         if (wpa_s->last_gas_resp &&
4998             os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
4999             dialog_token == wpa_s->last_gas_dialog_token)
5000                 resp = wpa_s->last_gas_resp;
5001         else if (wpa_s->prev_gas_resp &&
5002                  os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
5003                  dialog_token == wpa_s->prev_gas_dialog_token)
5004                 resp = wpa_s->prev_gas_resp;
5005         else
5006                 return -1;
5007
5008         resp_len = wpabuf_len(resp);
5009         start = 0;
5010         requested_len = resp_len;
5011
5012         pos = os_strchr(pos, ' ');
5013         if (pos) {
5014                 start = atoi(pos);
5015                 if (start > resp_len)
5016                         return os_snprintf(buf, buflen, "FAIL-Invalid range");
5017                 pos = os_strchr(pos, ',');
5018                 if (pos == NULL)
5019                         return -1;
5020                 pos++;
5021                 requested_len = atoi(pos);
5022                 if (start + requested_len > resp_len)
5023                         return os_snprintf(buf, buflen, "FAIL-Invalid range");
5024         }
5025
5026         if (requested_len * 2 + 1 > buflen)
5027                 return os_snprintf(buf, buflen, "FAIL-Too long response");
5028
5029         ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
5030                                requested_len);
5031
5032         if (start + requested_len == resp_len) {
5033                 /*
5034                  * Free memory by dropping the response after it has been
5035                  * fetched.
5036                  */
5037                 if (resp == wpa_s->prev_gas_resp) {
5038                         wpabuf_free(wpa_s->prev_gas_resp);
5039                         wpa_s->prev_gas_resp = NULL;
5040                 } else {
5041                         wpabuf_free(wpa_s->last_gas_resp);
5042                         wpa_s->last_gas_resp = NULL;
5043                 }
5044         }
5045
5046         return ret;
5047 }
5048 #endif /* CONFIG_INTERWORKING */
5049
5050
5051 #ifdef CONFIG_HS20
5052
5053 static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
5054 {
5055         u8 dst_addr[ETH_ALEN];
5056         int used;
5057         char *pos;
5058         u32 subtypes = 0;
5059
5060         used = hwaddr_aton2(dst, dst_addr);
5061         if (used < 0)
5062                 return -1;
5063         pos = dst + used;
5064         for (;;) {
5065                 int num = atoi(pos);
5066                 if (num <= 0 || num > 31)
5067                         return -1;
5068                 subtypes |= BIT(num);
5069                 pos = os_strchr(pos + 1, ',');
5070                 if (pos == NULL)
5071                         break;
5072                 pos++;
5073         }
5074
5075         if (subtypes == 0)
5076                 return -1;
5077
5078         return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
5079 }
5080
5081
5082 static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5083                                     const u8 *addr, const char *realm)
5084 {
5085         u8 *buf;
5086         size_t rlen, len;
5087         int ret;
5088
5089         rlen = os_strlen(realm);
5090         len = 3 + rlen;
5091         buf = os_malloc(len);
5092         if (buf == NULL)
5093                 return -1;
5094         buf[0] = 1; /* NAI Home Realm Count */
5095         buf[1] = 0; /* Formatted in accordance with RFC 4282 */
5096         buf[2] = rlen;
5097         os_memcpy(buf + 3, realm, rlen);
5098
5099         ret = hs20_anqp_send_req(wpa_s, addr,
5100                                  BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5101                                  buf, len);
5102
5103         os_free(buf);
5104
5105         return ret;
5106 }
5107
5108
5109 static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5110                                         char *dst)
5111 {
5112         struct wpa_cred *cred = wpa_s->conf->cred;
5113         u8 dst_addr[ETH_ALEN];
5114         int used;
5115         u8 *buf;
5116         size_t len;
5117         int ret;
5118
5119         used = hwaddr_aton2(dst, dst_addr);
5120         if (used < 0)
5121                 return -1;
5122
5123         while (dst[used] == ' ')
5124                 used++;
5125         if (os_strncmp(dst + used, "realm=", 6) == 0)
5126                 return hs20_nai_home_realm_list(wpa_s, dst_addr,
5127                                                 dst + used + 6);
5128
5129         len = os_strlen(dst + used);
5130
5131         if (len == 0 && cred && cred->realm)
5132                 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
5133
5134         if (len % 1)
5135                 return -1;
5136         len /= 2;
5137         buf = os_malloc(len);
5138         if (buf == NULL)
5139                 return -1;
5140         if (hexstr2bin(dst + used, buf, len) < 0) {
5141                 os_free(buf);
5142                 return -1;
5143         }
5144
5145         ret = hs20_anqp_send_req(wpa_s, dst_addr,
5146                                  BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5147                                  buf, len);
5148         os_free(buf);
5149
5150         return ret;
5151 }
5152
5153 #endif /* CONFIG_HS20 */
5154
5155
5156 static int wpa_supplicant_ctrl_iface_sta_autoconnect(
5157         struct wpa_supplicant *wpa_s, char *cmd)
5158 {
5159         wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
5160         return 0;
5161 }
5162
5163
5164 #ifdef CONFIG_AUTOSCAN
5165
5166 static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
5167                                               char *cmd)
5168 {
5169         enum wpa_states state = wpa_s->wpa_state;
5170         char *new_params = NULL;
5171
5172         if (os_strlen(cmd) > 0) {
5173                 new_params = os_strdup(cmd);
5174                 if (new_params == NULL)
5175                         return -1;
5176         }
5177
5178         os_free(wpa_s->conf->autoscan);
5179         wpa_s->conf->autoscan = new_params;
5180
5181         if (wpa_s->conf->autoscan == NULL)
5182                 autoscan_deinit(wpa_s);
5183         else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
5184                 autoscan_init(wpa_s, 1);
5185         else if (state == WPA_SCANNING)
5186                 wpa_supplicant_reinit_autoscan(wpa_s);
5187
5188         return 0;
5189 }
5190
5191 #endif /* CONFIG_AUTOSCAN */
5192
5193
5194 #ifdef CONFIG_WNM
5195
5196 static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
5197 {
5198         int enter;
5199         int intval = 0;
5200         char *pos;
5201         int ret;
5202         struct wpabuf *tfs_req = NULL;
5203
5204         if (os_strncmp(cmd, "enter", 5) == 0)
5205                 enter = 1;
5206         else if (os_strncmp(cmd, "exit", 4) == 0)
5207                 enter = 0;
5208         else
5209                 return -1;
5210
5211         pos = os_strstr(cmd, " interval=");
5212         if (pos)
5213                 intval = atoi(pos + 10);
5214
5215         pos = os_strstr(cmd, " tfs_req=");
5216         if (pos) {
5217                 char *end;
5218                 size_t len;
5219                 pos += 9;
5220                 end = os_strchr(pos, ' ');
5221                 if (end)
5222                         len = end - pos;
5223                 else
5224                         len = os_strlen(pos);
5225                 if (len & 1)
5226                         return -1;
5227                 len /= 2;
5228                 tfs_req = wpabuf_alloc(len);
5229                 if (tfs_req == NULL)
5230                         return -1;
5231                 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
5232                         wpabuf_free(tfs_req);
5233                         return -1;
5234                 }
5235         }
5236
5237         ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
5238                                            WNM_SLEEP_MODE_EXIT, intval,
5239                                            tfs_req);
5240         wpabuf_free(tfs_req);
5241
5242         return ret;
5243 }
5244
5245
5246 static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
5247 {
5248         int query_reason;
5249
5250         query_reason = atoi(cmd);
5251
5252         wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
5253                    query_reason);
5254
5255         return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
5256 }
5257
5258 #endif /* CONFIG_WNM */
5259
5260
5261 /* Get string representation of channel width */
5262 static const char * channel_width_name(enum chan_width width)
5263 {
5264         switch (width) {
5265         case CHAN_WIDTH_20_NOHT:
5266                 return "20 MHz (no HT)";
5267         case CHAN_WIDTH_20:
5268                 return "20 MHz";
5269         case CHAN_WIDTH_40:
5270                 return "40 MHz";
5271         case CHAN_WIDTH_80:
5272                 return "80 MHz";
5273         case CHAN_WIDTH_80P80:
5274                 return "80+80 MHz";
5275         case CHAN_WIDTH_160:
5276                 return "160 MHz";
5277         default:
5278                 return "unknown";
5279         }
5280 }
5281
5282
5283 static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
5284                                       size_t buflen)
5285 {
5286         struct wpa_signal_info si;
5287         int ret;
5288         char *pos, *end;
5289
5290         ret = wpa_drv_signal_poll(wpa_s, &si);
5291         if (ret)
5292                 return -1;
5293
5294         pos = buf;
5295         end = buf + buflen;
5296
5297         ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
5298                           "NOISE=%d\nFREQUENCY=%u\n",
5299                           si.current_signal, si.current_txrate / 1000,
5300                           si.current_noise, si.frequency);
5301         if (ret < 0 || ret > end - pos)
5302                 return -1;
5303         pos += ret;
5304
5305         if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
5306                 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
5307                                   channel_width_name(si.chanwidth));
5308                 if (ret < 0 || ret > end - pos)
5309                         return -1;
5310                 pos += ret;
5311         }
5312
5313         if (si.center_frq1 > 0 && si.center_frq2 > 0) {
5314                 ret = os_snprintf(pos, end - pos,
5315                                   "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
5316                                   si.center_frq1, si.center_frq2);
5317                 if (ret < 0 || ret > end - pos)
5318                         return -1;
5319                 pos += ret;
5320         }
5321
5322         if (si.avg_signal) {
5323                 ret = os_snprintf(pos, end - pos,
5324                                   "AVG_RSSI=%d\n", si.avg_signal);
5325                 if (ret < 0 || ret >= end - pos)
5326                         return -1;
5327                 pos += ret;
5328         }
5329
5330         return pos - buf;
5331 }
5332
5333
5334 static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
5335                                       size_t buflen)
5336 {
5337         struct hostap_sta_driver_data sta;
5338         int ret;
5339
5340         ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
5341         if (ret)
5342                 return -1;
5343
5344         ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
5345                           sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
5346         if (ret < 0 || (size_t) ret > buflen)
5347                 return -1;
5348         return ret;
5349 }
5350
5351
5352 #ifdef ANDROID
5353 static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5354                                      char *buf, size_t buflen)
5355 {
5356         int ret;
5357
5358         ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
5359         if (ret == 0) {
5360                 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
5361                         struct p2p_data *p2p = wpa_s->global->p2p;
5362                         if (p2p) {
5363                                 char country[3];
5364                                 country[0] = cmd[8];
5365                                 country[1] = cmd[9];
5366                                 country[2] = 0x04;
5367                                 p2p_set_country(p2p, country);
5368                         }
5369                 }
5370                 ret = os_snprintf(buf, buflen, "%s\n", "OK");
5371         }
5372         return ret;
5373 }
5374 #endif /* ANDROID */
5375
5376
5377 static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
5378 {
5379         wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
5380
5381 #ifdef CONFIG_P2P
5382         wpas_p2p_stop_find(wpa_s);
5383         p2p_ctrl_flush(wpa_s);
5384         wpas_p2p_group_remove(wpa_s, "*");
5385         wpas_p2p_service_flush(wpa_s);
5386         wpa_s->global->p2p_disabled = 0;
5387         wpa_s->global->p2p_per_sta_psk = 0;
5388         wpa_s->conf->num_sec_device_types = 0;
5389         wpa_s->p2p_disable_ip_addr_req = 0;
5390 #endif /* CONFIG_P2P */
5391
5392 #ifdef CONFIG_WPS_TESTING
5393         wps_version_number = 0x20;
5394         wps_testing_dummy_cred = 0;
5395         wps_corrupt_pkhash = 0;
5396 #endif /* CONFIG_WPS_TESTING */
5397 #ifdef CONFIG_WPS
5398         wpa_s->wps_fragment_size = 0;
5399         wpas_wps_cancel(wpa_s);
5400 #endif /* CONFIG_WPS */
5401         wpa_s->after_wps = 0;
5402         wpa_s->known_wps_freq = 0;
5403
5404 #ifdef CONFIG_TDLS
5405 #ifdef CONFIG_TDLS_TESTING
5406         extern unsigned int tdls_testing;
5407         tdls_testing = 0;
5408 #endif /* CONFIG_TDLS_TESTING */
5409         wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
5410         wpa_tdls_enable(wpa_s->wpa, 1);
5411 #endif /* CONFIG_TDLS */
5412
5413         eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
5414         wpa_supplicant_stop_countermeasures(wpa_s, NULL);
5415
5416         wpa_s->no_keep_alive = 0;
5417
5418         os_free(wpa_s->disallow_aps_bssid);
5419         wpa_s->disallow_aps_bssid = NULL;
5420         wpa_s->disallow_aps_bssid_count = 0;
5421         os_free(wpa_s->disallow_aps_ssid);
5422         wpa_s->disallow_aps_ssid = NULL;
5423         wpa_s->disallow_aps_ssid_count = 0;
5424
5425         wpa_s->set_sta_uapsd = 0;
5426         wpa_s->sta_uapsd = 0;
5427
5428         wpa_drv_radio_disable(wpa_s, 0);
5429
5430         wpa_bss_flush(wpa_s);
5431         wpa_blacklist_clear(wpa_s);
5432         wpa_s->extra_blacklist_count = 0;
5433         wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
5434         wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
5435         wpa_config_flush_blobs(wpa_s->conf);
5436         wpa_s->conf->auto_interworking = 0;
5437         wpa_s->conf->okc = 0;
5438         wpa_s->conf->pmf = 0;
5439
5440         wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
5441         wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
5442         wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
5443         eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5444
5445         radio_remove_unstarted_work(wpa_s, NULL);
5446 }
5447
5448
5449 static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
5450                                      char *buf, size_t buflen)
5451 {
5452         struct wpa_radio_work *work;
5453         char *pos, *end;
5454         struct os_reltime now, diff;
5455
5456         pos = buf;
5457         end = buf + buflen;
5458
5459         os_get_reltime(&now);
5460
5461         dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
5462         {
5463                 int ret;
5464
5465                 os_reltime_sub(&now, &work->time, &diff);
5466                 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
5467                                   work->type, work->wpa_s->ifname, work->freq,
5468                                   work->started, diff.sec, diff.usec);
5469                 if (ret < 0 || ret >= end - pos)
5470                         break;
5471                 pos += ret;
5472         }
5473
5474         return pos - buf;
5475 }
5476
5477
5478 static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
5479 {
5480         struct wpa_radio_work *work = eloop_ctx;
5481         struct wpa_external_work *ework = work->ctx;
5482
5483         wpa_dbg(work->wpa_s, MSG_DEBUG,
5484                 "Timing out external radio work %u (%s)",
5485                 ework->id, work->type);
5486         wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
5487         os_free(ework);
5488         radio_work_done(work);
5489 }
5490
5491
5492 static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
5493 {
5494         struct wpa_external_work *ework = work->ctx;
5495
5496         if (deinit) {
5497                 os_free(ework);
5498                 return;
5499         }
5500
5501         wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
5502                 ework->id, ework->type);
5503         wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
5504         if (!ework->timeout)
5505                 ework->timeout = 10;
5506         eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
5507                                work, NULL);
5508 }
5509
5510
5511 static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
5512                                     char *buf, size_t buflen)
5513 {
5514         struct wpa_external_work *ework;
5515         char *pos, *pos2;
5516         size_t type_len;
5517         int ret;
5518         unsigned int freq = 0;
5519
5520         /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
5521
5522         ework = os_zalloc(sizeof(*ework));
5523         if (ework == NULL)
5524                 return -1;
5525
5526         pos = os_strchr(cmd, ' ');
5527         if (pos) {
5528                 type_len = pos - cmd;
5529                 pos++;
5530
5531                 pos2 = os_strstr(pos, "freq=");
5532                 if (pos2)
5533                         freq = atoi(pos2 + 5);
5534
5535                 pos2 = os_strstr(pos, "timeout=");
5536                 if (pos2)
5537                         ework->timeout = atoi(pos2 + 8);
5538         } else {
5539                 type_len = os_strlen(cmd);
5540         }
5541         if (4 + type_len >= sizeof(ework->type))
5542                 type_len = sizeof(ework->type) - 4 - 1;
5543         os_strlcpy(ework->type, "ext:", sizeof(ework->type));
5544         os_memcpy(ework->type + 4, cmd, type_len);
5545         ework->type[4 + type_len] = '\0';
5546
5547         wpa_s->ext_work_id++;
5548         if (wpa_s->ext_work_id == 0)
5549                 wpa_s->ext_work_id++;
5550         ework->id = wpa_s->ext_work_id;
5551
5552         if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
5553                            ework) < 0) {
5554                 os_free(ework);
5555                 return -1;
5556         }
5557
5558         ret = os_snprintf(buf, buflen, "%u", ework->id);
5559         if (ret < 0 || (size_t) ret >= buflen)
5560                 return -1;
5561         return ret;
5562 }
5563
5564
5565 static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
5566 {
5567         struct wpa_radio_work *work;
5568         unsigned int id = atoi(cmd);
5569
5570         dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
5571         {
5572                 struct wpa_external_work *ework;
5573
5574                 if (os_strncmp(work->type, "ext:", 4) != 0)
5575                         continue;
5576                 ework = work->ctx;
5577                 if (id && ework->id != id)
5578                         continue;
5579                 wpa_dbg(wpa_s, MSG_DEBUG,
5580                         "Completed external radio work %u (%s)",
5581                         ework->id, ework->type);
5582                 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
5583                 os_free(ework);
5584                 radio_work_done(work);
5585                 return 3; /* "OK\n" */
5586         }
5587
5588         return -1;
5589 }
5590
5591
5592 static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
5593                                 char *buf, size_t buflen)
5594 {
5595         if (os_strcmp(cmd, "show") == 0)
5596                 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
5597         if (os_strncmp(cmd, "add ", 4) == 0)
5598                 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
5599         if (os_strncmp(cmd, "done ", 5) == 0)
5600                 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
5601         return -1;
5602 }
5603
5604
5605 void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
5606 {
5607         struct wpa_radio_work *work, *tmp;
5608
5609         if (!wpa_s || !wpa_s->radio)
5610                 return;
5611
5612         dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
5613                               struct wpa_radio_work, list) {
5614                 struct wpa_external_work *ework;
5615
5616                 if (os_strncmp(work->type, "ext:", 4) != 0)
5617                         continue;
5618                 ework = work->ctx;
5619                 wpa_dbg(wpa_s, MSG_DEBUG,
5620                         "Flushing %sexternal radio work %u (%s)",
5621                         work->started ? " started" : "", ework->id,
5622                         ework->type);
5623                 if (work->started)
5624                         eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
5625                                              work, NULL);
5626                 os_free(ework);
5627                 radio_work_done(work);
5628         }
5629 }
5630
5631
5632 static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
5633 {
5634         struct wpa_supplicant *wpa_s = eloop_ctx;
5635         eapol_sm_notify_ctrl_response(wpa_s->eapol);
5636 }
5637
5638
5639 static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
5640 {
5641         int *freqs = NULL;
5642
5643         freqs = freq_range_to_channel_list(wpa_s, val);
5644         if (freqs == NULL)
5645                 return -1;
5646
5647         os_free(wpa_s->manual_scan_freqs);
5648         wpa_s->manual_scan_freqs = freqs;
5649
5650         return 0;
5651 }
5652
5653
5654 static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
5655                            char *reply, int reply_size, int *reply_len)
5656 {
5657         char *pos;
5658
5659         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5660                 *reply_len = -1;
5661                 return;
5662         }
5663
5664         wpa_s->manual_scan_passive = 0;
5665         wpa_s->manual_scan_use_id = 0;
5666         wpa_s->manual_scan_only_new = 0;
5667
5668         if (params) {
5669                 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
5670                         wpa_s->scan_res_handler = scan_only_handler;
5671
5672                 pos = os_strstr(params, "freq=");
5673                 if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
5674                         *reply_len = -1;
5675                         return;
5676                 }
5677
5678                 pos = os_strstr(params, "passive=");
5679                 if (pos)
5680                         wpa_s->manual_scan_passive = !!atoi(pos + 8);
5681
5682                 pos = os_strstr(params, "use_id=");
5683                 if (pos)
5684                         wpa_s->manual_scan_use_id = atoi(pos + 7);
5685
5686                 pos = os_strstr(params, "only_new=1");
5687                 if (pos)
5688                         wpa_s->manual_scan_only_new = 1;
5689         } else {
5690                 os_free(wpa_s->manual_scan_freqs);
5691                 wpa_s->manual_scan_freqs = NULL;
5692                 if (wpa_s->scan_res_handler == scan_only_handler)
5693                         wpa_s->scan_res_handler = NULL;
5694         }
5695
5696         if (!wpa_s->sched_scanning && !wpa_s->scanning &&
5697             ((wpa_s->wpa_state <= WPA_SCANNING) ||
5698              (wpa_s->wpa_state == WPA_COMPLETED))) {
5699                 wpa_s->normal_scans = 0;
5700                 wpa_s->scan_req = MANUAL_SCAN_REQ;
5701                 wpa_s->after_wps = 0;
5702                 wpa_s->known_wps_freq = 0;
5703                 wpa_supplicant_req_scan(wpa_s, 0, 0);
5704                 if (wpa_s->manual_scan_use_id) {
5705                         wpa_s->manual_scan_id++;
5706                         wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
5707                                 wpa_s->manual_scan_id);
5708                         *reply_len = os_snprintf(reply, reply_size, "%u\n",
5709                                                  wpa_s->manual_scan_id);
5710                 }
5711         } else if (wpa_s->sched_scanning) {
5712                 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
5713                 wpa_supplicant_cancel_sched_scan(wpa_s);
5714                 wpa_s->scan_req = MANUAL_SCAN_REQ;
5715                 wpa_supplicant_req_scan(wpa_s, 0, 0);
5716                 if (wpa_s->manual_scan_use_id) {
5717                         wpa_s->manual_scan_id++;
5718                         *reply_len = os_snprintf(reply, reply_size, "%u\n",
5719                                                  wpa_s->manual_scan_id);
5720                         wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
5721                                 wpa_s->manual_scan_id);
5722                 }
5723         } else {
5724                 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
5725                 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
5726         }
5727 }
5728
5729
5730 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
5731                                          char *buf, size_t *resp_len)
5732 {
5733         char *reply;
5734         const int reply_size = 4096;
5735         int reply_len;
5736
5737         if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
5738             os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
5739                 if (wpa_debug_show_keys)
5740                         wpa_dbg(wpa_s, MSG_DEBUG,
5741                                 "Control interface command '%s'", buf);
5742                 else
5743                         wpa_dbg(wpa_s, MSG_DEBUG,
5744                                 "Control interface command '%s [REMOVED]'",
5745                                 os_strncmp(buf, WPA_CTRL_RSP,
5746                                            os_strlen(WPA_CTRL_RSP)) == 0 ?
5747                                 WPA_CTRL_RSP : "SET_NETWORK");
5748         } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
5749                    os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
5750                    os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
5751                 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
5752                                       (const u8 *) buf, os_strlen(buf));
5753         } else {
5754                 int level = MSG_DEBUG;
5755                 if (os_strcmp(buf, "PING") == 0)
5756                         level = MSG_EXCESSIVE;
5757                 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
5758         }
5759
5760         reply = os_malloc(reply_size);
5761         if (reply == NULL) {
5762                 *resp_len = 1;
5763                 return NULL;
5764         }
5765
5766         os_memcpy(reply, "OK\n", 3);
5767         reply_len = 3;
5768
5769         if (os_strcmp(buf, "PING") == 0) {
5770                 os_memcpy(reply, "PONG\n", 5);
5771                 reply_len = 5;
5772         } else if (os_strcmp(buf, "IFNAME") == 0) {
5773                 reply_len = os_strlen(wpa_s->ifname);
5774                 os_memcpy(reply, wpa_s->ifname, reply_len);
5775         } else if (os_strncmp(buf, "RELOG", 5) == 0) {
5776                 if (wpa_debug_reopen_file() < 0)
5777                         reply_len = -1;
5778         } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
5779                 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
5780         } else if (os_strcmp(buf, "MIB") == 0) {
5781                 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
5782                 if (reply_len >= 0) {
5783                         int res;
5784                         res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
5785                                                reply_size - reply_len);
5786                         if (res < 0)
5787                                 reply_len = -1;
5788                         else
5789                                 reply_len += res;
5790                 }
5791         } else if (os_strncmp(buf, "STATUS", 6) == 0) {
5792                 reply_len = wpa_supplicant_ctrl_iface_status(
5793                         wpa_s, buf + 6, reply, reply_size);
5794         } else if (os_strcmp(buf, "PMKSA") == 0) {
5795                 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
5796                                                     reply_size);
5797         } else if (os_strncmp(buf, "SET ", 4) == 0) {
5798                 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
5799                         reply_len = -1;
5800         } else if (os_strncmp(buf, "GET ", 4) == 0) {
5801                 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
5802                                                           reply, reply_size);
5803         } else if (os_strcmp(buf, "LOGON") == 0) {
5804                 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5805         } else if (os_strcmp(buf, "LOGOFF") == 0) {
5806                 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
5807         } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
5808                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5809                         reply_len = -1;
5810                 else
5811                         wpas_request_connection(wpa_s);
5812         } else if (os_strcmp(buf, "RECONNECT") == 0) {
5813                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5814                         reply_len = -1;
5815                 else if (wpa_s->disconnected)
5816                         wpas_request_connection(wpa_s);
5817 #ifdef IEEE8021X_EAPOL
5818         } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
5819                 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
5820                         reply_len = -1;
5821 #endif /* IEEE8021X_EAPOL */
5822 #ifdef CONFIG_PEERKEY
5823         } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
5824                 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
5825                         reply_len = -1;
5826 #endif /* CONFIG_PEERKEY */
5827 #ifdef CONFIG_IEEE80211R
5828         } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
5829                 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
5830                         reply_len = -1;
5831 #endif /* CONFIG_IEEE80211R */
5832 #ifdef CONFIG_WPS
5833         } else if (os_strcmp(buf, "WPS_PBC") == 0) {
5834                 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
5835                 if (res == -2) {
5836                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5837                         reply_len = 17;
5838                 } else if (res)
5839                         reply_len = -1;
5840         } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
5841                 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
5842                 if (res == -2) {
5843                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5844                         reply_len = 17;
5845                 } else if (res)
5846                         reply_len = -1;
5847         } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
5848                 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
5849                                                               reply,
5850                                                               reply_size);
5851         } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
5852                 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
5853                         wpa_s, buf + 14, reply, reply_size);
5854         } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
5855                 if (wpas_wps_cancel(wpa_s))
5856                         reply_len = -1;
5857 #ifdef CONFIG_WPS_NFC
5858         } else if (os_strcmp(buf, "WPS_NFC") == 0) {
5859                 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
5860                         reply_len = -1;
5861         } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
5862                 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
5863                         reply_len = -1;
5864         } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
5865                 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
5866                         wpa_s, buf + 21, reply, reply_size);
5867         } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
5868                 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
5869                         wpa_s, buf + 14, reply, reply_size);
5870         } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
5871                 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
5872                                                                buf + 17))
5873                         reply_len = -1;
5874         } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
5875                 reply_len = wpas_ctrl_nfc_get_handover_req(
5876                         wpa_s, buf + 21, reply, reply_size);
5877         } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
5878                 reply_len = wpas_ctrl_nfc_get_handover_sel(
5879                         wpa_s, buf + 21, reply, reply_size);
5880         } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
5881                 reply_len = wpas_ctrl_nfc_rx_handover_req(
5882                         wpa_s, buf + 20, reply, reply_size);
5883         } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
5884                 if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
5885                         reply_len = -1;
5886         } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
5887                 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
5888                         reply_len = -1;
5889 #endif /* CONFIG_WPS_NFC */
5890         } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
5891                 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
5892                         reply_len = -1;
5893 #ifdef CONFIG_AP
5894         } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
5895                 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
5896                         wpa_s, buf + 11, reply, reply_size);
5897 #endif /* CONFIG_AP */
5898 #ifdef CONFIG_WPS_ER
5899         } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
5900                 if (wpas_wps_er_start(wpa_s, NULL))
5901                         reply_len = -1;
5902         } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
5903                 if (wpas_wps_er_start(wpa_s, buf + 13))
5904                         reply_len = -1;
5905         } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
5906                 if (wpas_wps_er_stop(wpa_s))
5907                         reply_len = -1;
5908         } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
5909                 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
5910                         reply_len = -1;
5911         } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
5912                 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
5913                 if (ret == -2) {
5914                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5915                         reply_len = 17;
5916                 } else if (ret == -3) {
5917                         os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
5918                         reply_len = 18;
5919                 } else if (ret == -4) {
5920                         os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
5921                         reply_len = 20;
5922                 } else if (ret)
5923                         reply_len = -1;
5924         } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
5925                 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
5926                         reply_len = -1;
5927         } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
5928                 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
5929                                                                 buf + 18))
5930                         reply_len = -1;
5931         } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
5932                 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
5933                         reply_len = -1;
5934 #ifdef CONFIG_WPS_NFC
5935         } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
5936                 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
5937                         wpa_s, buf + 24, reply, reply_size);
5938 #endif /* CONFIG_WPS_NFC */
5939 #endif /* CONFIG_WPS_ER */
5940 #endif /* CONFIG_WPS */
5941 #ifdef CONFIG_IBSS_RSN
5942         } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
5943                 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
5944                         reply_len = -1;
5945 #endif /* CONFIG_IBSS_RSN */
5946 #ifdef CONFIG_P2P
5947         } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
5948                 if (p2p_ctrl_find(wpa_s, buf + 9))
5949                         reply_len = -1;
5950         } else if (os_strcmp(buf, "P2P_FIND") == 0) {
5951                 if (p2p_ctrl_find(wpa_s, ""))
5952                         reply_len = -1;
5953         } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
5954                 wpas_p2p_stop_find(wpa_s);
5955         } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
5956                 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
5957                                              reply_size);
5958         } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
5959                 if (p2p_ctrl_listen(wpa_s, buf + 11))
5960                         reply_len = -1;
5961         } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
5962                 if (p2p_ctrl_listen(wpa_s, ""))
5963                         reply_len = -1;
5964         } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
5965                 if (wpas_p2p_group_remove(wpa_s, buf + 17))
5966                         reply_len = -1;
5967         } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
5968                 if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
5969                         reply_len = -1;
5970         } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
5971                 if (p2p_ctrl_group_add(wpa_s, buf + 14))
5972                         reply_len = -1;
5973         } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
5974                 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
5975                         reply_len = -1;
5976         } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
5977                 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
5978         } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
5979                 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
5980                                                    reply_size);
5981         } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
5982                 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
5983                         reply_len = -1;
5984         } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
5985                 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
5986                         reply_len = -1;
5987         } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
5988                 wpas_p2p_sd_service_update(wpa_s);
5989         } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
5990                 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
5991                         reply_len = -1;
5992         } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
5993                 wpas_p2p_service_flush(wpa_s);
5994         } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
5995                 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
5996                         reply_len = -1;
5997         } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
5998                 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
5999                         reply_len = -1;
6000         } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
6001                 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
6002                         reply_len = -1;
6003         } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
6004                 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
6005                         reply_len = -1;
6006         } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
6007                 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
6008                                               reply_size);
6009         } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
6010                 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
6011                         reply_len = -1;
6012         } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
6013                 p2p_ctrl_flush(wpa_s);
6014         } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
6015                 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
6016                         reply_len = -1;
6017         } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
6018                 if (wpas_p2p_cancel(wpa_s))
6019                         reply_len = -1;
6020         } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
6021                 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
6022                         reply_len = -1;
6023         } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
6024                 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
6025                         reply_len = -1;
6026         } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
6027                 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
6028                         reply_len = -1;
6029         } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
6030                 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
6031                         reply_len = -1;
6032         } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
6033                 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
6034                         reply_len = -1;
6035 #endif /* CONFIG_P2P */
6036 #ifdef CONFIG_WIFI_DISPLAY
6037         } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
6038                 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
6039                         reply_len = -1;
6040         } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
6041                 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
6042                                                      reply, reply_size);
6043 #endif /* CONFIG_WIFI_DISPLAY */
6044 #ifdef CONFIG_INTERWORKING
6045         } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
6046                 if (interworking_fetch_anqp(wpa_s) < 0)
6047                         reply_len = -1;
6048         } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
6049                 interworking_stop_fetch_anqp(wpa_s);
6050         } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
6051                 if (ctrl_interworking_select(wpa_s, NULL) < 0)
6052                         reply_len = -1;
6053         } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
6054                 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
6055                         reply_len = -1;
6056         } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
6057                 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
6058                         reply_len = -1;
6059         } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
6060                 if (get_anqp(wpa_s, buf + 9) < 0)
6061                         reply_len = -1;
6062         } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
6063                 if (gas_request(wpa_s, buf + 12) < 0)
6064                         reply_len = -1;
6065         } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
6066                 reply_len = gas_response_get(wpa_s, buf + 17, reply,
6067                                              reply_size);
6068 #endif /* CONFIG_INTERWORKING */
6069 #ifdef CONFIG_HS20
6070         } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
6071                 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
6072                         reply_len = -1;
6073         } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
6074                 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
6075                         reply_len = -1;
6076 #endif /* CONFIG_HS20 */
6077         } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
6078         {
6079                 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
6080                             wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
6081                         reply_len = -1;
6082                 else {
6083                         /*
6084                          * Notify response from timeout to allow the control
6085                          * interface response to be sent first.
6086                          */
6087                         eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
6088                                                wpa_s, NULL);
6089                 }
6090         } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
6091                 if (wpa_supplicant_reload_configuration(wpa_s))
6092                         reply_len = -1;
6093         } else if (os_strcmp(buf, "TERMINATE") == 0) {
6094                 wpa_supplicant_terminate_proc(wpa_s->global);
6095         } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
6096                 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
6097                         reply_len = -1;
6098         } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
6099                 reply_len = wpa_supplicant_ctrl_iface_blacklist(
6100                         wpa_s, buf + 9, reply, reply_size);
6101         } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
6102                 reply_len = wpa_supplicant_ctrl_iface_log_level(
6103                         wpa_s, buf + 9, reply, reply_size);
6104         } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
6105                 reply_len = wpa_supplicant_ctrl_iface_list_networks(
6106                         wpa_s, reply, reply_size);
6107         } else if (os_strcmp(buf, "DISCONNECT") == 0) {
6108 #ifdef CONFIG_SME
6109                 wpa_s->sme.prev_bssid_set = 0;
6110 #endif /* CONFIG_SME */
6111                 wpa_s->reassociate = 0;
6112                 wpa_s->disconnected = 1;
6113                 wpa_supplicant_cancel_sched_scan(wpa_s);
6114                 wpa_supplicant_cancel_scan(wpa_s);
6115                 wpa_supplicant_deauthenticate(wpa_s,
6116                                               WLAN_REASON_DEAUTH_LEAVING);
6117         } else if (os_strcmp(buf, "SCAN") == 0) {
6118                 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
6119         } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
6120                 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
6121         } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
6122                 reply_len = wpa_supplicant_ctrl_iface_scan_results(
6123                         wpa_s, reply, reply_size);
6124         } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
6125                 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
6126                         reply_len = -1;
6127         } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
6128                 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
6129                         reply_len = -1;
6130         } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
6131                 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
6132                         reply_len = -1;
6133         } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
6134                 reply_len = wpa_supplicant_ctrl_iface_add_network(
6135                         wpa_s, reply, reply_size);
6136         } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
6137                 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
6138                         reply_len = -1;
6139         } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
6140                 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
6141                         reply_len = -1;
6142         } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
6143                 reply_len = wpa_supplicant_ctrl_iface_get_network(
6144                         wpa_s, buf + 12, reply, reply_size);
6145         } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
6146                 reply_len = wpa_supplicant_ctrl_iface_list_creds(
6147                         wpa_s, reply, reply_size);
6148         } else if (os_strcmp(buf, "ADD_CRED") == 0) {
6149                 reply_len = wpa_supplicant_ctrl_iface_add_cred(
6150                         wpa_s, reply, reply_size);
6151         } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
6152                 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
6153                         reply_len = -1;
6154         } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
6155                 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
6156                         reply_len = -1;
6157 #ifndef CONFIG_NO_CONFIG_WRITE
6158         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
6159                 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
6160                         reply_len = -1;
6161 #endif /* CONFIG_NO_CONFIG_WRITE */
6162         } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
6163                 reply_len = wpa_supplicant_ctrl_iface_get_capability(
6164                         wpa_s, buf + 15, reply, reply_size);
6165         } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
6166                 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
6167                         reply_len = -1;
6168         } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
6169                 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
6170                         reply_len = -1;
6171         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
6172                 reply_len = wpa_supplicant_global_iface_list(
6173                         wpa_s->global, reply, reply_size);
6174         } else if (os_strcmp(buf, "INTERFACES") == 0) {
6175                 reply_len = wpa_supplicant_global_iface_interfaces(
6176                         wpa_s->global, reply, reply_size);
6177         } else if (os_strncmp(buf, "BSS ", 4) == 0) {
6178                 reply_len = wpa_supplicant_ctrl_iface_bss(
6179                         wpa_s, buf + 4, reply, reply_size);
6180 #ifdef CONFIG_AP
6181         } else if (os_strcmp(buf, "STA-FIRST") == 0) {
6182                 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
6183         } else if (os_strncmp(buf, "STA ", 4) == 0) {
6184                 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
6185                                               reply_size);
6186         } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
6187                 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
6188                                                    reply_size);
6189         } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
6190                 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
6191                         reply_len = -1;
6192         } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
6193                 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
6194                         reply_len = -1;
6195         } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
6196                 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
6197                         reply_len = -1;
6198 #endif /* CONFIG_AP */
6199         } else if (os_strcmp(buf, "SUSPEND") == 0) {
6200                 wpas_notify_suspend(wpa_s->global);
6201         } else if (os_strcmp(buf, "RESUME") == 0) {
6202                 wpas_notify_resume(wpa_s->global);
6203         } else if (os_strcmp(buf, "DROP_SA") == 0) {
6204                 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
6205         } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
6206                 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
6207                         reply_len = -1;
6208         } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
6209                 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
6210                         reply_len = -1;
6211         } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
6212                 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
6213                         reply_len = -1;
6214         } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
6215                 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
6216                                                                buf + 17))
6217                         reply_len = -1;
6218         } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
6219                 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
6220                         reply_len = -1;
6221 #ifdef CONFIG_TDLS
6222         } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
6223                 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
6224                         reply_len = -1;
6225         } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
6226                 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
6227                         reply_len = -1;
6228         } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
6229                 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
6230                         reply_len = -1;
6231 #endif /* CONFIG_TDLS */
6232         } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
6233                 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
6234                                                        reply_size);
6235         } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
6236                 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
6237                                                        reply_size);
6238 #ifdef CONFIG_AUTOSCAN
6239         } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
6240                 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
6241                         reply_len = -1;
6242 #endif /* CONFIG_AUTOSCAN */
6243 #ifdef ANDROID
6244         } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
6245                 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
6246                                                       reply_size);
6247 #endif /* ANDROID */
6248         } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
6249                 pmksa_cache_clear_current(wpa_s->wpa);
6250                 eapol_sm_request_reauth(wpa_s->eapol);
6251 #ifdef CONFIG_WNM
6252         } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
6253                 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
6254                         reply_len = -1;
6255         } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
6256                 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
6257                                 reply_len = -1;
6258 #endif /* CONFIG_WNM */
6259         } else if (os_strcmp(buf, "FLUSH") == 0) {
6260                 wpa_supplicant_ctrl_iface_flush(wpa_s);
6261         } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
6262                 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
6263                                                  reply_size);
6264         } else {
6265                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
6266                 reply_len = 16;
6267         }
6268
6269         if (reply_len < 0) {
6270                 os_memcpy(reply, "FAIL\n", 5);
6271                 reply_len = 5;
6272         }
6273
6274         *resp_len = reply_len;
6275         return reply;
6276 }
6277
6278
6279 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
6280                                            char *cmd)
6281 {
6282         struct wpa_interface iface;
6283         char *pos;
6284
6285         /*
6286          * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
6287          * TAB<bridge_ifname>
6288          */
6289         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
6290
6291         os_memset(&iface, 0, sizeof(iface));
6292
6293         do {
6294                 iface.ifname = pos = cmd;
6295                 pos = os_strchr(pos, '\t');
6296                 if (pos)
6297                         *pos++ = '\0';
6298                 if (iface.ifname[0] == '\0')
6299                         return -1;
6300                 if (pos == NULL)
6301                         break;
6302
6303                 iface.confname = pos;
6304                 pos = os_strchr(pos, '\t');
6305                 if (pos)
6306                         *pos++ = '\0';
6307                 if (iface.confname[0] == '\0')
6308                         iface.confname = NULL;
6309                 if (pos == NULL)
6310                         break;
6311
6312                 iface.driver = pos;
6313                 pos = os_strchr(pos, '\t');
6314                 if (pos)
6315                         *pos++ = '\0';
6316                 if (iface.driver[0] == '\0')
6317                         iface.driver = NULL;
6318                 if (pos == NULL)
6319                         break;
6320
6321                 iface.ctrl_interface = pos;
6322                 pos = os_strchr(pos, '\t');
6323                 if (pos)
6324                         *pos++ = '\0';
6325                 if (iface.ctrl_interface[0] == '\0')
6326                         iface.ctrl_interface = NULL;
6327                 if (pos == NULL)
6328                         break;
6329
6330                 iface.driver_param = pos;
6331                 pos = os_strchr(pos, '\t');
6332                 if (pos)
6333                         *pos++ = '\0';
6334                 if (iface.driver_param[0] == '\0')
6335                         iface.driver_param = NULL;
6336                 if (pos == NULL)
6337                         break;
6338
6339                 iface.bridge_ifname = pos;
6340                 pos = os_strchr(pos, '\t');
6341                 if (pos)
6342                         *pos++ = '\0';
6343                 if (iface.bridge_ifname[0] == '\0')
6344                         iface.bridge_ifname = NULL;
6345                 if (pos == NULL)
6346                         break;
6347         } while (0);
6348
6349         if (wpa_supplicant_get_iface(global, iface.ifname))
6350                 return -1;
6351
6352         return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
6353 }
6354
6355
6356 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
6357                                               char *cmd)
6358 {
6359         struct wpa_supplicant *wpa_s;
6360
6361         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
6362
6363         wpa_s = wpa_supplicant_get_iface(global, cmd);
6364         if (wpa_s == NULL)
6365                 return -1;
6366         return wpa_supplicant_remove_iface(global, wpa_s, 0);
6367 }
6368
6369
6370 static void wpa_free_iface_info(struct wpa_interface_info *iface)
6371 {
6372         struct wpa_interface_info *prev;
6373
6374         while (iface) {
6375                 prev = iface;
6376                 iface = iface->next;
6377
6378                 os_free(prev->ifname);
6379                 os_free(prev->desc);
6380                 os_free(prev);
6381         }
6382 }
6383
6384
6385 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
6386                                             char *buf, int len)
6387 {
6388         int i, res;
6389         struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
6390         char *pos, *end;
6391
6392         for (i = 0; wpa_drivers[i]; i++) {
6393                 struct wpa_driver_ops *drv = wpa_drivers[i];
6394                 if (drv->get_interfaces == NULL)
6395                         continue;
6396                 tmp = drv->get_interfaces(global->drv_priv[i]);
6397                 if (tmp == NULL)
6398                         continue;
6399
6400                 if (last == NULL)
6401                         iface = last = tmp;
6402                 else
6403                         last->next = tmp;
6404                 while (last->next)
6405                         last = last->next;
6406         }
6407
6408         pos = buf;
6409         end = buf + len;
6410         for (tmp = iface; tmp; tmp = tmp->next) {
6411                 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
6412                                   tmp->drv_name, tmp->ifname,
6413                                   tmp->desc ? tmp->desc : "");
6414                 if (res < 0 || res >= end - pos) {
6415                         *pos = '\0';
6416                         break;
6417                 }
6418                 pos += res;
6419         }
6420
6421         wpa_free_iface_info(iface);
6422
6423         return pos - buf;
6424 }
6425
6426
6427 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
6428                                                   char *buf, int len)
6429 {
6430         int res;
6431         char *pos, *end;
6432         struct wpa_supplicant *wpa_s;
6433
6434         wpa_s = global->ifaces;
6435         pos = buf;
6436         end = buf + len;
6437
6438         while (wpa_s) {
6439                 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
6440                 if (res < 0 || res >= end - pos) {
6441                         *pos = '\0';
6442                         break;
6443                 }
6444                 pos += res;
6445                 wpa_s = wpa_s->next;
6446         }
6447         return pos - buf;
6448 }
6449
6450
6451 static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
6452                                             const char *ifname,
6453                                             char *cmd, size_t *resp_len)
6454 {
6455         struct wpa_supplicant *wpa_s;
6456
6457         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6458                 if (os_strcmp(ifname, wpa_s->ifname) == 0)
6459                         break;
6460         }
6461
6462         if (wpa_s == NULL) {
6463                 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
6464                 if (resp)
6465                         *resp_len = os_strlen(resp);
6466                 else
6467                         *resp_len = 1;
6468                 return resp;
6469         }
6470
6471         return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
6472 }
6473
6474
6475 static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
6476                                                char *buf, size_t *resp_len)
6477 {
6478 #ifdef CONFIG_P2P
6479         static const char * cmd[] = {
6480                 "LIST_NETWORKS",
6481                 "SAVE_CONFIG",
6482                 "P2P_FIND",
6483                 "P2P_STOP_FIND",
6484                 "P2P_LISTEN",
6485                 "P2P_GROUP_ADD",
6486                 "P2P_GET_PASSPHRASE",
6487                 "P2P_SERVICE_UPDATE",
6488                 "P2P_SERVICE_FLUSH",
6489                 "P2P_FLUSH",
6490                 "P2P_CANCEL",
6491                 "P2P_PRESENCE_REQ",
6492                 "P2P_EXT_LISTEN",
6493                 NULL
6494         };
6495         static const char * prefix[] = {
6496 #ifdef ANDROID
6497                 "DRIVER ",
6498 #endif /* ANDROID */
6499                 "GET_NETWORK ",
6500                 "REMOVE_NETWORK ",
6501                 "SET ",
6502                 "P2P_FIND ",
6503                 "P2P_CONNECT ",
6504                 "P2P_LISTEN ",
6505                 "P2P_GROUP_REMOVE ",
6506                 "P2P_GROUP_ADD ",
6507                 "P2P_PROV_DISC ",
6508                 "P2P_SERV_DISC_REQ ",
6509                 "P2P_SERV_DISC_CANCEL_REQ ",
6510                 "P2P_SERV_DISC_RESP ",
6511                 "P2P_SERV_DISC_EXTERNAL ",
6512                 "P2P_SERVICE_ADD ",
6513                 "P2P_SERVICE_DEL ",
6514                 "P2P_REJECT ",
6515                 "P2P_INVITE ",
6516                 "P2P_PEER ",
6517                 "P2P_SET ",
6518                 "P2P_UNAUTHORIZE ",
6519                 "P2P_PRESENCE_REQ ",
6520                 "P2P_EXT_LISTEN ",
6521                 "P2P_REMOVE_CLIENT ",
6522                 NULL
6523         };
6524         int found = 0;
6525         int i;
6526
6527         if (global->p2p_init_wpa_s == NULL)
6528                 return NULL;
6529
6530         for (i = 0; !found && cmd[i]; i++) {
6531                 if (os_strcmp(buf, cmd[i]) == 0)
6532                         found = 1;
6533         }
6534
6535         for (i = 0; !found && prefix[i]; i++) {
6536                 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
6537                         found = 1;
6538         }
6539
6540         if (found)
6541                 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6542                                                          buf, resp_len);
6543 #endif /* CONFIG_P2P */
6544         return NULL;
6545 }
6546
6547
6548 static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
6549                                                char *buf, size_t *resp_len)
6550 {
6551 #ifdef CONFIG_WIFI_DISPLAY
6552         if (global->p2p_init_wpa_s == NULL)
6553                 return NULL;
6554         if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
6555             os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
6556                 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6557                                                          buf, resp_len);
6558 #endif /* CONFIG_WIFI_DISPLAY */
6559         return NULL;
6560 }
6561
6562
6563 static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
6564                                            char *buf, size_t *resp_len)
6565 {
6566         char *ret;
6567
6568         ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
6569         if (ret)
6570                 return ret;
6571
6572         ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
6573         if (ret)
6574                 return ret;
6575
6576         return NULL;
6577 }
6578
6579
6580 static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
6581 {
6582         char *value;
6583
6584         value = os_strchr(cmd, ' ');
6585         if (value == NULL)
6586                 return -1;
6587         *value++ = '\0';
6588
6589         wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
6590
6591 #ifdef CONFIG_WIFI_DISPLAY
6592         if (os_strcasecmp(cmd, "wifi_display") == 0) {
6593                 wifi_display_enable(global, !!atoi(value));
6594                 return 0;
6595         }
6596 #endif /* CONFIG_WIFI_DISPLAY */
6597
6598         return -1;
6599 }
6600
6601
6602 #ifndef CONFIG_NO_CONFIG_WRITE
6603 static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
6604 {
6605         int ret = 0;
6606         struct wpa_supplicant *wpa_s;
6607
6608         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6609                 if (!wpa_s->conf->update_config) {
6610                         wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
6611                         continue;
6612                 }
6613
6614                 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
6615                         wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
6616                         ret = 1;
6617                 } else {
6618                         wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
6619                 }
6620         }
6621
6622         return ret;
6623 }
6624 #endif /* CONFIG_NO_CONFIG_WRITE */
6625
6626
6627 static int wpas_global_ctrl_iface_status(struct wpa_global *global,
6628                                          char *buf, size_t buflen)
6629 {
6630         char *pos, *end;
6631         int ret;
6632         struct wpa_supplicant *wpa_s;
6633
6634         pos = buf;
6635         end = buf + buflen;
6636
6637 #ifdef CONFIG_P2P
6638         if (global->p2p && !global->p2p_disabled) {
6639                 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
6640                                   "\n"
6641                                   "p2p_state=%s\n",
6642                                   MAC2STR(global->p2p_dev_addr),
6643                                   p2p_get_state_txt(global->p2p));
6644                 if (ret < 0 || ret >= end - pos)
6645                         return pos - buf;
6646                 pos += ret;
6647         } else if (global->p2p) {
6648                 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
6649                 if (ret < 0 || ret >= end - pos)
6650                         return pos - buf;
6651                 pos += ret;
6652         }
6653 #endif /* CONFIG_P2P */
6654
6655 #ifdef CONFIG_WIFI_DISPLAY
6656         ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
6657                           !!global->wifi_display);
6658         if (ret < 0 || ret >= end - pos)
6659                 return pos - buf;
6660         pos += ret;
6661 #endif /* CONFIG_WIFI_DISPLAY */
6662
6663         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6664                 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
6665                                   "address=" MACSTR "\n",
6666                                   wpa_s->ifname, MAC2STR(wpa_s->own_addr));
6667                 if (ret < 0 || ret >= end - pos)
6668                         return pos - buf;
6669                 pos += ret;
6670         }
6671
6672         return pos - buf;
6673 }
6674
6675
6676 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
6677                                                 char *buf, size_t *resp_len)
6678 {
6679         char *reply;
6680         const int reply_size = 2048;
6681         int reply_len;
6682         int level = MSG_DEBUG;
6683
6684         if (os_strncmp(buf, "IFNAME=", 7) == 0) {
6685                 char *pos = os_strchr(buf + 7, ' ');
6686                 if (pos) {
6687                         *pos++ = '\0';
6688                         return wpas_global_ctrl_iface_ifname(global,
6689                                                              buf + 7, pos,
6690                                                              resp_len);
6691                 }
6692         }
6693
6694         reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
6695         if (reply)
6696                 return reply;
6697
6698         if (os_strcmp(buf, "PING") == 0)
6699                 level = MSG_EXCESSIVE;
6700         wpa_hexdump_ascii(level, "RX global ctrl_iface",
6701                           (const u8 *) buf, os_strlen(buf));
6702
6703         reply = os_malloc(reply_size);
6704         if (reply == NULL) {
6705                 *resp_len = 1;
6706                 return NULL;
6707         }
6708
6709         os_memcpy(reply, "OK\n", 3);
6710         reply_len = 3;
6711
6712         if (os_strcmp(buf, "PING") == 0) {
6713                 os_memcpy(reply, "PONG\n", 5);
6714                 reply_len = 5;
6715         } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
6716                 if (wpa_supplicant_global_iface_add(global, buf + 14))
6717                         reply_len = -1;
6718         } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
6719                 if (wpa_supplicant_global_iface_remove(global, buf + 17))
6720                         reply_len = -1;
6721         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
6722                 reply_len = wpa_supplicant_global_iface_list(
6723                         global, reply, reply_size);
6724         } else if (os_strcmp(buf, "INTERFACES") == 0) {
6725                 reply_len = wpa_supplicant_global_iface_interfaces(
6726                         global, reply, reply_size);
6727         } else if (os_strcmp(buf, "TERMINATE") == 0) {
6728                 wpa_supplicant_terminate_proc(global);
6729         } else if (os_strcmp(buf, "SUSPEND") == 0) {
6730                 wpas_notify_suspend(global);
6731         } else if (os_strcmp(buf, "RESUME") == 0) {
6732                 wpas_notify_resume(global);
6733         } else if (os_strncmp(buf, "SET ", 4) == 0) {
6734                 if (wpas_global_ctrl_iface_set(global, buf + 4))
6735                         reply_len = -1;
6736 #ifndef CONFIG_NO_CONFIG_WRITE
6737         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
6738                 if (wpas_global_ctrl_iface_save_config(global))
6739                         reply_len = -1;
6740 #endif /* CONFIG_NO_CONFIG_WRITE */
6741         } else if (os_strcmp(buf, "STATUS") == 0) {
6742                 reply_len = wpas_global_ctrl_iface_status(global, reply,
6743                                                           reply_size);
6744         } else {
6745                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
6746                 reply_len = 16;
6747         }
6748
6749         if (reply_len < 0) {
6750                 os_memcpy(reply, "FAIL\n", 5);
6751                 reply_len = 5;
6752         }
6753
6754         *resp_len = reply_len;
6755         return reply;
6756 }