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