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