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