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