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