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