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