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