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