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