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