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