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