OSDN Git Service

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