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