bebb125961ba5ac5c0aec57a56e1a6736c14f42e
[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 #ifdef CONFIG_AP
452                 if (wpa_s->ap_iface) {
453                         pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
454                                                             end - pos,
455                                                             verbose);
456                 } else
457 #endif /* CONFIG_AP */
458                 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
459         }
460         ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
461                           wpa_supplicant_state_txt(wpa_s->wpa_state));
462         if (ret < 0 || ret >= end - pos)
463                 return pos - buf;
464         pos += ret;
465
466         if (wpa_s->l2 &&
467             l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
468                 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
469                 if (ret < 0 || ret >= end - pos)
470                         return pos - buf;
471                 pos += ret;
472         }
473
474         if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
475             wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
476                 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
477                                           verbose);
478                 if (res >= 0)
479                         pos += res;
480         }
481
482         res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
483         if (res >= 0)
484                 pos += res;
485
486         return pos - buf;
487 }
488
489
490 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
491                                            char *cmd)
492 {
493         char *pos;
494         int id;
495         struct wpa_ssid *ssid;
496         u8 bssid[ETH_ALEN];
497
498         /* cmd: "<network id> <BSSID>" */
499         pos = os_strchr(cmd, ' ');
500         if (pos == NULL)
501                 return -1;
502         *pos++ = '\0';
503         id = atoi(cmd);
504         wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
505         if (hwaddr_aton(pos, bssid)) {
506                 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
507                 return -1;
508         }
509
510         ssid = wpa_config_get_network(wpa_s->conf, id);
511         if (ssid == NULL) {
512                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
513                            "to update", id);
514                 return -1;
515         }
516
517         os_memcpy(ssid->bssid, bssid, ETH_ALEN);
518         ssid->bssid_set = !is_zero_ether_addr(bssid);
519
520         return 0;
521 }
522
523
524 static int wpa_supplicant_ctrl_iface_list_networks(
525         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
526 {
527         char *pos, *end;
528         struct wpa_ssid *ssid;
529         int ret;
530
531         pos = buf;
532         end = buf + buflen;
533         ret = os_snprintf(pos, end - pos,
534                           "network id / ssid / bssid / flags\n");
535         if (ret < 0 || ret >= end - pos)
536                 return pos - buf;
537         pos += ret;
538
539         ssid = wpa_s->conf->ssid;
540         while (ssid) {
541                 ret = os_snprintf(pos, end - pos, "%d\t%s",
542                                   ssid->id,
543                                   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
544                 if (ret < 0 || ret >= end - pos)
545                         return pos - buf;
546                 pos += ret;
547                 if (ssid->bssid_set) {
548                         ret = os_snprintf(pos, end - pos, "\t" MACSTR,
549                                           MAC2STR(ssid->bssid));
550                 } else {
551                         ret = os_snprintf(pos, end - pos, "\tany");
552                 }
553                 if (ret < 0 || ret >= end - pos)
554                         return pos - buf;
555                 pos += ret;
556                 ret = os_snprintf(pos, end - pos, "\t%s%s",
557                                   ssid == wpa_s->current_ssid ?
558                                   "[CURRENT]" : "",
559                                   ssid->disabled ? "[DISABLED]" : "");
560                 if (ret < 0 || ret >= end - pos)
561                         return pos - buf;
562                 pos += ret;
563                 ret = os_snprintf(pos, end - pos, "\n");
564                 if (ret < 0 || ret >= end - pos)
565                         return pos - buf;
566                 pos += ret;
567
568                 ssid = ssid->next;
569         }
570
571         return pos - buf;
572 }
573
574
575 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
576 {
577         int first = 1, ret;
578         ret = os_snprintf(pos, end - pos, "-");
579         if (ret < 0 || ret >= end - pos)
580                 return pos;
581         pos += ret;
582         if (cipher & WPA_CIPHER_NONE) {
583                 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
584                 if (ret < 0 || ret >= end - pos)
585                         return pos;
586                 pos += ret;
587                 first = 0;
588         }
589         if (cipher & WPA_CIPHER_WEP40) {
590                 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
591                 if (ret < 0 || ret >= end - pos)
592                         return pos;
593                 pos += ret;
594                 first = 0;
595         }
596         if (cipher & WPA_CIPHER_WEP104) {
597                 ret = os_snprintf(pos, end - pos, "%sWEP104",
598                                   first ? "" : "+");
599                 if (ret < 0 || ret >= end - pos)
600                         return pos;
601                 pos += ret;
602                 first = 0;
603         }
604         if (cipher & WPA_CIPHER_TKIP) {
605                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
606                 if (ret < 0 || ret >= end - pos)
607                         return pos;
608                 pos += ret;
609                 first = 0;
610         }
611         if (cipher & WPA_CIPHER_CCMP) {
612                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
613                 if (ret < 0 || ret >= end - pos)
614                         return pos;
615                 pos += ret;
616                 first = 0;
617         }
618         return pos;
619 }
620
621
622 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
623                                     const u8 *ie, size_t ie_len)
624 {
625         struct wpa_ie_data data;
626         int first, ret;
627
628         ret = os_snprintf(pos, end - pos, "[%s-", proto);
629         if (ret < 0 || ret >= end - pos)
630                 return pos;
631         pos += ret;
632
633         if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
634                 ret = os_snprintf(pos, end - pos, "?]");
635                 if (ret < 0 || ret >= end - pos)
636                         return pos;
637                 pos += ret;
638                 return pos;
639         }
640
641         first = 1;
642         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
643                 ret = os_snprintf(pos, end - pos, "%sEAP", 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_PSK) {
650                 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
651                 if (ret < 0 || ret >= end - pos)
652                         return pos;
653                 pos += ret;
654                 first = 0;
655         }
656         if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
657                 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
658                 if (ret < 0 || ret >= end - pos)
659                         return pos;
660                 pos += ret;
661                 first = 0;
662         }
663 #ifdef CONFIG_IEEE80211R
664         if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
665                 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
666                                   first ? "" : "+");
667                 if (ret < 0 || ret >= end - pos)
668                         return pos;
669                 pos += ret;
670                 first = 0;
671         }
672         if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
673                 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
674                                   first ? "" : "+");
675                 if (ret < 0 || ret >= end - pos)
676                         return pos;
677                 pos += ret;
678                 first = 0;
679         }
680 #endif /* CONFIG_IEEE80211R */
681 #ifdef CONFIG_IEEE80211W
682         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
683                 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
684                                   first ? "" : "+");
685                 if (ret < 0 || ret >= end - pos)
686                         return pos;
687                 pos += ret;
688                 first = 0;
689         }
690         if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
691                 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
692                                   first ? "" : "+");
693                 if (ret < 0 || ret >= end - pos)
694                         return pos;
695                 pos += ret;
696                 first = 0;
697         }
698 #endif /* CONFIG_IEEE80211W */
699
700         pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
701
702         if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
703                 ret = os_snprintf(pos, end - pos, "-preauth");
704                 if (ret < 0 || ret >= end - pos)
705                         return pos;
706                 pos += ret;
707         }
708
709         ret = os_snprintf(pos, end - pos, "]");
710         if (ret < 0 || ret >= end - pos)
711                 return pos;
712         pos += ret;
713
714         return pos;
715 }
716
717 static char * wpa_supplicant_wps_ie_txt(char *pos, char *end,
718                                         const struct wpa_scan_res *res)
719 {
720 #ifdef CONFIG_WPS
721         struct wpabuf *wps_ie;
722         int ret;
723         const char *txt;
724
725         wps_ie = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
726         if (wps_ie == NULL)
727                 return pos;
728
729         if (wps_is_selected_pbc_registrar(wps_ie))
730                 txt = "[WPS-PBC]";
731         else if (wps_is_selected_pin_registrar(wps_ie))
732                 txt = "[WPS-PIN]";
733         else
734                 txt = "[WPS]";
735
736         ret = os_snprintf(pos, end - pos, "%s", txt);
737         if (ret >= 0 && ret < end - pos)
738                 pos += ret;
739         wpabuf_free(wps_ie);
740 #endif /* CONFIG_WPS */
741
742         return pos;
743 }
744
745
746 /* Format one result on one text line into a buffer. */
747 static int wpa_supplicant_ctrl_iface_scan_result(
748         const struct wpa_scan_res *res, char *buf, size_t buflen)
749 {
750         char *pos, *end;
751         int ret;
752         const u8 *ie, *ie2;
753
754         pos = buf;
755         end = buf + buflen;
756
757         ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
758                           MAC2STR(res->bssid), res->freq, res->level);
759         if (ret < 0 || ret >= end - pos)
760                 return pos - buf;
761         pos += ret;
762         ie = wpa_scan_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
763         if (ie)
764                 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
765         ie2 = wpa_scan_get_ie(res, WLAN_EID_RSN);
766         if (ie2)
767                 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
768         pos = wpa_supplicant_wps_ie_txt(pos, end, res);
769         if (!ie && !ie2 && res->caps & IEEE80211_CAP_PRIVACY) {
770                 ret = os_snprintf(pos, end - pos, "[WEP]");
771                 if (ret < 0 || ret >= end - pos)
772                         return pos - buf;
773                 pos += ret;
774         }
775         if (res->caps & IEEE80211_CAP_IBSS) {
776                 ret = os_snprintf(pos, end - pos, "[IBSS]");
777                 if (ret < 0 || ret >= end - pos)
778                         return pos - buf;
779                 pos += ret;
780         }
781
782         ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
783         ret = os_snprintf(pos, end - pos, "\t%s",
784                           ie ? wpa_ssid_txt(ie + 2, ie[1]) : "");
785         if (ret < 0 || ret >= end - pos)
786                 return pos - buf;
787         pos += ret;
788
789         ret = os_snprintf(pos, end - pos, "\n");
790         if (ret < 0 || ret >= end - pos)
791                 return pos - buf;
792         pos += ret;
793
794         return pos - buf;
795 }
796
797
798 static int wpa_supplicant_ctrl_iface_scan_results(
799         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
800 {
801         char *pos, *end;
802         struct wpa_scan_res *res;
803         int ret;
804         size_t i;
805
806         if (wpa_s->scan_res == NULL &&
807             wpa_supplicant_get_scan_results(wpa_s) < 0)
808                 return 0;
809
810         pos = buf;
811         end = buf + buflen;
812         ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
813                           "flags / ssid\n");
814         if (ret < 0 || ret >= end - pos)
815                 return pos - buf;
816         pos += ret;
817
818         for (i = 0; i < wpa_s->scan_res->num; i++) {
819                 res = wpa_s->scan_res->res[i];
820                 ret = wpa_supplicant_ctrl_iface_scan_result(res, pos,
821                                                             end - pos);
822                 if (ret < 0 || ret >= end - pos)
823                         return pos - buf;
824                 pos += ret;
825         }
826
827         return pos - buf;
828 }
829
830
831 static int wpa_supplicant_ctrl_iface_select_network(
832         struct wpa_supplicant *wpa_s, char *cmd)
833 {
834         int id;
835         struct wpa_ssid *ssid;
836
837         /* cmd: "<network id>" or "any" */
838         if (os_strcmp(cmd, "any") == 0) {
839                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
840                 ssid = NULL;
841         } else {
842                 id = atoi(cmd);
843                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
844
845                 ssid = wpa_config_get_network(wpa_s->conf, id);
846                 if (ssid == NULL) {
847                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
848                                    "network id=%d", id);
849                         return -1;
850                 }
851         }
852
853         wpa_supplicant_select_network(wpa_s, ssid);
854
855         return 0;
856 }
857
858
859 static int wpa_supplicant_ctrl_iface_enable_network(
860         struct wpa_supplicant *wpa_s, char *cmd)
861 {
862         int id;
863         struct wpa_ssid *ssid;
864
865         /* cmd: "<network id>" or "all" */
866         if (os_strcmp(cmd, "all") == 0) {
867                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
868                 ssid = NULL;
869         } else {
870                 id = atoi(cmd);
871                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
872
873                 ssid = wpa_config_get_network(wpa_s->conf, id);
874                 if (ssid == NULL) {
875                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
876                                    "network id=%d", id);
877                         return -1;
878                 }
879         }
880         wpa_supplicant_enable_network(wpa_s, ssid);
881
882         return 0;
883 }
884
885
886 static int wpa_supplicant_ctrl_iface_disable_network(
887         struct wpa_supplicant *wpa_s, char *cmd)
888 {
889         int id;
890         struct wpa_ssid *ssid;
891
892         /* cmd: "<network id>" or "all" */
893         if (os_strcmp(cmd, "all") == 0) {
894                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
895                 ssid = NULL;
896         } else {
897                 id = atoi(cmd);
898                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
899
900                 ssid = wpa_config_get_network(wpa_s->conf, id);
901                 if (ssid == NULL) {
902                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
903                                    "network id=%d", id);
904                         return -1;
905                 }
906         }
907         wpa_supplicant_disable_network(wpa_s, ssid);
908
909         return 0;
910 }
911
912
913 static int wpa_supplicant_ctrl_iface_add_network(
914         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
915 {
916         struct wpa_ssid *ssid;
917         int ret;
918
919         wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
920
921         ssid = wpa_config_add_network(wpa_s->conf);
922         if (ssid == NULL)
923                 return -1;
924
925         wpas_notify_network_added(wpa_s, ssid);
926
927         ssid->disabled = 1;
928         wpa_config_set_network_defaults(ssid);
929
930         ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
931         if (ret < 0 || (size_t) ret >= buflen)
932                 return -1;
933         return ret;
934 }
935
936
937 static int wpa_supplicant_ctrl_iface_remove_network(
938         struct wpa_supplicant *wpa_s, char *cmd)
939 {
940         int id;
941         struct wpa_ssid *ssid;
942
943         /* cmd: "<network id>" or "all" */
944         if (os_strcmp(cmd, "all") == 0) {
945                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
946                 ssid = wpa_s->conf->ssid;
947                 while (ssid) {
948                         struct wpa_ssid *remove_ssid = ssid;
949                         id = ssid->id;
950                         ssid = ssid->next;
951                         wpas_notify_network_removed(wpa_s, remove_ssid);
952                         wpa_config_remove_network(wpa_s->conf, id);
953                 }
954                 if (wpa_s->current_ssid) {
955                         eapol_sm_invalidate_cached_session(wpa_s->eapol);
956                         wpa_supplicant_disassociate(wpa_s,
957                                                     WLAN_REASON_DEAUTH_LEAVING);
958                 }
959                 return 0;
960         }
961
962         id = atoi(cmd);
963         wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
964
965         ssid = wpa_config_get_network(wpa_s->conf, id);
966         if (ssid == NULL ||
967             wpa_config_remove_network(wpa_s->conf, id) < 0) {
968                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
969                            "id=%d", id);
970                 return -1;
971         }
972
973         if (ssid == wpa_s->current_ssid) {
974                 /*
975                  * Invalidate the EAP session cache if the current network is
976                  * removed.
977                  */
978                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
979
980                 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
981         }
982
983         return 0;
984 }
985
986
987 static int wpa_supplicant_ctrl_iface_set_network(
988         struct wpa_supplicant *wpa_s, char *cmd)
989 {
990         int id;
991         struct wpa_ssid *ssid;
992         char *name, *value;
993
994         /* cmd: "<network id> <variable name> <value>" */
995         name = os_strchr(cmd, ' ');
996         if (name == NULL)
997                 return -1;
998         *name++ = '\0';
999
1000         value = os_strchr(name, ' ');
1001         if (value == NULL)
1002                 return -1;
1003         *value++ = '\0';
1004
1005         id = atoi(cmd);
1006         wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
1007                    id, name);
1008         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1009                               (u8 *) value, os_strlen(value));
1010
1011         ssid = wpa_config_get_network(wpa_s->conf, id);
1012         if (ssid == NULL) {
1013                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1014                            "id=%d", id);
1015                 return -1;
1016         }
1017
1018         if (wpa_config_set(ssid, name, value, 0) < 0) {
1019                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
1020                            "variable '%s'", name);
1021                 return -1;
1022         }
1023
1024         if (wpa_s->current_ssid == ssid) {
1025                 /*
1026                  * Invalidate the EAP session cache if anything in the current
1027                  * configuration changes.
1028                  */
1029                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1030         }
1031
1032         if ((os_strcmp(name, "psk") == 0 &&
1033              value[0] == '"' && ssid->ssid_len) ||
1034             (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
1035                 wpa_config_update_psk(ssid);
1036
1037         return 0;
1038 }
1039
1040
1041 static int wpa_supplicant_ctrl_iface_get_network(
1042         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1043 {
1044         int id;
1045         size_t res;
1046         struct wpa_ssid *ssid;
1047         char *name, *value;
1048
1049         /* cmd: "<network id> <variable name>" */
1050         name = os_strchr(cmd, ' ');
1051         if (name == NULL || buflen == 0)
1052                 return -1;
1053         *name++ = '\0';
1054
1055         id = atoi(cmd);
1056         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
1057                    id, name);
1058
1059         ssid = wpa_config_get_network(wpa_s->conf, id);
1060         if (ssid == NULL) {
1061                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1062                            "id=%d", id);
1063                 return -1;
1064         }
1065
1066         value = wpa_config_get_no_key(ssid, name);
1067         if (value == NULL) {
1068                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
1069                            "variable '%s'", name);
1070                 return -1;
1071         }
1072
1073         res = os_strlcpy(buf, value, buflen);
1074         if (res >= buflen) {
1075                 os_free(value);
1076                 return -1;
1077         }
1078
1079         os_free(value);
1080
1081         return res;
1082 }
1083
1084
1085 #ifndef CONFIG_NO_CONFIG_WRITE
1086 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
1087 {
1088         int ret;
1089
1090         if (!wpa_s->conf->update_config) {
1091                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
1092                            "to update configuration (update_config=0)");
1093                 return -1;
1094         }
1095
1096         ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
1097         if (ret) {
1098                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
1099                            "update configuration");
1100         } else {
1101                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
1102                            " updated");
1103         }
1104
1105         return ret;
1106 }
1107 #endif /* CONFIG_NO_CONFIG_WRITE */
1108
1109
1110 static int ctrl_iface_get_capability_pairwise(int res, char *strict,
1111                                               struct wpa_driver_capa *capa,
1112                                               char *buf, size_t buflen)
1113 {
1114         int ret, first = 1;
1115         char *pos, *end;
1116         size_t len;
1117
1118         pos = buf;
1119         end = pos + buflen;
1120
1121         if (res < 0) {
1122                 if (strict)
1123                         return 0;
1124                 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
1125                 if (len >= buflen)
1126                         return -1;
1127                 return len;
1128         }
1129
1130         if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
1131                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
1132                 if (ret < 0 || ret >= end - pos)
1133                         return pos - buf;
1134                 pos += ret;
1135                 first = 0;
1136         }
1137
1138         if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
1139                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
1140                 if (ret < 0 || ret >= end - pos)
1141                         return pos - buf;
1142                 pos += ret;
1143                 first = 0;
1144         }
1145
1146         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
1147                 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
1148                 if (ret < 0 || ret >= end - pos)
1149                         return pos - buf;
1150                 pos += ret;
1151                 first = 0;
1152         }
1153
1154         return pos - buf;
1155 }
1156
1157
1158 static int ctrl_iface_get_capability_group(int res, char *strict,
1159                                            struct wpa_driver_capa *capa,
1160                                            char *buf, size_t buflen)
1161 {
1162         int ret, first = 1;
1163         char *pos, *end;
1164         size_t len;
1165
1166         pos = buf;
1167         end = pos + buflen;
1168
1169         if (res < 0) {
1170                 if (strict)
1171                         return 0;
1172                 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
1173                 if (len >= buflen)
1174                         return -1;
1175                 return len;
1176         }
1177
1178         if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
1179                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
1180                 if (ret < 0 || ret >= end - pos)
1181                         return pos - buf;
1182                 pos += ret;
1183                 first = 0;
1184         }
1185
1186         if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
1187                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
1188                 if (ret < 0 || ret >= end - pos)
1189                         return pos - buf;
1190                 pos += ret;
1191                 first = 0;
1192         }
1193
1194         if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
1195                 ret = os_snprintf(pos, end - pos, "%sWEP104",
1196                                   first ? "" : " ");
1197                 if (ret < 0 || ret >= end - pos)
1198                         return pos - buf;
1199                 pos += ret;
1200                 first = 0;
1201         }
1202
1203         if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
1204                 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
1205                 if (ret < 0 || ret >= end - pos)
1206                         return pos - buf;
1207                 pos += ret;
1208                 first = 0;
1209         }
1210
1211         return pos - buf;
1212 }
1213
1214
1215 static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
1216                                               struct wpa_driver_capa *capa,
1217                                               char *buf, size_t buflen)
1218 {
1219         int ret;
1220         char *pos, *end;
1221         size_t len;
1222
1223         pos = buf;
1224         end = pos + buflen;
1225
1226         if (res < 0) {
1227                 if (strict)
1228                         return 0;
1229                 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
1230                                  "NONE", buflen);
1231                 if (len >= buflen)
1232                         return -1;
1233                 return len;
1234         }
1235
1236         ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
1237         if (ret < 0 || ret >= end - pos)
1238                 return pos - buf;
1239         pos += ret;
1240
1241         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1242                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
1243                 ret = os_snprintf(pos, end - pos, " WPA-EAP");
1244                 if (ret < 0 || ret >= end - pos)
1245                         return pos - buf;
1246                 pos += ret;
1247         }
1248
1249         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
1250                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
1251                 ret = os_snprintf(pos, end - pos, " WPA-PSK");
1252                 if (ret < 0 || ret >= end - pos)
1253                         return pos - buf;
1254                 pos += ret;
1255         }
1256
1257         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
1258                 ret = os_snprintf(pos, end - pos, " WPA-NONE");
1259                 if (ret < 0 || ret >= end - pos)
1260                         return pos - buf;
1261                 pos += ret;
1262         }
1263
1264         return pos - buf;
1265 }
1266
1267
1268 static int ctrl_iface_get_capability_proto(int res, char *strict,
1269                                            struct wpa_driver_capa *capa,
1270                                            char *buf, size_t buflen)
1271 {
1272         int ret, first = 1;
1273         char *pos, *end;
1274         size_t len;
1275
1276         pos = buf;
1277         end = pos + buflen;
1278
1279         if (res < 0) {
1280                 if (strict)
1281                         return 0;
1282                 len = os_strlcpy(buf, "RSN WPA", buflen);
1283                 if (len >= buflen)
1284                         return -1;
1285                 return len;
1286         }
1287
1288         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1289                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
1290                 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
1291                 if (ret < 0 || ret >= end - pos)
1292                         return pos - buf;
1293                 pos += ret;
1294                 first = 0;
1295         }
1296
1297         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1298                               WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
1299                 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
1300                 if (ret < 0 || ret >= end - pos)
1301                         return pos - buf;
1302                 pos += ret;
1303                 first = 0;
1304         }
1305
1306         return pos - buf;
1307 }
1308
1309
1310 static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
1311                                               struct wpa_driver_capa *capa,
1312                                               char *buf, size_t buflen)
1313 {
1314         int ret, first = 1;
1315         char *pos, *end;
1316         size_t len;
1317
1318         pos = buf;
1319         end = pos + buflen;
1320
1321         if (res < 0) {
1322                 if (strict)
1323                         return 0;
1324                 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
1325                 if (len >= buflen)
1326                         return -1;
1327                 return len;
1328         }
1329
1330         if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
1331                 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
1332                 if (ret < 0 || ret >= end - pos)
1333                         return pos - buf;
1334                 pos += ret;
1335                 first = 0;
1336         }
1337
1338         if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
1339                 ret = os_snprintf(pos, end - pos, "%sSHARED",
1340                                   first ? "" : " ");
1341                 if (ret < 0 || ret >= end - pos)
1342                         return pos - buf;
1343                 pos += ret;
1344                 first = 0;
1345         }
1346
1347         if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
1348                 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
1349                 if (ret < 0 || ret >= end - pos)
1350                         return pos - buf;
1351                 pos += ret;
1352                 first = 0;
1353         }
1354
1355         return pos - buf;
1356 }
1357
1358
1359 static int wpa_supplicant_ctrl_iface_get_capability(
1360         struct wpa_supplicant *wpa_s, const char *_field, char *buf,
1361         size_t buflen)
1362 {
1363         struct wpa_driver_capa capa;
1364         int res;
1365         char *strict;
1366         char field[30];
1367         size_t len;
1368
1369         /* Determine whether or not strict checking was requested */
1370         len = os_strlcpy(field, _field, sizeof(field));
1371         if (len >= sizeof(field))
1372                 return -1;
1373         strict = os_strchr(field, ' ');
1374         if (strict != NULL) {
1375                 *strict++ = '\0';
1376                 if (os_strcmp(strict, "strict") != 0)
1377                         return -1;
1378         }
1379
1380         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
1381                 field, strict ? strict : "");
1382
1383         if (os_strcmp(field, "eap") == 0) {
1384                 return eap_get_names(buf, buflen);
1385         }
1386
1387         res = wpa_drv_get_capa(wpa_s, &capa);
1388
1389         if (os_strcmp(field, "pairwise") == 0)
1390                 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
1391                                                           buf, buflen);
1392
1393         if (os_strcmp(field, "group") == 0)
1394                 return ctrl_iface_get_capability_group(res, strict, &capa,
1395                                                        buf, buflen);
1396
1397         if (os_strcmp(field, "key_mgmt") == 0)
1398                 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
1399                                                           buf, buflen);
1400
1401         if (os_strcmp(field, "proto") == 0)
1402                 return ctrl_iface_get_capability_proto(res, strict, &capa,
1403                                                        buf, buflen);
1404
1405         if (os_strcmp(field, "auth_alg") == 0)
1406                 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
1407                                                           buf, buflen);
1408
1409         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
1410                    field);
1411
1412         return -1;
1413 }
1414
1415
1416 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
1417                                          const char *cmd, char *buf,
1418                                          size_t buflen)
1419 {
1420         u8 bssid[ETH_ALEN];
1421         size_t i;
1422         struct wpa_scan_results *results;
1423         struct wpa_scan_res *bss;
1424         int ret;
1425         char *pos, *end;
1426         const u8 *ie, *ie2;
1427
1428         if (wpa_s->scan_res == NULL &&
1429             wpa_supplicant_get_scan_results(wpa_s) < 0)
1430                 return 0;
1431
1432         results = wpa_s->scan_res;
1433         if (results == NULL)
1434                 return 0;
1435
1436         if (hwaddr_aton(cmd, bssid) == 0) {
1437                 for (i = 0; i < results->num; i++) {
1438                         if (os_memcmp(bssid, results->res[i]->bssid, ETH_ALEN)
1439                             == 0)
1440                                 break;
1441                 }
1442         } else
1443                 i = atoi(cmd);
1444
1445         if (i >= results->num || results->res[i] == NULL)
1446                 return 0; /* no match found */
1447
1448         bss = results->res[i];
1449         pos = buf;
1450         end = buf + buflen;
1451         ret = os_snprintf(pos, end - pos,
1452                           "bssid=" MACSTR "\n"
1453                           "freq=%d\n"
1454                           "beacon_int=%d\n"
1455                           "capabilities=0x%04x\n"
1456                           "qual=%d\n"
1457                           "noise=%d\n"
1458                           "level=%d\n"
1459                           "tsf=%016llu\n"
1460                           "ie=",
1461                           MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
1462                           bss->caps, bss->qual, bss->noise, bss->level,
1463                           (unsigned long long) bss->tsf);
1464         if (ret < 0 || ret >= end - pos)
1465                 return pos - buf;
1466         pos += ret;
1467
1468         ie = (const u8 *) (bss + 1);
1469         for (i = 0; i < bss->ie_len; i++) {
1470                 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
1471                 if (ret < 0 || ret >= end - pos)
1472                         return pos - buf;
1473                 pos += ret;
1474         }
1475
1476         ret = os_snprintf(pos, end - pos, "\n");
1477         if (ret < 0 || ret >= end - pos)
1478                 return pos - buf;
1479         pos += ret;
1480
1481         ret = os_snprintf(pos, end - pos, "flags=");
1482         if (ret < 0 || ret >= end - pos)
1483                 return pos - buf;
1484         pos += ret;
1485
1486         ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1487         if (ie)
1488                 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
1489         ie2 = wpa_scan_get_ie(bss, WLAN_EID_RSN);
1490         if (ie2)
1491                 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
1492         pos = wpa_supplicant_wps_ie_txt(pos, end, bss);
1493         if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
1494                 ret = os_snprintf(pos, end - pos, "[WEP]");
1495                 if (ret < 0 || ret >= end - pos)
1496                         return pos - buf;
1497                 pos += ret;
1498         }
1499         if (bss->caps & IEEE80211_CAP_IBSS) {
1500                 ret = os_snprintf(pos, end - pos, "[IBSS]");
1501                 if (ret < 0 || ret >= end - pos)
1502                         return pos - buf;
1503                 pos += ret;
1504         }
1505
1506         ret = os_snprintf(pos, end - pos, "\n");
1507         if (ret < 0 || ret >= end - pos)
1508                 return pos - buf;
1509         pos += ret;
1510
1511         ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1512         ret = os_snprintf(pos, end - pos, "ssid=%s\n",
1513                           ie ? wpa_ssid_txt(ie + 2, ie[1]) : "");
1514         if (ret < 0 || ret >= end - pos)
1515                 return pos - buf;
1516         pos += ret;
1517
1518 #ifdef CONFIG_WPS
1519         ie = (const u8 *) (bss + 1);
1520         ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
1521         if (ret < 0 || ret >= end - pos)
1522                 return pos - buf;
1523         pos += ret;
1524 #endif /* CONFIG_WPS */
1525
1526         return pos - buf;
1527 }
1528
1529
1530 static int wpa_supplicant_ctrl_iface_ap_scan(
1531         struct wpa_supplicant *wpa_s, char *cmd)
1532 {
1533         int ap_scan = atoi(cmd);
1534         return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
1535 }
1536
1537
1538 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
1539                                          char *buf, size_t *resp_len)
1540 {
1541         char *reply;
1542         const int reply_size = 2048;
1543         int ctrl_rsp = 0;
1544         int reply_len;
1545
1546         if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
1547             os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
1548                 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
1549                                       (const u8 *) buf, os_strlen(buf));
1550         } else {
1551                 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface",
1552                                   (const u8 *) buf, os_strlen(buf));
1553         }
1554
1555         reply = os_malloc(reply_size);
1556         if (reply == NULL) {
1557                 *resp_len = 1;
1558                 return NULL;
1559         }
1560
1561         os_memcpy(reply, "OK\n", 3);
1562         reply_len = 3;
1563
1564         if (os_strcmp(buf, "PING") == 0) {
1565                 os_memcpy(reply, "PONG\n", 5);
1566                 reply_len = 5;
1567         } else if (os_strcmp(buf, "MIB") == 0) {
1568                 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
1569                 if (reply_len >= 0) {
1570                         int res;
1571                         res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
1572                                                reply_size - reply_len);
1573                         if (res < 0)
1574                                 reply_len = -1;
1575                         else
1576                                 reply_len += res;
1577                 }
1578         } else if (os_strncmp(buf, "STATUS", 6) == 0) {
1579                 reply_len = wpa_supplicant_ctrl_iface_status(
1580                         wpa_s, buf + 6, reply, reply_size);
1581         } else if (os_strcmp(buf, "PMKSA") == 0) {
1582                 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
1583                                                     reply_size);
1584         } else if (os_strncmp(buf, "SET ", 4) == 0) {
1585                 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
1586                         reply_len = -1;
1587         } else if (os_strcmp(buf, "LOGON") == 0) {
1588                 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
1589         } else if (os_strcmp(buf, "LOGOFF") == 0) {
1590                 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
1591         } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
1592                 wpa_s->disconnected = 0;
1593                 wpa_s->reassociate = 1;
1594                 wpa_supplicant_req_scan(wpa_s, 0, 0);
1595         } else if (os_strcmp(buf, "RECONNECT") == 0) {
1596                 if (wpa_s->disconnected) {
1597                         wpa_s->disconnected = 0;
1598                         wpa_s->reassociate = 1;
1599                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1600                 }
1601 #ifdef IEEE8021X_EAPOL
1602         } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
1603                 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
1604                         reply_len = -1;
1605 #endif /* IEEE8021X_EAPOL */
1606 #ifdef CONFIG_PEERKEY
1607         } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
1608                 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
1609                         reply_len = -1;
1610 #endif /* CONFIG_PEERKEY */
1611 #ifdef CONFIG_IEEE80211R
1612         } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
1613                 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
1614                         reply_len = -1;
1615 #endif /* CONFIG_IEEE80211R */
1616 #ifdef CONFIG_WPS
1617         } else if (os_strcmp(buf, "WPS_PBC") == 0) {
1618                 if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL))
1619                         reply_len = -1;
1620         } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
1621                 if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8))
1622                         reply_len = -1;
1623         } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
1624                 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
1625                                                               reply,
1626                                                               reply_size);
1627 #ifdef CONFIG_WPS_OOB
1628         } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
1629                 if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
1630                         reply_len = -1;
1631 #endif /* CONFIG_WPS_OOB */
1632         } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
1633                 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
1634                         reply_len = -1;
1635 #endif /* CONFIG_WPS */
1636 #ifdef CONFIG_IBSS_RSN
1637         } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
1638                 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
1639                         reply_len = -1;
1640 #endif /* CONFIG_IBSS_RSN */
1641         } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
1642         {
1643                 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
1644                             wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
1645                         reply_len = -1;
1646                 else
1647                         ctrl_rsp = 1;
1648         } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
1649                 if (wpa_supplicant_reload_configuration(wpa_s))
1650                         reply_len = -1;
1651         } else if (os_strcmp(buf, "TERMINATE") == 0) {
1652                 eloop_terminate();
1653         } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
1654                 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
1655                         reply_len = -1;
1656         } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
1657                 reply_len = wpa_supplicant_ctrl_iface_list_networks(
1658                         wpa_s, reply, reply_size);
1659         } else if (os_strcmp(buf, "DISCONNECT") == 0) {
1660                 wpa_s->reassociate = 0;
1661                 wpa_s->disconnected = 1;
1662                 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1663         } else if (os_strcmp(buf, "SCAN") == 0) {
1664                 wpa_s->scan_req = 2;
1665                 wpa_supplicant_req_scan(wpa_s, 0, 0);
1666         } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
1667                 reply_len = wpa_supplicant_ctrl_iface_scan_results(
1668                         wpa_s, reply, reply_size);
1669         } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
1670                 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
1671                         reply_len = -1;
1672         } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
1673                 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
1674                         reply_len = -1;
1675         } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
1676                 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
1677                         reply_len = -1;
1678         } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
1679                 reply_len = wpa_supplicant_ctrl_iface_add_network(
1680                         wpa_s, reply, reply_size);
1681         } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
1682                 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
1683                         reply_len = -1;
1684         } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
1685                 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
1686                         reply_len = -1;
1687         } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
1688                 reply_len = wpa_supplicant_ctrl_iface_get_network(
1689                         wpa_s, buf + 12, reply, reply_size);
1690 #ifndef CONFIG_NO_CONFIG_WRITE
1691         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
1692                 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
1693                         reply_len = -1;
1694 #endif /* CONFIG_NO_CONFIG_WRITE */
1695         } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
1696                 reply_len = wpa_supplicant_ctrl_iface_get_capability(
1697                         wpa_s, buf + 15, reply, reply_size);
1698         } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
1699                 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
1700                         reply_len = -1;
1701         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
1702                 reply_len = wpa_supplicant_global_iface_list(
1703                         wpa_s->global, reply, reply_size);
1704         } else if (os_strcmp(buf, "INTERFACES") == 0) {
1705                 reply_len = wpa_supplicant_global_iface_interfaces(
1706                         wpa_s->global, reply, reply_size);
1707         } else if (os_strncmp(buf, "BSS ", 4) == 0) {
1708                 reply_len = wpa_supplicant_ctrl_iface_bss(
1709                         wpa_s, buf + 4, reply, reply_size);
1710 #ifdef CONFIG_AP
1711         } else if (os_strcmp(buf, "STA-FIRST") == 0) {
1712                 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
1713         } else if (os_strncmp(buf, "STA ", 4) == 0) {
1714                 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
1715                                               reply_size);
1716         } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
1717                 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
1718                                                    reply_size);
1719 #endif /* CONFIG_AP */
1720         } else {
1721                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
1722                 reply_len = 16;
1723         }
1724
1725         if (reply_len < 0) {
1726                 os_memcpy(reply, "FAIL\n", 5);
1727                 reply_len = 5;
1728         }
1729
1730         if (ctrl_rsp)
1731                 eapol_sm_notify_ctrl_response(wpa_s->eapol);
1732
1733         *resp_len = reply_len;
1734         return reply;
1735 }
1736
1737
1738 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
1739                                            char *cmd)
1740 {
1741         struct wpa_interface iface;
1742         char *pos;
1743
1744         /*
1745          * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
1746          * TAB<bridge_ifname>
1747          */
1748         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
1749
1750         os_memset(&iface, 0, sizeof(iface));
1751
1752         do {
1753                 iface.ifname = pos = cmd;
1754                 pos = os_strchr(pos, '\t');
1755                 if (pos)
1756                         *pos++ = '\0';
1757                 if (iface.ifname[0] == '\0')
1758                         return -1;
1759                 if (pos == NULL)
1760                         break;
1761
1762                 iface.confname = pos;
1763                 pos = os_strchr(pos, '\t');
1764                 if (pos)
1765                         *pos++ = '\0';
1766                 if (iface.confname[0] == '\0')
1767                         iface.confname = NULL;
1768                 if (pos == NULL)
1769                         break;
1770
1771                 iface.driver = pos;
1772                 pos = os_strchr(pos, '\t');
1773                 if (pos)
1774                         *pos++ = '\0';
1775                 if (iface.driver[0] == '\0')
1776                         iface.driver = NULL;
1777                 if (pos == NULL)
1778                         break;
1779
1780                 iface.ctrl_interface = pos;
1781                 pos = os_strchr(pos, '\t');
1782                 if (pos)
1783                         *pos++ = '\0';
1784                 if (iface.ctrl_interface[0] == '\0')
1785                         iface.ctrl_interface = NULL;
1786                 if (pos == NULL)
1787                         break;
1788
1789                 iface.driver_param = pos;
1790                 pos = os_strchr(pos, '\t');
1791                 if (pos)
1792                         *pos++ = '\0';
1793                 if (iface.driver_param[0] == '\0')
1794                         iface.driver_param = NULL;
1795                 if (pos == NULL)
1796                         break;
1797
1798                 iface.bridge_ifname = pos;
1799                 pos = os_strchr(pos, '\t');
1800                 if (pos)
1801                         *pos++ = '\0';
1802                 if (iface.bridge_ifname[0] == '\0')
1803                         iface.bridge_ifname = NULL;
1804                 if (pos == NULL)
1805                         break;
1806         } while (0);
1807
1808         if (wpa_supplicant_get_iface(global, iface.ifname))
1809                 return -1;
1810
1811         return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
1812 }
1813
1814
1815 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
1816                                               char *cmd)
1817 {
1818         struct wpa_supplicant *wpa_s;
1819
1820         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
1821
1822         wpa_s = wpa_supplicant_get_iface(global, cmd);
1823         if (wpa_s == NULL)
1824                 return -1;
1825         return wpa_supplicant_remove_iface(global, wpa_s);
1826 }
1827
1828
1829 static void wpa_free_iface_info(struct wpa_interface_info *iface)
1830 {
1831         struct wpa_interface_info *prev;
1832
1833         while (iface) {
1834                 prev = iface;
1835                 iface = iface->next;
1836
1837                 os_free(prev->ifname);
1838                 os_free(prev->desc);
1839                 os_free(prev);
1840         }
1841 }
1842
1843
1844 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
1845                                             char *buf, int len)
1846 {
1847         int i, res;
1848         struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
1849         char *pos, *end;
1850
1851         for (i = 0; wpa_drivers[i]; i++) {
1852                 struct wpa_driver_ops *drv = wpa_drivers[i];
1853                 if (drv->get_interfaces == NULL)
1854                         continue;
1855                 tmp = drv->get_interfaces(global->drv_priv);
1856                 if (tmp == NULL)
1857                         continue;
1858
1859                 if (last == NULL)
1860                         iface = last = tmp;
1861                 else
1862                         last->next = tmp;
1863                 while (last->next)
1864                         last = last->next;
1865         }
1866
1867         pos = buf;
1868         end = buf + len;
1869         for (tmp = iface; tmp; tmp = tmp->next) {
1870                 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
1871                                   tmp->drv_name, tmp->ifname,
1872                                   tmp->desc ? tmp->desc : "");
1873                 if (res < 0 || res >= end - pos) {
1874                         *pos = '\0';
1875                         break;
1876                 }
1877                 pos += res;
1878         }
1879
1880         wpa_free_iface_info(iface);
1881
1882         return pos - buf;
1883 }
1884
1885
1886 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
1887                                                   char *buf, int len)
1888 {
1889         int res;
1890         char *pos, *end;
1891         struct wpa_supplicant *wpa_s;
1892
1893         wpa_s = global->ifaces;
1894         pos = buf;
1895         end = buf + len;
1896
1897         while (wpa_s) {
1898                 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
1899                 if (res < 0 || res >= end - pos) {
1900                         *pos = '\0';
1901                         break;
1902                 }
1903                 pos += res;
1904                 wpa_s = wpa_s->next;
1905         }
1906         return pos - buf;
1907 }
1908
1909
1910 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
1911                                                 char *buf, size_t *resp_len)
1912 {
1913         char *reply;
1914         const int reply_size = 2048;
1915         int reply_len;
1916
1917         wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
1918                           (const u8 *) buf, os_strlen(buf));
1919
1920         reply = os_malloc(reply_size);
1921         if (reply == NULL) {
1922                 *resp_len = 1;
1923                 return NULL;
1924         }
1925
1926         os_memcpy(reply, "OK\n", 3);
1927         reply_len = 3;
1928
1929         if (os_strcmp(buf, "PING") == 0) {
1930                 os_memcpy(reply, "PONG\n", 5);
1931                 reply_len = 5;
1932         } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
1933                 if (wpa_supplicant_global_iface_add(global, buf + 14))
1934                         reply_len = -1;
1935         } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
1936                 if (wpa_supplicant_global_iface_remove(global, buf + 17))
1937                         reply_len = -1;
1938         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
1939                 reply_len = wpa_supplicant_global_iface_list(
1940                         global, reply, reply_size);
1941         } else if (os_strcmp(buf, "INTERFACES") == 0) {
1942                 reply_len = wpa_supplicant_global_iface_interfaces(
1943                         global, reply, reply_size);
1944         } else if (os_strcmp(buf, "TERMINATE") == 0) {
1945                 eloop_terminate();
1946         } else {
1947                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
1948                 reply_len = 16;
1949         }
1950
1951         if (reply_len < 0) {
1952                 os_memcpy(reply, "FAIL\n", 5);
1953                 reply_len = 5;
1954         }
1955
1956         *resp_len = reply_len;
1957         return reply;
1958 }