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