]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - build-utilities/hostap.git/blob - wpa_supplicant/ctrl_iface.c
Select the BSD license terms as the only license alternative
[build-utilities/hostap.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  */
15 #include "utils/includes.h"
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"
45 extern struct wpa_driver_ops *wpa_drivers[];
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);
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;
60         if (wpa_s->pno)
61                 return 0;
63         os_memset(&params, 0, sizeof(params));
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         }
78         if (num_ssid == 0) {
79                 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
80                 return -1;
81         }
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         }
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;
113 static int pno_stop(struct wpa_supplicant *wpa_s)
115         if (wpa_s->pno) {
116                 wpa_s->pno = 0;
117                 return wpa_drv_stop_sched_scan(wpa_s);
118         }
119         return 0;
123 static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
124                                          char *cmd)
126         char *value;
127         int ret = 0;
129         value = os_strchr(cmd, ' ');
130         if (value == NULL)
131                 return -1;
132         *value++ = '\0';
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         }
213         return ret;
217 static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
218                                          char *cmd, char *buf, size_t buflen)
220         int res = -1;
222         wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
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         }
233         if (res < 0 || (unsigned int) res >= buflen)
234                 return -1;
235         return res;
239 #ifdef IEEE8021X_EAPOL
240 static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
241                                              char *addr)
243         u8 bssid[ETH_ALEN];
244         struct wpa_ssid *ssid = wpa_s->current_ssid;
246         if (hwaddr_aton(addr, bssid)) {
247                 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
248                            "'%s'", addr);
249                 return -1;
250         }
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;
257         return 0;
259 #endif /* IEEE8021X_EAPOL */
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)
267         u8 peer[ETH_ALEN];
269         if (hwaddr_aton(addr, peer)) {
270                 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
271                            "address '%s'", addr);
272                 return -1;
273         }
275         wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
276                    MAC2STR(peer));
278         return wpa_sm_stkstart(wpa_s->wpa, peer);
280 #endif /* CONFIG_PEERKEY */
283 #ifdef CONFIG_TDLS
285 static int wpa_supplicant_ctrl_iface_tdls_discover(
286         struct wpa_supplicant *wpa_s, char *addr)
288         u8 peer[ETH_ALEN];
289         int ret;
291         if (hwaddr_aton(addr, peer)) {
292                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
293                            "address '%s'", addr);
294                 return -1;
295         }
297         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
298                    MAC2STR(peer));
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);
305         return ret;
309 static int wpa_supplicant_ctrl_iface_tdls_setup(
310         struct wpa_supplicant *wpa_s, char *addr)
312         u8 peer[ETH_ALEN];
313         int ret;
315         if (hwaddr_aton(addr, peer)) {
316                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
317                            "address '%s'", addr);
318                 return -1;
319         }
321         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
322                    MAC2STR(peer));
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         }
332         return ret;
336 static int wpa_supplicant_ctrl_iface_tdls_teardown(
337         struct wpa_supplicant *wpa_s, char *addr)
339         u8 peer[ETH_ALEN];
341         if (hwaddr_aton(addr, peer)) {
342                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
343                            "address '%s'", addr);
344                 return -1;
345         }
347         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
348                    MAC2STR(peer));
350         return wpa_tdls_teardown_link(wpa_s->wpa, peer,
351                                       WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
354 #endif /* CONFIG_TDLS */
357 #ifdef CONFIG_IEEE80211R
358 static int wpa_supplicant_ctrl_iface_ft_ds(
359         struct wpa_supplicant *wpa_s, char *addr)
361         u8 target_ap[ETH_ALEN];
362         struct wpa_bss *bss;
363         const u8 *mdie;
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         }
371         wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
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;
379         return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
381 #endif /* CONFIG_IEEE80211R */
384 #ifdef CONFIG_WPS
385 static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
386                                              char *cmd)
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 */
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         }
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 */
419         return wpas_wps_start_pbc(wpa_s, _bssid, 0);
423 static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
424                                              char *cmd, char *buf,
425                                              size_t buflen)
427         u8 bssid[ETH_ALEN], *_bssid = bssid;
428         char *pin;
429         int ret;
431         pin = os_strchr(cmd, ' ');
432         if (pin)
433                 *pin++ = '\0';
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         }
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 */
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         }
463         ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
464         if (ret < 0)
465                 return -1;
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;
476 static int wpa_supplicant_ctrl_iface_wps_check_pin(
477         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
479         char pin[9];
480         size_t len;
481         char *pos;
482         int ret;
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';
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         }
513         ret = os_snprintf(buf, buflen, "%s", pin);
514         if (ret < 0 || (size_t) ret >= buflen)
515                 return -1;
517         return ret;
521 #ifdef CONFIG_WPS_OOB
522 static int wpa_supplicant_ctrl_iface_wps_oob(struct wpa_supplicant *wpa_s,
523                                              char *cmd)
525         char *path, *method, *name;
527         path = os_strchr(cmd, ' ');
528         if (path == NULL)
529                 return -1;
530         *path++ = '\0';
532         method = os_strchr(path, ' ');
533         if (method == NULL)
534                 return -1;
535         *method++ = '\0';
537         name = os_strchr(method, ' ');
538         if (name != NULL)
539                 *name++ = '\0';
541         return wpas_wps_start_oob(wpa_s, cmd, path, method, name);
543 #endif /* CONFIG_WPS_OOB */
546 static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
547                                              char *cmd)
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;
557         pin = os_strchr(cmd, ' ');
558         if (pin == NULL)
559                 return -1;
560         *pin++ = '\0';
562         if (hwaddr_aton(cmd, bssid)) {
563                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
564                            cmd);
565                 return -1;
566         }
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';
573         new_auth = os_strchr(new_ssid, ' ');
574         if (new_auth == NULL)
575                 return -1;
576         *new_auth++ = '\0';
578         new_encr = os_strchr(new_auth, ' ');
579         if (new_encr == NULL)
580                 return -1;
581         *new_encr++ = '\0';
583         new_key = os_strchr(new_encr, ' ');
584         if (new_key == NULL)
585                 return -1;
586         *new_key++ = '\0';
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);
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)
602         int timeout = 300;
603         char *pos;
604         const char *pin_txt;
606         if (!wpa_s->ap_iface)
607                 return -1;
609         pos = os_strchr(cmd, ' ');
610         if (pos)
611                 *pos++ = '\0';
613         if (os_strcmp(cmd, "disable") == 0) {
614                 wpas_wps_ap_pin_disable(wpa_s);
615                 return os_snprintf(buf, buflen, "OK\n");
616         }
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         }
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         }
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         }
651         return -1;
653 #endif /* CONFIG_AP */
656 #ifdef CONFIG_WPS_ER
657 static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
658                                                 char *cmd)
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);
676 static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
677                                                   char *cmd)
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);
688 static int wpa_supplicant_ctrl_iface_wps_er_set_config(
689         struct wpa_supplicant *wpa_s, char *cmd)
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));
700 static int wpa_supplicant_ctrl_iface_wps_er_config(
701         struct wpa_supplicant *wpa_s, char *cmd)
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;
710         pin = os_strchr(cmd, ' ');
711         if (pin == NULL)
712                 return -1;
713         *pin++ = '\0';
715         new_ssid = os_strchr(pin, ' ');
716         if (new_ssid == NULL)
717                 return -1;
718         *new_ssid++ = '\0';
720         new_auth = os_strchr(new_ssid, ' ');
721         if (new_auth == NULL)
722                 return -1;
723         *new_auth++ = '\0';
725         new_encr = os_strchr(new_auth, ' ');
726         if (new_encr == NULL)
727                 return -1;
728         *new_encr++ = '\0';
730         new_key = os_strchr(new_encr, ' ');
731         if (new_key == NULL)
732                 return -1;
733         *new_key++ = '\0';
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);
742 #endif /* CONFIG_WPS_ER */
744 #endif /* CONFIG_WPS */
747 #ifdef CONFIG_IBSS_RSN
748 static int wpa_supplicant_ctrl_iface_ibss_rsn(
749         struct wpa_supplicant *wpa_s, char *addr)
751         u8 peer[ETH_ALEN];
753         if (hwaddr_aton(addr, peer)) {
754                 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
755                            "address '%s'", addr);
756                 return -1;
757         }
759         wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
760                    MAC2STR(peer));
762         return ibss_rsn_start(wpa_s->ibss_rsn, peer);
764 #endif /* CONFIG_IBSS_RSN */
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)
772 #ifdef IEEE8021X_EAPOL
773         struct eap_peer_config *eap = &ssid->eap;
775         wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
776         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
777                               (const u8 *) value, os_strlen(value));
779         switch (wpa_supplicant_ctrl_req_from_string(field)) {
780         case WPA_CTRL_REQ_EAP_IDENTITY:
781                 os_free(eap->identity);
782                 eap->identity = (u8 *) os_strdup(value);
783                 eap->identity_len = os_strlen(value);
784                 eap->pending_req_identity = 0;
785                 if (ssid == wpa_s->current_ssid)
786                         wpa_s->reassociate = 1;
787                 break;
788         case WPA_CTRL_REQ_EAP_PASSWORD:
789                 os_free(eap->password);
790                 eap->password = (u8 *) os_strdup(value);
791                 eap->password_len = os_strlen(value);
792                 eap->pending_req_password = 0;
793                 if (ssid == wpa_s->current_ssid)
794                         wpa_s->reassociate = 1;
795                 break;
796         case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
797                 os_free(eap->new_password);
798                 eap->new_password = (u8 *) os_strdup(value);
799                 eap->new_password_len = os_strlen(value);
800                 eap->pending_req_new_password = 0;
801                 if (ssid == wpa_s->current_ssid)
802                         wpa_s->reassociate = 1;
803                 break;
804         case WPA_CTRL_REQ_EAP_PIN:
805                 os_free(eap->pin);
806                 eap->pin = os_strdup(value);
807                 eap->pending_req_pin = 0;
808                 if (ssid == wpa_s->current_ssid)
809                         wpa_s->reassociate = 1;
810                 break;
811         case WPA_CTRL_REQ_EAP_OTP:
812                 os_free(eap->otp);
813                 eap->otp = (u8 *) os_strdup(value);
814                 eap->otp_len = os_strlen(value);
815                 os_free(eap->pending_req_otp);
816                 eap->pending_req_otp = NULL;
817                 eap->pending_req_otp_len = 0;
818                 break;
819         case WPA_CTRL_REQ_EAP_PASSPHRASE:
820                 os_free(eap->private_key_passwd);
821                 eap->private_key_passwd = (u8 *) os_strdup(value);
822                 eap->pending_req_passphrase = 0;
823                 if (ssid == wpa_s->current_ssid)
824                         wpa_s->reassociate = 1;
825                 break;
826         default:
827                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
828                 return -1;
829         }
831         return 0;
832 #else /* IEEE8021X_EAPOL */
833         wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
834         return -1;
835 #endif /* IEEE8021X_EAPOL */
839 static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
840                                               char *rsp)
842 #ifdef IEEE8021X_EAPOL
843         char *pos, *id_pos;
844         int id;
845         struct wpa_ssid *ssid;
847         pos = os_strchr(rsp, '-');
848         if (pos == NULL)
849                 return -1;
850         *pos++ = '\0';
851         id_pos = pos;
852         pos = os_strchr(pos, ':');
853         if (pos == NULL)
854                 return -1;
855         *pos++ = '\0';
856         id = atoi(id_pos);
857         wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
858         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
859                               (u8 *) pos, os_strlen(pos));
861         ssid = wpa_config_get_network(wpa_s->conf, id);
862         if (ssid == NULL) {
863                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
864                            "to update", id);
865                 return -1;
866         }
868         return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
869                                                          pos);
870 #else /* IEEE8021X_EAPOL */
871         wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
872         return -1;
873 #endif /* IEEE8021X_EAPOL */
877 static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
878                                             const char *params,
879                                             char *buf, size_t buflen)
881         char *pos, *end, tmp[30];
882         int res, verbose, wps, ret;
884         verbose = os_strcmp(params, "-VERBOSE") == 0;
885         wps = os_strcmp(params, "-WPS") == 0;
886         pos = buf;
887         end = buf + buflen;
888         if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
889                 struct wpa_ssid *ssid = wpa_s->current_ssid;
890                 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
891                                   MAC2STR(wpa_s->bssid));
892                 if (ret < 0 || ret >= end - pos)
893                         return pos - buf;
894                 pos += ret;
895                 if (ssid) {
896                         u8 *_ssid = ssid->ssid;
897                         size_t ssid_len = ssid->ssid_len;
898                         u8 ssid_buf[MAX_SSID_LEN];
899                         if (ssid_len == 0) {
900                                 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
901                                 if (_res < 0)
902                                         ssid_len = 0;
903                                 else
904                                         ssid_len = _res;
905                                 _ssid = ssid_buf;
906                         }
907                         ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
908                                           wpa_ssid_txt(_ssid, ssid_len),
909                                           ssid->id);
910                         if (ret < 0 || ret >= end - pos)
911                                 return pos - buf;
912                         pos += ret;
914                         if (wps && ssid->passphrase &&
915                             wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
916                             (ssid->mode == WPAS_MODE_AP ||
917                              ssid->mode == WPAS_MODE_P2P_GO)) {
918                                 ret = os_snprintf(pos, end - pos,
919                                                   "passphrase=%s\n",
920                                                   ssid->passphrase);
921                                 if (ret < 0 || ret >= end - pos)
922                                         return pos - buf;
923                                 pos += ret;
924                         }
925                         if (ssid->id_str) {
926                                 ret = os_snprintf(pos, end - pos,
927                                                   "id_str=%s\n",
928                                                   ssid->id_str);
929                                 if (ret < 0 || ret >= end - pos)
930                                         return pos - buf;
931                                 pos += ret;
932                         }
934                         switch (ssid->mode) {
935                         case WPAS_MODE_INFRA:
936                                 ret = os_snprintf(pos, end - pos,
937                                                   "mode=station\n");
938                                 break;
939                         case WPAS_MODE_IBSS:
940                                 ret = os_snprintf(pos, end - pos,
941                                                   "mode=IBSS\n");
942                                 break;
943                         case WPAS_MODE_AP:
944                                 ret = os_snprintf(pos, end - pos,
945                                                   "mode=AP\n");
946                                 break;
947                         case WPAS_MODE_P2P_GO:
948                                 ret = os_snprintf(pos, end - pos,
949                                                   "mode=P2P GO\n");
950                                 break;
951                         case WPAS_MODE_P2P_GROUP_FORMATION:
952                                 ret = os_snprintf(pos, end - pos,
953                                                   "mode=P2P GO - group "
954                                                   "formation\n");
955                                 break;
956                         default:
957                                 ret = 0;
958                                 break;
959                         }
960                         if (ret < 0 || ret >= end - pos)
961                                 return pos - buf;
962                         pos += ret;
963                 }
965 #ifdef CONFIG_AP
966                 if (wpa_s->ap_iface) {
967                         pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
968                                                             end - pos,
969                                                             verbose);
970                 } else
971 #endif /* CONFIG_AP */
972                 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
973         }
974         ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
975                           wpa_supplicant_state_txt(wpa_s->wpa_state));
976         if (ret < 0 || ret >= end - pos)
977                 return pos - buf;
978         pos += ret;
980         if (wpa_s->l2 &&
981             l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
982                 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
983                 if (ret < 0 || ret >= end - pos)
984                         return pos - buf;
985                 pos += ret;
986         }
988 #ifdef CONFIG_P2P
989         if (wpa_s->global->p2p) {
990                 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
991                                   "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
992                 if (ret < 0 || ret >= end - pos)
993                         return pos - buf;
994                 pos += ret;
995         }
996 #endif /* CONFIG_P2P */
998         ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
999                           MAC2STR(wpa_s->own_addr));
1000         if (ret < 0 || ret >= end - pos)
1001                 return pos - buf;
1002         pos += ret;
1004         if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1005             wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1006                 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1007                                           verbose);
1008                 if (res >= 0)
1009                         pos += res;
1010         }
1012         res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1013         if (res >= 0)
1014                 pos += res;
1016         return pos - buf;
1020 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1021                                            char *cmd)
1023         char *pos;
1024         int id;
1025         struct wpa_ssid *ssid;
1026         u8 bssid[ETH_ALEN];
1028         /* cmd: "<network id> <BSSID>" */
1029         pos = os_strchr(cmd, ' ');
1030         if (pos == NULL)
1031                 return -1;
1032         *pos++ = '\0';
1033         id = atoi(cmd);
1034         wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1035         if (hwaddr_aton(pos, bssid)) {
1036                 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1037                 return -1;
1038         }
1040         ssid = wpa_config_get_network(wpa_s->conf, id);
1041         if (ssid == NULL) {
1042                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1043                            "to update", id);
1044                 return -1;
1045         }
1047         os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1048         ssid->bssid_set = !is_zero_ether_addr(bssid);
1050         return 0;
1054 static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
1055                                                char *cmd, char *buf,
1056                                                size_t buflen)
1058         u8 bssid[ETH_ALEN];
1059         struct wpa_blacklist *e;
1060         char *pos, *end;
1061         int ret;
1063         /* cmd: "BLACKLIST [<BSSID>]" */
1064         if (*cmd == '\0') {
1065                 pos = buf;
1066                 end = buf + buflen;
1067                 e = wpa_s->blacklist;
1068                 while (e) {
1069                         ret = os_snprintf(pos, end - pos, MACSTR "\n",
1070                                           MAC2STR(e->bssid));
1071                         if (ret < 0 || ret >= end - pos)
1072                                 return pos - buf;
1073                         pos += ret;
1074                         e = e->next;
1075                 }
1076                 return pos - buf;
1077         }
1079         cmd++;
1080         if (os_strncmp(cmd, "clear", 5) == 0) {
1081                 wpa_blacklist_clear(wpa_s);
1082                 os_memcpy(buf, "OK\n", 3);
1083                 return 3;
1084         }
1086         wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1087         if (hwaddr_aton(cmd, bssid)) {
1088                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
1089                 return -1;
1090         }
1092         /*
1093          * Add the BSSID twice, so its count will be 2, causing it to be
1094          * skipped when processing scan results.
1095          */
1096         ret = wpa_blacklist_add(wpa_s, bssid);
1097         if (ret != 0)
1098                 return -1;
1099         ret = wpa_blacklist_add(wpa_s, bssid);
1100         if (ret != 0)
1101                 return -1;
1102         os_memcpy(buf, "OK\n", 3);
1103         return 3;
1107 extern int wpa_debug_level;
1108 extern int wpa_debug_timestamp;
1110 static const char * debug_level_str(int level)
1112         switch (level) {
1113         case MSG_EXCESSIVE:
1114                 return "EXCESSIVE";
1115         case MSG_MSGDUMP:
1116                 return "MSGDUMP";
1117         case MSG_DEBUG:
1118                 return "DEBUG";
1119         case MSG_INFO:
1120                 return "INFO";
1121         case MSG_WARNING:
1122                 return "WARNING";
1123         case MSG_ERROR:
1124                 return "ERROR";
1125         default:
1126                 return "?";
1127         }
1131 static int str_to_debug_level(const char *s)
1133         if (os_strcasecmp(s, "EXCESSIVE") == 0)
1134                 return MSG_EXCESSIVE;
1135         if (os_strcasecmp(s, "MSGDUMP") == 0)
1136                 return MSG_MSGDUMP;
1137         if (os_strcasecmp(s, "DEBUG") == 0)
1138                 return MSG_DEBUG;
1139         if (os_strcasecmp(s, "INFO") == 0)
1140                 return MSG_INFO;
1141         if (os_strcasecmp(s, "WARNING") == 0)
1142                 return MSG_WARNING;
1143         if (os_strcasecmp(s, "ERROR") == 0)
1144                 return MSG_ERROR;
1145         return -1;
1149 static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1150                                                char *cmd, char *buf,
1151                                                size_t buflen)
1153         char *pos, *end, *stamp;
1154         int ret;
1156         if (cmd == NULL) {
1157                 return -1;
1158         }
1160         /* cmd: "LOG_LEVEL [<level>]" */
1161         if (*cmd == '\0') {
1162                 pos = buf;
1163                 end = buf + buflen;
1164                 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1165                                   "Timestamp: %d\n",
1166                                   debug_level_str(wpa_debug_level),
1167                                   wpa_debug_timestamp);
1168                 if (ret < 0 || ret >= end - pos)
1169                         ret = 0;
1171                 return ret;
1172         }
1174         while (*cmd == ' ')
1175                 cmd++;
1177         stamp = os_strchr(cmd, ' ');
1178         if (stamp) {
1179                 *stamp++ = '\0';
1180                 while (*stamp == ' ') {
1181                         stamp++;
1182                 }
1183         }
1185         if (cmd && os_strlen(cmd)) {
1186                 int level = str_to_debug_level(cmd);
1187                 if (level < 0)
1188                         return -1;
1189                 wpa_debug_level = level;
1190         }
1192         if (stamp && os_strlen(stamp))
1193                 wpa_debug_timestamp = atoi(stamp);
1195         os_memcpy(buf, "OK\n", 3);
1196         return 3;
1200 static int wpa_supplicant_ctrl_iface_list_networks(
1201         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1203         char *pos, *end;
1204         struct wpa_ssid *ssid;
1205         int ret;
1207         pos = buf;
1208         end = buf + buflen;
1209         ret = os_snprintf(pos, end - pos,
1210                           "network id / ssid / bssid / flags\n");
1211         if (ret < 0 || ret >= end - pos)
1212                 return pos - buf;
1213         pos += ret;
1215         ssid = wpa_s->conf->ssid;
1216         while (ssid) {
1217                 ret = os_snprintf(pos, end - pos, "%d\t%s",
1218                                   ssid->id,
1219                                   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1220                 if (ret < 0 || ret >= end - pos)
1221                         return pos - buf;
1222                 pos += ret;
1223                 if (ssid->bssid_set) {
1224                         ret = os_snprintf(pos, end - pos, "\t" MACSTR,
1225                                           MAC2STR(ssid->bssid));
1226                 } else {
1227                         ret = os_snprintf(pos, end - pos, "\tany");
1228                 }
1229                 if (ret < 0 || ret >= end - pos)
1230                         return pos - buf;
1231                 pos += ret;
1232                 ret = os_snprintf(pos, end - pos, "\t%s%s%s",
1233                                   ssid == wpa_s->current_ssid ?
1234                                   "[CURRENT]" : "",
1235                                   ssid->disabled ? "[DISABLED]" : "",
1236                                   ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
1237                                   "");
1238                 if (ret < 0 || ret >= end - pos)
1239                         return pos - buf;
1240                 pos += ret;
1241                 ret = os_snprintf(pos, end - pos, "\n");
1242                 if (ret < 0 || ret >= end - pos)
1243                         return pos - buf;
1244                 pos += ret;
1246                 ssid = ssid->next;
1247         }
1249         return pos - buf;
1253 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
1255         int first = 1, ret;
1256         ret = os_snprintf(pos, end - pos, "-");
1257         if (ret < 0 || ret >= end - pos)
1258                 return pos;
1259         pos += ret;
1260         if (cipher & WPA_CIPHER_NONE) {
1261                 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
1262                 if (ret < 0 || ret >= end - pos)
1263                         return pos;
1264                 pos += ret;
1265                 first = 0;
1266         }
1267         if (cipher & WPA_CIPHER_WEP40) {
1268                 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
1269                 if (ret < 0 || ret >= end - pos)
1270                         return pos;
1271                 pos += ret;
1272                 first = 0;
1273         }
1274         if (cipher & WPA_CIPHER_WEP104) {
1275                 ret = os_snprintf(pos, end - pos, "%sWEP104",
1276                                   first ? "" : "+");
1277                 if (ret < 0 || ret >= end - pos)
1278                         return pos;
1279                 pos += ret;
1280                 first = 0;
1281         }
1282         if (cipher & WPA_CIPHER_TKIP) {
1283                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
1284                 if (ret < 0 || ret >= end - pos)
1285                         return pos;
1286                 pos += ret;
1287                 first = 0;
1288         }
1289         if (cipher & WPA_CIPHER_CCMP) {
1290                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
1291                 if (ret < 0 || ret >= end - pos)
1292                         return pos;
1293                 pos += ret;
1294                 first = 0;
1295         }
1296         return pos;
1300 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
1301                                     const u8 *ie, size_t ie_len)
1303         struct wpa_ie_data data;
1304         int first, ret;
1306         ret = os_snprintf(pos, end - pos, "[%s-", proto);
1307         if (ret < 0 || ret >= end - pos)
1308                 return pos;
1309         pos += ret;
1311         if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
1312                 ret = os_snprintf(pos, end - pos, "?]");
1313                 if (ret < 0 || ret >= end - pos)
1314                         return pos;
1315                 pos += ret;
1316                 return pos;
1317         }
1319         first = 1;
1320         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1321                 ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
1322                 if (ret < 0 || ret >= end - pos)
1323                         return pos;
1324                 pos += ret;
1325                 first = 0;
1326         }
1327         if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
1328                 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
1329                 if (ret < 0 || ret >= end - pos)
1330                         return pos;
1331                 pos += ret;
1332                 first = 0;
1333         }
1334         if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
1335                 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
1336                 if (ret < 0 || ret >= end - pos)
1337                         return pos;
1338                 pos += ret;
1339                 first = 0;
1340         }
1341 #ifdef CONFIG_IEEE80211R
1342         if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1343                 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
1344                                   first ? "" : "+");
1345                 if (ret < 0 || ret >= end - pos)
1346                         return pos;
1347                 pos += ret;
1348                 first = 0;
1349         }
1350         if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1351                 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
1352                                   first ? "" : "+");
1353                 if (ret < 0 || ret >= end - pos)
1354                         return pos;
1355                 pos += ret;
1356                 first = 0;
1357         }
1358 #endif /* CONFIG_IEEE80211R */
1359 #ifdef CONFIG_IEEE80211W
1360         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1361                 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
1362                                   first ? "" : "+");
1363                 if (ret < 0 || ret >= end - pos)
1364                         return pos;
1365                 pos += ret;
1366                 first = 0;
1367         }
1368         if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1369                 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
1370                                   first ? "" : "+");
1371                 if (ret < 0 || ret >= end - pos)
1372                         return pos;
1373                 pos += ret;
1374                 first = 0;
1375         }
1376 #endif /* CONFIG_IEEE80211W */
1378         pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
1380         if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
1381                 ret = os_snprintf(pos, end - pos, "-preauth");
1382                 if (ret < 0 || ret >= end - pos)
1383                         return pos;
1384                 pos += ret;
1385         }
1387         ret = os_snprintf(pos, end - pos, "]");
1388         if (ret < 0 || ret >= end - pos)
1389                 return pos;
1390         pos += ret;
1392         return pos;
1396 #ifdef CONFIG_WPS
1397 static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
1398                                             char *pos, char *end,
1399                                             struct wpabuf *wps_ie)
1401         int ret;
1402         const char *txt;
1404         if (wps_ie == NULL)
1405                 return pos;
1406         if (wps_is_selected_pbc_registrar(wps_ie))
1407                 txt = "[WPS-PBC]";
1408 #ifdef CONFIG_WPS2
1409         else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
1410                 txt = "[WPS-AUTH]";
1411 #endif /* CONFIG_WPS2 */
1412         else if (wps_is_selected_pin_registrar(wps_ie))
1413                 txt = "[WPS-PIN]";
1414         else
1415                 txt = "[WPS]";
1417         ret = os_snprintf(pos, end - pos, "%s", txt);
1418         if (ret >= 0 && ret < end - pos)
1419                 pos += ret;
1420         wpabuf_free(wps_ie);
1421         return pos;
1423 #endif /* CONFIG_WPS */
1426 static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
1427                                         char *pos, char *end,
1428                                         const struct wpa_bss *bss)
1430 #ifdef CONFIG_WPS
1431         struct wpabuf *wps_ie;
1432         wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1433         return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
1434 #else /* CONFIG_WPS */
1435         return pos;
1436 #endif /* CONFIG_WPS */
1440 /* Format one result on one text line into a buffer. */
1441 static int wpa_supplicant_ctrl_iface_scan_result(
1442         struct wpa_supplicant *wpa_s,
1443         const struct wpa_bss *bss, char *buf, size_t buflen)
1445         char *pos, *end;
1446         int ret;
1447         const u8 *ie, *ie2, *p2p;
1449         p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1450         if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
1451             os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
1452             0)
1453                 return 0; /* Do not show P2P listen discovery results here */
1455         pos = buf;
1456         end = buf + buflen;
1458         ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
1459                           MAC2STR(bss->bssid), bss->freq, bss->level);
1460         if (ret < 0 || ret >= end - pos)
1461                 return -1;
1462         pos += ret;
1463         ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1464         if (ie)
1465                 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
1466         ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1467         if (ie2)
1468                 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
1469         pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
1470         if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
1471                 ret = os_snprintf(pos, end - pos, "[WEP]");
1472                 if (ret < 0 || ret >= end - pos)
1473                         return -1;
1474                 pos += ret;
1475         }
1476         if (bss->caps & IEEE80211_CAP_IBSS) {
1477                 ret = os_snprintf(pos, end - pos, "[IBSS]");
1478                 if (ret < 0 || ret >= end - pos)
1479                         return -1;
1480                 pos += ret;
1481         }
1482         if (bss->caps & IEEE80211_CAP_ESS) {
1483                 ret = os_snprintf(pos, end - pos, "[ESS]");
1484                 if (ret < 0 || ret >= end - pos)
1485                         return -1;
1486                 pos += ret;
1487         }
1488         if (p2p) {
1489                 ret = os_snprintf(pos, end - pos, "[P2P]");
1490                 if (ret < 0 || ret >= end - pos)
1491                         return -1;
1492                 pos += ret;
1493         }
1495         ret = os_snprintf(pos, end - pos, "\t%s",
1496                           wpa_ssid_txt(bss->ssid, bss->ssid_len));
1497         if (ret < 0 || ret >= end - pos)
1498                 return -1;
1499         pos += ret;
1501         ret = os_snprintf(pos, end - pos, "\n");
1502         if (ret < 0 || ret >= end - pos)
1503                 return -1;
1504         pos += ret;
1506         return pos - buf;
1510 static int wpa_supplicant_ctrl_iface_scan_results(
1511         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1513         char *pos, *end;
1514         struct wpa_bss *bss;
1515         int ret;
1517         pos = buf;
1518         end = buf + buflen;
1519         ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
1520                           "flags / ssid\n");
1521         if (ret < 0 || ret >= end - pos)
1522                 return pos - buf;
1523         pos += ret;
1525         dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
1526                 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
1527                                                             end - pos);
1528                 if (ret < 0 || ret >= end - pos)
1529                         return pos - buf;
1530                 pos += ret;
1531         }
1533         return pos - buf;
1537 static int wpa_supplicant_ctrl_iface_select_network(
1538         struct wpa_supplicant *wpa_s, char *cmd)
1540         int id;
1541         struct wpa_ssid *ssid;
1543         /* cmd: "<network id>" or "any" */
1544         if (os_strcmp(cmd, "any") == 0) {
1545                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
1546                 ssid = NULL;
1547         } else {
1548                 id = atoi(cmd);
1549                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
1551                 ssid = wpa_config_get_network(wpa_s->conf, id);
1552                 if (ssid == NULL) {
1553                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1554                                    "network id=%d", id);
1555                         return -1;
1556                 }
1557                 if (ssid->disabled == 2) {
1558                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1559                                    "SELECT_NETWORK with persistent P2P group");
1560                         return -1;
1561                 }
1562         }
1564         wpa_supplicant_select_network(wpa_s, ssid);
1566         return 0;
1570 static int wpa_supplicant_ctrl_iface_enable_network(
1571         struct wpa_supplicant *wpa_s, char *cmd)
1573         int id;
1574         struct wpa_ssid *ssid;
1576         /* cmd: "<network id>" or "all" */
1577         if (os_strcmp(cmd, "all") == 0) {
1578                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
1579                 ssid = NULL;
1580         } else {
1581                 id = atoi(cmd);
1582                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
1584                 ssid = wpa_config_get_network(wpa_s->conf, id);
1585                 if (ssid == NULL) {
1586                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1587                                    "network id=%d", id);
1588                         return -1;
1589                 }
1590                 if (ssid->disabled == 2) {
1591                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1592                                    "ENABLE_NETWORK with persistent P2P group");
1593                         return -1;
1594                 }
1595         }
1596         wpa_supplicant_enable_network(wpa_s, ssid);
1598         return 0;
1602 static int wpa_supplicant_ctrl_iface_disable_network(
1603         struct wpa_supplicant *wpa_s, char *cmd)
1605         int id;
1606         struct wpa_ssid *ssid;
1608         /* cmd: "<network id>" or "all" */
1609         if (os_strcmp(cmd, "all") == 0) {
1610                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
1611                 ssid = NULL;
1612         } else {
1613                 id = atoi(cmd);
1614                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
1616                 ssid = wpa_config_get_network(wpa_s->conf, id);
1617                 if (ssid == NULL) {
1618                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1619                                    "network id=%d", id);
1620                         return -1;
1621                 }
1622                 if (ssid->disabled == 2) {
1623                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1624                                    "DISABLE_NETWORK with persistent P2P "
1625                                    "group");
1626                         return -1;
1627                 }
1628         }
1629         wpa_supplicant_disable_network(wpa_s, ssid);
1631         return 0;
1635 static int wpa_supplicant_ctrl_iface_add_network(
1636         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1638         struct wpa_ssid *ssid;
1639         int ret;
1641         wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
1643         ssid = wpa_config_add_network(wpa_s->conf);
1644         if (ssid == NULL)
1645                 return -1;
1647         wpas_notify_network_added(wpa_s, ssid);
1649         ssid->disabled = 1;
1650         wpa_config_set_network_defaults(ssid);
1652         ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
1653         if (ret < 0 || (size_t) ret >= buflen)
1654                 return -1;
1655         return ret;
1659 static int wpa_supplicant_ctrl_iface_remove_network(
1660         struct wpa_supplicant *wpa_s, char *cmd)
1662         int id;
1663         struct wpa_ssid *ssid;
1665         /* cmd: "<network id>" or "all" */
1666         if (os_strcmp(cmd, "all") == 0) {
1667                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
1668                 ssid = wpa_s->conf->ssid;
1669                 while (ssid) {
1670                         struct wpa_ssid *remove_ssid = ssid;
1671                         id = ssid->id;
1672                         ssid = ssid->next;
1673                         wpas_notify_network_removed(wpa_s, remove_ssid);
1674                         wpa_config_remove_network(wpa_s->conf, id);
1675                 }
1676                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1677                 if (wpa_s->current_ssid) {
1678                         wpa_sm_set_config(wpa_s->wpa, NULL);
1679                         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1680                         wpa_supplicant_disassociate(wpa_s,
1681                                                     WLAN_REASON_DEAUTH_LEAVING);
1682                 }
1683                 return 0;
1684         }
1686         id = atoi(cmd);
1687         wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
1689         ssid = wpa_config_get_network(wpa_s->conf, id);
1690         if (ssid)
1691                 wpas_notify_network_removed(wpa_s, ssid);
1692         if (ssid == NULL ||
1693             wpa_config_remove_network(wpa_s->conf, id) < 0) {
1694                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1695                            "id=%d", id);
1696                 return -1;
1697         }
1699         if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
1700                 /*
1701                  * Invalidate the EAP session cache if the current or
1702                  * previously used network is removed.
1703                  */
1704                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1705         }
1707         if (ssid == wpa_s->current_ssid) {
1708                 wpa_sm_set_config(wpa_s->wpa, NULL);
1709                 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1711                 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1712         }
1714         return 0;
1718 static int wpa_supplicant_ctrl_iface_set_network(
1719         struct wpa_supplicant *wpa_s, char *cmd)
1721         int id;
1722         struct wpa_ssid *ssid;
1723         char *name, *value;
1725         /* cmd: "<network id> <variable name> <value>" */
1726         name = os_strchr(cmd, ' ');
1727         if (name == NULL)
1728                 return -1;
1729         *name++ = '\0';
1731         value = os_strchr(name, ' ');
1732         if (value == NULL)
1733                 return -1;
1734         *value++ = '\0';
1736         id = atoi(cmd);
1737         wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
1738                    id, name);
1739         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1740                               (u8 *) value, os_strlen(value));
1742         ssid = wpa_config_get_network(wpa_s->conf, id);
1743         if (ssid == NULL) {
1744                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1745                            "id=%d", id);
1746                 return -1;
1747         }
1749         if (wpa_config_set(ssid, name, value, 0) < 0) {
1750                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
1751                            "variable '%s'", name);
1752                 return -1;
1753         }
1755         wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
1757         if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
1758                 /*
1759                  * Invalidate the EAP session cache if anything in the current
1760                  * or previously used configuration changes.
1761                  */
1762                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1763         }
1765         if ((os_strcmp(name, "psk") == 0 &&
1766              value[0] == '"' && ssid->ssid_len) ||
1767             (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
1768                 wpa_config_update_psk(ssid);
1769         else if (os_strcmp(name, "priority") == 0)
1770                 wpa_config_update_prio_list(wpa_s->conf);
1772         return 0;
1776 static int wpa_supplicant_ctrl_iface_get_network(
1777         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1779         int id;
1780         size_t res;
1781         struct wpa_ssid *ssid;
1782         char *name, *value;
1784         /* cmd: "<network id> <variable name>" */
1785         name = os_strchr(cmd, ' ');
1786         if (name == NULL || buflen == 0)
1787                 return -1;
1788         *name++ = '\0';
1790         id = atoi(cmd);
1791         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
1792                    id, name);
1794         ssid = wpa_config_get_network(wpa_s->conf, id);
1795         if (ssid == NULL) {
1796                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1797                            "id=%d", id);
1798                 return -1;
1799         }
1801         value = wpa_config_get_no_key(ssid, name);
1802         if (value == NULL) {
1803                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
1804                            "variable '%s'", name);
1805                 return -1;
1806         }
1808         res = os_strlcpy(buf, value, buflen);
1809         if (res >= buflen) {
1810                 os_free(value);
1811                 return -1;
1812         }
1814         os_free(value);
1816         return res;
1820 #ifndef CONFIG_NO_CONFIG_WRITE
1821 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
1823         int ret;
1825         if (!wpa_s->conf->update_config) {
1826                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
1827                            "to update configuration (update_config=0)");
1828                 return -1;
1829         }
1831         ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
1832         if (ret) {
1833                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
1834                            "update configuration");
1835         } else {
1836                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
1837                            " updated");
1838         }
1840         return ret;
1842 #endif /* CONFIG_NO_CONFIG_WRITE */
1845 static int ctrl_iface_get_capability_pairwise(int res, char *strict,
1846                                               struct wpa_driver_capa *capa,
1847                                               char *buf, size_t buflen)
1849         int ret, first = 1;
1850         char *pos, *end;
1851         size_t len;
1853         pos = buf;
1854         end = pos + buflen;
1856         if (res < 0) {
1857                 if (strict)
1858                         return 0;
1859                 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
1860                 if (len >= buflen)
1861                         return -1;
1862                 return len;
1863         }
1865         if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
1866                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
1867                 if (ret < 0 || ret >= end - pos)
1868                         return pos - buf;
1869                 pos += ret;
1870                 first = 0;
1871         }
1873         if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
1874                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
1875                 if (ret < 0 || ret >= end - pos)
1876                         return pos - buf;
1877                 pos += ret;
1878                 first = 0;
1879         }
1881         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
1882                 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
1883                 if (ret < 0 || ret >= end - pos)
1884                         return pos - buf;
1885                 pos += ret;
1886                 first = 0;
1887         }
1889         return pos - buf;
1893 static int ctrl_iface_get_capability_group(int res, char *strict,
1894                                            struct wpa_driver_capa *capa,
1895                                            char *buf, size_t buflen)
1897         int ret, first = 1;
1898         char *pos, *end;
1899         size_t len;
1901         pos = buf;
1902         end = pos + buflen;
1904         if (res < 0) {
1905                 if (strict)
1906                         return 0;
1907                 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
1908                 if (len >= buflen)
1909                         return -1;
1910                 return len;
1911         }
1913         if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
1914                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
1915                 if (ret < 0 || ret >= end - pos)
1916                         return pos - buf;
1917                 pos += ret;
1918                 first = 0;
1919         }
1921         if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
1922                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
1923                 if (ret < 0 || ret >= end - pos)
1924                         return pos - buf;
1925                 pos += ret;
1926                 first = 0;
1927         }
1929         if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
1930                 ret = os_snprintf(pos, end - pos, "%sWEP104",
1931                                   first ? "" : " ");
1932                 if (ret < 0 || ret >= end - pos)
1933                         return pos - buf;
1934                 pos += ret;
1935                 first = 0;
1936         }
1938         if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
1939                 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
1940                 if (ret < 0 || ret >= end - pos)
1941                         return pos - buf;
1942                 pos += ret;
1943                 first = 0;
1944         }
1946         return pos - buf;
1950 static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
1951                                               struct wpa_driver_capa *capa,
1952                                               char *buf, size_t buflen)
1954         int ret;
1955         char *pos, *end;
1956         size_t len;
1958         pos = buf;
1959         end = pos + buflen;
1961         if (res < 0) {
1962                 if (strict)
1963                         return 0;
1964                 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
1965                                  "NONE", buflen);
1966                 if (len >= buflen)
1967                         return -1;
1968                 return len;
1969         }
1971         ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
1972         if (ret < 0 || ret >= end - pos)
1973                 return pos - buf;
1974         pos += ret;
1976         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1977                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
1978                 ret = os_snprintf(pos, end - pos, " WPA-EAP");
1979                 if (ret < 0 || ret >= end - pos)
1980                         return pos - buf;
1981                 pos += ret;
1982         }
1984         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
1985                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
1986                 ret = os_snprintf(pos, end - pos, " WPA-PSK");
1987                 if (ret < 0 || ret >= end - pos)
1988                         return pos - buf;
1989                 pos += ret;
1990         }
1992         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
1993                 ret = os_snprintf(pos, end - pos, " WPA-NONE");
1994                 if (ret < 0 || ret >= end - pos)
1995                         return pos - buf;
1996                 pos += ret;
1997         }
1999         return pos - buf;
2003 static int ctrl_iface_get_capability_proto(int res, char *strict,
2004                                            struct wpa_driver_capa *capa,
2005                                            char *buf, size_t buflen)
2007         int ret, first = 1;
2008         char *pos, *end;
2009         size_t len;
2011         pos = buf;
2012         end = pos + buflen;
2014         if (res < 0) {
2015                 if (strict)
2016                         return 0;
2017                 len = os_strlcpy(buf, "RSN WPA", buflen);
2018                 if (len >= buflen)
2019                         return -1;
2020                 return len;
2021         }
2023         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2024                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2025                 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
2026                 if (ret < 0 || ret >= end - pos)
2027                         return pos - buf;
2028                 pos += ret;
2029                 first = 0;
2030         }
2032         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2033                               WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
2034                 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
2035                 if (ret < 0 || ret >= end - pos)
2036                         return pos - buf;
2037                 pos += ret;
2038                 first = 0;
2039         }
2041         return pos - buf;
2045 static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
2046                                               struct wpa_driver_capa *capa,
2047                                               char *buf, size_t buflen)
2049         int ret, first = 1;
2050         char *pos, *end;
2051         size_t len;
2053         pos = buf;
2054         end = pos + buflen;
2056         if (res < 0) {
2057                 if (strict)
2058                         return 0;
2059                 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
2060                 if (len >= buflen)
2061                         return -1;
2062                 return len;
2063         }
2065         if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
2066                 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
2067                 if (ret < 0 || ret >= end - pos)
2068                         return pos - buf;
2069                 pos += ret;
2070                 first = 0;
2071         }
2073         if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
2074                 ret = os_snprintf(pos, end - pos, "%sSHARED",
2075                                   first ? "" : " ");
2076                 if (ret < 0 || ret >= end - pos)
2077                         return pos - buf;
2078                 pos += ret;
2079                 first = 0;
2080         }
2082         if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
2083                 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
2084                 if (ret < 0 || ret >= end - pos)
2085                         return pos - buf;
2086                 pos += ret;
2087                 first = 0;
2088         }
2090         return pos - buf;
2094 static int wpa_supplicant_ctrl_iface_get_capability(
2095         struct wpa_supplicant *wpa_s, const char *_field, char *buf,
2096         size_t buflen)
2098         struct wpa_driver_capa capa;
2099         int res;
2100         char *strict;
2101         char field[30];
2102         size_t len;
2104         /* Determine whether or not strict checking was requested */
2105         len = os_strlcpy(field, _field, sizeof(field));
2106         if (len >= sizeof(field))
2107                 return -1;
2108         strict = os_strchr(field, ' ');
2109         if (strict != NULL) {
2110                 *strict++ = '\0';
2111                 if (os_strcmp(strict, "strict") != 0)
2112                         return -1;
2113         }
2115         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
2116                 field, strict ? strict : "");
2118         if (os_strcmp(field, "eap") == 0) {
2119                 return eap_get_names(buf, buflen);
2120         }
2122         res = wpa_drv_get_capa(wpa_s, &capa);
2124         if (os_strcmp(field, "pairwise") == 0)
2125                 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
2126                                                           buf, buflen);
2128         if (os_strcmp(field, "group") == 0)
2129                 return ctrl_iface_get_capability_group(res, strict, &capa,
2130                                                        buf, buflen);
2132         if (os_strcmp(field, "key_mgmt") == 0)
2133                 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
2134                                                           buf, buflen);
2136         if (os_strcmp(field, "proto") == 0)
2137                 return ctrl_iface_get_capability_proto(res, strict, &capa,
2138                                                        buf, buflen);
2140         if (os_strcmp(field, "auth_alg") == 0)
2141                 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
2142                                                           buf, buflen);
2144         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
2145                    field);
2147         return -1;
2151 #ifdef CONFIG_INTERWORKING
2152 static char * anqp_add_hex(char *pos, char *end, const char *title,
2153                            struct wpabuf *data)
2155         char *start = pos;
2156         size_t i;
2157         int ret;
2158         const u8 *d;
2160         if (data == NULL)
2161                 return start;
2163         ret = os_snprintf(pos, end - pos, "%s=", title);
2164         if (ret < 0 || ret >= end - pos)
2165                 return start;
2166         pos += ret;
2168         d = wpabuf_head_u8(data);
2169         for (i = 0; i < wpabuf_len(data); i++) {
2170                 ret = os_snprintf(pos, end - pos, "%02x", *d++);
2171                 if (ret < 0 || ret >= end - pos)
2172                         return start;
2173                 pos += ret;
2174         }
2176         ret = os_snprintf(pos, end - pos, "\n");
2177         if (ret < 0 || ret >= end - pos)
2178                 return start;
2179         pos += ret;
2181         return pos;
2183 #endif /* CONFIG_INTERWORKING */
2186 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
2187                                          const char *cmd, char *buf,
2188                                          size_t buflen)
2190         u8 bssid[ETH_ALEN];
2191         size_t i;
2192         struct wpa_bss *bss;
2193         int ret;
2194         char *pos, *end;
2195         const u8 *ie, *ie2;
2196         struct os_time now;
2198         if (os_strcmp(cmd, "FIRST") == 0)
2199                 bss = dl_list_first(&wpa_s->bss, struct wpa_bss, list);
2200         else if (os_strncmp(cmd, "ID-", 3) == 0) {
2201                 i = atoi(cmd + 3);
2202                 bss = wpa_bss_get_id(wpa_s, i);
2203         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
2204                 i = atoi(cmd + 5);
2205                 bss = wpa_bss_get_id(wpa_s, i);
2206                 if (bss) {
2207                         struct dl_list *next = bss->list_id.next;
2208                         if (next == &wpa_s->bss_id)
2209                                 bss = NULL;
2210                         else
2211                                 bss = dl_list_entry(next, struct wpa_bss,
2212                                                     list_id);
2213                 }
2214 #ifdef CONFIG_P2P
2215         } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
2216                 if (hwaddr_aton(cmd + 13, bssid) == 0)
2217                         bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
2218                 else
2219                         bss = NULL;
2220 #endif /* CONFIG_P2P */
2221         } else if (hwaddr_aton(cmd, bssid) == 0)
2222                 bss = wpa_bss_get_bssid(wpa_s, bssid);
2223         else {
2224                 struct wpa_bss *tmp;
2225                 i = atoi(cmd);
2226                 bss = NULL;
2227                 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
2228                 {
2229                         if (i-- == 0) {
2230                                 bss = tmp;
2231                                 break;
2232                         }
2233                 }
2234         }
2236         if (bss == NULL)
2237                 return 0;
2239         os_get_time(&now);
2240         pos = buf;
2241         end = buf + buflen;
2242         ret = os_snprintf(pos, end - pos,
2243                           "id=%u\n"
2244                           "bssid=" MACSTR "\n"
2245                           "freq=%d\n"
2246                           "beacon_int=%d\n"
2247                           "capabilities=0x%04x\n"
2248                           "qual=%d\n"
2249                           "noise=%d\n"
2250                           "level=%d\n"
2251                           "tsf=%016llu\n"
2252                           "age=%d\n"
2253                           "ie=",
2254                           bss->id,
2255                           MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
2256                           bss->caps, bss->qual, bss->noise, bss->level,
2257                           (unsigned long long) bss->tsf,
2258                           (int) (now.sec - bss->last_update.sec));
2259         if (ret < 0 || ret >= end - pos)
2260                 return pos - buf;
2261         pos += ret;
2263         ie = (const u8 *) (bss + 1);
2264         for (i = 0; i < bss->ie_len; i++) {
2265                 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
2266                 if (ret < 0 || ret >= end - pos)
2267                         return pos - buf;
2268                 pos += ret;
2269         }
2271         ret = os_snprintf(pos, end - pos, "\n");
2272         if (ret < 0 || ret >= end - pos)
2273                 return pos - buf;
2274         pos += ret;
2276         ret = os_snprintf(pos, end - pos, "flags=");
2277         if (ret < 0 || ret >= end - pos)
2278                 return pos - buf;
2279         pos += ret;
2281         ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2282         if (ie)
2283                 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2284         ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2285         if (ie2)
2286                 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
2287         pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2288         if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2289                 ret = os_snprintf(pos, end - pos, "[WEP]");
2290                 if (ret < 0 || ret >= end - pos)
2291                         return pos - buf;
2292                 pos += ret;
2293         }
2294         if (bss->caps & IEEE80211_CAP_IBSS) {
2295                 ret = os_snprintf(pos, end - pos, "[IBSS]");
2296                 if (ret < 0 || ret >= end - pos)
2297                         return pos - buf;
2298                 pos += ret;
2299         }
2300         if (bss->caps & IEEE80211_CAP_ESS) {
2301                 ret = os_snprintf(pos, end - pos, "[ESS]");
2302                 if (ret < 0 || ret >= end - pos)
2303                         return pos - buf;
2304                 pos += ret;
2305         }
2306         if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
2307                 ret = os_snprintf(pos, end - pos, "[P2P]");
2308                 if (ret < 0 || ret >= end - pos)
2309                         return pos - buf;
2310                 pos += ret;
2311         }
2313         ret = os_snprintf(pos, end - pos, "\n");
2314         if (ret < 0 || ret >= end - pos)
2315                 return pos - buf;
2316         pos += ret;
2318         ret = os_snprintf(pos, end - pos, "ssid=%s\n",
2319                           wpa_ssid_txt(bss->ssid, bss->ssid_len));
2320         if (ret < 0 || ret >= end - pos)
2321                 return pos - buf;
2322         pos += ret;
2324 #ifdef CONFIG_WPS
2325         ie = (const u8 *) (bss + 1);
2326         ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
2327         if (ret < 0 || ret >= end - pos)
2328                 return pos - buf;
2329         pos += ret;
2330 #endif /* CONFIG_WPS */
2332 #ifdef CONFIG_P2P
2333         ie = (const u8 *) (bss + 1);
2334         ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
2335         if (ret < 0 || ret >= end - pos)
2336                 return pos - buf;
2337         pos += ret;
2338 #endif /* CONFIG_P2P */
2340 #ifdef CONFIG_INTERWORKING
2341         pos = anqp_add_hex(pos, end, "anqp_venue_name", bss->anqp_venue_name);
2342         pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
2343                            bss->anqp_network_auth_type);
2344         pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
2345                            bss->anqp_roaming_consortium);
2346         pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
2347                            bss->anqp_ip_addr_type_availability);
2348         pos = anqp_add_hex(pos, end, "anqp_nai_realm", bss->anqp_nai_realm);
2349         pos = anqp_add_hex(pos, end, "anqp_3gpp", bss->anqp_3gpp);
2350         pos = anqp_add_hex(pos, end, "anqp_domain_name",
2351                            bss->anqp_domain_name);
2352 #endif /* CONFIG_INTERWORKING */
2354         return pos - buf;
2358 static int wpa_supplicant_ctrl_iface_ap_scan(
2359         struct wpa_supplicant *wpa_s, char *cmd)
2361         int ap_scan = atoi(cmd);
2362         return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
2366 static int wpa_supplicant_ctrl_iface_scan_interval(
2367         struct wpa_supplicant *wpa_s, char *cmd)
2369         int scan_int = atoi(cmd);
2370         if (scan_int < 0)
2371                 return -1;
2372         wpa_s->scan_interval = scan_int;
2373         return 0;
2377 static int wpa_supplicant_ctrl_iface_bss_expire_age(
2378         struct wpa_supplicant *wpa_s, char *cmd)
2380         int expire_age = atoi(cmd);
2381         return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
2385 static int wpa_supplicant_ctrl_iface_bss_expire_count(
2386         struct wpa_supplicant *wpa_s, char *cmd)
2388         int expire_count = atoi(cmd);
2389         return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
2393 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
2395         wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
2396         /* MLME-DELETEKEYS.request */
2397         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
2398         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
2399         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
2400         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
2401 #ifdef CONFIG_IEEE80211W
2402         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
2403         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
2404 #endif /* CONFIG_IEEE80211W */
2406         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
2407                         0);
2408         /* MLME-SETPROTECTION.request(None) */
2409         wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
2410                                    MLME_SETPROTECTION_PROTECT_TYPE_NONE,
2411                                    MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
2412         wpa_sm_drop_sa(wpa_s->wpa);
2416 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
2417                                           char *addr)
2419 #ifdef CONFIG_NO_SCAN_PROCESSING
2420         return -1;
2421 #else /* CONFIG_NO_SCAN_PROCESSING */
2422         u8 bssid[ETH_ALEN];
2423         struct wpa_bss *bss;
2424         struct wpa_ssid *ssid = wpa_s->current_ssid;
2426         if (hwaddr_aton(addr, bssid)) {
2427                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
2428                            "address '%s'", addr);
2429                 return -1;
2430         }
2432         wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
2434         bss = wpa_bss_get_bssid(wpa_s, bssid);
2435         if (!bss) {
2436                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
2437                            "from BSS table");
2438                 return -1;
2439         }
2441         /*
2442          * TODO: Find best network configuration block from configuration to
2443          * allow roaming to other networks
2444          */
2446         if (!ssid) {
2447                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
2448                            "configuration known for the target AP");
2449                 return -1;
2450         }
2452         wpa_s->reassociate = 1;
2453         wpa_supplicant_connect(wpa_s, bss, ssid);
2455         return 0;
2456 #endif /* CONFIG_NO_SCAN_PROCESSING */
2460 #ifdef CONFIG_P2P
2461 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
2463         unsigned int timeout = atoi(cmd);
2464         enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
2465         u8 dev_id[ETH_ALEN], *_dev_id = NULL;
2466         char *pos;
2468         if (os_strstr(cmd, "type=social"))
2469                 type = P2P_FIND_ONLY_SOCIAL;
2470         else if (os_strstr(cmd, "type=progressive"))
2471                 type = P2P_FIND_PROGRESSIVE;
2473         pos = os_strstr(cmd, "dev_id=");
2474         if (pos) {
2475                 pos += 7;
2476                 if (hwaddr_aton(pos, dev_id))
2477                         return -1;
2478                 _dev_id = dev_id;
2479         }
2481         return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id);
2485 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
2486                             char *buf, size_t buflen)
2488         u8 addr[ETH_ALEN];
2489         char *pos, *pos2;
2490         char *pin = NULL;
2491         enum p2p_wps_method wps_method;
2492         int new_pin;
2493         int ret;
2494         int persistent_group;
2495         int join;
2496         int auth;
2497         int go_intent = -1;
2498         int freq = 0;
2500         /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad] [persistent]
2501          * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] */
2503         if (hwaddr_aton(cmd, addr))
2504                 return -1;
2506         pos = cmd + 17;
2507         if (*pos != ' ')
2508                 return -1;
2509         pos++;
2511         persistent_group = os_strstr(pos, " persistent") != NULL;
2512         join = os_strstr(pos, " join") != NULL;
2513         auth = os_strstr(pos, " auth") != NULL;
2515         pos2 = os_strstr(pos, " go_intent=");
2516         if (pos2) {
2517                 pos2 += 11;
2518                 go_intent = atoi(pos2);
2519                 if (go_intent < 0 || go_intent > 15)
2520                         return -1;
2521         }
2523         pos2 = os_strstr(pos, " freq=");
2524         if (pos2) {
2525                 pos2 += 6;
2526                 freq = atoi(pos2);
2527                 if (freq <= 0)
2528                         return -1;
2529         }
2531         if (os_strncmp(pos, "pin", 3) == 0) {
2532                 /* Request random PIN (to be displayed) and enable the PIN */
2533                 wps_method = WPS_PIN_DISPLAY;
2534         } else if (os_strncmp(pos, "pbc", 3) == 0) {
2535                 wps_method = WPS_PBC;
2536         } else {
2537                 pin = pos;
2538                 pos = os_strchr(pin, ' ');
2539                 wps_method = WPS_PIN_KEYPAD;
2540                 if (pos) {
2541                         *pos++ = '\0';
2542                         if (os_strncmp(pos, "display", 7) == 0)
2543                                 wps_method = WPS_PIN_DISPLAY;
2544                 }
2545         }
2547         new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
2548                                    persistent_group, join, auth, go_intent,
2549                                    freq);
2550         if (new_pin == -2) {
2551                 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
2552                 return 25;
2553         }
2554         if (new_pin == -3) {
2555                 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
2556                 return 25;
2557         }
2558         if (new_pin < 0)
2559                 return -1;
2560         if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
2561                 ret = os_snprintf(buf, buflen, "%08d", new_pin);
2562                 if (ret < 0 || (size_t) ret >= buflen)
2563                         return -1;
2564                 return ret;
2565         }
2567         os_memcpy(buf, "OK\n", 3);
2568         return 3;
2572 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
2574         unsigned int timeout = atoi(cmd);
2575         return wpas_p2p_listen(wpa_s, timeout);
2579 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
2581         u8 addr[ETH_ALEN];
2582         char *pos;
2584         /* <addr> <config method> [join] */
2586         if (hwaddr_aton(cmd, addr))
2587                 return -1;
2589         pos = cmd + 17;
2590         if (*pos != ' ')
2591                 return -1;
2592         pos++;
2594         return wpas_p2p_prov_disc(wpa_s, addr, pos,
2595                                   os_strstr(pos, "join") != NULL);
2599 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
2600                               size_t buflen)
2602         struct wpa_ssid *ssid = wpa_s->current_ssid;
2604         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2605             ssid->passphrase == NULL)
2606                 return -1;
2608         os_strlcpy(buf, ssid->passphrase, buflen);
2609         return os_strlen(buf);
2613 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
2614                                   char *buf, size_t buflen)
2616         u64 ref;
2617         int res;
2618         u8 dst_buf[ETH_ALEN], *dst;
2619         struct wpabuf *tlvs;
2620         char *pos;
2621         size_t len;
2623         if (hwaddr_aton(cmd, dst_buf))
2624                 return -1;
2625         dst = dst_buf;
2626         if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
2627             dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
2628                 dst = NULL;
2629         pos = cmd + 17;
2630         if (*pos != ' ')
2631                 return -1;
2632         pos++;
2634         if (os_strncmp(pos, "upnp ", 5) == 0) {
2635                 u8 version;
2636                 pos += 5;
2637                 if (hexstr2bin(pos, &version, 1) < 0)
2638                         return -1;
2639                 pos += 2;
2640                 if (*pos != ' ')
2641                         return -1;
2642                 pos++;
2643                 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
2644         } else {
2645                 len = os_strlen(pos);
2646                 if (len & 1)
2647                         return -1;
2648                 len /= 2;
2649                 tlvs = wpabuf_alloc(len);
2650                 if (tlvs == NULL)
2651                         return -1;
2652                 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
2653                         wpabuf_free(tlvs);
2654                         return -1;
2655                 }
2657                 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2658                 wpabuf_free(tlvs);
2659         }
2660         if (ref == 0)
2661                 return -1;
2662         res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
2663         if (res < 0 || (unsigned) res >= buflen)
2664                 return -1;
2665         return res;
2669 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
2670                                          char *cmd)
2672         long long unsigned val;
2673         u64 req;
2674         if (sscanf(cmd, "%llx", &val) != 1)
2675                 return -1;
2676         req = val;
2677         return wpas_p2p_sd_cancel_request(wpa_s, req);
2681 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
2683         int freq;
2684         u8 dst[ETH_ALEN];
2685         u8 dialog_token;
2686         struct wpabuf *resp_tlvs;
2687         char *pos, *pos2;
2688         size_t len;
2690         pos = os_strchr(cmd, ' ');
2691         if (pos == NULL)
2692                 return -1;
2693         *pos++ = '\0';
2694         freq = atoi(cmd);
2695         if (freq == 0)
2696                 return -1;
2698         if (hwaddr_aton(pos, dst))
2699                 return -1;
2700         pos += 17;
2701         if (*pos != ' ')
2702                 return -1;
2703         pos++;
2705         pos2 = os_strchr(pos, ' ');
2706         if (pos2 == NULL)
2707                 return -1;
2708         *pos2++ = '\0';
2709         dialog_token = atoi(pos);
2711         len = os_strlen(pos2);
2712         if (len & 1)
2713                 return -1;
2714         len /= 2;
2715         resp_tlvs = wpabuf_alloc(len);
2716         if (resp_tlvs == NULL)
2717                 return -1;
2718         if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
2719                 wpabuf_free(resp_tlvs);
2720                 return -1;
2721         }
2723         wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
2724         wpabuf_free(resp_tlvs);
2725         return 0;
2729 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
2730                                        char *cmd)
2732         wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
2733         return 0;
2737 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
2738                                         char *cmd)
2740         char *pos;
2741         size_t len;
2742         struct wpabuf *query, *resp;
2744         pos = os_strchr(cmd, ' ');
2745         if (pos == NULL)
2746                 return -1;
2747         *pos++ = '\0';
2749         len = os_strlen(cmd);
2750         if (len & 1)
2751                 return -1;
2752         len /= 2;
2753         query = wpabuf_alloc(len);
2754         if (query == NULL)
2755                 return -1;
2756         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
2757                 wpabuf_free(query);
2758                 return -1;
2759         }
2761         len = os_strlen(pos);
2762         if (len & 1) {
2763                 wpabuf_free(query);
2764                 return -1;
2765         }
2766         len /= 2;
2767         resp = wpabuf_alloc(len);
2768         if (resp == NULL) {
2769                 wpabuf_free(query);
2770                 return -1;
2771         }
2772         if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
2773                 wpabuf_free(query);
2774                 wpabuf_free(resp);
2775                 return -1;
2776         }
2778         if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
2779                 wpabuf_free(query);
2780                 wpabuf_free(resp);
2781                 return -1;
2782         }
2783         return 0;
2787 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
2789         char *pos;
2790         u8 version;
2792         pos = os_strchr(cmd, ' ');
2793         if (pos == NULL)
2794                 return -1;
2795         *pos++ = '\0';
2797         if (hexstr2bin(cmd, &version, 1) < 0)
2798                 return -1;
2800         return wpas_p2p_service_add_upnp(wpa_s, version, pos);
2804 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
2806         char *pos;
2808         pos = os_strchr(cmd, ' ');
2809         if (pos == NULL)
2810                 return -1;
2811         *pos++ = '\0';
2813         if (os_strcmp(cmd, "bonjour") == 0)
2814                 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
2815         if (os_strcmp(cmd, "upnp") == 0)
2816                 return p2p_ctrl_service_add_upnp(wpa_s, pos);
2817         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
2818         return -1;
2822 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
2823                                         char *cmd)
2825         size_t len;
2826         struct wpabuf *query;
2827         int ret;
2829         len = os_strlen(cmd);
2830         if (len & 1)
2831                 return -1;
2832         len /= 2;
2833         query = wpabuf_alloc(len);
2834         if (query == NULL)
2835                 return -1;
2836         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
2837                 wpabuf_free(query);
2838                 return -1;
2839         }
2841         ret = wpas_p2p_service_del_bonjour(wpa_s, query);
2842         wpabuf_free(query);
2843         return ret;
2847 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
2849         char *pos;
2850         u8 version;
2852         pos = os_strchr(cmd, ' ');
2853         if (pos == NULL)
2854                 return -1;
2855         *pos++ = '\0';
2857         if (hexstr2bin(cmd, &version, 1) < 0)
2858                 return -1;
2860         return wpas_p2p_service_del_upnp(wpa_s, version, pos);
2864 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
2866         char *pos;
2868         pos = os_strchr(cmd, ' ');
2869         if (pos == NULL)
2870                 return -1;
2871         *pos++ = '\0';
2873         if (os_strcmp(cmd, "bonjour") == 0)
2874                 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
2875         if (os_strcmp(cmd, "upnp") == 0)
2876                 return p2p_ctrl_service_del_upnp(wpa_s, pos);
2877         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
2878         return -1;
2882 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
2884         u8 addr[ETH_ALEN];
2886         /* <addr> */
2888         if (hwaddr_aton(cmd, addr))
2889                 return -1;
2891         return wpas_p2p_reject(wpa_s, addr);
2895 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
2897         char *pos;
2898         int id;
2899         struct wpa_ssid *ssid;
2900         u8 peer[ETH_ALEN];
2902         id = atoi(cmd);
2903         pos = os_strstr(cmd, " peer=");
2904         if (pos) {
2905                 pos += 6;
2906                 if (hwaddr_aton(pos, peer))
2907                         return -1;
2908         }
2909         ssid = wpa_config_get_network(wpa_s->conf, id);
2910         if (ssid == NULL || ssid->disabled != 2) {
2911                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2912                            "for persistent P2P group",
2913                            id);
2914                 return -1;
2915         }
2917         return wpas_p2p_invite(wpa_s, pos ? peer : NULL, ssid, NULL);
2921 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
2923         char *pos;
2924         u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
2926         pos = os_strstr(cmd, " peer=");
2927         if (!pos)
2928                 return -1;
2930         *pos = '\0';
2931         pos += 6;
2932         if (hwaddr_aton(pos, peer)) {
2933                 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
2934                 return -1;
2935         }
2937         pos = os_strstr(pos, " go_dev_addr=");
2938         if (pos) {
2939                 pos += 13;
2940                 if (hwaddr_aton(pos, go_dev_addr)) {
2941                         wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
2942                                    pos);
2943                         return -1;
2944                 }
2945                 go_dev = go_dev_addr;
2946         }
2948         return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
2952 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
2954         if (os_strncmp(cmd, "persistent=", 11) == 0)
2955                 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
2956         if (os_strncmp(cmd, "group=", 6) == 0)
2957                 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
2959         return -1;
2963 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
2964                                          char *cmd, int freq)
2966         int id;
2967         struct wpa_ssid *ssid;
2969         id = atoi(cmd);
2970         ssid = wpa_config_get_network(wpa_s->conf, id);
2971         if (ssid == NULL || ssid->disabled != 2) {
2972                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2973                            "for persistent P2P group",
2974                            id);
2975                 return -1;
2976         }
2978         return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq);
2982 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
2984         int freq = 0;
2985         char *pos;
2987         pos = os_strstr(cmd, "freq=");
2988         if (pos)
2989                 freq = atoi(pos + 5);
2991         if (os_strncmp(cmd, "persistent=", 11) == 0)
2992                 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq);
2993         if (os_strcmp(cmd, "persistent") == 0 ||
2994             os_strncmp(cmd, "persistent ", 11) == 0)
2995                 return wpas_p2p_group_add(wpa_s, 1, freq);
2996         if (os_strncmp(cmd, "freq=", 5) == 0)
2997                 return wpas_p2p_group_add(wpa_s, 0, freq);
2999         wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
3000                    cmd);
3001         return -1;
3005 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
3006                          char *buf, size_t buflen)
3008         u8 addr[ETH_ALEN], *addr_ptr;
3009         int next, res;
3010         const struct p2p_peer_info *info;
3011         char *pos, *end;
3012         char devtype[WPS_DEV_TYPE_BUFSIZE];
3013         struct wpa_ssid *ssid;
3015         if (!wpa_s->global->p2p)
3016                 return -1;
3018         if (os_strcmp(cmd, "FIRST") == 0) {
3019                 addr_ptr = NULL;
3020                 next = 0;
3021         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3022                 if (hwaddr_aton(cmd + 5, addr) < 0)
3023                         return -1;
3024                 addr_ptr = addr;
3025                 next = 1;
3026         } else {
3027                 if (hwaddr_aton(cmd, addr) < 0)
3028                         return -1;
3029                 addr_ptr = addr;
3030                 next = 0;
3031         }
3033         info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
3034         if (info == NULL)
3035                 return -1;
3037         pos = buf;
3038         end = buf + buflen;
3040         res = os_snprintf(pos, end - pos, MACSTR "\n"
3041                           "pri_dev_type=%s\n"
3042                           "device_name=%s\n"
3043                           "manufacturer=%s\n"
3044                           "model_name=%s\n"
3045                           "model_number=%s\n"
3046                           "serial_number=%s\n"
3047                           "config_methods=0x%x\n"
3048                           "dev_capab=0x%x\n"
3049                           "group_capab=0x%x\n"
3050                           "level=%d\n",
3051                           MAC2STR(info->p2p_device_addr),
3052                           wps_dev_type_bin2str(info->pri_dev_type,
3053                                                devtype, sizeof(devtype)),
3054                           info->device_name,
3055                           info->manufacturer,
3056                           info->model_name,
3057                           info->model_number,
3058                           info->serial_number,
3059                           info->config_methods,
3060                           info->dev_capab,
3061                           info->group_capab,
3062                           info->level);
3063         if (res < 0 || res >= end - pos)
3064                 return pos - buf;
3065         pos += res;
3067         ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
3068         if (ssid) {
3069                 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
3070                 if (res < 0 || res >= end - pos)
3071                         return pos - buf;
3072                 pos += res;
3073         }
3075         res = p2p_get_peer_info_txt(info, pos, end - pos);
3076         if (res < 0)
3077                 return pos - buf;
3078         pos += res;
3080         return pos - buf;
3084 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
3086         char *param;
3088         if (wpa_s->global->p2p == NULL)
3089                 return -1;
3091         param = os_strchr(cmd, ' ');
3092         if (param == NULL)
3093                 return -1;
3094         *param++ = '\0';
3096         if (os_strcmp(cmd, "discoverability") == 0) {
3097                 p2p_set_client_discoverability(wpa_s->global->p2p,
3098                                                atoi(param));
3099                 return 0;
3100         }
3102         if (os_strcmp(cmd, "managed") == 0) {
3103                 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
3104                 return 0;
3105         }
3107         if (os_strcmp(cmd, "listen_channel") == 0) {
3108                 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
3109                                               atoi(param));
3110         }
3112         if (os_strcmp(cmd, "ssid_postfix") == 0) {
3113                 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
3114                                             os_strlen(param));
3115         }
3117         if (os_strcmp(cmd, "noa") == 0) {
3118                 char *pos;
3119                 int count, start, duration;
3120                 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
3121                 count = atoi(param);
3122                 pos = os_strchr(param, ',');
3123                 if (pos == NULL)
3124                         return -1;
3125                 pos++;
3126                 start = atoi(pos);
3127                 pos = os_strchr(pos, ',');
3128                 if (pos == NULL)
3129                         return -1;
3130                 pos++;
3131                 duration = atoi(pos);
3132                 if (count < 0 || count > 255 || start < 0 || duration < 0)
3133                         return -1;
3134                 if (count == 0 && duration > 0)
3135                         return -1;
3136                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
3137                            "start=%d duration=%d", count, start, duration);
3138                 return wpas_p2p_set_noa(wpa_s, count, start, duration);
3139         }
3141         if (os_strcmp(cmd, "ps") == 0)
3142                 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
3144         if (os_strcmp(cmd, "oppps") == 0)
3145                 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
3147         if (os_strcmp(cmd, "ctwindow") == 0)
3148                 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
3150         if (os_strcmp(cmd, "disabled") == 0) {
3151                 wpa_s->global->p2p_disabled = atoi(param);
3152                 wpa_printf(MSG_DEBUG, "P2P functionality %s",
3153                            wpa_s->global->p2p_disabled ?
3154                            "disabled" : "enabled");
3155                 if (wpa_s->global->p2p_disabled) {
3156                         wpas_p2p_stop_find(wpa_s);
3157                         os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3158                         p2p_flush(wpa_s->global->p2p);
3159                 }
3160                 return 0;
3161         }
3163         if (os_strcmp(cmd, "force_long_sd") == 0) {
3164                 wpa_s->force_long_sd = atoi(param);
3165                 return 0;
3166         }
3168         if (os_strcmp(cmd, "peer_filter") == 0) {
3169                 u8 addr[ETH_ALEN];
3170                 if (hwaddr_aton(param, addr))
3171                         return -1;
3172                 p2p_set_peer_filter(wpa_s->global->p2p, addr);
3173                 return 0;
3174         }
3176         if (os_strcmp(cmd, "cross_connect") == 0)
3177                 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
3179         if (os_strcmp(cmd, "go_apsd") == 0) {
3180                 if (os_strcmp(param, "disable") == 0)
3181                         wpa_s->set_ap_uapsd = 0;
3182                 else {
3183                         wpa_s->set_ap_uapsd = 1;
3184                         wpa_s->ap_uapsd = atoi(param);
3185                 }
3186                 return 0;
3187         }
3189         if (os_strcmp(cmd, "client_apsd") == 0) {
3190                 if (os_strcmp(param, "disable") == 0)
3191                         wpa_s->set_sta_uapsd = 0;
3192                 else {
3193                         int be, bk, vi, vo;
3194                         char *pos;
3195                         /* format: BE,BK,VI,VO;max SP Length */
3196                         be = atoi(param);
3197                         pos = os_strchr(param, ',');
3198                         if (pos == NULL)
3199                                 return -1;
3200                         pos++;
3201                         bk = atoi(pos);
3202                         pos = os_strchr(pos, ',');
3203                         if (pos == NULL)
3204                                 return -1;
3205                         pos++;
3206                         vi = atoi(pos);
3207                         pos = os_strchr(pos, ',');
3208                         if (pos == NULL)
3209                                 return -1;
3210                         pos++;
3211                         vo = atoi(pos);
3212                         /* ignore max SP Length for now */
3214                         wpa_s->set_sta_uapsd = 1;
3215                         wpa_s->sta_uapsd = 0;
3216                         if (be)
3217                                 wpa_s->sta_uapsd |= BIT(0);
3218                         if (bk)
3219                                 wpa_s->sta_uapsd |= BIT(1);
3220                         if (vi)
3221                                 wpa_s->sta_uapsd |= BIT(2);
3222                         if (vo)
3223                                 wpa_s->sta_uapsd |= BIT(3);
3224                 }
3225                 return 0;
3226         }
3228         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
3229                    cmd);
3231         return -1;
3235 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
3237         char *pos, *pos2;
3238         unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
3240         if (cmd[0]) {
3241                 pos = os_strchr(cmd, ' ');
3242                 if (pos == NULL)
3243                         return -1;
3244                 *pos++ = '\0';
3245                 dur1 = atoi(cmd);
3247                 pos2 = os_strchr(pos, ' ');
3248                 if (pos2)
3249                         *pos2++ = '\0';
3250                 int1 = atoi(pos);
3251         } else
3252                 pos2 = NULL;
3254         if (pos2) {
3255                 pos = os_strchr(pos2, ' ');
3256                 if (pos == NULL)
3257                         return -1;
3258                 *pos++ = '\0';
3259                 dur2 = atoi(pos2);
3260                 int2 = atoi(pos);
3261         }
3263         return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
3267 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
3269         char *pos;
3270         unsigned int period = 0, interval = 0;
3272         if (cmd[0]) {
3273                 pos = os_strchr(cmd, ' ');
3274                 if (pos == NULL)
3275                         return -1;
3276                 *pos++ = '\0';
3277                 period = atoi(cmd);
3278                 interval = atoi(pos);
3279         }
3281         return wpas_p2p_ext_listen(wpa_s, period, interval);
3284 #endif /* CONFIG_P2P */
3287 #ifdef CONFIG_INTERWORKING
3288 static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
3290         u8 bssid[ETH_ALEN];
3291         struct wpa_bss *bss;
3293         if (hwaddr_aton(dst, bssid)) {
3294                 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
3295                 return -1;
3296         }
3298         bss = wpa_bss_get_bssid(wpa_s, bssid);
3299         if (bss == NULL) {
3300                 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
3301                            MAC2STR(bssid));
3302                 return -1;
3303         }
3305         return interworking_connect(wpa_s, bss);
3309 static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
3311         u8 dst_addr[ETH_ALEN];
3312         int used;
3313         char *pos;
3314 #define MAX_ANQP_INFO_ID 100
3315         u16 id[MAX_ANQP_INFO_ID];
3316         size_t num_id = 0;
3318         used = hwaddr_aton2(dst, dst_addr);
3319         if (used < 0)
3320                 return -1;
3321         pos = dst + used;
3322         while (num_id < MAX_ANQP_INFO_ID) {
3323                 id[num_id] = atoi(pos);
3324                 if (id[num_id])
3325                         num_id++;
3326                 pos = os_strchr(pos + 1, ',');
3327                 if (pos == NULL)
3328                         break;
3329                 pos++;
3330         }
3332         if (num_id == 0)
3333                 return -1;
3335         return anqp_send_req(wpa_s, dst_addr, id, num_id);
3337 #endif /* CONFIG_INTERWORKING */
3340 static int wpa_supplicant_ctrl_iface_sta_autoconnect(
3341         struct wpa_supplicant *wpa_s, char *cmd)
3343         wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
3344         return 0;
3348 static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
3349                                       size_t buflen)
3351         struct wpa_signal_info si;
3352         int ret;
3354         ret = wpa_drv_signal_poll(wpa_s, &si);
3355         if (ret)
3356                 return -1;
3358         ret = os_snprintf(buf, buflen, "RSSI=%d\nLINKSPEED=%d\n"
3359                           "NOISE=%d\nFREQUENCY=%u\n",
3360                           si.current_signal, si.current_txrate / 1000,
3361                           si.current_noise, si.frequency);
3362         if (ret < 0 || (unsigned int) ret > buflen)
3363                 return -1;
3364         return ret;
3368 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
3369                                          char *buf, size_t *resp_len)
3371         char *reply;
3372         const int reply_size = 4096;
3373         int ctrl_rsp = 0;
3374         int reply_len;
3376         if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
3377             os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
3378                 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
3379                                       (const u8 *) buf, os_strlen(buf));
3380         } else {
3381                 int level = MSG_DEBUG;
3382                 if (os_strcmp(buf, "PING") == 0)
3383                         level = MSG_EXCESSIVE;
3384                 wpa_hexdump_ascii(level, "RX ctrl_iface",
3385                                   (const u8 *) buf, os_strlen(buf));
3386         }
3388         reply = os_malloc(reply_size);
3389         if (reply == NULL) {
3390                 *resp_len = 1;
3391                 return NULL;
3392         }
3394         os_memcpy(reply, "OK\n", 3);
3395         reply_len = 3;
3397         if (os_strcmp(buf, "PING") == 0) {
3398                 os_memcpy(reply, "PONG\n", 5);
3399                 reply_len = 5;
3400         } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3401                 if (wpa_debug_reopen_file() < 0)
3402                         reply_len = -1;
3403         } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
3404                 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
3405         } else if (os_strcmp(buf, "MIB") == 0) {
3406                 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
3407                 if (reply_len >= 0) {
3408                         int res;
3409                         res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
3410                                                reply_size - reply_len);
3411                         if (res < 0)
3412                                 reply_len = -1;
3413                         else
3414                                 reply_len += res;
3415                 }
3416         } else if (os_strncmp(buf, "STATUS", 6) == 0) {
3417                 reply_len = wpa_supplicant_ctrl_iface_status(
3418                         wpa_s, buf + 6, reply, reply_size);
3419         } else if (os_strcmp(buf, "PMKSA") == 0) {
3420                 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
3421                                                     reply_size);
3422         } else if (os_strncmp(buf, "SET ", 4) == 0) {
3423                 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
3424                         reply_len = -1;
3425         } else if (os_strncmp(buf, "GET ", 4) == 0) {
3426                 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
3427                                                           reply, reply_size);
3428         } else if (os_strcmp(buf, "LOGON") == 0) {
3429                 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
3430         } else if (os_strcmp(buf, "LOGOFF") == 0) {
3431                 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
3432         } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
3433                 wpa_s->normal_scans = 0;
3434                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3435                         reply_len = -1;
3436                 else {
3437                         wpa_s->disconnected = 0;
3438                         wpa_s->reassociate = 1;
3439                         wpa_supplicant_req_scan(wpa_s, 0, 0);
3440                 }
3441         } else if (os_strcmp(buf, "RECONNECT") == 0) {
3442                 wpa_s->normal_scans = 0;
3443                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3444                         reply_len = -1;
3445                 else if (wpa_s->disconnected) {
3446                         wpa_s->disconnected = 0;
3447                         wpa_s->reassociate = 1;
3448                         wpa_supplicant_req_scan(wpa_s, 0, 0);
3449                 }
3450 #ifdef IEEE8021X_EAPOL
3451         } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
3452                 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
3453                         reply_len = -1;
3454 #endif /* IEEE8021X_EAPOL */
3455 #ifdef CONFIG_PEERKEY
3456         } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
3457                 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
3458                         reply_len = -1;
3459 #endif /* CONFIG_PEERKEY */
3460 #ifdef CONFIG_IEEE80211R
3461         } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
3462                 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
3463                         reply_len = -1;
3464 #endif /* CONFIG_IEEE80211R */
3465 #ifdef CONFIG_WPS
3466         } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3467                 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
3468                 if (res == -2) {
3469                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
3470                         reply_len = 17;
3471                 } else if (res)
3472                         reply_len = -1;
3473         } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
3474                 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
3475                 if (res == -2) {
3476                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
3477                         reply_len = 17;
3478                 } else if (res)
3479                         reply_len = -1;
3480         } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
3481                 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
3482                                                               reply,
3483                                                               reply_size);
3484         } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
3485                 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
3486                         wpa_s, buf + 14, reply, reply_size);
3487         } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
3488                 if (wpas_wps_cancel(wpa_s))
3489                         reply_len = -1;
3490 #ifdef CONFIG_WPS_OOB
3491         } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
3492                 if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
3493                         reply_len = -1;
3494 #endif /* CONFIG_WPS_OOB */
3495         } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
3496                 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
3497                         reply_len = -1;
3498 #ifdef CONFIG_AP
3499         } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
3500                 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
3501                         wpa_s, buf + 11, reply, reply_size);
3502 #endif /* CONFIG_AP */
3503 #ifdef CONFIG_WPS_ER
3504         } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
3505                 if (wpas_wps_er_start(wpa_s, NULL))
3506                         reply_len = -1;
3507         } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
3508                 if (wpas_wps_er_start(wpa_s, buf + 13))
3509                         reply_len = -1;
3510         } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
3511                 if (wpas_wps_er_stop(wpa_s))
3512                         reply_len = -1;
3513         } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
3514                 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
3515                         reply_len = -1;
3516         } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
3517                 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
3518                 if (ret == -2) {
3519                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
3520                         reply_len = 17;
3521                 } else if (ret == -3) {
3522                         os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
3523                         reply_len = 18;
3524                 } else if (ret == -4) {
3525                         os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
3526                         reply_len = 20;
3527                 } else if (ret)
3528                         reply_len = -1;
3529         } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
3530                 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
3531                         reply_len = -1;
3532         } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
3533                 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
3534                                                                 buf + 18))
3535                         reply_len = -1;
3536         } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
3537                 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
3538                         reply_len = -1;
3539 #endif /* CONFIG_WPS_ER */
3540 #endif /* CONFIG_WPS */
3541 #ifdef CONFIG_IBSS_RSN
3542         } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
3543                 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
3544                         reply_len = -1;
3545 #endif /* CONFIG_IBSS_RSN */
3546 #ifdef CONFIG_P2P
3547         } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
3548                 if (p2p_ctrl_find(wpa_s, buf + 9))
3549                         reply_len = -1;
3550         } else if (os_strcmp(buf, "P2P_FIND") == 0) {
3551                 if (p2p_ctrl_find(wpa_s, ""))
3552                         reply_len = -1;
3553         } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
3554                 wpas_p2p_stop_find(wpa_s);
3555         } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
3556                 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
3557                                              reply_size);
3558         } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
3559                 if (p2p_ctrl_listen(wpa_s, buf + 11))
3560                         reply_len = -1;
3561         } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
3562                 if (p2p_ctrl_listen(wpa_s, ""))
3563                         reply_len = -1;
3564         } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
3565                 if (wpas_p2p_group_remove(wpa_s, buf + 17))
3566                         reply_len = -1;
3567         } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
3568                 if (wpas_p2p_group_add(wpa_s, 0, 0))
3569                         reply_len = -1;
3570         } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
3571                 if (p2p_ctrl_group_add(wpa_s, buf + 14))
3572                         reply_len = -1;
3573         } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
3574                 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
3575                         reply_len = -1;
3576         } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
3577                 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
3578         } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
3579                 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
3580                                                    reply_size);
3581         } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
3582                 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
3583                         reply_len = -1;
3584         } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
3585                 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
3586                         reply_len = -1;
3587         } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
3588                 wpas_p2p_sd_service_update(wpa_s);
3589         } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
3590                 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
3591                         reply_len = -1;
3592         } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
3593                 wpas_p2p_service_flush(wpa_s);
3594         } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
3595                 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
3596                         reply_len = -1;
3597         } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
3598                 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
3599                         reply_len = -1;
3600         } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
3601                 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
3602                         reply_len = -1;
3603         } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
3604                 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
3605                         reply_len = -1;
3606         } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
3607                 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
3608                                               reply_size);
3609         } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
3610                 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
3611                         reply_len = -1;
3612         } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
3613                 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3614                 wpa_s->force_long_sd = 0;
3615                 if (wpa_s->global->p2p)
3616                         p2p_flush(wpa_s->global->p2p);
3617         } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
3618                 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
3619                         reply_len = -1;
3620         } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
3621                 if (wpas_p2p_cancel(wpa_s))
3622                         reply_len = -1;
3623         } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
3624                 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
3625                         reply_len = -1;
3626         } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
3627                 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
3628                         reply_len = -1;
3629         } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
3630                 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
3631                         reply_len = -1;
3632         } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
3633                 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
3634                         reply_len = -1;
3635 #endif /* CONFIG_P2P */
3636 #ifdef CONFIG_INTERWORKING
3637         } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
3638                 if (interworking_fetch_anqp(wpa_s) < 0)
3639                         reply_len = -1;
3640         } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
3641                 interworking_stop_fetch_anqp(wpa_s);
3642         } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
3643                 if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
3644                                         NULL) < 0)
3645                         reply_len = -1;
3646         } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
3647                 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
3648                         reply_len = -1;
3649         } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
3650                 if (get_anqp(wpa_s, buf + 9) < 0)
3651                         reply_len = -1;
3652 #endif /* CONFIG_INTERWORKING */
3653         } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
3654         {
3655                 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
3656                             wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
3657                         reply_len = -1;
3658                 else
3659                         ctrl_rsp = 1;
3660         } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
3661                 if (wpa_supplicant_reload_configuration(wpa_s))
3662                         reply_len = -1;
3663         } else if (os_strcmp(buf, "TERMINATE") == 0) {
3664                 wpa_supplicant_terminate_proc(wpa_s->global);
3665         } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
3666                 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
3667                         reply_len = -1;
3668         } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
3669                 reply_len = wpa_supplicant_ctrl_iface_blacklist(
3670                         wpa_s, buf + 9, reply, reply_size);
3671         } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
3672                 reply_len = wpa_supplicant_ctrl_iface_log_level(
3673                         wpa_s, buf + 9, reply, reply_size);
3674         } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
3675                 reply_len = wpa_supplicant_ctrl_iface_list_networks(
3676                         wpa_s, reply, reply_size);
3677         } else if (os_strcmp(buf, "DISCONNECT") == 0) {
3678                 wpa_s->reassociate = 0;
3679                 wpa_s->disconnected = 1;
3680                 wpa_supplicant_cancel_sched_scan(wpa_s);
3681                 wpa_supplicant_deauthenticate(wpa_s,
3682                                               WLAN_REASON_DEAUTH_LEAVING);
3683         } else if (os_strcmp(buf, "SCAN") == 0) {
3684                 wpa_s->normal_scans = 0;
3685                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3686                         reply_len = -1;
3687                 else {
3688                         if (!wpa_s->scanning &&
3689                             ((wpa_s->wpa_state <= WPA_SCANNING) ||
3690                              (wpa_s->wpa_state == WPA_COMPLETED))) {
3691                                 wpa_s->scan_req = 2;
3692                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
3693                         } else {
3694                                 wpa_printf(MSG_DEBUG, "Ongoing scan action - "
3695                                            "reject new request");
3696                                 reply_len = os_snprintf(reply, reply_size,
3697                                                         "FAIL-BUSY\n");
3698                         }
3699                 }
3700         } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
3701                 reply_len = wpa_supplicant_ctrl_iface_scan_results(
3702                         wpa_s, reply, reply_size);
3703         } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
3704                 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
3705                         reply_len = -1;
3706         } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
3707                 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
3708                         reply_len = -1;
3709         } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
3710                 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
3711                         reply_len = -1;
3712         } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
3713                 reply_len = wpa_supplicant_ctrl_iface_add_network(
3714                         wpa_s, reply, reply_size);
3715         } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
3716                 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
3717                         reply_len = -1;
3718         } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
3719                 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
3720                         reply_len = -1;
3721         } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
3722                 reply_len = wpa_supplicant_ctrl_iface_get_network(
3723                         wpa_s, buf + 12, reply, reply_size);
3724 #ifndef CONFIG_NO_CONFIG_WRITE
3725         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
3726                 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
3727                         reply_len = -1;
3728 #endif /* CONFIG_NO_CONFIG_WRITE */
3729         } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
3730                 reply_len = wpa_supplicant_ctrl_iface_get_capability(
3731                         wpa_s, buf + 15, reply, reply_size);
3732         } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
3733                 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
3734                         reply_len = -1;
3735         } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
3736                 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
3737                         reply_len = -1;
3738         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
3739                 reply_len = wpa_supplicant_global_iface_list(
3740                         wpa_s->global, reply, reply_size);
3741         } else if (os_strcmp(buf, "INTERFACES") == 0) {
3742                 reply_len = wpa_supplicant_global_iface_interfaces(
3743                         wpa_s->global, reply, reply_size);
3744         } else if (os_strncmp(buf, "BSS ", 4) == 0) {
3745                 reply_len = wpa_supplicant_ctrl_iface_bss(
3746                         wpa_s, buf + 4, reply, reply_size);
3747 #ifdef CONFIG_AP
3748         } else if (os_strcmp(buf, "STA-FIRST") == 0) {
3749                 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
3750         } else if (os_strncmp(buf, "STA ", 4) == 0) {
3751                 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
3752                                               reply_size);
3753         } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3754                 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
3755                                                    reply_size);
3756 #endif /* CONFIG_AP */
3757         } else if (os_strcmp(buf, "SUSPEND") == 0) {
3758                 wpas_notify_suspend(wpa_s->global);
3759         } else if (os_strcmp(buf, "RESUME") == 0) {
3760                 wpas_notify_resume(wpa_s->global);
3761         } else if (os_strcmp(buf, "DROP_SA") == 0) {
3762                 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
3763         } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
3764                 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
3765                         reply_len = -1;
3766         } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
3767                 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
3768                         reply_len = -1;
3769         } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
3770                 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
3771                         reply_len = -1;
3772         } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
3773                 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
3774                                                                buf + 17))
3775                         reply_len = -1;
3776 #ifdef CONFIG_TDLS
3777         } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
3778                 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
3779                         reply_len = -1;
3780         } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
3781                 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
3782                         reply_len = -1;
3783         } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
3784                 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
3785                         reply_len = -1;
3786 #endif /* CONFIG_TDLS */
3787         } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
3788                 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
3789                                                        reply_size);
3790         } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
3791                 eapol_sm_request_reauth(wpa_s->eapol);
3792         } else {
3793                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
3794                 reply_len = 16;
3795         }
3797         if (reply_len < 0) {
3798                 os_memcpy(reply, "FAIL\n", 5);
3799                 reply_len = 5;
3800         }
3802         if (ctrl_rsp)
3803                 eapol_sm_notify_ctrl_response(wpa_s->eapol);
3805         *resp_len = reply_len;
3806         return reply;
3810 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
3811                                            char *cmd)
3813         struct wpa_interface iface;
3814         char *pos;
3816         /*
3817          * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
3818          * TAB<bridge_ifname>
3819          */
3820         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
3822         os_memset(&iface, 0, sizeof(iface));
3824         do {
3825                 iface.ifname = pos = cmd;
3826                 pos = os_strchr(pos, '\t');
3827                 if (pos)
3828                         *pos++ = '\0';
3829                 if (iface.ifname[0] == '\0')
3830                         return -1;
3831                 if (pos == NULL)
3832                         break;
3834                 iface.confname = pos;
3835                 pos = os_strchr(pos, '\t');
3836                 if (pos)
3837                         *pos++ = '\0';
3838                 if (iface.confname[0] == '\0')
3839                         iface.confname = NULL;
3840                 if (pos == NULL)
3841                         break;
3843                 iface.driver = pos;
3844                 pos = os_strchr(pos, '\t');
3845                 if (pos)
3846                         *pos++ = '\0';
3847                 if (iface.driver[0] == '\0')
3848                         iface.driver = NULL;
3849                 if (pos == NULL)
3850                         break;
3852                 iface.ctrl_interface = pos;
3853                 pos = os_strchr(pos, '\t');
3854                 if (pos)
3855                         *pos++ = '\0';
3856                 if (iface.ctrl_interface[0] == '\0')
3857                         iface.ctrl_interface = NULL;
3858                 if (pos == NULL)
3859                         break;
3861                 iface.driver_param = pos;
3862                 pos = os_strchr(pos, '\t');
3863                 if (pos)
3864                         *pos++ = '\0';
3865                 if (iface.driver_param[0] == '\0')
3866                         iface.driver_param = NULL;
3867                 if (pos == NULL)
3868                         break;
3870                 iface.bridge_ifname = pos;
3871                 pos = os_strchr(pos, '\t');
3872                 if (pos)
3873                         *pos++ = '\0';
3874                 if (iface.bridge_ifname[0] == '\0')
3875                         iface.bridge_ifname = NULL;
3876                 if (pos == NULL)
3877                         break;
3878         } while (0);
3880         if (wpa_supplicant_get_iface(global, iface.ifname))
3881                 return -1;
3883         return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
3887 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
3888                                               char *cmd)
3890         struct wpa_supplicant *wpa_s;
3892         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
3894         wpa_s = wpa_supplicant_get_iface(global, cmd);
3895         if (wpa_s == NULL)
3896                 return -1;
3897         return wpa_supplicant_remove_iface(global, wpa_s, 0);
3901 static void wpa_free_iface_info(struct wpa_interface_info *iface)
3903         struct wpa_interface_info *prev;
3905         while (iface) {
3906                 prev = iface;
3907                 iface = iface->next;
3909                 os_free(prev->ifname);
3910                 os_free(prev->desc);
3911                 os_free(prev);
3912         }
3916 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
3917                                             char *buf, int len)
3919         int i, res;
3920         struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
3921         char *pos, *end;
3923         for (i = 0; wpa_drivers[i]; i++) {
3924                 struct wpa_driver_ops *drv = wpa_drivers[i];
3925                 if (drv->get_interfaces == NULL)
3926                         continue;
3927                 tmp = drv->get_interfaces(global->drv_priv[i]);
3928                 if (tmp == NULL)
3929                         continue;
3931                 if (last == NULL)
3932                         iface = last = tmp;
3933                 else
3934                         last->next = tmp;
3935                 while (last->next)
3936                         last = last->next;
3937         }
3939         pos = buf;
3940         end = buf + len;
3941         for (tmp = iface; tmp; tmp = tmp->next) {
3942                 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
3943                                   tmp->drv_name, tmp->ifname,
3944                                   tmp->desc ? tmp->desc : "");
3945                 if (res < 0 || res >= end - pos) {
3946                         *pos = '\0';
3947                         break;
3948                 }
3949                 pos += res;
3950         }
3952         wpa_free_iface_info(iface);
3954         return pos - buf;
3958 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
3959                                                   char *buf, int len)
3961         int res;
3962         char *pos, *end;
3963         struct wpa_supplicant *wpa_s;
3965         wpa_s = global->ifaces;
3966         pos = buf;
3967         end = buf + len;
3969         while (wpa_s) {
3970                 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
3971                 if (res < 0 || res >= end - pos) {
3972                         *pos = '\0';
3973                         break;
3974                 }
3975                 pos += res;
3976                 wpa_s = wpa_s->next;
3977         }
3978         return pos - buf;
3982 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
3983                                                 char *buf, size_t *resp_len)
3985         char *reply;
3986         const int reply_size = 2048;
3987         int reply_len;
3988         int level = MSG_DEBUG;
3990         if (os_strcmp(buf, "PING") == 0)
3991                 level = MSG_EXCESSIVE;
3992         wpa_hexdump_ascii(level, "RX global ctrl_iface",
3993                           (const u8 *) buf, os_strlen(buf));
3995         reply = os_malloc(reply_size);
3996         if (reply == NULL) {
3997                 *resp_len = 1;
3998                 return NULL;
3999         }
4001         os_memcpy(reply, "OK\n", 3);
4002         reply_len = 3;
4004         if (os_strcmp(buf, "PING") == 0) {
4005                 os_memcpy(reply, "PONG\n", 5);
4006                 reply_len = 5;
4007         } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
4008                 if (wpa_supplicant_global_iface_add(global, buf + 14))
4009                         reply_len = -1;
4010         } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
4011                 if (wpa_supplicant_global_iface_remove(global, buf + 17))
4012                         reply_len = -1;
4013         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
4014                 reply_len = wpa_supplicant_global_iface_list(
4015                         global, reply, reply_size);
4016         } else if (os_strcmp(buf, "INTERFACES") == 0) {
4017                 reply_len = wpa_supplicant_global_iface_interfaces(
4018                         global, reply, reply_size);
4019         } else if (os_strcmp(buf, "TERMINATE") == 0) {
4020                 wpa_supplicant_terminate_proc(global);
4021         } else if (os_strcmp(buf, "SUSPEND") == 0) {
4022                 wpas_notify_suspend(global);
4023         } else if (os_strcmp(buf, "RESUME") == 0) {
4024                 wpas_notify_resume(global);
4025         } else {
4026                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
4027                 reply_len = 16;
4028         }
4030         if (reply_len < 0) {
4031                 os_memcpy(reply, "FAIL\n", 5);
4032                 reply_len = 5;
4033         }
4035         *resp_len = reply_len;
4036         return reply;