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