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