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