c7d34276281eb6136cf3fad1213c7ffdeae877e0
[libeap.git] / wpa_supplicant / ctrl_iface.c
1 /*
2  * WPA Supplicant / Control interface (shared code for all backends)
3  * Copyright (c) 2004-2010, 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         else if (os_strcmp(name, "priority") == 0)
1096                 wpa_config_update_prio_list(wpa_s->conf);
1097
1098         return 0;
1099 }
1100
1101
1102 static int wpa_supplicant_ctrl_iface_get_network(
1103         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1104 {
1105         int id;
1106         size_t res;
1107         struct wpa_ssid *ssid;
1108         char *name, *value;
1109
1110         /* cmd: "<network id> <variable name>" */
1111         name = os_strchr(cmd, ' ');
1112         if (name == NULL || buflen == 0)
1113                 return -1;
1114         *name++ = '\0';
1115
1116         id = atoi(cmd);
1117         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
1118                    id, name);
1119
1120         ssid = wpa_config_get_network(wpa_s->conf, id);
1121         if (ssid == NULL) {
1122                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1123                            "id=%d", id);
1124                 return -1;
1125         }
1126
1127         value = wpa_config_get_no_key(ssid, name);
1128         if (value == NULL) {
1129                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
1130                            "variable '%s'", name);
1131                 return -1;
1132         }
1133
1134         res = os_strlcpy(buf, value, buflen);
1135         if (res >= buflen) {
1136                 os_free(value);
1137                 return -1;
1138         }
1139
1140         os_free(value);
1141
1142         return res;
1143 }
1144
1145
1146 #ifndef CONFIG_NO_CONFIG_WRITE
1147 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
1148 {
1149         int ret;
1150
1151         if (!wpa_s->conf->update_config) {
1152                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
1153                            "to update configuration (update_config=0)");
1154                 return -1;
1155         }
1156
1157         ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
1158         if (ret) {
1159                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
1160                            "update configuration");
1161         } else {
1162                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
1163                            " updated");
1164         }
1165
1166         return ret;
1167 }
1168 #endif /* CONFIG_NO_CONFIG_WRITE */
1169
1170
1171 static int ctrl_iface_get_capability_pairwise(int res, char *strict,
1172                                               struct wpa_driver_capa *capa,
1173                                               char *buf, size_t buflen)
1174 {
1175         int ret, first = 1;
1176         char *pos, *end;
1177         size_t len;
1178
1179         pos = buf;
1180         end = pos + buflen;
1181
1182         if (res < 0) {
1183                 if (strict)
1184                         return 0;
1185                 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
1186                 if (len >= buflen)
1187                         return -1;
1188                 return len;
1189         }
1190
1191         if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
1192                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
1193                 if (ret < 0 || ret >= end - pos)
1194                         return pos - buf;
1195                 pos += ret;
1196                 first = 0;
1197         }
1198
1199         if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
1200                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
1201                 if (ret < 0 || ret >= end - pos)
1202                         return pos - buf;
1203                 pos += ret;
1204                 first = 0;
1205         }
1206
1207         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
1208                 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
1209                 if (ret < 0 || ret >= end - pos)
1210                         return pos - buf;
1211                 pos += ret;
1212                 first = 0;
1213         }
1214
1215         return pos - buf;
1216 }
1217
1218
1219 static int ctrl_iface_get_capability_group(int res, char *strict,
1220                                            struct wpa_driver_capa *capa,
1221                                            char *buf, size_t buflen)
1222 {
1223         int ret, first = 1;
1224         char *pos, *end;
1225         size_t len;
1226
1227         pos = buf;
1228         end = pos + buflen;
1229
1230         if (res < 0) {
1231                 if (strict)
1232                         return 0;
1233                 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
1234                 if (len >= buflen)
1235                         return -1;
1236                 return len;
1237         }
1238
1239         if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
1240                 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
1241                 if (ret < 0 || ret >= end - pos)
1242                         return pos - buf;
1243                 pos += ret;
1244                 first = 0;
1245         }
1246
1247         if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
1248                 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
1249                 if (ret < 0 || ret >= end - pos)
1250                         return pos - buf;
1251                 pos += ret;
1252                 first = 0;
1253         }
1254
1255         if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
1256                 ret = os_snprintf(pos, end - pos, "%sWEP104",
1257                                   first ? "" : " ");
1258                 if (ret < 0 || ret >= end - pos)
1259                         return pos - buf;
1260                 pos += ret;
1261                 first = 0;
1262         }
1263
1264         if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
1265                 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
1266                 if (ret < 0 || ret >= end - pos)
1267                         return pos - buf;
1268                 pos += ret;
1269                 first = 0;
1270         }
1271
1272         return pos - buf;
1273 }
1274
1275
1276 static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
1277                                               struct wpa_driver_capa *capa,
1278                                               char *buf, size_t buflen)
1279 {
1280         int ret;
1281         char *pos, *end;
1282         size_t len;
1283
1284         pos = buf;
1285         end = pos + buflen;
1286
1287         if (res < 0) {
1288                 if (strict)
1289                         return 0;
1290                 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
1291                                  "NONE", buflen);
1292                 if (len >= buflen)
1293                         return -1;
1294                 return len;
1295         }
1296
1297         ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
1298         if (ret < 0 || ret >= end - pos)
1299                 return pos - buf;
1300         pos += ret;
1301
1302         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1303                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
1304                 ret = os_snprintf(pos, end - pos, " WPA-EAP");
1305                 if (ret < 0 || ret >= end - pos)
1306                         return pos - buf;
1307                 pos += ret;
1308         }
1309
1310         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
1311                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
1312                 ret = os_snprintf(pos, end - pos, " WPA-PSK");
1313                 if (ret < 0 || ret >= end - pos)
1314                         return pos - buf;
1315                 pos += ret;
1316         }
1317
1318         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
1319                 ret = os_snprintf(pos, end - pos, " WPA-NONE");
1320                 if (ret < 0 || ret >= end - pos)
1321                         return pos - buf;
1322                 pos += ret;
1323         }
1324
1325         return pos - buf;
1326 }
1327
1328
1329 static int ctrl_iface_get_capability_proto(int res, char *strict,
1330                                            struct wpa_driver_capa *capa,
1331                                            char *buf, size_t buflen)
1332 {
1333         int ret, first = 1;
1334         char *pos, *end;
1335         size_t len;
1336
1337         pos = buf;
1338         end = pos + buflen;
1339
1340         if (res < 0) {
1341                 if (strict)
1342                         return 0;
1343                 len = os_strlcpy(buf, "RSN WPA", buflen);
1344                 if (len >= buflen)
1345                         return -1;
1346                 return len;
1347         }
1348
1349         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1350                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
1351                 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
1352                 if (ret < 0 || ret >= end - pos)
1353                         return pos - buf;
1354                 pos += ret;
1355                 first = 0;
1356         }
1357
1358         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1359                               WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
1360                 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
1361                 if (ret < 0 || ret >= end - pos)
1362                         return pos - buf;
1363                 pos += ret;
1364                 first = 0;
1365         }
1366
1367         return pos - buf;
1368 }
1369
1370
1371 static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
1372                                               struct wpa_driver_capa *capa,
1373                                               char *buf, size_t buflen)
1374 {
1375         int ret, first = 1;
1376         char *pos, *end;
1377         size_t len;
1378
1379         pos = buf;
1380         end = pos + buflen;
1381
1382         if (res < 0) {
1383                 if (strict)
1384                         return 0;
1385                 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
1386                 if (len >= buflen)
1387                         return -1;
1388                 return len;
1389         }
1390
1391         if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
1392                 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
1393                 if (ret < 0 || ret >= end - pos)
1394                         return pos - buf;
1395                 pos += ret;
1396                 first = 0;
1397         }
1398
1399         if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
1400                 ret = os_snprintf(pos, end - pos, "%sSHARED",
1401                                   first ? "" : " ");
1402                 if (ret < 0 || ret >= end - pos)
1403                         return pos - buf;
1404                 pos += ret;
1405                 first = 0;
1406         }
1407
1408         if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
1409                 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
1410                 if (ret < 0 || ret >= end - pos)
1411                         return pos - buf;
1412                 pos += ret;
1413                 first = 0;
1414         }
1415
1416         return pos - buf;
1417 }
1418
1419
1420 static int wpa_supplicant_ctrl_iface_get_capability(
1421         struct wpa_supplicant *wpa_s, const char *_field, char *buf,
1422         size_t buflen)
1423 {
1424         struct wpa_driver_capa capa;
1425         int res;
1426         char *strict;
1427         char field[30];
1428         size_t len;
1429
1430         /* Determine whether or not strict checking was requested */
1431         len = os_strlcpy(field, _field, sizeof(field));
1432         if (len >= sizeof(field))
1433                 return -1;
1434         strict = os_strchr(field, ' ');
1435         if (strict != NULL) {
1436                 *strict++ = '\0';
1437                 if (os_strcmp(strict, "strict") != 0)
1438                         return -1;
1439         }
1440
1441         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
1442                 field, strict ? strict : "");
1443
1444         if (os_strcmp(field, "eap") == 0) {
1445                 return eap_get_names(buf, buflen);
1446         }
1447
1448         res = wpa_drv_get_capa(wpa_s, &capa);
1449
1450         if (os_strcmp(field, "pairwise") == 0)
1451                 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
1452                                                           buf, buflen);
1453
1454         if (os_strcmp(field, "group") == 0)
1455                 return ctrl_iface_get_capability_group(res, strict, &capa,
1456                                                        buf, buflen);
1457
1458         if (os_strcmp(field, "key_mgmt") == 0)
1459                 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
1460                                                           buf, buflen);
1461
1462         if (os_strcmp(field, "proto") == 0)
1463                 return ctrl_iface_get_capability_proto(res, strict, &capa,
1464                                                        buf, buflen);
1465
1466         if (os_strcmp(field, "auth_alg") == 0)
1467                 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
1468                                                           buf, buflen);
1469
1470         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
1471                    field);
1472
1473         return -1;
1474 }
1475
1476
1477 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
1478                                          const char *cmd, char *buf,
1479                                          size_t buflen)
1480 {
1481         u8 bssid[ETH_ALEN];
1482         size_t i;
1483         struct wpa_bss *bss;
1484         int ret;
1485         char *pos, *end;
1486         const u8 *ie, *ie2;
1487
1488         if (os_strcmp(cmd, "FIRST") == 0)
1489                 bss = dl_list_first(&wpa_s->bss, struct wpa_bss, list);
1490         else if (os_strncmp(cmd, "ID-", 3) == 0) {
1491                 i = atoi(cmd + 3);
1492                 bss = wpa_bss_get_id(wpa_s, i);
1493         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
1494                 i = atoi(cmd + 5);
1495                 bss = wpa_bss_get_id(wpa_s, i);
1496                 if (bss) {
1497                         struct dl_list *next = bss->list_id.next;
1498                         if (next == &wpa_s->bss_id)
1499                                 bss = NULL;
1500                         else
1501                                 bss = dl_list_entry(next, struct wpa_bss,
1502                                                     list_id);
1503                 }
1504         } else if (hwaddr_aton(cmd, bssid) == 0)
1505                 bss = wpa_bss_get_bssid(wpa_s, bssid);
1506         else {
1507                 struct wpa_bss *tmp;
1508                 i = atoi(cmd);
1509                 bss = NULL;
1510                 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
1511                 {
1512                         if (i-- == 0) {
1513                                 bss = tmp;
1514                                 break;
1515                         }
1516                 }
1517         }
1518
1519         if (bss == NULL)
1520                 return 0;
1521
1522         pos = buf;
1523         end = buf + buflen;
1524         ret = os_snprintf(pos, end - pos,
1525                           "id=%u\n"
1526                           "bssid=" MACSTR "\n"
1527                           "freq=%d\n"
1528                           "beacon_int=%d\n"
1529                           "capabilities=0x%04x\n"
1530                           "qual=%d\n"
1531                           "noise=%d\n"
1532                           "level=%d\n"
1533                           "tsf=%016llu\n"
1534                           "ie=",
1535                           bss->id,
1536                           MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
1537                           bss->caps, bss->qual, bss->noise, bss->level,
1538                           (unsigned long long) bss->tsf);
1539         if (ret < 0 || ret >= end - pos)
1540                 return pos - buf;
1541         pos += ret;
1542
1543         ie = (const u8 *) (bss + 1);
1544         for (i = 0; i < bss->ie_len; i++) {
1545                 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
1546                 if (ret < 0 || ret >= end - pos)
1547                         return pos - buf;
1548                 pos += ret;
1549         }
1550
1551         ret = os_snprintf(pos, end - pos, "\n");
1552         if (ret < 0 || ret >= end - pos)
1553                 return pos - buf;
1554         pos += ret;
1555
1556         ret = os_snprintf(pos, end - pos, "flags=");
1557         if (ret < 0 || ret >= end - pos)
1558                 return pos - buf;
1559         pos += ret;
1560
1561         ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1562         if (ie)
1563                 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
1564         ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1565         if (ie2)
1566                 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
1567         pos = wpa_supplicant_wps_ie_txt(pos, end, bss);
1568         if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
1569                 ret = os_snprintf(pos, end - pos, "[WEP]");
1570                 if (ret < 0 || ret >= end - pos)
1571                         return pos - buf;
1572                 pos += ret;
1573         }
1574         if (bss->caps & IEEE80211_CAP_IBSS) {
1575                 ret = os_snprintf(pos, end - pos, "[IBSS]");
1576                 if (ret < 0 || ret >= end - pos)
1577                         return pos - buf;
1578                 pos += ret;
1579         }
1580         if (bss->caps & IEEE80211_CAP_ESS) {
1581                 ret = os_snprintf(pos, end - pos, "[ESS]");
1582                 if (ret < 0 || ret >= end - pos)
1583                         return pos - buf;
1584                 pos += ret;
1585         }
1586
1587         ret = os_snprintf(pos, end - pos, "\n");
1588         if (ret < 0 || ret >= end - pos)
1589                 return pos - buf;
1590         pos += ret;
1591
1592         ret = os_snprintf(pos, end - pos, "ssid=%s\n",
1593                           wpa_ssid_txt(bss->ssid, bss->ssid_len));
1594         if (ret < 0 || ret >= end - pos)
1595                 return pos - buf;
1596         pos += ret;
1597
1598 #ifdef CONFIG_WPS
1599         ie = (const u8 *) (bss + 1);
1600         ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
1601         if (ret < 0 || ret >= end - pos)
1602                 return pos - buf;
1603         pos += ret;
1604 #endif /* CONFIG_WPS */
1605
1606         return pos - buf;
1607 }
1608
1609
1610 static int wpa_supplicant_ctrl_iface_ap_scan(
1611         struct wpa_supplicant *wpa_s, char *cmd)
1612 {
1613         int ap_scan = atoi(cmd);
1614         return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
1615 }
1616
1617
1618 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
1619 {
1620         u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
1621
1622         wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
1623         /* MLME-DELETEKEYS.request */
1624         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
1625         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
1626         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
1627         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
1628 #ifdef CONFIG_IEEE80211W
1629         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
1630         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
1631 #endif /* CONFIG_IEEE80211W */
1632
1633         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
1634                         0);
1635         /* MLME-SETPROTECTION.request(None) */
1636         wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
1637                                    MLME_SETPROTECTION_PROTECT_TYPE_NONE,
1638                                    MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1639         wpa_sm_drop_sa(wpa_s->wpa);
1640 }
1641
1642
1643 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
1644                                          char *buf, size_t *resp_len)
1645 {
1646         char *reply;
1647         const int reply_size = 2048;
1648         int ctrl_rsp = 0;
1649         int reply_len;
1650
1651         if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
1652             os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
1653                 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
1654                                       (const u8 *) buf, os_strlen(buf));
1655         } else {
1656                 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface",
1657                                   (const u8 *) buf, os_strlen(buf));
1658         }
1659
1660         reply = os_malloc(reply_size);
1661         if (reply == NULL) {
1662                 *resp_len = 1;
1663                 return NULL;
1664         }
1665
1666         os_memcpy(reply, "OK\n", 3);
1667         reply_len = 3;
1668
1669         if (os_strcmp(buf, "PING") == 0) {
1670                 os_memcpy(reply, "PONG\n", 5);
1671                 reply_len = 5;
1672         } else if (os_strcmp(buf, "MIB") == 0) {
1673                 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
1674                 if (reply_len >= 0) {
1675                         int res;
1676                         res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
1677                                                reply_size - reply_len);
1678                         if (res < 0)
1679                                 reply_len = -1;
1680                         else
1681                                 reply_len += res;
1682                 }
1683         } else if (os_strncmp(buf, "STATUS", 6) == 0) {
1684                 reply_len = wpa_supplicant_ctrl_iface_status(
1685                         wpa_s, buf + 6, reply, reply_size);
1686         } else if (os_strcmp(buf, "PMKSA") == 0) {
1687                 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
1688                                                     reply_size);
1689         } else if (os_strncmp(buf, "SET ", 4) == 0) {
1690                 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
1691                         reply_len = -1;
1692         } else if (os_strcmp(buf, "LOGON") == 0) {
1693                 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
1694         } else if (os_strcmp(buf, "LOGOFF") == 0) {
1695                 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
1696         } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
1697                 wpa_s->disconnected = 0;
1698                 wpa_s->reassociate = 1;
1699                 wpa_supplicant_req_scan(wpa_s, 0, 0);
1700         } else if (os_strcmp(buf, "RECONNECT") == 0) {
1701                 if (wpa_s->disconnected) {
1702                         wpa_s->disconnected = 0;
1703                         wpa_s->reassociate = 1;
1704                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1705                 }
1706 #ifdef IEEE8021X_EAPOL
1707         } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
1708                 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
1709                         reply_len = -1;
1710 #endif /* IEEE8021X_EAPOL */
1711 #ifdef CONFIG_PEERKEY
1712         } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
1713                 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
1714                         reply_len = -1;
1715 #endif /* CONFIG_PEERKEY */
1716 #ifdef CONFIG_IEEE80211R
1717         } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
1718                 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
1719                         reply_len = -1;
1720 #endif /* CONFIG_IEEE80211R */
1721 #ifdef CONFIG_WPS
1722         } else if (os_strcmp(buf, "WPS_PBC") == 0) {
1723                 if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL))
1724                         reply_len = -1;
1725         } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
1726                 if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8))
1727                         reply_len = -1;
1728         } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
1729                 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
1730                                                               reply,
1731                                                               reply_size);
1732 #ifdef CONFIG_WPS_OOB
1733         } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
1734                 if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
1735                         reply_len = -1;
1736 #endif /* CONFIG_WPS_OOB */
1737         } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
1738                 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
1739                         reply_len = -1;
1740 #ifdef CONFIG_WPS_ER
1741         } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
1742                 if (wpas_wps_er_start(wpa_s))
1743                         reply_len = -1;
1744         } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
1745                 if (wpas_wps_er_stop(wpa_s))
1746                         reply_len = -1;
1747         } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
1748                 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
1749                         reply_len = -1;
1750         } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
1751                 if (wpas_wps_er_pbc(wpa_s, buf + 11))
1752                         reply_len = -1;
1753         } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
1754                 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
1755                         reply_len = -1;
1756 #endif /* CONFIG_WPS_ER */
1757 #endif /* CONFIG_WPS */
1758 #ifdef CONFIG_IBSS_RSN
1759         } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
1760                 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
1761                         reply_len = -1;
1762 #endif /* CONFIG_IBSS_RSN */
1763         } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
1764         {
1765                 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
1766                             wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
1767                         reply_len = -1;
1768                 else
1769                         ctrl_rsp = 1;
1770         } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
1771                 if (wpa_supplicant_reload_configuration(wpa_s))
1772                         reply_len = -1;
1773         } else if (os_strcmp(buf, "TERMINATE") == 0) {
1774                 wpa_supplicant_terminate_proc(wpa_s->global);
1775         } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
1776                 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
1777                         reply_len = -1;
1778         } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
1779                 reply_len = wpa_supplicant_ctrl_iface_list_networks(
1780                         wpa_s, reply, reply_size);
1781         } else if (os_strcmp(buf, "DISCONNECT") == 0) {
1782                 wpa_s->reassociate = 0;
1783                 wpa_s->disconnected = 1;
1784                 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1785         } else if (os_strcmp(buf, "SCAN") == 0) {
1786                 wpa_s->scan_req = 2;
1787                 wpa_supplicant_req_scan(wpa_s, 0, 0);
1788         } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
1789                 reply_len = wpa_supplicant_ctrl_iface_scan_results(
1790                         wpa_s, reply, reply_size);
1791         } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
1792                 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
1793                         reply_len = -1;
1794         } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
1795                 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
1796                         reply_len = -1;
1797         } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
1798                 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
1799                         reply_len = -1;
1800         } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
1801                 reply_len = wpa_supplicant_ctrl_iface_add_network(
1802                         wpa_s, reply, reply_size);
1803         } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
1804                 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
1805                         reply_len = -1;
1806         } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
1807                 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
1808                         reply_len = -1;
1809         } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
1810                 reply_len = wpa_supplicant_ctrl_iface_get_network(
1811                         wpa_s, buf + 12, reply, reply_size);
1812 #ifndef CONFIG_NO_CONFIG_WRITE
1813         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
1814                 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
1815                         reply_len = -1;
1816 #endif /* CONFIG_NO_CONFIG_WRITE */
1817         } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
1818                 reply_len = wpa_supplicant_ctrl_iface_get_capability(
1819                         wpa_s, buf + 15, reply, reply_size);
1820         } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
1821                 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
1822                         reply_len = -1;
1823         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
1824                 reply_len = wpa_supplicant_global_iface_list(
1825                         wpa_s->global, reply, reply_size);
1826         } else if (os_strcmp(buf, "INTERFACES") == 0) {
1827                 reply_len = wpa_supplicant_global_iface_interfaces(
1828                         wpa_s->global, reply, reply_size);
1829         } else if (os_strncmp(buf, "BSS ", 4) == 0) {
1830                 reply_len = wpa_supplicant_ctrl_iface_bss(
1831                         wpa_s, buf + 4, reply, reply_size);
1832 #ifdef CONFIG_AP
1833         } else if (os_strcmp(buf, "STA-FIRST") == 0) {
1834                 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
1835         } else if (os_strncmp(buf, "STA ", 4) == 0) {
1836                 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
1837                                               reply_size);
1838         } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
1839                 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
1840                                                    reply_size);
1841 #endif /* CONFIG_AP */
1842         } else if (os_strcmp(buf, "SUSPEND") == 0) {
1843                 wpas_notify_suspend(wpa_s->global);
1844         } else if (os_strcmp(buf, "RESUME") == 0) {
1845                 wpas_notify_resume(wpa_s->global);
1846         } else if (os_strcmp(buf, "DROP_SA") == 0) {
1847                 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
1848         } else {
1849                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
1850                 reply_len = 16;
1851         }
1852
1853         if (reply_len < 0) {
1854                 os_memcpy(reply, "FAIL\n", 5);
1855                 reply_len = 5;
1856         }
1857
1858         if (ctrl_rsp)
1859                 eapol_sm_notify_ctrl_response(wpa_s->eapol);
1860
1861         *resp_len = reply_len;
1862         return reply;
1863 }
1864
1865
1866 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
1867                                            char *cmd)
1868 {
1869         struct wpa_interface iface;
1870         char *pos;
1871
1872         /*
1873          * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
1874          * TAB<bridge_ifname>
1875          */
1876         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
1877
1878         os_memset(&iface, 0, sizeof(iface));
1879
1880         do {
1881                 iface.ifname = pos = cmd;
1882                 pos = os_strchr(pos, '\t');
1883                 if (pos)
1884                         *pos++ = '\0';
1885                 if (iface.ifname[0] == '\0')
1886                         return -1;
1887                 if (pos == NULL)
1888                         break;
1889
1890                 iface.confname = pos;
1891                 pos = os_strchr(pos, '\t');
1892                 if (pos)
1893                         *pos++ = '\0';
1894                 if (iface.confname[0] == '\0')
1895                         iface.confname = NULL;
1896                 if (pos == NULL)
1897                         break;
1898
1899                 iface.driver = pos;
1900                 pos = os_strchr(pos, '\t');
1901                 if (pos)
1902                         *pos++ = '\0';
1903                 if (iface.driver[0] == '\0')
1904                         iface.driver = NULL;
1905                 if (pos == NULL)
1906                         break;
1907
1908                 iface.ctrl_interface = pos;
1909                 pos = os_strchr(pos, '\t');
1910                 if (pos)
1911                         *pos++ = '\0';
1912                 if (iface.ctrl_interface[0] == '\0')
1913                         iface.ctrl_interface = NULL;
1914                 if (pos == NULL)
1915                         break;
1916
1917                 iface.driver_param = pos;
1918                 pos = os_strchr(pos, '\t');
1919                 if (pos)
1920                         *pos++ = '\0';
1921                 if (iface.driver_param[0] == '\0')
1922                         iface.driver_param = NULL;
1923                 if (pos == NULL)
1924                         break;
1925
1926                 iface.bridge_ifname = pos;
1927                 pos = os_strchr(pos, '\t');
1928                 if (pos)
1929                         *pos++ = '\0';
1930                 if (iface.bridge_ifname[0] == '\0')
1931                         iface.bridge_ifname = NULL;
1932                 if (pos == NULL)
1933                         break;
1934         } while (0);
1935
1936         if (wpa_supplicant_get_iface(global, iface.ifname))
1937                 return -1;
1938
1939         return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
1940 }
1941
1942
1943 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
1944                                               char *cmd)
1945 {
1946         struct wpa_supplicant *wpa_s;
1947
1948         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
1949
1950         wpa_s = wpa_supplicant_get_iface(global, cmd);
1951         if (wpa_s == NULL)
1952                 return -1;
1953         return wpa_supplicant_remove_iface(global, wpa_s);
1954 }
1955
1956
1957 static void wpa_free_iface_info(struct wpa_interface_info *iface)
1958 {
1959         struct wpa_interface_info *prev;
1960
1961         while (iface) {
1962                 prev = iface;
1963                 iface = iface->next;
1964
1965                 os_free(prev->ifname);
1966                 os_free(prev->desc);
1967                 os_free(prev);
1968         }
1969 }
1970
1971
1972 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
1973                                             char *buf, int len)
1974 {
1975         int i, res;
1976         struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
1977         char *pos, *end;
1978
1979         for (i = 0; wpa_drivers[i]; i++) {
1980                 struct wpa_driver_ops *drv = wpa_drivers[i];
1981                 if (drv->get_interfaces == NULL)
1982                         continue;
1983                 tmp = drv->get_interfaces(global->drv_priv);
1984                 if (tmp == NULL)
1985                         continue;
1986
1987                 if (last == NULL)
1988                         iface = last = tmp;
1989                 else
1990                         last->next = tmp;
1991                 while (last->next)
1992                         last = last->next;
1993         }
1994
1995         pos = buf;
1996         end = buf + len;
1997         for (tmp = iface; tmp; tmp = tmp->next) {
1998                 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
1999                                   tmp->drv_name, tmp->ifname,
2000                                   tmp->desc ? tmp->desc : "");
2001                 if (res < 0 || res >= end - pos) {
2002                         *pos = '\0';
2003                         break;
2004                 }
2005                 pos += res;
2006         }
2007
2008         wpa_free_iface_info(iface);
2009
2010         return pos - buf;
2011 }
2012
2013
2014 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
2015                                                   char *buf, int len)
2016 {
2017         int res;
2018         char *pos, *end;
2019         struct wpa_supplicant *wpa_s;
2020
2021         wpa_s = global->ifaces;
2022         pos = buf;
2023         end = buf + len;
2024
2025         while (wpa_s) {
2026                 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
2027                 if (res < 0 || res >= end - pos) {
2028                         *pos = '\0';
2029                         break;
2030                 }
2031                 pos += res;
2032                 wpa_s = wpa_s->next;
2033         }
2034         return pos - buf;
2035 }
2036
2037
2038 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
2039                                                 char *buf, size_t *resp_len)
2040 {
2041         char *reply;
2042         const int reply_size = 2048;
2043         int reply_len;
2044
2045         wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
2046                           (const u8 *) buf, os_strlen(buf));
2047
2048         reply = os_malloc(reply_size);
2049         if (reply == NULL) {
2050                 *resp_len = 1;
2051                 return NULL;
2052         }
2053
2054         os_memcpy(reply, "OK\n", 3);
2055         reply_len = 3;
2056
2057         if (os_strcmp(buf, "PING") == 0) {
2058                 os_memcpy(reply, "PONG\n", 5);
2059                 reply_len = 5;
2060         } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
2061                 if (wpa_supplicant_global_iface_add(global, buf + 14))
2062                         reply_len = -1;
2063         } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
2064                 if (wpa_supplicant_global_iface_remove(global, buf + 17))
2065                         reply_len = -1;
2066         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
2067                 reply_len = wpa_supplicant_global_iface_list(
2068                         global, reply, reply_size);
2069         } else if (os_strcmp(buf, "INTERFACES") == 0) {
2070                 reply_len = wpa_supplicant_global_iface_interfaces(
2071                         global, reply, reply_size);
2072         } else if (os_strcmp(buf, "TERMINATE") == 0) {
2073                 wpa_supplicant_terminate_proc(global);
2074         } else if (os_strcmp(buf, "SUSPEND") == 0) {
2075                 wpas_notify_suspend(global);
2076         } else if (os_strcmp(buf, "RESUME") == 0) {
2077                 wpas_notify_resume(global);
2078         } else {
2079                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2080                 reply_len = 16;
2081         }
2082
2083         if (reply_len < 0) {
2084                 os_memcpy(reply, "FAIL\n", 5);
2085                 reply_len = 5;
2086         }
2087
2088         *resp_len = reply_len;
2089         return reply;
2090 }