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