Add 'dup_network <id_s> <id_d> <name>' command
[mech_eap.git] / wpa_supplicant / ctrl_iface.c
1 /*
2  * WPA Supplicant / Control interface (shared code for all backends)
3  * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/uuid.h"
14 #include "common/version.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ieee802_11_common.h"
17 #include "common/wpa_ctrl.h"
18 #include "eap_peer/eap.h"
19 #include "eapol_supp/eapol_supp_sm.h"
20 #include "rsn_supp/wpa.h"
21 #include "rsn_supp/preauth.h"
22 #include "rsn_supp/pmksa_cache.h"
23 #include "l2_packet/l2_packet.h"
24 #include "wps/wps.h"
25 #include "config.h"
26 #include "wpa_supplicant_i.h"
27 #include "driver_i.h"
28 #include "wps_supplicant.h"
29 #include "ibss_rsn.h"
30 #include "ap.h"
31 #include "p2p_supplicant.h"
32 #include "p2p/p2p.h"
33 #include "hs20_supplicant.h"
34 #include "wifi_display.h"
35 #include "notify.h"
36 #include "bss.h"
37 #include "scan.h"
38 #include "ctrl_iface.h"
39 #include "interworking.h"
40 #include "blacklist.h"
41 #include "autoscan.h"
42 #include "wnm_sta.h"
43 #include "offchannel.h"
44
45 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
46                                             char *buf, int len);
47 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
48                                                   char *buf, int len);
49 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
50                                         char *val);
51
52 static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
53 {
54         char *pos;
55         u8 addr[ETH_ALEN], *filter = NULL, *n;
56         size_t count = 0;
57
58         pos = val;
59         while (pos) {
60                 if (*pos == '\0')
61                         break;
62                 if (hwaddr_aton(pos, addr)) {
63                         os_free(filter);
64                         return -1;
65                 }
66                 n = os_realloc_array(filter, count + 1, ETH_ALEN);
67                 if (n == NULL) {
68                         os_free(filter);
69                         return -1;
70                 }
71                 filter = n;
72                 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
73                 count++;
74
75                 pos = os_strchr(pos, ' ');
76                 if (pos)
77                         pos++;
78         }
79
80         wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
81         os_free(wpa_s->bssid_filter);
82         wpa_s->bssid_filter = filter;
83         wpa_s->bssid_filter_count = count;
84
85         return 0;
86 }
87
88
89 static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
90 {
91         char *pos;
92         u8 addr[ETH_ALEN], *bssid = NULL, *n;
93         struct wpa_ssid_value *ssid = NULL, *ns;
94         size_t count = 0, ssid_count = 0;
95         struct wpa_ssid *c;
96
97         /*
98          * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
99          * SSID_SPEC ::= ssid <SSID_HEX>
100          * BSSID_SPEC ::= bssid <BSSID_HEX>
101          */
102
103         pos = val;
104         while (pos) {
105                 if (*pos == '\0')
106                         break;
107                 if (os_strncmp(pos, "bssid ", 6) == 0) {
108                         int res;
109                         pos += 6;
110                         res = hwaddr_aton2(pos, addr);
111                         if (res < 0) {
112                                 os_free(ssid);
113                                 os_free(bssid);
114                                 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
115                                            "BSSID value '%s'", pos);
116                                 return -1;
117                         }
118                         pos += res;
119                         n = os_realloc_array(bssid, count + 1, ETH_ALEN);
120                         if (n == NULL) {
121                                 os_free(ssid);
122                                 os_free(bssid);
123                                 return -1;
124                         }
125                         bssid = n;
126                         os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
127                         count++;
128                 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
129                         char *end;
130                         pos += 5;
131
132                         end = pos;
133                         while (*end) {
134                                 if (*end == '\0' || *end == ' ')
135                                         break;
136                                 end++;
137                         }
138
139                         ns = os_realloc_array(ssid, ssid_count + 1,
140                                               sizeof(struct wpa_ssid_value));
141                         if (ns == NULL) {
142                                 os_free(ssid);
143                                 os_free(bssid);
144                                 return -1;
145                         }
146                         ssid = ns;
147
148                         if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
149                             hexstr2bin(pos, ssid[ssid_count].ssid,
150                                        (end - pos) / 2) < 0) {
151                                 os_free(ssid);
152                                 os_free(bssid);
153                                 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
154                                            "SSID value '%s'", pos);
155                                 return -1;
156                         }
157                         ssid[ssid_count].ssid_len = (end - pos) / 2;
158                         wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
159                                           ssid[ssid_count].ssid,
160                                           ssid[ssid_count].ssid_len);
161                         ssid_count++;
162                         pos = end;
163                 } else {
164                         wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
165                                    "'%s'", pos);
166                         os_free(ssid);
167                         os_free(bssid);
168                         return -1;
169                 }
170
171                 pos = os_strchr(pos, ' ');
172                 if (pos)
173                         pos++;
174         }
175
176         wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
177         os_free(wpa_s->disallow_aps_bssid);
178         wpa_s->disallow_aps_bssid = bssid;
179         wpa_s->disallow_aps_bssid_count = count;
180
181         wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
182         os_free(wpa_s->disallow_aps_ssid);
183         wpa_s->disallow_aps_ssid = ssid;
184         wpa_s->disallow_aps_ssid_count = ssid_count;
185
186         if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
187                 return 0;
188
189         c = wpa_s->current_ssid;
190         if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
191                 return 0;
192
193         if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
194             !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
195                 return 0;
196
197         wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
198                    "because current AP was marked disallowed");
199
200 #ifdef CONFIG_SME
201         wpa_s->sme.prev_bssid_set = 0;
202 #endif /* CONFIG_SME */
203         wpa_s->reassociate = 1;
204         wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
205         wpa_supplicant_req_scan(wpa_s, 0, 0);
206
207         return 0;
208 }
209
210
211 #ifndef CONFIG_NO_CONFIG_BLOBS
212 static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
213 {
214         char *name = pos;
215         struct wpa_config_blob *blob;
216         size_t len;
217
218         pos = os_strchr(pos, ' ');
219         if (pos == NULL)
220                 return -1;
221         *pos++ = '\0';
222         len = os_strlen(pos);
223         if (len & 1)
224                 return -1;
225
226         wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
227         blob = os_zalloc(sizeof(*blob));
228         if (blob == NULL)
229                 return -1;
230         blob->name = os_strdup(name);
231         blob->data = os_malloc(len / 2);
232         if (blob->name == NULL || blob->data == NULL) {
233                 wpa_config_free_blob(blob);
234                 return -1;
235         }
236
237         if (hexstr2bin(pos, blob->data, len / 2) < 0) {
238                 wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
239                 wpa_config_free_blob(blob);
240                 return -1;
241         }
242         blob->len = len / 2;
243
244         wpa_config_set_blob(wpa_s->conf, blob);
245
246         return 0;
247 }
248 #endif /* CONFIG_NO_CONFIG_BLOBS */
249
250
251 static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
252 {
253         char *params;
254         char *pos;
255         int *freqs = NULL;
256         int ret;
257
258         if (atoi(cmd)) {
259                 params = os_strchr(cmd, ' ');
260                 os_free(wpa_s->manual_sched_scan_freqs);
261                 if (params) {
262                         params++;
263                         pos = os_strstr(params, "freq=");
264                         if (pos)
265                                 freqs = freq_range_to_channel_list(wpa_s,
266                                                                    pos + 5);
267                 }
268                 wpa_s->manual_sched_scan_freqs = freqs;
269                 ret = wpas_start_pno(wpa_s);
270         } else {
271                 ret = wpas_stop_pno(wpa_s);
272         }
273         return ret;
274 }
275
276
277 static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
278                                          char *cmd)
279 {
280         char *value;
281         int ret = 0;
282
283         value = os_strchr(cmd, ' ');
284         if (value == NULL)
285                 return -1;
286         *value++ = '\0';
287
288         wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
289         if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
290                 eapol_sm_configure(wpa_s->eapol,
291                                    atoi(value), -1, -1, -1);
292         } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
293                 eapol_sm_configure(wpa_s->eapol,
294                                    -1, atoi(value), -1, -1);
295         } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
296                 eapol_sm_configure(wpa_s->eapol,
297                                    -1, -1, atoi(value), -1);
298         } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
299                 eapol_sm_configure(wpa_s->eapol,
300                                    -1, -1, -1, atoi(value));
301         } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
302                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
303                                      atoi(value)))
304                         ret = -1;
305         } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
306                    0) {
307                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
308                                      atoi(value)))
309                         ret = -1;
310         } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
311                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
312                         ret = -1;
313         } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
314                 wpa_s->wps_fragment_size = atoi(value);
315 #ifdef CONFIG_WPS_TESTING
316         } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
317                 long int val;
318                 val = strtol(value, NULL, 0);
319                 if (val < 0 || val > 0xff) {
320                         ret = -1;
321                         wpa_printf(MSG_DEBUG, "WPS: Invalid "
322                                    "wps_version_number %ld", val);
323                 } else {
324                         wps_version_number = val;
325                         wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
326                                    "version %u.%u",
327                                    (wps_version_number & 0xf0) >> 4,
328                                    wps_version_number & 0x0f);
329                 }
330         } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
331                 wps_testing_dummy_cred = atoi(value);
332                 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
333                            wps_testing_dummy_cred);
334         } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
335                 wps_corrupt_pkhash = atoi(value);
336                 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
337                            wps_corrupt_pkhash);
338 #endif /* CONFIG_WPS_TESTING */
339         } else if (os_strcasecmp(cmd, "ampdu") == 0) {
340                 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
341                         ret = -1;
342 #ifdef CONFIG_TDLS
343 #ifdef CONFIG_TDLS_TESTING
344         } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
345                 extern unsigned int tdls_testing;
346                 tdls_testing = strtol(value, NULL, 0);
347                 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
348 #endif /* CONFIG_TDLS_TESTING */
349         } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
350                 int disabled = atoi(value);
351                 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
352                 if (disabled) {
353                         if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
354                                 ret = -1;
355                 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
356                         ret = -1;
357                 wpa_tdls_enable(wpa_s->wpa, !disabled);
358 #endif /* CONFIG_TDLS */
359         } else if (os_strcasecmp(cmd, "pno") == 0) {
360                 ret = wpas_ctrl_pno(wpa_s, value);
361         } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
362                 int disabled = atoi(value);
363                 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
364                         ret = -1;
365                 else if (disabled)
366                         wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
367         } else if (os_strcasecmp(cmd, "uapsd") == 0) {
368                 if (os_strcmp(value, "disable") == 0)
369                         wpa_s->set_sta_uapsd = 0;
370                 else {
371                         int be, bk, vi, vo;
372                         char *pos;
373                         /* format: BE,BK,VI,VO;max SP Length */
374                         be = atoi(value);
375                         pos = os_strchr(value, ',');
376                         if (pos == NULL)
377                                 return -1;
378                         pos++;
379                         bk = atoi(pos);
380                         pos = os_strchr(pos, ',');
381                         if (pos == NULL)
382                                 return -1;
383                         pos++;
384                         vi = atoi(pos);
385                         pos = os_strchr(pos, ',');
386                         if (pos == NULL)
387                                 return -1;
388                         pos++;
389                         vo = atoi(pos);
390                         /* ignore max SP Length for now */
391
392                         wpa_s->set_sta_uapsd = 1;
393                         wpa_s->sta_uapsd = 0;
394                         if (be)
395                                 wpa_s->sta_uapsd |= BIT(0);
396                         if (bk)
397                                 wpa_s->sta_uapsd |= BIT(1);
398                         if (vi)
399                                 wpa_s->sta_uapsd |= BIT(2);
400                         if (vo)
401                                 wpa_s->sta_uapsd |= BIT(3);
402                 }
403         } else if (os_strcasecmp(cmd, "ps") == 0) {
404                 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
405 #ifdef CONFIG_WIFI_DISPLAY
406         } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
407                 int enabled = !!atoi(value);
408                 if (enabled && !wpa_s->global->p2p)
409                         ret = -1;
410                 else
411                         wifi_display_enable(wpa_s->global, enabled);
412 #endif /* CONFIG_WIFI_DISPLAY */
413         } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
414                 ret = set_bssid_filter(wpa_s, value);
415         } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
416                 ret = set_disallow_aps(wpa_s, value);
417         } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
418                 wpa_s->no_keep_alive = !!atoi(value);
419 #ifdef CONFIG_TESTING_OPTIONS
420         } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
421                 wpa_s->ext_mgmt_frame_handling = !!atoi(value);
422 #endif /* CONFIG_TESTING_OPTIONS */
423 #ifndef CONFIG_NO_CONFIG_BLOBS
424         } else if (os_strcmp(cmd, "blob") == 0) {
425                 ret = wpas_ctrl_set_blob(wpa_s, value);
426 #endif /* CONFIG_NO_CONFIG_BLOBS */
427         } else {
428                 value[-1] = '=';
429                 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
430                 if (ret == 0)
431                         wpa_supplicant_update_config(wpa_s);
432         }
433
434         return ret;
435 }
436
437
438 static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
439                                          char *cmd, char *buf, size_t buflen)
440 {
441         int res = -1;
442
443         wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
444
445         if (os_strcmp(cmd, "version") == 0) {
446                 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
447         } else if (os_strcasecmp(cmd, "country") == 0) {
448                 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
449                         res = os_snprintf(buf, buflen, "%c%c",
450                                           wpa_s->conf->country[0],
451                                           wpa_s->conf->country[1]);
452 #ifdef CONFIG_WIFI_DISPLAY
453         } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
454                 int enabled;
455                 if (wpa_s->global->p2p == NULL ||
456                     wpa_s->global->p2p_disabled)
457                         enabled = 0;
458                 else
459                         enabled = wpa_s->global->wifi_display;
460                 res = os_snprintf(buf, buflen, "%d", enabled);
461                 if (res < 0 || (unsigned int) res >= buflen)
462                         return -1;
463                 return res;
464 #endif /* CONFIG_WIFI_DISPLAY */
465 #ifdef CONFIG_TESTING_GET_GTK
466         } else if (os_strcmp(cmd, "gtk") == 0) {
467                 if (wpa_s->last_gtk_len == 0)
468                         return -1;
469                 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
470                                        wpa_s->last_gtk_len);
471                 return res;
472 #endif /* CONFIG_TESTING_GET_GTK */
473         }
474
475         if (res < 0 || (unsigned int) res >= buflen)
476                 return -1;
477         return res;
478 }
479
480
481 #ifdef IEEE8021X_EAPOL
482 static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
483                                              char *addr)
484 {
485         u8 bssid[ETH_ALEN];
486         struct wpa_ssid *ssid = wpa_s->current_ssid;
487
488         if (hwaddr_aton(addr, bssid)) {
489                 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
490                            "'%s'", addr);
491                 return -1;
492         }
493
494         wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
495         rsn_preauth_deinit(wpa_s->wpa);
496         if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
497                 return -1;
498
499         return 0;
500 }
501 #endif /* IEEE8021X_EAPOL */
502
503
504 #ifdef CONFIG_PEERKEY
505 /* MLME-STKSTART.request(peer) */
506 static int wpa_supplicant_ctrl_iface_stkstart(
507         struct wpa_supplicant *wpa_s, char *addr)
508 {
509         u8 peer[ETH_ALEN];
510
511         if (hwaddr_aton(addr, peer)) {
512                 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
513                            "address '%s'", addr);
514                 return -1;
515         }
516
517         wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
518                    MAC2STR(peer));
519
520         return wpa_sm_stkstart(wpa_s->wpa, peer);
521 }
522 #endif /* CONFIG_PEERKEY */
523
524
525 #ifdef CONFIG_TDLS
526
527 static int wpa_supplicant_ctrl_iface_tdls_discover(
528         struct wpa_supplicant *wpa_s, char *addr)
529 {
530         u8 peer[ETH_ALEN];
531         int ret;
532
533         if (hwaddr_aton(addr, peer)) {
534                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
535                            "address '%s'", addr);
536                 return -1;
537         }
538
539         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
540                    MAC2STR(peer));
541
542         if (wpa_tdls_is_external_setup(wpa_s->wpa))
543                 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
544         else
545                 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
546
547         return ret;
548 }
549
550
551 static int wpa_supplicant_ctrl_iface_tdls_setup(
552         struct wpa_supplicant *wpa_s, char *addr)
553 {
554         u8 peer[ETH_ALEN];
555         int ret;
556
557         if (hwaddr_aton(addr, peer)) {
558                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
559                            "address '%s'", addr);
560                 return -1;
561         }
562
563         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
564                    MAC2STR(peer));
565
566         if ((wpa_s->conf->tdls_external_control) &&
567             wpa_tdls_is_external_setup(wpa_s->wpa))
568                 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
569
570         wpa_tdls_remove(wpa_s->wpa, peer);
571
572         if (wpa_tdls_is_external_setup(wpa_s->wpa))
573                 ret = wpa_tdls_start(wpa_s->wpa, peer);
574         else
575                 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
576
577         return ret;
578 }
579
580
581 static int wpa_supplicant_ctrl_iface_tdls_teardown(
582         struct wpa_supplicant *wpa_s, char *addr)
583 {
584         u8 peer[ETH_ALEN];
585         int ret;
586
587         if (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_update_network(
2499         struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2500         char *name, char *value)
2501 {
2502         if (wpa_config_set(ssid, name, value, 0) < 0) {
2503                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2504                            "variable '%s'", name);
2505                 return -1;
2506         }
2507
2508         if (os_strcmp(name, "bssid") != 0 &&
2509             os_strcmp(name, "priority") != 0)
2510                 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
2511
2512         if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
2513                 /*
2514                  * Invalidate the EAP session cache if anything in the current
2515                  * or previously used configuration changes.
2516                  */
2517                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2518         }
2519
2520         if ((os_strcmp(name, "psk") == 0 &&
2521              value[0] == '"' && ssid->ssid_len) ||
2522             (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2523                 wpa_config_update_psk(ssid);
2524         else if (os_strcmp(name, "priority") == 0)
2525                 wpa_config_update_prio_list(wpa_s->conf);
2526
2527         return 0;
2528 }
2529
2530
2531 static int wpa_supplicant_ctrl_iface_set_network(
2532         struct wpa_supplicant *wpa_s, char *cmd)
2533 {
2534         int id;
2535         struct wpa_ssid *ssid;
2536         char *name, *value;
2537
2538         /* cmd: "<network id> <variable name> <value>" */
2539         name = os_strchr(cmd, ' ');
2540         if (name == NULL)
2541                 return -1;
2542         *name++ = '\0';
2543
2544         value = os_strchr(name, ' ');
2545         if (value == NULL)
2546                 return -1;
2547         *value++ = '\0';
2548
2549         id = atoi(cmd);
2550         wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2551                    id, name);
2552         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2553                               (u8 *) value, os_strlen(value));
2554
2555         ssid = wpa_config_get_network(wpa_s->conf, id);
2556         if (ssid == NULL) {
2557                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2558                            "id=%d", id);
2559                 return -1;
2560         }
2561
2562         return wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
2563                                                         value);
2564 }
2565
2566
2567 static int wpa_supplicant_ctrl_iface_get_network(
2568         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2569 {
2570         int id;
2571         size_t res;
2572         struct wpa_ssid *ssid;
2573         char *name, *value;
2574
2575         /* cmd: "<network id> <variable name>" */
2576         name = os_strchr(cmd, ' ');
2577         if (name == NULL || buflen == 0)
2578                 return -1;
2579         *name++ = '\0';
2580
2581         id = atoi(cmd);
2582         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2583                    id, name);
2584
2585         ssid = wpa_config_get_network(wpa_s->conf, id);
2586         if (ssid == NULL) {
2587                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2588                            "id=%d", id);
2589                 return -1;
2590         }
2591
2592         value = wpa_config_get_no_key(ssid, name);
2593         if (value == NULL) {
2594                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2595                            "variable '%s'", name);
2596                 return -1;
2597         }
2598
2599         res = os_strlcpy(buf, value, buflen);
2600         if (res >= buflen) {
2601                 os_free(value);
2602                 return -1;
2603         }
2604
2605         os_free(value);
2606
2607         return res;
2608 }
2609
2610
2611 static int wpa_supplicant_ctrl_iface_dup_network(
2612         struct wpa_supplicant *wpa_s, char *cmd)
2613 {
2614         struct wpa_ssid *ssid_s, *ssid_d;
2615         char *name, *id, *value;
2616         int id_s, id_d, ret;
2617
2618         /* cmd: "<src network id> <dst network id> <variable name>" */
2619         id = os_strchr(cmd, ' ');
2620         if (id == NULL)
2621                 return -1;
2622         *id++ = '\0';
2623
2624         name = os_strchr(id, ' ');
2625         if (name == NULL)
2626                 return -1;
2627         *name++ = '\0';
2628
2629         id_s = atoi(cmd);
2630         id_d = atoi(id);
2631         wpa_printf(MSG_DEBUG, "CTRL_IFACE: DUP_NETWORK id=%d -> %d name='%s'",
2632                    id_s, id_d, name);
2633
2634         ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
2635         if (ssid_s == NULL) {
2636                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2637                            "network id=%d", id_s);
2638                 return -1;
2639         }
2640
2641         ssid_d = wpa_config_get_network(wpa_s->conf, id_d);
2642         if (ssid_d == NULL) {
2643                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2644                            "network id=%d", id_s);
2645                 return -1;
2646         }
2647
2648         value = wpa_config_get(ssid_s, name);
2649         if (value == NULL) {
2650                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2651                            "variable '%s'", name);
2652                 return -1;
2653         }
2654
2655         ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid_d, name,
2656                                                        value);
2657
2658         os_free(value);
2659
2660         return ret;
2661 }
2662
2663
2664 static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2665                                                 char *buf, size_t buflen)
2666 {
2667         char *pos, *end;
2668         struct wpa_cred *cred;
2669         int ret;
2670
2671         pos = buf;
2672         end = buf + buflen;
2673         ret = os_snprintf(pos, end - pos,
2674                           "cred id / realm / username / domain / imsi\n");
2675         if (ret < 0 || ret >= end - pos)
2676                 return pos - buf;
2677         pos += ret;
2678
2679         cred = wpa_s->conf->cred;
2680         while (cred) {
2681                 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2682                                   cred->id, cred->realm ? cred->realm : "",
2683                                   cred->username ? cred->username : "",
2684                                   cred->domain ? cred->domain[0] : "",
2685                                   cred->imsi ? cred->imsi : "");
2686                 if (ret < 0 || ret >= end - pos)
2687                         return pos - buf;
2688                 pos += ret;
2689
2690                 cred = cred->next;
2691         }
2692
2693         return pos - buf;
2694 }
2695
2696
2697 static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2698                                               char *buf, size_t buflen)
2699 {
2700         struct wpa_cred *cred;
2701         int ret;
2702
2703         wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2704
2705         cred = wpa_config_add_cred(wpa_s->conf);
2706         if (cred == NULL)
2707                 return -1;
2708
2709         wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
2710
2711         ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2712         if (ret < 0 || (size_t) ret >= buflen)
2713                 return -1;
2714         return ret;
2715 }
2716
2717
2718 static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2719                                  struct wpa_cred *cred)
2720 {
2721         struct wpa_ssid *ssid;
2722         char str[20];
2723         int id;
2724
2725         if (cred == NULL) {
2726                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2727                 return -1;
2728         }
2729
2730         id = cred->id;
2731         if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
2732                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2733                 return -1;
2734         }
2735
2736         wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
2737
2738         /* Remove any network entry created based on the removed credential */
2739         ssid = wpa_s->conf->ssid;
2740         while (ssid) {
2741                 if (ssid->parent_cred == cred) {
2742                         wpa_printf(MSG_DEBUG, "Remove network id %d since it "
2743                                    "used the removed credential", ssid->id);
2744                         os_snprintf(str, sizeof(str), "%d", ssid->id);
2745                         ssid = ssid->next;
2746                         wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
2747                 } else
2748                         ssid = ssid->next;
2749         }
2750
2751         return 0;
2752 }
2753
2754
2755 static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2756                                                  char *cmd)
2757 {
2758         int id;
2759         struct wpa_cred *cred, *prev;
2760
2761         /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
2762          * "provisioning_sp=<FQDN> */
2763         if (os_strcmp(cmd, "all") == 0) {
2764                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2765                 cred = wpa_s->conf->cred;
2766                 while (cred) {
2767                         prev = cred;
2768                         cred = cred->next;
2769                         wpas_ctrl_remove_cred(wpa_s, prev);
2770                 }
2771                 return 0;
2772         }
2773
2774         if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
2775                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
2776                            cmd + 8);
2777                 cred = wpa_s->conf->cred;
2778                 while (cred) {
2779                         prev = cred;
2780                         cred = cred->next;
2781                         if (prev->domain) {
2782                                 size_t i;
2783                                 for (i = 0; i < prev->num_domain; i++) {
2784                                         if (os_strcmp(prev->domain[i], cmd + 8)
2785                                             != 0)
2786                                                 continue;
2787                                         wpas_ctrl_remove_cred(wpa_s, prev);
2788                                         break;
2789                                 }
2790                         }
2791                 }
2792                 return 0;
2793         }
2794
2795         if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
2796                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
2797                            cmd + 16);
2798                 cred = wpa_s->conf->cred;
2799                 while (cred) {
2800                         prev = cred;
2801                         cred = cred->next;
2802                         if (prev->provisioning_sp &&
2803                             os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
2804                                 wpas_ctrl_remove_cred(wpa_s, prev);
2805                 }
2806                 return 0;
2807         }
2808
2809         id = atoi(cmd);
2810         wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2811
2812         cred = wpa_config_get_cred(wpa_s->conf, id);
2813         return wpas_ctrl_remove_cred(wpa_s, cred);
2814 }
2815
2816
2817 static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2818                                               char *cmd)
2819 {
2820         int id;
2821         struct wpa_cred *cred;
2822         char *name, *value;
2823
2824         /* cmd: "<cred id> <variable name> <value>" */
2825         name = os_strchr(cmd, ' ');
2826         if (name == NULL)
2827                 return -1;
2828         *name++ = '\0';
2829
2830         value = os_strchr(name, ' ');
2831         if (value == NULL)
2832                 return -1;
2833         *value++ = '\0';
2834
2835         id = atoi(cmd);
2836         wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2837                    id, name);
2838         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2839                               (u8 *) value, os_strlen(value));
2840
2841         cred = wpa_config_get_cred(wpa_s->conf, id);
2842         if (cred == NULL) {
2843                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2844                            id);
2845                 return -1;
2846         }
2847
2848         if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2849                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2850                            "variable '%s'", name);
2851                 return -1;
2852         }
2853
2854         wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
2855
2856         return 0;
2857 }
2858
2859
2860 static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
2861                                               char *cmd, char *buf,
2862                                               size_t buflen)
2863 {
2864         int id;
2865         size_t res;
2866         struct wpa_cred *cred;
2867         char *name, *value;
2868
2869         /* cmd: "<cred id> <variable name>" */
2870         name = os_strchr(cmd, ' ');
2871         if (name == NULL)
2872                 return -1;
2873         *name++ = '\0';
2874
2875         id = atoi(cmd);
2876         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
2877                    id, name);
2878
2879         cred = wpa_config_get_cred(wpa_s->conf, id);
2880         if (cred == NULL) {
2881                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2882                            id);
2883                 return -1;
2884         }
2885
2886         value = wpa_config_get_cred_no_key(cred, name);
2887         if (value == NULL) {
2888                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
2889                            name);
2890                 return -1;
2891         }
2892
2893         res = os_strlcpy(buf, value, buflen);
2894         if (res >= buflen) {
2895                 os_free(value);
2896                 return -1;
2897         }
2898
2899         os_free(value);
2900
2901         return res;
2902 }
2903
2904
2905 #ifndef CONFIG_NO_CONFIG_WRITE
2906 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2907 {
2908         int ret;
2909
2910         if (!wpa_s->conf->update_config) {
2911                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2912                            "to update configuration (update_config=0)");
2913                 return -1;
2914         }
2915
2916         ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2917         if (ret) {
2918                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2919                            "update configuration");
2920         } else {
2921                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2922                            " updated");
2923         }
2924
2925         return ret;
2926 }
2927 #endif /* CONFIG_NO_CONFIG_WRITE */
2928
2929
2930 struct cipher_info {
2931         unsigned int capa;
2932         const char *name;
2933         int group_only;
2934 };
2935
2936 static const struct cipher_info ciphers[] = {
2937         { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
2938         { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
2939         { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
2940         { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
2941         { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
2942         { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
2943         { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
2944         { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
2945 };
2946
2947
2948 static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2949                                               struct wpa_driver_capa *capa,
2950                                               char *buf, size_t buflen)
2951 {
2952         int ret;
2953         char *pos, *end;
2954         size_t len;
2955         unsigned int i;
2956
2957         pos = buf;
2958         end = pos + buflen;
2959
2960         if (res < 0) {
2961                 if (strict)
2962                         return 0;
2963                 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2964                 if (len >= buflen)
2965                         return -1;
2966                 return len;
2967         }
2968
2969         for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
2970                 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
2971                         ret = os_snprintf(pos, end - pos, "%s%s",
2972                                           pos == buf ? "" : " ",
2973                                           ciphers[i].name);
2974                         if (ret < 0 || ret >= end - pos)
2975                                 return pos - buf;
2976                         pos += ret;
2977                 }
2978         }
2979
2980         return pos - buf;
2981 }
2982
2983
2984 static int ctrl_iface_get_capability_group(int res, char *strict,
2985                                            struct wpa_driver_capa *capa,
2986                                            char *buf, size_t buflen)
2987 {
2988         int ret;
2989         char *pos, *end;
2990         size_t len;
2991         unsigned int i;
2992
2993         pos = buf;
2994         end = pos + buflen;
2995
2996         if (res < 0) {
2997                 if (strict)
2998                         return 0;
2999                 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
3000                 if (len >= buflen)
3001                         return -1;
3002                 return len;
3003         }
3004
3005         for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3006                 if (capa->enc & ciphers[i].capa) {
3007                         ret = os_snprintf(pos, end - pos, "%s%s",
3008                                           pos == buf ? "" : " ",
3009                                           ciphers[i].name);
3010                         if (ret < 0 || ret >= end - pos)
3011                                 return pos - buf;
3012                         pos += ret;
3013                 }
3014         }
3015
3016         return pos - buf;
3017 }
3018
3019
3020 static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
3021                                               struct wpa_driver_capa *capa,
3022                                               char *buf, size_t buflen)
3023 {
3024         int ret;
3025         char *pos, *end;
3026         size_t len;
3027
3028         pos = buf;
3029         end = pos + buflen;
3030
3031         if (res < 0) {
3032                 if (strict)
3033                         return 0;
3034                 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
3035                                  "NONE", buflen);
3036                 if (len >= buflen)
3037                         return -1;
3038                 return len;
3039         }
3040
3041         ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
3042         if (ret < 0 || ret >= end - pos)
3043                 return pos - buf;
3044         pos += ret;
3045
3046         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3047                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
3048                 ret = os_snprintf(pos, end - pos, " WPA-EAP");
3049                 if (ret < 0 || ret >= end - pos)
3050                         return pos - buf;
3051                 pos += ret;
3052         }
3053
3054         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3055                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
3056                 ret = os_snprintf(pos, end - pos, " WPA-PSK");
3057                 if (ret < 0 || ret >= end - pos)
3058                         return pos - buf;
3059                 pos += ret;
3060         }
3061
3062         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
3063                 ret = os_snprintf(pos, end - pos, " WPA-NONE");
3064                 if (ret < 0 || ret >= end - pos)
3065                         return pos - buf;
3066                 pos += ret;
3067         }
3068
3069         return pos - buf;
3070 }
3071
3072
3073 static int ctrl_iface_get_capability_proto(int res, char *strict,
3074                                            struct wpa_driver_capa *capa,
3075                                            char *buf, size_t buflen)
3076 {
3077         int ret;
3078         char *pos, *end;
3079         size_t len;
3080
3081         pos = buf;
3082         end = pos + buflen;
3083
3084         if (res < 0) {
3085                 if (strict)
3086                         return 0;
3087                 len = os_strlcpy(buf, "RSN WPA", buflen);
3088                 if (len >= buflen)
3089                         return -1;
3090                 return len;
3091         }
3092
3093         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3094                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
3095                 ret = os_snprintf(pos, end - pos, "%sRSN",
3096                                   pos == buf ? "" : " ");
3097                 if (ret < 0 || ret >= end - pos)
3098                         return pos - buf;
3099                 pos += ret;
3100         }
3101
3102         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3103                               WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
3104                 ret = os_snprintf(pos, end - pos, "%sWPA",
3105                                   pos == buf ? "" : " ");
3106                 if (ret < 0 || ret >= end - pos)
3107                         return pos - buf;
3108                 pos += ret;
3109         }
3110
3111         return pos - buf;
3112 }
3113
3114
3115 static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
3116                                               struct wpa_driver_capa *capa,
3117                                               char *buf, size_t buflen)
3118 {
3119         int ret;
3120         char *pos, *end;
3121         size_t len;
3122
3123         pos = buf;
3124         end = pos + buflen;
3125
3126         if (res < 0) {
3127                 if (strict)
3128                         return 0;
3129                 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
3130                 if (len >= buflen)
3131                         return -1;
3132                 return len;
3133         }
3134
3135         if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
3136                 ret = os_snprintf(pos, end - pos, "%sOPEN",
3137                                   pos == buf ? "" : " ");
3138                 if (ret < 0 || ret >= end - pos)
3139                         return pos - buf;
3140                 pos += ret;
3141         }
3142
3143         if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
3144                 ret = os_snprintf(pos, end - pos, "%sSHARED",
3145                                   pos == buf ? "" : " ");
3146                 if (ret < 0 || ret >= end - pos)
3147                         return pos - buf;
3148                 pos += ret;
3149         }
3150
3151         if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
3152                 ret = os_snprintf(pos, end - pos, "%sLEAP",
3153                                   pos == buf ? "" : " ");
3154                 if (ret < 0 || ret >= end - pos)
3155                         return pos - buf;
3156                 pos += ret;
3157         }
3158
3159         return pos - buf;
3160 }
3161
3162
3163 static int ctrl_iface_get_capability_modes(int res, char *strict,
3164                                            struct wpa_driver_capa *capa,
3165                                            char *buf, size_t buflen)
3166 {
3167         int ret;
3168         char *pos, *end;
3169         size_t len;
3170
3171         pos = buf;
3172         end = pos + buflen;
3173
3174         if (res < 0) {
3175                 if (strict)
3176                         return 0;
3177                 len = os_strlcpy(buf, "IBSS AP", buflen);
3178                 if (len >= buflen)
3179                         return -1;
3180                 return len;
3181         }
3182
3183         if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
3184                 ret = os_snprintf(pos, end - pos, "%sIBSS",
3185                                   pos == buf ? "" : " ");
3186                 if (ret < 0 || ret >= end - pos)
3187                         return pos - buf;
3188                 pos += ret;
3189         }
3190
3191         if (capa->flags & WPA_DRIVER_FLAGS_AP) {
3192                 ret = os_snprintf(pos, end - pos, "%sAP",
3193                                   pos == buf ? "" : " ");
3194                 if (ret < 0 || ret >= end - pos)
3195                         return pos - buf;
3196                 pos += ret;
3197         }
3198
3199         return pos - buf;
3200 }
3201
3202
3203 static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
3204                                               char *buf, size_t buflen)
3205 {
3206         struct hostapd_channel_data *chnl;
3207         int ret, i, j;
3208         char *pos, *end, *hmode;
3209
3210         pos = buf;
3211         end = pos + buflen;
3212
3213         for (j = 0; j < wpa_s->hw.num_modes; j++) {
3214                 switch (wpa_s->hw.modes[j].mode) {
3215                 case HOSTAPD_MODE_IEEE80211B:
3216                         hmode = "B";
3217                         break;
3218                 case HOSTAPD_MODE_IEEE80211G:
3219                         hmode = "G";
3220                         break;
3221                 case HOSTAPD_MODE_IEEE80211A:
3222                         hmode = "A";
3223                         break;
3224                 case HOSTAPD_MODE_IEEE80211AD:
3225                         hmode = "AD";
3226                         break;
3227                 default:
3228                         continue;
3229                 }
3230                 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
3231                 if (ret < 0 || ret >= end - pos)
3232                         return pos - buf;
3233                 pos += ret;
3234                 chnl = wpa_s->hw.modes[j].channels;
3235                 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3236                         if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3237                                 continue;
3238                         ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
3239                         if (ret < 0 || ret >= end - pos)
3240                                 return pos - buf;
3241                         pos += ret;
3242                 }
3243                 ret = os_snprintf(pos, end - pos, "\n");
3244                 if (ret < 0 || ret >= end - pos)
3245                         return pos - buf;
3246                 pos += ret;
3247         }
3248
3249         return pos - buf;
3250 }
3251
3252
3253 static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3254                                           char *buf, size_t buflen)
3255 {
3256         struct hostapd_channel_data *chnl;
3257         int ret, i, j;
3258         char *pos, *end, *hmode;
3259
3260         pos = buf;
3261         end = pos + buflen;
3262
3263         for (j = 0; j < wpa_s->hw.num_modes; j++) {
3264                 switch (wpa_s->hw.modes[j].mode) {
3265                 case HOSTAPD_MODE_IEEE80211B:
3266                         hmode = "B";
3267                         break;
3268                 case HOSTAPD_MODE_IEEE80211G:
3269                         hmode = "G";
3270                         break;
3271                 case HOSTAPD_MODE_IEEE80211A:
3272                         hmode = "A";
3273                         break;
3274                 case HOSTAPD_MODE_IEEE80211AD:
3275                         hmode = "AD";
3276                         break;
3277                 default:
3278                         continue;
3279                 }
3280                 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3281                                   hmode);
3282                 if (ret < 0 || ret >= end - pos)
3283                         return pos - buf;
3284                 pos += ret;
3285                 chnl = wpa_s->hw.modes[j].channels;
3286                 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3287                         if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3288                                 continue;
3289                         ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
3290                                           chnl[i].chan, chnl[i].freq,
3291                                           chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
3292                                           " (NO_IBSS)" : "",
3293                                           chnl[i].flag & HOSTAPD_CHAN_RADAR ?
3294                                           " (DFS)" : "");
3295
3296                         if (ret < 0 || ret >= end - pos)
3297                                 return pos - buf;
3298                         pos += ret;
3299                 }
3300                 ret = os_snprintf(pos, end - pos, "\n");
3301                 if (ret < 0 || ret >= end - pos)
3302                         return pos - buf;
3303                 pos += ret;
3304         }
3305
3306         return pos - buf;
3307 }
3308
3309
3310 static int wpa_supplicant_ctrl_iface_get_capability(
3311         struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3312         size_t buflen)
3313 {
3314         struct wpa_driver_capa capa;
3315         int res;
3316         char *strict;
3317         char field[30];
3318         size_t len;
3319
3320         /* Determine whether or not strict checking was requested */
3321         len = os_strlcpy(field, _field, sizeof(field));
3322         if (len >= sizeof(field))
3323                 return -1;
3324         strict = os_strchr(field, ' ');
3325         if (strict != NULL) {
3326                 *strict++ = '\0';
3327                 if (os_strcmp(strict, "strict") != 0)
3328                         return -1;
3329         }
3330
3331         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3332                 field, strict ? strict : "");
3333
3334         if (os_strcmp(field, "eap") == 0) {
3335                 return eap_get_names(buf, buflen);
3336         }
3337
3338         res = wpa_drv_get_capa(wpa_s, &capa);
3339
3340         if (os_strcmp(field, "pairwise") == 0)
3341                 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3342                                                           buf, buflen);
3343
3344         if (os_strcmp(field, "group") == 0)
3345                 return ctrl_iface_get_capability_group(res, strict, &capa,
3346                                                        buf, buflen);
3347
3348         if (os_strcmp(field, "key_mgmt") == 0)
3349                 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3350                                                           buf, buflen);
3351
3352         if (os_strcmp(field, "proto") == 0)
3353                 return ctrl_iface_get_capability_proto(res, strict, &capa,
3354                                                        buf, buflen);
3355
3356         if (os_strcmp(field, "auth_alg") == 0)
3357                 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
3358                                                           buf, buflen);
3359
3360         if (os_strcmp(field, "modes") == 0)
3361                 return ctrl_iface_get_capability_modes(res, strict, &capa,
3362                                                        buf, buflen);
3363
3364         if (os_strcmp(field, "channels") == 0)
3365                 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3366
3367         if (os_strcmp(field, "freq") == 0)
3368                 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3369
3370 #ifdef CONFIG_TDLS
3371         if (os_strcmp(field, "tdls") == 0)
3372                 return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
3373 #endif /* CONFIG_TDLS */
3374
3375         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3376                    field);
3377
3378         return -1;
3379 }
3380
3381
3382 #ifdef CONFIG_INTERWORKING
3383 static char * anqp_add_hex(char *pos, char *end, const char *title,
3384                            struct wpabuf *data)
3385 {
3386         char *start = pos;
3387         size_t i;
3388         int ret;
3389         const u8 *d;
3390
3391         if (data == NULL)
3392                 return start;
3393
3394         ret = os_snprintf(pos, end - pos, "%s=", title);
3395         if (ret < 0 || ret >= end - pos)
3396                 return start;
3397         pos += ret;
3398
3399         d = wpabuf_head_u8(data);
3400         for (i = 0; i < wpabuf_len(data); i++) {
3401                 ret = os_snprintf(pos, end - pos, "%02x", *d++);
3402                 if (ret < 0 || ret >= end - pos)
3403                         return start;
3404                 pos += ret;
3405         }
3406
3407         ret = os_snprintf(pos, end - pos, "\n");
3408         if (ret < 0 || ret >= end - pos)
3409                 return start;
3410         pos += ret;
3411
3412         return pos;
3413 }
3414 #endif /* CONFIG_INTERWORKING */
3415
3416
3417 static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3418                           unsigned long mask, char *buf, size_t buflen)
3419 {
3420         size_t i;
3421         int ret;
3422         char *pos, *end;
3423         const u8 *ie, *ie2;
3424
3425         pos = buf;
3426         end = buf + buflen;
3427
3428         if (mask & WPA_BSS_MASK_ID) {
3429                 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
3430                 if (ret < 0 || ret >= end - pos)
3431                         return 0;
3432                 pos += ret;
3433         }
3434
3435         if (mask & WPA_BSS_MASK_BSSID) {
3436                 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3437                                   MAC2STR(bss->bssid));
3438                 if (ret < 0 || ret >= end - pos)
3439                         return 0;
3440                 pos += ret;
3441         }
3442
3443         if (mask & WPA_BSS_MASK_FREQ) {
3444                 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
3445                 if (ret < 0 || ret >= end - pos)
3446                         return 0;
3447                 pos += ret;
3448         }
3449
3450         if (mask & WPA_BSS_MASK_BEACON_INT) {
3451                 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3452                                   bss->beacon_int);
3453                 if (ret < 0 || ret >= end - pos)
3454                         return 0;
3455                 pos += ret;
3456         }
3457
3458         if (mask & WPA_BSS_MASK_CAPABILITIES) {
3459                 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3460                                   bss->caps);
3461                 if (ret < 0 || ret >= end - pos)
3462                         return 0;
3463                 pos += ret;
3464         }
3465
3466         if (mask & WPA_BSS_MASK_QUAL) {
3467                 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
3468                 if (ret < 0 || ret >= end - pos)
3469                         return 0;
3470                 pos += ret;
3471         }
3472
3473         if (mask & WPA_BSS_MASK_NOISE) {
3474                 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
3475                 if (ret < 0 || ret >= end - pos)
3476                         return 0;
3477                 pos += ret;
3478         }
3479
3480         if (mask & WPA_BSS_MASK_LEVEL) {
3481                 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
3482                 if (ret < 0 || ret >= end - pos)
3483                         return 0;
3484                 pos += ret;
3485         }
3486
3487         if (mask & WPA_BSS_MASK_TSF) {
3488                 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3489                                   (unsigned long long) bss->tsf);
3490                 if (ret < 0 || ret >= end - pos)
3491                         return 0;
3492                 pos += ret;
3493         }
3494
3495         if (mask & WPA_BSS_MASK_AGE) {
3496                 struct os_reltime now;
3497
3498                 os_get_reltime(&now);
3499                 ret = os_snprintf(pos, end - pos, "age=%d\n",
3500                                   (int) (now.sec - bss->last_update.sec));
3501                 if (ret < 0 || ret >= end - pos)
3502                         return 0;
3503                 pos += ret;
3504         }
3505
3506         if (mask & WPA_BSS_MASK_IE) {
3507                 ret = os_snprintf(pos, end - pos, "ie=");
3508                 if (ret < 0 || ret >= end - pos)
3509                         return 0;
3510                 pos += ret;
3511
3512                 ie = (const u8 *) (bss + 1);
3513                 for (i = 0; i < bss->ie_len; i++) {
3514                         ret = os_snprintf(pos, end - pos, "%02x", *ie++);
3515                         if (ret < 0 || ret >= end - pos)
3516                                 return 0;
3517                         pos += ret;
3518                 }
3519
3520                 ret = os_snprintf(pos, end - pos, "\n");
3521                 if (ret < 0 || ret >= end - pos)
3522                         return 0;
3523                 pos += ret;
3524         }
3525
3526         if (mask & WPA_BSS_MASK_FLAGS) {
3527                 ret = os_snprintf(pos, end - pos, "flags=");
3528                 if (ret < 0 || ret >= end - pos)
3529                         return 0;
3530                 pos += ret;
3531
3532                 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3533                 if (ie)
3534                         pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3535                                                     2 + ie[1]);
3536                 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3537                 if (ie2)
3538                         pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3539                                                     2 + ie2[1]);
3540                 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3541                 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3542                         ret = os_snprintf(pos, end - pos, "[WEP]");
3543                         if (ret < 0 || ret >= end - pos)
3544                                 return 0;
3545                         pos += ret;
3546                 }
3547                 if (bss->caps & IEEE80211_CAP_IBSS) {
3548                         ret = os_snprintf(pos, end - pos, "[IBSS]");
3549                         if (ret < 0 || ret >= end - pos)
3550                                 return 0;
3551                         pos += ret;
3552                 }
3553                 if (bss->caps & IEEE80211_CAP_ESS) {
3554                         ret = os_snprintf(pos, end - pos, "[ESS]");
3555                         if (ret < 0 || ret >= end - pos)
3556                                 return 0;
3557                         pos += ret;
3558                 }
3559                 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
3560                     wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
3561                         ret = os_snprintf(pos, end - pos, "[P2P]");
3562                         if (ret < 0 || ret >= end - pos)
3563                                 return 0;
3564                         pos += ret;
3565                 }
3566 #ifdef CONFIG_HS20
3567                 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3568                         ret = os_snprintf(pos, end - pos, "[HS20]");
3569                         if (ret < 0 || ret >= end - pos)
3570                                 return 0;
3571                         pos += ret;
3572                 }
3573 #endif /* CONFIG_HS20 */
3574
3575                 ret = os_snprintf(pos, end - pos, "\n");
3576                 if (ret < 0 || ret >= end - pos)
3577                         return 0;
3578                 pos += ret;
3579         }
3580
3581         if (mask & WPA_BSS_MASK_SSID) {
3582                 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3583                                   wpa_ssid_txt(bss->ssid, bss->ssid_len));
3584                 if (ret < 0 || ret >= end - pos)
3585                         return 0;
3586                 pos += ret;
3587         }
3588
3589 #ifdef CONFIG_WPS
3590         if (mask & WPA_BSS_MASK_WPS_SCAN) {
3591                 ie = (const u8 *) (bss + 1);
3592                 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3593                 if (ret < 0 || ret >= end - pos)
3594                         return 0;
3595                 pos += ret;
3596         }
3597 #endif /* CONFIG_WPS */
3598
3599 #ifdef CONFIG_P2P
3600         if (mask & WPA_BSS_MASK_P2P_SCAN) {
3601                 ie = (const u8 *) (bss + 1);
3602                 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3603                 if (ret < 0 || ret >= end - pos)
3604                         return 0;
3605                 pos += ret;
3606         }
3607 #endif /* CONFIG_P2P */
3608
3609 #ifdef CONFIG_WIFI_DISPLAY
3610         if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3611                 struct wpabuf *wfd;
3612                 ie = (const u8 *) (bss + 1);
3613                 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3614                                                   WFD_IE_VENDOR_TYPE);
3615                 if (wfd) {
3616                         ret = os_snprintf(pos, end - pos, "wfd_subelems=");
3617                         if (ret < 0 || ret >= end - pos) {
3618                                 wpabuf_free(wfd);
3619                                 return 0;
3620                         }
3621                         pos += ret;
3622
3623                         pos += wpa_snprintf_hex(pos, end - pos,
3624                                                 wpabuf_head(wfd),
3625                                                 wpabuf_len(wfd));
3626                         wpabuf_free(wfd);
3627
3628                         ret = os_snprintf(pos, end - pos, "\n");
3629                         if (ret < 0 || ret >= end - pos)
3630                                 return 0;
3631                         pos += ret;
3632                 }
3633         }
3634 #endif /* CONFIG_WIFI_DISPLAY */
3635
3636 #ifdef CONFIG_INTERWORKING
3637         if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3638                 struct wpa_bss_anqp *anqp = bss->anqp;
3639                 pos = anqp_add_hex(pos, end, "anqp_venue_name",
3640                                    anqp->venue_name);
3641                 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
3642                                    anqp->network_auth_type);
3643                 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
3644                                    anqp->roaming_consortium);
3645                 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
3646                                    anqp->ip_addr_type_availability);
3647                 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
3648                                    anqp->nai_realm);
3649                 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
3650                 pos = anqp_add_hex(pos, end, "anqp_domain_name",
3651                                    anqp->domain_name);
3652 #ifdef CONFIG_HS20
3653                 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
3654                                    anqp->hs20_operator_friendly_name);
3655                 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
3656                                    anqp->hs20_wan_metrics);
3657                 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
3658                                    anqp->hs20_connection_capability);
3659                 pos = anqp_add_hex(pos, end, "hs20_operating_class",
3660                                    anqp->hs20_operating_class);
3661                 pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
3662                                    anqp->hs20_osu_providers_list);
3663 #endif /* CONFIG_HS20 */
3664         }
3665 #endif /* CONFIG_INTERWORKING */
3666
3667         if (mask & WPA_BSS_MASK_DELIM) {
3668                 ret = os_snprintf(pos, end - pos, "====\n");
3669                 if (ret < 0 || ret >= end - pos)
3670                         return 0;
3671                 pos += ret;
3672         }
3673
3674         return pos - buf;
3675 }
3676
3677
3678 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
3679                                          const char *cmd, char *buf,
3680                                          size_t buflen)
3681 {
3682         u8 bssid[ETH_ALEN];
3683         size_t i;
3684         struct wpa_bss *bss;
3685         struct wpa_bss *bsslast = NULL;
3686         struct dl_list *next;
3687         int ret = 0;
3688         int len;
3689         char *ctmp;
3690         unsigned long mask = WPA_BSS_MASK_ALL;
3691
3692         if (os_strncmp(cmd, "RANGE=", 6) == 0) {
3693                 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
3694                         bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
3695                                             list_id);
3696                         bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
3697                                                list_id);
3698                 } else { /* N1-N2 */
3699                         unsigned int id1, id2;
3700
3701                         if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
3702                                 wpa_printf(MSG_INFO, "Wrong BSS range "
3703                                            "format");
3704                                 return 0;
3705                         }
3706
3707                         if (*(cmd + 6) == '-')
3708                                 id1 = 0;
3709                         else
3710                                 id1 = atoi(cmd + 6);
3711                         ctmp++;
3712                         if (*ctmp >= '0' && *ctmp <= '9')
3713                                 id2 = atoi(ctmp);
3714                         else
3715                                 id2 = (unsigned int) -1;
3716                         bss = wpa_bss_get_id_range(wpa_s, id1, id2);
3717                         if (id2 == (unsigned int) -1)
3718                                 bsslast = dl_list_last(&wpa_s->bss_id,
3719                                                        struct wpa_bss,
3720                                                        list_id);
3721                         else {
3722                                 bsslast = wpa_bss_get_id(wpa_s, id2);
3723                                 if (bsslast == NULL && bss && id2 > id1) {
3724                                         struct wpa_bss *tmp = bss;
3725                                         for (;;) {
3726                                                 next = tmp->list_id.next;
3727                                                 if (next == &wpa_s->bss_id)
3728                                                         break;
3729                                                 tmp = dl_list_entry(
3730                                                         next, struct wpa_bss,
3731                                                         list_id);
3732                                                 if (tmp->id > id2)
3733                                                         break;
3734                                                 bsslast = tmp;
3735                                         }
3736                                 }
3737                         }
3738                 }
3739         } else if (os_strncmp(cmd, "FIRST", 5) == 0)
3740                 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
3741         else if (os_strncmp(cmd, "LAST", 4) == 0)
3742                 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
3743         else if (os_strncmp(cmd, "ID-", 3) == 0) {
3744                 i = atoi(cmd + 3);
3745                 bss = wpa_bss_get_id(wpa_s, i);
3746         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3747                 i = atoi(cmd + 5);
3748                 bss = wpa_bss_get_id(wpa_s, i);
3749                 if (bss) {
3750                         next = bss->list_id.next;
3751                         if (next == &wpa_s->bss_id)
3752                                 bss = NULL;
3753                         else
3754                                 bss = dl_list_entry(next, struct wpa_bss,
3755                                                     list_id);
3756                 }
3757 #ifdef CONFIG_P2P
3758         } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
3759                 if (hwaddr_aton(cmd + 13, bssid) == 0)
3760                         bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
3761                 else
3762                         bss = NULL;
3763 #endif /* CONFIG_P2P */
3764         } else if (hwaddr_aton(cmd, bssid) == 0)
3765                 bss = wpa_bss_get_bssid(wpa_s, bssid);
3766         else {
3767                 struct wpa_bss *tmp;
3768                 i = atoi(cmd);
3769                 bss = NULL;
3770                 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
3771                 {
3772                         if (i-- == 0) {
3773                                 bss = tmp;
3774                                 break;
3775                         }
3776                 }
3777         }
3778
3779         if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
3780                 mask = strtoul(ctmp + 5, NULL, 0x10);
3781                 if (mask == 0)
3782                         mask = WPA_BSS_MASK_ALL;
3783         }
3784
3785         if (bss == NULL)
3786                 return 0;
3787
3788         if (bsslast == NULL)
3789                 bsslast = bss;
3790         do {
3791                 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
3792                 ret += len;
3793                 buf += len;
3794                 buflen -= len;
3795                 if (bss == bsslast) {
3796                         if ((mask & WPA_BSS_MASK_DELIM) && len &&
3797                             (bss == dl_list_last(&wpa_s->bss_id,
3798                                                  struct wpa_bss, list_id)))
3799                                 os_snprintf(buf - 5, 5, "####\n");
3800                         break;
3801                 }
3802                 next = bss->list_id.next;
3803                 if (next == &wpa_s->bss_id)
3804                         break;
3805                 bss = dl_list_entry(next, struct wpa_bss, list_id);
3806         } while (bss && len);
3807
3808         return ret;
3809 }
3810
3811
3812 static int wpa_supplicant_ctrl_iface_ap_scan(
3813         struct wpa_supplicant *wpa_s, char *cmd)
3814 {
3815         int ap_scan = atoi(cmd);
3816         return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
3817 }
3818
3819
3820 static int wpa_supplicant_ctrl_iface_scan_interval(
3821         struct wpa_supplicant *wpa_s, char *cmd)
3822 {
3823         int scan_int = atoi(cmd);
3824         return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
3825 }
3826
3827
3828 static int wpa_supplicant_ctrl_iface_bss_expire_age(
3829         struct wpa_supplicant *wpa_s, char *cmd)
3830 {
3831         int expire_age = atoi(cmd);
3832         return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
3833 }
3834
3835
3836 static int wpa_supplicant_ctrl_iface_bss_expire_count(
3837         struct wpa_supplicant *wpa_s, char *cmd)
3838 {
3839         int expire_count = atoi(cmd);
3840         return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
3841 }
3842
3843
3844 static int wpa_supplicant_ctrl_iface_bss_flush(
3845         struct wpa_supplicant *wpa_s, char *cmd)
3846 {
3847         int flush_age = atoi(cmd);
3848
3849         if (flush_age == 0)
3850                 wpa_bss_flush(wpa_s);
3851         else
3852                 wpa_bss_flush_by_age(wpa_s, flush_age);
3853         return 0;
3854 }
3855
3856
3857 #ifdef CONFIG_TESTING_OPTIONS
3858 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
3859 {
3860         wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
3861         /* MLME-DELETEKEYS.request */
3862         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
3863         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
3864         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
3865         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
3866 #ifdef CONFIG_IEEE80211W
3867         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
3868         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
3869 #endif /* CONFIG_IEEE80211W */
3870
3871         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
3872                         0);
3873         /* MLME-SETPROTECTION.request(None) */
3874         wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
3875                                    MLME_SETPROTECTION_PROTECT_TYPE_NONE,
3876                                    MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3877         wpa_sm_drop_sa(wpa_s->wpa);
3878 }
3879 #endif /* CONFIG_TESTING_OPTIONS */
3880
3881
3882 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
3883                                           char *addr)
3884 {
3885 #ifdef CONFIG_NO_SCAN_PROCESSING
3886         return -1;
3887 #else /* CONFIG_NO_SCAN_PROCESSING */
3888         u8 bssid[ETH_ALEN];
3889         struct wpa_bss *bss;
3890         struct wpa_ssid *ssid = wpa_s->current_ssid;
3891
3892         if (hwaddr_aton(addr, bssid)) {
3893                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
3894                            "address '%s'", addr);
3895                 return -1;
3896         }
3897
3898         wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
3899
3900         if (!ssid) {
3901                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
3902                            "configuration known for the target AP");
3903                 return -1;
3904         }
3905
3906         bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
3907         if (!bss) {
3908                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
3909                            "from BSS table");
3910                 return -1;
3911         }
3912
3913         /*
3914          * TODO: Find best network configuration block from configuration to
3915          * allow roaming to other networks
3916          */
3917
3918         wpa_s->reassociate = 1;
3919         wpa_supplicant_connect(wpa_s, bss, ssid);
3920
3921         return 0;
3922 #endif /* CONFIG_NO_SCAN_PROCESSING */
3923 }
3924
3925
3926 #ifdef CONFIG_P2P
3927 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
3928 {
3929         unsigned int timeout = atoi(cmd);
3930         enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
3931         u8 dev_id[ETH_ALEN], *_dev_id = NULL;
3932         u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
3933         char *pos;
3934         unsigned int search_delay;
3935
3936         if (os_strstr(cmd, "type=social"))
3937                 type = P2P_FIND_ONLY_SOCIAL;
3938         else if (os_strstr(cmd, "type=progressive"))
3939                 type = P2P_FIND_PROGRESSIVE;
3940
3941         pos = os_strstr(cmd, "dev_id=");
3942         if (pos) {
3943                 pos += 7;
3944                 if (hwaddr_aton(pos, dev_id))
3945                         return -1;
3946                 _dev_id = dev_id;
3947         }
3948
3949         pos = os_strstr(cmd, "dev_type=");
3950         if (pos) {
3951                 pos += 9;
3952                 if (wps_dev_type_str2bin(pos, dev_type) < 0)
3953                         return -1;
3954                 _dev_type = dev_type;
3955         }
3956
3957         pos = os_strstr(cmd, "delay=");
3958         if (pos) {
3959                 pos += 6;
3960                 search_delay = atoi(pos);
3961         } else
3962                 search_delay = wpas_p2p_search_delay(wpa_s);
3963
3964         return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
3965                              _dev_id, search_delay);
3966 }
3967
3968
3969 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
3970                             char *buf, size_t buflen)
3971 {
3972         u8 addr[ETH_ALEN];
3973         char *pos, *pos2;
3974         char *pin = NULL;
3975         enum p2p_wps_method wps_method;
3976         int new_pin;
3977         int ret;
3978         int persistent_group, persistent_id = -1;
3979         int join;
3980         int auth;
3981         int automatic;
3982         int go_intent = -1;
3983         int freq = 0;
3984         int pd;
3985         int ht40, vht;
3986
3987         /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
3988          * [persistent|persistent=<network id>]
3989          * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
3990          * [ht40] [vht] */
3991
3992         if (hwaddr_aton(cmd, addr))
3993                 return -1;
3994
3995         pos = cmd + 17;
3996         if (*pos != ' ')
3997                 return -1;
3998         pos++;
3999
4000         persistent_group = os_strstr(pos, " persistent") != NULL;
4001         pos2 = os_strstr(pos, " persistent=");
4002         if (pos2) {
4003                 struct wpa_ssid *ssid;
4004                 persistent_id = atoi(pos2 + 12);
4005                 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4006                 if (ssid == NULL || ssid->disabled != 2 ||
4007                     ssid->mode != WPAS_MODE_P2P_GO) {
4008                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
4009                                    "SSID id=%d for persistent P2P group (GO)",
4010                                    persistent_id);
4011                         return -1;
4012                 }
4013         }
4014         join = os_strstr(pos, " join") != NULL;
4015         auth = os_strstr(pos, " auth") != NULL;
4016         automatic = os_strstr(pos, " auto") != NULL;
4017         pd = os_strstr(pos, " provdisc") != NULL;
4018         vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4019         ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4020                 vht;
4021
4022         pos2 = os_strstr(pos, " go_intent=");
4023         if (pos2) {
4024                 pos2 += 11;
4025                 go_intent = atoi(pos2);
4026                 if (go_intent < 0 || go_intent > 15)
4027                         return -1;
4028         }
4029
4030         pos2 = os_strstr(pos, " freq=");
4031         if (pos2) {
4032                 pos2 += 6;
4033                 freq = atoi(pos2);
4034                 if (freq <= 0)
4035                         return -1;
4036         }
4037
4038         if (os_strncmp(pos, "pin", 3) == 0) {
4039                 /* Request random PIN (to be displayed) and enable the PIN */
4040                 wps_method = WPS_PIN_DISPLAY;
4041         } else if (os_strncmp(pos, "pbc", 3) == 0) {
4042                 wps_method = WPS_PBC;
4043         } else {
4044                 pin = pos;
4045                 pos = os_strchr(pin, ' ');
4046                 wps_method = WPS_PIN_KEYPAD;
4047                 if (pos) {
4048                         *pos++ = '\0';
4049                         if (os_strncmp(pos, "display", 7) == 0)
4050                                 wps_method = WPS_PIN_DISPLAY;
4051                 }
4052                 if (!wps_pin_str_valid(pin)) {
4053                         os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
4054                         return 17;
4055                 }
4056         }
4057
4058         new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
4059                                    persistent_group, automatic, join,
4060                                    auth, go_intent, freq, persistent_id, pd,
4061                                    ht40, vht);
4062         if (new_pin == -2) {
4063                 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
4064                 return 25;
4065         }
4066         if (new_pin == -3) {
4067                 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
4068                 return 25;
4069         }
4070         if (new_pin < 0)
4071                 return -1;
4072         if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
4073                 ret = os_snprintf(buf, buflen, "%08d", new_pin);
4074                 if (ret < 0 || (size_t) ret >= buflen)
4075                         return -1;
4076                 return ret;
4077         }
4078
4079         os_memcpy(buf, "OK\n", 3);
4080         return 3;
4081 }
4082
4083
4084 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
4085 {
4086         unsigned int timeout = atoi(cmd);
4087         return wpas_p2p_listen(wpa_s, timeout);
4088 }
4089
4090
4091 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
4092 {
4093         u8 addr[ETH_ALEN];
4094         char *pos;
4095         enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
4096
4097         /* <addr> <config method> [join|auto] */
4098
4099         if (hwaddr_aton(cmd, addr))
4100                 return -1;
4101
4102         pos = cmd + 17;
4103         if (*pos != ' ')
4104                 return -1;
4105         pos++;
4106
4107         if (os_strstr(pos, " join") != NULL)
4108                 use = WPAS_P2P_PD_FOR_JOIN;
4109         else if (os_strstr(pos, " auto") != NULL)
4110                 use = WPAS_P2P_PD_AUTO;
4111
4112         return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
4113 }
4114
4115
4116 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
4117                               size_t buflen)
4118 {
4119         struct wpa_ssid *ssid = wpa_s->current_ssid;
4120
4121         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
4122             ssid->passphrase == NULL)
4123                 return -1;
4124
4125         os_strlcpy(buf, ssid->passphrase, buflen);
4126         return os_strlen(buf);
4127 }
4128
4129
4130 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
4131                                   char *buf, size_t buflen)
4132 {
4133         u64 ref;
4134         int res;
4135         u8 dst_buf[ETH_ALEN], *dst;
4136         struct wpabuf *tlvs;
4137         char *pos;
4138         size_t len;
4139
4140         if (hwaddr_aton(cmd, dst_buf))
4141                 return -1;
4142         dst = dst_buf;
4143         if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
4144             dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
4145                 dst = NULL;
4146         pos = cmd + 17;
4147         if (*pos != ' ')
4148                 return -1;
4149         pos++;
4150
4151         if (os_strncmp(pos, "upnp ", 5) == 0) {
4152                 u8 version;
4153                 pos += 5;
4154                 if (hexstr2bin(pos, &version, 1) < 0)
4155                         return -1;
4156                 pos += 2;
4157                 if (*pos != ' ')
4158                         return -1;
4159                 pos++;
4160                 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
4161 #ifdef CONFIG_WIFI_DISPLAY
4162         } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
4163                 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
4164 #endif /* CONFIG_WIFI_DISPLAY */
4165         } else {
4166                 len = os_strlen(pos);
4167                 if (len & 1)
4168                         return -1;
4169                 len /= 2;
4170                 tlvs = wpabuf_alloc(len);
4171                 if (tlvs == NULL)
4172                         return -1;
4173                 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
4174                         wpabuf_free(tlvs);
4175                         return -1;
4176                 }
4177
4178                 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
4179                 wpabuf_free(tlvs);
4180         }
4181         if (ref == 0)
4182                 return -1;
4183         res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
4184         if (res < 0 || (unsigned) res >= buflen)
4185                 return -1;
4186         return res;
4187 }
4188
4189
4190 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
4191                                          char *cmd)
4192 {
4193         long long unsigned val;
4194         u64 req;
4195         if (sscanf(cmd, "%llx", &val) != 1)
4196                 return -1;
4197         req = val;
4198         return wpas_p2p_sd_cancel_request(wpa_s, req);
4199 }
4200
4201
4202 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
4203 {
4204         int freq;
4205         u8 dst[ETH_ALEN];
4206         u8 dialog_token;
4207         struct wpabuf *resp_tlvs;
4208         char *pos, *pos2;
4209         size_t len;
4210
4211         pos = os_strchr(cmd, ' ');
4212         if (pos == NULL)
4213                 return -1;
4214         *pos++ = '\0';
4215         freq = atoi(cmd);
4216         if (freq == 0)
4217                 return -1;
4218
4219         if (hwaddr_aton(pos, dst))
4220                 return -1;
4221         pos += 17;
4222         if (*pos != ' ')
4223                 return -1;
4224         pos++;
4225
4226         pos2 = os_strchr(pos, ' ');
4227         if (pos2 == NULL)
4228                 return -1;
4229         *pos2++ = '\0';
4230         dialog_token = atoi(pos);
4231
4232         len = os_strlen(pos2);
4233         if (len & 1)
4234                 return -1;
4235         len /= 2;
4236         resp_tlvs = wpabuf_alloc(len);
4237         if (resp_tlvs == NULL)
4238                 return -1;
4239         if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
4240                 wpabuf_free(resp_tlvs);
4241                 return -1;
4242         }
4243
4244         wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
4245         wpabuf_free(resp_tlvs);
4246         return 0;
4247 }
4248
4249
4250 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
4251                                        char *cmd)
4252 {
4253         if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
4254                 return -1;
4255         wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
4256         return 0;
4257 }
4258
4259
4260 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
4261                                         char *cmd)
4262 {
4263         char *pos;
4264         size_t len;
4265         struct wpabuf *query, *resp;
4266
4267         pos = os_strchr(cmd, ' ');
4268         if (pos == NULL)
4269                 return -1;
4270         *pos++ = '\0';
4271
4272         len = os_strlen(cmd);
4273         if (len & 1)
4274                 return -1;
4275         len /= 2;
4276         query = wpabuf_alloc(len);
4277         if (query == NULL)
4278                 return -1;
4279         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4280                 wpabuf_free(query);
4281                 return -1;
4282         }
4283
4284         len = os_strlen(pos);
4285         if (len & 1) {
4286                 wpabuf_free(query);
4287                 return -1;
4288         }
4289         len /= 2;
4290         resp = wpabuf_alloc(len);
4291         if (resp == NULL) {
4292                 wpabuf_free(query);
4293                 return -1;
4294         }
4295         if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4296                 wpabuf_free(query);
4297                 wpabuf_free(resp);
4298                 return -1;
4299         }
4300
4301         if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4302                 wpabuf_free(query);
4303                 wpabuf_free(resp);
4304                 return -1;
4305         }
4306         return 0;
4307 }
4308
4309
4310 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4311 {
4312         char *pos;
4313         u8 version;
4314
4315         pos = os_strchr(cmd, ' ');
4316         if (pos == NULL)
4317                 return -1;
4318         *pos++ = '\0';
4319
4320         if (hexstr2bin(cmd, &version, 1) < 0)
4321                 return -1;
4322
4323         return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4324 }
4325
4326
4327 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4328 {
4329         char *pos;
4330
4331         pos = os_strchr(cmd, ' ');
4332         if (pos == NULL)
4333                 return -1;
4334         *pos++ = '\0';
4335
4336         if (os_strcmp(cmd, "bonjour") == 0)
4337                 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4338         if (os_strcmp(cmd, "upnp") == 0)
4339                 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4340         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4341         return -1;
4342 }
4343
4344
4345 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4346                                         char *cmd)
4347 {
4348         size_t len;
4349         struct wpabuf *query;
4350         int ret;
4351
4352         len = os_strlen(cmd);
4353         if (len & 1)
4354                 return -1;
4355         len /= 2;
4356         query = wpabuf_alloc(len);
4357         if (query == NULL)
4358                 return -1;
4359         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4360                 wpabuf_free(query);
4361                 return -1;
4362         }
4363
4364         ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4365         wpabuf_free(query);
4366         return ret;
4367 }
4368
4369
4370 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4371 {
4372         char *pos;
4373         u8 version;
4374
4375         pos = os_strchr(cmd, ' ');
4376         if (pos == NULL)
4377                 return -1;
4378         *pos++ = '\0';
4379
4380         if (hexstr2bin(cmd, &version, 1) < 0)
4381                 return -1;
4382
4383         return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4384 }
4385
4386
4387 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4388 {
4389         char *pos;
4390
4391         pos = os_strchr(cmd, ' ');
4392         if (pos == NULL)
4393                 return -1;
4394         *pos++ = '\0';
4395
4396         if (os_strcmp(cmd, "bonjour") == 0)
4397                 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4398         if (os_strcmp(cmd, "upnp") == 0)
4399                 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4400         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4401         return -1;
4402 }
4403
4404
4405 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4406 {
4407         u8 addr[ETH_ALEN];
4408
4409         /* <addr> */
4410
4411         if (hwaddr_aton(cmd, addr))
4412                 return -1;
4413
4414         return wpas_p2p_reject(wpa_s, addr);
4415 }
4416
4417
4418 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4419 {
4420         char *pos;
4421         int id;
4422         struct wpa_ssid *ssid;
4423         u8 *_peer = NULL, peer[ETH_ALEN];
4424         int freq = 0, pref_freq = 0;
4425         int ht40, vht;
4426
4427         id = atoi(cmd);
4428         pos = os_strstr(cmd, " peer=");
4429         if (pos) {
4430                 pos += 6;
4431                 if (hwaddr_aton(pos, peer))
4432                         return -1;
4433                 _peer = peer;
4434         }
4435         ssid = wpa_config_get_network(wpa_s->conf, id);
4436         if (ssid == NULL || ssid->disabled != 2) {
4437                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4438                            "for persistent P2P group",
4439                            id);
4440                 return -1;
4441         }
4442
4443         pos = os_strstr(cmd, " freq=");
4444         if (pos) {
4445                 pos += 6;
4446                 freq = atoi(pos);
4447                 if (freq <= 0)
4448                         return -1;
4449         }
4450
4451         pos = os_strstr(cmd, " pref=");
4452         if (pos) {
4453                 pos += 6;
4454                 pref_freq = atoi(pos);
4455                 if (pref_freq <= 0)
4456                         return -1;
4457         }
4458
4459         vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4460         ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4461                 vht;
4462
4463         return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
4464                                pref_freq);
4465 }
4466
4467
4468 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
4469 {
4470         char *pos;
4471         u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
4472
4473         pos = os_strstr(cmd, " peer=");
4474         if (!pos)
4475                 return -1;
4476
4477         *pos = '\0';
4478         pos += 6;
4479         if (hwaddr_aton(pos, peer)) {
4480                 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
4481                 return -1;
4482         }
4483
4484         pos = os_strstr(pos, " go_dev_addr=");
4485         if (pos) {
4486                 pos += 13;
4487                 if (hwaddr_aton(pos, go_dev_addr)) {
4488                         wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
4489                                    pos);
4490                         return -1;
4491                 }
4492                 go_dev = go_dev_addr;
4493         }
4494
4495         return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
4496 }
4497
4498
4499 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
4500 {
4501         if (os_strncmp(cmd, "persistent=", 11) == 0)
4502                 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
4503         if (os_strncmp(cmd, "group=", 6) == 0)
4504                 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
4505
4506         return -1;
4507 }
4508
4509
4510 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
4511                                          char *cmd, int freq, int ht40,
4512                                          int vht)
4513 {
4514         int id;
4515         struct wpa_ssid *ssid;
4516
4517         id = atoi(cmd);
4518         ssid = wpa_config_get_network(wpa_s->conf, id);
4519         if (ssid == NULL || ssid->disabled != 2) {
4520                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4521                            "for persistent P2P group",
4522                            id);
4523                 return -1;
4524         }
4525
4526         return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
4527                                              NULL, 0);
4528 }
4529
4530
4531 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
4532 {
4533         int freq = 0, ht40, vht;
4534         char *pos;
4535
4536         pos = os_strstr(cmd, "freq=");
4537         if (pos)
4538                 freq = atoi(pos + 5);
4539
4540         vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
4541         ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4542                 vht;
4543
4544         if (os_strncmp(cmd, "persistent=", 11) == 0)
4545                 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
4546                                                      ht40, vht);
4547         if (os_strcmp(cmd, "persistent") == 0 ||
4548             os_strncmp(cmd, "persistent ", 11) == 0)
4549                 return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
4550         if (os_strncmp(cmd, "freq=", 5) == 0)
4551                 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
4552         if (ht40)
4553                 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
4554
4555         wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
4556                    cmd);
4557         return -1;
4558 }
4559
4560
4561 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
4562                          char *buf, size_t buflen)
4563 {
4564         u8 addr[ETH_ALEN], *addr_ptr;
4565         int next, res;
4566         const struct p2p_peer_info *info;
4567         char *pos, *end;
4568         char devtype[WPS_DEV_TYPE_BUFSIZE];
4569         struct wpa_ssid *ssid;
4570         size_t i;
4571
4572         if (!wpa_s->global->p2p)
4573                 return -1;
4574
4575         if (os_strcmp(cmd, "FIRST") == 0) {
4576                 addr_ptr = NULL;
4577                 next = 0;
4578         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4579                 if (hwaddr_aton(cmd + 5, addr) < 0)
4580                         return -1;
4581                 addr_ptr = addr;
4582                 next = 1;
4583         } else {
4584                 if (hwaddr_aton(cmd, addr) < 0)
4585                         return -1;
4586                 addr_ptr = addr;
4587                 next = 0;
4588         }
4589
4590         info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4591         if (info == NULL)
4592                 return -1;
4593
4594         pos = buf;
4595         end = buf + buflen;
4596
4597         res = os_snprintf(pos, end - pos, MACSTR "\n"
4598                           "pri_dev_type=%s\n"
4599                           "device_name=%s\n"
4600                           "manufacturer=%s\n"
4601                           "model_name=%s\n"
4602                           "model_number=%s\n"
4603                           "serial_number=%s\n"
4604                           "config_methods=0x%x\n"
4605                           "dev_capab=0x%x\n"
4606                           "group_capab=0x%x\n"
4607                           "level=%d\n",
4608                           MAC2STR(info->p2p_device_addr),
4609                           wps_dev_type_bin2str(info->pri_dev_type,
4610                                                devtype, sizeof(devtype)),
4611                           info->device_name,
4612                           info->manufacturer,
4613                           info->model_name,
4614                           info->model_number,
4615                           info->serial_number,
4616                           info->config_methods,
4617                           info->dev_capab,
4618                           info->group_capab,
4619                           info->level);
4620         if (res < 0 || res >= end - pos)
4621                 return pos - buf;
4622         pos += res;
4623
4624         for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4625         {
4626                 const u8 *t;
4627                 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4628                 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4629                                   wps_dev_type_bin2str(t, devtype,
4630                                                        sizeof(devtype)));
4631                 if (res < 0 || res >= end - pos)
4632                         return pos - buf;
4633                 pos += res;
4634         }
4635
4636         ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
4637         if (ssid) {
4638                 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
4639                 if (res < 0 || res >= end - pos)
4640                         return pos - buf;
4641                 pos += res;
4642         }
4643
4644         res = p2p_get_peer_info_txt(info, pos, end - pos);
4645         if (res < 0)
4646                 return pos - buf;
4647         pos += res;
4648
4649         return pos - buf;
4650 }
4651
4652
4653 static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
4654                                   const char *param)
4655 {
4656         unsigned int i;
4657
4658         if (wpa_s->global->p2p == NULL)
4659                 return -1;
4660
4661         if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
4662                 return -1;
4663
4664         for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
4665                 struct wpa_freq_range *freq;
4666                 freq = &wpa_s->global->p2p_disallow_freq.range[i];
4667                 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
4668                            freq->min, freq->max);
4669         }
4670
4671         wpas_p2p_update_channel_list(wpa_s);
4672         return 0;
4673 }
4674
4675
4676 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
4677 {
4678         char *param;
4679
4680         if (wpa_s->global->p2p == NULL)
4681                 return -1;
4682
4683         param = os_strchr(cmd, ' ');
4684         if (param == NULL)
4685                 return -1;
4686         *param++ = '\0';
4687
4688         if (os_strcmp(cmd, "discoverability") == 0) {
4689                 p2p_set_client_discoverability(wpa_s->global->p2p,
4690                                                atoi(param));
4691                 return 0;
4692         }
4693
4694         if (os_strcmp(cmd, "managed") == 0) {
4695                 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
4696                 return 0;
4697         }
4698
4699         if (os_strcmp(cmd, "listen_channel") == 0) {
4700                 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
4701                                               atoi(param));
4702         }
4703
4704         if (os_strcmp(cmd, "ssid_postfix") == 0) {
4705                 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
4706                                             os_strlen(param));
4707         }
4708
4709         if (os_strcmp(cmd, "noa") == 0) {
4710                 char *pos;
4711                 int count, start, duration;
4712                 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
4713                 count = atoi(param);
4714                 pos = os_strchr(param, ',');
4715                 if (pos == NULL)
4716                         return -1;
4717                 pos++;
4718                 start = atoi(pos);
4719                 pos = os_strchr(pos, ',');
4720                 if (pos == NULL)
4721                         return -1;
4722                 pos++;
4723                 duration = atoi(pos);
4724                 if (count < 0 || count > 255 || start < 0 || duration < 0)
4725                         return -1;
4726                 if (count == 0 && duration > 0)
4727                         return -1;
4728                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
4729                            "start=%d duration=%d", count, start, duration);
4730                 return wpas_p2p_set_noa(wpa_s, count, start, duration);
4731         }
4732
4733         if (os_strcmp(cmd, "ps") == 0)
4734                 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
4735
4736         if (os_strcmp(cmd, "oppps") == 0)
4737                 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
4738
4739         if (os_strcmp(cmd, "ctwindow") == 0)
4740                 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
4741
4742         if (os_strcmp(cmd, "disabled") == 0) {
4743                 wpa_s->global->p2p_disabled = atoi(param);
4744                 wpa_printf(MSG_DEBUG, "P2P functionality %s",
4745                            wpa_s->global->p2p_disabled ?
4746                            "disabled" : "enabled");
4747                 if (wpa_s->global->p2p_disabled) {
4748                         wpas_p2p_stop_find(wpa_s);
4749                         os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4750                         p2p_flush(wpa_s->global->p2p);
4751                 }
4752                 return 0;
4753         }
4754
4755         if (os_strcmp(cmd, "conc_pref") == 0) {
4756                 if (os_strcmp(param, "sta") == 0)
4757                         wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
4758                 else if (os_strcmp(param, "p2p") == 0)
4759                         wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
4760                 else {
4761                         wpa_printf(MSG_INFO, "Invalid conc_pref value");
4762                         return -1;
4763                 }
4764                 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
4765                            "%s", param);
4766                 return 0;
4767         }
4768
4769         if (os_strcmp(cmd, "force_long_sd") == 0) {
4770                 wpa_s->force_long_sd = atoi(param);
4771                 return 0;
4772         }
4773
4774         if (os_strcmp(cmd, "peer_filter") == 0) {
4775                 u8 addr[ETH_ALEN];
4776                 if (hwaddr_aton(param, addr))
4777                         return -1;
4778                 p2p_set_peer_filter(wpa_s->global->p2p, addr);
4779                 return 0;
4780         }
4781
4782         if (os_strcmp(cmd, "cross_connect") == 0)
4783                 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
4784
4785         if (os_strcmp(cmd, "go_apsd") == 0) {
4786                 if (os_strcmp(param, "disable") == 0)
4787                         wpa_s->set_ap_uapsd = 0;
4788                 else {
4789                         wpa_s->set_ap_uapsd = 1;
4790                         wpa_s->ap_uapsd = atoi(param);
4791                 }
4792                 return 0;
4793         }
4794
4795         if (os_strcmp(cmd, "client_apsd") == 0) {
4796                 if (os_strcmp(param, "disable") == 0)
4797                         wpa_s->set_sta_uapsd = 0;
4798                 else {
4799                         int be, bk, vi, vo;
4800                         char *pos;
4801                         /* format: BE,BK,VI,VO;max SP Length */
4802                         be = atoi(param);
4803                         pos = os_strchr(param, ',');
4804                         if (pos == NULL)
4805                                 return -1;
4806                         pos++;
4807                         bk = atoi(pos);
4808                         pos = os_strchr(pos, ',');
4809                         if (pos == NULL)
4810                                 return -1;
4811                         pos++;
4812                         vi = atoi(pos);
4813                         pos = os_strchr(pos, ',');
4814                         if (pos == NULL)
4815                                 return -1;
4816                         pos++;
4817                         vo = atoi(pos);
4818                         /* ignore max SP Length for now */
4819
4820                         wpa_s->set_sta_uapsd = 1;
4821                         wpa_s->sta_uapsd = 0;
4822                         if (be)
4823                                 wpa_s->sta_uapsd |= BIT(0);
4824                         if (bk)
4825                                 wpa_s->sta_uapsd |= BIT(1);
4826                         if (vi)
4827                                 wpa_s->sta_uapsd |= BIT(2);
4828                         if (vo)
4829                                 wpa_s->sta_uapsd |= BIT(3);
4830                 }
4831                 return 0;
4832         }
4833
4834         if (os_strcmp(cmd, "disallow_freq") == 0)
4835                 return p2p_ctrl_disallow_freq(wpa_s, param);
4836
4837         if (os_strcmp(cmd, "disc_int") == 0) {
4838                 int min_disc_int, max_disc_int, max_disc_tu;
4839                 char *pos;
4840
4841                 pos = param;
4842
4843                 min_disc_int = atoi(pos);
4844                 pos = os_strchr(pos, ' ');
4845                 if (pos == NULL)
4846                         return -1;
4847                 *pos++ = '\0';
4848
4849                 max_disc_int = atoi(pos);
4850                 pos = os_strchr(pos, ' ');
4851                 if (pos == NULL)
4852                         return -1;
4853                 *pos++ = '\0';
4854
4855                 max_disc_tu = atoi(pos);
4856
4857                 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
4858                                         max_disc_int, max_disc_tu);
4859         }
4860
4861         if (os_strcmp(cmd, "per_sta_psk") == 0) {
4862                 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
4863                 return 0;
4864         }
4865
4866 #ifdef CONFIG_WPS_NFC
4867         if (os_strcmp(cmd, "nfc_tag") == 0)
4868                 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
4869 #endif /* CONFIG_WPS_NFC */
4870
4871         if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
4872                 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
4873                 return 0;
4874         }
4875
4876         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
4877                    cmd);
4878
4879         return -1;
4880 }
4881
4882
4883 static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
4884 {
4885         os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4886         wpa_s->force_long_sd = 0;
4887         if (wpa_s->global->p2p)
4888                 p2p_flush(wpa_s->global->p2p);
4889 }
4890
4891
4892 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
4893 {
4894         char *pos, *pos2;
4895         unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
4896
4897         if (cmd[0]) {
4898                 pos = os_strchr(cmd, ' ');
4899                 if (pos == NULL)
4900                         return -1;
4901                 *pos++ = '\0';
4902                 dur1 = atoi(cmd);
4903
4904                 pos2 = os_strchr(pos, ' ');
4905                 if (pos2)
4906                         *pos2++ = '\0';
4907                 int1 = atoi(pos);
4908         } else
4909                 pos2 = NULL;
4910
4911         if (pos2) {
4912                 pos = os_strchr(pos2, ' ');
4913                 if (pos == NULL)
4914                         return -1;
4915                 *pos++ = '\0';
4916                 dur2 = atoi(pos2);
4917                 int2 = atoi(pos);
4918         }
4919
4920         return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
4921 }
4922
4923
4924 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
4925 {
4926         char *pos;
4927         unsigned int period = 0, interval = 0;
4928
4929         if (cmd[0]) {
4930                 pos = os_strchr(cmd, ' ');
4931                 if (pos == NULL)
4932                         return -1;
4933                 *pos++ = '\0';
4934                 period = atoi(cmd);
4935                 interval = atoi(pos);
4936         }
4937
4938         return wpas_p2p_ext_listen(wpa_s, period, interval);
4939 }
4940
4941
4942 static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
4943 {
4944         const char *pos;
4945         u8 peer[ETH_ALEN];
4946         int iface_addr = 0;
4947
4948         pos = cmd;
4949         if (os_strncmp(pos, "iface=", 6) == 0) {
4950                 iface_addr = 1;
4951                 pos += 6;
4952         }
4953         if (hwaddr_aton(pos, peer))
4954                 return -1;
4955
4956         wpas_p2p_remove_client(wpa_s, peer, iface_addr);
4957         return 0;
4958 }
4959
4960 #endif /* CONFIG_P2P */
4961
4962
4963 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
4964 {
4965         struct wpa_freq_range_list ranges;
4966         int *freqs = NULL;
4967         struct hostapd_hw_modes *mode;
4968         u16 i;
4969
4970         if (wpa_s->hw.modes == NULL)
4971                 return NULL;
4972
4973         os_memset(&ranges, 0, sizeof(ranges));
4974         if (freq_range_list_parse(&ranges, val) < 0)
4975                 return NULL;
4976
4977         for (i = 0; i < wpa_s->hw.num_modes; i++) {
4978                 int j;
4979
4980                 mode = &wpa_s->hw.modes[i];
4981                 for (j = 0; j < mode->num_channels; j++) {
4982                         unsigned int freq;
4983
4984                         if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
4985                                 continue;
4986
4987                         freq = mode->channels[j].freq;
4988                         if (!freq_range_list_includes(&ranges, freq))
4989                                 continue;
4990
4991                         int_array_add_unique(&freqs, freq);
4992                 }
4993         }
4994
4995         os_free(ranges.range);
4996         return freqs;
4997 }
4998
4999
5000 #ifdef CONFIG_INTERWORKING
5001
5002 static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
5003 {
5004         int auto_sel = 0;
5005         int *freqs = NULL;
5006
5007         if (param) {
5008                 char *pos;
5009
5010                 auto_sel = os_strstr(param, "auto") != NULL;
5011
5012                 pos = os_strstr(param, "freq=");
5013                 if (pos) {
5014                         freqs = freq_range_to_channel_list(wpa_s, pos + 5);
5015                         if (freqs == NULL)
5016                                 return -1;
5017                 }
5018
5019         }
5020
5021         return interworking_select(wpa_s, auto_sel, freqs);
5022 }
5023
5024
5025 static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
5026 {
5027         u8 bssid[ETH_ALEN];
5028         struct wpa_bss *bss;
5029
5030         if (hwaddr_aton(dst, bssid)) {
5031                 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
5032                 return -1;
5033         }
5034
5035         bss = wpa_bss_get_bssid(wpa_s, bssid);
5036         if (bss == NULL) {
5037                 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
5038                            MAC2STR(bssid));
5039                 return -1;
5040         }
5041
5042         return interworking_connect(wpa_s, bss);
5043 }
5044
5045
5046 static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
5047 {
5048         u8 dst_addr[ETH_ALEN];
5049         int used;
5050         char *pos;
5051 #define MAX_ANQP_INFO_ID 100
5052         u16 id[MAX_ANQP_INFO_ID];
5053         size_t num_id = 0;
5054         u32 subtypes = 0;
5055
5056         used = hwaddr_aton2(dst, dst_addr);
5057         if (used < 0)
5058                 return -1;
5059         pos = dst + used;
5060         while (num_id < MAX_ANQP_INFO_ID) {
5061                 if (os_strncmp(pos, "hs20:", 5) == 0) {
5062 #ifdef CONFIG_HS20
5063                         int num = atoi(pos + 5);
5064                         if (num <= 0 || num > 31)
5065                                 return -1;
5066                         subtypes |= BIT(num);
5067 #else /* CONFIG_HS20 */
5068                         return -1;
5069 #endif /* CONFIG_HS20 */
5070                 } else {
5071                         id[num_id] = atoi(pos);
5072                         if (id[num_id])
5073                                 num_id++;
5074                 }
5075                 pos = os_strchr(pos + 1, ',');
5076                 if (pos == NULL)
5077                         break;
5078                 pos++;
5079         }
5080
5081         if (num_id == 0)
5082                 return -1;
5083
5084         return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
5085 }
5086
5087
5088 static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
5089 {
5090         u8 dst_addr[ETH_ALEN];
5091         struct wpabuf *advproto, *query = NULL;
5092         int used, ret = -1;
5093         char *pos, *end;
5094         size_t len;
5095
5096         used = hwaddr_aton2(cmd, dst_addr);
5097         if (used < 0)
5098                 return -1;
5099
5100         pos = cmd + used;
5101         while (*pos == ' ')
5102                 pos++;
5103
5104         /* Advertisement Protocol ID */
5105         end = os_strchr(pos, ' ');
5106         if (end)
5107                 len = end - pos;
5108         else
5109                 len = os_strlen(pos);
5110         if (len & 0x01)
5111                 return -1;
5112         len /= 2;
5113         if (len == 0)
5114                 return -1;
5115         advproto = wpabuf_alloc(len);
5116         if (advproto == NULL)
5117                 return -1;
5118         if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
5119                 goto fail;
5120
5121         if (end) {
5122                 /* Optional Query Request */
5123                 pos = end + 1;
5124                 while (*pos == ' ')
5125                         pos++;
5126
5127                 len = os_strlen(pos);
5128                 if (len) {
5129                         if (len & 0x01)
5130                                 goto fail;
5131                         len /= 2;
5132                         if (len == 0)
5133                                 goto fail;
5134                         query = wpabuf_alloc(len);
5135                         if (query == NULL)
5136                                 goto fail;
5137                         if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
5138                                 goto fail;
5139                 }
5140         }
5141
5142         ret = gas_send_request(wpa_s, dst_addr, advproto, query);
5143
5144 fail:
5145         wpabuf_free(advproto);
5146         wpabuf_free(query);
5147
5148         return ret;
5149 }
5150
5151
5152 static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
5153                             size_t buflen)
5154 {
5155         u8 addr[ETH_ALEN];
5156         int dialog_token;
5157         int used;
5158         char *pos;
5159         size_t resp_len, start, requested_len;
5160         struct wpabuf *resp;
5161         int ret;
5162
5163         used = hwaddr_aton2(cmd, addr);
5164         if (used < 0)
5165                 return -1;
5166
5167         pos = cmd + used;
5168         while (*pos == ' ')
5169                 pos++;
5170         dialog_token = atoi(pos);
5171
5172         if (wpa_s->last_gas_resp &&
5173             os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
5174             dialog_token == wpa_s->last_gas_dialog_token)
5175                 resp = wpa_s->last_gas_resp;
5176         else if (wpa_s->prev_gas_resp &&
5177                  os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
5178                  dialog_token == wpa_s->prev_gas_dialog_token)
5179                 resp = wpa_s->prev_gas_resp;
5180         else
5181                 return -1;
5182
5183         resp_len = wpabuf_len(resp);
5184         start = 0;
5185         requested_len = resp_len;
5186
5187         pos = os_strchr(pos, ' ');
5188         if (pos) {
5189                 start = atoi(pos);
5190                 if (start > resp_len)
5191                         return os_snprintf(buf, buflen, "FAIL-Invalid range");
5192                 pos = os_strchr(pos, ',');
5193                 if (pos == NULL)
5194                         return -1;
5195                 pos++;
5196                 requested_len = atoi(pos);
5197                 if (start + requested_len > resp_len)
5198                         return os_snprintf(buf, buflen, "FAIL-Invalid range");
5199         }
5200
5201         if (requested_len * 2 + 1 > buflen)
5202                 return os_snprintf(buf, buflen, "FAIL-Too long response");
5203
5204         ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
5205                                requested_len);
5206
5207         if (start + requested_len == resp_len) {
5208                 /*
5209                  * Free memory by dropping the response after it has been
5210                  * fetched.
5211                  */
5212                 if (resp == wpa_s->prev_gas_resp) {
5213                         wpabuf_free(wpa_s->prev_gas_resp);
5214                         wpa_s->prev_gas_resp = NULL;
5215                 } else {
5216                         wpabuf_free(wpa_s->last_gas_resp);
5217                         wpa_s->last_gas_resp = NULL;
5218                 }
5219         }
5220
5221         return ret;
5222 }
5223 #endif /* CONFIG_INTERWORKING */
5224
5225
5226 #ifdef CONFIG_HS20
5227
5228 static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
5229 {
5230         u8 dst_addr[ETH_ALEN];
5231         int used;
5232         char *pos;
5233         u32 subtypes = 0;
5234
5235         used = hwaddr_aton2(dst, dst_addr);
5236         if (used < 0)
5237                 return -1;
5238         pos = dst + used;
5239         for (;;) {
5240                 int num = atoi(pos);
5241                 if (num <= 0 || num > 31)
5242                         return -1;
5243                 subtypes |= BIT(num);
5244                 pos = os_strchr(pos + 1, ',');
5245                 if (pos == NULL)
5246                         break;
5247                 pos++;
5248         }
5249
5250         if (subtypes == 0)
5251                 return -1;
5252
5253         return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
5254 }
5255
5256
5257 static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5258                                     const u8 *addr, const char *realm)
5259 {
5260         u8 *buf;
5261         size_t rlen, len;
5262         int ret;
5263
5264         rlen = os_strlen(realm);
5265         len = 3 + rlen;
5266         buf = os_malloc(len);
5267         if (buf == NULL)
5268                 return -1;
5269         buf[0] = 1; /* NAI Home Realm Count */
5270         buf[1] = 0; /* Formatted in accordance with RFC 4282 */
5271         buf[2] = rlen;
5272         os_memcpy(buf + 3, realm, rlen);
5273
5274         ret = hs20_anqp_send_req(wpa_s, addr,
5275                                  BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5276                                  buf, len);
5277
5278         os_free(buf);
5279
5280         return ret;
5281 }
5282
5283
5284 static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5285                                         char *dst)
5286 {
5287         struct wpa_cred *cred = wpa_s->conf->cred;
5288         u8 dst_addr[ETH_ALEN];
5289         int used;
5290         u8 *buf;
5291         size_t len;
5292         int ret;
5293
5294         used = hwaddr_aton2(dst, dst_addr);
5295         if (used < 0)
5296                 return -1;
5297
5298         while (dst[used] == ' ')
5299                 used++;
5300         if (os_strncmp(dst + used, "realm=", 6) == 0)
5301                 return hs20_nai_home_realm_list(wpa_s, dst_addr,
5302                                                 dst + used + 6);
5303
5304         len = os_strlen(dst + used);
5305
5306         if (len == 0 && cred && cred->realm)
5307                 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
5308
5309         if (len % 1)
5310                 return -1;
5311         len /= 2;
5312         buf = os_malloc(len);
5313         if (buf == NULL)
5314                 return -1;
5315         if (hexstr2bin(dst + used, buf, len) < 0) {
5316                 os_free(buf);
5317                 return -1;
5318         }
5319
5320         ret = hs20_anqp_send_req(wpa_s, dst_addr,
5321                                  BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5322                                  buf, len);
5323         os_free(buf);
5324
5325         return ret;
5326 }
5327
5328
5329 static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
5330 {
5331         u8 dst_addr[ETH_ALEN];
5332         int used;
5333         char *icon;
5334
5335         used = hwaddr_aton2(cmd, dst_addr);
5336         if (used < 0)
5337                 return -1;
5338
5339         while (cmd[used] == ' ')
5340                 used++;
5341         icon = &cmd[used];
5342
5343         wpa_s->fetch_osu_icon_in_progress = 0;
5344         return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
5345                                   (u8 *) icon, os_strlen(icon));
5346 }
5347
5348 #endif /* CONFIG_HS20 */
5349
5350
5351 static int wpa_supplicant_ctrl_iface_sta_autoconnect(
5352         struct wpa_supplicant *wpa_s, char *cmd)
5353 {
5354         wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
5355         return 0;
5356 }
5357
5358
5359 #ifdef CONFIG_AUTOSCAN
5360
5361 static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
5362                                               char *cmd)
5363 {
5364         enum wpa_states state = wpa_s->wpa_state;
5365         char *new_params = NULL;
5366
5367         if (os_strlen(cmd) > 0) {
5368                 new_params = os_strdup(cmd);
5369                 if (new_params == NULL)
5370                         return -1;
5371         }
5372
5373         os_free(wpa_s->conf->autoscan);
5374         wpa_s->conf->autoscan = new_params;
5375
5376         if (wpa_s->conf->autoscan == NULL)
5377                 autoscan_deinit(wpa_s);
5378         else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
5379                 autoscan_init(wpa_s, 1);
5380         else if (state == WPA_SCANNING)
5381                 wpa_supplicant_reinit_autoscan(wpa_s);
5382
5383         return 0;
5384 }
5385
5386 #endif /* CONFIG_AUTOSCAN */
5387
5388
5389 #ifdef CONFIG_WNM
5390
5391 static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
5392 {
5393         int enter;
5394         int intval = 0;
5395         char *pos;
5396         int ret;
5397         struct wpabuf *tfs_req = NULL;
5398
5399         if (os_strncmp(cmd, "enter", 5) == 0)
5400                 enter = 1;
5401         else if (os_strncmp(cmd, "exit", 4) == 0)
5402                 enter = 0;
5403         else
5404                 return -1;
5405
5406         pos = os_strstr(cmd, " interval=");
5407         if (pos)
5408                 intval = atoi(pos + 10);
5409
5410         pos = os_strstr(cmd, " tfs_req=");
5411         if (pos) {
5412                 char *end;
5413                 size_t len;
5414                 pos += 9;
5415                 end = os_strchr(pos, ' ');
5416                 if (end)
5417                         len = end - pos;
5418                 else
5419                         len = os_strlen(pos);
5420                 if (len & 1)
5421                         return -1;
5422                 len /= 2;
5423                 tfs_req = wpabuf_alloc(len);
5424                 if (tfs_req == NULL)
5425                         return -1;
5426                 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
5427                         wpabuf_free(tfs_req);
5428                         return -1;
5429                 }
5430         }
5431
5432         ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
5433                                            WNM_SLEEP_MODE_EXIT, intval,
5434                                            tfs_req);
5435         wpabuf_free(tfs_req);
5436
5437         return ret;
5438 }
5439
5440
5441 static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
5442 {
5443         int query_reason;
5444
5445         query_reason = atoi(cmd);
5446
5447         wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
5448                    query_reason);
5449
5450         return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
5451 }
5452
5453 #endif /* CONFIG_WNM */
5454
5455
5456 /* Get string representation of channel width */
5457 static const char * channel_width_name(enum chan_width width)
5458 {
5459         switch (width) {
5460         case CHAN_WIDTH_20_NOHT:
5461                 return "20 MHz (no HT)";
5462         case CHAN_WIDTH_20:
5463                 return "20 MHz";
5464         case CHAN_WIDTH_40:
5465                 return "40 MHz";
5466         case CHAN_WIDTH_80:
5467                 return "80 MHz";
5468         case CHAN_WIDTH_80P80:
5469                 return "80+80 MHz";
5470         case CHAN_WIDTH_160:
5471                 return "160 MHz";
5472         default:
5473                 return "unknown";
5474         }
5475 }
5476
5477
5478 static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
5479                                       size_t buflen)
5480 {
5481         struct wpa_signal_info si;
5482         int ret;
5483         char *pos, *end;
5484
5485         ret = wpa_drv_signal_poll(wpa_s, &si);
5486         if (ret)
5487                 return -1;
5488
5489         pos = buf;
5490         end = buf + buflen;
5491
5492         ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
5493                           "NOISE=%d\nFREQUENCY=%u\n",
5494                           si.current_signal, si.current_txrate / 1000,
5495                           si.current_noise, si.frequency);
5496         if (ret < 0 || ret > end - pos)
5497                 return -1;
5498         pos += ret;
5499
5500         if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
5501                 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
5502                                   channel_width_name(si.chanwidth));
5503                 if (ret < 0 || ret > end - pos)
5504                         return -1;
5505                 pos += ret;
5506         }
5507
5508         if (si.center_frq1 > 0 && si.center_frq2 > 0) {
5509                 ret = os_snprintf(pos, end - pos,
5510                                   "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
5511                                   si.center_frq1, si.center_frq2);
5512                 if (ret < 0 || ret > end - pos)
5513                         return -1;
5514                 pos += ret;
5515         }
5516
5517         if (si.avg_signal) {
5518                 ret = os_snprintf(pos, end - pos,
5519                                   "AVG_RSSI=%d\n", si.avg_signal);
5520                 if (ret < 0 || ret >= end - pos)
5521                         return -1;
5522                 pos += ret;
5523         }
5524
5525         return pos - buf;
5526 }
5527
5528
5529 static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
5530                                       size_t buflen)
5531 {
5532         struct hostap_sta_driver_data sta;
5533         int ret;
5534
5535         ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
5536         if (ret)
5537                 return -1;
5538
5539         ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
5540                           sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
5541         if (ret < 0 || (size_t) ret > buflen)
5542                 return -1;
5543         return ret;
5544 }
5545
5546
5547 #ifdef ANDROID
5548 static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5549                                      char *buf, size_t buflen)
5550 {
5551         int ret;
5552
5553         ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
5554         if (ret == 0) {
5555                 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
5556                         struct p2p_data *p2p = wpa_s->global->p2p;
5557                         if (p2p) {
5558                                 char country[3];
5559                                 country[0] = cmd[8];
5560                                 country[1] = cmd[9];
5561                                 country[2] = 0x04;
5562                                 p2p_set_country(p2p, country);
5563                         }
5564                 }
5565                 ret = os_snprintf(buf, buflen, "%s\n", "OK");
5566         }
5567         return ret;
5568 }
5569 #endif /* ANDROID */
5570
5571
5572 static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5573                                      char *buf, size_t buflen)
5574 {
5575         int ret;
5576         char *pos;
5577         u8 *data = NULL;
5578         unsigned int vendor_id, subcmd;
5579         struct wpabuf *reply;
5580         size_t data_len = 0;
5581
5582         /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
5583         vendor_id = strtoul(cmd, &pos, 16);
5584         if (!isblank(*pos))
5585                 return -EINVAL;
5586
5587         subcmd = strtoul(pos, &pos, 10);
5588
5589         if (*pos != '\0') {
5590                 if (!isblank(*pos++))
5591                         return -EINVAL;
5592                 data_len = os_strlen(pos);
5593         }
5594
5595         if (data_len) {
5596                 data_len /= 2;
5597                 data = os_malloc(data_len);
5598                 if (!data)
5599                         return -1;
5600
5601                 if (hexstr2bin(pos, data, data_len)) {
5602                         wpa_printf(MSG_DEBUG,
5603                                    "Vendor command: wrong parameter format");
5604                         os_free(data);
5605                         return -EINVAL;
5606                 }
5607         }
5608
5609         reply = wpabuf_alloc((buflen - 1) / 2);
5610         if (!reply) {
5611                 os_free(data);
5612                 return -1;
5613         }
5614
5615         ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
5616                                  reply);
5617
5618         if (ret == 0)
5619                 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
5620                                        wpabuf_len(reply));
5621
5622         wpabuf_free(reply);
5623         os_free(data);
5624
5625         return ret;
5626 }
5627
5628
5629 static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
5630 {
5631         wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
5632
5633 #ifdef CONFIG_P2P
5634         wpas_p2p_cancel(wpa_s);
5635         wpas_p2p_stop_find(wpa_s);
5636         p2p_ctrl_flush(wpa_s);
5637         wpas_p2p_group_remove(wpa_s, "*");
5638         wpas_p2p_service_flush(wpa_s);
5639         wpa_s->global->p2p_disabled = 0;
5640         wpa_s->global->p2p_per_sta_psk = 0;
5641         wpa_s->conf->num_sec_device_types = 0;
5642         wpa_s->p2p_disable_ip_addr_req = 0;
5643 #endif /* CONFIG_P2P */
5644
5645 #ifdef CONFIG_WPS_TESTING
5646         wps_version_number = 0x20;
5647         wps_testing_dummy_cred = 0;
5648         wps_corrupt_pkhash = 0;
5649 #endif /* CONFIG_WPS_TESTING */
5650 #ifdef CONFIG_WPS
5651         wpa_s->wps_fragment_size = 0;
5652         wpas_wps_cancel(wpa_s);
5653 #endif /* CONFIG_WPS */
5654         wpa_s->after_wps = 0;
5655         wpa_s->known_wps_freq = 0;
5656
5657 #ifdef CONFIG_TDLS
5658 #ifdef CONFIG_TDLS_TESTING
5659         extern unsigned int tdls_testing;
5660         tdls_testing = 0;
5661 #endif /* CONFIG_TDLS_TESTING */
5662         wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
5663         wpa_tdls_enable(wpa_s->wpa, 1);
5664 #endif /* CONFIG_TDLS */
5665
5666         eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
5667         wpa_supplicant_stop_countermeasures(wpa_s, NULL);
5668
5669         wpa_s->no_keep_alive = 0;
5670
5671         os_free(wpa_s->disallow_aps_bssid);
5672         wpa_s->disallow_aps_bssid = NULL;
5673         wpa_s->disallow_aps_bssid_count = 0;
5674         os_free(wpa_s->disallow_aps_ssid);
5675         wpa_s->disallow_aps_ssid = NULL;
5676         wpa_s->disallow_aps_ssid_count = 0;
5677
5678         wpa_s->set_sta_uapsd = 0;
5679         wpa_s->sta_uapsd = 0;
5680
5681         wpa_drv_radio_disable(wpa_s, 0);
5682
5683         wpa_bss_flush(wpa_s);
5684         wpa_blacklist_clear(wpa_s);
5685         wpa_s->extra_blacklist_count = 0;
5686         wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
5687         wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
5688         wpa_config_flush_blobs(wpa_s->conf);
5689         wpa_s->conf->auto_interworking = 0;
5690         wpa_s->conf->okc = 0;
5691
5692         wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
5693         wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
5694         wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
5695         eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5696
5697         radio_remove_works(wpa_s, NULL, 1);
5698
5699         wpa_s->next_ssid = NULL;
5700
5701 #ifdef CONFIG_INTERWORKING
5702         hs20_cancel_fetch_osu(wpa_s);
5703 #endif /* CONFIG_INTERWORKING */
5704
5705         wpa_s->ext_mgmt_frame_handling = 0;
5706 }
5707
5708
5709 static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
5710                                      char *buf, size_t buflen)
5711 {
5712         struct wpa_radio_work *work;
5713         char *pos, *end;
5714         struct os_reltime now, diff;
5715
5716         pos = buf;
5717         end = buf + buflen;
5718
5719         os_get_reltime(&now);
5720
5721         dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
5722         {
5723                 int ret;
5724
5725                 os_reltime_sub(&now, &work->time, &diff);
5726                 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
5727                                   work->type, work->wpa_s->ifname, work->freq,
5728                                   work->started, diff.sec, diff.usec);
5729                 if (ret < 0 || ret >= end - pos)
5730                         break;
5731                 pos += ret;
5732         }
5733
5734         return pos - buf;
5735 }
5736
5737
5738 static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
5739 {
5740         struct wpa_radio_work *work = eloop_ctx;
5741         struct wpa_external_work *ework = work->ctx;
5742
5743         wpa_dbg(work->wpa_s, MSG_DEBUG,
5744                 "Timing out external radio work %u (%s)",
5745                 ework->id, work->type);
5746         wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
5747         os_free(ework);
5748         radio_work_done(work);
5749 }
5750
5751
5752 static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
5753 {
5754         struct wpa_external_work *ework = work->ctx;
5755
5756         if (deinit) {
5757                 if (work->started)
5758                         eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
5759                                              work, NULL);
5760
5761                 os_free(ework);
5762                 return;
5763         }
5764
5765         wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
5766                 ework->id, ework->type);
5767         wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
5768         if (!ework->timeout)
5769                 ework->timeout = 10;
5770         eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
5771                                work, NULL);
5772 }
5773
5774
5775 static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
5776                                     char *buf, size_t buflen)
5777 {
5778         struct wpa_external_work *ework;
5779         char *pos, *pos2;
5780         size_t type_len;
5781         int ret;
5782         unsigned int freq = 0;
5783
5784         /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
5785
5786         ework = os_zalloc(sizeof(*ework));
5787         if (ework == NULL)
5788                 return -1;
5789
5790         pos = os_strchr(cmd, ' ');
5791         if (pos) {
5792                 type_len = pos - cmd;
5793                 pos++;
5794
5795                 pos2 = os_strstr(pos, "freq=");
5796                 if (pos2)
5797                         freq = atoi(pos2 + 5);
5798
5799                 pos2 = os_strstr(pos, "timeout=");
5800                 if (pos2)
5801                         ework->timeout = atoi(pos2 + 8);
5802         } else {
5803                 type_len = os_strlen(cmd);
5804         }
5805         if (4 + type_len >= sizeof(ework->type))
5806                 type_len = sizeof(ework->type) - 4 - 1;
5807         os_strlcpy(ework->type, "ext:", sizeof(ework->type));
5808         os_memcpy(ework->type + 4, cmd, type_len);
5809         ework->type[4 + type_len] = '\0';
5810
5811         wpa_s->ext_work_id++;
5812         if (wpa_s->ext_work_id == 0)
5813                 wpa_s->ext_work_id++;
5814         ework->id = wpa_s->ext_work_id;
5815
5816         if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
5817                            ework) < 0) {
5818                 os_free(ework);
5819                 return -1;
5820         }
5821
5822         ret = os_snprintf(buf, buflen, "%u", ework->id);
5823         if (ret < 0 || (size_t) ret >= buflen)
5824                 return -1;
5825         return ret;
5826 }
5827
5828
5829 static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
5830 {
5831         struct wpa_radio_work *work;
5832         unsigned int id = atoi(cmd);
5833
5834         dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
5835         {
5836                 struct wpa_external_work *ework;
5837
5838                 if (os_strncmp(work->type, "ext:", 4) != 0)
5839                         continue;
5840                 ework = work->ctx;
5841                 if (id && ework->id != id)
5842                         continue;
5843                 wpa_dbg(wpa_s, MSG_DEBUG,
5844                         "Completed external radio work %u (%s)",
5845                         ework->id, ework->type);
5846                 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
5847                 radio_work_done(work);
5848                 os_free(ework);
5849                 return 3; /* "OK\n" */
5850         }
5851
5852         return -1;
5853 }
5854
5855
5856 static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
5857                                 char *buf, size_t buflen)
5858 {
5859         if (os_strcmp(cmd, "show") == 0)
5860                 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
5861         if (os_strncmp(cmd, "add ", 4) == 0)
5862                 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
5863         if (os_strncmp(cmd, "done ", 5) == 0)
5864                 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
5865         return -1;
5866 }
5867
5868
5869 void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
5870 {
5871         struct wpa_radio_work *work, *tmp;
5872
5873         if (!wpa_s || !wpa_s->radio)
5874                 return;
5875
5876         dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
5877                               struct wpa_radio_work, list) {
5878                 struct wpa_external_work *ework;
5879
5880                 if (os_strncmp(work->type, "ext:", 4) != 0)
5881                         continue;
5882                 ework = work->ctx;
5883                 wpa_dbg(wpa_s, MSG_DEBUG,
5884                         "Flushing%s external radio work %u (%s)",
5885                         work->started ? " started" : "", ework->id,
5886                         ework->type);
5887                 if (work->started)
5888                         eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
5889                                              work, NULL);
5890                 os_free(ework);
5891                 radio_work_done(work);
5892         }
5893 }
5894
5895
5896 static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
5897 {
5898         struct wpa_supplicant *wpa_s = eloop_ctx;
5899         eapol_sm_notify_ctrl_response(wpa_s->eapol);
5900 }
5901
5902
5903 static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
5904 {
5905         int *freqs = NULL;
5906
5907         freqs = freq_range_to_channel_list(wpa_s, val);
5908         if (freqs == NULL)
5909                 return -1;
5910
5911         os_free(wpa_s->manual_scan_freqs);
5912         wpa_s->manual_scan_freqs = freqs;
5913
5914         return 0;
5915 }
5916
5917
5918 static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
5919                            char *reply, int reply_size, int *reply_len)
5920 {
5921         char *pos;
5922
5923         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5924                 *reply_len = -1;
5925                 return;
5926         }
5927
5928         wpa_s->manual_scan_passive = 0;
5929         wpa_s->manual_scan_use_id = 0;
5930         wpa_s->manual_scan_only_new = 0;
5931
5932         if (params) {
5933                 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
5934                         wpa_s->scan_res_handler = scan_only_handler;
5935
5936                 pos = os_strstr(params, "freq=");
5937                 if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
5938                         *reply_len = -1;
5939                         return;
5940                 }
5941
5942                 pos = os_strstr(params, "passive=");
5943                 if (pos)
5944                         wpa_s->manual_scan_passive = !!atoi(pos + 8);
5945
5946                 pos = os_strstr(params, "use_id=");
5947                 if (pos)
5948                         wpa_s->manual_scan_use_id = atoi(pos + 7);
5949
5950                 pos = os_strstr(params, "only_new=1");
5951                 if (pos)
5952                         wpa_s->manual_scan_only_new = 1;
5953         } else {
5954                 os_free(wpa_s->manual_scan_freqs);
5955                 wpa_s->manual_scan_freqs = NULL;
5956                 if (wpa_s->scan_res_handler == scan_only_handler)
5957                         wpa_s->scan_res_handler = NULL;
5958         }
5959
5960         if (!wpa_s->sched_scanning && !wpa_s->scanning &&
5961             ((wpa_s->wpa_state <= WPA_SCANNING) ||
5962              (wpa_s->wpa_state == WPA_COMPLETED))) {
5963                 wpa_s->normal_scans = 0;
5964                 wpa_s->scan_req = MANUAL_SCAN_REQ;
5965                 wpa_s->after_wps = 0;
5966                 wpa_s->known_wps_freq = 0;
5967                 wpa_supplicant_req_scan(wpa_s, 0, 0);
5968                 if (wpa_s->manual_scan_use_id) {
5969                         wpa_s->manual_scan_id++;
5970                         wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
5971                                 wpa_s->manual_scan_id);
5972                         *reply_len = os_snprintf(reply, reply_size, "%u\n",
5973                                                  wpa_s->manual_scan_id);
5974                 }
5975         } else if (wpa_s->sched_scanning) {
5976                 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
5977                 wpa_supplicant_cancel_sched_scan(wpa_s);
5978                 wpa_s->scan_req = MANUAL_SCAN_REQ;
5979                 wpa_supplicant_req_scan(wpa_s, 0, 0);
5980                 if (wpa_s->manual_scan_use_id) {
5981                         wpa_s->manual_scan_id++;
5982                         *reply_len = os_snprintf(reply, reply_size, "%u\n",
5983                                                  wpa_s->manual_scan_id);
5984                         wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
5985                                 wpa_s->manual_scan_id);
5986                 }
5987         } else {
5988                 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
5989                 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
5990         }
5991 }
5992
5993
5994 #ifdef CONFIG_TESTING_OPTIONS
5995
5996 static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
5997                                        unsigned int freq, const u8 *dst,
5998                                        const u8 *src, const u8 *bssid,
5999                                        const u8 *data, size_t data_len,
6000                                        enum offchannel_send_action_result
6001                                        result)
6002 {
6003         wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
6004                 " src=" MACSTR " bssid=" MACSTR " result=%s",
6005                 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
6006                 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
6007                 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
6008                              "NO_ACK" : "FAILED"));
6009 }
6010
6011
6012 static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
6013 {
6014         char *pos, *param;
6015         size_t len;
6016         u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
6017         int res, used;
6018         int freq = 0, no_cck = 0, wait_time = 0;
6019
6020         /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
6021          *    <action=Action frame payload> */
6022
6023         wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
6024
6025         pos = cmd;
6026         used = hwaddr_aton2(pos, da);
6027         if (used < 0)
6028                 return -1;
6029         pos += used;
6030         while (*pos == ' ')
6031                 pos++;
6032         used = hwaddr_aton2(pos, bssid);
6033         if (used < 0)
6034                 return -1;
6035         pos += used;
6036
6037         param = os_strstr(pos, " freq=");
6038         if (param) {
6039                 param += 6;
6040                 freq = atoi(param);
6041         }
6042
6043         param = os_strstr(pos, " no_cck=");
6044         if (param) {
6045                 param += 8;
6046                 no_cck = atoi(param);
6047         }
6048
6049         param = os_strstr(pos, " wait_time=");
6050         if (param) {
6051                 param += 11;
6052                 wait_time = atoi(param);
6053         }
6054
6055         param = os_strstr(pos, " action=");
6056         if (param == NULL)
6057                 return -1;
6058         param += 8;
6059
6060         len = os_strlen(param);
6061         if (len & 1)
6062                 return -1;
6063         len /= 2;
6064
6065         buf = os_malloc(len);
6066         if (buf == NULL)
6067                 return -1;
6068
6069         if (hexstr2bin(param, buf, len) < 0) {
6070                 os_free(buf);
6071                 return -1;
6072         }
6073
6074         res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
6075                                      buf, len, wait_time,
6076                                      wpas_ctrl_iface_mgmt_tx_cb, no_cck);
6077         os_free(buf);
6078         return res;
6079 }
6080
6081
6082 static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
6083 {
6084         wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
6085         offchannel_send_action_done(wpa_s);
6086 }
6087
6088 #endif /* CONFIG_TESTING_OPTIONS */
6089
6090
6091 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
6092                                          char *buf, size_t *resp_len)
6093 {
6094         char *reply;
6095         const int reply_size = 4096;
6096         int reply_len;
6097
6098         if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
6099             os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
6100                 if (wpa_debug_show_keys)
6101                         wpa_dbg(wpa_s, MSG_DEBUG,
6102                                 "Control interface command '%s'", buf);
6103                 else
6104                         wpa_dbg(wpa_s, MSG_DEBUG,
6105                                 "Control interface command '%s [REMOVED]'",
6106                                 os_strncmp(buf, WPA_CTRL_RSP,
6107                                            os_strlen(WPA_CTRL_RSP)) == 0 ?
6108                                 WPA_CTRL_RSP : "SET_NETWORK");
6109         } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
6110                    os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
6111                 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
6112                                       (const u8 *) buf, os_strlen(buf));
6113         } else {
6114                 int level = MSG_DEBUG;
6115                 if (os_strcmp(buf, "PING") == 0)
6116                         level = MSG_EXCESSIVE;
6117                 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
6118         }
6119
6120         reply = os_malloc(reply_size);
6121         if (reply == NULL) {
6122                 *resp_len = 1;
6123                 return NULL;
6124         }
6125
6126         os_memcpy(reply, "OK\n", 3);
6127         reply_len = 3;
6128
6129         if (os_strcmp(buf, "PING") == 0) {
6130                 os_memcpy(reply, "PONG\n", 5);
6131                 reply_len = 5;
6132         } else if (os_strcmp(buf, "IFNAME") == 0) {
6133                 reply_len = os_strlen(wpa_s->ifname);
6134                 os_memcpy(reply, wpa_s->ifname, reply_len);
6135         } else if (os_strncmp(buf, "RELOG", 5) == 0) {
6136                 if (wpa_debug_reopen_file() < 0)
6137                         reply_len = -1;
6138         } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
6139                 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
6140         } else if (os_strcmp(buf, "MIB") == 0) {
6141                 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
6142                 if (reply_len >= 0) {
6143                         int res;
6144                         res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
6145                                                reply_size - reply_len);
6146                         if (res < 0)
6147                                 reply_len = -1;
6148                         else
6149                                 reply_len += res;
6150                 }
6151         } else if (os_strncmp(buf, "STATUS", 6) == 0) {
6152                 reply_len = wpa_supplicant_ctrl_iface_status(
6153                         wpa_s, buf + 6, reply, reply_size);
6154         } else if (os_strcmp(buf, "PMKSA") == 0) {
6155                 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
6156                                                     reply_size);
6157         } else if (os_strncmp(buf, "SET ", 4) == 0) {
6158                 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
6159                         reply_len = -1;
6160         } else if (os_strncmp(buf, "GET ", 4) == 0) {
6161                 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
6162                                                           reply, reply_size);
6163         } else if (os_strcmp(buf, "LOGON") == 0) {
6164                 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
6165         } else if (os_strcmp(buf, "LOGOFF") == 0) {
6166                 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
6167         } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
6168                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
6169                         reply_len = -1;
6170                 else
6171                         wpas_request_connection(wpa_s);
6172         } else if (os_strcmp(buf, "REATTACH") == 0) {
6173                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
6174                     !wpa_s->current_ssid)
6175                         reply_len = -1;
6176                 else {
6177                         wpa_s->reattach = 1;
6178                         wpas_request_connection(wpa_s);
6179                 }
6180         } else if (os_strcmp(buf, "RECONNECT") == 0) {
6181                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
6182                         reply_len = -1;
6183                 else if (wpa_s->disconnected)
6184                         wpas_request_connection(wpa_s);
6185 #ifdef IEEE8021X_EAPOL
6186         } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
6187                 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
6188                         reply_len = -1;
6189 #endif /* IEEE8021X_EAPOL */
6190 #ifdef CONFIG_PEERKEY
6191         } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
6192                 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
6193                         reply_len = -1;
6194 #endif /* CONFIG_PEERKEY */
6195 #ifdef CONFIG_IEEE80211R
6196         } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
6197                 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
6198                         reply_len = -1;
6199 #endif /* CONFIG_IEEE80211R */
6200 #ifdef CONFIG_WPS
6201         } else if (os_strcmp(buf, "WPS_PBC") == 0) {
6202                 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
6203                 if (res == -2) {
6204                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
6205                         reply_len = 17;
6206                 } else if (res)
6207                         reply_len = -1;
6208         } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
6209                 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
6210                 if (res == -2) {
6211                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
6212                         reply_len = 17;
6213                 } else if (res)
6214                         reply_len = -1;
6215         } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
6216                 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
6217                                                               reply,
6218                                                               reply_size);
6219         } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
6220                 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
6221                         wpa_s, buf + 14, reply, reply_size);
6222         } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
6223                 if (wpas_wps_cancel(wpa_s))
6224                         reply_len = -1;
6225 #ifdef CONFIG_WPS_NFC
6226         } else if (os_strcmp(buf, "WPS_NFC") == 0) {
6227                 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
6228                         reply_len = -1;
6229         } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
6230                 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
6231                         reply_len = -1;
6232         } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
6233                 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
6234                         wpa_s, buf + 21, reply, reply_size);
6235         } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
6236                 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
6237                         wpa_s, buf + 14, reply, reply_size);
6238         } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
6239                 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
6240                                                                buf + 17))
6241                         reply_len = -1;
6242         } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
6243                 reply_len = wpas_ctrl_nfc_get_handover_req(
6244                         wpa_s, buf + 21, reply, reply_size);
6245         } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
6246                 reply_len = wpas_ctrl_nfc_get_handover_sel(
6247                         wpa_s, buf + 21, reply, reply_size);
6248         } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
6249                 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
6250                         reply_len = -1;
6251 #endif /* CONFIG_WPS_NFC */
6252         } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
6253                 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
6254                         reply_len = -1;
6255 #ifdef CONFIG_AP
6256         } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
6257                 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
6258                         wpa_s, buf + 11, reply, reply_size);
6259 #endif /* CONFIG_AP */
6260 #ifdef CONFIG_WPS_ER
6261         } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
6262                 if (wpas_wps_er_start(wpa_s, NULL))
6263                         reply_len = -1;
6264         } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
6265                 if (wpas_wps_er_start(wpa_s, buf + 13))
6266                         reply_len = -1;
6267         } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
6268                 if (wpas_wps_er_stop(wpa_s))
6269                         reply_len = -1;
6270         } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
6271                 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
6272                         reply_len = -1;
6273         } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
6274                 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
6275                 if (ret == -2) {
6276                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
6277                         reply_len = 17;
6278                 } else if (ret == -3) {
6279                         os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
6280                         reply_len = 18;
6281                 } else if (ret == -4) {
6282                         os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
6283                         reply_len = 20;
6284                 } else if (ret)
6285                         reply_len = -1;
6286         } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
6287                 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
6288                         reply_len = -1;
6289         } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
6290                 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
6291                                                                 buf + 18))
6292                         reply_len = -1;
6293         } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
6294                 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
6295                         reply_len = -1;
6296 #ifdef CONFIG_WPS_NFC
6297         } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
6298                 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
6299                         wpa_s, buf + 24, reply, reply_size);
6300 #endif /* CONFIG_WPS_NFC */
6301 #endif /* CONFIG_WPS_ER */
6302 #endif /* CONFIG_WPS */
6303 #ifdef CONFIG_IBSS_RSN
6304         } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
6305                 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
6306                         reply_len = -1;
6307 #endif /* CONFIG_IBSS_RSN */
6308 #ifdef CONFIG_P2P
6309         } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
6310                 if (p2p_ctrl_find(wpa_s, buf + 9))
6311                         reply_len = -1;
6312         } else if (os_strcmp(buf, "P2P_FIND") == 0) {
6313                 if (p2p_ctrl_find(wpa_s, ""))
6314                         reply_len = -1;
6315         } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
6316                 wpas_p2p_stop_find(wpa_s);
6317         } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
6318                 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
6319                                              reply_size);
6320         } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
6321                 if (p2p_ctrl_listen(wpa_s, buf + 11))
6322                         reply_len = -1;
6323         } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
6324                 if (p2p_ctrl_listen(wpa_s, ""))
6325                         reply_len = -1;
6326         } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
6327                 if (wpas_p2p_group_remove(wpa_s, buf + 17))
6328                         reply_len = -1;
6329         } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
6330                 if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
6331                         reply_len = -1;
6332         } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
6333                 if (p2p_ctrl_group_add(wpa_s, buf + 14))
6334                         reply_len = -1;
6335         } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
6336                 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
6337                         reply_len = -1;
6338         } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
6339                 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
6340         } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
6341                 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
6342                                                    reply_size);
6343         } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
6344                 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
6345                         reply_len = -1;
6346         } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
6347                 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
6348                         reply_len = -1;
6349         } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
6350                 wpas_p2p_sd_service_update(wpa_s);
6351         } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
6352                 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
6353                         reply_len = -1;
6354         } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
6355                 wpas_p2p_service_flush(wpa_s);
6356         } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
6357                 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
6358                         reply_len = -1;
6359         } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
6360                 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
6361                         reply_len = -1;
6362         } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
6363                 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
6364                         reply_len = -1;
6365         } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
6366                 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
6367                         reply_len = -1;
6368         } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
6369                 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
6370                                               reply_size);
6371         } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
6372                 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
6373                         reply_len = -1;
6374         } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
6375                 p2p_ctrl_flush(wpa_s);
6376         } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
6377                 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
6378                         reply_len = -1;
6379         } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
6380                 if (wpas_p2p_cancel(wpa_s))
6381                         reply_len = -1;
6382         } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
6383                 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
6384                         reply_len = -1;
6385         } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
6386                 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
6387                         reply_len = -1;
6388         } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
6389                 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
6390                         reply_len = -1;
6391         } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
6392                 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
6393                         reply_len = -1;
6394         } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
6395                 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
6396                         reply_len = -1;
6397 #endif /* CONFIG_P2P */
6398 #ifdef CONFIG_WIFI_DISPLAY
6399         } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
6400                 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
6401                         reply_len = -1;
6402         } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
6403                 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
6404                                                      reply, reply_size);
6405 #endif /* CONFIG_WIFI_DISPLAY */
6406 #ifdef CONFIG_INTERWORKING
6407         } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
6408                 if (interworking_fetch_anqp(wpa_s) < 0)
6409                         reply_len = -1;
6410         } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
6411                 interworking_stop_fetch_anqp(wpa_s);
6412         } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
6413                 if (ctrl_interworking_select(wpa_s, NULL) < 0)
6414                         reply_len = -1;
6415         } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
6416                 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
6417                         reply_len = -1;
6418         } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
6419                 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
6420                         reply_len = -1;
6421         } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
6422                 if (get_anqp(wpa_s, buf + 9) < 0)
6423                         reply_len = -1;
6424         } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
6425                 if (gas_request(wpa_s, buf + 12) < 0)
6426                         reply_len = -1;
6427         } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
6428                 reply_len = gas_response_get(wpa_s, buf + 17, reply,
6429                                              reply_size);
6430 #endif /* CONFIG_INTERWORKING */
6431 #ifdef CONFIG_HS20
6432         } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
6433                 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
6434                         reply_len = -1;
6435         } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
6436                 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
6437                         reply_len = -1;
6438         } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
6439                 if (hs20_icon_request(wpa_s, buf + 18) < 0)
6440                         reply_len = -1;
6441         } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
6442                 if (hs20_fetch_osu(wpa_s) < 0)
6443                         reply_len = -1;
6444         } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
6445                 hs20_cancel_fetch_osu(wpa_s);
6446 #endif /* CONFIG_HS20 */
6447         } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
6448         {
6449                 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
6450                             wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
6451                         reply_len = -1;
6452                 else {
6453                         /*
6454                          * Notify response from timeout to allow the control
6455                          * interface response to be sent first.
6456                          */
6457                         eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
6458                                                wpa_s, NULL);
6459                 }
6460         } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
6461                 if (wpa_supplicant_reload_configuration(wpa_s))
6462                         reply_len = -1;
6463         } else if (os_strcmp(buf, "TERMINATE") == 0) {
6464                 wpa_supplicant_terminate_proc(wpa_s->global);
6465         } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
6466                 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
6467                         reply_len = -1;
6468         } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
6469                 reply_len = wpa_supplicant_ctrl_iface_blacklist(
6470                         wpa_s, buf + 9, reply, reply_size);
6471         } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
6472                 reply_len = wpa_supplicant_ctrl_iface_log_level(
6473                         wpa_s, buf + 9, reply, reply_size);
6474         } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
6475                 reply_len = wpa_supplicant_ctrl_iface_list_networks(
6476                         wpa_s, reply, reply_size);
6477         } else if (os_strcmp(buf, "DISCONNECT") == 0) {
6478 #ifdef CONFIG_SME
6479                 wpa_s->sme.prev_bssid_set = 0;
6480 #endif /* CONFIG_SME */
6481                 wpa_s->reassociate = 0;
6482                 wpa_s->disconnected = 1;
6483                 wpa_supplicant_cancel_sched_scan(wpa_s);
6484                 wpa_supplicant_cancel_scan(wpa_s);
6485                 wpa_supplicant_deauthenticate(wpa_s,
6486                                               WLAN_REASON_DEAUTH_LEAVING);
6487         } else if (os_strcmp(buf, "SCAN") == 0) {
6488                 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
6489         } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
6490                 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
6491         } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
6492                 reply_len = wpa_supplicant_ctrl_iface_scan_results(
6493                         wpa_s, reply, reply_size);
6494         } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
6495                 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
6496                         reply_len = -1;
6497         } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
6498                 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
6499                         reply_len = -1;
6500         } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
6501                 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
6502                         reply_len = -1;
6503         } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
6504                 reply_len = wpa_supplicant_ctrl_iface_add_network(
6505                         wpa_s, reply, reply_size);
6506         } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
6507                 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
6508                         reply_len = -1;
6509         } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
6510                 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
6511                         reply_len = -1;
6512         } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
6513                 reply_len = wpa_supplicant_ctrl_iface_get_network(
6514                         wpa_s, buf + 12, reply, reply_size);
6515         } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
6516                 if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12))
6517                         reply_len = -1;
6518         } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
6519                 reply_len = wpa_supplicant_ctrl_iface_list_creds(
6520                         wpa_s, reply, reply_size);
6521         } else if (os_strcmp(buf, "ADD_CRED") == 0) {
6522                 reply_len = wpa_supplicant_ctrl_iface_add_cred(
6523                         wpa_s, reply, reply_size);
6524         } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
6525                 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
6526                         reply_len = -1;
6527         } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
6528                 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
6529                         reply_len = -1;
6530         } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
6531                 reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
6532                                                                reply,
6533                                                                reply_size);
6534 #ifndef CONFIG_NO_CONFIG_WRITE
6535         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
6536                 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
6537                         reply_len = -1;
6538 #endif /* CONFIG_NO_CONFIG_WRITE */
6539         } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
6540                 reply_len = wpa_supplicant_ctrl_iface_get_capability(
6541                         wpa_s, buf + 15, reply, reply_size);
6542         } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
6543                 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
6544                         reply_len = -1;
6545         } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
6546                 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
6547                         reply_len = -1;
6548         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
6549                 reply_len = wpa_supplicant_global_iface_list(
6550                         wpa_s->global, reply, reply_size);
6551         } else if (os_strcmp(buf, "INTERFACES") == 0) {
6552                 reply_len = wpa_supplicant_global_iface_interfaces(
6553                         wpa_s->global, reply, reply_size);
6554         } else if (os_strncmp(buf, "BSS ", 4) == 0) {
6555                 reply_len = wpa_supplicant_ctrl_iface_bss(
6556                         wpa_s, buf + 4, reply, reply_size);
6557 #ifdef CONFIG_AP
6558         } else if (os_strcmp(buf, "STA-FIRST") == 0) {
6559                 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
6560         } else if (os_strncmp(buf, "STA ", 4) == 0) {
6561                 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
6562                                               reply_size);
6563         } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
6564                 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
6565                                                    reply_size);
6566         } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
6567                 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
6568                         reply_len = -1;
6569         } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
6570                 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
6571                         reply_len = -1;
6572         } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
6573                 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
6574                         reply_len = -1;
6575 #endif /* CONFIG_AP */
6576         } else if (os_strcmp(buf, "SUSPEND") == 0) {
6577                 wpas_notify_suspend(wpa_s->global);
6578         } else if (os_strcmp(buf, "RESUME") == 0) {
6579                 wpas_notify_resume(wpa_s->global);
6580 #ifdef CONFIG_TESTING_OPTIONS
6581         } else if (os_strcmp(buf, "DROP_SA") == 0) {
6582                 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
6583 #endif /* CONFIG_TESTING_OPTIONS */
6584         } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
6585                 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
6586                         reply_len = -1;
6587         } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
6588                 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
6589                         reply_len = -1;
6590         } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
6591                 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
6592                         reply_len = -1;
6593         } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
6594                 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
6595                                                                buf + 17))
6596                         reply_len = -1;
6597         } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
6598                 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
6599                         reply_len = -1;
6600 #ifdef CONFIG_TDLS
6601         } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
6602                 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
6603                         reply_len = -1;
6604         } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
6605                 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
6606                         reply_len = -1;
6607         } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
6608                 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
6609                         reply_len = -1;
6610 #endif /* CONFIG_TDLS */
6611         } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
6612                 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
6613                                                        reply_size);
6614         } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
6615                 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
6616                                                        reply_size);
6617 #ifdef CONFIG_AUTOSCAN
6618         } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
6619                 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
6620                         reply_len = -1;
6621 #endif /* CONFIG_AUTOSCAN */
6622 #ifdef ANDROID
6623         } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
6624                 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
6625                                                       reply_size);
6626 #endif /* ANDROID */
6627         } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
6628                 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
6629                                                       reply_size);
6630         } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
6631                 pmksa_cache_clear_current(wpa_s->wpa);
6632                 eapol_sm_request_reauth(wpa_s->eapol);
6633 #ifdef CONFIG_WNM
6634         } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
6635                 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
6636                         reply_len = -1;
6637         } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
6638                 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
6639                                 reply_len = -1;
6640 #endif /* CONFIG_WNM */
6641         } else if (os_strcmp(buf, "FLUSH") == 0) {
6642                 wpa_supplicant_ctrl_iface_flush(wpa_s);
6643         } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
6644                 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
6645                                                  reply_size);
6646 #ifdef CONFIG_TESTING_OPTIONS
6647         } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
6648                 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
6649                         reply_len = -1;
6650         } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
6651                 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
6652 #endif /* CONFIG_TESTING_OPTIONS */
6653         } else {
6654                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
6655                 reply_len = 16;
6656         }
6657
6658         if (reply_len < 0) {
6659                 os_memcpy(reply, "FAIL\n", 5);
6660                 reply_len = 5;
6661         }
6662
6663         *resp_len = reply_len;
6664         return reply;
6665 }
6666
6667
6668 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
6669                                            char *cmd)
6670 {
6671         struct wpa_interface iface;
6672         char *pos;
6673
6674         /*
6675          * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
6676          * TAB<bridge_ifname>
6677          */
6678         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
6679
6680         os_memset(&iface, 0, sizeof(iface));
6681
6682         do {
6683                 iface.ifname = pos = cmd;
6684                 pos = os_strchr(pos, '\t');
6685                 if (pos)
6686                         *pos++ = '\0';
6687                 if (iface.ifname[0] == '\0')
6688                         return -1;
6689                 if (pos == NULL)
6690                         break;
6691
6692                 iface.confname = pos;
6693                 pos = os_strchr(pos, '\t');
6694                 if (pos)
6695                         *pos++ = '\0';
6696                 if (iface.confname[0] == '\0')
6697                         iface.confname = NULL;
6698                 if (pos == NULL)
6699                         break;
6700
6701                 iface.driver = pos;
6702                 pos = os_strchr(pos, '\t');
6703                 if (pos)
6704                         *pos++ = '\0';
6705                 if (iface.driver[0] == '\0')
6706                         iface.driver = NULL;
6707                 if (pos == NULL)
6708                         break;
6709
6710                 iface.ctrl_interface = pos;
6711                 pos = os_strchr(pos, '\t');
6712                 if (pos)
6713                         *pos++ = '\0';
6714                 if (iface.ctrl_interface[0] == '\0')
6715                         iface.ctrl_interface = NULL;
6716                 if (pos == NULL)
6717                         break;
6718
6719                 iface.driver_param = pos;
6720                 pos = os_strchr(pos, '\t');
6721                 if (pos)
6722                         *pos++ = '\0';
6723                 if (iface.driver_param[0] == '\0')
6724                         iface.driver_param = NULL;
6725                 if (pos == NULL)
6726                         break;
6727
6728                 iface.bridge_ifname = pos;
6729                 pos = os_strchr(pos, '\t');
6730                 if (pos)
6731                         *pos++ = '\0';
6732                 if (iface.bridge_ifname[0] == '\0')
6733                         iface.bridge_ifname = NULL;
6734                 if (pos == NULL)
6735                         break;
6736         } while (0);
6737
6738         if (wpa_supplicant_get_iface(global, iface.ifname))
6739                 return -1;
6740
6741         return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
6742 }
6743
6744
6745 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
6746                                               char *cmd)
6747 {
6748         struct wpa_supplicant *wpa_s;
6749
6750         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
6751
6752         wpa_s = wpa_supplicant_get_iface(global, cmd);
6753         if (wpa_s == NULL)
6754                 return -1;
6755         return wpa_supplicant_remove_iface(global, wpa_s, 0);
6756 }
6757
6758
6759 static void wpa_free_iface_info(struct wpa_interface_info *iface)
6760 {
6761         struct wpa_interface_info *prev;
6762
6763         while (iface) {
6764                 prev = iface;
6765                 iface = iface->next;
6766
6767                 os_free(prev->ifname);
6768                 os_free(prev->desc);
6769                 os_free(prev);
6770         }
6771 }
6772
6773
6774 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
6775                                             char *buf, int len)
6776 {
6777         int i, res;
6778         struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
6779         char *pos, *end;
6780
6781         for (i = 0; wpa_drivers[i]; i++) {
6782                 struct wpa_driver_ops *drv = wpa_drivers[i];
6783                 if (drv->get_interfaces == NULL)
6784                         continue;
6785                 tmp = drv->get_interfaces(global->drv_priv[i]);
6786                 if (tmp == NULL)
6787                         continue;
6788
6789                 if (last == NULL)
6790                         iface = last = tmp;
6791                 else
6792                         last->next = tmp;
6793                 while (last->next)
6794                         last = last->next;
6795         }
6796
6797         pos = buf;
6798         end = buf + len;
6799         for (tmp = iface; tmp; tmp = tmp->next) {
6800                 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
6801                                   tmp->drv_name, tmp->ifname,
6802                                   tmp->desc ? tmp->desc : "");
6803                 if (res < 0 || res >= end - pos) {
6804                         *pos = '\0';
6805                         break;
6806                 }
6807                 pos += res;
6808         }
6809
6810         wpa_free_iface_info(iface);
6811
6812         return pos - buf;
6813 }
6814
6815
6816 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
6817                                                   char *buf, int len)
6818 {
6819         int res;
6820         char *pos, *end;
6821         struct wpa_supplicant *wpa_s;
6822
6823         wpa_s = global->ifaces;
6824         pos = buf;
6825         end = buf + len;
6826
6827         while (wpa_s) {
6828                 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
6829                 if (res < 0 || res >= end - pos) {
6830                         *pos = '\0';
6831                         break;
6832                 }
6833                 pos += res;
6834                 wpa_s = wpa_s->next;
6835         }
6836         return pos - buf;
6837 }
6838
6839
6840 static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
6841                                             const char *ifname,
6842                                             char *cmd, size_t *resp_len)
6843 {
6844         struct wpa_supplicant *wpa_s;
6845
6846         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6847                 if (os_strcmp(ifname, wpa_s->ifname) == 0)
6848                         break;
6849         }
6850
6851         if (wpa_s == NULL) {
6852                 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
6853                 if (resp)
6854                         *resp_len = os_strlen(resp);
6855                 else
6856                         *resp_len = 1;
6857                 return resp;
6858         }
6859
6860         return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
6861 }
6862
6863
6864 static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
6865                                                char *buf, size_t *resp_len)
6866 {
6867 #ifdef CONFIG_P2P
6868         static const char * cmd[] = {
6869                 "LIST_NETWORKS",
6870                 "P2P_FIND",
6871                 "P2P_STOP_FIND",
6872                 "P2P_LISTEN",
6873                 "P2P_GROUP_ADD",
6874                 "P2P_GET_PASSPHRASE",
6875                 "P2P_SERVICE_UPDATE",
6876                 "P2P_SERVICE_FLUSH",
6877                 "P2P_FLUSH",
6878                 "P2P_CANCEL",
6879                 "P2P_PRESENCE_REQ",
6880                 "P2P_EXT_LISTEN",
6881                 NULL
6882         };
6883         static const char * prefix[] = {
6884 #ifdef ANDROID
6885                 "DRIVER ",
6886 #endif /* ANDROID */
6887                 "GET_NETWORK ",
6888                 "REMOVE_NETWORK ",
6889                 "P2P_FIND ",
6890                 "P2P_CONNECT ",
6891                 "P2P_LISTEN ",
6892                 "P2P_GROUP_REMOVE ",
6893                 "P2P_GROUP_ADD ",
6894                 "P2P_PROV_DISC ",
6895                 "P2P_SERV_DISC_REQ ",
6896                 "P2P_SERV_DISC_CANCEL_REQ ",
6897                 "P2P_SERV_DISC_RESP ",
6898                 "P2P_SERV_DISC_EXTERNAL ",
6899                 "P2P_SERVICE_ADD ",
6900                 "P2P_SERVICE_DEL ",
6901                 "P2P_REJECT ",
6902                 "P2P_INVITE ",
6903                 "P2P_PEER ",
6904                 "P2P_SET ",
6905                 "P2P_UNAUTHORIZE ",
6906                 "P2P_PRESENCE_REQ ",
6907                 "P2P_EXT_LISTEN ",
6908                 "P2P_REMOVE_CLIENT ",
6909                 "NFC_GET_HANDOVER_SEL ",
6910                 "NFC_GET_HANDOVER_REQ ",
6911                 "NFC_REPORT_HANDOVER ",
6912                 NULL
6913         };
6914         int found = 0;
6915         int i;
6916
6917         if (global->p2p_init_wpa_s == NULL)
6918                 return NULL;
6919
6920         for (i = 0; !found && cmd[i]; i++) {
6921                 if (os_strcmp(buf, cmd[i]) == 0)
6922                         found = 1;
6923         }
6924
6925         for (i = 0; !found && prefix[i]; i++) {
6926                 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
6927                         found = 1;
6928         }
6929
6930         if (found)
6931                 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6932                                                          buf, resp_len);
6933 #endif /* CONFIG_P2P */
6934         return NULL;
6935 }
6936
6937
6938 static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
6939                                                char *buf, size_t *resp_len)
6940 {
6941 #ifdef CONFIG_WIFI_DISPLAY
6942         if (global->p2p_init_wpa_s == NULL)
6943                 return NULL;
6944         if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
6945             os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
6946                 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6947                                                          buf, resp_len);
6948 #endif /* CONFIG_WIFI_DISPLAY */
6949         return NULL;
6950 }
6951
6952
6953 static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
6954                                            char *buf, size_t *resp_len)
6955 {
6956         char *ret;
6957
6958         ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
6959         if (ret)
6960                 return ret;
6961
6962         ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
6963         if (ret)
6964                 return ret;
6965
6966         return NULL;
6967 }
6968
6969
6970 static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
6971 {
6972         char *value;
6973
6974         value = os_strchr(cmd, ' ');
6975         if (value == NULL)
6976                 return -1;
6977         *value++ = '\0';
6978
6979         wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
6980
6981 #ifdef CONFIG_WIFI_DISPLAY
6982         if (os_strcasecmp(cmd, "wifi_display") == 0) {
6983                 wifi_display_enable(global, !!atoi(value));
6984                 return 0;
6985         }
6986 #endif /* CONFIG_WIFI_DISPLAY */
6987
6988         /* Restore cmd to its original value to allow redirection */
6989         value[-1] = ' ';
6990
6991         return -1;
6992 }
6993
6994
6995 #ifndef CONFIG_NO_CONFIG_WRITE
6996 static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
6997 {
6998         int ret = 0, saved = 0;
6999         struct wpa_supplicant *wpa_s;
7000
7001         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7002                 if (!wpa_s->conf->update_config) {
7003                         wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
7004                         continue;
7005                 }
7006
7007                 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
7008                         wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
7009                         ret = 1;
7010                 } else {
7011                         wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
7012                         saved++;
7013                 }
7014         }
7015
7016         if (!saved && !ret) {
7017                 wpa_dbg(wpa_s, MSG_DEBUG,
7018                         "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
7019                 ret = 1;
7020         }
7021
7022         return ret;
7023 }
7024 #endif /* CONFIG_NO_CONFIG_WRITE */
7025
7026
7027 static int wpas_global_ctrl_iface_status(struct wpa_global *global,
7028                                          char *buf, size_t buflen)
7029 {
7030         char *pos, *end;
7031         int ret;
7032         struct wpa_supplicant *wpa_s;
7033
7034         pos = buf;
7035         end = buf + buflen;
7036
7037 #ifdef CONFIG_P2P
7038         if (global->p2p && !global->p2p_disabled) {
7039                 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
7040                                   "\n"
7041                                   "p2p_state=%s\n",
7042                                   MAC2STR(global->p2p_dev_addr),
7043                                   p2p_get_state_txt(global->p2p));
7044                 if (ret < 0 || ret >= end - pos)
7045                         return pos - buf;
7046                 pos += ret;
7047         } else if (global->p2p) {
7048                 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
7049                 if (ret < 0 || ret >= end - pos)
7050                         return pos - buf;
7051                 pos += ret;
7052         }
7053 #endif /* CONFIG_P2P */
7054
7055 #ifdef CONFIG_WIFI_DISPLAY
7056         ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
7057                           !!global->wifi_display);
7058         if (ret < 0 || ret >= end - pos)
7059                 return pos - buf;
7060         pos += ret;
7061 #endif /* CONFIG_WIFI_DISPLAY */
7062
7063         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7064                 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
7065                                   "address=" MACSTR "\n",
7066                                   wpa_s->ifname, MAC2STR(wpa_s->own_addr));
7067                 if (ret < 0 || ret >= end - pos)
7068                         return pos - buf;
7069                 pos += ret;
7070         }
7071
7072         return pos - buf;
7073 }
7074
7075
7076 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
7077                                                 char *buf, size_t *resp_len)
7078 {
7079         char *reply;
7080         const int reply_size = 2048;
7081         int reply_len;
7082         int level = MSG_DEBUG;
7083
7084         if (os_strncmp(buf, "IFNAME=", 7) == 0) {
7085                 char *pos = os_strchr(buf + 7, ' ');
7086                 if (pos) {
7087                         *pos++ = '\0';
7088                         return wpas_global_ctrl_iface_ifname(global,
7089                                                              buf + 7, pos,
7090                                                              resp_len);
7091                 }
7092         }
7093
7094         reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
7095         if (reply)
7096                 return reply;
7097
7098         if (os_strcmp(buf, "PING") == 0)
7099                 level = MSG_EXCESSIVE;
7100         wpa_hexdump_ascii(level, "RX global ctrl_iface",
7101                           (const u8 *) buf, os_strlen(buf));
7102
7103         reply = os_malloc(reply_size);
7104         if (reply == NULL) {
7105                 *resp_len = 1;
7106                 return NULL;
7107         }
7108
7109         os_memcpy(reply, "OK\n", 3);
7110         reply_len = 3;
7111
7112         if (os_strcmp(buf, "PING") == 0) {
7113                 os_memcpy(reply, "PONG\n", 5);
7114                 reply_len = 5;
7115         } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
7116                 if (wpa_supplicant_global_iface_add(global, buf + 14))
7117                         reply_len = -1;
7118         } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
7119                 if (wpa_supplicant_global_iface_remove(global, buf + 17))
7120                         reply_len = -1;
7121         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
7122                 reply_len = wpa_supplicant_global_iface_list(
7123                         global, reply, reply_size);
7124         } else if (os_strcmp(buf, "INTERFACES") == 0) {
7125                 reply_len = wpa_supplicant_global_iface_interfaces(
7126                         global, reply, reply_size);
7127         } else if (os_strcmp(buf, "TERMINATE") == 0) {
7128                 wpa_supplicant_terminate_proc(global);
7129         } else if (os_strcmp(buf, "SUSPEND") == 0) {
7130                 wpas_notify_suspend(global);
7131         } else if (os_strcmp(buf, "RESUME") == 0) {
7132                 wpas_notify_resume(global);
7133         } else if (os_strncmp(buf, "SET ", 4) == 0) {
7134                 if (wpas_global_ctrl_iface_set(global, buf + 4)) {
7135 #ifdef CONFIG_P2P
7136                         if (global->p2p_init_wpa_s) {
7137                                 os_free(reply);
7138                                 /* Check if P2P redirection would work for this
7139                                  * command. */
7140                                 return wpa_supplicant_ctrl_iface_process(
7141                                         global->p2p_init_wpa_s,
7142                                         buf, resp_len);
7143                         }
7144 #endif /* CONFIG_P2P */
7145                         reply_len = -1;
7146                 }
7147 #ifndef CONFIG_NO_CONFIG_WRITE
7148         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
7149                 if (wpas_global_ctrl_iface_save_config(global))
7150                         reply_len = -1;
7151 #endif /* CONFIG_NO_CONFIG_WRITE */
7152         } else if (os_strcmp(buf, "STATUS") == 0) {
7153                 reply_len = wpas_global_ctrl_iface_status(global, reply,
7154                                                           reply_size);
7155 #ifdef CONFIG_MODULE_TESTS
7156         } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
7157                 int wpas_module_tests(void);
7158                 if (wpas_module_tests() < 0)
7159                         reply_len = -1;
7160 #endif /* CONFIG_MODULE_TESTS */
7161         } else {
7162                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
7163                 reply_len = 16;
7164         }
7165
7166         if (reply_len < 0) {
7167                 os_memcpy(reply, "FAIL\n", 5);
7168                 reply_len = 5;
7169         }
7170
7171         *resp_len = reply_len;
7172         return reply;
7173 }