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