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