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