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