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