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