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