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