0f98d6871fb26b0f88b0e77fdca5ee9f5861b9db
[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         wpas_p2p_find(wpa_s, timeout, type);
1993         return 0;
1994 }
1995
1996
1997 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
1998                             char *buf, size_t buflen)
1999 {
2000         u8 addr[ETH_ALEN];
2001         char *pos, *pos2;
2002         char *pin = NULL;
2003         enum p2p_wps_method wps_method;
2004         int new_pin;
2005         int ret;
2006         int persistent_group;
2007         int join;
2008         int auth;
2009         int go_intent = -1;
2010         int freq = 0;
2011
2012         /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad] [persistent]
2013          * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] */
2014
2015         if (hwaddr_aton(cmd, addr))
2016                 return -1;
2017
2018         pos = cmd + 17;
2019         if (*pos != ' ')
2020                 return -1;
2021         pos++;
2022
2023         persistent_group = os_strstr(pos, " persistent") != NULL;
2024         join = os_strstr(pos, " join") != NULL;
2025         auth = os_strstr(pos, " auth") != NULL;
2026
2027         pos2 = os_strstr(pos, " go_intent=");
2028         if (pos2) {
2029                 pos2 += 11;
2030                 go_intent = atoi(pos2);
2031                 if (go_intent < 0 || go_intent > 15)
2032                         return -1;
2033         }
2034
2035         pos2 = os_strstr(pos, " freq=");
2036         if (pos2) {
2037                 pos2 += 6;
2038                 freq = atoi(pos2);
2039                 if (freq <= 0)
2040                         return -1;
2041         }
2042
2043         if (os_strncmp(pos, "pin", 3) == 0) {
2044                 /* Request random PIN (to be displayed) and enable the PIN */
2045                 wps_method = WPS_PIN_DISPLAY;
2046         } else if (os_strncmp(pos, "pbc", 3) == 0) {
2047                 wps_method = WPS_PBC;
2048         } else {
2049                 pin = pos;
2050                 pos = os_strchr(pin, ' ');
2051                 wps_method = WPS_PIN_KEYPAD;
2052                 if (pos) {
2053                         *pos++ = '\0';
2054                         if (os_strncmp(pos, "label", 5) == 0)
2055                                 wps_method = WPS_PIN_LABEL;
2056                         else if (os_strncmp(pos, "display", 7) == 0)
2057                                 wps_method = WPS_PIN_DISPLAY;
2058                 }
2059         }
2060
2061         new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
2062                                    persistent_group, join, auth, go_intent,
2063                                    freq);
2064         if (new_pin == -2) {
2065                 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
2066                 return 25;
2067         }
2068         if (new_pin == -3) {
2069                 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
2070                 return 25;
2071         }
2072         if (new_pin < 0)
2073                 return -1;
2074         if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
2075                 ret = os_snprintf(buf, buflen, "%08d", new_pin);
2076                 if (ret < 0 || (size_t) ret >= buflen)
2077                         return -1;
2078                 return ret;
2079         }
2080
2081         os_memcpy(buf, "OK\n", 3);
2082         return 3;
2083 }
2084
2085
2086 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
2087 {
2088         unsigned int timeout = atoi(cmd);
2089         return wpas_p2p_listen(wpa_s, timeout);
2090 }
2091
2092
2093 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
2094 {
2095         u8 addr[ETH_ALEN];
2096         char *pos;
2097
2098         /* <addr> <config method> */
2099
2100         if (hwaddr_aton(cmd, addr))
2101                 return -1;
2102
2103         pos = cmd + 17;
2104         if (*pos != ' ')
2105                 return -1;
2106         pos++;
2107
2108         return wpas_p2p_prov_disc(wpa_s, addr, pos);
2109 }
2110
2111
2112 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
2113                               size_t buflen)
2114 {
2115         struct wpa_ssid *ssid = wpa_s->current_ssid;
2116
2117         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2118             ssid->passphrase == NULL)
2119                 return -1;
2120
2121         os_strlcpy(buf, ssid->passphrase, buflen);
2122         return os_strlen(buf);
2123 }
2124
2125
2126 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
2127                                   char *buf, size_t buflen)
2128 {
2129         u64 ref;
2130         int res;
2131         u8 dst_buf[ETH_ALEN], *dst;
2132         struct wpabuf *tlvs;
2133         char *pos;
2134         size_t len;
2135
2136         if (hwaddr_aton(cmd, dst_buf))
2137                 return -1;
2138         dst = dst_buf;
2139         if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
2140             dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
2141                 dst = NULL;
2142         pos = cmd + 17;
2143         if (*pos != ' ')
2144                 return -1;
2145         pos++;
2146
2147         if (os_strncmp(pos, "upnp ", 5) == 0) {
2148                 u8 version;
2149                 pos += 5;
2150                 if (hexstr2bin(pos, &version, 1) < 0)
2151                         return -1;
2152                 pos += 2;
2153                 if (*pos != ' ')
2154                         return -1;
2155                 pos++;
2156                 ref = (unsigned long) wpas_p2p_sd_request_upnp(wpa_s, dst,
2157                                                                version, pos);
2158         } else {
2159                 len = os_strlen(pos);
2160                 if (len & 1)
2161                         return -1;
2162                 len /= 2;
2163                 tlvs = wpabuf_alloc(len);
2164                 if (tlvs == NULL)
2165                         return -1;
2166                 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
2167                         wpabuf_free(tlvs);
2168                         return -1;
2169                 }
2170
2171                 ref = (unsigned long) wpas_p2p_sd_request(wpa_s, dst, tlvs);
2172                 wpabuf_free(tlvs);
2173         }
2174         res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
2175         if (res < 0 || (unsigned) res >= buflen)
2176                 return -1;
2177         return res;
2178 }
2179
2180
2181 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
2182                                          char *cmd)
2183 {
2184         long long unsigned val;
2185         u64 req;
2186         if (sscanf(cmd, "%llx", &val) != 1)
2187                 return -1;
2188         req = val;
2189         return wpas_p2p_sd_cancel_request(wpa_s, (void *) (unsigned long) req);
2190 }
2191
2192
2193 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
2194 {
2195         int freq;
2196         u8 dst[ETH_ALEN];
2197         u8 dialog_token;
2198         struct wpabuf *resp_tlvs;
2199         char *pos, *pos2;
2200         size_t len;
2201
2202         pos = os_strchr(cmd, ' ');
2203         if (pos == NULL)
2204                 return -1;
2205         *pos++ = '\0';
2206         freq = atoi(cmd);
2207         if (freq == 0)
2208                 return -1;
2209
2210         if (hwaddr_aton(pos, dst))
2211                 return -1;
2212         pos += 17;
2213         if (*pos != ' ')
2214                 return -1;
2215         pos++;
2216
2217         pos2 = os_strchr(pos, ' ');
2218         if (pos2 == NULL)
2219                 return -1;
2220         *pos2++ = '\0';
2221         dialog_token = atoi(pos);
2222
2223         len = os_strlen(pos2);
2224         if (len & 1)
2225                 return -1;
2226         len /= 2;
2227         resp_tlvs = wpabuf_alloc(len);
2228         if (resp_tlvs == NULL)
2229                 return -1;
2230         if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
2231                 wpabuf_free(resp_tlvs);
2232                 return -1;
2233         }
2234
2235         wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
2236         wpabuf_free(resp_tlvs);
2237         return 0;
2238 }
2239
2240
2241 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
2242                                        char *cmd)
2243 {
2244         wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
2245         return 0;
2246 }
2247
2248
2249 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
2250                                         char *cmd)
2251 {
2252         char *pos;
2253         size_t len;
2254         struct wpabuf *query, *resp;
2255
2256         pos = os_strchr(cmd, ' ');
2257         if (pos == NULL)
2258                 return -1;
2259         *pos++ = '\0';
2260
2261         len = os_strlen(cmd);
2262         if (len & 1)
2263                 return -1;
2264         len /= 2;
2265         query = wpabuf_alloc(len);
2266         if (query == NULL)
2267                 return -1;
2268         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
2269                 wpabuf_free(query);
2270                 return -1;
2271         }
2272
2273         len = os_strlen(pos);
2274         if (len & 1) {
2275                 wpabuf_free(query);
2276                 return -1;
2277         }
2278         len /= 2;
2279         resp = wpabuf_alloc(len);
2280         if (resp == NULL) {
2281                 wpabuf_free(query);
2282                 return -1;
2283         }
2284         if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
2285                 wpabuf_free(query);
2286                 wpabuf_free(resp);
2287                 return -1;
2288         }
2289
2290         if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
2291                 wpabuf_free(query);
2292                 wpabuf_free(resp);
2293                 return -1;
2294         }
2295         return 0;
2296 }
2297
2298
2299 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
2300 {
2301         char *pos;
2302         u8 version;
2303
2304         pos = os_strchr(cmd, ' ');
2305         if (pos == NULL)
2306                 return -1;
2307         *pos++ = '\0';
2308
2309         if (hexstr2bin(cmd, &version, 1) < 0)
2310                 return -1;
2311
2312         return wpas_p2p_service_add_upnp(wpa_s, version, pos);
2313 }
2314
2315
2316 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
2317 {
2318         char *pos;
2319
2320         pos = os_strchr(cmd, ' ');
2321         if (pos == NULL)
2322                 return -1;
2323         *pos++ = '\0';
2324
2325         if (os_strcmp(cmd, "bonjour") == 0)
2326                 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
2327         if (os_strcmp(cmd, "upnp") == 0)
2328                 return p2p_ctrl_service_add_upnp(wpa_s, pos);
2329         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
2330         return -1;
2331 }
2332
2333
2334 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
2335                                         char *cmd)
2336 {
2337         size_t len;
2338         struct wpabuf *query;
2339         int ret;
2340
2341         len = os_strlen(cmd);
2342         if (len & 1)
2343                 return -1;
2344         len /= 2;
2345         query = wpabuf_alloc(len);
2346         if (query == NULL)
2347                 return -1;
2348         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
2349                 wpabuf_free(query);
2350                 return -1;
2351         }
2352
2353         ret = wpas_p2p_service_del_bonjour(wpa_s, query);
2354         wpabuf_free(query);
2355         return ret;
2356 }
2357
2358
2359 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
2360 {
2361         char *pos;
2362         u8 version;
2363
2364         pos = os_strchr(cmd, ' ');
2365         if (pos == NULL)
2366                 return -1;
2367         *pos++ = '\0';
2368
2369         if (hexstr2bin(cmd, &version, 1) < 0)
2370                 return -1;
2371
2372         return wpas_p2p_service_del_upnp(wpa_s, version, pos);
2373 }
2374
2375
2376 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
2377 {
2378         char *pos;
2379
2380         pos = os_strchr(cmd, ' ');
2381         if (pos == NULL)
2382                 return -1;
2383         *pos++ = '\0';
2384
2385         if (os_strcmp(cmd, "bonjour") == 0)
2386                 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
2387         if (os_strcmp(cmd, "upnp") == 0)
2388                 return p2p_ctrl_service_del_upnp(wpa_s, pos);
2389         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
2390         return -1;
2391 }
2392
2393
2394 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
2395 {
2396         u8 addr[ETH_ALEN];
2397
2398         /* <addr> */
2399
2400         if (hwaddr_aton(cmd, addr))
2401                 return -1;
2402
2403         return wpas_p2p_reject(wpa_s, addr);
2404 }
2405
2406
2407 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
2408 {
2409         char *pos;
2410         int id;
2411         struct wpa_ssid *ssid;
2412         u8 peer[ETH_ALEN];
2413
2414         id = atoi(cmd);
2415         pos = os_strstr(cmd, " peer=");
2416         if (pos) {
2417                 pos += 6;
2418                 if (hwaddr_aton(pos, peer))
2419                         return -1;
2420         }
2421         ssid = wpa_config_get_network(wpa_s->conf, id);
2422         if (ssid == NULL || ssid->disabled != 2) {
2423                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2424                            "for persistent P2P group",
2425                            id);
2426                 return -1;
2427         }
2428
2429         return wpas_p2p_invite(wpa_s, pos ? peer : NULL, ssid, NULL);
2430 }
2431
2432
2433 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
2434 {
2435         char *pos;
2436         u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
2437
2438         pos = os_strstr(cmd, " peer=");
2439         if (!pos)
2440                 return -1;
2441
2442         *pos = '\0';
2443         pos += 6;
2444         if (hwaddr_aton(pos, peer)) {
2445                 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
2446                 return -1;
2447         }
2448
2449         pos = os_strstr(pos, " go_dev_addr=");
2450         if (pos) {
2451                 pos += 13;
2452                 if (hwaddr_aton(pos, go_dev_addr)) {
2453                         wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
2454                                    pos);
2455                         return -1;
2456                 }
2457                 go_dev = go_dev_addr;
2458         }
2459
2460         return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
2461 }
2462
2463
2464 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
2465 {
2466         if (os_strncmp(cmd, "persistent=", 11) == 0)
2467                 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
2468         if (os_strncmp(cmd, "group=", 6) == 0)
2469                 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
2470
2471         return -1;
2472 }
2473
2474
2475 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
2476                                          char *cmd, int freq)
2477 {
2478         int id;
2479         struct wpa_ssid *ssid;
2480
2481         id = atoi(cmd);
2482         ssid = wpa_config_get_network(wpa_s->conf, id);
2483         if (ssid == NULL || ssid->disabled != 2) {
2484                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2485                            "for persistent P2P group",
2486                            id);
2487                 return -1;
2488         }
2489
2490         return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq);
2491 }
2492
2493
2494 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
2495 {
2496         int freq = 0;
2497         char *pos;
2498
2499         pos = os_strstr(cmd, "freq=");
2500         if (pos)
2501                 freq = atoi(pos + 5);
2502
2503         if (os_strncmp(cmd, "persistent=", 11) == 0)
2504                 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq);
2505         if (os_strcmp(cmd, "persistent") == 0 ||
2506             os_strncmp(cmd, "persistent ", 11) == 0)
2507                 return wpas_p2p_group_add(wpa_s, 1, freq);
2508         if (os_strncmp(cmd, "freq=", 5) == 0)
2509                 return wpas_p2p_group_add(wpa_s, 0, freq);
2510
2511         wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
2512                    cmd);
2513         return -1;
2514 }
2515
2516
2517 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
2518                          char *buf, size_t buflen)
2519 {
2520         u8 addr[ETH_ALEN], *addr_ptr;
2521         int next;
2522
2523         if (!wpa_s->global->p2p)
2524                 return -1;
2525
2526         if (os_strcmp(cmd, "FIRST") == 0) {
2527                 addr_ptr = NULL;
2528                 next = 0;
2529         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
2530                 if (hwaddr_aton(cmd + 5, addr) < 0)
2531                         return -1;
2532                 addr_ptr = addr;
2533                 next = 1;
2534         } else {
2535                 if (hwaddr_aton(cmd, addr) < 0)
2536                         return -1;
2537                 addr_ptr = addr;
2538                 next = 0;
2539         }
2540
2541         return p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next,
2542                                  buf, buflen);
2543 }
2544
2545
2546 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
2547 {
2548         char *param;
2549
2550         if (wpa_s->global->p2p == NULL)
2551                 return -1;
2552
2553         param = os_strchr(cmd, ' ');
2554         if (param == NULL)
2555                 return -1;
2556         *param++ = '\0';
2557
2558         if (os_strcmp(cmd, "discoverability") == 0) {
2559                 p2p_set_client_discoverability(wpa_s->global->p2p,
2560                                                atoi(param));
2561                 return 0;
2562         }
2563
2564         if (os_strcmp(cmd, "managed") == 0) {
2565                 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
2566                 return 0;
2567         }
2568
2569         if (os_strcmp(cmd, "listen_channel") == 0) {
2570                 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
2571                                               atoi(param));
2572         }
2573
2574         if (os_strcmp(cmd, "ssid_postfix") == 0) {
2575                 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
2576                                             os_strlen(param));
2577         }
2578
2579         if (os_strcmp(cmd, "noa") == 0) {
2580                 char *pos;
2581                 int count, start, duration;
2582                 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
2583                 count = atoi(param);
2584                 pos = os_strchr(param, ',');
2585                 if (pos == NULL)
2586                         return -1;
2587                 pos++;
2588                 start = atoi(pos);
2589                 pos = os_strchr(pos, ',');
2590                 if (pos == NULL)
2591                         return -1;
2592                 pos++;
2593                 duration = atoi(pos);
2594                 if (count < 0 || count > 255 || start < 0 || duration < 0)
2595                         return -1;
2596                 if (count == 0 && duration > 0)
2597                         return -1;
2598                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
2599                            "start=%d duration=%d", count, start, duration);
2600                 return wpas_p2p_set_noa(wpa_s, count, start, duration);
2601         }
2602
2603         if (os_strcmp(cmd, "ps") == 0)
2604                 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
2605
2606         if (os_strcmp(cmd, "oppps") == 0)
2607                 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
2608
2609         if (os_strcmp(cmd, "ctwindow") == 0)
2610                 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
2611
2612         if (os_strcmp(cmd, "disabled") == 0) {
2613                 wpa_s->global->p2p_disabled = atoi(param);
2614                 wpa_printf(MSG_DEBUG, "P2P functionality %s",
2615                            wpa_s->global->p2p_disabled ?
2616                            "disabled" : "enabled");
2617                 if (wpa_s->global->p2p_disabled) {
2618                         wpas_p2p_stop_find(wpa_s);
2619                         os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
2620                         p2p_flush(wpa_s->global->p2p);
2621                 }
2622                 return 0;
2623         }
2624
2625         if (os_strcmp(cmd, "force_long_sd") == 0) {
2626                 wpa_s->force_long_sd = atoi(param);
2627                 return 0;
2628         }
2629
2630         if (os_strcmp(cmd, "peer_filter") == 0) {
2631                 u8 addr[ETH_ALEN];
2632                 if (hwaddr_aton(param, addr))
2633                         return -1;
2634                 p2p_set_peer_filter(wpa_s->global->p2p, addr);
2635                 return 0;
2636         }
2637
2638         if (os_strcmp(cmd, "cross_connect") == 0)
2639                 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
2640
2641         if (os_strcmp(cmd, "go_apsd") == 0) {
2642                 if (os_strcmp(param, "disable") == 0)
2643                         wpa_s->set_ap_uapsd = 0;
2644                 else {
2645                         wpa_s->set_ap_uapsd = 1;
2646                         wpa_s->ap_uapsd = atoi(param);
2647                 }
2648                 return 0;
2649         }
2650
2651         if (os_strcmp(cmd, "client_apsd") == 0) {
2652                 if (os_strcmp(param, "disable") == 0)
2653                         wpa_s->set_sta_uapsd = 0;
2654                 else {
2655                         int be, bk, vi, vo;
2656                         char *pos;
2657                         /* format: BE,BK,VI,VO;max SP Length */
2658                         be = atoi(param);
2659                         pos = os_strchr(param, ',');
2660                         if (pos == NULL)
2661                                 return -1;
2662                         pos++;
2663                         bk = atoi(pos);
2664                         pos = os_strchr(pos, ',');
2665                         if (pos == NULL)
2666                                 return -1;
2667                         pos++;
2668                         vi = atoi(pos);
2669                         pos = os_strchr(pos, ',');
2670                         if (pos == NULL)
2671                                 return -1;
2672                         pos++;
2673                         vo = atoi(pos);
2674                         /* ignore max SP Length for now */
2675
2676                         wpa_s->set_sta_uapsd = 1;
2677                         wpa_s->sta_uapsd = 0;
2678                         if (be)
2679                                 wpa_s->sta_uapsd |= BIT(0);
2680                         if (bk)
2681                                 wpa_s->sta_uapsd |= BIT(1);
2682                         if (vi)
2683                                 wpa_s->sta_uapsd |= BIT(2);
2684                         if (vo)
2685                                 wpa_s->sta_uapsd |= BIT(3);
2686                 }
2687                 return 0;
2688         }
2689
2690         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
2691                    cmd);
2692
2693         return -1;
2694 }
2695
2696
2697 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
2698 {
2699         char *pos, *pos2;
2700         unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
2701
2702         if (cmd[0]) {
2703                 pos = os_strchr(cmd, ' ');
2704                 if (pos == NULL)
2705                         return -1;
2706                 *pos++ = '\0';
2707                 dur1 = atoi(cmd);
2708
2709                 pos2 = os_strchr(pos, ' ');
2710                 if (pos2)
2711                         *pos2++ = '\0';
2712                 int1 = atoi(pos);
2713         } else
2714                 pos2 = NULL;
2715
2716         if (pos2) {
2717                 pos = os_strchr(pos2, ' ');
2718                 if (pos == NULL)
2719                         return -1;
2720                 *pos++ = '\0';
2721                 dur2 = atoi(pos2);
2722                 int2 = atoi(pos);
2723         }
2724
2725         return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
2726 }
2727
2728
2729 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
2730 {
2731         char *pos;
2732         unsigned int period = 0, interval = 0;
2733
2734         if (cmd[0]) {
2735                 pos = os_strchr(cmd, ' ');
2736                 if (pos == NULL)
2737                         return -1;
2738                 *pos++ = '\0';
2739                 period = atoi(cmd);
2740                 interval = atoi(pos);
2741         }
2742
2743         return wpas_p2p_ext_listen(wpa_s, period, interval);
2744 }
2745
2746 #endif /* CONFIG_P2P */
2747
2748
2749 static int wpa_supplicant_ctrl_iface_sta_autoconnect(
2750         struct wpa_supplicant *wpa_s, char *cmd)
2751 {
2752         wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
2753         return 0;
2754 }
2755
2756
2757 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
2758                                          char *buf, size_t *resp_len)
2759 {
2760         char *reply;
2761         const int reply_size = 4096;
2762         int ctrl_rsp = 0;
2763         int reply_len;
2764
2765         if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
2766             os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
2767                 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
2768                                       (const u8 *) buf, os_strlen(buf));
2769         } else {
2770                 int level = MSG_DEBUG;
2771                 if (os_strcmp(buf, "PING") == 0)
2772                         level = MSG_EXCESSIVE;
2773                 wpa_hexdump_ascii(level, "RX ctrl_iface",
2774                                   (const u8 *) buf, os_strlen(buf));
2775         }
2776
2777         reply = os_malloc(reply_size);
2778         if (reply == NULL) {
2779                 *resp_len = 1;
2780                 return NULL;
2781         }
2782
2783         os_memcpy(reply, "OK\n", 3);
2784         reply_len = 3;
2785
2786         if (os_strcmp(buf, "PING") == 0) {
2787                 os_memcpy(reply, "PONG\n", 5);
2788                 reply_len = 5;
2789         } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
2790                 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
2791         } else if (os_strcmp(buf, "MIB") == 0) {
2792                 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
2793                 if (reply_len >= 0) {
2794                         int res;
2795                         res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
2796                                                reply_size - reply_len);
2797                         if (res < 0)
2798                                 reply_len = -1;
2799                         else
2800                                 reply_len += res;
2801                 }
2802         } else if (os_strncmp(buf, "STATUS", 6) == 0) {
2803                 reply_len = wpa_supplicant_ctrl_iface_status(
2804                         wpa_s, buf + 6, reply, reply_size);
2805         } else if (os_strcmp(buf, "PMKSA") == 0) {
2806                 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
2807                                                     reply_size);
2808         } else if (os_strncmp(buf, "SET ", 4) == 0) {
2809                 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
2810                         reply_len = -1;
2811         } else if (os_strncmp(buf, "GET ", 4) == 0) {
2812                 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
2813                                                           reply, reply_size);
2814         } else if (os_strcmp(buf, "LOGON") == 0) {
2815                 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
2816         } else if (os_strcmp(buf, "LOGOFF") == 0) {
2817                 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
2818         } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
2819                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2820                         reply_len = -1;
2821                 else {
2822                         wpa_s->disconnected = 0;
2823                         wpa_s->reassociate = 1;
2824                         wpa_supplicant_req_scan(wpa_s, 0, 0);
2825                 }
2826         } else if (os_strcmp(buf, "RECONNECT") == 0) {
2827                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2828                         reply_len = -1;
2829                 else if (wpa_s->disconnected) {
2830                         wpa_s->disconnected = 0;
2831                         wpa_s->reassociate = 1;
2832                         wpa_supplicant_req_scan(wpa_s, 0, 0);
2833                 }
2834 #ifdef IEEE8021X_EAPOL
2835         } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
2836                 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
2837                         reply_len = -1;
2838 #endif /* IEEE8021X_EAPOL */
2839 #ifdef CONFIG_PEERKEY
2840         } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
2841                 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
2842                         reply_len = -1;
2843 #endif /* CONFIG_PEERKEY */
2844 #ifdef CONFIG_IEEE80211R
2845         } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
2846                 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
2847                         reply_len = -1;
2848 #endif /* CONFIG_IEEE80211R */
2849 #ifdef CONFIG_WPS
2850         } else if (os_strcmp(buf, "WPS_PBC") == 0) {
2851                 if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL))
2852                         reply_len = -1;
2853         } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
2854                 if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8))
2855                         reply_len = -1;
2856         } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2857                 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
2858                                                               reply,
2859                                                               reply_size);
2860         } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
2861                 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
2862                         wpa_s, buf + 14, reply, reply_size);
2863         } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
2864                 if (wpas_wps_cancel(wpa_s))
2865                         reply_len = -1;
2866 #ifdef CONFIG_WPS_OOB
2867         } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
2868                 if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
2869                         reply_len = -1;
2870 #endif /* CONFIG_WPS_OOB */
2871         } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
2872                 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
2873                         reply_len = -1;
2874 #ifdef CONFIG_AP
2875         } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
2876                 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
2877                         wpa_s, buf + 11, reply, reply_size);
2878 #endif /* CONFIG_AP */
2879 #ifdef CONFIG_WPS_ER
2880         } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
2881                 if (wpas_wps_er_start(wpa_s, NULL))
2882                         reply_len = -1;
2883         } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
2884                 if (wpas_wps_er_start(wpa_s, buf + 13))
2885                         reply_len = -1;
2886         } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
2887                 if (wpas_wps_er_stop(wpa_s))
2888                         reply_len = -1;
2889         } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
2890                 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
2891                         reply_len = -1;
2892         } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
2893                 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
2894                 if (ret == -2) {
2895                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
2896                         reply_len = 17;
2897                 } else if (ret == -3) {
2898                         os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
2899                         reply_len = 18;
2900                 } else if (ret == -4) {
2901                         os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
2902                         reply_len = 20;
2903                 } else if (ret)
2904                         reply_len = -1;
2905         } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
2906                 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
2907                         reply_len = -1;
2908         } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
2909                 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
2910                                                                 buf + 18))
2911                         reply_len = -1;
2912         } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
2913                 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
2914                         reply_len = -1;
2915 #endif /* CONFIG_WPS_ER */
2916 #endif /* CONFIG_WPS */
2917 #ifdef CONFIG_IBSS_RSN
2918         } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
2919                 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
2920                         reply_len = -1;
2921 #endif /* CONFIG_IBSS_RSN */
2922 #ifdef CONFIG_P2P
2923         } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
2924                 if (p2p_ctrl_find(wpa_s, buf + 9))
2925                         reply_len = -1;
2926         } else if (os_strcmp(buf, "P2P_FIND") == 0) {
2927                 if (p2p_ctrl_find(wpa_s, ""))
2928                         reply_len = -1;
2929         } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
2930                 wpas_p2p_stop_find(wpa_s);
2931         } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
2932                 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
2933                                              reply_size);
2934         } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
2935                 if (p2p_ctrl_listen(wpa_s, buf + 11))
2936                         reply_len = -1;
2937         } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
2938                 if (p2p_ctrl_listen(wpa_s, ""))
2939                         reply_len = -1;
2940         } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
2941                 if (wpas_p2p_group_remove(wpa_s, buf + 17))
2942                         reply_len = -1;
2943         } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
2944                 if (wpas_p2p_group_add(wpa_s, 0, 0))
2945                         reply_len = -1;
2946         } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
2947                 if (p2p_ctrl_group_add(wpa_s, buf + 14))
2948                         reply_len = -1;
2949         } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
2950                 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
2951                         reply_len = -1;
2952         } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
2953                 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
2954         } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
2955                 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
2956                                                    reply_size);
2957         } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
2958                 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
2959                         reply_len = -1;
2960         } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
2961                 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
2962                         reply_len = -1;
2963         } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
2964                 wpas_p2p_sd_service_update(wpa_s);
2965         } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
2966                 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
2967                         reply_len = -1;
2968         } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
2969                 wpas_p2p_service_flush(wpa_s);
2970         } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
2971                 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
2972                         reply_len = -1;
2973         } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
2974                 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
2975                         reply_len = -1;
2976         } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
2977                 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
2978                         reply_len = -1;
2979         } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
2980                 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
2981                         reply_len = -1;
2982         } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
2983                 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
2984                                               reply_size);
2985         } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
2986                 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
2987                         reply_len = -1;
2988         } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
2989                 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
2990                 wpa_s->force_long_sd = 0;
2991                 p2p_flush(wpa_s->global->p2p);
2992         } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
2993                 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
2994                         reply_len = -1;
2995         } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
2996                 if (wpas_p2p_cancel(wpa_s))
2997                         reply_len = -1;
2998         } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
2999                 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
3000                         reply_len = -1;
3001         } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
3002                 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
3003                         reply_len = -1;
3004         } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
3005                 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
3006                         reply_len = -1;
3007         } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
3008                 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
3009                         reply_len = -1;
3010 #endif /* CONFIG_P2P */
3011         } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
3012         {
3013                 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
3014                             wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
3015                         reply_len = -1;
3016                 else
3017                         ctrl_rsp = 1;
3018         } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
3019                 if (wpa_supplicant_reload_configuration(wpa_s))
3020                         reply_len = -1;
3021         } else if (os_strcmp(buf, "TERMINATE") == 0) {
3022                 wpa_supplicant_terminate_proc(wpa_s->global);
3023         } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
3024                 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
3025                         reply_len = -1;
3026         } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
3027                 reply_len = wpa_supplicant_ctrl_iface_list_networks(
3028                         wpa_s, reply, reply_size);
3029         } else if (os_strcmp(buf, "DISCONNECT") == 0) {
3030                 wpa_s->reassociate = 0;
3031                 wpa_s->disconnected = 1;
3032                 wpa_supplicant_deauthenticate(wpa_s,
3033                                               WLAN_REASON_DEAUTH_LEAVING);
3034         } else if (os_strcmp(buf, "SCAN") == 0) {
3035                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3036                         reply_len = -1;
3037                 else {
3038                         wpa_s->scan_req = 2;
3039                         wpa_supplicant_req_scan(wpa_s, 0, 0);
3040                 }
3041         } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
3042                 reply_len = wpa_supplicant_ctrl_iface_scan_results(
3043                         wpa_s, reply, reply_size);
3044         } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
3045                 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
3046                         reply_len = -1;
3047         } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
3048                 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
3049                         reply_len = -1;
3050         } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
3051                 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
3052                         reply_len = -1;
3053         } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
3054                 reply_len = wpa_supplicant_ctrl_iface_add_network(
3055                         wpa_s, reply, reply_size);
3056         } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
3057                 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
3058                         reply_len = -1;
3059         } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
3060                 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
3061                         reply_len = -1;
3062         } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
3063                 reply_len = wpa_supplicant_ctrl_iface_get_network(
3064                         wpa_s, buf + 12, reply, reply_size);
3065 #ifndef CONFIG_NO_CONFIG_WRITE
3066         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
3067                 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
3068                         reply_len = -1;
3069 #endif /* CONFIG_NO_CONFIG_WRITE */
3070         } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
3071                 reply_len = wpa_supplicant_ctrl_iface_get_capability(
3072                         wpa_s, buf + 15, reply, reply_size);
3073         } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
3074                 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
3075                         reply_len = -1;
3076         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
3077                 reply_len = wpa_supplicant_global_iface_list(
3078                         wpa_s->global, reply, reply_size);
3079         } else if (os_strcmp(buf, "INTERFACES") == 0) {
3080                 reply_len = wpa_supplicant_global_iface_interfaces(
3081                         wpa_s->global, reply, reply_size);
3082         } else if (os_strncmp(buf, "BSS ", 4) == 0) {
3083                 reply_len = wpa_supplicant_ctrl_iface_bss(
3084                         wpa_s, buf + 4, reply, reply_size);
3085 #ifdef CONFIG_AP
3086         } else if (os_strcmp(buf, "STA-FIRST") == 0) {
3087                 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
3088         } else if (os_strncmp(buf, "STA ", 4) == 0) {
3089                 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
3090                                               reply_size);
3091         } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3092                 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
3093                                                    reply_size);
3094 #endif /* CONFIG_AP */
3095         } else if (os_strcmp(buf, "SUSPEND") == 0) {
3096                 wpas_notify_suspend(wpa_s->global);
3097         } else if (os_strcmp(buf, "RESUME") == 0) {
3098                 wpas_notify_resume(wpa_s->global);
3099         } else if (os_strcmp(buf, "DROP_SA") == 0) {
3100                 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
3101         } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
3102                 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
3103                         reply_len = -1;
3104         } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
3105                 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
3106                         reply_len = -1;
3107         } else {
3108                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
3109                 reply_len = 16;
3110         }
3111
3112         if (reply_len < 0) {
3113                 os_memcpy(reply, "FAIL\n", 5);
3114                 reply_len = 5;
3115         }
3116
3117         if (ctrl_rsp)
3118                 eapol_sm_notify_ctrl_response(wpa_s->eapol);
3119
3120         *resp_len = reply_len;
3121         return reply;
3122 }
3123
3124
3125 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
3126                                            char *cmd)
3127 {
3128         struct wpa_interface iface;
3129         char *pos;
3130
3131         /*
3132          * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
3133          * TAB<bridge_ifname>
3134          */
3135         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
3136
3137         os_memset(&iface, 0, sizeof(iface));
3138
3139         do {
3140                 iface.ifname = pos = cmd;
3141                 pos = os_strchr(pos, '\t');
3142                 if (pos)
3143                         *pos++ = '\0';
3144                 if (iface.ifname[0] == '\0')
3145                         return -1;
3146                 if (pos == NULL)
3147                         break;
3148
3149                 iface.confname = pos;
3150                 pos = os_strchr(pos, '\t');
3151                 if (pos)
3152                         *pos++ = '\0';
3153                 if (iface.confname[0] == '\0')
3154                         iface.confname = NULL;
3155                 if (pos == NULL)
3156                         break;
3157
3158                 iface.driver = pos;
3159                 pos = os_strchr(pos, '\t');
3160                 if (pos)
3161                         *pos++ = '\0';
3162                 if (iface.driver[0] == '\0')
3163                         iface.driver = NULL;
3164                 if (pos == NULL)
3165                         break;
3166
3167                 iface.ctrl_interface = pos;
3168                 pos = os_strchr(pos, '\t');
3169                 if (pos)
3170                         *pos++ = '\0';
3171                 if (iface.ctrl_interface[0] == '\0')
3172                         iface.ctrl_interface = NULL;
3173                 if (pos == NULL)
3174                         break;
3175
3176                 iface.driver_param = pos;
3177                 pos = os_strchr(pos, '\t');
3178                 if (pos)
3179                         *pos++ = '\0';
3180                 if (iface.driver_param[0] == '\0')
3181                         iface.driver_param = NULL;
3182                 if (pos == NULL)
3183                         break;
3184
3185                 iface.bridge_ifname = pos;
3186                 pos = os_strchr(pos, '\t');
3187                 if (pos)
3188                         *pos++ = '\0';
3189                 if (iface.bridge_ifname[0] == '\0')
3190                         iface.bridge_ifname = NULL;
3191                 if (pos == NULL)
3192                         break;
3193         } while (0);
3194
3195         if (wpa_supplicant_get_iface(global, iface.ifname))
3196                 return -1;
3197
3198         return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
3199 }
3200
3201
3202 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
3203                                               char *cmd)
3204 {
3205         struct wpa_supplicant *wpa_s;
3206
3207         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
3208
3209         wpa_s = wpa_supplicant_get_iface(global, cmd);
3210         if (wpa_s == NULL)
3211                 return -1;
3212         return wpa_supplicant_remove_iface(global, wpa_s);
3213 }
3214
3215
3216 static void wpa_free_iface_info(struct wpa_interface_info *iface)
3217 {
3218         struct wpa_interface_info *prev;
3219
3220         while (iface) {
3221                 prev = iface;
3222                 iface = iface->next;
3223
3224                 os_free(prev->ifname);
3225                 os_free(prev->desc);
3226                 os_free(prev);
3227         }
3228 }
3229
3230
3231 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
3232                                             char *buf, int len)
3233 {
3234         int i, res;
3235         struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
3236         char *pos, *end;
3237
3238         for (i = 0; wpa_drivers[i]; i++) {
3239                 struct wpa_driver_ops *drv = wpa_drivers[i];
3240                 if (drv->get_interfaces == NULL)
3241                         continue;
3242                 tmp = drv->get_interfaces(global->drv_priv[i]);
3243                 if (tmp == NULL)
3244                         continue;
3245
3246                 if (last == NULL)
3247                         iface = last = tmp;
3248                 else
3249                         last->next = tmp;
3250                 while (last->next)
3251                         last = last->next;
3252         }
3253
3254         pos = buf;
3255         end = buf + len;
3256         for (tmp = iface; tmp; tmp = tmp->next) {
3257                 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
3258                                   tmp->drv_name, tmp->ifname,
3259                                   tmp->desc ? tmp->desc : "");
3260                 if (res < 0 || res >= end - pos) {
3261                         *pos = '\0';
3262                         break;
3263                 }
3264                 pos += res;
3265         }
3266
3267         wpa_free_iface_info(iface);
3268
3269         return pos - buf;
3270 }
3271
3272
3273 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
3274                                                   char *buf, int len)
3275 {
3276         int res;
3277         char *pos, *end;
3278         struct wpa_supplicant *wpa_s;
3279
3280         wpa_s = global->ifaces;
3281         pos = buf;
3282         end = buf + len;
3283
3284         while (wpa_s) {
3285                 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
3286                 if (res < 0 || res >= end - pos) {
3287                         *pos = '\0';
3288                         break;
3289                 }
3290                 pos += res;
3291                 wpa_s = wpa_s->next;
3292         }
3293         return pos - buf;
3294 }
3295
3296
3297 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
3298                                                 char *buf, size_t *resp_len)
3299 {
3300         char *reply;
3301         const int reply_size = 2048;
3302         int reply_len;
3303
3304         wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
3305                           (const u8 *) buf, os_strlen(buf));
3306
3307         reply = os_malloc(reply_size);
3308         if (reply == NULL) {
3309                 *resp_len = 1;
3310                 return NULL;
3311         }
3312
3313         os_memcpy(reply, "OK\n", 3);
3314         reply_len = 3;
3315
3316         if (os_strcmp(buf, "PING") == 0) {
3317                 os_memcpy(reply, "PONG\n", 5);
3318                 reply_len = 5;
3319         } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
3320                 if (wpa_supplicant_global_iface_add(global, buf + 14))
3321                         reply_len = -1;
3322         } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
3323                 if (wpa_supplicant_global_iface_remove(global, buf + 17))
3324                         reply_len = -1;
3325         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
3326                 reply_len = wpa_supplicant_global_iface_list(
3327                         global, reply, reply_size);
3328         } else if (os_strcmp(buf, "INTERFACES") == 0) {
3329                 reply_len = wpa_supplicant_global_iface_interfaces(
3330                         global, reply, reply_size);
3331         } else if (os_strcmp(buf, "TERMINATE") == 0) {
3332                 wpa_supplicant_terminate_proc(global);
3333         } else if (os_strcmp(buf, "SUSPEND") == 0) {
3334                 wpas_notify_suspend(global);
3335         } else if (os_strcmp(buf, "RESUME") == 0) {
3336                 wpas_notify_resume(global);
3337         } else {
3338                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
3339                 reply_len = 16;
3340         }
3341
3342         if (reply_len < 0) {
3343                 os_memcpy(reply, "FAIL\n", 5);
3344                 reply_len = 5;
3345         }
3346
3347         *resp_len = reply_len;
3348         return reply;
3349 }