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