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