ERP: Add support for ERP on EAP peer
[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 *cmd, char *buf, size_t buflen)
2052 {
2053         char *pos, *end, *prev;
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
2067         /* skip over ssids until we find next one */
2068         if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
2069                 int last_id = atoi(cmd + 8);
2070                 if (last_id != -1) {
2071                         while (ssid != NULL && ssid->id <= last_id) {
2072                                 ssid = ssid->next;
2073                         }
2074                 }
2075         }
2076
2077         while (ssid) {
2078                 prev = pos;
2079                 ret = os_snprintf(pos, end - pos, "%d\t%s",
2080                                   ssid->id,
2081                                   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2082                 if (ret < 0 || ret >= end - pos)
2083                         return prev - buf;
2084                 pos += ret;
2085                 if (ssid->bssid_set) {
2086                         ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2087                                           MAC2STR(ssid->bssid));
2088                 } else {
2089                         ret = os_snprintf(pos, end - pos, "\tany");
2090                 }
2091                 if (ret < 0 || ret >= end - pos)
2092                         return prev - buf;
2093                 pos += ret;
2094                 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
2095                                   ssid == wpa_s->current_ssid ?
2096                                   "[CURRENT]" : "",
2097                                   ssid->disabled ? "[DISABLED]" : "",
2098                                   ssid->disabled_until.sec ?
2099                                   "[TEMP-DISABLED]" : "",
2100                                   ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2101                                   "");
2102                 if (ret < 0 || ret >= end - pos)
2103                         return prev - buf;
2104                 pos += ret;
2105                 ret = os_snprintf(pos, end - pos, "\n");
2106                 if (ret < 0 || ret >= end - pos)
2107                         return prev - buf;
2108                 pos += ret;
2109
2110                 ssid = ssid->next;
2111         }
2112
2113         return pos - buf;
2114 }
2115
2116
2117 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2118 {
2119         int ret;
2120         ret = os_snprintf(pos, end - pos, "-");
2121         if (ret < 0 || ret >= end - pos)
2122                 return pos;
2123         pos += ret;
2124         ret = wpa_write_ciphers(pos, end, cipher, "+");
2125         if (ret < 0)
2126                 return pos;
2127         pos += ret;
2128         return pos;
2129 }
2130
2131
2132 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2133                                     const u8 *ie, size_t ie_len)
2134 {
2135         struct wpa_ie_data data;
2136         char *start;
2137         int ret;
2138
2139         ret = os_snprintf(pos, end - pos, "[%s-", proto);
2140         if (ret < 0 || ret >= end - pos)
2141                 return pos;
2142         pos += ret;
2143
2144         if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2145                 ret = os_snprintf(pos, end - pos, "?]");
2146                 if (ret < 0 || ret >= end - pos)
2147                         return pos;
2148                 pos += ret;
2149                 return pos;
2150         }
2151
2152         start = pos;
2153         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
2154                 ret = os_snprintf(pos, end - pos, "%sEAP",
2155                                   pos == start ? "" : "+");
2156                 if (ret < 0 || ret >= end - pos)
2157                         return pos;
2158                 pos += ret;
2159         }
2160         if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
2161                 ret = os_snprintf(pos, end - pos, "%sPSK",
2162                                   pos == start ? "" : "+");
2163                 if (ret < 0 || ret >= end - pos)
2164                         return pos;
2165                 pos += ret;
2166         }
2167         if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
2168                 ret = os_snprintf(pos, end - pos, "%sNone",
2169                                   pos == start ? "" : "+");
2170                 if (ret < 0 || ret >= end - pos)
2171                         return pos;
2172                 pos += ret;
2173         }
2174         if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
2175                 ret = os_snprintf(pos, end - pos, "%sSAE",
2176                                   pos == start ? "" : "+");
2177                 if (ret < 0 || ret >= end - pos)
2178                         return pos;
2179                 pos += ret;
2180         }
2181 #ifdef CONFIG_IEEE80211R
2182         if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2183                 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
2184                                   pos == start ? "" : "+");
2185                 if (ret < 0 || ret >= end - pos)
2186                         return pos;
2187                 pos += ret;
2188         }
2189         if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2190                 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
2191                                   pos == start ? "" : "+");
2192                 if (ret < 0 || ret >= end - pos)
2193                         return pos;
2194                 pos += ret;
2195         }
2196         if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2197                 ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2198                                   pos == start ? "" : "+");
2199                 if (ret < 0 || ret >= end - pos)
2200                         return pos;
2201                 pos += ret;
2202         }
2203 #endif /* CONFIG_IEEE80211R */
2204 #ifdef CONFIG_IEEE80211W
2205         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2206                 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
2207                                   pos == start ? "" : "+");
2208                 if (ret < 0 || ret >= end - pos)
2209                         return pos;
2210                 pos += ret;
2211         }
2212         if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2213                 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
2214                                   pos == start ? "" : "+");
2215                 if (ret < 0 || ret >= end - pos)
2216                         return pos;
2217                 pos += ret;
2218         }
2219 #endif /* CONFIG_IEEE80211W */
2220
2221         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2222                 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
2223                                   pos == start ? "" : "+");
2224                 if (ret < 0 || ret >= end - pos)
2225                         return pos;
2226                 pos += ret;
2227         }
2228
2229         pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2230
2231         if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2232                 ret = os_snprintf(pos, end - pos, "-preauth");
2233                 if (ret < 0 || ret >= end - pos)
2234                         return pos;
2235                 pos += ret;
2236         }
2237
2238         ret = os_snprintf(pos, end - pos, "]");
2239         if (ret < 0 || ret >= end - pos)
2240                 return pos;
2241         pos += ret;
2242
2243         return pos;
2244 }
2245
2246
2247 #ifdef CONFIG_WPS
2248 static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2249                                             char *pos, char *end,
2250                                             struct wpabuf *wps_ie)
2251 {
2252         int ret;
2253         const char *txt;
2254
2255         if (wps_ie == NULL)
2256                 return pos;
2257         if (wps_is_selected_pbc_registrar(wps_ie))
2258                 txt = "[WPS-PBC]";
2259         else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2260                 txt = "[WPS-AUTH]";
2261         else if (wps_is_selected_pin_registrar(wps_ie))
2262                 txt = "[WPS-PIN]";
2263         else
2264                 txt = "[WPS]";
2265
2266         ret = os_snprintf(pos, end - pos, "%s", txt);
2267         if (ret >= 0 && ret < end - pos)
2268                 pos += ret;
2269         wpabuf_free(wps_ie);
2270         return pos;
2271 }
2272 #endif /* CONFIG_WPS */
2273
2274
2275 static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2276                                         char *pos, char *end,
2277                                         const struct wpa_bss *bss)
2278 {
2279 #ifdef CONFIG_WPS
2280         struct wpabuf *wps_ie;
2281         wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2282         return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2283 #else /* CONFIG_WPS */
2284         return pos;
2285 #endif /* CONFIG_WPS */
2286 }
2287
2288
2289 /* Format one result on one text line into a buffer. */
2290 static int wpa_supplicant_ctrl_iface_scan_result(
2291         struct wpa_supplicant *wpa_s,
2292         const struct wpa_bss *bss, char *buf, size_t buflen)
2293 {
2294         char *pos, *end;
2295         int ret;
2296         const u8 *ie, *ie2, *p2p, *mesh;
2297
2298         mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
2299         p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
2300         if (!p2p)
2301                 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
2302         if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2303             os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2304             0)
2305                 return 0; /* Do not show P2P listen discovery results here */
2306
2307         pos = buf;
2308         end = buf + buflen;
2309
2310         ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2311                           MAC2STR(bss->bssid), bss->freq, bss->level);
2312         if (ret < 0 || ret >= end - pos)
2313                 return -1;
2314         pos += ret;
2315         ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2316         if (ie)
2317                 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2318         ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2319         if (ie2) {
2320                 pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
2321                                             ie2, 2 + ie2[1]);
2322         }
2323         pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2324         if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2325                 ret = os_snprintf(pos, end - pos, "[WEP]");
2326                 if (ret < 0 || ret >= end - pos)
2327                         return -1;
2328                 pos += ret;
2329         }
2330         if (mesh) {
2331                 ret = os_snprintf(pos, end - pos, "[MESH]");
2332                 if (ret < 0 || ret >= end - pos)
2333                         return -1;
2334                 pos += ret;
2335         }
2336         if (bss_is_dmg(bss)) {
2337                 const char *s;
2338                 ret = os_snprintf(pos, end - pos, "[DMG]");
2339                 if (ret < 0 || ret >= end - pos)
2340                         return -1;
2341                 pos += ret;
2342                 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
2343                 case IEEE80211_CAP_DMG_IBSS:
2344                         s = "[IBSS]";
2345                         break;
2346                 case IEEE80211_CAP_DMG_AP:
2347                         s = "[ESS]";
2348                         break;
2349                 case IEEE80211_CAP_DMG_PBSS:
2350                         s = "[PBSS]";
2351                         break;
2352                 default:
2353                         s = "";
2354                         break;
2355                 }
2356                 ret = os_snprintf(pos, end - pos, "%s", s);
2357                 if (ret < 0 || ret >= end - pos)
2358                         return -1;
2359                 pos += ret;
2360         } else {
2361                 if (bss->caps & IEEE80211_CAP_IBSS) {
2362                         ret = os_snprintf(pos, end - pos, "[IBSS]");
2363                         if (ret < 0 || ret >= end - pos)
2364                                 return -1;
2365                         pos += ret;
2366                 }
2367                 if (bss->caps & IEEE80211_CAP_ESS) {
2368                         ret = os_snprintf(pos, end - pos, "[ESS]");
2369                         if (ret < 0 || ret >= end - pos)
2370                                 return -1;
2371                         pos += ret;
2372                 }
2373         }
2374         if (p2p) {
2375                 ret = os_snprintf(pos, end - pos, "[P2P]");
2376                 if (ret < 0 || ret >= end - pos)
2377                         return -1;
2378                 pos += ret;
2379         }
2380 #ifdef CONFIG_HS20
2381         if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
2382                 ret = os_snprintf(pos, end - pos, "[HS20]");
2383                 if (ret < 0 || ret >= end - pos)
2384                         return -1;
2385                 pos += ret;
2386         }
2387 #endif /* CONFIG_HS20 */
2388
2389         ret = os_snprintf(pos, end - pos, "\t%s",
2390                           wpa_ssid_txt(bss->ssid, bss->ssid_len));
2391         if (ret < 0 || ret >= end - pos)
2392                 return -1;
2393         pos += ret;
2394
2395         ret = os_snprintf(pos, end - pos, "\n");
2396         if (ret < 0 || ret >= end - pos)
2397                 return -1;
2398         pos += ret;
2399
2400         return pos - buf;
2401 }
2402
2403
2404 static int wpa_supplicant_ctrl_iface_scan_results(
2405         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2406 {
2407         char *pos, *end;
2408         struct wpa_bss *bss;
2409         int ret;
2410
2411         pos = buf;
2412         end = buf + buflen;
2413         ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2414                           "flags / ssid\n");
2415         if (ret < 0 || ret >= end - pos)
2416                 return pos - buf;
2417         pos += ret;
2418
2419         dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2420                 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2421                                                             end - pos);
2422                 if (ret < 0 || ret >= end - pos)
2423                         return pos - buf;
2424                 pos += ret;
2425         }
2426
2427         return pos - buf;
2428 }
2429
2430
2431 #ifdef CONFIG_MESH
2432
2433 static int wpa_supplicant_ctrl_iface_mesh_group_add(
2434         struct wpa_supplicant *wpa_s, char *cmd)
2435 {
2436         int id;
2437         struct wpa_ssid *ssid;
2438
2439         id = atoi(cmd);
2440         wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
2441
2442         ssid = wpa_config_get_network(wpa_s->conf, id);
2443         if (ssid == NULL) {
2444                 wpa_printf(MSG_DEBUG,
2445                            "CTRL_IFACE: Could not find network id=%d", id);
2446                 return -1;
2447         }
2448         if (ssid->mode != WPAS_MODE_MESH) {
2449                 wpa_printf(MSG_DEBUG,
2450                            "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
2451                 return -1;
2452         }
2453         if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
2454             ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
2455                 wpa_printf(MSG_ERROR,
2456                            "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
2457                 return -1;
2458         }
2459
2460         /*
2461          * TODO: If necessary write our own group_add function,
2462          * for now we can reuse select_network
2463          */
2464         wpa_supplicant_select_network(wpa_s, ssid);
2465
2466         return 0;
2467 }
2468
2469
2470 static int wpa_supplicant_ctrl_iface_mesh_group_remove(
2471         struct wpa_supplicant *wpa_s, char *cmd)
2472 {
2473         if (!cmd) {
2474                 wpa_printf(MSG_ERROR,
2475                            "CTRL_IFACE: MESH_GROUP_REMOVE ifname cannot be empty");
2476                 return -1;
2477         }
2478
2479         /*
2480          * TODO: Support a multiple mesh and other iface type combinations
2481          */
2482         if (os_strcmp(cmd, wpa_s->ifname) != 0) {
2483                 wpa_printf(MSG_DEBUG,
2484                            "CTRL_IFACE: MESH_GROUP_REMOVE unknown interface name: %s",
2485                            cmd);
2486                 return -1;
2487         }
2488
2489         wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
2490
2491         wpa_s->reassociate = 0;
2492         wpa_s->disconnected = 1;
2493         wpa_supplicant_cancel_sched_scan(wpa_s);
2494         wpa_supplicant_cancel_scan(wpa_s);
2495
2496         /*
2497          * TODO: If necessary write our own group_remove function,
2498          * for now we can reuse deauthenticate
2499          */
2500         wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2501
2502         return 0;
2503 }
2504
2505 #endif /* CONFIG_MESH */
2506
2507
2508 static int wpa_supplicant_ctrl_iface_select_network(
2509         struct wpa_supplicant *wpa_s, char *cmd)
2510 {
2511         int id;
2512         struct wpa_ssid *ssid;
2513         char *pos;
2514
2515         /* cmd: "<network id>" or "any" */
2516         if (os_strncmp(cmd, "any", 3) == 0) {
2517                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2518                 ssid = NULL;
2519         } else {
2520                 id = atoi(cmd);
2521                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2522
2523                 ssid = wpa_config_get_network(wpa_s->conf, id);
2524                 if (ssid == NULL) {
2525                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2526                                    "network id=%d", id);
2527                         return -1;
2528                 }
2529                 if (ssid->disabled == 2) {
2530                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2531                                    "SELECT_NETWORK with persistent P2P group");
2532                         return -1;
2533                 }
2534         }
2535
2536         pos = os_strstr(cmd, " freq=");
2537         if (pos) {
2538                 int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
2539                 if (freqs) {
2540                         wpa_s->scan_req = MANUAL_SCAN_REQ;
2541                         os_free(wpa_s->manual_scan_freqs);
2542                         wpa_s->manual_scan_freqs = freqs;
2543                 }
2544         }
2545
2546         wpa_supplicant_select_network(wpa_s, ssid);
2547
2548         return 0;
2549 }
2550
2551
2552 static int wpa_supplicant_ctrl_iface_enable_network(
2553         struct wpa_supplicant *wpa_s, char *cmd)
2554 {
2555         int id;
2556         struct wpa_ssid *ssid;
2557
2558         /* cmd: "<network id>" or "all" */
2559         if (os_strcmp(cmd, "all") == 0) {
2560                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2561                 ssid = NULL;
2562         } else {
2563                 id = atoi(cmd);
2564                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2565
2566                 ssid = wpa_config_get_network(wpa_s->conf, id);
2567                 if (ssid == NULL) {
2568                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2569                                    "network id=%d", id);
2570                         return -1;
2571                 }
2572                 if (ssid->disabled == 2) {
2573                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2574                                    "ENABLE_NETWORK with persistent P2P group");
2575                         return -1;
2576                 }
2577
2578                 if (os_strstr(cmd, " no-connect")) {
2579                         ssid->disabled = 0;
2580                         return 0;
2581                 }
2582         }
2583         wpa_supplicant_enable_network(wpa_s, ssid);
2584
2585         return 0;
2586 }
2587
2588
2589 static int wpa_supplicant_ctrl_iface_disable_network(
2590         struct wpa_supplicant *wpa_s, char *cmd)
2591 {
2592         int id;
2593         struct wpa_ssid *ssid;
2594
2595         /* cmd: "<network id>" or "all" */
2596         if (os_strcmp(cmd, "all") == 0) {
2597                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2598                 ssid = NULL;
2599         } else {
2600                 id = atoi(cmd);
2601                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2602
2603                 ssid = wpa_config_get_network(wpa_s->conf, id);
2604                 if (ssid == NULL) {
2605                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2606                                    "network id=%d", id);
2607                         return -1;
2608                 }
2609                 if (ssid->disabled == 2) {
2610                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2611                                    "DISABLE_NETWORK with persistent P2P "
2612                                    "group");
2613                         return -1;
2614                 }
2615         }
2616         wpa_supplicant_disable_network(wpa_s, ssid);
2617
2618         return 0;
2619 }
2620
2621
2622 static int wpa_supplicant_ctrl_iface_add_network(
2623         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2624 {
2625         struct wpa_ssid *ssid;
2626         int ret;
2627
2628         wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2629
2630         ssid = wpa_config_add_network(wpa_s->conf);
2631         if (ssid == NULL)
2632                 return -1;
2633
2634         wpas_notify_network_added(wpa_s, ssid);
2635
2636         ssid->disabled = 1;
2637         wpa_config_set_network_defaults(ssid);
2638
2639         ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
2640         if (ret < 0 || (size_t) ret >= buflen)
2641                 return -1;
2642         return ret;
2643 }
2644
2645
2646 static int wpa_supplicant_ctrl_iface_remove_network(
2647         struct wpa_supplicant *wpa_s, char *cmd)
2648 {
2649         int id;
2650         struct wpa_ssid *ssid;
2651         int was_disabled;
2652
2653         /* cmd: "<network id>" or "all" */
2654         if (os_strcmp(cmd, "all") == 0) {
2655                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
2656                 if (wpa_s->sched_scanning)
2657                         wpa_supplicant_cancel_sched_scan(wpa_s);
2658
2659                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2660                 if (wpa_s->current_ssid) {
2661 #ifdef CONFIG_SME
2662                         wpa_s->sme.prev_bssid_set = 0;
2663 #endif /* CONFIG_SME */
2664                         wpa_sm_set_config(wpa_s->wpa, NULL);
2665                         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2666                         wpa_supplicant_deauthenticate(
2667                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2668                 }
2669                 ssid = wpa_s->conf->ssid;
2670                 while (ssid) {
2671                         struct wpa_ssid *remove_ssid = ssid;
2672                         id = ssid->id;
2673                         ssid = ssid->next;
2674                         if (wpa_s->last_ssid == remove_ssid)
2675                                 wpa_s->last_ssid = NULL;
2676                         wpas_notify_network_removed(wpa_s, remove_ssid);
2677                         wpa_config_remove_network(wpa_s->conf, id);
2678                 }
2679                 return 0;
2680         }
2681
2682         id = atoi(cmd);
2683         wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2684
2685         ssid = wpa_config_get_network(wpa_s->conf, id);
2686         if (ssid)
2687                 wpas_notify_network_removed(wpa_s, ssid);
2688         if (ssid == NULL) {
2689                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2690                            "id=%d", id);
2691                 return -1;
2692         }
2693
2694         if (wpa_s->last_ssid == ssid)
2695                 wpa_s->last_ssid = NULL;
2696
2697         if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
2698 #ifdef CONFIG_SME
2699                 wpa_s->sme.prev_bssid_set = 0;
2700 #endif /* CONFIG_SME */
2701                 /*
2702                  * Invalidate the EAP session cache if the current or
2703                  * previously used network is removed.
2704                  */
2705                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2706         }
2707
2708         if (ssid == wpa_s->current_ssid) {
2709                 wpa_sm_set_config(wpa_s->wpa, NULL);
2710                 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2711
2712                 wpa_supplicant_deauthenticate(wpa_s,
2713                                               WLAN_REASON_DEAUTH_LEAVING);
2714         }
2715
2716         was_disabled = ssid->disabled;
2717
2718         if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2719                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2720                            "network id=%d", id);
2721                 return -1;
2722         }
2723
2724         if (!was_disabled && wpa_s->sched_scanning) {
2725                 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2726                            "network from filters");
2727                 wpa_supplicant_cancel_sched_scan(wpa_s);
2728                 wpa_supplicant_req_scan(wpa_s, 0, 0);
2729         }
2730
2731         return 0;
2732 }
2733
2734
2735 static int wpa_supplicant_ctrl_iface_update_network(
2736         struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2737         char *name, char *value)
2738 {
2739         if (wpa_config_set(ssid, name, value, 0) < 0) {
2740                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2741                            "variable '%s'", name);
2742                 return -1;
2743         }
2744
2745         if (os_strcmp(name, "bssid") != 0 &&
2746             os_strcmp(name, "priority") != 0)
2747                 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
2748
2749         if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
2750                 /*
2751                  * Invalidate the EAP session cache if anything in the current
2752                  * or previously used configuration changes.
2753                  */
2754                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2755         }
2756
2757         if ((os_strcmp(name, "psk") == 0 &&
2758              value[0] == '"' && ssid->ssid_len) ||
2759             (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2760                 wpa_config_update_psk(ssid);
2761         else if (os_strcmp(name, "priority") == 0)
2762                 wpa_config_update_prio_list(wpa_s->conf);
2763         else if (os_strcmp(name, "no_auto_peer") == 0)
2764                 ssid->no_auto_peer = atoi(value);
2765
2766         return 0;
2767 }
2768
2769
2770 static int wpa_supplicant_ctrl_iface_set_network(
2771         struct wpa_supplicant *wpa_s, char *cmd)
2772 {
2773         int id, ret, prev_bssid_set;
2774         struct wpa_ssid *ssid;
2775         char *name, *value;
2776         u8 prev_bssid[ETH_ALEN];
2777
2778         /* cmd: "<network id> <variable name> <value>" */
2779         name = os_strchr(cmd, ' ');
2780         if (name == NULL)
2781                 return -1;
2782         *name++ = '\0';
2783
2784         value = os_strchr(name, ' ');
2785         if (value == NULL)
2786                 return -1;
2787         *value++ = '\0';
2788
2789         id = atoi(cmd);
2790         wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2791                    id, name);
2792         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2793                               (u8 *) value, os_strlen(value));
2794
2795         ssid = wpa_config_get_network(wpa_s->conf, id);
2796         if (ssid == NULL) {
2797                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2798                            "id=%d", id);
2799                 return -1;
2800         }
2801
2802         prev_bssid_set = ssid->bssid_set;
2803         os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
2804         ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
2805                                                        value);
2806         if (ret == 0 &&
2807             (ssid->bssid_set != prev_bssid_set ||
2808              os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
2809                 wpas_notify_network_bssid_set_changed(wpa_s, ssid);
2810         return ret;
2811 }
2812
2813
2814 static int wpa_supplicant_ctrl_iface_get_network(
2815         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2816 {
2817         int id;
2818         size_t res;
2819         struct wpa_ssid *ssid;
2820         char *name, *value;
2821
2822         /* cmd: "<network id> <variable name>" */
2823         name = os_strchr(cmd, ' ');
2824         if (name == NULL || buflen == 0)
2825                 return -1;
2826         *name++ = '\0';
2827
2828         id = atoi(cmd);
2829         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2830                    id, name);
2831
2832         ssid = wpa_config_get_network(wpa_s->conf, id);
2833         if (ssid == NULL) {
2834                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2835                            "id=%d", id);
2836                 return -1;
2837         }
2838
2839         value = wpa_config_get_no_key(ssid, name);
2840         if (value == NULL) {
2841                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2842                            "variable '%s'", name);
2843                 return -1;
2844         }
2845
2846         res = os_strlcpy(buf, value, buflen);
2847         if (res >= buflen) {
2848                 os_free(value);
2849                 return -1;
2850         }
2851
2852         os_free(value);
2853
2854         return res;
2855 }
2856
2857
2858 static int wpa_supplicant_ctrl_iface_dup_network(
2859         struct wpa_supplicant *wpa_s, char *cmd)
2860 {
2861         struct wpa_ssid *ssid_s, *ssid_d;
2862         char *name, *id, *value;
2863         int id_s, id_d, ret;
2864
2865         /* cmd: "<src network id> <dst network id> <variable name>" */
2866         id = os_strchr(cmd, ' ');
2867         if (id == NULL)
2868                 return -1;
2869         *id++ = '\0';
2870
2871         name = os_strchr(id, ' ');
2872         if (name == NULL)
2873                 return -1;
2874         *name++ = '\0';
2875
2876         id_s = atoi(cmd);
2877         id_d = atoi(id);
2878         wpa_printf(MSG_DEBUG, "CTRL_IFACE: DUP_NETWORK id=%d -> %d name='%s'",
2879                    id_s, id_d, name);
2880
2881         ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
2882         if (ssid_s == NULL) {
2883                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2884                            "network id=%d", id_s);
2885                 return -1;
2886         }
2887
2888         ssid_d = wpa_config_get_network(wpa_s->conf, id_d);
2889         if (ssid_d == NULL) {
2890                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2891                            "network id=%d", id_s);
2892                 return -1;
2893         }
2894
2895         value = wpa_config_get(ssid_s, name);
2896         if (value == NULL) {
2897                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2898                            "variable '%s'", name);
2899                 return -1;
2900         }
2901
2902         ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid_d, name,
2903                                                        value);
2904
2905         os_free(value);
2906
2907         return ret;
2908 }
2909
2910
2911 static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2912                                                 char *buf, size_t buflen)
2913 {
2914         char *pos, *end;
2915         struct wpa_cred *cred;
2916         int ret;
2917
2918         pos = buf;
2919         end = buf + buflen;
2920         ret = os_snprintf(pos, end - pos,
2921                           "cred id / realm / username / domain / imsi\n");
2922         if (ret < 0 || ret >= end - pos)
2923                 return pos - buf;
2924         pos += ret;
2925
2926         cred = wpa_s->conf->cred;
2927         while (cred) {
2928                 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2929                                   cred->id, cred->realm ? cred->realm : "",
2930                                   cred->username ? cred->username : "",
2931                                   cred->domain ? cred->domain[0] : "",
2932                                   cred->imsi ? cred->imsi : "");
2933                 if (ret < 0 || ret >= end - pos)
2934                         return pos - buf;
2935                 pos += ret;
2936
2937                 cred = cred->next;
2938         }
2939
2940         return pos - buf;
2941 }
2942
2943
2944 static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2945                                               char *buf, size_t buflen)
2946 {
2947         struct wpa_cred *cred;
2948         int ret;
2949
2950         wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2951
2952         cred = wpa_config_add_cred(wpa_s->conf);
2953         if (cred == NULL)
2954                 return -1;
2955
2956         wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
2957
2958         ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2959         if (ret < 0 || (size_t) ret >= buflen)
2960                 return -1;
2961         return ret;
2962 }
2963
2964
2965 static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2966                                  struct wpa_cred *cred)
2967 {
2968         struct wpa_ssid *ssid;
2969         char str[20];
2970         int id;
2971
2972         if (cred == NULL) {
2973                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2974                 return -1;
2975         }
2976
2977         id = cred->id;
2978         if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
2979                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2980                 return -1;
2981         }
2982
2983         wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
2984
2985         /* Remove any network entry created based on the removed credential */
2986         ssid = wpa_s->conf->ssid;
2987         while (ssid) {
2988                 if (ssid->parent_cred == cred) {
2989                         wpa_printf(MSG_DEBUG, "Remove network id %d since it "
2990                                    "used the removed credential", ssid->id);
2991                         os_snprintf(str, sizeof(str), "%d", ssid->id);
2992                         ssid = ssid->next;
2993                         wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
2994                 } else
2995                         ssid = ssid->next;
2996         }
2997
2998         return 0;
2999 }
3000
3001
3002 static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
3003                                                  char *cmd)
3004 {
3005         int id;
3006         struct wpa_cred *cred, *prev;
3007
3008         /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
3009          * "provisioning_sp=<FQDN> */
3010         if (os_strcmp(cmd, "all") == 0) {
3011                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
3012                 cred = wpa_s->conf->cred;
3013                 while (cred) {
3014                         prev = cred;
3015                         cred = cred->next;
3016                         wpas_ctrl_remove_cred(wpa_s, prev);
3017                 }
3018                 return 0;
3019         }
3020
3021         if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
3022                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
3023                            cmd + 8);
3024                 cred = wpa_s->conf->cred;
3025                 while (cred) {
3026                         prev = cred;
3027                         cred = cred->next;
3028                         if (prev->domain) {
3029                                 size_t i;
3030                                 for (i = 0; i < prev->num_domain; i++) {
3031                                         if (os_strcmp(prev->domain[i], cmd + 8)
3032                                             != 0)
3033                                                 continue;
3034                                         wpas_ctrl_remove_cred(wpa_s, prev);
3035                                         break;
3036                                 }
3037                         }
3038                 }
3039                 return 0;
3040         }
3041
3042         if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
3043                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
3044                            cmd + 16);
3045                 cred = wpa_s->conf->cred;
3046                 while (cred) {
3047                         prev = cred;
3048                         cred = cred->next;
3049                         if (prev->provisioning_sp &&
3050                             os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
3051                                 wpas_ctrl_remove_cred(wpa_s, prev);
3052                 }
3053                 return 0;
3054         }
3055
3056         id = atoi(cmd);
3057         wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
3058
3059         cred = wpa_config_get_cred(wpa_s->conf, id);
3060         return wpas_ctrl_remove_cred(wpa_s, cred);
3061 }
3062
3063
3064 static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
3065                                               char *cmd)
3066 {
3067         int id;
3068         struct wpa_cred *cred;
3069         char *name, *value;
3070
3071         /* cmd: "<cred id> <variable name> <value>" */
3072         name = os_strchr(cmd, ' ');
3073         if (name == NULL)
3074                 return -1;
3075         *name++ = '\0';
3076
3077         value = os_strchr(name, ' ');
3078         if (value == NULL)
3079                 return -1;
3080         *value++ = '\0';
3081
3082         id = atoi(cmd);
3083         wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
3084                    id, name);
3085         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3086                               (u8 *) value, os_strlen(value));
3087
3088         cred = wpa_config_get_cred(wpa_s->conf, id);
3089         if (cred == NULL) {
3090                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3091                            id);
3092                 return -1;
3093         }
3094
3095         if (wpa_config_set_cred(cred, name, value, 0) < 0) {
3096                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
3097                            "variable '%s'", name);
3098                 return -1;
3099         }
3100
3101         wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
3102
3103         return 0;
3104 }
3105
3106
3107 static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
3108                                               char *cmd, char *buf,
3109                                               size_t buflen)
3110 {
3111         int id;
3112         size_t res;
3113         struct wpa_cred *cred;
3114         char *name, *value;
3115
3116         /* cmd: "<cred id> <variable name>" */
3117         name = os_strchr(cmd, ' ');
3118         if (name == NULL)
3119                 return -1;
3120         *name++ = '\0';
3121
3122         id = atoi(cmd);
3123         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
3124                    id, name);
3125
3126         cred = wpa_config_get_cred(wpa_s->conf, id);
3127         if (cred == NULL) {
3128                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3129                            id);
3130                 return -1;
3131         }
3132
3133         value = wpa_config_get_cred_no_key(cred, name);
3134         if (value == NULL) {
3135                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
3136                            name);
3137                 return -1;
3138         }
3139
3140         res = os_strlcpy(buf, value, buflen);
3141         if (res >= buflen) {
3142                 os_free(value);
3143                 return -1;
3144         }
3145
3146         os_free(value);
3147
3148         return res;
3149 }
3150
3151
3152 #ifndef CONFIG_NO_CONFIG_WRITE
3153 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
3154 {
3155         int ret;
3156
3157         if (!wpa_s->conf->update_config) {
3158                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
3159                            "to update configuration (update_config=0)");
3160                 return -1;
3161         }
3162
3163         ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
3164         if (ret) {
3165                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
3166                            "update configuration");
3167         } else {
3168                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
3169                            " updated");
3170         }
3171
3172         return ret;
3173 }
3174 #endif /* CONFIG_NO_CONFIG_WRITE */
3175
3176
3177 struct cipher_info {
3178         unsigned int capa;
3179         const char *name;
3180         int group_only;
3181 };
3182
3183 static const struct cipher_info ciphers[] = {
3184         { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
3185         { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
3186         { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
3187         { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
3188         { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
3189         { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
3190         { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
3191         { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
3192 };
3193
3194
3195 static int ctrl_iface_get_capability_pairwise(int res, char *strict,
3196                                               struct wpa_driver_capa *capa,
3197                                               char *buf, size_t buflen)
3198 {
3199         int ret;
3200         char *pos, *end;
3201         size_t len;
3202         unsigned int i;
3203
3204         pos = buf;
3205         end = pos + buflen;
3206
3207         if (res < 0) {
3208                 if (strict)
3209                         return 0;
3210                 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
3211                 if (len >= buflen)
3212                         return -1;
3213                 return len;
3214         }
3215
3216         for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3217                 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
3218                         ret = os_snprintf(pos, end - pos, "%s%s",
3219                                           pos == buf ? "" : " ",
3220                                           ciphers[i].name);
3221                         if (ret < 0 || ret >= end - pos)
3222                                 return pos - buf;
3223                         pos += ret;
3224                 }
3225         }
3226
3227         return pos - buf;
3228 }
3229
3230
3231 static int ctrl_iface_get_capability_group(int res, char *strict,
3232                                            struct wpa_driver_capa *capa,
3233                                            char *buf, size_t buflen)
3234 {
3235         int ret;
3236         char *pos, *end;
3237         size_t len;
3238         unsigned int i;
3239
3240         pos = buf;
3241         end = pos + buflen;
3242
3243         if (res < 0) {
3244                 if (strict)
3245                         return 0;
3246                 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
3247                 if (len >= buflen)
3248                         return -1;
3249                 return len;
3250         }
3251
3252         for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3253                 if (capa->enc & ciphers[i].capa) {
3254                         ret = os_snprintf(pos, end - pos, "%s%s",
3255                                           pos == buf ? "" : " ",
3256                                           ciphers[i].name);
3257                         if (ret < 0 || ret >= end - pos)
3258                                 return pos - buf;
3259                         pos += ret;
3260                 }
3261         }
3262
3263         return pos - buf;
3264 }
3265
3266
3267 static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
3268                                               struct wpa_driver_capa *capa,
3269                                               char *buf, size_t buflen)
3270 {
3271         int ret;
3272         char *pos, *end;
3273         size_t len;
3274
3275         pos = buf;
3276         end = pos + buflen;
3277
3278         if (res < 0) {
3279                 if (strict)
3280                         return 0;
3281                 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
3282                                  "NONE", buflen);
3283                 if (len >= buflen)
3284                         return -1;
3285                 return len;
3286         }
3287
3288         ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
3289         if (ret < 0 || ret >= end - pos)
3290                 return pos - buf;
3291         pos += ret;
3292
3293         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3294                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
3295                 ret = os_snprintf(pos, end - pos, " WPA-EAP");
3296                 if (ret < 0 || ret >= end - pos)
3297                         return pos - buf;
3298                 pos += ret;
3299         }
3300
3301         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3302                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
3303                 ret = os_snprintf(pos, end - pos, " WPA-PSK");
3304                 if (ret < 0 || ret >= end - pos)
3305                         return pos - buf;
3306                 pos += ret;
3307         }
3308
3309         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
3310                 ret = os_snprintf(pos, end - pos, " WPA-NONE");
3311                 if (ret < 0 || ret >= end - pos)
3312                         return pos - buf;
3313                 pos += ret;
3314         }
3315
3316         return pos - buf;
3317 }
3318
3319
3320 static int ctrl_iface_get_capability_proto(int res, char *strict,
3321                                            struct wpa_driver_capa *capa,
3322                                            char *buf, size_t buflen)
3323 {
3324         int ret;
3325         char *pos, *end;
3326         size_t len;
3327
3328         pos = buf;
3329         end = pos + buflen;
3330
3331         if (res < 0) {
3332                 if (strict)
3333                         return 0;
3334                 len = os_strlcpy(buf, "RSN WPA", buflen);
3335                 if (len >= buflen)
3336                         return -1;
3337                 return len;
3338         }
3339
3340         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3341                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
3342                 ret = os_snprintf(pos, end - pos, "%sRSN",
3343                                   pos == buf ? "" : " ");
3344                 if (ret < 0 || ret >= end - pos)
3345                         return pos - buf;
3346                 pos += ret;
3347         }
3348
3349         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3350                               WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
3351                 ret = os_snprintf(pos, end - pos, "%sWPA",
3352                                   pos == buf ? "" : " ");
3353                 if (ret < 0 || ret >= end - pos)
3354                         return pos - buf;
3355                 pos += ret;
3356         }
3357
3358         return pos - buf;
3359 }
3360
3361
3362 static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
3363                                               struct wpa_driver_capa *capa,
3364                                               char *buf, size_t buflen)
3365 {
3366         int ret;
3367         char *pos, *end;
3368         size_t len;
3369
3370         pos = buf;
3371         end = pos + buflen;
3372
3373         if (res < 0) {
3374                 if (strict)
3375                         return 0;
3376                 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
3377                 if (len >= buflen)
3378                         return -1;
3379                 return len;
3380         }
3381
3382         if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
3383                 ret = os_snprintf(pos, end - pos, "%sOPEN",
3384                                   pos == buf ? "" : " ");
3385                 if (ret < 0 || ret >= end - pos)
3386                         return pos - buf;
3387                 pos += ret;
3388         }
3389
3390         if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
3391                 ret = os_snprintf(pos, end - pos, "%sSHARED",
3392                                   pos == buf ? "" : " ");
3393                 if (ret < 0 || ret >= end - pos)
3394                         return pos - buf;
3395                 pos += ret;
3396         }
3397
3398         if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
3399                 ret = os_snprintf(pos, end - pos, "%sLEAP",
3400                                   pos == buf ? "" : " ");
3401                 if (ret < 0 || ret >= end - pos)
3402                         return pos - buf;
3403                 pos += ret;
3404         }
3405
3406         return pos - buf;
3407 }
3408
3409
3410 static int ctrl_iface_get_capability_modes(int res, char *strict,
3411                                            struct wpa_driver_capa *capa,
3412                                            char *buf, size_t buflen)
3413 {
3414         int ret;
3415         char *pos, *end;
3416         size_t len;
3417
3418         pos = buf;
3419         end = pos + buflen;
3420
3421         if (res < 0) {
3422                 if (strict)
3423                         return 0;
3424                 len = os_strlcpy(buf, "IBSS AP", buflen);
3425                 if (len >= buflen)
3426                         return -1;
3427                 return len;
3428         }
3429
3430         if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
3431                 ret = os_snprintf(pos, end - pos, "%sIBSS",
3432                                   pos == buf ? "" : " ");
3433                 if (ret < 0 || ret >= end - pos)
3434                         return pos - buf;
3435                 pos += ret;
3436         }
3437
3438         if (capa->flags & WPA_DRIVER_FLAGS_AP) {
3439                 ret = os_snprintf(pos, end - pos, "%sAP",
3440                                   pos == buf ? "" : " ");
3441                 if (ret < 0 || ret >= end - pos)
3442                         return pos - buf;
3443                 pos += ret;
3444         }
3445
3446         return pos - buf;
3447 }
3448
3449
3450 static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
3451                                               char *buf, size_t buflen)
3452 {
3453         struct hostapd_channel_data *chnl;
3454         int ret, i, j;
3455         char *pos, *end, *hmode;
3456
3457         pos = buf;
3458         end = pos + buflen;
3459
3460         for (j = 0; j < wpa_s->hw.num_modes; j++) {
3461                 switch (wpa_s->hw.modes[j].mode) {
3462                 case HOSTAPD_MODE_IEEE80211B:
3463                         hmode = "B";
3464                         break;
3465                 case HOSTAPD_MODE_IEEE80211G:
3466                         hmode = "G";
3467                         break;
3468                 case HOSTAPD_MODE_IEEE80211A:
3469                         hmode = "A";
3470                         break;
3471                 case HOSTAPD_MODE_IEEE80211AD:
3472                         hmode = "AD";
3473                         break;
3474                 default:
3475                         continue;
3476                 }
3477                 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
3478                 if (ret < 0 || ret >= end - pos)
3479                         return pos - buf;
3480                 pos += ret;
3481                 chnl = wpa_s->hw.modes[j].channels;
3482                 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3483                         if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3484                                 continue;
3485                         ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
3486                         if (ret < 0 || ret >= end - pos)
3487                                 return pos - buf;
3488                         pos += ret;
3489                 }
3490                 ret = os_snprintf(pos, end - pos, "\n");
3491                 if (ret < 0 || ret >= end - pos)
3492                         return pos - buf;
3493                 pos += ret;
3494         }
3495
3496         return pos - buf;
3497 }
3498
3499
3500 static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3501                                           char *buf, size_t buflen)
3502 {
3503         struct hostapd_channel_data *chnl;
3504         int ret, i, j;
3505         char *pos, *end, *hmode;
3506
3507         pos = buf;
3508         end = pos + buflen;
3509
3510         for (j = 0; j < wpa_s->hw.num_modes; j++) {
3511                 switch (wpa_s->hw.modes[j].mode) {
3512                 case HOSTAPD_MODE_IEEE80211B:
3513                         hmode = "B";
3514                         break;
3515                 case HOSTAPD_MODE_IEEE80211G:
3516                         hmode = "G";
3517                         break;
3518                 case HOSTAPD_MODE_IEEE80211A:
3519                         hmode = "A";
3520                         break;
3521                 case HOSTAPD_MODE_IEEE80211AD:
3522                         hmode = "AD";
3523                         break;
3524                 default:
3525                         continue;
3526                 }
3527                 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3528                                   hmode);
3529                 if (ret < 0 || ret >= end - pos)
3530                         return pos - buf;
3531                 pos += ret;
3532                 chnl = wpa_s->hw.modes[j].channels;
3533                 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3534                         if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3535                                 continue;
3536                         ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
3537                                           chnl[i].chan, chnl[i].freq,
3538                                           chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
3539                                           " (NO_IR)" : "",
3540                                           chnl[i].flag & HOSTAPD_CHAN_RADAR ?
3541                                           " (DFS)" : "");
3542
3543                         if (ret < 0 || ret >= end - pos)
3544                                 return pos - buf;
3545                         pos += ret;
3546                 }
3547                 ret = os_snprintf(pos, end - pos, "\n");
3548                 if (ret < 0 || ret >= end - pos)
3549                         return pos - buf;
3550                 pos += ret;
3551         }
3552
3553         return pos - buf;
3554 }
3555
3556
3557 static int wpa_supplicant_ctrl_iface_get_capability(
3558         struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3559         size_t buflen)
3560 {
3561         struct wpa_driver_capa capa;
3562         int res;
3563         char *strict;
3564         char field[30];
3565         size_t len;
3566
3567         /* Determine whether or not strict checking was requested */
3568         len = os_strlcpy(field, _field, sizeof(field));
3569         if (len >= sizeof(field))
3570                 return -1;
3571         strict = os_strchr(field, ' ');
3572         if (strict != NULL) {
3573                 *strict++ = '\0';
3574                 if (os_strcmp(strict, "strict") != 0)
3575                         return -1;
3576         }
3577
3578         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3579                 field, strict ? strict : "");
3580
3581         if (os_strcmp(field, "eap") == 0) {
3582                 return eap_get_names(buf, buflen);
3583         }
3584
3585         res = wpa_drv_get_capa(wpa_s, &capa);
3586
3587         if (os_strcmp(field, "pairwise") == 0)
3588                 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3589                                                           buf, buflen);
3590
3591         if (os_strcmp(field, "group") == 0)
3592                 return ctrl_iface_get_capability_group(res, strict, &capa,
3593                                                        buf, buflen);
3594
3595         if (os_strcmp(field, "key_mgmt") == 0)
3596                 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3597                                                           buf, buflen);
3598
3599         if (os_strcmp(field, "proto") == 0)
3600                 return ctrl_iface_get_capability_proto(res, strict, &capa,
3601                                                        buf, buflen);
3602
3603         if (os_strcmp(field, "auth_alg") == 0)
3604                 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
3605                                                           buf, buflen);
3606
3607         if (os_strcmp(field, "modes") == 0)
3608                 return ctrl_iface_get_capability_modes(res, strict, &capa,
3609                                                        buf, buflen);
3610
3611         if (os_strcmp(field, "channels") == 0)
3612                 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3613
3614         if (os_strcmp(field, "freq") == 0)
3615                 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3616
3617 #ifdef CONFIG_TDLS
3618         if (os_strcmp(field, "tdls") == 0)
3619                 return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
3620 #endif /* CONFIG_TDLS */
3621
3622 #ifdef CONFIG_ERP
3623         if (os_strcmp(field, "erp") == 0) {
3624                 res = os_snprintf(buf, buflen, "ERP");
3625                 if (res < 0 || (unsigned int) res >= buflen)
3626                         return -1;
3627                 return res;
3628         }
3629 #endif /* CONFIG_EPR */
3630
3631         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3632                    field);
3633
3634         return -1;
3635 }
3636
3637
3638 #ifdef CONFIG_INTERWORKING
3639 static char * anqp_add_hex(char *pos, char *end, const char *title,
3640                            struct wpabuf *data)
3641 {
3642         char *start = pos;
3643         size_t i;
3644         int ret;
3645         const u8 *d;
3646
3647         if (data == NULL)
3648                 return start;
3649
3650         ret = os_snprintf(pos, end - pos, "%s=", title);
3651         if (ret < 0 || ret >= end - pos)
3652                 return start;
3653         pos += ret;
3654
3655         d = wpabuf_head_u8(data);
3656         for (i = 0; i < wpabuf_len(data); i++) {
3657                 ret = os_snprintf(pos, end - pos, "%02x", *d++);
3658                 if (ret < 0 || ret >= end - pos)
3659                         return start;
3660                 pos += ret;
3661         }
3662
3663         ret = os_snprintf(pos, end - pos, "\n");
3664         if (ret < 0 || ret >= end - pos)
3665                 return start;
3666         pos += ret;
3667
3668         return pos;
3669 }
3670 #endif /* CONFIG_INTERWORKING */
3671
3672
3673 static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3674                           unsigned long mask, char *buf, size_t buflen)
3675 {
3676         size_t i;
3677         int ret;
3678         char *pos, *end;
3679         const u8 *ie, *ie2;
3680
3681         pos = buf;
3682         end = buf + buflen;
3683
3684         if (mask & WPA_BSS_MASK_ID) {
3685                 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
3686                 if (ret < 0 || ret >= end - pos)
3687                         return 0;
3688                 pos += ret;
3689         }
3690
3691         if (mask & WPA_BSS_MASK_BSSID) {
3692                 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3693                                   MAC2STR(bss->bssid));
3694                 if (ret < 0 || ret >= end - pos)
3695                         return 0;
3696                 pos += ret;
3697         }
3698
3699         if (mask & WPA_BSS_MASK_FREQ) {
3700                 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
3701                 if (ret < 0 || ret >= end - pos)
3702                         return 0;
3703                 pos += ret;
3704         }
3705
3706         if (mask & WPA_BSS_MASK_BEACON_INT) {
3707                 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3708                                   bss->beacon_int);
3709                 if (ret < 0 || ret >= end - pos)
3710                         return 0;
3711                 pos += ret;
3712         }
3713
3714         if (mask & WPA_BSS_MASK_CAPABILITIES) {
3715                 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3716                                   bss->caps);
3717                 if (ret < 0 || ret >= end - pos)
3718                         return 0;
3719                 pos += ret;
3720         }
3721
3722         if (mask & WPA_BSS_MASK_QUAL) {
3723                 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
3724                 if (ret < 0 || ret >= end - pos)
3725                         return 0;
3726                 pos += ret;
3727         }
3728
3729         if (mask & WPA_BSS_MASK_NOISE) {
3730                 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
3731                 if (ret < 0 || ret >= end - pos)
3732                         return 0;
3733                 pos += ret;
3734         }
3735
3736         if (mask & WPA_BSS_MASK_LEVEL) {
3737                 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
3738                 if (ret < 0 || ret >= end - pos)
3739                         return 0;
3740                 pos += ret;
3741         }
3742
3743         if (mask & WPA_BSS_MASK_TSF) {
3744                 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3745                                   (unsigned long long) bss->tsf);
3746                 if (ret < 0 || ret >= end - pos)
3747                         return 0;
3748                 pos += ret;
3749         }
3750
3751         if (mask & WPA_BSS_MASK_AGE) {
3752                 struct os_reltime now;
3753
3754                 os_get_reltime(&now);
3755                 ret = os_snprintf(pos, end - pos, "age=%d\n",
3756                                   (int) (now.sec - bss->last_update.sec));
3757                 if (ret < 0 || ret >= end - pos)
3758                         return 0;
3759                 pos += ret;
3760         }
3761
3762         if (mask & WPA_BSS_MASK_IE) {
3763                 ret = os_snprintf(pos, end - pos, "ie=");
3764                 if (ret < 0 || ret >= end - pos)
3765                         return 0;
3766                 pos += ret;
3767
3768                 ie = (const u8 *) (bss + 1);
3769                 for (i = 0; i < bss->ie_len; i++) {
3770                         ret = os_snprintf(pos, end - pos, "%02x", *ie++);
3771                         if (ret < 0 || ret >= end - pos)
3772                                 return 0;
3773                         pos += ret;
3774                 }
3775
3776                 ret = os_snprintf(pos, end - pos, "\n");
3777                 if (ret < 0 || ret >= end - pos)
3778                         return 0;
3779                 pos += ret;
3780         }
3781
3782         if (mask & WPA_BSS_MASK_FLAGS) {
3783                 ret = os_snprintf(pos, end - pos, "flags=");
3784                 if (ret < 0 || ret >= end - pos)
3785                         return 0;
3786                 pos += ret;
3787
3788                 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3789                 if (ie)
3790                         pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3791                                                     2 + ie[1]);
3792                 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3793                 if (ie2)
3794                         pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3795                                                     2 + ie2[1]);
3796                 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3797                 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3798                         ret = os_snprintf(pos, end - pos, "[WEP]");
3799                         if (ret < 0 || ret >= end - pos)
3800                                 return 0;
3801                         pos += ret;
3802                 }
3803                 if (bss_is_dmg(bss)) {
3804                         const char *s;
3805                         ret = os_snprintf(pos, end - pos, "[DMG]");
3806                         if (ret < 0 || ret >= end - pos)
3807                                 return 0;
3808                         pos += ret;
3809                         switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
3810                         case IEEE80211_CAP_DMG_IBSS:
3811                                 s = "[IBSS]";
3812                                 break;
3813                         case IEEE80211_CAP_DMG_AP:
3814                                 s = "[ESS]";
3815                                 break;
3816                         case IEEE80211_CAP_DMG_PBSS:
3817                                 s = "[PBSS]";
3818                                 break;
3819                         default:
3820                                 s = "";
3821                                 break;
3822                         }
3823                         ret = os_snprintf(pos, end - pos, "%s", s);
3824                         if (ret < 0 || ret >= end - pos)
3825                                 return 0;
3826                         pos += ret;
3827                 } else {
3828                         if (bss->caps & IEEE80211_CAP_IBSS) {
3829                                 ret = os_snprintf(pos, end - pos, "[IBSS]");
3830                                 if (ret < 0 || ret >= end - pos)
3831                                         return 0;
3832                                 pos += ret;
3833                         }
3834                         if (bss->caps & IEEE80211_CAP_ESS) {
3835                                 ret = os_snprintf(pos, end - pos, "[ESS]");
3836                                 if (ret < 0 || ret >= end - pos)
3837                                         return 0;
3838                                 pos += ret;
3839                         }
3840                 }
3841                 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
3842                     wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
3843                         ret = os_snprintf(pos, end - pos, "[P2P]");
3844                         if (ret < 0 || ret >= end - pos)
3845                                 return 0;
3846                         pos += ret;
3847                 }
3848 #ifdef CONFIG_HS20
3849                 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3850                         ret = os_snprintf(pos, end - pos, "[HS20]");
3851                         if (ret < 0 || ret >= end - pos)
3852                                 return 0;
3853                         pos += ret;
3854                 }
3855 #endif /* CONFIG_HS20 */
3856
3857                 ret = os_snprintf(pos, end - pos, "\n");
3858                 if (ret < 0 || ret >= end - pos)
3859                         return 0;
3860                 pos += ret;
3861         }
3862
3863         if (mask & WPA_BSS_MASK_SSID) {
3864                 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3865                                   wpa_ssid_txt(bss->ssid, bss->ssid_len));
3866                 if (ret < 0 || ret >= end - pos)
3867                         return 0;
3868                 pos += ret;
3869         }
3870
3871 #ifdef CONFIG_WPS
3872         if (mask & WPA_BSS_MASK_WPS_SCAN) {
3873                 ie = (const u8 *) (bss + 1);
3874                 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3875                 if (ret < 0 || ret >= end - pos)
3876                         return 0;
3877                 pos += ret;
3878         }
3879 #endif /* CONFIG_WPS */
3880
3881 #ifdef CONFIG_P2P
3882         if (mask & WPA_BSS_MASK_P2P_SCAN) {
3883                 ie = (const u8 *) (bss + 1);
3884                 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3885                 if (ret < 0 || ret >= end - pos)
3886                         return 0;
3887                 pos += ret;
3888         }
3889 #endif /* CONFIG_P2P */
3890
3891 #ifdef CONFIG_WIFI_DISPLAY
3892         if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3893                 struct wpabuf *wfd;
3894                 ie = (const u8 *) (bss + 1);
3895                 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3896                                                   WFD_IE_VENDOR_TYPE);
3897                 if (wfd) {
3898                         ret = os_snprintf(pos, end - pos, "wfd_subelems=");
3899                         if (ret < 0 || ret >= end - pos) {
3900                                 wpabuf_free(wfd);
3901                                 return 0;
3902                         }
3903                         pos += ret;
3904
3905                         pos += wpa_snprintf_hex(pos, end - pos,
3906                                                 wpabuf_head(wfd),
3907                                                 wpabuf_len(wfd));
3908                         wpabuf_free(wfd);
3909
3910                         ret = os_snprintf(pos, end - pos, "\n");
3911                         if (ret < 0 || ret >= end - pos)
3912                                 return 0;
3913                         pos += ret;
3914                 }
3915         }
3916 #endif /* CONFIG_WIFI_DISPLAY */
3917
3918 #ifdef CONFIG_INTERWORKING
3919         if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3920                 struct wpa_bss_anqp *anqp = bss->anqp;
3921                 pos = anqp_add_hex(pos, end, "anqp_venue_name",
3922                                    anqp->venue_name);
3923                 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
3924                                    anqp->network_auth_type);
3925                 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
3926                                    anqp->roaming_consortium);
3927                 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
3928                                    anqp->ip_addr_type_availability);
3929                 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
3930                                    anqp->nai_realm);
3931                 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
3932                 pos = anqp_add_hex(pos, end, "anqp_domain_name",
3933                                    anqp->domain_name);
3934 #ifdef CONFIG_HS20
3935                 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
3936                                    anqp->hs20_operator_friendly_name);
3937                 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
3938                                    anqp->hs20_wan_metrics);
3939                 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
3940                                    anqp->hs20_connection_capability);
3941                 pos = anqp_add_hex(pos, end, "hs20_operating_class",
3942                                    anqp->hs20_operating_class);
3943                 pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
3944                                    anqp->hs20_osu_providers_list);
3945 #endif /* CONFIG_HS20 */
3946         }
3947 #endif /* CONFIG_INTERWORKING */
3948
3949 #ifdef CONFIG_MESH
3950         if (mask & WPA_BSS_MASK_MESH_SCAN) {
3951                 ie = (const u8 *) (bss + 1);
3952                 ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
3953                 if (ret < 0 || ret >= end - pos)
3954                         return 0;
3955                 pos += ret;
3956         }
3957 #endif /* CONFIG_MESH */
3958
3959         if (mask & WPA_BSS_MASK_DELIM) {
3960                 ret = os_snprintf(pos, end - pos, "====\n");
3961                 if (ret < 0 || ret >= end - pos)
3962                         return 0;
3963                 pos += ret;
3964         }
3965
3966         return pos - buf;
3967 }
3968
3969
3970 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
3971                                          const char *cmd, char *buf,
3972                                          size_t buflen)
3973 {
3974         u8 bssid[ETH_ALEN];
3975         size_t i;
3976         struct wpa_bss *bss;
3977         struct wpa_bss *bsslast = NULL;
3978         struct dl_list *next;
3979         int ret = 0;
3980         int len;
3981         char *ctmp;
3982         unsigned long mask = WPA_BSS_MASK_ALL;
3983
3984         if (os_strncmp(cmd, "RANGE=", 6) == 0) {
3985                 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
3986                         bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
3987                                             list_id);
3988                         bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
3989                                                list_id);
3990                 } else { /* N1-N2 */
3991                         unsigned int id1, id2;
3992
3993                         if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
3994                                 wpa_printf(MSG_INFO, "Wrong BSS range "
3995                                            "format");
3996                                 return 0;
3997                         }
3998
3999                         if (*(cmd + 6) == '-')
4000                                 id1 = 0;
4001                         else
4002                                 id1 = atoi(cmd + 6);
4003                         ctmp++;
4004                         if (*ctmp >= '0' && *ctmp <= '9')
4005                                 id2 = atoi(ctmp);
4006                         else
4007                                 id2 = (unsigned int) -1;
4008                         bss = wpa_bss_get_id_range(wpa_s, id1, id2);
4009                         if (id2 == (unsigned int) -1)
4010                                 bsslast = dl_list_last(&wpa_s->bss_id,
4011                                                        struct wpa_bss,
4012                                                        list_id);
4013                         else {
4014                                 bsslast = wpa_bss_get_id(wpa_s, id2);
4015                                 if (bsslast == NULL && bss && id2 > id1) {
4016                                         struct wpa_bss *tmp = bss;
4017                                         for (;;) {
4018                                                 next = tmp->list_id.next;
4019                                                 if (next == &wpa_s->bss_id)
4020                                                         break;
4021                                                 tmp = dl_list_entry(
4022                                                         next, struct wpa_bss,
4023                                                         list_id);
4024                                                 if (tmp->id > id2)
4025                                                         break;
4026                                                 bsslast = tmp;
4027                                         }
4028                                 }
4029                         }
4030                 }
4031         } else if (os_strncmp(cmd, "FIRST", 5) == 0)
4032                 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
4033         else if (os_strncmp(cmd, "LAST", 4) == 0)
4034                 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
4035         else if (os_strncmp(cmd, "ID-", 3) == 0) {
4036                 i = atoi(cmd + 3);
4037                 bss = wpa_bss_get_id(wpa_s, i);
4038         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4039                 i = atoi(cmd + 5);
4040                 bss = wpa_bss_get_id(wpa_s, i);
4041                 if (bss) {
4042                         next = bss->list_id.next;
4043                         if (next == &wpa_s->bss_id)
4044                                 bss = NULL;
4045                         else
4046                                 bss = dl_list_entry(next, struct wpa_bss,
4047                                                     list_id);
4048                 }
4049 #ifdef CONFIG_P2P
4050         } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
4051                 if (hwaddr_aton(cmd + 13, bssid) == 0)
4052                         bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
4053                 else
4054                         bss = NULL;
4055 #endif /* CONFIG_P2P */
4056         } else if (hwaddr_aton(cmd, bssid) == 0)
4057                 bss = wpa_bss_get_bssid(wpa_s, bssid);
4058         else {
4059                 struct wpa_bss *tmp;
4060                 i = atoi(cmd);
4061                 bss = NULL;
4062                 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
4063                 {
4064                         if (i-- == 0) {
4065                                 bss = tmp;
4066                                 break;
4067                         }
4068                 }
4069         }
4070
4071         if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
4072                 mask = strtoul(ctmp + 5, NULL, 0x10);
4073                 if (mask == 0)
4074                         mask = WPA_BSS_MASK_ALL;
4075         }
4076
4077         if (bss == NULL)
4078                 return 0;
4079
4080         if (bsslast == NULL)
4081                 bsslast = bss;
4082         do {
4083                 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
4084                 ret += len;
4085                 buf += len;
4086                 buflen -= len;
4087                 if (bss == bsslast) {
4088                         if ((mask & WPA_BSS_MASK_DELIM) && len &&
4089                             (bss == dl_list_last(&wpa_s->bss_id,
4090                                                  struct wpa_bss, list_id)))
4091                                 os_snprintf(buf - 5, 5, "####\n");
4092                         break;
4093                 }
4094                 next = bss->list_id.next;
4095                 if (next == &wpa_s->bss_id)
4096                         break;
4097                 bss = dl_list_entry(next, struct wpa_bss, list_id);
4098         } while (bss && len);
4099
4100         return ret;
4101 }
4102
4103
4104 static int wpa_supplicant_ctrl_iface_ap_scan(
4105         struct wpa_supplicant *wpa_s, char *cmd)
4106 {
4107         int ap_scan = atoi(cmd);
4108         return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
4109 }
4110
4111
4112 static int wpa_supplicant_ctrl_iface_scan_interval(
4113         struct wpa_supplicant *wpa_s, char *cmd)
4114 {
4115         int scan_int = atoi(cmd);
4116         return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
4117 }
4118
4119
4120 static int wpa_supplicant_ctrl_iface_bss_expire_age(
4121         struct wpa_supplicant *wpa_s, char *cmd)
4122 {
4123         int expire_age = atoi(cmd);
4124         return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
4125 }
4126
4127
4128 static int wpa_supplicant_ctrl_iface_bss_expire_count(
4129         struct wpa_supplicant *wpa_s, char *cmd)
4130 {
4131         int expire_count = atoi(cmd);
4132         return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
4133 }
4134
4135
4136 static int wpa_supplicant_ctrl_iface_bss_flush(
4137         struct wpa_supplicant *wpa_s, char *cmd)
4138 {
4139         int flush_age = atoi(cmd);
4140
4141         if (flush_age == 0)
4142                 wpa_bss_flush(wpa_s);
4143         else
4144                 wpa_bss_flush_by_age(wpa_s, flush_age);
4145         return 0;
4146 }
4147
4148
4149 #ifdef CONFIG_TESTING_OPTIONS
4150 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
4151 {
4152         wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
4153         /* MLME-DELETEKEYS.request */
4154         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
4155         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
4156         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
4157         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
4158 #ifdef CONFIG_IEEE80211W
4159         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
4160         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
4161 #endif /* CONFIG_IEEE80211W */
4162
4163         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
4164                         0);
4165         /* MLME-SETPROTECTION.request(None) */
4166         wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
4167                                    MLME_SETPROTECTION_PROTECT_TYPE_NONE,
4168                                    MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
4169         wpa_sm_drop_sa(wpa_s->wpa);
4170 }
4171 #endif /* CONFIG_TESTING_OPTIONS */
4172
4173
4174 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
4175                                           char *addr)
4176 {
4177 #ifdef CONFIG_NO_SCAN_PROCESSING
4178         return -1;
4179 #else /* CONFIG_NO_SCAN_PROCESSING */
4180         u8 bssid[ETH_ALEN];
4181         struct wpa_bss *bss;
4182         struct wpa_ssid *ssid = wpa_s->current_ssid;
4183
4184         if (hwaddr_aton(addr, bssid)) {
4185                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
4186                            "address '%s'", addr);
4187                 return -1;
4188         }
4189
4190         wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
4191
4192         if (!ssid) {
4193                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
4194                            "configuration known for the target AP");
4195                 return -1;
4196         }
4197
4198         bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
4199         if (!bss) {
4200                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
4201                            "from BSS table");
4202                 return -1;
4203         }
4204
4205         /*
4206          * TODO: Find best network configuration block from configuration to
4207          * allow roaming to other networks
4208          */
4209
4210         wpa_s->reassociate = 1;
4211         wpa_supplicant_connect(wpa_s, bss, ssid);
4212
4213         return 0;
4214 #endif /* CONFIG_NO_SCAN_PROCESSING */
4215 }
4216
4217
4218 #ifdef CONFIG_P2P
4219 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
4220 {
4221         unsigned int timeout = atoi(cmd);
4222         enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
4223         u8 dev_id[ETH_ALEN], *_dev_id = NULL;
4224         u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
4225         char *pos;
4226         unsigned int search_delay;
4227
4228         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4229                 wpa_dbg(wpa_s, MSG_INFO,
4230                         "Reject P2P_FIND since interface is disabled");
4231                 return -1;
4232         }
4233         if (os_strstr(cmd, "type=social"))
4234                 type = P2P_FIND_ONLY_SOCIAL;
4235         else if (os_strstr(cmd, "type=progressive"))
4236                 type = P2P_FIND_PROGRESSIVE;
4237
4238         pos = os_strstr(cmd, "dev_id=");
4239         if (pos) {
4240                 pos += 7;
4241                 if (hwaddr_aton(pos, dev_id))
4242                         return -1;
4243                 _dev_id = dev_id;
4244         }
4245
4246         pos = os_strstr(cmd, "dev_type=");
4247         if (pos) {
4248                 pos += 9;
4249                 if (wps_dev_type_str2bin(pos, dev_type) < 0)
4250                         return -1;
4251                 _dev_type = dev_type;
4252         }
4253
4254         pos = os_strstr(cmd, "delay=");
4255         if (pos) {
4256                 pos += 6;
4257                 search_delay = atoi(pos);
4258         } else
4259                 search_delay = wpas_p2p_search_delay(wpa_s);
4260
4261         return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
4262                              _dev_id, search_delay);
4263 }
4264
4265
4266 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
4267                             char *buf, size_t buflen)
4268 {
4269         u8 addr[ETH_ALEN];
4270         char *pos, *pos2;
4271         char *pin = NULL;
4272         enum p2p_wps_method wps_method;
4273         int new_pin;
4274         int ret;
4275         int persistent_group, persistent_id = -1;
4276         int join;
4277         int auth;
4278         int automatic;
4279         int go_intent = -1;
4280         int freq = 0;
4281         int pd;
4282         int ht40, vht;
4283
4284         /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
4285          * [persistent|persistent=<network id>]
4286          * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
4287          * [ht40] [vht] */
4288
4289         if (hwaddr_aton(cmd, addr))
4290                 return -1;
4291
4292         pos = cmd + 17;
4293         if (*pos != ' ')
4294                 return -1;
4295         pos++;
4296
4297         persistent_group = os_strstr(pos, " persistent") != NULL;
4298         pos2 = os_strstr(pos, " persistent=");
4299         if (pos2) {
4300                 struct wpa_ssid *ssid;
4301                 persistent_id = atoi(pos2 + 12);
4302                 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4303                 if (ssid == NULL || ssid->disabled != 2 ||
4304                     ssid->mode != WPAS_MODE_P2P_GO) {
4305                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
4306                                    "SSID id=%d for persistent P2P group (GO)",
4307                                    persistent_id);
4308                         return -1;
4309                 }
4310         }
4311         join = os_strstr(pos, " join") != NULL;
4312         auth = os_strstr(pos, " auth") != NULL;
4313         automatic = os_strstr(pos, " auto") != NULL;
4314         pd = os_strstr(pos, " provdisc") != NULL;
4315         vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4316         ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4317                 vht;
4318
4319         pos2 = os_strstr(pos, " go_intent=");
4320         if (pos2) {
4321                 pos2 += 11;
4322                 go_intent = atoi(pos2);
4323                 if (go_intent < 0 || go_intent > 15)
4324                         return -1;
4325         }
4326
4327         pos2 = os_strstr(pos, " freq=");
4328         if (pos2) {
4329                 pos2 += 6;
4330                 freq = atoi(pos2);
4331                 if (freq <= 0)
4332                         return -1;
4333         }
4334
4335         if (os_strncmp(pos, "pin", 3) == 0) {
4336                 /* Request random PIN (to be displayed) and enable the PIN */
4337                 wps_method = WPS_PIN_DISPLAY;
4338         } else if (os_strncmp(pos, "pbc", 3) == 0) {
4339                 wps_method = WPS_PBC;
4340         } else {
4341                 pin = pos;
4342                 pos = os_strchr(pin, ' ');
4343                 wps_method = WPS_PIN_KEYPAD;
4344                 if (pos) {
4345                         *pos++ = '\0';
4346                         if (os_strncmp(pos, "display", 7) == 0)
4347                                 wps_method = WPS_PIN_DISPLAY;
4348                 }
4349                 if (!wps_pin_str_valid(pin)) {
4350                         os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
4351                         return 17;
4352                 }
4353         }
4354
4355         new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
4356                                    persistent_group, automatic, join,
4357                                    auth, go_intent, freq, persistent_id, pd,
4358                                    ht40, vht);
4359         if (new_pin == -2) {
4360                 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
4361                 return 25;
4362         }
4363         if (new_pin == -3) {
4364                 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
4365                 return 25;
4366         }
4367         if (new_pin < 0)
4368                 return -1;
4369         if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
4370                 ret = os_snprintf(buf, buflen, "%08d", new_pin);
4371                 if (ret < 0 || (size_t) ret >= buflen)
4372                         return -1;
4373                 return ret;
4374         }
4375
4376         os_memcpy(buf, "OK\n", 3);
4377         return 3;
4378 }
4379
4380
4381 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
4382 {
4383         unsigned int timeout = atoi(cmd);
4384         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4385                 wpa_dbg(wpa_s, MSG_INFO,
4386                         "Reject P2P_LISTEN since interface is disabled");
4387                 return -1;
4388         }
4389         return wpas_p2p_listen(wpa_s, timeout);
4390 }
4391
4392
4393 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
4394 {
4395         u8 addr[ETH_ALEN];
4396         char *pos;
4397         enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
4398
4399         /* <addr> <config method> [join|auto] */
4400
4401         if (hwaddr_aton(cmd, addr))
4402                 return -1;
4403
4404         pos = cmd + 17;
4405         if (*pos != ' ')
4406                 return -1;
4407         pos++;
4408
4409         if (os_strstr(pos, " join") != NULL)
4410                 use = WPAS_P2P_PD_FOR_JOIN;
4411         else if (os_strstr(pos, " auto") != NULL)
4412                 use = WPAS_P2P_PD_AUTO;
4413
4414         return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
4415 }
4416
4417
4418 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
4419                               size_t buflen)
4420 {
4421         struct wpa_ssid *ssid = wpa_s->current_ssid;
4422
4423         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
4424             ssid->passphrase == NULL)
4425                 return -1;
4426
4427         os_strlcpy(buf, ssid->passphrase, buflen);
4428         return os_strlen(buf);
4429 }
4430
4431
4432 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
4433                                   char *buf, size_t buflen)
4434 {
4435         u64 ref;
4436         int res;
4437         u8 dst_buf[ETH_ALEN], *dst;
4438         struct wpabuf *tlvs;
4439         char *pos;
4440         size_t len;
4441
4442         if (hwaddr_aton(cmd, dst_buf))
4443                 return -1;
4444         dst = dst_buf;
4445         if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
4446             dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
4447                 dst = NULL;
4448         pos = cmd + 17;
4449         if (*pos != ' ')
4450                 return -1;
4451         pos++;
4452
4453         if (os_strncmp(pos, "upnp ", 5) == 0) {
4454                 u8 version;
4455                 pos += 5;
4456                 if (hexstr2bin(pos, &version, 1) < 0)
4457                         return -1;
4458                 pos += 2;
4459                 if (*pos != ' ')
4460                         return -1;
4461                 pos++;
4462                 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
4463 #ifdef CONFIG_WIFI_DISPLAY
4464         } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
4465                 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
4466 #endif /* CONFIG_WIFI_DISPLAY */
4467         } else {
4468                 len = os_strlen(pos);
4469                 if (len & 1)
4470                         return -1;
4471                 len /= 2;
4472                 tlvs = wpabuf_alloc(len);
4473                 if (tlvs == NULL)
4474                         return -1;
4475                 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
4476                         wpabuf_free(tlvs);
4477                         return -1;
4478                 }
4479
4480                 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
4481                 wpabuf_free(tlvs);
4482         }
4483         if (ref == 0)
4484                 return -1;
4485         res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
4486         if (res < 0 || (unsigned) res >= buflen)
4487                 return -1;
4488         return res;
4489 }
4490
4491
4492 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
4493                                          char *cmd)
4494 {
4495         long long unsigned val;
4496         u64 req;
4497         if (sscanf(cmd, "%llx", &val) != 1)
4498                 return -1;
4499         req = val;
4500         return wpas_p2p_sd_cancel_request(wpa_s, req);
4501 }
4502
4503
4504 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
4505 {
4506         int freq;
4507         u8 dst[ETH_ALEN];
4508         u8 dialog_token;
4509         struct wpabuf *resp_tlvs;
4510         char *pos, *pos2;
4511         size_t len;
4512
4513         pos = os_strchr(cmd, ' ');
4514         if (pos == NULL)
4515                 return -1;
4516         *pos++ = '\0';
4517         freq = atoi(cmd);
4518         if (freq == 0)
4519                 return -1;
4520
4521         if (hwaddr_aton(pos, dst))
4522                 return -1;
4523         pos += 17;
4524         if (*pos != ' ')
4525                 return -1;
4526         pos++;
4527
4528         pos2 = os_strchr(pos, ' ');
4529         if (pos2 == NULL)
4530                 return -1;
4531         *pos2++ = '\0';
4532         dialog_token = atoi(pos);
4533
4534         len = os_strlen(pos2);
4535         if (len & 1)
4536                 return -1;
4537         len /= 2;
4538         resp_tlvs = wpabuf_alloc(len);
4539         if (resp_tlvs == NULL)
4540                 return -1;
4541         if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
4542                 wpabuf_free(resp_tlvs);
4543                 return -1;
4544         }
4545
4546         wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
4547         wpabuf_free(resp_tlvs);
4548         return 0;
4549 }
4550
4551
4552 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
4553                                        char *cmd)
4554 {
4555         if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
4556                 return -1;
4557         wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
4558         return 0;
4559 }
4560
4561
4562 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
4563                                         char *cmd)
4564 {
4565         char *pos;
4566         size_t len;
4567         struct wpabuf *query, *resp;
4568
4569         pos = os_strchr(cmd, ' ');
4570         if (pos == NULL)
4571                 return -1;
4572         *pos++ = '\0';
4573
4574         len = os_strlen(cmd);
4575         if (len & 1)
4576                 return -1;
4577         len /= 2;
4578         query = wpabuf_alloc(len);
4579         if (query == NULL)
4580                 return -1;
4581         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4582                 wpabuf_free(query);
4583                 return -1;
4584         }
4585
4586         len = os_strlen(pos);
4587         if (len & 1) {
4588                 wpabuf_free(query);
4589                 return -1;
4590         }
4591         len /= 2;
4592         resp = wpabuf_alloc(len);
4593         if (resp == NULL) {
4594                 wpabuf_free(query);
4595                 return -1;
4596         }
4597         if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4598                 wpabuf_free(query);
4599                 wpabuf_free(resp);
4600                 return -1;
4601         }
4602
4603         if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4604                 wpabuf_free(query);
4605                 wpabuf_free(resp);
4606                 return -1;
4607         }
4608         return 0;
4609 }
4610
4611
4612 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4613 {
4614         char *pos;
4615         u8 version;
4616
4617         pos = os_strchr(cmd, ' ');
4618         if (pos == NULL)
4619                 return -1;
4620         *pos++ = '\0';
4621
4622         if (hexstr2bin(cmd, &version, 1) < 0)
4623                 return -1;
4624
4625         return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4626 }
4627
4628
4629 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4630 {
4631         char *pos;
4632
4633         pos = os_strchr(cmd, ' ');
4634         if (pos == NULL)
4635                 return -1;
4636         *pos++ = '\0';
4637
4638         if (os_strcmp(cmd, "bonjour") == 0)
4639                 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4640         if (os_strcmp(cmd, "upnp") == 0)
4641                 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4642         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4643         return -1;
4644 }
4645
4646
4647 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4648                                         char *cmd)
4649 {
4650         size_t len;
4651         struct wpabuf *query;
4652         int ret;
4653
4654         len = os_strlen(cmd);
4655         if (len & 1)
4656                 return -1;
4657         len /= 2;
4658         query = wpabuf_alloc(len);
4659         if (query == NULL)
4660                 return -1;
4661         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4662                 wpabuf_free(query);
4663                 return -1;
4664         }
4665
4666         ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4667         wpabuf_free(query);
4668         return ret;
4669 }
4670
4671
4672 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4673 {
4674         char *pos;
4675         u8 version;
4676
4677         pos = os_strchr(cmd, ' ');
4678         if (pos == NULL)
4679                 return -1;
4680         *pos++ = '\0';
4681
4682         if (hexstr2bin(cmd, &version, 1) < 0)
4683                 return -1;
4684
4685         return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4686 }
4687
4688
4689 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4690 {
4691         char *pos;
4692
4693         pos = os_strchr(cmd, ' ');
4694         if (pos == NULL)
4695                 return -1;
4696         *pos++ = '\0';
4697
4698         if (os_strcmp(cmd, "bonjour") == 0)
4699                 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4700         if (os_strcmp(cmd, "upnp") == 0)
4701                 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4702         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4703         return -1;
4704 }
4705
4706
4707 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4708 {
4709         u8 addr[ETH_ALEN];
4710
4711         /* <addr> */
4712
4713         if (hwaddr_aton(cmd, addr))
4714                 return -1;
4715
4716         return wpas_p2p_reject(wpa_s, addr);
4717 }
4718
4719
4720 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4721 {
4722         char *pos;
4723         int id;
4724         struct wpa_ssid *ssid;
4725         u8 *_peer = NULL, peer[ETH_ALEN];
4726         int freq = 0, pref_freq = 0;
4727         int ht40, vht;
4728
4729         id = atoi(cmd);
4730         pos = os_strstr(cmd, " peer=");
4731         if (pos) {
4732                 pos += 6;
4733                 if (hwaddr_aton(pos, peer))
4734                         return -1;
4735                 _peer = peer;
4736         }
4737         ssid = wpa_config_get_network(wpa_s->conf, id);
4738         if (ssid == NULL || ssid->disabled != 2) {
4739                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4740                            "for persistent P2P group",
4741                            id);
4742                 return -1;
4743         }
4744
4745         pos = os_strstr(cmd, " freq=");
4746         if (pos) {
4747                 pos += 6;
4748                 freq = atoi(pos);
4749                 if (freq <= 0)
4750                         return -1;
4751         }
4752
4753         pos = os_strstr(cmd, " pref=");
4754         if (pos) {
4755                 pos += 6;
4756                 pref_freq = atoi(pos);
4757                 if (pref_freq <= 0)
4758                         return -1;
4759         }
4760
4761         vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4762         ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4763                 vht;
4764
4765         return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
4766                                pref_freq);
4767 }
4768
4769
4770 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
4771 {
4772         char *pos;
4773         u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
4774
4775         pos = os_strstr(cmd, " peer=");
4776         if (!pos)
4777                 return -1;
4778
4779         *pos = '\0';
4780         pos += 6;
4781         if (hwaddr_aton(pos, peer)) {
4782                 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
4783                 return -1;
4784         }
4785
4786         pos = os_strstr(pos, " go_dev_addr=");
4787         if (pos) {
4788                 pos += 13;
4789                 if (hwaddr_aton(pos, go_dev_addr)) {
4790                         wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
4791                                    pos);
4792                         return -1;
4793                 }
4794                 go_dev = go_dev_addr;
4795         }
4796
4797         return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
4798 }
4799
4800
4801 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
4802 {
4803         if (os_strncmp(cmd, "persistent=", 11) == 0)
4804                 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
4805         if (os_strncmp(cmd, "group=", 6) == 0)
4806                 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
4807
4808         return -1;
4809 }
4810
4811
4812 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
4813                                          char *cmd, int freq, int ht40,
4814                                          int vht)
4815 {
4816         int id;
4817         struct wpa_ssid *ssid;
4818
4819         id = atoi(cmd);
4820         ssid = wpa_config_get_network(wpa_s->conf, id);
4821         if (ssid == NULL || ssid->disabled != 2) {
4822                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4823                            "for persistent P2P group",
4824                            id);
4825                 return -1;
4826         }
4827
4828         return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
4829                                              NULL, 0);
4830 }
4831
4832
4833 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
4834 {
4835         int freq = 0, ht40, vht;
4836         char *pos;
4837
4838         pos = os_strstr(cmd, "freq=");
4839         if (pos)
4840                 freq = atoi(pos + 5);
4841
4842         vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
4843         ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4844                 vht;
4845
4846         if (os_strncmp(cmd, "persistent=", 11) == 0)
4847                 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
4848                                                      ht40, vht);
4849         if (os_strcmp(cmd, "persistent") == 0 ||
4850             os_strncmp(cmd, "persistent ", 11) == 0)
4851                 return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
4852         if (os_strncmp(cmd, "freq=", 5) == 0)
4853                 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
4854         if (ht40)
4855                 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
4856
4857         wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
4858                    cmd);
4859         return -1;
4860 }
4861
4862
4863 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
4864                          char *buf, size_t buflen)
4865 {
4866         u8 addr[ETH_ALEN], *addr_ptr;
4867         int next, res;
4868         const struct p2p_peer_info *info;
4869         char *pos, *end;
4870         char devtype[WPS_DEV_TYPE_BUFSIZE];
4871         struct wpa_ssid *ssid;
4872         size_t i;
4873
4874         if (!wpa_s->global->p2p)
4875                 return -1;
4876
4877         if (os_strcmp(cmd, "FIRST") == 0) {
4878                 addr_ptr = NULL;
4879                 next = 0;
4880         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4881                 if (hwaddr_aton(cmd + 5, addr) < 0)
4882                         return -1;
4883                 addr_ptr = addr;
4884                 next = 1;
4885         } else {
4886                 if (hwaddr_aton(cmd, addr) < 0)
4887                         return -1;
4888                 addr_ptr = addr;
4889                 next = 0;
4890         }
4891
4892         info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4893         if (info == NULL)
4894                 return -1;
4895
4896         pos = buf;
4897         end = buf + buflen;
4898
4899         res = os_snprintf(pos, end - pos, MACSTR "\n"
4900                           "pri_dev_type=%s\n"
4901                           "device_name=%s\n"
4902                           "manufacturer=%s\n"
4903                           "model_name=%s\n"
4904                           "model_number=%s\n"
4905                           "serial_number=%s\n"
4906                           "config_methods=0x%x\n"
4907                           "dev_capab=0x%x\n"
4908                           "group_capab=0x%x\n"
4909                           "level=%d\n",
4910                           MAC2STR(info->p2p_device_addr),
4911                           wps_dev_type_bin2str(info->pri_dev_type,
4912                                                devtype, sizeof(devtype)),
4913                           info->device_name,
4914                           info->manufacturer,
4915                           info->model_name,
4916                           info->model_number,
4917                           info->serial_number,
4918                           info->config_methods,
4919                           info->dev_capab,
4920                           info->group_capab,
4921                           info->level);
4922         if (res < 0 || res >= end - pos)
4923                 return pos - buf;
4924         pos += res;
4925
4926         for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4927         {
4928                 const u8 *t;
4929                 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4930                 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4931                                   wps_dev_type_bin2str(t, devtype,
4932                                                        sizeof(devtype)));
4933                 if (res < 0 || res >= end - pos)
4934                         return pos - buf;
4935                 pos += res;
4936         }
4937
4938         ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
4939         if (ssid) {
4940                 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
4941                 if (res < 0 || res >= end - pos)
4942                         return pos - buf;
4943                 pos += res;
4944         }
4945
4946         res = p2p_get_peer_info_txt(info, pos, end - pos);
4947         if (res < 0)
4948                 return pos - buf;
4949         pos += res;
4950
4951         if (info->vendor_elems) {
4952                 res = os_snprintf(pos, end - pos, "vendor_elems=");
4953                 if (res < 0 || res >= end - pos)
4954                         return pos - buf;
4955                 pos += res;
4956
4957                 pos += wpa_snprintf_hex(pos, end - pos,
4958                                         wpabuf_head(info->vendor_elems),
4959                                         wpabuf_len(info->vendor_elems));
4960
4961                 res = os_snprintf(pos, end - pos, "\n");
4962                 if (res < 0 || res >= end - pos)
4963                         return pos - buf;
4964                 pos += res;
4965         }
4966
4967         return pos - buf;
4968 }
4969
4970
4971 static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
4972                                   const char *param)
4973 {
4974         unsigned int i;
4975
4976         if (wpa_s->global->p2p == NULL)
4977                 return -1;
4978
4979         if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
4980                 return -1;
4981
4982         for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
4983                 struct wpa_freq_range *freq;
4984                 freq = &wpa_s->global->p2p_disallow_freq.range[i];
4985                 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
4986                            freq->min, freq->max);
4987         }
4988
4989         wpas_p2p_update_channel_list(wpa_s);
4990         return 0;
4991 }
4992
4993
4994 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
4995 {
4996         char *param;
4997
4998         if (wpa_s->global->p2p == NULL)
4999                 return -1;
5000
5001         param = os_strchr(cmd, ' ');
5002         if (param == NULL)
5003                 return -1;
5004         *param++ = '\0';
5005
5006         if (os_strcmp(cmd, "discoverability") == 0) {
5007                 p2p_set_client_discoverability(wpa_s->global->p2p,
5008                                                atoi(param));
5009                 return 0;
5010         }
5011
5012         if (os_strcmp(cmd, "managed") == 0) {
5013                 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
5014                 return 0;
5015         }
5016
5017         if (os_strcmp(cmd, "listen_channel") == 0) {
5018                 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
5019                                               atoi(param), 1);
5020         }
5021
5022         if (os_strcmp(cmd, "ssid_postfix") == 0) {
5023                 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
5024                                             os_strlen(param));
5025         }
5026
5027         if (os_strcmp(cmd, "noa") == 0) {
5028                 char *pos;
5029                 int count, start, duration;
5030                 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
5031                 count = atoi(param);
5032                 pos = os_strchr(param, ',');
5033                 if (pos == NULL)
5034                         return -1;
5035                 pos++;
5036                 start = atoi(pos);
5037                 pos = os_strchr(pos, ',');
5038                 if (pos == NULL)
5039                         return -1;
5040                 pos++;
5041                 duration = atoi(pos);
5042                 if (count < 0 || count > 255 || start < 0 || duration < 0)
5043                         return -1;
5044                 if (count == 0 && duration > 0)
5045                         return -1;
5046                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
5047                            "start=%d duration=%d", count, start, duration);
5048                 return wpas_p2p_set_noa(wpa_s, count, start, duration);
5049         }
5050
5051         if (os_strcmp(cmd, "ps") == 0)
5052                 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
5053
5054         if (os_strcmp(cmd, "oppps") == 0)
5055                 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
5056
5057         if (os_strcmp(cmd, "ctwindow") == 0)
5058                 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
5059
5060         if (os_strcmp(cmd, "disabled") == 0) {
5061                 wpa_s->global->p2p_disabled = atoi(param);
5062                 wpa_printf(MSG_DEBUG, "P2P functionality %s",
5063                            wpa_s->global->p2p_disabled ?
5064                            "disabled" : "enabled");
5065                 if (wpa_s->global->p2p_disabled) {
5066                         wpas_p2p_stop_find(wpa_s);
5067                         os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5068                         p2p_flush(wpa_s->global->p2p);
5069                 }
5070                 return 0;
5071         }
5072
5073         if (os_strcmp(cmd, "conc_pref") == 0) {
5074                 if (os_strcmp(param, "sta") == 0)
5075                         wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
5076                 else if (os_strcmp(param, "p2p") == 0)
5077                         wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
5078                 else {
5079                         wpa_printf(MSG_INFO, "Invalid conc_pref value");
5080                         return -1;
5081                 }
5082                 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
5083                            "%s", param);
5084                 return 0;
5085         }
5086
5087         if (os_strcmp(cmd, "force_long_sd") == 0) {
5088                 wpa_s->force_long_sd = atoi(param);
5089                 return 0;
5090         }
5091
5092         if (os_strcmp(cmd, "peer_filter") == 0) {
5093                 u8 addr[ETH_ALEN];
5094                 if (hwaddr_aton(param, addr))
5095                         return -1;
5096                 p2p_set_peer_filter(wpa_s->global->p2p, addr);
5097                 return 0;
5098         }
5099
5100         if (os_strcmp(cmd, "cross_connect") == 0)
5101                 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
5102
5103         if (os_strcmp(cmd, "go_apsd") == 0) {
5104                 if (os_strcmp(param, "disable") == 0)
5105                         wpa_s->set_ap_uapsd = 0;
5106                 else {
5107                         wpa_s->set_ap_uapsd = 1;
5108                         wpa_s->ap_uapsd = atoi(param);
5109                 }
5110                 return 0;
5111         }
5112
5113         if (os_strcmp(cmd, "client_apsd") == 0) {
5114                 if (os_strcmp(param, "disable") == 0)
5115                         wpa_s->set_sta_uapsd = 0;
5116                 else {
5117                         int be, bk, vi, vo;
5118                         char *pos;
5119                         /* format: BE,BK,VI,VO;max SP Length */
5120                         be = atoi(param);
5121                         pos = os_strchr(param, ',');
5122                         if (pos == NULL)
5123                                 return -1;
5124                         pos++;
5125                         bk = atoi(pos);
5126                         pos = os_strchr(pos, ',');
5127                         if (pos == NULL)
5128                                 return -1;
5129                         pos++;
5130                         vi = atoi(pos);
5131                         pos = os_strchr(pos, ',');
5132                         if (pos == NULL)
5133                                 return -1;
5134                         pos++;
5135                         vo = atoi(pos);
5136                         /* ignore max SP Length for now */
5137
5138                         wpa_s->set_sta_uapsd = 1;
5139                         wpa_s->sta_uapsd = 0;
5140                         if (be)
5141                                 wpa_s->sta_uapsd |= BIT(0);
5142                         if (bk)
5143                                 wpa_s->sta_uapsd |= BIT(1);
5144                         if (vi)
5145                                 wpa_s->sta_uapsd |= BIT(2);
5146                         if (vo)
5147                                 wpa_s->sta_uapsd |= BIT(3);
5148                 }
5149                 return 0;
5150         }
5151
5152         if (os_strcmp(cmd, "disallow_freq") == 0)
5153                 return p2p_ctrl_disallow_freq(wpa_s, param);
5154
5155         if (os_strcmp(cmd, "disc_int") == 0) {
5156                 int min_disc_int, max_disc_int, max_disc_tu;
5157                 char *pos;
5158
5159                 pos = param;
5160
5161                 min_disc_int = atoi(pos);
5162                 pos = os_strchr(pos, ' ');
5163                 if (pos == NULL)
5164                         return -1;
5165                 *pos++ = '\0';
5166
5167                 max_disc_int = atoi(pos);
5168                 pos = os_strchr(pos, ' ');
5169                 if (pos == NULL)
5170                         return -1;
5171                 *pos++ = '\0';
5172
5173                 max_disc_tu = atoi(pos);
5174
5175                 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
5176                                         max_disc_int, max_disc_tu);
5177         }
5178
5179         if (os_strcmp(cmd, "per_sta_psk") == 0) {
5180                 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
5181                 return 0;
5182         }
5183
5184 #ifdef CONFIG_WPS_NFC
5185         if (os_strcmp(cmd, "nfc_tag") == 0)
5186                 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
5187 #endif /* CONFIG_WPS_NFC */
5188
5189         if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
5190                 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
5191                 return 0;
5192         }
5193
5194         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
5195                    cmd);
5196
5197         return -1;
5198 }
5199
5200
5201 static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
5202 {
5203         os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5204         wpa_s->force_long_sd = 0;
5205         wpas_p2p_stop_find(wpa_s);
5206         if (wpa_s->global->p2p)
5207                 p2p_flush(wpa_s->global->p2p);
5208 }
5209
5210
5211 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
5212 {
5213         char *pos, *pos2;
5214         unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
5215
5216         if (cmd[0]) {
5217                 pos = os_strchr(cmd, ' ');
5218                 if (pos == NULL)
5219                         return -1;
5220                 *pos++ = '\0';
5221                 dur1 = atoi(cmd);
5222
5223                 pos2 = os_strchr(pos, ' ');
5224                 if (pos2)
5225                         *pos2++ = '\0';
5226                 int1 = atoi(pos);
5227         } else
5228                 pos2 = NULL;
5229
5230         if (pos2) {
5231                 pos = os_strchr(pos2, ' ');
5232                 if (pos == NULL)
5233                         return -1;
5234                 *pos++ = '\0';
5235                 dur2 = atoi(pos2);
5236                 int2 = atoi(pos);
5237         }
5238
5239         return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
5240 }
5241
5242
5243 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
5244 {
5245         char *pos;
5246         unsigned int period = 0, interval = 0;
5247
5248         if (cmd[0]) {
5249                 pos = os_strchr(cmd, ' ');
5250                 if (pos == NULL)
5251                         return -1;
5252                 *pos++ = '\0';
5253                 period = atoi(cmd);
5254                 interval = atoi(pos);
5255         }
5256
5257         return wpas_p2p_ext_listen(wpa_s, period, interval);
5258 }
5259
5260
5261 static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
5262 {
5263         const char *pos;
5264         u8 peer[ETH_ALEN];
5265         int iface_addr = 0;
5266
5267         pos = cmd;
5268         if (os_strncmp(pos, "iface=", 6) == 0) {
5269                 iface_addr = 1;
5270                 pos += 6;
5271         }
5272         if (hwaddr_aton(pos, peer))
5273                 return -1;
5274
5275         wpas_p2p_remove_client(wpa_s, peer, iface_addr);
5276         return 0;
5277 }
5278
5279 #endif /* CONFIG_P2P */
5280
5281
5282 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
5283 {
5284         struct wpa_freq_range_list ranges;
5285         int *freqs = NULL;
5286         struct hostapd_hw_modes *mode;
5287         u16 i;
5288
5289         if (wpa_s->hw.modes == NULL)
5290                 return NULL;
5291
5292         os_memset(&ranges, 0, sizeof(ranges));
5293         if (freq_range_list_parse(&ranges, val) < 0)
5294                 return NULL;
5295
5296         for (i = 0; i < wpa_s->hw.num_modes; i++) {
5297                 int j;
5298
5299                 mode = &wpa_s->hw.modes[i];
5300                 for (j = 0; j < mode->num_channels; j++) {
5301                         unsigned int freq;
5302
5303                         if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
5304                                 continue;
5305
5306                         freq = mode->channels[j].freq;
5307                         if (!freq_range_list_includes(&ranges, freq))
5308                                 continue;
5309
5310                         int_array_add_unique(&freqs, freq);
5311                 }
5312         }
5313
5314         os_free(ranges.range);
5315         return freqs;
5316 }
5317
5318
5319 #ifdef CONFIG_INTERWORKING
5320
5321 static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
5322 {
5323         int auto_sel = 0;
5324         int *freqs = NULL;
5325
5326         if (param) {
5327                 char *pos;
5328
5329                 auto_sel = os_strstr(param, "auto") != NULL;
5330
5331                 pos = os_strstr(param, "freq=");
5332                 if (pos) {
5333                         freqs = freq_range_to_channel_list(wpa_s, pos + 5);
5334                         if (freqs == NULL)
5335                                 return -1;
5336                 }
5337
5338         }
5339
5340         return interworking_select(wpa_s, auto_sel, freqs);
5341 }
5342
5343
5344 static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
5345 {
5346         u8 bssid[ETH_ALEN];
5347         struct wpa_bss *bss;
5348
5349         if (hwaddr_aton(dst, bssid)) {
5350                 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
5351                 return -1;
5352         }
5353
5354         bss = wpa_bss_get_bssid(wpa_s, bssid);
5355         if (bss == NULL) {
5356                 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
5357                            MAC2STR(bssid));
5358                 return -1;
5359         }
5360
5361         return interworking_connect(wpa_s, bss);
5362 }
5363
5364
5365 static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
5366 {
5367         u8 dst_addr[ETH_ALEN];
5368         int used;
5369         char *pos;
5370 #define MAX_ANQP_INFO_ID 100
5371         u16 id[MAX_ANQP_INFO_ID];
5372         size_t num_id = 0;
5373         u32 subtypes = 0;
5374
5375         used = hwaddr_aton2(dst, dst_addr);
5376         if (used < 0)
5377                 return -1;
5378         pos = dst + used;
5379         while (num_id < MAX_ANQP_INFO_ID) {
5380                 if (os_strncmp(pos, "hs20:", 5) == 0) {
5381 #ifdef CONFIG_HS20
5382                         int num = atoi(pos + 5);
5383                         if (num <= 0 || num > 31)
5384                                 return -1;
5385                         subtypes |= BIT(num);
5386 #else /* CONFIG_HS20 */
5387                         return -1;
5388 #endif /* CONFIG_HS20 */
5389                 } else {
5390                         id[num_id] = atoi(pos);
5391                         if (id[num_id])
5392                                 num_id++;
5393                 }
5394                 pos = os_strchr(pos + 1, ',');
5395                 if (pos == NULL)
5396                         break;
5397                 pos++;
5398         }
5399
5400         if (num_id == 0)
5401                 return -1;
5402
5403         return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
5404 }
5405
5406
5407 static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
5408 {
5409         u8 dst_addr[ETH_ALEN];
5410         struct wpabuf *advproto, *query = NULL;
5411         int used, ret = -1;
5412         char *pos, *end;
5413         size_t len;
5414
5415         used = hwaddr_aton2(cmd, dst_addr);
5416         if (used < 0)
5417                 return -1;
5418
5419         pos = cmd + used;
5420         while (*pos == ' ')
5421                 pos++;
5422
5423         /* Advertisement Protocol ID */
5424         end = os_strchr(pos, ' ');
5425         if (end)
5426                 len = end - pos;
5427         else
5428                 len = os_strlen(pos);
5429         if (len & 0x01)
5430                 return -1;
5431         len /= 2;
5432         if (len == 0)
5433                 return -1;
5434         advproto = wpabuf_alloc(len);
5435         if (advproto == NULL)
5436                 return -1;
5437         if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
5438                 goto fail;
5439
5440         if (end) {
5441                 /* Optional Query Request */
5442                 pos = end + 1;
5443                 while (*pos == ' ')
5444                         pos++;
5445
5446                 len = os_strlen(pos);
5447                 if (len) {
5448                         if (len & 0x01)
5449                                 goto fail;
5450                         len /= 2;
5451                         if (len == 0)
5452                                 goto fail;
5453                         query = wpabuf_alloc(len);
5454                         if (query == NULL)
5455                                 goto fail;
5456                         if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
5457                                 goto fail;
5458                 }
5459         }
5460
5461         ret = gas_send_request(wpa_s, dst_addr, advproto, query);
5462
5463 fail:
5464         wpabuf_free(advproto);
5465         wpabuf_free(query);
5466
5467         return ret;
5468 }
5469
5470
5471 static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
5472                             size_t buflen)
5473 {
5474         u8 addr[ETH_ALEN];
5475         int dialog_token;
5476         int used;
5477         char *pos;
5478         size_t resp_len, start, requested_len;
5479         struct wpabuf *resp;
5480         int ret;
5481
5482         used = hwaddr_aton2(cmd, addr);
5483         if (used < 0)
5484                 return -1;
5485
5486         pos = cmd + used;
5487         while (*pos == ' ')
5488                 pos++;
5489         dialog_token = atoi(pos);
5490
5491         if (wpa_s->last_gas_resp &&
5492             os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
5493             dialog_token == wpa_s->last_gas_dialog_token)
5494                 resp = wpa_s->last_gas_resp;
5495         else if (wpa_s->prev_gas_resp &&
5496                  os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
5497                  dialog_token == wpa_s->prev_gas_dialog_token)
5498                 resp = wpa_s->prev_gas_resp;
5499         else
5500                 return -1;
5501
5502         resp_len = wpabuf_len(resp);
5503         start = 0;
5504         requested_len = resp_len;
5505
5506         pos = os_strchr(pos, ' ');
5507         if (pos) {
5508                 start = atoi(pos);
5509                 if (start > resp_len)
5510                         return os_snprintf(buf, buflen, "FAIL-Invalid range");
5511                 pos = os_strchr(pos, ',');
5512                 if (pos == NULL)
5513                         return -1;
5514                 pos++;
5515                 requested_len = atoi(pos);
5516                 if (start + requested_len > resp_len)
5517                         return os_snprintf(buf, buflen, "FAIL-Invalid range");
5518         }
5519
5520         if (requested_len * 2 + 1 > buflen)
5521                 return os_snprintf(buf, buflen, "FAIL-Too long response");
5522
5523         ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
5524                                requested_len);
5525
5526         if (start + requested_len == resp_len) {
5527                 /*
5528                  * Free memory by dropping the response after it has been
5529                  * fetched.
5530                  */
5531                 if (resp == wpa_s->prev_gas_resp) {
5532                         wpabuf_free(wpa_s->prev_gas_resp);
5533                         wpa_s->prev_gas_resp = NULL;
5534                 } else {
5535                         wpabuf_free(wpa_s->last_gas_resp);
5536                         wpa_s->last_gas_resp = NULL;
5537                 }
5538         }
5539
5540         return ret;
5541 }
5542 #endif /* CONFIG_INTERWORKING */
5543
5544
5545 #ifdef CONFIG_HS20
5546
5547 static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
5548 {
5549         u8 dst_addr[ETH_ALEN];
5550         int used;
5551         char *pos;
5552         u32 subtypes = 0;
5553
5554         used = hwaddr_aton2(dst, dst_addr);
5555         if (used < 0)
5556                 return -1;
5557         pos = dst + used;
5558         for (;;) {
5559                 int num = atoi(pos);
5560                 if (num <= 0 || num > 31)
5561                         return -1;
5562                 subtypes |= BIT(num);
5563                 pos = os_strchr(pos + 1, ',');
5564                 if (pos == NULL)
5565                         break;
5566                 pos++;
5567         }
5568
5569         if (subtypes == 0)
5570                 return -1;
5571
5572         return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
5573 }
5574
5575
5576 static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5577                                     const u8 *addr, const char *realm)
5578 {
5579         u8 *buf;
5580         size_t rlen, len;
5581         int ret;
5582
5583         rlen = os_strlen(realm);
5584         len = 3 + rlen;
5585         buf = os_malloc(len);
5586         if (buf == NULL)
5587                 return -1;
5588         buf[0] = 1; /* NAI Home Realm Count */
5589         buf[1] = 0; /* Formatted in accordance with RFC 4282 */
5590         buf[2] = rlen;
5591         os_memcpy(buf + 3, realm, rlen);
5592
5593         ret = hs20_anqp_send_req(wpa_s, addr,
5594                                  BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5595                                  buf, len);
5596
5597         os_free(buf);
5598
5599         return ret;
5600 }
5601
5602
5603 static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5604                                         char *dst)
5605 {
5606         struct wpa_cred *cred = wpa_s->conf->cred;
5607         u8 dst_addr[ETH_ALEN];
5608         int used;
5609         u8 *buf;
5610         size_t len;
5611         int ret;
5612
5613         used = hwaddr_aton2(dst, dst_addr);
5614         if (used < 0)
5615                 return -1;
5616
5617         while (dst[used] == ' ')
5618                 used++;
5619         if (os_strncmp(dst + used, "realm=", 6) == 0)
5620                 return hs20_nai_home_realm_list(wpa_s, dst_addr,
5621                                                 dst + used + 6);
5622
5623         len = os_strlen(dst + used);
5624
5625         if (len == 0 && cred && cred->realm)
5626                 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
5627
5628         if (len & 1)
5629                 return -1;
5630         len /= 2;
5631         buf = os_malloc(len);
5632         if (buf == NULL)
5633                 return -1;
5634         if (hexstr2bin(dst + used, buf, len) < 0) {
5635                 os_free(buf);
5636                 return -1;
5637         }
5638
5639         ret = hs20_anqp_send_req(wpa_s, dst_addr,
5640                                  BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5641                                  buf, len);
5642         os_free(buf);
5643
5644         return ret;
5645 }
5646
5647
5648 static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
5649 {
5650         u8 dst_addr[ETH_ALEN];
5651         int used;
5652         char *icon;
5653
5654         used = hwaddr_aton2(cmd, dst_addr);
5655         if (used < 0)
5656                 return -1;
5657
5658         while (cmd[used] == ' ')
5659                 used++;
5660         icon = &cmd[used];
5661
5662         wpa_s->fetch_osu_icon_in_progress = 0;
5663         return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
5664                                   (u8 *) icon, os_strlen(icon));
5665 }
5666
5667 #endif /* CONFIG_HS20 */
5668
5669
5670 static int wpa_supplicant_ctrl_iface_sta_autoconnect(
5671         struct wpa_supplicant *wpa_s, char *cmd)
5672 {
5673         wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
5674         return 0;
5675 }
5676
5677
5678 #ifdef CONFIG_AUTOSCAN
5679
5680 static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
5681                                               char *cmd)
5682 {
5683         enum wpa_states state = wpa_s->wpa_state;
5684         char *new_params = NULL;
5685
5686         if (os_strlen(cmd) > 0) {
5687                 new_params = os_strdup(cmd);
5688                 if (new_params == NULL)
5689                         return -1;
5690         }
5691
5692         os_free(wpa_s->conf->autoscan);
5693         wpa_s->conf->autoscan = new_params;
5694
5695         if (wpa_s->conf->autoscan == NULL)
5696                 autoscan_deinit(wpa_s);
5697         else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
5698                 autoscan_init(wpa_s, 1);
5699         else if (state == WPA_SCANNING)
5700                 wpa_supplicant_reinit_autoscan(wpa_s);
5701
5702         return 0;
5703 }
5704
5705 #endif /* CONFIG_AUTOSCAN */
5706
5707
5708 #ifdef CONFIG_WNM
5709
5710 static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
5711 {
5712         int enter;
5713         int intval = 0;
5714         char *pos;
5715         int ret;
5716         struct wpabuf *tfs_req = NULL;
5717
5718         if (os_strncmp(cmd, "enter", 5) == 0)
5719                 enter = 1;
5720         else if (os_strncmp(cmd, "exit", 4) == 0)
5721                 enter = 0;
5722         else
5723                 return -1;
5724
5725         pos = os_strstr(cmd, " interval=");
5726         if (pos)
5727                 intval = atoi(pos + 10);
5728
5729         pos = os_strstr(cmd, " tfs_req=");
5730         if (pos) {
5731                 char *end;
5732                 size_t len;
5733                 pos += 9;
5734                 end = os_strchr(pos, ' ');
5735                 if (end)
5736                         len = end - pos;
5737                 else
5738                         len = os_strlen(pos);
5739                 if (len & 1)
5740                         return -1;
5741                 len /= 2;
5742                 tfs_req = wpabuf_alloc(len);
5743                 if (tfs_req == NULL)
5744                         return -1;
5745                 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
5746                         wpabuf_free(tfs_req);
5747                         return -1;
5748                 }
5749         }
5750
5751         ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
5752                                            WNM_SLEEP_MODE_EXIT, intval,
5753                                            tfs_req);
5754         wpabuf_free(tfs_req);
5755
5756         return ret;
5757 }
5758
5759
5760 static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
5761 {
5762         int query_reason;
5763
5764         query_reason = atoi(cmd);
5765
5766         wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
5767                    query_reason);
5768
5769         return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
5770 }
5771
5772 #endif /* CONFIG_WNM */
5773
5774
5775 static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
5776                                       size_t buflen)
5777 {
5778         struct wpa_signal_info si;
5779         int ret;
5780         char *pos, *end;
5781
5782         ret = wpa_drv_signal_poll(wpa_s, &si);
5783         if (ret)
5784                 return -1;
5785
5786         pos = buf;
5787         end = buf + buflen;
5788
5789         ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
5790                           "NOISE=%d\nFREQUENCY=%u\n",
5791                           si.current_signal, si.current_txrate / 1000,
5792                           si.current_noise, si.frequency);
5793         if (ret < 0 || ret > end - pos)
5794                 return -1;
5795         pos += ret;
5796
5797         if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
5798                 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
5799                                   channel_width_to_string(si.chanwidth));
5800                 if (ret < 0 || ret > end - pos)
5801                         return -1;
5802                 pos += ret;
5803         }
5804
5805         if (si.center_frq1 > 0 && si.center_frq2 > 0) {
5806                 ret = os_snprintf(pos, end - pos,
5807                                   "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
5808                                   si.center_frq1, si.center_frq2);
5809                 if (ret < 0 || ret > end - pos)
5810                         return -1;
5811                 pos += ret;
5812         }
5813
5814         if (si.avg_signal) {
5815                 ret = os_snprintf(pos, end - pos,
5816                                   "AVG_RSSI=%d\n", si.avg_signal);
5817                 if (ret < 0 || ret >= end - pos)
5818                         return -1;
5819                 pos += ret;
5820         }
5821
5822         return pos - buf;
5823 }
5824
5825
5826 static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
5827                                       size_t buflen)
5828 {
5829         struct hostap_sta_driver_data sta;
5830         int ret;
5831
5832         ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
5833         if (ret)
5834                 return -1;
5835
5836         ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
5837                           sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
5838         if (ret < 0 || (size_t) ret > buflen)
5839                 return -1;
5840         return ret;
5841 }
5842
5843
5844 #ifdef ANDROID
5845 static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5846                                      char *buf, size_t buflen)
5847 {
5848         int ret;
5849
5850         ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
5851         if (ret == 0) {
5852                 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
5853                         struct p2p_data *p2p = wpa_s->global->p2p;
5854                         if (p2p) {
5855                                 char country[3];
5856                                 country[0] = cmd[8];
5857                                 country[1] = cmd[9];
5858                                 country[2] = 0x04;
5859                                 p2p_set_country(p2p, country);
5860                         }
5861                 }
5862                 ret = os_snprintf(buf, buflen, "%s\n", "OK");
5863         }
5864         return ret;
5865 }
5866 #endif /* ANDROID */
5867
5868
5869 static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5870                                      char *buf, size_t buflen)
5871 {
5872         int ret;
5873         char *pos;
5874         u8 *data = NULL;
5875         unsigned int vendor_id, subcmd;
5876         struct wpabuf *reply;
5877         size_t data_len = 0;
5878
5879         /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
5880         vendor_id = strtoul(cmd, &pos, 16);
5881         if (!isblank(*pos))
5882                 return -EINVAL;
5883
5884         subcmd = strtoul(pos, &pos, 10);
5885
5886         if (*pos != '\0') {
5887                 if (!isblank(*pos++))
5888                         return -EINVAL;
5889                 data_len = os_strlen(pos);
5890         }
5891
5892         if (data_len) {
5893                 data_len /= 2;
5894                 data = os_malloc(data_len);
5895                 if (!data)
5896                         return -1;
5897
5898                 if (hexstr2bin(pos, data, data_len)) {
5899                         wpa_printf(MSG_DEBUG,
5900                                    "Vendor command: wrong parameter format");
5901                         os_free(data);
5902                         return -EINVAL;
5903                 }
5904         }
5905
5906         reply = wpabuf_alloc((buflen - 1) / 2);
5907         if (!reply) {
5908                 os_free(data);
5909                 return -1;
5910         }
5911
5912         ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
5913                                  reply);
5914
5915         if (ret == 0)
5916                 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
5917                                        wpabuf_len(reply));
5918
5919         wpabuf_free(reply);
5920         os_free(data);
5921
5922         return ret;
5923 }
5924
5925
5926 static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
5927 {
5928         wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
5929
5930 #ifdef CONFIG_P2P
5931         wpas_p2p_cancel(wpa_s);
5932         wpas_p2p_stop_find(wpa_s);
5933         p2p_ctrl_flush(wpa_s);
5934         wpas_p2p_group_remove(wpa_s, "*");
5935         wpas_p2p_service_flush(wpa_s);
5936         wpa_s->global->p2p_disabled = 0;
5937         wpa_s->global->p2p_per_sta_psk = 0;
5938         wpa_s->conf->num_sec_device_types = 0;
5939         wpa_s->p2p_disable_ip_addr_req = 0;
5940         os_free(wpa_s->global->p2p_go_avoid_freq.range);
5941         wpa_s->global->p2p_go_avoid_freq.range = NULL;
5942 #endif /* CONFIG_P2P */
5943
5944 #ifdef CONFIG_WPS_TESTING
5945         wps_version_number = 0x20;
5946         wps_testing_dummy_cred = 0;
5947         wps_corrupt_pkhash = 0;
5948 #endif /* CONFIG_WPS_TESTING */
5949 #ifdef CONFIG_WPS
5950         wpa_s->wps_fragment_size = 0;
5951         wpas_wps_cancel(wpa_s);
5952 #endif /* CONFIG_WPS */
5953         wpa_s->after_wps = 0;
5954         wpa_s->known_wps_freq = 0;
5955
5956 #ifdef CONFIG_TDLS
5957 #ifdef CONFIG_TDLS_TESTING
5958         extern unsigned int tdls_testing;
5959         tdls_testing = 0;
5960 #endif /* CONFIG_TDLS_TESTING */
5961         wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
5962         wpa_tdls_enable(wpa_s->wpa, 1);
5963 #endif /* CONFIG_TDLS */
5964
5965         eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
5966         wpa_supplicant_stop_countermeasures(wpa_s, NULL);
5967
5968         wpa_s->no_keep_alive = 0;
5969
5970         os_free(wpa_s->disallow_aps_bssid);
5971         wpa_s->disallow_aps_bssid = NULL;
5972         wpa_s->disallow_aps_bssid_count = 0;
5973         os_free(wpa_s->disallow_aps_ssid);
5974         wpa_s->disallow_aps_ssid = NULL;
5975         wpa_s->disallow_aps_ssid_count = 0;
5976
5977         wpa_s->set_sta_uapsd = 0;
5978         wpa_s->sta_uapsd = 0;
5979
5980         wpa_drv_radio_disable(wpa_s, 0);
5981
5982         wpa_bss_flush(wpa_s);
5983         wpa_blacklist_clear(wpa_s);
5984         wpa_s->extra_blacklist_count = 0;
5985         wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
5986         wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
5987         wpa_config_flush_blobs(wpa_s->conf);
5988         wpa_s->conf->auto_interworking = 0;
5989         wpa_s->conf->okc = 0;
5990
5991         wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
5992         rsn_preauth_deinit(wpa_s->wpa);
5993
5994         wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
5995         wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
5996         wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
5997         eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5998
5999         radio_remove_works(wpa_s, NULL, 1);
6000         wpa_s->ext_work_in_progress = 0;
6001
6002         wpa_s->next_ssid = NULL;
6003
6004 #ifdef CONFIG_INTERWORKING
6005         hs20_cancel_fetch_osu(wpa_s);
6006 #endif /* CONFIG_INTERWORKING */
6007
6008         wpa_s->ext_mgmt_frame_handling = 0;
6009         wpa_s->ext_eapol_frame_io = 0;
6010 #ifdef CONFIG_TESTING_OPTIONS
6011         wpa_s->extra_roc_dur = 0;
6012 #endif /* CONFIG_TESTING_OPTIONS */
6013 }
6014
6015
6016 static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
6017                                      char *buf, size_t buflen)
6018 {
6019         struct wpa_radio_work *work;
6020         char *pos, *end;
6021         struct os_reltime now, diff;
6022
6023         pos = buf;
6024         end = buf + buflen;
6025
6026         os_get_reltime(&now);
6027
6028         dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
6029         {
6030                 int ret;
6031
6032                 os_reltime_sub(&now, &work->time, &diff);
6033                 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
6034                                   work->type, work->wpa_s->ifname, work->freq,
6035                                   work->started, diff.sec, diff.usec);
6036                 if (ret < 0 || ret >= end - pos)
6037                         break;
6038                 pos += ret;
6039         }
6040
6041         return pos - buf;
6042 }
6043
6044
6045 static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
6046 {
6047         struct wpa_radio_work *work = eloop_ctx;
6048         struct wpa_external_work *ework = work->ctx;
6049
6050         wpa_dbg(work->wpa_s, MSG_DEBUG,
6051                 "Timing out external radio work %u (%s)",
6052                 ework->id, work->type);
6053         wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
6054         work->wpa_s->ext_work_in_progress = 0;
6055         radio_work_done(work);
6056         os_free(ework);
6057 }
6058
6059
6060 static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
6061 {
6062         struct wpa_external_work *ework = work->ctx;
6063
6064         if (deinit) {
6065                 if (work->started)
6066                         eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
6067                                              work, NULL);
6068
6069                 os_free(ework);
6070                 return;
6071         }
6072
6073         wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
6074                 ework->id, ework->type);
6075         wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
6076         work->wpa_s->ext_work_in_progress = 1;
6077         if (!ework->timeout)
6078                 ework->timeout = 10;
6079         eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
6080                                work, NULL);
6081 }
6082
6083
6084 static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
6085                                     char *buf, size_t buflen)
6086 {
6087         struct wpa_external_work *ework;
6088         char *pos, *pos2;
6089         size_t type_len;
6090         int ret;
6091         unsigned int freq = 0;
6092
6093         /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
6094
6095         ework = os_zalloc(sizeof(*ework));
6096         if (ework == NULL)
6097                 return -1;
6098
6099         pos = os_strchr(cmd, ' ');
6100         if (pos) {
6101                 type_len = pos - cmd;
6102                 pos++;
6103
6104                 pos2 = os_strstr(pos, "freq=");
6105                 if (pos2)
6106                         freq = atoi(pos2 + 5);
6107
6108                 pos2 = os_strstr(pos, "timeout=");
6109                 if (pos2)
6110                         ework->timeout = atoi(pos2 + 8);
6111         } else {
6112                 type_len = os_strlen(cmd);
6113         }
6114         if (4 + type_len >= sizeof(ework->type))
6115                 type_len = sizeof(ework->type) - 4 - 1;
6116         os_strlcpy(ework->type, "ext:", sizeof(ework->type));
6117         os_memcpy(ework->type + 4, cmd, type_len);
6118         ework->type[4 + type_len] = '\0';
6119
6120         wpa_s->ext_work_id++;
6121         if (wpa_s->ext_work_id == 0)
6122                 wpa_s->ext_work_id++;
6123         ework->id = wpa_s->ext_work_id;
6124
6125         if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
6126                            ework) < 0) {
6127                 os_free(ework);
6128                 return -1;
6129         }
6130
6131         ret = os_snprintf(buf, buflen, "%u", ework->id);
6132         if (ret < 0 || (size_t) ret >= buflen)
6133                 return -1;
6134         return ret;
6135 }
6136
6137
6138 static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
6139 {
6140         struct wpa_radio_work *work;
6141         unsigned int id = atoi(cmd);
6142
6143         dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
6144         {
6145                 struct wpa_external_work *ework;
6146
6147                 if (os_strncmp(work->type, "ext:", 4) != 0)
6148                         continue;
6149                 ework = work->ctx;
6150                 if (id && ework->id != id)
6151                         continue;
6152                 wpa_dbg(wpa_s, MSG_DEBUG,
6153                         "Completed external radio work %u (%s)",
6154                         ework->id, ework->type);
6155                 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
6156                 wpa_s->ext_work_in_progress = 0;
6157                 radio_work_done(work);
6158                 os_free(ework);
6159                 return 3; /* "OK\n" */
6160         }
6161
6162         return -1;
6163 }
6164
6165
6166 static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
6167                                 char *buf, size_t buflen)
6168 {
6169         if (os_strcmp(cmd, "show") == 0)
6170                 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
6171         if (os_strncmp(cmd, "add ", 4) == 0)
6172                 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
6173         if (os_strncmp(cmd, "done ", 5) == 0)
6174                 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
6175         return -1;
6176 }
6177
6178
6179 void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
6180 {
6181         struct wpa_radio_work *work, *tmp;
6182
6183         if (!wpa_s || !wpa_s->radio)
6184                 return;
6185
6186         dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
6187                               struct wpa_radio_work, list) {
6188                 struct wpa_external_work *ework;
6189
6190                 if (os_strncmp(work->type, "ext:", 4) != 0)
6191                         continue;
6192                 ework = work->ctx;
6193                 wpa_dbg(wpa_s, MSG_DEBUG,
6194                         "Flushing%s external radio work %u (%s)",
6195                         work->started ? " started" : "", ework->id,
6196                         ework->type);
6197                 if (work->started)
6198                         eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
6199                                              work, NULL);
6200                 radio_work_done(work);
6201                 os_free(ework);
6202         }
6203 }
6204
6205
6206 static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
6207 {
6208         struct wpa_supplicant *wpa_s = eloop_ctx;
6209         eapol_sm_notify_ctrl_response(wpa_s->eapol);
6210 }
6211
6212
6213 static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
6214 {
6215         int *freqs = NULL;
6216
6217         freqs = freq_range_to_channel_list(wpa_s, val);
6218         if (freqs == NULL)
6219                 return -1;
6220
6221         os_free(wpa_s->manual_scan_freqs);
6222         wpa_s->manual_scan_freqs = freqs;
6223
6224         return 0;
6225 }
6226
6227
6228 static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value)
6229 {
6230         const char *pos = value;
6231
6232         while (pos) {
6233                 if (*pos == ' ' || *pos == '\0')
6234                         break;
6235                 if (wpa_s->scan_id_count == MAX_SCAN_ID)
6236                         return -1;
6237                 wpa_s->scan_id[wpa_s->scan_id_count++] = atoi(pos);
6238                 pos = os_strchr(pos, ',');
6239                 if (pos)
6240                         pos++;
6241         }
6242
6243         return 0;
6244 }
6245
6246
6247 static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
6248                            char *reply, int reply_size, int *reply_len)
6249 {
6250         char *pos;
6251
6252         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6253                 *reply_len = -1;
6254                 return;
6255         }
6256
6257         wpa_s->manual_scan_passive = 0;
6258         wpa_s->manual_scan_use_id = 0;
6259         wpa_s->manual_scan_only_new = 0;
6260         wpa_s->scan_id_count = 0;
6261
6262         if (params) {
6263                 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
6264                         wpa_s->scan_res_handler = scan_only_handler;
6265
6266                 pos = os_strstr(params, "freq=");
6267                 if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
6268                         *reply_len = -1;
6269                         return;
6270                 }
6271
6272                 pos = os_strstr(params, "passive=");
6273                 if (pos)
6274                         wpa_s->manual_scan_passive = !!atoi(pos + 8);
6275
6276                 pos = os_strstr(params, "use_id=");
6277                 if (pos)
6278                         wpa_s->manual_scan_use_id = atoi(pos + 7);
6279
6280                 pos = os_strstr(params, "only_new=1");
6281                 if (pos)
6282                         wpa_s->manual_scan_only_new = 1;
6283
6284                 pos = os_strstr(params, "scan_id=");
6285                 if (pos && scan_id_list_parse(wpa_s, pos + 8) < 0) {
6286                         *reply_len = -1;
6287                         return;
6288                 }
6289         } else {
6290                 os_free(wpa_s->manual_scan_freqs);
6291                 wpa_s->manual_scan_freqs = NULL;
6292                 if (wpa_s->scan_res_handler == scan_only_handler)
6293                         wpa_s->scan_res_handler = NULL;
6294         }
6295
6296         if (!wpa_s->sched_scanning && !wpa_s->scanning &&
6297             ((wpa_s->wpa_state <= WPA_SCANNING) ||
6298              (wpa_s->wpa_state == WPA_COMPLETED))) {
6299                 wpa_s->normal_scans = 0;
6300                 wpa_s->scan_req = MANUAL_SCAN_REQ;
6301                 wpa_s->after_wps = 0;
6302                 wpa_s->known_wps_freq = 0;
6303                 wpa_supplicant_req_scan(wpa_s, 0, 0);
6304                 if (wpa_s->manual_scan_use_id) {
6305                         wpa_s->manual_scan_id++;
6306                         wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
6307                                 wpa_s->manual_scan_id);
6308                         *reply_len = os_snprintf(reply, reply_size, "%u\n",
6309                                                  wpa_s->manual_scan_id);
6310                 }
6311         } else if (wpa_s->sched_scanning) {
6312                 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
6313                 wpa_supplicant_cancel_sched_scan(wpa_s);
6314                 wpa_s->scan_req = MANUAL_SCAN_REQ;
6315                 wpa_supplicant_req_scan(wpa_s, 0, 0);
6316                 if (wpa_s->manual_scan_use_id) {
6317                         wpa_s->manual_scan_id++;
6318                         *reply_len = os_snprintf(reply, reply_size, "%u\n",
6319                                                  wpa_s->manual_scan_id);
6320                         wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
6321                                 wpa_s->manual_scan_id);
6322                 }
6323         } else {
6324                 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
6325                 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
6326         }
6327 }
6328
6329
6330 #ifdef CONFIG_TESTING_OPTIONS
6331
6332 static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
6333                                        unsigned int freq, const u8 *dst,
6334                                        const u8 *src, const u8 *bssid,
6335                                        const u8 *data, size_t data_len,
6336                                        enum offchannel_send_action_result
6337                                        result)
6338 {
6339         wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
6340                 " src=" MACSTR " bssid=" MACSTR " result=%s",
6341                 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
6342                 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
6343                 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
6344                              "NO_ACK" : "FAILED"));
6345 }
6346
6347
6348 static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
6349 {
6350         char *pos, *param;
6351         size_t len;
6352         u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
6353         int res, used;
6354         int freq = 0, no_cck = 0, wait_time = 0;
6355
6356         /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
6357          *    <action=Action frame payload> */
6358
6359         wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
6360
6361         pos = cmd;
6362         used = hwaddr_aton2(pos, da);
6363         if (used < 0)
6364                 return -1;
6365         pos += used;
6366         while (*pos == ' ')
6367                 pos++;
6368         used = hwaddr_aton2(pos, bssid);
6369         if (used < 0)
6370                 return -1;
6371         pos += used;
6372
6373         param = os_strstr(pos, " freq=");
6374         if (param) {
6375                 param += 6;
6376                 freq = atoi(param);
6377         }
6378
6379         param = os_strstr(pos, " no_cck=");
6380         if (param) {
6381                 param += 8;
6382                 no_cck = atoi(param);
6383         }
6384
6385         param = os_strstr(pos, " wait_time=");
6386         if (param) {
6387                 param += 11;
6388                 wait_time = atoi(param);
6389         }
6390
6391         param = os_strstr(pos, " action=");
6392         if (param == NULL)
6393                 return -1;
6394         param += 8;
6395
6396         len = os_strlen(param);
6397         if (len & 1)
6398                 return -1;
6399         len /= 2;
6400
6401         buf = os_malloc(len);
6402         if (buf == NULL)
6403                 return -1;
6404
6405         if (hexstr2bin(param, buf, len) < 0) {
6406                 os_free(buf);
6407                 return -1;
6408         }
6409
6410         res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
6411                                      buf, len, wait_time,
6412                                      wpas_ctrl_iface_mgmt_tx_cb, no_cck);
6413         os_free(buf);
6414         return res;
6415 }
6416
6417
6418 static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
6419 {
6420         wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
6421         offchannel_send_action_done(wpa_s);
6422 }
6423
6424
6425 static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
6426 {
6427         char *pos, *param;
6428         union wpa_event_data event;
6429         enum wpa_event_type ev;
6430
6431         /* <event name> [parameters..] */
6432
6433         wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
6434
6435         pos = cmd;
6436         param = os_strchr(pos, ' ');
6437         if (param)
6438                 *param++ = '\0';
6439
6440         os_memset(&event, 0, sizeof(event));
6441
6442         if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
6443                 ev = EVENT_INTERFACE_ENABLED;
6444         } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
6445                 ev = EVENT_INTERFACE_DISABLED;
6446         } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
6447                 ev = EVENT_AVOID_FREQUENCIES;
6448                 if (param == NULL)
6449                         param = "";
6450                 if (freq_range_list_parse(&event.freq_range, param) < 0)
6451                         return -1;
6452                 wpa_supplicant_event(wpa_s, ev, &event);
6453                 os_free(event.freq_range.range);
6454                 return 0;
6455         } else {
6456                 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
6457                         cmd);
6458                 return -1;
6459         }
6460
6461         wpa_supplicant_event(wpa_s, ev, &event);
6462
6463         return 0;
6464 }
6465
6466
6467 static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
6468 {
6469         char *pos;
6470         u8 src[ETH_ALEN], *buf;
6471         int used;
6472         size_t len;
6473
6474         wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
6475
6476         pos = cmd;
6477         used = hwaddr_aton2(pos, src);
6478         if (used < 0)
6479                 return -1;
6480         pos += used;
6481         while (*pos == ' ')
6482                 pos++;
6483
6484         len = os_strlen(pos);
6485         if (len & 1)
6486                 return -1;
6487         len /= 2;
6488
6489         buf = os_malloc(len);
6490         if (buf == NULL)
6491                 return -1;
6492
6493         if (hexstr2bin(pos, buf, len) < 0) {
6494                 os_free(buf);
6495                 return -1;
6496         }
6497
6498         wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
6499         os_free(buf);
6500
6501         return 0;
6502 }
6503
6504
6505 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
6506 {
6507         size_t i;
6508         u32 sum = 0;
6509         const u16 *pos = buf;
6510
6511         for (i = 0; i < len / 2; i++)
6512                 sum += *pos++;
6513
6514         while (sum >> 16)
6515                 sum = (sum & 0xffff) + (sum >> 16);
6516
6517         return sum ^ 0xffff;
6518 }
6519
6520
6521 #define HWSIM_PACKETLEN 1500
6522 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
6523
6524 void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
6525 {
6526         struct wpa_supplicant *wpa_s = ctx;
6527         const struct ether_header *eth;
6528         const struct iphdr *ip;
6529         const u8 *pos;
6530         unsigned int i;
6531
6532         if (len != HWSIM_PACKETLEN)
6533                 return;
6534
6535         eth = (const struct ether_header *) buf;
6536         ip = (const struct iphdr *) (eth + 1);
6537         pos = (const u8 *) (ip + 1);
6538
6539         if (ip->ihl != 5 || ip->version != 4 ||
6540             ntohs(ip->tot_len) != HWSIM_IP_LEN)
6541                 return;
6542
6543         for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++) {
6544                 if (*pos != (u8) i)
6545                         return;
6546                 pos++;
6547         }
6548
6549         wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
6550                 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
6551 }
6552
6553
6554 static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
6555                                             char *cmd)
6556 {
6557         int enabled = atoi(cmd);
6558
6559         if (!enabled) {
6560                 if (wpa_s->l2_test) {
6561                         l2_packet_deinit(wpa_s->l2_test);
6562                         wpa_s->l2_test = NULL;
6563                         wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
6564                 }
6565                 return 0;
6566         }
6567
6568         if (wpa_s->l2_test)
6569                 return 0;
6570
6571         wpa_s->l2_test = l2_packet_init(wpa_s->ifname, wpa_s->own_addr,
6572                                         ETHERTYPE_IP, wpas_data_test_rx,
6573                                         wpa_s, 1);
6574         if (wpa_s->l2_test == NULL)
6575                 return -1;
6576
6577         wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
6578
6579         return 0;
6580 }
6581
6582
6583 static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
6584 {
6585         u8 dst[ETH_ALEN], src[ETH_ALEN];
6586         char *pos;
6587         int used;
6588         long int val;
6589         u8 tos;
6590         u8 buf[HWSIM_PACKETLEN];
6591         struct ether_header *eth;
6592         struct iphdr *ip;
6593         u8 *dpos;
6594         unsigned int i;
6595
6596         if (wpa_s->l2_test == NULL)
6597                 return -1;
6598
6599         /* format: <dst> <src> <tos> */
6600
6601         pos = cmd;
6602         used = hwaddr_aton2(pos, dst);
6603         if (used < 0)
6604                 return -1;
6605         pos += used;
6606         while (*pos == ' ')
6607                 pos++;
6608         used = hwaddr_aton2(pos, src);
6609         if (used < 0)
6610                 return -1;
6611         pos += used;
6612
6613         val = strtol(pos, NULL, 0);
6614         if (val < 0 || val > 0xff)
6615                 return -1;
6616         tos = val;
6617
6618         eth = (struct ether_header *) buf;
6619         os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
6620         os_memcpy(eth->ether_shost, src, ETH_ALEN);
6621         eth->ether_type = htons(ETHERTYPE_IP);
6622         ip = (struct iphdr *) (eth + 1);
6623         os_memset(ip, 0, sizeof(*ip));
6624         ip->ihl = 5;
6625         ip->version = 4;
6626         ip->ttl = 64;
6627         ip->tos = tos;
6628         ip->tot_len = htons(HWSIM_IP_LEN);
6629         ip->protocol = 1;
6630         ip->saddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 1);
6631         ip->daddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 2);
6632         ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
6633         dpos = (u8 *) (ip + 1);
6634         for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
6635                 *dpos++ = i;
6636
6637         if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, buf,
6638                            HWSIM_PACKETLEN) < 0)
6639                 return -1;
6640
6641         wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
6642                 " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
6643
6644         return 0;
6645 }
6646
6647
6648 static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
6649                                            char *cmd)
6650 {
6651         u8 *buf;
6652         struct ether_header *eth;
6653         struct l2_packet_data *l2 = NULL;
6654         size_t len;
6655         u16 ethertype;
6656         int res = -1;
6657
6658         len = os_strlen(cmd);
6659         if (len & 1 || len < ETH_HLEN * 2)
6660                 return -1;
6661         len /= 2;
6662
6663         buf = os_malloc(len);
6664         if (buf == NULL)
6665                 return -1;
6666
6667         if (hexstr2bin(cmd, buf, len) < 0)
6668                 goto done;
6669
6670         eth = (struct ether_header *) buf;
6671         ethertype = ntohs(eth->ether_type);
6672
6673         l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
6674                             wpas_data_test_rx, wpa_s, 1);
6675         if (l2 == NULL)
6676                 goto done;
6677
6678         res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
6679         wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
6680 done:
6681         if (l2)
6682                 l2_packet_deinit(l2);
6683         os_free(buf);
6684
6685         return res < 0 ? -1 : 0;
6686 }
6687
6688 #endif /* CONFIG_TESTING_OPTIONS */
6689
6690
6691 static void wpas_ctrl_vendor_elem_update(struct wpa_supplicant *wpa_s)
6692 {
6693         unsigned int i;
6694         char buf[30];
6695
6696         wpa_printf(MSG_DEBUG, "Update vendor elements");
6697
6698         for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
6699                 if (wpa_s->vendor_elem[i]) {
6700                         os_snprintf(buf, sizeof(buf), "frame[%u]", i);
6701                         wpa_hexdump_buf(MSG_DEBUG, buf, wpa_s->vendor_elem[i]);
6702                 }
6703         }
6704
6705 #ifdef CONFIG_P2P
6706         if (wpa_s->parent == wpa_s &&
6707             wpa_s->global->p2p &&
6708             !wpa_s->global->p2p_disabled)
6709                 p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
6710 #endif /* CONFIG_P2P */
6711 }
6712
6713
6714 static struct wpa_supplicant *
6715 wpas_ctrl_vendor_elem_iface(struct wpa_supplicant *wpa_s,
6716                             enum wpa_vendor_elem_frame frame)
6717 {
6718         switch (frame) {
6719 #ifdef CONFIG_P2P
6720         case VENDOR_ELEM_PROBE_REQ_P2P:
6721         case VENDOR_ELEM_PROBE_RESP_P2P:
6722         case VENDOR_ELEM_PROBE_RESP_P2P_GO:
6723         case VENDOR_ELEM_BEACON_P2P_GO:
6724         case VENDOR_ELEM_P2P_PD_REQ:
6725         case VENDOR_ELEM_P2P_PD_RESP:
6726         case VENDOR_ELEM_P2P_GO_NEG_REQ:
6727         case VENDOR_ELEM_P2P_GO_NEG_RESP:
6728         case VENDOR_ELEM_P2P_GO_NEG_CONF:
6729         case VENDOR_ELEM_P2P_INV_REQ:
6730         case VENDOR_ELEM_P2P_INV_RESP:
6731         case VENDOR_ELEM_P2P_ASSOC_REQ:
6732                 return wpa_s->parent;
6733 #endif /* CONFIG_P2P */
6734         default:
6735                 return wpa_s;
6736         }
6737 }
6738
6739
6740 static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
6741 {
6742         char *pos = cmd;
6743         int frame;
6744         size_t len;
6745         struct wpabuf *buf;
6746         struct ieee802_11_elems elems;
6747
6748         frame = atoi(pos);
6749         if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
6750                 return -1;
6751         wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
6752
6753         pos = os_strchr(pos, ' ');
6754         if (pos == NULL)
6755                 return -1;
6756         pos++;
6757
6758         len = os_strlen(pos);
6759         if (len == 0)
6760                 return 0;
6761         if (len & 1)
6762                 return -1;
6763         len /= 2;
6764
6765         buf = wpabuf_alloc(len);
6766         if (buf == NULL)
6767                 return -1;
6768
6769         if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
6770                 wpabuf_free(buf);
6771                 return -1;
6772         }
6773
6774         if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
6775             ParseFailed) {
6776                 wpabuf_free(buf);
6777                 return -1;
6778         }
6779
6780         if (wpa_s->vendor_elem[frame] == NULL) {
6781                 wpa_s->vendor_elem[frame] = buf;
6782                 wpas_ctrl_vendor_elem_update(wpa_s);
6783                 return 0;
6784         }
6785
6786         if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
6787                 wpabuf_free(buf);
6788                 return -1;
6789         }
6790
6791         wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
6792         wpabuf_free(buf);
6793         wpas_ctrl_vendor_elem_update(wpa_s);
6794
6795         return 0;
6796 }
6797
6798
6799 static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
6800                                      char *buf, size_t buflen)
6801 {
6802         int frame = atoi(cmd);
6803
6804         if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
6805                 return -1;
6806         wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
6807
6808         if (wpa_s->vendor_elem[frame] == NULL)
6809                 return 0;
6810
6811         return wpa_snprintf_hex(buf, buflen,
6812                                 wpabuf_head_u8(wpa_s->vendor_elem[frame]),
6813                                 wpabuf_len(wpa_s->vendor_elem[frame]));
6814 }
6815
6816
6817 static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
6818 {
6819         char *pos = cmd;
6820         int frame;
6821         size_t len;
6822         u8 *buf;
6823         struct ieee802_11_elems elems;
6824         u8 *ie, *end;
6825
6826         frame = atoi(pos);
6827         if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
6828                 return -1;
6829         wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
6830
6831         pos = os_strchr(pos, ' ');
6832         if (pos == NULL)
6833                 return -1;
6834         pos++;
6835
6836         if (*pos == '*') {
6837                 wpabuf_free(wpa_s->vendor_elem[frame]);
6838                 wpa_s->vendor_elem[frame] = NULL;
6839                 wpas_ctrl_vendor_elem_update(wpa_s);
6840                 return 0;
6841         }
6842
6843         if (wpa_s->vendor_elem[frame] == NULL)
6844                 return -1;
6845
6846         len = os_strlen(pos);
6847         if (len == 0)
6848                 return 0;
6849         if (len & 1)
6850                 return -1;
6851         len /= 2;
6852
6853         buf = os_malloc(len);
6854         if (buf == NULL)
6855                 return -1;
6856
6857         if (hexstr2bin(pos, buf, len) < 0) {
6858                 os_free(buf);
6859                 return -1;
6860         }
6861
6862         if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
6863                 os_free(buf);
6864                 return -1;
6865         }
6866
6867         ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
6868         end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
6869
6870         for (; ie + 1 < end; ie += 2 + ie[1]) {
6871                 if (ie + len > end)
6872                         break;
6873                 if (os_memcmp(ie, buf, len) != 0)
6874                         continue;
6875
6876                 if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
6877                         wpabuf_free(wpa_s->vendor_elem[frame]);
6878                         wpa_s->vendor_elem[frame] = NULL;
6879                 } else {
6880                         os_memmove(ie, ie + len,
6881                                    end - (ie + len));
6882                         wpa_s->vendor_elem[frame]->used -= len;
6883                 }
6884                 os_free(buf);
6885                 wpas_ctrl_vendor_elem_update(wpa_s);
6886                 return 0;
6887         }
6888
6889         os_free(buf);
6890
6891         return -1;
6892 }
6893
6894
6895 static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
6896 {
6897         struct wpa_supplicant *wpa_s = ctx;
6898
6899         if (neighbor_rep) {
6900                 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
6901                              "length=%u",
6902                              (unsigned int) wpabuf_len(neighbor_rep));
6903                 wpabuf_free(neighbor_rep);
6904         } else {
6905                 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
6906         }
6907 }
6908
6909
6910 static int wpas_ctrl_iface_send_neigbor_rep(struct wpa_supplicant *wpa_s,
6911                                             char *cmd)
6912 {
6913         struct wpa_ssid ssid;
6914         struct wpa_ssid *ssid_p = NULL;
6915         int ret = 0;
6916
6917         if (os_strncmp(cmd, " ssid=", 6) == 0) {
6918                 ssid.ssid_len = os_strlen(cmd + 6);
6919                 if (ssid.ssid_len > 32)
6920                         return -1;
6921                 ssid.ssid = (u8 *) (cmd + 6);
6922                 ssid_p = &ssid;
6923         }
6924
6925         ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p,
6926                                                  wpas_ctrl_neighbor_rep_cb,
6927                                                  wpa_s);
6928
6929         return ret;
6930 }
6931
6932
6933 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
6934                                          char *buf, size_t *resp_len)
6935 {
6936         char *reply;
6937         const int reply_size = 4096;
6938         int reply_len;
6939
6940         if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
6941             os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
6942                 if (wpa_debug_show_keys)
6943                         wpa_dbg(wpa_s, MSG_DEBUG,
6944                                 "Control interface command '%s'", buf);
6945                 else
6946                         wpa_dbg(wpa_s, MSG_DEBUG,
6947                                 "Control interface command '%s [REMOVED]'",
6948                                 os_strncmp(buf, WPA_CTRL_RSP,
6949                                            os_strlen(WPA_CTRL_RSP)) == 0 ?
6950                                 WPA_CTRL_RSP : "SET_NETWORK");
6951         } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
6952                    os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
6953                 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
6954                                       (const u8 *) buf, os_strlen(buf));
6955         } else {
6956                 int level = MSG_DEBUG;
6957                 if (os_strcmp(buf, "PING") == 0)
6958                         level = MSG_EXCESSIVE;
6959                 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
6960         }
6961
6962         reply = os_malloc(reply_size);
6963         if (reply == NULL) {
6964                 *resp_len = 1;
6965                 return NULL;
6966         }
6967
6968         os_memcpy(reply, "OK\n", 3);
6969         reply_len = 3;
6970
6971         if (os_strcmp(buf, "PING") == 0) {
6972                 os_memcpy(reply, "PONG\n", 5);
6973                 reply_len = 5;
6974         } else if (os_strcmp(buf, "IFNAME") == 0) {
6975                 reply_len = os_strlen(wpa_s->ifname);
6976                 os_memcpy(reply, wpa_s->ifname, reply_len);
6977         } else if (os_strncmp(buf, "RELOG", 5) == 0) {
6978                 if (wpa_debug_reopen_file() < 0)
6979                         reply_len = -1;
6980         } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
6981                 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
6982         } else if (os_strcmp(buf, "MIB") == 0) {
6983                 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
6984                 if (reply_len >= 0) {
6985                         int res;
6986                         res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
6987                                                reply_size - reply_len);
6988                         if (res < 0)
6989                                 reply_len = -1;
6990                         else
6991                                 reply_len += res;
6992                 }
6993         } else if (os_strncmp(buf, "STATUS", 6) == 0) {
6994                 reply_len = wpa_supplicant_ctrl_iface_status(
6995                         wpa_s, buf + 6, reply, reply_size);
6996         } else if (os_strcmp(buf, "PMKSA") == 0) {
6997                 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
6998                                                     reply_size);
6999         } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
7000                 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
7001         } else if (os_strncmp(buf, "SET ", 4) == 0) {
7002                 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
7003                         reply_len = -1;
7004         } else if (os_strncmp(buf, "GET ", 4) == 0) {
7005                 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
7006                                                           reply, reply_size);
7007         } else if (os_strcmp(buf, "LOGON") == 0) {
7008                 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
7009         } else if (os_strcmp(buf, "LOGOFF") == 0) {
7010                 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
7011         } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
7012                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
7013                         reply_len = -1;
7014                 else
7015                         wpas_request_connection(wpa_s);
7016         } else if (os_strcmp(buf, "REATTACH") == 0) {
7017                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
7018                     !wpa_s->current_ssid)
7019                         reply_len = -1;
7020                 else {
7021                         wpa_s->reattach = 1;
7022                         wpas_request_connection(wpa_s);
7023                 }
7024         } else if (os_strcmp(buf, "RECONNECT") == 0) {
7025                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
7026                         reply_len = -1;
7027                 else if (wpa_s->disconnected)
7028                         wpas_request_connection(wpa_s);
7029 #ifdef IEEE8021X_EAPOL
7030         } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
7031                 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
7032                         reply_len = -1;
7033 #endif /* IEEE8021X_EAPOL */
7034 #ifdef CONFIG_PEERKEY
7035         } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
7036                 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
7037                         reply_len = -1;
7038 #endif /* CONFIG_PEERKEY */
7039 #ifdef CONFIG_IEEE80211R
7040         } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
7041                 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
7042                         reply_len = -1;
7043 #endif /* CONFIG_IEEE80211R */
7044 #ifdef CONFIG_WPS
7045         } else if (os_strcmp(buf, "WPS_PBC") == 0) {
7046                 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
7047                 if (res == -2) {
7048                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
7049                         reply_len = 17;
7050                 } else if (res)
7051                         reply_len = -1;
7052         } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
7053                 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
7054                 if (res == -2) {
7055                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
7056                         reply_len = 17;
7057                 } else if (res)
7058                         reply_len = -1;
7059         } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
7060                 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
7061                                                               reply,
7062                                                               reply_size);
7063         } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
7064                 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
7065                         wpa_s, buf + 14, reply, reply_size);
7066         } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
7067                 if (wpas_wps_cancel(wpa_s))
7068                         reply_len = -1;
7069 #ifdef CONFIG_WPS_NFC
7070         } else if (os_strcmp(buf, "WPS_NFC") == 0) {
7071                 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
7072                         reply_len = -1;
7073         } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
7074                 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
7075                         reply_len = -1;
7076         } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
7077                 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
7078                         wpa_s, buf + 21, reply, reply_size);
7079         } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
7080                 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
7081                         wpa_s, buf + 14, reply, reply_size);
7082         } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
7083                 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
7084                                                                buf + 17))
7085                         reply_len = -1;
7086         } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
7087                 reply_len = wpas_ctrl_nfc_get_handover_req(
7088                         wpa_s, buf + 21, reply, reply_size);
7089         } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
7090                 reply_len = wpas_ctrl_nfc_get_handover_sel(
7091                         wpa_s, buf + 21, reply, reply_size);
7092         } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
7093                 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
7094                         reply_len = -1;
7095 #endif /* CONFIG_WPS_NFC */
7096         } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
7097                 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
7098                         reply_len = -1;
7099 #ifdef CONFIG_AP
7100         } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
7101                 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
7102                         wpa_s, buf + 11, reply, reply_size);
7103 #endif /* CONFIG_AP */
7104 #ifdef CONFIG_WPS_ER
7105         } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
7106                 if (wpas_wps_er_start(wpa_s, NULL))
7107                         reply_len = -1;
7108         } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
7109                 if (wpas_wps_er_start(wpa_s, buf + 13))
7110                         reply_len = -1;
7111         } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
7112                 if (wpas_wps_er_stop(wpa_s))
7113                         reply_len = -1;
7114         } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
7115                 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
7116                         reply_len = -1;
7117         } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
7118                 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
7119                 if (ret == -2) {
7120                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
7121                         reply_len = 17;
7122                 } else if (ret == -3) {
7123                         os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
7124                         reply_len = 18;
7125                 } else if (ret == -4) {
7126                         os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
7127                         reply_len = 20;
7128                 } else if (ret)
7129                         reply_len = -1;
7130         } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
7131                 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
7132                         reply_len = -1;
7133         } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
7134                 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
7135                                                                 buf + 18))
7136                         reply_len = -1;
7137         } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
7138                 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
7139                         reply_len = -1;
7140 #ifdef CONFIG_WPS_NFC
7141         } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
7142                 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
7143                         wpa_s, buf + 24, reply, reply_size);
7144 #endif /* CONFIG_WPS_NFC */
7145 #endif /* CONFIG_WPS_ER */
7146 #endif /* CONFIG_WPS */
7147 #ifdef CONFIG_IBSS_RSN
7148         } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
7149                 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
7150                         reply_len = -1;
7151 #endif /* CONFIG_IBSS_RSN */
7152 #ifdef CONFIG_MESH
7153         } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
7154                 if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
7155                         reply_len = -1;
7156         } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
7157                 if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
7158                                                                 buf + 18))
7159                         reply_len = -1;
7160 #endif /* CONFIG_MESH */
7161 #ifdef CONFIG_P2P
7162         } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
7163                 if (p2p_ctrl_find(wpa_s, buf + 9))
7164                         reply_len = -1;
7165         } else if (os_strcmp(buf, "P2P_FIND") == 0) {
7166                 if (p2p_ctrl_find(wpa_s, ""))
7167                         reply_len = -1;
7168         } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
7169                 wpas_p2p_stop_find(wpa_s);
7170         } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
7171                 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
7172                                              reply_size);
7173         } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
7174                 if (p2p_ctrl_listen(wpa_s, buf + 11))
7175                         reply_len = -1;
7176         } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
7177                 if (p2p_ctrl_listen(wpa_s, ""))
7178                         reply_len = -1;
7179         } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
7180                 if (wpas_p2p_group_remove(wpa_s, buf + 17))
7181                         reply_len = -1;
7182         } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
7183                 if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
7184                         reply_len = -1;
7185         } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
7186                 if (p2p_ctrl_group_add(wpa_s, buf + 14))
7187                         reply_len = -1;
7188         } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
7189                 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
7190                         reply_len = -1;
7191         } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
7192                 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
7193         } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
7194                 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
7195                                                    reply_size);
7196         } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
7197                 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
7198                         reply_len = -1;
7199         } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
7200                 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
7201                         reply_len = -1;
7202         } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
7203                 wpas_p2p_sd_service_update(wpa_s);
7204         } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
7205                 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
7206                         reply_len = -1;
7207         } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
7208                 wpas_p2p_service_flush(wpa_s);
7209         } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
7210                 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
7211                         reply_len = -1;
7212         } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
7213                 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
7214                         reply_len = -1;
7215         } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
7216                 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
7217                         reply_len = -1;
7218         } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
7219                 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
7220                         reply_len = -1;
7221         } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
7222                 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
7223                                               reply_size);
7224         } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
7225                 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
7226                         reply_len = -1;
7227         } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
7228                 p2p_ctrl_flush(wpa_s);
7229         } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
7230                 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
7231                         reply_len = -1;
7232         } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
7233                 if (wpas_p2p_cancel(wpa_s))
7234                         reply_len = -1;
7235         } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
7236                 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
7237                         reply_len = -1;
7238         } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
7239                 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
7240                         reply_len = -1;
7241         } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
7242                 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
7243                         reply_len = -1;
7244         } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
7245                 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
7246                         reply_len = -1;
7247         } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
7248                 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
7249                         reply_len = -1;
7250 #endif /* CONFIG_P2P */
7251 #ifdef CONFIG_WIFI_DISPLAY
7252         } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
7253                 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
7254                         reply_len = -1;
7255         } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
7256                 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
7257                                                      reply, reply_size);
7258 #endif /* CONFIG_WIFI_DISPLAY */
7259 #ifdef CONFIG_INTERWORKING
7260         } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
7261                 if (interworking_fetch_anqp(wpa_s) < 0)
7262                         reply_len = -1;
7263         } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
7264                 interworking_stop_fetch_anqp(wpa_s);
7265         } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
7266                 if (ctrl_interworking_select(wpa_s, NULL) < 0)
7267                         reply_len = -1;
7268         } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
7269                 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
7270                         reply_len = -1;
7271         } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
7272                 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
7273                         reply_len = -1;
7274         } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
7275                 if (get_anqp(wpa_s, buf + 9) < 0)
7276                         reply_len = -1;
7277         } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
7278                 if (gas_request(wpa_s, buf + 12) < 0)
7279                         reply_len = -1;
7280         } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
7281                 reply_len = gas_response_get(wpa_s, buf + 17, reply,
7282                                              reply_size);
7283 #endif /* CONFIG_INTERWORKING */
7284 #ifdef CONFIG_HS20
7285         } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
7286                 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
7287                         reply_len = -1;
7288         } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
7289                 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
7290                         reply_len = -1;
7291         } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
7292                 if (hs20_icon_request(wpa_s, buf + 18) < 0)
7293                         reply_len = -1;
7294         } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
7295                 if (hs20_fetch_osu(wpa_s) < 0)
7296                         reply_len = -1;
7297         } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
7298                 hs20_cancel_fetch_osu(wpa_s);
7299 #endif /* CONFIG_HS20 */
7300         } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
7301         {
7302                 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
7303                             wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
7304                         reply_len = -1;
7305                 else {
7306                         /*
7307                          * Notify response from timeout to allow the control
7308                          * interface response to be sent first.
7309                          */
7310                         eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
7311                                                wpa_s, NULL);
7312                 }
7313         } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
7314                 if (wpa_supplicant_reload_configuration(wpa_s))
7315                         reply_len = -1;
7316         } else if (os_strcmp(buf, "TERMINATE") == 0) {
7317                 wpa_supplicant_terminate_proc(wpa_s->global);
7318         } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
7319                 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
7320                         reply_len = -1;
7321         } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
7322                 reply_len = wpa_supplicant_ctrl_iface_blacklist(
7323                         wpa_s, buf + 9, reply, reply_size);
7324         } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
7325                 reply_len = wpa_supplicant_ctrl_iface_log_level(
7326                         wpa_s, buf + 9, reply, reply_size);
7327         } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
7328                 reply_len = wpa_supplicant_ctrl_iface_list_networks(
7329                         wpa_s, buf + 14, reply, reply_size);
7330         } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
7331                 reply_len = wpa_supplicant_ctrl_iface_list_networks(
7332                         wpa_s, NULL, reply, reply_size);
7333         } else if (os_strcmp(buf, "DISCONNECT") == 0) {
7334 #ifdef CONFIG_SME
7335                 wpa_s->sme.prev_bssid_set = 0;
7336 #endif /* CONFIG_SME */
7337                 wpa_s->reassociate = 0;
7338                 wpa_s->disconnected = 1;
7339                 wpa_supplicant_cancel_sched_scan(wpa_s);
7340                 wpa_supplicant_cancel_scan(wpa_s);
7341                 wpa_supplicant_deauthenticate(wpa_s,
7342                                               WLAN_REASON_DEAUTH_LEAVING);
7343         } else if (os_strcmp(buf, "SCAN") == 0) {
7344                 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
7345         } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
7346                 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
7347         } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
7348                 reply_len = wpa_supplicant_ctrl_iface_scan_results(
7349                         wpa_s, reply, reply_size);
7350         } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
7351                 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
7352                         reply_len = -1;
7353         } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
7354                 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
7355                         reply_len = -1;
7356         } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
7357                 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
7358                         reply_len = -1;
7359         } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
7360                 reply_len = wpa_supplicant_ctrl_iface_add_network(
7361                         wpa_s, reply, reply_size);
7362         } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
7363                 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
7364                         reply_len = -1;
7365         } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
7366                 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
7367                         reply_len = -1;
7368         } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
7369                 reply_len = wpa_supplicant_ctrl_iface_get_network(
7370                         wpa_s, buf + 12, reply, reply_size);
7371         } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
7372                 if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12))
7373                         reply_len = -1;
7374         } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
7375                 reply_len = wpa_supplicant_ctrl_iface_list_creds(
7376                         wpa_s, reply, reply_size);
7377         } else if (os_strcmp(buf, "ADD_CRED") == 0) {
7378                 reply_len = wpa_supplicant_ctrl_iface_add_cred(
7379                         wpa_s, reply, reply_size);
7380         } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
7381                 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
7382                         reply_len = -1;
7383         } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
7384                 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
7385                         reply_len = -1;
7386         } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
7387                 reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
7388                                                                reply,
7389                                                                reply_size);
7390 #ifndef CONFIG_NO_CONFIG_WRITE
7391         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
7392                 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
7393                         reply_len = -1;
7394 #endif /* CONFIG_NO_CONFIG_WRITE */
7395         } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
7396                 reply_len = wpa_supplicant_ctrl_iface_get_capability(
7397                         wpa_s, buf + 15, reply, reply_size);
7398         } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
7399                 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
7400                         reply_len = -1;
7401         } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
7402                 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
7403                         reply_len = -1;
7404         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
7405                 reply_len = wpa_supplicant_global_iface_list(
7406                         wpa_s->global, reply, reply_size);
7407         } else if (os_strcmp(buf, "INTERFACES") == 0) {
7408                 reply_len = wpa_supplicant_global_iface_interfaces(
7409                         wpa_s->global, reply, reply_size);
7410         } else if (os_strncmp(buf, "BSS ", 4) == 0) {
7411                 reply_len = wpa_supplicant_ctrl_iface_bss(
7412                         wpa_s, buf + 4, reply, reply_size);
7413 #ifdef CONFIG_AP
7414         } else if (os_strcmp(buf, "STA-FIRST") == 0) {
7415                 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
7416         } else if (os_strncmp(buf, "STA ", 4) == 0) {
7417                 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
7418                                               reply_size);
7419         } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
7420                 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
7421                                                    reply_size);
7422         } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
7423                 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
7424                         reply_len = -1;
7425         } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
7426                 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
7427                         reply_len = -1;
7428         } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
7429                 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
7430                         reply_len = -1;
7431 #endif /* CONFIG_AP */
7432         } else if (os_strcmp(buf, "SUSPEND") == 0) {
7433                 wpas_notify_suspend(wpa_s->global);
7434         } else if (os_strcmp(buf, "RESUME") == 0) {
7435                 wpas_notify_resume(wpa_s->global);
7436 #ifdef CONFIG_TESTING_OPTIONS
7437         } else if (os_strcmp(buf, "DROP_SA") == 0) {
7438                 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
7439 #endif /* CONFIG_TESTING_OPTIONS */
7440         } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
7441                 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
7442                         reply_len = -1;
7443         } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
7444                 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
7445                         reply_len = -1;
7446         } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
7447                 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
7448                         reply_len = -1;
7449         } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
7450                 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
7451                                                                buf + 17))
7452                         reply_len = -1;
7453         } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
7454                 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
7455                         reply_len = -1;
7456 #ifdef CONFIG_TDLS
7457         } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
7458                 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
7459                         reply_len = -1;
7460         } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
7461                 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
7462                         reply_len = -1;
7463         } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
7464                 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
7465                         reply_len = -1;
7466 #endif /* CONFIG_TDLS */
7467         } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
7468                 reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
7469         } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
7470                 if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
7471                         reply_len = -1;
7472         } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
7473                 if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
7474                         reply_len = -1;
7475         } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
7476                 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
7477                                                        reply_size);
7478         } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
7479                 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
7480                                                        reply_size);
7481 #ifdef CONFIG_AUTOSCAN
7482         } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
7483                 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
7484                         reply_len = -1;
7485 #endif /* CONFIG_AUTOSCAN */
7486 #ifdef ANDROID
7487         } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
7488                 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
7489                                                       reply_size);
7490 #endif /* ANDROID */
7491         } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
7492                 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
7493                                                       reply_size);
7494         } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
7495                 pmksa_cache_clear_current(wpa_s->wpa);
7496                 eapol_sm_request_reauth(wpa_s->eapol);
7497 #ifdef CONFIG_WNM
7498         } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
7499                 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
7500                         reply_len = -1;
7501         } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
7502                 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
7503                                 reply_len = -1;
7504 #endif /* CONFIG_WNM */
7505         } else if (os_strcmp(buf, "FLUSH") == 0) {
7506                 wpa_supplicant_ctrl_iface_flush(wpa_s);
7507         } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
7508                 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
7509                                                  reply_size);
7510 #ifdef CONFIG_TESTING_OPTIONS
7511         } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
7512                 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
7513                         reply_len = -1;
7514         } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
7515                 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
7516         } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
7517                 if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
7518                         reply_len = -1;
7519         } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
7520                 if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
7521                         reply_len = -1;
7522         } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
7523                 if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
7524                         reply_len = -1;
7525         } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
7526                 if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
7527                         reply_len = -1;
7528         } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
7529                 if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
7530                         reply_len = -1;
7531 #endif /* CONFIG_TESTING_OPTIONS */
7532         } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
7533                 if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
7534                         reply_len = -1;
7535         } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
7536                 reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
7537                                                       reply_size);
7538         } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
7539                 if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
7540                         reply_len = -1;
7541         } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
7542                 if (wpas_ctrl_iface_send_neigbor_rep(wpa_s, buf + 20))
7543                         reply_len = -1;
7544         } else {
7545                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
7546                 reply_len = 16;
7547         }
7548
7549         if (reply_len < 0) {
7550                 os_memcpy(reply, "FAIL\n", 5);
7551                 reply_len = 5;
7552         }
7553
7554         *resp_len = reply_len;
7555         return reply;
7556 }
7557
7558
7559 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
7560                                            char *cmd)
7561 {
7562         struct wpa_interface iface;
7563         char *pos;
7564
7565         /*
7566          * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
7567          * TAB<bridge_ifname>
7568          */
7569         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
7570
7571         os_memset(&iface, 0, sizeof(iface));
7572
7573         do {
7574                 iface.ifname = pos = cmd;
7575                 pos = os_strchr(pos, '\t');
7576                 if (pos)
7577                         *pos++ = '\0';
7578                 if (iface.ifname[0] == '\0')
7579                         return -1;
7580                 if (pos == NULL)
7581                         break;
7582
7583                 iface.confname = pos;
7584                 pos = os_strchr(pos, '\t');
7585                 if (pos)
7586                         *pos++ = '\0';
7587                 if (iface.confname[0] == '\0')
7588                         iface.confname = NULL;
7589                 if (pos == NULL)
7590                         break;
7591
7592                 iface.driver = pos;
7593                 pos = os_strchr(pos, '\t');
7594                 if (pos)
7595                         *pos++ = '\0';
7596                 if (iface.driver[0] == '\0')
7597                         iface.driver = NULL;
7598                 if (pos == NULL)
7599                         break;
7600
7601                 iface.ctrl_interface = pos;
7602                 pos = os_strchr(pos, '\t');
7603                 if (pos)
7604                         *pos++ = '\0';
7605                 if (iface.ctrl_interface[0] == '\0')
7606                         iface.ctrl_interface = NULL;
7607                 if (pos == NULL)
7608                         break;
7609
7610                 iface.driver_param = pos;
7611                 pos = os_strchr(pos, '\t');
7612                 if (pos)
7613                         *pos++ = '\0';
7614                 if (iface.driver_param[0] == '\0')
7615                         iface.driver_param = NULL;
7616                 if (pos == NULL)
7617                         break;
7618
7619                 iface.bridge_ifname = pos;
7620                 pos = os_strchr(pos, '\t');
7621                 if (pos)
7622                         *pos++ = '\0';
7623                 if (iface.bridge_ifname[0] == '\0')
7624                         iface.bridge_ifname = NULL;
7625                 if (pos == NULL)
7626                         break;
7627         } while (0);
7628
7629         if (wpa_supplicant_get_iface(global, iface.ifname))
7630                 return -1;
7631
7632         return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
7633 }
7634
7635
7636 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
7637                                               char *cmd)
7638 {
7639         struct wpa_supplicant *wpa_s;
7640
7641         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
7642
7643         wpa_s = wpa_supplicant_get_iface(global, cmd);
7644         if (wpa_s == NULL)
7645                 return -1;
7646         return wpa_supplicant_remove_iface(global, wpa_s, 0);
7647 }
7648
7649
7650 static void wpa_free_iface_info(struct wpa_interface_info *iface)
7651 {
7652         struct wpa_interface_info *prev;
7653
7654         while (iface) {
7655                 prev = iface;
7656                 iface = iface->next;
7657
7658                 os_free(prev->ifname);
7659                 os_free(prev->desc);
7660                 os_free(prev);
7661         }
7662 }
7663
7664
7665 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
7666                                             char *buf, int len)
7667 {
7668         int i, res;
7669         struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
7670         char *pos, *end;
7671
7672         for (i = 0; wpa_drivers[i]; i++) {
7673                 struct wpa_driver_ops *drv = wpa_drivers[i];
7674                 if (drv->get_interfaces == NULL)
7675                         continue;
7676                 tmp = drv->get_interfaces(global->drv_priv[i]);
7677                 if (tmp == NULL)
7678                         continue;
7679
7680                 if (last == NULL)
7681                         iface = last = tmp;
7682                 else
7683                         last->next = tmp;
7684                 while (last->next)
7685                         last = last->next;
7686         }
7687
7688         pos = buf;
7689         end = buf + len;
7690         for (tmp = iface; tmp; tmp = tmp->next) {
7691                 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
7692                                   tmp->drv_name, tmp->ifname,
7693                                   tmp->desc ? tmp->desc : "");
7694                 if (res < 0 || res >= end - pos) {
7695                         *pos = '\0';
7696                         break;
7697                 }
7698                 pos += res;
7699         }
7700
7701         wpa_free_iface_info(iface);
7702
7703         return pos - buf;
7704 }
7705
7706
7707 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
7708                                                   char *buf, int len)
7709 {
7710         int res;
7711         char *pos, *end;
7712         struct wpa_supplicant *wpa_s;
7713
7714         wpa_s = global->ifaces;
7715         pos = buf;
7716         end = buf + len;
7717
7718         while (wpa_s) {
7719                 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
7720                 if (res < 0 || res >= end - pos) {
7721                         *pos = '\0';
7722                         break;
7723                 }
7724                 pos += res;
7725                 wpa_s = wpa_s->next;
7726         }
7727         return pos - buf;
7728 }
7729
7730
7731 static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
7732                                             const char *ifname,
7733                                             char *cmd, size_t *resp_len)
7734 {
7735         struct wpa_supplicant *wpa_s;
7736
7737         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7738                 if (os_strcmp(ifname, wpa_s->ifname) == 0)
7739                         break;
7740         }
7741
7742         if (wpa_s == NULL) {
7743                 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
7744                 if (resp)
7745                         *resp_len = os_strlen(resp);
7746                 else
7747                         *resp_len = 1;
7748                 return resp;
7749         }
7750
7751         return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
7752 }
7753
7754
7755 static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
7756                                                char *buf, size_t *resp_len)
7757 {
7758 #ifdef CONFIG_P2P
7759         static const char * cmd[] = {
7760                 "LIST_NETWORKS",
7761                 "P2P_FIND",
7762                 "P2P_STOP_FIND",
7763                 "P2P_LISTEN",
7764                 "P2P_GROUP_ADD",
7765                 "P2P_GET_PASSPHRASE",
7766                 "P2P_SERVICE_UPDATE",
7767                 "P2P_SERVICE_FLUSH",
7768                 "P2P_FLUSH",
7769                 "P2P_CANCEL",
7770                 "P2P_PRESENCE_REQ",
7771                 "P2P_EXT_LISTEN",
7772                 NULL
7773         };
7774         static const char * prefix[] = {
7775 #ifdef ANDROID
7776                 "DRIVER ",
7777 #endif /* ANDROID */
7778                 "GET_NETWORK ",
7779                 "REMOVE_NETWORK ",
7780                 "P2P_FIND ",
7781                 "P2P_CONNECT ",
7782                 "P2P_LISTEN ",
7783                 "P2P_GROUP_REMOVE ",
7784                 "P2P_GROUP_ADD ",
7785                 "P2P_PROV_DISC ",
7786                 "P2P_SERV_DISC_REQ ",
7787                 "P2P_SERV_DISC_CANCEL_REQ ",
7788                 "P2P_SERV_DISC_RESP ",
7789                 "P2P_SERV_DISC_EXTERNAL ",
7790                 "P2P_SERVICE_ADD ",
7791                 "P2P_SERVICE_DEL ",
7792                 "P2P_REJECT ",
7793                 "P2P_INVITE ",
7794                 "P2P_PEER ",
7795                 "P2P_SET ",
7796                 "P2P_UNAUTHORIZE ",
7797                 "P2P_PRESENCE_REQ ",
7798                 "P2P_EXT_LISTEN ",
7799                 "P2P_REMOVE_CLIENT ",
7800                 "NFC_GET_HANDOVER_SEL ",
7801                 "NFC_GET_HANDOVER_REQ ",
7802                 "NFC_REPORT_HANDOVER ",
7803                 NULL
7804         };
7805         int found = 0;
7806         int i;
7807
7808         if (global->p2p_init_wpa_s == NULL)
7809                 return NULL;
7810
7811         for (i = 0; !found && cmd[i]; i++) {
7812                 if (os_strcmp(buf, cmd[i]) == 0)
7813                         found = 1;
7814         }
7815
7816         for (i = 0; !found && prefix[i]; i++) {
7817                 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
7818                         found = 1;
7819         }
7820
7821         if (found)
7822                 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
7823                                                          buf, resp_len);
7824 #endif /* CONFIG_P2P */
7825         return NULL;
7826 }
7827
7828
7829 static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
7830                                                char *buf, size_t *resp_len)
7831 {
7832 #ifdef CONFIG_WIFI_DISPLAY
7833         if (global->p2p_init_wpa_s == NULL)
7834                 return NULL;
7835         if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
7836             os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
7837                 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
7838                                                          buf, resp_len);
7839 #endif /* CONFIG_WIFI_DISPLAY */
7840         return NULL;
7841 }
7842
7843
7844 static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
7845                                            char *buf, size_t *resp_len)
7846 {
7847         char *ret;
7848
7849         ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
7850         if (ret)
7851                 return ret;
7852
7853         ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
7854         if (ret)
7855                 return ret;
7856
7857         return NULL;
7858 }
7859
7860
7861 static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
7862 {
7863         char *value;
7864
7865         value = os_strchr(cmd, ' ');
7866         if (value == NULL)
7867                 return -1;
7868         *value++ = '\0';
7869
7870         wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
7871
7872 #ifdef CONFIG_WIFI_DISPLAY
7873         if (os_strcasecmp(cmd, "wifi_display") == 0) {
7874                 wifi_display_enable(global, !!atoi(value));
7875                 return 0;
7876         }
7877 #endif /* CONFIG_WIFI_DISPLAY */
7878
7879         /* Restore cmd to its original value to allow redirection */
7880         value[-1] = ' ';
7881
7882         return -1;
7883 }
7884
7885
7886 #ifndef CONFIG_NO_CONFIG_WRITE
7887 static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
7888 {
7889         int ret = 0, saved = 0;
7890         struct wpa_supplicant *wpa_s;
7891
7892         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7893                 if (!wpa_s->conf->update_config) {
7894                         wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
7895                         continue;
7896                 }
7897
7898                 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
7899                         wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
7900                         ret = 1;
7901                 } else {
7902                         wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
7903                         saved++;
7904                 }
7905         }
7906
7907         if (!saved && !ret) {
7908                 wpa_dbg(wpa_s, MSG_DEBUG,
7909                         "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
7910                 ret = 1;
7911         }
7912
7913         return ret;
7914 }
7915 #endif /* CONFIG_NO_CONFIG_WRITE */
7916
7917
7918 static int wpas_global_ctrl_iface_status(struct wpa_global *global,
7919                                          char *buf, size_t buflen)
7920 {
7921         char *pos, *end;
7922         int ret;
7923         struct wpa_supplicant *wpa_s;
7924
7925         pos = buf;
7926         end = buf + buflen;
7927
7928 #ifdef CONFIG_P2P
7929         if (global->p2p && !global->p2p_disabled) {
7930                 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
7931                                   "\n"
7932                                   "p2p_state=%s\n",
7933                                   MAC2STR(global->p2p_dev_addr),
7934                                   p2p_get_state_txt(global->p2p));
7935                 if (ret < 0 || ret >= end - pos)
7936                         return pos - buf;
7937                 pos += ret;
7938         } else if (global->p2p) {
7939                 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
7940                 if (ret < 0 || ret >= end - pos)
7941                         return pos - buf;
7942                 pos += ret;
7943         }
7944 #endif /* CONFIG_P2P */
7945
7946 #ifdef CONFIG_WIFI_DISPLAY
7947         ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
7948                           !!global->wifi_display);
7949         if (ret < 0 || ret >= end - pos)
7950                 return pos - buf;
7951         pos += ret;
7952 #endif /* CONFIG_WIFI_DISPLAY */
7953
7954         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7955                 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
7956                                   "address=" MACSTR "\n",
7957                                   wpa_s->ifname, MAC2STR(wpa_s->own_addr));
7958                 if (ret < 0 || ret >= end - pos)
7959                         return pos - buf;
7960                 pos += ret;
7961         }
7962
7963         return pos - buf;
7964 }
7965
7966
7967 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
7968                                                 char *buf, size_t *resp_len)
7969 {
7970         char *reply;
7971         const int reply_size = 2048;
7972         int reply_len;
7973         int level = MSG_DEBUG;
7974
7975         if (os_strncmp(buf, "IFNAME=", 7) == 0) {
7976                 char *pos = os_strchr(buf + 7, ' ');
7977                 if (pos) {
7978                         *pos++ = '\0';
7979                         return wpas_global_ctrl_iface_ifname(global,
7980                                                              buf + 7, pos,
7981                                                              resp_len);
7982                 }
7983         }
7984
7985         reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
7986         if (reply)
7987                 return reply;
7988
7989         if (os_strcmp(buf, "PING") == 0)
7990                 level = MSG_EXCESSIVE;
7991         wpa_hexdump_ascii(level, "RX global ctrl_iface",
7992                           (const u8 *) buf, os_strlen(buf));
7993
7994         reply = os_malloc(reply_size);
7995         if (reply == NULL) {
7996                 *resp_len = 1;
7997                 return NULL;
7998         }
7999
8000         os_memcpy(reply, "OK\n", 3);
8001         reply_len = 3;
8002
8003         if (os_strcmp(buf, "PING") == 0) {
8004                 os_memcpy(reply, "PONG\n", 5);
8005                 reply_len = 5;
8006         } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
8007                 if (wpa_supplicant_global_iface_add(global, buf + 14))
8008                         reply_len = -1;
8009         } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
8010                 if (wpa_supplicant_global_iface_remove(global, buf + 17))
8011                         reply_len = -1;
8012         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
8013                 reply_len = wpa_supplicant_global_iface_list(
8014                         global, reply, reply_size);
8015         } else if (os_strcmp(buf, "INTERFACES") == 0) {
8016                 reply_len = wpa_supplicant_global_iface_interfaces(
8017                         global, reply, reply_size);
8018         } else if (os_strcmp(buf, "TERMINATE") == 0) {
8019                 wpa_supplicant_terminate_proc(global);
8020         } else if (os_strcmp(buf, "SUSPEND") == 0) {
8021                 wpas_notify_suspend(global);
8022         } else if (os_strcmp(buf, "RESUME") == 0) {
8023                 wpas_notify_resume(global);
8024         } else if (os_strncmp(buf, "SET ", 4) == 0) {
8025                 if (wpas_global_ctrl_iface_set(global, buf + 4)) {
8026 #ifdef CONFIG_P2P
8027                         if (global->p2p_init_wpa_s) {
8028                                 os_free(reply);
8029                                 /* Check if P2P redirection would work for this
8030                                  * command. */
8031                                 return wpa_supplicant_ctrl_iface_process(
8032                                         global->p2p_init_wpa_s,
8033                                         buf, resp_len);
8034                         }
8035 #endif /* CONFIG_P2P */
8036                         reply_len = -1;
8037                 }
8038 #ifndef CONFIG_NO_CONFIG_WRITE
8039         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
8040                 if (wpas_global_ctrl_iface_save_config(global))
8041                         reply_len = -1;
8042 #endif /* CONFIG_NO_CONFIG_WRITE */
8043         } else if (os_strcmp(buf, "STATUS") == 0) {
8044                 reply_len = wpas_global_ctrl_iface_status(global, reply,
8045                                                           reply_size);
8046 #ifdef CONFIG_MODULE_TESTS
8047         } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
8048                 int wpas_module_tests(void);
8049                 if (wpas_module_tests() < 0)
8050                         reply_len = -1;
8051 #endif /* CONFIG_MODULE_TESTS */
8052         } else if (os_strncmp(buf, "RELOG", 5) == 0) {
8053                 if (wpa_debug_reopen_file() < 0)
8054                         reply_len = -1;
8055         } else {
8056                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
8057                 reply_len = 16;
8058         }
8059
8060         if (reply_len < 0) {
8061                 os_memcpy(reply, "FAIL\n", 5);
8062                 reply_len = 5;
8063         }
8064
8065         *resp_len = reply_len;
8066         return reply;
8067 }