ctrl_iface: Check for ongoing sched_scan on SCAN command
[mech_eap.git] / wpa_supplicant / ctrl_iface.c
1 /*
2  * WPA Supplicant / Control interface (shared code for all backends)
3  * Copyright (c) 2004-2012, 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 "common/version.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/ieee802_11_common.h"
16 #include "common/wpa_ctrl.h"
17 #include "eap_peer/eap.h"
18 #include "eapol_supp/eapol_supp_sm.h"
19 #include "rsn_supp/wpa.h"
20 #include "rsn_supp/preauth.h"
21 #include "rsn_supp/pmksa_cache.h"
22 #include "l2_packet/l2_packet.h"
23 #include "wps/wps.h"
24 #include "config.h"
25 #include "wpa_supplicant_i.h"
26 #include "driver_i.h"
27 #include "wps_supplicant.h"
28 #include "ibss_rsn.h"
29 #include "ap.h"
30 #include "p2p_supplicant.h"
31 #include "p2p/p2p.h"
32 #include "hs20_supplicant.h"
33 #include "wifi_display.h"
34 #include "notify.h"
35 #include "bss.h"
36 #include "scan.h"
37 #include "ctrl_iface.h"
38 #include "interworking.h"
39 #include "blacklist.h"
40 #include "autoscan.h"
41 #include "wnm_sta.h"
42
43 extern struct wpa_driver_ops *wpa_drivers[];
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
50
51 static int pno_start(struct wpa_supplicant *wpa_s)
52 {
53         int ret;
54         size_t i, num_ssid;
55         struct wpa_ssid *ssid;
56         struct wpa_driver_scan_params params;
57
58         if (wpa_s->pno)
59                 return 0;
60
61         os_memset(&params, 0, sizeof(params));
62
63         num_ssid = 0;
64         ssid = wpa_s->conf->ssid;
65         while (ssid) {
66                 if (!wpas_network_disabled(wpa_s, ssid))
67                         num_ssid++;
68                 ssid = ssid->next;
69         }
70         if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
71                 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
72                            "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
73                 num_ssid = WPAS_MAX_SCAN_SSIDS;
74         }
75
76         if (num_ssid == 0) {
77                 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
78                 return -1;
79         }
80
81         params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
82                                         num_ssid);
83         if (params.filter_ssids == NULL)
84                 return -1;
85         i = 0;
86         ssid = wpa_s->conf->ssid;
87         while (ssid) {
88                 if (!wpas_network_disabled(wpa_s, ssid)) {
89                         params.ssids[i].ssid = ssid->ssid;
90                         params.ssids[i].ssid_len = ssid->ssid_len;
91                         params.num_ssids++;
92                         os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
93                                   ssid->ssid_len);
94                         params.filter_ssids[i].ssid_len = ssid->ssid_len;
95                         params.num_filter_ssids++;
96                         i++;
97                         if (i == num_ssid)
98                                 break;
99                 }
100                 ssid = ssid->next;
101         }
102
103         if (wpa_s->conf->filter_rssi)
104                 params.filter_rssi = wpa_s->conf->filter_rssi;
105
106         ret = wpa_drv_sched_scan(wpa_s, &params, 10 * 1000);
107         os_free(params.filter_ssids);
108         if (ret == 0)
109                 wpa_s->pno = 1;
110         return ret;
111 }
112
113
114 static int pno_stop(struct wpa_supplicant *wpa_s)
115 {
116         if (wpa_s->pno) {
117                 wpa_s->pno = 0;
118                 return wpa_drv_stop_sched_scan(wpa_s);
119         }
120         return 0;
121 }
122
123
124 static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
125 {
126         char *pos;
127         u8 addr[ETH_ALEN], *filter = NULL, *n;
128         size_t count = 0;
129
130         pos = val;
131         while (pos) {
132                 if (*pos == '\0')
133                         break;
134                 if (hwaddr_aton(pos, addr)) {
135                         os_free(filter);
136                         return -1;
137                 }
138                 n = os_realloc_array(filter, count + 1, ETH_ALEN);
139                 if (n == NULL) {
140                         os_free(filter);
141                         return -1;
142                 }
143                 filter = n;
144                 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
145                 count++;
146
147                 pos = os_strchr(pos, ' ');
148                 if (pos)
149                         pos++;
150         }
151
152         wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
153         os_free(wpa_s->bssid_filter);
154         wpa_s->bssid_filter = filter;
155         wpa_s->bssid_filter_count = count;
156
157         return 0;
158 }
159
160
161 static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
162 {
163         char *pos;
164         u8 addr[ETH_ALEN], *bssid = NULL, *n;
165         struct wpa_ssid_value *ssid = NULL, *ns;
166         size_t count = 0, ssid_count = 0;
167         struct wpa_ssid *c;
168
169         /*
170          * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | “”
171          * SSID_SPEC ::= ssid <SSID_HEX>
172          * BSSID_SPEC ::= bssid <BSSID_HEX>
173          */
174
175         pos = val;
176         while (pos) {
177                 if (*pos == '\0')
178                         break;
179                 if (os_strncmp(pos, "bssid ", 6) == 0) {
180                         int res;
181                         pos += 6;
182                         res = hwaddr_aton2(pos, addr);
183                         if (res < 0) {
184                                 os_free(ssid);
185                                 os_free(bssid);
186                                 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
187                                            "BSSID value '%s'", pos);
188                                 return -1;
189                         }
190                         pos += res;
191                         n = os_realloc_array(bssid, count + 1, ETH_ALEN);
192                         if (n == NULL) {
193                                 os_free(ssid);
194                                 os_free(bssid);
195                                 return -1;
196                         }
197                         bssid = n;
198                         os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
199                         count++;
200                 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
201                         char *end;
202                         pos += 5;
203
204                         end = pos;
205                         while (*end) {
206                                 if (*end == '\0' || *end == ' ')
207                                         break;
208                                 end++;
209                         }
210
211                         ns = os_realloc_array(ssid, ssid_count + 1,
212                                               sizeof(struct wpa_ssid_value));
213                         if (ns == NULL) {
214                                 os_free(ssid);
215                                 os_free(bssid);
216                                 return -1;
217                         }
218                         ssid = ns;
219
220                         if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
221                             hexstr2bin(pos, ssid[ssid_count].ssid,
222                                        (end - pos) / 2) < 0) {
223                                 os_free(ssid);
224                                 os_free(bssid);
225                                 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
226                                            "SSID value '%s'", pos);
227                                 return -1;
228                         }
229                         ssid[ssid_count].ssid_len = (end - pos) / 2;
230                         wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
231                                           ssid[ssid_count].ssid,
232                                           ssid[ssid_count].ssid_len);
233                         ssid_count++;
234                         pos = end;
235                 } else {
236                         wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
237                                    "'%s'", pos);
238                         os_free(ssid);
239                         os_free(bssid);
240                         return -1;
241                 }
242
243                 pos = os_strchr(pos, ' ');
244                 if (pos)
245                         pos++;
246         }
247
248         wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
249         os_free(wpa_s->disallow_aps_bssid);
250         wpa_s->disallow_aps_bssid = bssid;
251         wpa_s->disallow_aps_bssid_count = count;
252
253         wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
254         os_free(wpa_s->disallow_aps_ssid);
255         wpa_s->disallow_aps_ssid = ssid;
256         wpa_s->disallow_aps_ssid_count = ssid_count;
257
258         if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
259                 return 0;
260
261         c = wpa_s->current_ssid;
262         if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
263                 return 0;
264
265         if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
266             !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
267                 return 0;
268
269         wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
270                    "because current AP was marked disallowed");
271
272 #ifdef CONFIG_SME
273         wpa_s->sme.prev_bssid_set = 0;
274 #endif /* CONFIG_SME */
275         wpa_s->reassociate = 1;
276         wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
277         wpa_supplicant_req_scan(wpa_s, 0, 0);
278
279         return 0;
280 }
281
282
283 static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
284                                          char *cmd)
285 {
286         char *value;
287         int ret = 0;
288
289         value = os_strchr(cmd, ' ');
290         if (value == NULL)
291                 return -1;
292         *value++ = '\0';
293
294         wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
295         if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
296                 eapol_sm_configure(wpa_s->eapol,
297                                    atoi(value), -1, -1, -1);
298         } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
299                 eapol_sm_configure(wpa_s->eapol,
300                                    -1, atoi(value), -1, -1);
301         } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
302                 eapol_sm_configure(wpa_s->eapol,
303                                    -1, -1, atoi(value), -1);
304         } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
305                 eapol_sm_configure(wpa_s->eapol,
306                                    -1, -1, -1, atoi(value));
307         } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
308                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
309                                      atoi(value)))
310                         ret = -1;
311         } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
312                    0) {
313                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
314                                      atoi(value)))
315                         ret = -1;
316         } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
317                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
318                         ret = -1;
319         } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
320                 wpa_s->wps_fragment_size = atoi(value);
321 #ifdef CONFIG_WPS_TESTING
322         } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
323                 long int val;
324                 val = strtol(value, NULL, 0);
325                 if (val < 0 || val > 0xff) {
326                         ret = -1;
327                         wpa_printf(MSG_DEBUG, "WPS: Invalid "
328                                    "wps_version_number %ld", val);
329                 } else {
330                         wps_version_number = val;
331                         wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
332                                    "version %u.%u",
333                                    (wps_version_number & 0xf0) >> 4,
334                                    wps_version_number & 0x0f);
335                 }
336         } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
337                 wps_testing_dummy_cred = atoi(value);
338                 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
339                            wps_testing_dummy_cred);
340 #endif /* CONFIG_WPS_TESTING */
341         } else if (os_strcasecmp(cmd, "ampdu") == 0) {
342                 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
343                         ret = -1;
344 #ifdef CONFIG_TDLS_TESTING
345         } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
346                 extern unsigned int tdls_testing;
347                 tdls_testing = strtol(value, NULL, 0);
348                 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
349 #endif /* CONFIG_TDLS_TESTING */
350 #ifdef CONFIG_TDLS
351         } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
352                 int disabled = atoi(value);
353                 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
354                 if (disabled) {
355                         if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
356                                 ret = -1;
357                 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
358                         ret = -1;
359                 wpa_tdls_enable(wpa_s->wpa, !disabled);
360 #endif /* CONFIG_TDLS */
361         } else if (os_strcasecmp(cmd, "pno") == 0) {
362                 if (atoi(value))
363                         ret = pno_start(wpa_s);
364                 else
365                         ret = pno_stop(wpa_s);
366         } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
367                 int disabled = atoi(value);
368                 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
369                         ret = -1;
370                 else if (disabled)
371                         wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
372         } else if (os_strcasecmp(cmd, "uapsd") == 0) {
373                 if (os_strcmp(value, "disable") == 0)
374                         wpa_s->set_sta_uapsd = 0;
375                 else {
376                         int be, bk, vi, vo;
377                         char *pos;
378                         /* format: BE,BK,VI,VO;max SP Length */
379                         be = atoi(value);
380                         pos = os_strchr(value, ',');
381                         if (pos == NULL)
382                                 return -1;
383                         pos++;
384                         bk = atoi(pos);
385                         pos = os_strchr(pos, ',');
386                         if (pos == NULL)
387                                 return -1;
388                         pos++;
389                         vi = atoi(pos);
390                         pos = os_strchr(pos, ',');
391                         if (pos == NULL)
392                                 return -1;
393                         pos++;
394                         vo = atoi(pos);
395                         /* ignore max SP Length for now */
396
397                         wpa_s->set_sta_uapsd = 1;
398                         wpa_s->sta_uapsd = 0;
399                         if (be)
400                                 wpa_s->sta_uapsd |= BIT(0);
401                         if (bk)
402                                 wpa_s->sta_uapsd |= BIT(1);
403                         if (vi)
404                                 wpa_s->sta_uapsd |= BIT(2);
405                         if (vo)
406                                 wpa_s->sta_uapsd |= BIT(3);
407                 }
408         } else if (os_strcasecmp(cmd, "ps") == 0) {
409                 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
410 #ifdef CONFIG_WIFI_DISPLAY
411         } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
412                 wifi_display_enable(wpa_s->global, !!atoi(value));
413 #endif /* CONFIG_WIFI_DISPLAY */
414         } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
415                 ret = set_bssid_filter(wpa_s, value);
416         } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
417                 ret = set_disallow_aps(wpa_s, value);
418         } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
419                 wpa_s->no_keep_alive = !!atoi(value);
420         } else {
421                 value[-1] = '=';
422                 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
423                 if (ret == 0)
424                         wpa_supplicant_update_config(wpa_s);
425         }
426
427         return ret;
428 }
429
430
431 static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
432                                          char *cmd, char *buf, size_t buflen)
433 {
434         int res = -1;
435
436         wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
437
438         if (os_strcmp(cmd, "version") == 0) {
439                 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
440         } else if (os_strcasecmp(cmd, "country") == 0) {
441                 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
442                         res = os_snprintf(buf, buflen, "%c%c",
443                                           wpa_s->conf->country[0],
444                                           wpa_s->conf->country[1]);
445 #ifdef CONFIG_WIFI_DISPLAY
446         } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
447                 res = os_snprintf(buf, buflen, "%d",
448                                   wpa_s->global->wifi_display);
449                 if (res < 0 || (unsigned int) res >= buflen)
450                         return -1;
451                 return res;
452 #endif /* CONFIG_WIFI_DISPLAY */
453         }
454
455         if (res < 0 || (unsigned int) res >= buflen)
456                 return -1;
457         return res;
458 }
459
460
461 #ifdef IEEE8021X_EAPOL
462 static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
463                                              char *addr)
464 {
465         u8 bssid[ETH_ALEN];
466         struct wpa_ssid *ssid = wpa_s->current_ssid;
467
468         if (hwaddr_aton(addr, bssid)) {
469                 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
470                            "'%s'", addr);
471                 return -1;
472         }
473
474         wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
475         rsn_preauth_deinit(wpa_s->wpa);
476         if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
477                 return -1;
478
479         return 0;
480 }
481 #endif /* IEEE8021X_EAPOL */
482
483
484 #ifdef CONFIG_PEERKEY
485 /* MLME-STKSTART.request(peer) */
486 static int wpa_supplicant_ctrl_iface_stkstart(
487         struct wpa_supplicant *wpa_s, char *addr)
488 {
489         u8 peer[ETH_ALEN];
490
491         if (hwaddr_aton(addr, peer)) {
492                 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
493                            "address '%s'", addr);
494                 return -1;
495         }
496
497         wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
498                    MAC2STR(peer));
499
500         return wpa_sm_stkstart(wpa_s->wpa, peer);
501 }
502 #endif /* CONFIG_PEERKEY */
503
504
505 #ifdef CONFIG_TDLS
506
507 static int wpa_supplicant_ctrl_iface_tdls_discover(
508         struct wpa_supplicant *wpa_s, char *addr)
509 {
510         u8 peer[ETH_ALEN];
511         int ret;
512
513         if (hwaddr_aton(addr, peer)) {
514                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
515                            "address '%s'", addr);
516                 return -1;
517         }
518
519         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
520                    MAC2STR(peer));
521
522         if (wpa_tdls_is_external_setup(wpa_s->wpa))
523                 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
524         else
525                 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
526
527         return ret;
528 }
529
530
531 static int wpa_supplicant_ctrl_iface_tdls_setup(
532         struct wpa_supplicant *wpa_s, char *addr)
533 {
534         u8 peer[ETH_ALEN];
535         int ret;
536
537         if (hwaddr_aton(addr, peer)) {
538                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
539                            "address '%s'", addr);
540                 return -1;
541         }
542
543         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
544                    MAC2STR(peer));
545
546         ret = wpa_tdls_reneg(wpa_s->wpa, peer);
547         if (ret) {
548                 if (wpa_tdls_is_external_setup(wpa_s->wpa))
549                         ret = wpa_tdls_start(wpa_s->wpa, peer);
550                 else
551                         ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
552         }
553
554         return ret;
555 }
556
557
558 static int wpa_supplicant_ctrl_iface_tdls_teardown(
559         struct wpa_supplicant *wpa_s, char *addr)
560 {
561         u8 peer[ETH_ALEN];
562
563         if (hwaddr_aton(addr, peer)) {
564                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
565                            "address '%s'", addr);
566                 return -1;
567         }
568
569         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
570                    MAC2STR(peer));
571
572         return wpa_tdls_teardown_link(wpa_s->wpa, peer,
573                                       WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
574 }
575
576 #endif /* CONFIG_TDLS */
577
578
579 #ifdef CONFIG_IEEE80211R
580 static int wpa_supplicant_ctrl_iface_ft_ds(
581         struct wpa_supplicant *wpa_s, char *addr)
582 {
583         u8 target_ap[ETH_ALEN];
584         struct wpa_bss *bss;
585         const u8 *mdie;
586
587         if (hwaddr_aton(addr, target_ap)) {
588                 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
589                            "address '%s'", addr);
590                 return -1;
591         }
592
593         wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
594
595         bss = wpa_bss_get_bssid(wpa_s, target_ap);
596         if (bss)
597                 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
598         else
599                 mdie = NULL;
600
601         return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
602 }
603 #endif /* CONFIG_IEEE80211R */
604
605
606 #ifdef CONFIG_WPS
607 static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
608                                              char *cmd)
609 {
610         u8 bssid[ETH_ALEN], *_bssid = bssid;
611 #ifdef CONFIG_P2P
612         u8 p2p_dev_addr[ETH_ALEN];
613 #endif /* CONFIG_P2P */
614 #ifdef CONFIG_AP
615         u8 *_p2p_dev_addr = NULL;
616 #endif /* CONFIG_AP */
617
618         if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
619                 _bssid = NULL;
620 #ifdef CONFIG_P2P
621         } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
622                 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
623                         wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
624                                    "P2P Device Address '%s'",
625                                    cmd + 13);
626                         return -1;
627                 }
628                 _p2p_dev_addr = p2p_dev_addr;
629 #endif /* CONFIG_P2P */
630         } else if (hwaddr_aton(cmd, bssid)) {
631                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
632                            cmd);
633                 return -1;
634         }
635
636 #ifdef CONFIG_AP
637         if (wpa_s->ap_iface)
638                 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
639 #endif /* CONFIG_AP */
640
641         return wpas_wps_start_pbc(wpa_s, _bssid, 0);
642 }
643
644
645 static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
646                                              char *cmd, char *buf,
647                                              size_t buflen)
648 {
649         u8 bssid[ETH_ALEN], *_bssid = bssid;
650         char *pin;
651         int ret;
652
653         pin = os_strchr(cmd, ' ');
654         if (pin)
655                 *pin++ = '\0';
656
657         if (os_strcmp(cmd, "any") == 0)
658                 _bssid = NULL;
659         else if (os_strcmp(cmd, "get") == 0) {
660                 ret = wps_generate_pin();
661                 goto done;
662         } else if (hwaddr_aton(cmd, bssid)) {
663                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
664                            cmd);
665                 return -1;
666         }
667
668 #ifdef CONFIG_AP
669         if (wpa_s->ap_iface) {
670                 int timeout = 0;
671                 char *pos;
672
673                 if (pin) {
674                         pos = os_strchr(pin, ' ');
675                         if (pos) {
676                                 *pos++ = '\0';
677                                 timeout = atoi(pos);
678                         }
679                 }
680
681                 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
682                                                  buf, buflen, timeout);
683         }
684 #endif /* CONFIG_AP */
685
686         if (pin) {
687                 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
688                                          DEV_PW_DEFAULT);
689                 if (ret < 0)
690                         return -1;
691                 ret = os_snprintf(buf, buflen, "%s", pin);
692                 if (ret < 0 || (size_t) ret >= buflen)
693                         return -1;
694                 return ret;
695         }
696
697         ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
698         if (ret < 0)
699                 return -1;
700
701 done:
702         /* Return the generated PIN */
703         ret = os_snprintf(buf, buflen, "%08d", ret);
704         if (ret < 0 || (size_t) ret >= buflen)
705                 return -1;
706         return ret;
707 }
708
709
710 static int wpa_supplicant_ctrl_iface_wps_check_pin(
711         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
712 {
713         char pin[9];
714         size_t len;
715         char *pos;
716         int ret;
717
718         wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
719                               (u8 *) cmd, os_strlen(cmd));
720         for (pos = cmd, len = 0; *pos != '\0'; pos++) {
721                 if (*pos < '0' || *pos > '9')
722                         continue;
723                 pin[len++] = *pos;
724                 if (len == 9) {
725                         wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
726                         return -1;
727                 }
728         }
729         if (len != 4 && len != 8) {
730                 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
731                 return -1;
732         }
733         pin[len] = '\0';
734
735         if (len == 8) {
736                 unsigned int pin_val;
737                 pin_val = atoi(pin);
738                 if (!wps_pin_valid(pin_val)) {
739                         wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
740                         ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
741                         if (ret < 0 || (size_t) ret >= buflen)
742                                 return -1;
743                         return ret;
744                 }
745         }
746
747         ret = os_snprintf(buf, buflen, "%s", pin);
748         if (ret < 0 || (size_t) ret >= buflen)
749                 return -1;
750
751         return ret;
752 }
753
754
755 #ifdef CONFIG_WPS_NFC
756
757 static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
758                                              char *cmd)
759 {
760         u8 bssid[ETH_ALEN], *_bssid = bssid;
761
762         if (cmd == NULL || cmd[0] == '\0')
763                 _bssid = NULL;
764         else if (hwaddr_aton(cmd, bssid))
765                 return -1;
766
767         return wpas_wps_start_nfc(wpa_s, _bssid);
768 }
769
770
771 static int wpa_supplicant_ctrl_iface_wps_nfc_token(
772         struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
773 {
774         int ndef;
775         struct wpabuf *buf;
776         int res;
777
778         if (os_strcmp(cmd, "WPS") == 0)
779                 ndef = 0;
780         else if (os_strcmp(cmd, "NDEF") == 0)
781                 ndef = 1;
782         else
783                 return -1;
784
785         buf = wpas_wps_nfc_token(wpa_s, ndef);
786         if (buf == NULL)
787                 return -1;
788
789         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
790                                          wpabuf_len(buf));
791         reply[res++] = '\n';
792         reply[res] = '\0';
793
794         wpabuf_free(buf);
795
796         return res;
797 }
798
799
800 static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
801         struct wpa_supplicant *wpa_s, char *pos)
802 {
803         size_t len;
804         struct wpabuf *buf;
805         int ret;
806
807         len = os_strlen(pos);
808         if (len & 0x01)
809                 return -1;
810         len /= 2;
811
812         buf = wpabuf_alloc(len);
813         if (buf == NULL)
814                 return -1;
815         if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
816                 wpabuf_free(buf);
817                 return -1;
818         }
819
820         ret = wpas_wps_nfc_tag_read(wpa_s, buf);
821         wpabuf_free(buf);
822
823         return ret;
824 }
825
826
827 static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
828                                               char *reply, size_t max_len)
829 {
830         struct wpabuf *buf;
831         int res;
832
833         buf = wpas_wps_nfc_handover_req(wpa_s);
834         if (buf == NULL)
835                 return -1;
836
837         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
838                                          wpabuf_len(buf));
839         reply[res++] = '\n';
840         reply[res] = '\0';
841
842         wpabuf_free(buf);
843
844         return res;
845 }
846
847
848 static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
849                                           char *cmd, char *reply,
850                                           size_t max_len)
851 {
852         char *pos;
853
854         pos = os_strchr(cmd, ' ');
855         if (pos == NULL)
856                 return -1;
857         *pos++ = '\0';
858
859         if (os_strcmp(cmd, "NDEF") != 0)
860                 return -1;
861
862         if (os_strcmp(pos, "WPS") == 0) {
863                 return wpas_ctrl_nfc_get_handover_req_wps(wpa_s, reply,
864                                                           max_len);
865         }
866
867         return -1;
868 }
869
870
871 static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
872                                               char *reply, size_t max_len)
873 {
874         struct wpabuf *buf;
875         int res;
876
877         buf = wpas_wps_nfc_handover_sel(wpa_s);
878         if (buf == NULL)
879                 return -1;
880
881         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
882                                          wpabuf_len(buf));
883         reply[res++] = '\n';
884         reply[res] = '\0';
885
886         wpabuf_free(buf);
887
888         return res;
889 }
890
891
892 static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
893                                           char *cmd, char *reply,
894                                           size_t max_len)
895 {
896         char *pos;
897
898         pos = os_strchr(cmd, ' ');
899         if (pos == NULL)
900                 return -1;
901         *pos++ = '\0';
902
903         if (os_strcmp(cmd, "NDEF") != 0)
904                 return -1;
905
906         if (os_strcmp(pos, "WPS") == 0) {
907                 return wpas_ctrl_nfc_get_handover_sel_wps(wpa_s, reply,
908                                                           max_len);
909         }
910
911         return -1;
912 }
913
914
915 static int wpas_ctrl_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
916                                          char *cmd, char *reply,
917                                          size_t max_len)
918 {
919         size_t len;
920         struct wpabuf *buf;
921         int ret;
922
923         len = os_strlen(cmd);
924         if (len & 0x01)
925                 return -1;
926         len /= 2;
927
928         buf = wpabuf_alloc(len);
929         if (buf == NULL)
930                 return -1;
931         if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
932                 wpabuf_free(buf);
933                 return -1;
934         }
935
936         ret = wpas_wps_nfc_rx_handover_req(wpa_s, buf);
937         wpabuf_free(buf);
938
939         return ret;
940 }
941
942
943 static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
944                                          char *cmd)
945 {
946         size_t len;
947         struct wpabuf *buf;
948         int ret;
949
950         len = os_strlen(cmd);
951         if (len & 0x01)
952                 return -1;
953         len /= 2;
954
955         buf = wpabuf_alloc(len);
956         if (buf == NULL)
957                 return -1;
958         if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
959                 wpabuf_free(buf);
960                 return -1;
961         }
962
963         ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
964         wpabuf_free(buf);
965
966         return ret;
967 }
968
969 #endif /* CONFIG_WPS_NFC */
970
971
972 static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
973                                              char *cmd)
974 {
975         u8 bssid[ETH_ALEN];
976         char *pin;
977         char *new_ssid;
978         char *new_auth;
979         char *new_encr;
980         char *new_key;
981         struct wps_new_ap_settings ap;
982
983         pin = os_strchr(cmd, ' ');
984         if (pin == NULL)
985                 return -1;
986         *pin++ = '\0';
987
988         if (hwaddr_aton(cmd, bssid)) {
989                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
990                            cmd);
991                 return -1;
992         }
993
994         new_ssid = os_strchr(pin, ' ');
995         if (new_ssid == NULL)
996                 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
997         *new_ssid++ = '\0';
998
999         new_auth = os_strchr(new_ssid, ' ');
1000         if (new_auth == NULL)
1001                 return -1;
1002         *new_auth++ = '\0';
1003
1004         new_encr = os_strchr(new_auth, ' ');
1005         if (new_encr == NULL)
1006                 return -1;
1007         *new_encr++ = '\0';
1008
1009         new_key = os_strchr(new_encr, ' ');
1010         if (new_key == NULL)
1011                 return -1;
1012         *new_key++ = '\0';
1013
1014         os_memset(&ap, 0, sizeof(ap));
1015         ap.ssid_hex = new_ssid;
1016         ap.auth = new_auth;
1017         ap.encr = new_encr;
1018         ap.key_hex = new_key;
1019         return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1020 }
1021
1022
1023 #ifdef CONFIG_AP
1024 static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1025                                                 char *cmd, char *buf,
1026                                                 size_t buflen)
1027 {
1028         int timeout = 300;
1029         char *pos;
1030         const char *pin_txt;
1031
1032         if (!wpa_s->ap_iface)
1033                 return -1;
1034
1035         pos = os_strchr(cmd, ' ');
1036         if (pos)
1037                 *pos++ = '\0';
1038
1039         if (os_strcmp(cmd, "disable") == 0) {
1040                 wpas_wps_ap_pin_disable(wpa_s);
1041                 return os_snprintf(buf, buflen, "OK\n");
1042         }
1043
1044         if (os_strcmp(cmd, "random") == 0) {
1045                 if (pos)
1046                         timeout = atoi(pos);
1047                 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1048                 if (pin_txt == NULL)
1049                         return -1;
1050                 return os_snprintf(buf, buflen, "%s", pin_txt);
1051         }
1052
1053         if (os_strcmp(cmd, "get") == 0) {
1054                 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1055                 if (pin_txt == NULL)
1056                         return -1;
1057                 return os_snprintf(buf, buflen, "%s", pin_txt);
1058         }
1059
1060         if (os_strcmp(cmd, "set") == 0) {
1061                 char *pin;
1062                 if (pos == NULL)
1063                         return -1;
1064                 pin = pos;
1065                 pos = os_strchr(pos, ' ');
1066                 if (pos) {
1067                         *pos++ = '\0';
1068                         timeout = atoi(pos);
1069                 }
1070                 if (os_strlen(pin) > buflen)
1071                         return -1;
1072                 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1073                         return -1;
1074                 return os_snprintf(buf, buflen, "%s", pin);
1075         }
1076
1077         return -1;
1078 }
1079 #endif /* CONFIG_AP */
1080
1081
1082 #ifdef CONFIG_WPS_ER
1083 static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1084                                                 char *cmd)
1085 {
1086         char *uuid = cmd, *pin, *pos;
1087         u8 addr_buf[ETH_ALEN], *addr = NULL;
1088         pin = os_strchr(uuid, ' ');
1089         if (pin == NULL)
1090                 return -1;
1091         *pin++ = '\0';
1092         pos = os_strchr(pin, ' ');
1093         if (pos) {
1094                 *pos++ = '\0';
1095                 if (hwaddr_aton(pos, addr_buf) == 0)
1096                         addr = addr_buf;
1097         }
1098         return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1099 }
1100
1101
1102 static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1103                                                   char *cmd)
1104 {
1105         char *uuid = cmd, *pin;
1106         pin = os_strchr(uuid, ' ');
1107         if (pin == NULL)
1108                 return -1;
1109         *pin++ = '\0';
1110         return wpas_wps_er_learn(wpa_s, uuid, pin);
1111 }
1112
1113
1114 static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1115         struct wpa_supplicant *wpa_s, char *cmd)
1116 {
1117         char *uuid = cmd, *id;
1118         id = os_strchr(uuid, ' ');
1119         if (id == NULL)
1120                 return -1;
1121         *id++ = '\0';
1122         return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1123 }
1124
1125
1126 static int wpa_supplicant_ctrl_iface_wps_er_config(
1127         struct wpa_supplicant *wpa_s, char *cmd)
1128 {
1129         char *pin;
1130         char *new_ssid;
1131         char *new_auth;
1132         char *new_encr;
1133         char *new_key;
1134         struct wps_new_ap_settings ap;
1135
1136         pin = os_strchr(cmd, ' ');
1137         if (pin == NULL)
1138                 return -1;
1139         *pin++ = '\0';
1140
1141         new_ssid = os_strchr(pin, ' ');
1142         if (new_ssid == NULL)
1143                 return -1;
1144         *new_ssid++ = '\0';
1145
1146         new_auth = os_strchr(new_ssid, ' ');
1147         if (new_auth == NULL)
1148                 return -1;
1149         *new_auth++ = '\0';
1150
1151         new_encr = os_strchr(new_auth, ' ');
1152         if (new_encr == NULL)
1153                 return -1;
1154         *new_encr++ = '\0';
1155
1156         new_key = os_strchr(new_encr, ' ');
1157         if (new_key == NULL)
1158                 return -1;
1159         *new_key++ = '\0';
1160
1161         os_memset(&ap, 0, sizeof(ap));
1162         ap.ssid_hex = new_ssid;
1163         ap.auth = new_auth;
1164         ap.encr = new_encr;
1165         ap.key_hex = new_key;
1166         return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1167 }
1168
1169
1170 #ifdef CONFIG_WPS_NFC
1171 static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1172         struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1173 {
1174         int ndef;
1175         struct wpabuf *buf;
1176         int res;
1177         char *uuid;
1178
1179         uuid = os_strchr(cmd, ' ');
1180         if (uuid == NULL)
1181                 return -1;
1182         *uuid++ = '\0';
1183
1184         if (os_strcmp(cmd, "WPS") == 0)
1185                 ndef = 0;
1186         else if (os_strcmp(cmd, "NDEF") == 0)
1187                 ndef = 1;
1188         else
1189                 return -1;
1190
1191         buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1192         if (buf == NULL)
1193                 return -1;
1194
1195         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1196                                          wpabuf_len(buf));
1197         reply[res++] = '\n';
1198         reply[res] = '\0';
1199
1200         wpabuf_free(buf);
1201
1202         return res;
1203 }
1204 #endif /* CONFIG_WPS_NFC */
1205 #endif /* CONFIG_WPS_ER */
1206
1207 #endif /* CONFIG_WPS */
1208
1209
1210 #ifdef CONFIG_IBSS_RSN
1211 static int wpa_supplicant_ctrl_iface_ibss_rsn(
1212         struct wpa_supplicant *wpa_s, char *addr)
1213 {
1214         u8 peer[ETH_ALEN];
1215
1216         if (hwaddr_aton(addr, peer)) {
1217                 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1218                            "address '%s'", addr);
1219                 return -1;
1220         }
1221
1222         wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1223                    MAC2STR(peer));
1224
1225         return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1226 }
1227 #endif /* CONFIG_IBSS_RSN */
1228
1229
1230 static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1231                                               char *rsp)
1232 {
1233 #ifdef IEEE8021X_EAPOL
1234         char *pos, *id_pos;
1235         int id;
1236         struct wpa_ssid *ssid;
1237
1238         pos = os_strchr(rsp, '-');
1239         if (pos == NULL)
1240                 return -1;
1241         *pos++ = '\0';
1242         id_pos = pos;
1243         pos = os_strchr(pos, ':');
1244         if (pos == NULL)
1245                 return -1;
1246         *pos++ = '\0';
1247         id = atoi(id_pos);
1248         wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1249         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1250                               (u8 *) pos, os_strlen(pos));
1251
1252         ssid = wpa_config_get_network(wpa_s->conf, id);
1253         if (ssid == NULL) {
1254                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1255                            "to update", id);
1256                 return -1;
1257         }
1258
1259         return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1260                                                          pos);
1261 #else /* IEEE8021X_EAPOL */
1262         wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1263         return -1;
1264 #endif /* IEEE8021X_EAPOL */
1265 }
1266
1267
1268 static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1269                                             const char *params,
1270                                             char *buf, size_t buflen)
1271 {
1272         char *pos, *end, tmp[30];
1273         int res, verbose, wps, ret;
1274
1275         verbose = os_strcmp(params, "-VERBOSE") == 0;
1276         wps = os_strcmp(params, "-WPS") == 0;
1277         pos = buf;
1278         end = buf + buflen;
1279         if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1280                 struct wpa_ssid *ssid = wpa_s->current_ssid;
1281                 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1282                                   MAC2STR(wpa_s->bssid));
1283                 if (ret < 0 || ret >= end - pos)
1284                         return pos - buf;
1285                 pos += ret;
1286                 if (ssid) {
1287                         u8 *_ssid = ssid->ssid;
1288                         size_t ssid_len = ssid->ssid_len;
1289                         u8 ssid_buf[MAX_SSID_LEN];
1290                         if (ssid_len == 0) {
1291                                 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1292                                 if (_res < 0)
1293                                         ssid_len = 0;
1294                                 else
1295                                         ssid_len = _res;
1296                                 _ssid = ssid_buf;
1297                         }
1298                         ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1299                                           wpa_ssid_txt(_ssid, ssid_len),
1300                                           ssid->id);
1301                         if (ret < 0 || ret >= end - pos)
1302                                 return pos - buf;
1303                         pos += ret;
1304
1305                         if (wps && ssid->passphrase &&
1306                             wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1307                             (ssid->mode == WPAS_MODE_AP ||
1308                              ssid->mode == WPAS_MODE_P2P_GO)) {
1309                                 ret = os_snprintf(pos, end - pos,
1310                                                   "passphrase=%s\n",
1311                                                   ssid->passphrase);
1312                                 if (ret < 0 || ret >= end - pos)
1313                                         return pos - buf;
1314                                 pos += ret;
1315                         }
1316                         if (ssid->id_str) {
1317                                 ret = os_snprintf(pos, end - pos,
1318                                                   "id_str=%s\n",
1319                                                   ssid->id_str);
1320                                 if (ret < 0 || ret >= end - pos)
1321                                         return pos - buf;
1322                                 pos += ret;
1323                         }
1324
1325                         switch (ssid->mode) {
1326                         case WPAS_MODE_INFRA:
1327                                 ret = os_snprintf(pos, end - pos,
1328                                                   "mode=station\n");
1329                                 break;
1330                         case WPAS_MODE_IBSS:
1331                                 ret = os_snprintf(pos, end - pos,
1332                                                   "mode=IBSS\n");
1333                                 break;
1334                         case WPAS_MODE_AP:
1335                                 ret = os_snprintf(pos, end - pos,
1336                                                   "mode=AP\n");
1337                                 break;
1338                         case WPAS_MODE_P2P_GO:
1339                                 ret = os_snprintf(pos, end - pos,
1340                                                   "mode=P2P GO\n");
1341                                 break;
1342                         case WPAS_MODE_P2P_GROUP_FORMATION:
1343                                 ret = os_snprintf(pos, end - pos,
1344                                                   "mode=P2P GO - group "
1345                                                   "formation\n");
1346                                 break;
1347                         default:
1348                                 ret = 0;
1349                                 break;
1350                         }
1351                         if (ret < 0 || ret >= end - pos)
1352                                 return pos - buf;
1353                         pos += ret;
1354                 }
1355
1356 #ifdef CONFIG_AP
1357                 if (wpa_s->ap_iface) {
1358                         pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1359                                                             end - pos,
1360                                                             verbose);
1361                 } else
1362 #endif /* CONFIG_AP */
1363                 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1364         }
1365         ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1366                           wpa_supplicant_state_txt(wpa_s->wpa_state));
1367         if (ret < 0 || ret >= end - pos)
1368                 return pos - buf;
1369         pos += ret;
1370
1371         if (wpa_s->l2 &&
1372             l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1373                 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
1374                 if (ret < 0 || ret >= end - pos)
1375                         return pos - buf;
1376                 pos += ret;
1377         }
1378
1379 #ifdef CONFIG_P2P
1380         if (wpa_s->global->p2p) {
1381                 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1382                                   "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
1383                 if (ret < 0 || ret >= end - pos)
1384                         return pos - buf;
1385                 pos += ret;
1386         }
1387 #endif /* CONFIG_P2P */
1388
1389         ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1390                           MAC2STR(wpa_s->own_addr));
1391         if (ret < 0 || ret >= end - pos)
1392                 return pos - buf;
1393         pos += ret;
1394
1395 #ifdef CONFIG_HS20
1396         if (wpa_s->current_bss &&
1397             wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
1398             wpa_s->wpa_proto == WPA_PROTO_RSN &&
1399             wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1400                 ret = os_snprintf(pos, end - pos, "hs20=1\n");
1401                 if (ret < 0 || ret >= end - pos)
1402                         return pos - buf;
1403                 pos += ret;
1404         }
1405
1406         if (wpa_s->current_ssid) {
1407                 struct wpa_cred *cred;
1408                 char *type;
1409
1410                 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1411                         if (wpa_s->current_ssid->parent_cred != cred)
1412                                 continue;
1413                         if (!cred->domain)
1414                                 continue;
1415
1416                         ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
1417                                           cred->domain);
1418                         if (ret < 0 || ret >= end - pos)
1419                                 return pos - buf;
1420                         pos += ret;
1421
1422                         if (wpa_s->current_bss == NULL ||
1423                             wpa_s->current_bss->anqp == NULL)
1424                                 res = -1;
1425                         else
1426                                 res = interworking_home_sp_cred(
1427                                         wpa_s, cred,
1428                                         wpa_s->current_bss->anqp->domain_name);
1429                         if (res > 0)
1430                                 type = "home";
1431                         else if (res == 0)
1432                                 type = "roaming";
1433                         else
1434                                 type = "unknown";
1435
1436                         ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
1437                         if (ret < 0 || ret >= end - pos)
1438                                 return pos - buf;
1439                         pos += ret;
1440
1441                         break;
1442                 }
1443         }
1444 #endif /* CONFIG_HS20 */
1445
1446         if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1447             wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1448                 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1449                                           verbose);
1450                 if (res >= 0)
1451                         pos += res;
1452         }
1453
1454         res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1455         if (res >= 0)
1456                 pos += res;
1457
1458         return pos - buf;
1459 }
1460
1461
1462 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1463                                            char *cmd)
1464 {
1465         char *pos;
1466         int id;
1467         struct wpa_ssid *ssid;
1468         u8 bssid[ETH_ALEN];
1469
1470         /* cmd: "<network id> <BSSID>" */
1471         pos = os_strchr(cmd, ' ');
1472         if (pos == NULL)
1473                 return -1;
1474         *pos++ = '\0';
1475         id = atoi(cmd);
1476         wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1477         if (hwaddr_aton(pos, bssid)) {
1478                 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1479                 return -1;
1480         }
1481
1482         ssid = wpa_config_get_network(wpa_s->conf, id);
1483         if (ssid == NULL) {
1484                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1485                            "to update", id);
1486                 return -1;
1487         }
1488
1489         os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1490         ssid->bssid_set = !is_zero_ether_addr(bssid);
1491
1492         return 0;
1493 }
1494
1495
1496 static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
1497                                                char *cmd, char *buf,
1498                                                size_t buflen)
1499 {
1500         u8 bssid[ETH_ALEN];
1501         struct wpa_blacklist *e;
1502         char *pos, *end;
1503         int ret;
1504
1505         /* cmd: "BLACKLIST [<BSSID>]" */
1506         if (*cmd == '\0') {
1507                 pos = buf;
1508                 end = buf + buflen;
1509                 e = wpa_s->blacklist;
1510                 while (e) {
1511                         ret = os_snprintf(pos, end - pos, MACSTR "\n",
1512                                           MAC2STR(e->bssid));
1513                         if (ret < 0 || ret >= end - pos)
1514                                 return pos - buf;
1515                         pos += ret;
1516                         e = e->next;
1517                 }
1518                 return pos - buf;
1519         }
1520
1521         cmd++;
1522         if (os_strncmp(cmd, "clear", 5) == 0) {
1523                 wpa_blacklist_clear(wpa_s);
1524                 os_memcpy(buf, "OK\n", 3);
1525                 return 3;
1526         }
1527
1528         wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1529         if (hwaddr_aton(cmd, bssid)) {
1530                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
1531                 return -1;
1532         }
1533
1534         /*
1535          * Add the BSSID twice, so its count will be 2, causing it to be
1536          * skipped when processing scan results.
1537          */
1538         ret = wpa_blacklist_add(wpa_s, bssid);
1539         if (ret != 0)
1540                 return -1;
1541         ret = wpa_blacklist_add(wpa_s, bssid);
1542         if (ret != 0)
1543                 return -1;
1544         os_memcpy(buf, "OK\n", 3);
1545         return 3;
1546 }
1547
1548
1549 extern int wpa_debug_level;
1550 extern int wpa_debug_timestamp;
1551
1552 static const char * debug_level_str(int level)
1553 {
1554         switch (level) {
1555         case MSG_EXCESSIVE:
1556                 return "EXCESSIVE";
1557         case MSG_MSGDUMP:
1558                 return "MSGDUMP";
1559         case MSG_DEBUG:
1560                 return "DEBUG";
1561         case MSG_INFO:
1562                 return "INFO";
1563         case MSG_WARNING:
1564                 return "WARNING";
1565         case MSG_ERROR:
1566                 return "ERROR";
1567         default:
1568                 return "?";
1569         }
1570 }
1571
1572
1573 static int str_to_debug_level(const char *s)
1574 {
1575         if (os_strcasecmp(s, "EXCESSIVE") == 0)
1576                 return MSG_EXCESSIVE;
1577         if (os_strcasecmp(s, "MSGDUMP") == 0)
1578                 return MSG_MSGDUMP;
1579         if (os_strcasecmp(s, "DEBUG") == 0)
1580                 return MSG_DEBUG;
1581         if (os_strcasecmp(s, "INFO") == 0)
1582                 return MSG_INFO;
1583         if (os_strcasecmp(s, "WARNING") == 0)
1584                 return MSG_WARNING;
1585         if (os_strcasecmp(s, "ERROR") == 0)
1586                 return MSG_ERROR;
1587         return -1;
1588 }
1589
1590
1591 static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1592                                                char *cmd, char *buf,
1593                                                size_t buflen)
1594 {
1595         char *pos, *end, *stamp;
1596         int ret;
1597
1598         if (cmd == NULL) {
1599                 return -1;
1600         }
1601
1602         /* cmd: "LOG_LEVEL [<level>]" */
1603         if (*cmd == '\0') {
1604                 pos = buf;
1605                 end = buf + buflen;
1606                 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1607                                   "Timestamp: %d\n",
1608                                   debug_level_str(wpa_debug_level),
1609                                   wpa_debug_timestamp);
1610                 if (ret < 0 || ret >= end - pos)
1611                         ret = 0;
1612
1613                 return ret;
1614         }
1615
1616         while (*cmd == ' ')
1617                 cmd++;
1618
1619         stamp = os_strchr(cmd, ' ');
1620         if (stamp) {
1621                 *stamp++ = '\0';
1622                 while (*stamp == ' ') {
1623                         stamp++;
1624                 }
1625         }
1626
1627         if (cmd && os_strlen(cmd)) {
1628                 int level = str_to_debug_level(cmd);
1629                 if (level < 0)
1630                         return -1;
1631                 wpa_debug_level = level;
1632         }
1633
1634         if (stamp && os_strlen(stamp))
1635                 wpa_debug_timestamp = atoi(stamp);
1636
1637         os_memcpy(buf, "OK\n", 3);
1638         return 3;
1639 }
1640
1641
1642 static int wpa_supplicant_ctrl_iface_list_networks(
1643         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1644 {
1645         char *pos, *end;
1646         struct wpa_ssid *ssid;
1647         int ret;
1648
1649         pos = buf;
1650         end = buf + buflen;
1651         ret = os_snprintf(pos, end - pos,
1652                           "network id / ssid / bssid / flags\n");
1653         if (ret < 0 || ret >= end - pos)
1654                 return pos - buf;
1655         pos += ret;
1656
1657         ssid = wpa_s->conf->ssid;
1658         while (ssid) {
1659                 ret = os_snprintf(pos, end - pos, "%d\t%s",
1660                                   ssid->id,
1661                                   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1662                 if (ret < 0 || ret >= end - pos)
1663                         return pos - buf;
1664                 pos += ret;
1665                 if (ssid->bssid_set) {
1666                         ret = os_snprintf(pos, end - pos, "\t" MACSTR,
1667                                           MAC2STR(ssid->bssid));
1668                 } else {
1669                         ret = os_snprintf(pos, end - pos, "\tany");
1670                 }
1671                 if (ret < 0 || ret >= end - pos)
1672                         return pos - buf;
1673                 pos += ret;
1674                 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
1675                                   ssid == wpa_s->current_ssid ?
1676                                   "[CURRENT]" : "",
1677                                   ssid->disabled ? "[DISABLED]" : "",
1678                                   ssid->disabled_until.sec ?
1679                                   "[TEMP-DISABLED]" : "",
1680                                   ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
1681                                   "");
1682                 if (ret < 0 || ret >= end - pos)
1683                         return pos - buf;
1684                 pos += ret;
1685                 ret = os_snprintf(pos, end - pos, "\n");
1686                 if (ret < 0 || ret >= end - pos)
1687                         return pos - buf;
1688                 pos += ret;
1689
1690                 ssid = ssid->next;
1691         }
1692
1693         return pos - buf;
1694 }
1695
1696
1697 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
1698 {
1699         int first = 1, ret;
1700         ret = os_snprintf(pos, end - pos, "-");
1701         if (ret < 0 || ret >= end - pos)
1702                 return pos;
1703         pos += ret;
1704         if (cipher & WPA_CIPHER_NONE) {
1705                 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
1706                 if (ret < 0 || ret >= end - pos)
1707                         return pos;
1708                 pos += ret;
1709                 first = 0;
1710         }
1711         if (cipher & WPA_CIPHER_WEP40) {
1712                 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
1713                 if (ret < 0 || ret >= end - pos)
1714                         return pos;
1715                 pos += ret;
1716                 first = 0;
1717         }
1718         if (cipher & WPA_CIPHER_WEP104) {
1719                 ret = os_snprintf(pos, end - pos, "%sWEP104",
1720                                   first ? "" : "+");
1721                 if (ret < 0 || ret >= end - pos)
1722                         return pos;
1723                 pos += ret;
1724                 first = 0;
1725         }
1726         if (cipher & WPA_CIPHER_TKIP) {
1727                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
1728                 if (ret < 0 || ret >= end - pos)
1729                         return pos;
1730                 pos += ret;
1731                 first = 0;
1732         }
1733         if (cipher & WPA_CIPHER_CCMP) {
1734                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
1735                 if (ret < 0 || ret >= end - pos)
1736                         return pos;
1737                 pos += ret;
1738                 first = 0;
1739         }
1740         if (cipher & WPA_CIPHER_GCMP) {
1741                 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : "+");
1742                 if (ret < 0 || ret >= end - pos)
1743                         return pos;
1744                 pos += ret;
1745                 first = 0;
1746         }
1747         return pos;
1748 }
1749
1750
1751 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
1752                                     const u8 *ie, size_t ie_len)
1753 {
1754         struct wpa_ie_data data;
1755         int first, ret;
1756
1757         ret = os_snprintf(pos, end - pos, "[%s-", proto);
1758         if (ret < 0 || ret >= end - pos)
1759                 return pos;
1760         pos += ret;
1761
1762         if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
1763                 ret = os_snprintf(pos, end - pos, "?]");
1764                 if (ret < 0 || ret >= end - pos)
1765                         return pos;
1766                 pos += ret;
1767                 return pos;
1768         }
1769
1770         first = 1;
1771         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1772                 ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
1773                 if (ret < 0 || ret >= end - pos)
1774                         return pos;
1775                 pos += ret;
1776                 first = 0;
1777         }
1778         if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
1779                 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
1780                 if (ret < 0 || ret >= end - pos)
1781                         return pos;
1782                 pos += ret;
1783                 first = 0;
1784         }
1785         if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
1786                 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
1787                 if (ret < 0 || ret >= end - pos)
1788                         return pos;
1789                 pos += ret;
1790                 first = 0;
1791         }
1792 #ifdef CONFIG_IEEE80211R
1793         if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1794                 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
1795                                   first ? "" : "+");
1796                 if (ret < 0 || ret >= end - pos)
1797                         return pos;
1798                 pos += ret;
1799                 first = 0;
1800         }
1801         if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1802                 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
1803                                   first ? "" : "+");
1804                 if (ret < 0 || ret >= end - pos)
1805                         return pos;
1806                 pos += ret;
1807                 first = 0;
1808         }
1809 #endif /* CONFIG_IEEE80211R */
1810 #ifdef CONFIG_IEEE80211W
1811         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1812                 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
1813                                   first ? "" : "+");
1814                 if (ret < 0 || ret >= end - pos)
1815                         return pos;
1816                 pos += ret;
1817                 first = 0;
1818         }
1819         if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1820                 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
1821                                   first ? "" : "+");
1822                 if (ret < 0 || ret >= end - pos)
1823                         return pos;
1824                 pos += ret;
1825                 first = 0;
1826         }
1827 #endif /* CONFIG_IEEE80211W */
1828
1829         pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
1830
1831         if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
1832                 ret = os_snprintf(pos, end - pos, "-preauth");
1833                 if (ret < 0 || ret >= end - pos)
1834                         return pos;
1835                 pos += ret;
1836         }
1837
1838         ret = os_snprintf(pos, end - pos, "]");
1839         if (ret < 0 || ret >= end - pos)
1840                 return pos;
1841         pos += ret;
1842
1843         return pos;
1844 }
1845
1846
1847 #ifdef CONFIG_WPS
1848 static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
1849                                             char *pos, char *end,
1850                                             struct wpabuf *wps_ie)
1851 {
1852         int ret;
1853         const char *txt;
1854
1855         if (wps_ie == NULL)
1856                 return pos;
1857         if (wps_is_selected_pbc_registrar(wps_ie))
1858                 txt = "[WPS-PBC]";
1859 #ifdef CONFIG_WPS2
1860         else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
1861                 txt = "[WPS-AUTH]";
1862 #endif /* CONFIG_WPS2 */
1863         else if (wps_is_selected_pin_registrar(wps_ie))
1864                 txt = "[WPS-PIN]";
1865         else
1866                 txt = "[WPS]";
1867
1868         ret = os_snprintf(pos, end - pos, "%s", txt);
1869         if (ret >= 0 && ret < end - pos)
1870                 pos += ret;
1871         wpabuf_free(wps_ie);
1872         return pos;
1873 }
1874 #endif /* CONFIG_WPS */
1875
1876
1877 static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
1878                                         char *pos, char *end,
1879                                         const struct wpa_bss *bss)
1880 {
1881 #ifdef CONFIG_WPS
1882         struct wpabuf *wps_ie;
1883         wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1884         return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
1885 #else /* CONFIG_WPS */
1886         return pos;
1887 #endif /* CONFIG_WPS */
1888 }
1889
1890
1891 /* Format one result on one text line into a buffer. */
1892 static int wpa_supplicant_ctrl_iface_scan_result(
1893         struct wpa_supplicant *wpa_s,
1894         const struct wpa_bss *bss, char *buf, size_t buflen)
1895 {
1896         char *pos, *end;
1897         int ret;
1898         const u8 *ie, *ie2, *p2p;
1899
1900         p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1901         if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
1902             os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
1903             0)
1904                 return 0; /* Do not show P2P listen discovery results here */
1905
1906         pos = buf;
1907         end = buf + buflen;
1908
1909         ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
1910                           MAC2STR(bss->bssid), bss->freq, bss->level);
1911         if (ret < 0 || ret >= end - pos)
1912                 return -1;
1913         pos += ret;
1914         ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1915         if (ie)
1916                 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
1917         ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1918         if (ie2)
1919                 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
1920         pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
1921         if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
1922                 ret = os_snprintf(pos, end - pos, "[WEP]");
1923                 if (ret < 0 || ret >= end - pos)
1924                         return -1;
1925                 pos += ret;
1926         }
1927         if (bss->caps & IEEE80211_CAP_IBSS) {
1928                 ret = os_snprintf(pos, end - pos, "[IBSS]");
1929                 if (ret < 0 || ret >= end - pos)
1930                         return -1;
1931                 pos += ret;
1932         }
1933         if (bss->caps & IEEE80211_CAP_ESS) {
1934                 ret = os_snprintf(pos, end - pos, "[ESS]");
1935                 if (ret < 0 || ret >= end - pos)
1936                         return -1;
1937                 pos += ret;
1938         }
1939         if (p2p) {
1940                 ret = os_snprintf(pos, end - pos, "[P2P]");
1941                 if (ret < 0 || ret >= end - pos)
1942                         return -1;
1943                 pos += ret;
1944         }
1945 #ifdef CONFIG_HS20
1946         if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
1947                 ret = os_snprintf(pos, end - pos, "[HS20]");
1948                 if (ret < 0 || ret >= end - pos)
1949                         return -1;
1950                 pos += ret;
1951         }
1952 #endif /* CONFIG_HS20 */
1953
1954         ret = os_snprintf(pos, end - pos, "\t%s",
1955                           wpa_ssid_txt(bss->ssid, bss->ssid_len));
1956         if (ret < 0 || ret >= end - pos)
1957                 return -1;
1958         pos += ret;
1959
1960         ret = os_snprintf(pos, end - pos, "\n");
1961         if (ret < 0 || ret >= end - pos)
1962                 return -1;
1963         pos += ret;
1964
1965         return pos - buf;
1966 }
1967
1968
1969 static int wpa_supplicant_ctrl_iface_scan_results(
1970         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1971 {
1972         char *pos, *end;
1973         struct wpa_bss *bss;
1974         int ret;
1975
1976         pos = buf;
1977         end = buf + buflen;
1978         ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
1979                           "flags / ssid\n");
1980         if (ret < 0 || ret >= end - pos)
1981                 return pos - buf;
1982         pos += ret;
1983
1984         dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
1985                 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
1986                                                             end - pos);
1987                 if (ret < 0 || ret >= end - pos)
1988                         return pos - buf;
1989                 pos += ret;
1990         }
1991
1992         return pos - buf;
1993 }
1994
1995
1996 static int wpa_supplicant_ctrl_iface_select_network(
1997         struct wpa_supplicant *wpa_s, char *cmd)
1998 {
1999         int id;
2000         struct wpa_ssid *ssid;
2001
2002         /* cmd: "<network id>" or "any" */
2003         if (os_strcmp(cmd, "any") == 0) {
2004                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2005                 ssid = NULL;
2006         } else {
2007                 id = atoi(cmd);
2008                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2009
2010                 ssid = wpa_config_get_network(wpa_s->conf, id);
2011                 if (ssid == NULL) {
2012                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2013                                    "network id=%d", id);
2014                         return -1;
2015                 }
2016                 if (ssid->disabled == 2) {
2017                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2018                                    "SELECT_NETWORK with persistent P2P group");
2019                         return -1;
2020                 }
2021         }
2022
2023         wpa_supplicant_select_network(wpa_s, ssid);
2024
2025         return 0;
2026 }
2027
2028
2029 static int wpa_supplicant_ctrl_iface_enable_network(
2030         struct wpa_supplicant *wpa_s, char *cmd)
2031 {
2032         int id;
2033         struct wpa_ssid *ssid;
2034
2035         /* cmd: "<network id>" or "all" */
2036         if (os_strcmp(cmd, "all") == 0) {
2037                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2038                 ssid = NULL;
2039         } else {
2040                 id = atoi(cmd);
2041                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2042
2043                 ssid = wpa_config_get_network(wpa_s->conf, id);
2044                 if (ssid == NULL) {
2045                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2046                                    "network id=%d", id);
2047                         return -1;
2048                 }
2049                 if (ssid->disabled == 2) {
2050                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2051                                    "ENABLE_NETWORK with persistent P2P group");
2052                         return -1;
2053                 }
2054
2055                 if (os_strstr(cmd, " no-connect")) {
2056                         ssid->disabled = 0;
2057                         return 0;
2058                 }
2059         }
2060         wpa_supplicant_enable_network(wpa_s, ssid);
2061
2062         return 0;
2063 }
2064
2065
2066 static int wpa_supplicant_ctrl_iface_disable_network(
2067         struct wpa_supplicant *wpa_s, char *cmd)
2068 {
2069         int id;
2070         struct wpa_ssid *ssid;
2071
2072         /* cmd: "<network id>" or "all" */
2073         if (os_strcmp(cmd, "all") == 0) {
2074                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2075                 ssid = NULL;
2076         } else {
2077                 id = atoi(cmd);
2078                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2079
2080                 ssid = wpa_config_get_network(wpa_s->conf, id);
2081                 if (ssid == NULL) {
2082                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2083                                    "network id=%d", id);
2084                         return -1;
2085                 }
2086                 if (ssid->disabled == 2) {
2087                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2088                                    "DISABLE_NETWORK with persistent P2P "
2089                                    "group");
2090                         return -1;
2091                 }
2092         }
2093         wpa_supplicant_disable_network(wpa_s, ssid);
2094
2095         return 0;
2096 }
2097
2098
2099 static int wpa_supplicant_ctrl_iface_add_network(
2100         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2101 {
2102         struct wpa_ssid *ssid;
2103         int ret;
2104
2105         wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2106
2107         ssid = wpa_config_add_network(wpa_s->conf);
2108         if (ssid == NULL)
2109                 return -1;
2110
2111         wpas_notify_network_added(wpa_s, ssid);
2112
2113         ssid->disabled = 1;
2114         wpa_config_set_network_defaults(ssid);
2115
2116         ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
2117         if (ret < 0 || (size_t) ret >= buflen)
2118                 return -1;
2119         return ret;
2120 }
2121
2122
2123 static int wpa_supplicant_ctrl_iface_remove_network(
2124         struct wpa_supplicant *wpa_s, char *cmd)
2125 {
2126         int id;
2127         struct wpa_ssid *ssid;
2128
2129         /* cmd: "<network id>" or "all" */
2130         if (os_strcmp(cmd, "all") == 0) {
2131                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
2132                 ssid = wpa_s->conf->ssid;
2133                 while (ssid) {
2134                         struct wpa_ssid *remove_ssid = ssid;
2135                         id = ssid->id;
2136                         ssid = ssid->next;
2137                         wpas_notify_network_removed(wpa_s, remove_ssid);
2138                         wpa_config_remove_network(wpa_s->conf, id);
2139                 }
2140                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2141                 if (wpa_s->current_ssid) {
2142 #ifdef CONFIG_SME
2143                         wpa_s->sme.prev_bssid_set = 0;
2144 #endif /* CONFIG_SME */
2145                         wpa_sm_set_config(wpa_s->wpa, NULL);
2146                         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2147                         wpa_supplicant_deauthenticate(
2148                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2149                 }
2150                 return 0;
2151         }
2152
2153         id = atoi(cmd);
2154         wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2155
2156         ssid = wpa_config_get_network(wpa_s->conf, id);
2157         if (ssid)
2158                 wpas_notify_network_removed(wpa_s, ssid);
2159         if (ssid == NULL) {
2160                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2161                            "id=%d", id);
2162                 return -1;
2163         }
2164
2165         if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
2166 #ifdef CONFIG_SME
2167                 wpa_s->sme.prev_bssid_set = 0;
2168 #endif /* CONFIG_SME */
2169                 /*
2170                  * Invalidate the EAP session cache if the current or
2171                  * previously used network is removed.
2172                  */
2173                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2174         }
2175
2176         if (ssid == wpa_s->current_ssid) {
2177                 wpa_sm_set_config(wpa_s->wpa, NULL);
2178                 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2179
2180                 wpa_supplicant_deauthenticate(wpa_s,
2181                                               WLAN_REASON_DEAUTH_LEAVING);
2182         }
2183
2184         if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2185                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2186                            "network id=%d", id);
2187                 return -1;
2188         }
2189
2190         return 0;
2191 }
2192
2193
2194 static int wpa_supplicant_ctrl_iface_set_network(
2195         struct wpa_supplicant *wpa_s, char *cmd)
2196 {
2197         int id;
2198         struct wpa_ssid *ssid;
2199         char *name, *value;
2200
2201         /* cmd: "<network id> <variable name> <value>" */
2202         name = os_strchr(cmd, ' ');
2203         if (name == NULL)
2204                 return -1;
2205         *name++ = '\0';
2206
2207         value = os_strchr(name, ' ');
2208         if (value == NULL)
2209                 return -1;
2210         *value++ = '\0';
2211
2212         id = atoi(cmd);
2213         wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2214                    id, name);
2215         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2216                               (u8 *) value, os_strlen(value));
2217
2218         ssid = wpa_config_get_network(wpa_s->conf, id);
2219         if (ssid == NULL) {
2220                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2221                            "id=%d", id);
2222                 return -1;
2223         }
2224
2225         if (wpa_config_set(ssid, name, value, 0) < 0) {
2226                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2227                            "variable '%s'", name);
2228                 return -1;
2229         }
2230
2231         if (os_strcmp(name, "bssid") != 0 &&
2232             os_strcmp(name, "priority") != 0)
2233                 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
2234
2235         if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
2236                 /*
2237                  * Invalidate the EAP session cache if anything in the current
2238                  * or previously used configuration changes.
2239                  */
2240                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2241         }
2242
2243         if ((os_strcmp(name, "psk") == 0 &&
2244              value[0] == '"' && ssid->ssid_len) ||
2245             (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2246                 wpa_config_update_psk(ssid);
2247         else if (os_strcmp(name, "priority") == 0)
2248                 wpa_config_update_prio_list(wpa_s->conf);
2249
2250         return 0;
2251 }
2252
2253
2254 static int wpa_supplicant_ctrl_iface_get_network(
2255         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2256 {
2257         int id;
2258         size_t res;
2259         struct wpa_ssid *ssid;
2260         char *name, *value;
2261
2262         /* cmd: "<network id> <variable name>" */
2263         name = os_strchr(cmd, ' ');
2264         if (name == NULL || buflen == 0)
2265                 return -1;
2266         *name++ = '\0';
2267
2268         id = atoi(cmd);
2269         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2270                    id, name);
2271
2272         ssid = wpa_config_get_network(wpa_s->conf, id);
2273         if (ssid == NULL) {
2274                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2275                            "id=%d", id);
2276                 return -1;
2277         }
2278
2279         value = wpa_config_get_no_key(ssid, name);
2280         if (value == NULL) {
2281                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2282                            "variable '%s'", name);
2283                 return -1;
2284         }
2285
2286         res = os_strlcpy(buf, value, buflen);
2287         if (res >= buflen) {
2288                 os_free(value);
2289                 return -1;
2290         }
2291
2292         os_free(value);
2293
2294         return res;
2295 }
2296
2297
2298 static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2299                                                 char *buf, size_t buflen)
2300 {
2301         char *pos, *end;
2302         struct wpa_cred *cred;
2303         int ret;
2304
2305         pos = buf;
2306         end = buf + buflen;
2307         ret = os_snprintf(pos, end - pos,
2308                           "cred id / realm / username / domain / imsi\n");
2309         if (ret < 0 || ret >= end - pos)
2310                 return pos - buf;
2311         pos += ret;
2312
2313         cred = wpa_s->conf->cred;
2314         while (cred) {
2315                 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2316                                   cred->id, cred->realm ? cred->realm : "",
2317                                   cred->username ? cred->username : "",
2318                                   cred->domain ? cred->domain : "",
2319                                   cred->imsi ? cred->imsi : "");
2320                 if (ret < 0 || ret >= end - pos)
2321                         return pos - buf;
2322                 pos += ret;
2323
2324                 cred = cred->next;
2325         }
2326
2327         return pos - buf;
2328 }
2329
2330
2331 static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2332                                               char *buf, size_t buflen)
2333 {
2334         struct wpa_cred *cred;
2335         int ret;
2336
2337         wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2338
2339         cred = wpa_config_add_cred(wpa_s->conf);
2340         if (cred == NULL)
2341                 return -1;
2342
2343         ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2344         if (ret < 0 || (size_t) ret >= buflen)
2345                 return -1;
2346         return ret;
2347 }
2348
2349
2350 static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2351                                  struct wpa_cred *cred)
2352 {
2353         struct wpa_ssid *ssid;
2354         char str[20];
2355
2356         if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
2357                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2358                 return -1;
2359         }
2360
2361         /* Remove any network entry created based on the removed credential */
2362         ssid = wpa_s->conf->ssid;
2363         while (ssid) {
2364                 if (ssid->parent_cred == cred) {
2365                         wpa_printf(MSG_DEBUG, "Remove network id %d since it "
2366                                    "used the removed credential", ssid->id);
2367                         os_snprintf(str, sizeof(str), "%d", ssid->id);
2368                         ssid = ssid->next;
2369                         wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
2370                 } else
2371                         ssid = ssid->next;
2372         }
2373
2374         return 0;
2375 }
2376
2377
2378 static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2379                                                  char *cmd)
2380 {
2381         int id;
2382         struct wpa_cred *cred, *prev;
2383
2384         /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
2385         if (os_strcmp(cmd, "all") == 0) {
2386                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2387                 cred = wpa_s->conf->cred;
2388                 while (cred) {
2389                         prev = cred;
2390                         cred = cred->next;
2391                         wpas_ctrl_remove_cred(wpa_s, prev);
2392                 }
2393                 return 0;
2394         }
2395
2396         if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
2397                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
2398                            cmd + 8);
2399                 cred = wpa_s->conf->cred;
2400                 while (cred) {
2401                         prev = cred;
2402                         cred = cred->next;
2403                         if (prev->domain &&
2404                             os_strcmp(prev->domain, cmd + 8) == 0)
2405                                 wpas_ctrl_remove_cred(wpa_s, prev);
2406                 }
2407                 return 0;
2408         }
2409
2410         id = atoi(cmd);
2411         wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2412
2413         cred = wpa_config_get_cred(wpa_s->conf, id);
2414         return wpas_ctrl_remove_cred(wpa_s, cred);
2415 }
2416
2417
2418 static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2419                                               char *cmd)
2420 {
2421         int id;
2422         struct wpa_cred *cred;
2423         char *name, *value;
2424
2425         /* cmd: "<cred id> <variable name> <value>" */
2426         name = os_strchr(cmd, ' ');
2427         if (name == NULL)
2428                 return -1;
2429         *name++ = '\0';
2430
2431         value = os_strchr(name, ' ');
2432         if (value == NULL)
2433                 return -1;
2434         *value++ = '\0';
2435
2436         id = atoi(cmd);
2437         wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2438                    id, name);
2439         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2440                               (u8 *) value, os_strlen(value));
2441
2442         cred = wpa_config_get_cred(wpa_s->conf, id);
2443         if (cred == NULL) {
2444                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2445                            id);
2446                 return -1;
2447         }
2448
2449         if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2450                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2451                            "variable '%s'", name);
2452                 return -1;
2453         }
2454
2455         return 0;
2456 }
2457
2458
2459 #ifndef CONFIG_NO_CONFIG_WRITE
2460 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2461 {
2462         int ret;
2463
2464         if (!wpa_s->conf->update_config) {
2465                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2466                            "to update configuration (update_config=0)");
2467                 return -1;
2468         }
2469
2470         ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2471         if (ret) {
2472                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2473                            "update configuration");
2474         } else {
2475                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2476                            " updated");
2477         }
2478
2479         return ret;
2480 }
2481 #endif /* CONFIG_NO_CONFIG_WRITE */
2482
2483
2484 static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2485                                               struct wpa_driver_capa *capa,
2486                                               char *buf, size_t buflen)
2487 {
2488         int ret, first = 1;
2489         char *pos, *end;
2490         size_t len;
2491
2492         pos = buf;
2493         end = pos + buflen;
2494
2495         if (res < 0) {
2496                 if (strict)
2497                         return 0;
2498                 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2499                 if (len >= buflen)
2500                         return -1;
2501                 return len;
2502         }
2503
2504         if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2505                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2506                 if (ret < 0 || ret >= end - pos)
2507                         return pos - buf;
2508                 pos += ret;
2509                 first = 0;
2510         }
2511
2512         if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2513                 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
2514                 if (ret < 0 || ret >= end - pos)
2515                         return pos - buf;
2516                 pos += ret;
2517                 first = 0;
2518         }
2519
2520         if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2521                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2522                 if (ret < 0 || ret >= end - pos)
2523                         return pos - buf;
2524                 pos += ret;
2525                 first = 0;
2526         }
2527
2528         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2529                 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
2530                 if (ret < 0 || ret >= end - pos)
2531                         return pos - buf;
2532                 pos += ret;
2533                 first = 0;
2534         }
2535
2536         return pos - buf;
2537 }
2538
2539
2540 static int ctrl_iface_get_capability_group(int res, char *strict,
2541                                            struct wpa_driver_capa *capa,
2542                                            char *buf, size_t buflen)
2543 {
2544         int ret, first = 1;
2545         char *pos, *end;
2546         size_t len;
2547
2548         pos = buf;
2549         end = pos + buflen;
2550
2551         if (res < 0) {
2552                 if (strict)
2553                         return 0;
2554                 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
2555                 if (len >= buflen)
2556                         return -1;
2557                 return len;
2558         }
2559
2560         if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2561                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2562                 if (ret < 0 || ret >= end - pos)
2563                         return pos - buf;
2564                 pos += ret;
2565                 first = 0;
2566         }
2567
2568         if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2569                 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
2570                 if (ret < 0 || ret >= end - pos)
2571                         return pos - buf;
2572                 pos += ret;
2573                 first = 0;
2574         }
2575
2576         if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2577                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2578                 if (ret < 0 || ret >= end - pos)
2579                         return pos - buf;
2580                 pos += ret;
2581                 first = 0;
2582         }
2583
2584         if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
2585                 ret = os_snprintf(pos, end - pos, "%sWEP104",
2586                                   first ? "" : " ");
2587                 if (ret < 0 || ret >= end - pos)
2588                         return pos - buf;
2589                 pos += ret;
2590                 first = 0;
2591         }
2592
2593         if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
2594                 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
2595                 if (ret < 0 || ret >= end - pos)
2596                         return pos - buf;
2597                 pos += ret;
2598                 first = 0;
2599         }
2600
2601         return pos - buf;
2602 }
2603
2604
2605 static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
2606                                               struct wpa_driver_capa *capa,
2607                                               char *buf, size_t buflen)
2608 {
2609         int ret;
2610         char *pos, *end;
2611         size_t len;
2612
2613         pos = buf;
2614         end = pos + buflen;
2615
2616         if (res < 0) {
2617                 if (strict)
2618                         return 0;
2619                 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
2620                                  "NONE", buflen);
2621                 if (len >= buflen)
2622                         return -1;
2623                 return len;
2624         }
2625
2626         ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
2627         if (ret < 0 || ret >= end - pos)
2628                 return pos - buf;
2629         pos += ret;
2630
2631         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2632                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
2633                 ret = os_snprintf(pos, end - pos, " WPA-EAP");
2634                 if (ret < 0 || ret >= end - pos)
2635                         return pos - buf;
2636                 pos += ret;
2637         }
2638
2639         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2640                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2641                 ret = os_snprintf(pos, end - pos, " WPA-PSK");
2642                 if (ret < 0 || ret >= end - pos)
2643                         return pos - buf;
2644                 pos += ret;
2645         }
2646
2647         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2648                 ret = os_snprintf(pos, end - pos, " WPA-NONE");
2649                 if (ret < 0 || ret >= end - pos)
2650                         return pos - buf;
2651                 pos += ret;
2652         }
2653
2654         return pos - buf;
2655 }
2656
2657
2658 static int ctrl_iface_get_capability_proto(int res, char *strict,
2659                                            struct wpa_driver_capa *capa,
2660                                            char *buf, size_t buflen)
2661 {
2662         int ret, first = 1;
2663         char *pos, *end;
2664         size_t len;
2665
2666         pos = buf;
2667         end = pos + buflen;
2668
2669         if (res < 0) {
2670                 if (strict)
2671                         return 0;
2672                 len = os_strlcpy(buf, "RSN WPA", buflen);
2673                 if (len >= buflen)
2674                         return -1;
2675                 return len;
2676         }
2677
2678         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2679                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2680                 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
2681                 if (ret < 0 || ret >= end - pos)
2682                         return pos - buf;
2683                 pos += ret;
2684                 first = 0;
2685         }
2686
2687         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2688                               WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
2689                 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
2690                 if (ret < 0 || ret >= end - pos)
2691                         return pos - buf;
2692                 pos += ret;
2693                 first = 0;
2694         }
2695
2696         return pos - buf;
2697 }
2698
2699
2700 static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
2701                                               struct wpa_driver_capa *capa,
2702                                               char *buf, size_t buflen)
2703 {
2704         int ret, first = 1;
2705         char *pos, *end;
2706         size_t len;
2707
2708         pos = buf;
2709         end = pos + buflen;
2710
2711         if (res < 0) {
2712                 if (strict)
2713                         return 0;
2714                 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
2715                 if (len >= buflen)
2716                         return -1;
2717                 return len;
2718         }
2719
2720         if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
2721                 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
2722                 if (ret < 0 || ret >= end - pos)
2723                         return pos - buf;
2724                 pos += ret;
2725                 first = 0;
2726         }
2727
2728         if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
2729                 ret = os_snprintf(pos, end - pos, "%sSHARED",
2730                                   first ? "" : " ");
2731                 if (ret < 0 || ret >= end - pos)
2732                         return pos - buf;
2733                 pos += ret;
2734                 first = 0;
2735         }
2736
2737         if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
2738                 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
2739                 if (ret < 0 || ret >= end - pos)
2740                         return pos - buf;
2741                 pos += ret;
2742                 first = 0;
2743         }
2744
2745         return pos - buf;
2746 }
2747
2748
2749 static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
2750                                               char *buf, size_t buflen)
2751 {
2752         struct hostapd_channel_data *chnl;
2753         int ret, i, j;
2754         char *pos, *end, *hmode;
2755
2756         pos = buf;
2757         end = pos + buflen;
2758
2759         for (j = 0; j < wpa_s->hw.num_modes; j++) {
2760                 switch (wpa_s->hw.modes[j].mode) {
2761                 case HOSTAPD_MODE_IEEE80211B:
2762                         hmode = "B";
2763                         break;
2764                 case HOSTAPD_MODE_IEEE80211G:
2765                         hmode = "G";
2766                         break;
2767                 case HOSTAPD_MODE_IEEE80211A:
2768                         hmode = "A";
2769                         break;
2770                 case HOSTAPD_MODE_IEEE80211AD:
2771                         hmode = "AD";
2772                         break;
2773                 default:
2774                         continue;
2775                 }
2776                 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
2777                 if (ret < 0 || ret >= end - pos)
2778                         return pos - buf;
2779                 pos += ret;
2780                 chnl = wpa_s->hw.modes[j].channels;
2781                 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
2782                         if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
2783                                 continue;
2784                         ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
2785                         if (ret < 0 || ret >= end - pos)
2786                                 return pos - buf;
2787                         pos += ret;
2788                 }
2789                 ret = os_snprintf(pos, end - pos, "\n");
2790                 if (ret < 0 || ret >= end - pos)
2791                         return pos - buf;
2792                 pos += ret;
2793         }
2794
2795         return pos - buf;
2796 }
2797
2798
2799 static int wpa_supplicant_ctrl_iface_get_capability(
2800         struct wpa_supplicant *wpa_s, const char *_field, char *buf,
2801         size_t buflen)
2802 {
2803         struct wpa_driver_capa capa;
2804         int res;
2805         char *strict;
2806         char field[30];
2807         size_t len;
2808
2809         /* Determine whether or not strict checking was requested */
2810         len = os_strlcpy(field, _field, sizeof(field));
2811         if (len >= sizeof(field))
2812                 return -1;
2813         strict = os_strchr(field, ' ');
2814         if (strict != NULL) {
2815                 *strict++ = '\0';
2816                 if (os_strcmp(strict, "strict") != 0)
2817                         return -1;
2818         }
2819
2820         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
2821                 field, strict ? strict : "");
2822
2823         if (os_strcmp(field, "eap") == 0) {
2824                 return eap_get_names(buf, buflen);
2825         }
2826
2827         res = wpa_drv_get_capa(wpa_s, &capa);
2828
2829         if (os_strcmp(field, "pairwise") == 0)
2830                 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
2831                                                           buf, buflen);
2832
2833         if (os_strcmp(field, "group") == 0)
2834                 return ctrl_iface_get_capability_group(res, strict, &capa,
2835                                                        buf, buflen);
2836
2837         if (os_strcmp(field, "key_mgmt") == 0)
2838                 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
2839                                                           buf, buflen);
2840
2841         if (os_strcmp(field, "proto") == 0)
2842                 return ctrl_iface_get_capability_proto(res, strict, &capa,
2843                                                        buf, buflen);
2844
2845         if (os_strcmp(field, "auth_alg") == 0)
2846                 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
2847                                                           buf, buflen);
2848
2849         if (os_strcmp(field, "channels") == 0)
2850                 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
2851
2852         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
2853                    field);
2854
2855         return -1;
2856 }
2857
2858
2859 #ifdef CONFIG_INTERWORKING
2860 static char * anqp_add_hex(char *pos, char *end, const char *title,
2861                            struct wpabuf *data)
2862 {
2863         char *start = pos;
2864         size_t i;
2865         int ret;
2866         const u8 *d;
2867
2868         if (data == NULL)
2869                 return start;
2870
2871         ret = os_snprintf(pos, end - pos, "%s=", title);
2872         if (ret < 0 || ret >= end - pos)
2873                 return start;
2874         pos += ret;
2875
2876         d = wpabuf_head_u8(data);
2877         for (i = 0; i < wpabuf_len(data); i++) {
2878                 ret = os_snprintf(pos, end - pos, "%02x", *d++);
2879                 if (ret < 0 || ret >= end - pos)
2880                         return start;
2881                 pos += ret;
2882         }
2883
2884         ret = os_snprintf(pos, end - pos, "\n");
2885         if (ret < 0 || ret >= end - pos)
2886                 return start;
2887         pos += ret;
2888
2889         return pos;
2890 }
2891 #endif /* CONFIG_INTERWORKING */
2892
2893
2894 static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
2895                           unsigned long mask, char *buf, size_t buflen)
2896 {
2897         size_t i;
2898         int ret;
2899         char *pos, *end;
2900         const u8 *ie, *ie2;
2901
2902         pos = buf;
2903         end = buf + buflen;
2904
2905         if (mask & WPA_BSS_MASK_ID) {
2906                 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
2907                 if (ret < 0 || ret >= end - pos)
2908                         return 0;
2909                 pos += ret;
2910         }
2911
2912         if (mask & WPA_BSS_MASK_BSSID) {
2913                 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
2914                                   MAC2STR(bss->bssid));
2915                 if (ret < 0 || ret >= end - pos)
2916                         return 0;
2917                 pos += ret;
2918         }
2919
2920         if (mask & WPA_BSS_MASK_FREQ) {
2921                 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
2922                 if (ret < 0 || ret >= end - pos)
2923                         return 0;
2924                 pos += ret;
2925         }
2926
2927         if (mask & WPA_BSS_MASK_BEACON_INT) {
2928                 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
2929                                   bss->beacon_int);
2930                 if (ret < 0 || ret >= end - pos)
2931                         return 0;
2932                 pos += ret;
2933         }
2934
2935         if (mask & WPA_BSS_MASK_CAPABILITIES) {
2936                 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
2937                                   bss->caps);
2938                 if (ret < 0 || ret >= end - pos)
2939                         return 0;
2940                 pos += ret;
2941         }
2942
2943         if (mask & WPA_BSS_MASK_QUAL) {
2944                 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
2945                 if (ret < 0 || ret >= end - pos)
2946                         return 0;
2947                 pos += ret;
2948         }
2949
2950         if (mask & WPA_BSS_MASK_NOISE) {
2951                 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
2952                 if (ret < 0 || ret >= end - pos)
2953                         return 0;
2954                 pos += ret;
2955         }
2956
2957         if (mask & WPA_BSS_MASK_LEVEL) {
2958                 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
2959                 if (ret < 0 || ret >= end - pos)
2960                         return 0;
2961                 pos += ret;
2962         }
2963
2964         if (mask & WPA_BSS_MASK_TSF) {
2965                 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
2966                                   (unsigned long long) bss->tsf);
2967                 if (ret < 0 || ret >= end - pos)
2968                         return 0;
2969                 pos += ret;
2970         }
2971
2972         if (mask & WPA_BSS_MASK_AGE) {
2973                 struct os_time now;
2974
2975                 os_get_time(&now);
2976                 ret = os_snprintf(pos, end - pos, "age=%d\n",
2977                                   (int) (now.sec - bss->last_update.sec));
2978                 if (ret < 0 || ret >= end - pos)
2979                         return 0;
2980                 pos += ret;
2981         }
2982
2983         if (mask & WPA_BSS_MASK_IE) {
2984                 ret = os_snprintf(pos, end - pos, "ie=");
2985                 if (ret < 0 || ret >= end - pos)
2986                         return 0;
2987                 pos += ret;
2988
2989                 ie = (const u8 *) (bss + 1);
2990                 for (i = 0; i < bss->ie_len; i++) {
2991                         ret = os_snprintf(pos, end - pos, "%02x", *ie++);
2992                         if (ret < 0 || ret >= end - pos)
2993                                 return 0;
2994                         pos += ret;
2995                 }
2996
2997                 ret = os_snprintf(pos, end - pos, "\n");
2998                 if (ret < 0 || ret >= end - pos)
2999                         return 0;
3000                 pos += ret;
3001         }
3002
3003         if (mask & WPA_BSS_MASK_FLAGS) {
3004                 ret = os_snprintf(pos, end - pos, "flags=");
3005                 if (ret < 0 || ret >= end - pos)
3006                         return 0;
3007                 pos += ret;
3008
3009                 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3010                 if (ie)
3011                         pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3012                                                     2 + ie[1]);
3013                 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3014                 if (ie2)
3015                         pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3016                                                     2 + ie2[1]);
3017                 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3018                 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3019                         ret = os_snprintf(pos, end - pos, "[WEP]");
3020                         if (ret < 0 || ret >= end - pos)
3021                                 return 0;
3022                         pos += ret;
3023                 }
3024                 if (bss->caps & IEEE80211_CAP_IBSS) {
3025                         ret = os_snprintf(pos, end - pos, "[IBSS]");
3026                         if (ret < 0 || ret >= end - pos)
3027                                 return 0;
3028                         pos += ret;
3029                 }
3030                 if (bss->caps & IEEE80211_CAP_ESS) {
3031                         ret = os_snprintf(pos, end - pos, "[ESS]");
3032                         if (ret < 0 || ret >= end - pos)
3033                                 return 0;
3034                         pos += ret;
3035                 }
3036                 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
3037                         ret = os_snprintf(pos, end - pos, "[P2P]");
3038                         if (ret < 0 || ret >= end - pos)
3039                                 return 0;
3040                         pos += ret;
3041                 }
3042 #ifdef CONFIG_HS20
3043                 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3044                         ret = os_snprintf(pos, end - pos, "[HS20]");
3045                         if (ret < 0 || ret >= end - pos)
3046                                 return -1;
3047                         pos += ret;
3048                 }
3049 #endif /* CONFIG_HS20 */
3050
3051                 ret = os_snprintf(pos, end - pos, "\n");
3052                 if (ret < 0 || ret >= end - pos)
3053                         return 0;
3054                 pos += ret;
3055         }
3056
3057         if (mask & WPA_BSS_MASK_SSID) {
3058                 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3059                                   wpa_ssid_txt(bss->ssid, bss->ssid_len));
3060                 if (ret < 0 || ret >= end - pos)
3061                         return 0;
3062                 pos += ret;
3063         }
3064
3065 #ifdef CONFIG_WPS
3066         if (mask & WPA_BSS_MASK_WPS_SCAN) {
3067                 ie = (const u8 *) (bss + 1);
3068                 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3069                 if (ret < 0 || ret >= end - pos)
3070                         return 0;
3071                 pos += ret;
3072         }
3073 #endif /* CONFIG_WPS */
3074
3075 #ifdef CONFIG_P2P
3076         if (mask & WPA_BSS_MASK_P2P_SCAN) {
3077                 ie = (const u8 *) (bss + 1);
3078                 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3079                 if (ret < 0 || ret >= end - pos)
3080                         return 0;
3081                 pos += ret;
3082         }
3083 #endif /* CONFIG_P2P */
3084
3085 #ifdef CONFIG_WIFI_DISPLAY
3086         if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3087                 struct wpabuf *wfd;
3088                 ie = (const u8 *) (bss + 1);
3089                 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3090                                                   WFD_IE_VENDOR_TYPE);
3091                 if (wfd) {
3092                         ret = os_snprintf(pos, end - pos, "wfd_subelems=");
3093                         if (ret < 0 || ret >= end - pos)
3094                                 return pos - buf;
3095                         pos += ret;
3096
3097                         pos += wpa_snprintf_hex(pos, end - pos,
3098                                                 wpabuf_head(wfd),
3099                                                 wpabuf_len(wfd));
3100                         wpabuf_free(wfd);
3101
3102                         ret = os_snprintf(pos, end - pos, "\n");
3103                         if (ret < 0 || ret >= end - pos)
3104                                 return pos - buf;
3105                         pos += ret;
3106                 }
3107         }
3108 #endif /* CONFIG_WIFI_DISPLAY */
3109
3110 #ifdef CONFIG_INTERWORKING
3111         if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3112                 struct wpa_bss_anqp *anqp = bss->anqp;
3113                 pos = anqp_add_hex(pos, end, "anqp_venue_name",
3114                                    anqp->venue_name);
3115                 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
3116                                    anqp->network_auth_type);
3117                 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
3118                                    anqp->roaming_consortium);
3119                 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
3120                                    anqp->ip_addr_type_availability);
3121                 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
3122                                    anqp->nai_realm);
3123                 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
3124                 pos = anqp_add_hex(pos, end, "anqp_domain_name",
3125                                    anqp->domain_name);
3126 #ifdef CONFIG_HS20
3127                 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
3128                                    anqp->hs20_operator_friendly_name);
3129                 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
3130                                    anqp->hs20_wan_metrics);
3131                 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
3132                                    anqp->hs20_connection_capability);
3133 #endif /* CONFIG_HS20 */
3134         }
3135 #endif /* CONFIG_INTERWORKING */
3136
3137         return pos - buf;
3138 }
3139
3140
3141 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
3142                                          const char *cmd, char *buf,
3143                                          size_t buflen)
3144 {
3145         u8 bssid[ETH_ALEN];
3146         size_t i;
3147         struct wpa_bss *bss;
3148         struct wpa_bss *bsslast = NULL;
3149         struct dl_list *next;
3150         int ret = 0;
3151         int len;
3152         char *ctmp;
3153         unsigned long mask = WPA_BSS_MASK_ALL;
3154
3155         if (os_strncmp(cmd, "RANGE=", 6) == 0) {
3156                 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
3157                         bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
3158                                             list_id);
3159                         bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
3160                                                list_id);
3161                 } else { /* N1-N2 */
3162                         unsigned int id1, id2;
3163
3164                         if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
3165                                 wpa_printf(MSG_INFO, "Wrong BSS range "
3166                                            "format");
3167                                 return 0;
3168                         }
3169
3170                         id1 = atoi(cmd + 6);
3171                         bss = wpa_bss_get_id(wpa_s, id1);
3172                         id2 = atoi(ctmp + 1);
3173                         if (id2 == 0)
3174                                 bsslast = dl_list_last(&wpa_s->bss_id,
3175                                                        struct wpa_bss,
3176                                                        list_id);
3177                         else {
3178                                 bsslast = wpa_bss_get_id(wpa_s, id2);
3179                                 if (bsslast == NULL && bss && id2 > id1) {
3180                                         struct wpa_bss *tmp = bss;
3181                                         for (;;) {
3182                                                 next = tmp->list_id.next;
3183                                                 if (next == &wpa_s->bss_id)
3184                                                         break;
3185                                                 tmp = dl_list_entry(
3186                                                         next, struct wpa_bss,
3187                                                         list_id);
3188                                                 if (tmp->id > id2)
3189                                                         break;
3190                                                 bsslast = tmp;
3191                                         }
3192                                 }
3193                         }
3194                 }
3195         } else if (os_strcmp(cmd, "FIRST") == 0)
3196                 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
3197         else if (os_strncmp(cmd, "ID-", 3) == 0) {
3198                 i = atoi(cmd + 3);
3199                 bss = wpa_bss_get_id(wpa_s, i);
3200         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3201                 i = atoi(cmd + 5);
3202                 bss = wpa_bss_get_id(wpa_s, i);
3203                 if (bss) {
3204                         next = bss->list_id.next;
3205                         if (next == &wpa_s->bss_id)
3206                                 bss = NULL;
3207                         else
3208                                 bss = dl_list_entry(next, struct wpa_bss,
3209                                                     list_id);
3210                 }
3211 #ifdef CONFIG_P2P
3212         } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
3213                 if (hwaddr_aton(cmd + 13, bssid) == 0)
3214                         bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
3215                 else
3216                         bss = NULL;
3217 #endif /* CONFIG_P2P */
3218         } else if (hwaddr_aton(cmd, bssid) == 0)
3219                 bss = wpa_bss_get_bssid(wpa_s, bssid);
3220         else {
3221                 struct wpa_bss *tmp;
3222                 i = atoi(cmd);
3223                 bss = NULL;
3224                 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
3225                 {
3226                         if (i-- == 0) {
3227                                 bss = tmp;
3228                                 break;
3229                         }
3230                 }
3231         }
3232
3233         if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
3234                 mask = strtoul(ctmp + 5, NULL, 0x10);
3235                 if (mask == 0)
3236                         mask = WPA_BSS_MASK_ALL;
3237         }
3238
3239         if (bss == NULL)
3240                 return 0;
3241
3242         if (bsslast == NULL)
3243                 bsslast = bss;
3244         do {
3245                 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
3246                 ret += len;
3247                 buf += len;
3248                 buflen -= len;
3249                 if (bss == bsslast)
3250                         break;
3251                 next = bss->list_id.next;
3252                 if (next == &wpa_s->bss_id)
3253                         break;
3254                 bss = dl_list_entry(next, struct wpa_bss, list_id);
3255         } while (bss && len);
3256
3257         return ret;
3258 }
3259
3260
3261 static int wpa_supplicant_ctrl_iface_ap_scan(
3262         struct wpa_supplicant *wpa_s, char *cmd)
3263 {
3264         int ap_scan = atoi(cmd);
3265         return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
3266 }
3267
3268
3269 static int wpa_supplicant_ctrl_iface_scan_interval(
3270         struct wpa_supplicant *wpa_s, char *cmd)
3271 {
3272         int scan_int = atoi(cmd);
3273         return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
3274 }
3275
3276
3277 static int wpa_supplicant_ctrl_iface_bss_expire_age(
3278         struct wpa_supplicant *wpa_s, char *cmd)
3279 {
3280         int expire_age = atoi(cmd);
3281         return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
3282 }
3283
3284
3285 static int wpa_supplicant_ctrl_iface_bss_expire_count(
3286         struct wpa_supplicant *wpa_s, char *cmd)
3287 {
3288         int expire_count = atoi(cmd);
3289         return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
3290 }
3291
3292
3293 static int wpa_supplicant_ctrl_iface_bss_flush(
3294         struct wpa_supplicant *wpa_s, char *cmd)
3295 {
3296         int flush_age = atoi(cmd);
3297
3298         if (flush_age == 0)
3299                 wpa_bss_flush(wpa_s);
3300         else
3301                 wpa_bss_flush_by_age(wpa_s, flush_age);
3302         return 0;
3303 }
3304
3305
3306 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
3307 {
3308         wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
3309         /* MLME-DELETEKEYS.request */
3310         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
3311         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
3312         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
3313         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
3314 #ifdef CONFIG_IEEE80211W
3315         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
3316         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
3317 #endif /* CONFIG_IEEE80211W */
3318
3319         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
3320                         0);
3321         /* MLME-SETPROTECTION.request(None) */
3322         wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
3323                                    MLME_SETPROTECTION_PROTECT_TYPE_NONE,
3324                                    MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3325         wpa_sm_drop_sa(wpa_s->wpa);
3326 }
3327
3328
3329 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
3330                                           char *addr)
3331 {
3332 #ifdef CONFIG_NO_SCAN_PROCESSING
3333         return -1;
3334 #else /* CONFIG_NO_SCAN_PROCESSING */
3335         u8 bssid[ETH_ALEN];
3336         struct wpa_bss *bss;
3337         struct wpa_ssid *ssid = wpa_s->current_ssid;
3338
3339         if (hwaddr_aton(addr, bssid)) {
3340                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
3341                            "address '%s'", addr);
3342                 return -1;
3343         }
3344
3345         wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
3346
3347         bss = wpa_bss_get_bssid(wpa_s, bssid);
3348         if (!bss) {
3349                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
3350                            "from BSS table");
3351                 return -1;
3352         }
3353
3354         /*
3355          * TODO: Find best network configuration block from configuration to
3356          * allow roaming to other networks
3357          */
3358
3359         if (!ssid) {
3360                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
3361                            "configuration known for the target AP");
3362                 return -1;
3363         }
3364
3365         wpa_s->reassociate = 1;
3366         wpa_supplicant_connect(wpa_s, bss, ssid);
3367
3368         return 0;
3369 #endif /* CONFIG_NO_SCAN_PROCESSING */
3370 }
3371
3372
3373 #ifdef CONFIG_P2P
3374 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
3375 {
3376         unsigned int timeout = atoi(cmd);
3377         enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
3378         u8 dev_id[ETH_ALEN], *_dev_id = NULL;
3379         char *pos;
3380         unsigned int search_delay;
3381
3382         if (os_strstr(cmd, "type=social"))
3383                 type = P2P_FIND_ONLY_SOCIAL;
3384         else if (os_strstr(cmd, "type=progressive"))
3385                 type = P2P_FIND_PROGRESSIVE;
3386
3387         pos = os_strstr(cmd, "dev_id=");
3388         if (pos) {
3389                 pos += 7;
3390                 if (hwaddr_aton(pos, dev_id))
3391                         return -1;
3392                 _dev_id = dev_id;
3393         }
3394
3395         pos = os_strstr(cmd, "delay=");
3396         if (pos) {
3397                 pos += 6;
3398                 search_delay = atoi(pos);
3399         } else
3400                 search_delay = wpas_p2p_search_delay(wpa_s);
3401
3402         return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
3403                              search_delay);
3404 }
3405
3406
3407 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
3408                             char *buf, size_t buflen)
3409 {
3410         u8 addr[ETH_ALEN];
3411         char *pos, *pos2;
3412         char *pin = NULL;
3413         enum p2p_wps_method wps_method;
3414         int new_pin;
3415         int ret;
3416         int persistent_group, persistent_id = -1;
3417         int join;
3418         int auth;
3419         int automatic;
3420         int go_intent = -1;
3421         int freq = 0;
3422         int pd;
3423         int ht40;
3424
3425         /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
3426          * [persistent|persistent=<network id>]
3427          * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
3428          * [ht40] */
3429
3430         if (hwaddr_aton(cmd, addr))
3431                 return -1;
3432
3433         pos = cmd + 17;
3434         if (*pos != ' ')
3435                 return -1;
3436         pos++;
3437
3438         persistent_group = os_strstr(pos, " persistent") != NULL;
3439         pos2 = os_strstr(pos, " persistent=");
3440         if (pos2) {
3441                 struct wpa_ssid *ssid;
3442                 persistent_id = atoi(pos2 + 12);
3443                 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3444                 if (ssid == NULL || ssid->disabled != 2 ||
3445                     ssid->mode != WPAS_MODE_P2P_GO) {
3446                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3447                                    "SSID id=%d for persistent P2P group (GO)",
3448                                    persistent_id);
3449                         return -1;
3450                 }
3451         }
3452         join = os_strstr(pos, " join") != NULL;
3453         auth = os_strstr(pos, " auth") != NULL;
3454         automatic = os_strstr(pos, " auto") != NULL;
3455         pd = os_strstr(pos, " provdisc") != NULL;
3456         ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
3457
3458         pos2 = os_strstr(pos, " go_intent=");
3459         if (pos2) {
3460                 pos2 += 11;
3461                 go_intent = atoi(pos2);
3462                 if (go_intent < 0 || go_intent > 15)
3463                         return -1;
3464         }
3465
3466         pos2 = os_strstr(pos, " freq=");
3467         if (pos2) {
3468                 pos2 += 6;
3469                 freq = atoi(pos2);
3470                 if (freq <= 0)
3471                         return -1;
3472         }
3473
3474         if (os_strncmp(pos, "pin", 3) == 0) {
3475                 /* Request random PIN (to be displayed) and enable the PIN */
3476                 wps_method = WPS_PIN_DISPLAY;
3477         } else if (os_strncmp(pos, "pbc", 3) == 0) {
3478                 wps_method = WPS_PBC;
3479         } else {
3480                 pin = pos;
3481                 pos = os_strchr(pin, ' ');
3482                 wps_method = WPS_PIN_KEYPAD;
3483                 if (pos) {
3484                         *pos++ = '\0';
3485                         if (os_strncmp(pos, "display", 7) == 0)
3486                                 wps_method = WPS_PIN_DISPLAY;
3487                 }
3488                 if (!wps_pin_str_valid(pin)) {
3489                         os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
3490                         return 17;
3491                 }
3492         }
3493
3494         new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
3495                                    persistent_group, automatic, join,
3496                                    auth, go_intent, freq, persistent_id, pd,
3497                                    ht40);
3498         if (new_pin == -2) {
3499                 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
3500                 return 25;
3501         }
3502         if (new_pin == -3) {
3503                 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
3504                 return 25;
3505         }
3506         if (new_pin < 0)
3507                 return -1;
3508         if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
3509                 ret = os_snprintf(buf, buflen, "%08d", new_pin);
3510                 if (ret < 0 || (size_t) ret >= buflen)
3511                         return -1;
3512                 return ret;
3513         }
3514
3515         os_memcpy(buf, "OK\n", 3);
3516         return 3;
3517 }
3518
3519
3520 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
3521 {
3522         unsigned int timeout = atoi(cmd);
3523         return wpas_p2p_listen(wpa_s, timeout);
3524 }
3525
3526
3527 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
3528 {
3529         u8 addr[ETH_ALEN];
3530         char *pos;
3531         enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
3532
3533         /* <addr> <config method> [join|auto] */
3534
3535         if (hwaddr_aton(cmd, addr))
3536                 return -1;
3537
3538         pos = cmd + 17;
3539         if (*pos != ' ')
3540                 return -1;
3541         pos++;
3542
3543         if (os_strstr(pos, " join") != NULL)
3544                 use = WPAS_P2P_PD_FOR_JOIN;
3545         else if (os_strstr(pos, " auto") != NULL)
3546                 use = WPAS_P2P_PD_AUTO;
3547
3548         return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
3549 }
3550
3551
3552 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
3553                               size_t buflen)
3554 {
3555         struct wpa_ssid *ssid = wpa_s->current_ssid;
3556
3557         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3558             ssid->passphrase == NULL)
3559                 return -1;
3560
3561         os_strlcpy(buf, ssid->passphrase, buflen);
3562         return os_strlen(buf);
3563 }
3564
3565
3566 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
3567                                   char *buf, size_t buflen)
3568 {
3569         u64 ref;
3570         int res;
3571         u8 dst_buf[ETH_ALEN], *dst;
3572         struct wpabuf *tlvs;
3573         char *pos;
3574         size_t len;
3575
3576         if (hwaddr_aton(cmd, dst_buf))
3577                 return -1;
3578         dst = dst_buf;
3579         if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
3580             dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
3581                 dst = NULL;
3582         pos = cmd + 17;
3583         if (*pos != ' ')
3584                 return -1;
3585         pos++;
3586
3587         if (os_strncmp(pos, "upnp ", 5) == 0) {
3588                 u8 version;
3589                 pos += 5;
3590                 if (hexstr2bin(pos, &version, 1) < 0)
3591                         return -1;
3592                 pos += 2;
3593                 if (*pos != ' ')
3594                         return -1;
3595                 pos++;
3596                 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
3597 #ifdef CONFIG_WIFI_DISPLAY
3598         } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
3599                 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
3600 #endif /* CONFIG_WIFI_DISPLAY */
3601         } else {
3602                 len = os_strlen(pos);
3603                 if (len & 1)
3604                         return -1;
3605                 len /= 2;
3606                 tlvs = wpabuf_alloc(len);
3607                 if (tlvs == NULL)
3608                         return -1;
3609                 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
3610                         wpabuf_free(tlvs);
3611                         return -1;
3612                 }
3613
3614                 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
3615                 wpabuf_free(tlvs);
3616         }
3617         if (ref == 0)
3618                 return -1;
3619         res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
3620         if (res < 0 || (unsigned) res >= buflen)
3621                 return -1;
3622         return res;
3623 }
3624
3625
3626 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
3627                                          char *cmd)
3628 {
3629         long long unsigned val;
3630         u64 req;
3631         if (sscanf(cmd, "%llx", &val) != 1)
3632                 return -1;
3633         req = val;
3634         return wpas_p2p_sd_cancel_request(wpa_s, req);
3635 }
3636
3637
3638 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
3639 {
3640         int freq;
3641         u8 dst[ETH_ALEN];
3642         u8 dialog_token;
3643         struct wpabuf *resp_tlvs;
3644         char *pos, *pos2;
3645         size_t len;
3646
3647         pos = os_strchr(cmd, ' ');
3648         if (pos == NULL)
3649                 return -1;
3650         *pos++ = '\0';
3651         freq = atoi(cmd);
3652         if (freq == 0)
3653                 return -1;
3654
3655         if (hwaddr_aton(pos, dst))
3656                 return -1;
3657         pos += 17;
3658         if (*pos != ' ')
3659                 return -1;
3660         pos++;
3661
3662         pos2 = os_strchr(pos, ' ');
3663         if (pos2 == NULL)
3664                 return -1;
3665         *pos2++ = '\0';
3666         dialog_token = atoi(pos);
3667
3668         len = os_strlen(pos2);
3669         if (len & 1)
3670                 return -1;
3671         len /= 2;
3672         resp_tlvs = wpabuf_alloc(len);
3673         if (resp_tlvs == NULL)
3674                 return -1;
3675         if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
3676                 wpabuf_free(resp_tlvs);
3677                 return -1;
3678         }
3679
3680         wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
3681         wpabuf_free(resp_tlvs);
3682         return 0;
3683 }
3684
3685
3686 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
3687                                        char *cmd)
3688 {
3689         if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
3690                 return -1;
3691         wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
3692         return 0;
3693 }
3694
3695
3696 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
3697                                         char *cmd)
3698 {
3699         char *pos;
3700         size_t len;
3701         struct wpabuf *query, *resp;
3702
3703         pos = os_strchr(cmd, ' ');
3704         if (pos == NULL)
3705                 return -1;
3706         *pos++ = '\0';
3707
3708         len = os_strlen(cmd);
3709         if (len & 1)
3710                 return -1;
3711         len /= 2;
3712         query = wpabuf_alloc(len);
3713         if (query == NULL)
3714                 return -1;
3715         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
3716                 wpabuf_free(query);
3717                 return -1;
3718         }
3719
3720         len = os_strlen(pos);
3721         if (len & 1) {
3722                 wpabuf_free(query);
3723                 return -1;
3724         }
3725         len /= 2;
3726         resp = wpabuf_alloc(len);
3727         if (resp == NULL) {
3728                 wpabuf_free(query);
3729                 return -1;
3730         }
3731         if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
3732                 wpabuf_free(query);
3733                 wpabuf_free(resp);
3734                 return -1;
3735         }
3736
3737         if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
3738                 wpabuf_free(query);
3739                 wpabuf_free(resp);
3740                 return -1;
3741         }
3742         return 0;
3743 }
3744
3745
3746 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
3747 {
3748         char *pos;
3749         u8 version;
3750
3751         pos = os_strchr(cmd, ' ');
3752         if (pos == NULL)
3753                 return -1;
3754         *pos++ = '\0';
3755
3756         if (hexstr2bin(cmd, &version, 1) < 0)
3757                 return -1;
3758
3759         return wpas_p2p_service_add_upnp(wpa_s, version, pos);
3760 }
3761
3762
3763 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
3764 {
3765         char *pos;
3766
3767         pos = os_strchr(cmd, ' ');
3768         if (pos == NULL)
3769                 return -1;
3770         *pos++ = '\0';
3771
3772         if (os_strcmp(cmd, "bonjour") == 0)
3773                 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
3774         if (os_strcmp(cmd, "upnp") == 0)
3775                 return p2p_ctrl_service_add_upnp(wpa_s, pos);
3776         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
3777         return -1;
3778 }
3779
3780
3781 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
3782                                         char *cmd)
3783 {
3784         size_t len;
3785         struct wpabuf *query;
3786         int ret;
3787
3788         len = os_strlen(cmd);
3789         if (len & 1)
3790                 return -1;
3791         len /= 2;
3792         query = wpabuf_alloc(len);
3793         if (query == NULL)
3794                 return -1;
3795         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
3796                 wpabuf_free(query);
3797                 return -1;
3798         }
3799
3800         ret = wpas_p2p_service_del_bonjour(wpa_s, query);
3801         wpabuf_free(query);
3802         return ret;
3803 }
3804
3805
3806 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
3807 {
3808         char *pos;
3809         u8 version;
3810
3811         pos = os_strchr(cmd, ' ');
3812         if (pos == NULL)
3813                 return -1;
3814         *pos++ = '\0';
3815
3816         if (hexstr2bin(cmd, &version, 1) < 0)
3817                 return -1;
3818
3819         return wpas_p2p_service_del_upnp(wpa_s, version, pos);
3820 }
3821
3822
3823 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
3824 {
3825         char *pos;
3826
3827         pos = os_strchr(cmd, ' ');
3828         if (pos == NULL)
3829                 return -1;
3830         *pos++ = '\0';
3831
3832         if (os_strcmp(cmd, "bonjour") == 0)
3833                 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
3834         if (os_strcmp(cmd, "upnp") == 0)
3835                 return p2p_ctrl_service_del_upnp(wpa_s, pos);
3836         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
3837         return -1;
3838 }
3839
3840
3841 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
3842 {
3843         u8 addr[ETH_ALEN];
3844
3845         /* <addr> */
3846
3847         if (hwaddr_aton(cmd, addr))
3848                 return -1;
3849
3850         return wpas_p2p_reject(wpa_s, addr);
3851 }
3852
3853
3854 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
3855 {
3856         char *pos;
3857         int id;
3858         struct wpa_ssid *ssid;
3859         u8 *_peer = NULL, peer[ETH_ALEN];
3860         int freq = 0;
3861         int ht40;
3862
3863         id = atoi(cmd);
3864         pos = os_strstr(cmd, " peer=");
3865         if (pos) {
3866                 pos += 6;
3867                 if (hwaddr_aton(pos, peer))
3868                         return -1;
3869                 _peer = peer;
3870         }
3871         ssid = wpa_config_get_network(wpa_s->conf, id);
3872         if (ssid == NULL || ssid->disabled != 2) {
3873                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
3874                            "for persistent P2P group",
3875                            id);
3876                 return -1;
3877         }
3878
3879         pos = os_strstr(cmd, " freq=");
3880         if (pos) {
3881                 pos += 6;
3882                 freq = atoi(pos);
3883                 if (freq <= 0)
3884                         return -1;
3885         }
3886
3887         ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
3888
3889         return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40);
3890 }
3891
3892
3893 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
3894 {
3895         char *pos;
3896         u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
3897
3898         pos = os_strstr(cmd, " peer=");
3899         if (!pos)
3900                 return -1;
3901
3902         *pos = '\0';
3903         pos += 6;
3904         if (hwaddr_aton(pos, peer)) {
3905                 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
3906                 return -1;
3907         }
3908
3909         pos = os_strstr(pos, " go_dev_addr=");
3910         if (pos) {
3911                 pos += 13;
3912                 if (hwaddr_aton(pos, go_dev_addr)) {
3913                         wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
3914                                    pos);
3915                         return -1;
3916                 }
3917                 go_dev = go_dev_addr;
3918         }
3919
3920         return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
3921 }
3922
3923
3924 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
3925 {
3926         if (os_strncmp(cmd, "persistent=", 11) == 0)
3927                 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
3928         if (os_strncmp(cmd, "group=", 6) == 0)
3929                 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
3930
3931         return -1;
3932 }
3933
3934
3935 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
3936                                          char *cmd, int freq, int ht40)
3937 {
3938         int id;
3939         struct wpa_ssid *ssid;
3940
3941         id = atoi(cmd);
3942         ssid = wpa_config_get_network(wpa_s->conf, id);
3943         if (ssid == NULL || ssid->disabled != 2) {
3944                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
3945                            "for persistent P2P group",
3946                            id);
3947                 return -1;
3948         }
3949
3950         return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40);
3951 }
3952
3953
3954 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
3955 {
3956         int freq = 0, ht40;
3957         char *pos;
3958
3959         pos = os_strstr(cmd, "freq=");
3960         if (pos)
3961                 freq = atoi(pos + 5);
3962
3963         ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
3964
3965         if (os_strncmp(cmd, "persistent=", 11) == 0)
3966                 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
3967                                                      ht40);
3968         if (os_strcmp(cmd, "persistent") == 0 ||
3969             os_strncmp(cmd, "persistent ", 11) == 0)
3970                 return wpas_p2p_group_add(wpa_s, 1, freq, ht40);
3971         if (os_strncmp(cmd, "freq=", 5) == 0)
3972                 return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
3973         if (ht40)
3974                 return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
3975
3976         wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
3977                    cmd);
3978         return -1;
3979 }
3980
3981
3982 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
3983                          char *buf, size_t buflen)
3984 {
3985         u8 addr[ETH_ALEN], *addr_ptr;
3986         int next, res;
3987         const struct p2p_peer_info *info;
3988         char *pos, *end;
3989         char devtype[WPS_DEV_TYPE_BUFSIZE];
3990         struct wpa_ssid *ssid;
3991         size_t i;
3992
3993         if (!wpa_s->global->p2p)
3994                 return -1;
3995
3996         if (os_strcmp(cmd, "FIRST") == 0) {
3997                 addr_ptr = NULL;
3998                 next = 0;
3999         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4000                 if (hwaddr_aton(cmd + 5, addr) < 0)
4001                         return -1;
4002                 addr_ptr = addr;
4003                 next = 1;
4004         } else {
4005                 if (hwaddr_aton(cmd, addr) < 0)
4006                         return -1;
4007                 addr_ptr = addr;
4008                 next = 0;
4009         }
4010
4011         info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4012         if (info == NULL)
4013                 return -1;
4014
4015         pos = buf;
4016         end = buf + buflen;
4017
4018         res = os_snprintf(pos, end - pos, MACSTR "\n"
4019                           "pri_dev_type=%s\n"
4020                           "device_name=%s\n"
4021                           "manufacturer=%s\n"
4022                           "model_name=%s\n"
4023                           "model_number=%s\n"
4024                           "serial_number=%s\n"
4025                           "config_methods=0x%x\n"
4026                           "dev_capab=0x%x\n"
4027                           "group_capab=0x%x\n"
4028                           "level=%d\n",
4029                           MAC2STR(info->p2p_device_addr),
4030                           wps_dev_type_bin2str(info->pri_dev_type,
4031                                                devtype, sizeof(devtype)),
4032                           info->device_name,
4033                           info->manufacturer,
4034                           info->model_name,
4035                           info->model_number,
4036                           info->serial_number,
4037                           info->config_methods,
4038                           info->dev_capab,
4039                           info->group_capab,
4040                           info->level);
4041         if (res < 0 || res >= end - pos)
4042                 return pos - buf;
4043         pos += res;
4044
4045         for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4046         {
4047                 const u8 *t;
4048                 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4049                 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4050                                   wps_dev_type_bin2str(t, devtype,
4051                                                        sizeof(devtype)));
4052                 if (res < 0 || res >= end - pos)
4053                         return pos - buf;
4054                 pos += res;
4055         }
4056
4057         ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
4058         if (ssid) {
4059                 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
4060                 if (res < 0 || res >= end - pos)
4061                         return pos - buf;
4062                 pos += res;
4063         }
4064
4065         res = p2p_get_peer_info_txt(info, pos, end - pos);
4066         if (res < 0)
4067                 return pos - buf;
4068         pos += res;
4069
4070         return pos - buf;
4071 }
4072
4073
4074 static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
4075                                   const char *param)
4076 {
4077         struct wpa_freq_range *freq = NULL, *n;
4078         unsigned int count = 0, i;
4079         const char *pos, *pos2, *pos3;
4080
4081         if (wpa_s->global->p2p == NULL)
4082                 return -1;
4083
4084         /*
4085          * param includes comma separated frequency range.
4086          * For example: 2412-2432,2462,5000-6000
4087          */
4088         pos = param;
4089         while (pos && pos[0]) {
4090                 n = os_realloc_array(freq, count + 1,
4091                                      sizeof(struct wpa_freq_range));
4092                 if (n == NULL) {
4093                         os_free(freq);
4094                         return -1;
4095                 }
4096                 freq = n;
4097                 freq[count].min = atoi(pos);
4098                 pos2 = os_strchr(pos, '-');
4099                 pos3 = os_strchr(pos, ',');
4100                 if (pos2 && (!pos3 || pos2 < pos3)) {
4101                         pos2++;
4102                         freq[count].max = atoi(pos2);
4103                 } else
4104                         freq[count].max = freq[count].min;
4105                 pos = pos3;
4106                 if (pos)
4107                         pos++;
4108                 count++;
4109         }
4110
4111         for (i = 0; i < count; i++) {
4112                 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
4113                            freq[i].min, freq[i].max);
4114         }
4115
4116         os_free(wpa_s->global->p2p_disallow_freq);
4117         wpa_s->global->p2p_disallow_freq = freq;
4118         wpa_s->global->num_p2p_disallow_freq = count;
4119         wpas_p2p_update_channel_list(wpa_s);
4120         return 0;
4121 }
4122
4123
4124 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
4125 {
4126         char *param;
4127
4128         if (wpa_s->global->p2p == NULL)
4129                 return -1;
4130
4131         param = os_strchr(cmd, ' ');
4132         if (param == NULL)
4133                 return -1;
4134         *param++ = '\0';
4135
4136         if (os_strcmp(cmd, "discoverability") == 0) {
4137                 p2p_set_client_discoverability(wpa_s->global->p2p,
4138                                                atoi(param));
4139                 return 0;
4140         }
4141
4142         if (os_strcmp(cmd, "managed") == 0) {
4143                 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
4144                 return 0;
4145         }
4146
4147         if (os_strcmp(cmd, "listen_channel") == 0) {
4148                 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
4149                                               atoi(param));
4150         }
4151
4152         if (os_strcmp(cmd, "ssid_postfix") == 0) {
4153                 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
4154                                             os_strlen(param));
4155         }
4156
4157         if (os_strcmp(cmd, "noa") == 0) {
4158                 char *pos;
4159                 int count, start, duration;
4160                 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
4161                 count = atoi(param);
4162                 pos = os_strchr(param, ',');
4163                 if (pos == NULL)
4164                         return -1;
4165                 pos++;
4166                 start = atoi(pos);
4167                 pos = os_strchr(pos, ',');
4168                 if (pos == NULL)
4169                         return -1;
4170                 pos++;
4171                 duration = atoi(pos);
4172                 if (count < 0 || count > 255 || start < 0 || duration < 0)
4173                         return -1;
4174                 if (count == 0 && duration > 0)
4175                         return -1;
4176                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
4177                            "start=%d duration=%d", count, start, duration);
4178                 return wpas_p2p_set_noa(wpa_s, count, start, duration);
4179         }
4180
4181         if (os_strcmp(cmd, "ps") == 0)
4182                 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
4183
4184         if (os_strcmp(cmd, "oppps") == 0)
4185                 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
4186
4187         if (os_strcmp(cmd, "ctwindow") == 0)
4188                 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
4189
4190         if (os_strcmp(cmd, "disabled") == 0) {
4191                 wpa_s->global->p2p_disabled = atoi(param);
4192                 wpa_printf(MSG_DEBUG, "P2P functionality %s",
4193                            wpa_s->global->p2p_disabled ?
4194                            "disabled" : "enabled");
4195                 if (wpa_s->global->p2p_disabled) {
4196                         wpas_p2p_stop_find(wpa_s);
4197                         os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4198                         p2p_flush(wpa_s->global->p2p);
4199                 }
4200                 return 0;
4201         }
4202
4203         if (os_strcmp(cmd, "conc_pref") == 0) {
4204                 if (os_strcmp(param, "sta") == 0)
4205                         wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
4206                 else if (os_strcmp(param, "p2p") == 0)
4207                         wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
4208                 else {
4209                         wpa_printf(MSG_INFO, "Invalid conc_pref value");
4210                         return -1;
4211                 }
4212                 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
4213                            "%s", param);
4214                 return 0;
4215         }
4216
4217         if (os_strcmp(cmd, "force_long_sd") == 0) {
4218                 wpa_s->force_long_sd = atoi(param);
4219                 return 0;
4220         }
4221
4222         if (os_strcmp(cmd, "peer_filter") == 0) {
4223                 u8 addr[ETH_ALEN];
4224                 if (hwaddr_aton(param, addr))
4225                         return -1;
4226                 p2p_set_peer_filter(wpa_s->global->p2p, addr);
4227                 return 0;
4228         }
4229
4230         if (os_strcmp(cmd, "cross_connect") == 0)
4231                 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
4232
4233         if (os_strcmp(cmd, "go_apsd") == 0) {
4234                 if (os_strcmp(param, "disable") == 0)
4235                         wpa_s->set_ap_uapsd = 0;
4236                 else {
4237                         wpa_s->set_ap_uapsd = 1;
4238                         wpa_s->ap_uapsd = atoi(param);
4239                 }
4240                 return 0;
4241         }
4242
4243         if (os_strcmp(cmd, "client_apsd") == 0) {
4244                 if (os_strcmp(param, "disable") == 0)
4245                         wpa_s->set_sta_uapsd = 0;
4246                 else {
4247                         int be, bk, vi, vo;
4248                         char *pos;
4249                         /* format: BE,BK,VI,VO;max SP Length */
4250                         be = atoi(param);
4251                         pos = os_strchr(param, ',');
4252                         if (pos == NULL)
4253                                 return -1;
4254                         pos++;
4255                         bk = atoi(pos);
4256                         pos = os_strchr(pos, ',');
4257                         if (pos == NULL)
4258                                 return -1;
4259                         pos++;
4260                         vi = atoi(pos);
4261                         pos = os_strchr(pos, ',');
4262                         if (pos == NULL)
4263                                 return -1;
4264                         pos++;
4265                         vo = atoi(pos);
4266                         /* ignore max SP Length for now */
4267
4268                         wpa_s->set_sta_uapsd = 1;
4269                         wpa_s->sta_uapsd = 0;
4270                         if (be)
4271                                 wpa_s->sta_uapsd |= BIT(0);
4272                         if (bk)
4273                                 wpa_s->sta_uapsd |= BIT(1);
4274                         if (vi)
4275                                 wpa_s->sta_uapsd |= BIT(2);
4276                         if (vo)
4277                                 wpa_s->sta_uapsd |= BIT(3);
4278                 }
4279                 return 0;
4280         }
4281
4282         if (os_strcmp(cmd, "disallow_freq") == 0)
4283                 return p2p_ctrl_disallow_freq(wpa_s, param);
4284
4285         if (os_strcmp(cmd, "disc_int") == 0) {
4286                 int min_disc_int, max_disc_int, max_disc_tu;
4287                 char *pos;
4288
4289                 pos = param;
4290
4291                 min_disc_int = atoi(pos);
4292                 pos = os_strchr(pos, ' ');
4293                 if (pos == NULL)
4294                         return -1;
4295                 *pos++ = '\0';
4296
4297                 max_disc_int = atoi(pos);
4298                 pos = os_strchr(pos, ' ');
4299                 if (pos == NULL)
4300                         return -1;
4301                 *pos++ = '\0';
4302
4303                 max_disc_tu = atoi(pos);
4304
4305                 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
4306                                         max_disc_int, max_disc_tu);
4307         }
4308
4309         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
4310                    cmd);
4311
4312         return -1;
4313 }
4314
4315
4316 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
4317 {
4318         char *pos, *pos2;
4319         unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
4320
4321         if (cmd[0]) {
4322                 pos = os_strchr(cmd, ' ');
4323                 if (pos == NULL)
4324                         return -1;
4325                 *pos++ = '\0';
4326                 dur1 = atoi(cmd);
4327
4328                 pos2 = os_strchr(pos, ' ');
4329                 if (pos2)
4330                         *pos2++ = '\0';
4331                 int1 = atoi(pos);
4332         } else
4333                 pos2 = NULL;
4334
4335         if (pos2) {
4336                 pos = os_strchr(pos2, ' ');
4337                 if (pos == NULL)
4338                         return -1;
4339                 *pos++ = '\0';
4340                 dur2 = atoi(pos2);
4341                 int2 = atoi(pos);
4342         }
4343
4344         return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
4345 }
4346
4347
4348 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
4349 {
4350         char *pos;
4351         unsigned int period = 0, interval = 0;
4352
4353         if (cmd[0]) {
4354                 pos = os_strchr(cmd, ' ');
4355                 if (pos == NULL)
4356                         return -1;
4357                 *pos++ = '\0';
4358                 period = atoi(cmd);
4359                 interval = atoi(pos);
4360         }
4361
4362         return wpas_p2p_ext_listen(wpa_s, period, interval);
4363 }
4364
4365 #endif /* CONFIG_P2P */
4366
4367
4368 #ifdef CONFIG_INTERWORKING
4369 static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
4370 {
4371         u8 bssid[ETH_ALEN];
4372         struct wpa_bss *bss;
4373
4374         if (hwaddr_aton(dst, bssid)) {
4375                 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
4376                 return -1;
4377         }
4378
4379         bss = wpa_bss_get_bssid(wpa_s, bssid);
4380         if (bss == NULL) {
4381                 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
4382                            MAC2STR(bssid));
4383                 return -1;
4384         }
4385
4386         return interworking_connect(wpa_s, bss);
4387 }
4388
4389
4390 static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
4391 {
4392         u8 dst_addr[ETH_ALEN];
4393         int used;
4394         char *pos;
4395 #define MAX_ANQP_INFO_ID 100
4396         u16 id[MAX_ANQP_INFO_ID];
4397         size_t num_id = 0;
4398
4399         used = hwaddr_aton2(dst, dst_addr);
4400         if (used < 0)
4401                 return -1;
4402         pos = dst + used;
4403         while (num_id < MAX_ANQP_INFO_ID) {
4404                 id[num_id] = atoi(pos);
4405                 if (id[num_id])
4406                         num_id++;
4407                 pos = os_strchr(pos + 1, ',');
4408                 if (pos == NULL)
4409                         break;
4410                 pos++;
4411         }
4412
4413         if (num_id == 0)
4414                 return -1;
4415
4416         return anqp_send_req(wpa_s, dst_addr, id, num_id);
4417 }
4418
4419
4420 static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
4421 {
4422         u8 dst_addr[ETH_ALEN];
4423         struct wpabuf *advproto, *query = NULL;
4424         int used, ret = -1;
4425         char *pos, *end;
4426         size_t len;
4427
4428         used = hwaddr_aton2(cmd, dst_addr);
4429         if (used < 0)
4430                 return -1;
4431
4432         pos = cmd + used;
4433         while (*pos == ' ')
4434                 pos++;
4435
4436         /* Advertisement Protocol ID */
4437         end = os_strchr(pos, ' ');
4438         if (end)
4439                 len = end - pos;
4440         else
4441                 len = os_strlen(pos);
4442         if (len & 0x01)
4443                 return -1;
4444         len /= 2;
4445         if (len == 0)
4446                 return -1;
4447         advproto = wpabuf_alloc(len);
4448         if (advproto == NULL)
4449                 return -1;
4450         if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
4451                 goto fail;
4452
4453         if (end) {
4454                 /* Optional Query Request */
4455                 pos = end + 1;
4456                 while (*pos == ' ')
4457                         pos++;
4458
4459                 len = os_strlen(pos);
4460                 if (len) {
4461                         if (len & 0x01)
4462                                 goto fail;
4463                         len /= 2;
4464                         if (len == 0)
4465                                 goto fail;
4466                         query = wpabuf_alloc(len);
4467                         if (query == NULL)
4468                                 goto fail;
4469                         if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
4470                                 goto fail;
4471                 }
4472         }
4473
4474         ret = gas_send_request(wpa_s, dst_addr, advproto, query);
4475
4476 fail:
4477         wpabuf_free(advproto);
4478         wpabuf_free(query);
4479
4480         return ret;
4481 }
4482
4483
4484 static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
4485                             size_t buflen)
4486 {
4487         u8 addr[ETH_ALEN];
4488         int dialog_token;
4489         int used;
4490         char *pos;
4491         size_t resp_len, start, requested_len;
4492
4493         if (!wpa_s->last_gas_resp)
4494                 return -1;
4495
4496         used = hwaddr_aton2(cmd, addr);
4497         if (used < 0)
4498                 return -1;
4499
4500         pos = cmd + used;
4501         while (*pos == ' ')
4502                 pos++;
4503         dialog_token = atoi(pos);
4504
4505         if (os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) != 0 ||
4506             dialog_token != wpa_s->last_gas_dialog_token)
4507                 return -1;
4508
4509         resp_len = wpabuf_len(wpa_s->last_gas_resp);
4510         start = 0;
4511         requested_len = resp_len;
4512
4513         pos = os_strchr(pos, ' ');
4514         if (pos) {
4515                 start = atoi(pos);
4516                 if (start > resp_len)
4517                         return os_snprintf(buf, buflen, "FAIL-Invalid range");
4518                 pos = os_strchr(pos, ',');
4519                 if (pos == NULL)
4520                         return -1;
4521                 pos++;
4522                 requested_len = atoi(pos);
4523                 if (start + requested_len > resp_len)
4524                         return os_snprintf(buf, buflen, "FAIL-Invalid range");
4525         }
4526
4527         if (requested_len * 2 + 1 > buflen)
4528                 return os_snprintf(buf, buflen, "FAIL-Too long response");
4529
4530         return wpa_snprintf_hex(buf, buflen,
4531                                 wpabuf_head_u8(wpa_s->last_gas_resp) + start,
4532                                 requested_len);
4533 }
4534 #endif /* CONFIG_INTERWORKING */
4535
4536
4537 #ifdef CONFIG_HS20
4538
4539 static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
4540 {
4541         u8 dst_addr[ETH_ALEN];
4542         int used;
4543         char *pos;
4544         u32 subtypes = 0;
4545
4546         used = hwaddr_aton2(dst, dst_addr);
4547         if (used < 0)
4548                 return -1;
4549         pos = dst + used;
4550         for (;;) {
4551                 int num = atoi(pos);
4552                 if (num <= 0 || num > 31)
4553                         return -1;
4554                 subtypes |= BIT(num);
4555                 pos = os_strchr(pos + 1, ',');
4556                 if (pos == NULL)
4557                         break;
4558                 pos++;
4559         }
4560
4561         if (subtypes == 0)
4562                 return -1;
4563
4564         return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
4565 }
4566
4567
4568 static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4569                                     const u8 *addr, const char *realm)
4570 {
4571         u8 *buf;
4572         size_t rlen, len;
4573         int ret;
4574
4575         rlen = os_strlen(realm);
4576         len = 3 + rlen;
4577         buf = os_malloc(len);
4578         if (buf == NULL)
4579                 return -1;
4580         buf[0] = 1; /* NAI Home Realm Count */
4581         buf[1] = 0; /* Formatted in accordance with RFC 4282 */
4582         buf[2] = rlen;
4583         os_memcpy(buf + 3, realm, rlen);
4584
4585         ret = hs20_anqp_send_req(wpa_s, addr,
4586                                  BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4587                                  buf, len);
4588
4589         os_free(buf);
4590
4591         return ret;
4592 }
4593
4594
4595 static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4596                                         char *dst)
4597 {
4598         struct wpa_cred *cred = wpa_s->conf->cred;
4599         u8 dst_addr[ETH_ALEN];
4600         int used;
4601         u8 *buf;
4602         size_t len;
4603         int ret;
4604
4605         used = hwaddr_aton2(dst, dst_addr);
4606         if (used < 0)
4607                 return -1;
4608
4609         while (dst[used] == ' ')
4610                 used++;
4611         if (os_strncmp(dst + used, "realm=", 6) == 0)
4612                 return hs20_nai_home_realm_list(wpa_s, dst_addr,
4613                                                 dst + used + 6);
4614
4615         len = os_strlen(dst + used);
4616
4617         if (len == 0 && cred && cred->realm)
4618                 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
4619
4620         if (len % 1)
4621                 return -1;
4622         len /= 2;
4623         buf = os_malloc(len);
4624         if (buf == NULL)
4625                 return -1;
4626         if (hexstr2bin(dst + used, buf, len) < 0) {
4627                 os_free(buf);
4628                 return -1;
4629         }
4630
4631         ret = hs20_anqp_send_req(wpa_s, dst_addr,
4632                                  BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4633                                  buf, len);
4634         os_free(buf);
4635
4636         return ret;
4637 }
4638
4639 #endif /* CONFIG_HS20 */
4640
4641
4642 static int wpa_supplicant_ctrl_iface_sta_autoconnect(
4643         struct wpa_supplicant *wpa_s, char *cmd)
4644 {
4645         wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
4646         return 0;
4647 }
4648
4649
4650 #ifdef CONFIG_AUTOSCAN
4651
4652 static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
4653                                               char *cmd)
4654 {
4655         enum wpa_states state = wpa_s->wpa_state;
4656         char *new_params = NULL;
4657
4658         if (os_strlen(cmd) > 0) {
4659                 new_params = os_strdup(cmd);
4660                 if (new_params == NULL)
4661                         return -1;
4662         }
4663
4664         os_free(wpa_s->conf->autoscan);
4665         wpa_s->conf->autoscan = new_params;
4666
4667         if (wpa_s->conf->autoscan == NULL)
4668                 autoscan_deinit(wpa_s);
4669         else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
4670                 autoscan_init(wpa_s, 1);
4671         else if (state == WPA_SCANNING)
4672                 wpa_supplicant_reinit_autoscan(wpa_s);
4673
4674         return 0;
4675 }
4676
4677 #endif /* CONFIG_AUTOSCAN */
4678
4679
4680 #ifdef CONFIG_WNM
4681
4682 static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
4683 {
4684         int enter;
4685         int intval = 0;
4686         char *pos;
4687         int ret;
4688         struct wpabuf *tfs_req = NULL;
4689
4690         if (os_strncmp(cmd, "enter", 5) == 0)
4691                 enter = 1;
4692         else if (os_strncmp(cmd, "exit", 4) == 0)
4693                 enter = 0;
4694         else
4695                 return -1;
4696
4697         pos = os_strstr(cmd, " interval=");
4698         if (pos)
4699                 intval = atoi(pos + 10);
4700
4701         pos = os_strstr(cmd, " tfs_req=");
4702         if (pos) {
4703                 char *end;
4704                 size_t len;
4705                 pos += 9;
4706                 end = os_strchr(pos, ' ');
4707                 if (end)
4708                         len = end - pos;
4709                 else
4710                         len = os_strlen(pos);
4711                 if (len & 1)
4712                         return -1;
4713                 len /= 2;
4714                 tfs_req = wpabuf_alloc(len);
4715                 if (tfs_req == NULL)
4716                         return -1;
4717                 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
4718                         wpabuf_free(tfs_req);
4719                         return -1;
4720                 }
4721         }
4722
4723         ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
4724                                            WNM_SLEEP_MODE_EXIT, intval,
4725                                            tfs_req);
4726         wpabuf_free(tfs_req);
4727
4728         return ret;
4729 }
4730
4731 #endif /* CONFIG_WNM */
4732
4733
4734 static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
4735                                       size_t buflen)
4736 {
4737         struct wpa_signal_info si;
4738         int ret;
4739
4740         ret = wpa_drv_signal_poll(wpa_s, &si);
4741         if (ret)
4742                 return -1;
4743
4744         ret = os_snprintf(buf, buflen, "RSSI=%d\nLINKSPEED=%d\n"
4745                           "NOISE=%d\nFREQUENCY=%u\n",
4746                           si.current_signal, si.current_txrate / 1000,
4747                           si.current_noise, si.frequency);
4748         if (ret < 0 || (unsigned int) ret > buflen)
4749                 return -1;
4750         return ret;
4751 }
4752
4753
4754 static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
4755                                       size_t buflen)
4756 {
4757         struct hostap_sta_driver_data sta;
4758         int ret;
4759
4760         ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
4761         if (ret)
4762                 return -1;
4763
4764         ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
4765                           sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
4766         if (ret < 0 || (size_t) ret > buflen)
4767                 return -1;
4768         return ret;
4769 }
4770
4771
4772 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
4773                                          char *buf, size_t *resp_len)
4774 {
4775         char *reply;
4776         const int reply_size = 4096;
4777         int ctrl_rsp = 0;
4778         int reply_len;
4779
4780         if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
4781             os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
4782             os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
4783             os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
4784                 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
4785                                       (const u8 *) buf, os_strlen(buf));
4786         } else {
4787                 int level = MSG_DEBUG;
4788                 if (os_strcmp(buf, "PING") == 0)
4789                         level = MSG_EXCESSIVE;
4790                 wpa_hexdump_ascii(level, "RX ctrl_iface",
4791                                   (const u8 *) buf, os_strlen(buf));
4792                 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
4793         }
4794
4795         reply = os_malloc(reply_size);
4796         if (reply == NULL) {
4797                 *resp_len = 1;
4798                 return NULL;
4799         }
4800
4801         os_memcpy(reply, "OK\n", 3);
4802         reply_len = 3;
4803
4804         if (os_strcmp(buf, "PING") == 0) {
4805                 os_memcpy(reply, "PONG\n", 5);
4806                 reply_len = 5;
4807         } else if (os_strcmp(buf, "IFNAME") == 0) {
4808                 reply_len = os_strlen(wpa_s->ifname);
4809                 os_memcpy(reply, wpa_s->ifname, reply_len);
4810         } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4811                 if (wpa_debug_reopen_file() < 0)
4812                         reply_len = -1;
4813         } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
4814                 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
4815         } else if (os_strcmp(buf, "MIB") == 0) {
4816                 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
4817                 if (reply_len >= 0) {
4818                         int res;
4819                         res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
4820                                                reply_size - reply_len);
4821                         if (res < 0)
4822                                 reply_len = -1;
4823                         else
4824                                 reply_len += res;
4825                 }
4826         } else if (os_strncmp(buf, "STATUS", 6) == 0) {
4827                 reply_len = wpa_supplicant_ctrl_iface_status(
4828                         wpa_s, buf + 6, reply, reply_size);
4829         } else if (os_strcmp(buf, "PMKSA") == 0) {
4830                 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
4831                                                     reply_size);
4832         } else if (os_strncmp(buf, "SET ", 4) == 0) {
4833                 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
4834                         reply_len = -1;
4835         } else if (os_strncmp(buf, "GET ", 4) == 0) {
4836                 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
4837                                                           reply, reply_size);
4838         } else if (os_strcmp(buf, "LOGON") == 0) {
4839                 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
4840         } else if (os_strcmp(buf, "LOGOFF") == 0) {
4841                 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
4842         } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
4843                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4844                         reply_len = -1;
4845                 else
4846                         wpas_request_connection(wpa_s);
4847         } else if (os_strcmp(buf, "RECONNECT") == 0) {
4848                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4849                         reply_len = -1;
4850                 else if (wpa_s->disconnected)
4851                         wpas_request_connection(wpa_s);
4852 #ifdef IEEE8021X_EAPOL
4853         } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
4854                 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
4855                         reply_len = -1;
4856 #endif /* IEEE8021X_EAPOL */
4857 #ifdef CONFIG_PEERKEY
4858         } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
4859                 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
4860                         reply_len = -1;
4861 #endif /* CONFIG_PEERKEY */
4862 #ifdef CONFIG_IEEE80211R
4863         } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
4864                 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
4865                         reply_len = -1;
4866 #endif /* CONFIG_IEEE80211R */
4867 #ifdef CONFIG_WPS
4868         } else if (os_strcmp(buf, "WPS_PBC") == 0) {
4869                 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
4870                 if (res == -2) {
4871                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4872                         reply_len = 17;
4873                 } else if (res)
4874                         reply_len = -1;
4875         } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
4876                 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
4877                 if (res == -2) {
4878                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4879                         reply_len = 17;
4880                 } else if (res)
4881                         reply_len = -1;
4882         } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
4883                 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
4884                                                               reply,
4885                                                               reply_size);
4886         } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
4887                 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
4888                         wpa_s, buf + 14, reply, reply_size);
4889         } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
4890                 if (wpas_wps_cancel(wpa_s))
4891                         reply_len = -1;
4892 #ifdef CONFIG_WPS_NFC
4893         } else if (os_strcmp(buf, "WPS_NFC") == 0) {
4894                 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
4895                         reply_len = -1;
4896         } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
4897                 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
4898                         reply_len = -1;
4899         } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
4900                 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
4901                         wpa_s, buf + 14, reply, reply_size);
4902         } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
4903                 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
4904                                                                buf + 17))
4905                         reply_len = -1;
4906         } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
4907                 reply_len = wpas_ctrl_nfc_get_handover_req(
4908                         wpa_s, buf + 21, reply, reply_size);
4909         } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
4910                 reply_len = wpas_ctrl_nfc_get_handover_sel(
4911                         wpa_s, buf + 21, reply, reply_size);
4912         } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
4913                 reply_len = wpas_ctrl_nfc_rx_handover_req(
4914                         wpa_s, buf + 20, reply, reply_size);
4915         } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
4916                 if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
4917                         reply_len = -1;
4918 #endif /* CONFIG_WPS_NFC */
4919         } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
4920                 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
4921                         reply_len = -1;
4922 #ifdef CONFIG_AP
4923         } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
4924                 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
4925                         wpa_s, buf + 11, reply, reply_size);
4926 #endif /* CONFIG_AP */
4927 #ifdef CONFIG_WPS_ER
4928         } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
4929                 if (wpas_wps_er_start(wpa_s, NULL))
4930                         reply_len = -1;
4931         } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
4932                 if (wpas_wps_er_start(wpa_s, buf + 13))
4933                         reply_len = -1;
4934         } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
4935                 if (wpas_wps_er_stop(wpa_s))
4936                         reply_len = -1;
4937         } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
4938                 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
4939                         reply_len = -1;
4940         } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
4941                 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
4942                 if (ret == -2) {
4943                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4944                         reply_len = 17;
4945                 } else if (ret == -3) {
4946                         os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
4947                         reply_len = 18;
4948                 } else if (ret == -4) {
4949                         os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
4950                         reply_len = 20;
4951                 } else if (ret)
4952                         reply_len = -1;
4953         } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
4954                 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
4955                         reply_len = -1;
4956         } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
4957                 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
4958                                                                 buf + 18))
4959                         reply_len = -1;
4960         } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
4961                 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
4962                         reply_len = -1;
4963 #ifdef CONFIG_WPS_NFC
4964         } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
4965                 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
4966                         wpa_s, buf + 24, reply, reply_size);
4967 #endif /* CONFIG_WPS_NFC */
4968 #endif /* CONFIG_WPS_ER */
4969 #endif /* CONFIG_WPS */
4970 #ifdef CONFIG_IBSS_RSN
4971         } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
4972                 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
4973                         reply_len = -1;
4974 #endif /* CONFIG_IBSS_RSN */
4975 #ifdef CONFIG_P2P
4976         } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
4977                 if (p2p_ctrl_find(wpa_s, buf + 9))
4978                         reply_len = -1;
4979         } else if (os_strcmp(buf, "P2P_FIND") == 0) {
4980                 if (p2p_ctrl_find(wpa_s, ""))
4981                         reply_len = -1;
4982         } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
4983                 wpas_p2p_stop_find(wpa_s);
4984         } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
4985                 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
4986                                              reply_size);
4987         } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
4988                 if (p2p_ctrl_listen(wpa_s, buf + 11))
4989                         reply_len = -1;
4990         } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
4991                 if (p2p_ctrl_listen(wpa_s, ""))
4992                         reply_len = -1;
4993         } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
4994                 if (wpas_p2p_group_remove(wpa_s, buf + 17))
4995                         reply_len = -1;
4996         } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
4997                 if (wpas_p2p_group_add(wpa_s, 0, 0, 0))
4998                         reply_len = -1;
4999         } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
5000                 if (p2p_ctrl_group_add(wpa_s, buf + 14))
5001                         reply_len = -1;
5002         } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
5003                 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
5004                         reply_len = -1;
5005         } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
5006                 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
5007         } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
5008                 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
5009                                                    reply_size);
5010         } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
5011                 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
5012                         reply_len = -1;
5013         } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
5014                 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
5015                         reply_len = -1;
5016         } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
5017                 wpas_p2p_sd_service_update(wpa_s);
5018         } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
5019                 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
5020                         reply_len = -1;
5021         } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
5022                 wpas_p2p_service_flush(wpa_s);
5023         } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
5024                 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
5025                         reply_len = -1;
5026         } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
5027                 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
5028                         reply_len = -1;
5029         } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
5030                 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
5031                         reply_len = -1;
5032         } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
5033                 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
5034                         reply_len = -1;
5035         } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
5036                 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
5037                                               reply_size);
5038         } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
5039                 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
5040                         reply_len = -1;
5041         } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
5042                 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5043                 wpa_s->force_long_sd = 0;
5044                 if (wpa_s->global->p2p)
5045                         p2p_flush(wpa_s->global->p2p);
5046         } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
5047                 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
5048                         reply_len = -1;
5049         } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
5050                 if (wpas_p2p_cancel(wpa_s))
5051                         reply_len = -1;
5052         } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
5053                 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
5054                         reply_len = -1;
5055         } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
5056                 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
5057                         reply_len = -1;
5058         } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
5059                 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
5060                         reply_len = -1;
5061         } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
5062                 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
5063                         reply_len = -1;
5064 #endif /* CONFIG_P2P */
5065 #ifdef CONFIG_WIFI_DISPLAY
5066         } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
5067                 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
5068                         reply_len = -1;
5069         } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
5070                 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
5071                                                      reply, reply_size);
5072 #endif /* CONFIG_WIFI_DISPLAY */
5073 #ifdef CONFIG_INTERWORKING
5074         } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
5075                 if (interworking_fetch_anqp(wpa_s) < 0)
5076                         reply_len = -1;
5077         } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
5078                 interworking_stop_fetch_anqp(wpa_s);
5079         } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
5080                 if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
5081                                         NULL) < 0)
5082                         reply_len = -1;
5083         } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
5084                 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
5085                         reply_len = -1;
5086         } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
5087                 if (get_anqp(wpa_s, buf + 9) < 0)
5088                         reply_len = -1;
5089         } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
5090                 if (gas_request(wpa_s, buf + 12) < 0)
5091                         reply_len = -1;
5092         } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
5093                 reply_len = gas_response_get(wpa_s, buf + 17, reply,
5094                                              reply_size);
5095 #endif /* CONFIG_INTERWORKING */
5096 #ifdef CONFIG_HS20
5097         } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
5098                 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
5099                         reply_len = -1;
5100         } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
5101                 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
5102                         reply_len = -1;
5103 #endif /* CONFIG_HS20 */
5104         } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
5105         {
5106                 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
5107                             wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
5108                         reply_len = -1;
5109                 else
5110                         ctrl_rsp = 1;
5111         } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
5112                 if (wpa_supplicant_reload_configuration(wpa_s))
5113                         reply_len = -1;
5114         } else if (os_strcmp(buf, "TERMINATE") == 0) {
5115                 wpa_supplicant_terminate_proc(wpa_s->global);
5116         } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
5117                 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
5118                         reply_len = -1;
5119         } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
5120                 reply_len = wpa_supplicant_ctrl_iface_blacklist(
5121                         wpa_s, buf + 9, reply, reply_size);
5122         } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
5123                 reply_len = wpa_supplicant_ctrl_iface_log_level(
5124                         wpa_s, buf + 9, reply, reply_size);
5125         } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
5126                 reply_len = wpa_supplicant_ctrl_iface_list_networks(
5127                         wpa_s, reply, reply_size);
5128         } else if (os_strcmp(buf, "DISCONNECT") == 0) {
5129 #ifdef CONFIG_SME
5130                 wpa_s->sme.prev_bssid_set = 0;
5131 #endif /* CONFIG_SME */
5132                 wpa_s->reassociate = 0;
5133                 wpa_s->disconnected = 1;
5134                 wpa_supplicant_cancel_sched_scan(wpa_s);
5135                 wpa_supplicant_cancel_scan(wpa_s);
5136                 wpa_supplicant_deauthenticate(wpa_s,
5137                                               WLAN_REASON_DEAUTH_LEAVING);
5138         } else if (os_strcmp(buf, "SCAN") == 0) {
5139                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5140                         reply_len = -1;
5141                 else {
5142                         if (!wpa_s->sched_scanning && !wpa_s->scanning &&
5143                             ((wpa_s->wpa_state <= WPA_SCANNING) ||
5144                              (wpa_s->wpa_state == WPA_COMPLETED))) {
5145                                 wpa_s->normal_scans = 0;
5146                                 wpa_s->scan_req = MANUAL_SCAN_REQ;
5147                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
5148                         } else if (wpa_s->sched_scanning) {
5149                                 wpa_printf(MSG_DEBUG, "Stop ongoing "
5150                                            "sched_scan to allow requested "
5151                                            "full scan to proceed");
5152                                 wpa_supplicant_cancel_sched_scan(wpa_s);
5153                                 wpa_s->scan_req = MANUAL_SCAN_REQ;
5154                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
5155                         } else {
5156                                 wpa_printf(MSG_DEBUG, "Ongoing scan action - "
5157                                            "reject new request");
5158                                 reply_len = os_snprintf(reply, reply_size,
5159                                                         "FAIL-BUSY\n");
5160                         }
5161                 }
5162         } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
5163                 reply_len = wpa_supplicant_ctrl_iface_scan_results(
5164                         wpa_s, reply, reply_size);
5165         } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
5166                 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
5167                         reply_len = -1;
5168         } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
5169                 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
5170                         reply_len = -1;
5171         } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
5172                 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
5173                         reply_len = -1;
5174         } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
5175                 reply_len = wpa_supplicant_ctrl_iface_add_network(
5176                         wpa_s, reply, reply_size);
5177         } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
5178                 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
5179                         reply_len = -1;
5180         } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
5181                 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
5182                         reply_len = -1;
5183         } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
5184                 reply_len = wpa_supplicant_ctrl_iface_get_network(
5185                         wpa_s, buf + 12, reply, reply_size);
5186         } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
5187                 reply_len = wpa_supplicant_ctrl_iface_list_creds(
5188                         wpa_s, reply, reply_size);
5189         } else if (os_strcmp(buf, "ADD_CRED") == 0) {
5190                 reply_len = wpa_supplicant_ctrl_iface_add_cred(
5191                         wpa_s, reply, reply_size);
5192         } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
5193                 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
5194                         reply_len = -1;
5195         } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
5196                 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
5197                         reply_len = -1;
5198 #ifndef CONFIG_NO_CONFIG_WRITE
5199         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
5200                 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
5201                         reply_len = -1;
5202 #endif /* CONFIG_NO_CONFIG_WRITE */
5203         } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
5204                 reply_len = wpa_supplicant_ctrl_iface_get_capability(
5205                         wpa_s, buf + 15, reply, reply_size);
5206         } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
5207                 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
5208                         reply_len = -1;
5209         } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
5210                 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
5211                         reply_len = -1;
5212         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
5213                 reply_len = wpa_supplicant_global_iface_list(
5214                         wpa_s->global, reply, reply_size);
5215         } else if (os_strcmp(buf, "INTERFACES") == 0) {
5216                 reply_len = wpa_supplicant_global_iface_interfaces(
5217                         wpa_s->global, reply, reply_size);
5218         } else if (os_strncmp(buf, "BSS ", 4) == 0) {
5219                 reply_len = wpa_supplicant_ctrl_iface_bss(
5220                         wpa_s, buf + 4, reply, reply_size);
5221 #ifdef CONFIG_AP
5222         } else if (os_strcmp(buf, "STA-FIRST") == 0) {
5223                 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
5224         } else if (os_strncmp(buf, "STA ", 4) == 0) {
5225                 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
5226                                               reply_size);
5227         } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
5228                 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
5229                                                    reply_size);
5230         } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
5231                 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
5232                         reply_len = -1;
5233         } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
5234                 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
5235                         reply_len = -1;
5236 #endif /* CONFIG_AP */
5237         } else if (os_strcmp(buf, "SUSPEND") == 0) {
5238                 wpas_notify_suspend(wpa_s->global);
5239         } else if (os_strcmp(buf, "RESUME") == 0) {
5240                 wpas_notify_resume(wpa_s->global);
5241         } else if (os_strcmp(buf, "DROP_SA") == 0) {
5242                 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
5243         } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
5244                 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
5245                         reply_len = -1;
5246         } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
5247                 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
5248                         reply_len = -1;
5249         } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
5250                 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
5251                         reply_len = -1;
5252         } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
5253                 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
5254                                                                buf + 17))
5255                         reply_len = -1;
5256         } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
5257                 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
5258                         reply_len = -1;
5259 #ifdef CONFIG_TDLS
5260         } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
5261                 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
5262                         reply_len = -1;
5263         } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
5264                 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
5265                         reply_len = -1;
5266         } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
5267                 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
5268                         reply_len = -1;
5269 #endif /* CONFIG_TDLS */
5270         } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
5271                 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
5272                                                        reply_size);
5273         } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
5274                 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
5275                                                        reply_size);
5276 #ifdef CONFIG_AUTOSCAN
5277         } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
5278                 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
5279                         reply_len = -1;
5280 #endif /* CONFIG_AUTOSCAN */
5281         } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
5282                 pmksa_cache_clear_current(wpa_s->wpa);
5283                 eapol_sm_request_reauth(wpa_s->eapol);
5284 #ifdef CONFIG_WNM
5285         } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
5286                 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
5287                         reply_len = -1;
5288 #endif /* CONFIG_WNM */
5289         } else {
5290                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
5291                 reply_len = 16;
5292         }
5293
5294         if (reply_len < 0) {
5295                 os_memcpy(reply, "FAIL\n", 5);
5296                 reply_len = 5;
5297         }
5298
5299         if (ctrl_rsp)
5300                 eapol_sm_notify_ctrl_response(wpa_s->eapol);
5301
5302         *resp_len = reply_len;
5303         return reply;
5304 }
5305
5306
5307 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
5308                                            char *cmd)
5309 {
5310         struct wpa_interface iface;
5311         char *pos;
5312
5313         /*
5314          * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
5315          * TAB<bridge_ifname>
5316          */
5317         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
5318
5319         os_memset(&iface, 0, sizeof(iface));
5320
5321         do {
5322                 iface.ifname = pos = cmd;
5323                 pos = os_strchr(pos, '\t');
5324                 if (pos)
5325                         *pos++ = '\0';
5326                 if (iface.ifname[0] == '\0')
5327                         return -1;
5328                 if (pos == NULL)
5329                         break;
5330
5331                 iface.confname = pos;
5332                 pos = os_strchr(pos, '\t');
5333                 if (pos)
5334                         *pos++ = '\0';
5335                 if (iface.confname[0] == '\0')
5336                         iface.confname = NULL;
5337                 if (pos == NULL)
5338                         break;
5339
5340                 iface.driver = pos;
5341                 pos = os_strchr(pos, '\t');
5342                 if (pos)
5343                         *pos++ = '\0';
5344                 if (iface.driver[0] == '\0')
5345                         iface.driver = NULL;
5346                 if (pos == NULL)
5347                         break;
5348
5349                 iface.ctrl_interface = pos;
5350                 pos = os_strchr(pos, '\t');
5351                 if (pos)
5352                         *pos++ = '\0';
5353                 if (iface.ctrl_interface[0] == '\0')
5354                         iface.ctrl_interface = NULL;
5355                 if (pos == NULL)
5356                         break;
5357
5358                 iface.driver_param = pos;
5359                 pos = os_strchr(pos, '\t');
5360                 if (pos)
5361                         *pos++ = '\0';
5362                 if (iface.driver_param[0] == '\0')
5363                         iface.driver_param = NULL;
5364                 if (pos == NULL)
5365                         break;
5366
5367                 iface.bridge_ifname = pos;
5368                 pos = os_strchr(pos, '\t');
5369                 if (pos)
5370                         *pos++ = '\0';
5371                 if (iface.bridge_ifname[0] == '\0')
5372                         iface.bridge_ifname = NULL;
5373                 if (pos == NULL)
5374                         break;
5375         } while (0);
5376
5377         if (wpa_supplicant_get_iface(global, iface.ifname))
5378                 return -1;
5379
5380         return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
5381 }
5382
5383
5384 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
5385                                               char *cmd)
5386 {
5387         struct wpa_supplicant *wpa_s;
5388
5389         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
5390
5391         wpa_s = wpa_supplicant_get_iface(global, cmd);
5392         if (wpa_s == NULL)
5393                 return -1;
5394         return wpa_supplicant_remove_iface(global, wpa_s, 0);
5395 }
5396
5397
5398 static void wpa_free_iface_info(struct wpa_interface_info *iface)
5399 {
5400         struct wpa_interface_info *prev;
5401
5402         while (iface) {
5403                 prev = iface;
5404                 iface = iface->next;
5405
5406                 os_free(prev->ifname);
5407                 os_free(prev->desc);
5408                 os_free(prev);
5409         }
5410 }
5411
5412
5413 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
5414                                             char *buf, int len)
5415 {
5416         int i, res;
5417         struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
5418         char *pos, *end;
5419
5420         for (i = 0; wpa_drivers[i]; i++) {
5421                 struct wpa_driver_ops *drv = wpa_drivers[i];
5422                 if (drv->get_interfaces == NULL)
5423                         continue;
5424                 tmp = drv->get_interfaces(global->drv_priv[i]);
5425                 if (tmp == NULL)
5426                         continue;
5427
5428                 if (last == NULL)
5429                         iface = last = tmp;
5430                 else
5431                         last->next = tmp;
5432                 while (last->next)
5433                         last = last->next;
5434         }
5435
5436         pos = buf;
5437         end = buf + len;
5438         for (tmp = iface; tmp; tmp = tmp->next) {
5439                 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
5440                                   tmp->drv_name, tmp->ifname,
5441                                   tmp->desc ? tmp->desc : "");
5442                 if (res < 0 || res >= end - pos) {
5443                         *pos = '\0';
5444                         break;
5445                 }
5446                 pos += res;
5447         }
5448
5449         wpa_free_iface_info(iface);
5450
5451         return pos - buf;
5452 }
5453
5454
5455 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
5456                                                   char *buf, int len)
5457 {
5458         int res;
5459         char *pos, *end;
5460         struct wpa_supplicant *wpa_s;
5461
5462         wpa_s = global->ifaces;
5463         pos = buf;
5464         end = buf + len;
5465
5466         while (wpa_s) {
5467                 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
5468                 if (res < 0 || res >= end - pos) {
5469                         *pos = '\0';
5470                         break;
5471                 }
5472                 pos += res;
5473                 wpa_s = wpa_s->next;
5474         }
5475         return pos - buf;
5476 }
5477
5478
5479 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
5480                                                 char *buf, size_t *resp_len)
5481 {
5482         char *reply;
5483         const int reply_size = 2048;
5484         int reply_len;
5485         int level = MSG_DEBUG;
5486
5487         if (os_strcmp(buf, "PING") == 0)
5488                 level = MSG_EXCESSIVE;
5489         wpa_hexdump_ascii(level, "RX global ctrl_iface",
5490                           (const u8 *) buf, os_strlen(buf));
5491
5492         reply = os_malloc(reply_size);
5493         if (reply == NULL) {
5494                 *resp_len = 1;
5495                 return NULL;
5496         }
5497
5498         os_memcpy(reply, "OK\n", 3);
5499         reply_len = 3;
5500
5501         if (os_strcmp(buf, "PING") == 0) {
5502                 os_memcpy(reply, "PONG\n", 5);
5503                 reply_len = 5;
5504         } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
5505                 if (wpa_supplicant_global_iface_add(global, buf + 14))
5506                         reply_len = -1;
5507         } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
5508                 if (wpa_supplicant_global_iface_remove(global, buf + 17))
5509                         reply_len = -1;
5510         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
5511                 reply_len = wpa_supplicant_global_iface_list(
5512                         global, reply, reply_size);
5513         } else if (os_strcmp(buf, "INTERFACES") == 0) {
5514                 reply_len = wpa_supplicant_global_iface_interfaces(
5515                         global, reply, reply_size);
5516         } else if (os_strcmp(buf, "TERMINATE") == 0) {
5517                 wpa_supplicant_terminate_proc(global);
5518         } else if (os_strcmp(buf, "SUSPEND") == 0) {
5519                 wpas_notify_suspend(global);
5520         } else if (os_strcmp(buf, "RESUME") == 0) {
5521                 wpas_notify_resume(global);
5522         } else {
5523                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
5524                 reply_len = 16;
5525         }
5526
5527         if (reply_len < 0) {
5528                 os_memcpy(reply, "FAIL\n", 5);
5529                 reply_len = 5;
5530         }
5531
5532         *resp_len = reply_len;
5533         return reply;
5534 }