blob: caa480cb5b8ccb043d4adec4004f3d7be2a7ca5e [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;
212 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
213 wpa_supplicant_req_scan(wpa_s, 0, 0);
214
215 return 0;
216}
217
218
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700219#ifndef CONFIG_NO_CONFIG_BLOBS
220static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
221{
222 char *name = pos;
223 struct wpa_config_blob *blob;
224 size_t len;
225
226 pos = os_strchr(pos, ' ');
227 if (pos == NULL)
228 return -1;
229 *pos++ = '\0';
230 len = os_strlen(pos);
231 if (len & 1)
232 return -1;
233
234 wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
235 blob = os_zalloc(sizeof(*blob));
236 if (blob == NULL)
237 return -1;
238 blob->name = os_strdup(name);
239 blob->data = os_malloc(len / 2);
240 if (blob->name == NULL || blob->data == NULL) {
241 wpa_config_free_blob(blob);
242 return -1;
243 }
244
245 if (hexstr2bin(pos, blob->data, len / 2) < 0) {
246 wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
247 wpa_config_free_blob(blob);
248 return -1;
249 }
250 blob->len = len / 2;
251
252 wpa_config_set_blob(wpa_s->conf, blob);
253
254 return 0;
255}
256#endif /* CONFIG_NO_CONFIG_BLOBS */
257
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700258
259static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
260{
261 char *params;
262 char *pos;
263 int *freqs = NULL;
264 int ret;
265
266 if (atoi(cmd)) {
267 params = os_strchr(cmd, ' ');
268 os_free(wpa_s->manual_sched_scan_freqs);
269 if (params) {
270 params++;
271 pos = os_strstr(params, "freq=");
272 if (pos)
273 freqs = freq_range_to_channel_list(wpa_s,
274 pos + 5);
275 }
276 wpa_s->manual_sched_scan_freqs = freqs;
277 ret = wpas_start_pno(wpa_s);
278 } else {
279 ret = wpas_stop_pno(wpa_s);
280 }
281 return ret;
282}
283
284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
286 char *cmd)
287{
288 char *value;
289 int ret = 0;
290
291 value = os_strchr(cmd, ' ');
292 if (value == NULL)
293 return -1;
294 *value++ = '\0';
295
296 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
297 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
298 eapol_sm_configure(wpa_s->eapol,
299 atoi(value), -1, -1, -1);
300 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
301 eapol_sm_configure(wpa_s->eapol,
302 -1, atoi(value), -1, -1);
303 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
304 eapol_sm_configure(wpa_s->eapol,
305 -1, -1, atoi(value), -1);
306 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
307 eapol_sm_configure(wpa_s->eapol,
308 -1, -1, -1, atoi(value));
309 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
310 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
311 atoi(value)))
312 ret = -1;
313 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
314 0) {
315 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
316 atoi(value)))
317 ret = -1;
318 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
319 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
320 ret = -1;
321 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
322 wpa_s->wps_fragment_size = atoi(value);
323#ifdef CONFIG_WPS_TESTING
324 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
325 long int val;
326 val = strtol(value, NULL, 0);
327 if (val < 0 || val > 0xff) {
328 ret = -1;
329 wpa_printf(MSG_DEBUG, "WPS: Invalid "
330 "wps_version_number %ld", val);
331 } else {
332 wps_version_number = val;
333 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
334 "version %u.%u",
335 (wps_version_number & 0xf0) >> 4,
336 wps_version_number & 0x0f);
337 }
338 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
339 wps_testing_dummy_cred = atoi(value);
340 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
341 wps_testing_dummy_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800342 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
343 wps_corrupt_pkhash = atoi(value);
344 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
345 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346#endif /* CONFIG_WPS_TESTING */
347 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
348 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
349 ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800350#ifdef CONFIG_TDLS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351#ifdef CONFIG_TDLS_TESTING
352 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
353 extern unsigned int tdls_testing;
354 tdls_testing = strtol(value, NULL, 0);
355 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
356#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
358 int disabled = atoi(value);
359 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
360 if (disabled) {
361 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
362 ret = -1;
363 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
364 ret = -1;
365 wpa_tdls_enable(wpa_s->wpa, !disabled);
366#endif /* CONFIG_TDLS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800367 } else if (os_strcasecmp(cmd, "pno") == 0) {
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700368 ret = wpas_ctrl_pno(wpa_s, value);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700369 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
370 int disabled = atoi(value);
371 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
372 ret = -1;
373 else if (disabled)
374 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
375 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
376 if (os_strcmp(value, "disable") == 0)
377 wpa_s->set_sta_uapsd = 0;
378 else {
379 int be, bk, vi, vo;
380 char *pos;
381 /* format: BE,BK,VI,VO;max SP Length */
382 be = atoi(value);
383 pos = os_strchr(value, ',');
384 if (pos == NULL)
385 return -1;
386 pos++;
387 bk = atoi(pos);
388 pos = os_strchr(pos, ',');
389 if (pos == NULL)
390 return -1;
391 pos++;
392 vi = atoi(pos);
393 pos = os_strchr(pos, ',');
394 if (pos == NULL)
395 return -1;
396 pos++;
397 vo = atoi(pos);
398 /* ignore max SP Length for now */
399
400 wpa_s->set_sta_uapsd = 1;
401 wpa_s->sta_uapsd = 0;
402 if (be)
403 wpa_s->sta_uapsd |= BIT(0);
404 if (bk)
405 wpa_s->sta_uapsd |= BIT(1);
406 if (vi)
407 wpa_s->sta_uapsd |= BIT(2);
408 if (vo)
409 wpa_s->sta_uapsd |= BIT(3);
410 }
Jouni Malinen21d6bc82012-04-10 16:17:59 -0700411 } else if (os_strcasecmp(cmd, "ps") == 0) {
412 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700413#ifdef CONFIG_WIFI_DISPLAY
414 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
Dmitry Shmidted003d22014-02-06 10:09:12 -0800415 int enabled = !!atoi(value);
416 if (enabled && !wpa_s->global->p2p)
417 ret = -1;
418 else
419 wifi_display_enable(wpa_s->global, enabled);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700420#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700421 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
422 ret = set_bssid_filter(wpa_s, value);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800423 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
424 ret = set_disallow_aps(wpa_s, value);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800425 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
426 wpa_s->no_keep_alive = !!atoi(value);
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700427#ifdef CONFIG_TESTING_OPTIONS
428 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
429 wpa_s->ext_mgmt_frame_handling = !!atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800430 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
431 wpa_s->ext_eapol_frame_io = !!atoi(value);
432#ifdef CONFIG_AP
433 if (wpa_s->ap_iface) {
434 wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
435 wpa_s->ext_eapol_frame_io;
436 }
437#endif /* CONFIG_AP */
438 } else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
439 wpa_s->extra_roc_dur = atoi(value);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800440 } else if (os_strcasecmp(cmd, "test_failure") == 0) {
441 wpa_s->test_failure = atoi(value);
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700442#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700443#ifndef CONFIG_NO_CONFIG_BLOBS
444 } else if (os_strcmp(cmd, "blob") == 0) {
445 ret = wpas_ctrl_set_blob(wpa_s, value);
446#endif /* CONFIG_NO_CONFIG_BLOBS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800447 } else if (os_strcasecmp(cmd, "setband") == 0) {
448 if (os_strcmp(value, "AUTO") == 0)
449 wpa_s->setband = WPA_SETBAND_AUTO;
450 else if (os_strcmp(value, "5G") == 0)
451 wpa_s->setband = WPA_SETBAND_5G;
452 else if (os_strcmp(value, "2G") == 0)
453 wpa_s->setband = WPA_SETBAND_2G;
454 else
455 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456 } else {
457 value[-1] = '=';
458 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
459 if (ret == 0)
460 wpa_supplicant_update_config(wpa_s);
461 }
462
463 return ret;
464}
465
466
467static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
468 char *cmd, char *buf, size_t buflen)
469{
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700470 int res = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700471
472 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
473
474 if (os_strcmp(cmd, "version") == 0) {
475 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700476 } else if (os_strcasecmp(cmd, "country") == 0) {
477 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
478 res = os_snprintf(buf, buflen, "%c%c",
479 wpa_s->conf->country[0],
480 wpa_s->conf->country[1]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700481#ifdef CONFIG_WIFI_DISPLAY
482 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
Dmitry Shmidted003d22014-02-06 10:09:12 -0800483 int enabled;
484 if (wpa_s->global->p2p == NULL ||
485 wpa_s->global->p2p_disabled)
486 enabled = 0;
487 else
488 enabled = wpa_s->global->wifi_display;
489 res = os_snprintf(buf, buflen, "%d", enabled);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700490#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700491#ifdef CONFIG_TESTING_GET_GTK
492 } else if (os_strcmp(cmd, "gtk") == 0) {
493 if (wpa_s->last_gtk_len == 0)
494 return -1;
495 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
496 wpa_s->last_gtk_len);
497 return res;
498#endif /* CONFIG_TESTING_GET_GTK */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800499 } else if (os_strcmp(cmd, "tls_library") == 0) {
500 res = tls_get_library_version(buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501 }
502
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800503 if (os_snprintf_error(buflen, res))
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700504 return -1;
505 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506}
507
508
509#ifdef IEEE8021X_EAPOL
510static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
511 char *addr)
512{
513 u8 bssid[ETH_ALEN];
514 struct wpa_ssid *ssid = wpa_s->current_ssid;
515
516 if (hwaddr_aton(addr, bssid)) {
517 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
518 "'%s'", addr);
519 return -1;
520 }
521
522 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
523 rsn_preauth_deinit(wpa_s->wpa);
524 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
525 return -1;
526
527 return 0;
528}
529#endif /* IEEE8021X_EAPOL */
530
531
532#ifdef CONFIG_PEERKEY
533/* MLME-STKSTART.request(peer) */
534static int wpa_supplicant_ctrl_iface_stkstart(
535 struct wpa_supplicant *wpa_s, char *addr)
536{
537 u8 peer[ETH_ALEN];
538
539 if (hwaddr_aton(addr, peer)) {
540 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
541 "address '%s'", addr);
542 return -1;
543 }
544
545 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
546 MAC2STR(peer));
547
548 return wpa_sm_stkstart(wpa_s->wpa, peer);
549}
550#endif /* CONFIG_PEERKEY */
551
552
553#ifdef CONFIG_TDLS
554
555static int wpa_supplicant_ctrl_iface_tdls_discover(
556 struct wpa_supplicant *wpa_s, char *addr)
557{
558 u8 peer[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800559 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560
561 if (hwaddr_aton(addr, peer)) {
562 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
563 "address '%s'", addr);
564 return -1;
565 }
566
567 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
568 MAC2STR(peer));
569
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800570 if (wpa_tdls_is_external_setup(wpa_s->wpa))
571 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
572 else
573 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
574
575 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700576}
577
578
579static int wpa_supplicant_ctrl_iface_tdls_setup(
580 struct wpa_supplicant *wpa_s, char *addr)
581{
582 u8 peer[ETH_ALEN];
583 int ret;
584
585 if (hwaddr_aton(addr, peer)) {
586 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
587 "address '%s'", addr);
588 return -1;
589 }
590
591 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
592 MAC2STR(peer));
593
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800594 if ((wpa_s->conf->tdls_external_control) &&
595 wpa_tdls_is_external_setup(wpa_s->wpa))
596 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
597
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800598 wpa_tdls_remove(wpa_s->wpa, peer);
599
600 if (wpa_tdls_is_external_setup(wpa_s->wpa))
601 ret = wpa_tdls_start(wpa_s->wpa, peer);
602 else
603 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605 return ret;
606}
607
608
609static int wpa_supplicant_ctrl_iface_tdls_teardown(
610 struct wpa_supplicant *wpa_s, char *addr)
611{
612 u8 peer[ETH_ALEN];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700613 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700615 if (os_strcmp(addr, "*") == 0) {
616 /* remove everyone */
617 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
618 wpa_tdls_teardown_peers(wpa_s->wpa);
619 return 0;
620 }
621
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700622 if (hwaddr_aton(addr, peer)) {
623 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
624 "address '%s'", addr);
625 return -1;
626 }
627
628 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
629 MAC2STR(peer));
630
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800631 if ((wpa_s->conf->tdls_external_control) &&
632 wpa_tdls_is_external_setup(wpa_s->wpa))
633 return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
634
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700635 if (wpa_tdls_is_external_setup(wpa_s->wpa))
636 ret = wpa_tdls_teardown_link(
637 wpa_s->wpa, peer,
638 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
639 else
640 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
641
642 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643}
644
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700645
646static int ctrl_iface_get_capability_tdls(
647 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
648{
649 int ret;
650
651 ret = os_snprintf(buf, buflen, "%s\n",
652 wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
653 (wpa_s->drv_flags &
654 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
655 "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800656 if (os_snprintf_error(buflen, ret))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700657 return -1;
658 return ret;
659}
660
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800661
662static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
663 struct wpa_supplicant *wpa_s, char *cmd)
664{
665 u8 peer[ETH_ALEN];
666 struct hostapd_freq_params freq_params;
667 u8 oper_class;
668 char *pos, *end;
669
670 if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
671 wpa_printf(MSG_INFO,
672 "tdls_chanswitch: Only supported with external setup");
673 return -1;
674 }
675
676 os_memset(&freq_params, 0, sizeof(freq_params));
677
678 pos = os_strchr(cmd, ' ');
679 if (pos == NULL)
680 return -1;
681 *pos++ = '\0';
682
683 oper_class = strtol(pos, &end, 10);
684 if (pos == end) {
685 wpa_printf(MSG_INFO,
686 "tdls_chanswitch: Invalid op class provided");
687 return -1;
688 }
689
690 pos = end;
691 freq_params.freq = atoi(pos);
692 if (freq_params.freq == 0) {
693 wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
694 return -1;
695 }
696
697#define SET_FREQ_SETTING(str) \
698 do { \
699 const char *pos2 = os_strstr(pos, " " #str "="); \
700 if (pos2) { \
701 pos2 += sizeof(" " #str "=") - 1; \
702 freq_params.str = atoi(pos2); \
703 } \
704 } while (0)
705
706 SET_FREQ_SETTING(center_freq1);
707 SET_FREQ_SETTING(center_freq2);
708 SET_FREQ_SETTING(bandwidth);
709 SET_FREQ_SETTING(sec_channel_offset);
710#undef SET_FREQ_SETTING
711
712 freq_params.ht_enabled = !!os_strstr(pos, " ht");
713 freq_params.vht_enabled = !!os_strstr(pos, " vht");
714
715 if (hwaddr_aton(cmd, peer)) {
716 wpa_printf(MSG_DEBUG,
717 "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
718 cmd);
719 return -1;
720 }
721
722 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
723 " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
724 MAC2STR(peer), oper_class, freq_params.freq,
725 freq_params.center_freq1, freq_params.center_freq2,
726 freq_params.bandwidth, freq_params.sec_channel_offset,
727 freq_params.ht_enabled ? " HT" : "",
728 freq_params.vht_enabled ? " VHT" : "");
729
730 return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
731 &freq_params);
732}
733
734
735static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
736 struct wpa_supplicant *wpa_s, char *cmd)
737{
738 u8 peer[ETH_ALEN];
739
740 if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
741 wpa_printf(MSG_INFO,
742 "tdls_chanswitch: Only supported with external setup");
743 return -1;
744 }
745
746 if (hwaddr_aton(cmd, peer)) {
747 wpa_printf(MSG_DEBUG,
748 "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
749 cmd);
750 return -1;
751 }
752
753 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
754 MAC2STR(peer));
755
756 return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
757}
758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759#endif /* CONFIG_TDLS */
760
761
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800762static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
763{
764 char *token, *context = NULL;
765 struct wmm_ac_ts_setup_params params = {
766 .tsid = 0xff,
767 .direction = 0xff,
768 };
769
770 while ((token = str_token(cmd, " ", &context))) {
771 if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
772 sscanf(token, "up=%i", &params.user_priority) == 1 ||
773 sscanf(token, "nominal_msdu_size=%i",
774 &params.nominal_msdu_size) == 1 ||
775 sscanf(token, "mean_data_rate=%i",
776 &params.mean_data_rate) == 1 ||
777 sscanf(token, "min_phy_rate=%i",
778 &params.minimum_phy_rate) == 1 ||
779 sscanf(token, "sba=%i",
780 &params.surplus_bandwidth_allowance) == 1)
781 continue;
782
783 if (os_strcasecmp(token, "downlink") == 0) {
784 params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
785 } else if (os_strcasecmp(token, "uplink") == 0) {
786 params.direction = WMM_TSPEC_DIRECTION_UPLINK;
787 } else if (os_strcasecmp(token, "bidi") == 0) {
788 params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
789 } else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
790 params.fixed_nominal_msdu = 1;
791 } else {
792 wpa_printf(MSG_DEBUG,
793 "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
794 token);
795 return -1;
796 }
797
798 }
799
800 return wpas_wmm_ac_addts(wpa_s, &params);
801}
802
803
804static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
805{
806 u8 tsid = atoi(cmd);
807
808 return wpas_wmm_ac_delts(wpa_s, tsid);
809}
810
811
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812#ifdef CONFIG_IEEE80211R
813static int wpa_supplicant_ctrl_iface_ft_ds(
814 struct wpa_supplicant *wpa_s, char *addr)
815{
816 u8 target_ap[ETH_ALEN];
817 struct wpa_bss *bss;
818 const u8 *mdie;
819
820 if (hwaddr_aton(addr, target_ap)) {
821 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
822 "address '%s'", addr);
823 return -1;
824 }
825
826 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
827
828 bss = wpa_bss_get_bssid(wpa_s, target_ap);
829 if (bss)
830 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
831 else
832 mdie = NULL;
833
834 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
835}
836#endif /* CONFIG_IEEE80211R */
837
838
839#ifdef CONFIG_WPS
840static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
841 char *cmd)
842{
843 u8 bssid[ETH_ALEN], *_bssid = bssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700844#ifdef CONFIG_P2P
845 u8 p2p_dev_addr[ETH_ALEN];
846#endif /* CONFIG_P2P */
847#ifdef CONFIG_AP
848 u8 *_p2p_dev_addr = NULL;
849#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800850
Dmitry Shmidtc0a8db02013-11-08 11:18:33 -0800851 if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 _bssid = NULL;
853#ifdef CONFIG_P2P
854 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
855 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
856 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
857 "P2P Device Address '%s'",
858 cmd + 13);
859 return -1;
860 }
861 _p2p_dev_addr = p2p_dev_addr;
862#endif /* CONFIG_P2P */
863 } else if (hwaddr_aton(cmd, bssid)) {
864 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
865 cmd);
866 return -1;
867 }
868
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800869#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 if (wpa_s->ap_iface)
871 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
872#endif /* CONFIG_AP */
873
874 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
875}
876
877
878static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
879 char *cmd, char *buf,
880 size_t buflen)
881{
882 u8 bssid[ETH_ALEN], *_bssid = bssid;
883 char *pin;
884 int ret;
885
886 pin = os_strchr(cmd, ' ');
887 if (pin)
888 *pin++ = '\0';
889
890 if (os_strcmp(cmd, "any") == 0)
891 _bssid = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800892 else if (os_strcmp(cmd, "get") == 0) {
893 ret = wps_generate_pin();
894 goto done;
895 } else if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
897 cmd);
898 return -1;
899 }
900
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800901#ifdef CONFIG_AP
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800902 if (wpa_s->ap_iface) {
903 int timeout = 0;
904 char *pos;
905
906 if (pin) {
907 pos = os_strchr(pin, ' ');
908 if (pos) {
909 *pos++ = '\0';
910 timeout = atoi(pos);
911 }
912 }
913
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800915 buf, buflen, timeout);
916 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917#endif /* CONFIG_AP */
918
919 if (pin) {
920 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
921 DEV_PW_DEFAULT);
922 if (ret < 0)
923 return -1;
924 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800925 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 return -1;
927 return ret;
928 }
929
930 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
931 if (ret < 0)
932 return -1;
933
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800934done:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935 /* Return the generated PIN */
936 ret = os_snprintf(buf, buflen, "%08d", ret);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800937 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938 return -1;
939 return ret;
940}
941
942
943static int wpa_supplicant_ctrl_iface_wps_check_pin(
944 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
945{
946 char pin[9];
947 size_t len;
948 char *pos;
949 int ret;
950
951 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
952 (u8 *) cmd, os_strlen(cmd));
953 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
954 if (*pos < '0' || *pos > '9')
955 continue;
956 pin[len++] = *pos;
957 if (len == 9) {
958 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
959 return -1;
960 }
961 }
962 if (len != 4 && len != 8) {
963 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
964 return -1;
965 }
966 pin[len] = '\0';
967
968 if (len == 8) {
969 unsigned int pin_val;
970 pin_val = atoi(pin);
971 if (!wps_pin_valid(pin_val)) {
972 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
973 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800974 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 return -1;
976 return ret;
977 }
978 }
979
980 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800981 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700982 return -1;
983
984 return ret;
985}
986
987
Dmitry Shmidt04949592012-07-19 12:16:46 -0700988#ifdef CONFIG_WPS_NFC
989
990static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
991 char *cmd)
992{
993 u8 bssid[ETH_ALEN], *_bssid = bssid;
994
995 if (cmd == NULL || cmd[0] == '\0')
996 _bssid = NULL;
997 else if (hwaddr_aton(cmd, bssid))
998 return -1;
999
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001000 return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
1001 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001002}
1003
1004
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001005static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
1006 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1007{
1008 int ndef;
1009 struct wpabuf *buf;
1010 int res;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001011 char *pos;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001012
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001013 pos = os_strchr(cmd, ' ');
1014 if (pos)
1015 *pos++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001016 if (os_strcmp(cmd, "WPS") == 0)
1017 ndef = 0;
1018 else if (os_strcmp(cmd, "NDEF") == 0)
1019 ndef = 1;
1020 else
1021 return -1;
1022
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001023 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001024 if (buf == NULL)
1025 return -1;
1026
1027 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1028 wpabuf_len(buf));
1029 reply[res++] = '\n';
1030 reply[res] = '\0';
1031
1032 wpabuf_free(buf);
1033
1034 return res;
1035}
1036
1037
Dmitry Shmidt04949592012-07-19 12:16:46 -07001038static int wpa_supplicant_ctrl_iface_wps_nfc_token(
1039 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1040{
1041 int ndef;
1042 struct wpabuf *buf;
1043 int res;
1044
1045 if (os_strcmp(cmd, "WPS") == 0)
1046 ndef = 0;
1047 else if (os_strcmp(cmd, "NDEF") == 0)
1048 ndef = 1;
1049 else
1050 return -1;
1051
1052 buf = wpas_wps_nfc_token(wpa_s, ndef);
1053 if (buf == NULL)
1054 return -1;
1055
1056 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1057 wpabuf_len(buf));
1058 reply[res++] = '\n';
1059 reply[res] = '\0';
1060
1061 wpabuf_free(buf);
1062
1063 return res;
1064}
1065
1066
1067static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
1068 struct wpa_supplicant *wpa_s, char *pos)
1069{
1070 size_t len;
1071 struct wpabuf *buf;
1072 int ret;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001073 char *freq;
1074 int forced_freq = 0;
1075
1076 freq = strstr(pos, " freq=");
1077 if (freq) {
1078 *freq = '\0';
1079 freq += 6;
1080 forced_freq = atoi(freq);
1081 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001082
1083 len = os_strlen(pos);
1084 if (len & 0x01)
1085 return -1;
1086 len /= 2;
1087
1088 buf = wpabuf_alloc(len);
1089 if (buf == NULL)
1090 return -1;
1091 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
1092 wpabuf_free(buf);
1093 return -1;
1094 }
1095
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001096 ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001097 wpabuf_free(buf);
1098
1099 return ret;
1100}
1101
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001102
1103static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001104 char *reply, size_t max_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001105 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001106{
1107 struct wpabuf *buf;
1108 int res;
1109
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001110 buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001111 if (buf == NULL)
1112 return -1;
1113
1114 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1115 wpabuf_len(buf));
1116 reply[res++] = '\n';
1117 reply[res] = '\0';
1118
1119 wpabuf_free(buf);
1120
1121 return res;
1122}
1123
1124
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001125#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001126static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
1127 char *reply, size_t max_len,
1128 int ndef)
1129{
1130 struct wpabuf *buf;
1131 int res;
1132
1133 buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
1134 if (buf == NULL) {
1135 wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
1136 return -1;
1137 }
1138
1139 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1140 wpabuf_len(buf));
1141 reply[res++] = '\n';
1142 reply[res] = '\0';
1143
1144 wpabuf_free(buf);
1145
1146 return res;
1147}
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001148#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001149
1150
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001151static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
1152 char *cmd, char *reply,
1153 size_t max_len)
1154{
1155 char *pos;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001156 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001157
1158 pos = os_strchr(cmd, ' ');
1159 if (pos == NULL)
1160 return -1;
1161 *pos++ = '\0';
1162
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001163 if (os_strcmp(cmd, "WPS") == 0)
1164 ndef = 0;
1165 else if (os_strcmp(cmd, "NDEF") == 0)
1166 ndef = 1;
1167 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001168 return -1;
1169
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001170 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001171 if (!ndef)
1172 return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001173 return wpas_ctrl_nfc_get_handover_req_wps(
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001174 wpa_s, reply, max_len, ndef);
1175 }
1176
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001177#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001178 if (os_strcmp(pos, "P2P-CR") == 0) {
1179 return wpas_ctrl_nfc_get_handover_req_p2p(
1180 wpa_s, reply, max_len, ndef);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001181 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001182#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001183
1184 return -1;
1185}
1186
1187
1188static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001189 char *reply, size_t max_len,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001190 int ndef, int cr, char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001191{
1192 struct wpabuf *buf;
1193 int res;
1194
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001195 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001196 if (buf == NULL)
1197 return -1;
1198
1199 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1200 wpabuf_len(buf));
1201 reply[res++] = '\n';
1202 reply[res] = '\0';
1203
1204 wpabuf_free(buf);
1205
1206 return res;
1207}
1208
1209
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001210#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001211static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1212 char *reply, size_t max_len,
1213 int ndef, int tag)
1214{
1215 struct wpabuf *buf;
1216 int res;
1217
1218 buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1219 if (buf == NULL)
1220 return -1;
1221
1222 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1223 wpabuf_len(buf));
1224 reply[res++] = '\n';
1225 reply[res] = '\0';
1226
1227 wpabuf_free(buf);
1228
1229 return res;
1230}
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001231#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001232
1233
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001234static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1235 char *cmd, char *reply,
1236 size_t max_len)
1237{
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001238 char *pos, *pos2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001239 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001240
1241 pos = os_strchr(cmd, ' ');
1242 if (pos == NULL)
1243 return -1;
1244 *pos++ = '\0';
1245
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001246 if (os_strcmp(cmd, "WPS") == 0)
1247 ndef = 0;
1248 else if (os_strcmp(cmd, "NDEF") == 0)
1249 ndef = 1;
1250 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001251 return -1;
1252
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001253 pos2 = os_strchr(pos, ' ');
1254 if (pos2)
1255 *pos2++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001256 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001257 if (!ndef)
1258 return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001259 return wpas_ctrl_nfc_get_handover_sel_wps(
1260 wpa_s, reply, max_len, ndef,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001261 os_strcmp(pos, "WPS-CR") == 0, pos2);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001262 }
1263
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001264#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001265 if (os_strcmp(pos, "P2P-CR") == 0) {
1266 return wpas_ctrl_nfc_get_handover_sel_p2p(
1267 wpa_s, reply, max_len, ndef, 0);
1268 }
1269
1270 if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1271 return wpas_ctrl_nfc_get_handover_sel_p2p(
1272 wpa_s, reply, max_len, ndef, 1);
1273 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001274#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001275
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001276 return -1;
1277}
1278
1279
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001280static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1281 char *cmd)
1282{
1283 size_t len;
1284 struct wpabuf *req, *sel;
1285 int ret;
1286 char *pos, *role, *type, *pos2;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001287#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001288 char *freq;
1289 int forced_freq = 0;
1290
1291 freq = strstr(cmd, " freq=");
1292 if (freq) {
1293 *freq = '\0';
1294 freq += 6;
1295 forced_freq = atoi(freq);
1296 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001297#endif /* CONFIG_P2P */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001298
1299 role = cmd;
1300 pos = os_strchr(role, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001301 if (pos == NULL) {
1302 wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001303 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001304 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001305 *pos++ = '\0';
1306
1307 type = pos;
1308 pos = os_strchr(type, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001309 if (pos == NULL) {
1310 wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001311 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001312 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001313 *pos++ = '\0';
1314
1315 pos2 = os_strchr(pos, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001316 if (pos2 == NULL) {
1317 wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001318 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001319 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001320 *pos2++ = '\0';
1321
1322 len = os_strlen(pos);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001323 if (len & 0x01) {
1324 wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001325 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001326 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001327 len /= 2;
1328
1329 req = wpabuf_alloc(len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001330 if (req == NULL) {
1331 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001332 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001333 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001334 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001335 wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001336 wpabuf_free(req);
1337 return -1;
1338 }
1339
1340 len = os_strlen(pos2);
1341 if (len & 0x01) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001342 wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001343 wpabuf_free(req);
1344 return -1;
1345 }
1346 len /= 2;
1347
1348 sel = wpabuf_alloc(len);
1349 if (sel == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001350 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001351 wpabuf_free(req);
1352 return -1;
1353 }
1354 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001355 wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001356 wpabuf_free(req);
1357 wpabuf_free(sel);
1358 return -1;
1359 }
1360
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001361 wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1362 role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1363
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001364 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1365 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001366#ifdef CONFIG_AP
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001367 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1368 {
1369 ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1370 if (ret < 0)
1371 ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001372#endif /* CONFIG_AP */
1373#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001374 } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1375 {
1376 ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
1377 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1378 {
1379 ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1380 forced_freq);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001381#endif /* CONFIG_P2P */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001382 } else {
1383 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1384 "reported: role=%s type=%s", role, type);
1385 ret = -1;
1386 }
1387 wpabuf_free(req);
1388 wpabuf_free(sel);
1389
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001390 if (ret)
1391 wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1392
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001393 return ret;
1394}
1395
Dmitry Shmidt04949592012-07-19 12:16:46 -07001396#endif /* CONFIG_WPS_NFC */
1397
1398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1400 char *cmd)
1401{
1402 u8 bssid[ETH_ALEN];
1403 char *pin;
1404 char *new_ssid;
1405 char *new_auth;
1406 char *new_encr;
1407 char *new_key;
1408 struct wps_new_ap_settings ap;
1409
1410 pin = os_strchr(cmd, ' ');
1411 if (pin == NULL)
1412 return -1;
1413 *pin++ = '\0';
1414
1415 if (hwaddr_aton(cmd, bssid)) {
1416 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1417 cmd);
1418 return -1;
1419 }
1420
1421 new_ssid = os_strchr(pin, ' ');
1422 if (new_ssid == NULL)
1423 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1424 *new_ssid++ = '\0';
1425
1426 new_auth = os_strchr(new_ssid, ' ');
1427 if (new_auth == NULL)
1428 return -1;
1429 *new_auth++ = '\0';
1430
1431 new_encr = os_strchr(new_auth, ' ');
1432 if (new_encr == NULL)
1433 return -1;
1434 *new_encr++ = '\0';
1435
1436 new_key = os_strchr(new_encr, ' ');
1437 if (new_key == NULL)
1438 return -1;
1439 *new_key++ = '\0';
1440
1441 os_memset(&ap, 0, sizeof(ap));
1442 ap.ssid_hex = new_ssid;
1443 ap.auth = new_auth;
1444 ap.encr = new_encr;
1445 ap.key_hex = new_key;
1446 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1447}
1448
1449
1450#ifdef CONFIG_AP
1451static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1452 char *cmd, char *buf,
1453 size_t buflen)
1454{
1455 int timeout = 300;
1456 char *pos;
1457 const char *pin_txt;
1458
1459 if (!wpa_s->ap_iface)
1460 return -1;
1461
1462 pos = os_strchr(cmd, ' ');
1463 if (pos)
1464 *pos++ = '\0';
1465
1466 if (os_strcmp(cmd, "disable") == 0) {
1467 wpas_wps_ap_pin_disable(wpa_s);
1468 return os_snprintf(buf, buflen, "OK\n");
1469 }
1470
1471 if (os_strcmp(cmd, "random") == 0) {
1472 if (pos)
1473 timeout = atoi(pos);
1474 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1475 if (pin_txt == NULL)
1476 return -1;
1477 return os_snprintf(buf, buflen, "%s", pin_txt);
1478 }
1479
1480 if (os_strcmp(cmd, "get") == 0) {
1481 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1482 if (pin_txt == NULL)
1483 return -1;
1484 return os_snprintf(buf, buflen, "%s", pin_txt);
1485 }
1486
1487 if (os_strcmp(cmd, "set") == 0) {
1488 char *pin;
1489 if (pos == NULL)
1490 return -1;
1491 pin = pos;
1492 pos = os_strchr(pos, ' ');
1493 if (pos) {
1494 *pos++ = '\0';
1495 timeout = atoi(pos);
1496 }
1497 if (os_strlen(pin) > buflen)
1498 return -1;
1499 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1500 return -1;
1501 return os_snprintf(buf, buflen, "%s", pin);
1502 }
1503
1504 return -1;
1505}
1506#endif /* CONFIG_AP */
1507
1508
1509#ifdef CONFIG_WPS_ER
1510static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1511 char *cmd)
1512{
1513 char *uuid = cmd, *pin, *pos;
1514 u8 addr_buf[ETH_ALEN], *addr = NULL;
1515 pin = os_strchr(uuid, ' ');
1516 if (pin == NULL)
1517 return -1;
1518 *pin++ = '\0';
1519 pos = os_strchr(pin, ' ');
1520 if (pos) {
1521 *pos++ = '\0';
1522 if (hwaddr_aton(pos, addr_buf) == 0)
1523 addr = addr_buf;
1524 }
1525 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1526}
1527
1528
1529static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1530 char *cmd)
1531{
1532 char *uuid = cmd, *pin;
1533 pin = os_strchr(uuid, ' ');
1534 if (pin == NULL)
1535 return -1;
1536 *pin++ = '\0';
1537 return wpas_wps_er_learn(wpa_s, uuid, pin);
1538}
1539
1540
1541static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1542 struct wpa_supplicant *wpa_s, char *cmd)
1543{
1544 char *uuid = cmd, *id;
1545 id = os_strchr(uuid, ' ');
1546 if (id == NULL)
1547 return -1;
1548 *id++ = '\0';
1549 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1550}
1551
1552
1553static int wpa_supplicant_ctrl_iface_wps_er_config(
1554 struct wpa_supplicant *wpa_s, char *cmd)
1555{
1556 char *pin;
1557 char *new_ssid;
1558 char *new_auth;
1559 char *new_encr;
1560 char *new_key;
1561 struct wps_new_ap_settings ap;
1562
1563 pin = os_strchr(cmd, ' ');
1564 if (pin == NULL)
1565 return -1;
1566 *pin++ = '\0';
1567
1568 new_ssid = os_strchr(pin, ' ');
1569 if (new_ssid == NULL)
1570 return -1;
1571 *new_ssid++ = '\0';
1572
1573 new_auth = os_strchr(new_ssid, ' ');
1574 if (new_auth == NULL)
1575 return -1;
1576 *new_auth++ = '\0';
1577
1578 new_encr = os_strchr(new_auth, ' ');
1579 if (new_encr == NULL)
1580 return -1;
1581 *new_encr++ = '\0';
1582
1583 new_key = os_strchr(new_encr, ' ');
1584 if (new_key == NULL)
1585 return -1;
1586 *new_key++ = '\0';
1587
1588 os_memset(&ap, 0, sizeof(ap));
1589 ap.ssid_hex = new_ssid;
1590 ap.auth = new_auth;
1591 ap.encr = new_encr;
1592 ap.key_hex = new_key;
1593 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1594}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001595
1596
1597#ifdef CONFIG_WPS_NFC
1598static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1599 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1600{
1601 int ndef;
1602 struct wpabuf *buf;
1603 int res;
1604 char *uuid;
1605
1606 uuid = os_strchr(cmd, ' ');
1607 if (uuid == NULL)
1608 return -1;
1609 *uuid++ = '\0';
1610
1611 if (os_strcmp(cmd, "WPS") == 0)
1612 ndef = 0;
1613 else if (os_strcmp(cmd, "NDEF") == 0)
1614 ndef = 1;
1615 else
1616 return -1;
1617
1618 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1619 if (buf == NULL)
1620 return -1;
1621
1622 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1623 wpabuf_len(buf));
1624 reply[res++] = '\n';
1625 reply[res] = '\0';
1626
1627 wpabuf_free(buf);
1628
1629 return res;
1630}
1631#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001632#endif /* CONFIG_WPS_ER */
1633
1634#endif /* CONFIG_WPS */
1635
1636
1637#ifdef CONFIG_IBSS_RSN
1638static int wpa_supplicant_ctrl_iface_ibss_rsn(
1639 struct wpa_supplicant *wpa_s, char *addr)
1640{
1641 u8 peer[ETH_ALEN];
1642
1643 if (hwaddr_aton(addr, peer)) {
1644 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1645 "address '%s'", addr);
1646 return -1;
1647 }
1648
1649 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1650 MAC2STR(peer));
1651
1652 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1653}
1654#endif /* CONFIG_IBSS_RSN */
1655
1656
1657static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1658 char *rsp)
1659{
1660#ifdef IEEE8021X_EAPOL
1661 char *pos, *id_pos;
1662 int id;
1663 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001664
1665 pos = os_strchr(rsp, '-');
1666 if (pos == NULL)
1667 return -1;
1668 *pos++ = '\0';
1669 id_pos = pos;
1670 pos = os_strchr(pos, ':');
1671 if (pos == NULL)
1672 return -1;
1673 *pos++ = '\0';
1674 id = atoi(id_pos);
1675 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1676 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1677 (u8 *) pos, os_strlen(pos));
1678
1679 ssid = wpa_config_get_network(wpa_s->conf, id);
1680 if (ssid == NULL) {
1681 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1682 "to update", id);
1683 return -1;
1684 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001686 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1687 pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688#else /* IEEE8021X_EAPOL */
1689 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1690 return -1;
1691#endif /* IEEE8021X_EAPOL */
1692}
1693
1694
1695static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1696 const char *params,
1697 char *buf, size_t buflen)
1698{
1699 char *pos, *end, tmp[30];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001700 int res, verbose, wps, ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001701#ifdef CONFIG_HS20
1702 const u8 *hs20;
1703#endif /* CONFIG_HS20 */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001704
Dmitry Shmidt56052862013-10-04 10:23:25 -07001705 if (os_strcmp(params, "-DRIVER") == 0)
1706 return wpa_drv_status(wpa_s, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 verbose = os_strcmp(params, "-VERBOSE") == 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001708 wps = os_strcmp(params, "-WPS") == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709 pos = buf;
1710 end = buf + buflen;
1711 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1712 struct wpa_ssid *ssid = wpa_s->current_ssid;
1713 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1714 MAC2STR(wpa_s->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001715 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001716 return pos - buf;
1717 pos += ret;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001718 ret = os_snprintf(pos, end - pos, "freq=%u\n",
1719 wpa_s->assoc_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001720 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001721 return pos - buf;
1722 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723 if (ssid) {
1724 u8 *_ssid = ssid->ssid;
1725 size_t ssid_len = ssid->ssid_len;
1726 u8 ssid_buf[MAX_SSID_LEN];
1727 if (ssid_len == 0) {
1728 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1729 if (_res < 0)
1730 ssid_len = 0;
1731 else
1732 ssid_len = _res;
1733 _ssid = ssid_buf;
1734 }
1735 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1736 wpa_ssid_txt(_ssid, ssid_len),
1737 ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001738 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001739 return pos - buf;
1740 pos += ret;
1741
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001742 if (wps && ssid->passphrase &&
1743 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1744 (ssid->mode == WPAS_MODE_AP ||
1745 ssid->mode == WPAS_MODE_P2P_GO)) {
1746 ret = os_snprintf(pos, end - pos,
1747 "passphrase=%s\n",
1748 ssid->passphrase);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001749 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001750 return pos - buf;
1751 pos += ret;
1752 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001753 if (ssid->id_str) {
1754 ret = os_snprintf(pos, end - pos,
1755 "id_str=%s\n",
1756 ssid->id_str);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001757 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001758 return pos - buf;
1759 pos += ret;
1760 }
1761
1762 switch (ssid->mode) {
1763 case WPAS_MODE_INFRA:
1764 ret = os_snprintf(pos, end - pos,
1765 "mode=station\n");
1766 break;
1767 case WPAS_MODE_IBSS:
1768 ret = os_snprintf(pos, end - pos,
1769 "mode=IBSS\n");
1770 break;
1771 case WPAS_MODE_AP:
1772 ret = os_snprintf(pos, end - pos,
1773 "mode=AP\n");
1774 break;
1775 case WPAS_MODE_P2P_GO:
1776 ret = os_snprintf(pos, end - pos,
1777 "mode=P2P GO\n");
1778 break;
1779 case WPAS_MODE_P2P_GROUP_FORMATION:
1780 ret = os_snprintf(pos, end - pos,
1781 "mode=P2P GO - group "
1782 "formation\n");
1783 break;
1784 default:
1785 ret = 0;
1786 break;
1787 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001788 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 return pos - buf;
1790 pos += ret;
1791 }
1792
1793#ifdef CONFIG_AP
1794 if (wpa_s->ap_iface) {
1795 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1796 end - pos,
1797 verbose);
1798 } else
1799#endif /* CONFIG_AP */
1800 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1801 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001802#ifdef CONFIG_SAE
1803 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001804#ifdef CONFIG_AP
1805 !wpa_s->ap_iface &&
1806#endif /* CONFIG_AP */
1807 wpa_s->sme.sae.state == SAE_ACCEPTED) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001808 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1809 wpa_s->sme.sae.group);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001810 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001811 return pos - buf;
1812 pos += ret;
1813 }
1814#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001815 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1816 wpa_supplicant_state_txt(wpa_s->wpa_state));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001817 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818 return pos - buf;
1819 pos += ret;
1820
1821 if (wpa_s->l2 &&
1822 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1823 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001824 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825 return pos - buf;
1826 pos += ret;
1827 }
1828
1829#ifdef CONFIG_P2P
1830 if (wpa_s->global->p2p) {
1831 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1832 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001833 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001834 return pos - buf;
1835 pos += ret;
1836 }
1837#endif /* CONFIG_P2P */
1838
1839 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1840 MAC2STR(wpa_s->own_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001841 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001842 return pos - buf;
1843 pos += ret;
1844
Dmitry Shmidt04949592012-07-19 12:16:46 -07001845#ifdef CONFIG_HS20
1846 if (wpa_s->current_bss &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001847 (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1848 HS20_IE_VENDOR_TYPE)) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001849 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1850 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001851 int release = 1;
1852 if (hs20[1] >= 5) {
1853 u8 rel_num = (hs20[6] & 0xf0) >> 4;
1854 release = rel_num + 1;
1855 }
1856 ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001857 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001858 return pos - buf;
1859 pos += ret;
1860 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001861
1862 if (wpa_s->current_ssid) {
1863 struct wpa_cred *cred;
1864 char *type;
1865
1866 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001867 size_t i;
1868
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001869 if (wpa_s->current_ssid->parent_cred != cred)
1870 continue;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001871
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001872 if (cred->provisioning_sp) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001873 ret = os_snprintf(pos, end - pos,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001874 "provisioning_sp=%s\n",
1875 cred->provisioning_sp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001876 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001877 return pos - buf;
1878 pos += ret;
1879 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001880
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001881 if (!cred->domain)
1882 goto no_domain;
1883
1884 i = 0;
1885 if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
1886 struct wpabuf *names =
1887 wpa_s->current_bss->anqp->domain_name;
1888 for (i = 0; names && i < cred->num_domain; i++)
1889 {
1890 if (domain_name_list_contains(
1891 names, cred->domain[i], 1))
1892 break;
1893 }
1894 if (i == cred->num_domain)
1895 i = 0; /* show first entry by default */
1896 }
1897 ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
1898 cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001899 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001900 return pos - buf;
1901 pos += ret;
1902
1903 no_domain:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001904 if (wpa_s->current_bss == NULL ||
1905 wpa_s->current_bss->anqp == NULL)
1906 res = -1;
1907 else
1908 res = interworking_home_sp_cred(
1909 wpa_s, cred,
1910 wpa_s->current_bss->anqp->domain_name);
1911 if (res > 0)
1912 type = "home";
1913 else if (res == 0)
1914 type = "roaming";
1915 else
1916 type = "unknown";
1917
1918 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001919 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001920 return pos - buf;
1921 pos += ret;
1922
1923 break;
1924 }
1925 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001926#endif /* CONFIG_HS20 */
1927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001928 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1929 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1930 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1931 verbose);
1932 if (res >= 0)
1933 pos += res;
1934 }
1935
1936 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1937 if (res >= 0)
1938 pos += res;
1939
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001940#ifdef CONFIG_WPS
1941 {
1942 char uuid_str[100];
1943 uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
1944 ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001945 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001946 return pos - buf;
1947 pos += ret;
1948 }
1949#endif /* CONFIG_WPS */
1950
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001951#ifdef ANDROID
vandwalleffc70182014-09-11 11:40:14 -07001952 /*
1953 * Allow using the STATUS command with default behavior, say for debug,
1954 * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
1955 * events with STATUS-NO_EVENTS.
1956 */
1957 if (os_strcmp(params, "-NO_EVENTS")) {
1958 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
1959 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
1960 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
1961 wpa_s->wpa_state,
1962 MAC2STR(wpa_s->bssid),
1963 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1964 wpa_ssid_txt(wpa_s->current_ssid->ssid,
1965 wpa_s->current_ssid->ssid_len) : "");
1966 if (wpa_s->wpa_state == WPA_COMPLETED) {
1967 struct wpa_ssid *ssid = wpa_s->current_ssid;
1968 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
1969 "- connection to " MACSTR
1970 " completed %s [id=%d id_str=%s]",
1971 MAC2STR(wpa_s->bssid), "(auth)",
1972 ssid ? ssid->id : -1,
1973 ssid && ssid->id_str ? ssid->id_str : "");
1974 }
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001975 }
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001976#endif /* ANDROID */
1977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001978 return pos - buf;
1979}
1980
1981
1982static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1983 char *cmd)
1984{
1985 char *pos;
1986 int id;
1987 struct wpa_ssid *ssid;
1988 u8 bssid[ETH_ALEN];
1989
1990 /* cmd: "<network id> <BSSID>" */
1991 pos = os_strchr(cmd, ' ');
1992 if (pos == NULL)
1993 return -1;
1994 *pos++ = '\0';
1995 id = atoi(cmd);
1996 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1997 if (hwaddr_aton(pos, bssid)) {
1998 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1999 return -1;
2000 }
2001
2002 ssid = wpa_config_get_network(wpa_s->conf, id);
2003 if (ssid == NULL) {
2004 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2005 "to update", id);
2006 return -1;
2007 }
2008
2009 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
2010 ssid->bssid_set = !is_zero_ether_addr(bssid);
2011
2012 return 0;
2013}
2014
2015
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002016static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002017 char *cmd, char *buf,
2018 size_t buflen)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002019{
2020 u8 bssid[ETH_ALEN];
2021 struct wpa_blacklist *e;
2022 char *pos, *end;
2023 int ret;
2024
2025 /* cmd: "BLACKLIST [<BSSID>]" */
2026 if (*cmd == '\0') {
2027 pos = buf;
2028 end = buf + buflen;
2029 e = wpa_s->blacklist;
2030 while (e) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002031 ret = os_snprintf(pos, end - pos, MACSTR "\n",
2032 MAC2STR(e->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002033 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002034 return pos - buf;
2035 pos += ret;
2036 e = e->next;
2037 }
2038 return pos - buf;
2039 }
2040
2041 cmd++;
2042 if (os_strncmp(cmd, "clear", 5) == 0) {
2043 wpa_blacklist_clear(wpa_s);
2044 os_memcpy(buf, "OK\n", 3);
2045 return 3;
2046 }
2047
2048 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
2049 if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002050 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002051 return -1;
2052 }
2053
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002054 /*
2055 * Add the BSSID twice, so its count will be 2, causing it to be
2056 * skipped when processing scan results.
2057 */
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002058 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002059 if (ret < 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002060 return -1;
2061 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002062 if (ret < 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002063 return -1;
2064 os_memcpy(buf, "OK\n", 3);
2065 return 3;
2066}
2067
2068
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002069static const char * debug_level_str(int level)
2070{
2071 switch (level) {
2072 case MSG_EXCESSIVE:
2073 return "EXCESSIVE";
2074 case MSG_MSGDUMP:
2075 return "MSGDUMP";
2076 case MSG_DEBUG:
2077 return "DEBUG";
2078 case MSG_INFO:
2079 return "INFO";
2080 case MSG_WARNING:
2081 return "WARNING";
2082 case MSG_ERROR:
2083 return "ERROR";
2084 default:
2085 return "?";
2086 }
2087}
2088
2089
2090static int str_to_debug_level(const char *s)
2091{
2092 if (os_strcasecmp(s, "EXCESSIVE") == 0)
2093 return MSG_EXCESSIVE;
2094 if (os_strcasecmp(s, "MSGDUMP") == 0)
2095 return MSG_MSGDUMP;
2096 if (os_strcasecmp(s, "DEBUG") == 0)
2097 return MSG_DEBUG;
2098 if (os_strcasecmp(s, "INFO") == 0)
2099 return MSG_INFO;
2100 if (os_strcasecmp(s, "WARNING") == 0)
2101 return MSG_WARNING;
2102 if (os_strcasecmp(s, "ERROR") == 0)
2103 return MSG_ERROR;
2104 return -1;
2105}
2106
2107
2108static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
2109 char *cmd, char *buf,
2110 size_t buflen)
2111{
2112 char *pos, *end, *stamp;
2113 int ret;
2114
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002115 /* cmd: "LOG_LEVEL [<level>]" */
2116 if (*cmd == '\0') {
2117 pos = buf;
2118 end = buf + buflen;
2119 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2120 "Timestamp: %d\n",
2121 debug_level_str(wpa_debug_level),
2122 wpa_debug_timestamp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002123 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002124 ret = 0;
2125
2126 return ret;
2127 }
2128
2129 while (*cmd == ' ')
2130 cmd++;
2131
2132 stamp = os_strchr(cmd, ' ');
2133 if (stamp) {
2134 *stamp++ = '\0';
2135 while (*stamp == ' ') {
2136 stamp++;
2137 }
2138 }
2139
2140 if (cmd && os_strlen(cmd)) {
2141 int level = str_to_debug_level(cmd);
2142 if (level < 0)
2143 return -1;
2144 wpa_debug_level = level;
2145 }
2146
2147 if (stamp && os_strlen(stamp))
2148 wpa_debug_timestamp = atoi(stamp);
2149
2150 os_memcpy(buf, "OK\n", 3);
2151 return 3;
2152}
2153
2154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155static int wpa_supplicant_ctrl_iface_list_networks(
Vinit Deshpandeda134e92014-12-02 10:59:29 -08002156 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002157{
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002158 char *pos, *end, *prev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159 struct wpa_ssid *ssid;
2160 int ret;
2161
2162 pos = buf;
2163 end = buf + buflen;
2164 ret = os_snprintf(pos, end - pos,
2165 "network id / ssid / bssid / flags\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002166 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002167 return pos - buf;
2168 pos += ret;
2169
2170 ssid = wpa_s->conf->ssid;
Vinit Deshpandeda134e92014-12-02 10:59:29 -08002171
2172 /* skip over ssids until we find next one */
2173 if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
2174 int last_id = atoi(cmd + 8);
2175 if (last_id != -1) {
2176 while (ssid != NULL && ssid->id <= last_id) {
2177 ssid = ssid->next;
2178 }
2179 }
2180 }
2181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 while (ssid) {
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002183 prev = pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184 ret = os_snprintf(pos, end - pos, "%d\t%s",
2185 ssid->id,
2186 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002187 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002188 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002189 pos += ret;
2190 if (ssid->bssid_set) {
2191 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2192 MAC2STR(ssid->bssid));
2193 } else {
2194 ret = os_snprintf(pos, end - pos, "\tany");
2195 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002196 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002197 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002198 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002199 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200 ssid == wpa_s->current_ssid ?
2201 "[CURRENT]" : "",
2202 ssid->disabled ? "[DISABLED]" : "",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002203 ssid->disabled_until.sec ?
2204 "[TEMP-DISABLED]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002205 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2206 "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002207 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002208 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002209 pos += ret;
2210 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002211 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002212 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002213 pos += ret;
2214
2215 ssid = ssid->next;
2216 }
2217
2218 return pos - buf;
2219}
2220
2221
2222static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2223{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002224 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225 ret = os_snprintf(pos, end - pos, "-");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002226 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227 return pos;
2228 pos += ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002229 ret = wpa_write_ciphers(pos, end, cipher, "+");
2230 if (ret < 0)
2231 return pos;
2232 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 return pos;
2234}
2235
2236
2237static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2238 const u8 *ie, size_t ie_len)
2239{
2240 struct wpa_ie_data data;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002241 char *start;
2242 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243
2244 ret = os_snprintf(pos, end - pos, "[%s-", proto);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002245 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002246 return pos;
2247 pos += ret;
2248
2249 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2250 ret = os_snprintf(pos, end - pos, "?]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002251 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002252 return pos;
2253 pos += ret;
2254 return pos;
2255 }
2256
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002257 start = pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002259 ret = os_snprintf(pos, end - pos, "%sEAP",
2260 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002261 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262 return pos;
2263 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002264 }
2265 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002266 ret = os_snprintf(pos, end - pos, "%sPSK",
2267 pos == start ? "" : "+");
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;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002271 }
2272 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002273 ret = os_snprintf(pos, end - pos, "%sNone",
2274 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002275 if (os_snprintf_error(end - pos, ret))
2276 return pos;
2277 pos += ret;
2278 }
2279 if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
2280 ret = os_snprintf(pos, end - pos, "%sSAE",
2281 pos == start ? "" : "+");
2282 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283 return pos;
2284 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285 }
2286#ifdef CONFIG_IEEE80211R
2287 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2288 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002289 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002290 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002291 return pos;
2292 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293 }
2294 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2295 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002296 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002297 if (os_snprintf_error(end - pos, ret))
2298 return pos;
2299 pos += ret;
2300 }
2301 if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2302 ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2303 pos == start ? "" : "+");
2304 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002305 return pos;
2306 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307 }
2308#endif /* CONFIG_IEEE80211R */
2309#ifdef CONFIG_IEEE80211W
2310 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2311 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
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_PSK_SHA256) {
2318 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
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))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321 return pos;
2322 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002323 }
2324#endif /* CONFIG_IEEE80211W */
2325
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002326#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002327 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2328 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
2329 pos == start ? "" : "+");
2330 if (os_snprintf_error(end - pos, ret))
2331 return pos;
2332 pos += ret;
2333 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002334#endif /* CONFIG_SUITEB */
2335
2336#ifdef CONFIG_SUITEB192
2337 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
2338 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
2339 pos == start ? "" : "+");
2340 if (os_snprintf_error(end - pos, ret))
2341 return pos;
2342 pos += ret;
2343 }
2344#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2347
2348 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2349 ret = os_snprintf(pos, end - pos, "-preauth");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002350 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002351 return pos;
2352 pos += ret;
2353 }
2354
2355 ret = os_snprintf(pos, end - pos, "]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002356 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002357 return pos;
2358 pos += ret;
2359
2360 return pos;
2361}
2362
2363
2364#ifdef CONFIG_WPS
2365static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2366 char *pos, char *end,
2367 struct wpabuf *wps_ie)
2368{
2369 int ret;
2370 const char *txt;
2371
2372 if (wps_ie == NULL)
2373 return pos;
2374 if (wps_is_selected_pbc_registrar(wps_ie))
2375 txt = "[WPS-PBC]";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002376 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2377 txt = "[WPS-AUTH]";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002378 else if (wps_is_selected_pin_registrar(wps_ie))
2379 txt = "[WPS-PIN]";
2380 else
2381 txt = "[WPS]";
2382
2383 ret = os_snprintf(pos, end - pos, "%s", txt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002384 if (!os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385 pos += ret;
2386 wpabuf_free(wps_ie);
2387 return pos;
2388}
2389#endif /* CONFIG_WPS */
2390
2391
2392static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2393 char *pos, char *end,
2394 const struct wpa_bss *bss)
2395{
2396#ifdef CONFIG_WPS
2397 struct wpabuf *wps_ie;
2398 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2399 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2400#else /* CONFIG_WPS */
2401 return pos;
2402#endif /* CONFIG_WPS */
2403}
2404
2405
2406/* Format one result on one text line into a buffer. */
2407static int wpa_supplicant_ctrl_iface_scan_result(
2408 struct wpa_supplicant *wpa_s,
2409 const struct wpa_bss *bss, char *buf, size_t buflen)
2410{
2411 char *pos, *end;
2412 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002413 const u8 *ie, *ie2, *p2p, *mesh;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002414
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002415 mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002416 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07002417 if (!p2p)
2418 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002419 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2420 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2421 0)
2422 return 0; /* Do not show P2P listen discovery results here */
2423
2424 pos = buf;
2425 end = buf + buflen;
2426
2427 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2428 MAC2STR(bss->bssid), bss->freq, bss->level);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002429 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002430 return -1;
2431 pos += ret;
2432 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2433 if (ie)
2434 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2435 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002436 if (ie2) {
2437 pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
2438 ie2, 2 + ie2[1]);
2439 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2441 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2442 ret = os_snprintf(pos, end - pos, "[WEP]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002443 if (os_snprintf_error(end - pos, ret))
2444 return -1;
2445 pos += ret;
2446 }
2447 if (mesh) {
2448 ret = os_snprintf(pos, end - pos, "[MESH]");
2449 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002450 return -1;
2451 pos += ret;
2452 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002453 if (bss_is_dmg(bss)) {
2454 const char *s;
2455 ret = os_snprintf(pos, end - pos, "[DMG]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002456 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457 return -1;
2458 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002459 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
2460 case IEEE80211_CAP_DMG_IBSS:
2461 s = "[IBSS]";
2462 break;
2463 case IEEE80211_CAP_DMG_AP:
2464 s = "[ESS]";
2465 break;
2466 case IEEE80211_CAP_DMG_PBSS:
2467 s = "[PBSS]";
2468 break;
2469 default:
2470 s = "";
2471 break;
2472 }
2473 ret = os_snprintf(pos, end - pos, "%s", s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002474 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002475 return -1;
2476 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002477 } else {
2478 if (bss->caps & IEEE80211_CAP_IBSS) {
2479 ret = os_snprintf(pos, end - pos, "[IBSS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002480 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002481 return -1;
2482 pos += ret;
2483 }
2484 if (bss->caps & IEEE80211_CAP_ESS) {
2485 ret = os_snprintf(pos, end - pos, "[ESS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002486 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002487 return -1;
2488 pos += ret;
2489 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490 }
2491 if (p2p) {
2492 ret = os_snprintf(pos, end - pos, "[P2P]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002493 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494 return -1;
2495 pos += ret;
2496 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002497#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002498 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002499 ret = os_snprintf(pos, end - pos, "[HS20]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002500 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07002501 return -1;
2502 pos += ret;
2503 }
2504#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505
2506 ret = os_snprintf(pos, end - pos, "\t%s",
2507 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002508 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509 return -1;
2510 pos += ret;
2511
2512 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002513 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514 return -1;
2515 pos += ret;
2516
2517 return pos - buf;
2518}
2519
2520
2521static int wpa_supplicant_ctrl_iface_scan_results(
2522 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2523{
2524 char *pos, *end;
2525 struct wpa_bss *bss;
2526 int ret;
2527
2528 pos = buf;
2529 end = buf + buflen;
2530 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2531 "flags / ssid\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002532 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002533 return pos - buf;
2534 pos += ret;
2535
2536 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2537 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2538 end - pos);
2539 if (ret < 0 || ret >= end - pos)
2540 return pos - buf;
2541 pos += ret;
2542 }
2543
2544 return pos - buf;
2545}
2546
2547
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002548#ifdef CONFIG_MESH
2549
2550static int wpa_supplicant_ctrl_iface_mesh_interface_add(
2551 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
2552{
2553 char *pos, ifname[IFNAMSIZ + 1];
2554
2555 ifname[0] = '\0';
2556
2557 pos = os_strstr(cmd, "ifname=");
2558 if (pos) {
2559 pos += 7;
2560 os_strlcpy(ifname, pos, sizeof(ifname));
2561 }
2562
2563 if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
2564 return -1;
2565
2566 os_strlcpy(reply, ifname, max_len);
2567 return os_strlen(ifname);
2568}
2569
2570
2571static int wpa_supplicant_ctrl_iface_mesh_group_add(
2572 struct wpa_supplicant *wpa_s, char *cmd)
2573{
2574 int id;
2575 struct wpa_ssid *ssid;
2576
2577 id = atoi(cmd);
2578 wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
2579
2580 ssid = wpa_config_get_network(wpa_s->conf, id);
2581 if (ssid == NULL) {
2582 wpa_printf(MSG_DEBUG,
2583 "CTRL_IFACE: Could not find network id=%d", id);
2584 return -1;
2585 }
2586 if (ssid->mode != WPAS_MODE_MESH) {
2587 wpa_printf(MSG_DEBUG,
2588 "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
2589 return -1;
2590 }
2591 if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
2592 ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
2593 wpa_printf(MSG_ERROR,
2594 "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
2595 return -1;
2596 }
2597
2598 /*
2599 * TODO: If necessary write our own group_add function,
2600 * for now we can reuse select_network
2601 */
2602 wpa_supplicant_select_network(wpa_s, ssid);
2603
2604 return 0;
2605}
2606
2607
2608static int wpa_supplicant_ctrl_iface_mesh_group_remove(
2609 struct wpa_supplicant *wpa_s, char *cmd)
2610{
2611 struct wpa_supplicant *orig;
2612 struct wpa_global *global;
2613 int found = 0;
2614
2615 wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
2616
2617 global = wpa_s->global;
2618 orig = wpa_s;
2619
2620 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2621 if (os_strcmp(wpa_s->ifname, cmd) == 0) {
2622 found = 1;
2623 break;
2624 }
2625 }
2626 if (!found) {
2627 wpa_printf(MSG_ERROR,
2628 "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
2629 cmd);
2630 return -1;
2631 }
2632 if (wpa_s->mesh_if_created && wpa_s == orig) {
2633 wpa_printf(MSG_ERROR,
2634 "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
2635 return -1;
2636 }
2637
2638 wpa_s->reassociate = 0;
2639 wpa_s->disconnected = 1;
2640 wpa_supplicant_cancel_sched_scan(wpa_s);
2641 wpa_supplicant_cancel_scan(wpa_s);
2642
2643 /*
2644 * TODO: If necessary write our own group_remove function,
2645 * for now we can reuse deauthenticate
2646 */
2647 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2648
2649 if (wpa_s->mesh_if_created)
2650 wpa_supplicant_remove_iface(global, wpa_s, 0);
2651
2652 return 0;
2653}
2654
2655#endif /* CONFIG_MESH */
2656
2657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002658static int wpa_supplicant_ctrl_iface_select_network(
2659 struct wpa_supplicant *wpa_s, char *cmd)
2660{
2661 int id;
2662 struct wpa_ssid *ssid;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002663 char *pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664
2665 /* cmd: "<network id>" or "any" */
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002666 if (os_strncmp(cmd, "any", 3) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002667 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2668 ssid = NULL;
2669 } else {
2670 id = atoi(cmd);
2671 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2672
2673 ssid = wpa_config_get_network(wpa_s->conf, id);
2674 if (ssid == NULL) {
2675 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2676 "network id=%d", id);
2677 return -1;
2678 }
2679 if (ssid->disabled == 2) {
2680 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2681 "SELECT_NETWORK with persistent P2P group");
2682 return -1;
2683 }
2684 }
2685
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002686 pos = os_strstr(cmd, " freq=");
2687 if (pos) {
2688 int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
2689 if (freqs) {
2690 wpa_s->scan_req = MANUAL_SCAN_REQ;
2691 os_free(wpa_s->manual_scan_freqs);
2692 wpa_s->manual_scan_freqs = freqs;
2693 }
2694 }
2695
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002696 wpa_supplicant_select_network(wpa_s, ssid);
2697
2698 return 0;
2699}
2700
2701
2702static int wpa_supplicant_ctrl_iface_enable_network(
2703 struct wpa_supplicant *wpa_s, char *cmd)
2704{
2705 int id;
2706 struct wpa_ssid *ssid;
2707
2708 /* cmd: "<network id>" or "all" */
2709 if (os_strcmp(cmd, "all") == 0) {
2710 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2711 ssid = NULL;
2712 } else {
2713 id = atoi(cmd);
2714 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2715
2716 ssid = wpa_config_get_network(wpa_s->conf, id);
2717 if (ssid == NULL) {
2718 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2719 "network id=%d", id);
2720 return -1;
2721 }
2722 if (ssid->disabled == 2) {
2723 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2724 "ENABLE_NETWORK with persistent P2P group");
2725 return -1;
2726 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002727
2728 if (os_strstr(cmd, " no-connect")) {
2729 ssid->disabled = 0;
2730 return 0;
2731 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002732 }
2733 wpa_supplicant_enable_network(wpa_s, ssid);
2734
2735 return 0;
2736}
2737
2738
2739static int wpa_supplicant_ctrl_iface_disable_network(
2740 struct wpa_supplicant *wpa_s, char *cmd)
2741{
2742 int id;
2743 struct wpa_ssid *ssid;
2744
2745 /* cmd: "<network id>" or "all" */
2746 if (os_strcmp(cmd, "all") == 0) {
2747 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2748 ssid = NULL;
2749 } else {
2750 id = atoi(cmd);
2751 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2752
2753 ssid = wpa_config_get_network(wpa_s->conf, id);
2754 if (ssid == NULL) {
2755 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2756 "network id=%d", id);
2757 return -1;
2758 }
2759 if (ssid->disabled == 2) {
2760 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2761 "DISABLE_NETWORK with persistent P2P "
2762 "group");
2763 return -1;
2764 }
2765 }
2766 wpa_supplicant_disable_network(wpa_s, ssid);
2767
2768 return 0;
2769}
2770
2771
2772static int wpa_supplicant_ctrl_iface_add_network(
2773 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2774{
2775 struct wpa_ssid *ssid;
2776 int ret;
2777
2778 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2779
2780 ssid = wpa_config_add_network(wpa_s->conf);
2781 if (ssid == NULL)
2782 return -1;
2783
2784 wpas_notify_network_added(wpa_s, ssid);
2785
2786 ssid->disabled = 1;
2787 wpa_config_set_network_defaults(ssid);
2788
2789 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002790 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791 return -1;
2792 return ret;
2793}
2794
2795
2796static int wpa_supplicant_ctrl_iface_remove_network(
2797 struct wpa_supplicant *wpa_s, char *cmd)
2798{
2799 int id;
2800 struct wpa_ssid *ssid;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002801 int was_disabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002802
2803 /* cmd: "<network id>" or "all" */
2804 if (os_strcmp(cmd, "all") == 0) {
2805 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002806 if (wpa_s->sched_scanning)
2807 wpa_supplicant_cancel_sched_scan(wpa_s);
2808
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002809 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810 if (wpa_s->current_ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002811#ifdef CONFIG_SME
2812 wpa_s->sme.prev_bssid_set = 0;
2813#endif /* CONFIG_SME */
Jouni Malinen75ecf522011-06-27 15:19:46 -07002814 wpa_sm_set_config(wpa_s->wpa, NULL);
2815 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002816 wpa_supplicant_deauthenticate(
2817 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002819 ssid = wpa_s->conf->ssid;
2820 while (ssid) {
2821 struct wpa_ssid *remove_ssid = ssid;
2822 id = ssid->id;
2823 ssid = ssid->next;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002824 if (wpa_s->last_ssid == remove_ssid)
2825 wpa_s->last_ssid = NULL;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002826 wpas_notify_network_removed(wpa_s, remove_ssid);
2827 wpa_config_remove_network(wpa_s->conf, id);
2828 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002829 return 0;
2830 }
2831
2832 id = atoi(cmd);
2833 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2834
2835 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002836 if (ssid)
2837 wpas_notify_network_removed(wpa_s, ssid);
Deepthi Gowria831d782012-09-03 11:55:38 +03002838 if (ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2840 "id=%d", id);
2841 return -1;
2842 }
2843
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002844 if (wpa_s->last_ssid == ssid)
2845 wpa_s->last_ssid = NULL;
2846
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002847 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002848#ifdef CONFIG_SME
2849 wpa_s->sme.prev_bssid_set = 0;
2850#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002852 * Invalidate the EAP session cache if the current or
2853 * previously used network is removed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002854 */
2855 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002856 }
2857
2858 if (ssid == wpa_s->current_ssid) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002859 wpa_sm_set_config(wpa_s->wpa, NULL);
2860 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002861
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002862 wpa_supplicant_deauthenticate(wpa_s,
2863 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864 }
2865
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002866 was_disabled = ssid->disabled;
2867
Deepthi Gowria831d782012-09-03 11:55:38 +03002868 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2869 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2870 "network id=%d", id);
2871 return -1;
2872 }
2873
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002874 if (!was_disabled && wpa_s->sched_scanning) {
2875 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2876 "network from filters");
2877 wpa_supplicant_cancel_sched_scan(wpa_s);
2878 wpa_supplicant_req_scan(wpa_s, 0, 0);
2879 }
2880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881 return 0;
2882}
2883
2884
Dmitry Shmidt684785c2014-05-12 13:34:29 -07002885static int wpa_supplicant_ctrl_iface_update_network(
2886 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2887 char *name, char *value)
2888{
2889 if (wpa_config_set(ssid, name, value, 0) < 0) {
2890 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2891 "variable '%s'", name);
2892 return -1;
2893 }
2894
2895 if (os_strcmp(name, "bssid") != 0 &&
2896 os_strcmp(name, "priority") != 0)
2897 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
2898
2899 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
2900 /*
2901 * Invalidate the EAP session cache if anything in the current
2902 * or previously used configuration changes.
2903 */
2904 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2905 }
2906
2907 if ((os_strcmp(name, "psk") == 0 &&
2908 value[0] == '"' && ssid->ssid_len) ||
2909 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2910 wpa_config_update_psk(ssid);
2911 else if (os_strcmp(name, "priority") == 0)
2912 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002913 else if (os_strcmp(name, "no_auto_peer") == 0)
2914 ssid->no_auto_peer = atoi(value);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07002915
2916 return 0;
2917}
2918
2919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002920static int wpa_supplicant_ctrl_iface_set_network(
2921 struct wpa_supplicant *wpa_s, char *cmd)
2922{
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002923 int id, ret, prev_bssid_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002924 struct wpa_ssid *ssid;
2925 char *name, *value;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002926 u8 prev_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927
2928 /* cmd: "<network id> <variable name> <value>" */
2929 name = os_strchr(cmd, ' ');
2930 if (name == NULL)
2931 return -1;
2932 *name++ = '\0';
2933
2934 value = os_strchr(name, ' ');
2935 if (value == NULL)
2936 return -1;
2937 *value++ = '\0';
2938
2939 id = atoi(cmd);
2940 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2941 id, name);
2942 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2943 (u8 *) value, os_strlen(value));
2944
2945 ssid = wpa_config_get_network(wpa_s->conf, id);
2946 if (ssid == NULL) {
2947 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2948 "id=%d", id);
2949 return -1;
2950 }
2951
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002952 prev_bssid_set = ssid->bssid_set;
2953 os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
2954 ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
2955 value);
2956 if (ret == 0 &&
2957 (ssid->bssid_set != prev_bssid_set ||
2958 os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
2959 wpas_notify_network_bssid_set_changed(wpa_s, ssid);
2960 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002961}
2962
2963
2964static int wpa_supplicant_ctrl_iface_get_network(
2965 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2966{
2967 int id;
2968 size_t res;
2969 struct wpa_ssid *ssid;
2970 char *name, *value;
2971
2972 /* cmd: "<network id> <variable name>" */
2973 name = os_strchr(cmd, ' ');
2974 if (name == NULL || buflen == 0)
2975 return -1;
2976 *name++ = '\0';
2977
2978 id = atoi(cmd);
2979 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2980 id, name);
2981
2982 ssid = wpa_config_get_network(wpa_s->conf, id);
2983 if (ssid == NULL) {
2984 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2985 "id=%d", id);
2986 return -1;
2987 }
2988
2989 value = wpa_config_get_no_key(ssid, name);
2990 if (value == NULL) {
2991 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2992 "variable '%s'", name);
2993 return -1;
2994 }
2995
2996 res = os_strlcpy(buf, value, buflen);
2997 if (res >= buflen) {
2998 os_free(value);
2999 return -1;
3000 }
3001
3002 os_free(value);
3003
3004 return res;
3005}
3006
3007
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003008static int wpa_supplicant_ctrl_iface_dup_network(
3009 struct wpa_supplicant *wpa_s, char *cmd)
3010{
3011 struct wpa_ssid *ssid_s, *ssid_d;
3012 char *name, *id, *value;
3013 int id_s, id_d, ret;
3014
3015 /* cmd: "<src network id> <dst network id> <variable name>" */
3016 id = os_strchr(cmd, ' ');
3017 if (id == NULL)
3018 return -1;
3019 *id++ = '\0';
3020
3021 name = os_strchr(id, ' ');
3022 if (name == NULL)
3023 return -1;
3024 *name++ = '\0';
3025
3026 id_s = atoi(cmd);
3027 id_d = atoi(id);
3028 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DUP_NETWORK id=%d -> %d name='%s'",
3029 id_s, id_d, name);
3030
3031 ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
3032 if (ssid_s == NULL) {
3033 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3034 "network id=%d", id_s);
3035 return -1;
3036 }
3037
3038 ssid_d = wpa_config_get_network(wpa_s->conf, id_d);
3039 if (ssid_d == NULL) {
3040 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003041 "network id=%d", id_d);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003042 return -1;
3043 }
3044
3045 value = wpa_config_get(ssid_s, name);
3046 if (value == NULL) {
3047 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
3048 "variable '%s'", name);
3049 return -1;
3050 }
3051
3052 ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid_d, name,
3053 value);
3054
3055 os_free(value);
3056
3057 return ret;
3058}
3059
3060
Dmitry Shmidt04949592012-07-19 12:16:46 -07003061static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
3062 char *buf, size_t buflen)
3063{
3064 char *pos, *end;
3065 struct wpa_cred *cred;
3066 int ret;
3067
3068 pos = buf;
3069 end = buf + buflen;
3070 ret = os_snprintf(pos, end - pos,
3071 "cred id / realm / username / domain / imsi\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003072 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07003073 return pos - buf;
3074 pos += ret;
3075
3076 cred = wpa_s->conf->cred;
3077 while (cred) {
3078 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
3079 cred->id, cred->realm ? cred->realm : "",
3080 cred->username ? cred->username : "",
Dmitry Shmidt051af732013-10-22 13:52:46 -07003081 cred->domain ? cred->domain[0] : "",
Dmitry Shmidt04949592012-07-19 12:16:46 -07003082 cred->imsi ? cred->imsi : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003083 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07003084 return pos - buf;
3085 pos += ret;
3086
3087 cred = cred->next;
3088 }
3089
3090 return pos - buf;
3091}
3092
3093
3094static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
3095 char *buf, size_t buflen)
3096{
3097 struct wpa_cred *cred;
3098 int ret;
3099
3100 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
3101
3102 cred = wpa_config_add_cred(wpa_s->conf);
3103 if (cred == NULL)
3104 return -1;
3105
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003106 wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
3107
Dmitry Shmidt04949592012-07-19 12:16:46 -07003108 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003109 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07003110 return -1;
3111 return ret;
3112}
3113
3114
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003115static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
3116 struct wpa_cred *cred)
3117{
3118 struct wpa_ssid *ssid;
3119 char str[20];
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003120 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003121
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003122 if (cred == NULL) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003123 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3124 return -1;
3125 }
3126
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003127 id = cred->id;
3128 if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
3129 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3130 return -1;
3131 }
3132
3133 wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
3134
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003135 /* Remove any network entry created based on the removed credential */
3136 ssid = wpa_s->conf->ssid;
3137 while (ssid) {
3138 if (ssid->parent_cred == cred) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003139 int res;
3140
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003141 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
3142 "used the removed credential", ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003143 res = os_snprintf(str, sizeof(str), "%d", ssid->id);
3144 if (os_snprintf_error(sizeof(str), res))
3145 str[sizeof(str) - 1] = '\0';
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003146 ssid = ssid->next;
3147 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
3148 } else
3149 ssid = ssid->next;
3150 }
3151
3152 return 0;
3153}
3154
3155
Dmitry Shmidt04949592012-07-19 12:16:46 -07003156static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
3157 char *cmd)
3158{
3159 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003160 struct wpa_cred *cred, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003161
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003162 /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
3163 * "provisioning_sp=<FQDN> */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003164 if (os_strcmp(cmd, "all") == 0) {
3165 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
3166 cred = wpa_s->conf->cred;
3167 while (cred) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003168 prev = cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003169 cred = cred->next;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003170 wpas_ctrl_remove_cred(wpa_s, prev);
3171 }
3172 return 0;
3173 }
3174
3175 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
3176 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
3177 cmd + 8);
3178 cred = wpa_s->conf->cred;
3179 while (cred) {
3180 prev = cred;
3181 cred = cred->next;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003182 if (prev->domain) {
3183 size_t i;
3184 for (i = 0; i < prev->num_domain; i++) {
3185 if (os_strcmp(prev->domain[i], cmd + 8)
3186 != 0)
3187 continue;
3188 wpas_ctrl_remove_cred(wpa_s, prev);
3189 break;
3190 }
3191 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003192 }
3193 return 0;
3194 }
3195
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003196 if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
3197 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
3198 cmd + 16);
3199 cred = wpa_s->conf->cred;
3200 while (cred) {
3201 prev = cred;
3202 cred = cred->next;
3203 if (prev->provisioning_sp &&
3204 os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
3205 wpas_ctrl_remove_cred(wpa_s, prev);
3206 }
3207 return 0;
3208 }
3209
Dmitry Shmidt04949592012-07-19 12:16:46 -07003210 id = atoi(cmd);
3211 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
3212
3213 cred = wpa_config_get_cred(wpa_s->conf, id);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003214 return wpas_ctrl_remove_cred(wpa_s, cred);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003215}
3216
3217
3218static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
3219 char *cmd)
3220{
3221 int id;
3222 struct wpa_cred *cred;
3223 char *name, *value;
3224
3225 /* cmd: "<cred id> <variable name> <value>" */
3226 name = os_strchr(cmd, ' ');
3227 if (name == NULL)
3228 return -1;
3229 *name++ = '\0';
3230
3231 value = os_strchr(name, ' ');
3232 if (value == NULL)
3233 return -1;
3234 *value++ = '\0';
3235
3236 id = atoi(cmd);
3237 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
3238 id, name);
3239 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3240 (u8 *) value, os_strlen(value));
3241
3242 cred = wpa_config_get_cred(wpa_s->conf, id);
3243 if (cred == NULL) {
3244 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3245 id);
3246 return -1;
3247 }
3248
3249 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
3250 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
3251 "variable '%s'", name);
3252 return -1;
3253 }
3254
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003255 wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
3256
Dmitry Shmidt04949592012-07-19 12:16:46 -07003257 return 0;
3258}
3259
3260
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003261static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
3262 char *cmd, char *buf,
3263 size_t buflen)
3264{
3265 int id;
3266 size_t res;
3267 struct wpa_cred *cred;
3268 char *name, *value;
3269
3270 /* cmd: "<cred id> <variable name>" */
3271 name = os_strchr(cmd, ' ');
3272 if (name == NULL)
3273 return -1;
3274 *name++ = '\0';
3275
3276 id = atoi(cmd);
3277 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
3278 id, name);
3279
3280 cred = wpa_config_get_cred(wpa_s->conf, id);
3281 if (cred == NULL) {
3282 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3283 id);
3284 return -1;
3285 }
3286
3287 value = wpa_config_get_cred_no_key(cred, name);
3288 if (value == NULL) {
3289 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
3290 name);
3291 return -1;
3292 }
3293
3294 res = os_strlcpy(buf, value, buflen);
3295 if (res >= buflen) {
3296 os_free(value);
3297 return -1;
3298 }
3299
3300 os_free(value);
3301
3302 return res;
3303}
3304
3305
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003306#ifndef CONFIG_NO_CONFIG_WRITE
3307static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
3308{
3309 int ret;
3310
3311 if (!wpa_s->conf->update_config) {
3312 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
3313 "to update configuration (update_config=0)");
3314 return -1;
3315 }
3316
3317 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
3318 if (ret) {
3319 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
3320 "update configuration");
3321 } else {
3322 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
3323 " updated");
3324 }
3325
3326 return ret;
3327}
3328#endif /* CONFIG_NO_CONFIG_WRITE */
3329
3330
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003331struct cipher_info {
3332 unsigned int capa;
3333 const char *name;
3334 int group_only;
3335};
3336
3337static const struct cipher_info ciphers[] = {
3338 { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
3339 { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
3340 { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
3341 { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
3342 { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
3343 { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
3344 { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
3345 { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
3346};
3347
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003348static const struct cipher_info ciphers_group_mgmt[] = {
3349 { WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
3350 { WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
3351 { WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
3352 { WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
3353};
3354
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003356static int ctrl_iface_get_capability_pairwise(int res, char *strict,
3357 struct wpa_driver_capa *capa,
3358 char *buf, size_t buflen)
3359{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003360 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003361 char *pos, *end;
3362 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003363 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003364
3365 pos = buf;
3366 end = pos + buflen;
3367
3368 if (res < 0) {
3369 if (strict)
3370 return 0;
3371 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
3372 if (len >= buflen)
3373 return -1;
3374 return len;
3375 }
3376
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003377 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3378 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
3379 ret = os_snprintf(pos, end - pos, "%s%s",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003380 pos == buf ? "" : " ",
3381 ciphers[i].name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003382 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003383 return pos - buf;
3384 pos += ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003385 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003386 }
3387
3388 return pos - buf;
3389}
3390
3391
3392static int ctrl_iface_get_capability_group(int res, char *strict,
3393 struct wpa_driver_capa *capa,
3394 char *buf, size_t buflen)
3395{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003396 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003397 char *pos, *end;
3398 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003399 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003400
3401 pos = buf;
3402 end = pos + buflen;
3403
3404 if (res < 0) {
3405 if (strict)
3406 return 0;
3407 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
3408 if (len >= buflen)
3409 return -1;
3410 return len;
3411 }
3412
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003413 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3414 if (capa->enc & ciphers[i].capa) {
3415 ret = os_snprintf(pos, end - pos, "%s%s",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003416 pos == buf ? "" : " ",
3417 ciphers[i].name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003418 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003419 return pos - buf;
3420 pos += ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003421 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003422 }
3423
3424 return pos - buf;
3425}
3426
3427
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003428static int ctrl_iface_get_capability_group_mgmt(int res, char *strict,
3429 struct wpa_driver_capa *capa,
3430 char *buf, size_t buflen)
3431{
3432 int ret;
3433 char *pos, *end;
3434 unsigned int i;
3435
3436 pos = buf;
3437 end = pos + buflen;
3438
3439 if (res < 0)
3440 return 0;
3441
3442 for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
3443 if (capa->enc & ciphers_group_mgmt[i].capa) {
3444 ret = os_snprintf(pos, end - pos, "%s%s",
3445 pos == buf ? "" : " ",
3446 ciphers_group_mgmt[i].name);
3447 if (os_snprintf_error(end - pos, ret))
3448 return pos - buf;
3449 pos += ret;
3450 }
3451 }
3452
3453 return pos - buf;
3454}
3455
3456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003457static int ctrl_iface_get_capability_key_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 size_t len;
3464
3465 pos = buf;
3466 end = pos + buflen;
3467
3468 if (res < 0) {
3469 if (strict)
3470 return 0;
3471 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
3472 "NONE", buflen);
3473 if (len >= buflen)
3474 return -1;
3475 return len;
3476 }
3477
3478 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003479 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003480 return pos - buf;
3481 pos += ret;
3482
3483 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3484 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
3485 ret = os_snprintf(pos, end - pos, " WPA-EAP");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003486 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003487 return pos - buf;
3488 pos += ret;
3489 }
3490
3491 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3492 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
3493 ret = os_snprintf(pos, end - pos, " WPA-PSK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003494 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003495 return pos - buf;
3496 pos += ret;
3497 }
3498
3499 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
3500 ret = os_snprintf(pos, end - pos, " WPA-NONE");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003501 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502 return pos - buf;
3503 pos += ret;
3504 }
3505
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003506#ifdef CONFIG_SUITEB
3507 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
3508 ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
3509 if (os_snprintf_error(end - pos, ret))
3510 return pos - buf;
3511 pos += ret;
3512 }
3513#endif /* CONFIG_SUITEB */
3514#ifdef CONFIG_SUITEB192
3515 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
3516 ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
3517 if (os_snprintf_error(end - pos, ret))
3518 return pos - buf;
3519 pos += ret;
3520 }
3521#endif /* CONFIG_SUITEB192 */
3522
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003523 return pos - buf;
3524}
3525
3526
3527static int ctrl_iface_get_capability_proto(int res, char *strict,
3528 struct wpa_driver_capa *capa,
3529 char *buf, size_t buflen)
3530{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003531 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003532 char *pos, *end;
3533 size_t len;
3534
3535 pos = buf;
3536 end = pos + buflen;
3537
3538 if (res < 0) {
3539 if (strict)
3540 return 0;
3541 len = os_strlcpy(buf, "RSN WPA", buflen);
3542 if (len >= buflen)
3543 return -1;
3544 return len;
3545 }
3546
3547 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3548 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003549 ret = os_snprintf(pos, end - pos, "%sRSN",
3550 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003551 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552 return pos - buf;
3553 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003554 }
3555
3556 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3557 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003558 ret = os_snprintf(pos, end - pos, "%sWPA",
3559 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003560 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561 return pos - buf;
3562 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003563 }
3564
3565 return pos - buf;
3566}
3567
3568
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003569static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
3570 int res, char *strict,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003571 struct wpa_driver_capa *capa,
3572 char *buf, size_t buflen)
3573{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003574 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003575 char *pos, *end;
3576 size_t len;
3577
3578 pos = buf;
3579 end = pos + buflen;
3580
3581 if (res < 0) {
3582 if (strict)
3583 return 0;
3584 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
3585 if (len >= buflen)
3586 return -1;
3587 return len;
3588 }
3589
3590 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003591 ret = os_snprintf(pos, end - pos, "%sOPEN",
3592 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003593 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003594 return pos - buf;
3595 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003596 }
3597
3598 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
3599 ret = os_snprintf(pos, end - pos, "%sSHARED",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003600 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003601 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003602 return pos - buf;
3603 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604 }
3605
3606 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003607 ret = os_snprintf(pos, end - pos, "%sLEAP",
3608 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003609 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003610 return pos - buf;
3611 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003612 }
3613
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003614#ifdef CONFIG_SAE
3615 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
3616 ret = os_snprintf(pos, end - pos, "%sSAE",
3617 pos == buf ? "" : " ");
3618 if (os_snprintf_error(end - pos, ret))
3619 return pos - buf;
3620 pos += ret;
3621 }
3622#endif /* CONFIG_SAE */
3623
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003624 return pos - buf;
3625}
3626
3627
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003628static int ctrl_iface_get_capability_modes(int res, char *strict,
3629 struct wpa_driver_capa *capa,
3630 char *buf, size_t buflen)
3631{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003632 int ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003633 char *pos, *end;
3634 size_t len;
3635
3636 pos = buf;
3637 end = pos + buflen;
3638
3639 if (res < 0) {
3640 if (strict)
3641 return 0;
3642 len = os_strlcpy(buf, "IBSS AP", buflen);
3643 if (len >= buflen)
3644 return -1;
3645 return len;
3646 }
3647
3648 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003649 ret = os_snprintf(pos, end - pos, "%sIBSS",
3650 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003651 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003652 return pos - buf;
3653 pos += ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003654 }
3655
3656 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003657 ret = os_snprintf(pos, end - pos, "%sAP",
3658 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003659 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003660 return pos - buf;
3661 pos += ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003662 }
3663
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003664#ifdef CONFIG_MESH
3665 if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
3666 ret = os_snprintf(pos, end - pos, "%sMESH",
3667 pos == buf ? "" : " ");
3668 if (os_snprintf_error(end - pos, ret))
3669 return pos - buf;
3670 pos += ret;
3671 }
3672#endif /* CONFIG_MESH */
3673
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003674 return pos - buf;
3675}
3676
3677
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003678static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
3679 char *buf, size_t buflen)
3680{
3681 struct hostapd_channel_data *chnl;
3682 int ret, i, j;
3683 char *pos, *end, *hmode;
3684
3685 pos = buf;
3686 end = pos + buflen;
3687
3688 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3689 switch (wpa_s->hw.modes[j].mode) {
3690 case HOSTAPD_MODE_IEEE80211B:
3691 hmode = "B";
3692 break;
3693 case HOSTAPD_MODE_IEEE80211G:
3694 hmode = "G";
3695 break;
3696 case HOSTAPD_MODE_IEEE80211A:
3697 hmode = "A";
3698 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003699 case HOSTAPD_MODE_IEEE80211AD:
3700 hmode = "AD";
3701 break;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003702 default:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003703 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003704 }
3705 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003706 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003707 return pos - buf;
3708 pos += ret;
3709 chnl = wpa_s->hw.modes[j].channels;
3710 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003711 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3712 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003713 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003714 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003715 return pos - buf;
3716 pos += ret;
3717 }
3718 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003719 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003720 return pos - buf;
3721 pos += ret;
3722 }
3723
3724 return pos - buf;
3725}
3726
3727
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003728static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3729 char *buf, size_t buflen)
3730{
3731 struct hostapd_channel_data *chnl;
3732 int ret, i, j;
3733 char *pos, *end, *hmode;
3734
3735 pos = buf;
3736 end = pos + buflen;
3737
3738 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3739 switch (wpa_s->hw.modes[j].mode) {
3740 case HOSTAPD_MODE_IEEE80211B:
3741 hmode = "B";
3742 break;
3743 case HOSTAPD_MODE_IEEE80211G:
3744 hmode = "G";
3745 break;
3746 case HOSTAPD_MODE_IEEE80211A:
3747 hmode = "A";
3748 break;
3749 case HOSTAPD_MODE_IEEE80211AD:
3750 hmode = "AD";
3751 break;
3752 default:
3753 continue;
3754 }
3755 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3756 hmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003757 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003758 return pos - buf;
3759 pos += ret;
3760 chnl = wpa_s->hw.modes[j].channels;
3761 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3762 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3763 continue;
Dmitry Shmidt5da5e352014-02-03 13:30:46 -08003764 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003765 chnl[i].chan, chnl[i].freq,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003766 chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
3767 " (NO_IR)" : "",
Dmitry Shmidt5da5e352014-02-03 13:30:46 -08003768 chnl[i].flag & HOSTAPD_CHAN_RADAR ?
3769 " (DFS)" : "");
3770
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003771 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003772 return pos - buf;
3773 pos += ret;
3774 }
3775 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003776 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003777 return pos - buf;
3778 pos += ret;
3779 }
3780
3781 return pos - buf;
3782}
3783
3784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003785static int wpa_supplicant_ctrl_iface_get_capability(
3786 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3787 size_t buflen)
3788{
3789 struct wpa_driver_capa capa;
3790 int res;
3791 char *strict;
3792 char field[30];
3793 size_t len;
3794
3795 /* Determine whether or not strict checking was requested */
3796 len = os_strlcpy(field, _field, sizeof(field));
3797 if (len >= sizeof(field))
3798 return -1;
3799 strict = os_strchr(field, ' ');
3800 if (strict != NULL) {
3801 *strict++ = '\0';
3802 if (os_strcmp(strict, "strict") != 0)
3803 return -1;
3804 }
3805
3806 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3807 field, strict ? strict : "");
3808
3809 if (os_strcmp(field, "eap") == 0) {
3810 return eap_get_names(buf, buflen);
3811 }
3812
3813 res = wpa_drv_get_capa(wpa_s, &capa);
3814
3815 if (os_strcmp(field, "pairwise") == 0)
3816 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3817 buf, buflen);
3818
3819 if (os_strcmp(field, "group") == 0)
3820 return ctrl_iface_get_capability_group(res, strict, &capa,
3821 buf, buflen);
3822
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003823 if (os_strcmp(field, "group_mgmt") == 0)
3824 return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
3825 buf, buflen);
3826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003827 if (os_strcmp(field, "key_mgmt") == 0)
3828 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3829 buf, buflen);
3830
3831 if (os_strcmp(field, "proto") == 0)
3832 return ctrl_iface_get_capability_proto(res, strict, &capa,
3833 buf, buflen);
3834
3835 if (os_strcmp(field, "auth_alg") == 0)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003836 return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
3837 &capa, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003838
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003839 if (os_strcmp(field, "modes") == 0)
3840 return ctrl_iface_get_capability_modes(res, strict, &capa,
3841 buf, buflen);
3842
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003843 if (os_strcmp(field, "channels") == 0)
3844 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3845
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003846 if (os_strcmp(field, "freq") == 0)
3847 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3848
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003849#ifdef CONFIG_TDLS
3850 if (os_strcmp(field, "tdls") == 0)
3851 return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
3852#endif /* CONFIG_TDLS */
3853
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003854#ifdef CONFIG_ERP
3855 if (os_strcmp(field, "erp") == 0) {
3856 res = os_snprintf(buf, buflen, "ERP");
3857 if (os_snprintf_error(buflen, res))
3858 return -1;
3859 return res;
3860 }
3861#endif /* CONFIG_EPR */
3862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3864 field);
3865
3866 return -1;
3867}
3868
3869
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003870#ifdef CONFIG_INTERWORKING
3871static char * anqp_add_hex(char *pos, char *end, const char *title,
3872 struct wpabuf *data)
3873{
3874 char *start = pos;
3875 size_t i;
3876 int ret;
3877 const u8 *d;
3878
3879 if (data == NULL)
3880 return start;
3881
3882 ret = os_snprintf(pos, end - pos, "%s=", title);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003883 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003884 return start;
3885 pos += ret;
3886
3887 d = wpabuf_head_u8(data);
3888 for (i = 0; i < wpabuf_len(data); i++) {
3889 ret = os_snprintf(pos, end - pos, "%02x", *d++);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003890 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003891 return start;
3892 pos += ret;
3893 }
3894
3895 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003896 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003897 return start;
3898 pos += ret;
3899
3900 return pos;
3901}
3902#endif /* CONFIG_INTERWORKING */
3903
3904
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003905static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3906 unsigned long mask, char *buf, size_t buflen)
3907{
3908 size_t i;
3909 int ret;
3910 char *pos, *end;
3911 const u8 *ie, *ie2;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003912
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003913 pos = buf;
3914 end = buf + buflen;
3915
3916 if (mask & WPA_BSS_MASK_ID) {
3917 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003918 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003919 return 0;
3920 pos += ret;
3921 }
3922
3923 if (mask & WPA_BSS_MASK_BSSID) {
3924 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3925 MAC2STR(bss->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003926 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003927 return 0;
3928 pos += ret;
3929 }
3930
3931 if (mask & WPA_BSS_MASK_FREQ) {
3932 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003933 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003934 return 0;
3935 pos += ret;
3936 }
3937
3938 if (mask & WPA_BSS_MASK_BEACON_INT) {
3939 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3940 bss->beacon_int);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003941 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003942 return 0;
3943 pos += ret;
3944 }
3945
3946 if (mask & WPA_BSS_MASK_CAPABILITIES) {
3947 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3948 bss->caps);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003949 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003950 return 0;
3951 pos += ret;
3952 }
3953
3954 if (mask & WPA_BSS_MASK_QUAL) {
3955 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003956 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003957 return 0;
3958 pos += ret;
3959 }
3960
3961 if (mask & WPA_BSS_MASK_NOISE) {
3962 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003963 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003964 return 0;
3965 pos += ret;
3966 }
3967
3968 if (mask & WPA_BSS_MASK_LEVEL) {
3969 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
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_TSF) {
3976 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3977 (unsigned long long) bss->tsf);
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_AGE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003984 struct os_reltime now;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003985
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003986 os_get_reltime(&now);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003987 ret = os_snprintf(pos, end - pos, "age=%d\n",
3988 (int) (now.sec - bss->last_update.sec));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003989 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003990 return 0;
3991 pos += ret;
3992 }
3993
3994 if (mask & WPA_BSS_MASK_IE) {
3995 ret = os_snprintf(pos, end - pos, "ie=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003996 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003997 return 0;
3998 pos += ret;
3999
4000 ie = (const u8 *) (bss + 1);
4001 for (i = 0; i < bss->ie_len; i++) {
4002 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004003 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004004 return 0;
4005 pos += ret;
4006 }
4007
4008 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004009 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004010 return 0;
4011 pos += ret;
4012 }
4013
4014 if (mask & WPA_BSS_MASK_FLAGS) {
4015 ret = os_snprintf(pos, end - pos, "flags=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004016 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004017 return 0;
4018 pos += ret;
4019
4020 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
4021 if (ie)
4022 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
4023 2 + ie[1]);
4024 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
4025 if (ie2)
4026 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
4027 2 + ie2[1]);
4028 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
4029 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
4030 ret = os_snprintf(pos, end - pos, "[WEP]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004031 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004032 return 0;
4033 pos += ret;
4034 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004035 if (bss_is_dmg(bss)) {
4036 const char *s;
4037 ret = os_snprintf(pos, end - pos, "[DMG]");
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;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004041 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
4042 case IEEE80211_CAP_DMG_IBSS:
4043 s = "[IBSS]";
4044 break;
4045 case IEEE80211_CAP_DMG_AP:
4046 s = "[ESS]";
4047 break;
4048 case IEEE80211_CAP_DMG_PBSS:
4049 s = "[PBSS]";
4050 break;
4051 default:
4052 s = "";
4053 break;
4054 }
4055 ret = os_snprintf(pos, end - pos, "%s", s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004056 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004057 return 0;
4058 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004059 } else {
4060 if (bss->caps & IEEE80211_CAP_IBSS) {
4061 ret = os_snprintf(pos, end - pos, "[IBSS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004062 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004063 return 0;
4064 pos += ret;
4065 }
4066 if (bss->caps & IEEE80211_CAP_ESS) {
4067 ret = os_snprintf(pos, end - pos, "[ESS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004068 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004069 return 0;
4070 pos += ret;
4071 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004072 }
Dmitry Shmidt96571392013-10-14 12:54:46 -07004073 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
4074 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004075 ret = os_snprintf(pos, end - pos, "[P2P]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004076 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004077 return 0;
4078 pos += ret;
4079 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004080#ifdef CONFIG_HS20
4081 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
4082 ret = os_snprintf(pos, end - pos, "[HS20]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004083 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004084 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004085 pos += ret;
4086 }
4087#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004088
4089 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004090 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004091 return 0;
4092 pos += ret;
4093 }
4094
4095 if (mask & WPA_BSS_MASK_SSID) {
4096 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
4097 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004098 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004099 return 0;
4100 pos += ret;
4101 }
4102
4103#ifdef CONFIG_WPS
4104 if (mask & WPA_BSS_MASK_WPS_SCAN) {
4105 ie = (const u8 *) (bss + 1);
4106 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
4107 if (ret < 0 || ret >= end - pos)
4108 return 0;
4109 pos += ret;
4110 }
4111#endif /* CONFIG_WPS */
4112
4113#ifdef CONFIG_P2P
4114 if (mask & WPA_BSS_MASK_P2P_SCAN) {
4115 ie = (const u8 *) (bss + 1);
4116 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
4117 if (ret < 0 || ret >= end - pos)
4118 return 0;
4119 pos += ret;
4120 }
4121#endif /* CONFIG_P2P */
4122
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004123#ifdef CONFIG_WIFI_DISPLAY
4124 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
4125 struct wpabuf *wfd;
4126 ie = (const u8 *) (bss + 1);
4127 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
4128 WFD_IE_VENDOR_TYPE);
4129 if (wfd) {
4130 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004131 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004132 wpabuf_free(wfd);
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004133 return 0;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004134 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004135 pos += ret;
4136
4137 pos += wpa_snprintf_hex(pos, end - pos,
4138 wpabuf_head(wfd),
4139 wpabuf_len(wfd));
4140 wpabuf_free(wfd);
4141
4142 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004143 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004144 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004145 pos += ret;
4146 }
4147 }
4148#endif /* CONFIG_WIFI_DISPLAY */
4149
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004150#ifdef CONFIG_INTERWORKING
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004151 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
4152 struct wpa_bss_anqp *anqp = bss->anqp;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004153 pos = anqp_add_hex(pos, end, "anqp_venue_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004154 anqp->venue_name);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004155 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004156 anqp->network_auth_type);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004157 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004158 anqp->roaming_consortium);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004159 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004160 anqp->ip_addr_type_availability);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004161 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004162 anqp->nai_realm);
4163 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004164 pos = anqp_add_hex(pos, end, "anqp_domain_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004165 anqp->domain_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004166#ifdef CONFIG_HS20
4167 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004168 anqp->hs20_operator_friendly_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004169 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004170 anqp->hs20_wan_metrics);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004171 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004172 anqp->hs20_connection_capability);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004173 pos = anqp_add_hex(pos, end, "hs20_operating_class",
4174 anqp->hs20_operating_class);
4175 pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
4176 anqp->hs20_osu_providers_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004177#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004178 }
4179#endif /* CONFIG_INTERWORKING */
4180
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004181#ifdef CONFIG_MESH
4182 if (mask & WPA_BSS_MASK_MESH_SCAN) {
4183 ie = (const u8 *) (bss + 1);
4184 ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
4185 if (ret < 0 || ret >= end - pos)
4186 return 0;
4187 pos += ret;
4188 }
4189#endif /* CONFIG_MESH */
4190
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004191 if (mask & WPA_BSS_MASK_DELIM) {
4192 ret = os_snprintf(pos, end - pos, "====\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004193 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004194 return 0;
4195 pos += ret;
4196 }
Irfan Sheriffe2ea0082012-08-13 10:56:16 -07004197
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004198 return pos - buf;
4199}
4200
Dmitry Shmidt04949592012-07-19 12:16:46 -07004201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004202static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
4203 const char *cmd, char *buf,
4204 size_t buflen)
4205{
4206 u8 bssid[ETH_ALEN];
4207 size_t i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004208 struct wpa_bss *bss;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004209 struct wpa_bss *bsslast = NULL;
4210 struct dl_list *next;
4211 int ret = 0;
4212 int len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004213 char *ctmp, *end = buf + buflen;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004214 unsigned long mask = WPA_BSS_MASK_ALL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004215
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004216 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
4217 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
4218 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004219 list_id);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004220 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
4221 list_id);
4222 } else { /* N1-N2 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004223 unsigned int id1, id2;
4224
4225 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
4226 wpa_printf(MSG_INFO, "Wrong BSS range "
4227 "format");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004228 return 0;
4229 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004230
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004231 if (*(cmd + 6) == '-')
4232 id1 = 0;
4233 else
4234 id1 = atoi(cmd + 6);
4235 ctmp++;
4236 if (*ctmp >= '0' && *ctmp <= '9')
4237 id2 = atoi(ctmp);
4238 else
4239 id2 = (unsigned int) -1;
4240 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
4241 if (id2 == (unsigned int) -1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004242 bsslast = dl_list_last(&wpa_s->bss_id,
4243 struct wpa_bss,
4244 list_id);
4245 else {
4246 bsslast = wpa_bss_get_id(wpa_s, id2);
4247 if (bsslast == NULL && bss && id2 > id1) {
4248 struct wpa_bss *tmp = bss;
4249 for (;;) {
4250 next = tmp->list_id.next;
4251 if (next == &wpa_s->bss_id)
4252 break;
4253 tmp = dl_list_entry(
4254 next, struct wpa_bss,
4255 list_id);
4256 if (tmp->id > id2)
4257 break;
4258 bsslast = tmp;
4259 }
4260 }
4261 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004262 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004263 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004264 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004265 else if (os_strncmp(cmd, "LAST", 4) == 0)
4266 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004267 else if (os_strncmp(cmd, "ID-", 3) == 0) {
4268 i = atoi(cmd + 3);
4269 bss = wpa_bss_get_id(wpa_s, i);
4270 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4271 i = atoi(cmd + 5);
4272 bss = wpa_bss_get_id(wpa_s, i);
4273 if (bss) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004274 next = bss->list_id.next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004275 if (next == &wpa_s->bss_id)
4276 bss = NULL;
4277 else
4278 bss = dl_list_entry(next, struct wpa_bss,
4279 list_id);
4280 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004281#ifdef CONFIG_P2P
4282 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
4283 if (hwaddr_aton(cmd + 13, bssid) == 0)
4284 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
4285 else
4286 bss = NULL;
4287#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004288 } else if (hwaddr_aton(cmd, bssid) == 0)
4289 bss = wpa_bss_get_bssid(wpa_s, bssid);
4290 else {
4291 struct wpa_bss *tmp;
4292 i = atoi(cmd);
4293 bss = NULL;
4294 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
4295 {
4296 if (i-- == 0) {
4297 bss = tmp;
4298 break;
4299 }
4300 }
4301 }
4302
Dmitry Shmidt04949592012-07-19 12:16:46 -07004303 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
4304 mask = strtoul(ctmp + 5, NULL, 0x10);
4305 if (mask == 0)
4306 mask = WPA_BSS_MASK_ALL;
4307 }
4308
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309 if (bss == NULL)
4310 return 0;
4311
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004312 if (bsslast == NULL)
4313 bsslast = bss;
4314 do {
4315 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
4316 ret += len;
4317 buf += len;
4318 buflen -= len;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004319 if (bss == bsslast) {
4320 if ((mask & WPA_BSS_MASK_DELIM) && len &&
4321 (bss == dl_list_last(&wpa_s->bss_id,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004322 struct wpa_bss, list_id))) {
4323 int res;
4324
4325 res = os_snprintf(buf - 5, end - buf + 5,
4326 "####\n");
4327 if (os_snprintf_error(end - buf + 5, res)) {
4328 wpa_printf(MSG_DEBUG,
4329 "Could not add end delim");
4330 }
4331 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004332 break;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004333 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004334 next = bss->list_id.next;
4335 if (next == &wpa_s->bss_id)
4336 break;
4337 bss = dl_list_entry(next, struct wpa_bss, list_id);
4338 } while (bss && len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004339
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004340 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004341}
4342
4343
4344static int wpa_supplicant_ctrl_iface_ap_scan(
4345 struct wpa_supplicant *wpa_s, char *cmd)
4346{
4347 int ap_scan = atoi(cmd);
4348 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
4349}
4350
4351
4352static int wpa_supplicant_ctrl_iface_scan_interval(
4353 struct wpa_supplicant *wpa_s, char *cmd)
4354{
4355 int scan_int = atoi(cmd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004356 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004357}
4358
4359
4360static int wpa_supplicant_ctrl_iface_bss_expire_age(
4361 struct wpa_supplicant *wpa_s, char *cmd)
4362{
4363 int expire_age = atoi(cmd);
4364 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
4365}
4366
4367
4368static int wpa_supplicant_ctrl_iface_bss_expire_count(
4369 struct wpa_supplicant *wpa_s, char *cmd)
4370{
4371 int expire_count = atoi(cmd);
4372 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
4373}
4374
4375
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004376static void wpa_supplicant_ctrl_iface_bss_flush(
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07004377 struct wpa_supplicant *wpa_s, char *cmd)
4378{
4379 int flush_age = atoi(cmd);
4380
4381 if (flush_age == 0)
4382 wpa_bss_flush(wpa_s);
4383 else
4384 wpa_bss_flush_by_age(wpa_s, flush_age);
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07004385}
4386
4387
Dmitry Shmidt21de2142014-04-08 10:50:52 -07004388#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
4390{
4391 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
4392 /* MLME-DELETEKEYS.request */
4393 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
4394 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
4395 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
4396 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
4397#ifdef CONFIG_IEEE80211W
4398 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
4399 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
4400#endif /* CONFIG_IEEE80211W */
4401
4402 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
4403 0);
4404 /* MLME-SETPROTECTION.request(None) */
4405 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
4406 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
4407 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
4408 wpa_sm_drop_sa(wpa_s->wpa);
4409}
Dmitry Shmidt21de2142014-04-08 10:50:52 -07004410#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004411
4412
4413static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
4414 char *addr)
4415{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004416#ifdef CONFIG_NO_SCAN_PROCESSING
4417 return -1;
4418#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004419 u8 bssid[ETH_ALEN];
4420 struct wpa_bss *bss;
4421 struct wpa_ssid *ssid = wpa_s->current_ssid;
4422
4423 if (hwaddr_aton(addr, bssid)) {
4424 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
4425 "address '%s'", addr);
4426 return -1;
4427 }
4428
4429 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
4430
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004431 if (!ssid) {
4432 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
4433 "configuration known for the target AP");
4434 return -1;
4435 }
4436
4437 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004438 if (!bss) {
4439 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
4440 "from BSS table");
4441 return -1;
4442 }
4443
4444 /*
4445 * TODO: Find best network configuration block from configuration to
4446 * allow roaming to other networks
4447 */
4448
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004449 wpa_s->reassociate = 1;
4450 wpa_supplicant_connect(wpa_s, bss, ssid);
4451
4452 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004453#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004454}
4455
4456
4457#ifdef CONFIG_P2P
4458static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
4459{
4460 unsigned int timeout = atoi(cmd);
4461 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004462 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004463 u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004464 char *pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004465 unsigned int search_delay;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004466
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004467 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4468 wpa_dbg(wpa_s, MSG_INFO,
4469 "Reject P2P_FIND since interface is disabled");
4470 return -1;
4471 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004472 if (os_strstr(cmd, "type=social"))
4473 type = P2P_FIND_ONLY_SOCIAL;
4474 else if (os_strstr(cmd, "type=progressive"))
4475 type = P2P_FIND_PROGRESSIVE;
4476
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004477 pos = os_strstr(cmd, "dev_id=");
4478 if (pos) {
4479 pos += 7;
4480 if (hwaddr_aton(pos, dev_id))
4481 return -1;
4482 _dev_id = dev_id;
4483 }
4484
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004485 pos = os_strstr(cmd, "dev_type=");
4486 if (pos) {
4487 pos += 9;
4488 if (wps_dev_type_str2bin(pos, dev_type) < 0)
4489 return -1;
4490 _dev_type = dev_type;
4491 }
4492
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004493 pos = os_strstr(cmd, "delay=");
4494 if (pos) {
4495 pos += 6;
4496 search_delay = atoi(pos);
4497 } else
4498 search_delay = wpas_p2p_search_delay(wpa_s);
4499
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004500 return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
4501 _dev_id, search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004502}
4503
4504
4505static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
4506 char *buf, size_t buflen)
4507{
4508 u8 addr[ETH_ALEN];
4509 char *pos, *pos2;
4510 char *pin = NULL;
4511 enum p2p_wps_method wps_method;
4512 int new_pin;
4513 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004514 int persistent_group, persistent_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515 int join;
4516 int auth;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004517 int automatic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004518 int go_intent = -1;
4519 int freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004520 int pd;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004521 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004522
Dmitry Shmidt04949592012-07-19 12:16:46 -07004523 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
4524 * [persistent|persistent=<network id>]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004525 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004526 * [ht40] [vht] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004527
4528 if (hwaddr_aton(cmd, addr))
4529 return -1;
4530
4531 pos = cmd + 17;
4532 if (*pos != ' ')
4533 return -1;
4534 pos++;
4535
4536 persistent_group = os_strstr(pos, " persistent") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004537 pos2 = os_strstr(pos, " persistent=");
4538 if (pos2) {
4539 struct wpa_ssid *ssid;
4540 persistent_id = atoi(pos2 + 12);
4541 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4542 if (ssid == NULL || ssid->disabled != 2 ||
4543 ssid->mode != WPAS_MODE_P2P_GO) {
4544 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
4545 "SSID id=%d for persistent P2P group (GO)",
4546 persistent_id);
4547 return -1;
4548 }
4549 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004550 join = os_strstr(pos, " join") != NULL;
4551 auth = os_strstr(pos, " auth") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004552 automatic = os_strstr(pos, " auto") != NULL;
4553 pd = os_strstr(pos, " provdisc") != NULL;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004554 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4555 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4556 vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004557
4558 pos2 = os_strstr(pos, " go_intent=");
4559 if (pos2) {
4560 pos2 += 11;
4561 go_intent = atoi(pos2);
4562 if (go_intent < 0 || go_intent > 15)
4563 return -1;
4564 }
4565
4566 pos2 = os_strstr(pos, " freq=");
4567 if (pos2) {
4568 pos2 += 6;
4569 freq = atoi(pos2);
4570 if (freq <= 0)
4571 return -1;
4572 }
4573
4574 if (os_strncmp(pos, "pin", 3) == 0) {
4575 /* Request random PIN (to be displayed) and enable the PIN */
4576 wps_method = WPS_PIN_DISPLAY;
4577 } else if (os_strncmp(pos, "pbc", 3) == 0) {
4578 wps_method = WPS_PBC;
4579 } else {
4580 pin = pos;
4581 pos = os_strchr(pin, ' ');
4582 wps_method = WPS_PIN_KEYPAD;
4583 if (pos) {
4584 *pos++ = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004585 if (os_strncmp(pos, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004586 wps_method = WPS_PIN_DISPLAY;
4587 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004588 if (!wps_pin_str_valid(pin)) {
4589 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
4590 return 17;
4591 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004592 }
4593
4594 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004595 persistent_group, automatic, join,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004596 auth, go_intent, freq, persistent_id, pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004597 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004598 if (new_pin == -2) {
4599 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
4600 return 25;
4601 }
4602 if (new_pin == -3) {
4603 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
4604 return 25;
4605 }
4606 if (new_pin < 0)
4607 return -1;
4608 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
4609 ret = os_snprintf(buf, buflen, "%08d", new_pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004610 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611 return -1;
4612 return ret;
4613 }
4614
4615 os_memcpy(buf, "OK\n", 3);
4616 return 3;
4617}
4618
4619
4620static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
4621{
4622 unsigned int timeout = atoi(cmd);
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004623 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4624 wpa_dbg(wpa_s, MSG_INFO,
4625 "Reject P2P_LISTEN since interface is disabled");
4626 return -1;
4627 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004628 return wpas_p2p_listen(wpa_s, timeout);
4629}
4630
4631
4632static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
4633{
4634 u8 addr[ETH_ALEN];
4635 char *pos;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004636 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004637
Dmitry Shmidt04949592012-07-19 12:16:46 -07004638 /* <addr> <config method> [join|auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639
4640 if (hwaddr_aton(cmd, addr))
4641 return -1;
4642
4643 pos = cmd + 17;
4644 if (*pos != ' ')
4645 return -1;
4646 pos++;
4647
Dmitry Shmidt04949592012-07-19 12:16:46 -07004648 if (os_strstr(pos, " join") != NULL)
4649 use = WPAS_P2P_PD_FOR_JOIN;
4650 else if (os_strstr(pos, " auto") != NULL)
4651 use = WPAS_P2P_PD_AUTO;
4652
4653 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004654}
4655
4656
4657static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
4658 size_t buflen)
4659{
4660 struct wpa_ssid *ssid = wpa_s->current_ssid;
4661
4662 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
4663 ssid->passphrase == NULL)
4664 return -1;
4665
4666 os_strlcpy(buf, ssid->passphrase, buflen);
4667 return os_strlen(buf);
4668}
4669
4670
4671static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
4672 char *buf, size_t buflen)
4673{
4674 u64 ref;
4675 int res;
4676 u8 dst_buf[ETH_ALEN], *dst;
4677 struct wpabuf *tlvs;
4678 char *pos;
4679 size_t len;
4680
4681 if (hwaddr_aton(cmd, dst_buf))
4682 return -1;
4683 dst = dst_buf;
4684 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
4685 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
4686 dst = NULL;
4687 pos = cmd + 17;
4688 if (*pos != ' ')
4689 return -1;
4690 pos++;
4691
4692 if (os_strncmp(pos, "upnp ", 5) == 0) {
4693 u8 version;
4694 pos += 5;
4695 if (hexstr2bin(pos, &version, 1) < 0)
4696 return -1;
4697 pos += 2;
4698 if (*pos != ' ')
4699 return -1;
4700 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004701 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004702#ifdef CONFIG_WIFI_DISPLAY
4703 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
4704 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
4705#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004706 } else {
4707 len = os_strlen(pos);
4708 if (len & 1)
4709 return -1;
4710 len /= 2;
4711 tlvs = wpabuf_alloc(len);
4712 if (tlvs == NULL)
4713 return -1;
4714 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
4715 wpabuf_free(tlvs);
4716 return -1;
4717 }
4718
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004719 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720 wpabuf_free(tlvs);
4721 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004722 if (ref == 0)
4723 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004724 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004725 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726 return -1;
4727 return res;
4728}
4729
4730
4731static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
4732 char *cmd)
4733{
4734 long long unsigned val;
4735 u64 req;
4736 if (sscanf(cmd, "%llx", &val) != 1)
4737 return -1;
4738 req = val;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004739 return wpas_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004740}
4741
4742
4743static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
4744{
4745 int freq;
4746 u8 dst[ETH_ALEN];
4747 u8 dialog_token;
4748 struct wpabuf *resp_tlvs;
4749 char *pos, *pos2;
4750 size_t len;
4751
4752 pos = os_strchr(cmd, ' ');
4753 if (pos == NULL)
4754 return -1;
4755 *pos++ = '\0';
4756 freq = atoi(cmd);
4757 if (freq == 0)
4758 return -1;
4759
4760 if (hwaddr_aton(pos, dst))
4761 return -1;
4762 pos += 17;
4763 if (*pos != ' ')
4764 return -1;
4765 pos++;
4766
4767 pos2 = os_strchr(pos, ' ');
4768 if (pos2 == NULL)
4769 return -1;
4770 *pos2++ = '\0';
4771 dialog_token = atoi(pos);
4772
4773 len = os_strlen(pos2);
4774 if (len & 1)
4775 return -1;
4776 len /= 2;
4777 resp_tlvs = wpabuf_alloc(len);
4778 if (resp_tlvs == NULL)
4779 return -1;
4780 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
4781 wpabuf_free(resp_tlvs);
4782 return -1;
4783 }
4784
4785 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
4786 wpabuf_free(resp_tlvs);
4787 return 0;
4788}
4789
4790
4791static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
4792 char *cmd)
4793{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004794 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
4795 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004796 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
4797 return 0;
4798}
4799
4800
4801static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
4802 char *cmd)
4803{
4804 char *pos;
4805 size_t len;
4806 struct wpabuf *query, *resp;
4807
4808 pos = os_strchr(cmd, ' ');
4809 if (pos == NULL)
4810 return -1;
4811 *pos++ = '\0';
4812
4813 len = os_strlen(cmd);
4814 if (len & 1)
4815 return -1;
4816 len /= 2;
4817 query = wpabuf_alloc(len);
4818 if (query == NULL)
4819 return -1;
4820 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4821 wpabuf_free(query);
4822 return -1;
4823 }
4824
4825 len = os_strlen(pos);
4826 if (len & 1) {
4827 wpabuf_free(query);
4828 return -1;
4829 }
4830 len /= 2;
4831 resp = wpabuf_alloc(len);
4832 if (resp == NULL) {
4833 wpabuf_free(query);
4834 return -1;
4835 }
4836 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4837 wpabuf_free(query);
4838 wpabuf_free(resp);
4839 return -1;
4840 }
4841
4842 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4843 wpabuf_free(query);
4844 wpabuf_free(resp);
4845 return -1;
4846 }
4847 return 0;
4848}
4849
4850
4851static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4852{
4853 char *pos;
4854 u8 version;
4855
4856 pos = os_strchr(cmd, ' ');
4857 if (pos == NULL)
4858 return -1;
4859 *pos++ = '\0';
4860
4861 if (hexstr2bin(cmd, &version, 1) < 0)
4862 return -1;
4863
4864 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4865}
4866
4867
4868static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4869{
4870 char *pos;
4871
4872 pos = os_strchr(cmd, ' ');
4873 if (pos == NULL)
4874 return -1;
4875 *pos++ = '\0';
4876
4877 if (os_strcmp(cmd, "bonjour") == 0)
4878 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4879 if (os_strcmp(cmd, "upnp") == 0)
4880 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4881 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4882 return -1;
4883}
4884
4885
4886static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4887 char *cmd)
4888{
4889 size_t len;
4890 struct wpabuf *query;
4891 int ret;
4892
4893 len = os_strlen(cmd);
4894 if (len & 1)
4895 return -1;
4896 len /= 2;
4897 query = wpabuf_alloc(len);
4898 if (query == NULL)
4899 return -1;
4900 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4901 wpabuf_free(query);
4902 return -1;
4903 }
4904
4905 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4906 wpabuf_free(query);
4907 return ret;
4908}
4909
4910
4911static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4912{
4913 char *pos;
4914 u8 version;
4915
4916 pos = os_strchr(cmd, ' ');
4917 if (pos == NULL)
4918 return -1;
4919 *pos++ = '\0';
4920
4921 if (hexstr2bin(cmd, &version, 1) < 0)
4922 return -1;
4923
4924 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4925}
4926
4927
4928static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4929{
4930 char *pos;
4931
4932 pos = os_strchr(cmd, ' ');
4933 if (pos == NULL)
4934 return -1;
4935 *pos++ = '\0';
4936
4937 if (os_strcmp(cmd, "bonjour") == 0)
4938 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4939 if (os_strcmp(cmd, "upnp") == 0)
4940 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4941 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4942 return -1;
4943}
4944
4945
4946static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4947{
4948 u8 addr[ETH_ALEN];
4949
4950 /* <addr> */
4951
4952 if (hwaddr_aton(cmd, addr))
4953 return -1;
4954
4955 return wpas_p2p_reject(wpa_s, addr);
4956}
4957
4958
4959static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4960{
4961 char *pos;
4962 int id;
4963 struct wpa_ssid *ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004964 u8 *_peer = NULL, peer[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004965 int freq = 0, pref_freq = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004966 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967
4968 id = atoi(cmd);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004969 pos = os_strstr(cmd, " peer=");
4970 if (pos) {
4971 pos += 6;
4972 if (hwaddr_aton(pos, peer))
4973 return -1;
4974 _peer = peer;
4975 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004976 ssid = wpa_config_get_network(wpa_s->conf, id);
4977 if (ssid == NULL || ssid->disabled != 2) {
4978 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4979 "for persistent P2P group",
4980 id);
4981 return -1;
4982 }
4983
Jouni Malinen31be0a42012-08-31 21:20:51 +03004984 pos = os_strstr(cmd, " freq=");
4985 if (pos) {
4986 pos += 6;
4987 freq = atoi(pos);
4988 if (freq <= 0)
4989 return -1;
4990 }
4991
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004992 pos = os_strstr(cmd, " pref=");
4993 if (pos) {
4994 pos += 6;
4995 pref_freq = atoi(pos);
4996 if (pref_freq <= 0)
4997 return -1;
4998 }
4999
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005000 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
5001 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
5002 vht;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005003
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005004 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
5005 pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005006}
5007
5008
5009static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
5010{
5011 char *pos;
5012 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
5013
5014 pos = os_strstr(cmd, " peer=");
5015 if (!pos)
5016 return -1;
5017
5018 *pos = '\0';
5019 pos += 6;
5020 if (hwaddr_aton(pos, peer)) {
5021 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
5022 return -1;
5023 }
5024
5025 pos = os_strstr(pos, " go_dev_addr=");
5026 if (pos) {
5027 pos += 13;
5028 if (hwaddr_aton(pos, go_dev_addr)) {
5029 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
5030 pos);
5031 return -1;
5032 }
5033 go_dev = go_dev_addr;
5034 }
5035
5036 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
5037}
5038
5039
5040static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
5041{
5042 if (os_strncmp(cmd, "persistent=", 11) == 0)
5043 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
5044 if (os_strncmp(cmd, "group=", 6) == 0)
5045 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
5046
5047 return -1;
5048}
5049
5050
5051static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005052 char *cmd, int freq, int ht40,
5053 int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005054{
5055 int id;
5056 struct wpa_ssid *ssid;
5057
5058 id = atoi(cmd);
5059 ssid = wpa_config_get_network(wpa_s->conf, id);
5060 if (ssid == NULL || ssid->disabled != 2) {
5061 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
5062 "for persistent P2P group",
5063 id);
5064 return -1;
5065 }
5066
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005067 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005068 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005069}
5070
5071
5072static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
5073{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005074 int freq = 0, ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005075 char *pos;
5076
5077 pos = os_strstr(cmd, "freq=");
5078 if (pos)
5079 freq = atoi(pos + 5);
5080
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005081 vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
5082 ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
5083 vht;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005084
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005085 if (os_strncmp(cmd, "persistent=", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005086 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005087 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005088 if (os_strcmp(cmd, "persistent") == 0 ||
5089 os_strncmp(cmd, "persistent ", 11) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005090 return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005091 if (os_strncmp(cmd, "freq=", 5) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005092 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005093 if (ht40)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005094 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005095
5096 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
5097 cmd);
5098 return -1;
5099}
5100
5101
5102static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
5103 char *buf, size_t buflen)
5104{
5105 u8 addr[ETH_ALEN], *addr_ptr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005106 int next, res;
5107 const struct p2p_peer_info *info;
5108 char *pos, *end;
5109 char devtype[WPS_DEV_TYPE_BUFSIZE];
5110 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005111 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112
5113 if (!wpa_s->global->p2p)
5114 return -1;
5115
5116 if (os_strcmp(cmd, "FIRST") == 0) {
5117 addr_ptr = NULL;
5118 next = 0;
5119 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
5120 if (hwaddr_aton(cmd + 5, addr) < 0)
5121 return -1;
5122 addr_ptr = addr;
5123 next = 1;
5124 } else {
5125 if (hwaddr_aton(cmd, addr) < 0)
5126 return -1;
5127 addr_ptr = addr;
5128 next = 0;
5129 }
5130
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005131 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
5132 if (info == NULL)
5133 return -1;
5134
5135 pos = buf;
5136 end = buf + buflen;
5137
5138 res = os_snprintf(pos, end - pos, MACSTR "\n"
5139 "pri_dev_type=%s\n"
5140 "device_name=%s\n"
5141 "manufacturer=%s\n"
5142 "model_name=%s\n"
5143 "model_number=%s\n"
5144 "serial_number=%s\n"
5145 "config_methods=0x%x\n"
5146 "dev_capab=0x%x\n"
5147 "group_capab=0x%x\n"
5148 "level=%d\n",
5149 MAC2STR(info->p2p_device_addr),
5150 wps_dev_type_bin2str(info->pri_dev_type,
5151 devtype, sizeof(devtype)),
5152 info->device_name,
5153 info->manufacturer,
5154 info->model_name,
5155 info->model_number,
5156 info->serial_number,
5157 info->config_methods,
5158 info->dev_capab,
5159 info->group_capab,
5160 info->level);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005161 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005162 return pos - buf;
5163 pos += res;
5164
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005165 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
5166 {
5167 const u8 *t;
5168 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
5169 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
5170 wps_dev_type_bin2str(t, devtype,
5171 sizeof(devtype)));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005172 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005173 return pos - buf;
5174 pos += res;
5175 }
5176
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005177 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005178 if (ssid) {
5179 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005180 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005181 return pos - buf;
5182 pos += res;
5183 }
5184
5185 res = p2p_get_peer_info_txt(info, pos, end - pos);
5186 if (res < 0)
5187 return pos - buf;
5188 pos += res;
5189
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005190 if (info->vendor_elems) {
5191 res = os_snprintf(pos, end - pos, "vendor_elems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005192 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005193 return pos - buf;
5194 pos += res;
5195
5196 pos += wpa_snprintf_hex(pos, end - pos,
5197 wpabuf_head(info->vendor_elems),
5198 wpabuf_len(info->vendor_elems));
5199
5200 res = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005201 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005202 return pos - buf;
5203 pos += res;
5204 }
5205
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005206 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005207}
5208
5209
Dmitry Shmidt04949592012-07-19 12:16:46 -07005210static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
5211 const char *param)
5212{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005213 unsigned int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005214
5215 if (wpa_s->global->p2p == NULL)
5216 return -1;
5217
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005218 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
5219 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005220
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005221 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
5222 struct wpa_freq_range *freq;
5223 freq = &wpa_s->global->p2p_disallow_freq.range[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005224 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005225 freq->min, freq->max);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005226 }
5227
Dmitry Shmidt04949592012-07-19 12:16:46 -07005228 wpas_p2p_update_channel_list(wpa_s);
5229 return 0;
5230}
5231
5232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005233static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
5234{
5235 char *param;
5236
5237 if (wpa_s->global->p2p == NULL)
5238 return -1;
5239
5240 param = os_strchr(cmd, ' ');
5241 if (param == NULL)
5242 return -1;
5243 *param++ = '\0';
5244
5245 if (os_strcmp(cmd, "discoverability") == 0) {
5246 p2p_set_client_discoverability(wpa_s->global->p2p,
5247 atoi(param));
5248 return 0;
5249 }
5250
5251 if (os_strcmp(cmd, "managed") == 0) {
5252 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
5253 return 0;
5254 }
5255
5256 if (os_strcmp(cmd, "listen_channel") == 0) {
5257 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005258 atoi(param), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005259 }
5260
5261 if (os_strcmp(cmd, "ssid_postfix") == 0) {
5262 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
5263 os_strlen(param));
5264 }
5265
5266 if (os_strcmp(cmd, "noa") == 0) {
5267 char *pos;
5268 int count, start, duration;
5269 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
5270 count = atoi(param);
5271 pos = os_strchr(param, ',');
5272 if (pos == NULL)
5273 return -1;
5274 pos++;
5275 start = atoi(pos);
5276 pos = os_strchr(pos, ',');
5277 if (pos == NULL)
5278 return -1;
5279 pos++;
5280 duration = atoi(pos);
5281 if (count < 0 || count > 255 || start < 0 || duration < 0)
5282 return -1;
5283 if (count == 0 && duration > 0)
5284 return -1;
5285 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
5286 "start=%d duration=%d", count, start, duration);
5287 return wpas_p2p_set_noa(wpa_s, count, start, duration);
5288 }
5289
5290 if (os_strcmp(cmd, "ps") == 0)
5291 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
5292
5293 if (os_strcmp(cmd, "oppps") == 0)
5294 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
5295
5296 if (os_strcmp(cmd, "ctwindow") == 0)
5297 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
5298
5299 if (os_strcmp(cmd, "disabled") == 0) {
5300 wpa_s->global->p2p_disabled = atoi(param);
5301 wpa_printf(MSG_DEBUG, "P2P functionality %s",
5302 wpa_s->global->p2p_disabled ?
5303 "disabled" : "enabled");
5304 if (wpa_s->global->p2p_disabled) {
5305 wpas_p2p_stop_find(wpa_s);
5306 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5307 p2p_flush(wpa_s->global->p2p);
5308 }
5309 return 0;
5310 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005311
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07005312 if (os_strcmp(cmd, "conc_pref") == 0) {
5313 if (os_strcmp(param, "sta") == 0)
5314 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
5315 else if (os_strcmp(param, "p2p") == 0)
5316 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005317 else {
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07005318 wpa_printf(MSG_INFO, "Invalid conc_pref value");
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005319 return -1;
5320 }
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07005321 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
Dmitry Shmidt04949592012-07-19 12:16:46 -07005322 "%s", param);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005323 return 0;
5324 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005326 if (os_strcmp(cmd, "force_long_sd") == 0) {
5327 wpa_s->force_long_sd = atoi(param);
5328 return 0;
5329 }
5330
5331 if (os_strcmp(cmd, "peer_filter") == 0) {
5332 u8 addr[ETH_ALEN];
5333 if (hwaddr_aton(param, addr))
5334 return -1;
5335 p2p_set_peer_filter(wpa_s->global->p2p, addr);
5336 return 0;
5337 }
5338
5339 if (os_strcmp(cmd, "cross_connect") == 0)
5340 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
5341
5342 if (os_strcmp(cmd, "go_apsd") == 0) {
5343 if (os_strcmp(param, "disable") == 0)
5344 wpa_s->set_ap_uapsd = 0;
5345 else {
5346 wpa_s->set_ap_uapsd = 1;
5347 wpa_s->ap_uapsd = atoi(param);
5348 }
5349 return 0;
5350 }
5351
5352 if (os_strcmp(cmd, "client_apsd") == 0) {
5353 if (os_strcmp(param, "disable") == 0)
5354 wpa_s->set_sta_uapsd = 0;
5355 else {
5356 int be, bk, vi, vo;
5357 char *pos;
5358 /* format: BE,BK,VI,VO;max SP Length */
5359 be = atoi(param);
5360 pos = os_strchr(param, ',');
5361 if (pos == NULL)
5362 return -1;
5363 pos++;
5364 bk = atoi(pos);
5365 pos = os_strchr(pos, ',');
5366 if (pos == NULL)
5367 return -1;
5368 pos++;
5369 vi = atoi(pos);
5370 pos = os_strchr(pos, ',');
5371 if (pos == NULL)
5372 return -1;
5373 pos++;
5374 vo = atoi(pos);
5375 /* ignore max SP Length for now */
5376
5377 wpa_s->set_sta_uapsd = 1;
5378 wpa_s->sta_uapsd = 0;
5379 if (be)
5380 wpa_s->sta_uapsd |= BIT(0);
5381 if (bk)
5382 wpa_s->sta_uapsd |= BIT(1);
5383 if (vi)
5384 wpa_s->sta_uapsd |= BIT(2);
5385 if (vo)
5386 wpa_s->sta_uapsd |= BIT(3);
5387 }
5388 return 0;
5389 }
5390
Dmitry Shmidt04949592012-07-19 12:16:46 -07005391 if (os_strcmp(cmd, "disallow_freq") == 0)
5392 return p2p_ctrl_disallow_freq(wpa_s, param);
5393
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005394 if (os_strcmp(cmd, "disc_int") == 0) {
5395 int min_disc_int, max_disc_int, max_disc_tu;
5396 char *pos;
5397
5398 pos = param;
5399
5400 min_disc_int = atoi(pos);
5401 pos = os_strchr(pos, ' ');
5402 if (pos == NULL)
5403 return -1;
5404 *pos++ = '\0';
5405
5406 max_disc_int = atoi(pos);
5407 pos = os_strchr(pos, ' ');
5408 if (pos == NULL)
5409 return -1;
5410 *pos++ = '\0';
5411
5412 max_disc_tu = atoi(pos);
5413
5414 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
5415 max_disc_int, max_disc_tu);
5416 }
5417
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005418 if (os_strcmp(cmd, "per_sta_psk") == 0) {
5419 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
5420 return 0;
5421 }
5422
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005423#ifdef CONFIG_WPS_NFC
5424 if (os_strcmp(cmd, "nfc_tag") == 0)
5425 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
5426#endif /* CONFIG_WPS_NFC */
5427
5428 if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
5429 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
5430 return 0;
5431 }
5432
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005433 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
5434 cmd);
5435
5436 return -1;
5437}
5438
5439
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005440static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
5441{
5442 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5443 wpa_s->force_long_sd = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005444 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005445 if (wpa_s->global->p2p)
5446 p2p_flush(wpa_s->global->p2p);
5447}
5448
5449
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005450static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
5451{
5452 char *pos, *pos2;
5453 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
5454
5455 if (cmd[0]) {
5456 pos = os_strchr(cmd, ' ');
5457 if (pos == NULL)
5458 return -1;
5459 *pos++ = '\0';
5460 dur1 = atoi(cmd);
5461
5462 pos2 = os_strchr(pos, ' ');
5463 if (pos2)
5464 *pos2++ = '\0';
5465 int1 = atoi(pos);
5466 } else
5467 pos2 = NULL;
5468
5469 if (pos2) {
5470 pos = os_strchr(pos2, ' ');
5471 if (pos == NULL)
5472 return -1;
5473 *pos++ = '\0';
5474 dur2 = atoi(pos2);
5475 int2 = atoi(pos);
5476 }
5477
5478 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
5479}
5480
5481
5482static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
5483{
5484 char *pos;
5485 unsigned int period = 0, interval = 0;
5486
5487 if (cmd[0]) {
5488 pos = os_strchr(cmd, ' ');
5489 if (pos == NULL)
5490 return -1;
5491 *pos++ = '\0';
5492 period = atoi(cmd);
5493 interval = atoi(pos);
5494 }
5495
5496 return wpas_p2p_ext_listen(wpa_s, period, interval);
5497}
5498
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005499
5500static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
5501{
5502 const char *pos;
5503 u8 peer[ETH_ALEN];
5504 int iface_addr = 0;
5505
5506 pos = cmd;
5507 if (os_strncmp(pos, "iface=", 6) == 0) {
5508 iface_addr = 1;
5509 pos += 6;
5510 }
5511 if (hwaddr_aton(pos, peer))
5512 return -1;
5513
5514 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
5515 return 0;
5516}
5517
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005518#endif /* CONFIG_P2P */
5519
5520
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005521static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
5522{
5523 struct wpa_freq_range_list ranges;
5524 int *freqs = NULL;
5525 struct hostapd_hw_modes *mode;
5526 u16 i;
5527
5528 if (wpa_s->hw.modes == NULL)
5529 return NULL;
5530
5531 os_memset(&ranges, 0, sizeof(ranges));
5532 if (freq_range_list_parse(&ranges, val) < 0)
5533 return NULL;
5534
5535 for (i = 0; i < wpa_s->hw.num_modes; i++) {
5536 int j;
5537
5538 mode = &wpa_s->hw.modes[i];
5539 for (j = 0; j < mode->num_channels; j++) {
5540 unsigned int freq;
5541
5542 if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
5543 continue;
5544
5545 freq = mode->channels[j].freq;
5546 if (!freq_range_list_includes(&ranges, freq))
5547 continue;
5548
5549 int_array_add_unique(&freqs, freq);
5550 }
5551 }
5552
5553 os_free(ranges.range);
5554 return freqs;
5555}
5556
5557
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005558#ifdef CONFIG_INTERWORKING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005559
5560static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
5561{
5562 int auto_sel = 0;
5563 int *freqs = NULL;
5564
5565 if (param) {
5566 char *pos;
5567
5568 auto_sel = os_strstr(param, "auto") != NULL;
5569
5570 pos = os_strstr(param, "freq=");
5571 if (pos) {
5572 freqs = freq_range_to_channel_list(wpa_s, pos + 5);
5573 if (freqs == NULL)
5574 return -1;
5575 }
5576
5577 }
5578
5579 return interworking_select(wpa_s, auto_sel, freqs);
5580}
5581
5582
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005583static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
5584{
5585 u8 bssid[ETH_ALEN];
5586 struct wpa_bss *bss;
5587
5588 if (hwaddr_aton(dst, bssid)) {
5589 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
5590 return -1;
5591 }
5592
5593 bss = wpa_bss_get_bssid(wpa_s, bssid);
5594 if (bss == NULL) {
5595 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
5596 MAC2STR(bssid));
5597 return -1;
5598 }
5599
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005600 if (bss->ssid_len == 0) {
5601 int found = 0;
5602
5603 wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
5604 " does not have SSID information", MAC2STR(bssid));
5605
5606 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
5607 list) {
5608 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
5609 bss->ssid_len > 0) {
5610 found = 1;
5611 break;
5612 }
5613 }
5614
5615 if (!found)
5616 return -1;
5617 wpa_printf(MSG_DEBUG,
5618 "Found another matching BSS entry with SSID");
5619 }
5620
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005621 return interworking_connect(wpa_s, bss);
5622}
5623
5624
5625static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
5626{
5627 u8 dst_addr[ETH_ALEN];
5628 int used;
5629 char *pos;
5630#define MAX_ANQP_INFO_ID 100
5631 u16 id[MAX_ANQP_INFO_ID];
5632 size_t num_id = 0;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005633 u32 subtypes = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005634
5635 used = hwaddr_aton2(dst, dst_addr);
5636 if (used < 0)
5637 return -1;
5638 pos = dst + used;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005639 if (*pos == ' ')
5640 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005641 while (num_id < MAX_ANQP_INFO_ID) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07005642 if (os_strncmp(pos, "hs20:", 5) == 0) {
5643#ifdef CONFIG_HS20
5644 int num = atoi(pos + 5);
5645 if (num <= 0 || num > 31)
5646 return -1;
5647 subtypes |= BIT(num);
5648#else /* CONFIG_HS20 */
5649 return -1;
5650#endif /* CONFIG_HS20 */
5651 } else {
5652 id[num_id] = atoi(pos);
5653 if (id[num_id])
5654 num_id++;
5655 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005656 pos = os_strchr(pos + 1, ',');
5657 if (pos == NULL)
5658 break;
5659 pos++;
5660 }
5661
5662 if (num_id == 0)
5663 return -1;
5664
Dmitry Shmidt15907092014-03-25 10:42:57 -07005665 return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005666}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005667
5668
5669static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
5670{
5671 u8 dst_addr[ETH_ALEN];
5672 struct wpabuf *advproto, *query = NULL;
5673 int used, ret = -1;
5674 char *pos, *end;
5675 size_t len;
5676
5677 used = hwaddr_aton2(cmd, dst_addr);
5678 if (used < 0)
5679 return -1;
5680
5681 pos = cmd + used;
5682 while (*pos == ' ')
5683 pos++;
5684
5685 /* Advertisement Protocol ID */
5686 end = os_strchr(pos, ' ');
5687 if (end)
5688 len = end - pos;
5689 else
5690 len = os_strlen(pos);
5691 if (len & 0x01)
5692 return -1;
5693 len /= 2;
5694 if (len == 0)
5695 return -1;
5696 advproto = wpabuf_alloc(len);
5697 if (advproto == NULL)
5698 return -1;
5699 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
5700 goto fail;
5701
5702 if (end) {
5703 /* Optional Query Request */
5704 pos = end + 1;
5705 while (*pos == ' ')
5706 pos++;
5707
5708 len = os_strlen(pos);
5709 if (len) {
5710 if (len & 0x01)
5711 goto fail;
5712 len /= 2;
5713 if (len == 0)
5714 goto fail;
5715 query = wpabuf_alloc(len);
5716 if (query == NULL)
5717 goto fail;
5718 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
5719 goto fail;
5720 }
5721 }
5722
5723 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
5724
5725fail:
5726 wpabuf_free(advproto);
5727 wpabuf_free(query);
5728
5729 return ret;
5730}
5731
5732
5733static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
5734 size_t buflen)
5735{
5736 u8 addr[ETH_ALEN];
5737 int dialog_token;
5738 int used;
5739 char *pos;
5740 size_t resp_len, start, requested_len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005741 struct wpabuf *resp;
5742 int ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005743
5744 used = hwaddr_aton2(cmd, addr);
5745 if (used < 0)
5746 return -1;
5747
5748 pos = cmd + used;
5749 while (*pos == ' ')
5750 pos++;
5751 dialog_token = atoi(pos);
5752
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005753 if (wpa_s->last_gas_resp &&
5754 os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
5755 dialog_token == wpa_s->last_gas_dialog_token)
5756 resp = wpa_s->last_gas_resp;
5757 else if (wpa_s->prev_gas_resp &&
5758 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
5759 dialog_token == wpa_s->prev_gas_dialog_token)
5760 resp = wpa_s->prev_gas_resp;
5761 else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005762 return -1;
5763
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005764 resp_len = wpabuf_len(resp);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005765 start = 0;
5766 requested_len = resp_len;
5767
5768 pos = os_strchr(pos, ' ');
5769 if (pos) {
5770 start = atoi(pos);
5771 if (start > resp_len)
5772 return os_snprintf(buf, buflen, "FAIL-Invalid range");
5773 pos = os_strchr(pos, ',');
5774 if (pos == NULL)
5775 return -1;
5776 pos++;
5777 requested_len = atoi(pos);
5778 if (start + requested_len > resp_len)
5779 return os_snprintf(buf, buflen, "FAIL-Invalid range");
5780 }
5781
5782 if (requested_len * 2 + 1 > buflen)
5783 return os_snprintf(buf, buflen, "FAIL-Too long response");
5784
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005785 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
5786 requested_len);
5787
5788 if (start + requested_len == resp_len) {
5789 /*
5790 * Free memory by dropping the response after it has been
5791 * fetched.
5792 */
5793 if (resp == wpa_s->prev_gas_resp) {
5794 wpabuf_free(wpa_s->prev_gas_resp);
5795 wpa_s->prev_gas_resp = NULL;
5796 } else {
5797 wpabuf_free(wpa_s->last_gas_resp);
5798 wpa_s->last_gas_resp = NULL;
5799 }
5800 }
5801
5802 return ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005803}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005804#endif /* CONFIG_INTERWORKING */
5805
5806
Dmitry Shmidt04949592012-07-19 12:16:46 -07005807#ifdef CONFIG_HS20
5808
5809static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
5810{
5811 u8 dst_addr[ETH_ALEN];
5812 int used;
5813 char *pos;
5814 u32 subtypes = 0;
5815
5816 used = hwaddr_aton2(dst, dst_addr);
5817 if (used < 0)
5818 return -1;
5819 pos = dst + used;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005820 if (*pos == ' ')
5821 pos++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005822 for (;;) {
5823 int num = atoi(pos);
5824 if (num <= 0 || num > 31)
5825 return -1;
5826 subtypes |= BIT(num);
5827 pos = os_strchr(pos + 1, ',');
5828 if (pos == NULL)
5829 break;
5830 pos++;
5831 }
5832
5833 if (subtypes == 0)
5834 return -1;
5835
5836 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
5837}
5838
5839
5840static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5841 const u8 *addr, const char *realm)
5842{
5843 u8 *buf;
5844 size_t rlen, len;
5845 int ret;
5846
5847 rlen = os_strlen(realm);
5848 len = 3 + rlen;
5849 buf = os_malloc(len);
5850 if (buf == NULL)
5851 return -1;
5852 buf[0] = 1; /* NAI Home Realm Count */
5853 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
5854 buf[2] = rlen;
5855 os_memcpy(buf + 3, realm, rlen);
5856
5857 ret = hs20_anqp_send_req(wpa_s, addr,
5858 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5859 buf, len);
5860
5861 os_free(buf);
5862
5863 return ret;
5864}
5865
5866
5867static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5868 char *dst)
5869{
5870 struct wpa_cred *cred = wpa_s->conf->cred;
5871 u8 dst_addr[ETH_ALEN];
5872 int used;
5873 u8 *buf;
5874 size_t len;
5875 int ret;
5876
5877 used = hwaddr_aton2(dst, dst_addr);
5878 if (used < 0)
5879 return -1;
5880
5881 while (dst[used] == ' ')
5882 used++;
5883 if (os_strncmp(dst + used, "realm=", 6) == 0)
5884 return hs20_nai_home_realm_list(wpa_s, dst_addr,
5885 dst + used + 6);
5886
5887 len = os_strlen(dst + used);
5888
5889 if (len == 0 && cred && cred->realm)
5890 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
5891
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07005892 if (len & 1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07005893 return -1;
5894 len /= 2;
5895 buf = os_malloc(len);
5896 if (buf == NULL)
5897 return -1;
5898 if (hexstr2bin(dst + used, buf, len) < 0) {
5899 os_free(buf);
5900 return -1;
5901 }
5902
5903 ret = hs20_anqp_send_req(wpa_s, dst_addr,
5904 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5905 buf, len);
5906 os_free(buf);
5907
5908 return ret;
5909}
5910
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005911
5912static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
5913{
5914 u8 dst_addr[ETH_ALEN];
5915 int used;
5916 char *icon;
5917
5918 used = hwaddr_aton2(cmd, dst_addr);
5919 if (used < 0)
5920 return -1;
5921
5922 while (cmd[used] == ' ')
5923 used++;
5924 icon = &cmd[used];
5925
5926 wpa_s->fetch_osu_icon_in_progress = 0;
5927 return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
5928 (u8 *) icon, os_strlen(icon));
5929}
5930
Dmitry Shmidt04949592012-07-19 12:16:46 -07005931#endif /* CONFIG_HS20 */
5932
5933
Dmitry Shmidt04949592012-07-19 12:16:46 -07005934#ifdef CONFIG_AUTOSCAN
5935
5936static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
5937 char *cmd)
5938{
5939 enum wpa_states state = wpa_s->wpa_state;
5940 char *new_params = NULL;
5941
5942 if (os_strlen(cmd) > 0) {
5943 new_params = os_strdup(cmd);
5944 if (new_params == NULL)
5945 return -1;
5946 }
5947
5948 os_free(wpa_s->conf->autoscan);
5949 wpa_s->conf->autoscan = new_params;
5950
5951 if (wpa_s->conf->autoscan == NULL)
5952 autoscan_deinit(wpa_s);
5953 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
5954 autoscan_init(wpa_s, 1);
5955 else if (state == WPA_SCANNING)
5956 wpa_supplicant_reinit_autoscan(wpa_s);
5957
5958 return 0;
5959}
5960
5961#endif /* CONFIG_AUTOSCAN */
5962
5963
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005964#ifdef CONFIG_WNM
5965
5966static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
5967{
5968 int enter;
5969 int intval = 0;
5970 char *pos;
5971 int ret;
5972 struct wpabuf *tfs_req = NULL;
5973
5974 if (os_strncmp(cmd, "enter", 5) == 0)
5975 enter = 1;
5976 else if (os_strncmp(cmd, "exit", 4) == 0)
5977 enter = 0;
5978 else
5979 return -1;
5980
5981 pos = os_strstr(cmd, " interval=");
5982 if (pos)
5983 intval = atoi(pos + 10);
5984
5985 pos = os_strstr(cmd, " tfs_req=");
5986 if (pos) {
5987 char *end;
5988 size_t len;
5989 pos += 9;
5990 end = os_strchr(pos, ' ');
5991 if (end)
5992 len = end - pos;
5993 else
5994 len = os_strlen(pos);
5995 if (len & 1)
5996 return -1;
5997 len /= 2;
5998 tfs_req = wpabuf_alloc(len);
5999 if (tfs_req == NULL)
6000 return -1;
6001 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
6002 wpabuf_free(tfs_req);
6003 return -1;
6004 }
6005 }
6006
6007 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
6008 WNM_SLEEP_MODE_EXIT, intval,
6009 tfs_req);
6010 wpabuf_free(tfs_req);
6011
6012 return ret;
6013}
6014
Dmitry Shmidt44c95782013-05-17 09:51:35 -07006015
6016static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
6017{
6018 int query_reason;
6019
6020 query_reason = atoi(cmd);
6021
6022 wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
6023 query_reason);
6024
6025 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
6026}
6027
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006028#endif /* CONFIG_WNM */
6029
6030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006031static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
6032 size_t buflen)
6033{
6034 struct wpa_signal_info si;
6035 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006036 char *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037
6038 ret = wpa_drv_signal_poll(wpa_s, &si);
6039 if (ret)
6040 return -1;
6041
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006042 pos = buf;
6043 end = buf + buflen;
6044
6045 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006046 "NOISE=%d\nFREQUENCY=%u\n",
6047 si.current_signal, si.current_txrate / 1000,
6048 si.current_noise, si.frequency);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006049 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006050 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006051 pos += ret;
6052
6053 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
6054 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006055 channel_width_to_string(si.chanwidth));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006056 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006057 return -1;
6058 pos += ret;
6059 }
6060
6061 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
6062 ret = os_snprintf(pos, end - pos,
6063 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
6064 si.center_frq1, si.center_frq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006065 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006066 return -1;
6067 pos += ret;
6068 }
6069
6070 if (si.avg_signal) {
6071 ret = os_snprintf(pos, end - pos,
6072 "AVG_RSSI=%d\n", si.avg_signal);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006073 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006074 return -1;
6075 pos += ret;
6076 }
6077
6078 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006079}
6080
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006081
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07006082static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
6083 size_t buflen)
6084{
6085 struct hostap_sta_driver_data sta;
6086 int ret;
6087
6088 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
6089 if (ret)
6090 return -1;
6091
6092 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006093 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006094 if (os_snprintf_error(buflen, ret))
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07006095 return -1;
6096 return ret;
6097}
6098
6099
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006100#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07006101static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
6102 char *buf, size_t buflen)
6103{
6104 int ret;
6105
6106 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
Dmitry Shmidt9432e122013-09-12 12:39:30 -07006107 if (ret == 0) {
6108 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
6109 struct p2p_data *p2p = wpa_s->global->p2p;
6110 if (p2p) {
6111 char country[3];
6112 country[0] = cmd[8];
6113 country[1] = cmd[9];
6114 country[2] = 0x04;
6115 p2p_set_country(p2p, country);
6116 }
6117 }
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006118 ret = os_snprintf(buf, buflen, "%s\n", "OK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006119 if (os_snprintf_error(buflen, ret))
6120 ret = -1;
Dmitry Shmidt9432e122013-09-12 12:39:30 -07006121 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07006122 return ret;
6123}
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006124#endif /* ANDROID */
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07006125
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07006126
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006127static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
6128 char *buf, size_t buflen)
6129{
6130 int ret;
6131 char *pos;
6132 u8 *data = NULL;
6133 unsigned int vendor_id, subcmd;
6134 struct wpabuf *reply;
6135 size_t data_len = 0;
6136
6137 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
6138 vendor_id = strtoul(cmd, &pos, 16);
6139 if (!isblank(*pos))
6140 return -EINVAL;
6141
6142 subcmd = strtoul(pos, &pos, 10);
6143
6144 if (*pos != '\0') {
6145 if (!isblank(*pos++))
6146 return -EINVAL;
6147 data_len = os_strlen(pos);
6148 }
6149
6150 if (data_len) {
6151 data_len /= 2;
6152 data = os_malloc(data_len);
6153 if (!data)
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006154 return -1;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006155
6156 if (hexstr2bin(pos, data, data_len)) {
6157 wpa_printf(MSG_DEBUG,
6158 "Vendor command: wrong parameter format");
6159 os_free(data);
6160 return -EINVAL;
6161 }
6162 }
6163
6164 reply = wpabuf_alloc((buflen - 1) / 2);
6165 if (!reply) {
6166 os_free(data);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006167 return -1;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006168 }
6169
6170 ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
6171 reply);
6172
6173 if (ret == 0)
6174 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
6175 wpabuf_len(reply));
6176
6177 wpabuf_free(reply);
6178 os_free(data);
6179
6180 return ret;
6181}
6182
6183
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006184static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
6185{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006186#ifdef CONFIG_P2P
6187 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
6188 wpa_s->global->p2p_init_wpa_s : wpa_s;
6189#endif /* CONFIG_P2P */
6190
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006191 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
6192
6193#ifdef CONFIG_P2P
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006194 wpas_p2p_cancel(p2p_wpa_s);
6195 p2p_ctrl_flush(p2p_wpa_s);
6196 wpas_p2p_group_remove(p2p_wpa_s, "*");
6197 wpas_p2p_service_flush(p2p_wpa_s);
6198 p2p_wpa_s->global->p2p_disabled = 0;
6199 p2p_wpa_s->global->p2p_per_sta_psk = 0;
6200 p2p_wpa_s->conf->num_sec_device_types = 0;
6201 p2p_wpa_s->p2p_disable_ip_addr_req = 0;
6202 os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
6203 p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006204#endif /* CONFIG_P2P */
6205
6206#ifdef CONFIG_WPS_TESTING
6207 wps_version_number = 0x20;
6208 wps_testing_dummy_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006209 wps_corrupt_pkhash = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006210#endif /* CONFIG_WPS_TESTING */
6211#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006212 wpa_s->wps_fragment_size = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006213 wpas_wps_cancel(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006214 wps_registrar_flush(wpa_s->wps->registrar);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006215#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07006216 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006217 wpa_s->known_wps_freq = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006218
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006219#ifdef CONFIG_TDLS
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006220#ifdef CONFIG_TDLS_TESTING
6221 extern unsigned int tdls_testing;
6222 tdls_testing = 0;
6223#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006224 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
6225 wpa_tdls_enable(wpa_s->wpa, 1);
6226#endif /* CONFIG_TDLS */
6227
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006228 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
6229 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
6230
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006231 wpa_s->no_keep_alive = 0;
6232
6233 os_free(wpa_s->disallow_aps_bssid);
6234 wpa_s->disallow_aps_bssid = NULL;
6235 wpa_s->disallow_aps_bssid_count = 0;
6236 os_free(wpa_s->disallow_aps_ssid);
6237 wpa_s->disallow_aps_ssid = NULL;
6238 wpa_s->disallow_aps_ssid_count = 0;
6239
6240 wpa_s->set_sta_uapsd = 0;
6241 wpa_s->sta_uapsd = 0;
6242
6243 wpa_drv_radio_disable(wpa_s, 0);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006244 wpa_blacklist_clear(wpa_s);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006245 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006246 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
6247 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08006248 wpa_config_flush_blobs(wpa_s->conf);
Dmitry Shmidt18463232014-01-24 12:29:41 -08006249 wpa_s->conf->auto_interworking = 0;
6250 wpa_s->conf->okc = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006251
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006252 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
6253 rsn_preauth_deinit(wpa_s->wpa);
6254
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006255 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
6256 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
6257 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
6258 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
6259
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006260 radio_remove_works(wpa_s, NULL, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006261 wpa_s->ext_work_in_progress = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006262
6263 wpa_s->next_ssid = NULL;
6264
6265#ifdef CONFIG_INTERWORKING
6266 hs20_cancel_fetch_osu(wpa_s);
6267#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt818ea482014-03-10 13:15:21 -07006268
6269 wpa_s->ext_mgmt_frame_handling = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006270 wpa_s->ext_eapol_frame_io = 0;
6271#ifdef CONFIG_TESTING_OPTIONS
6272 wpa_s->extra_roc_dur = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006273 wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006274#endif /* CONFIG_TESTING_OPTIONS */
6275
6276 wpa_s->disconnected = 0;
6277 os_free(wpa_s->next_scan_freqs);
6278 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006279
6280 wpa_bss_flush(wpa_s);
6281 if (!dl_list_empty(&wpa_s->bss)) {
6282 wpa_printf(MSG_DEBUG,
6283 "BSS table not empty after flush: %u entries, current_bss=%p bssid="
6284 MACSTR " pending_bssid=" MACSTR,
6285 dl_list_len(&wpa_s->bss), wpa_s->current_bss,
6286 MAC2STR(wpa_s->bssid),
6287 MAC2STR(wpa_s->pending_bssid));
6288 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006289}
6290
6291
6292static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
6293 char *buf, size_t buflen)
6294{
6295 struct wpa_radio_work *work;
6296 char *pos, *end;
6297 struct os_reltime now, diff;
6298
6299 pos = buf;
6300 end = buf + buflen;
6301
6302 os_get_reltime(&now);
6303
6304 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
6305 {
6306 int ret;
6307
6308 os_reltime_sub(&now, &work->time, &diff);
6309 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
6310 work->type, work->wpa_s->ifname, work->freq,
6311 work->started, diff.sec, diff.usec);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006312 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006313 break;
6314 pos += ret;
6315 }
6316
6317 return pos - buf;
6318}
6319
6320
6321static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
6322{
6323 struct wpa_radio_work *work = eloop_ctx;
6324 struct wpa_external_work *ework = work->ctx;
6325
6326 wpa_dbg(work->wpa_s, MSG_DEBUG,
6327 "Timing out external radio work %u (%s)",
6328 ework->id, work->type);
6329 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006330 work->wpa_s->ext_work_in_progress = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006331 radio_work_done(work);
Dmitry Shmidt71757432014-06-02 13:50:35 -07006332 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006333}
6334
6335
6336static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
6337{
6338 struct wpa_external_work *ework = work->ctx;
6339
6340 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006341 if (work->started)
6342 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
6343 work, NULL);
6344
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006345 os_free(ework);
6346 return;
6347 }
6348
6349 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
6350 ework->id, ework->type);
6351 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006352 work->wpa_s->ext_work_in_progress = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006353 if (!ework->timeout)
6354 ework->timeout = 10;
6355 eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
6356 work, NULL);
6357}
6358
6359
6360static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
6361 char *buf, size_t buflen)
6362{
6363 struct wpa_external_work *ework;
6364 char *pos, *pos2;
6365 size_t type_len;
6366 int ret;
6367 unsigned int freq = 0;
6368
6369 /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
6370
6371 ework = os_zalloc(sizeof(*ework));
6372 if (ework == NULL)
6373 return -1;
6374
6375 pos = os_strchr(cmd, ' ');
6376 if (pos) {
6377 type_len = pos - cmd;
6378 pos++;
6379
6380 pos2 = os_strstr(pos, "freq=");
6381 if (pos2)
6382 freq = atoi(pos2 + 5);
6383
6384 pos2 = os_strstr(pos, "timeout=");
6385 if (pos2)
6386 ework->timeout = atoi(pos2 + 8);
6387 } else {
6388 type_len = os_strlen(cmd);
6389 }
6390 if (4 + type_len >= sizeof(ework->type))
6391 type_len = sizeof(ework->type) - 4 - 1;
6392 os_strlcpy(ework->type, "ext:", sizeof(ework->type));
6393 os_memcpy(ework->type + 4, cmd, type_len);
6394 ework->type[4 + type_len] = '\0';
6395
6396 wpa_s->ext_work_id++;
6397 if (wpa_s->ext_work_id == 0)
6398 wpa_s->ext_work_id++;
6399 ework->id = wpa_s->ext_work_id;
6400
6401 if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
6402 ework) < 0) {
6403 os_free(ework);
6404 return -1;
6405 }
6406
6407 ret = os_snprintf(buf, buflen, "%u", ework->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006408 if (os_snprintf_error(buflen, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006409 return -1;
6410 return ret;
6411}
6412
6413
6414static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
6415{
6416 struct wpa_radio_work *work;
6417 unsigned int id = atoi(cmd);
6418
6419 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
6420 {
6421 struct wpa_external_work *ework;
6422
6423 if (os_strncmp(work->type, "ext:", 4) != 0)
6424 continue;
6425 ework = work->ctx;
6426 if (id && ework->id != id)
6427 continue;
6428 wpa_dbg(wpa_s, MSG_DEBUG,
6429 "Completed external radio work %u (%s)",
6430 ework->id, ework->type);
6431 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006432 wpa_s->ext_work_in_progress = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006433 radio_work_done(work);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07006434 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006435 return 3; /* "OK\n" */
6436 }
6437
6438 return -1;
6439}
6440
6441
6442static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
6443 char *buf, size_t buflen)
6444{
6445 if (os_strcmp(cmd, "show") == 0)
6446 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
6447 if (os_strncmp(cmd, "add ", 4) == 0)
6448 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
6449 if (os_strncmp(cmd, "done ", 5) == 0)
6450 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
6451 return -1;
6452}
6453
6454
6455void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
6456{
6457 struct wpa_radio_work *work, *tmp;
6458
Dmitry Shmidt18463232014-01-24 12:29:41 -08006459 if (!wpa_s || !wpa_s->radio)
6460 return;
6461
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006462 dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
6463 struct wpa_radio_work, list) {
6464 struct wpa_external_work *ework;
6465
6466 if (os_strncmp(work->type, "ext:", 4) != 0)
6467 continue;
6468 ework = work->ctx;
6469 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07006470 "Flushing%s external radio work %u (%s)",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006471 work->started ? " started" : "", ework->id,
6472 ework->type);
6473 if (work->started)
6474 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
6475 work, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006476 radio_work_done(work);
Dmitry Shmidt71757432014-06-02 13:50:35 -07006477 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006478 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006479}
6480
6481
Dmitry Shmidt051af732013-10-22 13:52:46 -07006482static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
6483{
6484 struct wpa_supplicant *wpa_s = eloop_ctx;
6485 eapol_sm_notify_ctrl_response(wpa_s->eapol);
6486}
6487
6488
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006489static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
6490 unsigned int *scan_id_count, int scan_id[])
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006491{
6492 const char *pos = value;
6493
6494 while (pos) {
6495 if (*pos == ' ' || *pos == '\0')
6496 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006497 if (*scan_id_count == MAX_SCAN_ID)
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006498 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006499 scan_id[(*scan_id_count)++] = atoi(pos);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006500 pos = os_strchr(pos, ',');
6501 if (pos)
6502 pos++;
6503 }
6504
6505 return 0;
6506}
6507
6508
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006509static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
6510 char *reply, int reply_size, int *reply_len)
6511{
6512 char *pos;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006513 unsigned int manual_scan_passive = 0;
6514 unsigned int manual_scan_use_id = 0;
6515 unsigned int manual_scan_only_new = 0;
6516 unsigned int scan_only = 0;
6517 unsigned int scan_id_count = 0;
6518 int scan_id[MAX_SCAN_ID];
6519 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
6520 struct wpa_scan_results *scan_res);
6521 int *manual_scan_freqs = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006522
6523 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6524 *reply_len = -1;
6525 return;
6526 }
6527
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006528 if (radio_work_pending(wpa_s, "scan")) {
6529 wpa_printf(MSG_DEBUG,
6530 "Pending scan scheduled - reject new request");
6531 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
6532 return;
6533 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006534
6535 if (params) {
6536 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006537 scan_only = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006538
6539 pos = os_strstr(params, "freq=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006540 if (pos) {
6541 manual_scan_freqs = freq_range_to_channel_list(wpa_s,
6542 pos + 5);
6543 if (manual_scan_freqs == NULL) {
6544 *reply_len = -1;
6545 goto done;
6546 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006547 }
6548
6549 pos = os_strstr(params, "passive=");
6550 if (pos)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006551 manual_scan_passive = !!atoi(pos + 8);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006552
6553 pos = os_strstr(params, "use_id=");
6554 if (pos)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006555 manual_scan_use_id = atoi(pos + 7);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006556
6557 pos = os_strstr(params, "only_new=1");
6558 if (pos)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006559 manual_scan_only_new = 1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006560
6561 pos = os_strstr(params, "scan_id=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006562 if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
6563 scan_id) < 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006564 *reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006565 goto done;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006566 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006567 }
6568
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006569 if (scan_only)
6570 scan_res_handler = scan_only_handler;
6571 else if (wpa_s->scan_res_handler == scan_only_handler)
6572 scan_res_handler = NULL;
6573 else
6574 scan_res_handler = wpa_s->scan_res_handler;
6575
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006576 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
6577 ((wpa_s->wpa_state <= WPA_SCANNING) ||
6578 (wpa_s->wpa_state == WPA_COMPLETED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006579 wpa_s->manual_scan_passive = manual_scan_passive;
6580 wpa_s->manual_scan_use_id = manual_scan_use_id;
6581 wpa_s->manual_scan_only_new = manual_scan_only_new;
6582 wpa_s->scan_id_count = scan_id_count;
6583 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
6584 wpa_s->scan_res_handler = scan_res_handler;
6585 os_free(wpa_s->manual_scan_freqs);
6586 wpa_s->manual_scan_freqs = manual_scan_freqs;
6587 manual_scan_freqs = NULL;
6588
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006589 wpa_s->normal_scans = 0;
6590 wpa_s->scan_req = MANUAL_SCAN_REQ;
6591 wpa_s->after_wps = 0;
6592 wpa_s->known_wps_freq = 0;
6593 wpa_supplicant_req_scan(wpa_s, 0, 0);
6594 if (wpa_s->manual_scan_use_id) {
6595 wpa_s->manual_scan_id++;
6596 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
6597 wpa_s->manual_scan_id);
6598 *reply_len = os_snprintf(reply, reply_size, "%u\n",
6599 wpa_s->manual_scan_id);
6600 }
6601 } else if (wpa_s->sched_scanning) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006602 wpa_s->manual_scan_passive = manual_scan_passive;
6603 wpa_s->manual_scan_use_id = manual_scan_use_id;
6604 wpa_s->manual_scan_only_new = manual_scan_only_new;
6605 wpa_s->scan_id_count = scan_id_count;
6606 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
6607 wpa_s->scan_res_handler = scan_res_handler;
6608 os_free(wpa_s->manual_scan_freqs);
6609 wpa_s->manual_scan_freqs = manual_scan_freqs;
6610 manual_scan_freqs = NULL;
6611
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006612 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
6613 wpa_supplicant_cancel_sched_scan(wpa_s);
6614 wpa_s->scan_req = MANUAL_SCAN_REQ;
6615 wpa_supplicant_req_scan(wpa_s, 0, 0);
6616 if (wpa_s->manual_scan_use_id) {
6617 wpa_s->manual_scan_id++;
6618 *reply_len = os_snprintf(reply, reply_size, "%u\n",
6619 wpa_s->manual_scan_id);
6620 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
6621 wpa_s->manual_scan_id);
6622 }
6623 } else {
6624 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
6625 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
6626 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006627
6628done:
6629 os_free(manual_scan_freqs);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006630}
6631
6632
Dmitry Shmidt818ea482014-03-10 13:15:21 -07006633#ifdef CONFIG_TESTING_OPTIONS
6634
6635static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
6636 unsigned int freq, const u8 *dst,
6637 const u8 *src, const u8 *bssid,
6638 const u8 *data, size_t data_len,
6639 enum offchannel_send_action_result
6640 result)
6641{
6642 wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
6643 " src=" MACSTR " bssid=" MACSTR " result=%s",
6644 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
6645 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
6646 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
6647 "NO_ACK" : "FAILED"));
6648}
6649
6650
6651static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
6652{
6653 char *pos, *param;
6654 size_t len;
6655 u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
6656 int res, used;
6657 int freq = 0, no_cck = 0, wait_time = 0;
6658
6659 /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
6660 * <action=Action frame payload> */
6661
6662 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
6663
6664 pos = cmd;
6665 used = hwaddr_aton2(pos, da);
6666 if (used < 0)
6667 return -1;
6668 pos += used;
6669 while (*pos == ' ')
6670 pos++;
6671 used = hwaddr_aton2(pos, bssid);
6672 if (used < 0)
6673 return -1;
6674 pos += used;
6675
6676 param = os_strstr(pos, " freq=");
6677 if (param) {
6678 param += 6;
6679 freq = atoi(param);
6680 }
6681
6682 param = os_strstr(pos, " no_cck=");
6683 if (param) {
6684 param += 8;
6685 no_cck = atoi(param);
6686 }
6687
6688 param = os_strstr(pos, " wait_time=");
6689 if (param) {
6690 param += 11;
6691 wait_time = atoi(param);
6692 }
6693
6694 param = os_strstr(pos, " action=");
6695 if (param == NULL)
6696 return -1;
6697 param += 8;
6698
6699 len = os_strlen(param);
6700 if (len & 1)
6701 return -1;
6702 len /= 2;
6703
6704 buf = os_malloc(len);
6705 if (buf == NULL)
6706 return -1;
6707
6708 if (hexstr2bin(param, buf, len) < 0) {
6709 os_free(buf);
6710 return -1;
6711 }
6712
6713 res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
6714 buf, len, wait_time,
6715 wpas_ctrl_iface_mgmt_tx_cb, no_cck);
6716 os_free(buf);
6717 return res;
6718}
6719
6720
6721static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
6722{
6723 wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
6724 offchannel_send_action_done(wpa_s);
6725}
6726
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07006727
6728static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
6729{
6730 char *pos, *param;
6731 union wpa_event_data event;
6732 enum wpa_event_type ev;
6733
6734 /* <event name> [parameters..] */
6735
6736 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
6737
6738 pos = cmd;
6739 param = os_strchr(pos, ' ');
6740 if (param)
6741 *param++ = '\0';
6742
6743 os_memset(&event, 0, sizeof(event));
6744
6745 if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
6746 ev = EVENT_INTERFACE_ENABLED;
6747 } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
6748 ev = EVENT_INTERFACE_DISABLED;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006749 } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
6750 ev = EVENT_AVOID_FREQUENCIES;
6751 if (param == NULL)
6752 param = "";
6753 if (freq_range_list_parse(&event.freq_range, param) < 0)
6754 return -1;
6755 wpa_supplicant_event(wpa_s, ev, &event);
6756 os_free(event.freq_range.range);
6757 return 0;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07006758 } else {
6759 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
6760 cmd);
6761 return -1;
6762 }
6763
6764 wpa_supplicant_event(wpa_s, ev, &event);
6765
6766 return 0;
6767}
6768
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006769
6770static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
6771{
6772 char *pos;
6773 u8 src[ETH_ALEN], *buf;
6774 int used;
6775 size_t len;
6776
6777 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
6778
6779 pos = cmd;
6780 used = hwaddr_aton2(pos, src);
6781 if (used < 0)
6782 return -1;
6783 pos += used;
6784 while (*pos == ' ')
6785 pos++;
6786
6787 len = os_strlen(pos);
6788 if (len & 1)
6789 return -1;
6790 len /= 2;
6791
6792 buf = os_malloc(len);
6793 if (buf == NULL)
6794 return -1;
6795
6796 if (hexstr2bin(pos, buf, len) < 0) {
6797 os_free(buf);
6798 return -1;
6799 }
6800
6801 wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
6802 os_free(buf);
6803
6804 return 0;
6805}
6806
6807
6808static u16 ipv4_hdr_checksum(const void *buf, size_t len)
6809{
6810 size_t i;
6811 u32 sum = 0;
6812 const u16 *pos = buf;
6813
6814 for (i = 0; i < len / 2; i++)
6815 sum += *pos++;
6816
6817 while (sum >> 16)
6818 sum = (sum & 0xffff) + (sum >> 16);
6819
6820 return sum ^ 0xffff;
6821}
6822
6823
6824#define HWSIM_PACKETLEN 1500
6825#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
6826
6827void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
6828{
6829 struct wpa_supplicant *wpa_s = ctx;
6830 const struct ether_header *eth;
6831 const struct iphdr *ip;
6832 const u8 *pos;
6833 unsigned int i;
6834
6835 if (len != HWSIM_PACKETLEN)
6836 return;
6837
6838 eth = (const struct ether_header *) buf;
6839 ip = (const struct iphdr *) (eth + 1);
6840 pos = (const u8 *) (ip + 1);
6841
6842 if (ip->ihl != 5 || ip->version != 4 ||
6843 ntohs(ip->tot_len) != HWSIM_IP_LEN)
6844 return;
6845
6846 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++) {
6847 if (*pos != (u8) i)
6848 return;
6849 pos++;
6850 }
6851
6852 wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
6853 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
6854}
6855
6856
6857static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
6858 char *cmd)
6859{
6860 int enabled = atoi(cmd);
6861
6862 if (!enabled) {
6863 if (wpa_s->l2_test) {
6864 l2_packet_deinit(wpa_s->l2_test);
6865 wpa_s->l2_test = NULL;
6866 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
6867 }
6868 return 0;
6869 }
6870
6871 if (wpa_s->l2_test)
6872 return 0;
6873
6874 wpa_s->l2_test = l2_packet_init(wpa_s->ifname, wpa_s->own_addr,
6875 ETHERTYPE_IP, wpas_data_test_rx,
6876 wpa_s, 1);
6877 if (wpa_s->l2_test == NULL)
6878 return -1;
6879
6880 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
6881
6882 return 0;
6883}
6884
6885
6886static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
6887{
6888 u8 dst[ETH_ALEN], src[ETH_ALEN];
6889 char *pos;
6890 int used;
6891 long int val;
6892 u8 tos;
6893 u8 buf[HWSIM_PACKETLEN];
6894 struct ether_header *eth;
6895 struct iphdr *ip;
6896 u8 *dpos;
6897 unsigned int i;
6898
6899 if (wpa_s->l2_test == NULL)
6900 return -1;
6901
6902 /* format: <dst> <src> <tos> */
6903
6904 pos = cmd;
6905 used = hwaddr_aton2(pos, dst);
6906 if (used < 0)
6907 return -1;
6908 pos += used;
6909 while (*pos == ' ')
6910 pos++;
6911 used = hwaddr_aton2(pos, src);
6912 if (used < 0)
6913 return -1;
6914 pos += used;
6915
6916 val = strtol(pos, NULL, 0);
6917 if (val < 0 || val > 0xff)
6918 return -1;
6919 tos = val;
6920
6921 eth = (struct ether_header *) buf;
6922 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
6923 os_memcpy(eth->ether_shost, src, ETH_ALEN);
6924 eth->ether_type = htons(ETHERTYPE_IP);
6925 ip = (struct iphdr *) (eth + 1);
6926 os_memset(ip, 0, sizeof(*ip));
6927 ip->ihl = 5;
6928 ip->version = 4;
6929 ip->ttl = 64;
6930 ip->tos = tos;
6931 ip->tot_len = htons(HWSIM_IP_LEN);
6932 ip->protocol = 1;
6933 ip->saddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 1);
6934 ip->daddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 2);
6935 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
6936 dpos = (u8 *) (ip + 1);
6937 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
6938 *dpos++ = i;
6939
6940 if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, buf,
6941 HWSIM_PACKETLEN) < 0)
6942 return -1;
6943
6944 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
6945 " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
6946
6947 return 0;
6948}
6949
6950
6951static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
6952 char *cmd)
6953{
6954 u8 *buf;
6955 struct ether_header *eth;
6956 struct l2_packet_data *l2 = NULL;
6957 size_t len;
6958 u16 ethertype;
6959 int res = -1;
6960
6961 len = os_strlen(cmd);
6962 if (len & 1 || len < ETH_HLEN * 2)
6963 return -1;
6964 len /= 2;
6965
6966 buf = os_malloc(len);
6967 if (buf == NULL)
6968 return -1;
6969
6970 if (hexstr2bin(cmd, buf, len) < 0)
6971 goto done;
6972
6973 eth = (struct ether_header *) buf;
6974 ethertype = ntohs(eth->ether_type);
6975
6976 l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
6977 wpas_data_test_rx, wpa_s, 1);
6978 if (l2 == NULL)
6979 goto done;
6980
6981 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
6982 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
6983done:
6984 if (l2)
6985 l2_packet_deinit(l2);
6986 os_free(buf);
6987
6988 return res < 0 ? -1 : 0;
6989}
6990
Dmitry Shmidtff787d52015-01-12 13:01:47 -08006991
6992static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
6993{
6994#ifdef WPA_TRACE_BFD
6995 extern char wpa_trace_fail_func[256];
6996 extern unsigned int wpa_trace_fail_after;
6997 char *pos;
6998
6999 wpa_trace_fail_after = atoi(cmd);
7000 pos = os_strchr(cmd, ':');
7001 if (pos) {
7002 pos++;
7003 os_strlcpy(wpa_trace_fail_func, pos,
7004 sizeof(wpa_trace_fail_func));
7005 } else {
7006 wpa_trace_fail_after = 0;
7007 }
7008 return 0;
7009#else /* WPA_TRACE_BFD */
7010 return -1;
7011#endif /* WPA_TRACE_BFD */
7012}
7013
7014
7015static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
7016 char *buf, size_t buflen)
7017{
7018#ifdef WPA_TRACE_BFD
7019 extern char wpa_trace_fail_func[256];
7020 extern unsigned int wpa_trace_fail_after;
7021
7022 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
7023 wpa_trace_fail_func);
7024#else /* WPA_TRACE_BFD */
7025 return -1;
7026#endif /* WPA_TRACE_BFD */
7027}
7028
Dmitry Shmidt818ea482014-03-10 13:15:21 -07007029#endif /* CONFIG_TESTING_OPTIONS */
7030
7031
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07007032static void wpas_ctrl_vendor_elem_update(struct wpa_supplicant *wpa_s)
7033{
7034 unsigned int i;
7035 char buf[30];
7036
7037 wpa_printf(MSG_DEBUG, "Update vendor elements");
7038
7039 for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
7040 if (wpa_s->vendor_elem[i]) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007041 int res;
7042
7043 res = os_snprintf(buf, sizeof(buf), "frame[%u]", i);
7044 if (!os_snprintf_error(sizeof(buf), res)) {
7045 wpa_hexdump_buf(MSG_DEBUG, buf,
7046 wpa_s->vendor_elem[i]);
7047 }
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07007048 }
7049 }
7050
7051#ifdef CONFIG_P2P
7052 if (wpa_s->parent == wpa_s &&
7053 wpa_s->global->p2p &&
7054 !wpa_s->global->p2p_disabled)
7055 p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
7056#endif /* CONFIG_P2P */
7057}
7058
7059
7060static struct wpa_supplicant *
7061wpas_ctrl_vendor_elem_iface(struct wpa_supplicant *wpa_s,
7062 enum wpa_vendor_elem_frame frame)
7063{
7064 switch (frame) {
7065#ifdef CONFIG_P2P
7066 case VENDOR_ELEM_PROBE_REQ_P2P:
7067 case VENDOR_ELEM_PROBE_RESP_P2P:
7068 case VENDOR_ELEM_PROBE_RESP_P2P_GO:
7069 case VENDOR_ELEM_BEACON_P2P_GO:
7070 case VENDOR_ELEM_P2P_PD_REQ:
7071 case VENDOR_ELEM_P2P_PD_RESP:
7072 case VENDOR_ELEM_P2P_GO_NEG_REQ:
7073 case VENDOR_ELEM_P2P_GO_NEG_RESP:
7074 case VENDOR_ELEM_P2P_GO_NEG_CONF:
7075 case VENDOR_ELEM_P2P_INV_REQ:
7076 case VENDOR_ELEM_P2P_INV_RESP:
7077 case VENDOR_ELEM_P2P_ASSOC_REQ:
7078 return wpa_s->parent;
7079#endif /* CONFIG_P2P */
7080 default:
7081 return wpa_s;
7082 }
7083}
7084
7085
7086static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
7087{
7088 char *pos = cmd;
7089 int frame;
7090 size_t len;
7091 struct wpabuf *buf;
7092 struct ieee802_11_elems elems;
7093
7094 frame = atoi(pos);
7095 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
7096 return -1;
7097 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
7098
7099 pos = os_strchr(pos, ' ');
7100 if (pos == NULL)
7101 return -1;
7102 pos++;
7103
7104 len = os_strlen(pos);
7105 if (len == 0)
7106 return 0;
7107 if (len & 1)
7108 return -1;
7109 len /= 2;
7110
7111 buf = wpabuf_alloc(len);
7112 if (buf == NULL)
7113 return -1;
7114
7115 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
7116 wpabuf_free(buf);
7117 return -1;
7118 }
7119
7120 if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
7121 ParseFailed) {
7122 wpabuf_free(buf);
7123 return -1;
7124 }
7125
7126 if (wpa_s->vendor_elem[frame] == NULL) {
7127 wpa_s->vendor_elem[frame] = buf;
7128 wpas_ctrl_vendor_elem_update(wpa_s);
7129 return 0;
7130 }
7131
7132 if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
7133 wpabuf_free(buf);
7134 return -1;
7135 }
7136
7137 wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
7138 wpabuf_free(buf);
7139 wpas_ctrl_vendor_elem_update(wpa_s);
7140
7141 return 0;
7142}
7143
7144
7145static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
7146 char *buf, size_t buflen)
7147{
7148 int frame = atoi(cmd);
7149
7150 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
7151 return -1;
7152 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
7153
7154 if (wpa_s->vendor_elem[frame] == NULL)
7155 return 0;
7156
7157 return wpa_snprintf_hex(buf, buflen,
7158 wpabuf_head_u8(wpa_s->vendor_elem[frame]),
7159 wpabuf_len(wpa_s->vendor_elem[frame]));
7160}
7161
7162
7163static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
7164{
7165 char *pos = cmd;
7166 int frame;
7167 size_t len;
7168 u8 *buf;
7169 struct ieee802_11_elems elems;
7170 u8 *ie, *end;
7171
7172 frame = atoi(pos);
7173 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
7174 return -1;
7175 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
7176
7177 pos = os_strchr(pos, ' ');
7178 if (pos == NULL)
7179 return -1;
7180 pos++;
7181
7182 if (*pos == '*') {
7183 wpabuf_free(wpa_s->vendor_elem[frame]);
7184 wpa_s->vendor_elem[frame] = NULL;
7185 wpas_ctrl_vendor_elem_update(wpa_s);
7186 return 0;
7187 }
7188
7189 if (wpa_s->vendor_elem[frame] == NULL)
7190 return -1;
7191
7192 len = os_strlen(pos);
7193 if (len == 0)
7194 return 0;
7195 if (len & 1)
7196 return -1;
7197 len /= 2;
7198
7199 buf = os_malloc(len);
7200 if (buf == NULL)
7201 return -1;
7202
7203 if (hexstr2bin(pos, buf, len) < 0) {
7204 os_free(buf);
7205 return -1;
7206 }
7207
7208 if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
7209 os_free(buf);
7210 return -1;
7211 }
7212
7213 ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
7214 end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
7215
7216 for (; ie + 1 < end; ie += 2 + ie[1]) {
7217 if (ie + len > end)
7218 break;
7219 if (os_memcmp(ie, buf, len) != 0)
7220 continue;
7221
7222 if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
7223 wpabuf_free(wpa_s->vendor_elem[frame]);
7224 wpa_s->vendor_elem[frame] = NULL;
7225 } else {
7226 os_memmove(ie, ie + len,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007227 end - (ie + len));
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07007228 wpa_s->vendor_elem[frame]->used -= len;
7229 }
7230 os_free(buf);
7231 wpas_ctrl_vendor_elem_update(wpa_s);
7232 return 0;
7233 }
7234
7235 os_free(buf);
7236
7237 return -1;
7238}
7239
7240
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007241static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
7242{
7243 struct wpa_supplicant *wpa_s = ctx;
7244
7245 if (neighbor_rep) {
7246 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
7247 "length=%u",
7248 (unsigned int) wpabuf_len(neighbor_rep));
7249 wpabuf_free(neighbor_rep);
7250 } else {
7251 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
7252 }
7253}
7254
7255
7256static int wpas_ctrl_iface_send_neigbor_rep(struct wpa_supplicant *wpa_s,
7257 char *cmd)
7258{
7259 struct wpa_ssid ssid;
7260 struct wpa_ssid *ssid_p = NULL;
7261 int ret = 0;
7262
7263 if (os_strncmp(cmd, " ssid=", 6) == 0) {
7264 ssid.ssid_len = os_strlen(cmd + 6);
7265 if (ssid.ssid_len > 32)
7266 return -1;
7267 ssid.ssid = (u8 *) (cmd + 6);
7268 ssid_p = &ssid;
7269 }
7270
7271 ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p,
7272 wpas_ctrl_neighbor_rep_cb,
7273 wpa_s);
7274
7275 return ret;
7276}
7277
7278
7279static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
7280{
7281 eapol_sm_erp_flush(wpa_s->eapol);
7282 return 0;
7283}
7284
7285
7286static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
7287 char *cmd)
7288{
7289 char *token, *context = NULL;
7290 unsigned int enable = ~0, type = 0;
7291 u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
7292 u8 *addr = NULL, *mask = NULL;
7293
7294 while ((token = str_token(cmd, " ", &context))) {
7295 if (os_strcasecmp(token, "scan") == 0) {
7296 type |= MAC_ADDR_RAND_SCAN;
7297 } else if (os_strcasecmp(token, "sched") == 0) {
7298 type |= MAC_ADDR_RAND_SCHED_SCAN;
7299 } else if (os_strcasecmp(token, "pno") == 0) {
7300 type |= MAC_ADDR_RAND_PNO;
7301 } else if (os_strcasecmp(token, "all") == 0) {
7302 type = wpa_s->mac_addr_rand_supported;
7303 } else if (os_strncasecmp(token, "enable=", 7) == 0) {
7304 enable = atoi(token + 7);
7305 } else if (os_strncasecmp(token, "addr=", 5) == 0) {
7306 addr = _addr;
7307 if (hwaddr_aton(token + 5, addr)) {
7308 wpa_printf(MSG_INFO,
7309 "CTRL: Invalid MAC address: %s",
7310 token);
7311 return -1;
7312 }
7313 } else if (os_strncasecmp(token, "mask=", 5) == 0) {
7314 mask = _mask;
7315 if (hwaddr_aton(token + 5, mask)) {
7316 wpa_printf(MSG_INFO,
7317 "CTRL: Invalid MAC address mask: %s",
7318 token);
7319 return -1;
7320 }
7321 } else {
7322 wpa_printf(MSG_INFO,
7323 "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
7324 token);
7325 return -1;
7326 }
7327 }
7328
7329 if (!type) {
7330 wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
7331 return -1;
7332 }
7333
7334 if ((wpa_s->mac_addr_rand_supported & type) != type) {
7335 wpa_printf(MSG_INFO,
7336 "CTRL: MAC_RAND_SCAN types=%u != supported=%u",
7337 type, wpa_s->mac_addr_rand_supported);
7338 return -1;
7339 }
7340
7341 if (enable > 1) {
7342 wpa_printf(MSG_INFO,
7343 "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
7344 return -1;
7345 }
7346
7347 if (!enable) {
7348 wpas_mac_addr_rand_scan_clear(wpa_s, type);
7349 if (wpa_s->pno) {
7350 if (type & MAC_ADDR_RAND_PNO) {
7351 wpas_stop_pno(wpa_s);
7352 wpas_start_pno(wpa_s);
7353 }
7354 } else if (wpa_s->sched_scanning &&
7355 (type & MAC_ADDR_RAND_SCHED_SCAN)) {
7356 /* simulate timeout to restart the sched scan */
7357 wpa_s->sched_scan_timed_out = 1;
7358 wpa_s->prev_sched_ssid = NULL;
7359 wpa_supplicant_cancel_sched_scan(wpa_s);
7360 }
7361 return 0;
7362 }
7363
7364 if ((addr && !mask) || (!addr && mask)) {
7365 wpa_printf(MSG_INFO,
7366 "CTRL: MAC_RAND_SCAN invalid addr/mask combination");
7367 return -1;
7368 }
7369
7370 if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
7371 wpa_printf(MSG_INFO,
7372 "CTRL: MAC_RAND_SCAN cannot allow multicast address");
7373 return -1;
7374 }
7375
7376 if (type & MAC_ADDR_RAND_SCAN) {
7377 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
7378 addr, mask);
7379 }
7380
7381 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
7382 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
7383 addr, mask);
7384
7385 if (wpa_s->sched_scanning && !wpa_s->pno) {
7386 /* simulate timeout to restart the sched scan */
7387 wpa_s->sched_scan_timed_out = 1;
7388 wpa_s->prev_sched_ssid = NULL;
7389 wpa_supplicant_cancel_sched_scan(wpa_s);
7390 }
7391 }
7392
7393 if (type & MAC_ADDR_RAND_PNO) {
7394 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
7395 addr, mask);
7396 if (wpa_s->pno) {
7397 wpas_stop_pno(wpa_s);
7398 wpas_start_pno(wpa_s);
7399 }
7400 }
7401
7402 return 0;
7403}
7404
7405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007406char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
7407 char *buf, size_t *resp_len)
7408{
7409 char *reply;
7410 const int reply_size = 4096;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007411 int reply_len;
7412
7413 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007414 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
7415 if (wpa_debug_show_keys)
7416 wpa_dbg(wpa_s, MSG_DEBUG,
7417 "Control interface command '%s'", buf);
7418 else
7419 wpa_dbg(wpa_s, MSG_DEBUG,
7420 "Control interface command '%s [REMOVED]'",
7421 os_strncmp(buf, WPA_CTRL_RSP,
7422 os_strlen(WPA_CTRL_RSP)) == 0 ?
7423 WPA_CTRL_RSP : "SET_NETWORK");
7424 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007425 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007426 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
7427 (const u8 *) buf, os_strlen(buf));
7428 } else {
7429 int level = MSG_DEBUG;
7430 if (os_strcmp(buf, "PING") == 0)
7431 level = MSG_EXCESSIVE;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007432 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007433 }
7434
7435 reply = os_malloc(reply_size);
7436 if (reply == NULL) {
7437 *resp_len = 1;
7438 return NULL;
7439 }
7440
7441 os_memcpy(reply, "OK\n", 3);
7442 reply_len = 3;
7443
7444 if (os_strcmp(buf, "PING") == 0) {
7445 os_memcpy(reply, "PONG\n", 5);
7446 reply_len = 5;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007447 } else if (os_strcmp(buf, "IFNAME") == 0) {
7448 reply_len = os_strlen(wpa_s->ifname);
7449 os_memcpy(reply, wpa_s->ifname, reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007450 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
7451 if (wpa_debug_reopen_file() < 0)
7452 reply_len = -1;
7453 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
7454 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
7455 } else if (os_strcmp(buf, "MIB") == 0) {
7456 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
7457 if (reply_len >= 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007458 reply_len += eapol_sm_get_mib(wpa_s->eapol,
7459 reply + reply_len,
7460 reply_size - reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007461 }
7462 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
7463 reply_len = wpa_supplicant_ctrl_iface_status(
7464 wpa_s, buf + 6, reply, reply_size);
7465 } else if (os_strcmp(buf, "PMKSA") == 0) {
7466 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
7467 reply_size);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007468 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
7469 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007470 } else if (os_strncmp(buf, "SET ", 4) == 0) {
7471 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
7472 reply_len = -1;
7473 } else if (os_strncmp(buf, "GET ", 4) == 0) {
7474 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
7475 reply, reply_size);
7476 } else if (os_strcmp(buf, "LOGON") == 0) {
7477 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
7478 } else if (os_strcmp(buf, "LOGOFF") == 0) {
7479 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
7480 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
7481 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
7482 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007483 else
7484 wpas_request_connection(wpa_s);
Dmitry Shmidt98660862014-03-11 17:26:21 -07007485 } else if (os_strcmp(buf, "REATTACH") == 0) {
7486 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
7487 !wpa_s->current_ssid)
7488 reply_len = -1;
7489 else {
7490 wpa_s->reattach = 1;
7491 wpas_request_connection(wpa_s);
7492 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007493 } else if (os_strcmp(buf, "RECONNECT") == 0) {
7494 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
7495 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007496 else if (wpa_s->disconnected)
7497 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007498#ifdef IEEE8021X_EAPOL
7499 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
7500 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
7501 reply_len = -1;
7502#endif /* IEEE8021X_EAPOL */
7503#ifdef CONFIG_PEERKEY
7504 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
7505 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
7506 reply_len = -1;
7507#endif /* CONFIG_PEERKEY */
7508#ifdef CONFIG_IEEE80211R
7509 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
7510 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
7511 reply_len = -1;
7512#endif /* CONFIG_IEEE80211R */
7513#ifdef CONFIG_WPS
7514 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
7515 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
7516 if (res == -2) {
7517 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
7518 reply_len = 17;
7519 } else if (res)
7520 reply_len = -1;
7521 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
7522 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
7523 if (res == -2) {
7524 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
7525 reply_len = 17;
7526 } else if (res)
7527 reply_len = -1;
7528 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
7529 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
7530 reply,
7531 reply_size);
7532 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
7533 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
7534 wpa_s, buf + 14, reply, reply_size);
7535 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
7536 if (wpas_wps_cancel(wpa_s))
7537 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007538#ifdef CONFIG_WPS_NFC
7539 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
7540 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
7541 reply_len = -1;
7542 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
7543 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
7544 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007545 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
7546 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
7547 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007548 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
7549 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
7550 wpa_s, buf + 14, reply, reply_size);
7551 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
7552 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
7553 buf + 17))
7554 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007555 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
7556 reply_len = wpas_ctrl_nfc_get_handover_req(
7557 wpa_s, buf + 21, reply, reply_size);
7558 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
7559 reply_len = wpas_ctrl_nfc_get_handover_sel(
7560 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007561 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
7562 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
7563 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007564#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007565 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
7566 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
7567 reply_len = -1;
7568#ifdef CONFIG_AP
7569 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
7570 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
7571 wpa_s, buf + 11, reply, reply_size);
7572#endif /* CONFIG_AP */
7573#ifdef CONFIG_WPS_ER
7574 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
7575 if (wpas_wps_er_start(wpa_s, NULL))
7576 reply_len = -1;
7577 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
7578 if (wpas_wps_er_start(wpa_s, buf + 13))
7579 reply_len = -1;
7580 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007581 wpas_wps_er_stop(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007582 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
7583 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
7584 reply_len = -1;
7585 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
7586 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
7587 if (ret == -2) {
7588 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
7589 reply_len = 17;
7590 } else if (ret == -3) {
7591 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
7592 reply_len = 18;
7593 } else if (ret == -4) {
7594 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
7595 reply_len = 20;
7596 } else if (ret)
7597 reply_len = -1;
7598 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
7599 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
7600 reply_len = -1;
7601 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
7602 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
7603 buf + 18))
7604 reply_len = -1;
7605 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
7606 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
7607 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007608#ifdef CONFIG_WPS_NFC
7609 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
7610 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
7611 wpa_s, buf + 24, reply, reply_size);
7612#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007613#endif /* CONFIG_WPS_ER */
7614#endif /* CONFIG_WPS */
7615#ifdef CONFIG_IBSS_RSN
7616 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
7617 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
7618 reply_len = -1;
7619#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007620#ifdef CONFIG_MESH
7621 } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
7622 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
7623 wpa_s, buf + 19, reply, reply_size);
7624 } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
7625 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
7626 wpa_s, "", reply, reply_size);
7627 } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
7628 if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
7629 reply_len = -1;
7630 } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
7631 if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
7632 buf + 18))
7633 reply_len = -1;
7634#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007635#ifdef CONFIG_P2P
7636 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
7637 if (p2p_ctrl_find(wpa_s, buf + 9))
7638 reply_len = -1;
7639 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
7640 if (p2p_ctrl_find(wpa_s, ""))
7641 reply_len = -1;
7642 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
7643 wpas_p2p_stop_find(wpa_s);
7644 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
7645 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
7646 reply_size);
7647 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
7648 if (p2p_ctrl_listen(wpa_s, buf + 11))
7649 reply_len = -1;
7650 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
7651 if (p2p_ctrl_listen(wpa_s, ""))
7652 reply_len = -1;
7653 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
7654 if (wpas_p2p_group_remove(wpa_s, buf + 17))
7655 reply_len = -1;
7656 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007657 if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007658 reply_len = -1;
7659 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
7660 if (p2p_ctrl_group_add(wpa_s, buf + 14))
7661 reply_len = -1;
7662 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
7663 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
7664 reply_len = -1;
7665 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
7666 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
7667 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
7668 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
7669 reply_size);
7670 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
7671 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
7672 reply_len = -1;
7673 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
7674 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
7675 reply_len = -1;
7676 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
7677 wpas_p2p_sd_service_update(wpa_s);
7678 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
7679 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
7680 reply_len = -1;
7681 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
7682 wpas_p2p_service_flush(wpa_s);
7683 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
7684 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
7685 reply_len = -1;
7686 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
7687 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
7688 reply_len = -1;
7689 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
7690 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
7691 reply_len = -1;
7692 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
7693 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
7694 reply_len = -1;
7695 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
7696 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
7697 reply_size);
7698 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
7699 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
7700 reply_len = -1;
7701 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007702 p2p_ctrl_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007703 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
7704 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
7705 reply_len = -1;
7706 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
7707 if (wpas_p2p_cancel(wpa_s))
7708 reply_len = -1;
7709 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
7710 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
7711 reply_len = -1;
7712 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
7713 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
7714 reply_len = -1;
7715 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
7716 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
7717 reply_len = -1;
7718 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
7719 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
7720 reply_len = -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007721 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
7722 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
7723 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007724#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007725#ifdef CONFIG_WIFI_DISPLAY
7726 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
7727 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
7728 reply_len = -1;
7729 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
7730 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
7731 reply, reply_size);
7732#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007733#ifdef CONFIG_INTERWORKING
7734 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
7735 if (interworking_fetch_anqp(wpa_s) < 0)
7736 reply_len = -1;
7737 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
7738 interworking_stop_fetch_anqp(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007739 } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
7740 if (ctrl_interworking_select(wpa_s, NULL) < 0)
7741 reply_len = -1;
7742 } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
7743 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007744 reply_len = -1;
7745 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
7746 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
7747 reply_len = -1;
7748 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
7749 if (get_anqp(wpa_s, buf + 9) < 0)
7750 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007751 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
7752 if (gas_request(wpa_s, buf + 12) < 0)
7753 reply_len = -1;
7754 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
7755 reply_len = gas_response_get(wpa_s, buf + 17, reply,
7756 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007757#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07007758#ifdef CONFIG_HS20
7759 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
7760 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
7761 reply_len = -1;
7762 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
7763 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
7764 reply_len = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007765 } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
7766 if (hs20_icon_request(wpa_s, buf + 18) < 0)
7767 reply_len = -1;
7768 } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
7769 if (hs20_fetch_osu(wpa_s) < 0)
7770 reply_len = -1;
7771 } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
7772 hs20_cancel_fetch_osu(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007773#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007774 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
7775 {
7776 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
7777 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
7778 reply_len = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007779 else {
7780 /*
7781 * Notify response from timeout to allow the control
7782 * interface response to be sent first.
7783 */
7784 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
7785 wpa_s, NULL);
7786 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007787 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
7788 if (wpa_supplicant_reload_configuration(wpa_s))
7789 reply_len = -1;
7790 } else if (os_strcmp(buf, "TERMINATE") == 0) {
7791 wpa_supplicant_terminate_proc(wpa_s->global);
7792 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
7793 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
7794 reply_len = -1;
Dmitry Shmidte19501d2011-03-16 14:32:18 -07007795 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007796 reply_len = wpa_supplicant_ctrl_iface_blacklist(
7797 wpa_s, buf + 9, reply, reply_size);
7798 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
7799 reply_len = wpa_supplicant_ctrl_iface_log_level(
7800 wpa_s, buf + 9, reply, reply_size);
Vinit Deshpandeda134e92014-12-02 10:59:29 -08007801 } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
7802 reply_len = wpa_supplicant_ctrl_iface_list_networks(
7803 wpa_s, buf + 14, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007804 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
7805 reply_len = wpa_supplicant_ctrl_iface_list_networks(
Vinit Deshpandeda134e92014-12-02 10:59:29 -08007806 wpa_s, NULL, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007807 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007808#ifdef CONFIG_SME
7809 wpa_s->sme.prev_bssid_set = 0;
7810#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007811 wpa_s->reassociate = 0;
7812 wpa_s->disconnected = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007813 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007814 wpa_supplicant_cancel_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007815 wpa_supplicant_deauthenticate(wpa_s,
7816 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007817 } else if (os_strcmp(buf, "SCAN") == 0) {
7818 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
7819 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
7820 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007821 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
7822 reply_len = wpa_supplicant_ctrl_iface_scan_results(
7823 wpa_s, reply, reply_size);
7824 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
7825 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
7826 reply_len = -1;
7827 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
7828 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
7829 reply_len = -1;
7830 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
7831 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
7832 reply_len = -1;
7833 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
7834 reply_len = wpa_supplicant_ctrl_iface_add_network(
7835 wpa_s, reply, reply_size);
7836 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
7837 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
7838 reply_len = -1;
7839 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
7840 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
7841 reply_len = -1;
7842 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
7843 reply_len = wpa_supplicant_ctrl_iface_get_network(
7844 wpa_s, buf + 12, reply, reply_size);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007845 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
7846 if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12))
7847 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007848 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
7849 reply_len = wpa_supplicant_ctrl_iface_list_creds(
7850 wpa_s, reply, reply_size);
7851 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
7852 reply_len = wpa_supplicant_ctrl_iface_add_cred(
7853 wpa_s, reply, reply_size);
7854 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
7855 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
7856 reply_len = -1;
7857 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
7858 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
7859 reply_len = -1;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07007860 } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
7861 reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
7862 reply,
7863 reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007864#ifndef CONFIG_NO_CONFIG_WRITE
7865 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
7866 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
7867 reply_len = -1;
7868#endif /* CONFIG_NO_CONFIG_WRITE */
7869 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
7870 reply_len = wpa_supplicant_ctrl_iface_get_capability(
7871 wpa_s, buf + 15, reply, reply_size);
7872 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
7873 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
7874 reply_len = -1;
7875 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
7876 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
7877 reply_len = -1;
7878 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
7879 reply_len = wpa_supplicant_global_iface_list(
7880 wpa_s->global, reply, reply_size);
7881 } else if (os_strcmp(buf, "INTERFACES") == 0) {
7882 reply_len = wpa_supplicant_global_iface_interfaces(
7883 wpa_s->global, reply, reply_size);
7884 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
7885 reply_len = wpa_supplicant_ctrl_iface_bss(
7886 wpa_s, buf + 4, reply, reply_size);
7887#ifdef CONFIG_AP
7888 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
7889 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
7890 } else if (os_strncmp(buf, "STA ", 4) == 0) {
7891 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
7892 reply_size);
7893 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
7894 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
7895 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007896 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
7897 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
7898 reply_len = -1;
7899 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
7900 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
7901 reply_len = -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007902 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
7903 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
7904 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007905#endif /* CONFIG_AP */
7906 } else if (os_strcmp(buf, "SUSPEND") == 0) {
7907 wpas_notify_suspend(wpa_s->global);
7908 } else if (os_strcmp(buf, "RESUME") == 0) {
7909 wpas_notify_resume(wpa_s->global);
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007910#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007911 } else if (os_strcmp(buf, "DROP_SA") == 0) {
7912 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007913#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007914 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
7915 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
7916 reply_len = -1;
7917 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007918 wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007919 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
7920 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
7921 reply_len = -1;
7922 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
7923 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
7924 buf + 17))
7925 reply_len = -1;
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07007926 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007927 wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007928#ifdef CONFIG_TDLS
7929 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
7930 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
7931 reply_len = -1;
7932 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
7933 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
7934 reply_len = -1;
7935 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
7936 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
7937 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007938 } else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
7939 if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
7940 buf + 17))
7941 reply_len = -1;
7942 } else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
7943 if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
7944 buf + 24))
7945 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007946#endif /* CONFIG_TDLS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007947 } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
7948 reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
7949 } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
7950 if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
7951 reply_len = -1;
7952 } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
7953 if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
7954 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007955 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
7956 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
7957 reply_size);
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07007958 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
7959 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
7960 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007961#ifdef CONFIG_AUTOSCAN
7962 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
7963 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
7964 reply_len = -1;
7965#endif /* CONFIG_AUTOSCAN */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007966#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07007967 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
7968 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
7969 reply_size);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007970#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007971 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
7972 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
7973 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007974 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007975 pmksa_cache_clear_current(wpa_s->wpa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007976 eapol_sm_request_reauth(wpa_s->eapol);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007977#ifdef CONFIG_WNM
7978 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
7979 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
7980 reply_len = -1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007981 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
7982 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
Dmitry Shmidt44c95782013-05-17 09:51:35 -07007983 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007984#endif /* CONFIG_WNM */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007985 } else if (os_strcmp(buf, "FLUSH") == 0) {
7986 wpa_supplicant_ctrl_iface_flush(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007987 } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
7988 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
7989 reply_size);
Dmitry Shmidt818ea482014-03-10 13:15:21 -07007990#ifdef CONFIG_TESTING_OPTIONS
7991 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
7992 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
7993 reply_len = -1;
7994 } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
7995 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007996 } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
7997 if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
7998 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007999 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
8000 if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
8001 reply_len = -1;
8002 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
8003 if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
8004 reply_len = -1;
8005 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
8006 if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
8007 reply_len = -1;
8008 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
8009 if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
8010 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008011 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
8012 if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
8013 reply_len = -1;
8014 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
8015 reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
Dmitry Shmidt818ea482014-03-10 13:15:21 -07008016#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008017 } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
8018 if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
8019 reply_len = -1;
8020 } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
8021 reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
8022 reply_size);
8023 } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
8024 if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
8025 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008026 } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
8027 if (wpas_ctrl_iface_send_neigbor_rep(wpa_s, buf + 20))
8028 reply_len = -1;
8029 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
8030 wpas_ctrl_iface_erp_flush(wpa_s);
8031 } else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
8032 if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
8033 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008034 } else {
8035 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
8036 reply_len = 16;
8037 }
8038
8039 if (reply_len < 0) {
8040 os_memcpy(reply, "FAIL\n", 5);
8041 reply_len = 5;
8042 }
8043
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008044 *resp_len = reply_len;
8045 return reply;
8046}
8047
8048
8049static int wpa_supplicant_global_iface_add(struct wpa_global *global,
8050 char *cmd)
8051{
8052 struct wpa_interface iface;
8053 char *pos;
8054
8055 /*
8056 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
8057 * TAB<bridge_ifname>
8058 */
8059 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
8060
8061 os_memset(&iface, 0, sizeof(iface));
8062
8063 do {
8064 iface.ifname = pos = cmd;
8065 pos = os_strchr(pos, '\t');
8066 if (pos)
8067 *pos++ = '\0';
8068 if (iface.ifname[0] == '\0')
8069 return -1;
8070 if (pos == NULL)
8071 break;
8072
8073 iface.confname = pos;
8074 pos = os_strchr(pos, '\t');
8075 if (pos)
8076 *pos++ = '\0';
8077 if (iface.confname[0] == '\0')
8078 iface.confname = NULL;
8079 if (pos == NULL)
8080 break;
8081
8082 iface.driver = pos;
8083 pos = os_strchr(pos, '\t');
8084 if (pos)
8085 *pos++ = '\0';
8086 if (iface.driver[0] == '\0')
8087 iface.driver = NULL;
8088 if (pos == NULL)
8089 break;
8090
8091 iface.ctrl_interface = pos;
8092 pos = os_strchr(pos, '\t');
8093 if (pos)
8094 *pos++ = '\0';
8095 if (iface.ctrl_interface[0] == '\0')
8096 iface.ctrl_interface = NULL;
8097 if (pos == NULL)
8098 break;
8099
8100 iface.driver_param = pos;
8101 pos = os_strchr(pos, '\t');
8102 if (pos)
8103 *pos++ = '\0';
8104 if (iface.driver_param[0] == '\0')
8105 iface.driver_param = NULL;
8106 if (pos == NULL)
8107 break;
8108
8109 iface.bridge_ifname = pos;
8110 pos = os_strchr(pos, '\t');
8111 if (pos)
8112 *pos++ = '\0';
8113 if (iface.bridge_ifname[0] == '\0')
8114 iface.bridge_ifname = NULL;
8115 if (pos == NULL)
8116 break;
8117 } while (0);
8118
8119 if (wpa_supplicant_get_iface(global, iface.ifname))
8120 return -1;
8121
8122 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
8123}
8124
8125
8126static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
8127 char *cmd)
8128{
8129 struct wpa_supplicant *wpa_s;
8130
8131 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
8132
8133 wpa_s = wpa_supplicant_get_iface(global, cmd);
8134 if (wpa_s == NULL)
8135 return -1;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07008136 return wpa_supplicant_remove_iface(global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008137}
8138
8139
8140static void wpa_free_iface_info(struct wpa_interface_info *iface)
8141{
8142 struct wpa_interface_info *prev;
8143
8144 while (iface) {
8145 prev = iface;
8146 iface = iface->next;
8147
8148 os_free(prev->ifname);
8149 os_free(prev->desc);
8150 os_free(prev);
8151 }
8152}
8153
8154
8155static int wpa_supplicant_global_iface_list(struct wpa_global *global,
8156 char *buf, int len)
8157{
8158 int i, res;
8159 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
8160 char *pos, *end;
8161
8162 for (i = 0; wpa_drivers[i]; i++) {
8163 struct wpa_driver_ops *drv = wpa_drivers[i];
8164 if (drv->get_interfaces == NULL)
8165 continue;
8166 tmp = drv->get_interfaces(global->drv_priv[i]);
8167 if (tmp == NULL)
8168 continue;
8169
8170 if (last == NULL)
8171 iface = last = tmp;
8172 else
8173 last->next = tmp;
8174 while (last->next)
8175 last = last->next;
8176 }
8177
8178 pos = buf;
8179 end = buf + len;
8180 for (tmp = iface; tmp; tmp = tmp->next) {
8181 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
8182 tmp->drv_name, tmp->ifname,
8183 tmp->desc ? tmp->desc : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008184 if (os_snprintf_error(end - pos, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008185 *pos = '\0';
8186 break;
8187 }
8188 pos += res;
8189 }
8190
8191 wpa_free_iface_info(iface);
8192
8193 return pos - buf;
8194}
8195
8196
8197static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
8198 char *buf, int len)
8199{
8200 int res;
8201 char *pos, *end;
8202 struct wpa_supplicant *wpa_s;
8203
8204 wpa_s = global->ifaces;
8205 pos = buf;
8206 end = buf + len;
8207
8208 while (wpa_s) {
8209 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008210 if (os_snprintf_error(end - pos, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008211 *pos = '\0';
8212 break;
8213 }
8214 pos += res;
8215 wpa_s = wpa_s->next;
8216 }
8217 return pos - buf;
8218}
8219
8220
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008221static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
8222 const char *ifname,
8223 char *cmd, size_t *resp_len)
8224{
8225 struct wpa_supplicant *wpa_s;
8226
8227 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8228 if (os_strcmp(ifname, wpa_s->ifname) == 0)
8229 break;
8230 }
8231
8232 if (wpa_s == NULL) {
8233 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
8234 if (resp)
8235 *resp_len = os_strlen(resp);
8236 else
8237 *resp_len = 1;
8238 return resp;
8239 }
8240
8241 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
8242}
8243
8244
8245static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
8246 char *buf, size_t *resp_len)
8247{
8248#ifdef CONFIG_P2P
8249 static const char * cmd[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07008250 "LIST_NETWORKS",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008251 "P2P_FIND",
8252 "P2P_STOP_FIND",
8253 "P2P_LISTEN",
8254 "P2P_GROUP_ADD",
8255 "P2P_GET_PASSPHRASE",
8256 "P2P_SERVICE_UPDATE",
8257 "P2P_SERVICE_FLUSH",
8258 "P2P_FLUSH",
8259 "P2P_CANCEL",
8260 "P2P_PRESENCE_REQ",
8261 "P2P_EXT_LISTEN",
8262 NULL
8263 };
8264 static const char * prefix[] = {
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08008265#ifdef ANDROID
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07008266 "DRIVER ",
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08008267#endif /* ANDROID */
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07008268 "GET_NETWORK ",
8269 "REMOVE_NETWORK ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008270 "P2P_FIND ",
8271 "P2P_CONNECT ",
8272 "P2P_LISTEN ",
8273 "P2P_GROUP_REMOVE ",
8274 "P2P_GROUP_ADD ",
8275 "P2P_PROV_DISC ",
8276 "P2P_SERV_DISC_REQ ",
8277 "P2P_SERV_DISC_CANCEL_REQ ",
8278 "P2P_SERV_DISC_RESP ",
8279 "P2P_SERV_DISC_EXTERNAL ",
8280 "P2P_SERVICE_ADD ",
8281 "P2P_SERVICE_DEL ",
8282 "P2P_REJECT ",
8283 "P2P_INVITE ",
8284 "P2P_PEER ",
8285 "P2P_SET ",
8286 "P2P_UNAUTHORIZE ",
8287 "P2P_PRESENCE_REQ ",
8288 "P2P_EXT_LISTEN ",
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008289 "P2P_REMOVE_CLIENT ",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008290 "WPS_NFC_TOKEN ",
8291 "WPS_NFC_TAG_READ ",
Dmitry Shmidt413dde72014-04-11 10:23:22 -07008292 "NFC_GET_HANDOVER_SEL ",
8293 "NFC_GET_HANDOVER_REQ ",
8294 "NFC_REPORT_HANDOVER ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008295 NULL
8296 };
8297 int found = 0;
8298 int i;
8299
8300 if (global->p2p_init_wpa_s == NULL)
8301 return NULL;
8302
8303 for (i = 0; !found && cmd[i]; i++) {
8304 if (os_strcmp(buf, cmd[i]) == 0)
8305 found = 1;
8306 }
8307
8308 for (i = 0; !found && prefix[i]; i++) {
8309 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
8310 found = 1;
8311 }
8312
8313 if (found)
8314 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
8315 buf, resp_len);
8316#endif /* CONFIG_P2P */
8317 return NULL;
8318}
8319
8320
8321static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
8322 char *buf, size_t *resp_len)
8323{
8324#ifdef CONFIG_WIFI_DISPLAY
8325 if (global->p2p_init_wpa_s == NULL)
8326 return NULL;
8327 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
8328 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
8329 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
8330 buf, resp_len);
8331#endif /* CONFIG_WIFI_DISPLAY */
8332 return NULL;
8333}
8334
8335
8336static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
8337 char *buf, size_t *resp_len)
8338{
8339 char *ret;
8340
8341 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
8342 if (ret)
8343 return ret;
8344
8345 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
8346 if (ret)
8347 return ret;
8348
8349 return NULL;
8350}
8351
8352
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008353static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
8354{
8355 char *value;
8356
8357 value = os_strchr(cmd, ' ');
8358 if (value == NULL)
8359 return -1;
8360 *value++ = '\0';
8361
8362 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
8363
8364#ifdef CONFIG_WIFI_DISPLAY
8365 if (os_strcasecmp(cmd, "wifi_display") == 0) {
8366 wifi_display_enable(global, !!atoi(value));
8367 return 0;
8368 }
8369#endif /* CONFIG_WIFI_DISPLAY */
8370
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008371 /* Restore cmd to its original value to allow redirection */
8372 value[-1] = ' ';
8373
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008374 return -1;
8375}
8376
8377
8378#ifndef CONFIG_NO_CONFIG_WRITE
8379static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
8380{
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008381 int ret = 0, saved = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008382 struct wpa_supplicant *wpa_s;
8383
8384 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8385 if (!wpa_s->conf->update_config) {
8386 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
8387 continue;
8388 }
8389
8390 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
8391 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
8392 ret = 1;
8393 } else {
8394 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008395 saved++;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008396 }
8397 }
8398
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008399 if (!saved && !ret) {
8400 wpa_dbg(wpa_s, MSG_DEBUG,
8401 "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
8402 ret = 1;
8403 }
8404
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008405 return ret;
8406}
8407#endif /* CONFIG_NO_CONFIG_WRITE */
8408
8409
8410static int wpas_global_ctrl_iface_status(struct wpa_global *global,
8411 char *buf, size_t buflen)
8412{
8413 char *pos, *end;
8414 int ret;
8415 struct wpa_supplicant *wpa_s;
8416
8417 pos = buf;
8418 end = buf + buflen;
8419
8420#ifdef CONFIG_P2P
8421 if (global->p2p && !global->p2p_disabled) {
8422 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
8423 "\n"
8424 "p2p_state=%s\n",
8425 MAC2STR(global->p2p_dev_addr),
8426 p2p_get_state_txt(global->p2p));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008427 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008428 return pos - buf;
8429 pos += ret;
8430 } else if (global->p2p) {
8431 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008432 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008433 return pos - buf;
8434 pos += ret;
8435 }
8436#endif /* CONFIG_P2P */
8437
8438#ifdef CONFIG_WIFI_DISPLAY
8439 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
8440 !!global->wifi_display);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008441 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008442 return pos - buf;
8443 pos += ret;
8444#endif /* CONFIG_WIFI_DISPLAY */
8445
8446 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8447 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
8448 "address=" MACSTR "\n",
8449 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008450 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008451 return pos - buf;
8452 pos += ret;
8453 }
8454
8455 return pos - buf;
8456}
8457
8458
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008459char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
8460 char *buf, size_t *resp_len)
8461{
8462 char *reply;
8463 const int reply_size = 2048;
8464 int reply_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008465 int level = MSG_DEBUG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008466
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008467 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
8468 char *pos = os_strchr(buf + 7, ' ');
8469 if (pos) {
8470 *pos++ = '\0';
8471 return wpas_global_ctrl_iface_ifname(global,
8472 buf + 7, pos,
8473 resp_len);
8474 }
8475 }
8476
8477 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
8478 if (reply)
8479 return reply;
8480
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008481 if (os_strcmp(buf, "PING") == 0)
8482 level = MSG_EXCESSIVE;
8483 wpa_hexdump_ascii(level, "RX global ctrl_iface",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008484 (const u8 *) buf, os_strlen(buf));
8485
8486 reply = os_malloc(reply_size);
8487 if (reply == NULL) {
8488 *resp_len = 1;
8489 return NULL;
8490 }
8491
8492 os_memcpy(reply, "OK\n", 3);
8493 reply_len = 3;
8494
8495 if (os_strcmp(buf, "PING") == 0) {
8496 os_memcpy(reply, "PONG\n", 5);
8497 reply_len = 5;
8498 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
8499 if (wpa_supplicant_global_iface_add(global, buf + 14))
8500 reply_len = -1;
8501 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
8502 if (wpa_supplicant_global_iface_remove(global, buf + 17))
8503 reply_len = -1;
8504 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
8505 reply_len = wpa_supplicant_global_iface_list(
8506 global, reply, reply_size);
8507 } else if (os_strcmp(buf, "INTERFACES") == 0) {
8508 reply_len = wpa_supplicant_global_iface_interfaces(
8509 global, reply, reply_size);
8510 } else if (os_strcmp(buf, "TERMINATE") == 0) {
8511 wpa_supplicant_terminate_proc(global);
8512 } else if (os_strcmp(buf, "SUSPEND") == 0) {
8513 wpas_notify_suspend(global);
8514 } else if (os_strcmp(buf, "RESUME") == 0) {
8515 wpas_notify_resume(global);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008516 } else if (os_strncmp(buf, "SET ", 4) == 0) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008517 if (wpas_global_ctrl_iface_set(global, buf + 4)) {
8518#ifdef CONFIG_P2P
8519 if (global->p2p_init_wpa_s) {
8520 os_free(reply);
8521 /* Check if P2P redirection would work for this
8522 * command. */
8523 return wpa_supplicant_ctrl_iface_process(
8524 global->p2p_init_wpa_s,
8525 buf, resp_len);
8526 }
8527#endif /* CONFIG_P2P */
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008528 reply_len = -1;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008529 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008530#ifndef CONFIG_NO_CONFIG_WRITE
8531 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
8532 if (wpas_global_ctrl_iface_save_config(global))
8533 reply_len = -1;
8534#endif /* CONFIG_NO_CONFIG_WRITE */
8535 } else if (os_strcmp(buf, "STATUS") == 0) {
8536 reply_len = wpas_global_ctrl_iface_status(global, reply,
8537 reply_size);
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08008538#ifdef CONFIG_MODULE_TESTS
8539 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
8540 int wpas_module_tests(void);
8541 if (wpas_module_tests() < 0)
8542 reply_len = -1;
8543#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008544 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
8545 if (wpa_debug_reopen_file() < 0)
8546 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008547 } else {
8548 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
8549 reply_len = 16;
8550 }
8551
8552 if (reply_len < 0) {
8553 os_memcpy(reply, "FAIL\n", 5);
8554 reply_len = 5;
8555 }
8556
8557 *resp_len = reply_len;
8558 return reply;
8559}