blob: 87ed3e26c6a82e9de2d62e0057746750f398db2a [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Control interface (shared code for all backends)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2004-2014, 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"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080013#include "utils/uuid.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "common/version.h"
15#include "common/ieee802_11_defs.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070016#include "common/ieee802_11_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "common/wpa_ctrl.h"
18#include "eap_peer/eap.h"
19#include "eapol_supp/eapol_supp_sm.h"
20#include "rsn_supp/wpa.h"
21#include "rsn_supp/preauth.h"
22#include "rsn_supp/pmksa_cache.h"
23#include "l2_packet/l2_packet.h"
24#include "wps/wps.h"
25#include "config.h"
26#include "wpa_supplicant_i.h"
27#include "driver_i.h"
28#include "wps_supplicant.h"
29#include "ibss_rsn.h"
30#include "ap.h"
31#include "p2p_supplicant.h"
32#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070033#include "hs20_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070034#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "notify.h"
36#include "bss.h"
37#include "scan.h"
38#include "ctrl_iface.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080039#include "interworking.h"
Dmitry Shmidte19501d2011-03-16 14:32:18 -070040#include "blacklist.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070041#include "autoscan.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080042#include "wnm_sta.h"
Dmitry Shmidt818ea482014-03-10 13:15:21 -070043#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045static 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
Dmitry Shmidt04949592012-07-19 12:16:46 -070050static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
51{
52 char *pos;
53 u8 addr[ETH_ALEN], *filter = NULL, *n;
54 size_t count = 0;
55
56 pos = val;
57 while (pos) {
58 if (*pos == '\0')
59 break;
60 if (hwaddr_aton(pos, addr)) {
61 os_free(filter);
62 return -1;
63 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070064 n = os_realloc_array(filter, count + 1, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -070065 if (n == NULL) {
66 os_free(filter);
67 return -1;
68 }
69 filter = n;
70 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
71 count++;
72
73 pos = os_strchr(pos, ' ');
74 if (pos)
75 pos++;
76 }
77
78 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
79 os_free(wpa_s->bssid_filter);
80 wpa_s->bssid_filter = filter;
81 wpa_s->bssid_filter_count = count;
82
83 return 0;
84}
85
86
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080087static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
88{
89 char *pos;
90 u8 addr[ETH_ALEN], *bssid = NULL, *n;
91 struct wpa_ssid_value *ssid = NULL, *ns;
92 size_t count = 0, ssid_count = 0;
93 struct wpa_ssid *c;
94
95 /*
Dmitry Shmidtcce06662013-11-04 18:44:24 -080096 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080097 * SSID_SPEC ::= ssid <SSID_HEX>
98 * BSSID_SPEC ::= bssid <BSSID_HEX>
99 */
100
101 pos = val;
102 while (pos) {
103 if (*pos == '\0')
104 break;
105 if (os_strncmp(pos, "bssid ", 6) == 0) {
106 int res;
107 pos += 6;
108 res = hwaddr_aton2(pos, addr);
109 if (res < 0) {
110 os_free(ssid);
111 os_free(bssid);
112 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
113 "BSSID value '%s'", pos);
114 return -1;
115 }
116 pos += res;
117 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
118 if (n == NULL) {
119 os_free(ssid);
120 os_free(bssid);
121 return -1;
122 }
123 bssid = n;
124 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
125 count++;
126 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
127 char *end;
128 pos += 5;
129
130 end = pos;
131 while (*end) {
132 if (*end == '\0' || *end == ' ')
133 break;
134 end++;
135 }
136
137 ns = os_realloc_array(ssid, ssid_count + 1,
138 sizeof(struct wpa_ssid_value));
139 if (ns == NULL) {
140 os_free(ssid);
141 os_free(bssid);
142 return -1;
143 }
144 ssid = ns;
145
146 if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
147 hexstr2bin(pos, ssid[ssid_count].ssid,
148 (end - pos) / 2) < 0) {
149 os_free(ssid);
150 os_free(bssid);
151 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
152 "SSID value '%s'", pos);
153 return -1;
154 }
155 ssid[ssid_count].ssid_len = (end - pos) / 2;
156 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
157 ssid[ssid_count].ssid,
158 ssid[ssid_count].ssid_len);
159 ssid_count++;
160 pos = end;
161 } else {
162 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
163 "'%s'", pos);
164 os_free(ssid);
165 os_free(bssid);
166 return -1;
167 }
168
169 pos = os_strchr(pos, ' ');
170 if (pos)
171 pos++;
172 }
173
174 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
175 os_free(wpa_s->disallow_aps_bssid);
176 wpa_s->disallow_aps_bssid = bssid;
177 wpa_s->disallow_aps_bssid_count = count;
178
179 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
180 os_free(wpa_s->disallow_aps_ssid);
181 wpa_s->disallow_aps_ssid = ssid;
182 wpa_s->disallow_aps_ssid_count = ssid_count;
183
184 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
185 return 0;
186
187 c = wpa_s->current_ssid;
188 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
189 return 0;
190
191 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
192 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
193 return 0;
194
195 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
196 "because current AP was marked disallowed");
197
198#ifdef CONFIG_SME
199 wpa_s->sme.prev_bssid_set = 0;
200#endif /* CONFIG_SME */
201 wpa_s->reassociate = 1;
202 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
203 wpa_supplicant_req_scan(wpa_s, 0, 0);
204
205 return 0;
206}
207
208
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700209#ifndef CONFIG_NO_CONFIG_BLOBS
210static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
211{
212 char *name = pos;
213 struct wpa_config_blob *blob;
214 size_t len;
215
216 pos = os_strchr(pos, ' ');
217 if (pos == NULL)
218 return -1;
219 *pos++ = '\0';
220 len = os_strlen(pos);
221 if (len & 1)
222 return -1;
223
224 wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
225 blob = os_zalloc(sizeof(*blob));
226 if (blob == NULL)
227 return -1;
228 blob->name = os_strdup(name);
229 blob->data = os_malloc(len / 2);
230 if (blob->name == NULL || blob->data == NULL) {
231 wpa_config_free_blob(blob);
232 return -1;
233 }
234
235 if (hexstr2bin(pos, blob->data, len / 2) < 0) {
236 wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
237 wpa_config_free_blob(blob);
238 return -1;
239 }
240 blob->len = len / 2;
241
242 wpa_config_set_blob(wpa_s->conf, blob);
243
244 return 0;
245}
246#endif /* CONFIG_NO_CONFIG_BLOBS */
247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
249 char *cmd)
250{
251 char *value;
252 int ret = 0;
253
254 value = os_strchr(cmd, ' ');
255 if (value == NULL)
256 return -1;
257 *value++ = '\0';
258
259 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
260 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
261 eapol_sm_configure(wpa_s->eapol,
262 atoi(value), -1, -1, -1);
263 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
264 eapol_sm_configure(wpa_s->eapol,
265 -1, atoi(value), -1, -1);
266 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
267 eapol_sm_configure(wpa_s->eapol,
268 -1, -1, atoi(value), -1);
269 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
270 eapol_sm_configure(wpa_s->eapol,
271 -1, -1, -1, atoi(value));
272 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
273 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
274 atoi(value)))
275 ret = -1;
276 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
277 0) {
278 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
279 atoi(value)))
280 ret = -1;
281 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
282 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
283 ret = -1;
284 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
285 wpa_s->wps_fragment_size = atoi(value);
286#ifdef CONFIG_WPS_TESTING
287 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
288 long int val;
289 val = strtol(value, NULL, 0);
290 if (val < 0 || val > 0xff) {
291 ret = -1;
292 wpa_printf(MSG_DEBUG, "WPS: Invalid "
293 "wps_version_number %ld", val);
294 } else {
295 wps_version_number = val;
296 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
297 "version %u.%u",
298 (wps_version_number & 0xf0) >> 4,
299 wps_version_number & 0x0f);
300 }
301 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
302 wps_testing_dummy_cred = atoi(value);
303 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
304 wps_testing_dummy_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800305 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
306 wps_corrupt_pkhash = atoi(value);
307 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
308 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309#endif /* CONFIG_WPS_TESTING */
310 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
311 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
312 ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800313#ifdef CONFIG_TDLS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314#ifdef CONFIG_TDLS_TESTING
315 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
316 extern unsigned int tdls_testing;
317 tdls_testing = strtol(value, NULL, 0);
318 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
319#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
321 int disabled = atoi(value);
322 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
323 if (disabled) {
324 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
325 ret = -1;
326 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
327 ret = -1;
328 wpa_tdls_enable(wpa_s->wpa, !disabled);
329#endif /* CONFIG_TDLS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800330 } else if (os_strcasecmp(cmd, "pno") == 0) {
331 if (atoi(value))
Dmitry Shmidt98660862014-03-11 17:26:21 -0700332 ret = wpas_start_pno(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800333 else
Dmitry Shmidt98660862014-03-11 17:26:21 -0700334 ret = wpas_stop_pno(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700335 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
336 int disabled = atoi(value);
337 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
338 ret = -1;
339 else if (disabled)
340 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
341 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
342 if (os_strcmp(value, "disable") == 0)
343 wpa_s->set_sta_uapsd = 0;
344 else {
345 int be, bk, vi, vo;
346 char *pos;
347 /* format: BE,BK,VI,VO;max SP Length */
348 be = atoi(value);
349 pos = os_strchr(value, ',');
350 if (pos == NULL)
351 return -1;
352 pos++;
353 bk = atoi(pos);
354 pos = os_strchr(pos, ',');
355 if (pos == NULL)
356 return -1;
357 pos++;
358 vi = atoi(pos);
359 pos = os_strchr(pos, ',');
360 if (pos == NULL)
361 return -1;
362 pos++;
363 vo = atoi(pos);
364 /* ignore max SP Length for now */
365
366 wpa_s->set_sta_uapsd = 1;
367 wpa_s->sta_uapsd = 0;
368 if (be)
369 wpa_s->sta_uapsd |= BIT(0);
370 if (bk)
371 wpa_s->sta_uapsd |= BIT(1);
372 if (vi)
373 wpa_s->sta_uapsd |= BIT(2);
374 if (vo)
375 wpa_s->sta_uapsd |= BIT(3);
376 }
Jouni Malinen21d6bc82012-04-10 16:17:59 -0700377 } else if (os_strcasecmp(cmd, "ps") == 0) {
378 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700379#ifdef CONFIG_WIFI_DISPLAY
380 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
Dmitry Shmidted003d22014-02-06 10:09:12 -0800381 int enabled = !!atoi(value);
382 if (enabled && !wpa_s->global->p2p)
383 ret = -1;
384 else
385 wifi_display_enable(wpa_s->global, enabled);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700386#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700387 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
388 ret = set_bssid_filter(wpa_s, value);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800389 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
390 ret = set_disallow_aps(wpa_s, value);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800391 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
392 wpa_s->no_keep_alive = !!atoi(value);
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700393#ifdef CONFIG_TESTING_OPTIONS
394 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
395 wpa_s->ext_mgmt_frame_handling = !!atoi(value);
396#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700397#ifndef CONFIG_NO_CONFIG_BLOBS
398 } else if (os_strcmp(cmd, "blob") == 0) {
399 ret = wpas_ctrl_set_blob(wpa_s, value);
400#endif /* CONFIG_NO_CONFIG_BLOBS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401 } else {
402 value[-1] = '=';
403 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
404 if (ret == 0)
405 wpa_supplicant_update_config(wpa_s);
406 }
407
408 return ret;
409}
410
411
412static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
413 char *cmd, char *buf, size_t buflen)
414{
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700415 int res = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416
417 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
418
419 if (os_strcmp(cmd, "version") == 0) {
420 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700421 } else if (os_strcasecmp(cmd, "country") == 0) {
422 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
423 res = os_snprintf(buf, buflen, "%c%c",
424 wpa_s->conf->country[0],
425 wpa_s->conf->country[1]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700426#ifdef CONFIG_WIFI_DISPLAY
427 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
Dmitry Shmidted003d22014-02-06 10:09:12 -0800428 int enabled;
429 if (wpa_s->global->p2p == NULL ||
430 wpa_s->global->p2p_disabled)
431 enabled = 0;
432 else
433 enabled = wpa_s->global->wifi_display;
434 res = os_snprintf(buf, buflen, "%d", enabled);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700435 if (res < 0 || (unsigned int) res >= buflen)
436 return -1;
437 return res;
438#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700439#ifdef CONFIG_TESTING_GET_GTK
440 } else if (os_strcmp(cmd, "gtk") == 0) {
441 if (wpa_s->last_gtk_len == 0)
442 return -1;
443 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
444 wpa_s->last_gtk_len);
445 return res;
446#endif /* CONFIG_TESTING_GET_GTK */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 }
448
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700449 if (res < 0 || (unsigned int) res >= buflen)
450 return -1;
451 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452}
453
454
455#ifdef IEEE8021X_EAPOL
456static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
457 char *addr)
458{
459 u8 bssid[ETH_ALEN];
460 struct wpa_ssid *ssid = wpa_s->current_ssid;
461
462 if (hwaddr_aton(addr, bssid)) {
463 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
464 "'%s'", addr);
465 return -1;
466 }
467
468 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
469 rsn_preauth_deinit(wpa_s->wpa);
470 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
471 return -1;
472
473 return 0;
474}
475#endif /* IEEE8021X_EAPOL */
476
477
478#ifdef CONFIG_PEERKEY
479/* MLME-STKSTART.request(peer) */
480static int wpa_supplicant_ctrl_iface_stkstart(
481 struct wpa_supplicant *wpa_s, char *addr)
482{
483 u8 peer[ETH_ALEN];
484
485 if (hwaddr_aton(addr, peer)) {
486 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
487 "address '%s'", addr);
488 return -1;
489 }
490
491 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
492 MAC2STR(peer));
493
494 return wpa_sm_stkstart(wpa_s->wpa, peer);
495}
496#endif /* CONFIG_PEERKEY */
497
498
499#ifdef CONFIG_TDLS
500
501static int wpa_supplicant_ctrl_iface_tdls_discover(
502 struct wpa_supplicant *wpa_s, char *addr)
503{
504 u8 peer[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800505 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506
507 if (hwaddr_aton(addr, peer)) {
508 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
509 "address '%s'", addr);
510 return -1;
511 }
512
513 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
514 MAC2STR(peer));
515
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800516 if (wpa_tdls_is_external_setup(wpa_s->wpa))
517 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
518 else
519 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
520
521 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700522}
523
524
525static int wpa_supplicant_ctrl_iface_tdls_setup(
526 struct wpa_supplicant *wpa_s, char *addr)
527{
528 u8 peer[ETH_ALEN];
529 int ret;
530
531 if (hwaddr_aton(addr, peer)) {
532 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
533 "address '%s'", addr);
534 return -1;
535 }
536
537 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
538 MAC2STR(peer));
539
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800540 if ((wpa_s->conf->tdls_external_control) &&
541 wpa_tdls_is_external_setup(wpa_s->wpa))
542 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
543
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800544 wpa_tdls_remove(wpa_s->wpa, peer);
545
546 if (wpa_tdls_is_external_setup(wpa_s->wpa))
547 ret = wpa_tdls_start(wpa_s->wpa, peer);
548 else
549 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800550
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700551 return ret;
552}
553
554
555static int wpa_supplicant_ctrl_iface_tdls_teardown(
556 struct wpa_supplicant *wpa_s, char *addr)
557{
558 u8 peer[ETH_ALEN];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700559 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560
561 if (hwaddr_aton(addr, peer)) {
562 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
563 "address '%s'", addr);
564 return -1;
565 }
566
567 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
568 MAC2STR(peer));
569
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800570 if ((wpa_s->conf->tdls_external_control) &&
571 wpa_tdls_is_external_setup(wpa_s->wpa))
572 return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
573
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700574 if (wpa_tdls_is_external_setup(wpa_s->wpa))
575 ret = wpa_tdls_teardown_link(
576 wpa_s->wpa, peer,
577 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
578 else
579 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
580
581 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582}
583
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700584
585static int ctrl_iface_get_capability_tdls(
586 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
587{
588 int ret;
589
590 ret = os_snprintf(buf, buflen, "%s\n",
591 wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
592 (wpa_s->drv_flags &
593 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
594 "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
595 if (ret < 0 || (size_t) ret > buflen)
596 return -1;
597 return ret;
598}
599
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700600#endif /* CONFIG_TDLS */
601
602
603#ifdef CONFIG_IEEE80211R
604static int wpa_supplicant_ctrl_iface_ft_ds(
605 struct wpa_supplicant *wpa_s, char *addr)
606{
607 u8 target_ap[ETH_ALEN];
608 struct wpa_bss *bss;
609 const u8 *mdie;
610
611 if (hwaddr_aton(addr, target_ap)) {
612 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
613 "address '%s'", addr);
614 return -1;
615 }
616
617 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
618
619 bss = wpa_bss_get_bssid(wpa_s, target_ap);
620 if (bss)
621 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
622 else
623 mdie = NULL;
624
625 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
626}
627#endif /* CONFIG_IEEE80211R */
628
629
630#ifdef CONFIG_WPS
631static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
632 char *cmd)
633{
634 u8 bssid[ETH_ALEN], *_bssid = bssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700635#ifdef CONFIG_P2P
636 u8 p2p_dev_addr[ETH_ALEN];
637#endif /* CONFIG_P2P */
638#ifdef CONFIG_AP
639 u8 *_p2p_dev_addr = NULL;
640#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800641
Dmitry Shmidtc0a8db02013-11-08 11:18:33 -0800642 if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643 _bssid = NULL;
644#ifdef CONFIG_P2P
645 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
646 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
647 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
648 "P2P Device Address '%s'",
649 cmd + 13);
650 return -1;
651 }
652 _p2p_dev_addr = p2p_dev_addr;
653#endif /* CONFIG_P2P */
654 } else if (hwaddr_aton(cmd, bssid)) {
655 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
656 cmd);
657 return -1;
658 }
659
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800660#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661 if (wpa_s->ap_iface)
662 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
663#endif /* CONFIG_AP */
664
665 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
666}
667
668
669static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
670 char *cmd, char *buf,
671 size_t buflen)
672{
673 u8 bssid[ETH_ALEN], *_bssid = bssid;
674 char *pin;
675 int ret;
676
677 pin = os_strchr(cmd, ' ');
678 if (pin)
679 *pin++ = '\0';
680
681 if (os_strcmp(cmd, "any") == 0)
682 _bssid = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800683 else if (os_strcmp(cmd, "get") == 0) {
684 ret = wps_generate_pin();
685 goto done;
686 } else if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
688 cmd);
689 return -1;
690 }
691
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800692#ifdef CONFIG_AP
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800693 if (wpa_s->ap_iface) {
694 int timeout = 0;
695 char *pos;
696
697 if (pin) {
698 pos = os_strchr(pin, ' ');
699 if (pos) {
700 *pos++ = '\0';
701 timeout = atoi(pos);
702 }
703 }
704
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700705 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800706 buf, buflen, timeout);
707 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708#endif /* CONFIG_AP */
709
710 if (pin) {
711 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
712 DEV_PW_DEFAULT);
713 if (ret < 0)
714 return -1;
715 ret = os_snprintf(buf, buflen, "%s", pin);
716 if (ret < 0 || (size_t) ret >= buflen)
717 return -1;
718 return ret;
719 }
720
721 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
722 if (ret < 0)
723 return -1;
724
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800725done:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726 /* Return the generated PIN */
727 ret = os_snprintf(buf, buflen, "%08d", ret);
728 if (ret < 0 || (size_t) ret >= buflen)
729 return -1;
730 return ret;
731}
732
733
734static int wpa_supplicant_ctrl_iface_wps_check_pin(
735 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
736{
737 char pin[9];
738 size_t len;
739 char *pos;
740 int ret;
741
742 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
743 (u8 *) cmd, os_strlen(cmd));
744 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
745 if (*pos < '0' || *pos > '9')
746 continue;
747 pin[len++] = *pos;
748 if (len == 9) {
749 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
750 return -1;
751 }
752 }
753 if (len != 4 && len != 8) {
754 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
755 return -1;
756 }
757 pin[len] = '\0';
758
759 if (len == 8) {
760 unsigned int pin_val;
761 pin_val = atoi(pin);
762 if (!wps_pin_valid(pin_val)) {
763 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
764 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
765 if (ret < 0 || (size_t) ret >= buflen)
766 return -1;
767 return ret;
768 }
769 }
770
771 ret = os_snprintf(buf, buflen, "%s", pin);
772 if (ret < 0 || (size_t) ret >= buflen)
773 return -1;
774
775 return ret;
776}
777
778
Dmitry Shmidt04949592012-07-19 12:16:46 -0700779#ifdef CONFIG_WPS_NFC
780
781static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
782 char *cmd)
783{
784 u8 bssid[ETH_ALEN], *_bssid = bssid;
785
786 if (cmd == NULL || cmd[0] == '\0')
787 _bssid = NULL;
788 else if (hwaddr_aton(cmd, bssid))
789 return -1;
790
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800791 return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
792 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700793}
794
795
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800796static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
797 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
798{
799 int ndef;
800 struct wpabuf *buf;
801 int res;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700802 char *pos;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800803
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700804 pos = os_strchr(cmd, ' ');
805 if (pos)
806 *pos++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800807 if (os_strcmp(cmd, "WPS") == 0)
808 ndef = 0;
809 else if (os_strcmp(cmd, "NDEF") == 0)
810 ndef = 1;
811 else
812 return -1;
813
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700814 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800815 if (buf == NULL)
816 return -1;
817
818 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
819 wpabuf_len(buf));
820 reply[res++] = '\n';
821 reply[res] = '\0';
822
823 wpabuf_free(buf);
824
825 return res;
826}
827
828
Dmitry Shmidt04949592012-07-19 12:16:46 -0700829static int wpa_supplicant_ctrl_iface_wps_nfc_token(
830 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
831{
832 int ndef;
833 struct wpabuf *buf;
834 int res;
835
836 if (os_strcmp(cmd, "WPS") == 0)
837 ndef = 0;
838 else if (os_strcmp(cmd, "NDEF") == 0)
839 ndef = 1;
840 else
841 return -1;
842
843 buf = wpas_wps_nfc_token(wpa_s, ndef);
844 if (buf == NULL)
845 return -1;
846
847 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
848 wpabuf_len(buf));
849 reply[res++] = '\n';
850 reply[res] = '\0';
851
852 wpabuf_free(buf);
853
854 return res;
855}
856
857
858static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
859 struct wpa_supplicant *wpa_s, char *pos)
860{
861 size_t len;
862 struct wpabuf *buf;
863 int ret;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800864 char *freq;
865 int forced_freq = 0;
866
867 freq = strstr(pos, " freq=");
868 if (freq) {
869 *freq = '\0';
870 freq += 6;
871 forced_freq = atoi(freq);
872 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700873
874 len = os_strlen(pos);
875 if (len & 0x01)
876 return -1;
877 len /= 2;
878
879 buf = wpabuf_alloc(len);
880 if (buf == NULL)
881 return -1;
882 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
883 wpabuf_free(buf);
884 return -1;
885 }
886
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800887 ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700888 wpabuf_free(buf);
889
890 return ret;
891}
892
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800893
894static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800895 char *reply, size_t max_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800896 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800897{
898 struct wpabuf *buf;
899 int res;
900
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800901 buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800902 if (buf == NULL)
903 return -1;
904
905 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
906 wpabuf_len(buf));
907 reply[res++] = '\n';
908 reply[res] = '\0';
909
910 wpabuf_free(buf);
911
912 return res;
913}
914
915
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800916#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800917static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
918 char *reply, size_t max_len,
919 int ndef)
920{
921 struct wpabuf *buf;
922 int res;
923
924 buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
925 if (buf == NULL) {
926 wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
927 return -1;
928 }
929
930 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
931 wpabuf_len(buf));
932 reply[res++] = '\n';
933 reply[res] = '\0';
934
935 wpabuf_free(buf);
936
937 return res;
938}
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800939#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800940
941
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800942static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
943 char *cmd, char *reply,
944 size_t max_len)
945{
946 char *pos;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800947 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800948
949 pos = os_strchr(cmd, ' ');
950 if (pos == NULL)
951 return -1;
952 *pos++ = '\0';
953
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800954 if (os_strcmp(cmd, "WPS") == 0)
955 ndef = 0;
956 else if (os_strcmp(cmd, "NDEF") == 0)
957 ndef = 1;
958 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800959 return -1;
960
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800961 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800962 if (!ndef)
963 return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800964 return wpas_ctrl_nfc_get_handover_req_wps(
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800965 wpa_s, reply, max_len, ndef);
966 }
967
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800968#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800969 if (os_strcmp(pos, "P2P-CR") == 0) {
970 return wpas_ctrl_nfc_get_handover_req_p2p(
971 wpa_s, reply, max_len, ndef);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800972 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800973#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800974
975 return -1;
976}
977
978
979static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800980 char *reply, size_t max_len,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800981 int ndef, int cr, char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800982{
983 struct wpabuf *buf;
984 int res;
985
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800986 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800987 if (buf == NULL)
988 return -1;
989
990 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
991 wpabuf_len(buf));
992 reply[res++] = '\n';
993 reply[res] = '\0';
994
995 wpabuf_free(buf);
996
997 return res;
998}
999
1000
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001001#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001002static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1003 char *reply, size_t max_len,
1004 int ndef, int tag)
1005{
1006 struct wpabuf *buf;
1007 int res;
1008
1009 buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1010 if (buf == NULL)
1011 return -1;
1012
1013 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1014 wpabuf_len(buf));
1015 reply[res++] = '\n';
1016 reply[res] = '\0';
1017
1018 wpabuf_free(buf);
1019
1020 return res;
1021}
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001022#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001023
1024
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001025static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1026 char *cmd, char *reply,
1027 size_t max_len)
1028{
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001029 char *pos, *pos2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001030 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001031
1032 pos = os_strchr(cmd, ' ');
1033 if (pos == NULL)
1034 return -1;
1035 *pos++ = '\0';
1036
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001037 if (os_strcmp(cmd, "WPS") == 0)
1038 ndef = 0;
1039 else if (os_strcmp(cmd, "NDEF") == 0)
1040 ndef = 1;
1041 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001042 return -1;
1043
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001044 pos2 = os_strchr(pos, ' ');
1045 if (pos2)
1046 *pos2++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001047 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001048 if (!ndef)
1049 return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001050 return wpas_ctrl_nfc_get_handover_sel_wps(
1051 wpa_s, reply, max_len, ndef,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001052 os_strcmp(pos, "WPS-CR") == 0, pos2);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001053 }
1054
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001055#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001056 if (os_strcmp(pos, "P2P-CR") == 0) {
1057 return wpas_ctrl_nfc_get_handover_sel_p2p(
1058 wpa_s, reply, max_len, ndef, 0);
1059 }
1060
1061 if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1062 return wpas_ctrl_nfc_get_handover_sel_p2p(
1063 wpa_s, reply, max_len, ndef, 1);
1064 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001065#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001066
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001067 return -1;
1068}
1069
1070
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001071static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
1072 char *cmd)
1073{
1074 size_t len;
1075 struct wpabuf *buf;
1076 int ret;
1077
1078 len = os_strlen(cmd);
1079 if (len & 0x01)
1080 return -1;
1081 len /= 2;
1082
1083 buf = wpabuf_alloc(len);
1084 if (buf == NULL)
1085 return -1;
1086 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
1087 wpabuf_free(buf);
1088 return -1;
1089 }
1090
1091 ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
1092 wpabuf_free(buf);
1093
1094 return ret;
1095}
1096
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001097
1098static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1099 char *cmd)
1100{
1101 size_t len;
1102 struct wpabuf *req, *sel;
1103 int ret;
1104 char *pos, *role, *type, *pos2;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001105#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001106 char *freq;
1107 int forced_freq = 0;
1108
1109 freq = strstr(cmd, " freq=");
1110 if (freq) {
1111 *freq = '\0';
1112 freq += 6;
1113 forced_freq = atoi(freq);
1114 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001115#endif /* CONFIG_P2P */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001116
1117 role = cmd;
1118 pos = os_strchr(role, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001119 if (pos == NULL) {
1120 wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001121 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001122 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001123 *pos++ = '\0';
1124
1125 type = pos;
1126 pos = os_strchr(type, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001127 if (pos == NULL) {
1128 wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001129 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001130 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001131 *pos++ = '\0';
1132
1133 pos2 = os_strchr(pos, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001134 if (pos2 == NULL) {
1135 wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001136 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001137 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001138 *pos2++ = '\0';
1139
1140 len = os_strlen(pos);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001141 if (len & 0x01) {
1142 wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001143 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001144 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001145 len /= 2;
1146
1147 req = wpabuf_alloc(len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001148 if (req == NULL) {
1149 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001150 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001151 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001152 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001153 wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001154 wpabuf_free(req);
1155 return -1;
1156 }
1157
1158 len = os_strlen(pos2);
1159 if (len & 0x01) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001160 wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001161 wpabuf_free(req);
1162 return -1;
1163 }
1164 len /= 2;
1165
1166 sel = wpabuf_alloc(len);
1167 if (sel == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001168 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001169 wpabuf_free(req);
1170 return -1;
1171 }
1172 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001173 wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001174 wpabuf_free(req);
1175 wpabuf_free(sel);
1176 return -1;
1177 }
1178
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001179 wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1180 role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1181
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001182 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1183 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001184#ifdef CONFIG_AP
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001185 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1186 {
1187 ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1188 if (ret < 0)
1189 ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001190#endif /* CONFIG_AP */
1191#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001192 } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1193 {
1194 ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
1195 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1196 {
1197 ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1198 forced_freq);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001199#endif /* CONFIG_P2P */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001200 } else {
1201 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1202 "reported: role=%s type=%s", role, type);
1203 ret = -1;
1204 }
1205 wpabuf_free(req);
1206 wpabuf_free(sel);
1207
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001208 if (ret)
1209 wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1210
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001211 return ret;
1212}
1213
Dmitry Shmidt04949592012-07-19 12:16:46 -07001214#endif /* CONFIG_WPS_NFC */
1215
1216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1218 char *cmd)
1219{
1220 u8 bssid[ETH_ALEN];
1221 char *pin;
1222 char *new_ssid;
1223 char *new_auth;
1224 char *new_encr;
1225 char *new_key;
1226 struct wps_new_ap_settings ap;
1227
1228 pin = os_strchr(cmd, ' ');
1229 if (pin == NULL)
1230 return -1;
1231 *pin++ = '\0';
1232
1233 if (hwaddr_aton(cmd, bssid)) {
1234 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1235 cmd);
1236 return -1;
1237 }
1238
1239 new_ssid = os_strchr(pin, ' ');
1240 if (new_ssid == NULL)
1241 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1242 *new_ssid++ = '\0';
1243
1244 new_auth = os_strchr(new_ssid, ' ');
1245 if (new_auth == NULL)
1246 return -1;
1247 *new_auth++ = '\0';
1248
1249 new_encr = os_strchr(new_auth, ' ');
1250 if (new_encr == NULL)
1251 return -1;
1252 *new_encr++ = '\0';
1253
1254 new_key = os_strchr(new_encr, ' ');
1255 if (new_key == NULL)
1256 return -1;
1257 *new_key++ = '\0';
1258
1259 os_memset(&ap, 0, sizeof(ap));
1260 ap.ssid_hex = new_ssid;
1261 ap.auth = new_auth;
1262 ap.encr = new_encr;
1263 ap.key_hex = new_key;
1264 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1265}
1266
1267
1268#ifdef CONFIG_AP
1269static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1270 char *cmd, char *buf,
1271 size_t buflen)
1272{
1273 int timeout = 300;
1274 char *pos;
1275 const char *pin_txt;
1276
1277 if (!wpa_s->ap_iface)
1278 return -1;
1279
1280 pos = os_strchr(cmd, ' ');
1281 if (pos)
1282 *pos++ = '\0';
1283
1284 if (os_strcmp(cmd, "disable") == 0) {
1285 wpas_wps_ap_pin_disable(wpa_s);
1286 return os_snprintf(buf, buflen, "OK\n");
1287 }
1288
1289 if (os_strcmp(cmd, "random") == 0) {
1290 if (pos)
1291 timeout = atoi(pos);
1292 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1293 if (pin_txt == NULL)
1294 return -1;
1295 return os_snprintf(buf, buflen, "%s", pin_txt);
1296 }
1297
1298 if (os_strcmp(cmd, "get") == 0) {
1299 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1300 if (pin_txt == NULL)
1301 return -1;
1302 return os_snprintf(buf, buflen, "%s", pin_txt);
1303 }
1304
1305 if (os_strcmp(cmd, "set") == 0) {
1306 char *pin;
1307 if (pos == NULL)
1308 return -1;
1309 pin = pos;
1310 pos = os_strchr(pos, ' ');
1311 if (pos) {
1312 *pos++ = '\0';
1313 timeout = atoi(pos);
1314 }
1315 if (os_strlen(pin) > buflen)
1316 return -1;
1317 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1318 return -1;
1319 return os_snprintf(buf, buflen, "%s", pin);
1320 }
1321
1322 return -1;
1323}
1324#endif /* CONFIG_AP */
1325
1326
1327#ifdef CONFIG_WPS_ER
1328static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1329 char *cmd)
1330{
1331 char *uuid = cmd, *pin, *pos;
1332 u8 addr_buf[ETH_ALEN], *addr = NULL;
1333 pin = os_strchr(uuid, ' ');
1334 if (pin == NULL)
1335 return -1;
1336 *pin++ = '\0';
1337 pos = os_strchr(pin, ' ');
1338 if (pos) {
1339 *pos++ = '\0';
1340 if (hwaddr_aton(pos, addr_buf) == 0)
1341 addr = addr_buf;
1342 }
1343 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1344}
1345
1346
1347static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1348 char *cmd)
1349{
1350 char *uuid = cmd, *pin;
1351 pin = os_strchr(uuid, ' ');
1352 if (pin == NULL)
1353 return -1;
1354 *pin++ = '\0';
1355 return wpas_wps_er_learn(wpa_s, uuid, pin);
1356}
1357
1358
1359static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1360 struct wpa_supplicant *wpa_s, char *cmd)
1361{
1362 char *uuid = cmd, *id;
1363 id = os_strchr(uuid, ' ');
1364 if (id == NULL)
1365 return -1;
1366 *id++ = '\0';
1367 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1368}
1369
1370
1371static int wpa_supplicant_ctrl_iface_wps_er_config(
1372 struct wpa_supplicant *wpa_s, char *cmd)
1373{
1374 char *pin;
1375 char *new_ssid;
1376 char *new_auth;
1377 char *new_encr;
1378 char *new_key;
1379 struct wps_new_ap_settings ap;
1380
1381 pin = os_strchr(cmd, ' ');
1382 if (pin == NULL)
1383 return -1;
1384 *pin++ = '\0';
1385
1386 new_ssid = os_strchr(pin, ' ');
1387 if (new_ssid == NULL)
1388 return -1;
1389 *new_ssid++ = '\0';
1390
1391 new_auth = os_strchr(new_ssid, ' ');
1392 if (new_auth == NULL)
1393 return -1;
1394 *new_auth++ = '\0';
1395
1396 new_encr = os_strchr(new_auth, ' ');
1397 if (new_encr == NULL)
1398 return -1;
1399 *new_encr++ = '\0';
1400
1401 new_key = os_strchr(new_encr, ' ');
1402 if (new_key == NULL)
1403 return -1;
1404 *new_key++ = '\0';
1405
1406 os_memset(&ap, 0, sizeof(ap));
1407 ap.ssid_hex = new_ssid;
1408 ap.auth = new_auth;
1409 ap.encr = new_encr;
1410 ap.key_hex = new_key;
1411 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1412}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001413
1414
1415#ifdef CONFIG_WPS_NFC
1416static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1417 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1418{
1419 int ndef;
1420 struct wpabuf *buf;
1421 int res;
1422 char *uuid;
1423
1424 uuid = os_strchr(cmd, ' ');
1425 if (uuid == NULL)
1426 return -1;
1427 *uuid++ = '\0';
1428
1429 if (os_strcmp(cmd, "WPS") == 0)
1430 ndef = 0;
1431 else if (os_strcmp(cmd, "NDEF") == 0)
1432 ndef = 1;
1433 else
1434 return -1;
1435
1436 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1437 if (buf == NULL)
1438 return -1;
1439
1440 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1441 wpabuf_len(buf));
1442 reply[res++] = '\n';
1443 reply[res] = '\0';
1444
1445 wpabuf_free(buf);
1446
1447 return res;
1448}
1449#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450#endif /* CONFIG_WPS_ER */
1451
1452#endif /* CONFIG_WPS */
1453
1454
1455#ifdef CONFIG_IBSS_RSN
1456static int wpa_supplicant_ctrl_iface_ibss_rsn(
1457 struct wpa_supplicant *wpa_s, char *addr)
1458{
1459 u8 peer[ETH_ALEN];
1460
1461 if (hwaddr_aton(addr, peer)) {
1462 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1463 "address '%s'", addr);
1464 return -1;
1465 }
1466
1467 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1468 MAC2STR(peer));
1469
1470 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1471}
1472#endif /* CONFIG_IBSS_RSN */
1473
1474
1475static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1476 char *rsp)
1477{
1478#ifdef IEEE8021X_EAPOL
1479 char *pos, *id_pos;
1480 int id;
1481 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001482
1483 pos = os_strchr(rsp, '-');
1484 if (pos == NULL)
1485 return -1;
1486 *pos++ = '\0';
1487 id_pos = pos;
1488 pos = os_strchr(pos, ':');
1489 if (pos == NULL)
1490 return -1;
1491 *pos++ = '\0';
1492 id = atoi(id_pos);
1493 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1494 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1495 (u8 *) pos, os_strlen(pos));
1496
1497 ssid = wpa_config_get_network(wpa_s->conf, id);
1498 if (ssid == NULL) {
1499 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1500 "to update", id);
1501 return -1;
1502 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001503
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001504 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1505 pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001506#else /* IEEE8021X_EAPOL */
1507 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1508 return -1;
1509#endif /* IEEE8021X_EAPOL */
1510}
1511
1512
1513static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1514 const char *params,
1515 char *buf, size_t buflen)
1516{
1517 char *pos, *end, tmp[30];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001518 int res, verbose, wps, ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001519#ifdef CONFIG_HS20
1520 const u8 *hs20;
1521#endif /* CONFIG_HS20 */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001522
Dmitry Shmidt56052862013-10-04 10:23:25 -07001523 if (os_strcmp(params, "-DRIVER") == 0)
1524 return wpa_drv_status(wpa_s, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001525 verbose = os_strcmp(params, "-VERBOSE") == 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001526 wps = os_strcmp(params, "-WPS") == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001527 pos = buf;
1528 end = buf + buflen;
1529 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1530 struct wpa_ssid *ssid = wpa_s->current_ssid;
1531 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1532 MAC2STR(wpa_s->bssid));
1533 if (ret < 0 || ret >= end - pos)
1534 return pos - buf;
1535 pos += ret;
1536 if (ssid) {
1537 u8 *_ssid = ssid->ssid;
1538 size_t ssid_len = ssid->ssid_len;
1539 u8 ssid_buf[MAX_SSID_LEN];
1540 if (ssid_len == 0) {
1541 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1542 if (_res < 0)
1543 ssid_len = 0;
1544 else
1545 ssid_len = _res;
1546 _ssid = ssid_buf;
1547 }
1548 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1549 wpa_ssid_txt(_ssid, ssid_len),
1550 ssid->id);
1551 if (ret < 0 || ret >= end - pos)
1552 return pos - buf;
1553 pos += ret;
1554
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001555 if (wps && ssid->passphrase &&
1556 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1557 (ssid->mode == WPAS_MODE_AP ||
1558 ssid->mode == WPAS_MODE_P2P_GO)) {
1559 ret = os_snprintf(pos, end - pos,
1560 "passphrase=%s\n",
1561 ssid->passphrase);
1562 if (ret < 0 || ret >= end - pos)
1563 return pos - buf;
1564 pos += ret;
1565 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 if (ssid->id_str) {
1567 ret = os_snprintf(pos, end - pos,
1568 "id_str=%s\n",
1569 ssid->id_str);
1570 if (ret < 0 || ret >= end - pos)
1571 return pos - buf;
1572 pos += ret;
1573 }
1574
1575 switch (ssid->mode) {
1576 case WPAS_MODE_INFRA:
1577 ret = os_snprintf(pos, end - pos,
1578 "mode=station\n");
1579 break;
1580 case WPAS_MODE_IBSS:
1581 ret = os_snprintf(pos, end - pos,
1582 "mode=IBSS\n");
1583 break;
1584 case WPAS_MODE_AP:
1585 ret = os_snprintf(pos, end - pos,
1586 "mode=AP\n");
1587 break;
1588 case WPAS_MODE_P2P_GO:
1589 ret = os_snprintf(pos, end - pos,
1590 "mode=P2P GO\n");
1591 break;
1592 case WPAS_MODE_P2P_GROUP_FORMATION:
1593 ret = os_snprintf(pos, end - pos,
1594 "mode=P2P GO - group "
1595 "formation\n");
1596 break;
1597 default:
1598 ret = 0;
1599 break;
1600 }
1601 if (ret < 0 || ret >= end - pos)
1602 return pos - buf;
1603 pos += ret;
1604 }
1605
1606#ifdef CONFIG_AP
1607 if (wpa_s->ap_iface) {
1608 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1609 end - pos,
1610 verbose);
1611 } else
1612#endif /* CONFIG_AP */
1613 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1614 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001615#ifdef CONFIG_SAE
1616 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001617#ifdef CONFIG_AP
1618 !wpa_s->ap_iface &&
1619#endif /* CONFIG_AP */
1620 wpa_s->sme.sae.state == SAE_ACCEPTED) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001621 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1622 wpa_s->sme.sae.group);
1623 if (ret < 0 || ret >= end - pos)
1624 return pos - buf;
1625 pos += ret;
1626 }
1627#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1629 wpa_supplicant_state_txt(wpa_s->wpa_state));
1630 if (ret < 0 || ret >= end - pos)
1631 return pos - buf;
1632 pos += ret;
1633
1634 if (wpa_s->l2 &&
1635 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1636 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
1637 if (ret < 0 || ret >= end - pos)
1638 return pos - buf;
1639 pos += ret;
1640 }
1641
1642#ifdef CONFIG_P2P
1643 if (wpa_s->global->p2p) {
1644 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1645 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
1646 if (ret < 0 || ret >= end - pos)
1647 return pos - buf;
1648 pos += ret;
1649 }
1650#endif /* CONFIG_P2P */
1651
1652 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1653 MAC2STR(wpa_s->own_addr));
1654 if (ret < 0 || ret >= end - pos)
1655 return pos - buf;
1656 pos += ret;
1657
Dmitry Shmidt04949592012-07-19 12:16:46 -07001658#ifdef CONFIG_HS20
1659 if (wpa_s->current_bss &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001660 (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1661 HS20_IE_VENDOR_TYPE)) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001662 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1663 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001664 int release = 1;
1665 if (hs20[1] >= 5) {
1666 u8 rel_num = (hs20[6] & 0xf0) >> 4;
1667 release = rel_num + 1;
1668 }
1669 ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001670 if (ret < 0 || ret >= end - pos)
1671 return pos - buf;
1672 pos += ret;
1673 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001674
1675 if (wpa_s->current_ssid) {
1676 struct wpa_cred *cred;
1677 char *type;
1678
1679 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001680 size_t i;
1681
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001682 if (wpa_s->current_ssid->parent_cred != cred)
1683 continue;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001684
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001685 if (cred->provisioning_sp) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001686 ret = os_snprintf(pos, end - pos,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001687 "provisioning_sp=%s\n",
1688 cred->provisioning_sp);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001689 if (ret < 0 || ret >= end - pos)
1690 return pos - buf;
1691 pos += ret;
1692 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001693
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001694 if (!cred->domain)
1695 goto no_domain;
1696
1697 i = 0;
1698 if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
1699 struct wpabuf *names =
1700 wpa_s->current_bss->anqp->domain_name;
1701 for (i = 0; names && i < cred->num_domain; i++)
1702 {
1703 if (domain_name_list_contains(
1704 names, cred->domain[i], 1))
1705 break;
1706 }
1707 if (i == cred->num_domain)
1708 i = 0; /* show first entry by default */
1709 }
1710 ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
1711 cred->domain[i]);
1712 if (ret < 0 || ret >= end - pos)
1713 return pos - buf;
1714 pos += ret;
1715
1716 no_domain:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001717 if (wpa_s->current_bss == NULL ||
1718 wpa_s->current_bss->anqp == NULL)
1719 res = -1;
1720 else
1721 res = interworking_home_sp_cred(
1722 wpa_s, cred,
1723 wpa_s->current_bss->anqp->domain_name);
1724 if (res > 0)
1725 type = "home";
1726 else if (res == 0)
1727 type = "roaming";
1728 else
1729 type = "unknown";
1730
1731 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
1732 if (ret < 0 || ret >= end - pos)
1733 return pos - buf;
1734 pos += ret;
1735
1736 break;
1737 }
1738 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001739#endif /* CONFIG_HS20 */
1740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1742 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1743 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1744 verbose);
1745 if (res >= 0)
1746 pos += res;
1747 }
1748
1749 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1750 if (res >= 0)
1751 pos += res;
1752
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001753#ifdef CONFIG_WPS
1754 {
1755 char uuid_str[100];
1756 uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
1757 ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
1758 if (ret < 0 || ret >= end - pos)
1759 return pos - buf;
1760 pos += ret;
1761 }
1762#endif /* CONFIG_WPS */
1763
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001764#ifdef ANDROID
1765 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
Irfan Sherifff20a4432012-04-16 16:48:34 -07001766 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001767 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
Irfan Sherifff20a4432012-04-16 16:48:34 -07001768 wpa_s->wpa_state,
Irfan Sheriffe78e7672012-08-01 11:10:15 -07001769 MAC2STR(wpa_s->bssid),
andy2_kuo5b5fb022012-05-22 11:53:07 -07001770 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1771 wpa_ssid_txt(wpa_s->current_ssid->ssid,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001772 wpa_s->current_ssid->ssid_len) : "");
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001773 if (wpa_s->wpa_state == WPA_COMPLETED) {
1774 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001775 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
1776 "- connection to " MACSTR
1777 " completed %s [id=%d id_str=%s]",
1778 MAC2STR(wpa_s->bssid), "(auth)",
1779 ssid ? ssid->id : -1,
1780 ssid && ssid->id_str ? ssid->id_str : "");
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001781 }
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001782#endif /* ANDROID */
1783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001784 return pos - buf;
1785}
1786
1787
1788static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1789 char *cmd)
1790{
1791 char *pos;
1792 int id;
1793 struct wpa_ssid *ssid;
1794 u8 bssid[ETH_ALEN];
1795
1796 /* cmd: "<network id> <BSSID>" */
1797 pos = os_strchr(cmd, ' ');
1798 if (pos == NULL)
1799 return -1;
1800 *pos++ = '\0';
1801 id = atoi(cmd);
1802 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1803 if (hwaddr_aton(pos, bssid)) {
1804 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1805 return -1;
1806 }
1807
1808 ssid = wpa_config_get_network(wpa_s->conf, id);
1809 if (ssid == NULL) {
1810 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1811 "to update", id);
1812 return -1;
1813 }
1814
1815 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1816 ssid->bssid_set = !is_zero_ether_addr(bssid);
1817
1818 return 0;
1819}
1820
1821
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001822static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001823 char *cmd, char *buf,
1824 size_t buflen)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001825{
1826 u8 bssid[ETH_ALEN];
1827 struct wpa_blacklist *e;
1828 char *pos, *end;
1829 int ret;
1830
1831 /* cmd: "BLACKLIST [<BSSID>]" */
1832 if (*cmd == '\0') {
1833 pos = buf;
1834 end = buf + buflen;
1835 e = wpa_s->blacklist;
1836 while (e) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001837 ret = os_snprintf(pos, end - pos, MACSTR "\n",
1838 MAC2STR(e->bssid));
1839 if (ret < 0 || ret >= end - pos)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001840 return pos - buf;
1841 pos += ret;
1842 e = e->next;
1843 }
1844 return pos - buf;
1845 }
1846
1847 cmd++;
1848 if (os_strncmp(cmd, "clear", 5) == 0) {
1849 wpa_blacklist_clear(wpa_s);
1850 os_memcpy(buf, "OK\n", 3);
1851 return 3;
1852 }
1853
1854 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1855 if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001856 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001857 return -1;
1858 }
1859
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001860 /*
1861 * Add the BSSID twice, so its count will be 2, causing it to be
1862 * skipped when processing scan results.
1863 */
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001864 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001865 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001866 return -1;
1867 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001868 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001869 return -1;
1870 os_memcpy(buf, "OK\n", 3);
1871 return 3;
1872}
1873
1874
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001875static const char * debug_level_str(int level)
1876{
1877 switch (level) {
1878 case MSG_EXCESSIVE:
1879 return "EXCESSIVE";
1880 case MSG_MSGDUMP:
1881 return "MSGDUMP";
1882 case MSG_DEBUG:
1883 return "DEBUG";
1884 case MSG_INFO:
1885 return "INFO";
1886 case MSG_WARNING:
1887 return "WARNING";
1888 case MSG_ERROR:
1889 return "ERROR";
1890 default:
1891 return "?";
1892 }
1893}
1894
1895
1896static int str_to_debug_level(const char *s)
1897{
1898 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1899 return MSG_EXCESSIVE;
1900 if (os_strcasecmp(s, "MSGDUMP") == 0)
1901 return MSG_MSGDUMP;
1902 if (os_strcasecmp(s, "DEBUG") == 0)
1903 return MSG_DEBUG;
1904 if (os_strcasecmp(s, "INFO") == 0)
1905 return MSG_INFO;
1906 if (os_strcasecmp(s, "WARNING") == 0)
1907 return MSG_WARNING;
1908 if (os_strcasecmp(s, "ERROR") == 0)
1909 return MSG_ERROR;
1910 return -1;
1911}
1912
1913
1914static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1915 char *cmd, char *buf,
1916 size_t buflen)
1917{
1918 char *pos, *end, *stamp;
1919 int ret;
1920
1921 if (cmd == NULL) {
1922 return -1;
1923 }
1924
1925 /* cmd: "LOG_LEVEL [<level>]" */
1926 if (*cmd == '\0') {
1927 pos = buf;
1928 end = buf + buflen;
1929 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1930 "Timestamp: %d\n",
1931 debug_level_str(wpa_debug_level),
1932 wpa_debug_timestamp);
1933 if (ret < 0 || ret >= end - pos)
1934 ret = 0;
1935
1936 return ret;
1937 }
1938
1939 while (*cmd == ' ')
1940 cmd++;
1941
1942 stamp = os_strchr(cmd, ' ');
1943 if (stamp) {
1944 *stamp++ = '\0';
1945 while (*stamp == ' ') {
1946 stamp++;
1947 }
1948 }
1949
1950 if (cmd && os_strlen(cmd)) {
1951 int level = str_to_debug_level(cmd);
1952 if (level < 0)
1953 return -1;
1954 wpa_debug_level = level;
1955 }
1956
1957 if (stamp && os_strlen(stamp))
1958 wpa_debug_timestamp = atoi(stamp);
1959
1960 os_memcpy(buf, "OK\n", 3);
1961 return 3;
1962}
1963
1964
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965static int wpa_supplicant_ctrl_iface_list_networks(
1966 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1967{
1968 char *pos, *end;
1969 struct wpa_ssid *ssid;
1970 int ret;
1971
1972 pos = buf;
1973 end = buf + buflen;
1974 ret = os_snprintf(pos, end - pos,
1975 "network id / ssid / bssid / flags\n");
1976 if (ret < 0 || ret >= end - pos)
1977 return pos - buf;
1978 pos += ret;
1979
1980 ssid = wpa_s->conf->ssid;
1981 while (ssid) {
1982 ret = os_snprintf(pos, end - pos, "%d\t%s",
1983 ssid->id,
1984 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1985 if (ret < 0 || ret >= end - pos)
1986 return pos - buf;
1987 pos += ret;
1988 if (ssid->bssid_set) {
1989 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
1990 MAC2STR(ssid->bssid));
1991 } else {
1992 ret = os_snprintf(pos, end - pos, "\tany");
1993 }
1994 if (ret < 0 || ret >= end - pos)
1995 return pos - buf;
1996 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001997 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001998 ssid == wpa_s->current_ssid ?
1999 "[CURRENT]" : "",
2000 ssid->disabled ? "[DISABLED]" : "",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002001 ssid->disabled_until.sec ?
2002 "[TEMP-DISABLED]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2004 "");
2005 if (ret < 0 || ret >= end - pos)
2006 return pos - buf;
2007 pos += ret;
2008 ret = os_snprintf(pos, end - pos, "\n");
2009 if (ret < 0 || ret >= end - pos)
2010 return pos - buf;
2011 pos += ret;
2012
2013 ssid = ssid->next;
2014 }
2015
2016 return pos - buf;
2017}
2018
2019
2020static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2021{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002022 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023 ret = os_snprintf(pos, end - pos, "-");
2024 if (ret < 0 || ret >= end - pos)
2025 return pos;
2026 pos += ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002027 ret = wpa_write_ciphers(pos, end, cipher, "+");
2028 if (ret < 0)
2029 return pos;
2030 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031 return pos;
2032}
2033
2034
2035static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2036 const u8 *ie, size_t ie_len)
2037{
2038 struct wpa_ie_data data;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002039 char *start;
2040 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041
2042 ret = os_snprintf(pos, end - pos, "[%s-", proto);
2043 if (ret < 0 || ret >= end - pos)
2044 return pos;
2045 pos += ret;
2046
2047 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2048 ret = os_snprintf(pos, end - pos, "?]");
2049 if (ret < 0 || ret >= end - pos)
2050 return pos;
2051 pos += ret;
2052 return pos;
2053 }
2054
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002055 start = pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002057 ret = os_snprintf(pos, end - pos, "%sEAP",
2058 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059 if (ret < 0 || ret >= end - pos)
2060 return pos;
2061 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062 }
2063 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002064 ret = os_snprintf(pos, end - pos, "%sPSK",
2065 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002066 if (ret < 0 || ret >= end - pos)
2067 return pos;
2068 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002069 }
2070 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002071 ret = os_snprintf(pos, end - pos, "%sNone",
2072 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 if (ret < 0 || ret >= end - pos)
2074 return pos;
2075 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076 }
2077#ifdef CONFIG_IEEE80211R
2078 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2079 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002080 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081 if (ret < 0 || ret >= end - pos)
2082 return pos;
2083 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084 }
2085 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2086 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002087 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002088 if (ret < 0 || ret >= end - pos)
2089 return pos;
2090 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002091 }
2092#endif /* CONFIG_IEEE80211R */
2093#ifdef CONFIG_IEEE80211W
2094 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2095 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002096 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097 if (ret < 0 || ret >= end - pos)
2098 return pos;
2099 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100 }
2101 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2102 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002103 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 if (ret < 0 || ret >= end - pos)
2105 return pos;
2106 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107 }
2108#endif /* CONFIG_IEEE80211W */
2109
2110 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2111
2112 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2113 ret = os_snprintf(pos, end - pos, "-preauth");
2114 if (ret < 0 || ret >= end - pos)
2115 return pos;
2116 pos += ret;
2117 }
2118
2119 ret = os_snprintf(pos, end - pos, "]");
2120 if (ret < 0 || ret >= end - pos)
2121 return pos;
2122 pos += ret;
2123
2124 return pos;
2125}
2126
2127
2128#ifdef CONFIG_WPS
2129static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2130 char *pos, char *end,
2131 struct wpabuf *wps_ie)
2132{
2133 int ret;
2134 const char *txt;
2135
2136 if (wps_ie == NULL)
2137 return pos;
2138 if (wps_is_selected_pbc_registrar(wps_ie))
2139 txt = "[WPS-PBC]";
2140#ifdef CONFIG_WPS2
2141 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2142 txt = "[WPS-AUTH]";
2143#endif /* CONFIG_WPS2 */
2144 else if (wps_is_selected_pin_registrar(wps_ie))
2145 txt = "[WPS-PIN]";
2146 else
2147 txt = "[WPS]";
2148
2149 ret = os_snprintf(pos, end - pos, "%s", txt);
2150 if (ret >= 0 && ret < end - pos)
2151 pos += ret;
2152 wpabuf_free(wps_ie);
2153 return pos;
2154}
2155#endif /* CONFIG_WPS */
2156
2157
2158static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2159 char *pos, char *end,
2160 const struct wpa_bss *bss)
2161{
2162#ifdef CONFIG_WPS
2163 struct wpabuf *wps_ie;
2164 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2165 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2166#else /* CONFIG_WPS */
2167 return pos;
2168#endif /* CONFIG_WPS */
2169}
2170
2171
2172/* Format one result on one text line into a buffer. */
2173static int wpa_supplicant_ctrl_iface_scan_result(
2174 struct wpa_supplicant *wpa_s,
2175 const struct wpa_bss *bss, char *buf, size_t buflen)
2176{
2177 char *pos, *end;
2178 int ret;
2179 const u8 *ie, *ie2, *p2p;
2180
2181 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07002182 if (!p2p)
2183 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2185 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2186 0)
2187 return 0; /* Do not show P2P listen discovery results here */
2188
2189 pos = buf;
2190 end = buf + buflen;
2191
2192 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2193 MAC2STR(bss->bssid), bss->freq, bss->level);
2194 if (ret < 0 || ret >= end - pos)
2195 return -1;
2196 pos += ret;
2197 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2198 if (ie)
2199 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2200 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2201 if (ie2)
2202 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
2203 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2204 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2205 ret = os_snprintf(pos, end - pos, "[WEP]");
2206 if (ret < 0 || ret >= end - pos)
2207 return -1;
2208 pos += ret;
2209 }
2210 if (bss->caps & IEEE80211_CAP_IBSS) {
2211 ret = os_snprintf(pos, end - pos, "[IBSS]");
2212 if (ret < 0 || ret >= end - pos)
2213 return -1;
2214 pos += ret;
2215 }
2216 if (bss->caps & IEEE80211_CAP_ESS) {
2217 ret = os_snprintf(pos, end - pos, "[ESS]");
2218 if (ret < 0 || ret >= end - pos)
2219 return -1;
2220 pos += ret;
2221 }
2222 if (p2p) {
2223 ret = os_snprintf(pos, end - pos, "[P2P]");
2224 if (ret < 0 || ret >= end - pos)
2225 return -1;
2226 pos += ret;
2227 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002228#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002229 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002230 ret = os_snprintf(pos, end - pos, "[HS20]");
2231 if (ret < 0 || ret >= end - pos)
2232 return -1;
2233 pos += ret;
2234 }
2235#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236
2237 ret = os_snprintf(pos, end - pos, "\t%s",
2238 wpa_ssid_txt(bss->ssid, bss->ssid_len));
2239 if (ret < 0 || ret >= end - pos)
2240 return -1;
2241 pos += ret;
2242
2243 ret = os_snprintf(pos, end - pos, "\n");
2244 if (ret < 0 || ret >= end - pos)
2245 return -1;
2246 pos += ret;
2247
2248 return pos - buf;
2249}
2250
2251
2252static int wpa_supplicant_ctrl_iface_scan_results(
2253 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2254{
2255 char *pos, *end;
2256 struct wpa_bss *bss;
2257 int ret;
2258
2259 pos = buf;
2260 end = buf + buflen;
2261 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2262 "flags / ssid\n");
2263 if (ret < 0 || ret >= end - pos)
2264 return pos - buf;
2265 pos += ret;
2266
2267 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2268 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2269 end - pos);
2270 if (ret < 0 || ret >= end - pos)
2271 return pos - buf;
2272 pos += ret;
2273 }
2274
2275 return pos - buf;
2276}
2277
2278
2279static int wpa_supplicant_ctrl_iface_select_network(
2280 struct wpa_supplicant *wpa_s, char *cmd)
2281{
2282 int id;
2283 struct wpa_ssid *ssid;
2284
2285 /* cmd: "<network id>" or "any" */
2286 if (os_strcmp(cmd, "any") == 0) {
2287 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2288 ssid = NULL;
2289 } else {
2290 id = atoi(cmd);
2291 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2292
2293 ssid = wpa_config_get_network(wpa_s->conf, id);
2294 if (ssid == NULL) {
2295 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2296 "network id=%d", id);
2297 return -1;
2298 }
2299 if (ssid->disabled == 2) {
2300 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2301 "SELECT_NETWORK with persistent P2P group");
2302 return -1;
2303 }
2304 }
2305
2306 wpa_supplicant_select_network(wpa_s, ssid);
2307
2308 return 0;
2309}
2310
2311
2312static int wpa_supplicant_ctrl_iface_enable_network(
2313 struct wpa_supplicant *wpa_s, char *cmd)
2314{
2315 int id;
2316 struct wpa_ssid *ssid;
2317
2318 /* cmd: "<network id>" or "all" */
2319 if (os_strcmp(cmd, "all") == 0) {
2320 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2321 ssid = NULL;
2322 } else {
2323 id = atoi(cmd);
2324 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2325
2326 ssid = wpa_config_get_network(wpa_s->conf, id);
2327 if (ssid == NULL) {
2328 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2329 "network id=%d", id);
2330 return -1;
2331 }
2332 if (ssid->disabled == 2) {
2333 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2334 "ENABLE_NETWORK with persistent P2P group");
2335 return -1;
2336 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002337
2338 if (os_strstr(cmd, " no-connect")) {
2339 ssid->disabled = 0;
2340 return 0;
2341 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 }
2343 wpa_supplicant_enable_network(wpa_s, ssid);
2344
2345 return 0;
2346}
2347
2348
2349static int wpa_supplicant_ctrl_iface_disable_network(
2350 struct wpa_supplicant *wpa_s, char *cmd)
2351{
2352 int id;
2353 struct wpa_ssid *ssid;
2354
2355 /* cmd: "<network id>" or "all" */
2356 if (os_strcmp(cmd, "all") == 0) {
2357 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2358 ssid = NULL;
2359 } else {
2360 id = atoi(cmd);
2361 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2362
2363 ssid = wpa_config_get_network(wpa_s->conf, id);
2364 if (ssid == NULL) {
2365 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2366 "network id=%d", id);
2367 return -1;
2368 }
2369 if (ssid->disabled == 2) {
2370 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2371 "DISABLE_NETWORK with persistent P2P "
2372 "group");
2373 return -1;
2374 }
2375 }
2376 wpa_supplicant_disable_network(wpa_s, ssid);
2377
2378 return 0;
2379}
2380
2381
2382static int wpa_supplicant_ctrl_iface_add_network(
2383 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2384{
2385 struct wpa_ssid *ssid;
2386 int ret;
2387
2388 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2389
2390 ssid = wpa_config_add_network(wpa_s->conf);
2391 if (ssid == NULL)
2392 return -1;
2393
2394 wpas_notify_network_added(wpa_s, ssid);
2395
2396 ssid->disabled = 1;
2397 wpa_config_set_network_defaults(ssid);
2398
2399 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
2400 if (ret < 0 || (size_t) ret >= buflen)
2401 return -1;
2402 return ret;
2403}
2404
2405
2406static int wpa_supplicant_ctrl_iface_remove_network(
2407 struct wpa_supplicant *wpa_s, char *cmd)
2408{
2409 int id;
2410 struct wpa_ssid *ssid;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002411 int was_disabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412
2413 /* cmd: "<network id>" or "all" */
2414 if (os_strcmp(cmd, "all") == 0) {
2415 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002416 if (wpa_s->sched_scanning)
2417 wpa_supplicant_cancel_sched_scan(wpa_s);
2418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002419 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002420 if (wpa_s->current_ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002421#ifdef CONFIG_SME
2422 wpa_s->sme.prev_bssid_set = 0;
2423#endif /* CONFIG_SME */
Jouni Malinen75ecf522011-06-27 15:19:46 -07002424 wpa_sm_set_config(wpa_s->wpa, NULL);
2425 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002426 wpa_supplicant_deauthenticate(
2427 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002428 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002429 ssid = wpa_s->conf->ssid;
2430 while (ssid) {
2431 struct wpa_ssid *remove_ssid = ssid;
2432 id = ssid->id;
2433 ssid = ssid->next;
2434 wpas_notify_network_removed(wpa_s, remove_ssid);
2435 wpa_config_remove_network(wpa_s->conf, id);
2436 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002437 return 0;
2438 }
2439
2440 id = atoi(cmd);
2441 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2442
2443 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002444 if (ssid)
2445 wpas_notify_network_removed(wpa_s, ssid);
Deepthi Gowria831d782012-09-03 11:55:38 +03002446 if (ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2448 "id=%d", id);
2449 return -1;
2450 }
2451
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002452 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002453#ifdef CONFIG_SME
2454 wpa_s->sme.prev_bssid_set = 0;
2455#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002456 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002457 * Invalidate the EAP session cache if the current or
2458 * previously used network is removed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002459 */
2460 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002461 }
2462
2463 if (ssid == wpa_s->current_ssid) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002464 wpa_sm_set_config(wpa_s->wpa, NULL);
2465 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002467 wpa_supplicant_deauthenticate(wpa_s,
2468 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469 }
2470
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002471 was_disabled = ssid->disabled;
2472
Deepthi Gowria831d782012-09-03 11:55:38 +03002473 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2474 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2475 "network id=%d", id);
2476 return -1;
2477 }
2478
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002479 if (!was_disabled && wpa_s->sched_scanning) {
2480 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2481 "network from filters");
2482 wpa_supplicant_cancel_sched_scan(wpa_s);
2483 wpa_supplicant_req_scan(wpa_s, 0, 0);
2484 }
2485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486 return 0;
2487}
2488
2489
2490static int wpa_supplicant_ctrl_iface_set_network(
2491 struct wpa_supplicant *wpa_s, char *cmd)
2492{
2493 int id;
2494 struct wpa_ssid *ssid;
2495 char *name, *value;
2496
2497 /* cmd: "<network id> <variable name> <value>" */
2498 name = os_strchr(cmd, ' ');
2499 if (name == NULL)
2500 return -1;
2501 *name++ = '\0';
2502
2503 value = os_strchr(name, ' ');
2504 if (value == NULL)
2505 return -1;
2506 *value++ = '\0';
2507
2508 id = atoi(cmd);
2509 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2510 id, name);
2511 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2512 (u8 *) value, os_strlen(value));
2513
2514 ssid = wpa_config_get_network(wpa_s->conf, id);
2515 if (ssid == NULL) {
2516 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2517 "id=%d", id);
2518 return -1;
2519 }
2520
2521 if (wpa_config_set(ssid, name, value, 0) < 0) {
2522 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2523 "variable '%s'", name);
2524 return -1;
2525 }
2526
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002527 if (os_strcmp(name, "bssid") != 0 &&
2528 os_strcmp(name, "priority") != 0)
2529 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002530
2531 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 /*
2533 * Invalidate the EAP session cache if anything in the current
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002534 * or previously used configuration changes.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535 */
2536 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2537 }
2538
2539 if ((os_strcmp(name, "psk") == 0 &&
2540 value[0] == '"' && ssid->ssid_len) ||
2541 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2542 wpa_config_update_psk(ssid);
2543 else if (os_strcmp(name, "priority") == 0)
2544 wpa_config_update_prio_list(wpa_s->conf);
2545
2546 return 0;
2547}
2548
2549
2550static int wpa_supplicant_ctrl_iface_get_network(
2551 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2552{
2553 int id;
2554 size_t res;
2555 struct wpa_ssid *ssid;
2556 char *name, *value;
2557
2558 /* cmd: "<network id> <variable name>" */
2559 name = os_strchr(cmd, ' ');
2560 if (name == NULL || buflen == 0)
2561 return -1;
2562 *name++ = '\0';
2563
2564 id = atoi(cmd);
2565 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2566 id, name);
2567
2568 ssid = wpa_config_get_network(wpa_s->conf, id);
2569 if (ssid == NULL) {
2570 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2571 "id=%d", id);
2572 return -1;
2573 }
2574
2575 value = wpa_config_get_no_key(ssid, name);
2576 if (value == NULL) {
2577 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2578 "variable '%s'", name);
2579 return -1;
2580 }
2581
2582 res = os_strlcpy(buf, value, buflen);
2583 if (res >= buflen) {
2584 os_free(value);
2585 return -1;
2586 }
2587
2588 os_free(value);
2589
2590 return res;
2591}
2592
2593
Dmitry Shmidt04949592012-07-19 12:16:46 -07002594static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2595 char *buf, size_t buflen)
2596{
2597 char *pos, *end;
2598 struct wpa_cred *cred;
2599 int ret;
2600
2601 pos = buf;
2602 end = buf + buflen;
2603 ret = os_snprintf(pos, end - pos,
2604 "cred id / realm / username / domain / imsi\n");
2605 if (ret < 0 || ret >= end - pos)
2606 return pos - buf;
2607 pos += ret;
2608
2609 cred = wpa_s->conf->cred;
2610 while (cred) {
2611 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2612 cred->id, cred->realm ? cred->realm : "",
2613 cred->username ? cred->username : "",
Dmitry Shmidt051af732013-10-22 13:52:46 -07002614 cred->domain ? cred->domain[0] : "",
Dmitry Shmidt04949592012-07-19 12:16:46 -07002615 cred->imsi ? cred->imsi : "");
2616 if (ret < 0 || ret >= end - pos)
2617 return pos - buf;
2618 pos += ret;
2619
2620 cred = cred->next;
2621 }
2622
2623 return pos - buf;
2624}
2625
2626
2627static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2628 char *buf, size_t buflen)
2629{
2630 struct wpa_cred *cred;
2631 int ret;
2632
2633 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2634
2635 cred = wpa_config_add_cred(wpa_s->conf);
2636 if (cred == NULL)
2637 return -1;
2638
2639 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2640 if (ret < 0 || (size_t) ret >= buflen)
2641 return -1;
2642 return ret;
2643}
2644
2645
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002646static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2647 struct wpa_cred *cred)
2648{
2649 struct wpa_ssid *ssid;
2650 char str[20];
2651
2652 if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
2653 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2654 return -1;
2655 }
2656
2657 /* Remove any network entry created based on the removed credential */
2658 ssid = wpa_s->conf->ssid;
2659 while (ssid) {
2660 if (ssid->parent_cred == cred) {
2661 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
2662 "used the removed credential", ssid->id);
2663 os_snprintf(str, sizeof(str), "%d", ssid->id);
2664 ssid = ssid->next;
2665 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
2666 } else
2667 ssid = ssid->next;
2668 }
2669
2670 return 0;
2671}
2672
2673
Dmitry Shmidt04949592012-07-19 12:16:46 -07002674static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2675 char *cmd)
2676{
2677 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002678 struct wpa_cred *cred, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002679
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002680 /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
2681 * "provisioning_sp=<FQDN> */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002682 if (os_strcmp(cmd, "all") == 0) {
2683 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2684 cred = wpa_s->conf->cred;
2685 while (cred) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002686 prev = cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002687 cred = cred->next;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002688 wpas_ctrl_remove_cred(wpa_s, prev);
2689 }
2690 return 0;
2691 }
2692
2693 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
2694 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
2695 cmd + 8);
2696 cred = wpa_s->conf->cred;
2697 while (cred) {
2698 prev = cred;
2699 cred = cred->next;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002700 if (prev->domain) {
2701 size_t i;
2702 for (i = 0; i < prev->num_domain; i++) {
2703 if (os_strcmp(prev->domain[i], cmd + 8)
2704 != 0)
2705 continue;
2706 wpas_ctrl_remove_cred(wpa_s, prev);
2707 break;
2708 }
2709 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002710 }
2711 return 0;
2712 }
2713
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002714 if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
2715 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
2716 cmd + 16);
2717 cred = wpa_s->conf->cred;
2718 while (cred) {
2719 prev = cred;
2720 cred = cred->next;
2721 if (prev->provisioning_sp &&
2722 os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
2723 wpas_ctrl_remove_cred(wpa_s, prev);
2724 }
2725 return 0;
2726 }
2727
Dmitry Shmidt04949592012-07-19 12:16:46 -07002728 id = atoi(cmd);
2729 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2730
2731 cred = wpa_config_get_cred(wpa_s->conf, id);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002732 return wpas_ctrl_remove_cred(wpa_s, cred);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002733}
2734
2735
2736static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2737 char *cmd)
2738{
2739 int id;
2740 struct wpa_cred *cred;
2741 char *name, *value;
2742
2743 /* cmd: "<cred id> <variable name> <value>" */
2744 name = os_strchr(cmd, ' ');
2745 if (name == NULL)
2746 return -1;
2747 *name++ = '\0';
2748
2749 value = os_strchr(name, ' ');
2750 if (value == NULL)
2751 return -1;
2752 *value++ = '\0';
2753
2754 id = atoi(cmd);
2755 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2756 id, name);
2757 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2758 (u8 *) value, os_strlen(value));
2759
2760 cred = wpa_config_get_cred(wpa_s->conf, id);
2761 if (cred == NULL) {
2762 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2763 id);
2764 return -1;
2765 }
2766
2767 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2768 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2769 "variable '%s'", name);
2770 return -1;
2771 }
2772
2773 return 0;
2774}
2775
2776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777#ifndef CONFIG_NO_CONFIG_WRITE
2778static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2779{
2780 int ret;
2781
2782 if (!wpa_s->conf->update_config) {
2783 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2784 "to update configuration (update_config=0)");
2785 return -1;
2786 }
2787
2788 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2789 if (ret) {
2790 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2791 "update configuration");
2792 } else {
2793 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2794 " updated");
2795 }
2796
2797 return ret;
2798}
2799#endif /* CONFIG_NO_CONFIG_WRITE */
2800
2801
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002802struct cipher_info {
2803 unsigned int capa;
2804 const char *name;
2805 int group_only;
2806};
2807
2808static const struct cipher_info ciphers[] = {
2809 { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
2810 { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
2811 { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
2812 { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
2813 { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
2814 { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
2815 { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
2816 { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
2817};
2818
2819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002820static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2821 struct wpa_driver_capa *capa,
2822 char *buf, size_t buflen)
2823{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002824 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825 char *pos, *end;
2826 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002827 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002828
2829 pos = buf;
2830 end = pos + buflen;
2831
2832 if (res < 0) {
2833 if (strict)
2834 return 0;
2835 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2836 if (len >= buflen)
2837 return -1;
2838 return len;
2839 }
2840
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002841 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
2842 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
2843 ret = os_snprintf(pos, end - pos, "%s%s",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002844 pos == buf ? "" : " ",
2845 ciphers[i].name);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002846 if (ret < 0 || ret >= end - pos)
2847 return pos - buf;
2848 pos += ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002849 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002850 }
2851
2852 return pos - buf;
2853}
2854
2855
2856static int ctrl_iface_get_capability_group(int res, char *strict,
2857 struct wpa_driver_capa *capa,
2858 char *buf, size_t buflen)
2859{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002860 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002861 char *pos, *end;
2862 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002863 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864
2865 pos = buf;
2866 end = pos + buflen;
2867
2868 if (res < 0) {
2869 if (strict)
2870 return 0;
2871 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
2872 if (len >= buflen)
2873 return -1;
2874 return len;
2875 }
2876
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002877 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
2878 if (capa->enc & ciphers[i].capa) {
2879 ret = os_snprintf(pos, end - pos, "%s%s",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002880 pos == buf ? "" : " ",
2881 ciphers[i].name);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002882 if (ret < 0 || ret >= end - pos)
2883 return pos - buf;
2884 pos += ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002885 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886 }
2887
2888 return pos - buf;
2889}
2890
2891
2892static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
2893 struct wpa_driver_capa *capa,
2894 char *buf, size_t buflen)
2895{
2896 int ret;
2897 char *pos, *end;
2898 size_t len;
2899
2900 pos = buf;
2901 end = pos + buflen;
2902
2903 if (res < 0) {
2904 if (strict)
2905 return 0;
2906 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
2907 "NONE", buflen);
2908 if (len >= buflen)
2909 return -1;
2910 return len;
2911 }
2912
2913 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
2914 if (ret < 0 || ret >= end - pos)
2915 return pos - buf;
2916 pos += ret;
2917
2918 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2919 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
2920 ret = os_snprintf(pos, end - pos, " WPA-EAP");
2921 if (ret < 0 || ret >= end - pos)
2922 return pos - buf;
2923 pos += ret;
2924 }
2925
2926 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2927 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2928 ret = os_snprintf(pos, end - pos, " WPA-PSK");
2929 if (ret < 0 || ret >= end - pos)
2930 return pos - buf;
2931 pos += ret;
2932 }
2933
2934 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2935 ret = os_snprintf(pos, end - pos, " WPA-NONE");
2936 if (ret < 0 || ret >= end - pos)
2937 return pos - buf;
2938 pos += ret;
2939 }
2940
2941 return pos - buf;
2942}
2943
2944
2945static int ctrl_iface_get_capability_proto(int res, char *strict,
2946 struct wpa_driver_capa *capa,
2947 char *buf, size_t buflen)
2948{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002949 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002950 char *pos, *end;
2951 size_t len;
2952
2953 pos = buf;
2954 end = pos + buflen;
2955
2956 if (res < 0) {
2957 if (strict)
2958 return 0;
2959 len = os_strlcpy(buf, "RSN WPA", buflen);
2960 if (len >= buflen)
2961 return -1;
2962 return len;
2963 }
2964
2965 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2966 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002967 ret = os_snprintf(pos, end - pos, "%sRSN",
2968 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002969 if (ret < 0 || ret >= end - pos)
2970 return pos - buf;
2971 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002972 }
2973
2974 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2975 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002976 ret = os_snprintf(pos, end - pos, "%sWPA",
2977 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002978 if (ret < 0 || ret >= end - pos)
2979 return pos - buf;
2980 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002981 }
2982
2983 return pos - buf;
2984}
2985
2986
2987static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
2988 struct wpa_driver_capa *capa,
2989 char *buf, size_t buflen)
2990{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002991 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002992 char *pos, *end;
2993 size_t len;
2994
2995 pos = buf;
2996 end = pos + buflen;
2997
2998 if (res < 0) {
2999 if (strict)
3000 return 0;
3001 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
3002 if (len >= buflen)
3003 return -1;
3004 return len;
3005 }
3006
3007 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003008 ret = os_snprintf(pos, end - pos, "%sOPEN",
3009 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010 if (ret < 0 || ret >= end - pos)
3011 return pos - buf;
3012 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003013 }
3014
3015 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
3016 ret = os_snprintf(pos, end - pos, "%sSHARED",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003017 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018 if (ret < 0 || ret >= end - pos)
3019 return pos - buf;
3020 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003021 }
3022
3023 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003024 ret = os_snprintf(pos, end - pos, "%sLEAP",
3025 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003026 if (ret < 0 || ret >= end - pos)
3027 return pos - buf;
3028 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 }
3030
3031 return pos - buf;
3032}
3033
3034
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003035static int ctrl_iface_get_capability_modes(int res, char *strict,
3036 struct wpa_driver_capa *capa,
3037 char *buf, size_t buflen)
3038{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003039 int ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003040 char *pos, *end;
3041 size_t len;
3042
3043 pos = buf;
3044 end = pos + buflen;
3045
3046 if (res < 0) {
3047 if (strict)
3048 return 0;
3049 len = os_strlcpy(buf, "IBSS AP", buflen);
3050 if (len >= buflen)
3051 return -1;
3052 return len;
3053 }
3054
3055 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003056 ret = os_snprintf(pos, end - pos, "%sIBSS",
3057 pos == buf ? "" : " ");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003058 if (ret < 0 || ret >= end - pos)
3059 return pos - buf;
3060 pos += ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003061 }
3062
3063 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003064 ret = os_snprintf(pos, end - pos, "%sAP",
3065 pos == buf ? "" : " ");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003066 if (ret < 0 || ret >= end - pos)
3067 return pos - buf;
3068 pos += ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003069 }
3070
3071 return pos - buf;
3072}
3073
3074
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003075static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
3076 char *buf, size_t buflen)
3077{
3078 struct hostapd_channel_data *chnl;
3079 int ret, i, j;
3080 char *pos, *end, *hmode;
3081
3082 pos = buf;
3083 end = pos + buflen;
3084
3085 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3086 switch (wpa_s->hw.modes[j].mode) {
3087 case HOSTAPD_MODE_IEEE80211B:
3088 hmode = "B";
3089 break;
3090 case HOSTAPD_MODE_IEEE80211G:
3091 hmode = "G";
3092 break;
3093 case HOSTAPD_MODE_IEEE80211A:
3094 hmode = "A";
3095 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003096 case HOSTAPD_MODE_IEEE80211AD:
3097 hmode = "AD";
3098 break;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003099 default:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003100 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003101 }
3102 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
3103 if (ret < 0 || ret >= end - pos)
3104 return pos - buf;
3105 pos += ret;
3106 chnl = wpa_s->hw.modes[j].channels;
3107 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003108 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3109 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003110 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
3111 if (ret < 0 || ret >= end - pos)
3112 return pos - buf;
3113 pos += ret;
3114 }
3115 ret = os_snprintf(pos, end - pos, "\n");
3116 if (ret < 0 || ret >= end - pos)
3117 return pos - buf;
3118 pos += ret;
3119 }
3120
3121 return pos - buf;
3122}
3123
3124
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003125static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3126 char *buf, size_t buflen)
3127{
3128 struct hostapd_channel_data *chnl;
3129 int ret, i, j;
3130 char *pos, *end, *hmode;
3131
3132 pos = buf;
3133 end = pos + buflen;
3134
3135 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3136 switch (wpa_s->hw.modes[j].mode) {
3137 case HOSTAPD_MODE_IEEE80211B:
3138 hmode = "B";
3139 break;
3140 case HOSTAPD_MODE_IEEE80211G:
3141 hmode = "G";
3142 break;
3143 case HOSTAPD_MODE_IEEE80211A:
3144 hmode = "A";
3145 break;
3146 case HOSTAPD_MODE_IEEE80211AD:
3147 hmode = "AD";
3148 break;
3149 default:
3150 continue;
3151 }
3152 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3153 hmode);
3154 if (ret < 0 || ret >= end - pos)
3155 return pos - buf;
3156 pos += ret;
3157 chnl = wpa_s->hw.modes[j].channels;
3158 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3159 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3160 continue;
Dmitry Shmidt5da5e352014-02-03 13:30:46 -08003161 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003162 chnl[i].chan, chnl[i].freq,
3163 chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
Dmitry Shmidt5da5e352014-02-03 13:30:46 -08003164 " (NO_IBSS)" : "",
3165 chnl[i].flag & HOSTAPD_CHAN_RADAR ?
3166 " (DFS)" : "");
3167
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003168 if (ret < 0 || ret >= end - pos)
3169 return pos - buf;
3170 pos += ret;
3171 }
3172 ret = os_snprintf(pos, end - pos, "\n");
3173 if (ret < 0 || ret >= end - pos)
3174 return pos - buf;
3175 pos += ret;
3176 }
3177
3178 return pos - buf;
3179}
3180
3181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003182static int wpa_supplicant_ctrl_iface_get_capability(
3183 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3184 size_t buflen)
3185{
3186 struct wpa_driver_capa capa;
3187 int res;
3188 char *strict;
3189 char field[30];
3190 size_t len;
3191
3192 /* Determine whether or not strict checking was requested */
3193 len = os_strlcpy(field, _field, sizeof(field));
3194 if (len >= sizeof(field))
3195 return -1;
3196 strict = os_strchr(field, ' ');
3197 if (strict != NULL) {
3198 *strict++ = '\0';
3199 if (os_strcmp(strict, "strict") != 0)
3200 return -1;
3201 }
3202
3203 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3204 field, strict ? strict : "");
3205
3206 if (os_strcmp(field, "eap") == 0) {
3207 return eap_get_names(buf, buflen);
3208 }
3209
3210 res = wpa_drv_get_capa(wpa_s, &capa);
3211
3212 if (os_strcmp(field, "pairwise") == 0)
3213 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3214 buf, buflen);
3215
3216 if (os_strcmp(field, "group") == 0)
3217 return ctrl_iface_get_capability_group(res, strict, &capa,
3218 buf, buflen);
3219
3220 if (os_strcmp(field, "key_mgmt") == 0)
3221 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3222 buf, buflen);
3223
3224 if (os_strcmp(field, "proto") == 0)
3225 return ctrl_iface_get_capability_proto(res, strict, &capa,
3226 buf, buflen);
3227
3228 if (os_strcmp(field, "auth_alg") == 0)
3229 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
3230 buf, buflen);
3231
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003232 if (os_strcmp(field, "modes") == 0)
3233 return ctrl_iface_get_capability_modes(res, strict, &capa,
3234 buf, buflen);
3235
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003236 if (os_strcmp(field, "channels") == 0)
3237 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3238
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003239 if (os_strcmp(field, "freq") == 0)
3240 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3241
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003242#ifdef CONFIG_TDLS
3243 if (os_strcmp(field, "tdls") == 0)
3244 return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
3245#endif /* CONFIG_TDLS */
3246
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003247 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3248 field);
3249
3250 return -1;
3251}
3252
3253
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003254#ifdef CONFIG_INTERWORKING
3255static char * anqp_add_hex(char *pos, char *end, const char *title,
3256 struct wpabuf *data)
3257{
3258 char *start = pos;
3259 size_t i;
3260 int ret;
3261 const u8 *d;
3262
3263 if (data == NULL)
3264 return start;
3265
3266 ret = os_snprintf(pos, end - pos, "%s=", title);
3267 if (ret < 0 || ret >= end - pos)
3268 return start;
3269 pos += ret;
3270
3271 d = wpabuf_head_u8(data);
3272 for (i = 0; i < wpabuf_len(data); i++) {
3273 ret = os_snprintf(pos, end - pos, "%02x", *d++);
3274 if (ret < 0 || ret >= end - pos)
3275 return start;
3276 pos += ret;
3277 }
3278
3279 ret = os_snprintf(pos, end - pos, "\n");
3280 if (ret < 0 || ret >= end - pos)
3281 return start;
3282 pos += ret;
3283
3284 return pos;
3285}
3286#endif /* CONFIG_INTERWORKING */
3287
3288
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003289static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3290 unsigned long mask, char *buf, size_t buflen)
3291{
3292 size_t i;
3293 int ret;
3294 char *pos, *end;
3295 const u8 *ie, *ie2;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003296
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003297 pos = buf;
3298 end = buf + buflen;
3299
3300 if (mask & WPA_BSS_MASK_ID) {
3301 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
3302 if (ret < 0 || ret >= end - pos)
3303 return 0;
3304 pos += ret;
3305 }
3306
3307 if (mask & WPA_BSS_MASK_BSSID) {
3308 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3309 MAC2STR(bss->bssid));
3310 if (ret < 0 || ret >= end - pos)
3311 return 0;
3312 pos += ret;
3313 }
3314
3315 if (mask & WPA_BSS_MASK_FREQ) {
3316 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
3317 if (ret < 0 || ret >= end - pos)
3318 return 0;
3319 pos += ret;
3320 }
3321
3322 if (mask & WPA_BSS_MASK_BEACON_INT) {
3323 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3324 bss->beacon_int);
3325 if (ret < 0 || ret >= end - pos)
3326 return 0;
3327 pos += ret;
3328 }
3329
3330 if (mask & WPA_BSS_MASK_CAPABILITIES) {
3331 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3332 bss->caps);
3333 if (ret < 0 || ret >= end - pos)
3334 return 0;
3335 pos += ret;
3336 }
3337
3338 if (mask & WPA_BSS_MASK_QUAL) {
3339 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
3340 if (ret < 0 || ret >= end - pos)
3341 return 0;
3342 pos += ret;
3343 }
3344
3345 if (mask & WPA_BSS_MASK_NOISE) {
3346 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
3347 if (ret < 0 || ret >= end - pos)
3348 return 0;
3349 pos += ret;
3350 }
3351
3352 if (mask & WPA_BSS_MASK_LEVEL) {
3353 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
3354 if (ret < 0 || ret >= end - pos)
3355 return 0;
3356 pos += ret;
3357 }
3358
3359 if (mask & WPA_BSS_MASK_TSF) {
3360 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3361 (unsigned long long) bss->tsf);
3362 if (ret < 0 || ret >= end - pos)
3363 return 0;
3364 pos += ret;
3365 }
3366
3367 if (mask & WPA_BSS_MASK_AGE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003368 struct os_reltime now;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003369
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003370 os_get_reltime(&now);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003371 ret = os_snprintf(pos, end - pos, "age=%d\n",
3372 (int) (now.sec - bss->last_update.sec));
3373 if (ret < 0 || ret >= end - pos)
3374 return 0;
3375 pos += ret;
3376 }
3377
3378 if (mask & WPA_BSS_MASK_IE) {
3379 ret = os_snprintf(pos, end - pos, "ie=");
3380 if (ret < 0 || ret >= end - pos)
3381 return 0;
3382 pos += ret;
3383
3384 ie = (const u8 *) (bss + 1);
3385 for (i = 0; i < bss->ie_len; i++) {
3386 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
3387 if (ret < 0 || ret >= end - pos)
3388 return 0;
3389 pos += ret;
3390 }
3391
3392 ret = os_snprintf(pos, end - pos, "\n");
3393 if (ret < 0 || ret >= end - pos)
3394 return 0;
3395 pos += ret;
3396 }
3397
3398 if (mask & WPA_BSS_MASK_FLAGS) {
3399 ret = os_snprintf(pos, end - pos, "flags=");
3400 if (ret < 0 || ret >= end - pos)
3401 return 0;
3402 pos += ret;
3403
3404 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3405 if (ie)
3406 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3407 2 + ie[1]);
3408 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3409 if (ie2)
3410 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3411 2 + ie2[1]);
3412 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3413 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3414 ret = os_snprintf(pos, end - pos, "[WEP]");
3415 if (ret < 0 || ret >= end - pos)
3416 return 0;
3417 pos += ret;
3418 }
3419 if (bss->caps & IEEE80211_CAP_IBSS) {
3420 ret = os_snprintf(pos, end - pos, "[IBSS]");
3421 if (ret < 0 || ret >= end - pos)
3422 return 0;
3423 pos += ret;
3424 }
3425 if (bss->caps & IEEE80211_CAP_ESS) {
3426 ret = os_snprintf(pos, end - pos, "[ESS]");
3427 if (ret < 0 || ret >= end - pos)
3428 return 0;
3429 pos += ret;
3430 }
Dmitry Shmidt96571392013-10-14 12:54:46 -07003431 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
3432 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003433 ret = os_snprintf(pos, end - pos, "[P2P]");
3434 if (ret < 0 || ret >= end - pos)
3435 return 0;
3436 pos += ret;
3437 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003438#ifdef CONFIG_HS20
3439 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3440 ret = os_snprintf(pos, end - pos, "[HS20]");
3441 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003442 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003443 pos += ret;
3444 }
3445#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003446
3447 ret = os_snprintf(pos, end - pos, "\n");
3448 if (ret < 0 || ret >= end - pos)
3449 return 0;
3450 pos += ret;
3451 }
3452
3453 if (mask & WPA_BSS_MASK_SSID) {
3454 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3455 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3456 if (ret < 0 || ret >= end - pos)
3457 return 0;
3458 pos += ret;
3459 }
3460
3461#ifdef CONFIG_WPS
3462 if (mask & WPA_BSS_MASK_WPS_SCAN) {
3463 ie = (const u8 *) (bss + 1);
3464 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3465 if (ret < 0 || ret >= end - pos)
3466 return 0;
3467 pos += ret;
3468 }
3469#endif /* CONFIG_WPS */
3470
3471#ifdef CONFIG_P2P
3472 if (mask & WPA_BSS_MASK_P2P_SCAN) {
3473 ie = (const u8 *) (bss + 1);
3474 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3475 if (ret < 0 || ret >= end - pos)
3476 return 0;
3477 pos += ret;
3478 }
3479#endif /* CONFIG_P2P */
3480
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003481#ifdef CONFIG_WIFI_DISPLAY
3482 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3483 struct wpabuf *wfd;
3484 ie = (const u8 *) (bss + 1);
3485 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3486 WFD_IE_VENDOR_TYPE);
3487 if (wfd) {
3488 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003489 if (ret < 0 || ret >= end - pos) {
3490 wpabuf_free(wfd);
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003491 return 0;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003492 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003493 pos += ret;
3494
3495 pos += wpa_snprintf_hex(pos, end - pos,
3496 wpabuf_head(wfd),
3497 wpabuf_len(wfd));
3498 wpabuf_free(wfd);
3499
3500 ret = os_snprintf(pos, end - pos, "\n");
3501 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003502 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003503 pos += ret;
3504 }
3505 }
3506#endif /* CONFIG_WIFI_DISPLAY */
3507
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003508#ifdef CONFIG_INTERWORKING
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003509 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3510 struct wpa_bss_anqp *anqp = bss->anqp;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003511 pos = anqp_add_hex(pos, end, "anqp_venue_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003512 anqp->venue_name);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003513 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003514 anqp->network_auth_type);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003515 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003516 anqp->roaming_consortium);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003517 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003518 anqp->ip_addr_type_availability);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003519 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003520 anqp->nai_realm);
3521 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003522 pos = anqp_add_hex(pos, end, "anqp_domain_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003523 anqp->domain_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003524#ifdef CONFIG_HS20
3525 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003526 anqp->hs20_operator_friendly_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003527 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003528 anqp->hs20_wan_metrics);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003529 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003530 anqp->hs20_connection_capability);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003531 pos = anqp_add_hex(pos, end, "hs20_operating_class",
3532 anqp->hs20_operating_class);
3533 pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
3534 anqp->hs20_osu_providers_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003535#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003536 }
3537#endif /* CONFIG_INTERWORKING */
3538
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003539 if (mask & WPA_BSS_MASK_DELIM) {
3540 ret = os_snprintf(pos, end - pos, "====\n");
3541 if (ret < 0 || ret >= end - pos)
3542 return 0;
3543 pos += ret;
3544 }
Irfan Sheriffe2ea0082012-08-13 10:56:16 -07003545
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003546 return pos - buf;
3547}
3548
Dmitry Shmidt04949592012-07-19 12:16:46 -07003549
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003550static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
3551 const char *cmd, char *buf,
3552 size_t buflen)
3553{
3554 u8 bssid[ETH_ALEN];
3555 size_t i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003556 struct wpa_bss *bss;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003557 struct wpa_bss *bsslast = NULL;
3558 struct dl_list *next;
3559 int ret = 0;
3560 int len;
3561 char *ctmp;
3562 unsigned long mask = WPA_BSS_MASK_ALL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003563
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003564 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
3565 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
3566 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003567 list_id);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003568 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
3569 list_id);
3570 } else { /* N1-N2 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003571 unsigned int id1, id2;
3572
3573 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
3574 wpa_printf(MSG_INFO, "Wrong BSS range "
3575 "format");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003576 return 0;
3577 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003578
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003579 if (*(cmd + 6) == '-')
3580 id1 = 0;
3581 else
3582 id1 = atoi(cmd + 6);
3583 ctmp++;
3584 if (*ctmp >= '0' && *ctmp <= '9')
3585 id2 = atoi(ctmp);
3586 else
3587 id2 = (unsigned int) -1;
3588 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
3589 if (id2 == (unsigned int) -1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003590 bsslast = dl_list_last(&wpa_s->bss_id,
3591 struct wpa_bss,
3592 list_id);
3593 else {
3594 bsslast = wpa_bss_get_id(wpa_s, id2);
3595 if (bsslast == NULL && bss && id2 > id1) {
3596 struct wpa_bss *tmp = bss;
3597 for (;;) {
3598 next = tmp->list_id.next;
3599 if (next == &wpa_s->bss_id)
3600 break;
3601 tmp = dl_list_entry(
3602 next, struct wpa_bss,
3603 list_id);
3604 if (tmp->id > id2)
3605 break;
3606 bsslast = tmp;
3607 }
3608 }
3609 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003610 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003611 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003612 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003613 else if (os_strncmp(cmd, "LAST", 4) == 0)
3614 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003615 else if (os_strncmp(cmd, "ID-", 3) == 0) {
3616 i = atoi(cmd + 3);
3617 bss = wpa_bss_get_id(wpa_s, i);
3618 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3619 i = atoi(cmd + 5);
3620 bss = wpa_bss_get_id(wpa_s, i);
3621 if (bss) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003622 next = bss->list_id.next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623 if (next == &wpa_s->bss_id)
3624 bss = NULL;
3625 else
3626 bss = dl_list_entry(next, struct wpa_bss,
3627 list_id);
3628 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003629#ifdef CONFIG_P2P
3630 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
3631 if (hwaddr_aton(cmd + 13, bssid) == 0)
3632 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
3633 else
3634 bss = NULL;
3635#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636 } else if (hwaddr_aton(cmd, bssid) == 0)
3637 bss = wpa_bss_get_bssid(wpa_s, bssid);
3638 else {
3639 struct wpa_bss *tmp;
3640 i = atoi(cmd);
3641 bss = NULL;
3642 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
3643 {
3644 if (i-- == 0) {
3645 bss = tmp;
3646 break;
3647 }
3648 }
3649 }
3650
Dmitry Shmidt04949592012-07-19 12:16:46 -07003651 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
3652 mask = strtoul(ctmp + 5, NULL, 0x10);
3653 if (mask == 0)
3654 mask = WPA_BSS_MASK_ALL;
3655 }
3656
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003657 if (bss == NULL)
3658 return 0;
3659
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003660 if (bsslast == NULL)
3661 bsslast = bss;
3662 do {
3663 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
3664 ret += len;
3665 buf += len;
3666 buflen -= len;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003667 if (bss == bsslast) {
3668 if ((mask & WPA_BSS_MASK_DELIM) && len &&
3669 (bss == dl_list_last(&wpa_s->bss_id,
3670 struct wpa_bss, list_id)))
3671 os_snprintf(buf - 5, 5, "####\n");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003672 break;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003673 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003674 next = bss->list_id.next;
3675 if (next == &wpa_s->bss_id)
3676 break;
3677 bss = dl_list_entry(next, struct wpa_bss, list_id);
3678 } while (bss && len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003680 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003681}
3682
3683
3684static int wpa_supplicant_ctrl_iface_ap_scan(
3685 struct wpa_supplicant *wpa_s, char *cmd)
3686{
3687 int ap_scan = atoi(cmd);
3688 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
3689}
3690
3691
3692static int wpa_supplicant_ctrl_iface_scan_interval(
3693 struct wpa_supplicant *wpa_s, char *cmd)
3694{
3695 int scan_int = atoi(cmd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003696 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697}
3698
3699
3700static int wpa_supplicant_ctrl_iface_bss_expire_age(
3701 struct wpa_supplicant *wpa_s, char *cmd)
3702{
3703 int expire_age = atoi(cmd);
3704 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
3705}
3706
3707
3708static int wpa_supplicant_ctrl_iface_bss_expire_count(
3709 struct wpa_supplicant *wpa_s, char *cmd)
3710{
3711 int expire_count = atoi(cmd);
3712 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
3713}
3714
3715
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07003716static int wpa_supplicant_ctrl_iface_bss_flush(
3717 struct wpa_supplicant *wpa_s, char *cmd)
3718{
3719 int flush_age = atoi(cmd);
3720
3721 if (flush_age == 0)
3722 wpa_bss_flush(wpa_s);
3723 else
3724 wpa_bss_flush_by_age(wpa_s, flush_age);
3725 return 0;
3726}
3727
3728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003729static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
3730{
3731 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
3732 /* MLME-DELETEKEYS.request */
3733 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
3734 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
3735 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
3736 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
3737#ifdef CONFIG_IEEE80211W
3738 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
3739 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
3740#endif /* CONFIG_IEEE80211W */
3741
3742 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
3743 0);
3744 /* MLME-SETPROTECTION.request(None) */
3745 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
3746 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
3747 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3748 wpa_sm_drop_sa(wpa_s->wpa);
3749}
3750
3751
3752static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
3753 char *addr)
3754{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003755#ifdef CONFIG_NO_SCAN_PROCESSING
3756 return -1;
3757#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003758 u8 bssid[ETH_ALEN];
3759 struct wpa_bss *bss;
3760 struct wpa_ssid *ssid = wpa_s->current_ssid;
3761
3762 if (hwaddr_aton(addr, bssid)) {
3763 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
3764 "address '%s'", addr);
3765 return -1;
3766 }
3767
3768 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
3769
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003770 if (!ssid) {
3771 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
3772 "configuration known for the target AP");
3773 return -1;
3774 }
3775
3776 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003777 if (!bss) {
3778 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
3779 "from BSS table");
3780 return -1;
3781 }
3782
3783 /*
3784 * TODO: Find best network configuration block from configuration to
3785 * allow roaming to other networks
3786 */
3787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003788 wpa_s->reassociate = 1;
3789 wpa_supplicant_connect(wpa_s, bss, ssid);
3790
3791 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003792#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003793}
3794
3795
3796#ifdef CONFIG_P2P
3797static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
3798{
3799 unsigned int timeout = atoi(cmd);
3800 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003801 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003802 u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003803 char *pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003804 unsigned int search_delay;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003805
3806 if (os_strstr(cmd, "type=social"))
3807 type = P2P_FIND_ONLY_SOCIAL;
3808 else if (os_strstr(cmd, "type=progressive"))
3809 type = P2P_FIND_PROGRESSIVE;
3810
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003811 pos = os_strstr(cmd, "dev_id=");
3812 if (pos) {
3813 pos += 7;
3814 if (hwaddr_aton(pos, dev_id))
3815 return -1;
3816 _dev_id = dev_id;
3817 }
3818
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003819 pos = os_strstr(cmd, "dev_type=");
3820 if (pos) {
3821 pos += 9;
3822 if (wps_dev_type_str2bin(pos, dev_type) < 0)
3823 return -1;
3824 _dev_type = dev_type;
3825 }
3826
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003827 pos = os_strstr(cmd, "delay=");
3828 if (pos) {
3829 pos += 6;
3830 search_delay = atoi(pos);
3831 } else
3832 search_delay = wpas_p2p_search_delay(wpa_s);
3833
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003834 return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
3835 _dev_id, search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003836}
3837
3838
3839static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
3840 char *buf, size_t buflen)
3841{
3842 u8 addr[ETH_ALEN];
3843 char *pos, *pos2;
3844 char *pin = NULL;
3845 enum p2p_wps_method wps_method;
3846 int new_pin;
3847 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003848 int persistent_group, persistent_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849 int join;
3850 int auth;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003851 int automatic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003852 int go_intent = -1;
3853 int freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003854 int pd;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003855 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856
Dmitry Shmidt04949592012-07-19 12:16:46 -07003857 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
3858 * [persistent|persistent=<network id>]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003859 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003860 * [ht40] [vht] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003861
3862 if (hwaddr_aton(cmd, addr))
3863 return -1;
3864
3865 pos = cmd + 17;
3866 if (*pos != ' ')
3867 return -1;
3868 pos++;
3869
3870 persistent_group = os_strstr(pos, " persistent") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003871 pos2 = os_strstr(pos, " persistent=");
3872 if (pos2) {
3873 struct wpa_ssid *ssid;
3874 persistent_id = atoi(pos2 + 12);
3875 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3876 if (ssid == NULL || ssid->disabled != 2 ||
3877 ssid->mode != WPAS_MODE_P2P_GO) {
3878 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3879 "SSID id=%d for persistent P2P group (GO)",
3880 persistent_id);
3881 return -1;
3882 }
3883 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003884 join = os_strstr(pos, " join") != NULL;
3885 auth = os_strstr(pos, " auth") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003886 automatic = os_strstr(pos, " auto") != NULL;
3887 pd = os_strstr(pos, " provdisc") != NULL;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003888 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
3889 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
3890 vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003891
3892 pos2 = os_strstr(pos, " go_intent=");
3893 if (pos2) {
3894 pos2 += 11;
3895 go_intent = atoi(pos2);
3896 if (go_intent < 0 || go_intent > 15)
3897 return -1;
3898 }
3899
3900 pos2 = os_strstr(pos, " freq=");
3901 if (pos2) {
3902 pos2 += 6;
3903 freq = atoi(pos2);
3904 if (freq <= 0)
3905 return -1;
3906 }
3907
3908 if (os_strncmp(pos, "pin", 3) == 0) {
3909 /* Request random PIN (to be displayed) and enable the PIN */
3910 wps_method = WPS_PIN_DISPLAY;
3911 } else if (os_strncmp(pos, "pbc", 3) == 0) {
3912 wps_method = WPS_PBC;
3913 } else {
3914 pin = pos;
3915 pos = os_strchr(pin, ' ');
3916 wps_method = WPS_PIN_KEYPAD;
3917 if (pos) {
3918 *pos++ = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003919 if (os_strncmp(pos, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003920 wps_method = WPS_PIN_DISPLAY;
3921 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003922 if (!wps_pin_str_valid(pin)) {
3923 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
3924 return 17;
3925 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003926 }
3927
3928 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003929 persistent_group, automatic, join,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003930 auth, go_intent, freq, persistent_id, pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003931 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932 if (new_pin == -2) {
3933 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
3934 return 25;
3935 }
3936 if (new_pin == -3) {
3937 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
3938 return 25;
3939 }
3940 if (new_pin < 0)
3941 return -1;
3942 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
3943 ret = os_snprintf(buf, buflen, "%08d", new_pin);
3944 if (ret < 0 || (size_t) ret >= buflen)
3945 return -1;
3946 return ret;
3947 }
3948
3949 os_memcpy(buf, "OK\n", 3);
3950 return 3;
3951}
3952
3953
3954static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
3955{
3956 unsigned int timeout = atoi(cmd);
3957 return wpas_p2p_listen(wpa_s, timeout);
3958}
3959
3960
3961static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
3962{
3963 u8 addr[ETH_ALEN];
3964 char *pos;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003965 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003966
Dmitry Shmidt04949592012-07-19 12:16:46 -07003967 /* <addr> <config method> [join|auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968
3969 if (hwaddr_aton(cmd, addr))
3970 return -1;
3971
3972 pos = cmd + 17;
3973 if (*pos != ' ')
3974 return -1;
3975 pos++;
3976
Dmitry Shmidt04949592012-07-19 12:16:46 -07003977 if (os_strstr(pos, " join") != NULL)
3978 use = WPAS_P2P_PD_FOR_JOIN;
3979 else if (os_strstr(pos, " auto") != NULL)
3980 use = WPAS_P2P_PD_AUTO;
3981
3982 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003983}
3984
3985
3986static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
3987 size_t buflen)
3988{
3989 struct wpa_ssid *ssid = wpa_s->current_ssid;
3990
3991 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3992 ssid->passphrase == NULL)
3993 return -1;
3994
3995 os_strlcpy(buf, ssid->passphrase, buflen);
3996 return os_strlen(buf);
3997}
3998
3999
4000static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
4001 char *buf, size_t buflen)
4002{
4003 u64 ref;
4004 int res;
4005 u8 dst_buf[ETH_ALEN], *dst;
4006 struct wpabuf *tlvs;
4007 char *pos;
4008 size_t len;
4009
4010 if (hwaddr_aton(cmd, dst_buf))
4011 return -1;
4012 dst = dst_buf;
4013 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
4014 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
4015 dst = NULL;
4016 pos = cmd + 17;
4017 if (*pos != ' ')
4018 return -1;
4019 pos++;
4020
4021 if (os_strncmp(pos, "upnp ", 5) == 0) {
4022 u8 version;
4023 pos += 5;
4024 if (hexstr2bin(pos, &version, 1) < 0)
4025 return -1;
4026 pos += 2;
4027 if (*pos != ' ')
4028 return -1;
4029 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004030 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004031#ifdef CONFIG_WIFI_DISPLAY
4032 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
4033 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
4034#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004035 } else {
4036 len = os_strlen(pos);
4037 if (len & 1)
4038 return -1;
4039 len /= 2;
4040 tlvs = wpabuf_alloc(len);
4041 if (tlvs == NULL)
4042 return -1;
4043 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
4044 wpabuf_free(tlvs);
4045 return -1;
4046 }
4047
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004048 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004049 wpabuf_free(tlvs);
4050 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004051 if (ref == 0)
4052 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004053 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
4054 if (res < 0 || (unsigned) res >= buflen)
4055 return -1;
4056 return res;
4057}
4058
4059
4060static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
4061 char *cmd)
4062{
4063 long long unsigned val;
4064 u64 req;
4065 if (sscanf(cmd, "%llx", &val) != 1)
4066 return -1;
4067 req = val;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004068 return wpas_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069}
4070
4071
4072static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
4073{
4074 int freq;
4075 u8 dst[ETH_ALEN];
4076 u8 dialog_token;
4077 struct wpabuf *resp_tlvs;
4078 char *pos, *pos2;
4079 size_t len;
4080
4081 pos = os_strchr(cmd, ' ');
4082 if (pos == NULL)
4083 return -1;
4084 *pos++ = '\0';
4085 freq = atoi(cmd);
4086 if (freq == 0)
4087 return -1;
4088
4089 if (hwaddr_aton(pos, dst))
4090 return -1;
4091 pos += 17;
4092 if (*pos != ' ')
4093 return -1;
4094 pos++;
4095
4096 pos2 = os_strchr(pos, ' ');
4097 if (pos2 == NULL)
4098 return -1;
4099 *pos2++ = '\0';
4100 dialog_token = atoi(pos);
4101
4102 len = os_strlen(pos2);
4103 if (len & 1)
4104 return -1;
4105 len /= 2;
4106 resp_tlvs = wpabuf_alloc(len);
4107 if (resp_tlvs == NULL)
4108 return -1;
4109 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
4110 wpabuf_free(resp_tlvs);
4111 return -1;
4112 }
4113
4114 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
4115 wpabuf_free(resp_tlvs);
4116 return 0;
4117}
4118
4119
4120static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
4121 char *cmd)
4122{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004123 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
4124 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004125 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
4126 return 0;
4127}
4128
4129
4130static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
4131 char *cmd)
4132{
4133 char *pos;
4134 size_t len;
4135 struct wpabuf *query, *resp;
4136
4137 pos = os_strchr(cmd, ' ');
4138 if (pos == NULL)
4139 return -1;
4140 *pos++ = '\0';
4141
4142 len = os_strlen(cmd);
4143 if (len & 1)
4144 return -1;
4145 len /= 2;
4146 query = wpabuf_alloc(len);
4147 if (query == NULL)
4148 return -1;
4149 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4150 wpabuf_free(query);
4151 return -1;
4152 }
4153
4154 len = os_strlen(pos);
4155 if (len & 1) {
4156 wpabuf_free(query);
4157 return -1;
4158 }
4159 len /= 2;
4160 resp = wpabuf_alloc(len);
4161 if (resp == NULL) {
4162 wpabuf_free(query);
4163 return -1;
4164 }
4165 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4166 wpabuf_free(query);
4167 wpabuf_free(resp);
4168 return -1;
4169 }
4170
4171 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4172 wpabuf_free(query);
4173 wpabuf_free(resp);
4174 return -1;
4175 }
4176 return 0;
4177}
4178
4179
4180static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4181{
4182 char *pos;
4183 u8 version;
4184
4185 pos = os_strchr(cmd, ' ');
4186 if (pos == NULL)
4187 return -1;
4188 *pos++ = '\0';
4189
4190 if (hexstr2bin(cmd, &version, 1) < 0)
4191 return -1;
4192
4193 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4194}
4195
4196
4197static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4198{
4199 char *pos;
4200
4201 pos = os_strchr(cmd, ' ');
4202 if (pos == NULL)
4203 return -1;
4204 *pos++ = '\0';
4205
4206 if (os_strcmp(cmd, "bonjour") == 0)
4207 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4208 if (os_strcmp(cmd, "upnp") == 0)
4209 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4210 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4211 return -1;
4212}
4213
4214
4215static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4216 char *cmd)
4217{
4218 size_t len;
4219 struct wpabuf *query;
4220 int ret;
4221
4222 len = os_strlen(cmd);
4223 if (len & 1)
4224 return -1;
4225 len /= 2;
4226 query = wpabuf_alloc(len);
4227 if (query == NULL)
4228 return -1;
4229 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4230 wpabuf_free(query);
4231 return -1;
4232 }
4233
4234 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4235 wpabuf_free(query);
4236 return ret;
4237}
4238
4239
4240static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4241{
4242 char *pos;
4243 u8 version;
4244
4245 pos = os_strchr(cmd, ' ');
4246 if (pos == NULL)
4247 return -1;
4248 *pos++ = '\0';
4249
4250 if (hexstr2bin(cmd, &version, 1) < 0)
4251 return -1;
4252
4253 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4254}
4255
4256
4257static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4258{
4259 char *pos;
4260
4261 pos = os_strchr(cmd, ' ');
4262 if (pos == NULL)
4263 return -1;
4264 *pos++ = '\0';
4265
4266 if (os_strcmp(cmd, "bonjour") == 0)
4267 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4268 if (os_strcmp(cmd, "upnp") == 0)
4269 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4270 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4271 return -1;
4272}
4273
4274
4275static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4276{
4277 u8 addr[ETH_ALEN];
4278
4279 /* <addr> */
4280
4281 if (hwaddr_aton(cmd, addr))
4282 return -1;
4283
4284 return wpas_p2p_reject(wpa_s, addr);
4285}
4286
4287
4288static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4289{
4290 char *pos;
4291 int id;
4292 struct wpa_ssid *ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004293 u8 *_peer = NULL, peer[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004294 int freq = 0, pref_freq = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004295 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004296
4297 id = atoi(cmd);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004298 pos = os_strstr(cmd, " peer=");
4299 if (pos) {
4300 pos += 6;
4301 if (hwaddr_aton(pos, peer))
4302 return -1;
4303 _peer = peer;
4304 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004305 ssid = wpa_config_get_network(wpa_s->conf, id);
4306 if (ssid == NULL || ssid->disabled != 2) {
4307 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4308 "for persistent P2P group",
4309 id);
4310 return -1;
4311 }
4312
Jouni Malinen31be0a42012-08-31 21:20:51 +03004313 pos = os_strstr(cmd, " freq=");
4314 if (pos) {
4315 pos += 6;
4316 freq = atoi(pos);
4317 if (freq <= 0)
4318 return -1;
4319 }
4320
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004321 pos = os_strstr(cmd, " pref=");
4322 if (pos) {
4323 pos += 6;
4324 pref_freq = atoi(pos);
4325 if (pref_freq <= 0)
4326 return -1;
4327 }
4328
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004329 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4330 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4331 vht;
Jouni Malinen31be0a42012-08-31 21:20:51 +03004332
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004333 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
4334 pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004335}
4336
4337
4338static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
4339{
4340 char *pos;
4341 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
4342
4343 pos = os_strstr(cmd, " peer=");
4344 if (!pos)
4345 return -1;
4346
4347 *pos = '\0';
4348 pos += 6;
4349 if (hwaddr_aton(pos, peer)) {
4350 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
4351 return -1;
4352 }
4353
4354 pos = os_strstr(pos, " go_dev_addr=");
4355 if (pos) {
4356 pos += 13;
4357 if (hwaddr_aton(pos, go_dev_addr)) {
4358 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
4359 pos);
4360 return -1;
4361 }
4362 go_dev = go_dev_addr;
4363 }
4364
4365 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
4366}
4367
4368
4369static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
4370{
4371 if (os_strncmp(cmd, "persistent=", 11) == 0)
4372 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
4373 if (os_strncmp(cmd, "group=", 6) == 0)
4374 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
4375
4376 return -1;
4377}
4378
4379
4380static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004381 char *cmd, int freq, int ht40,
4382 int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004383{
4384 int id;
4385 struct wpa_ssid *ssid;
4386
4387 id = atoi(cmd);
4388 ssid = wpa_config_get_network(wpa_s->conf, id);
4389 if (ssid == NULL || ssid->disabled != 2) {
4390 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4391 "for persistent P2P group",
4392 id);
4393 return -1;
4394 }
4395
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004396 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004397 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004398}
4399
4400
4401static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
4402{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004403 int freq = 0, ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404 char *pos;
4405
4406 pos = os_strstr(cmd, "freq=");
4407 if (pos)
4408 freq = atoi(pos + 5);
4409
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004410 vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
4411 ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4412 vht;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004414 if (os_strncmp(cmd, "persistent=", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004415 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004416 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417 if (os_strcmp(cmd, "persistent") == 0 ||
4418 os_strncmp(cmd, "persistent ", 11) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004419 return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004420 if (os_strncmp(cmd, "freq=", 5) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004421 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004422 if (ht40)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004423 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004424
4425 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
4426 cmd);
4427 return -1;
4428}
4429
4430
4431static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
4432 char *buf, size_t buflen)
4433{
4434 u8 addr[ETH_ALEN], *addr_ptr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004435 int next, res;
4436 const struct p2p_peer_info *info;
4437 char *pos, *end;
4438 char devtype[WPS_DEV_TYPE_BUFSIZE];
4439 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004440 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004441
4442 if (!wpa_s->global->p2p)
4443 return -1;
4444
4445 if (os_strcmp(cmd, "FIRST") == 0) {
4446 addr_ptr = NULL;
4447 next = 0;
4448 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4449 if (hwaddr_aton(cmd + 5, addr) < 0)
4450 return -1;
4451 addr_ptr = addr;
4452 next = 1;
4453 } else {
4454 if (hwaddr_aton(cmd, addr) < 0)
4455 return -1;
4456 addr_ptr = addr;
4457 next = 0;
4458 }
4459
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004460 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4461 if (info == NULL)
4462 return -1;
4463
4464 pos = buf;
4465 end = buf + buflen;
4466
4467 res = os_snprintf(pos, end - pos, MACSTR "\n"
4468 "pri_dev_type=%s\n"
4469 "device_name=%s\n"
4470 "manufacturer=%s\n"
4471 "model_name=%s\n"
4472 "model_number=%s\n"
4473 "serial_number=%s\n"
4474 "config_methods=0x%x\n"
4475 "dev_capab=0x%x\n"
4476 "group_capab=0x%x\n"
4477 "level=%d\n",
4478 MAC2STR(info->p2p_device_addr),
4479 wps_dev_type_bin2str(info->pri_dev_type,
4480 devtype, sizeof(devtype)),
4481 info->device_name,
4482 info->manufacturer,
4483 info->model_name,
4484 info->model_number,
4485 info->serial_number,
4486 info->config_methods,
4487 info->dev_capab,
4488 info->group_capab,
4489 info->level);
4490 if (res < 0 || res >= end - pos)
4491 return pos - buf;
4492 pos += res;
4493
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004494 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4495 {
4496 const u8 *t;
4497 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4498 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4499 wps_dev_type_bin2str(t, devtype,
4500 sizeof(devtype)));
4501 if (res < 0 || res >= end - pos)
4502 return pos - buf;
4503 pos += res;
4504 }
4505
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004506 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004507 if (ssid) {
4508 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
4509 if (res < 0 || res >= end - pos)
4510 return pos - buf;
4511 pos += res;
4512 }
4513
4514 res = p2p_get_peer_info_txt(info, pos, end - pos);
4515 if (res < 0)
4516 return pos - buf;
4517 pos += res;
4518
4519 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004520}
4521
4522
Dmitry Shmidt04949592012-07-19 12:16:46 -07004523static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
4524 const char *param)
4525{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004526 unsigned int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004527
4528 if (wpa_s->global->p2p == NULL)
4529 return -1;
4530
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004531 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
4532 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004533
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004534 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
4535 struct wpa_freq_range *freq;
4536 freq = &wpa_s->global->p2p_disallow_freq.range[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004537 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004538 freq->min, freq->max);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004539 }
4540
Dmitry Shmidt04949592012-07-19 12:16:46 -07004541 wpas_p2p_update_channel_list(wpa_s);
4542 return 0;
4543}
4544
4545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004546static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
4547{
4548 char *param;
4549
4550 if (wpa_s->global->p2p == NULL)
4551 return -1;
4552
4553 param = os_strchr(cmd, ' ');
4554 if (param == NULL)
4555 return -1;
4556 *param++ = '\0';
4557
4558 if (os_strcmp(cmd, "discoverability") == 0) {
4559 p2p_set_client_discoverability(wpa_s->global->p2p,
4560 atoi(param));
4561 return 0;
4562 }
4563
4564 if (os_strcmp(cmd, "managed") == 0) {
4565 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
4566 return 0;
4567 }
4568
4569 if (os_strcmp(cmd, "listen_channel") == 0) {
4570 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
4571 atoi(param));
4572 }
4573
4574 if (os_strcmp(cmd, "ssid_postfix") == 0) {
4575 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
4576 os_strlen(param));
4577 }
4578
4579 if (os_strcmp(cmd, "noa") == 0) {
4580 char *pos;
4581 int count, start, duration;
4582 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
4583 count = atoi(param);
4584 pos = os_strchr(param, ',');
4585 if (pos == NULL)
4586 return -1;
4587 pos++;
4588 start = atoi(pos);
4589 pos = os_strchr(pos, ',');
4590 if (pos == NULL)
4591 return -1;
4592 pos++;
4593 duration = atoi(pos);
4594 if (count < 0 || count > 255 || start < 0 || duration < 0)
4595 return -1;
4596 if (count == 0 && duration > 0)
4597 return -1;
4598 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
4599 "start=%d duration=%d", count, start, duration);
4600 return wpas_p2p_set_noa(wpa_s, count, start, duration);
4601 }
4602
4603 if (os_strcmp(cmd, "ps") == 0)
4604 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
4605
4606 if (os_strcmp(cmd, "oppps") == 0)
4607 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
4608
4609 if (os_strcmp(cmd, "ctwindow") == 0)
4610 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
4611
4612 if (os_strcmp(cmd, "disabled") == 0) {
4613 wpa_s->global->p2p_disabled = atoi(param);
4614 wpa_printf(MSG_DEBUG, "P2P functionality %s",
4615 wpa_s->global->p2p_disabled ?
4616 "disabled" : "enabled");
4617 if (wpa_s->global->p2p_disabled) {
4618 wpas_p2p_stop_find(wpa_s);
4619 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4620 p2p_flush(wpa_s->global->p2p);
4621 }
4622 return 0;
4623 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004624
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004625 if (os_strcmp(cmd, "conc_pref") == 0) {
4626 if (os_strcmp(param, "sta") == 0)
4627 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
4628 else if (os_strcmp(param, "p2p") == 0)
4629 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004630 else {
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004631 wpa_printf(MSG_INFO, "Invalid conc_pref value");
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004632 return -1;
4633 }
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004634 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004635 "%s", param);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004636 return 0;
4637 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639 if (os_strcmp(cmd, "force_long_sd") == 0) {
4640 wpa_s->force_long_sd = atoi(param);
4641 return 0;
4642 }
4643
4644 if (os_strcmp(cmd, "peer_filter") == 0) {
4645 u8 addr[ETH_ALEN];
4646 if (hwaddr_aton(param, addr))
4647 return -1;
4648 p2p_set_peer_filter(wpa_s->global->p2p, addr);
4649 return 0;
4650 }
4651
4652 if (os_strcmp(cmd, "cross_connect") == 0)
4653 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
4654
4655 if (os_strcmp(cmd, "go_apsd") == 0) {
4656 if (os_strcmp(param, "disable") == 0)
4657 wpa_s->set_ap_uapsd = 0;
4658 else {
4659 wpa_s->set_ap_uapsd = 1;
4660 wpa_s->ap_uapsd = atoi(param);
4661 }
4662 return 0;
4663 }
4664
4665 if (os_strcmp(cmd, "client_apsd") == 0) {
4666 if (os_strcmp(param, "disable") == 0)
4667 wpa_s->set_sta_uapsd = 0;
4668 else {
4669 int be, bk, vi, vo;
4670 char *pos;
4671 /* format: BE,BK,VI,VO;max SP Length */
4672 be = atoi(param);
4673 pos = os_strchr(param, ',');
4674 if (pos == NULL)
4675 return -1;
4676 pos++;
4677 bk = atoi(pos);
4678 pos = os_strchr(pos, ',');
4679 if (pos == NULL)
4680 return -1;
4681 pos++;
4682 vi = atoi(pos);
4683 pos = os_strchr(pos, ',');
4684 if (pos == NULL)
4685 return -1;
4686 pos++;
4687 vo = atoi(pos);
4688 /* ignore max SP Length for now */
4689
4690 wpa_s->set_sta_uapsd = 1;
4691 wpa_s->sta_uapsd = 0;
4692 if (be)
4693 wpa_s->sta_uapsd |= BIT(0);
4694 if (bk)
4695 wpa_s->sta_uapsd |= BIT(1);
4696 if (vi)
4697 wpa_s->sta_uapsd |= BIT(2);
4698 if (vo)
4699 wpa_s->sta_uapsd |= BIT(3);
4700 }
4701 return 0;
4702 }
4703
Dmitry Shmidt04949592012-07-19 12:16:46 -07004704 if (os_strcmp(cmd, "disallow_freq") == 0)
4705 return p2p_ctrl_disallow_freq(wpa_s, param);
4706
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004707 if (os_strcmp(cmd, "disc_int") == 0) {
4708 int min_disc_int, max_disc_int, max_disc_tu;
4709 char *pos;
4710
4711 pos = param;
4712
4713 min_disc_int = atoi(pos);
4714 pos = os_strchr(pos, ' ');
4715 if (pos == NULL)
4716 return -1;
4717 *pos++ = '\0';
4718
4719 max_disc_int = atoi(pos);
4720 pos = os_strchr(pos, ' ');
4721 if (pos == NULL)
4722 return -1;
4723 *pos++ = '\0';
4724
4725 max_disc_tu = atoi(pos);
4726
4727 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
4728 max_disc_int, max_disc_tu);
4729 }
4730
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004731 if (os_strcmp(cmd, "per_sta_psk") == 0) {
4732 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
4733 return 0;
4734 }
4735
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004736#ifdef CONFIG_WPS_NFC
4737 if (os_strcmp(cmd, "nfc_tag") == 0)
4738 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
4739#endif /* CONFIG_WPS_NFC */
4740
4741 if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
4742 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
4743 return 0;
4744 }
4745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004746 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
4747 cmd);
4748
4749 return -1;
4750}
4751
4752
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004753static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
4754{
4755 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4756 wpa_s->force_long_sd = 0;
4757 if (wpa_s->global->p2p)
4758 p2p_flush(wpa_s->global->p2p);
4759}
4760
4761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
4763{
4764 char *pos, *pos2;
4765 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
4766
4767 if (cmd[0]) {
4768 pos = os_strchr(cmd, ' ');
4769 if (pos == NULL)
4770 return -1;
4771 *pos++ = '\0';
4772 dur1 = atoi(cmd);
4773
4774 pos2 = os_strchr(pos, ' ');
4775 if (pos2)
4776 *pos2++ = '\0';
4777 int1 = atoi(pos);
4778 } else
4779 pos2 = NULL;
4780
4781 if (pos2) {
4782 pos = os_strchr(pos2, ' ');
4783 if (pos == NULL)
4784 return -1;
4785 *pos++ = '\0';
4786 dur2 = atoi(pos2);
4787 int2 = atoi(pos);
4788 }
4789
4790 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
4791}
4792
4793
4794static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
4795{
4796 char *pos;
4797 unsigned int period = 0, interval = 0;
4798
4799 if (cmd[0]) {
4800 pos = os_strchr(cmd, ' ');
4801 if (pos == NULL)
4802 return -1;
4803 *pos++ = '\0';
4804 period = atoi(cmd);
4805 interval = atoi(pos);
4806 }
4807
4808 return wpas_p2p_ext_listen(wpa_s, period, interval);
4809}
4810
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004811
4812static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
4813{
4814 const char *pos;
4815 u8 peer[ETH_ALEN];
4816 int iface_addr = 0;
4817
4818 pos = cmd;
4819 if (os_strncmp(pos, "iface=", 6) == 0) {
4820 iface_addr = 1;
4821 pos += 6;
4822 }
4823 if (hwaddr_aton(pos, peer))
4824 return -1;
4825
4826 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
4827 return 0;
4828}
4829
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004830#endif /* CONFIG_P2P */
4831
4832
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004833static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
4834{
4835 struct wpa_freq_range_list ranges;
4836 int *freqs = NULL;
4837 struct hostapd_hw_modes *mode;
4838 u16 i;
4839
4840 if (wpa_s->hw.modes == NULL)
4841 return NULL;
4842
4843 os_memset(&ranges, 0, sizeof(ranges));
4844 if (freq_range_list_parse(&ranges, val) < 0)
4845 return NULL;
4846
4847 for (i = 0; i < wpa_s->hw.num_modes; i++) {
4848 int j;
4849
4850 mode = &wpa_s->hw.modes[i];
4851 for (j = 0; j < mode->num_channels; j++) {
4852 unsigned int freq;
4853
4854 if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
4855 continue;
4856
4857 freq = mode->channels[j].freq;
4858 if (!freq_range_list_includes(&ranges, freq))
4859 continue;
4860
4861 int_array_add_unique(&freqs, freq);
4862 }
4863 }
4864
4865 os_free(ranges.range);
4866 return freqs;
4867}
4868
4869
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004870#ifdef CONFIG_INTERWORKING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004871
4872static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
4873{
4874 int auto_sel = 0;
4875 int *freqs = NULL;
4876
4877 if (param) {
4878 char *pos;
4879
4880 auto_sel = os_strstr(param, "auto") != NULL;
4881
4882 pos = os_strstr(param, "freq=");
4883 if (pos) {
4884 freqs = freq_range_to_channel_list(wpa_s, pos + 5);
4885 if (freqs == NULL)
4886 return -1;
4887 }
4888
4889 }
4890
4891 return interworking_select(wpa_s, auto_sel, freqs);
4892}
4893
4894
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004895static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
4896{
4897 u8 bssid[ETH_ALEN];
4898 struct wpa_bss *bss;
4899
4900 if (hwaddr_aton(dst, bssid)) {
4901 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
4902 return -1;
4903 }
4904
4905 bss = wpa_bss_get_bssid(wpa_s, bssid);
4906 if (bss == NULL) {
4907 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
4908 MAC2STR(bssid));
4909 return -1;
4910 }
4911
4912 return interworking_connect(wpa_s, bss);
4913}
4914
4915
4916static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
4917{
4918 u8 dst_addr[ETH_ALEN];
4919 int used;
4920 char *pos;
4921#define MAX_ANQP_INFO_ID 100
4922 u16 id[MAX_ANQP_INFO_ID];
4923 size_t num_id = 0;
4924
4925 used = hwaddr_aton2(dst, dst_addr);
4926 if (used < 0)
4927 return -1;
4928 pos = dst + used;
4929 while (num_id < MAX_ANQP_INFO_ID) {
4930 id[num_id] = atoi(pos);
4931 if (id[num_id])
4932 num_id++;
4933 pos = os_strchr(pos + 1, ',');
4934 if (pos == NULL)
4935 break;
4936 pos++;
4937 }
4938
4939 if (num_id == 0)
4940 return -1;
4941
4942 return anqp_send_req(wpa_s, dst_addr, id, num_id);
4943}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004944
4945
4946static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
4947{
4948 u8 dst_addr[ETH_ALEN];
4949 struct wpabuf *advproto, *query = NULL;
4950 int used, ret = -1;
4951 char *pos, *end;
4952 size_t len;
4953
4954 used = hwaddr_aton2(cmd, dst_addr);
4955 if (used < 0)
4956 return -1;
4957
4958 pos = cmd + used;
4959 while (*pos == ' ')
4960 pos++;
4961
4962 /* Advertisement Protocol ID */
4963 end = os_strchr(pos, ' ');
4964 if (end)
4965 len = end - pos;
4966 else
4967 len = os_strlen(pos);
4968 if (len & 0x01)
4969 return -1;
4970 len /= 2;
4971 if (len == 0)
4972 return -1;
4973 advproto = wpabuf_alloc(len);
4974 if (advproto == NULL)
4975 return -1;
4976 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
4977 goto fail;
4978
4979 if (end) {
4980 /* Optional Query Request */
4981 pos = end + 1;
4982 while (*pos == ' ')
4983 pos++;
4984
4985 len = os_strlen(pos);
4986 if (len) {
4987 if (len & 0x01)
4988 goto fail;
4989 len /= 2;
4990 if (len == 0)
4991 goto fail;
4992 query = wpabuf_alloc(len);
4993 if (query == NULL)
4994 goto fail;
4995 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
4996 goto fail;
4997 }
4998 }
4999
5000 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
5001
5002fail:
5003 wpabuf_free(advproto);
5004 wpabuf_free(query);
5005
5006 return ret;
5007}
5008
5009
5010static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
5011 size_t buflen)
5012{
5013 u8 addr[ETH_ALEN];
5014 int dialog_token;
5015 int used;
5016 char *pos;
5017 size_t resp_len, start, requested_len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005018 struct wpabuf *resp;
5019 int ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005020
5021 used = hwaddr_aton2(cmd, addr);
5022 if (used < 0)
5023 return -1;
5024
5025 pos = cmd + used;
5026 while (*pos == ' ')
5027 pos++;
5028 dialog_token = atoi(pos);
5029
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005030 if (wpa_s->last_gas_resp &&
5031 os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
5032 dialog_token == wpa_s->last_gas_dialog_token)
5033 resp = wpa_s->last_gas_resp;
5034 else if (wpa_s->prev_gas_resp &&
5035 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
5036 dialog_token == wpa_s->prev_gas_dialog_token)
5037 resp = wpa_s->prev_gas_resp;
5038 else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005039 return -1;
5040
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005041 resp_len = wpabuf_len(resp);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005042 start = 0;
5043 requested_len = resp_len;
5044
5045 pos = os_strchr(pos, ' ');
5046 if (pos) {
5047 start = atoi(pos);
5048 if (start > resp_len)
5049 return os_snprintf(buf, buflen, "FAIL-Invalid range");
5050 pos = os_strchr(pos, ',');
5051 if (pos == NULL)
5052 return -1;
5053 pos++;
5054 requested_len = atoi(pos);
5055 if (start + requested_len > resp_len)
5056 return os_snprintf(buf, buflen, "FAIL-Invalid range");
5057 }
5058
5059 if (requested_len * 2 + 1 > buflen)
5060 return os_snprintf(buf, buflen, "FAIL-Too long response");
5061
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005062 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
5063 requested_len);
5064
5065 if (start + requested_len == resp_len) {
5066 /*
5067 * Free memory by dropping the response after it has been
5068 * fetched.
5069 */
5070 if (resp == wpa_s->prev_gas_resp) {
5071 wpabuf_free(wpa_s->prev_gas_resp);
5072 wpa_s->prev_gas_resp = NULL;
5073 } else {
5074 wpabuf_free(wpa_s->last_gas_resp);
5075 wpa_s->last_gas_resp = NULL;
5076 }
5077 }
5078
5079 return ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005080}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005081#endif /* CONFIG_INTERWORKING */
5082
5083
Dmitry Shmidt04949592012-07-19 12:16:46 -07005084#ifdef CONFIG_HS20
5085
5086static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
5087{
5088 u8 dst_addr[ETH_ALEN];
5089 int used;
5090 char *pos;
5091 u32 subtypes = 0;
5092
5093 used = hwaddr_aton2(dst, dst_addr);
5094 if (used < 0)
5095 return -1;
5096 pos = dst + used;
5097 for (;;) {
5098 int num = atoi(pos);
5099 if (num <= 0 || num > 31)
5100 return -1;
5101 subtypes |= BIT(num);
5102 pos = os_strchr(pos + 1, ',');
5103 if (pos == NULL)
5104 break;
5105 pos++;
5106 }
5107
5108 if (subtypes == 0)
5109 return -1;
5110
5111 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
5112}
5113
5114
5115static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5116 const u8 *addr, const char *realm)
5117{
5118 u8 *buf;
5119 size_t rlen, len;
5120 int ret;
5121
5122 rlen = os_strlen(realm);
5123 len = 3 + rlen;
5124 buf = os_malloc(len);
5125 if (buf == NULL)
5126 return -1;
5127 buf[0] = 1; /* NAI Home Realm Count */
5128 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
5129 buf[2] = rlen;
5130 os_memcpy(buf + 3, realm, rlen);
5131
5132 ret = hs20_anqp_send_req(wpa_s, addr,
5133 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5134 buf, len);
5135
5136 os_free(buf);
5137
5138 return ret;
5139}
5140
5141
5142static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5143 char *dst)
5144{
5145 struct wpa_cred *cred = wpa_s->conf->cred;
5146 u8 dst_addr[ETH_ALEN];
5147 int used;
5148 u8 *buf;
5149 size_t len;
5150 int ret;
5151
5152 used = hwaddr_aton2(dst, dst_addr);
5153 if (used < 0)
5154 return -1;
5155
5156 while (dst[used] == ' ')
5157 used++;
5158 if (os_strncmp(dst + used, "realm=", 6) == 0)
5159 return hs20_nai_home_realm_list(wpa_s, dst_addr,
5160 dst + used + 6);
5161
5162 len = os_strlen(dst + used);
5163
5164 if (len == 0 && cred && cred->realm)
5165 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
5166
5167 if (len % 1)
5168 return -1;
5169 len /= 2;
5170 buf = os_malloc(len);
5171 if (buf == NULL)
5172 return -1;
5173 if (hexstr2bin(dst + used, buf, len) < 0) {
5174 os_free(buf);
5175 return -1;
5176 }
5177
5178 ret = hs20_anqp_send_req(wpa_s, dst_addr,
5179 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5180 buf, len);
5181 os_free(buf);
5182
5183 return ret;
5184}
5185
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005186
5187static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
5188{
5189 u8 dst_addr[ETH_ALEN];
5190 int used;
5191 char *icon;
5192
5193 used = hwaddr_aton2(cmd, dst_addr);
5194 if (used < 0)
5195 return -1;
5196
5197 while (cmd[used] == ' ')
5198 used++;
5199 icon = &cmd[used];
5200
5201 wpa_s->fetch_osu_icon_in_progress = 0;
5202 return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
5203 (u8 *) icon, os_strlen(icon));
5204}
5205
Dmitry Shmidt04949592012-07-19 12:16:46 -07005206#endif /* CONFIG_HS20 */
5207
5208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005209static int wpa_supplicant_ctrl_iface_sta_autoconnect(
5210 struct wpa_supplicant *wpa_s, char *cmd)
5211{
5212 wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
5213 return 0;
5214}
5215
5216
Dmitry Shmidt04949592012-07-19 12:16:46 -07005217#ifdef CONFIG_AUTOSCAN
5218
5219static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
5220 char *cmd)
5221{
5222 enum wpa_states state = wpa_s->wpa_state;
5223 char *new_params = NULL;
5224
5225 if (os_strlen(cmd) > 0) {
5226 new_params = os_strdup(cmd);
5227 if (new_params == NULL)
5228 return -1;
5229 }
5230
5231 os_free(wpa_s->conf->autoscan);
5232 wpa_s->conf->autoscan = new_params;
5233
5234 if (wpa_s->conf->autoscan == NULL)
5235 autoscan_deinit(wpa_s);
5236 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
5237 autoscan_init(wpa_s, 1);
5238 else if (state == WPA_SCANNING)
5239 wpa_supplicant_reinit_autoscan(wpa_s);
5240
5241 return 0;
5242}
5243
5244#endif /* CONFIG_AUTOSCAN */
5245
5246
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005247#ifdef CONFIG_WNM
5248
5249static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
5250{
5251 int enter;
5252 int intval = 0;
5253 char *pos;
5254 int ret;
5255 struct wpabuf *tfs_req = NULL;
5256
5257 if (os_strncmp(cmd, "enter", 5) == 0)
5258 enter = 1;
5259 else if (os_strncmp(cmd, "exit", 4) == 0)
5260 enter = 0;
5261 else
5262 return -1;
5263
5264 pos = os_strstr(cmd, " interval=");
5265 if (pos)
5266 intval = atoi(pos + 10);
5267
5268 pos = os_strstr(cmd, " tfs_req=");
5269 if (pos) {
5270 char *end;
5271 size_t len;
5272 pos += 9;
5273 end = os_strchr(pos, ' ');
5274 if (end)
5275 len = end - pos;
5276 else
5277 len = os_strlen(pos);
5278 if (len & 1)
5279 return -1;
5280 len /= 2;
5281 tfs_req = wpabuf_alloc(len);
5282 if (tfs_req == NULL)
5283 return -1;
5284 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
5285 wpabuf_free(tfs_req);
5286 return -1;
5287 }
5288 }
5289
5290 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
5291 WNM_SLEEP_MODE_EXIT, intval,
5292 tfs_req);
5293 wpabuf_free(tfs_req);
5294
5295 return ret;
5296}
5297
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005298
5299static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
5300{
5301 int query_reason;
5302
5303 query_reason = atoi(cmd);
5304
5305 wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
5306 query_reason);
5307
5308 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
5309}
5310
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005311#endif /* CONFIG_WNM */
5312
5313
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005314/* Get string representation of channel width */
5315static const char * channel_width_name(enum chan_width width)
5316{
5317 switch (width) {
5318 case CHAN_WIDTH_20_NOHT:
5319 return "20 MHz (no HT)";
5320 case CHAN_WIDTH_20:
5321 return "20 MHz";
5322 case CHAN_WIDTH_40:
5323 return "40 MHz";
5324 case CHAN_WIDTH_80:
5325 return "80 MHz";
5326 case CHAN_WIDTH_80P80:
5327 return "80+80 MHz";
5328 case CHAN_WIDTH_160:
5329 return "160 MHz";
5330 default:
5331 return "unknown";
5332 }
5333}
5334
5335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005336static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
5337 size_t buflen)
5338{
5339 struct wpa_signal_info si;
5340 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005341 char *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005342
5343 ret = wpa_drv_signal_poll(wpa_s, &si);
5344 if (ret)
5345 return -1;
5346
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005347 pos = buf;
5348 end = buf + buflen;
5349
5350 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005351 "NOISE=%d\nFREQUENCY=%u\n",
5352 si.current_signal, si.current_txrate / 1000,
5353 si.current_noise, si.frequency);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005354 if (ret < 0 || ret > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005355 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005356 pos += ret;
5357
5358 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
5359 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
5360 channel_width_name(si.chanwidth));
5361 if (ret < 0 || ret > end - pos)
5362 return -1;
5363 pos += ret;
5364 }
5365
5366 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
5367 ret = os_snprintf(pos, end - pos,
5368 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
5369 si.center_frq1, si.center_frq2);
5370 if (ret < 0 || ret > end - pos)
5371 return -1;
5372 pos += ret;
5373 }
5374
5375 if (si.avg_signal) {
5376 ret = os_snprintf(pos, end - pos,
5377 "AVG_RSSI=%d\n", si.avg_signal);
5378 if (ret < 0 || ret >= end - pos)
5379 return -1;
5380 pos += ret;
5381 }
5382
5383 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005384}
5385
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005386
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005387static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
5388 size_t buflen)
5389{
5390 struct hostap_sta_driver_data sta;
5391 int ret;
5392
5393 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
5394 if (ret)
5395 return -1;
5396
5397 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005398 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
5399 if (ret < 0 || (size_t) ret > buflen)
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005400 return -1;
5401 return ret;
5402}
5403
5404
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005405#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005406static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5407 char *buf, size_t buflen)
5408{
5409 int ret;
5410
5411 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005412 if (ret == 0) {
5413 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
5414 struct p2p_data *p2p = wpa_s->global->p2p;
5415 if (p2p) {
5416 char country[3];
5417 country[0] = cmd[8];
5418 country[1] = cmd[9];
5419 country[2] = 0x04;
5420 p2p_set_country(p2p, country);
5421 }
5422 }
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08005423 ret = os_snprintf(buf, buflen, "%s\n", "OK");
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005424 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005425 return ret;
5426}
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08005427#endif /* ANDROID */
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005428
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07005429
Dmitry Shmidta38abf92014-03-06 13:38:44 -08005430static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5431 char *buf, size_t buflen)
5432{
5433 int ret;
5434 char *pos;
5435 u8 *data = NULL;
5436 unsigned int vendor_id, subcmd;
5437 struct wpabuf *reply;
5438 size_t data_len = 0;
5439
5440 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
5441 vendor_id = strtoul(cmd, &pos, 16);
5442 if (!isblank(*pos))
5443 return -EINVAL;
5444
5445 subcmd = strtoul(pos, &pos, 10);
5446
5447 if (*pos != '\0') {
5448 if (!isblank(*pos++))
5449 return -EINVAL;
5450 data_len = os_strlen(pos);
5451 }
5452
5453 if (data_len) {
5454 data_len /= 2;
5455 data = os_malloc(data_len);
5456 if (!data)
5457 return -ENOBUFS;
5458
5459 if (hexstr2bin(pos, data, data_len)) {
5460 wpa_printf(MSG_DEBUG,
5461 "Vendor command: wrong parameter format");
5462 os_free(data);
5463 return -EINVAL;
5464 }
5465 }
5466
5467 reply = wpabuf_alloc((buflen - 1) / 2);
5468 if (!reply) {
5469 os_free(data);
5470 return -ENOBUFS;
5471 }
5472
5473 ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
5474 reply);
5475
5476 if (ret == 0)
5477 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
5478 wpabuf_len(reply));
5479
5480 wpabuf_free(reply);
5481 os_free(data);
5482
5483 return ret;
5484}
5485
5486
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005487static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
5488{
5489 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
5490
5491#ifdef CONFIG_P2P
5492 wpas_p2p_stop_find(wpa_s);
5493 p2p_ctrl_flush(wpa_s);
5494 wpas_p2p_group_remove(wpa_s, "*");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005495 wpas_p2p_service_flush(wpa_s);
5496 wpa_s->global->p2p_disabled = 0;
5497 wpa_s->global->p2p_per_sta_psk = 0;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005498 wpa_s->conf->num_sec_device_types = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005499 wpa_s->p2p_disable_ip_addr_req = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005500#endif /* CONFIG_P2P */
5501
5502#ifdef CONFIG_WPS_TESTING
5503 wps_version_number = 0x20;
5504 wps_testing_dummy_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005505 wps_corrupt_pkhash = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005506#endif /* CONFIG_WPS_TESTING */
5507#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005508 wpa_s->wps_fragment_size = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005509 wpas_wps_cancel(wpa_s);
5510#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07005511 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005512 wpa_s->known_wps_freq = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005513
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005514#ifdef CONFIG_TDLS
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005515#ifdef CONFIG_TDLS_TESTING
5516 extern unsigned int tdls_testing;
5517 tdls_testing = 0;
5518#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005519 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
5520 wpa_tdls_enable(wpa_s->wpa, 1);
5521#endif /* CONFIG_TDLS */
5522
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005523 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
5524 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
5525
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005526 wpa_s->no_keep_alive = 0;
5527
5528 os_free(wpa_s->disallow_aps_bssid);
5529 wpa_s->disallow_aps_bssid = NULL;
5530 wpa_s->disallow_aps_bssid_count = 0;
5531 os_free(wpa_s->disallow_aps_ssid);
5532 wpa_s->disallow_aps_ssid = NULL;
5533 wpa_s->disallow_aps_ssid_count = 0;
5534
5535 wpa_s->set_sta_uapsd = 0;
5536 wpa_s->sta_uapsd = 0;
5537
5538 wpa_drv_radio_disable(wpa_s, 0);
5539
5540 wpa_bss_flush(wpa_s);
5541 wpa_blacklist_clear(wpa_s);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07005542 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005543 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
5544 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005545 wpa_config_flush_blobs(wpa_s->conf);
Dmitry Shmidt18463232014-01-24 12:29:41 -08005546 wpa_s->conf->auto_interworking = 0;
5547 wpa_s->conf->okc = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005548
5549 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
5550 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
5551 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
5552 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5553
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08005554 radio_remove_works(wpa_s, NULL, 1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005555
5556 wpa_s->next_ssid = NULL;
5557
5558#ifdef CONFIG_INTERWORKING
5559 hs20_cancel_fetch_osu(wpa_s);
5560#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt818ea482014-03-10 13:15:21 -07005561
5562 wpa_s->ext_mgmt_frame_handling = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005563}
5564
5565
5566static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
5567 char *buf, size_t buflen)
5568{
5569 struct wpa_radio_work *work;
5570 char *pos, *end;
5571 struct os_reltime now, diff;
5572
5573 pos = buf;
5574 end = buf + buflen;
5575
5576 os_get_reltime(&now);
5577
5578 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
5579 {
5580 int ret;
5581
5582 os_reltime_sub(&now, &work->time, &diff);
5583 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
5584 work->type, work->wpa_s->ifname, work->freq,
5585 work->started, diff.sec, diff.usec);
5586 if (ret < 0 || ret >= end - pos)
5587 break;
5588 pos += ret;
5589 }
5590
5591 return pos - buf;
5592}
5593
5594
5595static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
5596{
5597 struct wpa_radio_work *work = eloop_ctx;
5598 struct wpa_external_work *ework = work->ctx;
5599
5600 wpa_dbg(work->wpa_s, MSG_DEBUG,
5601 "Timing out external radio work %u (%s)",
5602 ework->id, work->type);
5603 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
5604 os_free(ework);
5605 radio_work_done(work);
5606}
5607
5608
5609static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
5610{
5611 struct wpa_external_work *ework = work->ctx;
5612
5613 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08005614 if (work->started)
5615 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
5616 work, NULL);
5617
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005618 os_free(ework);
5619 return;
5620 }
5621
5622 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
5623 ework->id, ework->type);
5624 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
5625 if (!ework->timeout)
5626 ework->timeout = 10;
5627 eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
5628 work, NULL);
5629}
5630
5631
5632static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
5633 char *buf, size_t buflen)
5634{
5635 struct wpa_external_work *ework;
5636 char *pos, *pos2;
5637 size_t type_len;
5638 int ret;
5639 unsigned int freq = 0;
5640
5641 /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
5642
5643 ework = os_zalloc(sizeof(*ework));
5644 if (ework == NULL)
5645 return -1;
5646
5647 pos = os_strchr(cmd, ' ');
5648 if (pos) {
5649 type_len = pos - cmd;
5650 pos++;
5651
5652 pos2 = os_strstr(pos, "freq=");
5653 if (pos2)
5654 freq = atoi(pos2 + 5);
5655
5656 pos2 = os_strstr(pos, "timeout=");
5657 if (pos2)
5658 ework->timeout = atoi(pos2 + 8);
5659 } else {
5660 type_len = os_strlen(cmd);
5661 }
5662 if (4 + type_len >= sizeof(ework->type))
5663 type_len = sizeof(ework->type) - 4 - 1;
5664 os_strlcpy(ework->type, "ext:", sizeof(ework->type));
5665 os_memcpy(ework->type + 4, cmd, type_len);
5666 ework->type[4 + type_len] = '\0';
5667
5668 wpa_s->ext_work_id++;
5669 if (wpa_s->ext_work_id == 0)
5670 wpa_s->ext_work_id++;
5671 ework->id = wpa_s->ext_work_id;
5672
5673 if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
5674 ework) < 0) {
5675 os_free(ework);
5676 return -1;
5677 }
5678
5679 ret = os_snprintf(buf, buflen, "%u", ework->id);
5680 if (ret < 0 || (size_t) ret >= buflen)
5681 return -1;
5682 return ret;
5683}
5684
5685
5686static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
5687{
5688 struct wpa_radio_work *work;
5689 unsigned int id = atoi(cmd);
5690
5691 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
5692 {
5693 struct wpa_external_work *ework;
5694
5695 if (os_strncmp(work->type, "ext:", 4) != 0)
5696 continue;
5697 ework = work->ctx;
5698 if (id && ework->id != id)
5699 continue;
5700 wpa_dbg(wpa_s, MSG_DEBUG,
5701 "Completed external radio work %u (%s)",
5702 ework->id, ework->type);
5703 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005704 radio_work_done(work);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07005705 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005706 return 3; /* "OK\n" */
5707 }
5708
5709 return -1;
5710}
5711
5712
5713static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
5714 char *buf, size_t buflen)
5715{
5716 if (os_strcmp(cmd, "show") == 0)
5717 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
5718 if (os_strncmp(cmd, "add ", 4) == 0)
5719 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
5720 if (os_strncmp(cmd, "done ", 5) == 0)
5721 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
5722 return -1;
5723}
5724
5725
5726void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
5727{
5728 struct wpa_radio_work *work, *tmp;
5729
Dmitry Shmidt18463232014-01-24 12:29:41 -08005730 if (!wpa_s || !wpa_s->radio)
5731 return;
5732
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005733 dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
5734 struct wpa_radio_work, list) {
5735 struct wpa_external_work *ework;
5736
5737 if (os_strncmp(work->type, "ext:", 4) != 0)
5738 continue;
5739 ework = work->ctx;
5740 wpa_dbg(wpa_s, MSG_DEBUG,
5741 "Flushing %sexternal radio work %u (%s)",
5742 work->started ? " started" : "", ework->id,
5743 ework->type);
5744 if (work->started)
5745 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
5746 work, NULL);
5747 os_free(ework);
5748 radio_work_done(work);
5749 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005750}
5751
5752
Dmitry Shmidt051af732013-10-22 13:52:46 -07005753static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
5754{
5755 struct wpa_supplicant *wpa_s = eloop_ctx;
5756 eapol_sm_notify_ctrl_response(wpa_s->eapol);
5757}
5758
5759
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005760static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
5761{
5762 int *freqs = NULL;
5763
5764 freqs = freq_range_to_channel_list(wpa_s, val);
5765 if (freqs == NULL)
5766 return -1;
5767
5768 os_free(wpa_s->manual_scan_freqs);
5769 wpa_s->manual_scan_freqs = freqs;
5770
5771 return 0;
5772}
5773
5774
5775static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
5776 char *reply, int reply_size, int *reply_len)
5777{
5778 char *pos;
5779
5780 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5781 *reply_len = -1;
5782 return;
5783 }
5784
5785 wpa_s->manual_scan_passive = 0;
5786 wpa_s->manual_scan_use_id = 0;
5787 wpa_s->manual_scan_only_new = 0;
5788
5789 if (params) {
5790 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
5791 wpa_s->scan_res_handler = scan_only_handler;
5792
5793 pos = os_strstr(params, "freq=");
5794 if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
5795 *reply_len = -1;
5796 return;
5797 }
5798
5799 pos = os_strstr(params, "passive=");
5800 if (pos)
5801 wpa_s->manual_scan_passive = !!atoi(pos + 8);
5802
5803 pos = os_strstr(params, "use_id=");
5804 if (pos)
5805 wpa_s->manual_scan_use_id = atoi(pos + 7);
5806
5807 pos = os_strstr(params, "only_new=1");
5808 if (pos)
5809 wpa_s->manual_scan_only_new = 1;
5810 } else {
5811 os_free(wpa_s->manual_scan_freqs);
5812 wpa_s->manual_scan_freqs = NULL;
5813 if (wpa_s->scan_res_handler == scan_only_handler)
5814 wpa_s->scan_res_handler = NULL;
5815 }
5816
5817 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
5818 ((wpa_s->wpa_state <= WPA_SCANNING) ||
5819 (wpa_s->wpa_state == WPA_COMPLETED))) {
5820 wpa_s->normal_scans = 0;
5821 wpa_s->scan_req = MANUAL_SCAN_REQ;
5822 wpa_s->after_wps = 0;
5823 wpa_s->known_wps_freq = 0;
5824 wpa_supplicant_req_scan(wpa_s, 0, 0);
5825 if (wpa_s->manual_scan_use_id) {
5826 wpa_s->manual_scan_id++;
5827 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
5828 wpa_s->manual_scan_id);
5829 *reply_len = os_snprintf(reply, reply_size, "%u\n",
5830 wpa_s->manual_scan_id);
5831 }
5832 } else if (wpa_s->sched_scanning) {
5833 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
5834 wpa_supplicant_cancel_sched_scan(wpa_s);
5835 wpa_s->scan_req = MANUAL_SCAN_REQ;
5836 wpa_supplicant_req_scan(wpa_s, 0, 0);
5837 if (wpa_s->manual_scan_use_id) {
5838 wpa_s->manual_scan_id++;
5839 *reply_len = os_snprintf(reply, reply_size, "%u\n",
5840 wpa_s->manual_scan_id);
5841 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
5842 wpa_s->manual_scan_id);
5843 }
5844 } else {
5845 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
5846 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
5847 }
5848}
5849
5850
Dmitry Shmidt818ea482014-03-10 13:15:21 -07005851#ifdef CONFIG_TESTING_OPTIONS
5852
5853static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
5854 unsigned int freq, const u8 *dst,
5855 const u8 *src, const u8 *bssid,
5856 const u8 *data, size_t data_len,
5857 enum offchannel_send_action_result
5858 result)
5859{
5860 wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
5861 " src=" MACSTR " bssid=" MACSTR " result=%s",
5862 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
5863 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
5864 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
5865 "NO_ACK" : "FAILED"));
5866}
5867
5868
5869static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
5870{
5871 char *pos, *param;
5872 size_t len;
5873 u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
5874 int res, used;
5875 int freq = 0, no_cck = 0, wait_time = 0;
5876
5877 /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
5878 * <action=Action frame payload> */
5879
5880 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
5881
5882 pos = cmd;
5883 used = hwaddr_aton2(pos, da);
5884 if (used < 0)
5885 return -1;
5886 pos += used;
5887 while (*pos == ' ')
5888 pos++;
5889 used = hwaddr_aton2(pos, bssid);
5890 if (used < 0)
5891 return -1;
5892 pos += used;
5893
5894 param = os_strstr(pos, " freq=");
5895 if (param) {
5896 param += 6;
5897 freq = atoi(param);
5898 }
5899
5900 param = os_strstr(pos, " no_cck=");
5901 if (param) {
5902 param += 8;
5903 no_cck = atoi(param);
5904 }
5905
5906 param = os_strstr(pos, " wait_time=");
5907 if (param) {
5908 param += 11;
5909 wait_time = atoi(param);
5910 }
5911
5912 param = os_strstr(pos, " action=");
5913 if (param == NULL)
5914 return -1;
5915 param += 8;
5916
5917 len = os_strlen(param);
5918 if (len & 1)
5919 return -1;
5920 len /= 2;
5921
5922 buf = os_malloc(len);
5923 if (buf == NULL)
5924 return -1;
5925
5926 if (hexstr2bin(param, buf, len) < 0) {
5927 os_free(buf);
5928 return -1;
5929 }
5930
5931 res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
5932 buf, len, wait_time,
5933 wpas_ctrl_iface_mgmt_tx_cb, no_cck);
5934 os_free(buf);
5935 return res;
5936}
5937
5938
5939static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
5940{
5941 wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
5942 offchannel_send_action_done(wpa_s);
5943}
5944
5945#endif /* CONFIG_TESTING_OPTIONS */
5946
5947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005948char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
5949 char *buf, size_t *resp_len)
5950{
5951 char *reply;
5952 const int reply_size = 4096;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005953 int reply_len;
5954
5955 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005956 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
5957 if (wpa_debug_show_keys)
5958 wpa_dbg(wpa_s, MSG_DEBUG,
5959 "Control interface command '%s'", buf);
5960 else
5961 wpa_dbg(wpa_s, MSG_DEBUG,
5962 "Control interface command '%s [REMOVED]'",
5963 os_strncmp(buf, WPA_CTRL_RSP,
5964 os_strlen(WPA_CTRL_RSP)) == 0 ?
5965 WPA_CTRL_RSP : "SET_NETWORK");
5966 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
5967 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
5968 os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005969 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
5970 (const u8 *) buf, os_strlen(buf));
5971 } else {
5972 int level = MSG_DEBUG;
5973 if (os_strcmp(buf, "PING") == 0)
5974 level = MSG_EXCESSIVE;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005975 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005976 }
5977
5978 reply = os_malloc(reply_size);
5979 if (reply == NULL) {
5980 *resp_len = 1;
5981 return NULL;
5982 }
5983
5984 os_memcpy(reply, "OK\n", 3);
5985 reply_len = 3;
5986
5987 if (os_strcmp(buf, "PING") == 0) {
5988 os_memcpy(reply, "PONG\n", 5);
5989 reply_len = 5;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005990 } else if (os_strcmp(buf, "IFNAME") == 0) {
5991 reply_len = os_strlen(wpa_s->ifname);
5992 os_memcpy(reply, wpa_s->ifname, reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005993 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
5994 if (wpa_debug_reopen_file() < 0)
5995 reply_len = -1;
5996 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
5997 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
5998 } else if (os_strcmp(buf, "MIB") == 0) {
5999 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
6000 if (reply_len >= 0) {
6001 int res;
6002 res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
6003 reply_size - reply_len);
6004 if (res < 0)
6005 reply_len = -1;
6006 else
6007 reply_len += res;
6008 }
6009 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
6010 reply_len = wpa_supplicant_ctrl_iface_status(
6011 wpa_s, buf + 6, reply, reply_size);
6012 } else if (os_strcmp(buf, "PMKSA") == 0) {
6013 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
6014 reply_size);
6015 } else if (os_strncmp(buf, "SET ", 4) == 0) {
6016 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
6017 reply_len = -1;
6018 } else if (os_strncmp(buf, "GET ", 4) == 0) {
6019 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
6020 reply, reply_size);
6021 } else if (os_strcmp(buf, "LOGON") == 0) {
6022 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
6023 } else if (os_strcmp(buf, "LOGOFF") == 0) {
6024 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
6025 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
6026 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
6027 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006028 else
6029 wpas_request_connection(wpa_s);
Dmitry Shmidt98660862014-03-11 17:26:21 -07006030 } else if (os_strcmp(buf, "REATTACH") == 0) {
6031 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
6032 !wpa_s->current_ssid)
6033 reply_len = -1;
6034 else {
6035 wpa_s->reattach = 1;
6036 wpas_request_connection(wpa_s);
6037 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006038 } else if (os_strcmp(buf, "RECONNECT") == 0) {
6039 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
6040 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006041 else if (wpa_s->disconnected)
6042 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006043#ifdef IEEE8021X_EAPOL
6044 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
6045 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
6046 reply_len = -1;
6047#endif /* IEEE8021X_EAPOL */
6048#ifdef CONFIG_PEERKEY
6049 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
6050 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
6051 reply_len = -1;
6052#endif /* CONFIG_PEERKEY */
6053#ifdef CONFIG_IEEE80211R
6054 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
6055 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
6056 reply_len = -1;
6057#endif /* CONFIG_IEEE80211R */
6058#ifdef CONFIG_WPS
6059 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
6060 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
6061 if (res == -2) {
6062 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
6063 reply_len = 17;
6064 } else if (res)
6065 reply_len = -1;
6066 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
6067 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
6068 if (res == -2) {
6069 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
6070 reply_len = 17;
6071 } else if (res)
6072 reply_len = -1;
6073 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
6074 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
6075 reply,
6076 reply_size);
6077 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
6078 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
6079 wpa_s, buf + 14, reply, reply_size);
6080 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
6081 if (wpas_wps_cancel(wpa_s))
6082 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006083#ifdef CONFIG_WPS_NFC
6084 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
6085 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
6086 reply_len = -1;
6087 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
6088 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
6089 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006090 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
6091 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
6092 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006093 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
6094 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
6095 wpa_s, buf + 14, reply, reply_size);
6096 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
6097 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
6098 buf + 17))
6099 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006100 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
6101 reply_len = wpas_ctrl_nfc_get_handover_req(
6102 wpa_s, buf + 21, reply, reply_size);
6103 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
6104 reply_len = wpas_ctrl_nfc_get_handover_sel(
6105 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006106 } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
6107 if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
6108 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006109 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
6110 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
6111 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006112#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006113 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
6114 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
6115 reply_len = -1;
6116#ifdef CONFIG_AP
6117 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
6118 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
6119 wpa_s, buf + 11, reply, reply_size);
6120#endif /* CONFIG_AP */
6121#ifdef CONFIG_WPS_ER
6122 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
6123 if (wpas_wps_er_start(wpa_s, NULL))
6124 reply_len = -1;
6125 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
6126 if (wpas_wps_er_start(wpa_s, buf + 13))
6127 reply_len = -1;
6128 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
6129 if (wpas_wps_er_stop(wpa_s))
6130 reply_len = -1;
6131 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
6132 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
6133 reply_len = -1;
6134 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
6135 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
6136 if (ret == -2) {
6137 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
6138 reply_len = 17;
6139 } else if (ret == -3) {
6140 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
6141 reply_len = 18;
6142 } else if (ret == -4) {
6143 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
6144 reply_len = 20;
6145 } else if (ret)
6146 reply_len = -1;
6147 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
6148 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
6149 reply_len = -1;
6150 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
6151 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
6152 buf + 18))
6153 reply_len = -1;
6154 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
6155 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
6156 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006157#ifdef CONFIG_WPS_NFC
6158 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
6159 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
6160 wpa_s, buf + 24, reply, reply_size);
6161#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006162#endif /* CONFIG_WPS_ER */
6163#endif /* CONFIG_WPS */
6164#ifdef CONFIG_IBSS_RSN
6165 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
6166 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
6167 reply_len = -1;
6168#endif /* CONFIG_IBSS_RSN */
6169#ifdef CONFIG_P2P
6170 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
6171 if (p2p_ctrl_find(wpa_s, buf + 9))
6172 reply_len = -1;
6173 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
6174 if (p2p_ctrl_find(wpa_s, ""))
6175 reply_len = -1;
6176 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
6177 wpas_p2p_stop_find(wpa_s);
6178 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
6179 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
6180 reply_size);
6181 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
6182 if (p2p_ctrl_listen(wpa_s, buf + 11))
6183 reply_len = -1;
6184 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
6185 if (p2p_ctrl_listen(wpa_s, ""))
6186 reply_len = -1;
6187 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
6188 if (wpas_p2p_group_remove(wpa_s, buf + 17))
6189 reply_len = -1;
6190 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006191 if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006192 reply_len = -1;
6193 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
6194 if (p2p_ctrl_group_add(wpa_s, buf + 14))
6195 reply_len = -1;
6196 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
6197 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
6198 reply_len = -1;
6199 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
6200 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
6201 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
6202 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
6203 reply_size);
6204 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
6205 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
6206 reply_len = -1;
6207 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
6208 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
6209 reply_len = -1;
6210 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
6211 wpas_p2p_sd_service_update(wpa_s);
6212 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
6213 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
6214 reply_len = -1;
6215 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
6216 wpas_p2p_service_flush(wpa_s);
6217 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
6218 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
6219 reply_len = -1;
6220 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
6221 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
6222 reply_len = -1;
6223 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
6224 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
6225 reply_len = -1;
6226 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
6227 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
6228 reply_len = -1;
6229 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
6230 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
6231 reply_size);
6232 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
6233 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
6234 reply_len = -1;
6235 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006236 p2p_ctrl_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006237 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
6238 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
6239 reply_len = -1;
6240 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
6241 if (wpas_p2p_cancel(wpa_s))
6242 reply_len = -1;
6243 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
6244 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
6245 reply_len = -1;
6246 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
6247 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
6248 reply_len = -1;
6249 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
6250 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
6251 reply_len = -1;
6252 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
6253 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
6254 reply_len = -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006255 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
6256 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
6257 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006258#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006259#ifdef CONFIG_WIFI_DISPLAY
6260 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
6261 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
6262 reply_len = -1;
6263 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
6264 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
6265 reply, reply_size);
6266#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006267#ifdef CONFIG_INTERWORKING
6268 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
6269 if (interworking_fetch_anqp(wpa_s) < 0)
6270 reply_len = -1;
6271 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
6272 interworking_stop_fetch_anqp(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006273 } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
6274 if (ctrl_interworking_select(wpa_s, NULL) < 0)
6275 reply_len = -1;
6276 } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
6277 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006278 reply_len = -1;
6279 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
6280 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
6281 reply_len = -1;
6282 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
6283 if (get_anqp(wpa_s, buf + 9) < 0)
6284 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006285 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
6286 if (gas_request(wpa_s, buf + 12) < 0)
6287 reply_len = -1;
6288 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
6289 reply_len = gas_response_get(wpa_s, buf + 17, reply,
6290 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006291#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07006292#ifdef CONFIG_HS20
6293 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
6294 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
6295 reply_len = -1;
6296 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
6297 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
6298 reply_len = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006299 } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
6300 if (hs20_icon_request(wpa_s, buf + 18) < 0)
6301 reply_len = -1;
6302 } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
6303 if (hs20_fetch_osu(wpa_s) < 0)
6304 reply_len = -1;
6305 } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
6306 hs20_cancel_fetch_osu(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006307#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006308 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
6309 {
6310 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
6311 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
6312 reply_len = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006313 else {
6314 /*
6315 * Notify response from timeout to allow the control
6316 * interface response to be sent first.
6317 */
6318 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
6319 wpa_s, NULL);
6320 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006321 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
6322 if (wpa_supplicant_reload_configuration(wpa_s))
6323 reply_len = -1;
6324 } else if (os_strcmp(buf, "TERMINATE") == 0) {
6325 wpa_supplicant_terminate_proc(wpa_s->global);
6326 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
6327 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
6328 reply_len = -1;
Dmitry Shmidte19501d2011-03-16 14:32:18 -07006329 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006330 reply_len = wpa_supplicant_ctrl_iface_blacklist(
6331 wpa_s, buf + 9, reply, reply_size);
6332 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
6333 reply_len = wpa_supplicant_ctrl_iface_log_level(
6334 wpa_s, buf + 9, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006335 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
6336 reply_len = wpa_supplicant_ctrl_iface_list_networks(
6337 wpa_s, reply, reply_size);
6338 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006339#ifdef CONFIG_SME
6340 wpa_s->sme.prev_bssid_set = 0;
6341#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006342 wpa_s->reassociate = 0;
6343 wpa_s->disconnected = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006344 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006345 wpa_supplicant_cancel_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006346 wpa_supplicant_deauthenticate(wpa_s,
6347 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006348 } else if (os_strcmp(buf, "SCAN") == 0) {
6349 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
6350 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
6351 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006352 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
6353 reply_len = wpa_supplicant_ctrl_iface_scan_results(
6354 wpa_s, reply, reply_size);
6355 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
6356 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
6357 reply_len = -1;
6358 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
6359 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
6360 reply_len = -1;
6361 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
6362 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
6363 reply_len = -1;
6364 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
6365 reply_len = wpa_supplicant_ctrl_iface_add_network(
6366 wpa_s, reply, reply_size);
6367 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
6368 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
6369 reply_len = -1;
6370 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
6371 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
6372 reply_len = -1;
6373 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
6374 reply_len = wpa_supplicant_ctrl_iface_get_network(
6375 wpa_s, buf + 12, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006376 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
6377 reply_len = wpa_supplicant_ctrl_iface_list_creds(
6378 wpa_s, reply, reply_size);
6379 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
6380 reply_len = wpa_supplicant_ctrl_iface_add_cred(
6381 wpa_s, reply, reply_size);
6382 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
6383 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
6384 reply_len = -1;
6385 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
6386 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
6387 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006388#ifndef CONFIG_NO_CONFIG_WRITE
6389 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
6390 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
6391 reply_len = -1;
6392#endif /* CONFIG_NO_CONFIG_WRITE */
6393 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
6394 reply_len = wpa_supplicant_ctrl_iface_get_capability(
6395 wpa_s, buf + 15, reply, reply_size);
6396 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
6397 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
6398 reply_len = -1;
6399 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
6400 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
6401 reply_len = -1;
6402 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
6403 reply_len = wpa_supplicant_global_iface_list(
6404 wpa_s->global, reply, reply_size);
6405 } else if (os_strcmp(buf, "INTERFACES") == 0) {
6406 reply_len = wpa_supplicant_global_iface_interfaces(
6407 wpa_s->global, reply, reply_size);
6408 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
6409 reply_len = wpa_supplicant_ctrl_iface_bss(
6410 wpa_s, buf + 4, reply, reply_size);
6411#ifdef CONFIG_AP
6412 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
6413 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
6414 } else if (os_strncmp(buf, "STA ", 4) == 0) {
6415 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
6416 reply_size);
6417 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
6418 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
6419 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006420 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
6421 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
6422 reply_len = -1;
6423 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
6424 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
6425 reply_len = -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006426 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
6427 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
6428 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006429#endif /* CONFIG_AP */
6430 } else if (os_strcmp(buf, "SUSPEND") == 0) {
6431 wpas_notify_suspend(wpa_s->global);
6432 } else if (os_strcmp(buf, "RESUME") == 0) {
6433 wpas_notify_resume(wpa_s->global);
6434 } else if (os_strcmp(buf, "DROP_SA") == 0) {
6435 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
6436 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
6437 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
6438 reply_len = -1;
6439 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
6440 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
6441 reply_len = -1;
6442 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
6443 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
6444 reply_len = -1;
6445 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
6446 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
6447 buf + 17))
6448 reply_len = -1;
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07006449 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
6450 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
6451 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006452#ifdef CONFIG_TDLS
6453 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
6454 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
6455 reply_len = -1;
6456 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
6457 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
6458 reply_len = -1;
6459 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
6460 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
6461 reply_len = -1;
6462#endif /* CONFIG_TDLS */
6463 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
6464 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
6465 reply_size);
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07006466 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
6467 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
6468 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006469#ifdef CONFIG_AUTOSCAN
6470 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
6471 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
6472 reply_len = -1;
6473#endif /* CONFIG_AUTOSCAN */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006474#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07006475 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
6476 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
6477 reply_size);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006478#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006479 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
6480 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
6481 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006482 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006483 pmksa_cache_clear_current(wpa_s->wpa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006484 eapol_sm_request_reauth(wpa_s->eapol);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006485#ifdef CONFIG_WNM
6486 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
6487 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
6488 reply_len = -1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07006489 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
6490 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
6491 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006492#endif /* CONFIG_WNM */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006493 } else if (os_strcmp(buf, "FLUSH") == 0) {
6494 wpa_supplicant_ctrl_iface_flush(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006495 } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
6496 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
6497 reply_size);
Dmitry Shmidt818ea482014-03-10 13:15:21 -07006498#ifdef CONFIG_TESTING_OPTIONS
6499 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
6500 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
6501 reply_len = -1;
6502 } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
6503 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
6504#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006505 } else {
6506 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
6507 reply_len = 16;
6508 }
6509
6510 if (reply_len < 0) {
6511 os_memcpy(reply, "FAIL\n", 5);
6512 reply_len = 5;
6513 }
6514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006515 *resp_len = reply_len;
6516 return reply;
6517}
6518
6519
6520static int wpa_supplicant_global_iface_add(struct wpa_global *global,
6521 char *cmd)
6522{
6523 struct wpa_interface iface;
6524 char *pos;
6525
6526 /*
6527 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
6528 * TAB<bridge_ifname>
6529 */
6530 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
6531
6532 os_memset(&iface, 0, sizeof(iface));
6533
6534 do {
6535 iface.ifname = pos = cmd;
6536 pos = os_strchr(pos, '\t');
6537 if (pos)
6538 *pos++ = '\0';
6539 if (iface.ifname[0] == '\0')
6540 return -1;
6541 if (pos == NULL)
6542 break;
6543
6544 iface.confname = pos;
6545 pos = os_strchr(pos, '\t');
6546 if (pos)
6547 *pos++ = '\0';
6548 if (iface.confname[0] == '\0')
6549 iface.confname = NULL;
6550 if (pos == NULL)
6551 break;
6552
6553 iface.driver = pos;
6554 pos = os_strchr(pos, '\t');
6555 if (pos)
6556 *pos++ = '\0';
6557 if (iface.driver[0] == '\0')
6558 iface.driver = NULL;
6559 if (pos == NULL)
6560 break;
6561
6562 iface.ctrl_interface = pos;
6563 pos = os_strchr(pos, '\t');
6564 if (pos)
6565 *pos++ = '\0';
6566 if (iface.ctrl_interface[0] == '\0')
6567 iface.ctrl_interface = NULL;
6568 if (pos == NULL)
6569 break;
6570
6571 iface.driver_param = pos;
6572 pos = os_strchr(pos, '\t');
6573 if (pos)
6574 *pos++ = '\0';
6575 if (iface.driver_param[0] == '\0')
6576 iface.driver_param = NULL;
6577 if (pos == NULL)
6578 break;
6579
6580 iface.bridge_ifname = pos;
6581 pos = os_strchr(pos, '\t');
6582 if (pos)
6583 *pos++ = '\0';
6584 if (iface.bridge_ifname[0] == '\0')
6585 iface.bridge_ifname = NULL;
6586 if (pos == NULL)
6587 break;
6588 } while (0);
6589
6590 if (wpa_supplicant_get_iface(global, iface.ifname))
6591 return -1;
6592
6593 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
6594}
6595
6596
6597static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
6598 char *cmd)
6599{
6600 struct wpa_supplicant *wpa_s;
6601
6602 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
6603
6604 wpa_s = wpa_supplicant_get_iface(global, cmd);
6605 if (wpa_s == NULL)
6606 return -1;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07006607 return wpa_supplicant_remove_iface(global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006608}
6609
6610
6611static void wpa_free_iface_info(struct wpa_interface_info *iface)
6612{
6613 struct wpa_interface_info *prev;
6614
6615 while (iface) {
6616 prev = iface;
6617 iface = iface->next;
6618
6619 os_free(prev->ifname);
6620 os_free(prev->desc);
6621 os_free(prev);
6622 }
6623}
6624
6625
6626static int wpa_supplicant_global_iface_list(struct wpa_global *global,
6627 char *buf, int len)
6628{
6629 int i, res;
6630 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
6631 char *pos, *end;
6632
6633 for (i = 0; wpa_drivers[i]; i++) {
6634 struct wpa_driver_ops *drv = wpa_drivers[i];
6635 if (drv->get_interfaces == NULL)
6636 continue;
6637 tmp = drv->get_interfaces(global->drv_priv[i]);
6638 if (tmp == NULL)
6639 continue;
6640
6641 if (last == NULL)
6642 iface = last = tmp;
6643 else
6644 last->next = tmp;
6645 while (last->next)
6646 last = last->next;
6647 }
6648
6649 pos = buf;
6650 end = buf + len;
6651 for (tmp = iface; tmp; tmp = tmp->next) {
6652 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
6653 tmp->drv_name, tmp->ifname,
6654 tmp->desc ? tmp->desc : "");
6655 if (res < 0 || res >= end - pos) {
6656 *pos = '\0';
6657 break;
6658 }
6659 pos += res;
6660 }
6661
6662 wpa_free_iface_info(iface);
6663
6664 return pos - buf;
6665}
6666
6667
6668static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
6669 char *buf, int len)
6670{
6671 int res;
6672 char *pos, *end;
6673 struct wpa_supplicant *wpa_s;
6674
6675 wpa_s = global->ifaces;
6676 pos = buf;
6677 end = buf + len;
6678
6679 while (wpa_s) {
6680 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
6681 if (res < 0 || res >= end - pos) {
6682 *pos = '\0';
6683 break;
6684 }
6685 pos += res;
6686 wpa_s = wpa_s->next;
6687 }
6688 return pos - buf;
6689}
6690
6691
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006692static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
6693 const char *ifname,
6694 char *cmd, size_t *resp_len)
6695{
6696 struct wpa_supplicant *wpa_s;
6697
6698 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6699 if (os_strcmp(ifname, wpa_s->ifname) == 0)
6700 break;
6701 }
6702
6703 if (wpa_s == NULL) {
6704 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
6705 if (resp)
6706 *resp_len = os_strlen(resp);
6707 else
6708 *resp_len = 1;
6709 return resp;
6710 }
6711
6712 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
6713}
6714
6715
6716static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
6717 char *buf, size_t *resp_len)
6718{
6719#ifdef CONFIG_P2P
6720 static const char * cmd[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07006721 "LIST_NETWORKS",
6722 "SAVE_CONFIG",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006723 "P2P_FIND",
6724 "P2P_STOP_FIND",
6725 "P2P_LISTEN",
6726 "P2P_GROUP_ADD",
6727 "P2P_GET_PASSPHRASE",
6728 "P2P_SERVICE_UPDATE",
6729 "P2P_SERVICE_FLUSH",
6730 "P2P_FLUSH",
6731 "P2P_CANCEL",
6732 "P2P_PRESENCE_REQ",
6733 "P2P_EXT_LISTEN",
6734 NULL
6735 };
6736 static const char * prefix[] = {
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08006737#ifdef ANDROID
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07006738 "DRIVER ",
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08006739#endif /* ANDROID */
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07006740 "GET_NETWORK ",
6741 "REMOVE_NETWORK ",
6742 "SET ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006743 "P2P_FIND ",
6744 "P2P_CONNECT ",
6745 "P2P_LISTEN ",
6746 "P2P_GROUP_REMOVE ",
6747 "P2P_GROUP_ADD ",
6748 "P2P_PROV_DISC ",
6749 "P2P_SERV_DISC_REQ ",
6750 "P2P_SERV_DISC_CANCEL_REQ ",
6751 "P2P_SERV_DISC_RESP ",
6752 "P2P_SERV_DISC_EXTERNAL ",
6753 "P2P_SERVICE_ADD ",
6754 "P2P_SERVICE_DEL ",
6755 "P2P_REJECT ",
6756 "P2P_INVITE ",
6757 "P2P_PEER ",
6758 "P2P_SET ",
6759 "P2P_UNAUTHORIZE ",
6760 "P2P_PRESENCE_REQ ",
6761 "P2P_EXT_LISTEN ",
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006762 "P2P_REMOVE_CLIENT ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006763 NULL
6764 };
6765 int found = 0;
6766 int i;
6767
6768 if (global->p2p_init_wpa_s == NULL)
6769 return NULL;
6770
6771 for (i = 0; !found && cmd[i]; i++) {
6772 if (os_strcmp(buf, cmd[i]) == 0)
6773 found = 1;
6774 }
6775
6776 for (i = 0; !found && prefix[i]; i++) {
6777 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
6778 found = 1;
6779 }
6780
6781 if (found)
6782 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6783 buf, resp_len);
6784#endif /* CONFIG_P2P */
6785 return NULL;
6786}
6787
6788
6789static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
6790 char *buf, size_t *resp_len)
6791{
6792#ifdef CONFIG_WIFI_DISPLAY
6793 if (global->p2p_init_wpa_s == NULL)
6794 return NULL;
6795 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
6796 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
6797 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6798 buf, resp_len);
6799#endif /* CONFIG_WIFI_DISPLAY */
6800 return NULL;
6801}
6802
6803
6804static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
6805 char *buf, size_t *resp_len)
6806{
6807 char *ret;
6808
6809 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
6810 if (ret)
6811 return ret;
6812
6813 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
6814 if (ret)
6815 return ret;
6816
6817 return NULL;
6818}
6819
6820
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006821static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
6822{
6823 char *value;
6824
6825 value = os_strchr(cmd, ' ');
6826 if (value == NULL)
6827 return -1;
6828 *value++ = '\0';
6829
6830 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
6831
6832#ifdef CONFIG_WIFI_DISPLAY
6833 if (os_strcasecmp(cmd, "wifi_display") == 0) {
6834 wifi_display_enable(global, !!atoi(value));
6835 return 0;
6836 }
6837#endif /* CONFIG_WIFI_DISPLAY */
6838
6839 return -1;
6840}
6841
6842
6843#ifndef CONFIG_NO_CONFIG_WRITE
6844static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
6845{
6846 int ret = 0;
6847 struct wpa_supplicant *wpa_s;
6848
6849 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6850 if (!wpa_s->conf->update_config) {
6851 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
6852 continue;
6853 }
6854
6855 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
6856 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
6857 ret = 1;
6858 } else {
6859 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
6860 }
6861 }
6862
6863 return ret;
6864}
6865#endif /* CONFIG_NO_CONFIG_WRITE */
6866
6867
6868static int wpas_global_ctrl_iface_status(struct wpa_global *global,
6869 char *buf, size_t buflen)
6870{
6871 char *pos, *end;
6872 int ret;
6873 struct wpa_supplicant *wpa_s;
6874
6875 pos = buf;
6876 end = buf + buflen;
6877
6878#ifdef CONFIG_P2P
6879 if (global->p2p && !global->p2p_disabled) {
6880 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
6881 "\n"
6882 "p2p_state=%s\n",
6883 MAC2STR(global->p2p_dev_addr),
6884 p2p_get_state_txt(global->p2p));
6885 if (ret < 0 || ret >= end - pos)
6886 return pos - buf;
6887 pos += ret;
6888 } else if (global->p2p) {
6889 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
6890 if (ret < 0 || ret >= end - pos)
6891 return pos - buf;
6892 pos += ret;
6893 }
6894#endif /* CONFIG_P2P */
6895
6896#ifdef CONFIG_WIFI_DISPLAY
6897 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
6898 !!global->wifi_display);
6899 if (ret < 0 || ret >= end - pos)
6900 return pos - buf;
6901 pos += ret;
6902#endif /* CONFIG_WIFI_DISPLAY */
6903
6904 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6905 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
6906 "address=" MACSTR "\n",
6907 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
6908 if (ret < 0 || ret >= end - pos)
6909 return pos - buf;
6910 pos += ret;
6911 }
6912
6913 return pos - buf;
6914}
6915
6916
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006917char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
6918 char *buf, size_t *resp_len)
6919{
6920 char *reply;
6921 const int reply_size = 2048;
6922 int reply_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006923 int level = MSG_DEBUG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006924
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006925 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
6926 char *pos = os_strchr(buf + 7, ' ');
6927 if (pos) {
6928 *pos++ = '\0';
6929 return wpas_global_ctrl_iface_ifname(global,
6930 buf + 7, pos,
6931 resp_len);
6932 }
6933 }
6934
6935 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
6936 if (reply)
6937 return reply;
6938
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006939 if (os_strcmp(buf, "PING") == 0)
6940 level = MSG_EXCESSIVE;
6941 wpa_hexdump_ascii(level, "RX global ctrl_iface",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006942 (const u8 *) buf, os_strlen(buf));
6943
6944 reply = os_malloc(reply_size);
6945 if (reply == NULL) {
6946 *resp_len = 1;
6947 return NULL;
6948 }
6949
6950 os_memcpy(reply, "OK\n", 3);
6951 reply_len = 3;
6952
6953 if (os_strcmp(buf, "PING") == 0) {
6954 os_memcpy(reply, "PONG\n", 5);
6955 reply_len = 5;
6956 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
6957 if (wpa_supplicant_global_iface_add(global, buf + 14))
6958 reply_len = -1;
6959 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
6960 if (wpa_supplicant_global_iface_remove(global, buf + 17))
6961 reply_len = -1;
6962 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
6963 reply_len = wpa_supplicant_global_iface_list(
6964 global, reply, reply_size);
6965 } else if (os_strcmp(buf, "INTERFACES") == 0) {
6966 reply_len = wpa_supplicant_global_iface_interfaces(
6967 global, reply, reply_size);
6968 } else if (os_strcmp(buf, "TERMINATE") == 0) {
6969 wpa_supplicant_terminate_proc(global);
6970 } else if (os_strcmp(buf, "SUSPEND") == 0) {
6971 wpas_notify_suspend(global);
6972 } else if (os_strcmp(buf, "RESUME") == 0) {
6973 wpas_notify_resume(global);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006974 } else if (os_strncmp(buf, "SET ", 4) == 0) {
6975 if (wpas_global_ctrl_iface_set(global, buf + 4))
6976 reply_len = -1;
6977#ifndef CONFIG_NO_CONFIG_WRITE
6978 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
6979 if (wpas_global_ctrl_iface_save_config(global))
6980 reply_len = -1;
6981#endif /* CONFIG_NO_CONFIG_WRITE */
6982 } else if (os_strcmp(buf, "STATUS") == 0) {
6983 reply_len = wpas_global_ctrl_iface_status(global, reply,
6984 reply_size);
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006985#ifdef CONFIG_MODULE_TESTS
6986 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
6987 int wpas_module_tests(void);
6988 if (wpas_module_tests() < 0)
6989 reply_len = -1;
6990#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006991 } else {
6992 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
6993 reply_len = 16;
6994 }
6995
6996 if (reply_len < 0) {
6997 os_memcpy(reply, "FAIL\n", 5);
6998 reply_len = 5;
6999 }
7000
7001 *resp_len = reply_len;
7002 return reply;
7003}