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