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