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