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