Use shared functions for network operations and param changes
[libeap.git] / wpa_supplicant / ctrl_iface.c
1 /*
2  * WPA Supplicant / Control interface (shared code for all backends)
3  * Copyright (c) 2004-2009, 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 "includes.h"
16
17 #include "common.h"
18 #include "eloop.h"
19 #include "wpa.h"
20 #include "config.h"
21 #include "eapol_supp/eapol_supp_sm.h"
22 #include "wpa_supplicant_i.h"
23 #include "driver_i.h"
24 #include "ctrl_iface.h"
25 #include "l2_packet/l2_packet.h"
26 #include "preauth.h"
27 #include "pmksa_cache.h"
28 #include "wpa_ctrl.h"
29 #include "eap_peer/eap.h"
30 #include "ieee802_11_defs.h"
31 #include "wps_supplicant.h"
32 #include "wps/wps.h"
33 #include "ibss_rsn.h"
34 #include "ap.h"
35 #include "notify.h"
36
37 extern struct wpa_driver_ops *wpa_drivers[];
38
39 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
40                                             char *buf, int len);
41 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
42                                                   char *buf, int len);
43
44
45 static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
46                                          char *cmd)
47 {
48         char *value;
49         int ret = 0;
50
51         value = os_strchr(cmd, ' ');
52         if (value == NULL)
53                 return -1;
54         *value++ = '\0';
55
56         wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
57         if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
58                 eapol_sm_configure(wpa_s->eapol,
59                                    atoi(value), -1, -1, -1);
60         } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
61                 eapol_sm_configure(wpa_s->eapol,
62                                    -1, atoi(value), -1, -1);
63         } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
64                 eapol_sm_configure(wpa_s->eapol,
65                                    -1, -1, atoi(value), -1);
66         } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
67                 eapol_sm_configure(wpa_s->eapol,
68                                    -1, -1, -1, atoi(value));
69         } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
70                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
71                                      atoi(value)))
72                         ret = -1;
73         } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
74                    0) {
75                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
76                                      atoi(value)))
77                         ret = -1;
78         } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
79                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
80                         ret = -1;
81         } else
82                 ret = -1;
83
84         return ret;
85 }
86
87
88 #ifdef IEEE8021X_EAPOL
89 static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
90                                              char *addr)
91 {
92         u8 bssid[ETH_ALEN];
93         struct wpa_ssid *ssid = wpa_s->current_ssid;
94
95         if (hwaddr_aton(addr, bssid)) {
96                 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
97                            "'%s'", addr);
98                 return -1;
99         }
100
101         wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
102         rsn_preauth_deinit(wpa_s->wpa);
103         if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
104                 return -1;
105
106         return 0;
107 }
108 #endif /* IEEE8021X_EAPOL */
109
110
111 #ifdef CONFIG_PEERKEY
112 /* MLME-STKSTART.request(peer) */
113 static int wpa_supplicant_ctrl_iface_stkstart(
114         struct wpa_supplicant *wpa_s, char *addr)
115 {
116         u8 peer[ETH_ALEN];
117
118         if (hwaddr_aton(addr, peer)) {
119                 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
120                            "address '%s'", peer);
121                 return -1;
122         }
123
124         wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
125                    MAC2STR(peer));
126
127         return wpa_sm_stkstart(wpa_s->wpa, peer);
128 }
129 #endif /* CONFIG_PEERKEY */
130
131
132 #ifdef CONFIG_IEEE80211R
133 static int wpa_supplicant_ctrl_iface_ft_ds(
134         struct wpa_supplicant *wpa_s, char *addr)
135 {
136         u8 target_ap[ETH_ALEN];
137
138         if (hwaddr_aton(addr, target_ap)) {
139                 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
140                            "address '%s'", target_ap);
141                 return -1;
142         }
143
144         wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
145
146         return wpa_ft_start_over_ds(wpa_s->wpa, target_ap);
147 }
148 #endif /* CONFIG_IEEE80211R */
149
150
151 #ifdef CONFIG_WPS
152 static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
153                                              char *cmd)
154 {
155         u8 bssid[ETH_ALEN], *_bssid = bssid;
156
157         if (cmd == NULL || os_strcmp(cmd, "any") == 0)
158                 _bssid = NULL;
159         else if (hwaddr_aton(cmd, bssid)) {
160                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
161                            cmd);
162                 return -1;
163         }
164
165 #ifdef CONFIG_AP
166         if (wpa_s->ap_iface)
167                 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid);
168 #endif /* CONFIG_AP */
169
170         return wpas_wps_start_pbc(wpa_s, _bssid);
171 }
172
173
174 static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
175                                              char *cmd, char *buf,
176                                              size_t buflen)
177 {
178         u8 bssid[ETH_ALEN], *_bssid = bssid;
179         char *pin;
180         int ret;
181
182         pin = os_strchr(cmd, ' ');
183         if (pin)
184                 *pin++ = '\0';
185
186         if (os_strcmp(cmd, "any") == 0)
187                 _bssid = NULL;
188         else if (hwaddr_aton(cmd, bssid)) {
189                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
190                            cmd);
191                 return -1;
192         }
193
194 #ifdef CONFIG_AP
195         if (wpa_s->ap_iface)
196                 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
197                                                  buf, buflen);
198 #endif /* CONFIG_AP */
199
200         if (pin) {
201                 ret = wpas_wps_start_pin(wpa_s, _bssid, pin);
202                 if (ret < 0)
203                         return -1;
204                 ret = os_snprintf(buf, buflen, "%s", pin);
205                 if (ret < 0 || (size_t) ret >= buflen)
206                         return -1;
207                 return ret;
208         }
209
210         ret = wpas_wps_start_pin(wpa_s, _bssid, NULL);
211         if (ret < 0)
212                 return -1;
213
214         /* Return the generated PIN */
215         ret = os_snprintf(buf, buflen, "%08d", ret);
216         if (ret < 0 || (size_t) ret >= buflen)
217                 return -1;
218         return ret;
219 }
220
221
222 #ifdef CONFIG_WPS_OOB
223 static int wpa_supplicant_ctrl_iface_wps_oob(struct wpa_supplicant *wpa_s,
224                                              char *cmd)
225 {
226         char *path, *method, *name;
227
228         path = os_strchr(cmd, ' ');
229         if (path == NULL)
230                 return -1;
231         *path++ = '\0';
232
233         method = os_strchr(path, ' ');
234         if (method == NULL)
235                 return -1;
236         *method++ = '\0';
237
238         name = os_strchr(method, ' ');
239         if (name != NULL)
240                 *name++ = '\0';
241
242         return wpas_wps_start_oob(wpa_s, cmd, path, method, name);
243 }
244 #endif /* CONFIG_WPS_OOB */
245
246
247 static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
248                                              char *cmd)
249 {
250         u8 bssid[ETH_ALEN], *_bssid = bssid;
251         char *pin;
252         char *new_ssid;
253         char *new_auth;
254         char *new_encr;
255         char *new_key;
256         struct wps_new_ap_settings ap;
257
258         pin = os_strchr(cmd, ' ');
259         if (pin == NULL)
260                 return -1;
261         *pin++ = '\0';
262
263         if (os_strcmp(cmd, "any") == 0)
264                 _bssid = NULL;
265         else if (hwaddr_aton(cmd, bssid)) {
266                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
267                            cmd);
268                 return -1;
269         }
270
271         new_ssid = os_strchr(pin, ' ');
272         if (new_ssid == NULL)
273                 return wpas_wps_start_reg(wpa_s, _bssid, pin, NULL);
274         *new_ssid++ = '\0';
275
276         new_auth = os_strchr(new_ssid, ' ');
277         if (new_auth == NULL)
278                 return -1;
279         *new_auth++ = '\0';
280
281         new_encr = os_strchr(new_auth, ' ');
282         if (new_encr == NULL)
283                 return -1;
284         *new_encr++ = '\0';
285
286         new_key = os_strchr(new_encr, ' ');
287         if (new_key == NULL)
288                 return -1;
289         *new_key++ = '\0';
290
291         os_memset(&ap, 0, sizeof(ap));
292         ap.ssid_hex = new_ssid;
293         ap.auth = new_auth;
294         ap.encr = new_encr;
295         ap.key_hex = new_key;
296         return wpas_wps_start_reg(wpa_s, _bssid, pin, &ap);
297 }
298 #endif /* CONFIG_WPS */
299
300
301 #ifdef CONFIG_IBSS_RSN
302 static int wpa_supplicant_ctrl_iface_ibss_rsn(
303         struct wpa_supplicant *wpa_s, char *addr)
304 {
305         u8 peer[ETH_ALEN];
306
307         if (hwaddr_aton(addr, peer)) {
308                 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
309                            "address '%s'", peer);
310                 return -1;
311         }
312
313         wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
314                    MAC2STR(peer));
315
316         return ibss_rsn_start(wpa_s->ibss_rsn, peer);
317 }
318 #endif /* CONFIG_IBSS_RSN */
319
320
321 static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
322                                               char *rsp)
323 {
324 #ifdef IEEE8021X_EAPOL
325         char *pos, *id_pos;
326         int id;
327         struct wpa_ssid *ssid;
328         struct eap_peer_config *eap;
329
330         pos = os_strchr(rsp, '-');
331         if (pos == NULL)
332                 return -1;
333         *pos++ = '\0';
334         id_pos = pos;
335         pos = os_strchr(pos, ':');
336         if (pos == NULL)
337                 return -1;
338         *pos++ = '\0';
339         id = atoi(id_pos);
340         wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
341         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
342                               (u8 *) pos, os_strlen(pos));
343
344         ssid = wpa_config_get_network(wpa_s->conf, id);
345         if (ssid == NULL) {
346                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
347                            "to update", id);
348                 return -1;
349         }
350         eap = &ssid->eap;
351
352         if (os_strcmp(rsp, "IDENTITY") == 0) {
353                 os_free(eap->identity);
354                 eap->identity = (u8 *) os_strdup(pos);
355                 eap->identity_len = os_strlen(pos);
356                 eap->pending_req_identity = 0;
357                 if (ssid == wpa_s->current_ssid)
358                         wpa_s->reassociate = 1;
359         } else if (os_strcmp(rsp, "PASSWORD") == 0) {
360                 os_free(eap->password);
361                 eap->password = (u8 *) os_strdup(pos);
362                 eap->password_len = os_strlen(pos);
363                 eap->pending_req_password = 0;
364                 if (ssid == wpa_s->current_ssid)
365                         wpa_s->reassociate = 1;
366         } else if (os_strcmp(rsp, "NEW_PASSWORD") == 0) {
367                 os_free(eap->new_password);
368                 eap->new_password = (u8 *) os_strdup(pos);
369                 eap->new_password_len = os_strlen(pos);
370                 eap->pending_req_new_password = 0;
371                 if (ssid == wpa_s->current_ssid)
372                         wpa_s->reassociate = 1;
373         } else if (os_strcmp(rsp, "PIN") == 0) {
374                 os_free(eap->pin);
375                 eap->pin = os_strdup(pos);
376                 eap->pending_req_pin = 0;
377                 if (ssid == wpa_s->current_ssid)
378                         wpa_s->reassociate = 1;
379         } else if (os_strcmp(rsp, "OTP") == 0) {
380                 os_free(eap->otp);
381                 eap->otp = (u8 *) os_strdup(pos);
382                 eap->otp_len = os_strlen(pos);
383                 os_free(eap->pending_req_otp);
384                 eap->pending_req_otp = NULL;
385                 eap->pending_req_otp_len = 0;
386         } else if (os_strcmp(rsp, "PASSPHRASE") == 0) {
387                 os_free(eap->private_key_passwd);
388                 eap->private_key_passwd = (u8 *) os_strdup(pos);
389                 eap->pending_req_passphrase = 0;
390                 if (ssid == wpa_s->current_ssid)
391                         wpa_s->reassociate = 1;
392         } else {
393                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", rsp);
394                 return -1;
395         }
396
397         return 0;
398 #else /* IEEE8021X_EAPOL */
399         wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
400         return -1;
401 #endif /* IEEE8021X_EAPOL */
402 }
403
404
405 static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
406                                             const char *params,
407                                             char *buf, size_t buflen)
408 {
409         char *pos, *end, tmp[30];
410         int res, verbose, ret;
411
412         verbose = os_strcmp(params, "-VERBOSE") == 0;
413         pos = buf;
414         end = buf + buflen;
415         if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
416                 struct wpa_ssid *ssid = wpa_s->current_ssid;
417                 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
418                                   MAC2STR(wpa_s->bssid));
419                 if (ret < 0 || ret >= end - pos)
420                         return pos - buf;
421                 pos += ret;
422                 if (ssid) {
423                         u8 *_ssid = ssid->ssid;
424                         size_t ssid_len = ssid->ssid_len;
425                         u8 ssid_buf[MAX_SSID_LEN];
426                         if (ssid_len == 0) {
427                                 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
428                                 if (_res < 0)
429                                         ssid_len = 0;
430                                 else
431                                         ssid_len = _res;
432                                 _ssid = ssid_buf;
433                         }
434                         ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
435                                           wpa_ssid_txt(_ssid, ssid_len),
436                                           ssid->id);
437                         if (ret < 0 || ret >= end - pos)
438                                 return pos - buf;
439                         pos += ret;
440
441                         if (ssid->id_str) {
442                                 ret = os_snprintf(pos, end - pos,
443                                                   "id_str=%s\n",
444                                                   ssid->id_str);
445                                 if (ret < 0 || ret >= end - pos)
446                                         return pos - buf;
447                                 pos += ret;
448                         }
449                 }
450
451                 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
452         }
453         ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
454                           wpa_supplicant_state_txt(wpa_s->wpa_state));
455         if (ret < 0 || ret >= end - pos)
456                 return pos - buf;
457         pos += ret;
458
459         if (wpa_s->l2 &&
460             l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
461                 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
462                 if (ret < 0 || ret >= end - pos)
463                         return pos - buf;
464                 pos += ret;
465         }
466
467         if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
468             wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
469                 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
470                                           verbose);
471                 if (res >= 0)
472                         pos += res;
473         }
474
475         res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
476         if (res >= 0)
477                 pos += res;
478
479         return pos - buf;
480 }
481
482
483 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
484                                            char *cmd)
485 {
486         char *pos;
487         int id;
488         struct wpa_ssid *ssid;
489         u8 bssid[ETH_ALEN];
490
491         /* cmd: "<network id> <BSSID>" */
492         pos = os_strchr(cmd, ' ');
493         if (pos == NULL)
494                 return -1;
495         *pos++ = '\0';
496         id = atoi(cmd);
497         wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
498         if (hwaddr_aton(pos, bssid)) {
499                 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
500                 return -1;
501         }
502
503         ssid = wpa_config_get_network(wpa_s->conf, id);
504         if (ssid == NULL) {
505                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
506                            "to update", id);
507                 return -1;
508         }
509
510         os_memcpy(ssid->bssid, bssid, ETH_ALEN);
511         ssid->bssid_set = !is_zero_ether_addr(bssid);
512
513         return 0;
514 }
515
516
517 static int wpa_supplicant_ctrl_iface_list_networks(
518         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
519 {
520         char *pos, *end;
521         struct wpa_ssid *ssid;
522         int ret;
523
524         pos = buf;
525         end = buf + buflen;
526         ret = os_snprintf(pos, end - pos,
527                           "network id / ssid / bssid / flags\n");
528         if (ret < 0 || ret >= end - pos)
529                 return pos - buf;
530         pos += ret;
531
532         ssid = wpa_s->conf->ssid;
533         while (ssid) {
534                 ret = os_snprintf(pos, end - pos, "%d\t%s",
535                                   ssid->id,
536                                   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
537                 if (ret < 0 || ret >= end - pos)
538                         return pos - buf;
539                 pos += ret;
540                 if (ssid->bssid_set) {
541                         ret = os_snprintf(pos, end - pos, "\t" MACSTR,
542                                           MAC2STR(ssid->bssid));
543                 } else {
544                         ret = os_snprintf(pos, end - pos, "\tany");
545                 }
546                 if (ret < 0 || ret >= end - pos)
547                         return pos - buf;
548                 pos += ret;
549                 ret = os_snprintf(pos, end - pos, "\t%s%s",
550                                   ssid == wpa_s->current_ssid ?
551                                   "[CURRENT]" : "",
552                                   ssid->disabled ? "[DISABLED]" : "");
553                 if (ret < 0 || ret >= end - pos)
554                         return pos - buf;
555                 pos += ret;
556                 ret = os_snprintf(pos, end - pos, "\n");
557                 if (ret < 0 || ret >= end - pos)
558                         return pos - buf;
559                 pos += ret;
560
561                 ssid = ssid->next;
562         }
563
564         return pos - buf;
565 }
566
567
568 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
569 {
570         int first = 1, ret;
571         ret = os_snprintf(pos, end - pos, "-");
572         if (ret < 0 || ret >= end - pos)
573                 return pos;
574         pos += ret;
575         if (cipher & WPA_CIPHER_NONE) {
576                 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
577                 if (ret < 0 || ret >= end - pos)
578                         return pos;
579                 pos += ret;
580                 first = 0;
581         }
582         if (cipher & WPA_CIPHER_WEP40) {
583                 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
584                 if (ret < 0 || ret >= end - pos)
585                         return pos;
586                 pos += ret;
587                 first = 0;
588         }
589         if (cipher & WPA_CIPHER_WEP104) {
590                 ret = os_snprintf(pos, end - pos, "%sWEP104",
591                                   first ? "" : "+");
592                 if (ret < 0 || ret >= end - pos)
593                         return pos;
594                 pos += ret;
595                 first = 0;
596         }
597         if (cipher & WPA_CIPHER_TKIP) {
598                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
599                 if (ret < 0 || ret >= end - pos)
600                         return pos;
601                 pos += ret;
602                 first = 0;
603         }
604         if (cipher & WPA_CIPHER_CCMP) {
605                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
606                 if (ret < 0 || ret >= end - pos)
607                         return pos;
608                 pos += ret;
609                 first = 0;
610         }
611         return pos;
612 }
613
614
615 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
616                                     const u8 *ie, size_t ie_len)
617 {
618         struct wpa_ie_data data;
619         int first, ret;
620
621         ret = os_snprintf(pos, end - pos, "[%s-", proto);
622         if (ret < 0 || ret >= end - pos)
623                 return pos;
624         pos += ret;
625
626         if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
627                 ret = os_snprintf(pos, end - pos, "?]");
628                 if (ret < 0 || ret >= end - pos)
629                         return pos;
630                 pos += ret;
631                 return pos;
632         }
633
634         first = 1;
635         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
636                 ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
637                 if (ret < 0 || ret >= end - pos)
638                         return pos;
639                 pos += ret;
640                 first = 0;
641         }
642         if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
643                 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
644                 if (ret < 0 || ret >= end - pos)
645                         return pos;
646                 pos += ret;
647                 first = 0;
648         }
649         if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
650                 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
651                 if (ret < 0 || ret >= end - pos)
652                         return pos;
653                 pos += ret;
654                 first = 0;
655         }
656 #ifdef CONFIG_IEEE80211R
657         if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
658                 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
659                                   first ? "" : "+");
660                 if (ret < 0 || ret >= end - pos)
661                         return pos;
662                 pos += ret;
663                 first = 0;
664         }
665         if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
666                 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
667                                   first ? "" : "+");
668                 if (ret < 0 || ret >= end - pos)
669                         return pos;
670                 pos += ret;
671                 first = 0;
672         }
673 #endif /* CONFIG_IEEE80211R */
674 #ifdef CONFIG_IEEE80211W
675         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
676                 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
677                                   first ? "" : "+");
678                 if (ret < 0 || ret >= end - pos)
679                         return pos;
680                 pos += ret;
681                 first = 0;
682         }
683         if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
684                 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
685                                   first ? "" : "+");
686                 if (ret < 0 || ret >= end - pos)
687                         return pos;
688                 pos += ret;
689                 first = 0;
690         }
691 #endif /* CONFIG_IEEE80211W */
692
693         pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
694
695         if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
696                 ret = os_snprintf(pos, end - pos, "-preauth");
697                 if (ret < 0 || ret >= end - pos)
698                         return pos;
699                 pos += ret;
700         }
701
702         ret = os_snprintf(pos, end - pos, "]");
703         if (ret < 0 || ret >= end - pos)
704                 return pos;
705         pos += ret;
706
707         return pos;
708 }
709
710 static char * wpa_supplicant_wps_ie_txt(char *pos, char *end,
711                                         const struct wpa_scan_res *res)
712 {
713 #ifdef CONFIG_WPS
714         struct wpabuf *wps_ie;
715         int ret;
716         const char *txt;
717
718         wps_ie = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
719         if (wps_ie == NULL)
720                 return pos;
721
722         if (wps_is_selected_pbc_registrar(wps_ie))
723                 txt = "[WPS-PBC]";
724         else if (wps_is_selected_pin_registrar(wps_ie))
725                 txt = "[WPS-PIN]";
726         else
727                 txt = "[WPS]";
728
729         ret = os_snprintf(pos, end - pos, "%s", txt);
730         if (ret >= 0 && ret < end - pos)
731                 pos += ret;
732         wpabuf_free(wps_ie);
733 #endif /* CONFIG_WPS */
734
735         return pos;
736 }
737
738
739 /* Format one result on one text line into a buffer. */
740 static int wpa_supplicant_ctrl_iface_scan_result(
741         const struct wpa_scan_res *res, char *buf, size_t buflen)
742 {
743         char *pos, *end;
744         int ret;
745         const u8 *ie, *ie2;
746
747         pos = buf;
748         end = buf + buflen;
749
750         ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
751                           MAC2STR(res->bssid), res->freq, res->level);
752         if (ret < 0 || ret >= end - pos)
753                 return pos - buf;
754         pos += ret;
755         ie = wpa_scan_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
756         if (ie)
757                 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
758         ie2 = wpa_scan_get_ie(res, WLAN_EID_RSN);
759         if (ie2)
760                 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
761         pos = wpa_supplicant_wps_ie_txt(pos, end, res);
762         if (!ie && !ie2 && res->caps & IEEE80211_CAP_PRIVACY) {
763                 ret = os_snprintf(pos, end - pos, "[WEP]");
764                 if (ret < 0 || ret >= end - pos)
765                         return pos - buf;
766                 pos += ret;
767         }
768         if (res->caps & IEEE80211_CAP_IBSS) {
769                 ret = os_snprintf(pos, end - pos, "[IBSS]");
770                 if (ret < 0 || ret >= end - pos)
771                         return pos - buf;
772                 pos += ret;
773         }
774
775         ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
776         ret = os_snprintf(pos, end - pos, "\t%s",
777                           ie ? wpa_ssid_txt(ie + 2, ie[1]) : "");
778         if (ret < 0 || ret >= end - pos)
779                 return pos - buf;
780         pos += ret;
781
782         ret = os_snprintf(pos, end - pos, "\n");
783         if (ret < 0 || ret >= end - pos)
784                 return pos - buf;
785         pos += ret;
786
787         return pos - buf;
788 }
789
790
791 static int wpa_supplicant_ctrl_iface_scan_results(
792         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
793 {
794         char *pos, *end;
795         struct wpa_scan_res *res;
796         int ret;
797         size_t i;
798
799         if (wpa_s->scan_res == NULL &&
800             wpa_supplicant_get_scan_results(wpa_s) < 0)
801                 return 0;
802
803         pos = buf;
804         end = buf + buflen;
805         ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
806                           "flags / ssid\n");
807         if (ret < 0 || ret >= end - pos)
808                 return pos - buf;
809         pos += ret;
810
811         for (i = 0; i < wpa_s->scan_res->num; i++) {
812                 res = wpa_s->scan_res->res[i];
813                 ret = wpa_supplicant_ctrl_iface_scan_result(res, pos,
814                                                             end - pos);
815                 if (ret < 0 || ret >= end - pos)
816                         return pos - buf;
817                 pos += ret;
818         }
819
820         return pos - buf;
821 }
822
823
824 static int wpa_supplicant_ctrl_iface_select_network(
825         struct wpa_supplicant *wpa_s, char *cmd)
826 {
827         int id;
828         struct wpa_ssid *ssid;
829
830         /* cmd: "<network id>" or "any" */
831         if (os_strcmp(cmd, "any") == 0) {
832                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
833                 ssid = NULL;
834         } else {
835                 id = atoi(cmd);
836                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
837
838                 ssid = wpa_config_get_network(wpa_s->conf, id);
839                 if (ssid == NULL) {
840                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
841                                    "network id=%d", id);
842                         return -1;
843                 }
844         }
845
846         wpa_supplicant_select_network(wpa_s, ssid);
847
848         return 0;
849 }
850
851
852 static int wpa_supplicant_ctrl_iface_enable_network(
853         struct wpa_supplicant *wpa_s, char *cmd)
854 {
855         int id;
856         struct wpa_ssid *ssid;
857
858         /* cmd: "<network id>" or "all" */
859         if (os_strcmp(cmd, "all") == 0) {
860                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
861                 ssid = NULL;
862         } else {
863                 id = atoi(cmd);
864                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
865
866                 ssid = wpa_config_get_network(wpa_s->conf, id);
867                 if (ssid == NULL) {
868                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
869                                    "network id=%d", id);
870                         return -1;
871                 }
872         }
873         wpa_supplicant_enable_network(wpa_s, ssid);
874
875         return 0;
876 }
877
878
879 static int wpa_supplicant_ctrl_iface_disable_network(
880         struct wpa_supplicant *wpa_s, char *cmd)
881 {
882         int id;
883         struct wpa_ssid *ssid;
884
885         /* cmd: "<network id>" or "all" */
886         if (os_strcmp(cmd, "all") == 0) {
887                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
888                 ssid = NULL;
889         } else {
890                 id = atoi(cmd);
891                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
892
893                 ssid = wpa_config_get_network(wpa_s->conf, id);
894                 if (ssid == NULL) {
895                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
896                                    "network id=%d", id);
897                         return -1;
898                 }
899         }
900         wpa_supplicant_disable_network(wpa_s, ssid);
901
902         return 0;
903 }
904
905
906 static int wpa_supplicant_ctrl_iface_add_network(
907         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
908 {
909         struct wpa_ssid *ssid;
910         int ret;
911
912         wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
913
914         ssid = wpa_config_add_network(wpa_s->conf);
915         if (ssid == NULL)
916                 return -1;
917
918         wpas_notify_network_added(wpa_s, ssid);
919
920         ssid->disabled = 1;
921         wpa_config_set_network_defaults(ssid);
922
923         ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
924         if (ret < 0 || (size_t) ret >= buflen)
925                 return -1;
926         return ret;
927 }
928
929
930 static int wpa_supplicant_ctrl_iface_remove_network(
931         struct wpa_supplicant *wpa_s, char *cmd)
932 {
933         int id;
934         struct wpa_ssid *ssid;
935
936         /* cmd: "<network id>" or "all" */
937         if (os_strcmp(cmd, "all") == 0) {
938                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
939                 ssid = wpa_s->conf->ssid;
940                 while (ssid) {
941                         struct wpa_ssid *remove_ssid = ssid;
942                         id = ssid->id;
943                         ssid = ssid->next;
944                         wpas_notify_network_removed(wpa_s, remove_ssid);
945                         wpa_config_remove_network(wpa_s->conf, id);
946                 }
947                 if (wpa_s->current_ssid) {
948                         eapol_sm_invalidate_cached_session(wpa_s->eapol);
949                         wpa_supplicant_disassociate(wpa_s,
950                                                     WLAN_REASON_DEAUTH_LEAVING);
951                 }
952                 return 0;
953         }
954
955         id = atoi(cmd);
956         wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
957
958         ssid = wpa_config_get_network(wpa_s->conf, id);
959         if (ssid == NULL ||
960             wpa_config_remove_network(wpa_s->conf, id) < 0) {
961                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
962                            "id=%d", id);
963                 return -1;
964         }
965
966         if (ssid == wpa_s->current_ssid) {
967                 /*
968                  * Invalidate the EAP session cache if the current network is
969                  * removed.
970                  */
971                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
972
973                 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
974         }
975
976         return 0;
977 }
978
979
980 static int wpa_supplicant_ctrl_iface_set_network(
981         struct wpa_supplicant *wpa_s, char *cmd)
982 {
983         int id;
984         struct wpa_ssid *ssid;
985         char *name, *value;
986
987         /* cmd: "<network id> <variable name> <value>" */
988         name = os_strchr(cmd, ' ');
989         if (name == NULL)
990                 return -1;
991         *name++ = '\0';
992
993         value = os_strchr(name, ' ');
994         if (value == NULL)
995                 return -1;
996         *value++ = '\0';
997
998         id = atoi(cmd);
999         wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
1000                    id, name);
1001         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1002                               (u8 *) value, os_strlen(value));
1003
1004         ssid = wpa_config_get_network(wpa_s->conf, id);
1005         if (ssid == NULL) {
1006                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1007                            "id=%d", id);
1008                 return -1;
1009         }
1010
1011         if (wpa_config_set(ssid, name, value, 0) < 0) {
1012                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
1013                            "variable '%s'", name);
1014                 return -1;
1015         }
1016
1017         if (wpa_s->current_ssid == ssid) {
1018                 /*
1019                  * Invalidate the EAP session cache if anything in the current
1020                  * configuration changes.
1021                  */
1022                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1023         }
1024
1025         if ((os_strcmp(name, "psk") == 0 &&
1026              value[0] == '"' && ssid->ssid_len) ||
1027             (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
1028                 wpa_config_update_psk(ssid);
1029
1030         return 0;
1031 }
1032
1033
1034 static int wpa_supplicant_ctrl_iface_get_network(
1035         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1036 {
1037         int id;
1038         size_t res;
1039         struct wpa_ssid *ssid;
1040         char *name, *value;
1041
1042         /* cmd: "<network id> <variable name>" */
1043         name = os_strchr(cmd, ' ');
1044         if (name == NULL || buflen == 0)
1045                 return -1;
1046         *name++ = '\0';
1047
1048         id = atoi(cmd);
1049         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
1050                    id, name);
1051
1052         ssid = wpa_config_get_network(wpa_s->conf, id);
1053         if (ssid == NULL) {
1054                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1055                            "id=%d", id);
1056                 return -1;
1057         }
1058
1059         value = wpa_config_get_no_key(ssid, name);
1060         if (value == NULL) {
1061                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
1062                            "variable '%s'", name);
1063                 return -1;
1064         }
1065
1066         res = os_strlcpy(buf, value, buflen);
1067         if (res >= buflen) {
1068                 os_free(value);
1069                 return -1;
1070         }
1071
1072         os_free(value);
1073
1074         return res;
1075 }
1076
1077
1078 #ifndef CONFIG_NO_CONFIG_WRITE
1079 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
1080 {
1081         int ret;
1082
1083         if (!wpa_s->conf->update_config) {
1084                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
1085                            "to update configuration (update_config=0)");
1086                 return -1;
1087         }
1088
1089         ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
1090         if (ret) {
1091                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
1092                            "update configuration");
1093         } else {
1094                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
1095                            " updated");
1096         }
1097
1098         return ret;
1099 }
1100 #endif /* CONFIG_NO_CONFIG_WRITE */
1101
1102
1103 static int ctrl_iface_get_capability_pairwise(int res, char *strict,
1104                                               struct wpa_driver_capa *capa,
1105                                               char *buf, size_t buflen)
1106 {
1107         int ret, first = 1;
1108         char *pos, *end;
1109         size_t len;
1110
1111         pos = buf;
1112         end = pos + buflen;
1113
1114         if (res < 0) {
1115                 if (strict)
1116                         return 0;
1117                 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
1118                 if (len >= buflen)
1119                         return -1;
1120                 return len;
1121         }
1122
1123         if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
1124                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
1125                 if (ret < 0 || ret >= end - pos)
1126                         return pos - buf;
1127                 pos += ret;
1128                 first = 0;
1129         }
1130
1131         if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
1132                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
1133                 if (ret < 0 || ret >= end - pos)
1134                         return pos - buf;
1135                 pos += ret;
1136                 first = 0;
1137         }
1138
1139         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
1140                 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
1141                 if (ret < 0 || ret >= end - pos)
1142                         return pos - buf;
1143                 pos += ret;
1144                 first = 0;
1145         }
1146
1147         return pos - buf;
1148 }
1149
1150
1151 static int ctrl_iface_get_capability_group(int res, char *strict,
1152                                            struct wpa_driver_capa *capa,
1153                                            char *buf, size_t buflen)
1154 {
1155         int ret, first = 1;
1156         char *pos, *end;
1157         size_t len;
1158
1159         pos = buf;
1160         end = pos + buflen;
1161
1162         if (res < 0) {
1163                 if (strict)
1164                         return 0;
1165                 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
1166                 if (len >= buflen)
1167                         return -1;
1168                 return len;
1169         }
1170
1171         if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
1172                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
1173                 if (ret < 0 || ret >= end - pos)
1174                         return pos - buf;
1175                 pos += ret;
1176                 first = 0;
1177         }
1178
1179         if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
1180                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
1181                 if (ret < 0 || ret >= end - pos)
1182                         return pos - buf;
1183                 pos += ret;
1184                 first = 0;
1185         }
1186
1187         if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
1188                 ret = os_snprintf(pos, end - pos, "%sWEP104",
1189                                   first ? "" : " ");
1190                 if (ret < 0 || ret >= end - pos)
1191                         return pos - buf;
1192                 pos += ret;
1193                 first = 0;
1194         }
1195
1196         if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
1197                 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
1198                 if (ret < 0 || ret >= end - pos)
1199                         return pos - buf;
1200                 pos += ret;
1201                 first = 0;
1202         }
1203
1204         return pos - buf;
1205 }
1206
1207
1208 static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
1209                                               struct wpa_driver_capa *capa,
1210                                               char *buf, size_t buflen)
1211 {
1212         int ret;
1213         char *pos, *end;
1214         size_t len;
1215
1216         pos = buf;
1217         end = pos + buflen;
1218
1219         if (res < 0) {
1220                 if (strict)
1221                         return 0;
1222                 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
1223                                  "NONE", buflen);
1224                 if (len >= buflen)
1225                         return -1;
1226                 return len;
1227         }
1228
1229         ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
1230         if (ret < 0 || ret >= end - pos)
1231                 return pos - buf;
1232         pos += ret;
1233
1234         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1235                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
1236                 ret = os_snprintf(pos, end - pos, " WPA-EAP");
1237                 if (ret < 0 || ret >= end - pos)
1238                         return pos - buf;
1239                 pos += ret;
1240         }
1241
1242         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
1243                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
1244                 ret = os_snprintf(pos, end - pos, " WPA-PSK");
1245                 if (ret < 0 || ret >= end - pos)
1246                         return pos - buf;
1247                 pos += ret;
1248         }
1249
1250         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
1251                 ret = os_snprintf(pos, end - pos, " WPA-NONE");
1252                 if (ret < 0 || ret >= end - pos)
1253                         return pos - buf;
1254                 pos += ret;
1255         }
1256
1257         return pos - buf;
1258 }
1259
1260
1261 static int ctrl_iface_get_capability_proto(int res, char *strict,
1262                                            struct wpa_driver_capa *capa,
1263                                            char *buf, size_t buflen)
1264 {
1265         int ret, first = 1;
1266         char *pos, *end;
1267         size_t len;
1268
1269         pos = buf;
1270         end = pos + buflen;
1271
1272         if (res < 0) {
1273                 if (strict)
1274                         return 0;
1275                 len = os_strlcpy(buf, "RSN WPA", buflen);
1276                 if (len >= buflen)
1277                         return -1;
1278                 return len;
1279         }
1280
1281         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1282                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
1283                 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
1284                 if (ret < 0 || ret >= end - pos)
1285                         return pos - buf;
1286                 pos += ret;
1287                 first = 0;
1288         }
1289
1290         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1291                               WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
1292                 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
1293                 if (ret < 0 || ret >= end - pos)
1294                         return pos - buf;
1295                 pos += ret;
1296                 first = 0;
1297         }
1298
1299         return pos - buf;
1300 }
1301
1302
1303 static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
1304                                               struct wpa_driver_capa *capa,
1305                                               char *buf, size_t buflen)
1306 {
1307         int ret, first = 1;
1308         char *pos, *end;
1309         size_t len;
1310
1311         pos = buf;
1312         end = pos + buflen;
1313
1314         if (res < 0) {
1315                 if (strict)
1316                         return 0;
1317                 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
1318                 if (len >= buflen)
1319                         return -1;
1320                 return len;
1321         }
1322
1323         if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
1324                 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
1325                 if (ret < 0 || ret >= end - pos)
1326                         return pos - buf;
1327                 pos += ret;
1328                 first = 0;
1329         }
1330
1331         if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
1332                 ret = os_snprintf(pos, end - pos, "%sSHARED",
1333                                   first ? "" : " ");
1334                 if (ret < 0 || ret >= end - pos)
1335                         return pos - buf;
1336                 pos += ret;
1337                 first = 0;
1338         }
1339
1340         if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
1341                 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
1342                 if (ret < 0 || ret >= end - pos)
1343                         return pos - buf;
1344                 pos += ret;
1345                 first = 0;
1346         }
1347
1348         return pos - buf;
1349 }
1350
1351
1352 static int wpa_supplicant_ctrl_iface_get_capability(
1353         struct wpa_supplicant *wpa_s, const char *_field, char *buf,
1354         size_t buflen)
1355 {
1356         struct wpa_driver_capa capa;
1357         int res;
1358         char *strict;
1359         char field[30];
1360         size_t len;
1361
1362         /* Determine whether or not strict checking was requested */
1363         len = os_strlcpy(field, _field, sizeof(field));
1364         if (len >= sizeof(field))
1365                 return -1;
1366         strict = os_strchr(field, ' ');
1367         if (strict != NULL) {
1368                 *strict++ = '\0';
1369                 if (os_strcmp(strict, "strict") != 0)
1370                         return -1;
1371         }
1372
1373         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
1374                 field, strict ? strict : "");
1375
1376         if (os_strcmp(field, "eap") == 0) {
1377                 return eap_get_names(buf, buflen);
1378         }
1379
1380         res = wpa_drv_get_capa(wpa_s, &capa);
1381
1382         if (os_strcmp(field, "pairwise") == 0)
1383                 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
1384                                                           buf, buflen);
1385
1386         if (os_strcmp(field, "group") == 0)
1387                 return ctrl_iface_get_capability_group(res, strict, &capa,
1388                                                        buf, buflen);
1389
1390         if (os_strcmp(field, "key_mgmt") == 0)
1391                 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
1392                                                           buf, buflen);
1393
1394         if (os_strcmp(field, "proto") == 0)
1395                 return ctrl_iface_get_capability_proto(res, strict, &capa,
1396                                                        buf, buflen);
1397
1398         if (os_strcmp(field, "auth_alg") == 0)
1399                 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
1400                                                           buf, buflen);
1401
1402         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
1403                    field);
1404
1405         return -1;
1406 }
1407
1408
1409 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
1410                                          const char *cmd, char *buf,
1411                                          size_t buflen)
1412 {
1413         u8 bssid[ETH_ALEN];
1414         size_t i;
1415         struct wpa_scan_results *results;
1416         struct wpa_scan_res *bss;
1417         int ret;
1418         char *pos, *end;
1419         const u8 *ie, *ie2;
1420
1421         if (wpa_s->scan_res == NULL &&
1422             wpa_supplicant_get_scan_results(wpa_s) < 0)
1423                 return 0;
1424
1425         results = wpa_s->scan_res;
1426         if (results == NULL)
1427                 return 0;
1428
1429         if (hwaddr_aton(cmd, bssid) == 0) {
1430                 for (i = 0; i < results->num; i++) {
1431                         if (os_memcmp(bssid, results->res[i]->bssid, ETH_ALEN)
1432                             == 0)
1433                                 break;
1434                 }
1435         } else
1436                 i = atoi(cmd);
1437
1438         if (i >= results->num || results->res[i] == NULL)
1439                 return 0; /* no match found */
1440
1441         bss = results->res[i];
1442         pos = buf;
1443         end = buf + buflen;
1444         ret = os_snprintf(pos, end - pos,
1445                           "bssid=" MACSTR "\n"
1446                           "freq=%d\n"
1447                           "beacon_int=%d\n"
1448                           "capabilities=0x%04x\n"
1449                           "qual=%d\n"
1450                           "noise=%d\n"
1451                           "level=%d\n"
1452                           "tsf=%016llu\n"
1453                           "ie=",
1454                           MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
1455                           bss->caps, bss->qual, bss->noise, bss->level,
1456                           (unsigned long long) bss->tsf);
1457         if (ret < 0 || ret >= end - pos)
1458                 return pos - buf;
1459         pos += ret;
1460
1461         ie = (const u8 *) (bss + 1);
1462         for (i = 0; i < bss->ie_len; i++) {
1463                 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
1464                 if (ret < 0 || ret >= end - pos)
1465                         return pos - buf;
1466                 pos += ret;
1467         }
1468
1469         ret = os_snprintf(pos, end - pos, "\n");
1470         if (ret < 0 || ret >= end - pos)
1471                 return pos - buf;
1472         pos += ret;
1473
1474         ret = os_snprintf(pos, end - pos, "flags=");
1475         if (ret < 0 || ret >= end - pos)
1476                 return pos - buf;
1477         pos += ret;
1478
1479         ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1480         if (ie)
1481                 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
1482         ie2 = wpa_scan_get_ie(bss, WLAN_EID_RSN);
1483         if (ie2)
1484                 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
1485         pos = wpa_supplicant_wps_ie_txt(pos, end, bss);
1486         if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
1487                 ret = os_snprintf(pos, end - pos, "[WEP]");
1488                 if (ret < 0 || ret >= end - pos)
1489                         return pos - buf;
1490                 pos += ret;
1491         }
1492         if (bss->caps & IEEE80211_CAP_IBSS) {
1493                 ret = os_snprintf(pos, end - pos, "[IBSS]");
1494                 if (ret < 0 || ret >= end - pos)
1495                         return pos - buf;
1496                 pos += ret;
1497         }
1498
1499         ret = os_snprintf(pos, end - pos, "\n");
1500         if (ret < 0 || ret >= end - pos)
1501                 return pos - buf;
1502         pos += ret;
1503
1504         ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1505         ret = os_snprintf(pos, end - pos, "ssid=%s\n",
1506                           ie ? wpa_ssid_txt(ie + 2, ie[1]) : "");
1507         if (ret < 0 || ret >= end - pos)
1508                 return pos - buf;
1509         pos += ret;
1510
1511 #ifdef CONFIG_WPS
1512         ie = (const u8 *) (bss + 1);
1513         ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
1514         if (ret < 0 || ret >= end - pos)
1515                 return pos - buf;
1516         pos += ret;
1517 #endif /* CONFIG_WPS */
1518
1519         return pos - buf;
1520 }
1521
1522
1523 static int wpa_supplicant_ctrl_iface_ap_scan(
1524         struct wpa_supplicant *wpa_s, char *cmd)
1525 {
1526         int ap_scan = atoi(cmd);
1527         return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
1528 }
1529
1530
1531 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
1532                                          char *buf, size_t *resp_len)
1533 {
1534         char *reply;
1535         const int reply_size = 2048;
1536         int ctrl_rsp = 0;
1537         int reply_len;
1538
1539         if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
1540             os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
1541                 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
1542                                       (const u8 *) buf, os_strlen(buf));
1543         } else {
1544                 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface",
1545                                   (const u8 *) buf, os_strlen(buf));
1546         }
1547
1548         reply = os_malloc(reply_size);
1549         if (reply == NULL) {
1550                 *resp_len = 1;
1551                 return NULL;
1552         }
1553
1554         os_memcpy(reply, "OK\n", 3);
1555         reply_len = 3;
1556
1557         if (os_strcmp(buf, "PING") == 0) {
1558                 os_memcpy(reply, "PONG\n", 5);
1559                 reply_len = 5;
1560         } else if (os_strcmp(buf, "MIB") == 0) {
1561                 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
1562                 if (reply_len >= 0) {
1563                         int res;
1564                         res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
1565                                                reply_size - reply_len);
1566                         if (res < 0)
1567                                 reply_len = -1;
1568                         else
1569                                 reply_len += res;
1570                 }
1571         } else if (os_strncmp(buf, "STATUS", 6) == 0) {
1572                 reply_len = wpa_supplicant_ctrl_iface_status(
1573                         wpa_s, buf + 6, reply, reply_size);
1574         } else if (os_strcmp(buf, "PMKSA") == 0) {
1575                 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
1576                                                     reply_size);
1577         } else if (os_strncmp(buf, "SET ", 4) == 0) {
1578                 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
1579                         reply_len = -1;
1580         } else if (os_strcmp(buf, "LOGON") == 0) {
1581                 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
1582         } else if (os_strcmp(buf, "LOGOFF") == 0) {
1583                 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
1584         } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
1585                 wpa_s->disconnected = 0;
1586                 wpa_s->reassociate = 1;
1587                 wpa_supplicant_req_scan(wpa_s, 0, 0);
1588         } else if (os_strcmp(buf, "RECONNECT") == 0) {
1589                 if (wpa_s->disconnected) {
1590                         wpa_s->disconnected = 0;
1591                         wpa_s->reassociate = 1;
1592                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1593                 }
1594 #ifdef IEEE8021X_EAPOL
1595         } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
1596                 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
1597                         reply_len = -1;
1598 #endif /* IEEE8021X_EAPOL */
1599 #ifdef CONFIG_PEERKEY
1600         } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
1601                 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
1602                         reply_len = -1;
1603 #endif /* CONFIG_PEERKEY */
1604 #ifdef CONFIG_IEEE80211R
1605         } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
1606                 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
1607                         reply_len = -1;
1608 #endif /* CONFIG_IEEE80211R */
1609 #ifdef CONFIG_WPS
1610         } else if (os_strcmp(buf, "WPS_PBC") == 0) {
1611                 if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL))
1612                         reply_len = -1;
1613         } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
1614                 if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8))
1615                         reply_len = -1;
1616         } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
1617                 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
1618                                                               reply,
1619                                                               reply_size);
1620 #ifdef CONFIG_WPS_OOB
1621         } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
1622                 if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
1623                         reply_len = -1;
1624 #endif /* CONFIG_WPS_OOB */
1625         } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
1626                 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
1627                         reply_len = -1;
1628 #endif /* CONFIG_WPS */
1629 #ifdef CONFIG_IBSS_RSN
1630         } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
1631                 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
1632                         reply_len = -1;
1633 #endif /* CONFIG_IBSS_RSN */
1634         } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
1635         {
1636                 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
1637                             wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
1638                         reply_len = -1;
1639                 else
1640                         ctrl_rsp = 1;
1641         } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
1642                 if (wpa_supplicant_reload_configuration(wpa_s))
1643                         reply_len = -1;
1644         } else if (os_strcmp(buf, "TERMINATE") == 0) {
1645                 eloop_terminate();
1646         } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
1647                 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
1648                         reply_len = -1;
1649         } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
1650                 reply_len = wpa_supplicant_ctrl_iface_list_networks(
1651                         wpa_s, reply, reply_size);
1652         } else if (os_strcmp(buf, "DISCONNECT") == 0) {
1653                 wpa_s->reassociate = 0;
1654                 wpa_s->disconnected = 1;
1655                 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1656         } else if (os_strcmp(buf, "SCAN") == 0) {
1657                 wpa_s->scan_req = 2;
1658                 wpa_supplicant_req_scan(wpa_s, 0, 0);
1659         } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
1660                 reply_len = wpa_supplicant_ctrl_iface_scan_results(
1661                         wpa_s, reply, reply_size);
1662         } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
1663                 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
1664                         reply_len = -1;
1665         } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
1666                 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
1667                         reply_len = -1;
1668         } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
1669                 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
1670                         reply_len = -1;
1671         } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
1672                 reply_len = wpa_supplicant_ctrl_iface_add_network(
1673                         wpa_s, reply, reply_size);
1674         } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
1675                 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
1676                         reply_len = -1;
1677         } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
1678                 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
1679                         reply_len = -1;
1680         } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
1681                 reply_len = wpa_supplicant_ctrl_iface_get_network(
1682                         wpa_s, buf + 12, reply, reply_size);
1683 #ifndef CONFIG_NO_CONFIG_WRITE
1684         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
1685                 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
1686                         reply_len = -1;
1687 #endif /* CONFIG_NO_CONFIG_WRITE */
1688         } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
1689                 reply_len = wpa_supplicant_ctrl_iface_get_capability(
1690                         wpa_s, buf + 15, reply, reply_size);
1691         } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
1692                 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
1693                         reply_len = -1;
1694         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
1695                 reply_len = wpa_supplicant_global_iface_list(
1696                         wpa_s->global, reply, reply_size);
1697         } else if (os_strcmp(buf, "INTERFACES") == 0) {
1698                 reply_len = wpa_supplicant_global_iface_interfaces(
1699                         wpa_s->global, reply, reply_size);
1700         } else if (os_strncmp(buf, "BSS ", 4) == 0) {
1701                 reply_len = wpa_supplicant_ctrl_iface_bss(
1702                         wpa_s, buf + 4, reply, reply_size);
1703 #ifdef CONFIG_AP
1704         } else if (os_strcmp(buf, "STA-FIRST") == 0) {
1705                 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
1706         } else if (os_strncmp(buf, "STA ", 4) == 0) {
1707                 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
1708                                               reply_size);
1709         } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
1710                 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
1711                                                    reply_size);
1712 #endif /* CONFIG_AP */
1713         } else {
1714                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
1715                 reply_len = 16;
1716         }
1717
1718         if (reply_len < 0) {
1719                 os_memcpy(reply, "FAIL\n", 5);
1720                 reply_len = 5;
1721         }
1722
1723         if (ctrl_rsp)
1724                 eapol_sm_notify_ctrl_response(wpa_s->eapol);
1725
1726         *resp_len = reply_len;
1727         return reply;
1728 }
1729
1730
1731 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
1732                                            char *cmd)
1733 {
1734         struct wpa_interface iface;
1735         char *pos;
1736
1737         /*
1738          * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
1739          * TAB<bridge_ifname>
1740          */
1741         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
1742
1743         os_memset(&iface, 0, sizeof(iface));
1744
1745         do {
1746                 iface.ifname = pos = cmd;
1747                 pos = os_strchr(pos, '\t');
1748                 if (pos)
1749                         *pos++ = '\0';
1750                 if (iface.ifname[0] == '\0')
1751                         return -1;
1752                 if (pos == NULL)
1753                         break;
1754
1755                 iface.confname = pos;
1756                 pos = os_strchr(pos, '\t');
1757                 if (pos)
1758                         *pos++ = '\0';
1759                 if (iface.confname[0] == '\0')
1760                         iface.confname = NULL;
1761                 if (pos == NULL)
1762                         break;
1763
1764                 iface.driver = pos;
1765                 pos = os_strchr(pos, '\t');
1766                 if (pos)
1767                         *pos++ = '\0';
1768                 if (iface.driver[0] == '\0')
1769                         iface.driver = NULL;
1770                 if (pos == NULL)
1771                         break;
1772
1773                 iface.ctrl_interface = pos;
1774                 pos = os_strchr(pos, '\t');
1775                 if (pos)
1776                         *pos++ = '\0';
1777                 if (iface.ctrl_interface[0] == '\0')
1778                         iface.ctrl_interface = NULL;
1779                 if (pos == NULL)
1780                         break;
1781
1782                 iface.driver_param = pos;
1783                 pos = os_strchr(pos, '\t');
1784                 if (pos)
1785                         *pos++ = '\0';
1786                 if (iface.driver_param[0] == '\0')
1787                         iface.driver_param = NULL;
1788                 if (pos == NULL)
1789                         break;
1790
1791                 iface.bridge_ifname = pos;
1792                 pos = os_strchr(pos, '\t');
1793                 if (pos)
1794                         *pos++ = '\0';
1795                 if (iface.bridge_ifname[0] == '\0')
1796                         iface.bridge_ifname = NULL;
1797                 if (pos == NULL)
1798                         break;
1799         } while (0);
1800
1801         if (wpa_supplicant_get_iface(global, iface.ifname))
1802                 return -1;
1803
1804         return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
1805 }
1806
1807
1808 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
1809                                               char *cmd)
1810 {
1811         struct wpa_supplicant *wpa_s;
1812
1813         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
1814
1815         wpa_s = wpa_supplicant_get_iface(global, cmd);
1816         if (wpa_s == NULL)
1817                 return -1;
1818         return wpa_supplicant_remove_iface(global, wpa_s);
1819 }
1820
1821
1822 static void wpa_free_iface_info(struct wpa_interface_info *iface)
1823 {
1824         struct wpa_interface_info *prev;
1825
1826         while (iface) {
1827                 prev = iface;
1828                 iface = iface->next;
1829
1830                 os_free(prev->ifname);
1831                 os_free(prev->desc);
1832                 os_free(prev);
1833         }
1834 }
1835
1836
1837 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
1838                                             char *buf, int len)
1839 {
1840         int i, res;
1841         struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
1842         char *pos, *end;
1843
1844         for (i = 0; wpa_drivers[i]; i++) {
1845                 struct wpa_driver_ops *drv = wpa_drivers[i];
1846                 if (drv->get_interfaces == NULL)
1847                         continue;
1848                 tmp = drv->get_interfaces(global->drv_priv);
1849                 if (tmp == NULL)
1850                         continue;
1851
1852                 if (last == NULL)
1853                         iface = last = tmp;
1854                 else
1855                         last->next = tmp;
1856                 while (last->next)
1857                         last = last->next;
1858         }
1859
1860         pos = buf;
1861         end = buf + len;
1862         for (tmp = iface; tmp; tmp = tmp->next) {
1863                 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
1864                                   tmp->drv_name, tmp->ifname,
1865                                   tmp->desc ? tmp->desc : "");
1866                 if (res < 0 || res >= end - pos) {
1867                         *pos = '\0';
1868                         break;
1869                 }
1870                 pos += res;
1871         }
1872
1873         wpa_free_iface_info(iface);
1874
1875         return pos - buf;
1876 }
1877
1878
1879 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
1880                                                   char *buf, int len)
1881 {
1882         int res;
1883         char *pos, *end;
1884         struct wpa_supplicant *wpa_s;
1885
1886         wpa_s = global->ifaces;
1887         pos = buf;
1888         end = buf + len;
1889
1890         while (wpa_s) {
1891                 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
1892                 if (res < 0 || res >= end - pos) {
1893                         *pos = '\0';
1894                         break;
1895                 }
1896                 pos += res;
1897                 wpa_s = wpa_s->next;
1898         }
1899         return pos - buf;
1900 }
1901
1902
1903 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
1904                                                 char *buf, size_t *resp_len)
1905 {
1906         char *reply;
1907         const int reply_size = 2048;
1908         int reply_len;
1909
1910         wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
1911                           (const u8 *) buf, os_strlen(buf));
1912
1913         reply = os_malloc(reply_size);
1914         if (reply == NULL) {
1915                 *resp_len = 1;
1916                 return NULL;
1917         }
1918
1919         os_memcpy(reply, "OK\n", 3);
1920         reply_len = 3;
1921
1922         if (os_strcmp(buf, "PING") == 0) {
1923                 os_memcpy(reply, "PONG\n", 5);
1924                 reply_len = 5;
1925         } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
1926                 if (wpa_supplicant_global_iface_add(global, buf + 14))
1927                         reply_len = -1;
1928         } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
1929                 if (wpa_supplicant_global_iface_remove(global, buf + 17))
1930                         reply_len = -1;
1931         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
1932                 reply_len = wpa_supplicant_global_iface_list(
1933                         global, reply, reply_size);
1934         } else if (os_strcmp(buf, "INTERFACES") == 0) {
1935                 reply_len = wpa_supplicant_global_iface_interfaces(
1936                         global, reply, reply_size);
1937         } else if (os_strcmp(buf, "TERMINATE") == 0) {
1938                 eloop_terminate();
1939         } else {
1940                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
1941                 reply_len = 16;
1942         }
1943
1944         if (reply_len < 0) {
1945                 os_memcpy(reply, "FAIL\n", 5);
1946                 reply_len = 5;
1947         }
1948
1949         *resp_len = reply_len;
1950         return reply;
1951 }