blob: 53d2d015556f91685a5780237bbfd2fd666cccc8 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Control interface (shared code for all backends)
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2004-2015, 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"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010#ifdef CONFIG_TESTING_OPTIONS
11#include <net/ethernet.h>
12#include <netinet/ip.h>
13#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014
15#include "utils/common.h"
16#include "utils/eloop.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080017#include "utils/uuid.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "common/version.h"
19#include "common/ieee802_11_defs.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070020#include "common/ieee802_11_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "common/wpa_ctrl.h"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080022#include "crypto/tls.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080023#include "ap/hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "eap_peer/eap.h"
25#include "eapol_supp/eapol_supp_sm.h"
26#include "rsn_supp/wpa.h"
27#include "rsn_supp/preauth.h"
28#include "rsn_supp/pmksa_cache.h"
29#include "l2_packet/l2_packet.h"
30#include "wps/wps.h"
31#include "config.h"
32#include "wpa_supplicant_i.h"
33#include "driver_i.h"
34#include "wps_supplicant.h"
35#include "ibss_rsn.h"
36#include "ap.h"
37#include "p2p_supplicant.h"
38#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070039#include "hs20_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070040#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041#include "notify.h"
42#include "bss.h"
43#include "scan.h"
44#include "ctrl_iface.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080045#include "interworking.h"
Dmitry Shmidte19501d2011-03-16 14:32:18 -070046#include "blacklist.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070047#include "autoscan.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080048#include "wnm_sta.h"
Dmitry Shmidt818ea482014-03-10 13:15:21 -070049#include "offchannel.h"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070050#include "drivers/driver.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080051#include "mesh.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053static int wpa_supplicant_global_iface_list(struct wpa_global *global,
54 char *buf, int len);
55static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
56 char *buf, int len);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -070057static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
58 char *val);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059
Dmitry Shmidt04949592012-07-19 12:16:46 -070060static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
61{
62 char *pos;
63 u8 addr[ETH_ALEN], *filter = NULL, *n;
64 size_t count = 0;
65
66 pos = val;
67 while (pos) {
68 if (*pos == '\0')
69 break;
70 if (hwaddr_aton(pos, addr)) {
71 os_free(filter);
72 return -1;
73 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070074 n = os_realloc_array(filter, count + 1, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -070075 if (n == NULL) {
76 os_free(filter);
77 return -1;
78 }
79 filter = n;
80 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
81 count++;
82
83 pos = os_strchr(pos, ' ');
84 if (pos)
85 pos++;
86 }
87
88 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
89 os_free(wpa_s->bssid_filter);
90 wpa_s->bssid_filter = filter;
91 wpa_s->bssid_filter_count = count;
92
93 return 0;
94}
95
96
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080097static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
98{
99 char *pos;
100 u8 addr[ETH_ALEN], *bssid = NULL, *n;
101 struct wpa_ssid_value *ssid = NULL, *ns;
102 size_t count = 0, ssid_count = 0;
103 struct wpa_ssid *c;
104
105 /*
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800106 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800107 * SSID_SPEC ::= ssid <SSID_HEX>
108 * BSSID_SPEC ::= bssid <BSSID_HEX>
109 */
110
111 pos = val;
112 while (pos) {
113 if (*pos == '\0')
114 break;
115 if (os_strncmp(pos, "bssid ", 6) == 0) {
116 int res;
117 pos += 6;
118 res = hwaddr_aton2(pos, addr);
119 if (res < 0) {
120 os_free(ssid);
121 os_free(bssid);
122 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
123 "BSSID value '%s'", pos);
124 return -1;
125 }
126 pos += res;
127 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
128 if (n == NULL) {
129 os_free(ssid);
130 os_free(bssid);
131 return -1;
132 }
133 bssid = n;
134 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
135 count++;
136 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
137 char *end;
138 pos += 5;
139
140 end = pos;
141 while (*end) {
142 if (*end == '\0' || *end == ' ')
143 break;
144 end++;
145 }
146
147 ns = os_realloc_array(ssid, ssid_count + 1,
148 sizeof(struct wpa_ssid_value));
149 if (ns == NULL) {
150 os_free(ssid);
151 os_free(bssid);
152 return -1;
153 }
154 ssid = ns;
155
156 if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
157 hexstr2bin(pos, ssid[ssid_count].ssid,
158 (end - pos) / 2) < 0) {
159 os_free(ssid);
160 os_free(bssid);
161 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
162 "SSID value '%s'", pos);
163 return -1;
164 }
165 ssid[ssid_count].ssid_len = (end - pos) / 2;
166 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
167 ssid[ssid_count].ssid,
168 ssid[ssid_count].ssid_len);
169 ssid_count++;
170 pos = end;
171 } else {
172 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
173 "'%s'", pos);
174 os_free(ssid);
175 os_free(bssid);
176 return -1;
177 }
178
179 pos = os_strchr(pos, ' ');
180 if (pos)
181 pos++;
182 }
183
184 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
185 os_free(wpa_s->disallow_aps_bssid);
186 wpa_s->disallow_aps_bssid = bssid;
187 wpa_s->disallow_aps_bssid_count = count;
188
189 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
190 os_free(wpa_s->disallow_aps_ssid);
191 wpa_s->disallow_aps_ssid = ssid;
192 wpa_s->disallow_aps_ssid_count = ssid_count;
193
194 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
195 return 0;
196
197 c = wpa_s->current_ssid;
198 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
199 return 0;
200
201 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
202 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
203 return 0;
204
205 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
206 "because current AP was marked disallowed");
207
208#ifdef CONFIG_SME
209 wpa_s->sme.prev_bssid_set = 0;
210#endif /* CONFIG_SME */
211 wpa_s->reassociate = 1;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800212 wpa_s->own_disconnect_req = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800213 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
214 wpa_supplicant_req_scan(wpa_s, 0, 0);
215
216 return 0;
217}
218
219
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700220#ifndef CONFIG_NO_CONFIG_BLOBS
221static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
222{
223 char *name = pos;
224 struct wpa_config_blob *blob;
225 size_t len;
226
227 pos = os_strchr(pos, ' ');
228 if (pos == NULL)
229 return -1;
230 *pos++ = '\0';
231 len = os_strlen(pos);
232 if (len & 1)
233 return -1;
234
235 wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
236 blob = os_zalloc(sizeof(*blob));
237 if (blob == NULL)
238 return -1;
239 blob->name = os_strdup(name);
240 blob->data = os_malloc(len / 2);
241 if (blob->name == NULL || blob->data == NULL) {
242 wpa_config_free_blob(blob);
243 return -1;
244 }
245
246 if (hexstr2bin(pos, blob->data, len / 2) < 0) {
247 wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
248 wpa_config_free_blob(blob);
249 return -1;
250 }
251 blob->len = len / 2;
252
253 wpa_config_set_blob(wpa_s->conf, blob);
254
255 return 0;
256}
257#endif /* CONFIG_NO_CONFIG_BLOBS */
258
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700259
260static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
261{
262 char *params;
263 char *pos;
264 int *freqs = NULL;
265 int ret;
266
267 if (atoi(cmd)) {
268 params = os_strchr(cmd, ' ');
269 os_free(wpa_s->manual_sched_scan_freqs);
270 if (params) {
271 params++;
272 pos = os_strstr(params, "freq=");
273 if (pos)
274 freqs = freq_range_to_channel_list(wpa_s,
275 pos + 5);
276 }
277 wpa_s->manual_sched_scan_freqs = freqs;
278 ret = wpas_start_pno(wpa_s);
279 } else {
280 ret = wpas_stop_pno(wpa_s);
281 }
282 return ret;
283}
284
285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
287 char *cmd)
288{
289 char *value;
290 int ret = 0;
291
292 value = os_strchr(cmd, ' ');
293 if (value == NULL)
294 return -1;
295 *value++ = '\0';
296
297 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
298 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
299 eapol_sm_configure(wpa_s->eapol,
300 atoi(value), -1, -1, -1);
301 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
302 eapol_sm_configure(wpa_s->eapol,
303 -1, atoi(value), -1, -1);
304 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
305 eapol_sm_configure(wpa_s->eapol,
306 -1, -1, atoi(value), -1);
307 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
308 eapol_sm_configure(wpa_s->eapol,
309 -1, -1, -1, atoi(value));
310 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
311 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
312 atoi(value)))
313 ret = -1;
314 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
315 0) {
316 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
317 atoi(value)))
318 ret = -1;
319 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
320 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
321 ret = -1;
322 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
323 wpa_s->wps_fragment_size = atoi(value);
324#ifdef CONFIG_WPS_TESTING
325 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
326 long int val;
327 val = strtol(value, NULL, 0);
328 if (val < 0 || val > 0xff) {
329 ret = -1;
330 wpa_printf(MSG_DEBUG, "WPS: Invalid "
331 "wps_version_number %ld", val);
332 } else {
333 wps_version_number = val;
334 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
335 "version %u.%u",
336 (wps_version_number & 0xf0) >> 4,
337 wps_version_number & 0x0f);
338 }
339 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
340 wps_testing_dummy_cred = atoi(value);
341 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
342 wps_testing_dummy_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800343 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
344 wps_corrupt_pkhash = atoi(value);
345 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
346 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347#endif /* CONFIG_WPS_TESTING */
348 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
349 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
350 ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800351#ifdef CONFIG_TDLS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352#ifdef CONFIG_TDLS_TESTING
353 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
354 extern unsigned int tdls_testing;
355 tdls_testing = strtol(value, NULL, 0);
356 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
357#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
359 int disabled = atoi(value);
360 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
361 if (disabled) {
362 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
363 ret = -1;
364 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
365 ret = -1;
366 wpa_tdls_enable(wpa_s->wpa, !disabled);
367#endif /* CONFIG_TDLS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800368 } else if (os_strcasecmp(cmd, "pno") == 0) {
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700369 ret = wpas_ctrl_pno(wpa_s, value);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700370 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
371 int disabled = atoi(value);
372 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
373 ret = -1;
374 else if (disabled)
375 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
376 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
377 if (os_strcmp(value, "disable") == 0)
378 wpa_s->set_sta_uapsd = 0;
379 else {
380 int be, bk, vi, vo;
381 char *pos;
382 /* format: BE,BK,VI,VO;max SP Length */
383 be = atoi(value);
384 pos = os_strchr(value, ',');
385 if (pos == NULL)
386 return -1;
387 pos++;
388 bk = atoi(pos);
389 pos = os_strchr(pos, ',');
390 if (pos == NULL)
391 return -1;
392 pos++;
393 vi = atoi(pos);
394 pos = os_strchr(pos, ',');
395 if (pos == NULL)
396 return -1;
397 pos++;
398 vo = atoi(pos);
399 /* ignore max SP Length for now */
400
401 wpa_s->set_sta_uapsd = 1;
402 wpa_s->sta_uapsd = 0;
403 if (be)
404 wpa_s->sta_uapsd |= BIT(0);
405 if (bk)
406 wpa_s->sta_uapsd |= BIT(1);
407 if (vi)
408 wpa_s->sta_uapsd |= BIT(2);
409 if (vo)
410 wpa_s->sta_uapsd |= BIT(3);
411 }
Jouni Malinen21d6bc82012-04-10 16:17:59 -0700412 } else if (os_strcasecmp(cmd, "ps") == 0) {
413 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700414#ifdef CONFIG_WIFI_DISPLAY
415 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
Dmitry Shmidted003d22014-02-06 10:09:12 -0800416 int enabled = !!atoi(value);
417 if (enabled && !wpa_s->global->p2p)
418 ret = -1;
419 else
420 wifi_display_enable(wpa_s->global, enabled);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700421#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700422 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
423 ret = set_bssid_filter(wpa_s, value);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800424 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
425 ret = set_disallow_aps(wpa_s, value);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800426 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
427 wpa_s->no_keep_alive = !!atoi(value);
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700428#ifdef CONFIG_TESTING_OPTIONS
429 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
430 wpa_s->ext_mgmt_frame_handling = !!atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800431 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
432 wpa_s->ext_eapol_frame_io = !!atoi(value);
433#ifdef CONFIG_AP
434 if (wpa_s->ap_iface) {
435 wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
436 wpa_s->ext_eapol_frame_io;
437 }
438#endif /* CONFIG_AP */
439 } else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
440 wpa_s->extra_roc_dur = atoi(value);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800441 } else if (os_strcasecmp(cmd, "test_failure") == 0) {
442 wpa_s->test_failure = atoi(value);
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700443#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700444#ifndef CONFIG_NO_CONFIG_BLOBS
445 } else if (os_strcmp(cmd, "blob") == 0) {
446 ret = wpas_ctrl_set_blob(wpa_s, value);
447#endif /* CONFIG_NO_CONFIG_BLOBS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800448 } else if (os_strcasecmp(cmd, "setband") == 0) {
449 if (os_strcmp(value, "AUTO") == 0)
450 wpa_s->setband = WPA_SETBAND_AUTO;
451 else if (os_strcmp(value, "5G") == 0)
452 wpa_s->setband = WPA_SETBAND_5G;
453 else if (os_strcmp(value, "2G") == 0)
454 wpa_s->setband = WPA_SETBAND_2G;
455 else
456 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 } else {
458 value[-1] = '=';
459 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
460 if (ret == 0)
461 wpa_supplicant_update_config(wpa_s);
462 }
463
464 return ret;
465}
466
467
468static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
469 char *cmd, char *buf, size_t buflen)
470{
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700471 int res = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700472
473 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
474
475 if (os_strcmp(cmd, "version") == 0) {
476 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700477 } else if (os_strcasecmp(cmd, "country") == 0) {
478 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
479 res = os_snprintf(buf, buflen, "%c%c",
480 wpa_s->conf->country[0],
481 wpa_s->conf->country[1]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700482#ifdef CONFIG_WIFI_DISPLAY
483 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
Dmitry Shmidted003d22014-02-06 10:09:12 -0800484 int enabled;
485 if (wpa_s->global->p2p == NULL ||
486 wpa_s->global->p2p_disabled)
487 enabled = 0;
488 else
489 enabled = wpa_s->global->wifi_display;
490 res = os_snprintf(buf, buflen, "%d", enabled);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700491#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700492#ifdef CONFIG_TESTING_GET_GTK
493 } else if (os_strcmp(cmd, "gtk") == 0) {
494 if (wpa_s->last_gtk_len == 0)
495 return -1;
496 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
497 wpa_s->last_gtk_len);
498 return res;
499#endif /* CONFIG_TESTING_GET_GTK */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800500 } else if (os_strcmp(cmd, "tls_library") == 0) {
501 res = tls_get_library_version(buf, buflen);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800502 } else {
503 res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700504 }
505
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800506 if (os_snprintf_error(buflen, res))
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700507 return -1;
508 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509}
510
511
512#ifdef IEEE8021X_EAPOL
513static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
514 char *addr)
515{
516 u8 bssid[ETH_ALEN];
517 struct wpa_ssid *ssid = wpa_s->current_ssid;
518
519 if (hwaddr_aton(addr, bssid)) {
520 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
521 "'%s'", addr);
522 return -1;
523 }
524
525 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
526 rsn_preauth_deinit(wpa_s->wpa);
527 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
528 return -1;
529
530 return 0;
531}
532#endif /* IEEE8021X_EAPOL */
533
534
535#ifdef CONFIG_PEERKEY
536/* MLME-STKSTART.request(peer) */
537static int wpa_supplicant_ctrl_iface_stkstart(
538 struct wpa_supplicant *wpa_s, char *addr)
539{
540 u8 peer[ETH_ALEN];
541
542 if (hwaddr_aton(addr, peer)) {
543 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
544 "address '%s'", addr);
545 return -1;
546 }
547
548 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
549 MAC2STR(peer));
550
551 return wpa_sm_stkstart(wpa_s->wpa, peer);
552}
553#endif /* CONFIG_PEERKEY */
554
555
556#ifdef CONFIG_TDLS
557
558static int wpa_supplicant_ctrl_iface_tdls_discover(
559 struct wpa_supplicant *wpa_s, char *addr)
560{
561 u8 peer[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800562 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563
564 if (hwaddr_aton(addr, peer)) {
565 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
566 "address '%s'", addr);
567 return -1;
568 }
569
570 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
571 MAC2STR(peer));
572
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800573 if (wpa_tdls_is_external_setup(wpa_s->wpa))
574 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
575 else
576 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
577
578 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700579}
580
581
582static int wpa_supplicant_ctrl_iface_tdls_setup(
583 struct wpa_supplicant *wpa_s, char *addr)
584{
585 u8 peer[ETH_ALEN];
586 int ret;
587
588 if (hwaddr_aton(addr, peer)) {
589 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
590 "address '%s'", addr);
591 return -1;
592 }
593
594 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
595 MAC2STR(peer));
596
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800597 if ((wpa_s->conf->tdls_external_control) &&
598 wpa_tdls_is_external_setup(wpa_s->wpa))
599 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
600
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800601 wpa_tdls_remove(wpa_s->wpa, peer);
602
603 if (wpa_tdls_is_external_setup(wpa_s->wpa))
604 ret = wpa_tdls_start(wpa_s->wpa, peer);
605 else
606 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608 return ret;
609}
610
611
612static int wpa_supplicant_ctrl_iface_tdls_teardown(
613 struct wpa_supplicant *wpa_s, char *addr)
614{
615 u8 peer[ETH_ALEN];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700616 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700618 if (os_strcmp(addr, "*") == 0) {
619 /* remove everyone */
620 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
621 wpa_tdls_teardown_peers(wpa_s->wpa);
622 return 0;
623 }
624
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700625 if (hwaddr_aton(addr, peer)) {
626 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
627 "address '%s'", addr);
628 return -1;
629 }
630
631 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
632 MAC2STR(peer));
633
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800634 if ((wpa_s->conf->tdls_external_control) &&
635 wpa_tdls_is_external_setup(wpa_s->wpa))
636 return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
637
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700638 if (wpa_tdls_is_external_setup(wpa_s->wpa))
639 ret = wpa_tdls_teardown_link(
640 wpa_s->wpa, peer,
641 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
642 else
643 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
644
645 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700646}
647
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700648
649static int ctrl_iface_get_capability_tdls(
650 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
651{
652 int ret;
653
654 ret = os_snprintf(buf, buflen, "%s\n",
655 wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
656 (wpa_s->drv_flags &
657 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
658 "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800659 if (os_snprintf_error(buflen, ret))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700660 return -1;
661 return ret;
662}
663
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800664
665static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
666 struct wpa_supplicant *wpa_s, char *cmd)
667{
668 u8 peer[ETH_ALEN];
669 struct hostapd_freq_params freq_params;
670 u8 oper_class;
671 char *pos, *end;
672
673 if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
674 wpa_printf(MSG_INFO,
675 "tdls_chanswitch: Only supported with external setup");
676 return -1;
677 }
678
679 os_memset(&freq_params, 0, sizeof(freq_params));
680
681 pos = os_strchr(cmd, ' ');
682 if (pos == NULL)
683 return -1;
684 *pos++ = '\0';
685
686 oper_class = strtol(pos, &end, 10);
687 if (pos == end) {
688 wpa_printf(MSG_INFO,
689 "tdls_chanswitch: Invalid op class provided");
690 return -1;
691 }
692
693 pos = end;
694 freq_params.freq = atoi(pos);
695 if (freq_params.freq == 0) {
696 wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
697 return -1;
698 }
699
700#define SET_FREQ_SETTING(str) \
701 do { \
702 const char *pos2 = os_strstr(pos, " " #str "="); \
703 if (pos2) { \
704 pos2 += sizeof(" " #str "=") - 1; \
705 freq_params.str = atoi(pos2); \
706 } \
707 } while (0)
708
709 SET_FREQ_SETTING(center_freq1);
710 SET_FREQ_SETTING(center_freq2);
711 SET_FREQ_SETTING(bandwidth);
712 SET_FREQ_SETTING(sec_channel_offset);
713#undef SET_FREQ_SETTING
714
715 freq_params.ht_enabled = !!os_strstr(pos, " ht");
716 freq_params.vht_enabled = !!os_strstr(pos, " vht");
717
718 if (hwaddr_aton(cmd, peer)) {
719 wpa_printf(MSG_DEBUG,
720 "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
721 cmd);
722 return -1;
723 }
724
725 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
726 " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
727 MAC2STR(peer), oper_class, freq_params.freq,
728 freq_params.center_freq1, freq_params.center_freq2,
729 freq_params.bandwidth, freq_params.sec_channel_offset,
730 freq_params.ht_enabled ? " HT" : "",
731 freq_params.vht_enabled ? " VHT" : "");
732
733 return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
734 &freq_params);
735}
736
737
738static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
739 struct wpa_supplicant *wpa_s, char *cmd)
740{
741 u8 peer[ETH_ALEN];
742
743 if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
744 wpa_printf(MSG_INFO,
745 "tdls_chanswitch: Only supported with external setup");
746 return -1;
747 }
748
749 if (hwaddr_aton(cmd, peer)) {
750 wpa_printf(MSG_DEBUG,
751 "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
752 cmd);
753 return -1;
754 }
755
756 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
757 MAC2STR(peer));
758
759 return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
760}
761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762#endif /* CONFIG_TDLS */
763
764
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800765static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
766{
767 char *token, *context = NULL;
768 struct wmm_ac_ts_setup_params params = {
769 .tsid = 0xff,
770 .direction = 0xff,
771 };
772
773 while ((token = str_token(cmd, " ", &context))) {
774 if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
775 sscanf(token, "up=%i", &params.user_priority) == 1 ||
776 sscanf(token, "nominal_msdu_size=%i",
777 &params.nominal_msdu_size) == 1 ||
778 sscanf(token, "mean_data_rate=%i",
779 &params.mean_data_rate) == 1 ||
780 sscanf(token, "min_phy_rate=%i",
781 &params.minimum_phy_rate) == 1 ||
782 sscanf(token, "sba=%i",
783 &params.surplus_bandwidth_allowance) == 1)
784 continue;
785
786 if (os_strcasecmp(token, "downlink") == 0) {
787 params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
788 } else if (os_strcasecmp(token, "uplink") == 0) {
789 params.direction = WMM_TSPEC_DIRECTION_UPLINK;
790 } else if (os_strcasecmp(token, "bidi") == 0) {
791 params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
792 } else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
793 params.fixed_nominal_msdu = 1;
794 } else {
795 wpa_printf(MSG_DEBUG,
796 "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
797 token);
798 return -1;
799 }
800
801 }
802
803 return wpas_wmm_ac_addts(wpa_s, &params);
804}
805
806
807static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
808{
809 u8 tsid = atoi(cmd);
810
811 return wpas_wmm_ac_delts(wpa_s, tsid);
812}
813
814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815#ifdef CONFIG_IEEE80211R
816static int wpa_supplicant_ctrl_iface_ft_ds(
817 struct wpa_supplicant *wpa_s, char *addr)
818{
819 u8 target_ap[ETH_ALEN];
820 struct wpa_bss *bss;
821 const u8 *mdie;
822
823 if (hwaddr_aton(addr, target_ap)) {
824 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
825 "address '%s'", addr);
826 return -1;
827 }
828
829 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
830
831 bss = wpa_bss_get_bssid(wpa_s, target_ap);
832 if (bss)
833 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
834 else
835 mdie = NULL;
836
837 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
838}
839#endif /* CONFIG_IEEE80211R */
840
841
842#ifdef CONFIG_WPS
843static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
844 char *cmd)
845{
846 u8 bssid[ETH_ALEN], *_bssid = bssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700847#ifdef CONFIG_P2P
848 u8 p2p_dev_addr[ETH_ALEN];
849#endif /* CONFIG_P2P */
850#ifdef CONFIG_AP
851 u8 *_p2p_dev_addr = NULL;
852#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800853
Dmitry Shmidtc0a8db02013-11-08 11:18:33 -0800854 if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855 _bssid = NULL;
856#ifdef CONFIG_P2P
857 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
858 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
859 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
860 "P2P Device Address '%s'",
861 cmd + 13);
862 return -1;
863 }
864 _p2p_dev_addr = p2p_dev_addr;
865#endif /* CONFIG_P2P */
866 } else if (hwaddr_aton(cmd, bssid)) {
867 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
868 cmd);
869 return -1;
870 }
871
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800872#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700873 if (wpa_s->ap_iface)
874 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
875#endif /* CONFIG_AP */
876
877 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
878}
879
880
881static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
882 char *cmd, char *buf,
883 size_t buflen)
884{
885 u8 bssid[ETH_ALEN], *_bssid = bssid;
886 char *pin;
887 int ret;
888
889 pin = os_strchr(cmd, ' ');
890 if (pin)
891 *pin++ = '\0';
892
893 if (os_strcmp(cmd, "any") == 0)
894 _bssid = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800895 else if (os_strcmp(cmd, "get") == 0) {
896 ret = wps_generate_pin();
897 goto done;
898 } else if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
900 cmd);
901 return -1;
902 }
903
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800904#ifdef CONFIG_AP
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800905 if (wpa_s->ap_iface) {
906 int timeout = 0;
907 char *pos;
908
909 if (pin) {
910 pos = os_strchr(pin, ' ');
911 if (pos) {
912 *pos++ = '\0';
913 timeout = atoi(pos);
914 }
915 }
916
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800918 buf, buflen, timeout);
919 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920#endif /* CONFIG_AP */
921
922 if (pin) {
923 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
924 DEV_PW_DEFAULT);
925 if (ret < 0)
926 return -1;
927 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800928 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 return -1;
930 return ret;
931 }
932
933 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
934 if (ret < 0)
935 return -1;
936
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800937done:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938 /* Return the generated PIN */
939 ret = os_snprintf(buf, buflen, "%08d", ret);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800940 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 return -1;
942 return ret;
943}
944
945
946static int wpa_supplicant_ctrl_iface_wps_check_pin(
947 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
948{
949 char pin[9];
950 size_t len;
951 char *pos;
952 int ret;
953
954 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
955 (u8 *) cmd, os_strlen(cmd));
956 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
957 if (*pos < '0' || *pos > '9')
958 continue;
959 pin[len++] = *pos;
960 if (len == 9) {
961 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
962 return -1;
963 }
964 }
965 if (len != 4 && len != 8) {
966 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
967 return -1;
968 }
969 pin[len] = '\0';
970
971 if (len == 8) {
972 unsigned int pin_val;
973 pin_val = atoi(pin);
974 if (!wps_pin_valid(pin_val)) {
975 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
976 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800977 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978 return -1;
979 return ret;
980 }
981 }
982
983 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800984 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 return -1;
986
987 return ret;
988}
989
990
Dmitry Shmidt04949592012-07-19 12:16:46 -0700991#ifdef CONFIG_WPS_NFC
992
993static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
994 char *cmd)
995{
996 u8 bssid[ETH_ALEN], *_bssid = bssid;
997
998 if (cmd == NULL || cmd[0] == '\0')
999 _bssid = NULL;
1000 else if (hwaddr_aton(cmd, bssid))
1001 return -1;
1002
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001003 return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
1004 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001005}
1006
1007
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001008static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
1009 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1010{
1011 int ndef;
1012 struct wpabuf *buf;
1013 int res;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001014 char *pos;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001015
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001016 pos = os_strchr(cmd, ' ');
1017 if (pos)
1018 *pos++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001019 if (os_strcmp(cmd, "WPS") == 0)
1020 ndef = 0;
1021 else if (os_strcmp(cmd, "NDEF") == 0)
1022 ndef = 1;
1023 else
1024 return -1;
1025
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001026 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001027 if (buf == NULL)
1028 return -1;
1029
1030 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1031 wpabuf_len(buf));
1032 reply[res++] = '\n';
1033 reply[res] = '\0';
1034
1035 wpabuf_free(buf);
1036
1037 return res;
1038}
1039
1040
Dmitry Shmidt04949592012-07-19 12:16:46 -07001041static int wpa_supplicant_ctrl_iface_wps_nfc_token(
1042 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1043{
1044 int ndef;
1045 struct wpabuf *buf;
1046 int res;
1047
1048 if (os_strcmp(cmd, "WPS") == 0)
1049 ndef = 0;
1050 else if (os_strcmp(cmd, "NDEF") == 0)
1051 ndef = 1;
1052 else
1053 return -1;
1054
1055 buf = wpas_wps_nfc_token(wpa_s, ndef);
1056 if (buf == NULL)
1057 return -1;
1058
1059 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1060 wpabuf_len(buf));
1061 reply[res++] = '\n';
1062 reply[res] = '\0';
1063
1064 wpabuf_free(buf);
1065
1066 return res;
1067}
1068
1069
1070static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
1071 struct wpa_supplicant *wpa_s, char *pos)
1072{
1073 size_t len;
1074 struct wpabuf *buf;
1075 int ret;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001076 char *freq;
1077 int forced_freq = 0;
1078
1079 freq = strstr(pos, " freq=");
1080 if (freq) {
1081 *freq = '\0';
1082 freq += 6;
1083 forced_freq = atoi(freq);
1084 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001085
1086 len = os_strlen(pos);
1087 if (len & 0x01)
1088 return -1;
1089 len /= 2;
1090
1091 buf = wpabuf_alloc(len);
1092 if (buf == NULL)
1093 return -1;
1094 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
1095 wpabuf_free(buf);
1096 return -1;
1097 }
1098
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001099 ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001100 wpabuf_free(buf);
1101
1102 return ret;
1103}
1104
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001105
1106static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001107 char *reply, size_t max_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001108 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001109{
1110 struct wpabuf *buf;
1111 int res;
1112
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001113 buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001114 if (buf == NULL)
1115 return -1;
1116
1117 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1118 wpabuf_len(buf));
1119 reply[res++] = '\n';
1120 reply[res] = '\0';
1121
1122 wpabuf_free(buf);
1123
1124 return res;
1125}
1126
1127
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001128#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001129static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
1130 char *reply, size_t max_len,
1131 int ndef)
1132{
1133 struct wpabuf *buf;
1134 int res;
1135
1136 buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
1137 if (buf == NULL) {
1138 wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
1139 return -1;
1140 }
1141
1142 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1143 wpabuf_len(buf));
1144 reply[res++] = '\n';
1145 reply[res] = '\0';
1146
1147 wpabuf_free(buf);
1148
1149 return res;
1150}
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001151#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001152
1153
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001154static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
1155 char *cmd, char *reply,
1156 size_t max_len)
1157{
1158 char *pos;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001159 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001160
1161 pos = os_strchr(cmd, ' ');
1162 if (pos == NULL)
1163 return -1;
1164 *pos++ = '\0';
1165
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001166 if (os_strcmp(cmd, "WPS") == 0)
1167 ndef = 0;
1168 else if (os_strcmp(cmd, "NDEF") == 0)
1169 ndef = 1;
1170 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001171 return -1;
1172
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001173 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001174 if (!ndef)
1175 return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001176 return wpas_ctrl_nfc_get_handover_req_wps(
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001177 wpa_s, reply, max_len, ndef);
1178 }
1179
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001180#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001181 if (os_strcmp(pos, "P2P-CR") == 0) {
1182 return wpas_ctrl_nfc_get_handover_req_p2p(
1183 wpa_s, reply, max_len, ndef);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001184 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001185#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001186
1187 return -1;
1188}
1189
1190
1191static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001192 char *reply, size_t max_len,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001193 int ndef, int cr, char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001194{
1195 struct wpabuf *buf;
1196 int res;
1197
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001198 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001199 if (buf == NULL)
1200 return -1;
1201
1202 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1203 wpabuf_len(buf));
1204 reply[res++] = '\n';
1205 reply[res] = '\0';
1206
1207 wpabuf_free(buf);
1208
1209 return res;
1210}
1211
1212
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001213#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001214static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1215 char *reply, size_t max_len,
1216 int ndef, int tag)
1217{
1218 struct wpabuf *buf;
1219 int res;
1220
1221 buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1222 if (buf == NULL)
1223 return -1;
1224
1225 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1226 wpabuf_len(buf));
1227 reply[res++] = '\n';
1228 reply[res] = '\0';
1229
1230 wpabuf_free(buf);
1231
1232 return res;
1233}
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001234#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001235
1236
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001237static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1238 char *cmd, char *reply,
1239 size_t max_len)
1240{
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001241 char *pos, *pos2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001242 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001243
1244 pos = os_strchr(cmd, ' ');
1245 if (pos == NULL)
1246 return -1;
1247 *pos++ = '\0';
1248
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001249 if (os_strcmp(cmd, "WPS") == 0)
1250 ndef = 0;
1251 else if (os_strcmp(cmd, "NDEF") == 0)
1252 ndef = 1;
1253 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001254 return -1;
1255
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001256 pos2 = os_strchr(pos, ' ');
1257 if (pos2)
1258 *pos2++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001259 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001260 if (!ndef)
1261 return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001262 return wpas_ctrl_nfc_get_handover_sel_wps(
1263 wpa_s, reply, max_len, ndef,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001264 os_strcmp(pos, "WPS-CR") == 0, pos2);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001265 }
1266
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001267#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001268 if (os_strcmp(pos, "P2P-CR") == 0) {
1269 return wpas_ctrl_nfc_get_handover_sel_p2p(
1270 wpa_s, reply, max_len, ndef, 0);
1271 }
1272
1273 if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1274 return wpas_ctrl_nfc_get_handover_sel_p2p(
1275 wpa_s, reply, max_len, ndef, 1);
1276 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001277#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001278
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001279 return -1;
1280}
1281
1282
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001283static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1284 char *cmd)
1285{
1286 size_t len;
1287 struct wpabuf *req, *sel;
1288 int ret;
1289 char *pos, *role, *type, *pos2;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001290#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001291 char *freq;
1292 int forced_freq = 0;
1293
1294 freq = strstr(cmd, " freq=");
1295 if (freq) {
1296 *freq = '\0';
1297 freq += 6;
1298 forced_freq = atoi(freq);
1299 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001300#endif /* CONFIG_P2P */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001301
1302 role = cmd;
1303 pos = os_strchr(role, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001304 if (pos == NULL) {
1305 wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001306 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001307 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001308 *pos++ = '\0';
1309
1310 type = pos;
1311 pos = os_strchr(type, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001312 if (pos == NULL) {
1313 wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001314 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001315 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001316 *pos++ = '\0';
1317
1318 pos2 = os_strchr(pos, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001319 if (pos2 == NULL) {
1320 wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001321 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001322 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001323 *pos2++ = '\0';
1324
1325 len = os_strlen(pos);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001326 if (len & 0x01) {
1327 wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001328 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001329 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001330 len /= 2;
1331
1332 req = wpabuf_alloc(len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001333 if (req == NULL) {
1334 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001335 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001336 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001337 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001338 wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001339 wpabuf_free(req);
1340 return -1;
1341 }
1342
1343 len = os_strlen(pos2);
1344 if (len & 0x01) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001345 wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001346 wpabuf_free(req);
1347 return -1;
1348 }
1349 len /= 2;
1350
1351 sel = wpabuf_alloc(len);
1352 if (sel == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001353 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001354 wpabuf_free(req);
1355 return -1;
1356 }
1357 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001358 wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001359 wpabuf_free(req);
1360 wpabuf_free(sel);
1361 return -1;
1362 }
1363
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001364 wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1365 role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1366
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001367 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1368 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001369#ifdef CONFIG_AP
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001370 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1371 {
1372 ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1373 if (ret < 0)
1374 ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001375#endif /* CONFIG_AP */
1376#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001377 } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1378 {
1379 ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
1380 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1381 {
1382 ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1383 forced_freq);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001384#endif /* CONFIG_P2P */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001385 } else {
1386 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1387 "reported: role=%s type=%s", role, type);
1388 ret = -1;
1389 }
1390 wpabuf_free(req);
1391 wpabuf_free(sel);
1392
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001393 if (ret)
1394 wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1395
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001396 return ret;
1397}
1398
Dmitry Shmidt04949592012-07-19 12:16:46 -07001399#endif /* CONFIG_WPS_NFC */
1400
1401
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1403 char *cmd)
1404{
1405 u8 bssid[ETH_ALEN];
1406 char *pin;
1407 char *new_ssid;
1408 char *new_auth;
1409 char *new_encr;
1410 char *new_key;
1411 struct wps_new_ap_settings ap;
1412
1413 pin = os_strchr(cmd, ' ');
1414 if (pin == NULL)
1415 return -1;
1416 *pin++ = '\0';
1417
1418 if (hwaddr_aton(cmd, bssid)) {
1419 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1420 cmd);
1421 return -1;
1422 }
1423
1424 new_ssid = os_strchr(pin, ' ');
1425 if (new_ssid == NULL)
1426 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1427 *new_ssid++ = '\0';
1428
1429 new_auth = os_strchr(new_ssid, ' ');
1430 if (new_auth == NULL)
1431 return -1;
1432 *new_auth++ = '\0';
1433
1434 new_encr = os_strchr(new_auth, ' ');
1435 if (new_encr == NULL)
1436 return -1;
1437 *new_encr++ = '\0';
1438
1439 new_key = os_strchr(new_encr, ' ');
1440 if (new_key == NULL)
1441 return -1;
1442 *new_key++ = '\0';
1443
1444 os_memset(&ap, 0, sizeof(ap));
1445 ap.ssid_hex = new_ssid;
1446 ap.auth = new_auth;
1447 ap.encr = new_encr;
1448 ap.key_hex = new_key;
1449 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1450}
1451
1452
1453#ifdef CONFIG_AP
1454static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1455 char *cmd, char *buf,
1456 size_t buflen)
1457{
1458 int timeout = 300;
1459 char *pos;
1460 const char *pin_txt;
1461
1462 if (!wpa_s->ap_iface)
1463 return -1;
1464
1465 pos = os_strchr(cmd, ' ');
1466 if (pos)
1467 *pos++ = '\0';
1468
1469 if (os_strcmp(cmd, "disable") == 0) {
1470 wpas_wps_ap_pin_disable(wpa_s);
1471 return os_snprintf(buf, buflen, "OK\n");
1472 }
1473
1474 if (os_strcmp(cmd, "random") == 0) {
1475 if (pos)
1476 timeout = atoi(pos);
1477 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1478 if (pin_txt == NULL)
1479 return -1;
1480 return os_snprintf(buf, buflen, "%s", pin_txt);
1481 }
1482
1483 if (os_strcmp(cmd, "get") == 0) {
1484 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1485 if (pin_txt == NULL)
1486 return -1;
1487 return os_snprintf(buf, buflen, "%s", pin_txt);
1488 }
1489
1490 if (os_strcmp(cmd, "set") == 0) {
1491 char *pin;
1492 if (pos == NULL)
1493 return -1;
1494 pin = pos;
1495 pos = os_strchr(pos, ' ');
1496 if (pos) {
1497 *pos++ = '\0';
1498 timeout = atoi(pos);
1499 }
1500 if (os_strlen(pin) > buflen)
1501 return -1;
1502 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1503 return -1;
1504 return os_snprintf(buf, buflen, "%s", pin);
1505 }
1506
1507 return -1;
1508}
1509#endif /* CONFIG_AP */
1510
1511
1512#ifdef CONFIG_WPS_ER
1513static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1514 char *cmd)
1515{
1516 char *uuid = cmd, *pin, *pos;
1517 u8 addr_buf[ETH_ALEN], *addr = NULL;
1518 pin = os_strchr(uuid, ' ');
1519 if (pin == NULL)
1520 return -1;
1521 *pin++ = '\0';
1522 pos = os_strchr(pin, ' ');
1523 if (pos) {
1524 *pos++ = '\0';
1525 if (hwaddr_aton(pos, addr_buf) == 0)
1526 addr = addr_buf;
1527 }
1528 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1529}
1530
1531
1532static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1533 char *cmd)
1534{
1535 char *uuid = cmd, *pin;
1536 pin = os_strchr(uuid, ' ');
1537 if (pin == NULL)
1538 return -1;
1539 *pin++ = '\0';
1540 return wpas_wps_er_learn(wpa_s, uuid, pin);
1541}
1542
1543
1544static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1545 struct wpa_supplicant *wpa_s, char *cmd)
1546{
1547 char *uuid = cmd, *id;
1548 id = os_strchr(uuid, ' ');
1549 if (id == NULL)
1550 return -1;
1551 *id++ = '\0';
1552 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1553}
1554
1555
1556static int wpa_supplicant_ctrl_iface_wps_er_config(
1557 struct wpa_supplicant *wpa_s, char *cmd)
1558{
1559 char *pin;
1560 char *new_ssid;
1561 char *new_auth;
1562 char *new_encr;
1563 char *new_key;
1564 struct wps_new_ap_settings ap;
1565
1566 pin = os_strchr(cmd, ' ');
1567 if (pin == NULL)
1568 return -1;
1569 *pin++ = '\0';
1570
1571 new_ssid = os_strchr(pin, ' ');
1572 if (new_ssid == NULL)
1573 return -1;
1574 *new_ssid++ = '\0';
1575
1576 new_auth = os_strchr(new_ssid, ' ');
1577 if (new_auth == NULL)
1578 return -1;
1579 *new_auth++ = '\0';
1580
1581 new_encr = os_strchr(new_auth, ' ');
1582 if (new_encr == NULL)
1583 return -1;
1584 *new_encr++ = '\0';
1585
1586 new_key = os_strchr(new_encr, ' ');
1587 if (new_key == NULL)
1588 return -1;
1589 *new_key++ = '\0';
1590
1591 os_memset(&ap, 0, sizeof(ap));
1592 ap.ssid_hex = new_ssid;
1593 ap.auth = new_auth;
1594 ap.encr = new_encr;
1595 ap.key_hex = new_key;
1596 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1597}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001598
1599
1600#ifdef CONFIG_WPS_NFC
1601static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1602 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1603{
1604 int ndef;
1605 struct wpabuf *buf;
1606 int res;
1607 char *uuid;
1608
1609 uuid = os_strchr(cmd, ' ');
1610 if (uuid == NULL)
1611 return -1;
1612 *uuid++ = '\0';
1613
1614 if (os_strcmp(cmd, "WPS") == 0)
1615 ndef = 0;
1616 else if (os_strcmp(cmd, "NDEF") == 0)
1617 ndef = 1;
1618 else
1619 return -1;
1620
1621 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1622 if (buf == NULL)
1623 return -1;
1624
1625 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1626 wpabuf_len(buf));
1627 reply[res++] = '\n';
1628 reply[res] = '\0';
1629
1630 wpabuf_free(buf);
1631
1632 return res;
1633}
1634#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635#endif /* CONFIG_WPS_ER */
1636
1637#endif /* CONFIG_WPS */
1638
1639
1640#ifdef CONFIG_IBSS_RSN
1641static int wpa_supplicant_ctrl_iface_ibss_rsn(
1642 struct wpa_supplicant *wpa_s, char *addr)
1643{
1644 u8 peer[ETH_ALEN];
1645
1646 if (hwaddr_aton(addr, peer)) {
1647 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1648 "address '%s'", addr);
1649 return -1;
1650 }
1651
1652 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1653 MAC2STR(peer));
1654
1655 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1656}
1657#endif /* CONFIG_IBSS_RSN */
1658
1659
1660static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1661 char *rsp)
1662{
1663#ifdef IEEE8021X_EAPOL
1664 char *pos, *id_pos;
1665 int id;
1666 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667
1668 pos = os_strchr(rsp, '-');
1669 if (pos == NULL)
1670 return -1;
1671 *pos++ = '\0';
1672 id_pos = pos;
1673 pos = os_strchr(pos, ':');
1674 if (pos == NULL)
1675 return -1;
1676 *pos++ = '\0';
1677 id = atoi(id_pos);
1678 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1679 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1680 (u8 *) pos, os_strlen(pos));
1681
1682 ssid = wpa_config_get_network(wpa_s->conf, id);
1683 if (ssid == NULL) {
1684 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1685 "to update", id);
1686 return -1;
1687 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001689 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1690 pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691#else /* IEEE8021X_EAPOL */
1692 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1693 return -1;
1694#endif /* IEEE8021X_EAPOL */
1695}
1696
1697
1698static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1699 const char *params,
1700 char *buf, size_t buflen)
1701{
1702 char *pos, *end, tmp[30];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001703 int res, verbose, wps, ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001704#ifdef CONFIG_HS20
1705 const u8 *hs20;
1706#endif /* CONFIG_HS20 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001707 const u8 *sess_id;
1708 size_t sess_id_len;
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001709
Dmitry Shmidt56052862013-10-04 10:23:25 -07001710 if (os_strcmp(params, "-DRIVER") == 0)
1711 return wpa_drv_status(wpa_s, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712 verbose = os_strcmp(params, "-VERBOSE") == 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001713 wps = os_strcmp(params, "-WPS") == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 pos = buf;
1715 end = buf + buflen;
1716 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1717 struct wpa_ssid *ssid = wpa_s->current_ssid;
1718 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1719 MAC2STR(wpa_s->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001720 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 return pos - buf;
1722 pos += ret;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001723 ret = os_snprintf(pos, end - pos, "freq=%u\n",
1724 wpa_s->assoc_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001725 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001726 return pos - buf;
1727 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001728 if (ssid) {
1729 u8 *_ssid = ssid->ssid;
1730 size_t ssid_len = ssid->ssid_len;
1731 u8 ssid_buf[MAX_SSID_LEN];
1732 if (ssid_len == 0) {
1733 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1734 if (_res < 0)
1735 ssid_len = 0;
1736 else
1737 ssid_len = _res;
1738 _ssid = ssid_buf;
1739 }
1740 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1741 wpa_ssid_txt(_ssid, ssid_len),
1742 ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001743 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001744 return pos - buf;
1745 pos += ret;
1746
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001747 if (wps && ssid->passphrase &&
1748 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1749 (ssid->mode == WPAS_MODE_AP ||
1750 ssid->mode == WPAS_MODE_P2P_GO)) {
1751 ret = os_snprintf(pos, end - pos,
1752 "passphrase=%s\n",
1753 ssid->passphrase);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001754 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001755 return pos - buf;
1756 pos += ret;
1757 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001758 if (ssid->id_str) {
1759 ret = os_snprintf(pos, end - pos,
1760 "id_str=%s\n",
1761 ssid->id_str);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001762 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 return pos - buf;
1764 pos += ret;
1765 }
1766
1767 switch (ssid->mode) {
1768 case WPAS_MODE_INFRA:
1769 ret = os_snprintf(pos, end - pos,
1770 "mode=station\n");
1771 break;
1772 case WPAS_MODE_IBSS:
1773 ret = os_snprintf(pos, end - pos,
1774 "mode=IBSS\n");
1775 break;
1776 case WPAS_MODE_AP:
1777 ret = os_snprintf(pos, end - pos,
1778 "mode=AP\n");
1779 break;
1780 case WPAS_MODE_P2P_GO:
1781 ret = os_snprintf(pos, end - pos,
1782 "mode=P2P GO\n");
1783 break;
1784 case WPAS_MODE_P2P_GROUP_FORMATION:
1785 ret = os_snprintf(pos, end - pos,
1786 "mode=P2P GO - group "
1787 "formation\n");
1788 break;
1789 default:
1790 ret = 0;
1791 break;
1792 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001793 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794 return pos - buf;
1795 pos += ret;
1796 }
1797
1798#ifdef CONFIG_AP
1799 if (wpa_s->ap_iface) {
1800 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1801 end - pos,
1802 verbose);
1803 } else
1804#endif /* CONFIG_AP */
1805 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1806 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001807#ifdef CONFIG_SAE
1808 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001809#ifdef CONFIG_AP
1810 !wpa_s->ap_iface &&
1811#endif /* CONFIG_AP */
1812 wpa_s->sme.sae.state == SAE_ACCEPTED) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001813 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1814 wpa_s->sme.sae.group);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001815 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001816 return pos - buf;
1817 pos += ret;
1818 }
1819#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1821 wpa_supplicant_state_txt(wpa_s->wpa_state));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001822 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823 return pos - buf;
1824 pos += ret;
1825
1826 if (wpa_s->l2 &&
1827 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1828 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001829 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830 return pos - buf;
1831 pos += ret;
1832 }
1833
1834#ifdef CONFIG_P2P
1835 if (wpa_s->global->p2p) {
1836 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1837 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001838 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839 return pos - buf;
1840 pos += ret;
1841 }
1842#endif /* CONFIG_P2P */
1843
1844 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1845 MAC2STR(wpa_s->own_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001846 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847 return pos - buf;
1848 pos += ret;
1849
Dmitry Shmidt04949592012-07-19 12:16:46 -07001850#ifdef CONFIG_HS20
1851 if (wpa_s->current_bss &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001852 (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1853 HS20_IE_VENDOR_TYPE)) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001854 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1855 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001856 int release = 1;
1857 if (hs20[1] >= 5) {
1858 u8 rel_num = (hs20[6] & 0xf0) >> 4;
1859 release = rel_num + 1;
1860 }
1861 ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001862 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001863 return pos - buf;
1864 pos += ret;
1865 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001866
1867 if (wpa_s->current_ssid) {
1868 struct wpa_cred *cred;
1869 char *type;
1870
1871 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001872 size_t i;
1873
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001874 if (wpa_s->current_ssid->parent_cred != cred)
1875 continue;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001876
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001877 if (cred->provisioning_sp) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001878 ret = os_snprintf(pos, end - pos,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001879 "provisioning_sp=%s\n",
1880 cred->provisioning_sp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001881 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001882 return pos - buf;
1883 pos += ret;
1884 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001885
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001886 if (!cred->domain)
1887 goto no_domain;
1888
1889 i = 0;
1890 if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
1891 struct wpabuf *names =
1892 wpa_s->current_bss->anqp->domain_name;
1893 for (i = 0; names && i < cred->num_domain; i++)
1894 {
1895 if (domain_name_list_contains(
1896 names, cred->domain[i], 1))
1897 break;
1898 }
1899 if (i == cred->num_domain)
1900 i = 0; /* show first entry by default */
1901 }
1902 ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
1903 cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001904 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001905 return pos - buf;
1906 pos += ret;
1907
1908 no_domain:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001909 if (wpa_s->current_bss == NULL ||
1910 wpa_s->current_bss->anqp == NULL)
1911 res = -1;
1912 else
1913 res = interworking_home_sp_cred(
1914 wpa_s, cred,
1915 wpa_s->current_bss->anqp->domain_name);
1916 if (res > 0)
1917 type = "home";
1918 else if (res == 0)
1919 type = "roaming";
1920 else
1921 type = "unknown";
1922
1923 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001924 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001925 return pos - buf;
1926 pos += ret;
1927
1928 break;
1929 }
1930 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001931#endif /* CONFIG_HS20 */
1932
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001933 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1934 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1935 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1936 verbose);
1937 if (res >= 0)
1938 pos += res;
1939 }
1940
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001941 sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
1942 if (sess_id) {
1943 char *start = pos;
1944
1945 ret = os_snprintf(pos, end - pos, "eap_session_id=");
1946 if (os_snprintf_error(end - pos, ret))
1947 return start - buf;
1948 pos += ret;
1949 ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
1950 if (ret <= 0)
1951 return start - buf;
1952 pos += ret;
1953 ret = os_snprintf(pos, end - pos, "\n");
1954 if (os_snprintf_error(end - pos, ret))
1955 return start - buf;
1956 pos += ret;
1957 }
1958
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1960 if (res >= 0)
1961 pos += res;
1962
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001963#ifdef CONFIG_WPS
1964 {
1965 char uuid_str[100];
1966 uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
1967 ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001968 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001969 return pos - buf;
1970 pos += ret;
1971 }
1972#endif /* CONFIG_WPS */
1973
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001974#ifdef ANDROID
vandwalleffc70182014-09-11 11:40:14 -07001975 /*
1976 * Allow using the STATUS command with default behavior, say for debug,
1977 * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
1978 * events with STATUS-NO_EVENTS.
1979 */
1980 if (os_strcmp(params, "-NO_EVENTS")) {
1981 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
1982 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
1983 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
1984 wpa_s->wpa_state,
1985 MAC2STR(wpa_s->bssid),
1986 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1987 wpa_ssid_txt(wpa_s->current_ssid->ssid,
1988 wpa_s->current_ssid->ssid_len) : "");
1989 if (wpa_s->wpa_state == WPA_COMPLETED) {
1990 struct wpa_ssid *ssid = wpa_s->current_ssid;
1991 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
1992 "- connection to " MACSTR
1993 " completed %s [id=%d id_str=%s]",
1994 MAC2STR(wpa_s->bssid), "(auth)",
1995 ssid ? ssid->id : -1,
1996 ssid && ssid->id_str ? ssid->id_str : "");
1997 }
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001998 }
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001999#endif /* ANDROID */
2000
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001 return pos - buf;
2002}
2003
2004
2005static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
2006 char *cmd)
2007{
2008 char *pos;
2009 int id;
2010 struct wpa_ssid *ssid;
2011 u8 bssid[ETH_ALEN];
2012
2013 /* cmd: "<network id> <BSSID>" */
2014 pos = os_strchr(cmd, ' ');
2015 if (pos == NULL)
2016 return -1;
2017 *pos++ = '\0';
2018 id = atoi(cmd);
2019 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
2020 if (hwaddr_aton(pos, bssid)) {
2021 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
2022 return -1;
2023 }
2024
2025 ssid = wpa_config_get_network(wpa_s->conf, id);
2026 if (ssid == NULL) {
2027 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2028 "to update", id);
2029 return -1;
2030 }
2031
2032 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
2033 ssid->bssid_set = !is_zero_ether_addr(bssid);
2034
2035 return 0;
2036}
2037
2038
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002039static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002040 char *cmd, char *buf,
2041 size_t buflen)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002042{
2043 u8 bssid[ETH_ALEN];
2044 struct wpa_blacklist *e;
2045 char *pos, *end;
2046 int ret;
2047
2048 /* cmd: "BLACKLIST [<BSSID>]" */
2049 if (*cmd == '\0') {
2050 pos = buf;
2051 end = buf + buflen;
2052 e = wpa_s->blacklist;
2053 while (e) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002054 ret = os_snprintf(pos, end - pos, MACSTR "\n",
2055 MAC2STR(e->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002056 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002057 return pos - buf;
2058 pos += ret;
2059 e = e->next;
2060 }
2061 return pos - buf;
2062 }
2063
2064 cmd++;
2065 if (os_strncmp(cmd, "clear", 5) == 0) {
2066 wpa_blacklist_clear(wpa_s);
2067 os_memcpy(buf, "OK\n", 3);
2068 return 3;
2069 }
2070
2071 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
2072 if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002073 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002074 return -1;
2075 }
2076
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002077 /*
2078 * Add the BSSID twice, so its count will be 2, causing it to be
2079 * skipped when processing scan results.
2080 */
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002081 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002082 if (ret < 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002083 return -1;
2084 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002085 if (ret < 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002086 return -1;
2087 os_memcpy(buf, "OK\n", 3);
2088 return 3;
2089}
2090
2091
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002092static const char * debug_level_str(int level)
2093{
2094 switch (level) {
2095 case MSG_EXCESSIVE:
2096 return "EXCESSIVE";
2097 case MSG_MSGDUMP:
2098 return "MSGDUMP";
2099 case MSG_DEBUG:
2100 return "DEBUG";
2101 case MSG_INFO:
2102 return "INFO";
2103 case MSG_WARNING:
2104 return "WARNING";
2105 case MSG_ERROR:
2106 return "ERROR";
2107 default:
2108 return "?";
2109 }
2110}
2111
2112
2113static int str_to_debug_level(const char *s)
2114{
2115 if (os_strcasecmp(s, "EXCESSIVE") == 0)
2116 return MSG_EXCESSIVE;
2117 if (os_strcasecmp(s, "MSGDUMP") == 0)
2118 return MSG_MSGDUMP;
2119 if (os_strcasecmp(s, "DEBUG") == 0)
2120 return MSG_DEBUG;
2121 if (os_strcasecmp(s, "INFO") == 0)
2122 return MSG_INFO;
2123 if (os_strcasecmp(s, "WARNING") == 0)
2124 return MSG_WARNING;
2125 if (os_strcasecmp(s, "ERROR") == 0)
2126 return MSG_ERROR;
2127 return -1;
2128}
2129
2130
2131static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
2132 char *cmd, char *buf,
2133 size_t buflen)
2134{
2135 char *pos, *end, *stamp;
2136 int ret;
2137
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002138 /* cmd: "LOG_LEVEL [<level>]" */
2139 if (*cmd == '\0') {
2140 pos = buf;
2141 end = buf + buflen;
2142 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2143 "Timestamp: %d\n",
2144 debug_level_str(wpa_debug_level),
2145 wpa_debug_timestamp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002146 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002147 ret = 0;
2148
2149 return ret;
2150 }
2151
2152 while (*cmd == ' ')
2153 cmd++;
2154
2155 stamp = os_strchr(cmd, ' ');
2156 if (stamp) {
2157 *stamp++ = '\0';
2158 while (*stamp == ' ') {
2159 stamp++;
2160 }
2161 }
2162
2163 if (cmd && os_strlen(cmd)) {
2164 int level = str_to_debug_level(cmd);
2165 if (level < 0)
2166 return -1;
2167 wpa_debug_level = level;
2168 }
2169
2170 if (stamp && os_strlen(stamp))
2171 wpa_debug_timestamp = atoi(stamp);
2172
2173 os_memcpy(buf, "OK\n", 3);
2174 return 3;
2175}
2176
2177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178static int wpa_supplicant_ctrl_iface_list_networks(
Vinit Deshpandeda134e92014-12-02 10:59:29 -08002179 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180{
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002181 char *pos, *end, *prev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 struct wpa_ssid *ssid;
2183 int ret;
2184
2185 pos = buf;
2186 end = buf + buflen;
2187 ret = os_snprintf(pos, end - pos,
2188 "network id / ssid / bssid / flags\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002189 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002190 return pos - buf;
2191 pos += ret;
2192
2193 ssid = wpa_s->conf->ssid;
Vinit Deshpandeda134e92014-12-02 10:59:29 -08002194
2195 /* skip over ssids until we find next one */
2196 if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
2197 int last_id = atoi(cmd + 8);
2198 if (last_id != -1) {
2199 while (ssid != NULL && ssid->id <= last_id) {
2200 ssid = ssid->next;
2201 }
2202 }
2203 }
2204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002205 while (ssid) {
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002206 prev = pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002207 ret = os_snprintf(pos, end - pos, "%d\t%s",
2208 ssid->id,
2209 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002210 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002211 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002212 pos += ret;
2213 if (ssid->bssid_set) {
2214 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2215 MAC2STR(ssid->bssid));
2216 } else {
2217 ret = os_snprintf(pos, end - pos, "\tany");
2218 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002219 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002220 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002221 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002222 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223 ssid == wpa_s->current_ssid ?
2224 "[CURRENT]" : "",
2225 ssid->disabled ? "[DISABLED]" : "",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002226 ssid->disabled_until.sec ?
2227 "[TEMP-DISABLED]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002228 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2229 "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002230 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002231 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232 pos += ret;
2233 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002234 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002235 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236 pos += ret;
2237
2238 ssid = ssid->next;
2239 }
2240
2241 return pos - buf;
2242}
2243
2244
2245static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2246{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002247 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248 ret = os_snprintf(pos, end - pos, "-");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002249 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002250 return pos;
2251 pos += ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002252 ret = wpa_write_ciphers(pos, end, cipher, "+");
2253 if (ret < 0)
2254 return pos;
2255 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 return pos;
2257}
2258
2259
2260static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2261 const u8 *ie, size_t ie_len)
2262{
2263 struct wpa_ie_data data;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002264 char *start;
2265 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266
2267 ret = os_snprintf(pos, end - pos, "[%s-", proto);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002268 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002269 return pos;
2270 pos += ret;
2271
2272 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2273 ret = os_snprintf(pos, end - pos, "?]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002274 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002275 return pos;
2276 pos += ret;
2277 return pos;
2278 }
2279
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002280 start = pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002281 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002282 ret = os_snprintf(pos, end - pos, "%sEAP",
2283 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002284 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285 return pos;
2286 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002287 }
2288 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002289 ret = os_snprintf(pos, end - pos, "%sPSK",
2290 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002291 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292 return pos;
2293 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294 }
2295 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002296 ret = os_snprintf(pos, end - pos, "%sNone",
2297 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002298 if (os_snprintf_error(end - pos, ret))
2299 return pos;
2300 pos += ret;
2301 }
2302 if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
2303 ret = os_snprintf(pos, end - pos, "%sSAE",
2304 pos == start ? "" : "+");
2305 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002306 return pos;
2307 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308 }
2309#ifdef CONFIG_IEEE80211R
2310 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2311 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002312 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002313 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002314 return pos;
2315 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316 }
2317 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2318 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002319 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002320 if (os_snprintf_error(end - pos, ret))
2321 return pos;
2322 pos += ret;
2323 }
2324 if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2325 ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2326 pos == start ? "" : "+");
2327 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002328 return pos;
2329 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002330 }
2331#endif /* CONFIG_IEEE80211R */
2332#ifdef CONFIG_IEEE80211W
2333 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2334 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002335 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002336 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337 return pos;
2338 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002339 }
2340 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2341 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002342 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002343 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344 return pos;
2345 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 }
2347#endif /* CONFIG_IEEE80211W */
2348
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002349#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002350 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2351 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
2352 pos == start ? "" : "+");
2353 if (os_snprintf_error(end - pos, ret))
2354 return pos;
2355 pos += ret;
2356 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002357#endif /* CONFIG_SUITEB */
2358
2359#ifdef CONFIG_SUITEB192
2360 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
2361 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
2362 pos == start ? "" : "+");
2363 if (os_snprintf_error(end - pos, ret))
2364 return pos;
2365 pos += ret;
2366 }
2367#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002369 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2370
2371 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2372 ret = os_snprintf(pos, end - pos, "-preauth");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002373 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374 return pos;
2375 pos += ret;
2376 }
2377
2378 ret = os_snprintf(pos, end - pos, "]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002379 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002380 return pos;
2381 pos += ret;
2382
2383 return pos;
2384}
2385
2386
2387#ifdef CONFIG_WPS
2388static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2389 char *pos, char *end,
2390 struct wpabuf *wps_ie)
2391{
2392 int ret;
2393 const char *txt;
2394
2395 if (wps_ie == NULL)
2396 return pos;
2397 if (wps_is_selected_pbc_registrar(wps_ie))
2398 txt = "[WPS-PBC]";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002399 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2400 txt = "[WPS-AUTH]";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002401 else if (wps_is_selected_pin_registrar(wps_ie))
2402 txt = "[WPS-PIN]";
2403 else
2404 txt = "[WPS]";
2405
2406 ret = os_snprintf(pos, end - pos, "%s", txt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002407 if (!os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002408 pos += ret;
2409 wpabuf_free(wps_ie);
2410 return pos;
2411}
2412#endif /* CONFIG_WPS */
2413
2414
2415static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2416 char *pos, char *end,
2417 const struct wpa_bss *bss)
2418{
2419#ifdef CONFIG_WPS
2420 struct wpabuf *wps_ie;
2421 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2422 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2423#else /* CONFIG_WPS */
2424 return pos;
2425#endif /* CONFIG_WPS */
2426}
2427
2428
2429/* Format one result on one text line into a buffer. */
2430static int wpa_supplicant_ctrl_iface_scan_result(
2431 struct wpa_supplicant *wpa_s,
2432 const struct wpa_bss *bss, char *buf, size_t buflen)
2433{
2434 char *pos, *end;
2435 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002436 const u8 *ie, *ie2, *p2p, *mesh;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002437
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002438 mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07002440 if (!p2p)
2441 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002442 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2443 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2444 0)
2445 return 0; /* Do not show P2P listen discovery results here */
2446
2447 pos = buf;
2448 end = buf + buflen;
2449
2450 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2451 MAC2STR(bss->bssid), bss->freq, bss->level);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002452 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453 return -1;
2454 pos += ret;
2455 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2456 if (ie)
2457 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2458 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002459 if (ie2) {
2460 pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
2461 ie2, 2 + ie2[1]);
2462 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002463 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2464 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2465 ret = os_snprintf(pos, end - pos, "[WEP]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002466 if (os_snprintf_error(end - pos, ret))
2467 return -1;
2468 pos += ret;
2469 }
2470 if (mesh) {
2471 ret = os_snprintf(pos, end - pos, "[MESH]");
2472 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473 return -1;
2474 pos += ret;
2475 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002476 if (bss_is_dmg(bss)) {
2477 const char *s;
2478 ret = os_snprintf(pos, end - pos, "[DMG]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002479 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480 return -1;
2481 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002482 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
2483 case IEEE80211_CAP_DMG_IBSS:
2484 s = "[IBSS]";
2485 break;
2486 case IEEE80211_CAP_DMG_AP:
2487 s = "[ESS]";
2488 break;
2489 case IEEE80211_CAP_DMG_PBSS:
2490 s = "[PBSS]";
2491 break;
2492 default:
2493 s = "";
2494 break;
2495 }
2496 ret = os_snprintf(pos, end - pos, "%s", s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002497 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498 return -1;
2499 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002500 } else {
2501 if (bss->caps & IEEE80211_CAP_IBSS) {
2502 ret = os_snprintf(pos, end - pos, "[IBSS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002503 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002504 return -1;
2505 pos += ret;
2506 }
2507 if (bss->caps & IEEE80211_CAP_ESS) {
2508 ret = os_snprintf(pos, end - pos, "[ESS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002509 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002510 return -1;
2511 pos += ret;
2512 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513 }
2514 if (p2p) {
2515 ret = os_snprintf(pos, end - pos, "[P2P]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002516 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002517 return -1;
2518 pos += ret;
2519 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002520#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002521 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002522 ret = os_snprintf(pos, end - pos, "[HS20]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002523 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07002524 return -1;
2525 pos += ret;
2526 }
2527#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528
2529 ret = os_snprintf(pos, end - pos, "\t%s",
2530 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002531 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 return -1;
2533 pos += ret;
2534
2535 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002536 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 return -1;
2538 pos += ret;
2539
2540 return pos - buf;
2541}
2542
2543
2544static int wpa_supplicant_ctrl_iface_scan_results(
2545 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2546{
2547 char *pos, *end;
2548 struct wpa_bss *bss;
2549 int ret;
2550
2551 pos = buf;
2552 end = buf + buflen;
2553 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2554 "flags / ssid\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002555 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556 return pos - buf;
2557 pos += ret;
2558
2559 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2560 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2561 end - pos);
2562 if (ret < 0 || ret >= end - pos)
2563 return pos - buf;
2564 pos += ret;
2565 }
2566
2567 return pos - buf;
2568}
2569
2570
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002571#ifdef CONFIG_MESH
2572
2573static int wpa_supplicant_ctrl_iface_mesh_interface_add(
2574 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
2575{
2576 char *pos, ifname[IFNAMSIZ + 1];
2577
2578 ifname[0] = '\0';
2579
2580 pos = os_strstr(cmd, "ifname=");
2581 if (pos) {
2582 pos += 7;
2583 os_strlcpy(ifname, pos, sizeof(ifname));
2584 }
2585
2586 if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
2587 return -1;
2588
2589 os_strlcpy(reply, ifname, max_len);
2590 return os_strlen(ifname);
2591}
2592
2593
2594static int wpa_supplicant_ctrl_iface_mesh_group_add(
2595 struct wpa_supplicant *wpa_s, char *cmd)
2596{
2597 int id;
2598 struct wpa_ssid *ssid;
2599
2600 id = atoi(cmd);
2601 wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
2602
2603 ssid = wpa_config_get_network(wpa_s->conf, id);
2604 if (ssid == NULL) {
2605 wpa_printf(MSG_DEBUG,
2606 "CTRL_IFACE: Could not find network id=%d", id);
2607 return -1;
2608 }
2609 if (ssid->mode != WPAS_MODE_MESH) {
2610 wpa_printf(MSG_DEBUG,
2611 "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
2612 return -1;
2613 }
2614 if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
2615 ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
2616 wpa_printf(MSG_ERROR,
2617 "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
2618 return -1;
2619 }
2620
2621 /*
2622 * TODO: If necessary write our own group_add function,
2623 * for now we can reuse select_network
2624 */
2625 wpa_supplicant_select_network(wpa_s, ssid);
2626
2627 return 0;
2628}
2629
2630
2631static int wpa_supplicant_ctrl_iface_mesh_group_remove(
2632 struct wpa_supplicant *wpa_s, char *cmd)
2633{
2634 struct wpa_supplicant *orig;
2635 struct wpa_global *global;
2636 int found = 0;
2637
2638 wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
2639
2640 global = wpa_s->global;
2641 orig = wpa_s;
2642
2643 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2644 if (os_strcmp(wpa_s->ifname, cmd) == 0) {
2645 found = 1;
2646 break;
2647 }
2648 }
2649 if (!found) {
2650 wpa_printf(MSG_ERROR,
2651 "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
2652 cmd);
2653 return -1;
2654 }
2655 if (wpa_s->mesh_if_created && wpa_s == orig) {
2656 wpa_printf(MSG_ERROR,
2657 "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
2658 return -1;
2659 }
2660
2661 wpa_s->reassociate = 0;
2662 wpa_s->disconnected = 1;
2663 wpa_supplicant_cancel_sched_scan(wpa_s);
2664 wpa_supplicant_cancel_scan(wpa_s);
2665
2666 /*
2667 * TODO: If necessary write our own group_remove function,
2668 * for now we can reuse deauthenticate
2669 */
2670 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2671
2672 if (wpa_s->mesh_if_created)
2673 wpa_supplicant_remove_iface(global, wpa_s, 0);
2674
2675 return 0;
2676}
2677
2678#endif /* CONFIG_MESH */
2679
2680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681static int wpa_supplicant_ctrl_iface_select_network(
2682 struct wpa_supplicant *wpa_s, char *cmd)
2683{
2684 int id;
2685 struct wpa_ssid *ssid;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002686 char *pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002687
2688 /* cmd: "<network id>" or "any" */
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002689 if (os_strncmp(cmd, "any", 3) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002690 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2691 ssid = NULL;
2692 } else {
2693 id = atoi(cmd);
2694 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2695
2696 ssid = wpa_config_get_network(wpa_s->conf, id);
2697 if (ssid == NULL) {
2698 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2699 "network id=%d", id);
2700 return -1;
2701 }
2702 if (ssid->disabled == 2) {
2703 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2704 "SELECT_NETWORK with persistent P2P group");
2705 return -1;
2706 }
2707 }
2708
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002709 pos = os_strstr(cmd, " freq=");
2710 if (pos) {
2711 int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
2712 if (freqs) {
2713 wpa_s->scan_req = MANUAL_SCAN_REQ;
2714 os_free(wpa_s->manual_scan_freqs);
2715 wpa_s->manual_scan_freqs = freqs;
2716 }
2717 }
2718
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002719 wpa_supplicant_select_network(wpa_s, ssid);
2720
2721 return 0;
2722}
2723
2724
2725static int wpa_supplicant_ctrl_iface_enable_network(
2726 struct wpa_supplicant *wpa_s, char *cmd)
2727{
2728 int id;
2729 struct wpa_ssid *ssid;
2730
2731 /* cmd: "<network id>" or "all" */
2732 if (os_strcmp(cmd, "all") == 0) {
2733 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2734 ssid = NULL;
2735 } else {
2736 id = atoi(cmd);
2737 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2738
2739 ssid = wpa_config_get_network(wpa_s->conf, id);
2740 if (ssid == NULL) {
2741 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2742 "network id=%d", id);
2743 return -1;
2744 }
2745 if (ssid->disabled == 2) {
2746 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2747 "ENABLE_NETWORK with persistent P2P group");
2748 return -1;
2749 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002750
2751 if (os_strstr(cmd, " no-connect")) {
2752 ssid->disabled = 0;
2753 return 0;
2754 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002755 }
2756 wpa_supplicant_enable_network(wpa_s, ssid);
2757
2758 return 0;
2759}
2760
2761
2762static int wpa_supplicant_ctrl_iface_disable_network(
2763 struct wpa_supplicant *wpa_s, char *cmd)
2764{
2765 int id;
2766 struct wpa_ssid *ssid;
2767
2768 /* cmd: "<network id>" or "all" */
2769 if (os_strcmp(cmd, "all") == 0) {
2770 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2771 ssid = NULL;
2772 } else {
2773 id = atoi(cmd);
2774 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2775
2776 ssid = wpa_config_get_network(wpa_s->conf, id);
2777 if (ssid == NULL) {
2778 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2779 "network id=%d", id);
2780 return -1;
2781 }
2782 if (ssid->disabled == 2) {
2783 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2784 "DISABLE_NETWORK with persistent P2P "
2785 "group");
2786 return -1;
2787 }
2788 }
2789 wpa_supplicant_disable_network(wpa_s, ssid);
2790
2791 return 0;
2792}
2793
2794
2795static int wpa_supplicant_ctrl_iface_add_network(
2796 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2797{
2798 struct wpa_ssid *ssid;
2799 int ret;
2800
2801 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2802
2803 ssid = wpa_config_add_network(wpa_s->conf);
2804 if (ssid == NULL)
2805 return -1;
2806
2807 wpas_notify_network_added(wpa_s, ssid);
2808
2809 ssid->disabled = 1;
2810 wpa_config_set_network_defaults(ssid);
2811
2812 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002813 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814 return -1;
2815 return ret;
2816}
2817
2818
2819static int wpa_supplicant_ctrl_iface_remove_network(
2820 struct wpa_supplicant *wpa_s, char *cmd)
2821{
2822 int id;
2823 struct wpa_ssid *ssid;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002824 int was_disabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825
2826 /* cmd: "<network id>" or "all" */
2827 if (os_strcmp(cmd, "all") == 0) {
2828 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002829 if (wpa_s->sched_scanning)
2830 wpa_supplicant_cancel_sched_scan(wpa_s);
2831
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002832 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833 if (wpa_s->current_ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002834#ifdef CONFIG_SME
2835 wpa_s->sme.prev_bssid_set = 0;
2836#endif /* CONFIG_SME */
Jouni Malinen75ecf522011-06-27 15:19:46 -07002837 wpa_sm_set_config(wpa_s->wpa, NULL);
2838 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002839 wpa_s->own_disconnect_req = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002840 wpa_supplicant_deauthenticate(
2841 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002842 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002843 ssid = wpa_s->conf->ssid;
2844 while (ssid) {
2845 struct wpa_ssid *remove_ssid = ssid;
2846 id = ssid->id;
2847 ssid = ssid->next;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002848 if (wpa_s->last_ssid == remove_ssid)
2849 wpa_s->last_ssid = NULL;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002850 wpas_notify_network_removed(wpa_s, remove_ssid);
2851 wpa_config_remove_network(wpa_s->conf, id);
2852 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002853 return 0;
2854 }
2855
2856 id = atoi(cmd);
2857 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2858
2859 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002860 if (ssid)
2861 wpas_notify_network_removed(wpa_s, ssid);
Deepthi Gowria831d782012-09-03 11:55:38 +03002862 if (ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2864 "id=%d", id);
2865 return -1;
2866 }
2867
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002868 if (wpa_s->last_ssid == ssid)
2869 wpa_s->last_ssid = NULL;
2870
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002871 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002872#ifdef CONFIG_SME
2873 wpa_s->sme.prev_bssid_set = 0;
2874#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002876 * Invalidate the EAP session cache if the current or
2877 * previously used network is removed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002878 */
2879 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002880 }
2881
2882 if (ssid == wpa_s->current_ssid) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002883 wpa_sm_set_config(wpa_s->wpa, NULL);
2884 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002886 wpa_s->own_disconnect_req = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002887 wpa_supplicant_deauthenticate(wpa_s,
2888 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889 }
2890
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002891 was_disabled = ssid->disabled;
2892
Deepthi Gowria831d782012-09-03 11:55:38 +03002893 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2894 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2895 "network id=%d", id);
2896 return -1;
2897 }
2898
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002899 if (!was_disabled && wpa_s->sched_scanning) {
2900 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2901 "network from filters");
2902 wpa_supplicant_cancel_sched_scan(wpa_s);
2903 wpa_supplicant_req_scan(wpa_s, 0, 0);
2904 }
2905
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002906 return 0;
2907}
2908
2909
Dmitry Shmidt684785c2014-05-12 13:34:29 -07002910static int wpa_supplicant_ctrl_iface_update_network(
2911 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2912 char *name, char *value)
2913{
2914 if (wpa_config_set(ssid, name, value, 0) < 0) {
2915 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2916 "variable '%s'", name);
2917 return -1;
2918 }
2919
2920 if (os_strcmp(name, "bssid") != 0 &&
2921 os_strcmp(name, "priority") != 0)
2922 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
2923
2924 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
2925 /*
2926 * Invalidate the EAP session cache if anything in the current
2927 * or previously used configuration changes.
2928 */
2929 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2930 }
2931
2932 if ((os_strcmp(name, "psk") == 0 &&
2933 value[0] == '"' && ssid->ssid_len) ||
2934 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2935 wpa_config_update_psk(ssid);
2936 else if (os_strcmp(name, "priority") == 0)
2937 wpa_config_update_prio_list(wpa_s->conf);
2938
2939 return 0;
2940}
2941
2942
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002943static int wpa_supplicant_ctrl_iface_set_network(
2944 struct wpa_supplicant *wpa_s, char *cmd)
2945{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002946 int id, ret, prev_bssid_set, prev_disabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002947 struct wpa_ssid *ssid;
2948 char *name, *value;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002949 u8 prev_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002950
2951 /* cmd: "<network id> <variable name> <value>" */
2952 name = os_strchr(cmd, ' ');
2953 if (name == NULL)
2954 return -1;
2955 *name++ = '\0';
2956
2957 value = os_strchr(name, ' ');
2958 if (value == NULL)
2959 return -1;
2960 *value++ = '\0';
2961
2962 id = atoi(cmd);
2963 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2964 id, name);
2965 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2966 (u8 *) value, os_strlen(value));
2967
2968 ssid = wpa_config_get_network(wpa_s->conf, id);
2969 if (ssid == NULL) {
2970 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2971 "id=%d", id);
2972 return -1;
2973 }
2974
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002975 prev_bssid_set = ssid->bssid_set;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002976 prev_disabled = ssid->disabled;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002977 os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
2978 ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
2979 value);
2980 if (ret == 0 &&
2981 (ssid->bssid_set != prev_bssid_set ||
2982 os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
2983 wpas_notify_network_bssid_set_changed(wpa_s, ssid);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002984
2985 if (prev_disabled != ssid->disabled &&
2986 (prev_disabled == 2 || ssid->disabled == 2))
2987 wpas_notify_network_type_changed(wpa_s, ssid);
2988
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002989 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002990}
2991
2992
2993static int wpa_supplicant_ctrl_iface_get_network(
2994 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2995{
2996 int id;
2997 size_t res;
2998 struct wpa_ssid *ssid;
2999 char *name, *value;
3000
3001 /* cmd: "<network id> <variable name>" */
3002 name = os_strchr(cmd, ' ');
3003 if (name == NULL || buflen == 0)
3004 return -1;
3005 *name++ = '\0';
3006
3007 id = atoi(cmd);
3008 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
3009 id, name);
3010
3011 ssid = wpa_config_get_network(wpa_s->conf, id);
3012 if (ssid == NULL) {
3013 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3014 "id=%d", id);
3015 return -1;
3016 }
3017
3018 value = wpa_config_get_no_key(ssid, name);
3019 if (value == NULL) {
3020 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
3021 "variable '%s'", name);
3022 return -1;
3023 }
3024
3025 res = os_strlcpy(buf, value, buflen);
3026 if (res >= buflen) {
3027 os_free(value);
3028 return -1;
3029 }
3030
3031 os_free(value);
3032
3033 return res;
3034}
3035
3036
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003037static int wpa_supplicant_ctrl_iface_dup_network(
3038 struct wpa_supplicant *wpa_s, char *cmd)
3039{
3040 struct wpa_ssid *ssid_s, *ssid_d;
3041 char *name, *id, *value;
3042 int id_s, id_d, ret;
3043
3044 /* cmd: "<src network id> <dst network id> <variable name>" */
3045 id = os_strchr(cmd, ' ');
3046 if (id == NULL)
3047 return -1;
3048 *id++ = '\0';
3049
3050 name = os_strchr(id, ' ');
3051 if (name == NULL)
3052 return -1;
3053 *name++ = '\0';
3054
3055 id_s = atoi(cmd);
3056 id_d = atoi(id);
3057 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DUP_NETWORK id=%d -> %d name='%s'",
3058 id_s, id_d, name);
3059
3060 ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
3061 if (ssid_s == NULL) {
3062 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3063 "network id=%d", id_s);
3064 return -1;
3065 }
3066
3067 ssid_d = wpa_config_get_network(wpa_s->conf, id_d);
3068 if (ssid_d == NULL) {
3069 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003070 "network id=%d", id_d);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003071 return -1;
3072 }
3073
3074 value = wpa_config_get(ssid_s, name);
3075 if (value == NULL) {
3076 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
3077 "variable '%s'", name);
3078 return -1;
3079 }
3080
3081 ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid_d, name,
3082 value);
3083
3084 os_free(value);
3085
3086 return ret;
3087}
3088
3089
Dmitry Shmidt04949592012-07-19 12:16:46 -07003090static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
3091 char *buf, size_t buflen)
3092{
3093 char *pos, *end;
3094 struct wpa_cred *cred;
3095 int ret;
3096
3097 pos = buf;
3098 end = buf + buflen;
3099 ret = os_snprintf(pos, end - pos,
3100 "cred id / realm / username / domain / imsi\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003101 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07003102 return pos - buf;
3103 pos += ret;
3104
3105 cred = wpa_s->conf->cred;
3106 while (cred) {
3107 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
3108 cred->id, cred->realm ? cred->realm : "",
3109 cred->username ? cred->username : "",
Dmitry Shmidt051af732013-10-22 13:52:46 -07003110 cred->domain ? cred->domain[0] : "",
Dmitry Shmidt04949592012-07-19 12:16:46 -07003111 cred->imsi ? cred->imsi : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003112 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07003113 return pos - buf;
3114 pos += ret;
3115
3116 cred = cred->next;
3117 }
3118
3119 return pos - buf;
3120}
3121
3122
3123static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
3124 char *buf, size_t buflen)
3125{
3126 struct wpa_cred *cred;
3127 int ret;
3128
3129 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
3130
3131 cred = wpa_config_add_cred(wpa_s->conf);
3132 if (cred == NULL)
3133 return -1;
3134
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003135 wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
3136
Dmitry Shmidt04949592012-07-19 12:16:46 -07003137 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003138 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07003139 return -1;
3140 return ret;
3141}
3142
3143
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003144static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
3145 struct wpa_cred *cred)
3146{
3147 struct wpa_ssid *ssid;
3148 char str[20];
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003149 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003150
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003151 if (cred == NULL) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003152 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3153 return -1;
3154 }
3155
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003156 id = cred->id;
3157 if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
3158 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3159 return -1;
3160 }
3161
3162 wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
3163
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003164 /* Remove any network entry created based on the removed credential */
3165 ssid = wpa_s->conf->ssid;
3166 while (ssid) {
3167 if (ssid->parent_cred == cred) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003168 int res;
3169
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003170 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
3171 "used the removed credential", ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003172 res = os_snprintf(str, sizeof(str), "%d", ssid->id);
3173 if (os_snprintf_error(sizeof(str), res))
3174 str[sizeof(str) - 1] = '\0';
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003175 ssid = ssid->next;
3176 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
3177 } else
3178 ssid = ssid->next;
3179 }
3180
3181 return 0;
3182}
3183
3184
Dmitry Shmidt04949592012-07-19 12:16:46 -07003185static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
3186 char *cmd)
3187{
3188 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003189 struct wpa_cred *cred, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003190
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003191 /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
3192 * "provisioning_sp=<FQDN> */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003193 if (os_strcmp(cmd, "all") == 0) {
3194 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
3195 cred = wpa_s->conf->cred;
3196 while (cred) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003197 prev = cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003198 cred = cred->next;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003199 wpas_ctrl_remove_cred(wpa_s, prev);
3200 }
3201 return 0;
3202 }
3203
3204 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
3205 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
3206 cmd + 8);
3207 cred = wpa_s->conf->cred;
3208 while (cred) {
3209 prev = cred;
3210 cred = cred->next;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003211 if (prev->domain) {
3212 size_t i;
3213 for (i = 0; i < prev->num_domain; i++) {
3214 if (os_strcmp(prev->domain[i], cmd + 8)
3215 != 0)
3216 continue;
3217 wpas_ctrl_remove_cred(wpa_s, prev);
3218 break;
3219 }
3220 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003221 }
3222 return 0;
3223 }
3224
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003225 if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
3226 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
3227 cmd + 16);
3228 cred = wpa_s->conf->cred;
3229 while (cred) {
3230 prev = cred;
3231 cred = cred->next;
3232 if (prev->provisioning_sp &&
3233 os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
3234 wpas_ctrl_remove_cred(wpa_s, prev);
3235 }
3236 return 0;
3237 }
3238
Dmitry Shmidt04949592012-07-19 12:16:46 -07003239 id = atoi(cmd);
3240 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
3241
3242 cred = wpa_config_get_cred(wpa_s->conf, id);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003243 return wpas_ctrl_remove_cred(wpa_s, cred);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003244}
3245
3246
3247static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
3248 char *cmd)
3249{
3250 int id;
3251 struct wpa_cred *cred;
3252 char *name, *value;
3253
3254 /* cmd: "<cred id> <variable name> <value>" */
3255 name = os_strchr(cmd, ' ');
3256 if (name == NULL)
3257 return -1;
3258 *name++ = '\0';
3259
3260 value = os_strchr(name, ' ');
3261 if (value == NULL)
3262 return -1;
3263 *value++ = '\0';
3264
3265 id = atoi(cmd);
3266 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
3267 id, name);
3268 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3269 (u8 *) value, os_strlen(value));
3270
3271 cred = wpa_config_get_cred(wpa_s->conf, id);
3272 if (cred == NULL) {
3273 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3274 id);
3275 return -1;
3276 }
3277
3278 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
3279 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
3280 "variable '%s'", name);
3281 return -1;
3282 }
3283
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003284 wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
3285
Dmitry Shmidt04949592012-07-19 12:16:46 -07003286 return 0;
3287}
3288
3289
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003290static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
3291 char *cmd, char *buf,
3292 size_t buflen)
3293{
3294 int id;
3295 size_t res;
3296 struct wpa_cred *cred;
3297 char *name, *value;
3298
3299 /* cmd: "<cred id> <variable name>" */
3300 name = os_strchr(cmd, ' ');
3301 if (name == NULL)
3302 return -1;
3303 *name++ = '\0';
3304
3305 id = atoi(cmd);
3306 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
3307 id, name);
3308
3309 cred = wpa_config_get_cred(wpa_s->conf, id);
3310 if (cred == NULL) {
3311 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3312 id);
3313 return -1;
3314 }
3315
3316 value = wpa_config_get_cred_no_key(cred, name);
3317 if (value == NULL) {
3318 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
3319 name);
3320 return -1;
3321 }
3322
3323 res = os_strlcpy(buf, value, buflen);
3324 if (res >= buflen) {
3325 os_free(value);
3326 return -1;
3327 }
3328
3329 os_free(value);
3330
3331 return res;
3332}
3333
3334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003335#ifndef CONFIG_NO_CONFIG_WRITE
3336static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
3337{
3338 int ret;
3339
3340 if (!wpa_s->conf->update_config) {
3341 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
3342 "to update configuration (update_config=0)");
3343 return -1;
3344 }
3345
3346 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
3347 if (ret) {
3348 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
3349 "update configuration");
3350 } else {
3351 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
3352 " updated");
3353 }
3354
3355 return ret;
3356}
3357#endif /* CONFIG_NO_CONFIG_WRITE */
3358
3359
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003360struct cipher_info {
3361 unsigned int capa;
3362 const char *name;
3363 int group_only;
3364};
3365
3366static const struct cipher_info ciphers[] = {
3367 { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
3368 { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
3369 { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
3370 { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
3371 { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
3372 { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
3373 { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
3374 { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
3375};
3376
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003377static const struct cipher_info ciphers_group_mgmt[] = {
3378 { WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
3379 { WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
3380 { WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
3381 { WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
3382};
3383
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003385static int ctrl_iface_get_capability_pairwise(int res, char *strict,
3386 struct wpa_driver_capa *capa,
3387 char *buf, size_t buflen)
3388{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003389 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003390 char *pos, *end;
3391 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003392 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003393
3394 pos = buf;
3395 end = pos + buflen;
3396
3397 if (res < 0) {
3398 if (strict)
3399 return 0;
3400 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
3401 if (len >= buflen)
3402 return -1;
3403 return len;
3404 }
3405
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003406 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3407 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
3408 ret = os_snprintf(pos, end - pos, "%s%s",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003409 pos == buf ? "" : " ",
3410 ciphers[i].name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003411 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003412 return pos - buf;
3413 pos += ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003414 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415 }
3416
3417 return pos - buf;
3418}
3419
3420
3421static int ctrl_iface_get_capability_group(int res, char *strict,
3422 struct wpa_driver_capa *capa,
3423 char *buf, size_t buflen)
3424{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003425 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426 char *pos, *end;
3427 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003428 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003429
3430 pos = buf;
3431 end = pos + buflen;
3432
3433 if (res < 0) {
3434 if (strict)
3435 return 0;
3436 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
3437 if (len >= buflen)
3438 return -1;
3439 return len;
3440 }
3441
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003442 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3443 if (capa->enc & ciphers[i].capa) {
3444 ret = os_snprintf(pos, end - pos, "%s%s",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003445 pos == buf ? "" : " ",
3446 ciphers[i].name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003447 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003448 return pos - buf;
3449 pos += ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003450 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003451 }
3452
3453 return pos - buf;
3454}
3455
3456
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003457static int ctrl_iface_get_capability_group_mgmt(int res, char *strict,
3458 struct wpa_driver_capa *capa,
3459 char *buf, size_t buflen)
3460{
3461 int ret;
3462 char *pos, *end;
3463 unsigned int i;
3464
3465 pos = buf;
3466 end = pos + buflen;
3467
3468 if (res < 0)
3469 return 0;
3470
3471 for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
3472 if (capa->enc & ciphers_group_mgmt[i].capa) {
3473 ret = os_snprintf(pos, end - pos, "%s%s",
3474 pos == buf ? "" : " ",
3475 ciphers_group_mgmt[i].name);
3476 if (os_snprintf_error(end - pos, ret))
3477 return pos - buf;
3478 pos += ret;
3479 }
3480 }
3481
3482 return pos - buf;
3483}
3484
3485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003486static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
3487 struct wpa_driver_capa *capa,
3488 char *buf, size_t buflen)
3489{
3490 int ret;
3491 char *pos, *end;
3492 size_t len;
3493
3494 pos = buf;
3495 end = pos + buflen;
3496
3497 if (res < 0) {
3498 if (strict)
3499 return 0;
3500 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
3501 "NONE", buflen);
3502 if (len >= buflen)
3503 return -1;
3504 return len;
3505 }
3506
3507 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003508 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003509 return pos - buf;
3510 pos += ret;
3511
3512 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3513 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
3514 ret = os_snprintf(pos, end - pos, " WPA-EAP");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003515 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003516 return pos - buf;
3517 pos += ret;
3518 }
3519
3520 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3521 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
3522 ret = os_snprintf(pos, end - pos, " WPA-PSK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003523 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003524 return pos - buf;
3525 pos += ret;
3526 }
3527
3528 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
3529 ret = os_snprintf(pos, end - pos, " WPA-NONE");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003530 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003531 return pos - buf;
3532 pos += ret;
3533 }
3534
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003535#ifdef CONFIG_SUITEB
3536 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
3537 ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
3538 if (os_snprintf_error(end - pos, ret))
3539 return pos - buf;
3540 pos += ret;
3541 }
3542#endif /* CONFIG_SUITEB */
3543#ifdef CONFIG_SUITEB192
3544 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
3545 ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
3546 if (os_snprintf_error(end - pos, ret))
3547 return pos - buf;
3548 pos += ret;
3549 }
3550#endif /* CONFIG_SUITEB192 */
3551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552 return pos - buf;
3553}
3554
3555
3556static int ctrl_iface_get_capability_proto(int res, char *strict,
3557 struct wpa_driver_capa *capa,
3558 char *buf, size_t buflen)
3559{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003560 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561 char *pos, *end;
3562 size_t len;
3563
3564 pos = buf;
3565 end = pos + buflen;
3566
3567 if (res < 0) {
3568 if (strict)
3569 return 0;
3570 len = os_strlcpy(buf, "RSN WPA", buflen);
3571 if (len >= buflen)
3572 return -1;
3573 return len;
3574 }
3575
3576 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3577 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003578 ret = os_snprintf(pos, end - pos, "%sRSN",
3579 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003580 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003581 return pos - buf;
3582 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583 }
3584
3585 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3586 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003587 ret = os_snprintf(pos, end - pos, "%sWPA",
3588 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003589 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590 return pos - buf;
3591 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003592 }
3593
3594 return pos - buf;
3595}
3596
3597
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003598static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
3599 int res, char *strict,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003600 struct wpa_driver_capa *capa,
3601 char *buf, size_t buflen)
3602{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003603 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604 char *pos, *end;
3605 size_t len;
3606
3607 pos = buf;
3608 end = pos + buflen;
3609
3610 if (res < 0) {
3611 if (strict)
3612 return 0;
3613 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
3614 if (len >= buflen)
3615 return -1;
3616 return len;
3617 }
3618
3619 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003620 ret = os_snprintf(pos, end - pos, "%sOPEN",
3621 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003622 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623 return pos - buf;
3624 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003625 }
3626
3627 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
3628 ret = os_snprintf(pos, end - pos, "%sSHARED",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003629 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003630 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003631 return pos - buf;
3632 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003633 }
3634
3635 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003636 ret = os_snprintf(pos, end - pos, "%sLEAP",
3637 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003638 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003639 return pos - buf;
3640 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003641 }
3642
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003643#ifdef CONFIG_SAE
3644 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
3645 ret = os_snprintf(pos, end - pos, "%sSAE",
3646 pos == buf ? "" : " ");
3647 if (os_snprintf_error(end - pos, ret))
3648 return pos - buf;
3649 pos += ret;
3650 }
3651#endif /* CONFIG_SAE */
3652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003653 return pos - buf;
3654}
3655
3656
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003657static int ctrl_iface_get_capability_modes(int res, char *strict,
3658 struct wpa_driver_capa *capa,
3659 char *buf, size_t buflen)
3660{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003661 int ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003662 char *pos, *end;
3663 size_t len;
3664
3665 pos = buf;
3666 end = pos + buflen;
3667
3668 if (res < 0) {
3669 if (strict)
3670 return 0;
3671 len = os_strlcpy(buf, "IBSS AP", buflen);
3672 if (len >= buflen)
3673 return -1;
3674 return len;
3675 }
3676
3677 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003678 ret = os_snprintf(pos, end - pos, "%sIBSS",
3679 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003680 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003681 return pos - buf;
3682 pos += ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003683 }
3684
3685 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003686 ret = os_snprintf(pos, end - pos, "%sAP",
3687 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003688 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003689 return pos - buf;
3690 pos += ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003691 }
3692
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003693#ifdef CONFIG_MESH
3694 if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
3695 ret = os_snprintf(pos, end - pos, "%sMESH",
3696 pos == buf ? "" : " ");
3697 if (os_snprintf_error(end - pos, ret))
3698 return pos - buf;
3699 pos += ret;
3700 }
3701#endif /* CONFIG_MESH */
3702
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003703 return pos - buf;
3704}
3705
3706
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003707static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
3708 char *buf, size_t buflen)
3709{
3710 struct hostapd_channel_data *chnl;
3711 int ret, i, j;
3712 char *pos, *end, *hmode;
3713
3714 pos = buf;
3715 end = pos + buflen;
3716
3717 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3718 switch (wpa_s->hw.modes[j].mode) {
3719 case HOSTAPD_MODE_IEEE80211B:
3720 hmode = "B";
3721 break;
3722 case HOSTAPD_MODE_IEEE80211G:
3723 hmode = "G";
3724 break;
3725 case HOSTAPD_MODE_IEEE80211A:
3726 hmode = "A";
3727 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003728 case HOSTAPD_MODE_IEEE80211AD:
3729 hmode = "AD";
3730 break;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003731 default:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003732 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003733 }
3734 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003735 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003736 return pos - buf;
3737 pos += ret;
3738 chnl = wpa_s->hw.modes[j].channels;
3739 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003740 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3741 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003742 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003743 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003744 return pos - buf;
3745 pos += ret;
3746 }
3747 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003748 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003749 return pos - buf;
3750 pos += ret;
3751 }
3752
3753 return pos - buf;
3754}
3755
3756
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003757static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3758 char *buf, size_t buflen)
3759{
3760 struct hostapd_channel_data *chnl;
3761 int ret, i, j;
3762 char *pos, *end, *hmode;
3763
3764 pos = buf;
3765 end = pos + buflen;
3766
3767 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3768 switch (wpa_s->hw.modes[j].mode) {
3769 case HOSTAPD_MODE_IEEE80211B:
3770 hmode = "B";
3771 break;
3772 case HOSTAPD_MODE_IEEE80211G:
3773 hmode = "G";
3774 break;
3775 case HOSTAPD_MODE_IEEE80211A:
3776 hmode = "A";
3777 break;
3778 case HOSTAPD_MODE_IEEE80211AD:
3779 hmode = "AD";
3780 break;
3781 default:
3782 continue;
3783 }
3784 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3785 hmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003786 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003787 return pos - buf;
3788 pos += ret;
3789 chnl = wpa_s->hw.modes[j].channels;
3790 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3791 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3792 continue;
Dmitry Shmidt5da5e352014-02-03 13:30:46 -08003793 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003794 chnl[i].chan, chnl[i].freq,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003795 chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
3796 " (NO_IR)" : "",
Dmitry Shmidt5da5e352014-02-03 13:30:46 -08003797 chnl[i].flag & HOSTAPD_CHAN_RADAR ?
3798 " (DFS)" : "");
3799
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003800 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003801 return pos - buf;
3802 pos += ret;
3803 }
3804 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003805 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003806 return pos - buf;
3807 pos += ret;
3808 }
3809
3810 return pos - buf;
3811}
3812
3813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003814static int wpa_supplicant_ctrl_iface_get_capability(
3815 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3816 size_t buflen)
3817{
3818 struct wpa_driver_capa capa;
3819 int res;
3820 char *strict;
3821 char field[30];
3822 size_t len;
3823
3824 /* Determine whether or not strict checking was requested */
3825 len = os_strlcpy(field, _field, sizeof(field));
3826 if (len >= sizeof(field))
3827 return -1;
3828 strict = os_strchr(field, ' ');
3829 if (strict != NULL) {
3830 *strict++ = '\0';
3831 if (os_strcmp(strict, "strict") != 0)
3832 return -1;
3833 }
3834
3835 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3836 field, strict ? strict : "");
3837
3838 if (os_strcmp(field, "eap") == 0) {
3839 return eap_get_names(buf, buflen);
3840 }
3841
3842 res = wpa_drv_get_capa(wpa_s, &capa);
3843
3844 if (os_strcmp(field, "pairwise") == 0)
3845 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3846 buf, buflen);
3847
3848 if (os_strcmp(field, "group") == 0)
3849 return ctrl_iface_get_capability_group(res, strict, &capa,
3850 buf, buflen);
3851
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003852 if (os_strcmp(field, "group_mgmt") == 0)
3853 return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
3854 buf, buflen);
3855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856 if (os_strcmp(field, "key_mgmt") == 0)
3857 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3858 buf, buflen);
3859
3860 if (os_strcmp(field, "proto") == 0)
3861 return ctrl_iface_get_capability_proto(res, strict, &capa,
3862 buf, buflen);
3863
3864 if (os_strcmp(field, "auth_alg") == 0)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003865 return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
3866 &capa, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003867
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003868 if (os_strcmp(field, "modes") == 0)
3869 return ctrl_iface_get_capability_modes(res, strict, &capa,
3870 buf, buflen);
3871
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003872 if (os_strcmp(field, "channels") == 0)
3873 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3874
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003875 if (os_strcmp(field, "freq") == 0)
3876 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3877
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003878#ifdef CONFIG_TDLS
3879 if (os_strcmp(field, "tdls") == 0)
3880 return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
3881#endif /* CONFIG_TDLS */
3882
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003883#ifdef CONFIG_ERP
3884 if (os_strcmp(field, "erp") == 0) {
3885 res = os_snprintf(buf, buflen, "ERP");
3886 if (os_snprintf_error(buflen, res))
3887 return -1;
3888 return res;
3889 }
3890#endif /* CONFIG_EPR */
3891
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003892 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3893 field);
3894
3895 return -1;
3896}
3897
3898
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003899#ifdef CONFIG_INTERWORKING
3900static char * anqp_add_hex(char *pos, char *end, const char *title,
3901 struct wpabuf *data)
3902{
3903 char *start = pos;
3904 size_t i;
3905 int ret;
3906 const u8 *d;
3907
3908 if (data == NULL)
3909 return start;
3910
3911 ret = os_snprintf(pos, end - pos, "%s=", title);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003912 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003913 return start;
3914 pos += ret;
3915
3916 d = wpabuf_head_u8(data);
3917 for (i = 0; i < wpabuf_len(data); i++) {
3918 ret = os_snprintf(pos, end - pos, "%02x", *d++);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003919 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003920 return start;
3921 pos += ret;
3922 }
3923
3924 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003925 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003926 return start;
3927 pos += ret;
3928
3929 return pos;
3930}
3931#endif /* CONFIG_INTERWORKING */
3932
3933
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003934static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3935 unsigned long mask, char *buf, size_t buflen)
3936{
3937 size_t i;
3938 int ret;
3939 char *pos, *end;
3940 const u8 *ie, *ie2;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003941
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003942 pos = buf;
3943 end = buf + buflen;
3944
3945 if (mask & WPA_BSS_MASK_ID) {
3946 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003947 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003948 return 0;
3949 pos += ret;
3950 }
3951
3952 if (mask & WPA_BSS_MASK_BSSID) {
3953 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3954 MAC2STR(bss->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003955 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003956 return 0;
3957 pos += ret;
3958 }
3959
3960 if (mask & WPA_BSS_MASK_FREQ) {
3961 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003962 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003963 return 0;
3964 pos += ret;
3965 }
3966
3967 if (mask & WPA_BSS_MASK_BEACON_INT) {
3968 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3969 bss->beacon_int);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003970 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003971 return 0;
3972 pos += ret;
3973 }
3974
3975 if (mask & WPA_BSS_MASK_CAPABILITIES) {
3976 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3977 bss->caps);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003978 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003979 return 0;
3980 pos += ret;
3981 }
3982
3983 if (mask & WPA_BSS_MASK_QUAL) {
3984 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003985 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003986 return 0;
3987 pos += ret;
3988 }
3989
3990 if (mask & WPA_BSS_MASK_NOISE) {
3991 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003992 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003993 return 0;
3994 pos += ret;
3995 }
3996
3997 if (mask & WPA_BSS_MASK_LEVEL) {
3998 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003999 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004000 return 0;
4001 pos += ret;
4002 }
4003
4004 if (mask & WPA_BSS_MASK_TSF) {
4005 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
4006 (unsigned long long) bss->tsf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004007 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004008 return 0;
4009 pos += ret;
4010 }
4011
4012 if (mask & WPA_BSS_MASK_AGE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004013 struct os_reltime now;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004014
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004015 os_get_reltime(&now);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004016 ret = os_snprintf(pos, end - pos, "age=%d\n",
4017 (int) (now.sec - bss->last_update.sec));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004018 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004019 return 0;
4020 pos += ret;
4021 }
4022
4023 if (mask & WPA_BSS_MASK_IE) {
4024 ret = os_snprintf(pos, end - pos, "ie=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004025 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004026 return 0;
4027 pos += ret;
4028
4029 ie = (const u8 *) (bss + 1);
4030 for (i = 0; i < bss->ie_len; i++) {
4031 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004032 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004033 return 0;
4034 pos += ret;
4035 }
4036
4037 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004038 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004039 return 0;
4040 pos += ret;
4041 }
4042
4043 if (mask & WPA_BSS_MASK_FLAGS) {
4044 ret = os_snprintf(pos, end - pos, "flags=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004045 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004046 return 0;
4047 pos += ret;
4048
4049 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
4050 if (ie)
4051 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
4052 2 + ie[1]);
4053 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
4054 if (ie2)
4055 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
4056 2 + ie2[1]);
4057 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
4058 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
4059 ret = os_snprintf(pos, end - pos, "[WEP]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004060 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004061 return 0;
4062 pos += ret;
4063 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004064 if (bss_is_dmg(bss)) {
4065 const char *s;
4066 ret = os_snprintf(pos, end - pos, "[DMG]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004067 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004068 return 0;
4069 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004070 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
4071 case IEEE80211_CAP_DMG_IBSS:
4072 s = "[IBSS]";
4073 break;
4074 case IEEE80211_CAP_DMG_AP:
4075 s = "[ESS]";
4076 break;
4077 case IEEE80211_CAP_DMG_PBSS:
4078 s = "[PBSS]";
4079 break;
4080 default:
4081 s = "";
4082 break;
4083 }
4084 ret = os_snprintf(pos, end - pos, "%s", s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004085 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004086 return 0;
4087 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004088 } else {
4089 if (bss->caps & IEEE80211_CAP_IBSS) {
4090 ret = os_snprintf(pos, end - pos, "[IBSS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004091 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004092 return 0;
4093 pos += ret;
4094 }
4095 if (bss->caps & IEEE80211_CAP_ESS) {
4096 ret = os_snprintf(pos, end - pos, "[ESS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004097 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004098 return 0;
4099 pos += ret;
4100 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004101 }
Dmitry Shmidt96571392013-10-14 12:54:46 -07004102 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
4103 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004104 ret = os_snprintf(pos, end - pos, "[P2P]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004105 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004106 return 0;
4107 pos += ret;
4108 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004109#ifdef CONFIG_HS20
4110 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
4111 ret = os_snprintf(pos, end - pos, "[HS20]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004112 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004113 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004114 pos += ret;
4115 }
4116#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004117
4118 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004119 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004120 return 0;
4121 pos += ret;
4122 }
4123
4124 if (mask & WPA_BSS_MASK_SSID) {
4125 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
4126 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004127 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004128 return 0;
4129 pos += ret;
4130 }
4131
4132#ifdef CONFIG_WPS
4133 if (mask & WPA_BSS_MASK_WPS_SCAN) {
4134 ie = (const u8 *) (bss + 1);
4135 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
4136 if (ret < 0 || ret >= end - pos)
4137 return 0;
4138 pos += ret;
4139 }
4140#endif /* CONFIG_WPS */
4141
4142#ifdef CONFIG_P2P
4143 if (mask & WPA_BSS_MASK_P2P_SCAN) {
4144 ie = (const u8 *) (bss + 1);
4145 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
4146 if (ret < 0 || ret >= end - pos)
4147 return 0;
4148 pos += ret;
4149 }
4150#endif /* CONFIG_P2P */
4151
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004152#ifdef CONFIG_WIFI_DISPLAY
4153 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
4154 struct wpabuf *wfd;
4155 ie = (const u8 *) (bss + 1);
4156 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
4157 WFD_IE_VENDOR_TYPE);
4158 if (wfd) {
4159 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004160 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004161 wpabuf_free(wfd);
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004162 return 0;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004163 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004164 pos += ret;
4165
4166 pos += wpa_snprintf_hex(pos, end - pos,
4167 wpabuf_head(wfd),
4168 wpabuf_len(wfd));
4169 wpabuf_free(wfd);
4170
4171 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004172 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004173 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004174 pos += ret;
4175 }
4176 }
4177#endif /* CONFIG_WIFI_DISPLAY */
4178
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004179#ifdef CONFIG_INTERWORKING
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004180 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
4181 struct wpa_bss_anqp *anqp = bss->anqp;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004182 pos = anqp_add_hex(pos, end, "anqp_capability_list",
4183 anqp->capability_list);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004184 pos = anqp_add_hex(pos, end, "anqp_venue_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004185 anqp->venue_name);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004186 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004187 anqp->network_auth_type);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004188 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004189 anqp->roaming_consortium);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004190 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004191 anqp->ip_addr_type_availability);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004192 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004193 anqp->nai_realm);
4194 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004195 pos = anqp_add_hex(pos, end, "anqp_domain_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004196 anqp->domain_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004197#ifdef CONFIG_HS20
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004198 pos = anqp_add_hex(pos, end, "hs20_capability_list",
4199 anqp->hs20_capability_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004200 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004201 anqp->hs20_operator_friendly_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004202 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004203 anqp->hs20_wan_metrics);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004204 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004205 anqp->hs20_connection_capability);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004206 pos = anqp_add_hex(pos, end, "hs20_operating_class",
4207 anqp->hs20_operating_class);
4208 pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
4209 anqp->hs20_osu_providers_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004210#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004211 }
4212#endif /* CONFIG_INTERWORKING */
4213
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004214#ifdef CONFIG_MESH
4215 if (mask & WPA_BSS_MASK_MESH_SCAN) {
4216 ie = (const u8 *) (bss + 1);
4217 ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
4218 if (ret < 0 || ret >= end - pos)
4219 return 0;
4220 pos += ret;
4221 }
4222#endif /* CONFIG_MESH */
4223
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004224 if (mask & WPA_BSS_MASK_SNR) {
4225 ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
4226 if (os_snprintf_error(end - pos, ret))
4227 return 0;
4228 pos += ret;
4229 }
4230
4231 if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
4232 ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
4233 bss->est_throughput);
4234 if (os_snprintf_error(end - pos, ret))
4235 return 0;
4236 pos += ret;
4237 }
4238
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004239 if (mask & WPA_BSS_MASK_DELIM) {
4240 ret = os_snprintf(pos, end - pos, "====\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004241 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004242 return 0;
4243 pos += ret;
4244 }
Irfan Sheriffe2ea0082012-08-13 10:56:16 -07004245
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004246 return pos - buf;
4247}
4248
Dmitry Shmidt04949592012-07-19 12:16:46 -07004249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
4251 const char *cmd, char *buf,
4252 size_t buflen)
4253{
4254 u8 bssid[ETH_ALEN];
4255 size_t i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004256 struct wpa_bss *bss;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004257 struct wpa_bss *bsslast = NULL;
4258 struct dl_list *next;
4259 int ret = 0;
4260 int len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004261 char *ctmp, *end = buf + buflen;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004262 unsigned long mask = WPA_BSS_MASK_ALL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004264 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
4265 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
4266 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004267 list_id);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004268 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
4269 list_id);
4270 } else { /* N1-N2 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004271 unsigned int id1, id2;
4272
4273 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
4274 wpa_printf(MSG_INFO, "Wrong BSS range "
4275 "format");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004276 return 0;
4277 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004278
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004279 if (*(cmd + 6) == '-')
4280 id1 = 0;
4281 else
4282 id1 = atoi(cmd + 6);
4283 ctmp++;
4284 if (*ctmp >= '0' && *ctmp <= '9')
4285 id2 = atoi(ctmp);
4286 else
4287 id2 = (unsigned int) -1;
4288 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
4289 if (id2 == (unsigned int) -1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004290 bsslast = dl_list_last(&wpa_s->bss_id,
4291 struct wpa_bss,
4292 list_id);
4293 else {
4294 bsslast = wpa_bss_get_id(wpa_s, id2);
4295 if (bsslast == NULL && bss && id2 > id1) {
4296 struct wpa_bss *tmp = bss;
4297 for (;;) {
4298 next = tmp->list_id.next;
4299 if (next == &wpa_s->bss_id)
4300 break;
4301 tmp = dl_list_entry(
4302 next, struct wpa_bss,
4303 list_id);
4304 if (tmp->id > id2)
4305 break;
4306 bsslast = tmp;
4307 }
4308 }
4309 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004310 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004311 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004312 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004313 else if (os_strncmp(cmd, "LAST", 4) == 0)
4314 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004315 else if (os_strncmp(cmd, "ID-", 3) == 0) {
4316 i = atoi(cmd + 3);
4317 bss = wpa_bss_get_id(wpa_s, i);
4318 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4319 i = atoi(cmd + 5);
4320 bss = wpa_bss_get_id(wpa_s, i);
4321 if (bss) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004322 next = bss->list_id.next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004323 if (next == &wpa_s->bss_id)
4324 bss = NULL;
4325 else
4326 bss = dl_list_entry(next, struct wpa_bss,
4327 list_id);
4328 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004329#ifdef CONFIG_P2P
4330 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
4331 if (hwaddr_aton(cmd + 13, bssid) == 0)
4332 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
4333 else
4334 bss = NULL;
4335#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004336 } else if (hwaddr_aton(cmd, bssid) == 0)
4337 bss = wpa_bss_get_bssid(wpa_s, bssid);
4338 else {
4339 struct wpa_bss *tmp;
4340 i = atoi(cmd);
4341 bss = NULL;
4342 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
4343 {
4344 if (i-- == 0) {
4345 bss = tmp;
4346 break;
4347 }
4348 }
4349 }
4350
Dmitry Shmidt04949592012-07-19 12:16:46 -07004351 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
4352 mask = strtoul(ctmp + 5, NULL, 0x10);
4353 if (mask == 0)
4354 mask = WPA_BSS_MASK_ALL;
4355 }
4356
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004357 if (bss == NULL)
4358 return 0;
4359
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004360 if (bsslast == NULL)
4361 bsslast = bss;
4362 do {
4363 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
4364 ret += len;
4365 buf += len;
4366 buflen -= len;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004367 if (bss == bsslast) {
4368 if ((mask & WPA_BSS_MASK_DELIM) && len &&
4369 (bss == dl_list_last(&wpa_s->bss_id,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004370 struct wpa_bss, list_id))) {
4371 int res;
4372
4373 res = os_snprintf(buf - 5, end - buf + 5,
4374 "####\n");
4375 if (os_snprintf_error(end - buf + 5, res)) {
4376 wpa_printf(MSG_DEBUG,
4377 "Could not add end delim");
4378 }
4379 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004380 break;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004381 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004382 next = bss->list_id.next;
4383 if (next == &wpa_s->bss_id)
4384 break;
4385 bss = dl_list_entry(next, struct wpa_bss, list_id);
4386 } while (bss && len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004387
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004388 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389}
4390
4391
4392static int wpa_supplicant_ctrl_iface_ap_scan(
4393 struct wpa_supplicant *wpa_s, char *cmd)
4394{
4395 int ap_scan = atoi(cmd);
4396 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
4397}
4398
4399
4400static int wpa_supplicant_ctrl_iface_scan_interval(
4401 struct wpa_supplicant *wpa_s, char *cmd)
4402{
4403 int scan_int = atoi(cmd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004404 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004405}
4406
4407
4408static int wpa_supplicant_ctrl_iface_bss_expire_age(
4409 struct wpa_supplicant *wpa_s, char *cmd)
4410{
4411 int expire_age = atoi(cmd);
4412 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
4413}
4414
4415
4416static int wpa_supplicant_ctrl_iface_bss_expire_count(
4417 struct wpa_supplicant *wpa_s, char *cmd)
4418{
4419 int expire_count = atoi(cmd);
4420 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
4421}
4422
4423
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004424static void wpa_supplicant_ctrl_iface_bss_flush(
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07004425 struct wpa_supplicant *wpa_s, char *cmd)
4426{
4427 int flush_age = atoi(cmd);
4428
4429 if (flush_age == 0)
4430 wpa_bss_flush(wpa_s);
4431 else
4432 wpa_bss_flush_by_age(wpa_s, flush_age);
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07004433}
4434
4435
Dmitry Shmidt21de2142014-04-08 10:50:52 -07004436#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004437static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
4438{
4439 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
4440 /* MLME-DELETEKEYS.request */
4441 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
4442 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
4443 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
4444 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
4445#ifdef CONFIG_IEEE80211W
4446 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
4447 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
4448#endif /* CONFIG_IEEE80211W */
4449
4450 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
4451 0);
4452 /* MLME-SETPROTECTION.request(None) */
4453 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
4454 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
4455 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
4456 wpa_sm_drop_sa(wpa_s->wpa);
4457}
Dmitry Shmidt21de2142014-04-08 10:50:52 -07004458#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004459
4460
4461static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
4462 char *addr)
4463{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004464#ifdef CONFIG_NO_SCAN_PROCESSING
4465 return -1;
4466#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004467 u8 bssid[ETH_ALEN];
4468 struct wpa_bss *bss;
4469 struct wpa_ssid *ssid = wpa_s->current_ssid;
4470
4471 if (hwaddr_aton(addr, bssid)) {
4472 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
4473 "address '%s'", addr);
4474 return -1;
4475 }
4476
4477 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
4478
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004479 if (!ssid) {
4480 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
4481 "configuration known for the target AP");
4482 return -1;
4483 }
4484
4485 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004486 if (!bss) {
4487 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
4488 "from BSS table");
4489 return -1;
4490 }
4491
4492 /*
4493 * TODO: Find best network configuration block from configuration to
4494 * allow roaming to other networks
4495 */
4496
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497 wpa_s->reassociate = 1;
4498 wpa_supplicant_connect(wpa_s, bss, ssid);
4499
4500 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004501#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004502}
4503
4504
4505#ifdef CONFIG_P2P
4506static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
4507{
4508 unsigned int timeout = atoi(cmd);
4509 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004510 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004511 u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004512 char *pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004513 unsigned int search_delay;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004514 const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004515 u8 seek_count = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004516 int freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004517
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004518 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4519 wpa_dbg(wpa_s, MSG_INFO,
4520 "Reject P2P_FIND since interface is disabled");
4521 return -1;
4522 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004523 if (os_strstr(cmd, "type=social"))
4524 type = P2P_FIND_ONLY_SOCIAL;
4525 else if (os_strstr(cmd, "type=progressive"))
4526 type = P2P_FIND_PROGRESSIVE;
4527
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004528 pos = os_strstr(cmd, "dev_id=");
4529 if (pos) {
4530 pos += 7;
4531 if (hwaddr_aton(pos, dev_id))
4532 return -1;
4533 _dev_id = dev_id;
4534 }
4535
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004536 pos = os_strstr(cmd, "dev_type=");
4537 if (pos) {
4538 pos += 9;
4539 if (wps_dev_type_str2bin(pos, dev_type) < 0)
4540 return -1;
4541 _dev_type = dev_type;
4542 }
4543
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004544 pos = os_strstr(cmd, "delay=");
4545 if (pos) {
4546 pos += 6;
4547 search_delay = atoi(pos);
4548 } else
4549 search_delay = wpas_p2p_search_delay(wpa_s);
4550
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004551 /* Must be searched for last, because it adds nul termination */
4552 pos = os_strstr(cmd, " seek=");
4553 while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
4554 char *term;
4555
4556 term = os_strchr(pos + 1, ' ');
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004557 _seek[seek_count++] = pos + 6;
4558 seek = _seek;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004559 pos = os_strstr(pos + 6, " seek=");
4560
4561 if (term)
4562 *term = '\0';
4563 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004564 if (seek_count > P2P_MAX_QUERY_HASH) {
4565 seek[0] = NULL;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004566 seek_count = 1;
4567 }
4568
4569 pos = os_strstr(cmd, "freq=");
4570 if (pos) {
4571 pos += 5;
4572 freq = atoi(pos);
4573 if (freq <= 0)
4574 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004575 }
4576
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004577 return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004578 _dev_id, search_delay, seek_count, seek, freq);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004579}
4580
4581
4582static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
4583{
4584 struct p2ps_provision *p2ps_prov;
4585 char *pos;
4586 size_t info_len = 0;
4587 char *info = NULL;
4588 u8 role = P2PS_SETUP_NONE;
4589 long long unsigned val;
4590
4591 pos = os_strstr(cmd, "info=");
4592 if (pos) {
4593 pos += 5;
4594 info_len = os_strlen(pos);
4595
4596 if (info_len) {
4597 info = os_malloc(info_len + 1);
4598 if (info) {
4599 info_len = utf8_unescape(pos, info_len,
4600 info, info_len + 1);
4601 } else
4602 info_len = 0;
4603 }
4604 }
4605
4606 p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
4607 if (p2ps_prov == NULL) {
4608 os_free(info);
4609 return NULL;
4610 }
4611
4612 if (info) {
4613 os_memcpy(p2ps_prov->info, info, info_len);
4614 p2ps_prov->info[info_len] = '\0';
4615 os_free(info);
4616 }
4617
4618 pos = os_strstr(cmd, "status=");
4619 if (pos)
4620 p2ps_prov->status = atoi(pos + 7);
4621 else
4622 p2ps_prov->status = -1;
4623
4624 pos = os_strstr(cmd, "adv_id=");
4625 if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
4626 goto invalid_args;
4627 p2ps_prov->adv_id = val;
4628
4629 pos = os_strstr(cmd, "method=");
4630 if (pos)
4631 p2ps_prov->method = strtol(pos + 7, NULL, 16);
4632 else
4633 p2ps_prov->method = 0;
4634
4635 pos = os_strstr(cmd, "session=");
4636 if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
4637 goto invalid_args;
4638 p2ps_prov->session_id = val;
4639
4640 pos = os_strstr(cmd, "adv_mac=");
4641 if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
4642 goto invalid_args;
4643
4644 pos = os_strstr(cmd, "session_mac=");
4645 if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
4646 goto invalid_args;
4647
4648 /* force conncap with tstCap (no sanity checks) */
4649 pos = os_strstr(cmd, "tstCap=");
4650 if (pos) {
4651 role = strtol(pos + 7, NULL, 16);
4652 } else {
4653 pos = os_strstr(cmd, "role=");
4654 if (pos) {
4655 role = strtol(pos + 5, NULL, 16);
4656 if (role != P2PS_SETUP_CLIENT &&
4657 role != P2PS_SETUP_GROUP_OWNER)
4658 role = P2PS_SETUP_NONE;
4659 }
4660 }
4661 p2ps_prov->role = role;
4662
4663 return p2ps_prov;
4664
4665invalid_args:
4666 os_free(p2ps_prov);
4667 return NULL;
4668}
4669
4670
4671static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
4672{
4673 u8 addr[ETH_ALEN];
4674 struct p2ps_provision *p2ps_prov;
4675 char *pos;
4676
4677 /* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
4678
4679 wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
4680
4681 if (hwaddr_aton(cmd, addr))
4682 return -1;
4683
4684 pos = cmd + 17;
4685 if (*pos != ' ')
4686 return -1;
4687
4688 p2ps_prov = p2p_parse_asp_provision_cmd(pos);
4689 if (!p2ps_prov)
4690 return -1;
4691
4692 if (p2ps_prov->status < 0) {
4693 os_free(p2ps_prov);
4694 return -1;
4695 }
4696
4697 return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
4698 p2ps_prov);
4699}
4700
4701
4702static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
4703{
4704 u8 addr[ETH_ALEN];
4705 struct p2ps_provision *p2ps_prov;
4706 char *pos;
4707
4708 /* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
4709 * session=<ses_id> mac=<ses_mac> [info=<infodata>]
4710 */
4711
4712 wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
4713 if (hwaddr_aton(cmd, addr))
4714 return -1;
4715
4716 pos = cmd + 17;
4717 if (*pos != ' ')
4718 return -1;
4719
4720 p2ps_prov = p2p_parse_asp_provision_cmd(pos);
4721 if (!p2ps_prov)
4722 return -1;
4723
4724 return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
4725 p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726}
4727
4728
4729static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
4730 char *buf, size_t buflen)
4731{
4732 u8 addr[ETH_ALEN];
4733 char *pos, *pos2;
4734 char *pin = NULL;
4735 enum p2p_wps_method wps_method;
4736 int new_pin;
4737 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004738 int persistent_group, persistent_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004739 int join;
4740 int auth;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004741 int automatic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004742 int go_intent = -1;
4743 int freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004744 int pd;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004745 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004746
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004747 if (!wpa_s->global->p2p_init_wpa_s)
4748 return -1;
4749 if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
4750 wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
4751 wpa_s->global->p2p_init_wpa_s->ifname);
4752 wpa_s = wpa_s->global->p2p_init_wpa_s;
4753 }
4754
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004755 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
Dmitry Shmidt04949592012-07-19 12:16:46 -07004756 * [persistent|persistent=<network id>]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004757 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004758 * [ht40] [vht] [auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004759
4760 if (hwaddr_aton(cmd, addr))
4761 return -1;
4762
4763 pos = cmd + 17;
4764 if (*pos != ' ')
4765 return -1;
4766 pos++;
4767
4768 persistent_group = os_strstr(pos, " persistent") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004769 pos2 = os_strstr(pos, " persistent=");
4770 if (pos2) {
4771 struct wpa_ssid *ssid;
4772 persistent_id = atoi(pos2 + 12);
4773 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4774 if (ssid == NULL || ssid->disabled != 2 ||
4775 ssid->mode != WPAS_MODE_P2P_GO) {
4776 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
4777 "SSID id=%d for persistent P2P group (GO)",
4778 persistent_id);
4779 return -1;
4780 }
4781 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004782 join = os_strstr(pos, " join") != NULL;
4783 auth = os_strstr(pos, " auth") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004784 automatic = os_strstr(pos, " auto") != NULL;
4785 pd = os_strstr(pos, " provdisc") != NULL;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004786 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4787 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4788 vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004789
4790 pos2 = os_strstr(pos, " go_intent=");
4791 if (pos2) {
4792 pos2 += 11;
4793 go_intent = atoi(pos2);
4794 if (go_intent < 0 || go_intent > 15)
4795 return -1;
4796 }
4797
4798 pos2 = os_strstr(pos, " freq=");
4799 if (pos2) {
4800 pos2 += 6;
4801 freq = atoi(pos2);
4802 if (freq <= 0)
4803 return -1;
4804 }
4805
4806 if (os_strncmp(pos, "pin", 3) == 0) {
4807 /* Request random PIN (to be displayed) and enable the PIN */
4808 wps_method = WPS_PIN_DISPLAY;
4809 } else if (os_strncmp(pos, "pbc", 3) == 0) {
4810 wps_method = WPS_PBC;
4811 } else {
4812 pin = pos;
4813 pos = os_strchr(pin, ' ');
4814 wps_method = WPS_PIN_KEYPAD;
4815 if (pos) {
4816 *pos++ = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004817 if (os_strncmp(pos, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004818 wps_method = WPS_PIN_DISPLAY;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004819 else if (os_strncmp(pos, "p2ps", 4) == 0)
4820 wps_method = WPS_P2PS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004821 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004822 if (!wps_pin_str_valid(pin)) {
4823 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
4824 return 17;
4825 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004826 }
4827
4828 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004829 persistent_group, automatic, join,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004830 auth, go_intent, freq, persistent_id, pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004831 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004832 if (new_pin == -2) {
4833 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
4834 return 25;
4835 }
4836 if (new_pin == -3) {
4837 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
4838 return 25;
4839 }
4840 if (new_pin < 0)
4841 return -1;
4842 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
4843 ret = os_snprintf(buf, buflen, "%08d", new_pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004844 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004845 return -1;
4846 return ret;
4847 }
4848
4849 os_memcpy(buf, "OK\n", 3);
4850 return 3;
4851}
4852
4853
4854static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
4855{
4856 unsigned int timeout = atoi(cmd);
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004857 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4858 wpa_dbg(wpa_s, MSG_INFO,
4859 "Reject P2P_LISTEN since interface is disabled");
4860 return -1;
4861 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004862 return wpas_p2p_listen(wpa_s, timeout);
4863}
4864
4865
4866static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
4867{
4868 u8 addr[ETH_ALEN];
4869 char *pos;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004870 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004871
Dmitry Shmidt04949592012-07-19 12:16:46 -07004872 /* <addr> <config method> [join|auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004873
4874 if (hwaddr_aton(cmd, addr))
4875 return -1;
4876
4877 pos = cmd + 17;
4878 if (*pos != ' ')
4879 return -1;
4880 pos++;
4881
Dmitry Shmidt04949592012-07-19 12:16:46 -07004882 if (os_strstr(pos, " join") != NULL)
4883 use = WPAS_P2P_PD_FOR_JOIN;
4884 else if (os_strstr(pos, " auto") != NULL)
4885 use = WPAS_P2P_PD_AUTO;
4886
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004887 return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888}
4889
4890
4891static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
4892 size_t buflen)
4893{
4894 struct wpa_ssid *ssid = wpa_s->current_ssid;
4895
4896 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
4897 ssid->passphrase == NULL)
4898 return -1;
4899
4900 os_strlcpy(buf, ssid->passphrase, buflen);
4901 return os_strlen(buf);
4902}
4903
4904
4905static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
4906 char *buf, size_t buflen)
4907{
4908 u64 ref;
4909 int res;
4910 u8 dst_buf[ETH_ALEN], *dst;
4911 struct wpabuf *tlvs;
4912 char *pos;
4913 size_t len;
4914
4915 if (hwaddr_aton(cmd, dst_buf))
4916 return -1;
4917 dst = dst_buf;
4918 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
4919 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
4920 dst = NULL;
4921 pos = cmd + 17;
4922 if (*pos != ' ')
4923 return -1;
4924 pos++;
4925
4926 if (os_strncmp(pos, "upnp ", 5) == 0) {
4927 u8 version;
4928 pos += 5;
4929 if (hexstr2bin(pos, &version, 1) < 0)
4930 return -1;
4931 pos += 2;
4932 if (*pos != ' ')
4933 return -1;
4934 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004935 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004936#ifdef CONFIG_WIFI_DISPLAY
4937 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
4938 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
4939#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004940 } else if (os_strncmp(pos, "asp ", 4) == 0) {
4941 char *svc_str;
4942 char *svc_info = NULL;
4943 u32 id;
4944
4945 pos += 4;
4946 if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
4947 return -1;
4948
4949 pos = os_strchr(pos, ' ');
4950 if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
4951 return -1;
4952
4953 svc_str = pos + 1;
4954
4955 pos = os_strchr(svc_str, ' ');
4956
4957 if (pos)
4958 *pos++ = '\0';
4959
4960 /* All remaining data is the svc_info string */
4961 if (pos && pos[0] && pos[0] != ' ') {
4962 len = os_strlen(pos);
4963
4964 /* Unescape in place */
4965 len = utf8_unescape(pos, len, pos, len);
4966 if (len > 0xff)
4967 return -1;
4968
4969 svc_info = pos;
4970 }
4971
4972 ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
4973 svc_str, svc_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004974 } else {
4975 len = os_strlen(pos);
4976 if (len & 1)
4977 return -1;
4978 len /= 2;
4979 tlvs = wpabuf_alloc(len);
4980 if (tlvs == NULL)
4981 return -1;
4982 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
4983 wpabuf_free(tlvs);
4984 return -1;
4985 }
4986
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004987 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004988 wpabuf_free(tlvs);
4989 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004990 if (ref == 0)
4991 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004992 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004993 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004994 return -1;
4995 return res;
4996}
4997
4998
4999static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
5000 char *cmd)
5001{
5002 long long unsigned val;
5003 u64 req;
5004 if (sscanf(cmd, "%llx", &val) != 1)
5005 return -1;
5006 req = val;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005007 return wpas_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008}
5009
5010
5011static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
5012{
5013 int freq;
5014 u8 dst[ETH_ALEN];
5015 u8 dialog_token;
5016 struct wpabuf *resp_tlvs;
5017 char *pos, *pos2;
5018 size_t len;
5019
5020 pos = os_strchr(cmd, ' ');
5021 if (pos == NULL)
5022 return -1;
5023 *pos++ = '\0';
5024 freq = atoi(cmd);
5025 if (freq == 0)
5026 return -1;
5027
5028 if (hwaddr_aton(pos, dst))
5029 return -1;
5030 pos += 17;
5031 if (*pos != ' ')
5032 return -1;
5033 pos++;
5034
5035 pos2 = os_strchr(pos, ' ');
5036 if (pos2 == NULL)
5037 return -1;
5038 *pos2++ = '\0';
5039 dialog_token = atoi(pos);
5040
5041 len = os_strlen(pos2);
5042 if (len & 1)
5043 return -1;
5044 len /= 2;
5045 resp_tlvs = wpabuf_alloc(len);
5046 if (resp_tlvs == NULL)
5047 return -1;
5048 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
5049 wpabuf_free(resp_tlvs);
5050 return -1;
5051 }
5052
5053 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
5054 wpabuf_free(resp_tlvs);
5055 return 0;
5056}
5057
5058
5059static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
5060 char *cmd)
5061{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005062 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
5063 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005064 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
5065 return 0;
5066}
5067
5068
5069static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
5070 char *cmd)
5071{
5072 char *pos;
5073 size_t len;
5074 struct wpabuf *query, *resp;
5075
5076 pos = os_strchr(cmd, ' ');
5077 if (pos == NULL)
5078 return -1;
5079 *pos++ = '\0';
5080
5081 len = os_strlen(cmd);
5082 if (len & 1)
5083 return -1;
5084 len /= 2;
5085 query = wpabuf_alloc(len);
5086 if (query == NULL)
5087 return -1;
5088 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
5089 wpabuf_free(query);
5090 return -1;
5091 }
5092
5093 len = os_strlen(pos);
5094 if (len & 1) {
5095 wpabuf_free(query);
5096 return -1;
5097 }
5098 len /= 2;
5099 resp = wpabuf_alloc(len);
5100 if (resp == NULL) {
5101 wpabuf_free(query);
5102 return -1;
5103 }
5104 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
5105 wpabuf_free(query);
5106 wpabuf_free(resp);
5107 return -1;
5108 }
5109
5110 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
5111 wpabuf_free(query);
5112 wpabuf_free(resp);
5113 return -1;
5114 }
5115 return 0;
5116}
5117
5118
5119static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
5120{
5121 char *pos;
5122 u8 version;
5123
5124 pos = os_strchr(cmd, ' ');
5125 if (pos == NULL)
5126 return -1;
5127 *pos++ = '\0';
5128
5129 if (hexstr2bin(cmd, &version, 1) < 0)
5130 return -1;
5131
5132 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
5133}
5134
5135
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005136static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
5137 u8 replace, char *cmd)
5138{
5139 char *pos;
5140 char *adv_str;
5141 u32 auto_accept, adv_id, svc_state, config_methods;
5142 char *svc_info = NULL;
5143
5144 pos = os_strchr(cmd, ' ');
5145 if (pos == NULL)
5146 return -1;
5147 *pos++ = '\0';
5148
5149 /* Auto-Accept value is mandatory, and must be one of the
5150 * single values (0, 1, 2, 4) */
5151 auto_accept = atoi(cmd);
5152 switch (auto_accept) {
5153 case P2PS_SETUP_NONE: /* No auto-accept */
5154 case P2PS_SETUP_NEW:
5155 case P2PS_SETUP_CLIENT:
5156 case P2PS_SETUP_GROUP_OWNER:
5157 break;
5158 default:
5159 return -1;
5160 }
5161
5162 /* Advertisement ID is mandatory */
5163 cmd = pos;
5164 pos = os_strchr(cmd, ' ');
5165 if (pos == NULL)
5166 return -1;
5167 *pos++ = '\0';
5168
5169 /* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
5170 if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
5171 return -1;
5172
5173 /* Only allow replacements if exist, and adds if not */
5174 if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
5175 if (!replace)
5176 return -1;
5177 } else {
5178 if (replace)
5179 return -1;
5180 }
5181
5182 /* svc_state between 0 - 0xff is mandatory */
5183 if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
5184 return -1;
5185
5186 pos = os_strchr(pos, ' ');
5187 if (pos == NULL)
5188 return -1;
5189
5190 /* config_methods is mandatory */
5191 pos++;
5192 if (sscanf(pos, "%x", &config_methods) != 1)
5193 return -1;
5194
5195 if (!(config_methods &
5196 (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
5197 return -1;
5198
5199 pos = os_strchr(pos, ' ');
5200 if (pos == NULL)
5201 return -1;
5202
5203 pos++;
5204 adv_str = pos;
5205
5206 /* Advertisement string is mandatory */
5207 if (!pos[0] || pos[0] == ' ')
5208 return -1;
5209
5210 /* Terminate svc string */
5211 pos = os_strchr(pos, ' ');
5212 if (pos != NULL)
5213 *pos++ = '\0';
5214
5215 /* Service and Response Information are optional */
5216 if (pos && pos[0]) {
5217 size_t len;
5218
5219 /* Note the bare ' included, which cannot exist legally
5220 * in unescaped string. */
5221 svc_info = os_strstr(pos, "svc_info='");
5222
5223 if (svc_info) {
5224 svc_info += 9;
5225 len = os_strlen(svc_info);
5226 utf8_unescape(svc_info, len, svc_info, len);
5227 }
5228 }
5229
5230 return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
5231 (u8) svc_state, (u16) config_methods,
5232 svc_info);
5233}
5234
5235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005236static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
5237{
5238 char *pos;
5239
5240 pos = os_strchr(cmd, ' ');
5241 if (pos == NULL)
5242 return -1;
5243 *pos++ = '\0';
5244
5245 if (os_strcmp(cmd, "bonjour") == 0)
5246 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
5247 if (os_strcmp(cmd, "upnp") == 0)
5248 return p2p_ctrl_service_add_upnp(wpa_s, pos);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005249 if (os_strcmp(cmd, "asp") == 0)
5250 return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005251 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
5252 return -1;
5253}
5254
5255
5256static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
5257 char *cmd)
5258{
5259 size_t len;
5260 struct wpabuf *query;
5261 int ret;
5262
5263 len = os_strlen(cmd);
5264 if (len & 1)
5265 return -1;
5266 len /= 2;
5267 query = wpabuf_alloc(len);
5268 if (query == NULL)
5269 return -1;
5270 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
5271 wpabuf_free(query);
5272 return -1;
5273 }
5274
5275 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
5276 wpabuf_free(query);
5277 return ret;
5278}
5279
5280
5281static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
5282{
5283 char *pos;
5284 u8 version;
5285
5286 pos = os_strchr(cmd, ' ');
5287 if (pos == NULL)
5288 return -1;
5289 *pos++ = '\0';
5290
5291 if (hexstr2bin(cmd, &version, 1) < 0)
5292 return -1;
5293
5294 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
5295}
5296
5297
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005298static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
5299{
5300 u32 adv_id;
5301
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07005302 if (os_strcmp(cmd, "all") == 0) {
5303 wpas_p2p_service_flush_asp(wpa_s);
5304 return 0;
5305 }
5306
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005307 if (sscanf(cmd, "%x", &adv_id) != 1)
5308 return -1;
5309
5310 return wpas_p2p_service_del_asp(wpa_s, adv_id);
5311}
5312
5313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005314static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
5315{
5316 char *pos;
5317
5318 pos = os_strchr(cmd, ' ');
5319 if (pos == NULL)
5320 return -1;
5321 *pos++ = '\0';
5322
5323 if (os_strcmp(cmd, "bonjour") == 0)
5324 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
5325 if (os_strcmp(cmd, "upnp") == 0)
5326 return p2p_ctrl_service_del_upnp(wpa_s, pos);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005327 if (os_strcmp(cmd, "asp") == 0)
5328 return p2p_ctrl_service_del_asp(wpa_s, pos);
5329 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
5330 return -1;
5331}
5332
5333
5334static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
5335{
5336 char *pos;
5337
5338 pos = os_strchr(cmd, ' ');
5339 if (pos == NULL)
5340 return -1;
5341 *pos++ = '\0';
5342
5343 if (os_strcmp(cmd, "asp") == 0)
5344 return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
5345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
5347 return -1;
5348}
5349
5350
5351static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
5352{
5353 u8 addr[ETH_ALEN];
5354
5355 /* <addr> */
5356
5357 if (hwaddr_aton(cmd, addr))
5358 return -1;
5359
5360 return wpas_p2p_reject(wpa_s, addr);
5361}
5362
5363
5364static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
5365{
5366 char *pos;
5367 int id;
5368 struct wpa_ssid *ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005369 u8 *_peer = NULL, peer[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005370 int freq = 0, pref_freq = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005371 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005372
5373 id = atoi(cmd);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005374 pos = os_strstr(cmd, " peer=");
5375 if (pos) {
5376 pos += 6;
5377 if (hwaddr_aton(pos, peer))
5378 return -1;
5379 _peer = peer;
5380 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005381 ssid = wpa_config_get_network(wpa_s->conf, id);
5382 if (ssid == NULL || ssid->disabled != 2) {
5383 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
5384 "for persistent P2P group",
5385 id);
5386 return -1;
5387 }
5388
Jouni Malinen31be0a42012-08-31 21:20:51 +03005389 pos = os_strstr(cmd, " freq=");
5390 if (pos) {
5391 pos += 6;
5392 freq = atoi(pos);
5393 if (freq <= 0)
5394 return -1;
5395 }
5396
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005397 pos = os_strstr(cmd, " pref=");
5398 if (pos) {
5399 pos += 6;
5400 pref_freq = atoi(pos);
5401 if (pref_freq <= 0)
5402 return -1;
5403 }
5404
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005405 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
5406 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
5407 vht;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005408
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005409 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
5410 pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005411}
5412
5413
5414static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
5415{
5416 char *pos;
5417 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
5418
5419 pos = os_strstr(cmd, " peer=");
5420 if (!pos)
5421 return -1;
5422
5423 *pos = '\0';
5424 pos += 6;
5425 if (hwaddr_aton(pos, peer)) {
5426 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
5427 return -1;
5428 }
5429
5430 pos = os_strstr(pos, " go_dev_addr=");
5431 if (pos) {
5432 pos += 13;
5433 if (hwaddr_aton(pos, go_dev_addr)) {
5434 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
5435 pos);
5436 return -1;
5437 }
5438 go_dev = go_dev_addr;
5439 }
5440
5441 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
5442}
5443
5444
5445static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
5446{
5447 if (os_strncmp(cmd, "persistent=", 11) == 0)
5448 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
5449 if (os_strncmp(cmd, "group=", 6) == 0)
5450 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
5451
5452 return -1;
5453}
5454
5455
5456static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005457 char *cmd, int freq, int ht40,
5458 int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005459{
5460 int id;
5461 struct wpa_ssid *ssid;
5462
5463 id = atoi(cmd);
5464 ssid = wpa_config_get_network(wpa_s->conf, id);
5465 if (ssid == NULL || ssid->disabled != 2) {
5466 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
5467 "for persistent P2P group",
5468 id);
5469 return -1;
5470 }
5471
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005472 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005473 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005474}
5475
5476
5477static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
5478{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005479 int freq = 0, ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005480 char *pos;
5481
5482 pos = os_strstr(cmd, "freq=");
5483 if (pos)
5484 freq = atoi(pos + 5);
5485
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005486 vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
5487 ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
5488 vht;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005490 if (os_strncmp(cmd, "persistent=", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005491 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005492 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005493 if (os_strcmp(cmd, "persistent") == 0 ||
5494 os_strncmp(cmd, "persistent ", 11) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005495 return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005496 if (os_strncmp(cmd, "freq=", 5) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005497 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005498 if (ht40)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005499 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005500
5501 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
5502 cmd);
5503 return -1;
5504}
5505
5506
5507static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
5508 char *buf, size_t buflen)
5509{
5510 u8 addr[ETH_ALEN], *addr_ptr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005511 int next, res;
5512 const struct p2p_peer_info *info;
5513 char *pos, *end;
5514 char devtype[WPS_DEV_TYPE_BUFSIZE];
5515 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005516 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005517
5518 if (!wpa_s->global->p2p)
5519 return -1;
5520
5521 if (os_strcmp(cmd, "FIRST") == 0) {
5522 addr_ptr = NULL;
5523 next = 0;
5524 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
5525 if (hwaddr_aton(cmd + 5, addr) < 0)
5526 return -1;
5527 addr_ptr = addr;
5528 next = 1;
5529 } else {
5530 if (hwaddr_aton(cmd, addr) < 0)
5531 return -1;
5532 addr_ptr = addr;
5533 next = 0;
5534 }
5535
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005536 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
5537 if (info == NULL)
5538 return -1;
5539
5540 pos = buf;
5541 end = buf + buflen;
5542
5543 res = os_snprintf(pos, end - pos, MACSTR "\n"
5544 "pri_dev_type=%s\n"
5545 "device_name=%s\n"
5546 "manufacturer=%s\n"
5547 "model_name=%s\n"
5548 "model_number=%s\n"
5549 "serial_number=%s\n"
5550 "config_methods=0x%x\n"
5551 "dev_capab=0x%x\n"
5552 "group_capab=0x%x\n"
5553 "level=%d\n",
5554 MAC2STR(info->p2p_device_addr),
5555 wps_dev_type_bin2str(info->pri_dev_type,
5556 devtype, sizeof(devtype)),
5557 info->device_name,
5558 info->manufacturer,
5559 info->model_name,
5560 info->model_number,
5561 info->serial_number,
5562 info->config_methods,
5563 info->dev_capab,
5564 info->group_capab,
5565 info->level);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005566 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005567 return pos - buf;
5568 pos += res;
5569
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005570 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
5571 {
5572 const u8 *t;
5573 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
5574 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
5575 wps_dev_type_bin2str(t, devtype,
5576 sizeof(devtype)));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005577 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005578 return pos - buf;
5579 pos += res;
5580 }
5581
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005582 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005583 if (ssid) {
5584 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005585 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005586 return pos - buf;
5587 pos += res;
5588 }
5589
5590 res = p2p_get_peer_info_txt(info, pos, end - pos);
5591 if (res < 0)
5592 return pos - buf;
5593 pos += res;
5594
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005595 if (info->vendor_elems) {
5596 res = os_snprintf(pos, end - pos, "vendor_elems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005597 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005598 return pos - buf;
5599 pos += res;
5600
5601 pos += wpa_snprintf_hex(pos, end - pos,
5602 wpabuf_head(info->vendor_elems),
5603 wpabuf_len(info->vendor_elems));
5604
5605 res = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005606 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005607 return pos - buf;
5608 pos += res;
5609 }
5610
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005611 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005612}
5613
5614
Dmitry Shmidt04949592012-07-19 12:16:46 -07005615static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
5616 const char *param)
5617{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005618 unsigned int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005619
5620 if (wpa_s->global->p2p == NULL)
5621 return -1;
5622
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005623 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
5624 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005625
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005626 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
5627 struct wpa_freq_range *freq;
5628 freq = &wpa_s->global->p2p_disallow_freq.range[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005629 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005630 freq->min, freq->max);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005631 }
5632
Dmitry Shmidt04949592012-07-19 12:16:46 -07005633 wpas_p2p_update_channel_list(wpa_s);
5634 return 0;
5635}
5636
5637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005638static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
5639{
5640 char *param;
5641
5642 if (wpa_s->global->p2p == NULL)
5643 return -1;
5644
5645 param = os_strchr(cmd, ' ');
5646 if (param == NULL)
5647 return -1;
5648 *param++ = '\0';
5649
5650 if (os_strcmp(cmd, "discoverability") == 0) {
5651 p2p_set_client_discoverability(wpa_s->global->p2p,
5652 atoi(param));
5653 return 0;
5654 }
5655
5656 if (os_strcmp(cmd, "managed") == 0) {
5657 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
5658 return 0;
5659 }
5660
5661 if (os_strcmp(cmd, "listen_channel") == 0) {
5662 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005663 atoi(param), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005664 }
5665
5666 if (os_strcmp(cmd, "ssid_postfix") == 0) {
5667 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
5668 os_strlen(param));
5669 }
5670
5671 if (os_strcmp(cmd, "noa") == 0) {
5672 char *pos;
5673 int count, start, duration;
5674 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
5675 count = atoi(param);
5676 pos = os_strchr(param, ',');
5677 if (pos == NULL)
5678 return -1;
5679 pos++;
5680 start = atoi(pos);
5681 pos = os_strchr(pos, ',');
5682 if (pos == NULL)
5683 return -1;
5684 pos++;
5685 duration = atoi(pos);
5686 if (count < 0 || count > 255 || start < 0 || duration < 0)
5687 return -1;
5688 if (count == 0 && duration > 0)
5689 return -1;
5690 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
5691 "start=%d duration=%d", count, start, duration);
5692 return wpas_p2p_set_noa(wpa_s, count, start, duration);
5693 }
5694
5695 if (os_strcmp(cmd, "ps") == 0)
5696 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
5697
5698 if (os_strcmp(cmd, "oppps") == 0)
5699 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
5700
5701 if (os_strcmp(cmd, "ctwindow") == 0)
5702 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
5703
5704 if (os_strcmp(cmd, "disabled") == 0) {
5705 wpa_s->global->p2p_disabled = atoi(param);
5706 wpa_printf(MSG_DEBUG, "P2P functionality %s",
5707 wpa_s->global->p2p_disabled ?
5708 "disabled" : "enabled");
5709 if (wpa_s->global->p2p_disabled) {
5710 wpas_p2p_stop_find(wpa_s);
5711 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5712 p2p_flush(wpa_s->global->p2p);
5713 }
5714 return 0;
5715 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005716
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07005717 if (os_strcmp(cmd, "conc_pref") == 0) {
5718 if (os_strcmp(param, "sta") == 0)
5719 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
5720 else if (os_strcmp(param, "p2p") == 0)
5721 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005722 else {
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07005723 wpa_printf(MSG_INFO, "Invalid conc_pref value");
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005724 return -1;
5725 }
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07005726 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
Dmitry Shmidt04949592012-07-19 12:16:46 -07005727 "%s", param);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005728 return 0;
5729 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005730
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005731 if (os_strcmp(cmd, "force_long_sd") == 0) {
5732 wpa_s->force_long_sd = atoi(param);
5733 return 0;
5734 }
5735
5736 if (os_strcmp(cmd, "peer_filter") == 0) {
5737 u8 addr[ETH_ALEN];
5738 if (hwaddr_aton(param, addr))
5739 return -1;
5740 p2p_set_peer_filter(wpa_s->global->p2p, addr);
5741 return 0;
5742 }
5743
5744 if (os_strcmp(cmd, "cross_connect") == 0)
5745 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
5746
5747 if (os_strcmp(cmd, "go_apsd") == 0) {
5748 if (os_strcmp(param, "disable") == 0)
5749 wpa_s->set_ap_uapsd = 0;
5750 else {
5751 wpa_s->set_ap_uapsd = 1;
5752 wpa_s->ap_uapsd = atoi(param);
5753 }
5754 return 0;
5755 }
5756
5757 if (os_strcmp(cmd, "client_apsd") == 0) {
5758 if (os_strcmp(param, "disable") == 0)
5759 wpa_s->set_sta_uapsd = 0;
5760 else {
5761 int be, bk, vi, vo;
5762 char *pos;
5763 /* format: BE,BK,VI,VO;max SP Length */
5764 be = atoi(param);
5765 pos = os_strchr(param, ',');
5766 if (pos == NULL)
5767 return -1;
5768 pos++;
5769 bk = atoi(pos);
5770 pos = os_strchr(pos, ',');
5771 if (pos == NULL)
5772 return -1;
5773 pos++;
5774 vi = atoi(pos);
5775 pos = os_strchr(pos, ',');
5776 if (pos == NULL)
5777 return -1;
5778 pos++;
5779 vo = atoi(pos);
5780 /* ignore max SP Length for now */
5781
5782 wpa_s->set_sta_uapsd = 1;
5783 wpa_s->sta_uapsd = 0;
5784 if (be)
5785 wpa_s->sta_uapsd |= BIT(0);
5786 if (bk)
5787 wpa_s->sta_uapsd |= BIT(1);
5788 if (vi)
5789 wpa_s->sta_uapsd |= BIT(2);
5790 if (vo)
5791 wpa_s->sta_uapsd |= BIT(3);
5792 }
5793 return 0;
5794 }
5795
Dmitry Shmidt04949592012-07-19 12:16:46 -07005796 if (os_strcmp(cmd, "disallow_freq") == 0)
5797 return p2p_ctrl_disallow_freq(wpa_s, param);
5798
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005799 if (os_strcmp(cmd, "disc_int") == 0) {
5800 int min_disc_int, max_disc_int, max_disc_tu;
5801 char *pos;
5802
5803 pos = param;
5804
5805 min_disc_int = atoi(pos);
5806 pos = os_strchr(pos, ' ');
5807 if (pos == NULL)
5808 return -1;
5809 *pos++ = '\0';
5810
5811 max_disc_int = atoi(pos);
5812 pos = os_strchr(pos, ' ');
5813 if (pos == NULL)
5814 return -1;
5815 *pos++ = '\0';
5816
5817 max_disc_tu = atoi(pos);
5818
5819 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
5820 max_disc_int, max_disc_tu);
5821 }
5822
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005823 if (os_strcmp(cmd, "per_sta_psk") == 0) {
5824 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
5825 return 0;
5826 }
5827
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005828#ifdef CONFIG_WPS_NFC
5829 if (os_strcmp(cmd, "nfc_tag") == 0)
5830 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
5831#endif /* CONFIG_WPS_NFC */
5832
5833 if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
5834 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
5835 return 0;
5836 }
5837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005838 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
5839 cmd);
5840
5841 return -1;
5842}
5843
5844
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005845static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
5846{
5847 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5848 wpa_s->force_long_sd = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005849 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005850 if (wpa_s->global->p2p)
5851 p2p_flush(wpa_s->global->p2p);
5852}
5853
5854
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005855static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
5856{
5857 char *pos, *pos2;
5858 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
5859
5860 if (cmd[0]) {
5861 pos = os_strchr(cmd, ' ');
5862 if (pos == NULL)
5863 return -1;
5864 *pos++ = '\0';
5865 dur1 = atoi(cmd);
5866
5867 pos2 = os_strchr(pos, ' ');
5868 if (pos2)
5869 *pos2++ = '\0';
5870 int1 = atoi(pos);
5871 } else
5872 pos2 = NULL;
5873
5874 if (pos2) {
5875 pos = os_strchr(pos2, ' ');
5876 if (pos == NULL)
5877 return -1;
5878 *pos++ = '\0';
5879 dur2 = atoi(pos2);
5880 int2 = atoi(pos);
5881 }
5882
5883 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
5884}
5885
5886
5887static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
5888{
5889 char *pos;
5890 unsigned int period = 0, interval = 0;
5891
5892 if (cmd[0]) {
5893 pos = os_strchr(cmd, ' ');
5894 if (pos == NULL)
5895 return -1;
5896 *pos++ = '\0';
5897 period = atoi(cmd);
5898 interval = atoi(pos);
5899 }
5900
5901 return wpas_p2p_ext_listen(wpa_s, period, interval);
5902}
5903
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005904
5905static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
5906{
5907 const char *pos;
5908 u8 peer[ETH_ALEN];
5909 int iface_addr = 0;
5910
5911 pos = cmd;
5912 if (os_strncmp(pos, "iface=", 6) == 0) {
5913 iface_addr = 1;
5914 pos += 6;
5915 }
5916 if (hwaddr_aton(pos, peer))
5917 return -1;
5918
5919 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
5920 return 0;
5921}
5922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005923#endif /* CONFIG_P2P */
5924
5925
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005926static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
5927{
5928 struct wpa_freq_range_list ranges;
5929 int *freqs = NULL;
5930 struct hostapd_hw_modes *mode;
5931 u16 i;
5932
5933 if (wpa_s->hw.modes == NULL)
5934 return NULL;
5935
5936 os_memset(&ranges, 0, sizeof(ranges));
5937 if (freq_range_list_parse(&ranges, val) < 0)
5938 return NULL;
5939
5940 for (i = 0; i < wpa_s->hw.num_modes; i++) {
5941 int j;
5942
5943 mode = &wpa_s->hw.modes[i];
5944 for (j = 0; j < mode->num_channels; j++) {
5945 unsigned int freq;
5946
5947 if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
5948 continue;
5949
5950 freq = mode->channels[j].freq;
5951 if (!freq_range_list_includes(&ranges, freq))
5952 continue;
5953
5954 int_array_add_unique(&freqs, freq);
5955 }
5956 }
5957
5958 os_free(ranges.range);
5959 return freqs;
5960}
5961
5962
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005963#ifdef CONFIG_INTERWORKING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005964
5965static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
5966{
5967 int auto_sel = 0;
5968 int *freqs = NULL;
5969
5970 if (param) {
5971 char *pos;
5972
5973 auto_sel = os_strstr(param, "auto") != NULL;
5974
5975 pos = os_strstr(param, "freq=");
5976 if (pos) {
5977 freqs = freq_range_to_channel_list(wpa_s, pos + 5);
5978 if (freqs == NULL)
5979 return -1;
5980 }
5981
5982 }
5983
5984 return interworking_select(wpa_s, auto_sel, freqs);
5985}
5986
5987
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005988static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
5989 int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005990{
5991 u8 bssid[ETH_ALEN];
5992 struct wpa_bss *bss;
5993
5994 if (hwaddr_aton(dst, bssid)) {
5995 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
5996 return -1;
5997 }
5998
5999 bss = wpa_bss_get_bssid(wpa_s, bssid);
6000 if (bss == NULL) {
6001 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
6002 MAC2STR(bssid));
6003 return -1;
6004 }
6005
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006006 if (bss->ssid_len == 0) {
6007 int found = 0;
6008
6009 wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
6010 " does not have SSID information", MAC2STR(bssid));
6011
6012 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
6013 list) {
6014 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
6015 bss->ssid_len > 0) {
6016 found = 1;
6017 break;
6018 }
6019 }
6020
6021 if (!found)
6022 return -1;
6023 wpa_printf(MSG_DEBUG,
6024 "Found another matching BSS entry with SSID");
6025 }
6026
Dmitry Shmidt7f656022015-02-25 14:36:37 -08006027 return interworking_connect(wpa_s, bss, only_add);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006028}
6029
6030
6031static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
6032{
6033 u8 dst_addr[ETH_ALEN];
6034 int used;
6035 char *pos;
6036#define MAX_ANQP_INFO_ID 100
6037 u16 id[MAX_ANQP_INFO_ID];
6038 size_t num_id = 0;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006039 u32 subtypes = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006040
6041 used = hwaddr_aton2(dst, dst_addr);
6042 if (used < 0)
6043 return -1;
6044 pos = dst + used;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006045 if (*pos == ' ')
6046 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006047 while (num_id < MAX_ANQP_INFO_ID) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006048 if (os_strncmp(pos, "hs20:", 5) == 0) {
6049#ifdef CONFIG_HS20
6050 int num = atoi(pos + 5);
6051 if (num <= 0 || num > 31)
6052 return -1;
6053 subtypes |= BIT(num);
6054#else /* CONFIG_HS20 */
6055 return -1;
6056#endif /* CONFIG_HS20 */
6057 } else {
6058 id[num_id] = atoi(pos);
6059 if (id[num_id])
6060 num_id++;
6061 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006062 pos = os_strchr(pos + 1, ',');
6063 if (pos == NULL)
6064 break;
6065 pos++;
6066 }
6067
6068 if (num_id == 0)
6069 return -1;
6070
Dmitry Shmidt15907092014-03-25 10:42:57 -07006071 return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006072}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006073
6074
6075static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
6076{
6077 u8 dst_addr[ETH_ALEN];
6078 struct wpabuf *advproto, *query = NULL;
6079 int used, ret = -1;
6080 char *pos, *end;
6081 size_t len;
6082
6083 used = hwaddr_aton2(cmd, dst_addr);
6084 if (used < 0)
6085 return -1;
6086
6087 pos = cmd + used;
6088 while (*pos == ' ')
6089 pos++;
6090
6091 /* Advertisement Protocol ID */
6092 end = os_strchr(pos, ' ');
6093 if (end)
6094 len = end - pos;
6095 else
6096 len = os_strlen(pos);
6097 if (len & 0x01)
6098 return -1;
6099 len /= 2;
6100 if (len == 0)
6101 return -1;
6102 advproto = wpabuf_alloc(len);
6103 if (advproto == NULL)
6104 return -1;
6105 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
6106 goto fail;
6107
6108 if (end) {
6109 /* Optional Query Request */
6110 pos = end + 1;
6111 while (*pos == ' ')
6112 pos++;
6113
6114 len = os_strlen(pos);
6115 if (len) {
6116 if (len & 0x01)
6117 goto fail;
6118 len /= 2;
6119 if (len == 0)
6120 goto fail;
6121 query = wpabuf_alloc(len);
6122 if (query == NULL)
6123 goto fail;
6124 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
6125 goto fail;
6126 }
6127 }
6128
6129 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
6130
6131fail:
6132 wpabuf_free(advproto);
6133 wpabuf_free(query);
6134
6135 return ret;
6136}
6137
6138
6139static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
6140 size_t buflen)
6141{
6142 u8 addr[ETH_ALEN];
6143 int dialog_token;
6144 int used;
6145 char *pos;
6146 size_t resp_len, start, requested_len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006147 struct wpabuf *resp;
6148 int ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006149
6150 used = hwaddr_aton2(cmd, addr);
6151 if (used < 0)
6152 return -1;
6153
6154 pos = cmd + used;
6155 while (*pos == ' ')
6156 pos++;
6157 dialog_token = atoi(pos);
6158
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006159 if (wpa_s->last_gas_resp &&
6160 os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
6161 dialog_token == wpa_s->last_gas_dialog_token)
6162 resp = wpa_s->last_gas_resp;
6163 else if (wpa_s->prev_gas_resp &&
6164 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
6165 dialog_token == wpa_s->prev_gas_dialog_token)
6166 resp = wpa_s->prev_gas_resp;
6167 else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006168 return -1;
6169
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006170 resp_len = wpabuf_len(resp);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006171 start = 0;
6172 requested_len = resp_len;
6173
6174 pos = os_strchr(pos, ' ');
6175 if (pos) {
6176 start = atoi(pos);
6177 if (start > resp_len)
6178 return os_snprintf(buf, buflen, "FAIL-Invalid range");
6179 pos = os_strchr(pos, ',');
6180 if (pos == NULL)
6181 return -1;
6182 pos++;
6183 requested_len = atoi(pos);
6184 if (start + requested_len > resp_len)
6185 return os_snprintf(buf, buflen, "FAIL-Invalid range");
6186 }
6187
6188 if (requested_len * 2 + 1 > buflen)
6189 return os_snprintf(buf, buflen, "FAIL-Too long response");
6190
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006191 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
6192 requested_len);
6193
6194 if (start + requested_len == resp_len) {
6195 /*
6196 * Free memory by dropping the response after it has been
6197 * fetched.
6198 */
6199 if (resp == wpa_s->prev_gas_resp) {
6200 wpabuf_free(wpa_s->prev_gas_resp);
6201 wpa_s->prev_gas_resp = NULL;
6202 } else {
6203 wpabuf_free(wpa_s->last_gas_resp);
6204 wpa_s->last_gas_resp = NULL;
6205 }
6206 }
6207
6208 return ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006209}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006210#endif /* CONFIG_INTERWORKING */
6211
6212
Dmitry Shmidt04949592012-07-19 12:16:46 -07006213#ifdef CONFIG_HS20
6214
6215static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
6216{
6217 u8 dst_addr[ETH_ALEN];
6218 int used;
6219 char *pos;
6220 u32 subtypes = 0;
6221
6222 used = hwaddr_aton2(dst, dst_addr);
6223 if (used < 0)
6224 return -1;
6225 pos = dst + used;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006226 if (*pos == ' ')
6227 pos++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006228 for (;;) {
6229 int num = atoi(pos);
6230 if (num <= 0 || num > 31)
6231 return -1;
6232 subtypes |= BIT(num);
6233 pos = os_strchr(pos + 1, ',');
6234 if (pos == NULL)
6235 break;
6236 pos++;
6237 }
6238
6239 if (subtypes == 0)
6240 return -1;
6241
6242 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
6243}
6244
6245
6246static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
6247 const u8 *addr, const char *realm)
6248{
6249 u8 *buf;
6250 size_t rlen, len;
6251 int ret;
6252
6253 rlen = os_strlen(realm);
6254 len = 3 + rlen;
6255 buf = os_malloc(len);
6256 if (buf == NULL)
6257 return -1;
6258 buf[0] = 1; /* NAI Home Realm Count */
6259 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
6260 buf[2] = rlen;
6261 os_memcpy(buf + 3, realm, rlen);
6262
6263 ret = hs20_anqp_send_req(wpa_s, addr,
6264 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
6265 buf, len);
6266
6267 os_free(buf);
6268
6269 return ret;
6270}
6271
6272
6273static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
6274 char *dst)
6275{
6276 struct wpa_cred *cred = wpa_s->conf->cred;
6277 u8 dst_addr[ETH_ALEN];
6278 int used;
6279 u8 *buf;
6280 size_t len;
6281 int ret;
6282
6283 used = hwaddr_aton2(dst, dst_addr);
6284 if (used < 0)
6285 return -1;
6286
6287 while (dst[used] == ' ')
6288 used++;
6289 if (os_strncmp(dst + used, "realm=", 6) == 0)
6290 return hs20_nai_home_realm_list(wpa_s, dst_addr,
6291 dst + used + 6);
6292
6293 len = os_strlen(dst + used);
6294
6295 if (len == 0 && cred && cred->realm)
6296 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
6297
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07006298 if (len & 1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07006299 return -1;
6300 len /= 2;
6301 buf = os_malloc(len);
6302 if (buf == NULL)
6303 return -1;
6304 if (hexstr2bin(dst + used, buf, len) < 0) {
6305 os_free(buf);
6306 return -1;
6307 }
6308
6309 ret = hs20_anqp_send_req(wpa_s, dst_addr,
6310 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
6311 buf, len);
6312 os_free(buf);
6313
6314 return ret;
6315}
6316
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006317
6318static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
6319{
6320 u8 dst_addr[ETH_ALEN];
6321 int used;
6322 char *icon;
6323
6324 used = hwaddr_aton2(cmd, dst_addr);
6325 if (used < 0)
6326 return -1;
6327
6328 while (cmd[used] == ' ')
6329 used++;
6330 icon = &cmd[used];
6331
6332 wpa_s->fetch_osu_icon_in_progress = 0;
6333 return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
6334 (u8 *) icon, os_strlen(icon));
6335}
6336
Dmitry Shmidt04949592012-07-19 12:16:46 -07006337#endif /* CONFIG_HS20 */
6338
6339
Dmitry Shmidt04949592012-07-19 12:16:46 -07006340#ifdef CONFIG_AUTOSCAN
6341
6342static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
6343 char *cmd)
6344{
6345 enum wpa_states state = wpa_s->wpa_state;
6346 char *new_params = NULL;
6347
6348 if (os_strlen(cmd) > 0) {
6349 new_params = os_strdup(cmd);
6350 if (new_params == NULL)
6351 return -1;
6352 }
6353
6354 os_free(wpa_s->conf->autoscan);
6355 wpa_s->conf->autoscan = new_params;
6356
6357 if (wpa_s->conf->autoscan == NULL)
6358 autoscan_deinit(wpa_s);
6359 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
6360 autoscan_init(wpa_s, 1);
6361 else if (state == WPA_SCANNING)
6362 wpa_supplicant_reinit_autoscan(wpa_s);
6363
6364 return 0;
6365}
6366
6367#endif /* CONFIG_AUTOSCAN */
6368
6369
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006370#ifdef CONFIG_WNM
6371
6372static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
6373{
6374 int enter;
6375 int intval = 0;
6376 char *pos;
6377 int ret;
6378 struct wpabuf *tfs_req = NULL;
6379
6380 if (os_strncmp(cmd, "enter", 5) == 0)
6381 enter = 1;
6382 else if (os_strncmp(cmd, "exit", 4) == 0)
6383 enter = 0;
6384 else
6385 return -1;
6386
6387 pos = os_strstr(cmd, " interval=");
6388 if (pos)
6389 intval = atoi(pos + 10);
6390
6391 pos = os_strstr(cmd, " tfs_req=");
6392 if (pos) {
6393 char *end;
6394 size_t len;
6395 pos += 9;
6396 end = os_strchr(pos, ' ');
6397 if (end)
6398 len = end - pos;
6399 else
6400 len = os_strlen(pos);
6401 if (len & 1)
6402 return -1;
6403 len /= 2;
6404 tfs_req = wpabuf_alloc(len);
6405 if (tfs_req == NULL)
6406 return -1;
6407 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
6408 wpabuf_free(tfs_req);
6409 return -1;
6410 }
6411 }
6412
6413 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
6414 WNM_SLEEP_MODE_EXIT, intval,
6415 tfs_req);
6416 wpabuf_free(tfs_req);
6417
6418 return ret;
6419}
6420
Dmitry Shmidt44c95782013-05-17 09:51:35 -07006421
6422static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
6423{
6424 int query_reason;
6425
6426 query_reason = atoi(cmd);
6427
6428 wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
6429 query_reason);
6430
6431 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
6432}
6433
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006434#endif /* CONFIG_WNM */
6435
6436
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006437static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
6438 size_t buflen)
6439{
6440 struct wpa_signal_info si;
6441 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006442 char *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006443
6444 ret = wpa_drv_signal_poll(wpa_s, &si);
6445 if (ret)
6446 return -1;
6447
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006448 pos = buf;
6449 end = buf + buflen;
6450
6451 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006452 "NOISE=%d\nFREQUENCY=%u\n",
6453 si.current_signal, si.current_txrate / 1000,
6454 si.current_noise, si.frequency);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006455 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006456 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006457 pos += ret;
6458
6459 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
6460 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006461 channel_width_to_string(si.chanwidth));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006462 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006463 return -1;
6464 pos += ret;
6465 }
6466
6467 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
6468 ret = os_snprintf(pos, end - pos,
6469 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
6470 si.center_frq1, si.center_frq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006471 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006472 return -1;
6473 pos += ret;
6474 }
6475
6476 if (si.avg_signal) {
6477 ret = os_snprintf(pos, end - pos,
6478 "AVG_RSSI=%d\n", si.avg_signal);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006479 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006480 return -1;
6481 pos += ret;
6482 }
6483
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07006484 if (si.avg_beacon_signal) {
6485 ret = os_snprintf(pos, end - pos,
6486 "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
6487 if (os_snprintf_error(end - pos, ret))
6488 return -1;
6489 pos += ret;
6490 }
6491
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006492 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006493}
6494
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006495
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07006496static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
6497 size_t buflen)
6498{
6499 struct hostap_sta_driver_data sta;
6500 int ret;
6501
6502 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
6503 if (ret)
6504 return -1;
6505
6506 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006507 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006508 if (os_snprintf_error(buflen, ret))
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07006509 return -1;
6510 return ret;
6511}
6512
6513
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006514#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07006515static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
6516 char *buf, size_t buflen)
6517{
6518 int ret;
6519
6520 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
Dmitry Shmidt9432e122013-09-12 12:39:30 -07006521 if (ret == 0) {
6522 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
6523 struct p2p_data *p2p = wpa_s->global->p2p;
6524 if (p2p) {
6525 char country[3];
6526 country[0] = cmd[8];
6527 country[1] = cmd[9];
6528 country[2] = 0x04;
6529 p2p_set_country(p2p, country);
6530 }
6531 }
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006532 ret = os_snprintf(buf, buflen, "%s\n", "OK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006533 if (os_snprintf_error(buflen, ret))
6534 ret = -1;
Dmitry Shmidt9432e122013-09-12 12:39:30 -07006535 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07006536 return ret;
6537}
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006538#endif /* ANDROID */
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07006539
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07006540
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006541static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
6542 char *buf, size_t buflen)
6543{
6544 int ret;
6545 char *pos;
6546 u8 *data = NULL;
6547 unsigned int vendor_id, subcmd;
6548 struct wpabuf *reply;
6549 size_t data_len = 0;
6550
6551 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
6552 vendor_id = strtoul(cmd, &pos, 16);
6553 if (!isblank(*pos))
6554 return -EINVAL;
6555
6556 subcmd = strtoul(pos, &pos, 10);
6557
6558 if (*pos != '\0') {
6559 if (!isblank(*pos++))
6560 return -EINVAL;
6561 data_len = os_strlen(pos);
6562 }
6563
6564 if (data_len) {
6565 data_len /= 2;
6566 data = os_malloc(data_len);
6567 if (!data)
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006568 return -1;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006569
6570 if (hexstr2bin(pos, data, data_len)) {
6571 wpa_printf(MSG_DEBUG,
6572 "Vendor command: wrong parameter format");
6573 os_free(data);
6574 return -EINVAL;
6575 }
6576 }
6577
6578 reply = wpabuf_alloc((buflen - 1) / 2);
6579 if (!reply) {
6580 os_free(data);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006581 return -1;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006582 }
6583
6584 ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
6585 reply);
6586
6587 if (ret == 0)
6588 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
6589 wpabuf_len(reply));
6590
6591 wpabuf_free(reply);
6592 os_free(data);
6593
6594 return ret;
6595}
6596
6597
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006598static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
6599{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006600#ifdef CONFIG_P2P
6601 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
6602 wpa_s->global->p2p_init_wpa_s : wpa_s;
6603#endif /* CONFIG_P2P */
6604
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006605 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
6606
6607#ifdef CONFIG_P2P
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006608 wpas_p2p_cancel(p2p_wpa_s);
6609 p2p_ctrl_flush(p2p_wpa_s);
6610 wpas_p2p_group_remove(p2p_wpa_s, "*");
6611 wpas_p2p_service_flush(p2p_wpa_s);
6612 p2p_wpa_s->global->p2p_disabled = 0;
6613 p2p_wpa_s->global->p2p_per_sta_psk = 0;
6614 p2p_wpa_s->conf->num_sec_device_types = 0;
6615 p2p_wpa_s->p2p_disable_ip_addr_req = 0;
6616 os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
6617 p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006618 p2p_wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006619#endif /* CONFIG_P2P */
6620
6621#ifdef CONFIG_WPS_TESTING
6622 wps_version_number = 0x20;
6623 wps_testing_dummy_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006624 wps_corrupt_pkhash = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006625#endif /* CONFIG_WPS_TESTING */
6626#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006627 wpa_s->wps_fragment_size = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006628 wpas_wps_cancel(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006629 wps_registrar_flush(wpa_s->wps->registrar);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006630#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07006631 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006632 wpa_s->known_wps_freq = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006633
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006634#ifdef CONFIG_TDLS
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006635#ifdef CONFIG_TDLS_TESTING
6636 extern unsigned int tdls_testing;
6637 tdls_testing = 0;
6638#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006639 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
6640 wpa_tdls_enable(wpa_s->wpa, 1);
6641#endif /* CONFIG_TDLS */
6642
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006643 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
6644 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
6645
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006646 wpa_s->no_keep_alive = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006647 wpa_s->own_disconnect_req = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006648
6649 os_free(wpa_s->disallow_aps_bssid);
6650 wpa_s->disallow_aps_bssid = NULL;
6651 wpa_s->disallow_aps_bssid_count = 0;
6652 os_free(wpa_s->disallow_aps_ssid);
6653 wpa_s->disallow_aps_ssid = NULL;
6654 wpa_s->disallow_aps_ssid_count = 0;
6655
6656 wpa_s->set_sta_uapsd = 0;
6657 wpa_s->sta_uapsd = 0;
6658
6659 wpa_drv_radio_disable(wpa_s, 0);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006660 wpa_blacklist_clear(wpa_s);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006661 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006662 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
6663 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08006664 wpa_config_flush_blobs(wpa_s->conf);
Dmitry Shmidt18463232014-01-24 12:29:41 -08006665 wpa_s->conf->auto_interworking = 0;
6666 wpa_s->conf->okc = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006667
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006668 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
6669 rsn_preauth_deinit(wpa_s->wpa);
6670
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006671 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
6672 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
6673 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
6674 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
6675
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006676 radio_remove_works(wpa_s, NULL, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006677 wpa_s->ext_work_in_progress = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006678
6679 wpa_s->next_ssid = NULL;
6680
6681#ifdef CONFIG_INTERWORKING
6682 hs20_cancel_fetch_osu(wpa_s);
6683#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt818ea482014-03-10 13:15:21 -07006684
6685 wpa_s->ext_mgmt_frame_handling = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006686 wpa_s->ext_eapol_frame_io = 0;
6687#ifdef CONFIG_TESTING_OPTIONS
6688 wpa_s->extra_roc_dur = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006689 wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006690#endif /* CONFIG_TESTING_OPTIONS */
6691
6692 wpa_s->disconnected = 0;
6693 os_free(wpa_s->next_scan_freqs);
6694 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006695
6696 wpa_bss_flush(wpa_s);
6697 if (!dl_list_empty(&wpa_s->bss)) {
6698 wpa_printf(MSG_DEBUG,
6699 "BSS table not empty after flush: %u entries, current_bss=%p bssid="
6700 MACSTR " pending_bssid=" MACSTR,
6701 dl_list_len(&wpa_s->bss), wpa_s->current_bss,
6702 MAC2STR(wpa_s->bssid),
6703 MAC2STR(wpa_s->pending_bssid));
6704 }
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07006705
6706 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006707}
6708
6709
6710static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
6711 char *buf, size_t buflen)
6712{
6713 struct wpa_radio_work *work;
6714 char *pos, *end;
6715 struct os_reltime now, diff;
6716
6717 pos = buf;
6718 end = buf + buflen;
6719
6720 os_get_reltime(&now);
6721
6722 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
6723 {
6724 int ret;
6725
6726 os_reltime_sub(&now, &work->time, &diff);
6727 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
6728 work->type, work->wpa_s->ifname, work->freq,
6729 work->started, diff.sec, diff.usec);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006730 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006731 break;
6732 pos += ret;
6733 }
6734
6735 return pos - buf;
6736}
6737
6738
6739static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
6740{
6741 struct wpa_radio_work *work = eloop_ctx;
6742 struct wpa_external_work *ework = work->ctx;
6743
6744 wpa_dbg(work->wpa_s, MSG_DEBUG,
6745 "Timing out external radio work %u (%s)",
6746 ework->id, work->type);
6747 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006748 work->wpa_s->ext_work_in_progress = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006749 radio_work_done(work);
Dmitry Shmidt71757432014-06-02 13:50:35 -07006750 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006751}
6752
6753
6754static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
6755{
6756 struct wpa_external_work *ework = work->ctx;
6757
6758 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006759 if (work->started)
6760 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
6761 work, NULL);
6762
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006763 os_free(ework);
6764 return;
6765 }
6766
6767 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
6768 ework->id, ework->type);
6769 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006770 work->wpa_s->ext_work_in_progress = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006771 if (!ework->timeout)
6772 ework->timeout = 10;
6773 eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
6774 work, NULL);
6775}
6776
6777
6778static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
6779 char *buf, size_t buflen)
6780{
6781 struct wpa_external_work *ework;
6782 char *pos, *pos2;
6783 size_t type_len;
6784 int ret;
6785 unsigned int freq = 0;
6786
6787 /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
6788
6789 ework = os_zalloc(sizeof(*ework));
6790 if (ework == NULL)
6791 return -1;
6792
6793 pos = os_strchr(cmd, ' ');
6794 if (pos) {
6795 type_len = pos - cmd;
6796 pos++;
6797
6798 pos2 = os_strstr(pos, "freq=");
6799 if (pos2)
6800 freq = atoi(pos2 + 5);
6801
6802 pos2 = os_strstr(pos, "timeout=");
6803 if (pos2)
6804 ework->timeout = atoi(pos2 + 8);
6805 } else {
6806 type_len = os_strlen(cmd);
6807 }
6808 if (4 + type_len >= sizeof(ework->type))
6809 type_len = sizeof(ework->type) - 4 - 1;
6810 os_strlcpy(ework->type, "ext:", sizeof(ework->type));
6811 os_memcpy(ework->type + 4, cmd, type_len);
6812 ework->type[4 + type_len] = '\0';
6813
6814 wpa_s->ext_work_id++;
6815 if (wpa_s->ext_work_id == 0)
6816 wpa_s->ext_work_id++;
6817 ework->id = wpa_s->ext_work_id;
6818
6819 if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
6820 ework) < 0) {
6821 os_free(ework);
6822 return -1;
6823 }
6824
6825 ret = os_snprintf(buf, buflen, "%u", ework->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006826 if (os_snprintf_error(buflen, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006827 return -1;
6828 return ret;
6829}
6830
6831
6832static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
6833{
6834 struct wpa_radio_work *work;
6835 unsigned int id = atoi(cmd);
6836
6837 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
6838 {
6839 struct wpa_external_work *ework;
6840
6841 if (os_strncmp(work->type, "ext:", 4) != 0)
6842 continue;
6843 ework = work->ctx;
6844 if (id && ework->id != id)
6845 continue;
6846 wpa_dbg(wpa_s, MSG_DEBUG,
6847 "Completed external radio work %u (%s)",
6848 ework->id, ework->type);
6849 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006850 wpa_s->ext_work_in_progress = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006851 radio_work_done(work);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07006852 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006853 return 3; /* "OK\n" */
6854 }
6855
6856 return -1;
6857}
6858
6859
6860static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
6861 char *buf, size_t buflen)
6862{
6863 if (os_strcmp(cmd, "show") == 0)
6864 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
6865 if (os_strncmp(cmd, "add ", 4) == 0)
6866 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
6867 if (os_strncmp(cmd, "done ", 5) == 0)
6868 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
6869 return -1;
6870}
6871
6872
6873void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
6874{
6875 struct wpa_radio_work *work, *tmp;
6876
Dmitry Shmidt18463232014-01-24 12:29:41 -08006877 if (!wpa_s || !wpa_s->radio)
6878 return;
6879
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006880 dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
6881 struct wpa_radio_work, list) {
6882 struct wpa_external_work *ework;
6883
6884 if (os_strncmp(work->type, "ext:", 4) != 0)
6885 continue;
6886 ework = work->ctx;
6887 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07006888 "Flushing%s external radio work %u (%s)",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006889 work->started ? " started" : "", ework->id,
6890 ework->type);
6891 if (work->started)
6892 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
6893 work, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006894 radio_work_done(work);
Dmitry Shmidt71757432014-06-02 13:50:35 -07006895 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006896 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006897}
6898
6899
Dmitry Shmidt051af732013-10-22 13:52:46 -07006900static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
6901{
6902 struct wpa_supplicant *wpa_s = eloop_ctx;
6903 eapol_sm_notify_ctrl_response(wpa_s->eapol);
6904}
6905
6906
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006907static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
6908 unsigned int *scan_id_count, int scan_id[])
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006909{
6910 const char *pos = value;
6911
6912 while (pos) {
6913 if (*pos == ' ' || *pos == '\0')
6914 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006915 if (*scan_id_count == MAX_SCAN_ID)
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006916 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006917 scan_id[(*scan_id_count)++] = atoi(pos);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006918 pos = os_strchr(pos, ',');
6919 if (pos)
6920 pos++;
6921 }
6922
6923 return 0;
6924}
6925
6926
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006927static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
6928 char *reply, int reply_size, int *reply_len)
6929{
6930 char *pos;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006931 unsigned int manual_scan_passive = 0;
6932 unsigned int manual_scan_use_id = 0;
6933 unsigned int manual_scan_only_new = 0;
6934 unsigned int scan_only = 0;
6935 unsigned int scan_id_count = 0;
6936 int scan_id[MAX_SCAN_ID];
6937 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
6938 struct wpa_scan_results *scan_res);
6939 int *manual_scan_freqs = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006940
6941 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6942 *reply_len = -1;
6943 return;
6944 }
6945
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006946 if (radio_work_pending(wpa_s, "scan")) {
6947 wpa_printf(MSG_DEBUG,
6948 "Pending scan scheduled - reject new request");
6949 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
6950 return;
6951 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006952
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07006953#ifdef CONFIG_INTERWORKING
6954 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
6955 wpa_printf(MSG_DEBUG,
6956 "Interworking select in progress - reject new scan");
6957 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
6958 return;
6959 }
6960#endif /* CONFIG_INTERWORKING */
6961
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006962 if (params) {
6963 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006964 scan_only = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006965
6966 pos = os_strstr(params, "freq=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006967 if (pos) {
6968 manual_scan_freqs = freq_range_to_channel_list(wpa_s,
6969 pos + 5);
6970 if (manual_scan_freqs == NULL) {
6971 *reply_len = -1;
6972 goto done;
6973 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006974 }
6975
6976 pos = os_strstr(params, "passive=");
6977 if (pos)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006978 manual_scan_passive = !!atoi(pos + 8);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006979
6980 pos = os_strstr(params, "use_id=");
6981 if (pos)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006982 manual_scan_use_id = atoi(pos + 7);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006983
6984 pos = os_strstr(params, "only_new=1");
6985 if (pos)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006986 manual_scan_only_new = 1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006987
6988 pos = os_strstr(params, "scan_id=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006989 if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
6990 scan_id) < 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006991 *reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006992 goto done;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006993 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006994 }
6995
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006996 if (scan_only)
6997 scan_res_handler = scan_only_handler;
6998 else if (wpa_s->scan_res_handler == scan_only_handler)
6999 scan_res_handler = NULL;
7000 else
7001 scan_res_handler = wpa_s->scan_res_handler;
7002
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007003 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
7004 ((wpa_s->wpa_state <= WPA_SCANNING) ||
7005 (wpa_s->wpa_state == WPA_COMPLETED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007006 wpa_s->manual_scan_passive = manual_scan_passive;
7007 wpa_s->manual_scan_use_id = manual_scan_use_id;
7008 wpa_s->manual_scan_only_new = manual_scan_only_new;
7009 wpa_s->scan_id_count = scan_id_count;
7010 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
7011 wpa_s->scan_res_handler = scan_res_handler;
7012 os_free(wpa_s->manual_scan_freqs);
7013 wpa_s->manual_scan_freqs = manual_scan_freqs;
7014 manual_scan_freqs = NULL;
7015
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007016 wpa_s->normal_scans = 0;
7017 wpa_s->scan_req = MANUAL_SCAN_REQ;
7018 wpa_s->after_wps = 0;
7019 wpa_s->known_wps_freq = 0;
7020 wpa_supplicant_req_scan(wpa_s, 0, 0);
7021 if (wpa_s->manual_scan_use_id) {
7022 wpa_s->manual_scan_id++;
7023 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
7024 wpa_s->manual_scan_id);
7025 *reply_len = os_snprintf(reply, reply_size, "%u\n",
7026 wpa_s->manual_scan_id);
7027 }
7028 } else if (wpa_s->sched_scanning) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007029 wpa_s->manual_scan_passive = manual_scan_passive;
7030 wpa_s->manual_scan_use_id = manual_scan_use_id;
7031 wpa_s->manual_scan_only_new = manual_scan_only_new;
7032 wpa_s->scan_id_count = scan_id_count;
7033 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
7034 wpa_s->scan_res_handler = scan_res_handler;
7035 os_free(wpa_s->manual_scan_freqs);
7036 wpa_s->manual_scan_freqs = manual_scan_freqs;
7037 manual_scan_freqs = NULL;
7038
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007039 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
7040 wpa_supplicant_cancel_sched_scan(wpa_s);
7041 wpa_s->scan_req = MANUAL_SCAN_REQ;
7042 wpa_supplicant_req_scan(wpa_s, 0, 0);
7043 if (wpa_s->manual_scan_use_id) {
7044 wpa_s->manual_scan_id++;
7045 *reply_len = os_snprintf(reply, reply_size, "%u\n",
7046 wpa_s->manual_scan_id);
7047 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
7048 wpa_s->manual_scan_id);
7049 }
7050 } else {
7051 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
7052 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
7053 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007054
7055done:
7056 os_free(manual_scan_freqs);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007057}
7058
7059
Dmitry Shmidt818ea482014-03-10 13:15:21 -07007060#ifdef CONFIG_TESTING_OPTIONS
7061
7062static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
7063 unsigned int freq, const u8 *dst,
7064 const u8 *src, const u8 *bssid,
7065 const u8 *data, size_t data_len,
7066 enum offchannel_send_action_result
7067 result)
7068{
7069 wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
7070 " src=" MACSTR " bssid=" MACSTR " result=%s",
7071 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
7072 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
7073 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
7074 "NO_ACK" : "FAILED"));
7075}
7076
7077
7078static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
7079{
7080 char *pos, *param;
7081 size_t len;
7082 u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
7083 int res, used;
7084 int freq = 0, no_cck = 0, wait_time = 0;
7085
7086 /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
7087 * <action=Action frame payload> */
7088
7089 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
7090
7091 pos = cmd;
7092 used = hwaddr_aton2(pos, da);
7093 if (used < 0)
7094 return -1;
7095 pos += used;
7096 while (*pos == ' ')
7097 pos++;
7098 used = hwaddr_aton2(pos, bssid);
7099 if (used < 0)
7100 return -1;
7101 pos += used;
7102
7103 param = os_strstr(pos, " freq=");
7104 if (param) {
7105 param += 6;
7106 freq = atoi(param);
7107 }
7108
7109 param = os_strstr(pos, " no_cck=");
7110 if (param) {
7111 param += 8;
7112 no_cck = atoi(param);
7113 }
7114
7115 param = os_strstr(pos, " wait_time=");
7116 if (param) {
7117 param += 11;
7118 wait_time = atoi(param);
7119 }
7120
7121 param = os_strstr(pos, " action=");
7122 if (param == NULL)
7123 return -1;
7124 param += 8;
7125
7126 len = os_strlen(param);
7127 if (len & 1)
7128 return -1;
7129 len /= 2;
7130
7131 buf = os_malloc(len);
7132 if (buf == NULL)
7133 return -1;
7134
7135 if (hexstr2bin(param, buf, len) < 0) {
7136 os_free(buf);
7137 return -1;
7138 }
7139
7140 res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
7141 buf, len, wait_time,
7142 wpas_ctrl_iface_mgmt_tx_cb, no_cck);
7143 os_free(buf);
7144 return res;
7145}
7146
7147
7148static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
7149{
7150 wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
7151 offchannel_send_action_done(wpa_s);
7152}
7153
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007154
7155static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
7156{
7157 char *pos, *param;
7158 union wpa_event_data event;
7159 enum wpa_event_type ev;
7160
7161 /* <event name> [parameters..] */
7162
7163 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
7164
7165 pos = cmd;
7166 param = os_strchr(pos, ' ');
7167 if (param)
7168 *param++ = '\0';
7169
7170 os_memset(&event, 0, sizeof(event));
7171
7172 if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
7173 ev = EVENT_INTERFACE_ENABLED;
7174 } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
7175 ev = EVENT_INTERFACE_DISABLED;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007176 } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
7177 ev = EVENT_AVOID_FREQUENCIES;
7178 if (param == NULL)
7179 param = "";
7180 if (freq_range_list_parse(&event.freq_range, param) < 0)
7181 return -1;
7182 wpa_supplicant_event(wpa_s, ev, &event);
7183 os_free(event.freq_range.range);
7184 return 0;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007185 } else {
7186 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
7187 cmd);
7188 return -1;
7189 }
7190
7191 wpa_supplicant_event(wpa_s, ev, &event);
7192
7193 return 0;
7194}
7195
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007196
7197static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
7198{
7199 char *pos;
7200 u8 src[ETH_ALEN], *buf;
7201 int used;
7202 size_t len;
7203
7204 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
7205
7206 pos = cmd;
7207 used = hwaddr_aton2(pos, src);
7208 if (used < 0)
7209 return -1;
7210 pos += used;
7211 while (*pos == ' ')
7212 pos++;
7213
7214 len = os_strlen(pos);
7215 if (len & 1)
7216 return -1;
7217 len /= 2;
7218
7219 buf = os_malloc(len);
7220 if (buf == NULL)
7221 return -1;
7222
7223 if (hexstr2bin(pos, buf, len) < 0) {
7224 os_free(buf);
7225 return -1;
7226 }
7227
7228 wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
7229 os_free(buf);
7230
7231 return 0;
7232}
7233
7234
7235static u16 ipv4_hdr_checksum(const void *buf, size_t len)
7236{
7237 size_t i;
7238 u32 sum = 0;
7239 const u16 *pos = buf;
7240
7241 for (i = 0; i < len / 2; i++)
7242 sum += *pos++;
7243
7244 while (sum >> 16)
7245 sum = (sum & 0xffff) + (sum >> 16);
7246
7247 return sum ^ 0xffff;
7248}
7249
7250
7251#define HWSIM_PACKETLEN 1500
7252#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
7253
7254void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
7255{
7256 struct wpa_supplicant *wpa_s = ctx;
7257 const struct ether_header *eth;
7258 const struct iphdr *ip;
7259 const u8 *pos;
7260 unsigned int i;
7261
7262 if (len != HWSIM_PACKETLEN)
7263 return;
7264
7265 eth = (const struct ether_header *) buf;
7266 ip = (const struct iphdr *) (eth + 1);
7267 pos = (const u8 *) (ip + 1);
7268
7269 if (ip->ihl != 5 || ip->version != 4 ||
7270 ntohs(ip->tot_len) != HWSIM_IP_LEN)
7271 return;
7272
7273 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++) {
7274 if (*pos != (u8) i)
7275 return;
7276 pos++;
7277 }
7278
7279 wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
7280 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
7281}
7282
7283
7284static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
7285 char *cmd)
7286{
7287 int enabled = atoi(cmd);
7288
7289 if (!enabled) {
7290 if (wpa_s->l2_test) {
7291 l2_packet_deinit(wpa_s->l2_test);
7292 wpa_s->l2_test = NULL;
7293 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
7294 }
7295 return 0;
7296 }
7297
7298 if (wpa_s->l2_test)
7299 return 0;
7300
7301 wpa_s->l2_test = l2_packet_init(wpa_s->ifname, wpa_s->own_addr,
7302 ETHERTYPE_IP, wpas_data_test_rx,
7303 wpa_s, 1);
7304 if (wpa_s->l2_test == NULL)
7305 return -1;
7306
7307 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
7308
7309 return 0;
7310}
7311
7312
7313static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
7314{
7315 u8 dst[ETH_ALEN], src[ETH_ALEN];
7316 char *pos;
7317 int used;
7318 long int val;
7319 u8 tos;
7320 u8 buf[HWSIM_PACKETLEN];
7321 struct ether_header *eth;
7322 struct iphdr *ip;
7323 u8 *dpos;
7324 unsigned int i;
7325
7326 if (wpa_s->l2_test == NULL)
7327 return -1;
7328
7329 /* format: <dst> <src> <tos> */
7330
7331 pos = cmd;
7332 used = hwaddr_aton2(pos, dst);
7333 if (used < 0)
7334 return -1;
7335 pos += used;
7336 while (*pos == ' ')
7337 pos++;
7338 used = hwaddr_aton2(pos, src);
7339 if (used < 0)
7340 return -1;
7341 pos += used;
7342
7343 val = strtol(pos, NULL, 0);
7344 if (val < 0 || val > 0xff)
7345 return -1;
7346 tos = val;
7347
7348 eth = (struct ether_header *) buf;
7349 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
7350 os_memcpy(eth->ether_shost, src, ETH_ALEN);
7351 eth->ether_type = htons(ETHERTYPE_IP);
7352 ip = (struct iphdr *) (eth + 1);
7353 os_memset(ip, 0, sizeof(*ip));
7354 ip->ihl = 5;
7355 ip->version = 4;
7356 ip->ttl = 64;
7357 ip->tos = tos;
7358 ip->tot_len = htons(HWSIM_IP_LEN);
7359 ip->protocol = 1;
7360 ip->saddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 1);
7361 ip->daddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 2);
7362 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
7363 dpos = (u8 *) (ip + 1);
7364 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
7365 *dpos++ = i;
7366
7367 if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, buf,
7368 HWSIM_PACKETLEN) < 0)
7369 return -1;
7370
7371 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
7372 " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
7373
7374 return 0;
7375}
7376
7377
7378static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
7379 char *cmd)
7380{
7381 u8 *buf;
7382 struct ether_header *eth;
7383 struct l2_packet_data *l2 = NULL;
7384 size_t len;
7385 u16 ethertype;
7386 int res = -1;
7387
7388 len = os_strlen(cmd);
7389 if (len & 1 || len < ETH_HLEN * 2)
7390 return -1;
7391 len /= 2;
7392
7393 buf = os_malloc(len);
7394 if (buf == NULL)
7395 return -1;
7396
7397 if (hexstr2bin(cmd, buf, len) < 0)
7398 goto done;
7399
7400 eth = (struct ether_header *) buf;
7401 ethertype = ntohs(eth->ether_type);
7402
7403 l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
7404 wpas_data_test_rx, wpa_s, 1);
7405 if (l2 == NULL)
7406 goto done;
7407
7408 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
7409 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
7410done:
7411 if (l2)
7412 l2_packet_deinit(l2);
7413 os_free(buf);
7414
7415 return res < 0 ? -1 : 0;
7416}
7417
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007418
7419static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
7420{
7421#ifdef WPA_TRACE_BFD
7422 extern char wpa_trace_fail_func[256];
7423 extern unsigned int wpa_trace_fail_after;
7424 char *pos;
7425
7426 wpa_trace_fail_after = atoi(cmd);
7427 pos = os_strchr(cmd, ':');
7428 if (pos) {
7429 pos++;
7430 os_strlcpy(wpa_trace_fail_func, pos,
7431 sizeof(wpa_trace_fail_func));
7432 } else {
7433 wpa_trace_fail_after = 0;
7434 }
7435 return 0;
7436#else /* WPA_TRACE_BFD */
7437 return -1;
7438#endif /* WPA_TRACE_BFD */
7439}
7440
7441
7442static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
7443 char *buf, size_t buflen)
7444{
7445#ifdef WPA_TRACE_BFD
7446 extern char wpa_trace_fail_func[256];
7447 extern unsigned int wpa_trace_fail_after;
7448
7449 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
7450 wpa_trace_fail_func);
7451#else /* WPA_TRACE_BFD */
7452 return -1;
7453#endif /* WPA_TRACE_BFD */
7454}
7455
Dmitry Shmidt818ea482014-03-10 13:15:21 -07007456#endif /* CONFIG_TESTING_OPTIONS */
7457
7458
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07007459static void wpas_ctrl_vendor_elem_update(struct wpa_supplicant *wpa_s)
7460{
7461 unsigned int i;
7462 char buf[30];
7463
7464 wpa_printf(MSG_DEBUG, "Update vendor elements");
7465
7466 for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
7467 if (wpa_s->vendor_elem[i]) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007468 int res;
7469
7470 res = os_snprintf(buf, sizeof(buf), "frame[%u]", i);
7471 if (!os_snprintf_error(sizeof(buf), res)) {
7472 wpa_hexdump_buf(MSG_DEBUG, buf,
7473 wpa_s->vendor_elem[i]);
7474 }
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07007475 }
7476 }
7477
7478#ifdef CONFIG_P2P
7479 if (wpa_s->parent == wpa_s &&
7480 wpa_s->global->p2p &&
7481 !wpa_s->global->p2p_disabled)
7482 p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
7483#endif /* CONFIG_P2P */
7484}
7485
7486
7487static struct wpa_supplicant *
7488wpas_ctrl_vendor_elem_iface(struct wpa_supplicant *wpa_s,
7489 enum wpa_vendor_elem_frame frame)
7490{
7491 switch (frame) {
7492#ifdef CONFIG_P2P
7493 case VENDOR_ELEM_PROBE_REQ_P2P:
7494 case VENDOR_ELEM_PROBE_RESP_P2P:
7495 case VENDOR_ELEM_PROBE_RESP_P2P_GO:
7496 case VENDOR_ELEM_BEACON_P2P_GO:
7497 case VENDOR_ELEM_P2P_PD_REQ:
7498 case VENDOR_ELEM_P2P_PD_RESP:
7499 case VENDOR_ELEM_P2P_GO_NEG_REQ:
7500 case VENDOR_ELEM_P2P_GO_NEG_RESP:
7501 case VENDOR_ELEM_P2P_GO_NEG_CONF:
7502 case VENDOR_ELEM_P2P_INV_REQ:
7503 case VENDOR_ELEM_P2P_INV_RESP:
7504 case VENDOR_ELEM_P2P_ASSOC_REQ:
7505 return wpa_s->parent;
7506#endif /* CONFIG_P2P */
7507 default:
7508 return wpa_s;
7509 }
7510}
7511
7512
7513static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
7514{
7515 char *pos = cmd;
7516 int frame;
7517 size_t len;
7518 struct wpabuf *buf;
7519 struct ieee802_11_elems elems;
7520
7521 frame = atoi(pos);
7522 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
7523 return -1;
7524 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
7525
7526 pos = os_strchr(pos, ' ');
7527 if (pos == NULL)
7528 return -1;
7529 pos++;
7530
7531 len = os_strlen(pos);
7532 if (len == 0)
7533 return 0;
7534 if (len & 1)
7535 return -1;
7536 len /= 2;
7537
7538 buf = wpabuf_alloc(len);
7539 if (buf == NULL)
7540 return -1;
7541
7542 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
7543 wpabuf_free(buf);
7544 return -1;
7545 }
7546
7547 if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
7548 ParseFailed) {
7549 wpabuf_free(buf);
7550 return -1;
7551 }
7552
7553 if (wpa_s->vendor_elem[frame] == NULL) {
7554 wpa_s->vendor_elem[frame] = buf;
7555 wpas_ctrl_vendor_elem_update(wpa_s);
7556 return 0;
7557 }
7558
7559 if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
7560 wpabuf_free(buf);
7561 return -1;
7562 }
7563
7564 wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
7565 wpabuf_free(buf);
7566 wpas_ctrl_vendor_elem_update(wpa_s);
7567
7568 return 0;
7569}
7570
7571
7572static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
7573 char *buf, size_t buflen)
7574{
7575 int frame = atoi(cmd);
7576
7577 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
7578 return -1;
7579 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
7580
7581 if (wpa_s->vendor_elem[frame] == NULL)
7582 return 0;
7583
7584 return wpa_snprintf_hex(buf, buflen,
7585 wpabuf_head_u8(wpa_s->vendor_elem[frame]),
7586 wpabuf_len(wpa_s->vendor_elem[frame]));
7587}
7588
7589
7590static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
7591{
7592 char *pos = cmd;
7593 int frame;
7594 size_t len;
7595 u8 *buf;
7596 struct ieee802_11_elems elems;
7597 u8 *ie, *end;
7598
7599 frame = atoi(pos);
7600 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
7601 return -1;
7602 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
7603
7604 pos = os_strchr(pos, ' ');
7605 if (pos == NULL)
7606 return -1;
7607 pos++;
7608
7609 if (*pos == '*') {
7610 wpabuf_free(wpa_s->vendor_elem[frame]);
7611 wpa_s->vendor_elem[frame] = NULL;
7612 wpas_ctrl_vendor_elem_update(wpa_s);
7613 return 0;
7614 }
7615
7616 if (wpa_s->vendor_elem[frame] == NULL)
7617 return -1;
7618
7619 len = os_strlen(pos);
7620 if (len == 0)
7621 return 0;
7622 if (len & 1)
7623 return -1;
7624 len /= 2;
7625
7626 buf = os_malloc(len);
7627 if (buf == NULL)
7628 return -1;
7629
7630 if (hexstr2bin(pos, buf, len) < 0) {
7631 os_free(buf);
7632 return -1;
7633 }
7634
7635 if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
7636 os_free(buf);
7637 return -1;
7638 }
7639
7640 ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
7641 end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
7642
7643 for (; ie + 1 < end; ie += 2 + ie[1]) {
7644 if (ie + len > end)
7645 break;
7646 if (os_memcmp(ie, buf, len) != 0)
7647 continue;
7648
7649 if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
7650 wpabuf_free(wpa_s->vendor_elem[frame]);
7651 wpa_s->vendor_elem[frame] = NULL;
7652 } else {
7653 os_memmove(ie, ie + len,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007654 end - (ie + len));
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07007655 wpa_s->vendor_elem[frame]->used -= len;
7656 }
7657 os_free(buf);
7658 wpas_ctrl_vendor_elem_update(wpa_s);
7659 return 0;
7660 }
7661
7662 os_free(buf);
7663
7664 return -1;
7665}
7666
7667
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007668static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
7669{
7670 struct wpa_supplicant *wpa_s = ctx;
7671
7672 if (neighbor_rep) {
7673 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
7674 "length=%u",
7675 (unsigned int) wpabuf_len(neighbor_rep));
7676 wpabuf_free(neighbor_rep);
7677 } else {
7678 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
7679 }
7680}
7681
7682
7683static int wpas_ctrl_iface_send_neigbor_rep(struct wpa_supplicant *wpa_s,
7684 char *cmd)
7685{
7686 struct wpa_ssid ssid;
7687 struct wpa_ssid *ssid_p = NULL;
7688 int ret = 0;
7689
7690 if (os_strncmp(cmd, " ssid=", 6) == 0) {
7691 ssid.ssid_len = os_strlen(cmd + 6);
7692 if (ssid.ssid_len > 32)
7693 return -1;
7694 ssid.ssid = (u8 *) (cmd + 6);
7695 ssid_p = &ssid;
7696 }
7697
7698 ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p,
7699 wpas_ctrl_neighbor_rep_cb,
7700 wpa_s);
7701
7702 return ret;
7703}
7704
7705
7706static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
7707{
7708 eapol_sm_erp_flush(wpa_s->eapol);
7709 return 0;
7710}
7711
7712
7713static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
7714 char *cmd)
7715{
7716 char *token, *context = NULL;
7717 unsigned int enable = ~0, type = 0;
7718 u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
7719 u8 *addr = NULL, *mask = NULL;
7720
7721 while ((token = str_token(cmd, " ", &context))) {
7722 if (os_strcasecmp(token, "scan") == 0) {
7723 type |= MAC_ADDR_RAND_SCAN;
7724 } else if (os_strcasecmp(token, "sched") == 0) {
7725 type |= MAC_ADDR_RAND_SCHED_SCAN;
7726 } else if (os_strcasecmp(token, "pno") == 0) {
7727 type |= MAC_ADDR_RAND_PNO;
7728 } else if (os_strcasecmp(token, "all") == 0) {
7729 type = wpa_s->mac_addr_rand_supported;
7730 } else if (os_strncasecmp(token, "enable=", 7) == 0) {
7731 enable = atoi(token + 7);
7732 } else if (os_strncasecmp(token, "addr=", 5) == 0) {
7733 addr = _addr;
7734 if (hwaddr_aton(token + 5, addr)) {
7735 wpa_printf(MSG_INFO,
7736 "CTRL: Invalid MAC address: %s",
7737 token);
7738 return -1;
7739 }
7740 } else if (os_strncasecmp(token, "mask=", 5) == 0) {
7741 mask = _mask;
7742 if (hwaddr_aton(token + 5, mask)) {
7743 wpa_printf(MSG_INFO,
7744 "CTRL: Invalid MAC address mask: %s",
7745 token);
7746 return -1;
7747 }
7748 } else {
7749 wpa_printf(MSG_INFO,
7750 "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
7751 token);
7752 return -1;
7753 }
7754 }
7755
7756 if (!type) {
7757 wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
7758 return -1;
7759 }
7760
7761 if ((wpa_s->mac_addr_rand_supported & type) != type) {
7762 wpa_printf(MSG_INFO,
7763 "CTRL: MAC_RAND_SCAN types=%u != supported=%u",
7764 type, wpa_s->mac_addr_rand_supported);
7765 return -1;
7766 }
7767
7768 if (enable > 1) {
7769 wpa_printf(MSG_INFO,
7770 "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
7771 return -1;
7772 }
7773
7774 if (!enable) {
7775 wpas_mac_addr_rand_scan_clear(wpa_s, type);
7776 if (wpa_s->pno) {
7777 if (type & MAC_ADDR_RAND_PNO) {
7778 wpas_stop_pno(wpa_s);
7779 wpas_start_pno(wpa_s);
7780 }
7781 } else if (wpa_s->sched_scanning &&
7782 (type & MAC_ADDR_RAND_SCHED_SCAN)) {
7783 /* simulate timeout to restart the sched scan */
7784 wpa_s->sched_scan_timed_out = 1;
7785 wpa_s->prev_sched_ssid = NULL;
7786 wpa_supplicant_cancel_sched_scan(wpa_s);
7787 }
7788 return 0;
7789 }
7790
7791 if ((addr && !mask) || (!addr && mask)) {
7792 wpa_printf(MSG_INFO,
7793 "CTRL: MAC_RAND_SCAN invalid addr/mask combination");
7794 return -1;
7795 }
7796
7797 if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
7798 wpa_printf(MSG_INFO,
7799 "CTRL: MAC_RAND_SCAN cannot allow multicast address");
7800 return -1;
7801 }
7802
7803 if (type & MAC_ADDR_RAND_SCAN) {
7804 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
7805 addr, mask);
7806 }
7807
7808 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
7809 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
7810 addr, mask);
7811
7812 if (wpa_s->sched_scanning && !wpa_s->pno) {
7813 /* simulate timeout to restart the sched scan */
7814 wpa_s->sched_scan_timed_out = 1;
7815 wpa_s->prev_sched_ssid = NULL;
7816 wpa_supplicant_cancel_sched_scan(wpa_s);
7817 }
7818 }
7819
7820 if (type & MAC_ADDR_RAND_PNO) {
7821 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
7822 addr, mask);
7823 if (wpa_s->pno) {
7824 wpas_stop_pno(wpa_s);
7825 wpas_start_pno(wpa_s);
7826 }
7827 }
7828
7829 return 0;
7830}
7831
7832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007833char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
7834 char *buf, size_t *resp_len)
7835{
7836 char *reply;
7837 const int reply_size = 4096;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007838 int reply_len;
7839
7840 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007841 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
7842 if (wpa_debug_show_keys)
7843 wpa_dbg(wpa_s, MSG_DEBUG,
7844 "Control interface command '%s'", buf);
7845 else
7846 wpa_dbg(wpa_s, MSG_DEBUG,
7847 "Control interface command '%s [REMOVED]'",
7848 os_strncmp(buf, WPA_CTRL_RSP,
7849 os_strlen(WPA_CTRL_RSP)) == 0 ?
7850 WPA_CTRL_RSP : "SET_NETWORK");
7851 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007852 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007853 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
7854 (const u8 *) buf, os_strlen(buf));
7855 } else {
7856 int level = MSG_DEBUG;
7857 if (os_strcmp(buf, "PING") == 0)
7858 level = MSG_EXCESSIVE;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007859 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007860 }
7861
7862 reply = os_malloc(reply_size);
7863 if (reply == NULL) {
7864 *resp_len = 1;
7865 return NULL;
7866 }
7867
7868 os_memcpy(reply, "OK\n", 3);
7869 reply_len = 3;
7870
7871 if (os_strcmp(buf, "PING") == 0) {
7872 os_memcpy(reply, "PONG\n", 5);
7873 reply_len = 5;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007874 } else if (os_strcmp(buf, "IFNAME") == 0) {
7875 reply_len = os_strlen(wpa_s->ifname);
7876 os_memcpy(reply, wpa_s->ifname, reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007877 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
7878 if (wpa_debug_reopen_file() < 0)
7879 reply_len = -1;
7880 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
7881 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
7882 } else if (os_strcmp(buf, "MIB") == 0) {
7883 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
7884 if (reply_len >= 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007885 reply_len += eapol_sm_get_mib(wpa_s->eapol,
7886 reply + reply_len,
7887 reply_size - reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007888 }
7889 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
7890 reply_len = wpa_supplicant_ctrl_iface_status(
7891 wpa_s, buf + 6, reply, reply_size);
7892 } else if (os_strcmp(buf, "PMKSA") == 0) {
7893 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
7894 reply_size);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007895 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
7896 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007897 } else if (os_strncmp(buf, "SET ", 4) == 0) {
7898 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
7899 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007900 } else if (os_strncmp(buf, "DUMP", 4) == 0) {
7901 reply_len = wpa_config_dump_values(wpa_s->conf,
7902 reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007903 } else if (os_strncmp(buf, "GET ", 4) == 0) {
7904 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
7905 reply, reply_size);
7906 } else if (os_strcmp(buf, "LOGON") == 0) {
7907 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
7908 } else if (os_strcmp(buf, "LOGOFF") == 0) {
7909 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
7910 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
7911 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
7912 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007913 else
7914 wpas_request_connection(wpa_s);
Dmitry Shmidt98660862014-03-11 17:26:21 -07007915 } else if (os_strcmp(buf, "REATTACH") == 0) {
7916 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
7917 !wpa_s->current_ssid)
7918 reply_len = -1;
7919 else {
7920 wpa_s->reattach = 1;
7921 wpas_request_connection(wpa_s);
7922 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007923 } else if (os_strcmp(buf, "RECONNECT") == 0) {
7924 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
7925 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007926 else if (wpa_s->disconnected)
7927 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007928#ifdef IEEE8021X_EAPOL
7929 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
7930 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
7931 reply_len = -1;
7932#endif /* IEEE8021X_EAPOL */
7933#ifdef CONFIG_PEERKEY
7934 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
7935 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
7936 reply_len = -1;
7937#endif /* CONFIG_PEERKEY */
7938#ifdef CONFIG_IEEE80211R
7939 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
7940 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
7941 reply_len = -1;
7942#endif /* CONFIG_IEEE80211R */
7943#ifdef CONFIG_WPS
7944 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
7945 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
7946 if (res == -2) {
7947 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
7948 reply_len = 17;
7949 } else if (res)
7950 reply_len = -1;
7951 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
7952 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
7953 if (res == -2) {
7954 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
7955 reply_len = 17;
7956 } else if (res)
7957 reply_len = -1;
7958 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
7959 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
7960 reply,
7961 reply_size);
7962 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
7963 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
7964 wpa_s, buf + 14, reply, reply_size);
7965 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
7966 if (wpas_wps_cancel(wpa_s))
7967 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007968#ifdef CONFIG_WPS_NFC
7969 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
7970 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
7971 reply_len = -1;
7972 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
7973 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
7974 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007975 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
7976 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
7977 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007978 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
7979 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
7980 wpa_s, buf + 14, reply, reply_size);
7981 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
7982 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
7983 buf + 17))
7984 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007985 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
7986 reply_len = wpas_ctrl_nfc_get_handover_req(
7987 wpa_s, buf + 21, reply, reply_size);
7988 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
7989 reply_len = wpas_ctrl_nfc_get_handover_sel(
7990 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007991 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
7992 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
7993 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007994#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007995 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
7996 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
7997 reply_len = -1;
7998#ifdef CONFIG_AP
7999 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
8000 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
8001 wpa_s, buf + 11, reply, reply_size);
8002#endif /* CONFIG_AP */
8003#ifdef CONFIG_WPS_ER
8004 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
8005 if (wpas_wps_er_start(wpa_s, NULL))
8006 reply_len = -1;
8007 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
8008 if (wpas_wps_er_start(wpa_s, buf + 13))
8009 reply_len = -1;
8010 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008011 wpas_wps_er_stop(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008012 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
8013 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
8014 reply_len = -1;
8015 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
8016 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
8017 if (ret == -2) {
8018 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
8019 reply_len = 17;
8020 } else if (ret == -3) {
8021 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
8022 reply_len = 18;
8023 } else if (ret == -4) {
8024 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
8025 reply_len = 20;
8026 } else if (ret)
8027 reply_len = -1;
8028 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
8029 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
8030 reply_len = -1;
8031 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
8032 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
8033 buf + 18))
8034 reply_len = -1;
8035 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
8036 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
8037 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008038#ifdef CONFIG_WPS_NFC
8039 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
8040 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
8041 wpa_s, buf + 24, reply, reply_size);
8042#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008043#endif /* CONFIG_WPS_ER */
8044#endif /* CONFIG_WPS */
8045#ifdef CONFIG_IBSS_RSN
8046 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
8047 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
8048 reply_len = -1;
8049#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008050#ifdef CONFIG_MESH
8051 } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
8052 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
8053 wpa_s, buf + 19, reply, reply_size);
8054 } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
8055 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
8056 wpa_s, "", reply, reply_size);
8057 } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
8058 if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
8059 reply_len = -1;
8060 } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
8061 if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
8062 buf + 18))
8063 reply_len = -1;
8064#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008065#ifdef CONFIG_P2P
8066 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008067 if (p2p_ctrl_find(wpa_s, buf + 8))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008068 reply_len = -1;
8069 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
8070 if (p2p_ctrl_find(wpa_s, ""))
8071 reply_len = -1;
8072 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
8073 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008074 } else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
8075 if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
8076 reply_len = -1;
8077 } else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
8078 if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
8079 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008080 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
8081 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
8082 reply_size);
8083 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
8084 if (p2p_ctrl_listen(wpa_s, buf + 11))
8085 reply_len = -1;
8086 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
8087 if (p2p_ctrl_listen(wpa_s, ""))
8088 reply_len = -1;
8089 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
8090 if (wpas_p2p_group_remove(wpa_s, buf + 17))
8091 reply_len = -1;
8092 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008093 if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008094 reply_len = -1;
8095 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
8096 if (p2p_ctrl_group_add(wpa_s, buf + 14))
8097 reply_len = -1;
8098 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
8099 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
8100 reply_len = -1;
8101 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
8102 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
8103 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
8104 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
8105 reply_size);
8106 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
8107 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
8108 reply_len = -1;
8109 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
8110 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
8111 reply_len = -1;
8112 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
8113 wpas_p2p_sd_service_update(wpa_s);
8114 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
8115 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
8116 reply_len = -1;
8117 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
8118 wpas_p2p_service_flush(wpa_s);
8119 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
8120 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
8121 reply_len = -1;
8122 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
8123 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
8124 reply_len = -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008125 } else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
8126 if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
8127 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008128 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
8129 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
8130 reply_len = -1;
8131 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
8132 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
8133 reply_len = -1;
8134 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
8135 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
8136 reply_size);
8137 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
8138 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
8139 reply_len = -1;
8140 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07008141 p2p_ctrl_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008142 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
8143 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
8144 reply_len = -1;
8145 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
8146 if (wpas_p2p_cancel(wpa_s))
8147 reply_len = -1;
8148 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
8149 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
8150 reply_len = -1;
8151 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
8152 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
8153 reply_len = -1;
8154 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
8155 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
8156 reply_len = -1;
8157 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
8158 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
8159 reply_len = -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008160 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
8161 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
8162 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008163#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008164#ifdef CONFIG_WIFI_DISPLAY
8165 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
8166 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
8167 reply_len = -1;
8168 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
8169 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
8170 reply, reply_size);
8171#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008172#ifdef CONFIG_INTERWORKING
8173 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
8174 if (interworking_fetch_anqp(wpa_s) < 0)
8175 reply_len = -1;
8176 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
8177 interworking_stop_fetch_anqp(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008178 } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
8179 if (ctrl_interworking_select(wpa_s, NULL) < 0)
8180 reply_len = -1;
8181 } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
8182 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008183 reply_len = -1;
8184 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008185 if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008186 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008187 } else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
8188 int id;
8189
8190 id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
8191 if (id < 0)
8192 reply_len = -1;
8193 else {
8194 reply_len = os_snprintf(reply, reply_size, "%d\n", id);
8195 if (os_snprintf_error(reply_size, reply_len))
8196 reply_len = -1;
8197 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008198 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
8199 if (get_anqp(wpa_s, buf + 9) < 0)
8200 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008201 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
8202 if (gas_request(wpa_s, buf + 12) < 0)
8203 reply_len = -1;
8204 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
8205 reply_len = gas_response_get(wpa_s, buf + 17, reply,
8206 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008207#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07008208#ifdef CONFIG_HS20
8209 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
8210 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
8211 reply_len = -1;
8212 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
8213 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
8214 reply_len = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008215 } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
8216 if (hs20_icon_request(wpa_s, buf + 18) < 0)
8217 reply_len = -1;
8218 } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
8219 if (hs20_fetch_osu(wpa_s) < 0)
8220 reply_len = -1;
8221 } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
8222 hs20_cancel_fetch_osu(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008223#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008224 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
8225 {
8226 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
8227 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
8228 reply_len = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07008229 else {
8230 /*
8231 * Notify response from timeout to allow the control
8232 * interface response to be sent first.
8233 */
8234 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
8235 wpa_s, NULL);
8236 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008237 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
8238 if (wpa_supplicant_reload_configuration(wpa_s))
8239 reply_len = -1;
8240 } else if (os_strcmp(buf, "TERMINATE") == 0) {
8241 wpa_supplicant_terminate_proc(wpa_s->global);
8242 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
8243 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
8244 reply_len = -1;
Dmitry Shmidte19501d2011-03-16 14:32:18 -07008245 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008246 reply_len = wpa_supplicant_ctrl_iface_blacklist(
8247 wpa_s, buf + 9, reply, reply_size);
8248 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
8249 reply_len = wpa_supplicant_ctrl_iface_log_level(
8250 wpa_s, buf + 9, reply, reply_size);
Vinit Deshpandeda134e92014-12-02 10:59:29 -08008251 } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
8252 reply_len = wpa_supplicant_ctrl_iface_list_networks(
8253 wpa_s, buf + 14, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008254 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
8255 reply_len = wpa_supplicant_ctrl_iface_list_networks(
Vinit Deshpandeda134e92014-12-02 10:59:29 -08008256 wpa_s, NULL, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008257 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008258#ifdef CONFIG_SME
8259 wpa_s->sme.prev_bssid_set = 0;
8260#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008261 wpa_s->reassociate = 0;
8262 wpa_s->disconnected = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008263 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008264 wpa_supplicant_cancel_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008265 wpa_supplicant_deauthenticate(wpa_s,
8266 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07008267 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008268 } else if (os_strcmp(buf, "SCAN") == 0) {
8269 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
8270 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
8271 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008272 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
8273 reply_len = wpa_supplicant_ctrl_iface_scan_results(
8274 wpa_s, reply, reply_size);
8275 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
8276 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
8277 reply_len = -1;
8278 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
8279 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
8280 reply_len = -1;
8281 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
8282 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
8283 reply_len = -1;
8284 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
8285 reply_len = wpa_supplicant_ctrl_iface_add_network(
8286 wpa_s, reply, reply_size);
8287 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
8288 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
8289 reply_len = -1;
8290 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
8291 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
8292 reply_len = -1;
8293 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
8294 reply_len = wpa_supplicant_ctrl_iface_get_network(
8295 wpa_s, buf + 12, reply, reply_size);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008296 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
8297 if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12))
8298 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008299 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
8300 reply_len = wpa_supplicant_ctrl_iface_list_creds(
8301 wpa_s, reply, reply_size);
8302 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
8303 reply_len = wpa_supplicant_ctrl_iface_add_cred(
8304 wpa_s, reply, reply_size);
8305 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
8306 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
8307 reply_len = -1;
8308 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
8309 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
8310 reply_len = -1;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07008311 } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
8312 reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
8313 reply,
8314 reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008315#ifndef CONFIG_NO_CONFIG_WRITE
8316 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
8317 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
8318 reply_len = -1;
8319#endif /* CONFIG_NO_CONFIG_WRITE */
8320 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
8321 reply_len = wpa_supplicant_ctrl_iface_get_capability(
8322 wpa_s, buf + 15, reply, reply_size);
8323 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
8324 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
8325 reply_len = -1;
8326 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
8327 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
8328 reply_len = -1;
8329 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
8330 reply_len = wpa_supplicant_global_iface_list(
8331 wpa_s->global, reply, reply_size);
8332 } else if (os_strcmp(buf, "INTERFACES") == 0) {
8333 reply_len = wpa_supplicant_global_iface_interfaces(
8334 wpa_s->global, reply, reply_size);
8335 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
8336 reply_len = wpa_supplicant_ctrl_iface_bss(
8337 wpa_s, buf + 4, reply, reply_size);
8338#ifdef CONFIG_AP
8339 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
8340 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
8341 } else if (os_strncmp(buf, "STA ", 4) == 0) {
8342 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
8343 reply_size);
8344 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
8345 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
8346 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008347 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
8348 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
8349 reply_len = -1;
8350 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
8351 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
8352 reply_len = -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008353 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
8354 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
8355 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008356 } else if (os_strcmp(buf, "STOP_AP") == 0) {
8357 if (wpas_ap_stop_ap(wpa_s))
8358 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008359#endif /* CONFIG_AP */
8360 } else if (os_strcmp(buf, "SUSPEND") == 0) {
8361 wpas_notify_suspend(wpa_s->global);
8362 } else if (os_strcmp(buf, "RESUME") == 0) {
8363 wpas_notify_resume(wpa_s->global);
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008364#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008365 } else if (os_strcmp(buf, "DROP_SA") == 0) {
8366 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008367#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008368 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
8369 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
8370 reply_len = -1;
8371 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008372 wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008373 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
8374 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
8375 reply_len = -1;
8376 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
8377 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
8378 buf + 17))
8379 reply_len = -1;
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07008380 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008381 wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008382#ifdef CONFIG_TDLS
8383 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
8384 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
8385 reply_len = -1;
8386 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
8387 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
8388 reply_len = -1;
8389 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
8390 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
8391 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008392 } else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
8393 if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
8394 buf + 17))
8395 reply_len = -1;
8396 } else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
8397 if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
8398 buf + 24))
8399 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008400#endif /* CONFIG_TDLS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008401 } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
8402 reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
8403 } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
8404 if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
8405 reply_len = -1;
8406 } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
8407 if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
8408 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008409 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
8410 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
8411 reply_size);
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07008412 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
8413 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
8414 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008415#ifdef CONFIG_AUTOSCAN
8416 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
8417 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
8418 reply_len = -1;
8419#endif /* CONFIG_AUTOSCAN */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008420#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07008421 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
8422 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
8423 reply_size);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008424#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008425 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
8426 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
8427 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008428 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008429 pmksa_cache_clear_current(wpa_s->wpa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008430 eapol_sm_request_reauth(wpa_s->eapol);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008431#ifdef CONFIG_WNM
8432 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
8433 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
8434 reply_len = -1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008435 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
8436 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
Dmitry Shmidt44c95782013-05-17 09:51:35 -07008437 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008438#endif /* CONFIG_WNM */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07008439 } else if (os_strcmp(buf, "FLUSH") == 0) {
8440 wpa_supplicant_ctrl_iface_flush(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008441 } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
8442 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
8443 reply_size);
Dmitry Shmidt818ea482014-03-10 13:15:21 -07008444#ifdef CONFIG_TESTING_OPTIONS
8445 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
8446 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
8447 reply_len = -1;
8448 } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
8449 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07008450 } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
8451 if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
8452 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008453 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
8454 if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
8455 reply_len = -1;
8456 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
8457 if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
8458 reply_len = -1;
8459 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
8460 if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
8461 reply_len = -1;
8462 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
8463 if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
8464 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008465 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
8466 if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
8467 reply_len = -1;
8468 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
8469 reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
Dmitry Shmidt818ea482014-03-10 13:15:21 -07008470#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008471 } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
8472 if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
8473 reply_len = -1;
8474 } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
8475 reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
8476 reply_size);
8477 } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
8478 if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
8479 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008480 } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
8481 if (wpas_ctrl_iface_send_neigbor_rep(wpa_s, buf + 20))
8482 reply_len = -1;
8483 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
8484 wpas_ctrl_iface_erp_flush(wpa_s);
8485 } else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
8486 if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
8487 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008488 } else {
8489 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
8490 reply_len = 16;
8491 }
8492
8493 if (reply_len < 0) {
8494 os_memcpy(reply, "FAIL\n", 5);
8495 reply_len = 5;
8496 }
8497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008498 *resp_len = reply_len;
8499 return reply;
8500}
8501
8502
8503static int wpa_supplicant_global_iface_add(struct wpa_global *global,
8504 char *cmd)
8505{
8506 struct wpa_interface iface;
8507 char *pos;
8508
8509 /*
8510 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
8511 * TAB<bridge_ifname>
8512 */
8513 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
8514
8515 os_memset(&iface, 0, sizeof(iface));
8516
8517 do {
8518 iface.ifname = pos = cmd;
8519 pos = os_strchr(pos, '\t');
8520 if (pos)
8521 *pos++ = '\0';
8522 if (iface.ifname[0] == '\0')
8523 return -1;
8524 if (pos == NULL)
8525 break;
8526
8527 iface.confname = pos;
8528 pos = os_strchr(pos, '\t');
8529 if (pos)
8530 *pos++ = '\0';
8531 if (iface.confname[0] == '\0')
8532 iface.confname = NULL;
8533 if (pos == NULL)
8534 break;
8535
8536 iface.driver = pos;
8537 pos = os_strchr(pos, '\t');
8538 if (pos)
8539 *pos++ = '\0';
8540 if (iface.driver[0] == '\0')
8541 iface.driver = NULL;
8542 if (pos == NULL)
8543 break;
8544
8545 iface.ctrl_interface = pos;
8546 pos = os_strchr(pos, '\t');
8547 if (pos)
8548 *pos++ = '\0';
8549 if (iface.ctrl_interface[0] == '\0')
8550 iface.ctrl_interface = NULL;
8551 if (pos == NULL)
8552 break;
8553
8554 iface.driver_param = pos;
8555 pos = os_strchr(pos, '\t');
8556 if (pos)
8557 *pos++ = '\0';
8558 if (iface.driver_param[0] == '\0')
8559 iface.driver_param = NULL;
8560 if (pos == NULL)
8561 break;
8562
8563 iface.bridge_ifname = pos;
8564 pos = os_strchr(pos, '\t');
8565 if (pos)
8566 *pos++ = '\0';
8567 if (iface.bridge_ifname[0] == '\0')
8568 iface.bridge_ifname = NULL;
8569 if (pos == NULL)
8570 break;
8571 } while (0);
8572
8573 if (wpa_supplicant_get_iface(global, iface.ifname))
8574 return -1;
8575
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08008576 return wpa_supplicant_add_iface(global, &iface, NULL) ? 0 : -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008577}
8578
8579
8580static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
8581 char *cmd)
8582{
8583 struct wpa_supplicant *wpa_s;
8584
8585 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
8586
8587 wpa_s = wpa_supplicant_get_iface(global, cmd);
8588 if (wpa_s == NULL)
8589 return -1;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07008590 return wpa_supplicant_remove_iface(global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008591}
8592
8593
8594static void wpa_free_iface_info(struct wpa_interface_info *iface)
8595{
8596 struct wpa_interface_info *prev;
8597
8598 while (iface) {
8599 prev = iface;
8600 iface = iface->next;
8601
8602 os_free(prev->ifname);
8603 os_free(prev->desc);
8604 os_free(prev);
8605 }
8606}
8607
8608
8609static int wpa_supplicant_global_iface_list(struct wpa_global *global,
8610 char *buf, int len)
8611{
8612 int i, res;
8613 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
8614 char *pos, *end;
8615
8616 for (i = 0; wpa_drivers[i]; i++) {
8617 struct wpa_driver_ops *drv = wpa_drivers[i];
8618 if (drv->get_interfaces == NULL)
8619 continue;
8620 tmp = drv->get_interfaces(global->drv_priv[i]);
8621 if (tmp == NULL)
8622 continue;
8623
8624 if (last == NULL)
8625 iface = last = tmp;
8626 else
8627 last->next = tmp;
8628 while (last->next)
8629 last = last->next;
8630 }
8631
8632 pos = buf;
8633 end = buf + len;
8634 for (tmp = iface; tmp; tmp = tmp->next) {
8635 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
8636 tmp->drv_name, tmp->ifname,
8637 tmp->desc ? tmp->desc : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008638 if (os_snprintf_error(end - pos, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008639 *pos = '\0';
8640 break;
8641 }
8642 pos += res;
8643 }
8644
8645 wpa_free_iface_info(iface);
8646
8647 return pos - buf;
8648}
8649
8650
8651static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
8652 char *buf, int len)
8653{
8654 int res;
8655 char *pos, *end;
8656 struct wpa_supplicant *wpa_s;
8657
8658 wpa_s = global->ifaces;
8659 pos = buf;
8660 end = buf + len;
8661
8662 while (wpa_s) {
8663 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008664 if (os_snprintf_error(end - pos, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008665 *pos = '\0';
8666 break;
8667 }
8668 pos += res;
8669 wpa_s = wpa_s->next;
8670 }
8671 return pos - buf;
8672}
8673
8674
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008675static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
8676 const char *ifname,
8677 char *cmd, size_t *resp_len)
8678{
8679 struct wpa_supplicant *wpa_s;
8680
8681 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8682 if (os_strcmp(ifname, wpa_s->ifname) == 0)
8683 break;
8684 }
8685
8686 if (wpa_s == NULL) {
8687 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
8688 if (resp)
8689 *resp_len = os_strlen(resp);
8690 else
8691 *resp_len = 1;
8692 return resp;
8693 }
8694
8695 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
8696}
8697
8698
8699static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
8700 char *buf, size_t *resp_len)
8701{
8702#ifdef CONFIG_P2P
8703 static const char * cmd[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07008704 "LIST_NETWORKS",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008705 "P2P_FIND",
8706 "P2P_STOP_FIND",
8707 "P2P_LISTEN",
8708 "P2P_GROUP_ADD",
8709 "P2P_GET_PASSPHRASE",
8710 "P2P_SERVICE_UPDATE",
8711 "P2P_SERVICE_FLUSH",
8712 "P2P_FLUSH",
8713 "P2P_CANCEL",
8714 "P2P_PRESENCE_REQ",
8715 "P2P_EXT_LISTEN",
8716 NULL
8717 };
8718 static const char * prefix[] = {
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08008719#ifdef ANDROID
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07008720 "DRIVER ",
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08008721#endif /* ANDROID */
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07008722 "GET_NETWORK ",
8723 "REMOVE_NETWORK ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008724 "P2P_FIND ",
8725 "P2P_CONNECT ",
8726 "P2P_LISTEN ",
8727 "P2P_GROUP_REMOVE ",
8728 "P2P_GROUP_ADD ",
8729 "P2P_PROV_DISC ",
8730 "P2P_SERV_DISC_REQ ",
8731 "P2P_SERV_DISC_CANCEL_REQ ",
8732 "P2P_SERV_DISC_RESP ",
8733 "P2P_SERV_DISC_EXTERNAL ",
8734 "P2P_SERVICE_ADD ",
8735 "P2P_SERVICE_DEL ",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008736 "P2P_SERVICE_REP ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008737 "P2P_REJECT ",
8738 "P2P_INVITE ",
8739 "P2P_PEER ",
8740 "P2P_SET ",
8741 "P2P_UNAUTHORIZE ",
8742 "P2P_PRESENCE_REQ ",
8743 "P2P_EXT_LISTEN ",
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008744 "P2P_REMOVE_CLIENT ",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008745 "WPS_NFC_TOKEN ",
8746 "WPS_NFC_TAG_READ ",
Dmitry Shmidt413dde72014-04-11 10:23:22 -07008747 "NFC_GET_HANDOVER_SEL ",
8748 "NFC_GET_HANDOVER_REQ ",
8749 "NFC_REPORT_HANDOVER ",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008750 "P2P_ASP_PROVISION ",
8751 "P2P_ASP_PROVISION_RESP ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008752 NULL
8753 };
8754 int found = 0;
8755 int i;
8756
8757 if (global->p2p_init_wpa_s == NULL)
8758 return NULL;
8759
8760 for (i = 0; !found && cmd[i]; i++) {
8761 if (os_strcmp(buf, cmd[i]) == 0)
8762 found = 1;
8763 }
8764
8765 for (i = 0; !found && prefix[i]; i++) {
8766 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
8767 found = 1;
8768 }
8769
8770 if (found)
8771 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
8772 buf, resp_len);
8773#endif /* CONFIG_P2P */
8774 return NULL;
8775}
8776
8777
8778static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
8779 char *buf, size_t *resp_len)
8780{
8781#ifdef CONFIG_WIFI_DISPLAY
8782 if (global->p2p_init_wpa_s == NULL)
8783 return NULL;
8784 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
8785 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
8786 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
8787 buf, resp_len);
8788#endif /* CONFIG_WIFI_DISPLAY */
8789 return NULL;
8790}
8791
8792
8793static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
8794 char *buf, size_t *resp_len)
8795{
8796 char *ret;
8797
8798 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
8799 if (ret)
8800 return ret;
8801
8802 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
8803 if (ret)
8804 return ret;
8805
8806 return NULL;
8807}
8808
8809
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008810static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
8811{
8812 char *value;
8813
8814 value = os_strchr(cmd, ' ');
8815 if (value == NULL)
8816 return -1;
8817 *value++ = '\0';
8818
8819 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
8820
8821#ifdef CONFIG_WIFI_DISPLAY
8822 if (os_strcasecmp(cmd, "wifi_display") == 0) {
8823 wifi_display_enable(global, !!atoi(value));
8824 return 0;
8825 }
8826#endif /* CONFIG_WIFI_DISPLAY */
8827
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008828 /* Restore cmd to its original value to allow redirection */
8829 value[-1] = ' ';
8830
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008831 return -1;
8832}
8833
8834
8835#ifndef CONFIG_NO_CONFIG_WRITE
8836static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
8837{
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008838 int ret = 0, saved = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008839 struct wpa_supplicant *wpa_s;
8840
8841 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8842 if (!wpa_s->conf->update_config) {
8843 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
8844 continue;
8845 }
8846
8847 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
8848 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
8849 ret = 1;
8850 } else {
8851 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008852 saved++;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008853 }
8854 }
8855
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008856 if (!saved && !ret) {
8857 wpa_dbg(wpa_s, MSG_DEBUG,
8858 "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
8859 ret = 1;
8860 }
8861
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008862 return ret;
8863}
8864#endif /* CONFIG_NO_CONFIG_WRITE */
8865
8866
8867static int wpas_global_ctrl_iface_status(struct wpa_global *global,
8868 char *buf, size_t buflen)
8869{
8870 char *pos, *end;
8871 int ret;
8872 struct wpa_supplicant *wpa_s;
8873
8874 pos = buf;
8875 end = buf + buflen;
8876
8877#ifdef CONFIG_P2P
8878 if (global->p2p && !global->p2p_disabled) {
8879 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
8880 "\n"
8881 "p2p_state=%s\n",
8882 MAC2STR(global->p2p_dev_addr),
8883 p2p_get_state_txt(global->p2p));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008884 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008885 return pos - buf;
8886 pos += ret;
8887 } else if (global->p2p) {
8888 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008889 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008890 return pos - buf;
8891 pos += ret;
8892 }
8893#endif /* CONFIG_P2P */
8894
8895#ifdef CONFIG_WIFI_DISPLAY
8896 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
8897 !!global->wifi_display);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008898 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008899 return pos - buf;
8900 pos += ret;
8901#endif /* CONFIG_WIFI_DISPLAY */
8902
8903 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8904 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
8905 "address=" MACSTR "\n",
8906 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008907 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008908 return pos - buf;
8909 pos += ret;
8910 }
8911
8912 return pos - buf;
8913}
8914
8915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008916char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
8917 char *buf, size_t *resp_len)
8918{
8919 char *reply;
8920 const int reply_size = 2048;
8921 int reply_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008922 int level = MSG_DEBUG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008923
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008924 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
8925 char *pos = os_strchr(buf + 7, ' ');
8926 if (pos) {
8927 *pos++ = '\0';
8928 return wpas_global_ctrl_iface_ifname(global,
8929 buf + 7, pos,
8930 resp_len);
8931 }
8932 }
8933
8934 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
8935 if (reply)
8936 return reply;
8937
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008938 if (os_strcmp(buf, "PING") == 0)
8939 level = MSG_EXCESSIVE;
8940 wpa_hexdump_ascii(level, "RX global ctrl_iface",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008941 (const u8 *) buf, os_strlen(buf));
8942
8943 reply = os_malloc(reply_size);
8944 if (reply == NULL) {
8945 *resp_len = 1;
8946 return NULL;
8947 }
8948
8949 os_memcpy(reply, "OK\n", 3);
8950 reply_len = 3;
8951
8952 if (os_strcmp(buf, "PING") == 0) {
8953 os_memcpy(reply, "PONG\n", 5);
8954 reply_len = 5;
8955 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
8956 if (wpa_supplicant_global_iface_add(global, buf + 14))
8957 reply_len = -1;
8958 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
8959 if (wpa_supplicant_global_iface_remove(global, buf + 17))
8960 reply_len = -1;
8961 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
8962 reply_len = wpa_supplicant_global_iface_list(
8963 global, reply, reply_size);
8964 } else if (os_strcmp(buf, "INTERFACES") == 0) {
8965 reply_len = wpa_supplicant_global_iface_interfaces(
8966 global, reply, reply_size);
8967 } else if (os_strcmp(buf, "TERMINATE") == 0) {
8968 wpa_supplicant_terminate_proc(global);
8969 } else if (os_strcmp(buf, "SUSPEND") == 0) {
8970 wpas_notify_suspend(global);
8971 } else if (os_strcmp(buf, "RESUME") == 0) {
8972 wpas_notify_resume(global);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008973 } else if (os_strncmp(buf, "SET ", 4) == 0) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008974 if (wpas_global_ctrl_iface_set(global, buf + 4)) {
8975#ifdef CONFIG_P2P
8976 if (global->p2p_init_wpa_s) {
8977 os_free(reply);
8978 /* Check if P2P redirection would work for this
8979 * command. */
8980 return wpa_supplicant_ctrl_iface_process(
8981 global->p2p_init_wpa_s,
8982 buf, resp_len);
8983 }
8984#endif /* CONFIG_P2P */
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008985 reply_len = -1;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008986 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008987#ifndef CONFIG_NO_CONFIG_WRITE
8988 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
8989 if (wpas_global_ctrl_iface_save_config(global))
8990 reply_len = -1;
8991#endif /* CONFIG_NO_CONFIG_WRITE */
8992 } else if (os_strcmp(buf, "STATUS") == 0) {
8993 reply_len = wpas_global_ctrl_iface_status(global, reply,
8994 reply_size);
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08008995#ifdef CONFIG_MODULE_TESTS
8996 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
8997 int wpas_module_tests(void);
8998 if (wpas_module_tests() < 0)
8999 reply_len = -1;
9000#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009001 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
9002 if (wpa_debug_reopen_file() < 0)
9003 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009004 } else {
9005 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
9006 reply_len = 16;
9007 }
9008
9009 if (reply_len < 0) {
9010 os_memcpy(reply, "FAIL\n", 5);
9011 reply_len = 5;
9012 }
9013
9014 *resp_len = reply_len;
9015 return reply;
9016}