blob: bafe1e9a1a22ae5a829f6010e70e7b26740b83f7 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Control interface (shared code for all backends)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003 * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/version.h"
14#include "common/ieee802_11_defs.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070015#include "common/ieee802_11_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016#include "common/wpa_ctrl.h"
17#include "eap_peer/eap.h"
18#include "eapol_supp/eapol_supp_sm.h"
19#include "rsn_supp/wpa.h"
20#include "rsn_supp/preauth.h"
21#include "rsn_supp/pmksa_cache.h"
22#include "l2_packet/l2_packet.h"
23#include "wps/wps.h"
24#include "config.h"
25#include "wpa_supplicant_i.h"
26#include "driver_i.h"
27#include "wps_supplicant.h"
28#include "ibss_rsn.h"
29#include "ap.h"
30#include "p2p_supplicant.h"
31#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070032#include "hs20_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070033#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "notify.h"
35#include "bss.h"
36#include "scan.h"
37#include "ctrl_iface.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080038#include "interworking.h"
Dmitry Shmidte19501d2011-03-16 14:32:18 -070039#include "blacklist.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070040#include "autoscan.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080041#include "wnm_sta.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042
43extern struct wpa_driver_ops *wpa_drivers[];
44
45static int wpa_supplicant_global_iface_list(struct wpa_global *global,
46 char *buf, int len);
47static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
48 char *buf, int len);
49
50
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080051static int pno_start(struct wpa_supplicant *wpa_s)
52{
53 int ret;
54 size_t i, num_ssid;
55 struct wpa_ssid *ssid;
56 struct wpa_driver_scan_params params;
57
58 if (wpa_s->pno)
59 return 0;
60
Dmitry Shmidtf708dcd2013-11-05 18:08:27 -080061 if ((wpa_s->wpa_state > WPA_SCANNING) &&
62 (wpa_s->wpa_state <= WPA_COMPLETED)) {
63 wpa_printf(MSG_ERROR, "PNO: In assoc process");
64 return -EAGAIN;
65 }
66
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080067 if (wpa_s->wpa_state == WPA_SCANNING) {
68 wpa_supplicant_cancel_sched_scan(wpa_s);
69 wpa_supplicant_cancel_scan(wpa_s);
70 }
71
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080072 os_memset(&params, 0, sizeof(params));
73
74 num_ssid = 0;
75 ssid = wpa_s->conf->ssid;
76 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070077 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080078 num_ssid++;
79 ssid = ssid->next;
80 }
81 if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
82 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
83 "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
84 num_ssid = WPAS_MAX_SCAN_SSIDS;
85 }
86
87 if (num_ssid == 0) {
88 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
89 return -1;
90 }
91
92 params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
93 num_ssid);
94 if (params.filter_ssids == NULL)
95 return -1;
96 i = 0;
97 ssid = wpa_s->conf->ssid;
98 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070099 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800100 params.ssids[i].ssid = ssid->ssid;
101 params.ssids[i].ssid_len = ssid->ssid_len;
102 params.num_ssids++;
103 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
104 ssid->ssid_len);
105 params.filter_ssids[i].ssid_len = ssid->ssid_len;
106 params.num_filter_ssids++;
107 i++;
108 if (i == num_ssid)
109 break;
110 }
111 ssid = ssid->next;
112 }
113
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700114 if (wpa_s->conf->filter_rssi)
115 params.filter_rssi = wpa_s->conf->filter_rssi;
116
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800117 ret = wpa_drv_sched_scan(wpa_s, &params, 10 * 1000);
118 os_free(params.filter_ssids);
119 if (ret == 0)
120 wpa_s->pno = 1;
121 return ret;
122}
123
124
125static int pno_stop(struct wpa_supplicant *wpa_s)
126{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800127 int ret = 0;
128
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800129 if (wpa_s->pno) {
130 wpa_s->pno = 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800131 ret = wpa_drv_stop_sched_scan(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800132 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800133
134 if (wpa_s->wpa_state == WPA_SCANNING)
135 wpa_supplicant_req_scan(wpa_s, 0, 0);
136
137 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800138}
139
140
Dmitry Shmidt04949592012-07-19 12:16:46 -0700141static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
142{
143 char *pos;
144 u8 addr[ETH_ALEN], *filter = NULL, *n;
145 size_t count = 0;
146
147 pos = val;
148 while (pos) {
149 if (*pos == '\0')
150 break;
151 if (hwaddr_aton(pos, addr)) {
152 os_free(filter);
153 return -1;
154 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700155 n = os_realloc_array(filter, count + 1, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700156 if (n == NULL) {
157 os_free(filter);
158 return -1;
159 }
160 filter = n;
161 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
162 count++;
163
164 pos = os_strchr(pos, ' ');
165 if (pos)
166 pos++;
167 }
168
169 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
170 os_free(wpa_s->bssid_filter);
171 wpa_s->bssid_filter = filter;
172 wpa_s->bssid_filter_count = count;
173
174 return 0;
175}
176
177
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800178static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
179{
180 char *pos;
181 u8 addr[ETH_ALEN], *bssid = NULL, *n;
182 struct wpa_ssid_value *ssid = NULL, *ns;
183 size_t count = 0, ssid_count = 0;
184 struct wpa_ssid *c;
185
186 /*
187 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | “”
188 * SSID_SPEC ::= ssid <SSID_HEX>
189 * BSSID_SPEC ::= bssid <BSSID_HEX>
190 */
191
192 pos = val;
193 while (pos) {
194 if (*pos == '\0')
195 break;
196 if (os_strncmp(pos, "bssid ", 6) == 0) {
197 int res;
198 pos += 6;
199 res = hwaddr_aton2(pos, addr);
200 if (res < 0) {
201 os_free(ssid);
202 os_free(bssid);
203 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
204 "BSSID value '%s'", pos);
205 return -1;
206 }
207 pos += res;
208 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
209 if (n == NULL) {
210 os_free(ssid);
211 os_free(bssid);
212 return -1;
213 }
214 bssid = n;
215 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
216 count++;
217 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
218 char *end;
219 pos += 5;
220
221 end = pos;
222 while (*end) {
223 if (*end == '\0' || *end == ' ')
224 break;
225 end++;
226 }
227
228 ns = os_realloc_array(ssid, ssid_count + 1,
229 sizeof(struct wpa_ssid_value));
230 if (ns == NULL) {
231 os_free(ssid);
232 os_free(bssid);
233 return -1;
234 }
235 ssid = ns;
236
237 if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
238 hexstr2bin(pos, ssid[ssid_count].ssid,
239 (end - pos) / 2) < 0) {
240 os_free(ssid);
241 os_free(bssid);
242 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
243 "SSID value '%s'", pos);
244 return -1;
245 }
246 ssid[ssid_count].ssid_len = (end - pos) / 2;
247 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
248 ssid[ssid_count].ssid,
249 ssid[ssid_count].ssid_len);
250 ssid_count++;
251 pos = end;
252 } else {
253 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
254 "'%s'", pos);
255 os_free(ssid);
256 os_free(bssid);
257 return -1;
258 }
259
260 pos = os_strchr(pos, ' ');
261 if (pos)
262 pos++;
263 }
264
265 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
266 os_free(wpa_s->disallow_aps_bssid);
267 wpa_s->disallow_aps_bssid = bssid;
268 wpa_s->disallow_aps_bssid_count = count;
269
270 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
271 os_free(wpa_s->disallow_aps_ssid);
272 wpa_s->disallow_aps_ssid = ssid;
273 wpa_s->disallow_aps_ssid_count = ssid_count;
274
275 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
276 return 0;
277
278 c = wpa_s->current_ssid;
279 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
280 return 0;
281
282 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
283 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
284 return 0;
285
286 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
287 "because current AP was marked disallowed");
288
289#ifdef CONFIG_SME
290 wpa_s->sme.prev_bssid_set = 0;
291#endif /* CONFIG_SME */
292 wpa_s->reassociate = 1;
293 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
294 wpa_supplicant_req_scan(wpa_s, 0, 0);
295
296 return 0;
297}
298
299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
301 char *cmd)
302{
303 char *value;
304 int ret = 0;
305
306 value = os_strchr(cmd, ' ');
307 if (value == NULL)
308 return -1;
309 *value++ = '\0';
310
311 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
312 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
313 eapol_sm_configure(wpa_s->eapol,
314 atoi(value), -1, -1, -1);
315 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
316 eapol_sm_configure(wpa_s->eapol,
317 -1, atoi(value), -1, -1);
318 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
319 eapol_sm_configure(wpa_s->eapol,
320 -1, -1, atoi(value), -1);
321 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
322 eapol_sm_configure(wpa_s->eapol,
323 -1, -1, -1, atoi(value));
324 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
325 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
326 atoi(value)))
327 ret = -1;
328 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
329 0) {
330 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
331 atoi(value)))
332 ret = -1;
333 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
334 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
335 ret = -1;
336 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
337 wpa_s->wps_fragment_size = atoi(value);
338#ifdef CONFIG_WPS_TESTING
339 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
340 long int val;
341 val = strtol(value, NULL, 0);
342 if (val < 0 || val > 0xff) {
343 ret = -1;
344 wpa_printf(MSG_DEBUG, "WPS: Invalid "
345 "wps_version_number %ld", val);
346 } else {
347 wps_version_number = val;
348 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
349 "version %u.%u",
350 (wps_version_number & 0xf0) >> 4,
351 wps_version_number & 0x0f);
352 }
353 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
354 wps_testing_dummy_cred = atoi(value);
355 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
356 wps_testing_dummy_cred);
357#endif /* CONFIG_WPS_TESTING */
358 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
359 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
360 ret = -1;
361#ifdef CONFIG_TDLS_TESTING
362 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
363 extern unsigned int tdls_testing;
364 tdls_testing = strtol(value, NULL, 0);
365 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
366#endif /* CONFIG_TDLS_TESTING */
367#ifdef CONFIG_TDLS
368 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
369 int disabled = atoi(value);
370 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
371 if (disabled) {
372 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
373 ret = -1;
374 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
375 ret = -1;
376 wpa_tdls_enable(wpa_s->wpa, !disabled);
377#endif /* CONFIG_TDLS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800378 } else if (os_strcasecmp(cmd, "pno") == 0) {
379 if (atoi(value))
380 ret = pno_start(wpa_s);
381 else
382 ret = pno_stop(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700383 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
384 int disabled = atoi(value);
385 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
386 ret = -1;
387 else if (disabled)
388 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
389 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
390 if (os_strcmp(value, "disable") == 0)
391 wpa_s->set_sta_uapsd = 0;
392 else {
393 int be, bk, vi, vo;
394 char *pos;
395 /* format: BE,BK,VI,VO;max SP Length */
396 be = atoi(value);
397 pos = os_strchr(value, ',');
398 if (pos == NULL)
399 return -1;
400 pos++;
401 bk = atoi(pos);
402 pos = os_strchr(pos, ',');
403 if (pos == NULL)
404 return -1;
405 pos++;
406 vi = atoi(pos);
407 pos = os_strchr(pos, ',');
408 if (pos == NULL)
409 return -1;
410 pos++;
411 vo = atoi(pos);
412 /* ignore max SP Length for now */
413
414 wpa_s->set_sta_uapsd = 1;
415 wpa_s->sta_uapsd = 0;
416 if (be)
417 wpa_s->sta_uapsd |= BIT(0);
418 if (bk)
419 wpa_s->sta_uapsd |= BIT(1);
420 if (vi)
421 wpa_s->sta_uapsd |= BIT(2);
422 if (vo)
423 wpa_s->sta_uapsd |= BIT(3);
424 }
Jouni Malinen21d6bc82012-04-10 16:17:59 -0700425 } else if (os_strcasecmp(cmd, "ps") == 0) {
426 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700427#ifdef CONFIG_WIFI_DISPLAY
428 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
429 wifi_display_enable(wpa_s->global, !!atoi(value));
430#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700431 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
432 ret = set_bssid_filter(wpa_s, value);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800433 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
434 ret = set_disallow_aps(wpa_s, value);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800435 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
436 wpa_s->no_keep_alive = !!atoi(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437 } else {
438 value[-1] = '=';
439 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
440 if (ret == 0)
441 wpa_supplicant_update_config(wpa_s);
442 }
443
444 return ret;
445}
446
447
448static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
449 char *cmd, char *buf, size_t buflen)
450{
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700451 int res = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452
453 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
454
455 if (os_strcmp(cmd, "version") == 0) {
456 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700457 } else if (os_strcasecmp(cmd, "country") == 0) {
458 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
459 res = os_snprintf(buf, buflen, "%c%c",
460 wpa_s->conf->country[0],
461 wpa_s->conf->country[1]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700462#ifdef CONFIG_WIFI_DISPLAY
463 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
464 res = os_snprintf(buf, buflen, "%d",
465 wpa_s->global->wifi_display);
466 if (res < 0 || (unsigned int) res >= buflen)
467 return -1;
468 return res;
469#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700470#ifdef CONFIG_TESTING_GET_GTK
471 } else if (os_strcmp(cmd, "gtk") == 0) {
472 if (wpa_s->last_gtk_len == 0)
473 return -1;
474 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
475 wpa_s->last_gtk_len);
476 return res;
477#endif /* CONFIG_TESTING_GET_GTK */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478 }
479
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700480 if (res < 0 || (unsigned int) res >= buflen)
481 return -1;
482 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483}
484
485
486#ifdef IEEE8021X_EAPOL
487static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
488 char *addr)
489{
490 u8 bssid[ETH_ALEN];
491 struct wpa_ssid *ssid = wpa_s->current_ssid;
492
493 if (hwaddr_aton(addr, bssid)) {
494 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
495 "'%s'", addr);
496 return -1;
497 }
498
499 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
500 rsn_preauth_deinit(wpa_s->wpa);
501 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
502 return -1;
503
504 return 0;
505}
506#endif /* IEEE8021X_EAPOL */
507
508
509#ifdef CONFIG_PEERKEY
510/* MLME-STKSTART.request(peer) */
511static int wpa_supplicant_ctrl_iface_stkstart(
512 struct wpa_supplicant *wpa_s, char *addr)
513{
514 u8 peer[ETH_ALEN];
515
516 if (hwaddr_aton(addr, peer)) {
517 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
518 "address '%s'", addr);
519 return -1;
520 }
521
522 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
523 MAC2STR(peer));
524
525 return wpa_sm_stkstart(wpa_s->wpa, peer);
526}
527#endif /* CONFIG_PEERKEY */
528
529
530#ifdef CONFIG_TDLS
531
532static int wpa_supplicant_ctrl_iface_tdls_discover(
533 struct wpa_supplicant *wpa_s, char *addr)
534{
535 u8 peer[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800536 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537
538 if (hwaddr_aton(addr, peer)) {
539 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
540 "address '%s'", addr);
541 return -1;
542 }
543
544 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
545 MAC2STR(peer));
546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800547 if (wpa_tdls_is_external_setup(wpa_s->wpa))
548 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
549 else
550 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
551
552 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553}
554
555
556static int wpa_supplicant_ctrl_iface_tdls_setup(
557 struct wpa_supplicant *wpa_s, char *addr)
558{
559 u8 peer[ETH_ALEN];
560 int ret;
561
562 if (hwaddr_aton(addr, peer)) {
563 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
564 "address '%s'", addr);
565 return -1;
566 }
567
568 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
569 MAC2STR(peer));
570
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800571 wpa_tdls_remove(wpa_s->wpa, peer);
572
573 if (wpa_tdls_is_external_setup(wpa_s->wpa))
574 ret = wpa_tdls_start(wpa_s->wpa, peer);
575 else
576 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700578 return ret;
579}
580
581
582static int wpa_supplicant_ctrl_iface_tdls_teardown(
583 struct wpa_supplicant *wpa_s, char *addr)
584{
585 u8 peer[ETH_ALEN];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700586 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587
588 if (hwaddr_aton(addr, peer)) {
589 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
590 "address '%s'", addr);
591 return -1;
592 }
593
594 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
595 MAC2STR(peer));
596
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700597 if (wpa_tdls_is_external_setup(wpa_s->wpa))
598 ret = wpa_tdls_teardown_link(
599 wpa_s->wpa, peer,
600 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
601 else
602 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
603
604 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605}
606
607#endif /* CONFIG_TDLS */
608
609
610#ifdef CONFIG_IEEE80211R
611static int wpa_supplicant_ctrl_iface_ft_ds(
612 struct wpa_supplicant *wpa_s, char *addr)
613{
614 u8 target_ap[ETH_ALEN];
615 struct wpa_bss *bss;
616 const u8 *mdie;
617
618 if (hwaddr_aton(addr, target_ap)) {
619 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
620 "address '%s'", addr);
621 return -1;
622 }
623
624 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
625
626 bss = wpa_bss_get_bssid(wpa_s, target_ap);
627 if (bss)
628 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
629 else
630 mdie = NULL;
631
632 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
633}
634#endif /* CONFIG_IEEE80211R */
635
636
637#ifdef CONFIG_WPS
638static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
639 char *cmd)
640{
641 u8 bssid[ETH_ALEN], *_bssid = bssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700642#ifdef CONFIG_P2P
643 u8 p2p_dev_addr[ETH_ALEN];
644#endif /* CONFIG_P2P */
645#ifdef CONFIG_AP
646 u8 *_p2p_dev_addr = NULL;
647#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800648
Irfan Sheriffa7534b92012-09-06 18:30:39 -0700649 if (cmd == NULL || os_strcmp(cmd, "any") == 0 || cmd[0] == '\0') {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 _bssid = NULL;
651#ifdef CONFIG_P2P
652 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
653 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
654 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
655 "P2P Device Address '%s'",
656 cmd + 13);
657 return -1;
658 }
659 _p2p_dev_addr = p2p_dev_addr;
660#endif /* CONFIG_P2P */
661 } else if (hwaddr_aton(cmd, bssid)) {
662 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
663 cmd);
664 return -1;
665 }
666
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800667#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700668 if (wpa_s->ap_iface)
669 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
670#endif /* CONFIG_AP */
671
672 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
673}
674
675
676static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
677 char *cmd, char *buf,
678 size_t buflen)
679{
680 u8 bssid[ETH_ALEN], *_bssid = bssid;
681 char *pin;
682 int ret;
683
684 pin = os_strchr(cmd, ' ');
685 if (pin)
686 *pin++ = '\0';
687
688 if (os_strcmp(cmd, "any") == 0)
689 _bssid = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800690 else if (os_strcmp(cmd, "get") == 0) {
691 ret = wps_generate_pin();
692 goto done;
693 } else if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700694 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
695 cmd);
696 return -1;
697 }
698
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800699#ifdef CONFIG_AP
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800700 if (wpa_s->ap_iface) {
701 int timeout = 0;
702 char *pos;
703
704 if (pin) {
705 pos = os_strchr(pin, ' ');
706 if (pos) {
707 *pos++ = '\0';
708 timeout = atoi(pos);
709 }
710 }
711
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700712 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800713 buf, buflen, timeout);
714 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715#endif /* CONFIG_AP */
716
717 if (pin) {
718 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
719 DEV_PW_DEFAULT);
720 if (ret < 0)
721 return -1;
722 ret = os_snprintf(buf, buflen, "%s", pin);
723 if (ret < 0 || (size_t) ret >= buflen)
724 return -1;
725 return ret;
726 }
727
728 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
729 if (ret < 0)
730 return -1;
731
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800732done:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 /* Return the generated PIN */
734 ret = os_snprintf(buf, buflen, "%08d", ret);
735 if (ret < 0 || (size_t) ret >= buflen)
736 return -1;
737 return ret;
738}
739
740
741static int wpa_supplicant_ctrl_iface_wps_check_pin(
742 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
743{
744 char pin[9];
745 size_t len;
746 char *pos;
747 int ret;
748
749 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
750 (u8 *) cmd, os_strlen(cmd));
751 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
752 if (*pos < '0' || *pos > '9')
753 continue;
754 pin[len++] = *pos;
755 if (len == 9) {
756 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
757 return -1;
758 }
759 }
760 if (len != 4 && len != 8) {
761 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
762 return -1;
763 }
764 pin[len] = '\0';
765
766 if (len == 8) {
767 unsigned int pin_val;
768 pin_val = atoi(pin);
769 if (!wps_pin_valid(pin_val)) {
770 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
771 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
772 if (ret < 0 || (size_t) ret >= buflen)
773 return -1;
774 return ret;
775 }
776 }
777
778 ret = os_snprintf(buf, buflen, "%s", pin);
779 if (ret < 0 || (size_t) ret >= buflen)
780 return -1;
781
782 return ret;
783}
784
785
Dmitry Shmidt04949592012-07-19 12:16:46 -0700786#ifdef CONFIG_WPS_NFC
787
788static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
789 char *cmd)
790{
791 u8 bssid[ETH_ALEN], *_bssid = bssid;
792
793 if (cmd == NULL || cmd[0] == '\0')
794 _bssid = NULL;
795 else if (hwaddr_aton(cmd, bssid))
796 return -1;
797
798 return wpas_wps_start_nfc(wpa_s, _bssid);
799}
800
801
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800802static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
803 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
804{
805 int ndef;
806 struct wpabuf *buf;
807 int res;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700808 char *pos;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800809
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700810 pos = os_strchr(cmd, ' ');
811 if (pos)
812 *pos++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800813 if (os_strcmp(cmd, "WPS") == 0)
814 ndef = 0;
815 else if (os_strcmp(cmd, "NDEF") == 0)
816 ndef = 1;
817 else
818 return -1;
819
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700820 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800821 if (buf == NULL)
822 return -1;
823
824 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
825 wpabuf_len(buf));
826 reply[res++] = '\n';
827 reply[res] = '\0';
828
829 wpabuf_free(buf);
830
831 return res;
832}
833
834
Dmitry Shmidt04949592012-07-19 12:16:46 -0700835static int wpa_supplicant_ctrl_iface_wps_nfc_token(
836 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
837{
838 int ndef;
839 struct wpabuf *buf;
840 int res;
841
842 if (os_strcmp(cmd, "WPS") == 0)
843 ndef = 0;
844 else if (os_strcmp(cmd, "NDEF") == 0)
845 ndef = 1;
846 else
847 return -1;
848
849 buf = wpas_wps_nfc_token(wpa_s, ndef);
850 if (buf == NULL)
851 return -1;
852
853 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
854 wpabuf_len(buf));
855 reply[res++] = '\n';
856 reply[res] = '\0';
857
858 wpabuf_free(buf);
859
860 return res;
861}
862
863
864static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
865 struct wpa_supplicant *wpa_s, char *pos)
866{
867 size_t len;
868 struct wpabuf *buf;
869 int ret;
870
871 len = os_strlen(pos);
872 if (len & 0x01)
873 return -1;
874 len /= 2;
875
876 buf = wpabuf_alloc(len);
877 if (buf == NULL)
878 return -1;
879 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
880 wpabuf_free(buf);
881 return -1;
882 }
883
884 ret = wpas_wps_nfc_tag_read(wpa_s, buf);
885 wpabuf_free(buf);
886
887 return ret;
888}
889
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800890
891static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800892 char *reply, size_t max_len,
893 int cr)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800894{
895 struct wpabuf *buf;
896 int res;
897
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800898 buf = wpas_wps_nfc_handover_req(wpa_s, cr);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800899 if (buf == NULL)
900 return -1;
901
902 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
903 wpabuf_len(buf));
904 reply[res++] = '\n';
905 reply[res] = '\0';
906
907 wpabuf_free(buf);
908
909 return res;
910}
911
912
913static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
914 char *cmd, char *reply,
915 size_t max_len)
916{
917 char *pos;
918
919 pos = os_strchr(cmd, ' ');
920 if (pos == NULL)
921 return -1;
922 *pos++ = '\0';
923
924 if (os_strcmp(cmd, "NDEF") != 0)
925 return -1;
926
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800927 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
928 return wpas_ctrl_nfc_get_handover_req_wps(
929 wpa_s, reply, max_len, os_strcmp(pos, "WPS-CR") == 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800930 }
931
932 return -1;
933}
934
935
936static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800937 char *reply, size_t max_len,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800938 int ndef, int cr, char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800939{
940 struct wpabuf *buf;
941 int res;
942
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800943 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800944 if (buf == NULL)
945 return -1;
946
947 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
948 wpabuf_len(buf));
949 reply[res++] = '\n';
950 reply[res] = '\0';
951
952 wpabuf_free(buf);
953
954 return res;
955}
956
957
958static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
959 char *cmd, char *reply,
960 size_t max_len)
961{
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800962 char *pos, *pos2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800963 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800964
965 pos = os_strchr(cmd, ' ');
966 if (pos == NULL)
967 return -1;
968 *pos++ = '\0';
969
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800970 if (os_strcmp(cmd, "WPS") == 0)
971 ndef = 0;
972 else if (os_strcmp(cmd, "NDEF") == 0)
973 ndef = 1;
974 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800975 return -1;
976
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800977 pos2 = os_strchr(pos, ' ');
978 if (pos2)
979 *pos2++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800980 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
981 return wpas_ctrl_nfc_get_handover_sel_wps(
982 wpa_s, reply, max_len, ndef,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800983 os_strcmp(pos, "WPS-CR") == 0, pos2);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800984 }
985
986 return -1;
987}
988
989
990static int wpas_ctrl_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
991 char *cmd, char *reply,
992 size_t max_len)
993{
994 size_t len;
995 struct wpabuf *buf;
996 int ret;
997
998 len = os_strlen(cmd);
999 if (len & 0x01)
1000 return -1;
1001 len /= 2;
1002
1003 buf = wpabuf_alloc(len);
1004 if (buf == NULL)
1005 return -1;
1006 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
1007 wpabuf_free(buf);
1008 return -1;
1009 }
1010
1011 ret = wpas_wps_nfc_rx_handover_req(wpa_s, buf);
1012 wpabuf_free(buf);
1013
1014 return ret;
1015}
1016
1017
1018static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
1019 char *cmd)
1020{
1021 size_t len;
1022 struct wpabuf *buf;
1023 int ret;
1024
1025 len = os_strlen(cmd);
1026 if (len & 0x01)
1027 return -1;
1028 len /= 2;
1029
1030 buf = wpabuf_alloc(len);
1031 if (buf == NULL)
1032 return -1;
1033 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
1034 wpabuf_free(buf);
1035 return -1;
1036 }
1037
1038 ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
1039 wpabuf_free(buf);
1040
1041 return ret;
1042}
1043
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001044
1045static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1046 char *cmd)
1047{
1048 size_t len;
1049 struct wpabuf *req, *sel;
1050 int ret;
1051 char *pos, *role, *type, *pos2;
1052
1053 role = cmd;
1054 pos = os_strchr(role, ' ');
1055 if (pos == NULL)
1056 return -1;
1057 *pos++ = '\0';
1058
1059 type = pos;
1060 pos = os_strchr(type, ' ');
1061 if (pos == NULL)
1062 return -1;
1063 *pos++ = '\0';
1064
1065 pos2 = os_strchr(pos, ' ');
1066 if (pos2 == NULL)
1067 return -1;
1068 *pos2++ = '\0';
1069
1070 len = os_strlen(pos);
1071 if (len & 0x01)
1072 return -1;
1073 len /= 2;
1074
1075 req = wpabuf_alloc(len);
1076 if (req == NULL)
1077 return -1;
1078 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
1079 wpabuf_free(req);
1080 return -1;
1081 }
1082
1083 len = os_strlen(pos2);
1084 if (len & 0x01) {
1085 wpabuf_free(req);
1086 return -1;
1087 }
1088 len /= 2;
1089
1090 sel = wpabuf_alloc(len);
1091 if (sel == NULL) {
1092 wpabuf_free(req);
1093 return -1;
1094 }
1095 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
1096 wpabuf_free(req);
1097 wpabuf_free(sel);
1098 return -1;
1099 }
1100
1101 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1102 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
1103 } else {
1104 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1105 "reported: role=%s type=%s", role, type);
1106 ret = -1;
1107 }
1108 wpabuf_free(req);
1109 wpabuf_free(sel);
1110
1111 return ret;
1112}
1113
Dmitry Shmidt04949592012-07-19 12:16:46 -07001114#endif /* CONFIG_WPS_NFC */
1115
1116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1118 char *cmd)
1119{
1120 u8 bssid[ETH_ALEN];
1121 char *pin;
1122 char *new_ssid;
1123 char *new_auth;
1124 char *new_encr;
1125 char *new_key;
1126 struct wps_new_ap_settings ap;
1127
1128 pin = os_strchr(cmd, ' ');
1129 if (pin == NULL)
1130 return -1;
1131 *pin++ = '\0';
1132
1133 if (hwaddr_aton(cmd, bssid)) {
1134 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1135 cmd);
1136 return -1;
1137 }
1138
1139 new_ssid = os_strchr(pin, ' ');
1140 if (new_ssid == NULL)
1141 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1142 *new_ssid++ = '\0';
1143
1144 new_auth = os_strchr(new_ssid, ' ');
1145 if (new_auth == NULL)
1146 return -1;
1147 *new_auth++ = '\0';
1148
1149 new_encr = os_strchr(new_auth, ' ');
1150 if (new_encr == NULL)
1151 return -1;
1152 *new_encr++ = '\0';
1153
1154 new_key = os_strchr(new_encr, ' ');
1155 if (new_key == NULL)
1156 return -1;
1157 *new_key++ = '\0';
1158
1159 os_memset(&ap, 0, sizeof(ap));
1160 ap.ssid_hex = new_ssid;
1161 ap.auth = new_auth;
1162 ap.encr = new_encr;
1163 ap.key_hex = new_key;
1164 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1165}
1166
1167
1168#ifdef CONFIG_AP
1169static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1170 char *cmd, char *buf,
1171 size_t buflen)
1172{
1173 int timeout = 300;
1174 char *pos;
1175 const char *pin_txt;
1176
1177 if (!wpa_s->ap_iface)
1178 return -1;
1179
1180 pos = os_strchr(cmd, ' ');
1181 if (pos)
1182 *pos++ = '\0';
1183
1184 if (os_strcmp(cmd, "disable") == 0) {
1185 wpas_wps_ap_pin_disable(wpa_s);
1186 return os_snprintf(buf, buflen, "OK\n");
1187 }
1188
1189 if (os_strcmp(cmd, "random") == 0) {
1190 if (pos)
1191 timeout = atoi(pos);
1192 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1193 if (pin_txt == NULL)
1194 return -1;
1195 return os_snprintf(buf, buflen, "%s", pin_txt);
1196 }
1197
1198 if (os_strcmp(cmd, "get") == 0) {
1199 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1200 if (pin_txt == NULL)
1201 return -1;
1202 return os_snprintf(buf, buflen, "%s", pin_txt);
1203 }
1204
1205 if (os_strcmp(cmd, "set") == 0) {
1206 char *pin;
1207 if (pos == NULL)
1208 return -1;
1209 pin = pos;
1210 pos = os_strchr(pos, ' ');
1211 if (pos) {
1212 *pos++ = '\0';
1213 timeout = atoi(pos);
1214 }
1215 if (os_strlen(pin) > buflen)
1216 return -1;
1217 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1218 return -1;
1219 return os_snprintf(buf, buflen, "%s", pin);
1220 }
1221
1222 return -1;
1223}
1224#endif /* CONFIG_AP */
1225
1226
1227#ifdef CONFIG_WPS_ER
1228static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1229 char *cmd)
1230{
1231 char *uuid = cmd, *pin, *pos;
1232 u8 addr_buf[ETH_ALEN], *addr = NULL;
1233 pin = os_strchr(uuid, ' ');
1234 if (pin == NULL)
1235 return -1;
1236 *pin++ = '\0';
1237 pos = os_strchr(pin, ' ');
1238 if (pos) {
1239 *pos++ = '\0';
1240 if (hwaddr_aton(pos, addr_buf) == 0)
1241 addr = addr_buf;
1242 }
1243 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1244}
1245
1246
1247static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1248 char *cmd)
1249{
1250 char *uuid = cmd, *pin;
1251 pin = os_strchr(uuid, ' ');
1252 if (pin == NULL)
1253 return -1;
1254 *pin++ = '\0';
1255 return wpas_wps_er_learn(wpa_s, uuid, pin);
1256}
1257
1258
1259static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1260 struct wpa_supplicant *wpa_s, char *cmd)
1261{
1262 char *uuid = cmd, *id;
1263 id = os_strchr(uuid, ' ');
1264 if (id == NULL)
1265 return -1;
1266 *id++ = '\0';
1267 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1268}
1269
1270
1271static int wpa_supplicant_ctrl_iface_wps_er_config(
1272 struct wpa_supplicant *wpa_s, char *cmd)
1273{
1274 char *pin;
1275 char *new_ssid;
1276 char *new_auth;
1277 char *new_encr;
1278 char *new_key;
1279 struct wps_new_ap_settings ap;
1280
1281 pin = os_strchr(cmd, ' ');
1282 if (pin == NULL)
1283 return -1;
1284 *pin++ = '\0';
1285
1286 new_ssid = os_strchr(pin, ' ');
1287 if (new_ssid == NULL)
1288 return -1;
1289 *new_ssid++ = '\0';
1290
1291 new_auth = os_strchr(new_ssid, ' ');
1292 if (new_auth == NULL)
1293 return -1;
1294 *new_auth++ = '\0';
1295
1296 new_encr = os_strchr(new_auth, ' ');
1297 if (new_encr == NULL)
1298 return -1;
1299 *new_encr++ = '\0';
1300
1301 new_key = os_strchr(new_encr, ' ');
1302 if (new_key == NULL)
1303 return -1;
1304 *new_key++ = '\0';
1305
1306 os_memset(&ap, 0, sizeof(ap));
1307 ap.ssid_hex = new_ssid;
1308 ap.auth = new_auth;
1309 ap.encr = new_encr;
1310 ap.key_hex = new_key;
1311 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1312}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001313
1314
1315#ifdef CONFIG_WPS_NFC
1316static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1317 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1318{
1319 int ndef;
1320 struct wpabuf *buf;
1321 int res;
1322 char *uuid;
1323
1324 uuid = os_strchr(cmd, ' ');
1325 if (uuid == NULL)
1326 return -1;
1327 *uuid++ = '\0';
1328
1329 if (os_strcmp(cmd, "WPS") == 0)
1330 ndef = 0;
1331 else if (os_strcmp(cmd, "NDEF") == 0)
1332 ndef = 1;
1333 else
1334 return -1;
1335
1336 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1337 if (buf == NULL)
1338 return -1;
1339
1340 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1341 wpabuf_len(buf));
1342 reply[res++] = '\n';
1343 reply[res] = '\0';
1344
1345 wpabuf_free(buf);
1346
1347 return res;
1348}
1349#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350#endif /* CONFIG_WPS_ER */
1351
1352#endif /* CONFIG_WPS */
1353
1354
1355#ifdef CONFIG_IBSS_RSN
1356static int wpa_supplicant_ctrl_iface_ibss_rsn(
1357 struct wpa_supplicant *wpa_s, char *addr)
1358{
1359 u8 peer[ETH_ALEN];
1360
1361 if (hwaddr_aton(addr, peer)) {
1362 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1363 "address '%s'", addr);
1364 return -1;
1365 }
1366
1367 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1368 MAC2STR(peer));
1369
1370 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1371}
1372#endif /* CONFIG_IBSS_RSN */
1373
1374
1375static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1376 char *rsp)
1377{
1378#ifdef IEEE8021X_EAPOL
1379 char *pos, *id_pos;
1380 int id;
1381 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001382
1383 pos = os_strchr(rsp, '-');
1384 if (pos == NULL)
1385 return -1;
1386 *pos++ = '\0';
1387 id_pos = pos;
1388 pos = os_strchr(pos, ':');
1389 if (pos == NULL)
1390 return -1;
1391 *pos++ = '\0';
1392 id = atoi(id_pos);
1393 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1394 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1395 (u8 *) pos, os_strlen(pos));
1396
1397 ssid = wpa_config_get_network(wpa_s->conf, id);
1398 if (ssid == NULL) {
1399 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1400 "to update", id);
1401 return -1;
1402 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001404 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1405 pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406#else /* IEEE8021X_EAPOL */
1407 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1408 return -1;
1409#endif /* IEEE8021X_EAPOL */
1410}
1411
1412
1413static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1414 const char *params,
1415 char *buf, size_t buflen)
1416{
1417 char *pos, *end, tmp[30];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001418 int res, verbose, wps, ret;
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001420 verbose = os_strcmp(params, "-VERBOSE") == 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001421 wps = os_strcmp(params, "-WPS") == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422 pos = buf;
1423 end = buf + buflen;
1424 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1425 struct wpa_ssid *ssid = wpa_s->current_ssid;
1426 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1427 MAC2STR(wpa_s->bssid));
1428 if (ret < 0 || ret >= end - pos)
1429 return pos - buf;
1430 pos += ret;
1431 if (ssid) {
1432 u8 *_ssid = ssid->ssid;
1433 size_t ssid_len = ssid->ssid_len;
1434 u8 ssid_buf[MAX_SSID_LEN];
1435 if (ssid_len == 0) {
1436 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1437 if (_res < 0)
1438 ssid_len = 0;
1439 else
1440 ssid_len = _res;
1441 _ssid = ssid_buf;
1442 }
1443 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1444 wpa_ssid_txt(_ssid, ssid_len),
1445 ssid->id);
1446 if (ret < 0 || ret >= end - pos)
1447 return pos - buf;
1448 pos += ret;
1449
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001450 if (wps && ssid->passphrase &&
1451 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1452 (ssid->mode == WPAS_MODE_AP ||
1453 ssid->mode == WPAS_MODE_P2P_GO)) {
1454 ret = os_snprintf(pos, end - pos,
1455 "passphrase=%s\n",
1456 ssid->passphrase);
1457 if (ret < 0 || ret >= end - pos)
1458 return pos - buf;
1459 pos += ret;
1460 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461 if (ssid->id_str) {
1462 ret = os_snprintf(pos, end - pos,
1463 "id_str=%s\n",
1464 ssid->id_str);
1465 if (ret < 0 || ret >= end - pos)
1466 return pos - buf;
1467 pos += ret;
1468 }
1469
1470 switch (ssid->mode) {
1471 case WPAS_MODE_INFRA:
1472 ret = os_snprintf(pos, end - pos,
1473 "mode=station\n");
1474 break;
1475 case WPAS_MODE_IBSS:
1476 ret = os_snprintf(pos, end - pos,
1477 "mode=IBSS\n");
1478 break;
1479 case WPAS_MODE_AP:
1480 ret = os_snprintf(pos, end - pos,
1481 "mode=AP\n");
1482 break;
1483 case WPAS_MODE_P2P_GO:
1484 ret = os_snprintf(pos, end - pos,
1485 "mode=P2P GO\n");
1486 break;
1487 case WPAS_MODE_P2P_GROUP_FORMATION:
1488 ret = os_snprintf(pos, end - pos,
1489 "mode=P2P GO - group "
1490 "formation\n");
1491 break;
1492 default:
1493 ret = 0;
1494 break;
1495 }
1496 if (ret < 0 || ret >= end - pos)
1497 return pos - buf;
1498 pos += ret;
1499 }
1500
1501#ifdef CONFIG_AP
1502 if (wpa_s->ap_iface) {
1503 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1504 end - pos,
1505 verbose);
1506 } else
1507#endif /* CONFIG_AP */
1508 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1509 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001510#ifdef CONFIG_SAE
1511 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001512#ifdef CONFIG_AP
1513 !wpa_s->ap_iface &&
1514#endif /* CONFIG_AP */
1515 wpa_s->sme.sae.state == SAE_ACCEPTED) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001516 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1517 wpa_s->sme.sae.group);
1518 if (ret < 0 || ret >= end - pos)
1519 return pos - buf;
1520 pos += ret;
1521 }
1522#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001523 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1524 wpa_supplicant_state_txt(wpa_s->wpa_state));
1525 if (ret < 0 || ret >= end - pos)
1526 return pos - buf;
1527 pos += ret;
1528
1529 if (wpa_s->l2 &&
1530 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1531 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
1532 if (ret < 0 || ret >= end - pos)
1533 return pos - buf;
1534 pos += ret;
1535 }
1536
1537#ifdef CONFIG_P2P
1538 if (wpa_s->global->p2p) {
1539 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1540 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
1541 if (ret < 0 || ret >= end - pos)
1542 return pos - buf;
1543 pos += ret;
1544 }
1545#endif /* CONFIG_P2P */
1546
1547 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1548 MAC2STR(wpa_s->own_addr));
1549 if (ret < 0 || ret >= end - pos)
1550 return pos - buf;
1551 pos += ret;
1552
Dmitry Shmidt04949592012-07-19 12:16:46 -07001553#ifdef CONFIG_HS20
1554 if (wpa_s->current_bss &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001555 wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
1556 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1557 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001558 ret = os_snprintf(pos, end - pos, "hs20=1\n");
1559 if (ret < 0 || ret >= end - pos)
1560 return pos - buf;
1561 pos += ret;
1562 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001563
1564 if (wpa_s->current_ssid) {
1565 struct wpa_cred *cred;
1566 char *type;
1567
1568 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1569 if (wpa_s->current_ssid->parent_cred != cred)
1570 continue;
1571 if (!cred->domain)
1572 continue;
1573
1574 ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
1575 cred->domain);
1576 if (ret < 0 || ret >= end - pos)
1577 return pos - buf;
1578 pos += ret;
1579
1580 if (wpa_s->current_bss == NULL ||
1581 wpa_s->current_bss->anqp == NULL)
1582 res = -1;
1583 else
1584 res = interworking_home_sp_cred(
1585 wpa_s, cred,
1586 wpa_s->current_bss->anqp->domain_name);
1587 if (res > 0)
1588 type = "home";
1589 else if (res == 0)
1590 type = "roaming";
1591 else
1592 type = "unknown";
1593
1594 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
1595 if (ret < 0 || ret >= end - pos)
1596 return pos - buf;
1597 pos += ret;
1598
1599 break;
1600 }
1601 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001602#endif /* CONFIG_HS20 */
1603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001604 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1605 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1606 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1607 verbose);
1608 if (res >= 0)
1609 pos += res;
1610 }
1611
1612 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1613 if (res >= 0)
1614 pos += res;
1615
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001616#ifdef ANDROID
1617 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
Irfan Sherifff20a4432012-04-16 16:48:34 -07001618 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001619 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
Irfan Sherifff20a4432012-04-16 16:48:34 -07001620 wpa_s->wpa_state,
Irfan Sheriffe78e7672012-08-01 11:10:15 -07001621 MAC2STR(wpa_s->bssid),
andy2_kuo5b5fb022012-05-22 11:53:07 -07001622 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1623 wpa_ssid_txt(wpa_s->current_ssid->ssid,
Irfan Sheriff10294772012-05-11 11:23:35 -07001624 wpa_s->current_ssid->ssid_len) : "");
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001625 if (wpa_s->wpa_state == WPA_COMPLETED) {
1626 struct wpa_ssid *ssid = wpa_s->current_ssid;
1627 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- connection to "
1628 MACSTR " completed %s [id=%d id_str=%s]",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001629 MAC2STR(wpa_s->bssid), "(auth)",
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001630 ssid ? ssid->id : -1,
1631 ssid && ssid->id_str ? ssid->id_str : "");
1632 }
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001633#endif /* ANDROID */
1634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635 return pos - buf;
1636}
1637
1638
1639static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1640 char *cmd)
1641{
1642 char *pos;
1643 int id;
1644 struct wpa_ssid *ssid;
1645 u8 bssid[ETH_ALEN];
1646
1647 /* cmd: "<network id> <BSSID>" */
1648 pos = os_strchr(cmd, ' ');
1649 if (pos == NULL)
1650 return -1;
1651 *pos++ = '\0';
1652 id = atoi(cmd);
1653 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1654 if (hwaddr_aton(pos, bssid)) {
1655 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1656 return -1;
1657 }
1658
1659 ssid = wpa_config_get_network(wpa_s->conf, id);
1660 if (ssid == NULL) {
1661 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1662 "to update", id);
1663 return -1;
1664 }
1665
1666 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1667 ssid->bssid_set = !is_zero_ether_addr(bssid);
1668
1669 return 0;
1670}
1671
1672
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001673static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001674 char *cmd, char *buf,
1675 size_t buflen)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001676{
1677 u8 bssid[ETH_ALEN];
1678 struct wpa_blacklist *e;
1679 char *pos, *end;
1680 int ret;
1681
1682 /* cmd: "BLACKLIST [<BSSID>]" */
1683 if (*cmd == '\0') {
1684 pos = buf;
1685 end = buf + buflen;
1686 e = wpa_s->blacklist;
1687 while (e) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001688 ret = os_snprintf(pos, end - pos, MACSTR "\n",
1689 MAC2STR(e->bssid));
1690 if (ret < 0 || ret >= end - pos)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001691 return pos - buf;
1692 pos += ret;
1693 e = e->next;
1694 }
1695 return pos - buf;
1696 }
1697
1698 cmd++;
1699 if (os_strncmp(cmd, "clear", 5) == 0) {
1700 wpa_blacklist_clear(wpa_s);
1701 os_memcpy(buf, "OK\n", 3);
1702 return 3;
1703 }
1704
1705 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1706 if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001707 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001708 return -1;
1709 }
1710
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001711 /*
1712 * Add the BSSID twice, so its count will be 2, causing it to be
1713 * skipped when processing scan results.
1714 */
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001715 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001716 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001717 return -1;
1718 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001719 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001720 return -1;
1721 os_memcpy(buf, "OK\n", 3);
1722 return 3;
1723}
1724
1725
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001726extern int wpa_debug_level;
1727extern int wpa_debug_timestamp;
1728
1729static const char * debug_level_str(int level)
1730{
1731 switch (level) {
1732 case MSG_EXCESSIVE:
1733 return "EXCESSIVE";
1734 case MSG_MSGDUMP:
1735 return "MSGDUMP";
1736 case MSG_DEBUG:
1737 return "DEBUG";
1738 case MSG_INFO:
1739 return "INFO";
1740 case MSG_WARNING:
1741 return "WARNING";
1742 case MSG_ERROR:
1743 return "ERROR";
1744 default:
1745 return "?";
1746 }
1747}
1748
1749
1750static int str_to_debug_level(const char *s)
1751{
1752 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1753 return MSG_EXCESSIVE;
1754 if (os_strcasecmp(s, "MSGDUMP") == 0)
1755 return MSG_MSGDUMP;
1756 if (os_strcasecmp(s, "DEBUG") == 0)
1757 return MSG_DEBUG;
1758 if (os_strcasecmp(s, "INFO") == 0)
1759 return MSG_INFO;
1760 if (os_strcasecmp(s, "WARNING") == 0)
1761 return MSG_WARNING;
1762 if (os_strcasecmp(s, "ERROR") == 0)
1763 return MSG_ERROR;
1764 return -1;
1765}
1766
1767
1768static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1769 char *cmd, char *buf,
1770 size_t buflen)
1771{
1772 char *pos, *end, *stamp;
1773 int ret;
1774
1775 if (cmd == NULL) {
1776 return -1;
1777 }
1778
1779 /* cmd: "LOG_LEVEL [<level>]" */
1780 if (*cmd == '\0') {
1781 pos = buf;
1782 end = buf + buflen;
1783 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1784 "Timestamp: %d\n",
1785 debug_level_str(wpa_debug_level),
1786 wpa_debug_timestamp);
1787 if (ret < 0 || ret >= end - pos)
1788 ret = 0;
1789
1790 return ret;
1791 }
1792
1793 while (*cmd == ' ')
1794 cmd++;
1795
1796 stamp = os_strchr(cmd, ' ');
1797 if (stamp) {
1798 *stamp++ = '\0';
1799 while (*stamp == ' ') {
1800 stamp++;
1801 }
1802 }
1803
1804 if (cmd && os_strlen(cmd)) {
1805 int level = str_to_debug_level(cmd);
1806 if (level < 0)
1807 return -1;
1808 wpa_debug_level = level;
1809 }
1810
1811 if (stamp && os_strlen(stamp))
1812 wpa_debug_timestamp = atoi(stamp);
1813
1814 os_memcpy(buf, "OK\n", 3);
1815 return 3;
1816}
1817
1818
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819static int wpa_supplicant_ctrl_iface_list_networks(
1820 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1821{
1822 char *pos, *end;
1823 struct wpa_ssid *ssid;
1824 int ret;
1825
1826 pos = buf;
1827 end = buf + buflen;
1828 ret = os_snprintf(pos, end - pos,
1829 "network id / ssid / bssid / flags\n");
1830 if (ret < 0 || ret >= end - pos)
1831 return pos - buf;
1832 pos += ret;
1833
1834 ssid = wpa_s->conf->ssid;
1835 while (ssid) {
1836 ret = os_snprintf(pos, end - pos, "%d\t%s",
1837 ssid->id,
1838 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1839 if (ret < 0 || ret >= end - pos)
1840 return pos - buf;
1841 pos += ret;
1842 if (ssid->bssid_set) {
1843 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
1844 MAC2STR(ssid->bssid));
1845 } else {
1846 ret = os_snprintf(pos, end - pos, "\tany");
1847 }
1848 if (ret < 0 || ret >= end - pos)
1849 return pos - buf;
1850 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001851 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001852 ssid == wpa_s->current_ssid ?
1853 "[CURRENT]" : "",
1854 ssid->disabled ? "[DISABLED]" : "",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001855 ssid->disabled_until.sec ?
1856 "[TEMP-DISABLED]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
1858 "");
1859 if (ret < 0 || ret >= end - pos)
1860 return pos - buf;
1861 pos += ret;
1862 ret = os_snprintf(pos, end - pos, "\n");
1863 if (ret < 0 || ret >= end - pos)
1864 return pos - buf;
1865 pos += ret;
1866
1867 ssid = ssid->next;
1868 }
1869
1870 return pos - buf;
1871}
1872
1873
1874static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
1875{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001876 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001877 ret = os_snprintf(pos, end - pos, "-");
1878 if (ret < 0 || ret >= end - pos)
1879 return pos;
1880 pos += ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001881 ret = wpa_write_ciphers(pos, end, cipher, "+");
1882 if (ret < 0)
1883 return pos;
1884 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 return pos;
1886}
1887
1888
1889static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
1890 const u8 *ie, size_t ie_len)
1891{
1892 struct wpa_ie_data data;
1893 int first, ret;
1894
1895 ret = os_snprintf(pos, end - pos, "[%s-", proto);
1896 if (ret < 0 || ret >= end - pos)
1897 return pos;
1898 pos += ret;
1899
1900 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
1901 ret = os_snprintf(pos, end - pos, "?]");
1902 if (ret < 0 || ret >= end - pos)
1903 return pos;
1904 pos += ret;
1905 return pos;
1906 }
1907
1908 first = 1;
1909 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1910 ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
1911 if (ret < 0 || ret >= end - pos)
1912 return pos;
1913 pos += ret;
1914 first = 0;
1915 }
1916 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
1917 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
1918 if (ret < 0 || ret >= end - pos)
1919 return pos;
1920 pos += ret;
1921 first = 0;
1922 }
1923 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
1924 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
1925 if (ret < 0 || ret >= end - pos)
1926 return pos;
1927 pos += ret;
1928 first = 0;
1929 }
1930#ifdef CONFIG_IEEE80211R
1931 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1932 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
1933 first ? "" : "+");
1934 if (ret < 0 || ret >= end - pos)
1935 return pos;
1936 pos += ret;
1937 first = 0;
1938 }
1939 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1940 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
1941 first ? "" : "+");
1942 if (ret < 0 || ret >= end - pos)
1943 return pos;
1944 pos += ret;
1945 first = 0;
1946 }
1947#endif /* CONFIG_IEEE80211R */
1948#ifdef CONFIG_IEEE80211W
1949 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1950 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
1951 first ? "" : "+");
1952 if (ret < 0 || ret >= end - pos)
1953 return pos;
1954 pos += ret;
1955 first = 0;
1956 }
1957 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1958 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
1959 first ? "" : "+");
1960 if (ret < 0 || ret >= end - pos)
1961 return pos;
1962 pos += ret;
1963 first = 0;
1964 }
1965#endif /* CONFIG_IEEE80211W */
1966
1967 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
1968
1969 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
1970 ret = os_snprintf(pos, end - pos, "-preauth");
1971 if (ret < 0 || ret >= end - pos)
1972 return pos;
1973 pos += ret;
1974 }
1975
1976 ret = os_snprintf(pos, end - pos, "]");
1977 if (ret < 0 || ret >= end - pos)
1978 return pos;
1979 pos += ret;
1980
1981 return pos;
1982}
1983
1984
1985#ifdef CONFIG_WPS
1986static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
1987 char *pos, char *end,
1988 struct wpabuf *wps_ie)
1989{
1990 int ret;
1991 const char *txt;
1992
1993 if (wps_ie == NULL)
1994 return pos;
1995 if (wps_is_selected_pbc_registrar(wps_ie))
1996 txt = "[WPS-PBC]";
1997#ifdef CONFIG_WPS2
1998 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
1999 txt = "[WPS-AUTH]";
2000#endif /* CONFIG_WPS2 */
2001 else if (wps_is_selected_pin_registrar(wps_ie))
2002 txt = "[WPS-PIN]";
2003 else
2004 txt = "[WPS]";
2005
2006 ret = os_snprintf(pos, end - pos, "%s", txt);
2007 if (ret >= 0 && ret < end - pos)
2008 pos += ret;
2009 wpabuf_free(wps_ie);
2010 return pos;
2011}
2012#endif /* CONFIG_WPS */
2013
2014
2015static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2016 char *pos, char *end,
2017 const struct wpa_bss *bss)
2018{
2019#ifdef CONFIG_WPS
2020 struct wpabuf *wps_ie;
2021 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2022 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2023#else /* CONFIG_WPS */
2024 return pos;
2025#endif /* CONFIG_WPS */
2026}
2027
2028
2029/* Format one result on one text line into a buffer. */
2030static int wpa_supplicant_ctrl_iface_scan_result(
2031 struct wpa_supplicant *wpa_s,
2032 const struct wpa_bss *bss, char *buf, size_t buflen)
2033{
2034 char *pos, *end;
2035 int ret;
2036 const u8 *ie, *ie2, *p2p;
2037
2038 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
2039 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2040 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2041 0)
2042 return 0; /* Do not show P2P listen discovery results here */
2043
2044 pos = buf;
2045 end = buf + buflen;
2046
2047 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2048 MAC2STR(bss->bssid), bss->freq, bss->level);
2049 if (ret < 0 || ret >= end - pos)
2050 return -1;
2051 pos += ret;
2052 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2053 if (ie)
2054 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2055 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2056 if (ie2)
2057 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
2058 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2059 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2060 ret = os_snprintf(pos, end - pos, "[WEP]");
2061 if (ret < 0 || ret >= end - pos)
2062 return -1;
2063 pos += ret;
2064 }
2065 if (bss->caps & IEEE80211_CAP_IBSS) {
2066 ret = os_snprintf(pos, end - pos, "[IBSS]");
2067 if (ret < 0 || ret >= end - pos)
2068 return -1;
2069 pos += ret;
2070 }
2071 if (bss->caps & IEEE80211_CAP_ESS) {
2072 ret = os_snprintf(pos, end - pos, "[ESS]");
2073 if (ret < 0 || ret >= end - pos)
2074 return -1;
2075 pos += ret;
2076 }
2077 if (p2p) {
2078 ret = os_snprintf(pos, end - pos, "[P2P]");
2079 if (ret < 0 || ret >= end - pos)
2080 return -1;
2081 pos += ret;
2082 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002083#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002084 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002085 ret = os_snprintf(pos, end - pos, "[HS20]");
2086 if (ret < 0 || ret >= end - pos)
2087 return -1;
2088 pos += ret;
2089 }
2090#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002091
2092 ret = os_snprintf(pos, end - pos, "\t%s",
2093 wpa_ssid_txt(bss->ssid, bss->ssid_len));
2094 if (ret < 0 || ret >= end - pos)
2095 return -1;
2096 pos += ret;
2097
2098 ret = os_snprintf(pos, end - pos, "\n");
2099 if (ret < 0 || ret >= end - pos)
2100 return -1;
2101 pos += ret;
2102
2103 return pos - buf;
2104}
2105
2106
2107static int wpa_supplicant_ctrl_iface_scan_results(
2108 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2109{
2110 char *pos, *end;
2111 struct wpa_bss *bss;
2112 int ret;
2113
2114 pos = buf;
2115 end = buf + buflen;
2116 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2117 "flags / ssid\n");
2118 if (ret < 0 || ret >= end - pos)
2119 return pos - buf;
2120 pos += ret;
2121
2122 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2123 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2124 end - pos);
2125 if (ret < 0 || ret >= end - pos)
2126 return pos - buf;
2127 pos += ret;
2128 }
2129
2130 return pos - buf;
2131}
2132
2133
2134static int wpa_supplicant_ctrl_iface_select_network(
2135 struct wpa_supplicant *wpa_s, char *cmd)
2136{
2137 int id;
2138 struct wpa_ssid *ssid;
2139
2140 /* cmd: "<network id>" or "any" */
2141 if (os_strcmp(cmd, "any") == 0) {
2142 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2143 ssid = NULL;
2144 } else {
2145 id = atoi(cmd);
2146 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2147
2148 ssid = wpa_config_get_network(wpa_s->conf, id);
2149 if (ssid == NULL) {
2150 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2151 "network id=%d", id);
2152 return -1;
2153 }
2154 if (ssid->disabled == 2) {
2155 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2156 "SELECT_NETWORK with persistent P2P group");
2157 return -1;
2158 }
2159 }
2160
2161 wpa_supplicant_select_network(wpa_s, ssid);
2162
2163 return 0;
2164}
2165
2166
2167static int wpa_supplicant_ctrl_iface_enable_network(
2168 struct wpa_supplicant *wpa_s, char *cmd)
2169{
2170 int id;
2171 struct wpa_ssid *ssid;
2172
2173 /* cmd: "<network id>" or "all" */
2174 if (os_strcmp(cmd, "all") == 0) {
2175 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2176 ssid = NULL;
2177 } else {
2178 id = atoi(cmd);
2179 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2180
2181 ssid = wpa_config_get_network(wpa_s->conf, id);
2182 if (ssid == NULL) {
2183 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2184 "network id=%d", id);
2185 return -1;
2186 }
2187 if (ssid->disabled == 2) {
2188 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2189 "ENABLE_NETWORK with persistent P2P group");
2190 return -1;
2191 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002192
2193 if (os_strstr(cmd, " no-connect")) {
2194 ssid->disabled = 0;
2195 return 0;
2196 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197 }
2198 wpa_supplicant_enable_network(wpa_s, ssid);
2199
2200 return 0;
2201}
2202
2203
2204static int wpa_supplicant_ctrl_iface_disable_network(
2205 struct wpa_supplicant *wpa_s, char *cmd)
2206{
2207 int id;
2208 struct wpa_ssid *ssid;
2209
2210 /* cmd: "<network id>" or "all" */
2211 if (os_strcmp(cmd, "all") == 0) {
2212 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2213 ssid = NULL;
2214 } else {
2215 id = atoi(cmd);
2216 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2217
2218 ssid = wpa_config_get_network(wpa_s->conf, id);
2219 if (ssid == NULL) {
2220 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2221 "network id=%d", id);
2222 return -1;
2223 }
2224 if (ssid->disabled == 2) {
2225 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2226 "DISABLE_NETWORK with persistent P2P "
2227 "group");
2228 return -1;
2229 }
2230 }
2231 wpa_supplicant_disable_network(wpa_s, ssid);
2232
2233 return 0;
2234}
2235
2236
2237static int wpa_supplicant_ctrl_iface_add_network(
2238 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2239{
2240 struct wpa_ssid *ssid;
2241 int ret;
2242
2243 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2244
2245 ssid = wpa_config_add_network(wpa_s->conf);
2246 if (ssid == NULL)
2247 return -1;
2248
2249 wpas_notify_network_added(wpa_s, ssid);
2250
2251 ssid->disabled = 1;
2252 wpa_config_set_network_defaults(ssid);
2253
2254 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
2255 if (ret < 0 || (size_t) ret >= buflen)
2256 return -1;
2257 return ret;
2258}
2259
2260
2261static int wpa_supplicant_ctrl_iface_remove_network(
2262 struct wpa_supplicant *wpa_s, char *cmd)
2263{
2264 int id;
2265 struct wpa_ssid *ssid;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002266 int was_disabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002267
2268 /* cmd: "<network id>" or "all" */
2269 if (os_strcmp(cmd, "all") == 0) {
2270 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002271 if (wpa_s->sched_scanning)
2272 wpa_supplicant_cancel_sched_scan(wpa_s);
2273
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002274 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002275 if (wpa_s->current_ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002276#ifdef CONFIG_SME
2277 wpa_s->sme.prev_bssid_set = 0;
2278#endif /* CONFIG_SME */
Jouni Malinen75ecf522011-06-27 15:19:46 -07002279 wpa_sm_set_config(wpa_s->wpa, NULL);
2280 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002281 wpa_supplicant_deauthenticate(
2282 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002284 ssid = wpa_s->conf->ssid;
2285 while (ssid) {
2286 struct wpa_ssid *remove_ssid = ssid;
2287 id = ssid->id;
2288 ssid = ssid->next;
2289 wpas_notify_network_removed(wpa_s, remove_ssid);
2290 wpa_config_remove_network(wpa_s->conf, id);
2291 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292 return 0;
2293 }
2294
2295 id = atoi(cmd);
2296 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2297
2298 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002299 if (ssid)
2300 wpas_notify_network_removed(wpa_s, ssid);
Deepthi Gowria831d782012-09-03 11:55:38 +03002301 if (ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002302 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2303 "id=%d", id);
2304 return -1;
2305 }
2306
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002307 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002308#ifdef CONFIG_SME
2309 wpa_s->sme.prev_bssid_set = 0;
2310#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002312 * Invalidate the EAP session cache if the current or
2313 * previously used network is removed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002314 */
2315 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002316 }
2317
2318 if (ssid == wpa_s->current_ssid) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002319 wpa_sm_set_config(wpa_s->wpa, NULL);
2320 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002322 wpa_supplicant_deauthenticate(wpa_s,
2323 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002324 }
2325
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002326 was_disabled = ssid->disabled;
2327
Deepthi Gowria831d782012-09-03 11:55:38 +03002328 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2329 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2330 "network id=%d", id);
2331 return -1;
2332 }
2333
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002334 if (!was_disabled && wpa_s->sched_scanning) {
2335 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2336 "network from filters");
2337 wpa_supplicant_cancel_sched_scan(wpa_s);
2338 wpa_supplicant_req_scan(wpa_s, 0, 0);
2339 }
2340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002341 return 0;
2342}
2343
2344
2345static int wpa_supplicant_ctrl_iface_set_network(
2346 struct wpa_supplicant *wpa_s, char *cmd)
2347{
2348 int id;
2349 struct wpa_ssid *ssid;
2350 char *name, *value;
2351
2352 /* cmd: "<network id> <variable name> <value>" */
2353 name = os_strchr(cmd, ' ');
2354 if (name == NULL)
2355 return -1;
2356 *name++ = '\0';
2357
2358 value = os_strchr(name, ' ');
2359 if (value == NULL)
2360 return -1;
2361 *value++ = '\0';
2362
2363 id = atoi(cmd);
2364 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2365 id, name);
2366 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2367 (u8 *) value, os_strlen(value));
2368
2369 ssid = wpa_config_get_network(wpa_s->conf, id);
2370 if (ssid == NULL) {
2371 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2372 "id=%d", id);
2373 return -1;
2374 }
2375
2376 if (wpa_config_set(ssid, name, value, 0) < 0) {
2377 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2378 "variable '%s'", name);
2379 return -1;
2380 }
2381
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002382 if (os_strcmp(name, "bssid") != 0 &&
2383 os_strcmp(name, "priority") != 0)
2384 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002385
2386 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387 /*
2388 * Invalidate the EAP session cache if anything in the current
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002389 * or previously used configuration changes.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002390 */
2391 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2392 }
2393
2394 if ((os_strcmp(name, "psk") == 0 &&
2395 value[0] == '"' && ssid->ssid_len) ||
2396 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2397 wpa_config_update_psk(ssid);
2398 else if (os_strcmp(name, "priority") == 0)
2399 wpa_config_update_prio_list(wpa_s->conf);
2400
2401 return 0;
2402}
2403
2404
2405static int wpa_supplicant_ctrl_iface_get_network(
2406 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2407{
2408 int id;
2409 size_t res;
2410 struct wpa_ssid *ssid;
2411 char *name, *value;
2412
2413 /* cmd: "<network id> <variable name>" */
2414 name = os_strchr(cmd, ' ');
2415 if (name == NULL || buflen == 0)
2416 return -1;
2417 *name++ = '\0';
2418
2419 id = atoi(cmd);
2420 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2421 id, name);
2422
2423 ssid = wpa_config_get_network(wpa_s->conf, id);
2424 if (ssid == NULL) {
2425 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2426 "id=%d", id);
2427 return -1;
2428 }
2429
2430 value = wpa_config_get_no_key(ssid, name);
2431 if (value == NULL) {
2432 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2433 "variable '%s'", name);
2434 return -1;
2435 }
2436
2437 res = os_strlcpy(buf, value, buflen);
2438 if (res >= buflen) {
2439 os_free(value);
2440 return -1;
2441 }
2442
2443 os_free(value);
2444
2445 return res;
2446}
2447
2448
Dmitry Shmidt04949592012-07-19 12:16:46 -07002449static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2450 char *buf, size_t buflen)
2451{
2452 char *pos, *end;
2453 struct wpa_cred *cred;
2454 int ret;
2455
2456 pos = buf;
2457 end = buf + buflen;
2458 ret = os_snprintf(pos, end - pos,
2459 "cred id / realm / username / domain / imsi\n");
2460 if (ret < 0 || ret >= end - pos)
2461 return pos - buf;
2462 pos += ret;
2463
2464 cred = wpa_s->conf->cred;
2465 while (cred) {
2466 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2467 cred->id, cred->realm ? cred->realm : "",
2468 cred->username ? cred->username : "",
2469 cred->domain ? cred->domain : "",
2470 cred->imsi ? cred->imsi : "");
2471 if (ret < 0 || ret >= end - pos)
2472 return pos - buf;
2473 pos += ret;
2474
2475 cred = cred->next;
2476 }
2477
2478 return pos - buf;
2479}
2480
2481
2482static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2483 char *buf, size_t buflen)
2484{
2485 struct wpa_cred *cred;
2486 int ret;
2487
2488 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2489
2490 cred = wpa_config_add_cred(wpa_s->conf);
2491 if (cred == NULL)
2492 return -1;
2493
2494 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2495 if (ret < 0 || (size_t) ret >= buflen)
2496 return -1;
2497 return ret;
2498}
2499
2500
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002501static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2502 struct wpa_cred *cred)
2503{
2504 struct wpa_ssid *ssid;
2505 char str[20];
2506
2507 if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
2508 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2509 return -1;
2510 }
2511
2512 /* Remove any network entry created based on the removed credential */
2513 ssid = wpa_s->conf->ssid;
2514 while (ssid) {
2515 if (ssid->parent_cred == cred) {
2516 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
2517 "used the removed credential", ssid->id);
2518 os_snprintf(str, sizeof(str), "%d", ssid->id);
2519 ssid = ssid->next;
2520 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
2521 } else
2522 ssid = ssid->next;
2523 }
2524
2525 return 0;
2526}
2527
2528
Dmitry Shmidt04949592012-07-19 12:16:46 -07002529static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2530 char *cmd)
2531{
2532 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002533 struct wpa_cred *cred, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002534
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002535 /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002536 if (os_strcmp(cmd, "all") == 0) {
2537 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2538 cred = wpa_s->conf->cred;
2539 while (cred) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002540 prev = cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002541 cred = cred->next;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002542 wpas_ctrl_remove_cred(wpa_s, prev);
2543 }
2544 return 0;
2545 }
2546
2547 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
2548 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
2549 cmd + 8);
2550 cred = wpa_s->conf->cred;
2551 while (cred) {
2552 prev = cred;
2553 cred = cred->next;
2554 if (prev->domain &&
2555 os_strcmp(prev->domain, cmd + 8) == 0)
2556 wpas_ctrl_remove_cred(wpa_s, prev);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002557 }
2558 return 0;
2559 }
2560
2561 id = atoi(cmd);
2562 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2563
2564 cred = wpa_config_get_cred(wpa_s->conf, id);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002565 return wpas_ctrl_remove_cred(wpa_s, cred);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002566}
2567
2568
2569static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2570 char *cmd)
2571{
2572 int id;
2573 struct wpa_cred *cred;
2574 char *name, *value;
2575
2576 /* cmd: "<cred id> <variable name> <value>" */
2577 name = os_strchr(cmd, ' ');
2578 if (name == NULL)
2579 return -1;
2580 *name++ = '\0';
2581
2582 value = os_strchr(name, ' ');
2583 if (value == NULL)
2584 return -1;
2585 *value++ = '\0';
2586
2587 id = atoi(cmd);
2588 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2589 id, name);
2590 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2591 (u8 *) value, os_strlen(value));
2592
2593 cred = wpa_config_get_cred(wpa_s->conf, id);
2594 if (cred == NULL) {
2595 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2596 id);
2597 return -1;
2598 }
2599
2600 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2601 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2602 "variable '%s'", name);
2603 return -1;
2604 }
2605
2606 return 0;
2607}
2608
2609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002610#ifndef CONFIG_NO_CONFIG_WRITE
2611static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2612{
2613 int ret;
2614
2615 if (!wpa_s->conf->update_config) {
2616 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2617 "to update configuration (update_config=0)");
2618 return -1;
2619 }
2620
2621 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2622 if (ret) {
2623 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2624 "update configuration");
2625 } else {
2626 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2627 " updated");
2628 }
2629
2630 return ret;
2631}
2632#endif /* CONFIG_NO_CONFIG_WRITE */
2633
2634
2635static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2636 struct wpa_driver_capa *capa,
2637 char *buf, size_t buflen)
2638{
2639 int ret, first = 1;
2640 char *pos, *end;
2641 size_t len;
2642
2643 pos = buf;
2644 end = pos + buflen;
2645
2646 if (res < 0) {
2647 if (strict)
2648 return 0;
2649 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2650 if (len >= buflen)
2651 return -1;
2652 return len;
2653 }
2654
2655 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2656 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2657 if (ret < 0 || ret >= end - pos)
2658 return pos - buf;
2659 pos += ret;
2660 first = 0;
2661 }
2662
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002663 if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2664 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
2665 if (ret < 0 || ret >= end - pos)
2666 return pos - buf;
2667 pos += ret;
2668 first = 0;
2669 }
2670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2672 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2673 if (ret < 0 || ret >= end - pos)
2674 return pos - buf;
2675 pos += ret;
2676 first = 0;
2677 }
2678
2679 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2680 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
2681 if (ret < 0 || ret >= end - pos)
2682 return pos - buf;
2683 pos += ret;
2684 first = 0;
2685 }
2686
2687 return pos - buf;
2688}
2689
2690
2691static int ctrl_iface_get_capability_group(int res, char *strict,
2692 struct wpa_driver_capa *capa,
2693 char *buf, size_t buflen)
2694{
2695 int ret, first = 1;
2696 char *pos, *end;
2697 size_t len;
2698
2699 pos = buf;
2700 end = pos + buflen;
2701
2702 if (res < 0) {
2703 if (strict)
2704 return 0;
2705 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
2706 if (len >= buflen)
2707 return -1;
2708 return len;
2709 }
2710
2711 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2712 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2713 if (ret < 0 || ret >= end - pos)
2714 return pos - buf;
2715 pos += ret;
2716 first = 0;
2717 }
2718
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002719 if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2720 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
2721 if (ret < 0 || ret >= end - pos)
2722 return pos - buf;
2723 pos += ret;
2724 first = 0;
2725 }
2726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002727 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2728 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2729 if (ret < 0 || ret >= end - pos)
2730 return pos - buf;
2731 pos += ret;
2732 first = 0;
2733 }
2734
2735 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
2736 ret = os_snprintf(pos, end - pos, "%sWEP104",
2737 first ? "" : " ");
2738 if (ret < 0 || ret >= end - pos)
2739 return pos - buf;
2740 pos += ret;
2741 first = 0;
2742 }
2743
2744 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
2745 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
2746 if (ret < 0 || ret >= end - pos)
2747 return pos - buf;
2748 pos += ret;
2749 first = 0;
2750 }
2751
2752 return pos - buf;
2753}
2754
2755
2756static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
2757 struct wpa_driver_capa *capa,
2758 char *buf, size_t buflen)
2759{
2760 int ret;
2761 char *pos, *end;
2762 size_t len;
2763
2764 pos = buf;
2765 end = pos + buflen;
2766
2767 if (res < 0) {
2768 if (strict)
2769 return 0;
2770 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
2771 "NONE", buflen);
2772 if (len >= buflen)
2773 return -1;
2774 return len;
2775 }
2776
2777 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
2778 if (ret < 0 || ret >= end - pos)
2779 return pos - buf;
2780 pos += ret;
2781
2782 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2783 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
2784 ret = os_snprintf(pos, end - pos, " WPA-EAP");
2785 if (ret < 0 || ret >= end - pos)
2786 return pos - buf;
2787 pos += ret;
2788 }
2789
2790 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2791 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2792 ret = os_snprintf(pos, end - pos, " WPA-PSK");
2793 if (ret < 0 || ret >= end - pos)
2794 return pos - buf;
2795 pos += ret;
2796 }
2797
2798 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2799 ret = os_snprintf(pos, end - pos, " WPA-NONE");
2800 if (ret < 0 || ret >= end - pos)
2801 return pos - buf;
2802 pos += ret;
2803 }
2804
2805 return pos - buf;
2806}
2807
2808
2809static int ctrl_iface_get_capability_proto(int res, char *strict,
2810 struct wpa_driver_capa *capa,
2811 char *buf, size_t buflen)
2812{
2813 int ret, first = 1;
2814 char *pos, *end;
2815 size_t len;
2816
2817 pos = buf;
2818 end = pos + buflen;
2819
2820 if (res < 0) {
2821 if (strict)
2822 return 0;
2823 len = os_strlcpy(buf, "RSN WPA", buflen);
2824 if (len >= buflen)
2825 return -1;
2826 return len;
2827 }
2828
2829 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2830 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2831 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
2832 if (ret < 0 || ret >= end - pos)
2833 return pos - buf;
2834 pos += ret;
2835 first = 0;
2836 }
2837
2838 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2839 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
2840 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
2841 if (ret < 0 || ret >= end - pos)
2842 return pos - buf;
2843 pos += ret;
2844 first = 0;
2845 }
2846
2847 return pos - buf;
2848}
2849
2850
2851static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
2852 struct wpa_driver_capa *capa,
2853 char *buf, size_t buflen)
2854{
2855 int ret, first = 1;
2856 char *pos, *end;
2857 size_t len;
2858
2859 pos = buf;
2860 end = pos + buflen;
2861
2862 if (res < 0) {
2863 if (strict)
2864 return 0;
2865 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
2866 if (len >= buflen)
2867 return -1;
2868 return len;
2869 }
2870
2871 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
2872 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
2873 if (ret < 0 || ret >= end - pos)
2874 return pos - buf;
2875 pos += ret;
2876 first = 0;
2877 }
2878
2879 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
2880 ret = os_snprintf(pos, end - pos, "%sSHARED",
2881 first ? "" : " ");
2882 if (ret < 0 || ret >= end - pos)
2883 return pos - buf;
2884 pos += ret;
2885 first = 0;
2886 }
2887
2888 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
2889 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
2890 if (ret < 0 || ret >= end - pos)
2891 return pos - buf;
2892 pos += ret;
2893 first = 0;
2894 }
2895
2896 return pos - buf;
2897}
2898
2899
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002900static int ctrl_iface_get_capability_modes(int res, char *strict,
2901 struct wpa_driver_capa *capa,
2902 char *buf, size_t buflen)
2903{
2904 int ret, first = 1;
2905 char *pos, *end;
2906 size_t len;
2907
2908 pos = buf;
2909 end = pos + buflen;
2910
2911 if (res < 0) {
2912 if (strict)
2913 return 0;
2914 len = os_strlcpy(buf, "IBSS AP", buflen);
2915 if (len >= buflen)
2916 return -1;
2917 return len;
2918 }
2919
2920 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
2921 ret = os_snprintf(pos, end - pos, "%sIBSS", first ? "" : " ");
2922 if (ret < 0 || ret >= end - pos)
2923 return pos - buf;
2924 pos += ret;
2925 first = 0;
2926 }
2927
2928 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
2929 ret = os_snprintf(pos, end - pos, "%sAP", first ? "" : " ");
2930 if (ret < 0 || ret >= end - pos)
2931 return pos - buf;
2932 pos += ret;
2933 first = 0;
2934 }
2935
2936 return pos - buf;
2937}
2938
2939
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002940static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
2941 char *buf, size_t buflen)
2942{
2943 struct hostapd_channel_data *chnl;
2944 int ret, i, j;
2945 char *pos, *end, *hmode;
2946
2947 pos = buf;
2948 end = pos + buflen;
2949
2950 for (j = 0; j < wpa_s->hw.num_modes; j++) {
2951 switch (wpa_s->hw.modes[j].mode) {
2952 case HOSTAPD_MODE_IEEE80211B:
2953 hmode = "B";
2954 break;
2955 case HOSTAPD_MODE_IEEE80211G:
2956 hmode = "G";
2957 break;
2958 case HOSTAPD_MODE_IEEE80211A:
2959 hmode = "A";
2960 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002961 case HOSTAPD_MODE_IEEE80211AD:
2962 hmode = "AD";
2963 break;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002964 default:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002965 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002966 }
2967 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
2968 if (ret < 0 || ret >= end - pos)
2969 return pos - buf;
2970 pos += ret;
2971 chnl = wpa_s->hw.modes[j].channels;
2972 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002973 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
2974 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002975 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
2976 if (ret < 0 || ret >= end - pos)
2977 return pos - buf;
2978 pos += ret;
2979 }
2980 ret = os_snprintf(pos, end - pos, "\n");
2981 if (ret < 0 || ret >= end - pos)
2982 return pos - buf;
2983 pos += ret;
2984 }
2985
2986 return pos - buf;
2987}
2988
2989
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002990static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
2991 char *buf, size_t buflen)
2992{
2993 struct hostapd_channel_data *chnl;
2994 int ret, i, j;
2995 char *pos, *end, *hmode;
2996
2997 pos = buf;
2998 end = pos + buflen;
2999
3000 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3001 switch (wpa_s->hw.modes[j].mode) {
3002 case HOSTAPD_MODE_IEEE80211B:
3003 hmode = "B";
3004 break;
3005 case HOSTAPD_MODE_IEEE80211G:
3006 hmode = "G";
3007 break;
3008 case HOSTAPD_MODE_IEEE80211A:
3009 hmode = "A";
3010 break;
3011 case HOSTAPD_MODE_IEEE80211AD:
3012 hmode = "AD";
3013 break;
3014 default:
3015 continue;
3016 }
3017 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3018 hmode);
3019 if (ret < 0 || ret >= end - pos)
3020 return pos - buf;
3021 pos += ret;
3022 chnl = wpa_s->hw.modes[j].channels;
3023 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3024 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3025 continue;
3026 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s\n",
3027 chnl[i].chan, chnl[i].freq,
3028 chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
3029 " (NO_IBSS)" : "");
3030 if (ret < 0 || ret >= end - pos)
3031 return pos - buf;
3032 pos += ret;
3033 }
3034 ret = os_snprintf(pos, end - pos, "\n");
3035 if (ret < 0 || ret >= end - pos)
3036 return pos - buf;
3037 pos += ret;
3038 }
3039
3040 return pos - buf;
3041}
3042
3043
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003044static int wpa_supplicant_ctrl_iface_get_capability(
3045 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3046 size_t buflen)
3047{
3048 struct wpa_driver_capa capa;
3049 int res;
3050 char *strict;
3051 char field[30];
3052 size_t len;
3053
3054 /* Determine whether or not strict checking was requested */
3055 len = os_strlcpy(field, _field, sizeof(field));
3056 if (len >= sizeof(field))
3057 return -1;
3058 strict = os_strchr(field, ' ');
3059 if (strict != NULL) {
3060 *strict++ = '\0';
3061 if (os_strcmp(strict, "strict") != 0)
3062 return -1;
3063 }
3064
3065 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3066 field, strict ? strict : "");
3067
3068 if (os_strcmp(field, "eap") == 0) {
3069 return eap_get_names(buf, buflen);
3070 }
3071
3072 res = wpa_drv_get_capa(wpa_s, &capa);
3073
3074 if (os_strcmp(field, "pairwise") == 0)
3075 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3076 buf, buflen);
3077
3078 if (os_strcmp(field, "group") == 0)
3079 return ctrl_iface_get_capability_group(res, strict, &capa,
3080 buf, buflen);
3081
3082 if (os_strcmp(field, "key_mgmt") == 0)
3083 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3084 buf, buflen);
3085
3086 if (os_strcmp(field, "proto") == 0)
3087 return ctrl_iface_get_capability_proto(res, strict, &capa,
3088 buf, buflen);
3089
3090 if (os_strcmp(field, "auth_alg") == 0)
3091 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
3092 buf, buflen);
3093
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003094 if (os_strcmp(field, "modes") == 0)
3095 return ctrl_iface_get_capability_modes(res, strict, &capa,
3096 buf, buflen);
3097
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003098 if (os_strcmp(field, "channels") == 0)
3099 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3100
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003101 if (os_strcmp(field, "freq") == 0)
3102 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003104 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3105 field);
3106
3107 return -1;
3108}
3109
3110
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003111#ifdef CONFIG_INTERWORKING
3112static char * anqp_add_hex(char *pos, char *end, const char *title,
3113 struct wpabuf *data)
3114{
3115 char *start = pos;
3116 size_t i;
3117 int ret;
3118 const u8 *d;
3119
3120 if (data == NULL)
3121 return start;
3122
3123 ret = os_snprintf(pos, end - pos, "%s=", title);
3124 if (ret < 0 || ret >= end - pos)
3125 return start;
3126 pos += ret;
3127
3128 d = wpabuf_head_u8(data);
3129 for (i = 0; i < wpabuf_len(data); i++) {
3130 ret = os_snprintf(pos, end - pos, "%02x", *d++);
3131 if (ret < 0 || ret >= end - pos)
3132 return start;
3133 pos += ret;
3134 }
3135
3136 ret = os_snprintf(pos, end - pos, "\n");
3137 if (ret < 0 || ret >= end - pos)
3138 return start;
3139 pos += ret;
3140
3141 return pos;
3142}
3143#endif /* CONFIG_INTERWORKING */
3144
3145
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003146static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3147 unsigned long mask, char *buf, size_t buflen)
3148{
3149 size_t i;
3150 int ret;
3151 char *pos, *end;
3152 const u8 *ie, *ie2;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003153
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003154 pos = buf;
3155 end = buf + buflen;
3156
3157 if (mask & WPA_BSS_MASK_ID) {
3158 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
3159 if (ret < 0 || ret >= end - pos)
3160 return 0;
3161 pos += ret;
3162 }
3163
3164 if (mask & WPA_BSS_MASK_BSSID) {
3165 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3166 MAC2STR(bss->bssid));
3167 if (ret < 0 || ret >= end - pos)
3168 return 0;
3169 pos += ret;
3170 }
3171
3172 if (mask & WPA_BSS_MASK_FREQ) {
3173 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
3174 if (ret < 0 || ret >= end - pos)
3175 return 0;
3176 pos += ret;
3177 }
3178
3179 if (mask & WPA_BSS_MASK_BEACON_INT) {
3180 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3181 bss->beacon_int);
3182 if (ret < 0 || ret >= end - pos)
3183 return 0;
3184 pos += ret;
3185 }
3186
3187 if (mask & WPA_BSS_MASK_CAPABILITIES) {
3188 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3189 bss->caps);
3190 if (ret < 0 || ret >= end - pos)
3191 return 0;
3192 pos += ret;
3193 }
3194
3195 if (mask & WPA_BSS_MASK_QUAL) {
3196 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
3197 if (ret < 0 || ret >= end - pos)
3198 return 0;
3199 pos += ret;
3200 }
3201
3202 if (mask & WPA_BSS_MASK_NOISE) {
3203 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
3204 if (ret < 0 || ret >= end - pos)
3205 return 0;
3206 pos += ret;
3207 }
3208
3209 if (mask & WPA_BSS_MASK_LEVEL) {
3210 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
3211 if (ret < 0 || ret >= end - pos)
3212 return 0;
3213 pos += ret;
3214 }
3215
3216 if (mask & WPA_BSS_MASK_TSF) {
3217 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3218 (unsigned long long) bss->tsf);
3219 if (ret < 0 || ret >= end - pos)
3220 return 0;
3221 pos += ret;
3222 }
3223
3224 if (mask & WPA_BSS_MASK_AGE) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003225 struct os_time now;
3226
3227 os_get_time(&now);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003228 ret = os_snprintf(pos, end - pos, "age=%d\n",
3229 (int) (now.sec - bss->last_update.sec));
3230 if (ret < 0 || ret >= end - pos)
3231 return 0;
3232 pos += ret;
3233 }
3234
3235 if (mask & WPA_BSS_MASK_IE) {
3236 ret = os_snprintf(pos, end - pos, "ie=");
3237 if (ret < 0 || ret >= end - pos)
3238 return 0;
3239 pos += ret;
3240
3241 ie = (const u8 *) (bss + 1);
3242 for (i = 0; i < bss->ie_len; i++) {
3243 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
3244 if (ret < 0 || ret >= end - pos)
3245 return 0;
3246 pos += ret;
3247 }
3248
3249 ret = os_snprintf(pos, end - pos, "\n");
3250 if (ret < 0 || ret >= end - pos)
3251 return 0;
3252 pos += ret;
3253 }
3254
3255 if (mask & WPA_BSS_MASK_FLAGS) {
3256 ret = os_snprintf(pos, end - pos, "flags=");
3257 if (ret < 0 || ret >= end - pos)
3258 return 0;
3259 pos += ret;
3260
3261 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3262 if (ie)
3263 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3264 2 + ie[1]);
3265 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3266 if (ie2)
3267 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3268 2 + ie2[1]);
3269 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3270 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3271 ret = os_snprintf(pos, end - pos, "[WEP]");
3272 if (ret < 0 || ret >= end - pos)
3273 return 0;
3274 pos += ret;
3275 }
3276 if (bss->caps & IEEE80211_CAP_IBSS) {
3277 ret = os_snprintf(pos, end - pos, "[IBSS]");
3278 if (ret < 0 || ret >= end - pos)
3279 return 0;
3280 pos += ret;
3281 }
3282 if (bss->caps & IEEE80211_CAP_ESS) {
3283 ret = os_snprintf(pos, end - pos, "[ESS]");
3284 if (ret < 0 || ret >= end - pos)
3285 return 0;
3286 pos += ret;
3287 }
3288 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
3289 ret = os_snprintf(pos, end - pos, "[P2P]");
3290 if (ret < 0 || ret >= end - pos)
3291 return 0;
3292 pos += ret;
3293 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003294#ifdef CONFIG_HS20
3295 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3296 ret = os_snprintf(pos, end - pos, "[HS20]");
3297 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003298 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003299 pos += ret;
3300 }
3301#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003302
3303 ret = os_snprintf(pos, end - pos, "\n");
3304 if (ret < 0 || ret >= end - pos)
3305 return 0;
3306 pos += ret;
3307 }
3308
3309 if (mask & WPA_BSS_MASK_SSID) {
3310 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3311 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3312 if (ret < 0 || ret >= end - pos)
3313 return 0;
3314 pos += ret;
3315 }
3316
3317#ifdef CONFIG_WPS
3318 if (mask & WPA_BSS_MASK_WPS_SCAN) {
3319 ie = (const u8 *) (bss + 1);
3320 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3321 if (ret < 0 || ret >= end - pos)
3322 return 0;
3323 pos += ret;
3324 }
3325#endif /* CONFIG_WPS */
3326
3327#ifdef CONFIG_P2P
3328 if (mask & WPA_BSS_MASK_P2P_SCAN) {
3329 ie = (const u8 *) (bss + 1);
3330 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3331 if (ret < 0 || ret >= end - pos)
3332 return 0;
3333 pos += ret;
3334 }
3335#endif /* CONFIG_P2P */
3336
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003337#ifdef CONFIG_WIFI_DISPLAY
3338 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3339 struct wpabuf *wfd;
3340 ie = (const u8 *) (bss + 1);
3341 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3342 WFD_IE_VENDOR_TYPE);
3343 if (wfd) {
3344 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
3345 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003346 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003347 pos += ret;
3348
3349 pos += wpa_snprintf_hex(pos, end - pos,
3350 wpabuf_head(wfd),
3351 wpabuf_len(wfd));
3352 wpabuf_free(wfd);
3353
3354 ret = os_snprintf(pos, end - pos, "\n");
3355 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003356 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003357 pos += ret;
3358 }
3359 }
3360#endif /* CONFIG_WIFI_DISPLAY */
3361
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003362#ifdef CONFIG_INTERWORKING
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003363 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3364 struct wpa_bss_anqp *anqp = bss->anqp;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003365 pos = anqp_add_hex(pos, end, "anqp_venue_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003366 anqp->venue_name);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003367 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003368 anqp->network_auth_type);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003369 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003370 anqp->roaming_consortium);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003371 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003372 anqp->ip_addr_type_availability);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003373 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003374 anqp->nai_realm);
3375 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003376 pos = anqp_add_hex(pos, end, "anqp_domain_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003377 anqp->domain_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003378#ifdef CONFIG_HS20
3379 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003380 anqp->hs20_operator_friendly_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003381 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003382 anqp->hs20_wan_metrics);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003383 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003384 anqp->hs20_connection_capability);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003385#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003386 }
3387#endif /* CONFIG_INTERWORKING */
3388
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003389 if (mask & WPA_BSS_MASK_DELIM) {
3390 ret = os_snprintf(pos, end - pos, "====\n");
3391 if (ret < 0 || ret >= end - pos)
3392 return 0;
3393 pos += ret;
3394 }
Irfan Sheriffe2ea0082012-08-13 10:56:16 -07003395
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003396 return pos - buf;
3397}
3398
Dmitry Shmidt04949592012-07-19 12:16:46 -07003399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003400static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
3401 const char *cmd, char *buf,
3402 size_t buflen)
3403{
3404 u8 bssid[ETH_ALEN];
3405 size_t i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003406 struct wpa_bss *bss;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003407 struct wpa_bss *bsslast = NULL;
3408 struct dl_list *next;
3409 int ret = 0;
3410 int len;
3411 char *ctmp;
3412 unsigned long mask = WPA_BSS_MASK_ALL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003413
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003414 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
3415 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
3416 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003417 list_id);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003418 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
3419 list_id);
3420 } else { /* N1-N2 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003421 unsigned int id1, id2;
3422
3423 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
3424 wpa_printf(MSG_INFO, "Wrong BSS range "
3425 "format");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003426 return 0;
3427 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003428
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003429 if (*(cmd + 6) == '-')
3430 id1 = 0;
3431 else
3432 id1 = atoi(cmd + 6);
3433 ctmp++;
3434 if (*ctmp >= '0' && *ctmp <= '9')
3435 id2 = atoi(ctmp);
3436 else
3437 id2 = (unsigned int) -1;
3438 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
3439 if (id2 == (unsigned int) -1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003440 bsslast = dl_list_last(&wpa_s->bss_id,
3441 struct wpa_bss,
3442 list_id);
3443 else {
3444 bsslast = wpa_bss_get_id(wpa_s, id2);
3445 if (bsslast == NULL && bss && id2 > id1) {
3446 struct wpa_bss *tmp = bss;
3447 for (;;) {
3448 next = tmp->list_id.next;
3449 if (next == &wpa_s->bss_id)
3450 break;
3451 tmp = dl_list_entry(
3452 next, struct wpa_bss,
3453 list_id);
3454 if (tmp->id > id2)
3455 break;
3456 bsslast = tmp;
3457 }
3458 }
3459 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003460 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003461 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003462 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003463 else if (os_strncmp(cmd, "LAST", 4) == 0)
3464 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003465 else if (os_strncmp(cmd, "ID-", 3) == 0) {
3466 i = atoi(cmd + 3);
3467 bss = wpa_bss_get_id(wpa_s, i);
3468 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3469 i = atoi(cmd + 5);
3470 bss = wpa_bss_get_id(wpa_s, i);
3471 if (bss) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003472 next = bss->list_id.next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003473 if (next == &wpa_s->bss_id)
3474 bss = NULL;
3475 else
3476 bss = dl_list_entry(next, struct wpa_bss,
3477 list_id);
3478 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003479#ifdef CONFIG_P2P
3480 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
3481 if (hwaddr_aton(cmd + 13, bssid) == 0)
3482 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
3483 else
3484 bss = NULL;
3485#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003486 } else if (hwaddr_aton(cmd, bssid) == 0)
3487 bss = wpa_bss_get_bssid(wpa_s, bssid);
3488 else {
3489 struct wpa_bss *tmp;
3490 i = atoi(cmd);
3491 bss = NULL;
3492 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
3493 {
3494 if (i-- == 0) {
3495 bss = tmp;
3496 break;
3497 }
3498 }
3499 }
3500
Dmitry Shmidt04949592012-07-19 12:16:46 -07003501 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
3502 mask = strtoul(ctmp + 5, NULL, 0x10);
3503 if (mask == 0)
3504 mask = WPA_BSS_MASK_ALL;
3505 }
3506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003507 if (bss == NULL)
3508 return 0;
3509
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003510 if (bsslast == NULL)
3511 bsslast = bss;
3512 do {
3513 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
3514 ret += len;
3515 buf += len;
3516 buflen -= len;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003517 if (bss == bsslast) {
3518 if ((mask & WPA_BSS_MASK_DELIM) && len &&
3519 (bss == dl_list_last(&wpa_s->bss_id,
3520 struct wpa_bss, list_id)))
3521 os_snprintf(buf - 5, 5, "####\n");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003522 break;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003523 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003524 next = bss->list_id.next;
3525 if (next == &wpa_s->bss_id)
3526 break;
3527 bss = dl_list_entry(next, struct wpa_bss, list_id);
3528 } while (bss && len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003530 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003531}
3532
3533
3534static int wpa_supplicant_ctrl_iface_ap_scan(
3535 struct wpa_supplicant *wpa_s, char *cmd)
3536{
3537 int ap_scan = atoi(cmd);
3538 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
3539}
3540
3541
3542static int wpa_supplicant_ctrl_iface_scan_interval(
3543 struct wpa_supplicant *wpa_s, char *cmd)
3544{
3545 int scan_int = atoi(cmd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003546 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003547}
3548
3549
3550static int wpa_supplicant_ctrl_iface_bss_expire_age(
3551 struct wpa_supplicant *wpa_s, char *cmd)
3552{
3553 int expire_age = atoi(cmd);
3554 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
3555}
3556
3557
3558static int wpa_supplicant_ctrl_iface_bss_expire_count(
3559 struct wpa_supplicant *wpa_s, char *cmd)
3560{
3561 int expire_count = atoi(cmd);
3562 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
3563}
3564
3565
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07003566static int wpa_supplicant_ctrl_iface_bss_flush(
3567 struct wpa_supplicant *wpa_s, char *cmd)
3568{
3569 int flush_age = atoi(cmd);
3570
3571 if (flush_age == 0)
3572 wpa_bss_flush(wpa_s);
3573 else
3574 wpa_bss_flush_by_age(wpa_s, flush_age);
3575 return 0;
3576}
3577
3578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
3580{
3581 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
3582 /* MLME-DELETEKEYS.request */
3583 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
3584 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
3585 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
3586 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
3587#ifdef CONFIG_IEEE80211W
3588 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
3589 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
3590#endif /* CONFIG_IEEE80211W */
3591
3592 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
3593 0);
3594 /* MLME-SETPROTECTION.request(None) */
3595 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
3596 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
3597 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3598 wpa_sm_drop_sa(wpa_s->wpa);
3599}
3600
3601
3602static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
3603 char *addr)
3604{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003605#ifdef CONFIG_NO_SCAN_PROCESSING
3606 return -1;
3607#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003608 u8 bssid[ETH_ALEN];
3609 struct wpa_bss *bss;
3610 struct wpa_ssid *ssid = wpa_s->current_ssid;
3611
3612 if (hwaddr_aton(addr, bssid)) {
3613 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
3614 "address '%s'", addr);
3615 return -1;
3616 }
3617
3618 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
3619
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003620 if (!ssid) {
3621 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
3622 "configuration known for the target AP");
3623 return -1;
3624 }
3625
3626 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003627 if (!bss) {
3628 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
3629 "from BSS table");
3630 return -1;
3631 }
3632
3633 /*
3634 * TODO: Find best network configuration block from configuration to
3635 * allow roaming to other networks
3636 */
3637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003638 wpa_s->reassociate = 1;
3639 wpa_supplicant_connect(wpa_s, bss, ssid);
3640
3641 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003642#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003643}
3644
3645
3646#ifdef CONFIG_P2P
3647static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
3648{
3649 unsigned int timeout = atoi(cmd);
3650 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003651 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
3652 char *pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003653 unsigned int search_delay;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003654
3655 if (os_strstr(cmd, "type=social"))
3656 type = P2P_FIND_ONLY_SOCIAL;
3657 else if (os_strstr(cmd, "type=progressive"))
3658 type = P2P_FIND_PROGRESSIVE;
3659
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003660 pos = os_strstr(cmd, "dev_id=");
3661 if (pos) {
3662 pos += 7;
3663 if (hwaddr_aton(pos, dev_id))
3664 return -1;
3665 _dev_id = dev_id;
3666 }
3667
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003668 pos = os_strstr(cmd, "delay=");
3669 if (pos) {
3670 pos += 6;
3671 search_delay = atoi(pos);
3672 } else
3673 search_delay = wpas_p2p_search_delay(wpa_s);
3674
3675 return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
3676 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003677}
3678
3679
3680static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
3681 char *buf, size_t buflen)
3682{
3683 u8 addr[ETH_ALEN];
3684 char *pos, *pos2;
3685 char *pin = NULL;
3686 enum p2p_wps_method wps_method;
3687 int new_pin;
3688 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003689 int persistent_group, persistent_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003690 int join;
3691 int auth;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003692 int automatic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003693 int go_intent = -1;
3694 int freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003695 int pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003696 int ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697
Dmitry Shmidt04949592012-07-19 12:16:46 -07003698 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
3699 * [persistent|persistent=<network id>]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003700 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
3701 * [ht40] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003702
3703 if (hwaddr_aton(cmd, addr))
3704 return -1;
3705
3706 pos = cmd + 17;
3707 if (*pos != ' ')
3708 return -1;
3709 pos++;
3710
3711 persistent_group = os_strstr(pos, " persistent") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003712 pos2 = os_strstr(pos, " persistent=");
3713 if (pos2) {
3714 struct wpa_ssid *ssid;
3715 persistent_id = atoi(pos2 + 12);
3716 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3717 if (ssid == NULL || ssid->disabled != 2 ||
3718 ssid->mode != WPAS_MODE_P2P_GO) {
3719 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3720 "SSID id=%d for persistent P2P group (GO)",
3721 persistent_id);
3722 return -1;
3723 }
3724 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003725 join = os_strstr(pos, " join") != NULL;
3726 auth = os_strstr(pos, " auth") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003727 automatic = os_strstr(pos, " auto") != NULL;
3728 pd = os_strstr(pos, " provdisc") != NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003729 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003730
3731 pos2 = os_strstr(pos, " go_intent=");
3732 if (pos2) {
3733 pos2 += 11;
3734 go_intent = atoi(pos2);
3735 if (go_intent < 0 || go_intent > 15)
3736 return -1;
3737 }
3738
3739 pos2 = os_strstr(pos, " freq=");
3740 if (pos2) {
3741 pos2 += 6;
3742 freq = atoi(pos2);
3743 if (freq <= 0)
3744 return -1;
3745 }
3746
3747 if (os_strncmp(pos, "pin", 3) == 0) {
3748 /* Request random PIN (to be displayed) and enable the PIN */
3749 wps_method = WPS_PIN_DISPLAY;
3750 } else if (os_strncmp(pos, "pbc", 3) == 0) {
3751 wps_method = WPS_PBC;
3752 } else {
3753 pin = pos;
3754 pos = os_strchr(pin, ' ');
3755 wps_method = WPS_PIN_KEYPAD;
3756 if (pos) {
3757 *pos++ = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003758 if (os_strncmp(pos, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003759 wps_method = WPS_PIN_DISPLAY;
3760 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003761 if (!wps_pin_str_valid(pin)) {
3762 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
3763 return 17;
3764 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003765 }
3766
3767 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003768 persistent_group, automatic, join,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003769 auth, go_intent, freq, persistent_id, pd,
3770 ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003771 if (new_pin == -2) {
3772 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
3773 return 25;
3774 }
3775 if (new_pin == -3) {
3776 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
3777 return 25;
3778 }
3779 if (new_pin < 0)
3780 return -1;
3781 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
3782 ret = os_snprintf(buf, buflen, "%08d", new_pin);
3783 if (ret < 0 || (size_t) ret >= buflen)
3784 return -1;
3785 return ret;
3786 }
3787
3788 os_memcpy(buf, "OK\n", 3);
3789 return 3;
3790}
3791
3792
3793static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
3794{
3795 unsigned int timeout = atoi(cmd);
3796 return wpas_p2p_listen(wpa_s, timeout);
3797}
3798
3799
3800static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
3801{
3802 u8 addr[ETH_ALEN];
3803 char *pos;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003804 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003805
Dmitry Shmidt04949592012-07-19 12:16:46 -07003806 /* <addr> <config method> [join|auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003807
3808 if (hwaddr_aton(cmd, addr))
3809 return -1;
3810
3811 pos = cmd + 17;
3812 if (*pos != ' ')
3813 return -1;
3814 pos++;
3815
Dmitry Shmidt04949592012-07-19 12:16:46 -07003816 if (os_strstr(pos, " join") != NULL)
3817 use = WPAS_P2P_PD_FOR_JOIN;
3818 else if (os_strstr(pos, " auto") != NULL)
3819 use = WPAS_P2P_PD_AUTO;
3820
3821 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003822}
3823
3824
3825static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
3826 size_t buflen)
3827{
3828 struct wpa_ssid *ssid = wpa_s->current_ssid;
3829
3830 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3831 ssid->passphrase == NULL)
3832 return -1;
3833
3834 os_strlcpy(buf, ssid->passphrase, buflen);
3835 return os_strlen(buf);
3836}
3837
3838
3839static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
3840 char *buf, size_t buflen)
3841{
3842 u64 ref;
3843 int res;
3844 u8 dst_buf[ETH_ALEN], *dst;
3845 struct wpabuf *tlvs;
3846 char *pos;
3847 size_t len;
3848
3849 if (hwaddr_aton(cmd, dst_buf))
3850 return -1;
3851 dst = dst_buf;
3852 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
3853 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
3854 dst = NULL;
3855 pos = cmd + 17;
3856 if (*pos != ' ')
3857 return -1;
3858 pos++;
3859
3860 if (os_strncmp(pos, "upnp ", 5) == 0) {
3861 u8 version;
3862 pos += 5;
3863 if (hexstr2bin(pos, &version, 1) < 0)
3864 return -1;
3865 pos += 2;
3866 if (*pos != ' ')
3867 return -1;
3868 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003869 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003870#ifdef CONFIG_WIFI_DISPLAY
3871 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
3872 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
3873#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003874 } else {
3875 len = os_strlen(pos);
3876 if (len & 1)
3877 return -1;
3878 len /= 2;
3879 tlvs = wpabuf_alloc(len);
3880 if (tlvs == NULL)
3881 return -1;
3882 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
3883 wpabuf_free(tlvs);
3884 return -1;
3885 }
3886
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003887 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 wpabuf_free(tlvs);
3889 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003890 if (ref == 0)
3891 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003892 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
3893 if (res < 0 || (unsigned) res >= buflen)
3894 return -1;
3895 return res;
3896}
3897
3898
3899static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
3900 char *cmd)
3901{
3902 long long unsigned val;
3903 u64 req;
3904 if (sscanf(cmd, "%llx", &val) != 1)
3905 return -1;
3906 req = val;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003907 return wpas_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003908}
3909
3910
3911static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
3912{
3913 int freq;
3914 u8 dst[ETH_ALEN];
3915 u8 dialog_token;
3916 struct wpabuf *resp_tlvs;
3917 char *pos, *pos2;
3918 size_t len;
3919
3920 pos = os_strchr(cmd, ' ');
3921 if (pos == NULL)
3922 return -1;
3923 *pos++ = '\0';
3924 freq = atoi(cmd);
3925 if (freq == 0)
3926 return -1;
3927
3928 if (hwaddr_aton(pos, dst))
3929 return -1;
3930 pos += 17;
3931 if (*pos != ' ')
3932 return -1;
3933 pos++;
3934
3935 pos2 = os_strchr(pos, ' ');
3936 if (pos2 == NULL)
3937 return -1;
3938 *pos2++ = '\0';
3939 dialog_token = atoi(pos);
3940
3941 len = os_strlen(pos2);
3942 if (len & 1)
3943 return -1;
3944 len /= 2;
3945 resp_tlvs = wpabuf_alloc(len);
3946 if (resp_tlvs == NULL)
3947 return -1;
3948 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
3949 wpabuf_free(resp_tlvs);
3950 return -1;
3951 }
3952
3953 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
3954 wpabuf_free(resp_tlvs);
3955 return 0;
3956}
3957
3958
3959static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
3960 char *cmd)
3961{
Dmitry Shmidt04949592012-07-19 12:16:46 -07003962 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
3963 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003964 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
3965 return 0;
3966}
3967
3968
3969static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
3970 char *cmd)
3971{
3972 char *pos;
3973 size_t len;
3974 struct wpabuf *query, *resp;
3975
3976 pos = os_strchr(cmd, ' ');
3977 if (pos == NULL)
3978 return -1;
3979 *pos++ = '\0';
3980
3981 len = os_strlen(cmd);
3982 if (len & 1)
3983 return -1;
3984 len /= 2;
3985 query = wpabuf_alloc(len);
3986 if (query == NULL)
3987 return -1;
3988 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
3989 wpabuf_free(query);
3990 return -1;
3991 }
3992
3993 len = os_strlen(pos);
3994 if (len & 1) {
3995 wpabuf_free(query);
3996 return -1;
3997 }
3998 len /= 2;
3999 resp = wpabuf_alloc(len);
4000 if (resp == NULL) {
4001 wpabuf_free(query);
4002 return -1;
4003 }
4004 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4005 wpabuf_free(query);
4006 wpabuf_free(resp);
4007 return -1;
4008 }
4009
4010 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4011 wpabuf_free(query);
4012 wpabuf_free(resp);
4013 return -1;
4014 }
4015 return 0;
4016}
4017
4018
4019static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4020{
4021 char *pos;
4022 u8 version;
4023
4024 pos = os_strchr(cmd, ' ');
4025 if (pos == NULL)
4026 return -1;
4027 *pos++ = '\0';
4028
4029 if (hexstr2bin(cmd, &version, 1) < 0)
4030 return -1;
4031
4032 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4033}
4034
4035
4036static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4037{
4038 char *pos;
4039
4040 pos = os_strchr(cmd, ' ');
4041 if (pos == NULL)
4042 return -1;
4043 *pos++ = '\0';
4044
4045 if (os_strcmp(cmd, "bonjour") == 0)
4046 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4047 if (os_strcmp(cmd, "upnp") == 0)
4048 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4049 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4050 return -1;
4051}
4052
4053
4054static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4055 char *cmd)
4056{
4057 size_t len;
4058 struct wpabuf *query;
4059 int ret;
4060
4061 len = os_strlen(cmd);
4062 if (len & 1)
4063 return -1;
4064 len /= 2;
4065 query = wpabuf_alloc(len);
4066 if (query == NULL)
4067 return -1;
4068 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4069 wpabuf_free(query);
4070 return -1;
4071 }
4072
4073 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4074 wpabuf_free(query);
4075 return ret;
4076}
4077
4078
4079static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4080{
4081 char *pos;
4082 u8 version;
4083
4084 pos = os_strchr(cmd, ' ');
4085 if (pos == NULL)
4086 return -1;
4087 *pos++ = '\0';
4088
4089 if (hexstr2bin(cmd, &version, 1) < 0)
4090 return -1;
4091
4092 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4093}
4094
4095
4096static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4097{
4098 char *pos;
4099
4100 pos = os_strchr(cmd, ' ');
4101 if (pos == NULL)
4102 return -1;
4103 *pos++ = '\0';
4104
4105 if (os_strcmp(cmd, "bonjour") == 0)
4106 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4107 if (os_strcmp(cmd, "upnp") == 0)
4108 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4109 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4110 return -1;
4111}
4112
4113
4114static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4115{
4116 u8 addr[ETH_ALEN];
4117
4118 /* <addr> */
4119
4120 if (hwaddr_aton(cmd, addr))
4121 return -1;
4122
4123 return wpas_p2p_reject(wpa_s, addr);
4124}
4125
4126
4127static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4128{
4129 char *pos;
4130 int id;
4131 struct wpa_ssid *ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004132 u8 *_peer = NULL, peer[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004133 int freq = 0, pref_freq = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03004134 int ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004135
4136 id = atoi(cmd);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004137 pos = os_strstr(cmd, " peer=");
4138 if (pos) {
4139 pos += 6;
4140 if (hwaddr_aton(pos, peer))
4141 return -1;
4142 _peer = peer;
4143 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144 ssid = wpa_config_get_network(wpa_s->conf, id);
4145 if (ssid == NULL || ssid->disabled != 2) {
4146 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4147 "for persistent P2P group",
4148 id);
4149 return -1;
4150 }
4151
Jouni Malinen31be0a42012-08-31 21:20:51 +03004152 pos = os_strstr(cmd, " freq=");
4153 if (pos) {
4154 pos += 6;
4155 freq = atoi(pos);
4156 if (freq <= 0)
4157 return -1;
4158 }
4159
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004160 pos = os_strstr(cmd, " pref=");
4161 if (pos) {
4162 pos += 6;
4163 pref_freq = atoi(pos);
4164 if (pref_freq <= 0)
4165 return -1;
4166 }
4167
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004168 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
Jouni Malinen31be0a42012-08-31 21:20:51 +03004169
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004170 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004171}
4172
4173
4174static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
4175{
4176 char *pos;
4177 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
4178
4179 pos = os_strstr(cmd, " peer=");
4180 if (!pos)
4181 return -1;
4182
4183 *pos = '\0';
4184 pos += 6;
4185 if (hwaddr_aton(pos, peer)) {
4186 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
4187 return -1;
4188 }
4189
4190 pos = os_strstr(pos, " go_dev_addr=");
4191 if (pos) {
4192 pos += 13;
4193 if (hwaddr_aton(pos, go_dev_addr)) {
4194 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
4195 pos);
4196 return -1;
4197 }
4198 go_dev = go_dev_addr;
4199 }
4200
4201 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
4202}
4203
4204
4205static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
4206{
4207 if (os_strncmp(cmd, "persistent=", 11) == 0)
4208 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
4209 if (os_strncmp(cmd, "group=", 6) == 0)
4210 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
4211
4212 return -1;
4213}
4214
4215
4216static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004217 char *cmd, int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004218{
4219 int id;
4220 struct wpa_ssid *ssid;
4221
4222 id = atoi(cmd);
4223 ssid = wpa_config_get_network(wpa_s->conf, id);
4224 if (ssid == NULL || ssid->disabled != 2) {
4225 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4226 "for persistent P2P group",
4227 id);
4228 return -1;
4229 }
4230
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004231 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004232}
4233
4234
4235static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
4236{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004237 int freq = 0, ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004238 char *pos;
4239
4240 pos = os_strstr(cmd, "freq=");
4241 if (pos)
4242 freq = atoi(pos + 5);
4243
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004244 ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004245
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004246 if (os_strncmp(cmd, "persistent=", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004247 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
4248 ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004249 if (os_strcmp(cmd, "persistent") == 0 ||
4250 os_strncmp(cmd, "persistent ", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004251 return wpas_p2p_group_add(wpa_s, 1, freq, ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004252 if (os_strncmp(cmd, "freq=", 5) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004253 return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
4254 if (ht40)
4255 return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004256
4257 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
4258 cmd);
4259 return -1;
4260}
4261
4262
4263static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
4264 char *buf, size_t buflen)
4265{
4266 u8 addr[ETH_ALEN], *addr_ptr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004267 int next, res;
4268 const struct p2p_peer_info *info;
4269 char *pos, *end;
4270 char devtype[WPS_DEV_TYPE_BUFSIZE];
4271 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004272 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004273
4274 if (!wpa_s->global->p2p)
4275 return -1;
4276
4277 if (os_strcmp(cmd, "FIRST") == 0) {
4278 addr_ptr = NULL;
4279 next = 0;
4280 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4281 if (hwaddr_aton(cmd + 5, addr) < 0)
4282 return -1;
4283 addr_ptr = addr;
4284 next = 1;
4285 } else {
4286 if (hwaddr_aton(cmd, addr) < 0)
4287 return -1;
4288 addr_ptr = addr;
4289 next = 0;
4290 }
4291
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004292 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4293 if (info == NULL)
4294 return -1;
4295
4296 pos = buf;
4297 end = buf + buflen;
4298
4299 res = os_snprintf(pos, end - pos, MACSTR "\n"
4300 "pri_dev_type=%s\n"
4301 "device_name=%s\n"
4302 "manufacturer=%s\n"
4303 "model_name=%s\n"
4304 "model_number=%s\n"
4305 "serial_number=%s\n"
4306 "config_methods=0x%x\n"
4307 "dev_capab=0x%x\n"
4308 "group_capab=0x%x\n"
4309 "level=%d\n",
4310 MAC2STR(info->p2p_device_addr),
4311 wps_dev_type_bin2str(info->pri_dev_type,
4312 devtype, sizeof(devtype)),
4313 info->device_name,
4314 info->manufacturer,
4315 info->model_name,
4316 info->model_number,
4317 info->serial_number,
4318 info->config_methods,
4319 info->dev_capab,
4320 info->group_capab,
4321 info->level);
4322 if (res < 0 || res >= end - pos)
4323 return pos - buf;
4324 pos += res;
4325
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004326 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4327 {
4328 const u8 *t;
4329 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4330 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4331 wps_dev_type_bin2str(t, devtype,
4332 sizeof(devtype)));
4333 if (res < 0 || res >= end - pos)
4334 return pos - buf;
4335 pos += res;
4336 }
4337
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004338 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004339 if (ssid) {
4340 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
4341 if (res < 0 || res >= end - pos)
4342 return pos - buf;
4343 pos += res;
4344 }
4345
4346 res = p2p_get_peer_info_txt(info, pos, end - pos);
4347 if (res < 0)
4348 return pos - buf;
4349 pos += res;
4350
4351 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004352}
4353
4354
Dmitry Shmidt04949592012-07-19 12:16:46 -07004355static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
4356 const char *param)
4357{
4358 struct wpa_freq_range *freq = NULL, *n;
4359 unsigned int count = 0, i;
4360 const char *pos, *pos2, *pos3;
4361
4362 if (wpa_s->global->p2p == NULL)
4363 return -1;
4364
4365 /*
4366 * param includes comma separated frequency range.
4367 * For example: 2412-2432,2462,5000-6000
4368 */
4369 pos = param;
4370 while (pos && pos[0]) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004371 n = os_realloc_array(freq, count + 1,
4372 sizeof(struct wpa_freq_range));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004373 if (n == NULL) {
4374 os_free(freq);
4375 return -1;
4376 }
4377 freq = n;
4378 freq[count].min = atoi(pos);
4379 pos2 = os_strchr(pos, '-');
4380 pos3 = os_strchr(pos, ',');
4381 if (pos2 && (!pos3 || pos2 < pos3)) {
4382 pos2++;
4383 freq[count].max = atoi(pos2);
4384 } else
4385 freq[count].max = freq[count].min;
4386 pos = pos3;
4387 if (pos)
4388 pos++;
4389 count++;
4390 }
4391
4392 for (i = 0; i < count; i++) {
4393 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
4394 freq[i].min, freq[i].max);
4395 }
4396
4397 os_free(wpa_s->global->p2p_disallow_freq);
4398 wpa_s->global->p2p_disallow_freq = freq;
4399 wpa_s->global->num_p2p_disallow_freq = count;
4400 wpas_p2p_update_channel_list(wpa_s);
4401 return 0;
4402}
4403
4404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004405static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
4406{
4407 char *param;
4408
4409 if (wpa_s->global->p2p == NULL)
4410 return -1;
4411
4412 param = os_strchr(cmd, ' ');
4413 if (param == NULL)
4414 return -1;
4415 *param++ = '\0';
4416
4417 if (os_strcmp(cmd, "discoverability") == 0) {
4418 p2p_set_client_discoverability(wpa_s->global->p2p,
4419 atoi(param));
4420 return 0;
4421 }
4422
4423 if (os_strcmp(cmd, "managed") == 0) {
4424 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
4425 return 0;
4426 }
4427
4428 if (os_strcmp(cmd, "listen_channel") == 0) {
4429 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
4430 atoi(param));
4431 }
4432
4433 if (os_strcmp(cmd, "ssid_postfix") == 0) {
4434 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
4435 os_strlen(param));
4436 }
4437
4438 if (os_strcmp(cmd, "noa") == 0) {
4439 char *pos;
4440 int count, start, duration;
4441 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
4442 count = atoi(param);
4443 pos = os_strchr(param, ',');
4444 if (pos == NULL)
4445 return -1;
4446 pos++;
4447 start = atoi(pos);
4448 pos = os_strchr(pos, ',');
4449 if (pos == NULL)
4450 return -1;
4451 pos++;
4452 duration = atoi(pos);
4453 if (count < 0 || count > 255 || start < 0 || duration < 0)
4454 return -1;
4455 if (count == 0 && duration > 0)
4456 return -1;
4457 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
4458 "start=%d duration=%d", count, start, duration);
4459 return wpas_p2p_set_noa(wpa_s, count, start, duration);
4460 }
4461
4462 if (os_strcmp(cmd, "ps") == 0)
4463 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
4464
4465 if (os_strcmp(cmd, "oppps") == 0)
4466 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
4467
4468 if (os_strcmp(cmd, "ctwindow") == 0)
4469 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
4470
4471 if (os_strcmp(cmd, "disabled") == 0) {
4472 wpa_s->global->p2p_disabled = atoi(param);
4473 wpa_printf(MSG_DEBUG, "P2P functionality %s",
4474 wpa_s->global->p2p_disabled ?
4475 "disabled" : "enabled");
4476 if (wpa_s->global->p2p_disabled) {
4477 wpas_p2p_stop_find(wpa_s);
4478 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4479 p2p_flush(wpa_s->global->p2p);
4480 }
4481 return 0;
4482 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004483
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004484 if (os_strcmp(cmd, "conc_pref") == 0) {
4485 if (os_strcmp(param, "sta") == 0)
4486 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
4487 else if (os_strcmp(param, "p2p") == 0)
4488 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004489 else {
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004490 wpa_printf(MSG_INFO, "Invalid conc_pref value");
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004491 return -1;
4492 }
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004493 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004494 "%s", param);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004495 return 0;
4496 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004498 if (os_strcmp(cmd, "force_long_sd") == 0) {
4499 wpa_s->force_long_sd = atoi(param);
4500 return 0;
4501 }
4502
4503 if (os_strcmp(cmd, "peer_filter") == 0) {
4504 u8 addr[ETH_ALEN];
4505 if (hwaddr_aton(param, addr))
4506 return -1;
4507 p2p_set_peer_filter(wpa_s->global->p2p, addr);
4508 return 0;
4509 }
4510
4511 if (os_strcmp(cmd, "cross_connect") == 0)
4512 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
4513
4514 if (os_strcmp(cmd, "go_apsd") == 0) {
4515 if (os_strcmp(param, "disable") == 0)
4516 wpa_s->set_ap_uapsd = 0;
4517 else {
4518 wpa_s->set_ap_uapsd = 1;
4519 wpa_s->ap_uapsd = atoi(param);
4520 }
4521 return 0;
4522 }
4523
4524 if (os_strcmp(cmd, "client_apsd") == 0) {
4525 if (os_strcmp(param, "disable") == 0)
4526 wpa_s->set_sta_uapsd = 0;
4527 else {
4528 int be, bk, vi, vo;
4529 char *pos;
4530 /* format: BE,BK,VI,VO;max SP Length */
4531 be = atoi(param);
4532 pos = os_strchr(param, ',');
4533 if (pos == NULL)
4534 return -1;
4535 pos++;
4536 bk = atoi(pos);
4537 pos = os_strchr(pos, ',');
4538 if (pos == NULL)
4539 return -1;
4540 pos++;
4541 vi = atoi(pos);
4542 pos = os_strchr(pos, ',');
4543 if (pos == NULL)
4544 return -1;
4545 pos++;
4546 vo = atoi(pos);
4547 /* ignore max SP Length for now */
4548
4549 wpa_s->set_sta_uapsd = 1;
4550 wpa_s->sta_uapsd = 0;
4551 if (be)
4552 wpa_s->sta_uapsd |= BIT(0);
4553 if (bk)
4554 wpa_s->sta_uapsd |= BIT(1);
4555 if (vi)
4556 wpa_s->sta_uapsd |= BIT(2);
4557 if (vo)
4558 wpa_s->sta_uapsd |= BIT(3);
4559 }
4560 return 0;
4561 }
4562
Dmitry Shmidt04949592012-07-19 12:16:46 -07004563 if (os_strcmp(cmd, "disallow_freq") == 0)
4564 return p2p_ctrl_disallow_freq(wpa_s, param);
4565
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004566 if (os_strcmp(cmd, "disc_int") == 0) {
4567 int min_disc_int, max_disc_int, max_disc_tu;
4568 char *pos;
4569
4570 pos = param;
4571
4572 min_disc_int = atoi(pos);
4573 pos = os_strchr(pos, ' ');
4574 if (pos == NULL)
4575 return -1;
4576 *pos++ = '\0';
4577
4578 max_disc_int = atoi(pos);
4579 pos = os_strchr(pos, ' ');
4580 if (pos == NULL)
4581 return -1;
4582 *pos++ = '\0';
4583
4584 max_disc_tu = atoi(pos);
4585
4586 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
4587 max_disc_int, max_disc_tu);
4588 }
4589
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004590 if (os_strcmp(cmd, "per_sta_psk") == 0) {
4591 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
4592 return 0;
4593 }
4594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004595 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
4596 cmd);
4597
4598 return -1;
4599}
4600
4601
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004602static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
4603{
4604 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4605 wpa_s->force_long_sd = 0;
4606 if (wpa_s->global->p2p)
4607 p2p_flush(wpa_s->global->p2p);
4608}
4609
4610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
4612{
4613 char *pos, *pos2;
4614 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
4615
4616 if (cmd[0]) {
4617 pos = os_strchr(cmd, ' ');
4618 if (pos == NULL)
4619 return -1;
4620 *pos++ = '\0';
4621 dur1 = atoi(cmd);
4622
4623 pos2 = os_strchr(pos, ' ');
4624 if (pos2)
4625 *pos2++ = '\0';
4626 int1 = atoi(pos);
4627 } else
4628 pos2 = NULL;
4629
4630 if (pos2) {
4631 pos = os_strchr(pos2, ' ');
4632 if (pos == NULL)
4633 return -1;
4634 *pos++ = '\0';
4635 dur2 = atoi(pos2);
4636 int2 = atoi(pos);
4637 }
4638
4639 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
4640}
4641
4642
4643static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
4644{
4645 char *pos;
4646 unsigned int period = 0, interval = 0;
4647
4648 if (cmd[0]) {
4649 pos = os_strchr(cmd, ' ');
4650 if (pos == NULL)
4651 return -1;
4652 *pos++ = '\0';
4653 period = atoi(cmd);
4654 interval = atoi(pos);
4655 }
4656
4657 return wpas_p2p_ext_listen(wpa_s, period, interval);
4658}
4659
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004660
4661static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
4662{
4663 const char *pos;
4664 u8 peer[ETH_ALEN];
4665 int iface_addr = 0;
4666
4667 pos = cmd;
4668 if (os_strncmp(pos, "iface=", 6) == 0) {
4669 iface_addr = 1;
4670 pos += 6;
4671 }
4672 if (hwaddr_aton(pos, peer))
4673 return -1;
4674
4675 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
4676 return 0;
4677}
4678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004679#endif /* CONFIG_P2P */
4680
4681
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004682#ifdef CONFIG_INTERWORKING
4683static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
4684{
4685 u8 bssid[ETH_ALEN];
4686 struct wpa_bss *bss;
4687
4688 if (hwaddr_aton(dst, bssid)) {
4689 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
4690 return -1;
4691 }
4692
4693 bss = wpa_bss_get_bssid(wpa_s, bssid);
4694 if (bss == NULL) {
4695 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
4696 MAC2STR(bssid));
4697 return -1;
4698 }
4699
4700 return interworking_connect(wpa_s, bss);
4701}
4702
4703
4704static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
4705{
4706 u8 dst_addr[ETH_ALEN];
4707 int used;
4708 char *pos;
4709#define MAX_ANQP_INFO_ID 100
4710 u16 id[MAX_ANQP_INFO_ID];
4711 size_t num_id = 0;
4712
4713 used = hwaddr_aton2(dst, dst_addr);
4714 if (used < 0)
4715 return -1;
4716 pos = dst + used;
4717 while (num_id < MAX_ANQP_INFO_ID) {
4718 id[num_id] = atoi(pos);
4719 if (id[num_id])
4720 num_id++;
4721 pos = os_strchr(pos + 1, ',');
4722 if (pos == NULL)
4723 break;
4724 pos++;
4725 }
4726
4727 if (num_id == 0)
4728 return -1;
4729
4730 return anqp_send_req(wpa_s, dst_addr, id, num_id);
4731}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004732
4733
4734static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
4735{
4736 u8 dst_addr[ETH_ALEN];
4737 struct wpabuf *advproto, *query = NULL;
4738 int used, ret = -1;
4739 char *pos, *end;
4740 size_t len;
4741
4742 used = hwaddr_aton2(cmd, dst_addr);
4743 if (used < 0)
4744 return -1;
4745
4746 pos = cmd + used;
4747 while (*pos == ' ')
4748 pos++;
4749
4750 /* Advertisement Protocol ID */
4751 end = os_strchr(pos, ' ');
4752 if (end)
4753 len = end - pos;
4754 else
4755 len = os_strlen(pos);
4756 if (len & 0x01)
4757 return -1;
4758 len /= 2;
4759 if (len == 0)
4760 return -1;
4761 advproto = wpabuf_alloc(len);
4762 if (advproto == NULL)
4763 return -1;
4764 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
4765 goto fail;
4766
4767 if (end) {
4768 /* Optional Query Request */
4769 pos = end + 1;
4770 while (*pos == ' ')
4771 pos++;
4772
4773 len = os_strlen(pos);
4774 if (len) {
4775 if (len & 0x01)
4776 goto fail;
4777 len /= 2;
4778 if (len == 0)
4779 goto fail;
4780 query = wpabuf_alloc(len);
4781 if (query == NULL)
4782 goto fail;
4783 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
4784 goto fail;
4785 }
4786 }
4787
4788 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
4789
4790fail:
4791 wpabuf_free(advproto);
4792 wpabuf_free(query);
4793
4794 return ret;
4795}
4796
4797
4798static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
4799 size_t buflen)
4800{
4801 u8 addr[ETH_ALEN];
4802 int dialog_token;
4803 int used;
4804 char *pos;
4805 size_t resp_len, start, requested_len;
4806
4807 if (!wpa_s->last_gas_resp)
4808 return -1;
4809
4810 used = hwaddr_aton2(cmd, addr);
4811 if (used < 0)
4812 return -1;
4813
4814 pos = cmd + used;
4815 while (*pos == ' ')
4816 pos++;
4817 dialog_token = atoi(pos);
4818
4819 if (os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) != 0 ||
4820 dialog_token != wpa_s->last_gas_dialog_token)
4821 return -1;
4822
4823 resp_len = wpabuf_len(wpa_s->last_gas_resp);
4824 start = 0;
4825 requested_len = resp_len;
4826
4827 pos = os_strchr(pos, ' ');
4828 if (pos) {
4829 start = atoi(pos);
4830 if (start > resp_len)
4831 return os_snprintf(buf, buflen, "FAIL-Invalid range");
4832 pos = os_strchr(pos, ',');
4833 if (pos == NULL)
4834 return -1;
4835 pos++;
4836 requested_len = atoi(pos);
4837 if (start + requested_len > resp_len)
4838 return os_snprintf(buf, buflen, "FAIL-Invalid range");
4839 }
4840
4841 if (requested_len * 2 + 1 > buflen)
4842 return os_snprintf(buf, buflen, "FAIL-Too long response");
4843
4844 return wpa_snprintf_hex(buf, buflen,
4845 wpabuf_head_u8(wpa_s->last_gas_resp) + start,
4846 requested_len);
4847}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004848#endif /* CONFIG_INTERWORKING */
4849
4850
Dmitry Shmidt04949592012-07-19 12:16:46 -07004851#ifdef CONFIG_HS20
4852
4853static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
4854{
4855 u8 dst_addr[ETH_ALEN];
4856 int used;
4857 char *pos;
4858 u32 subtypes = 0;
4859
4860 used = hwaddr_aton2(dst, dst_addr);
4861 if (used < 0)
4862 return -1;
4863 pos = dst + used;
4864 for (;;) {
4865 int num = atoi(pos);
4866 if (num <= 0 || num > 31)
4867 return -1;
4868 subtypes |= BIT(num);
4869 pos = os_strchr(pos + 1, ',');
4870 if (pos == NULL)
4871 break;
4872 pos++;
4873 }
4874
4875 if (subtypes == 0)
4876 return -1;
4877
4878 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
4879}
4880
4881
4882static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4883 const u8 *addr, const char *realm)
4884{
4885 u8 *buf;
4886 size_t rlen, len;
4887 int ret;
4888
4889 rlen = os_strlen(realm);
4890 len = 3 + rlen;
4891 buf = os_malloc(len);
4892 if (buf == NULL)
4893 return -1;
4894 buf[0] = 1; /* NAI Home Realm Count */
4895 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
4896 buf[2] = rlen;
4897 os_memcpy(buf + 3, realm, rlen);
4898
4899 ret = hs20_anqp_send_req(wpa_s, addr,
4900 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4901 buf, len);
4902
4903 os_free(buf);
4904
4905 return ret;
4906}
4907
4908
4909static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4910 char *dst)
4911{
4912 struct wpa_cred *cred = wpa_s->conf->cred;
4913 u8 dst_addr[ETH_ALEN];
4914 int used;
4915 u8 *buf;
4916 size_t len;
4917 int ret;
4918
4919 used = hwaddr_aton2(dst, dst_addr);
4920 if (used < 0)
4921 return -1;
4922
4923 while (dst[used] == ' ')
4924 used++;
4925 if (os_strncmp(dst + used, "realm=", 6) == 0)
4926 return hs20_nai_home_realm_list(wpa_s, dst_addr,
4927 dst + used + 6);
4928
4929 len = os_strlen(dst + used);
4930
4931 if (len == 0 && cred && cred->realm)
4932 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
4933
4934 if (len % 1)
4935 return -1;
4936 len /= 2;
4937 buf = os_malloc(len);
4938 if (buf == NULL)
4939 return -1;
4940 if (hexstr2bin(dst + used, buf, len) < 0) {
4941 os_free(buf);
4942 return -1;
4943 }
4944
4945 ret = hs20_anqp_send_req(wpa_s, dst_addr,
4946 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4947 buf, len);
4948 os_free(buf);
4949
4950 return ret;
4951}
4952
4953#endif /* CONFIG_HS20 */
4954
4955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004956static int wpa_supplicant_ctrl_iface_sta_autoconnect(
4957 struct wpa_supplicant *wpa_s, char *cmd)
4958{
4959 wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
4960 return 0;
4961}
4962
4963
Dmitry Shmidt04949592012-07-19 12:16:46 -07004964#ifdef CONFIG_AUTOSCAN
4965
4966static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
4967 char *cmd)
4968{
4969 enum wpa_states state = wpa_s->wpa_state;
4970 char *new_params = NULL;
4971
4972 if (os_strlen(cmd) > 0) {
4973 new_params = os_strdup(cmd);
4974 if (new_params == NULL)
4975 return -1;
4976 }
4977
4978 os_free(wpa_s->conf->autoscan);
4979 wpa_s->conf->autoscan = new_params;
4980
4981 if (wpa_s->conf->autoscan == NULL)
4982 autoscan_deinit(wpa_s);
4983 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
4984 autoscan_init(wpa_s, 1);
4985 else if (state == WPA_SCANNING)
4986 wpa_supplicant_reinit_autoscan(wpa_s);
4987
4988 return 0;
4989}
4990
4991#endif /* CONFIG_AUTOSCAN */
4992
4993
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004994#ifdef CONFIG_WNM
4995
4996static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
4997{
4998 int enter;
4999 int intval = 0;
5000 char *pos;
5001 int ret;
5002 struct wpabuf *tfs_req = NULL;
5003
5004 if (os_strncmp(cmd, "enter", 5) == 0)
5005 enter = 1;
5006 else if (os_strncmp(cmd, "exit", 4) == 0)
5007 enter = 0;
5008 else
5009 return -1;
5010
5011 pos = os_strstr(cmd, " interval=");
5012 if (pos)
5013 intval = atoi(pos + 10);
5014
5015 pos = os_strstr(cmd, " tfs_req=");
5016 if (pos) {
5017 char *end;
5018 size_t len;
5019 pos += 9;
5020 end = os_strchr(pos, ' ');
5021 if (end)
5022 len = end - pos;
5023 else
5024 len = os_strlen(pos);
5025 if (len & 1)
5026 return -1;
5027 len /= 2;
5028 tfs_req = wpabuf_alloc(len);
5029 if (tfs_req == NULL)
5030 return -1;
5031 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
5032 wpabuf_free(tfs_req);
5033 return -1;
5034 }
5035 }
5036
5037 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
5038 WNM_SLEEP_MODE_EXIT, intval,
5039 tfs_req);
5040 wpabuf_free(tfs_req);
5041
5042 return ret;
5043}
5044
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005045
5046static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
5047{
5048 int query_reason;
5049
5050 query_reason = atoi(cmd);
5051
5052 wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
5053 query_reason);
5054
5055 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
5056}
5057
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005058#endif /* CONFIG_WNM */
5059
5060
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005061/* Get string representation of channel width */
5062static const char * channel_width_name(enum chan_width width)
5063{
5064 switch (width) {
5065 case CHAN_WIDTH_20_NOHT:
5066 return "20 MHz (no HT)";
5067 case CHAN_WIDTH_20:
5068 return "20 MHz";
5069 case CHAN_WIDTH_40:
5070 return "40 MHz";
5071 case CHAN_WIDTH_80:
5072 return "80 MHz";
5073 case CHAN_WIDTH_80P80:
5074 return "80+80 MHz";
5075 case CHAN_WIDTH_160:
5076 return "160 MHz";
5077 default:
5078 return "unknown";
5079 }
5080}
5081
5082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005083static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
5084 size_t buflen)
5085{
5086 struct wpa_signal_info si;
5087 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005088 char *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005089
5090 ret = wpa_drv_signal_poll(wpa_s, &si);
5091 if (ret)
5092 return -1;
5093
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005094 pos = buf;
5095 end = buf + buflen;
5096
5097 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098 "NOISE=%d\nFREQUENCY=%u\n",
5099 si.current_signal, si.current_txrate / 1000,
5100 si.current_noise, si.frequency);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005101 if (ret < 0 || ret > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005103 pos += ret;
5104
5105 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
5106 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
5107 channel_width_name(si.chanwidth));
5108 if (ret < 0 || ret > end - pos)
5109 return -1;
5110 pos += ret;
5111 }
5112
5113 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
5114 ret = os_snprintf(pos, end - pos,
5115 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
5116 si.center_frq1, si.center_frq2);
5117 if (ret < 0 || ret > end - pos)
5118 return -1;
5119 pos += ret;
5120 }
5121
5122 if (si.avg_signal) {
5123 ret = os_snprintf(pos, end - pos,
5124 "AVG_RSSI=%d\n", si.avg_signal);
5125 if (ret < 0 || ret >= end - pos)
5126 return -1;
5127 pos += ret;
5128 }
5129
5130 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005131}
5132
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005133
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005134static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
5135 size_t buflen)
5136{
5137 struct hostap_sta_driver_data sta;
5138 int ret;
5139
5140 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
5141 if (ret)
5142 return -1;
5143
5144 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005145 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
5146 if (ret < 0 || (size_t) ret > buflen)
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005147 return -1;
5148 return ret;
5149}
5150
5151
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005152#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005153static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5154 char *buf, size_t buflen)
5155{
5156 int ret;
5157
5158 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005159 if (ret == 0) {
5160 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
5161 struct p2p_data *p2p = wpa_s->global->p2p;
5162 if (p2p) {
5163 char country[3];
5164 country[0] = cmd[8];
5165 country[1] = cmd[9];
5166 country[2] = 0x04;
5167 p2p_set_country(p2p, country);
5168 }
5169 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005170 ret = sprintf(buf, "%s\n", "OK");
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005171 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005172 return ret;
5173}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005174#endif
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005175
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07005176
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005177static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
5178{
5179 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
5180
5181#ifdef CONFIG_P2P
5182 wpas_p2p_stop_find(wpa_s);
5183 p2p_ctrl_flush(wpa_s);
5184 wpas_p2p_group_remove(wpa_s, "*");
5185#endif /* CONFIG_P2P */
5186
5187#ifdef CONFIG_WPS_TESTING
5188 wps_version_number = 0x20;
5189 wps_testing_dummy_cred = 0;
5190#endif /* CONFIG_WPS_TESTING */
5191#ifdef CONFIG_WPS
5192 wpas_wps_cancel(wpa_s);
5193#endif /* CONFIG_WPS */
5194
5195#ifdef CONFIG_TDLS_TESTING
5196 extern unsigned int tdls_testing;
5197 tdls_testing = 0;
5198#endif /* CONFIG_TDLS_TESTING */
5199#ifdef CONFIG_TDLS
5200 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
5201 wpa_tdls_enable(wpa_s->wpa, 1);
5202#endif /* CONFIG_TDLS */
5203
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005204 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
5205 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
5206
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005207 wpa_s->no_keep_alive = 0;
5208
5209 os_free(wpa_s->disallow_aps_bssid);
5210 wpa_s->disallow_aps_bssid = NULL;
5211 wpa_s->disallow_aps_bssid_count = 0;
5212 os_free(wpa_s->disallow_aps_ssid);
5213 wpa_s->disallow_aps_ssid = NULL;
5214 wpa_s->disallow_aps_ssid_count = 0;
5215
5216 wpa_s->set_sta_uapsd = 0;
5217 wpa_s->sta_uapsd = 0;
5218
5219 wpa_drv_radio_disable(wpa_s, 0);
5220
5221 wpa_bss_flush(wpa_s);
5222 wpa_blacklist_clear(wpa_s);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07005223 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005224 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
5225 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
5226}
5227
5228
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005229char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
5230 char *buf, size_t *resp_len)
5231{
5232 char *reply;
5233 const int reply_size = 4096;
5234 int ctrl_rsp = 0;
5235 int reply_len;
5236
5237 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005238 os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
5239 os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005240 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005241 os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005242 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
5243 (const u8 *) buf, os_strlen(buf));
5244 } else {
5245 int level = MSG_DEBUG;
5246 if (os_strcmp(buf, "PING") == 0)
5247 level = MSG_EXCESSIVE;
5248 wpa_hexdump_ascii(level, "RX ctrl_iface",
5249 (const u8 *) buf, os_strlen(buf));
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005250 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005251 }
5252
5253 reply = os_malloc(reply_size);
5254 if (reply == NULL) {
5255 *resp_len = 1;
5256 return NULL;
5257 }
5258
5259 os_memcpy(reply, "OK\n", 3);
5260 reply_len = 3;
5261
5262 if (os_strcmp(buf, "PING") == 0) {
5263 os_memcpy(reply, "PONG\n", 5);
5264 reply_len = 5;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005265 } else if (os_strcmp(buf, "IFNAME") == 0) {
5266 reply_len = os_strlen(wpa_s->ifname);
5267 os_memcpy(reply, wpa_s->ifname, reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005268 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
5269 if (wpa_debug_reopen_file() < 0)
5270 reply_len = -1;
5271 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
5272 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
5273 } else if (os_strcmp(buf, "MIB") == 0) {
5274 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
5275 if (reply_len >= 0) {
5276 int res;
5277 res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
5278 reply_size - reply_len);
5279 if (res < 0)
5280 reply_len = -1;
5281 else
5282 reply_len += res;
5283 }
5284 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
5285 reply_len = wpa_supplicant_ctrl_iface_status(
5286 wpa_s, buf + 6, reply, reply_size);
5287 } else if (os_strcmp(buf, "PMKSA") == 0) {
5288 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
5289 reply_size);
5290 } else if (os_strncmp(buf, "SET ", 4) == 0) {
5291 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
5292 reply_len = -1;
5293 } else if (os_strncmp(buf, "GET ", 4) == 0) {
5294 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
5295 reply, reply_size);
5296 } else if (os_strcmp(buf, "LOGON") == 0) {
5297 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5298 } else if (os_strcmp(buf, "LOGOFF") == 0) {
5299 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
5300 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
5301 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5302 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005303 else
5304 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005305 } else if (os_strcmp(buf, "RECONNECT") == 0) {
5306 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5307 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005308 else if (wpa_s->disconnected)
5309 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005310#ifdef IEEE8021X_EAPOL
5311 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
5312 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
5313 reply_len = -1;
5314#endif /* IEEE8021X_EAPOL */
5315#ifdef CONFIG_PEERKEY
5316 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
5317 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
5318 reply_len = -1;
5319#endif /* CONFIG_PEERKEY */
5320#ifdef CONFIG_IEEE80211R
5321 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
5322 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
5323 reply_len = -1;
5324#endif /* CONFIG_IEEE80211R */
5325#ifdef CONFIG_WPS
5326 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
5327 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
5328 if (res == -2) {
5329 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5330 reply_len = 17;
5331 } else if (res)
5332 reply_len = -1;
5333 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
5334 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
5335 if (res == -2) {
5336 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5337 reply_len = 17;
5338 } else if (res)
5339 reply_len = -1;
5340 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
5341 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
5342 reply,
5343 reply_size);
5344 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
5345 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
5346 wpa_s, buf + 14, reply, reply_size);
5347 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
5348 if (wpas_wps_cancel(wpa_s))
5349 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005350#ifdef CONFIG_WPS_NFC
5351 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
5352 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
5353 reply_len = -1;
5354 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
5355 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
5356 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005357 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
5358 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
5359 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005360 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
5361 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
5362 wpa_s, buf + 14, reply, reply_size);
5363 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
5364 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
5365 buf + 17))
5366 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005367 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
5368 reply_len = wpas_ctrl_nfc_get_handover_req(
5369 wpa_s, buf + 21, reply, reply_size);
5370 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
5371 reply_len = wpas_ctrl_nfc_get_handover_sel(
5372 wpa_s, buf + 21, reply, reply_size);
5373 } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
5374 reply_len = wpas_ctrl_nfc_rx_handover_req(
5375 wpa_s, buf + 20, reply, reply_size);
5376 } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
5377 if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
5378 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005379 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
5380 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
5381 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005382#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005383 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
5384 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
5385 reply_len = -1;
5386#ifdef CONFIG_AP
5387 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
5388 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
5389 wpa_s, buf + 11, reply, reply_size);
5390#endif /* CONFIG_AP */
5391#ifdef CONFIG_WPS_ER
5392 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
5393 if (wpas_wps_er_start(wpa_s, NULL))
5394 reply_len = -1;
5395 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
5396 if (wpas_wps_er_start(wpa_s, buf + 13))
5397 reply_len = -1;
5398 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
5399 if (wpas_wps_er_stop(wpa_s))
5400 reply_len = -1;
5401 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
5402 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
5403 reply_len = -1;
5404 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
5405 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
5406 if (ret == -2) {
5407 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5408 reply_len = 17;
5409 } else if (ret == -3) {
5410 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
5411 reply_len = 18;
5412 } else if (ret == -4) {
5413 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
5414 reply_len = 20;
5415 } else if (ret)
5416 reply_len = -1;
5417 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
5418 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
5419 reply_len = -1;
5420 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
5421 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
5422 buf + 18))
5423 reply_len = -1;
5424 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
5425 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
5426 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005427#ifdef CONFIG_WPS_NFC
5428 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
5429 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
5430 wpa_s, buf + 24, reply, reply_size);
5431#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005432#endif /* CONFIG_WPS_ER */
5433#endif /* CONFIG_WPS */
5434#ifdef CONFIG_IBSS_RSN
5435 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
5436 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
5437 reply_len = -1;
5438#endif /* CONFIG_IBSS_RSN */
5439#ifdef CONFIG_P2P
5440 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
5441 if (p2p_ctrl_find(wpa_s, buf + 9))
5442 reply_len = -1;
5443 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
5444 if (p2p_ctrl_find(wpa_s, ""))
5445 reply_len = -1;
5446 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
5447 wpas_p2p_stop_find(wpa_s);
5448 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
5449 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
5450 reply_size);
5451 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
5452 if (p2p_ctrl_listen(wpa_s, buf + 11))
5453 reply_len = -1;
5454 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
5455 if (p2p_ctrl_listen(wpa_s, ""))
5456 reply_len = -1;
5457 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
5458 if (wpas_p2p_group_remove(wpa_s, buf + 17))
5459 reply_len = -1;
5460 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005461 if (wpas_p2p_group_add(wpa_s, 0, 0, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462 reply_len = -1;
5463 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
5464 if (p2p_ctrl_group_add(wpa_s, buf + 14))
5465 reply_len = -1;
5466 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
5467 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
5468 reply_len = -1;
5469 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
5470 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
5471 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
5472 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
5473 reply_size);
5474 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
5475 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
5476 reply_len = -1;
5477 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
5478 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
5479 reply_len = -1;
5480 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
5481 wpas_p2p_sd_service_update(wpa_s);
5482 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
5483 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
5484 reply_len = -1;
5485 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
5486 wpas_p2p_service_flush(wpa_s);
5487 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
5488 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
5489 reply_len = -1;
5490 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
5491 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
5492 reply_len = -1;
5493 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
5494 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
5495 reply_len = -1;
5496 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
5497 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
5498 reply_len = -1;
5499 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
5500 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
5501 reply_size);
5502 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
5503 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
5504 reply_len = -1;
5505 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005506 p2p_ctrl_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005507 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
5508 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
5509 reply_len = -1;
5510 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
5511 if (wpas_p2p_cancel(wpa_s))
5512 reply_len = -1;
5513 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
5514 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
5515 reply_len = -1;
5516 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
5517 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
5518 reply_len = -1;
5519 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
5520 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
5521 reply_len = -1;
5522 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
5523 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
5524 reply_len = -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005525 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
5526 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
5527 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005528#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005529#ifdef CONFIG_WIFI_DISPLAY
5530 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
5531 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
5532 reply_len = -1;
5533 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
5534 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
5535 reply, reply_size);
5536#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005537#ifdef CONFIG_INTERWORKING
5538 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
5539 if (interworking_fetch_anqp(wpa_s) < 0)
5540 reply_len = -1;
5541 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
5542 interworking_stop_fetch_anqp(wpa_s);
5543 } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
5544 if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
5545 NULL) < 0)
5546 reply_len = -1;
5547 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
5548 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
5549 reply_len = -1;
5550 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
5551 if (get_anqp(wpa_s, buf + 9) < 0)
5552 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005553 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
5554 if (gas_request(wpa_s, buf + 12) < 0)
5555 reply_len = -1;
5556 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
5557 reply_len = gas_response_get(wpa_s, buf + 17, reply,
5558 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005559#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005560#ifdef CONFIG_HS20
5561 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
5562 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
5563 reply_len = -1;
5564 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
5565 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
5566 reply_len = -1;
5567#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005568 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
5569 {
5570 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
5571 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
5572 reply_len = -1;
5573 else
5574 ctrl_rsp = 1;
5575 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
5576 if (wpa_supplicant_reload_configuration(wpa_s))
5577 reply_len = -1;
5578 } else if (os_strcmp(buf, "TERMINATE") == 0) {
5579 wpa_supplicant_terminate_proc(wpa_s->global);
5580 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
5581 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
5582 reply_len = -1;
Dmitry Shmidte19501d2011-03-16 14:32:18 -07005583 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005584 reply_len = wpa_supplicant_ctrl_iface_blacklist(
5585 wpa_s, buf + 9, reply, reply_size);
5586 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
5587 reply_len = wpa_supplicant_ctrl_iface_log_level(
5588 wpa_s, buf + 9, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005589 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
5590 reply_len = wpa_supplicant_ctrl_iface_list_networks(
5591 wpa_s, reply, reply_size);
5592 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005593#ifdef CONFIG_SME
5594 wpa_s->sme.prev_bssid_set = 0;
5595#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005596 wpa_s->reassociate = 0;
5597 wpa_s->disconnected = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005598 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005599 wpa_supplicant_cancel_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005600 wpa_supplicant_deauthenticate(wpa_s,
5601 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005602 } else if (os_strcmp(buf, "SCAN") == 0 ||
5603 os_strncmp(buf, "SCAN ", 5) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005604 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5605 reply_len = -1;
5606 else {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005607 if (os_strlen(buf) > 4 &&
5608 os_strncasecmp(buf + 5, "TYPE=ONLY", 9) == 0)
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08005609 wpa_s->scan_res_handler = scan_only_handler;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005610 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611 ((wpa_s->wpa_state <= WPA_SCANNING) ||
5612 (wpa_s->wpa_state == WPA_COMPLETED))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005613 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005614 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005615 wpa_supplicant_req_scan(wpa_s, 0, 0);
5616 } else if (wpa_s->sched_scanning) {
5617 wpa_printf(MSG_DEBUG, "Stop ongoing "
5618 "sched_scan to allow requested "
5619 "full scan to proceed");
5620 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005621 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005622 wpa_supplicant_req_scan(wpa_s, 0, 0);
5623 } else {
5624 wpa_printf(MSG_DEBUG, "Ongoing scan action - "
5625 "reject new request");
5626 reply_len = os_snprintf(reply, reply_size,
5627 "FAIL-BUSY\n");
5628 }
5629 }
5630 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
5631 reply_len = wpa_supplicant_ctrl_iface_scan_results(
5632 wpa_s, reply, reply_size);
5633 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
5634 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
5635 reply_len = -1;
5636 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
5637 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
5638 reply_len = -1;
5639 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
5640 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
5641 reply_len = -1;
5642 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
5643 reply_len = wpa_supplicant_ctrl_iface_add_network(
5644 wpa_s, reply, reply_size);
5645 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
5646 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
5647 reply_len = -1;
5648 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
5649 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
5650 reply_len = -1;
5651 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
5652 reply_len = wpa_supplicant_ctrl_iface_get_network(
5653 wpa_s, buf + 12, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005654 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
5655 reply_len = wpa_supplicant_ctrl_iface_list_creds(
5656 wpa_s, reply, reply_size);
5657 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
5658 reply_len = wpa_supplicant_ctrl_iface_add_cred(
5659 wpa_s, reply, reply_size);
5660 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
5661 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
5662 reply_len = -1;
5663 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
5664 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
5665 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005666#ifndef CONFIG_NO_CONFIG_WRITE
5667 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
5668 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
5669 reply_len = -1;
5670#endif /* CONFIG_NO_CONFIG_WRITE */
5671 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
5672 reply_len = wpa_supplicant_ctrl_iface_get_capability(
5673 wpa_s, buf + 15, reply, reply_size);
5674 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
5675 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
5676 reply_len = -1;
5677 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
5678 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
5679 reply_len = -1;
5680 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
5681 reply_len = wpa_supplicant_global_iface_list(
5682 wpa_s->global, reply, reply_size);
5683 } else if (os_strcmp(buf, "INTERFACES") == 0) {
5684 reply_len = wpa_supplicant_global_iface_interfaces(
5685 wpa_s->global, reply, reply_size);
5686 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
5687 reply_len = wpa_supplicant_ctrl_iface_bss(
5688 wpa_s, buf + 4, reply, reply_size);
5689#ifdef CONFIG_AP
5690 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
5691 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
5692 } else if (os_strncmp(buf, "STA ", 4) == 0) {
5693 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
5694 reply_size);
5695 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
5696 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
5697 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005698 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
5699 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
5700 reply_len = -1;
5701 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
5702 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
5703 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005704#endif /* CONFIG_AP */
5705 } else if (os_strcmp(buf, "SUSPEND") == 0) {
5706 wpas_notify_suspend(wpa_s->global);
5707 } else if (os_strcmp(buf, "RESUME") == 0) {
5708 wpas_notify_resume(wpa_s->global);
5709 } else if (os_strcmp(buf, "DROP_SA") == 0) {
5710 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
5711 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
5712 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
5713 reply_len = -1;
5714 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
5715 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
5716 reply_len = -1;
5717 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
5718 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
5719 reply_len = -1;
5720 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
5721 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
5722 buf + 17))
5723 reply_len = -1;
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07005724 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
5725 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
5726 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005727#ifdef CONFIG_TDLS
5728 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
5729 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
5730 reply_len = -1;
5731 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
5732 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
5733 reply_len = -1;
5734 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
5735 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
5736 reply_len = -1;
5737#endif /* CONFIG_TDLS */
5738 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
5739 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
5740 reply_size);
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005741 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
5742 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
5743 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005744#ifdef CONFIG_AUTOSCAN
5745 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
5746 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
5747 reply_len = -1;
5748#endif /* CONFIG_AUTOSCAN */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005749#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005750 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
5751 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
5752 reply_size);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005753#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005754 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005755 pmksa_cache_clear_current(wpa_s->wpa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005756 eapol_sm_request_reauth(wpa_s->eapol);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005757#ifdef CONFIG_WNM
5758 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
5759 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
5760 reply_len = -1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005761 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
5762 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
5763 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005764#endif /* CONFIG_WNM */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005765 } else if (os_strcmp(buf, "FLUSH") == 0) {
5766 wpa_supplicant_ctrl_iface_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005767 } else {
5768 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
5769 reply_len = 16;
5770 }
5771
5772 if (reply_len < 0) {
5773 os_memcpy(reply, "FAIL\n", 5);
5774 reply_len = 5;
5775 }
5776
5777 if (ctrl_rsp)
5778 eapol_sm_notify_ctrl_response(wpa_s->eapol);
5779
5780 *resp_len = reply_len;
5781 return reply;
5782}
5783
5784
5785static int wpa_supplicant_global_iface_add(struct wpa_global *global,
5786 char *cmd)
5787{
5788 struct wpa_interface iface;
5789 char *pos;
5790
5791 /*
5792 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
5793 * TAB<bridge_ifname>
5794 */
5795 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
5796
5797 os_memset(&iface, 0, sizeof(iface));
5798
5799 do {
5800 iface.ifname = pos = cmd;
5801 pos = os_strchr(pos, '\t');
5802 if (pos)
5803 *pos++ = '\0';
5804 if (iface.ifname[0] == '\0')
5805 return -1;
5806 if (pos == NULL)
5807 break;
5808
5809 iface.confname = pos;
5810 pos = os_strchr(pos, '\t');
5811 if (pos)
5812 *pos++ = '\0';
5813 if (iface.confname[0] == '\0')
5814 iface.confname = NULL;
5815 if (pos == NULL)
5816 break;
5817
5818 iface.driver = pos;
5819 pos = os_strchr(pos, '\t');
5820 if (pos)
5821 *pos++ = '\0';
5822 if (iface.driver[0] == '\0')
5823 iface.driver = NULL;
5824 if (pos == NULL)
5825 break;
5826
5827 iface.ctrl_interface = pos;
5828 pos = os_strchr(pos, '\t');
5829 if (pos)
5830 *pos++ = '\0';
5831 if (iface.ctrl_interface[0] == '\0')
5832 iface.ctrl_interface = NULL;
5833 if (pos == NULL)
5834 break;
5835
5836 iface.driver_param = pos;
5837 pos = os_strchr(pos, '\t');
5838 if (pos)
5839 *pos++ = '\0';
5840 if (iface.driver_param[0] == '\0')
5841 iface.driver_param = NULL;
5842 if (pos == NULL)
5843 break;
5844
5845 iface.bridge_ifname = pos;
5846 pos = os_strchr(pos, '\t');
5847 if (pos)
5848 *pos++ = '\0';
5849 if (iface.bridge_ifname[0] == '\0')
5850 iface.bridge_ifname = NULL;
5851 if (pos == NULL)
5852 break;
5853 } while (0);
5854
5855 if (wpa_supplicant_get_iface(global, iface.ifname))
5856 return -1;
5857
5858 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
5859}
5860
5861
5862static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
5863 char *cmd)
5864{
5865 struct wpa_supplicant *wpa_s;
5866
5867 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
5868
5869 wpa_s = wpa_supplicant_get_iface(global, cmd);
5870 if (wpa_s == NULL)
5871 return -1;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07005872 return wpa_supplicant_remove_iface(global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005873}
5874
5875
5876static void wpa_free_iface_info(struct wpa_interface_info *iface)
5877{
5878 struct wpa_interface_info *prev;
5879
5880 while (iface) {
5881 prev = iface;
5882 iface = iface->next;
5883
5884 os_free(prev->ifname);
5885 os_free(prev->desc);
5886 os_free(prev);
5887 }
5888}
5889
5890
5891static int wpa_supplicant_global_iface_list(struct wpa_global *global,
5892 char *buf, int len)
5893{
5894 int i, res;
5895 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
5896 char *pos, *end;
5897
5898 for (i = 0; wpa_drivers[i]; i++) {
5899 struct wpa_driver_ops *drv = wpa_drivers[i];
5900 if (drv->get_interfaces == NULL)
5901 continue;
5902 tmp = drv->get_interfaces(global->drv_priv[i]);
5903 if (tmp == NULL)
5904 continue;
5905
5906 if (last == NULL)
5907 iface = last = tmp;
5908 else
5909 last->next = tmp;
5910 while (last->next)
5911 last = last->next;
5912 }
5913
5914 pos = buf;
5915 end = buf + len;
5916 for (tmp = iface; tmp; tmp = tmp->next) {
5917 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
5918 tmp->drv_name, tmp->ifname,
5919 tmp->desc ? tmp->desc : "");
5920 if (res < 0 || res >= end - pos) {
5921 *pos = '\0';
5922 break;
5923 }
5924 pos += res;
5925 }
5926
5927 wpa_free_iface_info(iface);
5928
5929 return pos - buf;
5930}
5931
5932
5933static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
5934 char *buf, int len)
5935{
5936 int res;
5937 char *pos, *end;
5938 struct wpa_supplicant *wpa_s;
5939
5940 wpa_s = global->ifaces;
5941 pos = buf;
5942 end = buf + len;
5943
5944 while (wpa_s) {
5945 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
5946 if (res < 0 || res >= end - pos) {
5947 *pos = '\0';
5948 break;
5949 }
5950 pos += res;
5951 wpa_s = wpa_s->next;
5952 }
5953 return pos - buf;
5954}
5955
5956
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005957static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
5958 const char *ifname,
5959 char *cmd, size_t *resp_len)
5960{
5961 struct wpa_supplicant *wpa_s;
5962
5963 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5964 if (os_strcmp(ifname, wpa_s->ifname) == 0)
5965 break;
5966 }
5967
5968 if (wpa_s == NULL) {
5969 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
5970 if (resp)
5971 *resp_len = os_strlen(resp);
5972 else
5973 *resp_len = 1;
5974 return resp;
5975 }
5976
5977 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
5978}
5979
5980
5981static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
5982 char *buf, size_t *resp_len)
5983{
5984#ifdef CONFIG_P2P
5985 static const char * cmd[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07005986#ifdef ANDROID_P2P
5987 "LIST_NETWORKS",
5988 "SAVE_CONFIG",
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07005989#endif
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005990 "P2P_FIND",
5991 "P2P_STOP_FIND",
5992 "P2P_LISTEN",
5993 "P2P_GROUP_ADD",
5994 "P2P_GET_PASSPHRASE",
5995 "P2P_SERVICE_UPDATE",
5996 "P2P_SERVICE_FLUSH",
5997 "P2P_FLUSH",
5998 "P2P_CANCEL",
5999 "P2P_PRESENCE_REQ",
6000 "P2P_EXT_LISTEN",
6001 NULL
6002 };
6003 static const char * prefix[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07006004#ifdef ANDROID_P2P
6005 "DRIVER ",
6006 "GET_NETWORK ",
6007 "REMOVE_NETWORK ",
6008 "SET ",
6009#endif
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006010 "P2P_FIND ",
6011 "P2P_CONNECT ",
6012 "P2P_LISTEN ",
6013 "P2P_GROUP_REMOVE ",
6014 "P2P_GROUP_ADD ",
6015 "P2P_PROV_DISC ",
6016 "P2P_SERV_DISC_REQ ",
6017 "P2P_SERV_DISC_CANCEL_REQ ",
6018 "P2P_SERV_DISC_RESP ",
6019 "P2P_SERV_DISC_EXTERNAL ",
6020 "P2P_SERVICE_ADD ",
6021 "P2P_SERVICE_DEL ",
6022 "P2P_REJECT ",
6023 "P2P_INVITE ",
6024 "P2P_PEER ",
6025 "P2P_SET ",
6026 "P2P_UNAUTHORIZE ",
6027 "P2P_PRESENCE_REQ ",
6028 "P2P_EXT_LISTEN ",
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006029 "P2P_REMOVE_CLIENT ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006030 NULL
6031 };
6032 int found = 0;
6033 int i;
6034
6035 if (global->p2p_init_wpa_s == NULL)
6036 return NULL;
6037
6038 for (i = 0; !found && cmd[i]; i++) {
6039 if (os_strcmp(buf, cmd[i]) == 0)
6040 found = 1;
6041 }
6042
6043 for (i = 0; !found && prefix[i]; i++) {
6044 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
6045 found = 1;
6046 }
6047
6048 if (found)
6049 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6050 buf, resp_len);
6051#endif /* CONFIG_P2P */
6052 return NULL;
6053}
6054
6055
6056static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
6057 char *buf, size_t *resp_len)
6058{
6059#ifdef CONFIG_WIFI_DISPLAY
6060 if (global->p2p_init_wpa_s == NULL)
6061 return NULL;
6062 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
6063 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
6064 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6065 buf, resp_len);
6066#endif /* CONFIG_WIFI_DISPLAY */
6067 return NULL;
6068}
6069
6070
6071static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
6072 char *buf, size_t *resp_len)
6073{
6074 char *ret;
6075
6076 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
6077 if (ret)
6078 return ret;
6079
6080 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
6081 if (ret)
6082 return ret;
6083
6084 return NULL;
6085}
6086
6087
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006088static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
6089{
6090 char *value;
6091
6092 value = os_strchr(cmd, ' ');
6093 if (value == NULL)
6094 return -1;
6095 *value++ = '\0';
6096
6097 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
6098
6099#ifdef CONFIG_WIFI_DISPLAY
6100 if (os_strcasecmp(cmd, "wifi_display") == 0) {
6101 wifi_display_enable(global, !!atoi(value));
6102 return 0;
6103 }
6104#endif /* CONFIG_WIFI_DISPLAY */
6105
6106 return -1;
6107}
6108
6109
6110#ifndef CONFIG_NO_CONFIG_WRITE
6111static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
6112{
6113 int ret = 0;
6114 struct wpa_supplicant *wpa_s;
6115
6116 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6117 if (!wpa_s->conf->update_config) {
6118 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
6119 continue;
6120 }
6121
6122 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
6123 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
6124 ret = 1;
6125 } else {
6126 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
6127 }
6128 }
6129
6130 return ret;
6131}
6132#endif /* CONFIG_NO_CONFIG_WRITE */
6133
6134
6135static int wpas_global_ctrl_iface_status(struct wpa_global *global,
6136 char *buf, size_t buflen)
6137{
6138 char *pos, *end;
6139 int ret;
6140 struct wpa_supplicant *wpa_s;
6141
6142 pos = buf;
6143 end = buf + buflen;
6144
6145#ifdef CONFIG_P2P
6146 if (global->p2p && !global->p2p_disabled) {
6147 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
6148 "\n"
6149 "p2p_state=%s\n",
6150 MAC2STR(global->p2p_dev_addr),
6151 p2p_get_state_txt(global->p2p));
6152 if (ret < 0 || ret >= end - pos)
6153 return pos - buf;
6154 pos += ret;
6155 } else if (global->p2p) {
6156 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
6157 if (ret < 0 || ret >= end - pos)
6158 return pos - buf;
6159 pos += ret;
6160 }
6161#endif /* CONFIG_P2P */
6162
6163#ifdef CONFIG_WIFI_DISPLAY
6164 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
6165 !!global->wifi_display);
6166 if (ret < 0 || ret >= end - pos)
6167 return pos - buf;
6168 pos += ret;
6169#endif /* CONFIG_WIFI_DISPLAY */
6170
6171 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6172 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
6173 "address=" MACSTR "\n",
6174 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
6175 if (ret < 0 || ret >= end - pos)
6176 return pos - buf;
6177 pos += ret;
6178 }
6179
6180 return pos - buf;
6181}
6182
6183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006184char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
6185 char *buf, size_t *resp_len)
6186{
6187 char *reply;
6188 const int reply_size = 2048;
6189 int reply_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006190 int level = MSG_DEBUG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006191
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006192 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
6193 char *pos = os_strchr(buf + 7, ' ');
6194 if (pos) {
6195 *pos++ = '\0';
6196 return wpas_global_ctrl_iface_ifname(global,
6197 buf + 7, pos,
6198 resp_len);
6199 }
6200 }
6201
6202 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
6203 if (reply)
6204 return reply;
6205
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006206 if (os_strcmp(buf, "PING") == 0)
6207 level = MSG_EXCESSIVE;
6208 wpa_hexdump_ascii(level, "RX global ctrl_iface",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006209 (const u8 *) buf, os_strlen(buf));
6210
6211 reply = os_malloc(reply_size);
6212 if (reply == NULL) {
6213 *resp_len = 1;
6214 return NULL;
6215 }
6216
6217 os_memcpy(reply, "OK\n", 3);
6218 reply_len = 3;
6219
6220 if (os_strcmp(buf, "PING") == 0) {
6221 os_memcpy(reply, "PONG\n", 5);
6222 reply_len = 5;
6223 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
6224 if (wpa_supplicant_global_iface_add(global, buf + 14))
6225 reply_len = -1;
6226 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
6227 if (wpa_supplicant_global_iface_remove(global, buf + 17))
6228 reply_len = -1;
6229 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
6230 reply_len = wpa_supplicant_global_iface_list(
6231 global, reply, reply_size);
6232 } else if (os_strcmp(buf, "INTERFACES") == 0) {
6233 reply_len = wpa_supplicant_global_iface_interfaces(
6234 global, reply, reply_size);
6235 } else if (os_strcmp(buf, "TERMINATE") == 0) {
6236 wpa_supplicant_terminate_proc(global);
6237 } else if (os_strcmp(buf, "SUSPEND") == 0) {
6238 wpas_notify_suspend(global);
6239 } else if (os_strcmp(buf, "RESUME") == 0) {
6240 wpas_notify_resume(global);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006241 } else if (os_strncmp(buf, "SET ", 4) == 0) {
6242 if (wpas_global_ctrl_iface_set(global, buf + 4))
6243 reply_len = -1;
6244#ifndef CONFIG_NO_CONFIG_WRITE
6245 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
6246 if (wpas_global_ctrl_iface_save_config(global))
6247 reply_len = -1;
6248#endif /* CONFIG_NO_CONFIG_WRITE */
6249 } else if (os_strcmp(buf, "STATUS") == 0) {
6250 reply_len = wpas_global_ctrl_iface_status(global, reply,
6251 reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006252 } else {
6253 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
6254 reply_len = 16;
6255 }
6256
6257 if (reply_len < 0) {
6258 os_memcpy(reply, "FAIL\n", 5);
6259 reply_len = 5;
6260 }
6261
6262 *resp_len = reply_len;
6263 return reply;
6264}