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