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