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