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