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