blob: acdc90d7d46686df31311ac7d58b83acdae4b920 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Control interface (shared code for all backends)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
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 Shmidt6c0da2b2015-01-05 13:08:17 -080022#include "ap/hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#include "eap_peer/eap.h"
24#include "eapol_supp/eapol_supp_sm.h"
25#include "rsn_supp/wpa.h"
26#include "rsn_supp/preauth.h"
27#include "rsn_supp/pmksa_cache.h"
28#include "l2_packet/l2_packet.h"
29#include "wps/wps.h"
30#include "config.h"
31#include "wpa_supplicant_i.h"
32#include "driver_i.h"
33#include "wps_supplicant.h"
34#include "ibss_rsn.h"
35#include "ap.h"
36#include "p2p_supplicant.h"
37#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070038#include "hs20_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070039#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040#include "notify.h"
41#include "bss.h"
42#include "scan.h"
43#include "ctrl_iface.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080044#include "interworking.h"
Dmitry Shmidte19501d2011-03-16 14:32:18 -070045#include "blacklist.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070046#include "autoscan.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080047#include "wnm_sta.h"
Dmitry Shmidt818ea482014-03-10 13:15:21 -070048#include "offchannel.h"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070049#include "drivers/driver.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080050#include "mesh.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052static int wpa_supplicant_global_iface_list(struct wpa_global *global,
53 char *buf, int len);
54static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
55 char *buf, int len);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -070056static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
57 char *val);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058
Dmitry Shmidt04949592012-07-19 12:16:46 -070059static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
60{
61 char *pos;
62 u8 addr[ETH_ALEN], *filter = NULL, *n;
63 size_t count = 0;
64
65 pos = val;
66 while (pos) {
67 if (*pos == '\0')
68 break;
69 if (hwaddr_aton(pos, addr)) {
70 os_free(filter);
71 return -1;
72 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070073 n = os_realloc_array(filter, count + 1, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -070074 if (n == NULL) {
75 os_free(filter);
76 return -1;
77 }
78 filter = n;
79 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
80 count++;
81
82 pos = os_strchr(pos, ' ');
83 if (pos)
84 pos++;
85 }
86
87 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
88 os_free(wpa_s->bssid_filter);
89 wpa_s->bssid_filter = filter;
90 wpa_s->bssid_filter_count = count;
91
92 return 0;
93}
94
95
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080096static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
97{
98 char *pos;
99 u8 addr[ETH_ALEN], *bssid = NULL, *n;
100 struct wpa_ssid_value *ssid = NULL, *ns;
101 size_t count = 0, ssid_count = 0;
102 struct wpa_ssid *c;
103
104 /*
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800105 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800106 * SSID_SPEC ::= ssid <SSID_HEX>
107 * BSSID_SPEC ::= bssid <BSSID_HEX>
108 */
109
110 pos = val;
111 while (pos) {
112 if (*pos == '\0')
113 break;
114 if (os_strncmp(pos, "bssid ", 6) == 0) {
115 int res;
116 pos += 6;
117 res = hwaddr_aton2(pos, addr);
118 if (res < 0) {
119 os_free(ssid);
120 os_free(bssid);
121 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
122 "BSSID value '%s'", pos);
123 return -1;
124 }
125 pos += res;
126 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
127 if (n == NULL) {
128 os_free(ssid);
129 os_free(bssid);
130 return -1;
131 }
132 bssid = n;
133 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
134 count++;
135 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
136 char *end;
137 pos += 5;
138
139 end = pos;
140 while (*end) {
141 if (*end == '\0' || *end == ' ')
142 break;
143 end++;
144 }
145
146 ns = os_realloc_array(ssid, ssid_count + 1,
147 sizeof(struct wpa_ssid_value));
148 if (ns == NULL) {
149 os_free(ssid);
150 os_free(bssid);
151 return -1;
152 }
153 ssid = ns;
154
155 if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
156 hexstr2bin(pos, ssid[ssid_count].ssid,
157 (end - pos) / 2) < 0) {
158 os_free(ssid);
159 os_free(bssid);
160 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
161 "SSID value '%s'", pos);
162 return -1;
163 }
164 ssid[ssid_count].ssid_len = (end - pos) / 2;
165 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
166 ssid[ssid_count].ssid,
167 ssid[ssid_count].ssid_len);
168 ssid_count++;
169 pos = end;
170 } else {
171 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
172 "'%s'", pos);
173 os_free(ssid);
174 os_free(bssid);
175 return -1;
176 }
177
178 pos = os_strchr(pos, ' ');
179 if (pos)
180 pos++;
181 }
182
183 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
184 os_free(wpa_s->disallow_aps_bssid);
185 wpa_s->disallow_aps_bssid = bssid;
186 wpa_s->disallow_aps_bssid_count = count;
187
188 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
189 os_free(wpa_s->disallow_aps_ssid);
190 wpa_s->disallow_aps_ssid = ssid;
191 wpa_s->disallow_aps_ssid_count = ssid_count;
192
193 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
194 return 0;
195
196 c = wpa_s->current_ssid;
197 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
198 return 0;
199
200 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
201 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
202 return 0;
203
204 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
205 "because current AP was marked disallowed");
206
207#ifdef CONFIG_SME
208 wpa_s->sme.prev_bssid_set = 0;
209#endif /* CONFIG_SME */
210 wpa_s->reassociate = 1;
211 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
212 wpa_supplicant_req_scan(wpa_s, 0, 0);
213
214 return 0;
215}
216
217
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700218#ifndef CONFIG_NO_CONFIG_BLOBS
219static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
220{
221 char *name = pos;
222 struct wpa_config_blob *blob;
223 size_t len;
224
225 pos = os_strchr(pos, ' ');
226 if (pos == NULL)
227 return -1;
228 *pos++ = '\0';
229 len = os_strlen(pos);
230 if (len & 1)
231 return -1;
232
233 wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
234 blob = os_zalloc(sizeof(*blob));
235 if (blob == NULL)
236 return -1;
237 blob->name = os_strdup(name);
238 blob->data = os_malloc(len / 2);
239 if (blob->name == NULL || blob->data == NULL) {
240 wpa_config_free_blob(blob);
241 return -1;
242 }
243
244 if (hexstr2bin(pos, blob->data, len / 2) < 0) {
245 wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
246 wpa_config_free_blob(blob);
247 return -1;
248 }
249 blob->len = len / 2;
250
251 wpa_config_set_blob(wpa_s->conf, blob);
252
253 return 0;
254}
255#endif /* CONFIG_NO_CONFIG_BLOBS */
256
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700257
258static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
259{
260 char *params;
261 char *pos;
262 int *freqs = NULL;
263 int ret;
264
265 if (atoi(cmd)) {
266 params = os_strchr(cmd, ' ');
267 os_free(wpa_s->manual_sched_scan_freqs);
268 if (params) {
269 params++;
270 pos = os_strstr(params, "freq=");
271 if (pos)
272 freqs = freq_range_to_channel_list(wpa_s,
273 pos + 5);
274 }
275 wpa_s->manual_sched_scan_freqs = freqs;
276 ret = wpas_start_pno(wpa_s);
277 } else {
278 ret = wpas_stop_pno(wpa_s);
279 }
280 return ret;
281}
282
283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
285 char *cmd)
286{
287 char *value;
288 int ret = 0;
289
290 value = os_strchr(cmd, ' ');
291 if (value == NULL)
292 return -1;
293 *value++ = '\0';
294
295 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
296 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
297 eapol_sm_configure(wpa_s->eapol,
298 atoi(value), -1, -1, -1);
299 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
300 eapol_sm_configure(wpa_s->eapol,
301 -1, atoi(value), -1, -1);
302 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
303 eapol_sm_configure(wpa_s->eapol,
304 -1, -1, atoi(value), -1);
305 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
306 eapol_sm_configure(wpa_s->eapol,
307 -1, -1, -1, atoi(value));
308 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
309 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
310 atoi(value)))
311 ret = -1;
312 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
313 0) {
314 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
315 atoi(value)))
316 ret = -1;
317 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
318 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
319 ret = -1;
320 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
321 wpa_s->wps_fragment_size = atoi(value);
322#ifdef CONFIG_WPS_TESTING
323 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
324 long int val;
325 val = strtol(value, NULL, 0);
326 if (val < 0 || val > 0xff) {
327 ret = -1;
328 wpa_printf(MSG_DEBUG, "WPS: Invalid "
329 "wps_version_number %ld", val);
330 } else {
331 wps_version_number = val;
332 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
333 "version %u.%u",
334 (wps_version_number & 0xf0) >> 4,
335 wps_version_number & 0x0f);
336 }
337 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
338 wps_testing_dummy_cred = atoi(value);
339 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
340 wps_testing_dummy_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800341 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
342 wps_corrupt_pkhash = atoi(value);
343 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
344 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345#endif /* CONFIG_WPS_TESTING */
346 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
347 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
348 ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800349#ifdef CONFIG_TDLS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350#ifdef CONFIG_TDLS_TESTING
351 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
352 extern unsigned int tdls_testing;
353 tdls_testing = strtol(value, NULL, 0);
354 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
355#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
357 int disabled = atoi(value);
358 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
359 if (disabled) {
360 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
361 ret = -1;
362 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
363 ret = -1;
364 wpa_tdls_enable(wpa_s->wpa, !disabled);
365#endif /* CONFIG_TDLS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800366 } else if (os_strcasecmp(cmd, "pno") == 0) {
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700367 ret = wpas_ctrl_pno(wpa_s, value);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700368 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
369 int disabled = atoi(value);
370 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
371 ret = -1;
372 else if (disabled)
373 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
374 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
375 if (os_strcmp(value, "disable") == 0)
376 wpa_s->set_sta_uapsd = 0;
377 else {
378 int be, bk, vi, vo;
379 char *pos;
380 /* format: BE,BK,VI,VO;max SP Length */
381 be = atoi(value);
382 pos = os_strchr(value, ',');
383 if (pos == NULL)
384 return -1;
385 pos++;
386 bk = atoi(pos);
387 pos = os_strchr(pos, ',');
388 if (pos == NULL)
389 return -1;
390 pos++;
391 vi = atoi(pos);
392 pos = os_strchr(pos, ',');
393 if (pos == NULL)
394 return -1;
395 pos++;
396 vo = atoi(pos);
397 /* ignore max SP Length for now */
398
399 wpa_s->set_sta_uapsd = 1;
400 wpa_s->sta_uapsd = 0;
401 if (be)
402 wpa_s->sta_uapsd |= BIT(0);
403 if (bk)
404 wpa_s->sta_uapsd |= BIT(1);
405 if (vi)
406 wpa_s->sta_uapsd |= BIT(2);
407 if (vo)
408 wpa_s->sta_uapsd |= BIT(3);
409 }
Jouni Malinen21d6bc82012-04-10 16:17:59 -0700410 } else if (os_strcasecmp(cmd, "ps") == 0) {
411 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700412#ifdef CONFIG_WIFI_DISPLAY
413 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
Dmitry Shmidted003d22014-02-06 10:09:12 -0800414 int enabled = !!atoi(value);
415 if (enabled && !wpa_s->global->p2p)
416 ret = -1;
417 else
418 wifi_display_enable(wpa_s->global, enabled);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700419#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700420 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
421 ret = set_bssid_filter(wpa_s, value);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800422 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
423 ret = set_disallow_aps(wpa_s, value);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800424 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
425 wpa_s->no_keep_alive = !!atoi(value);
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700426#ifdef CONFIG_TESTING_OPTIONS
427 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
428 wpa_s->ext_mgmt_frame_handling = !!atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800429 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
430 wpa_s->ext_eapol_frame_io = !!atoi(value);
431#ifdef CONFIG_AP
432 if (wpa_s->ap_iface) {
433 wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
434 wpa_s->ext_eapol_frame_io;
435 }
436#endif /* CONFIG_AP */
437 } else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
438 wpa_s->extra_roc_dur = atoi(value);
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700439#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700440#ifndef CONFIG_NO_CONFIG_BLOBS
441 } else if (os_strcmp(cmd, "blob") == 0) {
442 ret = wpas_ctrl_set_blob(wpa_s, value);
443#endif /* CONFIG_NO_CONFIG_BLOBS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800444 } else if (os_strcasecmp(cmd, "setband") == 0) {
445 if (os_strcmp(value, "AUTO") == 0)
446 wpa_s->setband = WPA_SETBAND_AUTO;
447 else if (os_strcmp(value, "5G") == 0)
448 wpa_s->setband = WPA_SETBAND_5G;
449 else if (os_strcmp(value, "2G") == 0)
450 wpa_s->setband = WPA_SETBAND_2G;
451 else
452 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700453 } else {
454 value[-1] = '=';
455 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
456 if (ret == 0)
457 wpa_supplicant_update_config(wpa_s);
458 }
459
460 return ret;
461}
462
463
464static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
465 char *cmd, char *buf, size_t buflen)
466{
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700467 int res = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468
469 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
470
471 if (os_strcmp(cmd, "version") == 0) {
472 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700473 } else if (os_strcasecmp(cmd, "country") == 0) {
474 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
475 res = os_snprintf(buf, buflen, "%c%c",
476 wpa_s->conf->country[0],
477 wpa_s->conf->country[1]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700478#ifdef CONFIG_WIFI_DISPLAY
479 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
Dmitry Shmidted003d22014-02-06 10:09:12 -0800480 int enabled;
481 if (wpa_s->global->p2p == NULL ||
482 wpa_s->global->p2p_disabled)
483 enabled = 0;
484 else
485 enabled = wpa_s->global->wifi_display;
486 res = os_snprintf(buf, buflen, "%d", enabled);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700487#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700488#ifdef CONFIG_TESTING_GET_GTK
489 } else if (os_strcmp(cmd, "gtk") == 0) {
490 if (wpa_s->last_gtk_len == 0)
491 return -1;
492 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
493 wpa_s->last_gtk_len);
494 return res;
495#endif /* CONFIG_TESTING_GET_GTK */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700496 }
497
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800498 if (os_snprintf_error(buflen, res))
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700499 return -1;
500 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501}
502
503
504#ifdef IEEE8021X_EAPOL
505static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
506 char *addr)
507{
508 u8 bssid[ETH_ALEN];
509 struct wpa_ssid *ssid = wpa_s->current_ssid;
510
511 if (hwaddr_aton(addr, bssid)) {
512 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
513 "'%s'", addr);
514 return -1;
515 }
516
517 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
518 rsn_preauth_deinit(wpa_s->wpa);
519 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
520 return -1;
521
522 return 0;
523}
524#endif /* IEEE8021X_EAPOL */
525
526
527#ifdef CONFIG_PEERKEY
528/* MLME-STKSTART.request(peer) */
529static int wpa_supplicant_ctrl_iface_stkstart(
530 struct wpa_supplicant *wpa_s, char *addr)
531{
532 u8 peer[ETH_ALEN];
533
534 if (hwaddr_aton(addr, peer)) {
535 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
536 "address '%s'", addr);
537 return -1;
538 }
539
540 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
541 MAC2STR(peer));
542
543 return wpa_sm_stkstart(wpa_s->wpa, peer);
544}
545#endif /* CONFIG_PEERKEY */
546
547
548#ifdef CONFIG_TDLS
549
550static int wpa_supplicant_ctrl_iface_tdls_discover(
551 struct wpa_supplicant *wpa_s, char *addr)
552{
553 u8 peer[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800554 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555
556 if (hwaddr_aton(addr, peer)) {
557 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
558 "address '%s'", addr);
559 return -1;
560 }
561
562 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
563 MAC2STR(peer));
564
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800565 if (wpa_tdls_is_external_setup(wpa_s->wpa))
566 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
567 else
568 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
569
570 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700571}
572
573
574static int wpa_supplicant_ctrl_iface_tdls_setup(
575 struct wpa_supplicant *wpa_s, char *addr)
576{
577 u8 peer[ETH_ALEN];
578 int ret;
579
580 if (hwaddr_aton(addr, peer)) {
581 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
582 "address '%s'", addr);
583 return -1;
584 }
585
586 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
587 MAC2STR(peer));
588
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800589 if ((wpa_s->conf->tdls_external_control) &&
590 wpa_tdls_is_external_setup(wpa_s->wpa))
591 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
592
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800593 wpa_tdls_remove(wpa_s->wpa, peer);
594
595 if (wpa_tdls_is_external_setup(wpa_s->wpa))
596 ret = wpa_tdls_start(wpa_s->wpa, peer);
597 else
598 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800599
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700600 return ret;
601}
602
603
604static int wpa_supplicant_ctrl_iface_tdls_teardown(
605 struct wpa_supplicant *wpa_s, char *addr)
606{
607 u8 peer[ETH_ALEN];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700608 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700609
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700610 if (os_strcmp(addr, "*") == 0) {
611 /* remove everyone */
612 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
613 wpa_tdls_teardown_peers(wpa_s->wpa);
614 return 0;
615 }
616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617 if (hwaddr_aton(addr, peer)) {
618 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
619 "address '%s'", addr);
620 return -1;
621 }
622
623 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
624 MAC2STR(peer));
625
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800626 if ((wpa_s->conf->tdls_external_control) &&
627 wpa_tdls_is_external_setup(wpa_s->wpa))
628 return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
629
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700630 if (wpa_tdls_is_external_setup(wpa_s->wpa))
631 ret = wpa_tdls_teardown_link(
632 wpa_s->wpa, peer,
633 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
634 else
635 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
636
637 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700638}
639
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700640
641static int ctrl_iface_get_capability_tdls(
642 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
643{
644 int ret;
645
646 ret = os_snprintf(buf, buflen, "%s\n",
647 wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
648 (wpa_s->drv_flags &
649 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
650 "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800651 if (os_snprintf_error(buflen, ret))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700652 return -1;
653 return ret;
654}
655
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800656
657static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
658 struct wpa_supplicant *wpa_s, char *cmd)
659{
660 u8 peer[ETH_ALEN];
661 struct hostapd_freq_params freq_params;
662 u8 oper_class;
663 char *pos, *end;
664
665 if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
666 wpa_printf(MSG_INFO,
667 "tdls_chanswitch: Only supported with external setup");
668 return -1;
669 }
670
671 os_memset(&freq_params, 0, sizeof(freq_params));
672
673 pos = os_strchr(cmd, ' ');
674 if (pos == NULL)
675 return -1;
676 *pos++ = '\0';
677
678 oper_class = strtol(pos, &end, 10);
679 if (pos == end) {
680 wpa_printf(MSG_INFO,
681 "tdls_chanswitch: Invalid op class provided");
682 return -1;
683 }
684
685 pos = end;
686 freq_params.freq = atoi(pos);
687 if (freq_params.freq == 0) {
688 wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
689 return -1;
690 }
691
692#define SET_FREQ_SETTING(str) \
693 do { \
694 const char *pos2 = os_strstr(pos, " " #str "="); \
695 if (pos2) { \
696 pos2 += sizeof(" " #str "=") - 1; \
697 freq_params.str = atoi(pos2); \
698 } \
699 } while (0)
700
701 SET_FREQ_SETTING(center_freq1);
702 SET_FREQ_SETTING(center_freq2);
703 SET_FREQ_SETTING(bandwidth);
704 SET_FREQ_SETTING(sec_channel_offset);
705#undef SET_FREQ_SETTING
706
707 freq_params.ht_enabled = !!os_strstr(pos, " ht");
708 freq_params.vht_enabled = !!os_strstr(pos, " vht");
709
710 if (hwaddr_aton(cmd, peer)) {
711 wpa_printf(MSG_DEBUG,
712 "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
713 cmd);
714 return -1;
715 }
716
717 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
718 " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
719 MAC2STR(peer), oper_class, freq_params.freq,
720 freq_params.center_freq1, freq_params.center_freq2,
721 freq_params.bandwidth, freq_params.sec_channel_offset,
722 freq_params.ht_enabled ? " HT" : "",
723 freq_params.vht_enabled ? " VHT" : "");
724
725 return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
726 &freq_params);
727}
728
729
730static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
731 struct wpa_supplicant *wpa_s, char *cmd)
732{
733 u8 peer[ETH_ALEN];
734
735 if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
736 wpa_printf(MSG_INFO,
737 "tdls_chanswitch: Only supported with external setup");
738 return -1;
739 }
740
741 if (hwaddr_aton(cmd, peer)) {
742 wpa_printf(MSG_DEBUG,
743 "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
744 cmd);
745 return -1;
746 }
747
748 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
749 MAC2STR(peer));
750
751 return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
752}
753
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754#endif /* CONFIG_TDLS */
755
756
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800757static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
758{
759 char *token, *context = NULL;
760 struct wmm_ac_ts_setup_params params = {
761 .tsid = 0xff,
762 .direction = 0xff,
763 };
764
765 while ((token = str_token(cmd, " ", &context))) {
766 if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
767 sscanf(token, "up=%i", &params.user_priority) == 1 ||
768 sscanf(token, "nominal_msdu_size=%i",
769 &params.nominal_msdu_size) == 1 ||
770 sscanf(token, "mean_data_rate=%i",
771 &params.mean_data_rate) == 1 ||
772 sscanf(token, "min_phy_rate=%i",
773 &params.minimum_phy_rate) == 1 ||
774 sscanf(token, "sba=%i",
775 &params.surplus_bandwidth_allowance) == 1)
776 continue;
777
778 if (os_strcasecmp(token, "downlink") == 0) {
779 params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
780 } else if (os_strcasecmp(token, "uplink") == 0) {
781 params.direction = WMM_TSPEC_DIRECTION_UPLINK;
782 } else if (os_strcasecmp(token, "bidi") == 0) {
783 params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
784 } else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
785 params.fixed_nominal_msdu = 1;
786 } else {
787 wpa_printf(MSG_DEBUG,
788 "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
789 token);
790 return -1;
791 }
792
793 }
794
795 return wpas_wmm_ac_addts(wpa_s, &params);
796}
797
798
799static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
800{
801 u8 tsid = atoi(cmd);
802
803 return wpas_wmm_ac_delts(wpa_s, tsid);
804}
805
806
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807#ifdef CONFIG_IEEE80211R
808static int wpa_supplicant_ctrl_iface_ft_ds(
809 struct wpa_supplicant *wpa_s, char *addr)
810{
811 u8 target_ap[ETH_ALEN];
812 struct wpa_bss *bss;
813 const u8 *mdie;
814
815 if (hwaddr_aton(addr, target_ap)) {
816 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
817 "address '%s'", addr);
818 return -1;
819 }
820
821 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
822
823 bss = wpa_bss_get_bssid(wpa_s, target_ap);
824 if (bss)
825 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
826 else
827 mdie = NULL;
828
829 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
830}
831#endif /* CONFIG_IEEE80211R */
832
833
834#ifdef CONFIG_WPS
835static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
836 char *cmd)
837{
838 u8 bssid[ETH_ALEN], *_bssid = bssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700839#ifdef CONFIG_P2P
840 u8 p2p_dev_addr[ETH_ALEN];
841#endif /* CONFIG_P2P */
842#ifdef CONFIG_AP
843 u8 *_p2p_dev_addr = NULL;
844#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800845
Dmitry Shmidtc0a8db02013-11-08 11:18:33 -0800846 if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 _bssid = NULL;
848#ifdef CONFIG_P2P
849 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
850 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
851 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
852 "P2P Device Address '%s'",
853 cmd + 13);
854 return -1;
855 }
856 _p2p_dev_addr = p2p_dev_addr;
857#endif /* CONFIG_P2P */
858 } else if (hwaddr_aton(cmd, bssid)) {
859 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
860 cmd);
861 return -1;
862 }
863
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800864#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865 if (wpa_s->ap_iface)
866 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
867#endif /* CONFIG_AP */
868
869 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
870}
871
872
873static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
874 char *cmd, char *buf,
875 size_t buflen)
876{
877 u8 bssid[ETH_ALEN], *_bssid = bssid;
878 char *pin;
879 int ret;
880
881 pin = os_strchr(cmd, ' ');
882 if (pin)
883 *pin++ = '\0';
884
885 if (os_strcmp(cmd, "any") == 0)
886 _bssid = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800887 else if (os_strcmp(cmd, "get") == 0) {
888 ret = wps_generate_pin();
889 goto done;
890 } else if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
892 cmd);
893 return -1;
894 }
895
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800896#ifdef CONFIG_AP
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800897 if (wpa_s->ap_iface) {
898 int timeout = 0;
899 char *pos;
900
901 if (pin) {
902 pos = os_strchr(pin, ' ');
903 if (pos) {
904 *pos++ = '\0';
905 timeout = atoi(pos);
906 }
907 }
908
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800910 buf, buflen, timeout);
911 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912#endif /* CONFIG_AP */
913
914 if (pin) {
915 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
916 DEV_PW_DEFAULT);
917 if (ret < 0)
918 return -1;
919 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800920 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921 return -1;
922 return ret;
923 }
924
925 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
926 if (ret < 0)
927 return -1;
928
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800929done:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 /* Return the generated PIN */
931 ret = os_snprintf(buf, buflen, "%08d", ret);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800932 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933 return -1;
934 return ret;
935}
936
937
938static int wpa_supplicant_ctrl_iface_wps_check_pin(
939 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
940{
941 char pin[9];
942 size_t len;
943 char *pos;
944 int ret;
945
946 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
947 (u8 *) cmd, os_strlen(cmd));
948 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
949 if (*pos < '0' || *pos > '9')
950 continue;
951 pin[len++] = *pos;
952 if (len == 9) {
953 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
954 return -1;
955 }
956 }
957 if (len != 4 && len != 8) {
958 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
959 return -1;
960 }
961 pin[len] = '\0';
962
963 if (len == 8) {
964 unsigned int pin_val;
965 pin_val = atoi(pin);
966 if (!wps_pin_valid(pin_val)) {
967 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
968 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800969 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970 return -1;
971 return ret;
972 }
973 }
974
975 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800976 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977 return -1;
978
979 return ret;
980}
981
982
Dmitry Shmidt04949592012-07-19 12:16:46 -0700983#ifdef CONFIG_WPS_NFC
984
985static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
986 char *cmd)
987{
988 u8 bssid[ETH_ALEN], *_bssid = bssid;
989
990 if (cmd == NULL || cmd[0] == '\0')
991 _bssid = NULL;
992 else if (hwaddr_aton(cmd, bssid))
993 return -1;
994
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800995 return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
996 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700997}
998
999
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001000static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
1001 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1002{
1003 int ndef;
1004 struct wpabuf *buf;
1005 int res;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001006 char *pos;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001007
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001008 pos = os_strchr(cmd, ' ');
1009 if (pos)
1010 *pos++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001011 if (os_strcmp(cmd, "WPS") == 0)
1012 ndef = 0;
1013 else if (os_strcmp(cmd, "NDEF") == 0)
1014 ndef = 1;
1015 else
1016 return -1;
1017
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001018 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001019 if (buf == NULL)
1020 return -1;
1021
1022 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1023 wpabuf_len(buf));
1024 reply[res++] = '\n';
1025 reply[res] = '\0';
1026
1027 wpabuf_free(buf);
1028
1029 return res;
1030}
1031
1032
Dmitry Shmidt04949592012-07-19 12:16:46 -07001033static int wpa_supplicant_ctrl_iface_wps_nfc_token(
1034 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1035{
1036 int ndef;
1037 struct wpabuf *buf;
1038 int res;
1039
1040 if (os_strcmp(cmd, "WPS") == 0)
1041 ndef = 0;
1042 else if (os_strcmp(cmd, "NDEF") == 0)
1043 ndef = 1;
1044 else
1045 return -1;
1046
1047 buf = wpas_wps_nfc_token(wpa_s, ndef);
1048 if (buf == NULL)
1049 return -1;
1050
1051 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1052 wpabuf_len(buf));
1053 reply[res++] = '\n';
1054 reply[res] = '\0';
1055
1056 wpabuf_free(buf);
1057
1058 return res;
1059}
1060
1061
1062static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
1063 struct wpa_supplicant *wpa_s, char *pos)
1064{
1065 size_t len;
1066 struct wpabuf *buf;
1067 int ret;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001068 char *freq;
1069 int forced_freq = 0;
1070
1071 freq = strstr(pos, " freq=");
1072 if (freq) {
1073 *freq = '\0';
1074 freq += 6;
1075 forced_freq = atoi(freq);
1076 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001077
1078 len = os_strlen(pos);
1079 if (len & 0x01)
1080 return -1;
1081 len /= 2;
1082
1083 buf = wpabuf_alloc(len);
1084 if (buf == NULL)
1085 return -1;
1086 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
1087 wpabuf_free(buf);
1088 return -1;
1089 }
1090
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001091 ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001092 wpabuf_free(buf);
1093
1094 return ret;
1095}
1096
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001097
1098static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001099 char *reply, size_t max_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001100 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001101{
1102 struct wpabuf *buf;
1103 int res;
1104
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001105 buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001106 if (buf == NULL)
1107 return -1;
1108
1109 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1110 wpabuf_len(buf));
1111 reply[res++] = '\n';
1112 reply[res] = '\0';
1113
1114 wpabuf_free(buf);
1115
1116 return res;
1117}
1118
1119
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001120#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001121static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
1122 char *reply, size_t max_len,
1123 int ndef)
1124{
1125 struct wpabuf *buf;
1126 int res;
1127
1128 buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
1129 if (buf == NULL) {
1130 wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
1131 return -1;
1132 }
1133
1134 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1135 wpabuf_len(buf));
1136 reply[res++] = '\n';
1137 reply[res] = '\0';
1138
1139 wpabuf_free(buf);
1140
1141 return res;
1142}
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001143#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001144
1145
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001146static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
1147 char *cmd, char *reply,
1148 size_t max_len)
1149{
1150 char *pos;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001151 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001152
1153 pos = os_strchr(cmd, ' ');
1154 if (pos == NULL)
1155 return -1;
1156 *pos++ = '\0';
1157
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001158 if (os_strcmp(cmd, "WPS") == 0)
1159 ndef = 0;
1160 else if (os_strcmp(cmd, "NDEF") == 0)
1161 ndef = 1;
1162 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001163 return -1;
1164
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001165 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001166 if (!ndef)
1167 return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001168 return wpas_ctrl_nfc_get_handover_req_wps(
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001169 wpa_s, reply, max_len, ndef);
1170 }
1171
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001172#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001173 if (os_strcmp(pos, "P2P-CR") == 0) {
1174 return wpas_ctrl_nfc_get_handover_req_p2p(
1175 wpa_s, reply, max_len, ndef);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001176 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001177#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001178
1179 return -1;
1180}
1181
1182
1183static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001184 char *reply, size_t max_len,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001185 int ndef, int cr, char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001186{
1187 struct wpabuf *buf;
1188 int res;
1189
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001190 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001191 if (buf == NULL)
1192 return -1;
1193
1194 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1195 wpabuf_len(buf));
1196 reply[res++] = '\n';
1197 reply[res] = '\0';
1198
1199 wpabuf_free(buf);
1200
1201 return res;
1202}
1203
1204
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001205#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001206static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1207 char *reply, size_t max_len,
1208 int ndef, int tag)
1209{
1210 struct wpabuf *buf;
1211 int res;
1212
1213 buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1214 if (buf == NULL)
1215 return -1;
1216
1217 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1218 wpabuf_len(buf));
1219 reply[res++] = '\n';
1220 reply[res] = '\0';
1221
1222 wpabuf_free(buf);
1223
1224 return res;
1225}
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001226#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001227
1228
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001229static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1230 char *cmd, char *reply,
1231 size_t max_len)
1232{
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001233 char *pos, *pos2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001234 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001235
1236 pos = os_strchr(cmd, ' ');
1237 if (pos == NULL)
1238 return -1;
1239 *pos++ = '\0';
1240
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001241 if (os_strcmp(cmd, "WPS") == 0)
1242 ndef = 0;
1243 else if (os_strcmp(cmd, "NDEF") == 0)
1244 ndef = 1;
1245 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001246 return -1;
1247
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001248 pos2 = os_strchr(pos, ' ');
1249 if (pos2)
1250 *pos2++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001251 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001252 if (!ndef)
1253 return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001254 return wpas_ctrl_nfc_get_handover_sel_wps(
1255 wpa_s, reply, max_len, ndef,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001256 os_strcmp(pos, "WPS-CR") == 0, pos2);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001257 }
1258
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001259#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001260 if (os_strcmp(pos, "P2P-CR") == 0) {
1261 return wpas_ctrl_nfc_get_handover_sel_p2p(
1262 wpa_s, reply, max_len, ndef, 0);
1263 }
1264
1265 if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1266 return wpas_ctrl_nfc_get_handover_sel_p2p(
1267 wpa_s, reply, max_len, ndef, 1);
1268 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001269#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001270
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001271 return -1;
1272}
1273
1274
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001275static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1276 char *cmd)
1277{
1278 size_t len;
1279 struct wpabuf *req, *sel;
1280 int ret;
1281 char *pos, *role, *type, *pos2;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001282#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001283 char *freq;
1284 int forced_freq = 0;
1285
1286 freq = strstr(cmd, " freq=");
1287 if (freq) {
1288 *freq = '\0';
1289 freq += 6;
1290 forced_freq = atoi(freq);
1291 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001292#endif /* CONFIG_P2P */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001293
1294 role = cmd;
1295 pos = os_strchr(role, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001296 if (pos == NULL) {
1297 wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001298 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001299 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001300 *pos++ = '\0';
1301
1302 type = pos;
1303 pos = os_strchr(type, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001304 if (pos == NULL) {
1305 wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001306 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001307 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001308 *pos++ = '\0';
1309
1310 pos2 = os_strchr(pos, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001311 if (pos2 == NULL) {
1312 wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001313 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001314 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001315 *pos2++ = '\0';
1316
1317 len = os_strlen(pos);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001318 if (len & 0x01) {
1319 wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001320 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001321 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001322 len /= 2;
1323
1324 req = wpabuf_alloc(len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001325 if (req == NULL) {
1326 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001327 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001328 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001329 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001330 wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001331 wpabuf_free(req);
1332 return -1;
1333 }
1334
1335 len = os_strlen(pos2);
1336 if (len & 0x01) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001337 wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001338 wpabuf_free(req);
1339 return -1;
1340 }
1341 len /= 2;
1342
1343 sel = wpabuf_alloc(len);
1344 if (sel == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001345 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001346 wpabuf_free(req);
1347 return -1;
1348 }
1349 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001350 wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001351 wpabuf_free(req);
1352 wpabuf_free(sel);
1353 return -1;
1354 }
1355
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001356 wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1357 role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1358
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001359 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1360 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001361#ifdef CONFIG_AP
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001362 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1363 {
1364 ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1365 if (ret < 0)
1366 ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001367#endif /* CONFIG_AP */
1368#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001369 } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1370 {
1371 ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
1372 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1373 {
1374 ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1375 forced_freq);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001376#endif /* CONFIG_P2P */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001377 } else {
1378 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1379 "reported: role=%s type=%s", role, type);
1380 ret = -1;
1381 }
1382 wpabuf_free(req);
1383 wpabuf_free(sel);
1384
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001385 if (ret)
1386 wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1387
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001388 return ret;
1389}
1390
Dmitry Shmidt04949592012-07-19 12:16:46 -07001391#endif /* CONFIG_WPS_NFC */
1392
1393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1395 char *cmd)
1396{
1397 u8 bssid[ETH_ALEN];
1398 char *pin;
1399 char *new_ssid;
1400 char *new_auth;
1401 char *new_encr;
1402 char *new_key;
1403 struct wps_new_ap_settings ap;
1404
1405 pin = os_strchr(cmd, ' ');
1406 if (pin == NULL)
1407 return -1;
1408 *pin++ = '\0';
1409
1410 if (hwaddr_aton(cmd, bssid)) {
1411 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1412 cmd);
1413 return -1;
1414 }
1415
1416 new_ssid = os_strchr(pin, ' ');
1417 if (new_ssid == NULL)
1418 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1419 *new_ssid++ = '\0';
1420
1421 new_auth = os_strchr(new_ssid, ' ');
1422 if (new_auth == NULL)
1423 return -1;
1424 *new_auth++ = '\0';
1425
1426 new_encr = os_strchr(new_auth, ' ');
1427 if (new_encr == NULL)
1428 return -1;
1429 *new_encr++ = '\0';
1430
1431 new_key = os_strchr(new_encr, ' ');
1432 if (new_key == NULL)
1433 return -1;
1434 *new_key++ = '\0';
1435
1436 os_memset(&ap, 0, sizeof(ap));
1437 ap.ssid_hex = new_ssid;
1438 ap.auth = new_auth;
1439 ap.encr = new_encr;
1440 ap.key_hex = new_key;
1441 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1442}
1443
1444
1445#ifdef CONFIG_AP
1446static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1447 char *cmd, char *buf,
1448 size_t buflen)
1449{
1450 int timeout = 300;
1451 char *pos;
1452 const char *pin_txt;
1453
1454 if (!wpa_s->ap_iface)
1455 return -1;
1456
1457 pos = os_strchr(cmd, ' ');
1458 if (pos)
1459 *pos++ = '\0';
1460
1461 if (os_strcmp(cmd, "disable") == 0) {
1462 wpas_wps_ap_pin_disable(wpa_s);
1463 return os_snprintf(buf, buflen, "OK\n");
1464 }
1465
1466 if (os_strcmp(cmd, "random") == 0) {
1467 if (pos)
1468 timeout = atoi(pos);
1469 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1470 if (pin_txt == NULL)
1471 return -1;
1472 return os_snprintf(buf, buflen, "%s", pin_txt);
1473 }
1474
1475 if (os_strcmp(cmd, "get") == 0) {
1476 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1477 if (pin_txt == NULL)
1478 return -1;
1479 return os_snprintf(buf, buflen, "%s", pin_txt);
1480 }
1481
1482 if (os_strcmp(cmd, "set") == 0) {
1483 char *pin;
1484 if (pos == NULL)
1485 return -1;
1486 pin = pos;
1487 pos = os_strchr(pos, ' ');
1488 if (pos) {
1489 *pos++ = '\0';
1490 timeout = atoi(pos);
1491 }
1492 if (os_strlen(pin) > buflen)
1493 return -1;
1494 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1495 return -1;
1496 return os_snprintf(buf, buflen, "%s", pin);
1497 }
1498
1499 return -1;
1500}
1501#endif /* CONFIG_AP */
1502
1503
1504#ifdef CONFIG_WPS_ER
1505static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1506 char *cmd)
1507{
1508 char *uuid = cmd, *pin, *pos;
1509 u8 addr_buf[ETH_ALEN], *addr = NULL;
1510 pin = os_strchr(uuid, ' ');
1511 if (pin == NULL)
1512 return -1;
1513 *pin++ = '\0';
1514 pos = os_strchr(pin, ' ');
1515 if (pos) {
1516 *pos++ = '\0';
1517 if (hwaddr_aton(pos, addr_buf) == 0)
1518 addr = addr_buf;
1519 }
1520 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1521}
1522
1523
1524static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1525 char *cmd)
1526{
1527 char *uuid = cmd, *pin;
1528 pin = os_strchr(uuid, ' ');
1529 if (pin == NULL)
1530 return -1;
1531 *pin++ = '\0';
1532 return wpas_wps_er_learn(wpa_s, uuid, pin);
1533}
1534
1535
1536static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1537 struct wpa_supplicant *wpa_s, char *cmd)
1538{
1539 char *uuid = cmd, *id;
1540 id = os_strchr(uuid, ' ');
1541 if (id == NULL)
1542 return -1;
1543 *id++ = '\0';
1544 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1545}
1546
1547
1548static int wpa_supplicant_ctrl_iface_wps_er_config(
1549 struct wpa_supplicant *wpa_s, char *cmd)
1550{
1551 char *pin;
1552 char *new_ssid;
1553 char *new_auth;
1554 char *new_encr;
1555 char *new_key;
1556 struct wps_new_ap_settings ap;
1557
1558 pin = os_strchr(cmd, ' ');
1559 if (pin == NULL)
1560 return -1;
1561 *pin++ = '\0';
1562
1563 new_ssid = os_strchr(pin, ' ');
1564 if (new_ssid == NULL)
1565 return -1;
1566 *new_ssid++ = '\0';
1567
1568 new_auth = os_strchr(new_ssid, ' ');
1569 if (new_auth == NULL)
1570 return -1;
1571 *new_auth++ = '\0';
1572
1573 new_encr = os_strchr(new_auth, ' ');
1574 if (new_encr == NULL)
1575 return -1;
1576 *new_encr++ = '\0';
1577
1578 new_key = os_strchr(new_encr, ' ');
1579 if (new_key == NULL)
1580 return -1;
1581 *new_key++ = '\0';
1582
1583 os_memset(&ap, 0, sizeof(ap));
1584 ap.ssid_hex = new_ssid;
1585 ap.auth = new_auth;
1586 ap.encr = new_encr;
1587 ap.key_hex = new_key;
1588 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1589}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001590
1591
1592#ifdef CONFIG_WPS_NFC
1593static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1594 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1595{
1596 int ndef;
1597 struct wpabuf *buf;
1598 int res;
1599 char *uuid;
1600
1601 uuid = os_strchr(cmd, ' ');
1602 if (uuid == NULL)
1603 return -1;
1604 *uuid++ = '\0';
1605
1606 if (os_strcmp(cmd, "WPS") == 0)
1607 ndef = 0;
1608 else if (os_strcmp(cmd, "NDEF") == 0)
1609 ndef = 1;
1610 else
1611 return -1;
1612
1613 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1614 if (buf == NULL)
1615 return -1;
1616
1617 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1618 wpabuf_len(buf));
1619 reply[res++] = '\n';
1620 reply[res] = '\0';
1621
1622 wpabuf_free(buf);
1623
1624 return res;
1625}
1626#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627#endif /* CONFIG_WPS_ER */
1628
1629#endif /* CONFIG_WPS */
1630
1631
1632#ifdef CONFIG_IBSS_RSN
1633static int wpa_supplicant_ctrl_iface_ibss_rsn(
1634 struct wpa_supplicant *wpa_s, char *addr)
1635{
1636 u8 peer[ETH_ALEN];
1637
1638 if (hwaddr_aton(addr, peer)) {
1639 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1640 "address '%s'", addr);
1641 return -1;
1642 }
1643
1644 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1645 MAC2STR(peer));
1646
1647 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1648}
1649#endif /* CONFIG_IBSS_RSN */
1650
1651
1652static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1653 char *rsp)
1654{
1655#ifdef IEEE8021X_EAPOL
1656 char *pos, *id_pos;
1657 int id;
1658 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659
1660 pos = os_strchr(rsp, '-');
1661 if (pos == NULL)
1662 return -1;
1663 *pos++ = '\0';
1664 id_pos = pos;
1665 pos = os_strchr(pos, ':');
1666 if (pos == NULL)
1667 return -1;
1668 *pos++ = '\0';
1669 id = atoi(id_pos);
1670 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1671 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1672 (u8 *) pos, os_strlen(pos));
1673
1674 ssid = wpa_config_get_network(wpa_s->conf, id);
1675 if (ssid == NULL) {
1676 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1677 "to update", id);
1678 return -1;
1679 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001680
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001681 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1682 pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683#else /* IEEE8021X_EAPOL */
1684 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1685 return -1;
1686#endif /* IEEE8021X_EAPOL */
1687}
1688
1689
1690static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1691 const char *params,
1692 char *buf, size_t buflen)
1693{
1694 char *pos, *end, tmp[30];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001695 int res, verbose, wps, ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001696#ifdef CONFIG_HS20
1697 const u8 *hs20;
1698#endif /* CONFIG_HS20 */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001699
Dmitry Shmidt56052862013-10-04 10:23:25 -07001700 if (os_strcmp(params, "-DRIVER") == 0)
1701 return wpa_drv_status(wpa_s, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 verbose = os_strcmp(params, "-VERBOSE") == 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001703 wps = os_strcmp(params, "-WPS") == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704 pos = buf;
1705 end = buf + buflen;
1706 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1707 struct wpa_ssid *ssid = wpa_s->current_ssid;
1708 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1709 MAC2STR(wpa_s->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001710 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 return pos - buf;
1712 pos += ret;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001713 ret = os_snprintf(pos, end - pos, "freq=%u\n",
1714 wpa_s->assoc_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001715 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001716 return pos - buf;
1717 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718 if (ssid) {
1719 u8 *_ssid = ssid->ssid;
1720 size_t ssid_len = ssid->ssid_len;
1721 u8 ssid_buf[MAX_SSID_LEN];
1722 if (ssid_len == 0) {
1723 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1724 if (_res < 0)
1725 ssid_len = 0;
1726 else
1727 ssid_len = _res;
1728 _ssid = ssid_buf;
1729 }
1730 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1731 wpa_ssid_txt(_ssid, ssid_len),
1732 ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001733 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734 return pos - buf;
1735 pos += ret;
1736
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001737 if (wps && ssid->passphrase &&
1738 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1739 (ssid->mode == WPAS_MODE_AP ||
1740 ssid->mode == WPAS_MODE_P2P_GO)) {
1741 ret = os_snprintf(pos, end - pos,
1742 "passphrase=%s\n",
1743 ssid->passphrase);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001744 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001745 return pos - buf;
1746 pos += ret;
1747 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 if (ssid->id_str) {
1749 ret = os_snprintf(pos, end - pos,
1750 "id_str=%s\n",
1751 ssid->id_str);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001752 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001753 return pos - buf;
1754 pos += ret;
1755 }
1756
1757 switch (ssid->mode) {
1758 case WPAS_MODE_INFRA:
1759 ret = os_snprintf(pos, end - pos,
1760 "mode=station\n");
1761 break;
1762 case WPAS_MODE_IBSS:
1763 ret = os_snprintf(pos, end - pos,
1764 "mode=IBSS\n");
1765 break;
1766 case WPAS_MODE_AP:
1767 ret = os_snprintf(pos, end - pos,
1768 "mode=AP\n");
1769 break;
1770 case WPAS_MODE_P2P_GO:
1771 ret = os_snprintf(pos, end - pos,
1772 "mode=P2P GO\n");
1773 break;
1774 case WPAS_MODE_P2P_GROUP_FORMATION:
1775 ret = os_snprintf(pos, end - pos,
1776 "mode=P2P GO - group "
1777 "formation\n");
1778 break;
1779 default:
1780 ret = 0;
1781 break;
1782 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001783 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001784 return pos - buf;
1785 pos += ret;
1786 }
1787
1788#ifdef CONFIG_AP
1789 if (wpa_s->ap_iface) {
1790 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1791 end - pos,
1792 verbose);
1793 } else
1794#endif /* CONFIG_AP */
1795 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1796 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001797#ifdef CONFIG_SAE
1798 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001799#ifdef CONFIG_AP
1800 !wpa_s->ap_iface &&
1801#endif /* CONFIG_AP */
1802 wpa_s->sme.sae.state == SAE_ACCEPTED) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001803 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1804 wpa_s->sme.sae.group);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001805 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001806 return pos - buf;
1807 pos += ret;
1808 }
1809#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1811 wpa_supplicant_state_txt(wpa_s->wpa_state));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001812 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813 return pos - buf;
1814 pos += ret;
1815
1816 if (wpa_s->l2 &&
1817 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1818 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001819 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820 return pos - buf;
1821 pos += ret;
1822 }
1823
1824#ifdef CONFIG_P2P
1825 if (wpa_s->global->p2p) {
1826 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1827 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001828 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829 return pos - buf;
1830 pos += ret;
1831 }
1832#endif /* CONFIG_P2P */
1833
1834 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1835 MAC2STR(wpa_s->own_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001836 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837 return pos - buf;
1838 pos += ret;
1839
Dmitry Shmidt04949592012-07-19 12:16:46 -07001840#ifdef CONFIG_HS20
1841 if (wpa_s->current_bss &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001842 (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1843 HS20_IE_VENDOR_TYPE)) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001844 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1845 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001846 int release = 1;
1847 if (hs20[1] >= 5) {
1848 u8 rel_num = (hs20[6] & 0xf0) >> 4;
1849 release = rel_num + 1;
1850 }
1851 ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001852 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001853 return pos - buf;
1854 pos += ret;
1855 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001856
1857 if (wpa_s->current_ssid) {
1858 struct wpa_cred *cred;
1859 char *type;
1860
1861 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001862 size_t i;
1863
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001864 if (wpa_s->current_ssid->parent_cred != cred)
1865 continue;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001866
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001867 if (cred->provisioning_sp) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001868 ret = os_snprintf(pos, end - pos,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001869 "provisioning_sp=%s\n",
1870 cred->provisioning_sp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001871 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001872 return pos - buf;
1873 pos += ret;
1874 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001875
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001876 if (!cred->domain)
1877 goto no_domain;
1878
1879 i = 0;
1880 if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
1881 struct wpabuf *names =
1882 wpa_s->current_bss->anqp->domain_name;
1883 for (i = 0; names && i < cred->num_domain; i++)
1884 {
1885 if (domain_name_list_contains(
1886 names, cred->domain[i], 1))
1887 break;
1888 }
1889 if (i == cred->num_domain)
1890 i = 0; /* show first entry by default */
1891 }
1892 ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
1893 cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001894 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001895 return pos - buf;
1896 pos += ret;
1897
1898 no_domain:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001899 if (wpa_s->current_bss == NULL ||
1900 wpa_s->current_bss->anqp == NULL)
1901 res = -1;
1902 else
1903 res = interworking_home_sp_cred(
1904 wpa_s, cred,
1905 wpa_s->current_bss->anqp->domain_name);
1906 if (res > 0)
1907 type = "home";
1908 else if (res == 0)
1909 type = "roaming";
1910 else
1911 type = "unknown";
1912
1913 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001914 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001915 return pos - buf;
1916 pos += ret;
1917
1918 break;
1919 }
1920 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001921#endif /* CONFIG_HS20 */
1922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1924 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1925 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1926 verbose);
1927 if (res >= 0)
1928 pos += res;
1929 }
1930
1931 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1932 if (res >= 0)
1933 pos += res;
1934
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001935#ifdef CONFIG_WPS
1936 {
1937 char uuid_str[100];
1938 uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
1939 ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001940 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001941 return pos - buf;
1942 pos += ret;
1943 }
1944#endif /* CONFIG_WPS */
1945
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001946#ifdef ANDROID
vandwalleffc70182014-09-11 11:40:14 -07001947 /*
1948 * Allow using the STATUS command with default behavior, say for debug,
1949 * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
1950 * events with STATUS-NO_EVENTS.
1951 */
1952 if (os_strcmp(params, "-NO_EVENTS")) {
1953 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
1954 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
1955 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
1956 wpa_s->wpa_state,
1957 MAC2STR(wpa_s->bssid),
1958 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1959 wpa_ssid_txt(wpa_s->current_ssid->ssid,
1960 wpa_s->current_ssid->ssid_len) : "");
1961 if (wpa_s->wpa_state == WPA_COMPLETED) {
1962 struct wpa_ssid *ssid = wpa_s->current_ssid;
1963 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
1964 "- connection to " MACSTR
1965 " completed %s [id=%d id_str=%s]",
1966 MAC2STR(wpa_s->bssid), "(auth)",
1967 ssid ? ssid->id : -1,
1968 ssid && ssid->id_str ? ssid->id_str : "");
1969 }
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001970 }
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001971#endif /* ANDROID */
1972
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001973 return pos - buf;
1974}
1975
1976
1977static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1978 char *cmd)
1979{
1980 char *pos;
1981 int id;
1982 struct wpa_ssid *ssid;
1983 u8 bssid[ETH_ALEN];
1984
1985 /* cmd: "<network id> <BSSID>" */
1986 pos = os_strchr(cmd, ' ');
1987 if (pos == NULL)
1988 return -1;
1989 *pos++ = '\0';
1990 id = atoi(cmd);
1991 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1992 if (hwaddr_aton(pos, bssid)) {
1993 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1994 return -1;
1995 }
1996
1997 ssid = wpa_config_get_network(wpa_s->conf, id);
1998 if (ssid == NULL) {
1999 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2000 "to update", id);
2001 return -1;
2002 }
2003
2004 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
2005 ssid->bssid_set = !is_zero_ether_addr(bssid);
2006
2007 return 0;
2008}
2009
2010
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002011static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002012 char *cmd, char *buf,
2013 size_t buflen)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002014{
2015 u8 bssid[ETH_ALEN];
2016 struct wpa_blacklist *e;
2017 char *pos, *end;
2018 int ret;
2019
2020 /* cmd: "BLACKLIST [<BSSID>]" */
2021 if (*cmd == '\0') {
2022 pos = buf;
2023 end = buf + buflen;
2024 e = wpa_s->blacklist;
2025 while (e) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002026 ret = os_snprintf(pos, end - pos, MACSTR "\n",
2027 MAC2STR(e->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002028 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002029 return pos - buf;
2030 pos += ret;
2031 e = e->next;
2032 }
2033 return pos - buf;
2034 }
2035
2036 cmd++;
2037 if (os_strncmp(cmd, "clear", 5) == 0) {
2038 wpa_blacklist_clear(wpa_s);
2039 os_memcpy(buf, "OK\n", 3);
2040 return 3;
2041 }
2042
2043 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
2044 if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002045 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002046 return -1;
2047 }
2048
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002049 /*
2050 * Add the BSSID twice, so its count will be 2, causing it to be
2051 * skipped when processing scan results.
2052 */
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002053 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002054 if (ret < 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002055 return -1;
2056 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002057 if (ret < 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002058 return -1;
2059 os_memcpy(buf, "OK\n", 3);
2060 return 3;
2061}
2062
2063
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002064static const char * debug_level_str(int level)
2065{
2066 switch (level) {
2067 case MSG_EXCESSIVE:
2068 return "EXCESSIVE";
2069 case MSG_MSGDUMP:
2070 return "MSGDUMP";
2071 case MSG_DEBUG:
2072 return "DEBUG";
2073 case MSG_INFO:
2074 return "INFO";
2075 case MSG_WARNING:
2076 return "WARNING";
2077 case MSG_ERROR:
2078 return "ERROR";
2079 default:
2080 return "?";
2081 }
2082}
2083
2084
2085static int str_to_debug_level(const char *s)
2086{
2087 if (os_strcasecmp(s, "EXCESSIVE") == 0)
2088 return MSG_EXCESSIVE;
2089 if (os_strcasecmp(s, "MSGDUMP") == 0)
2090 return MSG_MSGDUMP;
2091 if (os_strcasecmp(s, "DEBUG") == 0)
2092 return MSG_DEBUG;
2093 if (os_strcasecmp(s, "INFO") == 0)
2094 return MSG_INFO;
2095 if (os_strcasecmp(s, "WARNING") == 0)
2096 return MSG_WARNING;
2097 if (os_strcasecmp(s, "ERROR") == 0)
2098 return MSG_ERROR;
2099 return -1;
2100}
2101
2102
2103static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
2104 char *cmd, char *buf,
2105 size_t buflen)
2106{
2107 char *pos, *end, *stamp;
2108 int ret;
2109
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002110 /* cmd: "LOG_LEVEL [<level>]" */
2111 if (*cmd == '\0') {
2112 pos = buf;
2113 end = buf + buflen;
2114 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2115 "Timestamp: %d\n",
2116 debug_level_str(wpa_debug_level),
2117 wpa_debug_timestamp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002118 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002119 ret = 0;
2120
2121 return ret;
2122 }
2123
2124 while (*cmd == ' ')
2125 cmd++;
2126
2127 stamp = os_strchr(cmd, ' ');
2128 if (stamp) {
2129 *stamp++ = '\0';
2130 while (*stamp == ' ') {
2131 stamp++;
2132 }
2133 }
2134
2135 if (cmd && os_strlen(cmd)) {
2136 int level = str_to_debug_level(cmd);
2137 if (level < 0)
2138 return -1;
2139 wpa_debug_level = level;
2140 }
2141
2142 if (stamp && os_strlen(stamp))
2143 wpa_debug_timestamp = atoi(stamp);
2144
2145 os_memcpy(buf, "OK\n", 3);
2146 return 3;
2147}
2148
2149
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150static int wpa_supplicant_ctrl_iface_list_networks(
Vinit Deshpandeda134e92014-12-02 10:59:29 -08002151 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152{
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002153 char *pos, *end, *prev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154 struct wpa_ssid *ssid;
2155 int ret;
2156
2157 pos = buf;
2158 end = buf + buflen;
2159 ret = os_snprintf(pos, end - pos,
2160 "network id / ssid / bssid / flags\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002161 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162 return pos - buf;
2163 pos += ret;
2164
2165 ssid = wpa_s->conf->ssid;
Vinit Deshpandeda134e92014-12-02 10:59:29 -08002166
2167 /* skip over ssids until we find next one */
2168 if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
2169 int last_id = atoi(cmd + 8);
2170 if (last_id != -1) {
2171 while (ssid != NULL && ssid->id <= last_id) {
2172 ssid = ssid->next;
2173 }
2174 }
2175 }
2176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177 while (ssid) {
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002178 prev = pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179 ret = os_snprintf(pos, end - pos, "%d\t%s",
2180 ssid->id,
2181 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002182 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002183 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184 pos += ret;
2185 if (ssid->bssid_set) {
2186 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2187 MAC2STR(ssid->bssid));
2188 } else {
2189 ret = os_snprintf(pos, end - pos, "\tany");
2190 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002191 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002192 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002194 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002195 ssid == wpa_s->current_ssid ?
2196 "[CURRENT]" : "",
2197 ssid->disabled ? "[DISABLED]" : "",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002198 ssid->disabled_until.sec ?
2199 "[TEMP-DISABLED]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2201 "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002202 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002203 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204 pos += ret;
2205 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002206 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002207 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208 pos += ret;
2209
2210 ssid = ssid->next;
2211 }
2212
2213 return pos - buf;
2214}
2215
2216
2217static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2218{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002219 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002220 ret = os_snprintf(pos, end - pos, "-");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002221 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222 return pos;
2223 pos += ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002224 ret = wpa_write_ciphers(pos, end, cipher, "+");
2225 if (ret < 0)
2226 return pos;
2227 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002228 return pos;
2229}
2230
2231
2232static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2233 const u8 *ie, size_t ie_len)
2234{
2235 struct wpa_ie_data data;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002236 char *start;
2237 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238
2239 ret = os_snprintf(pos, end - pos, "[%s-", proto);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002240 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241 return pos;
2242 pos += ret;
2243
2244 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2245 ret = os_snprintf(pos, end - pos, "?]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002246 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002247 return pos;
2248 pos += ret;
2249 return pos;
2250 }
2251
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002252 start = pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002254 ret = os_snprintf(pos, end - pos, "%sEAP",
2255 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002256 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257 return pos;
2258 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259 }
2260 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002261 ret = os_snprintf(pos, end - pos, "%sPSK",
2262 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002263 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002264 return pos;
2265 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266 }
2267 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002268 ret = os_snprintf(pos, end - pos, "%sNone",
2269 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002270 if (os_snprintf_error(end - pos, ret))
2271 return pos;
2272 pos += ret;
2273 }
2274 if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
2275 ret = os_snprintf(pos, end - pos, "%sSAE",
2276 pos == start ? "" : "+");
2277 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002278 return pos;
2279 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002280 }
2281#ifdef CONFIG_IEEE80211R
2282 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2283 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002284 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002285 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002286 return pos;
2287 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288 }
2289 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2290 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002291 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002292 if (os_snprintf_error(end - pos, ret))
2293 return pos;
2294 pos += ret;
2295 }
2296 if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2297 ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2298 pos == start ? "" : "+");
2299 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002300 return pos;
2301 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002302 }
2303#endif /* CONFIG_IEEE80211R */
2304#ifdef CONFIG_IEEE80211W
2305 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2306 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002307 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002308 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002309 return pos;
2310 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311 }
2312 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2313 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002314 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002315 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316 return pos;
2317 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318 }
2319#endif /* CONFIG_IEEE80211W */
2320
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002321 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2322 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
2323 pos == start ? "" : "+");
2324 if (os_snprintf_error(end - pos, ret))
2325 return pos;
2326 pos += ret;
2327 }
2328
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2330
2331 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2332 ret = os_snprintf(pos, end - pos, "-preauth");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002333 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334 return pos;
2335 pos += ret;
2336 }
2337
2338 ret = os_snprintf(pos, end - pos, "]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002339 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340 return pos;
2341 pos += ret;
2342
2343 return pos;
2344}
2345
2346
2347#ifdef CONFIG_WPS
2348static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2349 char *pos, char *end,
2350 struct wpabuf *wps_ie)
2351{
2352 int ret;
2353 const char *txt;
2354
2355 if (wps_ie == NULL)
2356 return pos;
2357 if (wps_is_selected_pbc_registrar(wps_ie))
2358 txt = "[WPS-PBC]";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002359 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2360 txt = "[WPS-AUTH]";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361 else if (wps_is_selected_pin_registrar(wps_ie))
2362 txt = "[WPS-PIN]";
2363 else
2364 txt = "[WPS]";
2365
2366 ret = os_snprintf(pos, end - pos, "%s", txt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002367 if (!os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368 pos += ret;
2369 wpabuf_free(wps_ie);
2370 return pos;
2371}
2372#endif /* CONFIG_WPS */
2373
2374
2375static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2376 char *pos, char *end,
2377 const struct wpa_bss *bss)
2378{
2379#ifdef CONFIG_WPS
2380 struct wpabuf *wps_ie;
2381 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2382 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2383#else /* CONFIG_WPS */
2384 return pos;
2385#endif /* CONFIG_WPS */
2386}
2387
2388
2389/* Format one result on one text line into a buffer. */
2390static int wpa_supplicant_ctrl_iface_scan_result(
2391 struct wpa_supplicant *wpa_s,
2392 const struct wpa_bss *bss, char *buf, size_t buflen)
2393{
2394 char *pos, *end;
2395 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002396 const u8 *ie, *ie2, *p2p, *mesh;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002397
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002398 mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002399 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07002400 if (!p2p)
2401 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002402 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2403 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2404 0)
2405 return 0; /* Do not show P2P listen discovery results here */
2406
2407 pos = buf;
2408 end = buf + buflen;
2409
2410 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2411 MAC2STR(bss->bssid), bss->freq, bss->level);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002412 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413 return -1;
2414 pos += ret;
2415 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2416 if (ie)
2417 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2418 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002419 if (ie2) {
2420 pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
2421 ie2, 2 + ie2[1]);
2422 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2424 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2425 ret = os_snprintf(pos, end - pos, "[WEP]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002426 if (os_snprintf_error(end - pos, ret))
2427 return -1;
2428 pos += ret;
2429 }
2430 if (mesh) {
2431 ret = os_snprintf(pos, end - pos, "[MESH]");
2432 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002433 return -1;
2434 pos += ret;
2435 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002436 if (bss_is_dmg(bss)) {
2437 const char *s;
2438 ret = os_snprintf(pos, end - pos, "[DMG]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002439 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440 return -1;
2441 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002442 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
2443 case IEEE80211_CAP_DMG_IBSS:
2444 s = "[IBSS]";
2445 break;
2446 case IEEE80211_CAP_DMG_AP:
2447 s = "[ESS]";
2448 break;
2449 case IEEE80211_CAP_DMG_PBSS:
2450 s = "[PBSS]";
2451 break;
2452 default:
2453 s = "";
2454 break;
2455 }
2456 ret = os_snprintf(pos, end - pos, "%s", s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002457 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458 return -1;
2459 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002460 } else {
2461 if (bss->caps & IEEE80211_CAP_IBSS) {
2462 ret = os_snprintf(pos, end - pos, "[IBSS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002463 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002464 return -1;
2465 pos += ret;
2466 }
2467 if (bss->caps & IEEE80211_CAP_ESS) {
2468 ret = os_snprintf(pos, end - pos, "[ESS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002469 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002470 return -1;
2471 pos += ret;
2472 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473 }
2474 if (p2p) {
2475 ret = os_snprintf(pos, end - pos, "[P2P]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002476 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477 return -1;
2478 pos += ret;
2479 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002480#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002481 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002482 ret = os_snprintf(pos, end - pos, "[HS20]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002483 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07002484 return -1;
2485 pos += ret;
2486 }
2487#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488
2489 ret = os_snprintf(pos, end - pos, "\t%s",
2490 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002491 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492 return -1;
2493 pos += ret;
2494
2495 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002496 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497 return -1;
2498 pos += ret;
2499
2500 return pos - buf;
2501}
2502
2503
2504static int wpa_supplicant_ctrl_iface_scan_results(
2505 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2506{
2507 char *pos, *end;
2508 struct wpa_bss *bss;
2509 int ret;
2510
2511 pos = buf;
2512 end = buf + buflen;
2513 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2514 "flags / ssid\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002515 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516 return pos - buf;
2517 pos += ret;
2518
2519 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2520 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2521 end - pos);
2522 if (ret < 0 || ret >= end - pos)
2523 return pos - buf;
2524 pos += ret;
2525 }
2526
2527 return pos - buf;
2528}
2529
2530
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002531#ifdef CONFIG_MESH
2532
2533static int wpa_supplicant_ctrl_iface_mesh_interface_add(
2534 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
2535{
2536 char *pos, ifname[IFNAMSIZ + 1];
2537
2538 ifname[0] = '\0';
2539
2540 pos = os_strstr(cmd, "ifname=");
2541 if (pos) {
2542 pos += 7;
2543 os_strlcpy(ifname, pos, sizeof(ifname));
2544 }
2545
2546 if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
2547 return -1;
2548
2549 os_strlcpy(reply, ifname, max_len);
2550 return os_strlen(ifname);
2551}
2552
2553
2554static int wpa_supplicant_ctrl_iface_mesh_group_add(
2555 struct wpa_supplicant *wpa_s, char *cmd)
2556{
2557 int id;
2558 struct wpa_ssid *ssid;
2559
2560 id = atoi(cmd);
2561 wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
2562
2563 ssid = wpa_config_get_network(wpa_s->conf, id);
2564 if (ssid == NULL) {
2565 wpa_printf(MSG_DEBUG,
2566 "CTRL_IFACE: Could not find network id=%d", id);
2567 return -1;
2568 }
2569 if (ssid->mode != WPAS_MODE_MESH) {
2570 wpa_printf(MSG_DEBUG,
2571 "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
2572 return -1;
2573 }
2574 if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
2575 ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
2576 wpa_printf(MSG_ERROR,
2577 "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
2578 return -1;
2579 }
2580
2581 /*
2582 * TODO: If necessary write our own group_add function,
2583 * for now we can reuse select_network
2584 */
2585 wpa_supplicant_select_network(wpa_s, ssid);
2586
2587 return 0;
2588}
2589
2590
2591static int wpa_supplicant_ctrl_iface_mesh_group_remove(
2592 struct wpa_supplicant *wpa_s, char *cmd)
2593{
2594 struct wpa_supplicant *orig;
2595 struct wpa_global *global;
2596 int found = 0;
2597
2598 wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
2599
2600 global = wpa_s->global;
2601 orig = wpa_s;
2602
2603 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2604 if (os_strcmp(wpa_s->ifname, cmd) == 0) {
2605 found = 1;
2606 break;
2607 }
2608 }
2609 if (!found) {
2610 wpa_printf(MSG_ERROR,
2611 "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
2612 cmd);
2613 return -1;
2614 }
2615 if (wpa_s->mesh_if_created && wpa_s == orig) {
2616 wpa_printf(MSG_ERROR,
2617 "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
2618 return -1;
2619 }
2620
2621 wpa_s->reassociate = 0;
2622 wpa_s->disconnected = 1;
2623 wpa_supplicant_cancel_sched_scan(wpa_s);
2624 wpa_supplicant_cancel_scan(wpa_s);
2625
2626 /*
2627 * TODO: If necessary write our own group_remove function,
2628 * for now we can reuse deauthenticate
2629 */
2630 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2631
2632 if (wpa_s->mesh_if_created)
2633 wpa_supplicant_remove_iface(global, wpa_s, 0);
2634
2635 return 0;
2636}
2637
2638#endif /* CONFIG_MESH */
2639
2640
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641static int wpa_supplicant_ctrl_iface_select_network(
2642 struct wpa_supplicant *wpa_s, char *cmd)
2643{
2644 int id;
2645 struct wpa_ssid *ssid;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002646 char *pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002647
2648 /* cmd: "<network id>" or "any" */
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002649 if (os_strncmp(cmd, "any", 3) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002650 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2651 ssid = NULL;
2652 } else {
2653 id = atoi(cmd);
2654 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2655
2656 ssid = wpa_config_get_network(wpa_s->conf, id);
2657 if (ssid == NULL) {
2658 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2659 "network id=%d", id);
2660 return -1;
2661 }
2662 if (ssid->disabled == 2) {
2663 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2664 "SELECT_NETWORK with persistent P2P group");
2665 return -1;
2666 }
2667 }
2668
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002669 pos = os_strstr(cmd, " freq=");
2670 if (pos) {
2671 int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
2672 if (freqs) {
2673 wpa_s->scan_req = MANUAL_SCAN_REQ;
2674 os_free(wpa_s->manual_scan_freqs);
2675 wpa_s->manual_scan_freqs = freqs;
2676 }
2677 }
2678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002679 wpa_supplicant_select_network(wpa_s, ssid);
2680
2681 return 0;
2682}
2683
2684
2685static int wpa_supplicant_ctrl_iface_enable_network(
2686 struct wpa_supplicant *wpa_s, char *cmd)
2687{
2688 int id;
2689 struct wpa_ssid *ssid;
2690
2691 /* cmd: "<network id>" or "all" */
2692 if (os_strcmp(cmd, "all") == 0) {
2693 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2694 ssid = NULL;
2695 } else {
2696 id = atoi(cmd);
2697 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2698
2699 ssid = wpa_config_get_network(wpa_s->conf, id);
2700 if (ssid == NULL) {
2701 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2702 "network id=%d", id);
2703 return -1;
2704 }
2705 if (ssid->disabled == 2) {
2706 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2707 "ENABLE_NETWORK with persistent P2P group");
2708 return -1;
2709 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002710
2711 if (os_strstr(cmd, " no-connect")) {
2712 ssid->disabled = 0;
2713 return 0;
2714 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715 }
2716 wpa_supplicant_enable_network(wpa_s, ssid);
2717
2718 return 0;
2719}
2720
2721
2722static int wpa_supplicant_ctrl_iface_disable_network(
2723 struct wpa_supplicant *wpa_s, char *cmd)
2724{
2725 int id;
2726 struct wpa_ssid *ssid;
2727
2728 /* cmd: "<network id>" or "all" */
2729 if (os_strcmp(cmd, "all") == 0) {
2730 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2731 ssid = NULL;
2732 } else {
2733 id = atoi(cmd);
2734 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2735
2736 ssid = wpa_config_get_network(wpa_s->conf, id);
2737 if (ssid == NULL) {
2738 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2739 "network id=%d", id);
2740 return -1;
2741 }
2742 if (ssid->disabled == 2) {
2743 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2744 "DISABLE_NETWORK with persistent P2P "
2745 "group");
2746 return -1;
2747 }
2748 }
2749 wpa_supplicant_disable_network(wpa_s, ssid);
2750
2751 return 0;
2752}
2753
2754
2755static int wpa_supplicant_ctrl_iface_add_network(
2756 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2757{
2758 struct wpa_ssid *ssid;
2759 int ret;
2760
2761 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2762
2763 ssid = wpa_config_add_network(wpa_s->conf);
2764 if (ssid == NULL)
2765 return -1;
2766
2767 wpas_notify_network_added(wpa_s, ssid);
2768
2769 ssid->disabled = 1;
2770 wpa_config_set_network_defaults(ssid);
2771
2772 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002773 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002774 return -1;
2775 return ret;
2776}
2777
2778
2779static int wpa_supplicant_ctrl_iface_remove_network(
2780 struct wpa_supplicant *wpa_s, char *cmd)
2781{
2782 int id;
2783 struct wpa_ssid *ssid;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002784 int was_disabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002785
2786 /* cmd: "<network id>" or "all" */
2787 if (os_strcmp(cmd, "all") == 0) {
2788 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002789 if (wpa_s->sched_scanning)
2790 wpa_supplicant_cancel_sched_scan(wpa_s);
2791
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002792 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002793 if (wpa_s->current_ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002794#ifdef CONFIG_SME
2795 wpa_s->sme.prev_bssid_set = 0;
2796#endif /* CONFIG_SME */
Jouni Malinen75ecf522011-06-27 15:19:46 -07002797 wpa_sm_set_config(wpa_s->wpa, NULL);
2798 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002799 wpa_supplicant_deauthenticate(
2800 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002801 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002802 ssid = wpa_s->conf->ssid;
2803 while (ssid) {
2804 struct wpa_ssid *remove_ssid = ssid;
2805 id = ssid->id;
2806 ssid = ssid->next;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002807 if (wpa_s->last_ssid == remove_ssid)
2808 wpa_s->last_ssid = NULL;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002809 wpas_notify_network_removed(wpa_s, remove_ssid);
2810 wpa_config_remove_network(wpa_s->conf, id);
2811 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812 return 0;
2813 }
2814
2815 id = atoi(cmd);
2816 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2817
2818 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002819 if (ssid)
2820 wpas_notify_network_removed(wpa_s, ssid);
Deepthi Gowria831d782012-09-03 11:55:38 +03002821 if (ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002822 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2823 "id=%d", id);
2824 return -1;
2825 }
2826
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002827 if (wpa_s->last_ssid == ssid)
2828 wpa_s->last_ssid = NULL;
2829
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002830 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002831#ifdef CONFIG_SME
2832 wpa_s->sme.prev_bssid_set = 0;
2833#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002835 * Invalidate the EAP session cache if the current or
2836 * previously used network is removed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002837 */
2838 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002839 }
2840
2841 if (ssid == wpa_s->current_ssid) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002842 wpa_sm_set_config(wpa_s->wpa, NULL);
2843 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002845 wpa_supplicant_deauthenticate(wpa_s,
2846 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002847 }
2848
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002849 was_disabled = ssid->disabled;
2850
Deepthi Gowria831d782012-09-03 11:55:38 +03002851 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2852 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2853 "network id=%d", id);
2854 return -1;
2855 }
2856
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002857 if (!was_disabled && wpa_s->sched_scanning) {
2858 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2859 "network from filters");
2860 wpa_supplicant_cancel_sched_scan(wpa_s);
2861 wpa_supplicant_req_scan(wpa_s, 0, 0);
2862 }
2863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864 return 0;
2865}
2866
2867
Dmitry Shmidt684785c2014-05-12 13:34:29 -07002868static int wpa_supplicant_ctrl_iface_update_network(
2869 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2870 char *name, char *value)
2871{
2872 if (wpa_config_set(ssid, name, value, 0) < 0) {
2873 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2874 "variable '%s'", name);
2875 return -1;
2876 }
2877
2878 if (os_strcmp(name, "bssid") != 0 &&
2879 os_strcmp(name, "priority") != 0)
2880 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
2881
2882 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
2883 /*
2884 * Invalidate the EAP session cache if anything in the current
2885 * or previously used configuration changes.
2886 */
2887 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2888 }
2889
2890 if ((os_strcmp(name, "psk") == 0 &&
2891 value[0] == '"' && ssid->ssid_len) ||
2892 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2893 wpa_config_update_psk(ssid);
2894 else if (os_strcmp(name, "priority") == 0)
2895 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002896 else if (os_strcmp(name, "no_auto_peer") == 0)
2897 ssid->no_auto_peer = atoi(value);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07002898
2899 return 0;
2900}
2901
2902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903static int wpa_supplicant_ctrl_iface_set_network(
2904 struct wpa_supplicant *wpa_s, char *cmd)
2905{
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002906 int id, ret, prev_bssid_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002907 struct wpa_ssid *ssid;
2908 char *name, *value;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002909 u8 prev_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910
2911 /* cmd: "<network id> <variable name> <value>" */
2912 name = os_strchr(cmd, ' ');
2913 if (name == NULL)
2914 return -1;
2915 *name++ = '\0';
2916
2917 value = os_strchr(name, ' ');
2918 if (value == NULL)
2919 return -1;
2920 *value++ = '\0';
2921
2922 id = atoi(cmd);
2923 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2924 id, name);
2925 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2926 (u8 *) value, os_strlen(value));
2927
2928 ssid = wpa_config_get_network(wpa_s->conf, id);
2929 if (ssid == NULL) {
2930 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2931 "id=%d", id);
2932 return -1;
2933 }
2934
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002935 prev_bssid_set = ssid->bssid_set;
2936 os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
2937 ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
2938 value);
2939 if (ret == 0 &&
2940 (ssid->bssid_set != prev_bssid_set ||
2941 os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
2942 wpas_notify_network_bssid_set_changed(wpa_s, ssid);
2943 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002944}
2945
2946
2947static int wpa_supplicant_ctrl_iface_get_network(
2948 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2949{
2950 int id;
2951 size_t res;
2952 struct wpa_ssid *ssid;
2953 char *name, *value;
2954
2955 /* cmd: "<network id> <variable name>" */
2956 name = os_strchr(cmd, ' ');
2957 if (name == NULL || buflen == 0)
2958 return -1;
2959 *name++ = '\0';
2960
2961 id = atoi(cmd);
2962 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2963 id, name);
2964
2965 ssid = wpa_config_get_network(wpa_s->conf, id);
2966 if (ssid == NULL) {
2967 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2968 "id=%d", id);
2969 return -1;
2970 }
2971
2972 value = wpa_config_get_no_key(ssid, name);
2973 if (value == NULL) {
2974 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2975 "variable '%s'", name);
2976 return -1;
2977 }
2978
2979 res = os_strlcpy(buf, value, buflen);
2980 if (res >= buflen) {
2981 os_free(value);
2982 return -1;
2983 }
2984
2985 os_free(value);
2986
2987 return res;
2988}
2989
2990
Dmitry Shmidt684785c2014-05-12 13:34:29 -07002991static int wpa_supplicant_ctrl_iface_dup_network(
2992 struct wpa_supplicant *wpa_s, char *cmd)
2993{
2994 struct wpa_ssid *ssid_s, *ssid_d;
2995 char *name, *id, *value;
2996 int id_s, id_d, ret;
2997
2998 /* cmd: "<src network id> <dst network id> <variable name>" */
2999 id = os_strchr(cmd, ' ');
3000 if (id == NULL)
3001 return -1;
3002 *id++ = '\0';
3003
3004 name = os_strchr(id, ' ');
3005 if (name == NULL)
3006 return -1;
3007 *name++ = '\0';
3008
3009 id_s = atoi(cmd);
3010 id_d = atoi(id);
3011 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DUP_NETWORK id=%d -> %d name='%s'",
3012 id_s, id_d, name);
3013
3014 ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
3015 if (ssid_s == NULL) {
3016 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3017 "network id=%d", id_s);
3018 return -1;
3019 }
3020
3021 ssid_d = wpa_config_get_network(wpa_s->conf, id_d);
3022 if (ssid_d == NULL) {
3023 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003024 "network id=%d", id_d);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003025 return -1;
3026 }
3027
3028 value = wpa_config_get(ssid_s, name);
3029 if (value == NULL) {
3030 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
3031 "variable '%s'", name);
3032 return -1;
3033 }
3034
3035 ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid_d, name,
3036 value);
3037
3038 os_free(value);
3039
3040 return ret;
3041}
3042
3043
Dmitry Shmidt04949592012-07-19 12:16:46 -07003044static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
3045 char *buf, size_t buflen)
3046{
3047 char *pos, *end;
3048 struct wpa_cred *cred;
3049 int ret;
3050
3051 pos = buf;
3052 end = buf + buflen;
3053 ret = os_snprintf(pos, end - pos,
3054 "cred id / realm / username / domain / imsi\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003055 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07003056 return pos - buf;
3057 pos += ret;
3058
3059 cred = wpa_s->conf->cred;
3060 while (cred) {
3061 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
3062 cred->id, cred->realm ? cred->realm : "",
3063 cred->username ? cred->username : "",
Dmitry Shmidt051af732013-10-22 13:52:46 -07003064 cred->domain ? cred->domain[0] : "",
Dmitry Shmidt04949592012-07-19 12:16:46 -07003065 cred->imsi ? cred->imsi : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003066 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07003067 return pos - buf;
3068 pos += ret;
3069
3070 cred = cred->next;
3071 }
3072
3073 return pos - buf;
3074}
3075
3076
3077static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
3078 char *buf, size_t buflen)
3079{
3080 struct wpa_cred *cred;
3081 int ret;
3082
3083 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
3084
3085 cred = wpa_config_add_cred(wpa_s->conf);
3086 if (cred == NULL)
3087 return -1;
3088
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003089 wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
3090
Dmitry Shmidt04949592012-07-19 12:16:46 -07003091 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003092 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07003093 return -1;
3094 return ret;
3095}
3096
3097
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003098static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
3099 struct wpa_cred *cred)
3100{
3101 struct wpa_ssid *ssid;
3102 char str[20];
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003103 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003104
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003105 if (cred == NULL) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003106 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3107 return -1;
3108 }
3109
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003110 id = cred->id;
3111 if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
3112 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3113 return -1;
3114 }
3115
3116 wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
3117
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003118 /* Remove any network entry created based on the removed credential */
3119 ssid = wpa_s->conf->ssid;
3120 while (ssid) {
3121 if (ssid->parent_cred == cred) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003122 int res;
3123
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003124 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
3125 "used the removed credential", ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003126 res = os_snprintf(str, sizeof(str), "%d", ssid->id);
3127 if (os_snprintf_error(sizeof(str), res))
3128 str[sizeof(str) - 1] = '\0';
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003129 ssid = ssid->next;
3130 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
3131 } else
3132 ssid = ssid->next;
3133 }
3134
3135 return 0;
3136}
3137
3138
Dmitry Shmidt04949592012-07-19 12:16:46 -07003139static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
3140 char *cmd)
3141{
3142 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003143 struct wpa_cred *cred, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003144
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003145 /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
3146 * "provisioning_sp=<FQDN> */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003147 if (os_strcmp(cmd, "all") == 0) {
3148 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
3149 cred = wpa_s->conf->cred;
3150 while (cred) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003151 prev = cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003152 cred = cred->next;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003153 wpas_ctrl_remove_cred(wpa_s, prev);
3154 }
3155 return 0;
3156 }
3157
3158 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
3159 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
3160 cmd + 8);
3161 cred = wpa_s->conf->cred;
3162 while (cred) {
3163 prev = cred;
3164 cred = cred->next;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003165 if (prev->domain) {
3166 size_t i;
3167 for (i = 0; i < prev->num_domain; i++) {
3168 if (os_strcmp(prev->domain[i], cmd + 8)
3169 != 0)
3170 continue;
3171 wpas_ctrl_remove_cred(wpa_s, prev);
3172 break;
3173 }
3174 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003175 }
3176 return 0;
3177 }
3178
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003179 if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
3180 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
3181 cmd + 16);
3182 cred = wpa_s->conf->cred;
3183 while (cred) {
3184 prev = cred;
3185 cred = cred->next;
3186 if (prev->provisioning_sp &&
3187 os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
3188 wpas_ctrl_remove_cred(wpa_s, prev);
3189 }
3190 return 0;
3191 }
3192
Dmitry Shmidt04949592012-07-19 12:16:46 -07003193 id = atoi(cmd);
3194 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
3195
3196 cred = wpa_config_get_cred(wpa_s->conf, id);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003197 return wpas_ctrl_remove_cred(wpa_s, cred);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003198}
3199
3200
3201static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
3202 char *cmd)
3203{
3204 int id;
3205 struct wpa_cred *cred;
3206 char *name, *value;
3207
3208 /* cmd: "<cred id> <variable name> <value>" */
3209 name = os_strchr(cmd, ' ');
3210 if (name == NULL)
3211 return -1;
3212 *name++ = '\0';
3213
3214 value = os_strchr(name, ' ');
3215 if (value == NULL)
3216 return -1;
3217 *value++ = '\0';
3218
3219 id = atoi(cmd);
3220 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
3221 id, name);
3222 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3223 (u8 *) value, os_strlen(value));
3224
3225 cred = wpa_config_get_cred(wpa_s->conf, id);
3226 if (cred == NULL) {
3227 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3228 id);
3229 return -1;
3230 }
3231
3232 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
3233 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
3234 "variable '%s'", name);
3235 return -1;
3236 }
3237
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003238 wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
3239
Dmitry Shmidt04949592012-07-19 12:16:46 -07003240 return 0;
3241}
3242
3243
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003244static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
3245 char *cmd, char *buf,
3246 size_t buflen)
3247{
3248 int id;
3249 size_t res;
3250 struct wpa_cred *cred;
3251 char *name, *value;
3252
3253 /* cmd: "<cred id> <variable name>" */
3254 name = os_strchr(cmd, ' ');
3255 if (name == NULL)
3256 return -1;
3257 *name++ = '\0';
3258
3259 id = atoi(cmd);
3260 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
3261 id, name);
3262
3263 cred = wpa_config_get_cred(wpa_s->conf, id);
3264 if (cred == NULL) {
3265 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3266 id);
3267 return -1;
3268 }
3269
3270 value = wpa_config_get_cred_no_key(cred, name);
3271 if (value == NULL) {
3272 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
3273 name);
3274 return -1;
3275 }
3276
3277 res = os_strlcpy(buf, value, buflen);
3278 if (res >= buflen) {
3279 os_free(value);
3280 return -1;
3281 }
3282
3283 os_free(value);
3284
3285 return res;
3286}
3287
3288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003289#ifndef CONFIG_NO_CONFIG_WRITE
3290static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
3291{
3292 int ret;
3293
3294 if (!wpa_s->conf->update_config) {
3295 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
3296 "to update configuration (update_config=0)");
3297 return -1;
3298 }
3299
3300 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
3301 if (ret) {
3302 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
3303 "update configuration");
3304 } else {
3305 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
3306 " updated");
3307 }
3308
3309 return ret;
3310}
3311#endif /* CONFIG_NO_CONFIG_WRITE */
3312
3313
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003314struct cipher_info {
3315 unsigned int capa;
3316 const char *name;
3317 int group_only;
3318};
3319
3320static const struct cipher_info ciphers[] = {
3321 { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
3322 { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
3323 { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
3324 { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
3325 { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
3326 { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
3327 { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
3328 { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
3329};
3330
3331
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003332static int ctrl_iface_get_capability_pairwise(int res, char *strict,
3333 struct wpa_driver_capa *capa,
3334 char *buf, size_t buflen)
3335{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003336 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003337 char *pos, *end;
3338 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003339 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003340
3341 pos = buf;
3342 end = pos + buflen;
3343
3344 if (res < 0) {
3345 if (strict)
3346 return 0;
3347 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
3348 if (len >= buflen)
3349 return -1;
3350 return len;
3351 }
3352
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003353 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3354 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
3355 ret = os_snprintf(pos, end - pos, "%s%s",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003356 pos == buf ? "" : " ",
3357 ciphers[i].name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003358 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003359 return pos - buf;
3360 pos += ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003361 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003362 }
3363
3364 return pos - buf;
3365}
3366
3367
3368static int ctrl_iface_get_capability_group(int res, char *strict,
3369 struct wpa_driver_capa *capa,
3370 char *buf, size_t buflen)
3371{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003372 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003373 char *pos, *end;
3374 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003375 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003376
3377 pos = buf;
3378 end = pos + buflen;
3379
3380 if (res < 0) {
3381 if (strict)
3382 return 0;
3383 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
3384 if (len >= buflen)
3385 return -1;
3386 return len;
3387 }
3388
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003389 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3390 if (capa->enc & ciphers[i].capa) {
3391 ret = os_snprintf(pos, end - pos, "%s%s",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003392 pos == buf ? "" : " ",
3393 ciphers[i].name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003394 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003395 return pos - buf;
3396 pos += ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003397 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003398 }
3399
3400 return pos - buf;
3401}
3402
3403
3404static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
3405 struct wpa_driver_capa *capa,
3406 char *buf, size_t buflen)
3407{
3408 int ret;
3409 char *pos, *end;
3410 size_t len;
3411
3412 pos = buf;
3413 end = pos + buflen;
3414
3415 if (res < 0) {
3416 if (strict)
3417 return 0;
3418 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
3419 "NONE", buflen);
3420 if (len >= buflen)
3421 return -1;
3422 return len;
3423 }
3424
3425 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003426 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003427 return pos - buf;
3428 pos += ret;
3429
3430 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3431 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
3432 ret = os_snprintf(pos, end - pos, " WPA-EAP");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003433 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434 return pos - buf;
3435 pos += ret;
3436 }
3437
3438 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3439 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
3440 ret = os_snprintf(pos, end - pos, " WPA-PSK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003441 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003442 return pos - buf;
3443 pos += ret;
3444 }
3445
3446 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
3447 ret = os_snprintf(pos, end - pos, " WPA-NONE");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003448 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003449 return pos - buf;
3450 pos += ret;
3451 }
3452
3453 return pos - buf;
3454}
3455
3456
3457static int ctrl_iface_get_capability_proto(int res, char *strict,
3458 struct wpa_driver_capa *capa,
3459 char *buf, size_t buflen)
3460{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003461 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003462 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, "RSN WPA", buflen);
3472 if (len >= buflen)
3473 return -1;
3474 return len;
3475 }
3476
3477 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3478 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003479 ret = os_snprintf(pos, end - pos, "%sRSN",
3480 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003481 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003482 return pos - buf;
3483 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003484 }
3485
3486 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3487 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003488 ret = os_snprintf(pos, end - pos, "%sWPA",
3489 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003490 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003491 return pos - buf;
3492 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003493 }
3494
3495 return pos - buf;
3496}
3497
3498
3499static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
3500 struct wpa_driver_capa *capa,
3501 char *buf, size_t buflen)
3502{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003503 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003504 char *pos, *end;
3505 size_t len;
3506
3507 pos = buf;
3508 end = pos + buflen;
3509
3510 if (res < 0) {
3511 if (strict)
3512 return 0;
3513 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
3514 if (len >= buflen)
3515 return -1;
3516 return len;
3517 }
3518
3519 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003520 ret = os_snprintf(pos, end - pos, "%sOPEN",
3521 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003522 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003523 return pos - buf;
3524 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003525 }
3526
3527 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
3528 ret = os_snprintf(pos, end - pos, "%sSHARED",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003529 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003530 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003531 return pos - buf;
3532 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003533 }
3534
3535 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003536 ret = os_snprintf(pos, end - pos, "%sLEAP",
3537 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003538 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003539 return pos - buf;
3540 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003541 }
3542
3543 return pos - buf;
3544}
3545
3546
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003547static int ctrl_iface_get_capability_modes(int res, char *strict,
3548 struct wpa_driver_capa *capa,
3549 char *buf, size_t buflen)
3550{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003551 int ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003552 char *pos, *end;
3553 size_t len;
3554
3555 pos = buf;
3556 end = pos + buflen;
3557
3558 if (res < 0) {
3559 if (strict)
3560 return 0;
3561 len = os_strlcpy(buf, "IBSS AP", buflen);
3562 if (len >= buflen)
3563 return -1;
3564 return len;
3565 }
3566
3567 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003568 ret = os_snprintf(pos, end - pos, "%sIBSS",
3569 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003570 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003571 return pos - buf;
3572 pos += ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003573 }
3574
3575 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003576 ret = os_snprintf(pos, end - pos, "%sAP",
3577 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003578 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003579 return pos - buf;
3580 pos += ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003581 }
3582
3583 return pos - buf;
3584}
3585
3586
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003587static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
3588 char *buf, size_t buflen)
3589{
3590 struct hostapd_channel_data *chnl;
3591 int ret, i, j;
3592 char *pos, *end, *hmode;
3593
3594 pos = buf;
3595 end = pos + buflen;
3596
3597 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3598 switch (wpa_s->hw.modes[j].mode) {
3599 case HOSTAPD_MODE_IEEE80211B:
3600 hmode = "B";
3601 break;
3602 case HOSTAPD_MODE_IEEE80211G:
3603 hmode = "G";
3604 break;
3605 case HOSTAPD_MODE_IEEE80211A:
3606 hmode = "A";
3607 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003608 case HOSTAPD_MODE_IEEE80211AD:
3609 hmode = "AD";
3610 break;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003611 default:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003612 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003613 }
3614 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003615 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003616 return pos - buf;
3617 pos += ret;
3618 chnl = wpa_s->hw.modes[j].channels;
3619 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003620 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3621 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003622 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003623 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003624 return pos - buf;
3625 pos += ret;
3626 }
3627 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003628 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003629 return pos - buf;
3630 pos += ret;
3631 }
3632
3633 return pos - buf;
3634}
3635
3636
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003637static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3638 char *buf, size_t buflen)
3639{
3640 struct hostapd_channel_data *chnl;
3641 int ret, i, j;
3642 char *pos, *end, *hmode;
3643
3644 pos = buf;
3645 end = pos + buflen;
3646
3647 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3648 switch (wpa_s->hw.modes[j].mode) {
3649 case HOSTAPD_MODE_IEEE80211B:
3650 hmode = "B";
3651 break;
3652 case HOSTAPD_MODE_IEEE80211G:
3653 hmode = "G";
3654 break;
3655 case HOSTAPD_MODE_IEEE80211A:
3656 hmode = "A";
3657 break;
3658 case HOSTAPD_MODE_IEEE80211AD:
3659 hmode = "AD";
3660 break;
3661 default:
3662 continue;
3663 }
3664 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3665 hmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003666 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003667 return pos - buf;
3668 pos += ret;
3669 chnl = wpa_s->hw.modes[j].channels;
3670 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3671 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3672 continue;
Dmitry Shmidt5da5e352014-02-03 13:30:46 -08003673 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003674 chnl[i].chan, chnl[i].freq,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003675 chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
3676 " (NO_IR)" : "",
Dmitry Shmidt5da5e352014-02-03 13:30:46 -08003677 chnl[i].flag & HOSTAPD_CHAN_RADAR ?
3678 " (DFS)" : "");
3679
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003680 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003681 return pos - buf;
3682 pos += ret;
3683 }
3684 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003685 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003686 return pos - buf;
3687 pos += ret;
3688 }
3689
3690 return pos - buf;
3691}
3692
3693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003694static int wpa_supplicant_ctrl_iface_get_capability(
3695 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3696 size_t buflen)
3697{
3698 struct wpa_driver_capa capa;
3699 int res;
3700 char *strict;
3701 char field[30];
3702 size_t len;
3703
3704 /* Determine whether or not strict checking was requested */
3705 len = os_strlcpy(field, _field, sizeof(field));
3706 if (len >= sizeof(field))
3707 return -1;
3708 strict = os_strchr(field, ' ');
3709 if (strict != NULL) {
3710 *strict++ = '\0';
3711 if (os_strcmp(strict, "strict") != 0)
3712 return -1;
3713 }
3714
3715 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3716 field, strict ? strict : "");
3717
3718 if (os_strcmp(field, "eap") == 0) {
3719 return eap_get_names(buf, buflen);
3720 }
3721
3722 res = wpa_drv_get_capa(wpa_s, &capa);
3723
3724 if (os_strcmp(field, "pairwise") == 0)
3725 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3726 buf, buflen);
3727
3728 if (os_strcmp(field, "group") == 0)
3729 return ctrl_iface_get_capability_group(res, strict, &capa,
3730 buf, buflen);
3731
3732 if (os_strcmp(field, "key_mgmt") == 0)
3733 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3734 buf, buflen);
3735
3736 if (os_strcmp(field, "proto") == 0)
3737 return ctrl_iface_get_capability_proto(res, strict, &capa,
3738 buf, buflen);
3739
3740 if (os_strcmp(field, "auth_alg") == 0)
3741 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
3742 buf, buflen);
3743
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003744 if (os_strcmp(field, "modes") == 0)
3745 return ctrl_iface_get_capability_modes(res, strict, &capa,
3746 buf, buflen);
3747
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003748 if (os_strcmp(field, "channels") == 0)
3749 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3750
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003751 if (os_strcmp(field, "freq") == 0)
3752 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3753
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003754#ifdef CONFIG_TDLS
3755 if (os_strcmp(field, "tdls") == 0)
3756 return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
3757#endif /* CONFIG_TDLS */
3758
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003759#ifdef CONFIG_ERP
3760 if (os_strcmp(field, "erp") == 0) {
3761 res = os_snprintf(buf, buflen, "ERP");
3762 if (os_snprintf_error(buflen, res))
3763 return -1;
3764 return res;
3765 }
3766#endif /* CONFIG_EPR */
3767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003768 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3769 field);
3770
3771 return -1;
3772}
3773
3774
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003775#ifdef CONFIG_INTERWORKING
3776static char * anqp_add_hex(char *pos, char *end, const char *title,
3777 struct wpabuf *data)
3778{
3779 char *start = pos;
3780 size_t i;
3781 int ret;
3782 const u8 *d;
3783
3784 if (data == NULL)
3785 return start;
3786
3787 ret = os_snprintf(pos, end - pos, "%s=", title);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003788 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003789 return start;
3790 pos += ret;
3791
3792 d = wpabuf_head_u8(data);
3793 for (i = 0; i < wpabuf_len(data); i++) {
3794 ret = os_snprintf(pos, end - pos, "%02x", *d++);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003795 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003796 return start;
3797 pos += ret;
3798 }
3799
3800 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003801 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003802 return start;
3803 pos += ret;
3804
3805 return pos;
3806}
3807#endif /* CONFIG_INTERWORKING */
3808
3809
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003810static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3811 unsigned long mask, char *buf, size_t buflen)
3812{
3813 size_t i;
3814 int ret;
3815 char *pos, *end;
3816 const u8 *ie, *ie2;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003817
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003818 pos = buf;
3819 end = buf + buflen;
3820
3821 if (mask & WPA_BSS_MASK_ID) {
3822 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003823 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003824 return 0;
3825 pos += ret;
3826 }
3827
3828 if (mask & WPA_BSS_MASK_BSSID) {
3829 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3830 MAC2STR(bss->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003831 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003832 return 0;
3833 pos += ret;
3834 }
3835
3836 if (mask & WPA_BSS_MASK_FREQ) {
3837 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003838 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003839 return 0;
3840 pos += ret;
3841 }
3842
3843 if (mask & WPA_BSS_MASK_BEACON_INT) {
3844 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3845 bss->beacon_int);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003846 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003847 return 0;
3848 pos += ret;
3849 }
3850
3851 if (mask & WPA_BSS_MASK_CAPABILITIES) {
3852 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3853 bss->caps);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003854 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003855 return 0;
3856 pos += ret;
3857 }
3858
3859 if (mask & WPA_BSS_MASK_QUAL) {
3860 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003861 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003862 return 0;
3863 pos += ret;
3864 }
3865
3866 if (mask & WPA_BSS_MASK_NOISE) {
3867 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003868 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003869 return 0;
3870 pos += ret;
3871 }
3872
3873 if (mask & WPA_BSS_MASK_LEVEL) {
3874 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003875 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003876 return 0;
3877 pos += ret;
3878 }
3879
3880 if (mask & WPA_BSS_MASK_TSF) {
3881 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3882 (unsigned long long) bss->tsf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003883 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003884 return 0;
3885 pos += ret;
3886 }
3887
3888 if (mask & WPA_BSS_MASK_AGE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003889 struct os_reltime now;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003890
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003891 os_get_reltime(&now);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003892 ret = os_snprintf(pos, end - pos, "age=%d\n",
3893 (int) (now.sec - bss->last_update.sec));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003894 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003895 return 0;
3896 pos += ret;
3897 }
3898
3899 if (mask & WPA_BSS_MASK_IE) {
3900 ret = os_snprintf(pos, end - pos, "ie=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003901 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003902 return 0;
3903 pos += ret;
3904
3905 ie = (const u8 *) (bss + 1);
3906 for (i = 0; i < bss->ie_len; i++) {
3907 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003908 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003909 return 0;
3910 pos += ret;
3911 }
3912
3913 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003914 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003915 return 0;
3916 pos += ret;
3917 }
3918
3919 if (mask & WPA_BSS_MASK_FLAGS) {
3920 ret = os_snprintf(pos, end - pos, "flags=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003921 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003922 return 0;
3923 pos += ret;
3924
3925 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3926 if (ie)
3927 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3928 2 + ie[1]);
3929 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3930 if (ie2)
3931 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3932 2 + ie2[1]);
3933 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3934 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3935 ret = os_snprintf(pos, end - pos, "[WEP]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003936 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003937 return 0;
3938 pos += ret;
3939 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003940 if (bss_is_dmg(bss)) {
3941 const char *s;
3942 ret = os_snprintf(pos, end - pos, "[DMG]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003943 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003944 return 0;
3945 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003946 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
3947 case IEEE80211_CAP_DMG_IBSS:
3948 s = "[IBSS]";
3949 break;
3950 case IEEE80211_CAP_DMG_AP:
3951 s = "[ESS]";
3952 break;
3953 case IEEE80211_CAP_DMG_PBSS:
3954 s = "[PBSS]";
3955 break;
3956 default:
3957 s = "";
3958 break;
3959 }
3960 ret = os_snprintf(pos, end - pos, "%s", s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003961 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003962 return 0;
3963 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003964 } else {
3965 if (bss->caps & IEEE80211_CAP_IBSS) {
3966 ret = os_snprintf(pos, end - pos, "[IBSS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003967 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003968 return 0;
3969 pos += ret;
3970 }
3971 if (bss->caps & IEEE80211_CAP_ESS) {
3972 ret = os_snprintf(pos, end - pos, "[ESS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003973 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003974 return 0;
3975 pos += ret;
3976 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003977 }
Dmitry Shmidt96571392013-10-14 12:54:46 -07003978 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
3979 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003980 ret = os_snprintf(pos, end - pos, "[P2P]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003981 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003982 return 0;
3983 pos += ret;
3984 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003985#ifdef CONFIG_HS20
3986 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3987 ret = os_snprintf(pos, end - pos, "[HS20]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003988 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003989 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003990 pos += ret;
3991 }
3992#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003993
3994 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003995 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003996 return 0;
3997 pos += ret;
3998 }
3999
4000 if (mask & WPA_BSS_MASK_SSID) {
4001 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
4002 wpa_ssid_txt(bss->ssid, bss->ssid_len));
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#ifdef CONFIG_WPS
4009 if (mask & WPA_BSS_MASK_WPS_SCAN) {
4010 ie = (const u8 *) (bss + 1);
4011 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
4012 if (ret < 0 || ret >= end - pos)
4013 return 0;
4014 pos += ret;
4015 }
4016#endif /* CONFIG_WPS */
4017
4018#ifdef CONFIG_P2P
4019 if (mask & WPA_BSS_MASK_P2P_SCAN) {
4020 ie = (const u8 *) (bss + 1);
4021 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
4022 if (ret < 0 || ret >= end - pos)
4023 return 0;
4024 pos += ret;
4025 }
4026#endif /* CONFIG_P2P */
4027
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004028#ifdef CONFIG_WIFI_DISPLAY
4029 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
4030 struct wpabuf *wfd;
4031 ie = (const u8 *) (bss + 1);
4032 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
4033 WFD_IE_VENDOR_TYPE);
4034 if (wfd) {
4035 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004036 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004037 wpabuf_free(wfd);
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004038 return 0;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004039 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004040 pos += ret;
4041
4042 pos += wpa_snprintf_hex(pos, end - pos,
4043 wpabuf_head(wfd),
4044 wpabuf_len(wfd));
4045 wpabuf_free(wfd);
4046
4047 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004048 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004049 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004050 pos += ret;
4051 }
4052 }
4053#endif /* CONFIG_WIFI_DISPLAY */
4054
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004055#ifdef CONFIG_INTERWORKING
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004056 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
4057 struct wpa_bss_anqp *anqp = bss->anqp;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004058 pos = anqp_add_hex(pos, end, "anqp_venue_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004059 anqp->venue_name);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004060 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004061 anqp->network_auth_type);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004062 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004063 anqp->roaming_consortium);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004064 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004065 anqp->ip_addr_type_availability);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004066 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004067 anqp->nai_realm);
4068 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004069 pos = anqp_add_hex(pos, end, "anqp_domain_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004070 anqp->domain_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004071#ifdef CONFIG_HS20
4072 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004073 anqp->hs20_operator_friendly_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004074 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004075 anqp->hs20_wan_metrics);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004076 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004077 anqp->hs20_connection_capability);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004078 pos = anqp_add_hex(pos, end, "hs20_operating_class",
4079 anqp->hs20_operating_class);
4080 pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
4081 anqp->hs20_osu_providers_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004082#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004083 }
4084#endif /* CONFIG_INTERWORKING */
4085
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004086#ifdef CONFIG_MESH
4087 if (mask & WPA_BSS_MASK_MESH_SCAN) {
4088 ie = (const u8 *) (bss + 1);
4089 ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
4090 if (ret < 0 || ret >= end - pos)
4091 return 0;
4092 pos += ret;
4093 }
4094#endif /* CONFIG_MESH */
4095
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004096 if (mask & WPA_BSS_MASK_DELIM) {
4097 ret = os_snprintf(pos, end - pos, "====\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004098 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004099 return 0;
4100 pos += ret;
4101 }
Irfan Sheriffe2ea0082012-08-13 10:56:16 -07004102
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004103 return pos - buf;
4104}
4105
Dmitry Shmidt04949592012-07-19 12:16:46 -07004106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
4108 const char *cmd, char *buf,
4109 size_t buflen)
4110{
4111 u8 bssid[ETH_ALEN];
4112 size_t i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004113 struct wpa_bss *bss;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004114 struct wpa_bss *bsslast = NULL;
4115 struct dl_list *next;
4116 int ret = 0;
4117 int len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004118 char *ctmp, *end = buf + buflen;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004119 unsigned long mask = WPA_BSS_MASK_ALL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004120
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004121 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
4122 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
4123 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004124 list_id);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004125 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
4126 list_id);
4127 } else { /* N1-N2 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004128 unsigned int id1, id2;
4129
4130 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
4131 wpa_printf(MSG_INFO, "Wrong BSS range "
4132 "format");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004133 return 0;
4134 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004135
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004136 if (*(cmd + 6) == '-')
4137 id1 = 0;
4138 else
4139 id1 = atoi(cmd + 6);
4140 ctmp++;
4141 if (*ctmp >= '0' && *ctmp <= '9')
4142 id2 = atoi(ctmp);
4143 else
4144 id2 = (unsigned int) -1;
4145 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
4146 if (id2 == (unsigned int) -1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004147 bsslast = dl_list_last(&wpa_s->bss_id,
4148 struct wpa_bss,
4149 list_id);
4150 else {
4151 bsslast = wpa_bss_get_id(wpa_s, id2);
4152 if (bsslast == NULL && bss && id2 > id1) {
4153 struct wpa_bss *tmp = bss;
4154 for (;;) {
4155 next = tmp->list_id.next;
4156 if (next == &wpa_s->bss_id)
4157 break;
4158 tmp = dl_list_entry(
4159 next, struct wpa_bss,
4160 list_id);
4161 if (tmp->id > id2)
4162 break;
4163 bsslast = tmp;
4164 }
4165 }
4166 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004167 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004168 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004169 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004170 else if (os_strncmp(cmd, "LAST", 4) == 0)
4171 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004172 else if (os_strncmp(cmd, "ID-", 3) == 0) {
4173 i = atoi(cmd + 3);
4174 bss = wpa_bss_get_id(wpa_s, i);
4175 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4176 i = atoi(cmd + 5);
4177 bss = wpa_bss_get_id(wpa_s, i);
4178 if (bss) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004179 next = bss->list_id.next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004180 if (next == &wpa_s->bss_id)
4181 bss = NULL;
4182 else
4183 bss = dl_list_entry(next, struct wpa_bss,
4184 list_id);
4185 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004186#ifdef CONFIG_P2P
4187 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
4188 if (hwaddr_aton(cmd + 13, bssid) == 0)
4189 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
4190 else
4191 bss = NULL;
4192#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004193 } else if (hwaddr_aton(cmd, bssid) == 0)
4194 bss = wpa_bss_get_bssid(wpa_s, bssid);
4195 else {
4196 struct wpa_bss *tmp;
4197 i = atoi(cmd);
4198 bss = NULL;
4199 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
4200 {
4201 if (i-- == 0) {
4202 bss = tmp;
4203 break;
4204 }
4205 }
4206 }
4207
Dmitry Shmidt04949592012-07-19 12:16:46 -07004208 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
4209 mask = strtoul(ctmp + 5, NULL, 0x10);
4210 if (mask == 0)
4211 mask = WPA_BSS_MASK_ALL;
4212 }
4213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004214 if (bss == NULL)
4215 return 0;
4216
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004217 if (bsslast == NULL)
4218 bsslast = bss;
4219 do {
4220 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
4221 ret += len;
4222 buf += len;
4223 buflen -= len;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004224 if (bss == bsslast) {
4225 if ((mask & WPA_BSS_MASK_DELIM) && len &&
4226 (bss == dl_list_last(&wpa_s->bss_id,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004227 struct wpa_bss, list_id))) {
4228 int res;
4229
4230 res = os_snprintf(buf - 5, end - buf + 5,
4231 "####\n");
4232 if (os_snprintf_error(end - buf + 5, res)) {
4233 wpa_printf(MSG_DEBUG,
4234 "Could not add end delim");
4235 }
4236 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004237 break;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004238 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004239 next = bss->list_id.next;
4240 if (next == &wpa_s->bss_id)
4241 break;
4242 bss = dl_list_entry(next, struct wpa_bss, list_id);
4243 } while (bss && len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004244
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004245 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004246}
4247
4248
4249static int wpa_supplicant_ctrl_iface_ap_scan(
4250 struct wpa_supplicant *wpa_s, char *cmd)
4251{
4252 int ap_scan = atoi(cmd);
4253 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
4254}
4255
4256
4257static int wpa_supplicant_ctrl_iface_scan_interval(
4258 struct wpa_supplicant *wpa_s, char *cmd)
4259{
4260 int scan_int = atoi(cmd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004261 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004262}
4263
4264
4265static int wpa_supplicant_ctrl_iface_bss_expire_age(
4266 struct wpa_supplicant *wpa_s, char *cmd)
4267{
4268 int expire_age = atoi(cmd);
4269 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
4270}
4271
4272
4273static int wpa_supplicant_ctrl_iface_bss_expire_count(
4274 struct wpa_supplicant *wpa_s, char *cmd)
4275{
4276 int expire_count = atoi(cmd);
4277 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
4278}
4279
4280
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004281static void wpa_supplicant_ctrl_iface_bss_flush(
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07004282 struct wpa_supplicant *wpa_s, char *cmd)
4283{
4284 int flush_age = atoi(cmd);
4285
4286 if (flush_age == 0)
4287 wpa_bss_flush(wpa_s);
4288 else
4289 wpa_bss_flush_by_age(wpa_s, flush_age);
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07004290}
4291
4292
Dmitry Shmidt21de2142014-04-08 10:50:52 -07004293#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004294static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
4295{
4296 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
4297 /* MLME-DELETEKEYS.request */
4298 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
4299 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
4300 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
4301 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
4302#ifdef CONFIG_IEEE80211W
4303 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
4304 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
4305#endif /* CONFIG_IEEE80211W */
4306
4307 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
4308 0);
4309 /* MLME-SETPROTECTION.request(None) */
4310 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
4311 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
4312 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
4313 wpa_sm_drop_sa(wpa_s->wpa);
4314}
Dmitry Shmidt21de2142014-04-08 10:50:52 -07004315#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004316
4317
4318static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
4319 char *addr)
4320{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004321#ifdef CONFIG_NO_SCAN_PROCESSING
4322 return -1;
4323#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324 u8 bssid[ETH_ALEN];
4325 struct wpa_bss *bss;
4326 struct wpa_ssid *ssid = wpa_s->current_ssid;
4327
4328 if (hwaddr_aton(addr, bssid)) {
4329 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
4330 "address '%s'", addr);
4331 return -1;
4332 }
4333
4334 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
4335
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004336 if (!ssid) {
4337 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
4338 "configuration known for the target AP");
4339 return -1;
4340 }
4341
4342 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004343 if (!bss) {
4344 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
4345 "from BSS table");
4346 return -1;
4347 }
4348
4349 /*
4350 * TODO: Find best network configuration block from configuration to
4351 * allow roaming to other networks
4352 */
4353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004354 wpa_s->reassociate = 1;
4355 wpa_supplicant_connect(wpa_s, bss, ssid);
4356
4357 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004358#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004359}
4360
4361
4362#ifdef CONFIG_P2P
4363static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
4364{
4365 unsigned int timeout = atoi(cmd);
4366 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004367 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004368 u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004369 char *pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004370 unsigned int search_delay;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004371
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004372 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4373 wpa_dbg(wpa_s, MSG_INFO,
4374 "Reject P2P_FIND since interface is disabled");
4375 return -1;
4376 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004377 if (os_strstr(cmd, "type=social"))
4378 type = P2P_FIND_ONLY_SOCIAL;
4379 else if (os_strstr(cmd, "type=progressive"))
4380 type = P2P_FIND_PROGRESSIVE;
4381
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004382 pos = os_strstr(cmd, "dev_id=");
4383 if (pos) {
4384 pos += 7;
4385 if (hwaddr_aton(pos, dev_id))
4386 return -1;
4387 _dev_id = dev_id;
4388 }
4389
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004390 pos = os_strstr(cmd, "dev_type=");
4391 if (pos) {
4392 pos += 9;
4393 if (wps_dev_type_str2bin(pos, dev_type) < 0)
4394 return -1;
4395 _dev_type = dev_type;
4396 }
4397
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004398 pos = os_strstr(cmd, "delay=");
4399 if (pos) {
4400 pos += 6;
4401 search_delay = atoi(pos);
4402 } else
4403 search_delay = wpas_p2p_search_delay(wpa_s);
4404
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004405 return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
4406 _dev_id, search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407}
4408
4409
4410static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
4411 char *buf, size_t buflen)
4412{
4413 u8 addr[ETH_ALEN];
4414 char *pos, *pos2;
4415 char *pin = NULL;
4416 enum p2p_wps_method wps_method;
4417 int new_pin;
4418 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004419 int persistent_group, persistent_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004420 int join;
4421 int auth;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004422 int automatic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004423 int go_intent = -1;
4424 int freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004425 int pd;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004426 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004427
Dmitry Shmidt04949592012-07-19 12:16:46 -07004428 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
4429 * [persistent|persistent=<network id>]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004430 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004431 * [ht40] [vht] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432
4433 if (hwaddr_aton(cmd, addr))
4434 return -1;
4435
4436 pos = cmd + 17;
4437 if (*pos != ' ')
4438 return -1;
4439 pos++;
4440
4441 persistent_group = os_strstr(pos, " persistent") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004442 pos2 = os_strstr(pos, " persistent=");
4443 if (pos2) {
4444 struct wpa_ssid *ssid;
4445 persistent_id = atoi(pos2 + 12);
4446 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4447 if (ssid == NULL || ssid->disabled != 2 ||
4448 ssid->mode != WPAS_MODE_P2P_GO) {
4449 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
4450 "SSID id=%d for persistent P2P group (GO)",
4451 persistent_id);
4452 return -1;
4453 }
4454 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004455 join = os_strstr(pos, " join") != NULL;
4456 auth = os_strstr(pos, " auth") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004457 automatic = os_strstr(pos, " auto") != NULL;
4458 pd = os_strstr(pos, " provdisc") != NULL;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004459 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4460 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4461 vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004462
4463 pos2 = os_strstr(pos, " go_intent=");
4464 if (pos2) {
4465 pos2 += 11;
4466 go_intent = atoi(pos2);
4467 if (go_intent < 0 || go_intent > 15)
4468 return -1;
4469 }
4470
4471 pos2 = os_strstr(pos, " freq=");
4472 if (pos2) {
4473 pos2 += 6;
4474 freq = atoi(pos2);
4475 if (freq <= 0)
4476 return -1;
4477 }
4478
4479 if (os_strncmp(pos, "pin", 3) == 0) {
4480 /* Request random PIN (to be displayed) and enable the PIN */
4481 wps_method = WPS_PIN_DISPLAY;
4482 } else if (os_strncmp(pos, "pbc", 3) == 0) {
4483 wps_method = WPS_PBC;
4484 } else {
4485 pin = pos;
4486 pos = os_strchr(pin, ' ');
4487 wps_method = WPS_PIN_KEYPAD;
4488 if (pos) {
4489 *pos++ = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004490 if (os_strncmp(pos, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004491 wps_method = WPS_PIN_DISPLAY;
4492 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004493 if (!wps_pin_str_valid(pin)) {
4494 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
4495 return 17;
4496 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497 }
4498
4499 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004500 persistent_group, automatic, join,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004501 auth, go_intent, freq, persistent_id, pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004502 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503 if (new_pin == -2) {
4504 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
4505 return 25;
4506 }
4507 if (new_pin == -3) {
4508 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
4509 return 25;
4510 }
4511 if (new_pin < 0)
4512 return -1;
4513 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
4514 ret = os_snprintf(buf, buflen, "%08d", new_pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004515 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004516 return -1;
4517 return ret;
4518 }
4519
4520 os_memcpy(buf, "OK\n", 3);
4521 return 3;
4522}
4523
4524
4525static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
4526{
4527 unsigned int timeout = atoi(cmd);
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004528 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4529 wpa_dbg(wpa_s, MSG_INFO,
4530 "Reject P2P_LISTEN since interface is disabled");
4531 return -1;
4532 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004533 return wpas_p2p_listen(wpa_s, timeout);
4534}
4535
4536
4537static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
4538{
4539 u8 addr[ETH_ALEN];
4540 char *pos;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004541 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542
Dmitry Shmidt04949592012-07-19 12:16:46 -07004543 /* <addr> <config method> [join|auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004544
4545 if (hwaddr_aton(cmd, addr))
4546 return -1;
4547
4548 pos = cmd + 17;
4549 if (*pos != ' ')
4550 return -1;
4551 pos++;
4552
Dmitry Shmidt04949592012-07-19 12:16:46 -07004553 if (os_strstr(pos, " join") != NULL)
4554 use = WPAS_P2P_PD_FOR_JOIN;
4555 else if (os_strstr(pos, " auto") != NULL)
4556 use = WPAS_P2P_PD_AUTO;
4557
4558 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004559}
4560
4561
4562static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
4563 size_t buflen)
4564{
4565 struct wpa_ssid *ssid = wpa_s->current_ssid;
4566
4567 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
4568 ssid->passphrase == NULL)
4569 return -1;
4570
4571 os_strlcpy(buf, ssid->passphrase, buflen);
4572 return os_strlen(buf);
4573}
4574
4575
4576static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
4577 char *buf, size_t buflen)
4578{
4579 u64 ref;
4580 int res;
4581 u8 dst_buf[ETH_ALEN], *dst;
4582 struct wpabuf *tlvs;
4583 char *pos;
4584 size_t len;
4585
4586 if (hwaddr_aton(cmd, dst_buf))
4587 return -1;
4588 dst = dst_buf;
4589 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
4590 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
4591 dst = NULL;
4592 pos = cmd + 17;
4593 if (*pos != ' ')
4594 return -1;
4595 pos++;
4596
4597 if (os_strncmp(pos, "upnp ", 5) == 0) {
4598 u8 version;
4599 pos += 5;
4600 if (hexstr2bin(pos, &version, 1) < 0)
4601 return -1;
4602 pos += 2;
4603 if (*pos != ' ')
4604 return -1;
4605 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004606 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004607#ifdef CONFIG_WIFI_DISPLAY
4608 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
4609 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
4610#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611 } else {
4612 len = os_strlen(pos);
4613 if (len & 1)
4614 return -1;
4615 len /= 2;
4616 tlvs = wpabuf_alloc(len);
4617 if (tlvs == NULL)
4618 return -1;
4619 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
4620 wpabuf_free(tlvs);
4621 return -1;
4622 }
4623
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004624 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004625 wpabuf_free(tlvs);
4626 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004627 if (ref == 0)
4628 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004629 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004630 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004631 return -1;
4632 return res;
4633}
4634
4635
4636static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
4637 char *cmd)
4638{
4639 long long unsigned val;
4640 u64 req;
4641 if (sscanf(cmd, "%llx", &val) != 1)
4642 return -1;
4643 req = val;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004644 return wpas_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645}
4646
4647
4648static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
4649{
4650 int freq;
4651 u8 dst[ETH_ALEN];
4652 u8 dialog_token;
4653 struct wpabuf *resp_tlvs;
4654 char *pos, *pos2;
4655 size_t len;
4656
4657 pos = os_strchr(cmd, ' ');
4658 if (pos == NULL)
4659 return -1;
4660 *pos++ = '\0';
4661 freq = atoi(cmd);
4662 if (freq == 0)
4663 return -1;
4664
4665 if (hwaddr_aton(pos, dst))
4666 return -1;
4667 pos += 17;
4668 if (*pos != ' ')
4669 return -1;
4670 pos++;
4671
4672 pos2 = os_strchr(pos, ' ');
4673 if (pos2 == NULL)
4674 return -1;
4675 *pos2++ = '\0';
4676 dialog_token = atoi(pos);
4677
4678 len = os_strlen(pos2);
4679 if (len & 1)
4680 return -1;
4681 len /= 2;
4682 resp_tlvs = wpabuf_alloc(len);
4683 if (resp_tlvs == NULL)
4684 return -1;
4685 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
4686 wpabuf_free(resp_tlvs);
4687 return -1;
4688 }
4689
4690 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
4691 wpabuf_free(resp_tlvs);
4692 return 0;
4693}
4694
4695
4696static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
4697 char *cmd)
4698{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004699 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
4700 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004701 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
4702 return 0;
4703}
4704
4705
4706static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
4707 char *cmd)
4708{
4709 char *pos;
4710 size_t len;
4711 struct wpabuf *query, *resp;
4712
4713 pos = os_strchr(cmd, ' ');
4714 if (pos == NULL)
4715 return -1;
4716 *pos++ = '\0';
4717
4718 len = os_strlen(cmd);
4719 if (len & 1)
4720 return -1;
4721 len /= 2;
4722 query = wpabuf_alloc(len);
4723 if (query == NULL)
4724 return -1;
4725 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4726 wpabuf_free(query);
4727 return -1;
4728 }
4729
4730 len = os_strlen(pos);
4731 if (len & 1) {
4732 wpabuf_free(query);
4733 return -1;
4734 }
4735 len /= 2;
4736 resp = wpabuf_alloc(len);
4737 if (resp == NULL) {
4738 wpabuf_free(query);
4739 return -1;
4740 }
4741 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4742 wpabuf_free(query);
4743 wpabuf_free(resp);
4744 return -1;
4745 }
4746
4747 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4748 wpabuf_free(query);
4749 wpabuf_free(resp);
4750 return -1;
4751 }
4752 return 0;
4753}
4754
4755
4756static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4757{
4758 char *pos;
4759 u8 version;
4760
4761 pos = os_strchr(cmd, ' ');
4762 if (pos == NULL)
4763 return -1;
4764 *pos++ = '\0';
4765
4766 if (hexstr2bin(cmd, &version, 1) < 0)
4767 return -1;
4768
4769 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4770}
4771
4772
4773static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4774{
4775 char *pos;
4776
4777 pos = os_strchr(cmd, ' ');
4778 if (pos == NULL)
4779 return -1;
4780 *pos++ = '\0';
4781
4782 if (os_strcmp(cmd, "bonjour") == 0)
4783 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4784 if (os_strcmp(cmd, "upnp") == 0)
4785 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4786 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4787 return -1;
4788}
4789
4790
4791static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4792 char *cmd)
4793{
4794 size_t len;
4795 struct wpabuf *query;
4796 int ret;
4797
4798 len = os_strlen(cmd);
4799 if (len & 1)
4800 return -1;
4801 len /= 2;
4802 query = wpabuf_alloc(len);
4803 if (query == NULL)
4804 return -1;
4805 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4806 wpabuf_free(query);
4807 return -1;
4808 }
4809
4810 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4811 wpabuf_free(query);
4812 return ret;
4813}
4814
4815
4816static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4817{
4818 char *pos;
4819 u8 version;
4820
4821 pos = os_strchr(cmd, ' ');
4822 if (pos == NULL)
4823 return -1;
4824 *pos++ = '\0';
4825
4826 if (hexstr2bin(cmd, &version, 1) < 0)
4827 return -1;
4828
4829 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4830}
4831
4832
4833static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4834{
4835 char *pos;
4836
4837 pos = os_strchr(cmd, ' ');
4838 if (pos == NULL)
4839 return -1;
4840 *pos++ = '\0';
4841
4842 if (os_strcmp(cmd, "bonjour") == 0)
4843 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4844 if (os_strcmp(cmd, "upnp") == 0)
4845 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4846 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4847 return -1;
4848}
4849
4850
4851static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4852{
4853 u8 addr[ETH_ALEN];
4854
4855 /* <addr> */
4856
4857 if (hwaddr_aton(cmd, addr))
4858 return -1;
4859
4860 return wpas_p2p_reject(wpa_s, addr);
4861}
4862
4863
4864static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4865{
4866 char *pos;
4867 int id;
4868 struct wpa_ssid *ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004869 u8 *_peer = NULL, peer[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004870 int freq = 0, pref_freq = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004871 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872
4873 id = atoi(cmd);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004874 pos = os_strstr(cmd, " peer=");
4875 if (pos) {
4876 pos += 6;
4877 if (hwaddr_aton(pos, peer))
4878 return -1;
4879 _peer = peer;
4880 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881 ssid = wpa_config_get_network(wpa_s->conf, id);
4882 if (ssid == NULL || ssid->disabled != 2) {
4883 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4884 "for persistent P2P group",
4885 id);
4886 return -1;
4887 }
4888
Jouni Malinen31be0a42012-08-31 21:20:51 +03004889 pos = os_strstr(cmd, " freq=");
4890 if (pos) {
4891 pos += 6;
4892 freq = atoi(pos);
4893 if (freq <= 0)
4894 return -1;
4895 }
4896
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004897 pos = os_strstr(cmd, " pref=");
4898 if (pos) {
4899 pos += 6;
4900 pref_freq = atoi(pos);
4901 if (pref_freq <= 0)
4902 return -1;
4903 }
4904
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004905 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4906 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4907 vht;
Jouni Malinen31be0a42012-08-31 21:20:51 +03004908
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004909 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
4910 pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004911}
4912
4913
4914static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
4915{
4916 char *pos;
4917 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
4918
4919 pos = os_strstr(cmd, " peer=");
4920 if (!pos)
4921 return -1;
4922
4923 *pos = '\0';
4924 pos += 6;
4925 if (hwaddr_aton(pos, peer)) {
4926 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
4927 return -1;
4928 }
4929
4930 pos = os_strstr(pos, " go_dev_addr=");
4931 if (pos) {
4932 pos += 13;
4933 if (hwaddr_aton(pos, go_dev_addr)) {
4934 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
4935 pos);
4936 return -1;
4937 }
4938 go_dev = go_dev_addr;
4939 }
4940
4941 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
4942}
4943
4944
4945static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
4946{
4947 if (os_strncmp(cmd, "persistent=", 11) == 0)
4948 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
4949 if (os_strncmp(cmd, "group=", 6) == 0)
4950 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
4951
4952 return -1;
4953}
4954
4955
4956static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004957 char *cmd, int freq, int ht40,
4958 int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004959{
4960 int id;
4961 struct wpa_ssid *ssid;
4962
4963 id = atoi(cmd);
4964 ssid = wpa_config_get_network(wpa_s->conf, id);
4965 if (ssid == NULL || ssid->disabled != 2) {
4966 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4967 "for persistent P2P group",
4968 id);
4969 return -1;
4970 }
4971
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004972 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004973 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004974}
4975
4976
4977static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
4978{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004979 int freq = 0, ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004980 char *pos;
4981
4982 pos = os_strstr(cmd, "freq=");
4983 if (pos)
4984 freq = atoi(pos + 5);
4985
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004986 vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
4987 ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4988 vht;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004990 if (os_strncmp(cmd, "persistent=", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004991 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004992 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004993 if (os_strcmp(cmd, "persistent") == 0 ||
4994 os_strncmp(cmd, "persistent ", 11) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004995 return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004996 if (os_strncmp(cmd, "freq=", 5) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004997 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004998 if (ht40)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004999 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005000
5001 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
5002 cmd);
5003 return -1;
5004}
5005
5006
5007static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
5008 char *buf, size_t buflen)
5009{
5010 u8 addr[ETH_ALEN], *addr_ptr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005011 int next, res;
5012 const struct p2p_peer_info *info;
5013 char *pos, *end;
5014 char devtype[WPS_DEV_TYPE_BUFSIZE];
5015 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005016 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005017
5018 if (!wpa_s->global->p2p)
5019 return -1;
5020
5021 if (os_strcmp(cmd, "FIRST") == 0) {
5022 addr_ptr = NULL;
5023 next = 0;
5024 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
5025 if (hwaddr_aton(cmd + 5, addr) < 0)
5026 return -1;
5027 addr_ptr = addr;
5028 next = 1;
5029 } else {
5030 if (hwaddr_aton(cmd, addr) < 0)
5031 return -1;
5032 addr_ptr = addr;
5033 next = 0;
5034 }
5035
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005036 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
5037 if (info == NULL)
5038 return -1;
5039
5040 pos = buf;
5041 end = buf + buflen;
5042
5043 res = os_snprintf(pos, end - pos, MACSTR "\n"
5044 "pri_dev_type=%s\n"
5045 "device_name=%s\n"
5046 "manufacturer=%s\n"
5047 "model_name=%s\n"
5048 "model_number=%s\n"
5049 "serial_number=%s\n"
5050 "config_methods=0x%x\n"
5051 "dev_capab=0x%x\n"
5052 "group_capab=0x%x\n"
5053 "level=%d\n",
5054 MAC2STR(info->p2p_device_addr),
5055 wps_dev_type_bin2str(info->pri_dev_type,
5056 devtype, sizeof(devtype)),
5057 info->device_name,
5058 info->manufacturer,
5059 info->model_name,
5060 info->model_number,
5061 info->serial_number,
5062 info->config_methods,
5063 info->dev_capab,
5064 info->group_capab,
5065 info->level);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005066 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005067 return pos - buf;
5068 pos += res;
5069
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005070 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
5071 {
5072 const u8 *t;
5073 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
5074 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
5075 wps_dev_type_bin2str(t, devtype,
5076 sizeof(devtype)));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005077 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005078 return pos - buf;
5079 pos += res;
5080 }
5081
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005082 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005083 if (ssid) {
5084 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005085 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005086 return pos - buf;
5087 pos += res;
5088 }
5089
5090 res = p2p_get_peer_info_txt(info, pos, end - pos);
5091 if (res < 0)
5092 return pos - buf;
5093 pos += res;
5094
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005095 if (info->vendor_elems) {
5096 res = os_snprintf(pos, end - pos, "vendor_elems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005097 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005098 return pos - buf;
5099 pos += res;
5100
5101 pos += wpa_snprintf_hex(pos, end - pos,
5102 wpabuf_head(info->vendor_elems),
5103 wpabuf_len(info->vendor_elems));
5104
5105 res = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005106 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005107 return pos - buf;
5108 pos += res;
5109 }
5110
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005111 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112}
5113
5114
Dmitry Shmidt04949592012-07-19 12:16:46 -07005115static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
5116 const char *param)
5117{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005118 unsigned int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005119
5120 if (wpa_s->global->p2p == NULL)
5121 return -1;
5122
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005123 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
5124 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005125
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005126 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
5127 struct wpa_freq_range *freq;
5128 freq = &wpa_s->global->p2p_disallow_freq.range[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005129 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005130 freq->min, freq->max);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005131 }
5132
Dmitry Shmidt04949592012-07-19 12:16:46 -07005133 wpas_p2p_update_channel_list(wpa_s);
5134 return 0;
5135}
5136
5137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005138static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
5139{
5140 char *param;
5141
5142 if (wpa_s->global->p2p == NULL)
5143 return -1;
5144
5145 param = os_strchr(cmd, ' ');
5146 if (param == NULL)
5147 return -1;
5148 *param++ = '\0';
5149
5150 if (os_strcmp(cmd, "discoverability") == 0) {
5151 p2p_set_client_discoverability(wpa_s->global->p2p,
5152 atoi(param));
5153 return 0;
5154 }
5155
5156 if (os_strcmp(cmd, "managed") == 0) {
5157 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
5158 return 0;
5159 }
5160
5161 if (os_strcmp(cmd, "listen_channel") == 0) {
5162 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005163 atoi(param), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005164 }
5165
5166 if (os_strcmp(cmd, "ssid_postfix") == 0) {
5167 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
5168 os_strlen(param));
5169 }
5170
5171 if (os_strcmp(cmd, "noa") == 0) {
5172 char *pos;
5173 int count, start, duration;
5174 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
5175 count = atoi(param);
5176 pos = os_strchr(param, ',');
5177 if (pos == NULL)
5178 return -1;
5179 pos++;
5180 start = atoi(pos);
5181 pos = os_strchr(pos, ',');
5182 if (pos == NULL)
5183 return -1;
5184 pos++;
5185 duration = atoi(pos);
5186 if (count < 0 || count > 255 || start < 0 || duration < 0)
5187 return -1;
5188 if (count == 0 && duration > 0)
5189 return -1;
5190 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
5191 "start=%d duration=%d", count, start, duration);
5192 return wpas_p2p_set_noa(wpa_s, count, start, duration);
5193 }
5194
5195 if (os_strcmp(cmd, "ps") == 0)
5196 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
5197
5198 if (os_strcmp(cmd, "oppps") == 0)
5199 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
5200
5201 if (os_strcmp(cmd, "ctwindow") == 0)
5202 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
5203
5204 if (os_strcmp(cmd, "disabled") == 0) {
5205 wpa_s->global->p2p_disabled = atoi(param);
5206 wpa_printf(MSG_DEBUG, "P2P functionality %s",
5207 wpa_s->global->p2p_disabled ?
5208 "disabled" : "enabled");
5209 if (wpa_s->global->p2p_disabled) {
5210 wpas_p2p_stop_find(wpa_s);
5211 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5212 p2p_flush(wpa_s->global->p2p);
5213 }
5214 return 0;
5215 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005216
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07005217 if (os_strcmp(cmd, "conc_pref") == 0) {
5218 if (os_strcmp(param, "sta") == 0)
5219 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
5220 else if (os_strcmp(param, "p2p") == 0)
5221 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005222 else {
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07005223 wpa_printf(MSG_INFO, "Invalid conc_pref value");
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005224 return -1;
5225 }
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07005226 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
Dmitry Shmidt04949592012-07-19 12:16:46 -07005227 "%s", param);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005228 return 0;
5229 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005231 if (os_strcmp(cmd, "force_long_sd") == 0) {
5232 wpa_s->force_long_sd = atoi(param);
5233 return 0;
5234 }
5235
5236 if (os_strcmp(cmd, "peer_filter") == 0) {
5237 u8 addr[ETH_ALEN];
5238 if (hwaddr_aton(param, addr))
5239 return -1;
5240 p2p_set_peer_filter(wpa_s->global->p2p, addr);
5241 return 0;
5242 }
5243
5244 if (os_strcmp(cmd, "cross_connect") == 0)
5245 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
5246
5247 if (os_strcmp(cmd, "go_apsd") == 0) {
5248 if (os_strcmp(param, "disable") == 0)
5249 wpa_s->set_ap_uapsd = 0;
5250 else {
5251 wpa_s->set_ap_uapsd = 1;
5252 wpa_s->ap_uapsd = atoi(param);
5253 }
5254 return 0;
5255 }
5256
5257 if (os_strcmp(cmd, "client_apsd") == 0) {
5258 if (os_strcmp(param, "disable") == 0)
5259 wpa_s->set_sta_uapsd = 0;
5260 else {
5261 int be, bk, vi, vo;
5262 char *pos;
5263 /* format: BE,BK,VI,VO;max SP Length */
5264 be = atoi(param);
5265 pos = os_strchr(param, ',');
5266 if (pos == NULL)
5267 return -1;
5268 pos++;
5269 bk = atoi(pos);
5270 pos = os_strchr(pos, ',');
5271 if (pos == NULL)
5272 return -1;
5273 pos++;
5274 vi = atoi(pos);
5275 pos = os_strchr(pos, ',');
5276 if (pos == NULL)
5277 return -1;
5278 pos++;
5279 vo = atoi(pos);
5280 /* ignore max SP Length for now */
5281
5282 wpa_s->set_sta_uapsd = 1;
5283 wpa_s->sta_uapsd = 0;
5284 if (be)
5285 wpa_s->sta_uapsd |= BIT(0);
5286 if (bk)
5287 wpa_s->sta_uapsd |= BIT(1);
5288 if (vi)
5289 wpa_s->sta_uapsd |= BIT(2);
5290 if (vo)
5291 wpa_s->sta_uapsd |= BIT(3);
5292 }
5293 return 0;
5294 }
5295
Dmitry Shmidt04949592012-07-19 12:16:46 -07005296 if (os_strcmp(cmd, "disallow_freq") == 0)
5297 return p2p_ctrl_disallow_freq(wpa_s, param);
5298
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005299 if (os_strcmp(cmd, "disc_int") == 0) {
5300 int min_disc_int, max_disc_int, max_disc_tu;
5301 char *pos;
5302
5303 pos = param;
5304
5305 min_disc_int = atoi(pos);
5306 pos = os_strchr(pos, ' ');
5307 if (pos == NULL)
5308 return -1;
5309 *pos++ = '\0';
5310
5311 max_disc_int = atoi(pos);
5312 pos = os_strchr(pos, ' ');
5313 if (pos == NULL)
5314 return -1;
5315 *pos++ = '\0';
5316
5317 max_disc_tu = atoi(pos);
5318
5319 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
5320 max_disc_int, max_disc_tu);
5321 }
5322
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005323 if (os_strcmp(cmd, "per_sta_psk") == 0) {
5324 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
5325 return 0;
5326 }
5327
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005328#ifdef CONFIG_WPS_NFC
5329 if (os_strcmp(cmd, "nfc_tag") == 0)
5330 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
5331#endif /* CONFIG_WPS_NFC */
5332
5333 if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
5334 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
5335 return 0;
5336 }
5337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005338 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
5339 cmd);
5340
5341 return -1;
5342}
5343
5344
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005345static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
5346{
5347 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5348 wpa_s->force_long_sd = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005349 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005350 if (wpa_s->global->p2p)
5351 p2p_flush(wpa_s->global->p2p);
5352}
5353
5354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005355static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
5356{
5357 char *pos, *pos2;
5358 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
5359
5360 if (cmd[0]) {
5361 pos = os_strchr(cmd, ' ');
5362 if (pos == NULL)
5363 return -1;
5364 *pos++ = '\0';
5365 dur1 = atoi(cmd);
5366
5367 pos2 = os_strchr(pos, ' ');
5368 if (pos2)
5369 *pos2++ = '\0';
5370 int1 = atoi(pos);
5371 } else
5372 pos2 = NULL;
5373
5374 if (pos2) {
5375 pos = os_strchr(pos2, ' ');
5376 if (pos == NULL)
5377 return -1;
5378 *pos++ = '\0';
5379 dur2 = atoi(pos2);
5380 int2 = atoi(pos);
5381 }
5382
5383 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
5384}
5385
5386
5387static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
5388{
5389 char *pos;
5390 unsigned int period = 0, interval = 0;
5391
5392 if (cmd[0]) {
5393 pos = os_strchr(cmd, ' ');
5394 if (pos == NULL)
5395 return -1;
5396 *pos++ = '\0';
5397 period = atoi(cmd);
5398 interval = atoi(pos);
5399 }
5400
5401 return wpas_p2p_ext_listen(wpa_s, period, interval);
5402}
5403
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005404
5405static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
5406{
5407 const char *pos;
5408 u8 peer[ETH_ALEN];
5409 int iface_addr = 0;
5410
5411 pos = cmd;
5412 if (os_strncmp(pos, "iface=", 6) == 0) {
5413 iface_addr = 1;
5414 pos += 6;
5415 }
5416 if (hwaddr_aton(pos, peer))
5417 return -1;
5418
5419 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
5420 return 0;
5421}
5422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005423#endif /* CONFIG_P2P */
5424
5425
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005426static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
5427{
5428 struct wpa_freq_range_list ranges;
5429 int *freqs = NULL;
5430 struct hostapd_hw_modes *mode;
5431 u16 i;
5432
5433 if (wpa_s->hw.modes == NULL)
5434 return NULL;
5435
5436 os_memset(&ranges, 0, sizeof(ranges));
5437 if (freq_range_list_parse(&ranges, val) < 0)
5438 return NULL;
5439
5440 for (i = 0; i < wpa_s->hw.num_modes; i++) {
5441 int j;
5442
5443 mode = &wpa_s->hw.modes[i];
5444 for (j = 0; j < mode->num_channels; j++) {
5445 unsigned int freq;
5446
5447 if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
5448 continue;
5449
5450 freq = mode->channels[j].freq;
5451 if (!freq_range_list_includes(&ranges, freq))
5452 continue;
5453
5454 int_array_add_unique(&freqs, freq);
5455 }
5456 }
5457
5458 os_free(ranges.range);
5459 return freqs;
5460}
5461
5462
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005463#ifdef CONFIG_INTERWORKING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005464
5465static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
5466{
5467 int auto_sel = 0;
5468 int *freqs = NULL;
5469
5470 if (param) {
5471 char *pos;
5472
5473 auto_sel = os_strstr(param, "auto") != NULL;
5474
5475 pos = os_strstr(param, "freq=");
5476 if (pos) {
5477 freqs = freq_range_to_channel_list(wpa_s, pos + 5);
5478 if (freqs == NULL)
5479 return -1;
5480 }
5481
5482 }
5483
5484 return interworking_select(wpa_s, auto_sel, freqs);
5485}
5486
5487
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005488static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
5489{
5490 u8 bssid[ETH_ALEN];
5491 struct wpa_bss *bss;
5492
5493 if (hwaddr_aton(dst, bssid)) {
5494 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
5495 return -1;
5496 }
5497
5498 bss = wpa_bss_get_bssid(wpa_s, bssid);
5499 if (bss == NULL) {
5500 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
5501 MAC2STR(bssid));
5502 return -1;
5503 }
5504
5505 return interworking_connect(wpa_s, bss);
5506}
5507
5508
5509static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
5510{
5511 u8 dst_addr[ETH_ALEN];
5512 int used;
5513 char *pos;
5514#define MAX_ANQP_INFO_ID 100
5515 u16 id[MAX_ANQP_INFO_ID];
5516 size_t num_id = 0;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005517 u32 subtypes = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005518
5519 used = hwaddr_aton2(dst, dst_addr);
5520 if (used < 0)
5521 return -1;
5522 pos = dst + used;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005523 if (*pos == ' ')
5524 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005525 while (num_id < MAX_ANQP_INFO_ID) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07005526 if (os_strncmp(pos, "hs20:", 5) == 0) {
5527#ifdef CONFIG_HS20
5528 int num = atoi(pos + 5);
5529 if (num <= 0 || num > 31)
5530 return -1;
5531 subtypes |= BIT(num);
5532#else /* CONFIG_HS20 */
5533 return -1;
5534#endif /* CONFIG_HS20 */
5535 } else {
5536 id[num_id] = atoi(pos);
5537 if (id[num_id])
5538 num_id++;
5539 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005540 pos = os_strchr(pos + 1, ',');
5541 if (pos == NULL)
5542 break;
5543 pos++;
5544 }
5545
5546 if (num_id == 0)
5547 return -1;
5548
Dmitry Shmidt15907092014-03-25 10:42:57 -07005549 return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005550}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005551
5552
5553static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
5554{
5555 u8 dst_addr[ETH_ALEN];
5556 struct wpabuf *advproto, *query = NULL;
5557 int used, ret = -1;
5558 char *pos, *end;
5559 size_t len;
5560
5561 used = hwaddr_aton2(cmd, dst_addr);
5562 if (used < 0)
5563 return -1;
5564
5565 pos = cmd + used;
5566 while (*pos == ' ')
5567 pos++;
5568
5569 /* Advertisement Protocol ID */
5570 end = os_strchr(pos, ' ');
5571 if (end)
5572 len = end - pos;
5573 else
5574 len = os_strlen(pos);
5575 if (len & 0x01)
5576 return -1;
5577 len /= 2;
5578 if (len == 0)
5579 return -1;
5580 advproto = wpabuf_alloc(len);
5581 if (advproto == NULL)
5582 return -1;
5583 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
5584 goto fail;
5585
5586 if (end) {
5587 /* Optional Query Request */
5588 pos = end + 1;
5589 while (*pos == ' ')
5590 pos++;
5591
5592 len = os_strlen(pos);
5593 if (len) {
5594 if (len & 0x01)
5595 goto fail;
5596 len /= 2;
5597 if (len == 0)
5598 goto fail;
5599 query = wpabuf_alloc(len);
5600 if (query == NULL)
5601 goto fail;
5602 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
5603 goto fail;
5604 }
5605 }
5606
5607 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
5608
5609fail:
5610 wpabuf_free(advproto);
5611 wpabuf_free(query);
5612
5613 return ret;
5614}
5615
5616
5617static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
5618 size_t buflen)
5619{
5620 u8 addr[ETH_ALEN];
5621 int dialog_token;
5622 int used;
5623 char *pos;
5624 size_t resp_len, start, requested_len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005625 struct wpabuf *resp;
5626 int ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005627
5628 used = hwaddr_aton2(cmd, addr);
5629 if (used < 0)
5630 return -1;
5631
5632 pos = cmd + used;
5633 while (*pos == ' ')
5634 pos++;
5635 dialog_token = atoi(pos);
5636
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005637 if (wpa_s->last_gas_resp &&
5638 os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
5639 dialog_token == wpa_s->last_gas_dialog_token)
5640 resp = wpa_s->last_gas_resp;
5641 else if (wpa_s->prev_gas_resp &&
5642 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
5643 dialog_token == wpa_s->prev_gas_dialog_token)
5644 resp = wpa_s->prev_gas_resp;
5645 else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005646 return -1;
5647
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005648 resp_len = wpabuf_len(resp);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005649 start = 0;
5650 requested_len = resp_len;
5651
5652 pos = os_strchr(pos, ' ');
5653 if (pos) {
5654 start = atoi(pos);
5655 if (start > resp_len)
5656 return os_snprintf(buf, buflen, "FAIL-Invalid range");
5657 pos = os_strchr(pos, ',');
5658 if (pos == NULL)
5659 return -1;
5660 pos++;
5661 requested_len = atoi(pos);
5662 if (start + requested_len > resp_len)
5663 return os_snprintf(buf, buflen, "FAIL-Invalid range");
5664 }
5665
5666 if (requested_len * 2 + 1 > buflen)
5667 return os_snprintf(buf, buflen, "FAIL-Too long response");
5668
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005669 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
5670 requested_len);
5671
5672 if (start + requested_len == resp_len) {
5673 /*
5674 * Free memory by dropping the response after it has been
5675 * fetched.
5676 */
5677 if (resp == wpa_s->prev_gas_resp) {
5678 wpabuf_free(wpa_s->prev_gas_resp);
5679 wpa_s->prev_gas_resp = NULL;
5680 } else {
5681 wpabuf_free(wpa_s->last_gas_resp);
5682 wpa_s->last_gas_resp = NULL;
5683 }
5684 }
5685
5686 return ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005687}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005688#endif /* CONFIG_INTERWORKING */
5689
5690
Dmitry Shmidt04949592012-07-19 12:16:46 -07005691#ifdef CONFIG_HS20
5692
5693static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
5694{
5695 u8 dst_addr[ETH_ALEN];
5696 int used;
5697 char *pos;
5698 u32 subtypes = 0;
5699
5700 used = hwaddr_aton2(dst, dst_addr);
5701 if (used < 0)
5702 return -1;
5703 pos = dst + used;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005704 if (*pos == ' ')
5705 pos++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005706 for (;;) {
5707 int num = atoi(pos);
5708 if (num <= 0 || num > 31)
5709 return -1;
5710 subtypes |= BIT(num);
5711 pos = os_strchr(pos + 1, ',');
5712 if (pos == NULL)
5713 break;
5714 pos++;
5715 }
5716
5717 if (subtypes == 0)
5718 return -1;
5719
5720 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
5721}
5722
5723
5724static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5725 const u8 *addr, const char *realm)
5726{
5727 u8 *buf;
5728 size_t rlen, len;
5729 int ret;
5730
5731 rlen = os_strlen(realm);
5732 len = 3 + rlen;
5733 buf = os_malloc(len);
5734 if (buf == NULL)
5735 return -1;
5736 buf[0] = 1; /* NAI Home Realm Count */
5737 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
5738 buf[2] = rlen;
5739 os_memcpy(buf + 3, realm, rlen);
5740
5741 ret = hs20_anqp_send_req(wpa_s, addr,
5742 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5743 buf, len);
5744
5745 os_free(buf);
5746
5747 return ret;
5748}
5749
5750
5751static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5752 char *dst)
5753{
5754 struct wpa_cred *cred = wpa_s->conf->cred;
5755 u8 dst_addr[ETH_ALEN];
5756 int used;
5757 u8 *buf;
5758 size_t len;
5759 int ret;
5760
5761 used = hwaddr_aton2(dst, dst_addr);
5762 if (used < 0)
5763 return -1;
5764
5765 while (dst[used] == ' ')
5766 used++;
5767 if (os_strncmp(dst + used, "realm=", 6) == 0)
5768 return hs20_nai_home_realm_list(wpa_s, dst_addr,
5769 dst + used + 6);
5770
5771 len = os_strlen(dst + used);
5772
5773 if (len == 0 && cred && cred->realm)
5774 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
5775
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07005776 if (len & 1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07005777 return -1;
5778 len /= 2;
5779 buf = os_malloc(len);
5780 if (buf == NULL)
5781 return -1;
5782 if (hexstr2bin(dst + used, buf, len) < 0) {
5783 os_free(buf);
5784 return -1;
5785 }
5786
5787 ret = hs20_anqp_send_req(wpa_s, dst_addr,
5788 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5789 buf, len);
5790 os_free(buf);
5791
5792 return ret;
5793}
5794
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005795
5796static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
5797{
5798 u8 dst_addr[ETH_ALEN];
5799 int used;
5800 char *icon;
5801
5802 used = hwaddr_aton2(cmd, dst_addr);
5803 if (used < 0)
5804 return -1;
5805
5806 while (cmd[used] == ' ')
5807 used++;
5808 icon = &cmd[used];
5809
5810 wpa_s->fetch_osu_icon_in_progress = 0;
5811 return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
5812 (u8 *) icon, os_strlen(icon));
5813}
5814
Dmitry Shmidt04949592012-07-19 12:16:46 -07005815#endif /* CONFIG_HS20 */
5816
5817
Dmitry Shmidt04949592012-07-19 12:16:46 -07005818#ifdef CONFIG_AUTOSCAN
5819
5820static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
5821 char *cmd)
5822{
5823 enum wpa_states state = wpa_s->wpa_state;
5824 char *new_params = NULL;
5825
5826 if (os_strlen(cmd) > 0) {
5827 new_params = os_strdup(cmd);
5828 if (new_params == NULL)
5829 return -1;
5830 }
5831
5832 os_free(wpa_s->conf->autoscan);
5833 wpa_s->conf->autoscan = new_params;
5834
5835 if (wpa_s->conf->autoscan == NULL)
5836 autoscan_deinit(wpa_s);
5837 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
5838 autoscan_init(wpa_s, 1);
5839 else if (state == WPA_SCANNING)
5840 wpa_supplicant_reinit_autoscan(wpa_s);
5841
5842 return 0;
5843}
5844
5845#endif /* CONFIG_AUTOSCAN */
5846
5847
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005848#ifdef CONFIG_WNM
5849
5850static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
5851{
5852 int enter;
5853 int intval = 0;
5854 char *pos;
5855 int ret;
5856 struct wpabuf *tfs_req = NULL;
5857
5858 if (os_strncmp(cmd, "enter", 5) == 0)
5859 enter = 1;
5860 else if (os_strncmp(cmd, "exit", 4) == 0)
5861 enter = 0;
5862 else
5863 return -1;
5864
5865 pos = os_strstr(cmd, " interval=");
5866 if (pos)
5867 intval = atoi(pos + 10);
5868
5869 pos = os_strstr(cmd, " tfs_req=");
5870 if (pos) {
5871 char *end;
5872 size_t len;
5873 pos += 9;
5874 end = os_strchr(pos, ' ');
5875 if (end)
5876 len = end - pos;
5877 else
5878 len = os_strlen(pos);
5879 if (len & 1)
5880 return -1;
5881 len /= 2;
5882 tfs_req = wpabuf_alloc(len);
5883 if (tfs_req == NULL)
5884 return -1;
5885 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
5886 wpabuf_free(tfs_req);
5887 return -1;
5888 }
5889 }
5890
5891 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
5892 WNM_SLEEP_MODE_EXIT, intval,
5893 tfs_req);
5894 wpabuf_free(tfs_req);
5895
5896 return ret;
5897}
5898
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005899
5900static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
5901{
5902 int query_reason;
5903
5904 query_reason = atoi(cmd);
5905
5906 wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
5907 query_reason);
5908
5909 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
5910}
5911
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005912#endif /* CONFIG_WNM */
5913
5914
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005915static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
5916 size_t buflen)
5917{
5918 struct wpa_signal_info si;
5919 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005920 char *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005921
5922 ret = wpa_drv_signal_poll(wpa_s, &si);
5923 if (ret)
5924 return -1;
5925
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005926 pos = buf;
5927 end = buf + buflen;
5928
5929 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005930 "NOISE=%d\nFREQUENCY=%u\n",
5931 si.current_signal, si.current_txrate / 1000,
5932 si.current_noise, si.frequency);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005933 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005934 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005935 pos += ret;
5936
5937 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
5938 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005939 channel_width_to_string(si.chanwidth));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005940 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005941 return -1;
5942 pos += ret;
5943 }
5944
5945 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
5946 ret = os_snprintf(pos, end - pos,
5947 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
5948 si.center_frq1, si.center_frq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005949 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005950 return -1;
5951 pos += ret;
5952 }
5953
5954 if (si.avg_signal) {
5955 ret = os_snprintf(pos, end - pos,
5956 "AVG_RSSI=%d\n", si.avg_signal);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005957 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005958 return -1;
5959 pos += ret;
5960 }
5961
5962 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005963}
5964
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005965
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005966static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
5967 size_t buflen)
5968{
5969 struct hostap_sta_driver_data sta;
5970 int ret;
5971
5972 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
5973 if (ret)
5974 return -1;
5975
5976 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005977 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005978 if (os_snprintf_error(buflen, ret))
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005979 return -1;
5980 return ret;
5981}
5982
5983
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005984#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005985static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5986 char *buf, size_t buflen)
5987{
5988 int ret;
5989
5990 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005991 if (ret == 0) {
5992 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
5993 struct p2p_data *p2p = wpa_s->global->p2p;
5994 if (p2p) {
5995 char country[3];
5996 country[0] = cmd[8];
5997 country[1] = cmd[9];
5998 country[2] = 0x04;
5999 p2p_set_country(p2p, country);
6000 }
6001 }
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006002 ret = os_snprintf(buf, buflen, "%s\n", "OK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006003 if (os_snprintf_error(buflen, ret))
6004 ret = -1;
Dmitry Shmidt9432e122013-09-12 12:39:30 -07006005 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07006006 return ret;
6007}
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006008#endif /* ANDROID */
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07006009
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07006010
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006011static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
6012 char *buf, size_t buflen)
6013{
6014 int ret;
6015 char *pos;
6016 u8 *data = NULL;
6017 unsigned int vendor_id, subcmd;
6018 struct wpabuf *reply;
6019 size_t data_len = 0;
6020
6021 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
6022 vendor_id = strtoul(cmd, &pos, 16);
6023 if (!isblank(*pos))
6024 return -EINVAL;
6025
6026 subcmd = strtoul(pos, &pos, 10);
6027
6028 if (*pos != '\0') {
6029 if (!isblank(*pos++))
6030 return -EINVAL;
6031 data_len = os_strlen(pos);
6032 }
6033
6034 if (data_len) {
6035 data_len /= 2;
6036 data = os_malloc(data_len);
6037 if (!data)
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006038 return -1;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006039
6040 if (hexstr2bin(pos, data, data_len)) {
6041 wpa_printf(MSG_DEBUG,
6042 "Vendor command: wrong parameter format");
6043 os_free(data);
6044 return -EINVAL;
6045 }
6046 }
6047
6048 reply = wpabuf_alloc((buflen - 1) / 2);
6049 if (!reply) {
6050 os_free(data);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006051 return -1;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006052 }
6053
6054 ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
6055 reply);
6056
6057 if (ret == 0)
6058 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
6059 wpabuf_len(reply));
6060
6061 wpabuf_free(reply);
6062 os_free(data);
6063
6064 return ret;
6065}
6066
6067
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006068static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
6069{
6070 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
6071
6072#ifdef CONFIG_P2P
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006073 wpas_p2p_cancel(wpa_s);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006074 wpas_p2p_stop_find(wpa_s);
6075 p2p_ctrl_flush(wpa_s);
6076 wpas_p2p_group_remove(wpa_s, "*");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006077 wpas_p2p_service_flush(wpa_s);
6078 wpa_s->global->p2p_disabled = 0;
6079 wpa_s->global->p2p_per_sta_psk = 0;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08006080 wpa_s->conf->num_sec_device_types = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006081 wpa_s->p2p_disable_ip_addr_req = 0;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006082 os_free(wpa_s->global->p2p_go_avoid_freq.range);
6083 wpa_s->global->p2p_go_avoid_freq.range = NULL;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006084#endif /* CONFIG_P2P */
6085
6086#ifdef CONFIG_WPS_TESTING
6087 wps_version_number = 0x20;
6088 wps_testing_dummy_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006089 wps_corrupt_pkhash = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006090#endif /* CONFIG_WPS_TESTING */
6091#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006092 wpa_s->wps_fragment_size = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006093 wpas_wps_cancel(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006094 wps_registrar_flush(wpa_s->wps->registrar);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006095#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07006096 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006097 wpa_s->known_wps_freq = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006098
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006099#ifdef CONFIG_TDLS
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006100#ifdef CONFIG_TDLS_TESTING
6101 extern unsigned int tdls_testing;
6102 tdls_testing = 0;
6103#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006104 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
6105 wpa_tdls_enable(wpa_s->wpa, 1);
6106#endif /* CONFIG_TDLS */
6107
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006108 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
6109 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
6110
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006111 wpa_s->no_keep_alive = 0;
6112
6113 os_free(wpa_s->disallow_aps_bssid);
6114 wpa_s->disallow_aps_bssid = NULL;
6115 wpa_s->disallow_aps_bssid_count = 0;
6116 os_free(wpa_s->disallow_aps_ssid);
6117 wpa_s->disallow_aps_ssid = NULL;
6118 wpa_s->disallow_aps_ssid_count = 0;
6119
6120 wpa_s->set_sta_uapsd = 0;
6121 wpa_s->sta_uapsd = 0;
6122
6123 wpa_drv_radio_disable(wpa_s, 0);
6124
6125 wpa_bss_flush(wpa_s);
6126 wpa_blacklist_clear(wpa_s);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006127 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006128 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
6129 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08006130 wpa_config_flush_blobs(wpa_s->conf);
Dmitry Shmidt18463232014-01-24 12:29:41 -08006131 wpa_s->conf->auto_interworking = 0;
6132 wpa_s->conf->okc = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006133
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006134 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
6135 rsn_preauth_deinit(wpa_s->wpa);
6136
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006137 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
6138 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
6139 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
6140 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
6141
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006142 radio_remove_works(wpa_s, NULL, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006143 wpa_s->ext_work_in_progress = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006144
6145 wpa_s->next_ssid = NULL;
6146
6147#ifdef CONFIG_INTERWORKING
6148 hs20_cancel_fetch_osu(wpa_s);
6149#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt818ea482014-03-10 13:15:21 -07006150
6151 wpa_s->ext_mgmt_frame_handling = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006152 wpa_s->ext_eapol_frame_io = 0;
6153#ifdef CONFIG_TESTING_OPTIONS
6154 wpa_s->extra_roc_dur = 0;
6155#endif /* CONFIG_TESTING_OPTIONS */
6156
6157 wpa_s->disconnected = 0;
6158 os_free(wpa_s->next_scan_freqs);
6159 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006160}
6161
6162
6163static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
6164 char *buf, size_t buflen)
6165{
6166 struct wpa_radio_work *work;
6167 char *pos, *end;
6168 struct os_reltime now, diff;
6169
6170 pos = buf;
6171 end = buf + buflen;
6172
6173 os_get_reltime(&now);
6174
6175 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
6176 {
6177 int ret;
6178
6179 os_reltime_sub(&now, &work->time, &diff);
6180 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
6181 work->type, work->wpa_s->ifname, work->freq,
6182 work->started, diff.sec, diff.usec);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006183 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006184 break;
6185 pos += ret;
6186 }
6187
6188 return pos - buf;
6189}
6190
6191
6192static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
6193{
6194 struct wpa_radio_work *work = eloop_ctx;
6195 struct wpa_external_work *ework = work->ctx;
6196
6197 wpa_dbg(work->wpa_s, MSG_DEBUG,
6198 "Timing out external radio work %u (%s)",
6199 ework->id, work->type);
6200 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006201 work->wpa_s->ext_work_in_progress = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006202 radio_work_done(work);
Dmitry Shmidt71757432014-06-02 13:50:35 -07006203 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006204}
6205
6206
6207static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
6208{
6209 struct wpa_external_work *ework = work->ctx;
6210
6211 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006212 if (work->started)
6213 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
6214 work, NULL);
6215
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006216 os_free(ework);
6217 return;
6218 }
6219
6220 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
6221 ework->id, ework->type);
6222 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006223 work->wpa_s->ext_work_in_progress = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006224 if (!ework->timeout)
6225 ework->timeout = 10;
6226 eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
6227 work, NULL);
6228}
6229
6230
6231static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
6232 char *buf, size_t buflen)
6233{
6234 struct wpa_external_work *ework;
6235 char *pos, *pos2;
6236 size_t type_len;
6237 int ret;
6238 unsigned int freq = 0;
6239
6240 /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
6241
6242 ework = os_zalloc(sizeof(*ework));
6243 if (ework == NULL)
6244 return -1;
6245
6246 pos = os_strchr(cmd, ' ');
6247 if (pos) {
6248 type_len = pos - cmd;
6249 pos++;
6250
6251 pos2 = os_strstr(pos, "freq=");
6252 if (pos2)
6253 freq = atoi(pos2 + 5);
6254
6255 pos2 = os_strstr(pos, "timeout=");
6256 if (pos2)
6257 ework->timeout = atoi(pos2 + 8);
6258 } else {
6259 type_len = os_strlen(cmd);
6260 }
6261 if (4 + type_len >= sizeof(ework->type))
6262 type_len = sizeof(ework->type) - 4 - 1;
6263 os_strlcpy(ework->type, "ext:", sizeof(ework->type));
6264 os_memcpy(ework->type + 4, cmd, type_len);
6265 ework->type[4 + type_len] = '\0';
6266
6267 wpa_s->ext_work_id++;
6268 if (wpa_s->ext_work_id == 0)
6269 wpa_s->ext_work_id++;
6270 ework->id = wpa_s->ext_work_id;
6271
6272 if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
6273 ework) < 0) {
6274 os_free(ework);
6275 return -1;
6276 }
6277
6278 ret = os_snprintf(buf, buflen, "%u", ework->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006279 if (os_snprintf_error(buflen, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006280 return -1;
6281 return ret;
6282}
6283
6284
6285static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
6286{
6287 struct wpa_radio_work *work;
6288 unsigned int id = atoi(cmd);
6289
6290 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
6291 {
6292 struct wpa_external_work *ework;
6293
6294 if (os_strncmp(work->type, "ext:", 4) != 0)
6295 continue;
6296 ework = work->ctx;
6297 if (id && ework->id != id)
6298 continue;
6299 wpa_dbg(wpa_s, MSG_DEBUG,
6300 "Completed external radio work %u (%s)",
6301 ework->id, ework->type);
6302 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006303 wpa_s->ext_work_in_progress = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006304 radio_work_done(work);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07006305 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006306 return 3; /* "OK\n" */
6307 }
6308
6309 return -1;
6310}
6311
6312
6313static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
6314 char *buf, size_t buflen)
6315{
6316 if (os_strcmp(cmd, "show") == 0)
6317 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
6318 if (os_strncmp(cmd, "add ", 4) == 0)
6319 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
6320 if (os_strncmp(cmd, "done ", 5) == 0)
6321 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
6322 return -1;
6323}
6324
6325
6326void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
6327{
6328 struct wpa_radio_work *work, *tmp;
6329
Dmitry Shmidt18463232014-01-24 12:29:41 -08006330 if (!wpa_s || !wpa_s->radio)
6331 return;
6332
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006333 dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
6334 struct wpa_radio_work, list) {
6335 struct wpa_external_work *ework;
6336
6337 if (os_strncmp(work->type, "ext:", 4) != 0)
6338 continue;
6339 ework = work->ctx;
6340 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07006341 "Flushing%s external radio work %u (%s)",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006342 work->started ? " started" : "", ework->id,
6343 ework->type);
6344 if (work->started)
6345 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
6346 work, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006347 radio_work_done(work);
Dmitry Shmidt71757432014-06-02 13:50:35 -07006348 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006349 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006350}
6351
6352
Dmitry Shmidt051af732013-10-22 13:52:46 -07006353static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
6354{
6355 struct wpa_supplicant *wpa_s = eloop_ctx;
6356 eapol_sm_notify_ctrl_response(wpa_s->eapol);
6357}
6358
6359
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006360static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
6361 unsigned int *scan_id_count, int scan_id[])
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006362{
6363 const char *pos = value;
6364
6365 while (pos) {
6366 if (*pos == ' ' || *pos == '\0')
6367 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006368 if (*scan_id_count == MAX_SCAN_ID)
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006369 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006370 scan_id[(*scan_id_count)++] = atoi(pos);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006371 pos = os_strchr(pos, ',');
6372 if (pos)
6373 pos++;
6374 }
6375
6376 return 0;
6377}
6378
6379
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006380static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
6381 char *reply, int reply_size, int *reply_len)
6382{
6383 char *pos;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006384 unsigned int manual_scan_passive = 0;
6385 unsigned int manual_scan_use_id = 0;
6386 unsigned int manual_scan_only_new = 0;
6387 unsigned int scan_only = 0;
6388 unsigned int scan_id_count = 0;
6389 int scan_id[MAX_SCAN_ID];
6390 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
6391 struct wpa_scan_results *scan_res);
6392 int *manual_scan_freqs = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006393
6394 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6395 *reply_len = -1;
6396 return;
6397 }
6398
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006399 if (radio_work_pending(wpa_s, "scan")) {
6400 wpa_printf(MSG_DEBUG,
6401 "Pending scan scheduled - reject new request");
6402 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
6403 return;
6404 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006405
6406 if (params) {
6407 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006408 scan_only = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006409
6410 pos = os_strstr(params, "freq=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006411 if (pos) {
6412 manual_scan_freqs = freq_range_to_channel_list(wpa_s,
6413 pos + 5);
6414 if (manual_scan_freqs == NULL) {
6415 *reply_len = -1;
6416 goto done;
6417 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006418 }
6419
6420 pos = os_strstr(params, "passive=");
6421 if (pos)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006422 manual_scan_passive = !!atoi(pos + 8);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006423
6424 pos = os_strstr(params, "use_id=");
6425 if (pos)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006426 manual_scan_use_id = atoi(pos + 7);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006427
6428 pos = os_strstr(params, "only_new=1");
6429 if (pos)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006430 manual_scan_only_new = 1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006431
6432 pos = os_strstr(params, "scan_id=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006433 if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
6434 scan_id) < 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006435 *reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006436 goto done;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006437 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006438 }
6439
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006440 if (scan_only)
6441 scan_res_handler = scan_only_handler;
6442 else if (wpa_s->scan_res_handler == scan_only_handler)
6443 scan_res_handler = NULL;
6444 else
6445 scan_res_handler = wpa_s->scan_res_handler;
6446
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006447 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
6448 ((wpa_s->wpa_state <= WPA_SCANNING) ||
6449 (wpa_s->wpa_state == WPA_COMPLETED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006450 wpa_s->manual_scan_passive = manual_scan_passive;
6451 wpa_s->manual_scan_use_id = manual_scan_use_id;
6452 wpa_s->manual_scan_only_new = manual_scan_only_new;
6453 wpa_s->scan_id_count = scan_id_count;
6454 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
6455 wpa_s->scan_res_handler = scan_res_handler;
6456 os_free(wpa_s->manual_scan_freqs);
6457 wpa_s->manual_scan_freqs = manual_scan_freqs;
6458 manual_scan_freqs = NULL;
6459
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006460 wpa_s->normal_scans = 0;
6461 wpa_s->scan_req = MANUAL_SCAN_REQ;
6462 wpa_s->after_wps = 0;
6463 wpa_s->known_wps_freq = 0;
6464 wpa_supplicant_req_scan(wpa_s, 0, 0);
6465 if (wpa_s->manual_scan_use_id) {
6466 wpa_s->manual_scan_id++;
6467 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
6468 wpa_s->manual_scan_id);
6469 *reply_len = os_snprintf(reply, reply_size, "%u\n",
6470 wpa_s->manual_scan_id);
6471 }
6472 } else if (wpa_s->sched_scanning) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006473 wpa_s->manual_scan_passive = manual_scan_passive;
6474 wpa_s->manual_scan_use_id = manual_scan_use_id;
6475 wpa_s->manual_scan_only_new = manual_scan_only_new;
6476 wpa_s->scan_id_count = scan_id_count;
6477 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
6478 wpa_s->scan_res_handler = scan_res_handler;
6479 os_free(wpa_s->manual_scan_freqs);
6480 wpa_s->manual_scan_freqs = manual_scan_freqs;
6481 manual_scan_freqs = NULL;
6482
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006483 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
6484 wpa_supplicant_cancel_sched_scan(wpa_s);
6485 wpa_s->scan_req = MANUAL_SCAN_REQ;
6486 wpa_supplicant_req_scan(wpa_s, 0, 0);
6487 if (wpa_s->manual_scan_use_id) {
6488 wpa_s->manual_scan_id++;
6489 *reply_len = os_snprintf(reply, reply_size, "%u\n",
6490 wpa_s->manual_scan_id);
6491 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
6492 wpa_s->manual_scan_id);
6493 }
6494 } else {
6495 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
6496 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
6497 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006498
6499done:
6500 os_free(manual_scan_freqs);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006501}
6502
6503
Dmitry Shmidt818ea482014-03-10 13:15:21 -07006504#ifdef CONFIG_TESTING_OPTIONS
6505
6506static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
6507 unsigned int freq, const u8 *dst,
6508 const u8 *src, const u8 *bssid,
6509 const u8 *data, size_t data_len,
6510 enum offchannel_send_action_result
6511 result)
6512{
6513 wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
6514 " src=" MACSTR " bssid=" MACSTR " result=%s",
6515 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
6516 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
6517 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
6518 "NO_ACK" : "FAILED"));
6519}
6520
6521
6522static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
6523{
6524 char *pos, *param;
6525 size_t len;
6526 u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
6527 int res, used;
6528 int freq = 0, no_cck = 0, wait_time = 0;
6529
6530 /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
6531 * <action=Action frame payload> */
6532
6533 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
6534
6535 pos = cmd;
6536 used = hwaddr_aton2(pos, da);
6537 if (used < 0)
6538 return -1;
6539 pos += used;
6540 while (*pos == ' ')
6541 pos++;
6542 used = hwaddr_aton2(pos, bssid);
6543 if (used < 0)
6544 return -1;
6545 pos += used;
6546
6547 param = os_strstr(pos, " freq=");
6548 if (param) {
6549 param += 6;
6550 freq = atoi(param);
6551 }
6552
6553 param = os_strstr(pos, " no_cck=");
6554 if (param) {
6555 param += 8;
6556 no_cck = atoi(param);
6557 }
6558
6559 param = os_strstr(pos, " wait_time=");
6560 if (param) {
6561 param += 11;
6562 wait_time = atoi(param);
6563 }
6564
6565 param = os_strstr(pos, " action=");
6566 if (param == NULL)
6567 return -1;
6568 param += 8;
6569
6570 len = os_strlen(param);
6571 if (len & 1)
6572 return -1;
6573 len /= 2;
6574
6575 buf = os_malloc(len);
6576 if (buf == NULL)
6577 return -1;
6578
6579 if (hexstr2bin(param, buf, len) < 0) {
6580 os_free(buf);
6581 return -1;
6582 }
6583
6584 res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
6585 buf, len, wait_time,
6586 wpas_ctrl_iface_mgmt_tx_cb, no_cck);
6587 os_free(buf);
6588 return res;
6589}
6590
6591
6592static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
6593{
6594 wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
6595 offchannel_send_action_done(wpa_s);
6596}
6597
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07006598
6599static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
6600{
6601 char *pos, *param;
6602 union wpa_event_data event;
6603 enum wpa_event_type ev;
6604
6605 /* <event name> [parameters..] */
6606
6607 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
6608
6609 pos = cmd;
6610 param = os_strchr(pos, ' ');
6611 if (param)
6612 *param++ = '\0';
6613
6614 os_memset(&event, 0, sizeof(event));
6615
6616 if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
6617 ev = EVENT_INTERFACE_ENABLED;
6618 } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
6619 ev = EVENT_INTERFACE_DISABLED;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006620 } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
6621 ev = EVENT_AVOID_FREQUENCIES;
6622 if (param == NULL)
6623 param = "";
6624 if (freq_range_list_parse(&event.freq_range, param) < 0)
6625 return -1;
6626 wpa_supplicant_event(wpa_s, ev, &event);
6627 os_free(event.freq_range.range);
6628 return 0;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07006629 } else {
6630 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
6631 cmd);
6632 return -1;
6633 }
6634
6635 wpa_supplicant_event(wpa_s, ev, &event);
6636
6637 return 0;
6638}
6639
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006640
6641static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
6642{
6643 char *pos;
6644 u8 src[ETH_ALEN], *buf;
6645 int used;
6646 size_t len;
6647
6648 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
6649
6650 pos = cmd;
6651 used = hwaddr_aton2(pos, src);
6652 if (used < 0)
6653 return -1;
6654 pos += used;
6655 while (*pos == ' ')
6656 pos++;
6657
6658 len = os_strlen(pos);
6659 if (len & 1)
6660 return -1;
6661 len /= 2;
6662
6663 buf = os_malloc(len);
6664 if (buf == NULL)
6665 return -1;
6666
6667 if (hexstr2bin(pos, buf, len) < 0) {
6668 os_free(buf);
6669 return -1;
6670 }
6671
6672 wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
6673 os_free(buf);
6674
6675 return 0;
6676}
6677
6678
6679static u16 ipv4_hdr_checksum(const void *buf, size_t len)
6680{
6681 size_t i;
6682 u32 sum = 0;
6683 const u16 *pos = buf;
6684
6685 for (i = 0; i < len / 2; i++)
6686 sum += *pos++;
6687
6688 while (sum >> 16)
6689 sum = (sum & 0xffff) + (sum >> 16);
6690
6691 return sum ^ 0xffff;
6692}
6693
6694
6695#define HWSIM_PACKETLEN 1500
6696#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
6697
6698void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
6699{
6700 struct wpa_supplicant *wpa_s = ctx;
6701 const struct ether_header *eth;
6702 const struct iphdr *ip;
6703 const u8 *pos;
6704 unsigned int i;
6705
6706 if (len != HWSIM_PACKETLEN)
6707 return;
6708
6709 eth = (const struct ether_header *) buf;
6710 ip = (const struct iphdr *) (eth + 1);
6711 pos = (const u8 *) (ip + 1);
6712
6713 if (ip->ihl != 5 || ip->version != 4 ||
6714 ntohs(ip->tot_len) != HWSIM_IP_LEN)
6715 return;
6716
6717 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++) {
6718 if (*pos != (u8) i)
6719 return;
6720 pos++;
6721 }
6722
6723 wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
6724 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
6725}
6726
6727
6728static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
6729 char *cmd)
6730{
6731 int enabled = atoi(cmd);
6732
6733 if (!enabled) {
6734 if (wpa_s->l2_test) {
6735 l2_packet_deinit(wpa_s->l2_test);
6736 wpa_s->l2_test = NULL;
6737 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
6738 }
6739 return 0;
6740 }
6741
6742 if (wpa_s->l2_test)
6743 return 0;
6744
6745 wpa_s->l2_test = l2_packet_init(wpa_s->ifname, wpa_s->own_addr,
6746 ETHERTYPE_IP, wpas_data_test_rx,
6747 wpa_s, 1);
6748 if (wpa_s->l2_test == NULL)
6749 return -1;
6750
6751 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
6752
6753 return 0;
6754}
6755
6756
6757static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
6758{
6759 u8 dst[ETH_ALEN], src[ETH_ALEN];
6760 char *pos;
6761 int used;
6762 long int val;
6763 u8 tos;
6764 u8 buf[HWSIM_PACKETLEN];
6765 struct ether_header *eth;
6766 struct iphdr *ip;
6767 u8 *dpos;
6768 unsigned int i;
6769
6770 if (wpa_s->l2_test == NULL)
6771 return -1;
6772
6773 /* format: <dst> <src> <tos> */
6774
6775 pos = cmd;
6776 used = hwaddr_aton2(pos, dst);
6777 if (used < 0)
6778 return -1;
6779 pos += used;
6780 while (*pos == ' ')
6781 pos++;
6782 used = hwaddr_aton2(pos, src);
6783 if (used < 0)
6784 return -1;
6785 pos += used;
6786
6787 val = strtol(pos, NULL, 0);
6788 if (val < 0 || val > 0xff)
6789 return -1;
6790 tos = val;
6791
6792 eth = (struct ether_header *) buf;
6793 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
6794 os_memcpy(eth->ether_shost, src, ETH_ALEN);
6795 eth->ether_type = htons(ETHERTYPE_IP);
6796 ip = (struct iphdr *) (eth + 1);
6797 os_memset(ip, 0, sizeof(*ip));
6798 ip->ihl = 5;
6799 ip->version = 4;
6800 ip->ttl = 64;
6801 ip->tos = tos;
6802 ip->tot_len = htons(HWSIM_IP_LEN);
6803 ip->protocol = 1;
6804 ip->saddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 1);
6805 ip->daddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 2);
6806 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
6807 dpos = (u8 *) (ip + 1);
6808 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
6809 *dpos++ = i;
6810
6811 if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, buf,
6812 HWSIM_PACKETLEN) < 0)
6813 return -1;
6814
6815 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
6816 " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
6817
6818 return 0;
6819}
6820
6821
6822static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
6823 char *cmd)
6824{
6825 u8 *buf;
6826 struct ether_header *eth;
6827 struct l2_packet_data *l2 = NULL;
6828 size_t len;
6829 u16 ethertype;
6830 int res = -1;
6831
6832 len = os_strlen(cmd);
6833 if (len & 1 || len < ETH_HLEN * 2)
6834 return -1;
6835 len /= 2;
6836
6837 buf = os_malloc(len);
6838 if (buf == NULL)
6839 return -1;
6840
6841 if (hexstr2bin(cmd, buf, len) < 0)
6842 goto done;
6843
6844 eth = (struct ether_header *) buf;
6845 ethertype = ntohs(eth->ether_type);
6846
6847 l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
6848 wpas_data_test_rx, wpa_s, 1);
6849 if (l2 == NULL)
6850 goto done;
6851
6852 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
6853 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
6854done:
6855 if (l2)
6856 l2_packet_deinit(l2);
6857 os_free(buf);
6858
6859 return res < 0 ? -1 : 0;
6860}
6861
Dmitry Shmidt818ea482014-03-10 13:15:21 -07006862#endif /* CONFIG_TESTING_OPTIONS */
6863
6864
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07006865static void wpas_ctrl_vendor_elem_update(struct wpa_supplicant *wpa_s)
6866{
6867 unsigned int i;
6868 char buf[30];
6869
6870 wpa_printf(MSG_DEBUG, "Update vendor elements");
6871
6872 for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
6873 if (wpa_s->vendor_elem[i]) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006874 int res;
6875
6876 res = os_snprintf(buf, sizeof(buf), "frame[%u]", i);
6877 if (!os_snprintf_error(sizeof(buf), res)) {
6878 wpa_hexdump_buf(MSG_DEBUG, buf,
6879 wpa_s->vendor_elem[i]);
6880 }
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07006881 }
6882 }
6883
6884#ifdef CONFIG_P2P
6885 if (wpa_s->parent == wpa_s &&
6886 wpa_s->global->p2p &&
6887 !wpa_s->global->p2p_disabled)
6888 p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
6889#endif /* CONFIG_P2P */
6890}
6891
6892
6893static struct wpa_supplicant *
6894wpas_ctrl_vendor_elem_iface(struct wpa_supplicant *wpa_s,
6895 enum wpa_vendor_elem_frame frame)
6896{
6897 switch (frame) {
6898#ifdef CONFIG_P2P
6899 case VENDOR_ELEM_PROBE_REQ_P2P:
6900 case VENDOR_ELEM_PROBE_RESP_P2P:
6901 case VENDOR_ELEM_PROBE_RESP_P2P_GO:
6902 case VENDOR_ELEM_BEACON_P2P_GO:
6903 case VENDOR_ELEM_P2P_PD_REQ:
6904 case VENDOR_ELEM_P2P_PD_RESP:
6905 case VENDOR_ELEM_P2P_GO_NEG_REQ:
6906 case VENDOR_ELEM_P2P_GO_NEG_RESP:
6907 case VENDOR_ELEM_P2P_GO_NEG_CONF:
6908 case VENDOR_ELEM_P2P_INV_REQ:
6909 case VENDOR_ELEM_P2P_INV_RESP:
6910 case VENDOR_ELEM_P2P_ASSOC_REQ:
6911 return wpa_s->parent;
6912#endif /* CONFIG_P2P */
6913 default:
6914 return wpa_s;
6915 }
6916}
6917
6918
6919static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
6920{
6921 char *pos = cmd;
6922 int frame;
6923 size_t len;
6924 struct wpabuf *buf;
6925 struct ieee802_11_elems elems;
6926
6927 frame = atoi(pos);
6928 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
6929 return -1;
6930 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
6931
6932 pos = os_strchr(pos, ' ');
6933 if (pos == NULL)
6934 return -1;
6935 pos++;
6936
6937 len = os_strlen(pos);
6938 if (len == 0)
6939 return 0;
6940 if (len & 1)
6941 return -1;
6942 len /= 2;
6943
6944 buf = wpabuf_alloc(len);
6945 if (buf == NULL)
6946 return -1;
6947
6948 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
6949 wpabuf_free(buf);
6950 return -1;
6951 }
6952
6953 if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
6954 ParseFailed) {
6955 wpabuf_free(buf);
6956 return -1;
6957 }
6958
6959 if (wpa_s->vendor_elem[frame] == NULL) {
6960 wpa_s->vendor_elem[frame] = buf;
6961 wpas_ctrl_vendor_elem_update(wpa_s);
6962 return 0;
6963 }
6964
6965 if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
6966 wpabuf_free(buf);
6967 return -1;
6968 }
6969
6970 wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
6971 wpabuf_free(buf);
6972 wpas_ctrl_vendor_elem_update(wpa_s);
6973
6974 return 0;
6975}
6976
6977
6978static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
6979 char *buf, size_t buflen)
6980{
6981 int frame = atoi(cmd);
6982
6983 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
6984 return -1;
6985 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
6986
6987 if (wpa_s->vendor_elem[frame] == NULL)
6988 return 0;
6989
6990 return wpa_snprintf_hex(buf, buflen,
6991 wpabuf_head_u8(wpa_s->vendor_elem[frame]),
6992 wpabuf_len(wpa_s->vendor_elem[frame]));
6993}
6994
6995
6996static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
6997{
6998 char *pos = cmd;
6999 int frame;
7000 size_t len;
7001 u8 *buf;
7002 struct ieee802_11_elems elems;
7003 u8 *ie, *end;
7004
7005 frame = atoi(pos);
7006 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
7007 return -1;
7008 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
7009
7010 pos = os_strchr(pos, ' ');
7011 if (pos == NULL)
7012 return -1;
7013 pos++;
7014
7015 if (*pos == '*') {
7016 wpabuf_free(wpa_s->vendor_elem[frame]);
7017 wpa_s->vendor_elem[frame] = NULL;
7018 wpas_ctrl_vendor_elem_update(wpa_s);
7019 return 0;
7020 }
7021
7022 if (wpa_s->vendor_elem[frame] == NULL)
7023 return -1;
7024
7025 len = os_strlen(pos);
7026 if (len == 0)
7027 return 0;
7028 if (len & 1)
7029 return -1;
7030 len /= 2;
7031
7032 buf = os_malloc(len);
7033 if (buf == NULL)
7034 return -1;
7035
7036 if (hexstr2bin(pos, buf, len) < 0) {
7037 os_free(buf);
7038 return -1;
7039 }
7040
7041 if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
7042 os_free(buf);
7043 return -1;
7044 }
7045
7046 ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
7047 end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
7048
7049 for (; ie + 1 < end; ie += 2 + ie[1]) {
7050 if (ie + len > end)
7051 break;
7052 if (os_memcmp(ie, buf, len) != 0)
7053 continue;
7054
7055 if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
7056 wpabuf_free(wpa_s->vendor_elem[frame]);
7057 wpa_s->vendor_elem[frame] = NULL;
7058 } else {
7059 os_memmove(ie, ie + len,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007060 end - (ie + len));
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07007061 wpa_s->vendor_elem[frame]->used -= len;
7062 }
7063 os_free(buf);
7064 wpas_ctrl_vendor_elem_update(wpa_s);
7065 return 0;
7066 }
7067
7068 os_free(buf);
7069
7070 return -1;
7071}
7072
7073
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007074static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
7075{
7076 struct wpa_supplicant *wpa_s = ctx;
7077
7078 if (neighbor_rep) {
7079 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
7080 "length=%u",
7081 (unsigned int) wpabuf_len(neighbor_rep));
7082 wpabuf_free(neighbor_rep);
7083 } else {
7084 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
7085 }
7086}
7087
7088
7089static int wpas_ctrl_iface_send_neigbor_rep(struct wpa_supplicant *wpa_s,
7090 char *cmd)
7091{
7092 struct wpa_ssid ssid;
7093 struct wpa_ssid *ssid_p = NULL;
7094 int ret = 0;
7095
7096 if (os_strncmp(cmd, " ssid=", 6) == 0) {
7097 ssid.ssid_len = os_strlen(cmd + 6);
7098 if (ssid.ssid_len > 32)
7099 return -1;
7100 ssid.ssid = (u8 *) (cmd + 6);
7101 ssid_p = &ssid;
7102 }
7103
7104 ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p,
7105 wpas_ctrl_neighbor_rep_cb,
7106 wpa_s);
7107
7108 return ret;
7109}
7110
7111
7112static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
7113{
7114 eapol_sm_erp_flush(wpa_s->eapol);
7115 return 0;
7116}
7117
7118
7119static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
7120 char *cmd)
7121{
7122 char *token, *context = NULL;
7123 unsigned int enable = ~0, type = 0;
7124 u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
7125 u8 *addr = NULL, *mask = NULL;
7126
7127 while ((token = str_token(cmd, " ", &context))) {
7128 if (os_strcasecmp(token, "scan") == 0) {
7129 type |= MAC_ADDR_RAND_SCAN;
7130 } else if (os_strcasecmp(token, "sched") == 0) {
7131 type |= MAC_ADDR_RAND_SCHED_SCAN;
7132 } else if (os_strcasecmp(token, "pno") == 0) {
7133 type |= MAC_ADDR_RAND_PNO;
7134 } else if (os_strcasecmp(token, "all") == 0) {
7135 type = wpa_s->mac_addr_rand_supported;
7136 } else if (os_strncasecmp(token, "enable=", 7) == 0) {
7137 enable = atoi(token + 7);
7138 } else if (os_strncasecmp(token, "addr=", 5) == 0) {
7139 addr = _addr;
7140 if (hwaddr_aton(token + 5, addr)) {
7141 wpa_printf(MSG_INFO,
7142 "CTRL: Invalid MAC address: %s",
7143 token);
7144 return -1;
7145 }
7146 } else if (os_strncasecmp(token, "mask=", 5) == 0) {
7147 mask = _mask;
7148 if (hwaddr_aton(token + 5, mask)) {
7149 wpa_printf(MSG_INFO,
7150 "CTRL: Invalid MAC address mask: %s",
7151 token);
7152 return -1;
7153 }
7154 } else {
7155 wpa_printf(MSG_INFO,
7156 "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
7157 token);
7158 return -1;
7159 }
7160 }
7161
7162 if (!type) {
7163 wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
7164 return -1;
7165 }
7166
7167 if ((wpa_s->mac_addr_rand_supported & type) != type) {
7168 wpa_printf(MSG_INFO,
7169 "CTRL: MAC_RAND_SCAN types=%u != supported=%u",
7170 type, wpa_s->mac_addr_rand_supported);
7171 return -1;
7172 }
7173
7174 if (enable > 1) {
7175 wpa_printf(MSG_INFO,
7176 "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
7177 return -1;
7178 }
7179
7180 if (!enable) {
7181 wpas_mac_addr_rand_scan_clear(wpa_s, type);
7182 if (wpa_s->pno) {
7183 if (type & MAC_ADDR_RAND_PNO) {
7184 wpas_stop_pno(wpa_s);
7185 wpas_start_pno(wpa_s);
7186 }
7187 } else if (wpa_s->sched_scanning &&
7188 (type & MAC_ADDR_RAND_SCHED_SCAN)) {
7189 /* simulate timeout to restart the sched scan */
7190 wpa_s->sched_scan_timed_out = 1;
7191 wpa_s->prev_sched_ssid = NULL;
7192 wpa_supplicant_cancel_sched_scan(wpa_s);
7193 }
7194 return 0;
7195 }
7196
7197 if ((addr && !mask) || (!addr && mask)) {
7198 wpa_printf(MSG_INFO,
7199 "CTRL: MAC_RAND_SCAN invalid addr/mask combination");
7200 return -1;
7201 }
7202
7203 if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
7204 wpa_printf(MSG_INFO,
7205 "CTRL: MAC_RAND_SCAN cannot allow multicast address");
7206 return -1;
7207 }
7208
7209 if (type & MAC_ADDR_RAND_SCAN) {
7210 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
7211 addr, mask);
7212 }
7213
7214 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
7215 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
7216 addr, mask);
7217
7218 if (wpa_s->sched_scanning && !wpa_s->pno) {
7219 /* simulate timeout to restart the sched scan */
7220 wpa_s->sched_scan_timed_out = 1;
7221 wpa_s->prev_sched_ssid = NULL;
7222 wpa_supplicant_cancel_sched_scan(wpa_s);
7223 }
7224 }
7225
7226 if (type & MAC_ADDR_RAND_PNO) {
7227 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
7228 addr, mask);
7229 if (wpa_s->pno) {
7230 wpas_stop_pno(wpa_s);
7231 wpas_start_pno(wpa_s);
7232 }
7233 }
7234
7235 return 0;
7236}
7237
7238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007239char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
7240 char *buf, size_t *resp_len)
7241{
7242 char *reply;
7243 const int reply_size = 4096;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007244 int reply_len;
7245
7246 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007247 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
7248 if (wpa_debug_show_keys)
7249 wpa_dbg(wpa_s, MSG_DEBUG,
7250 "Control interface command '%s'", buf);
7251 else
7252 wpa_dbg(wpa_s, MSG_DEBUG,
7253 "Control interface command '%s [REMOVED]'",
7254 os_strncmp(buf, WPA_CTRL_RSP,
7255 os_strlen(WPA_CTRL_RSP)) == 0 ?
7256 WPA_CTRL_RSP : "SET_NETWORK");
7257 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007258 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007259 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
7260 (const u8 *) buf, os_strlen(buf));
7261 } else {
7262 int level = MSG_DEBUG;
7263 if (os_strcmp(buf, "PING") == 0)
7264 level = MSG_EXCESSIVE;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007265 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007266 }
7267
7268 reply = os_malloc(reply_size);
7269 if (reply == NULL) {
7270 *resp_len = 1;
7271 return NULL;
7272 }
7273
7274 os_memcpy(reply, "OK\n", 3);
7275 reply_len = 3;
7276
7277 if (os_strcmp(buf, "PING") == 0) {
7278 os_memcpy(reply, "PONG\n", 5);
7279 reply_len = 5;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007280 } else if (os_strcmp(buf, "IFNAME") == 0) {
7281 reply_len = os_strlen(wpa_s->ifname);
7282 os_memcpy(reply, wpa_s->ifname, reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007283 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
7284 if (wpa_debug_reopen_file() < 0)
7285 reply_len = -1;
7286 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
7287 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
7288 } else if (os_strcmp(buf, "MIB") == 0) {
7289 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
7290 if (reply_len >= 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007291 reply_len += eapol_sm_get_mib(wpa_s->eapol,
7292 reply + reply_len,
7293 reply_size - reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007294 }
7295 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
7296 reply_len = wpa_supplicant_ctrl_iface_status(
7297 wpa_s, buf + 6, reply, reply_size);
7298 } else if (os_strcmp(buf, "PMKSA") == 0) {
7299 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
7300 reply_size);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007301 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
7302 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007303 } else if (os_strncmp(buf, "SET ", 4) == 0) {
7304 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
7305 reply_len = -1;
7306 } else if (os_strncmp(buf, "GET ", 4) == 0) {
7307 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
7308 reply, reply_size);
7309 } else if (os_strcmp(buf, "LOGON") == 0) {
7310 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
7311 } else if (os_strcmp(buf, "LOGOFF") == 0) {
7312 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
7313 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
7314 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
7315 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007316 else
7317 wpas_request_connection(wpa_s);
Dmitry Shmidt98660862014-03-11 17:26:21 -07007318 } else if (os_strcmp(buf, "REATTACH") == 0) {
7319 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
7320 !wpa_s->current_ssid)
7321 reply_len = -1;
7322 else {
7323 wpa_s->reattach = 1;
7324 wpas_request_connection(wpa_s);
7325 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007326 } else if (os_strcmp(buf, "RECONNECT") == 0) {
7327 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
7328 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007329 else if (wpa_s->disconnected)
7330 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007331#ifdef IEEE8021X_EAPOL
7332 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
7333 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
7334 reply_len = -1;
7335#endif /* IEEE8021X_EAPOL */
7336#ifdef CONFIG_PEERKEY
7337 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
7338 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
7339 reply_len = -1;
7340#endif /* CONFIG_PEERKEY */
7341#ifdef CONFIG_IEEE80211R
7342 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
7343 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
7344 reply_len = -1;
7345#endif /* CONFIG_IEEE80211R */
7346#ifdef CONFIG_WPS
7347 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
7348 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
7349 if (res == -2) {
7350 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
7351 reply_len = 17;
7352 } else if (res)
7353 reply_len = -1;
7354 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
7355 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
7356 if (res == -2) {
7357 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
7358 reply_len = 17;
7359 } else if (res)
7360 reply_len = -1;
7361 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
7362 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
7363 reply,
7364 reply_size);
7365 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
7366 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
7367 wpa_s, buf + 14, reply, reply_size);
7368 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
7369 if (wpas_wps_cancel(wpa_s))
7370 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007371#ifdef CONFIG_WPS_NFC
7372 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
7373 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
7374 reply_len = -1;
7375 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
7376 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
7377 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007378 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
7379 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
7380 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007381 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
7382 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
7383 wpa_s, buf + 14, reply, reply_size);
7384 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
7385 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
7386 buf + 17))
7387 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007388 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
7389 reply_len = wpas_ctrl_nfc_get_handover_req(
7390 wpa_s, buf + 21, reply, reply_size);
7391 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
7392 reply_len = wpas_ctrl_nfc_get_handover_sel(
7393 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007394 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
7395 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
7396 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007397#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007398 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
7399 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
7400 reply_len = -1;
7401#ifdef CONFIG_AP
7402 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
7403 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
7404 wpa_s, buf + 11, reply, reply_size);
7405#endif /* CONFIG_AP */
7406#ifdef CONFIG_WPS_ER
7407 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
7408 if (wpas_wps_er_start(wpa_s, NULL))
7409 reply_len = -1;
7410 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
7411 if (wpas_wps_er_start(wpa_s, buf + 13))
7412 reply_len = -1;
7413 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007414 wpas_wps_er_stop(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007415 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
7416 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
7417 reply_len = -1;
7418 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
7419 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
7420 if (ret == -2) {
7421 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
7422 reply_len = 17;
7423 } else if (ret == -3) {
7424 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
7425 reply_len = 18;
7426 } else if (ret == -4) {
7427 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
7428 reply_len = 20;
7429 } else if (ret)
7430 reply_len = -1;
7431 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
7432 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
7433 reply_len = -1;
7434 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
7435 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
7436 buf + 18))
7437 reply_len = -1;
7438 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
7439 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
7440 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007441#ifdef CONFIG_WPS_NFC
7442 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
7443 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
7444 wpa_s, buf + 24, reply, reply_size);
7445#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007446#endif /* CONFIG_WPS_ER */
7447#endif /* CONFIG_WPS */
7448#ifdef CONFIG_IBSS_RSN
7449 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
7450 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
7451 reply_len = -1;
7452#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007453#ifdef CONFIG_MESH
7454 } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
7455 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
7456 wpa_s, buf + 19, reply, reply_size);
7457 } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
7458 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
7459 wpa_s, "", reply, reply_size);
7460 } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
7461 if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
7462 reply_len = -1;
7463 } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
7464 if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
7465 buf + 18))
7466 reply_len = -1;
7467#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007468#ifdef CONFIG_P2P
7469 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
7470 if (p2p_ctrl_find(wpa_s, buf + 9))
7471 reply_len = -1;
7472 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
7473 if (p2p_ctrl_find(wpa_s, ""))
7474 reply_len = -1;
7475 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
7476 wpas_p2p_stop_find(wpa_s);
7477 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
7478 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
7479 reply_size);
7480 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
7481 if (p2p_ctrl_listen(wpa_s, buf + 11))
7482 reply_len = -1;
7483 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
7484 if (p2p_ctrl_listen(wpa_s, ""))
7485 reply_len = -1;
7486 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
7487 if (wpas_p2p_group_remove(wpa_s, buf + 17))
7488 reply_len = -1;
7489 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007490 if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007491 reply_len = -1;
7492 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
7493 if (p2p_ctrl_group_add(wpa_s, buf + 14))
7494 reply_len = -1;
7495 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
7496 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
7497 reply_len = -1;
7498 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
7499 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
7500 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
7501 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
7502 reply_size);
7503 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
7504 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
7505 reply_len = -1;
7506 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
7507 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
7508 reply_len = -1;
7509 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
7510 wpas_p2p_sd_service_update(wpa_s);
7511 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
7512 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
7513 reply_len = -1;
7514 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
7515 wpas_p2p_service_flush(wpa_s);
7516 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
7517 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
7518 reply_len = -1;
7519 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
7520 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
7521 reply_len = -1;
7522 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
7523 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
7524 reply_len = -1;
7525 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
7526 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
7527 reply_len = -1;
7528 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
7529 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
7530 reply_size);
7531 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
7532 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
7533 reply_len = -1;
7534 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007535 p2p_ctrl_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007536 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
7537 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
7538 reply_len = -1;
7539 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
7540 if (wpas_p2p_cancel(wpa_s))
7541 reply_len = -1;
7542 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
7543 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
7544 reply_len = -1;
7545 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
7546 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
7547 reply_len = -1;
7548 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
7549 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
7550 reply_len = -1;
7551 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
7552 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
7553 reply_len = -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007554 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
7555 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
7556 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007557#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007558#ifdef CONFIG_WIFI_DISPLAY
7559 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
7560 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
7561 reply_len = -1;
7562 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
7563 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
7564 reply, reply_size);
7565#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007566#ifdef CONFIG_INTERWORKING
7567 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
7568 if (interworking_fetch_anqp(wpa_s) < 0)
7569 reply_len = -1;
7570 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
7571 interworking_stop_fetch_anqp(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007572 } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
7573 if (ctrl_interworking_select(wpa_s, NULL) < 0)
7574 reply_len = -1;
7575 } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
7576 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007577 reply_len = -1;
7578 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
7579 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
7580 reply_len = -1;
7581 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
7582 if (get_anqp(wpa_s, buf + 9) < 0)
7583 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007584 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
7585 if (gas_request(wpa_s, buf + 12) < 0)
7586 reply_len = -1;
7587 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
7588 reply_len = gas_response_get(wpa_s, buf + 17, reply,
7589 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007590#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07007591#ifdef CONFIG_HS20
7592 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
7593 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
7594 reply_len = -1;
7595 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
7596 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
7597 reply_len = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007598 } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
7599 if (hs20_icon_request(wpa_s, buf + 18) < 0)
7600 reply_len = -1;
7601 } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
7602 if (hs20_fetch_osu(wpa_s) < 0)
7603 reply_len = -1;
7604 } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
7605 hs20_cancel_fetch_osu(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007606#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007607 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
7608 {
7609 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
7610 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
7611 reply_len = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007612 else {
7613 /*
7614 * Notify response from timeout to allow the control
7615 * interface response to be sent first.
7616 */
7617 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
7618 wpa_s, NULL);
7619 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007620 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
7621 if (wpa_supplicant_reload_configuration(wpa_s))
7622 reply_len = -1;
7623 } else if (os_strcmp(buf, "TERMINATE") == 0) {
7624 wpa_supplicant_terminate_proc(wpa_s->global);
7625 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
7626 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
7627 reply_len = -1;
Dmitry Shmidte19501d2011-03-16 14:32:18 -07007628 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007629 reply_len = wpa_supplicant_ctrl_iface_blacklist(
7630 wpa_s, buf + 9, reply, reply_size);
7631 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
7632 reply_len = wpa_supplicant_ctrl_iface_log_level(
7633 wpa_s, buf + 9, reply, reply_size);
Vinit Deshpandeda134e92014-12-02 10:59:29 -08007634 } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
7635 reply_len = wpa_supplicant_ctrl_iface_list_networks(
7636 wpa_s, buf + 14, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007637 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
7638 reply_len = wpa_supplicant_ctrl_iface_list_networks(
Vinit Deshpandeda134e92014-12-02 10:59:29 -08007639 wpa_s, NULL, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007640 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007641#ifdef CONFIG_SME
7642 wpa_s->sme.prev_bssid_set = 0;
7643#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007644 wpa_s->reassociate = 0;
7645 wpa_s->disconnected = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007646 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007647 wpa_supplicant_cancel_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007648 wpa_supplicant_deauthenticate(wpa_s,
7649 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007650 } else if (os_strcmp(buf, "SCAN") == 0) {
7651 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
7652 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
7653 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007654 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
7655 reply_len = wpa_supplicant_ctrl_iface_scan_results(
7656 wpa_s, reply, reply_size);
7657 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
7658 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
7659 reply_len = -1;
7660 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
7661 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
7662 reply_len = -1;
7663 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
7664 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
7665 reply_len = -1;
7666 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
7667 reply_len = wpa_supplicant_ctrl_iface_add_network(
7668 wpa_s, reply, reply_size);
7669 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
7670 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
7671 reply_len = -1;
7672 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
7673 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
7674 reply_len = -1;
7675 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
7676 reply_len = wpa_supplicant_ctrl_iface_get_network(
7677 wpa_s, buf + 12, reply, reply_size);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007678 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
7679 if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12))
7680 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007681 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
7682 reply_len = wpa_supplicant_ctrl_iface_list_creds(
7683 wpa_s, reply, reply_size);
7684 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
7685 reply_len = wpa_supplicant_ctrl_iface_add_cred(
7686 wpa_s, reply, reply_size);
7687 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
7688 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
7689 reply_len = -1;
7690 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
7691 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
7692 reply_len = -1;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07007693 } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
7694 reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
7695 reply,
7696 reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007697#ifndef CONFIG_NO_CONFIG_WRITE
7698 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
7699 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
7700 reply_len = -1;
7701#endif /* CONFIG_NO_CONFIG_WRITE */
7702 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
7703 reply_len = wpa_supplicant_ctrl_iface_get_capability(
7704 wpa_s, buf + 15, reply, reply_size);
7705 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
7706 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
7707 reply_len = -1;
7708 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
7709 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
7710 reply_len = -1;
7711 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
7712 reply_len = wpa_supplicant_global_iface_list(
7713 wpa_s->global, reply, reply_size);
7714 } else if (os_strcmp(buf, "INTERFACES") == 0) {
7715 reply_len = wpa_supplicant_global_iface_interfaces(
7716 wpa_s->global, reply, reply_size);
7717 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
7718 reply_len = wpa_supplicant_ctrl_iface_bss(
7719 wpa_s, buf + 4, reply, reply_size);
7720#ifdef CONFIG_AP
7721 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
7722 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
7723 } else if (os_strncmp(buf, "STA ", 4) == 0) {
7724 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
7725 reply_size);
7726 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
7727 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
7728 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007729 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
7730 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
7731 reply_len = -1;
7732 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
7733 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
7734 reply_len = -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007735 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
7736 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
7737 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007738#endif /* CONFIG_AP */
7739 } else if (os_strcmp(buf, "SUSPEND") == 0) {
7740 wpas_notify_suspend(wpa_s->global);
7741 } else if (os_strcmp(buf, "RESUME") == 0) {
7742 wpas_notify_resume(wpa_s->global);
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007743#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007744 } else if (os_strcmp(buf, "DROP_SA") == 0) {
7745 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007746#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007747 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
7748 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
7749 reply_len = -1;
7750 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007751 wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007752 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
7753 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
7754 reply_len = -1;
7755 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
7756 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
7757 buf + 17))
7758 reply_len = -1;
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07007759 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007760 wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007761#ifdef CONFIG_TDLS
7762 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
7763 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
7764 reply_len = -1;
7765 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
7766 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
7767 reply_len = -1;
7768 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
7769 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
7770 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007771 } else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
7772 if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
7773 buf + 17))
7774 reply_len = -1;
7775 } else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
7776 if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
7777 buf + 24))
7778 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007779#endif /* CONFIG_TDLS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007780 } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
7781 reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
7782 } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
7783 if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
7784 reply_len = -1;
7785 } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
7786 if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
7787 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007788 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
7789 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
7790 reply_size);
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07007791 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
7792 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
7793 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007794#ifdef CONFIG_AUTOSCAN
7795 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
7796 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
7797 reply_len = -1;
7798#endif /* CONFIG_AUTOSCAN */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007799#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07007800 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
7801 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
7802 reply_size);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007803#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007804 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
7805 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
7806 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007807 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007808 pmksa_cache_clear_current(wpa_s->wpa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007809 eapol_sm_request_reauth(wpa_s->eapol);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007810#ifdef CONFIG_WNM
7811 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
7812 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
7813 reply_len = -1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07007814 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
7815 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
7816 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007817#endif /* CONFIG_WNM */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007818 } else if (os_strcmp(buf, "FLUSH") == 0) {
7819 wpa_supplicant_ctrl_iface_flush(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007820 } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
7821 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
7822 reply_size);
Dmitry Shmidt818ea482014-03-10 13:15:21 -07007823#ifdef CONFIG_TESTING_OPTIONS
7824 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
7825 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
7826 reply_len = -1;
7827 } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
7828 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007829 } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
7830 if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
7831 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007832 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
7833 if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
7834 reply_len = -1;
7835 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
7836 if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
7837 reply_len = -1;
7838 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
7839 if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
7840 reply_len = -1;
7841 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
7842 if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
7843 reply_len = -1;
Dmitry Shmidt818ea482014-03-10 13:15:21 -07007844#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07007845 } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
7846 if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
7847 reply_len = -1;
7848 } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
7849 reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
7850 reply_size);
7851 } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
7852 if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
7853 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007854 } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
7855 if (wpas_ctrl_iface_send_neigbor_rep(wpa_s, buf + 20))
7856 reply_len = -1;
7857 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
7858 wpas_ctrl_iface_erp_flush(wpa_s);
7859 } else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
7860 if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
7861 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007862 } else {
7863 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
7864 reply_len = 16;
7865 }
7866
7867 if (reply_len < 0) {
7868 os_memcpy(reply, "FAIL\n", 5);
7869 reply_len = 5;
7870 }
7871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007872 *resp_len = reply_len;
7873 return reply;
7874}
7875
7876
7877static int wpa_supplicant_global_iface_add(struct wpa_global *global,
7878 char *cmd)
7879{
7880 struct wpa_interface iface;
7881 char *pos;
7882
7883 /*
7884 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
7885 * TAB<bridge_ifname>
7886 */
7887 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
7888
7889 os_memset(&iface, 0, sizeof(iface));
7890
7891 do {
7892 iface.ifname = pos = cmd;
7893 pos = os_strchr(pos, '\t');
7894 if (pos)
7895 *pos++ = '\0';
7896 if (iface.ifname[0] == '\0')
7897 return -1;
7898 if (pos == NULL)
7899 break;
7900
7901 iface.confname = pos;
7902 pos = os_strchr(pos, '\t');
7903 if (pos)
7904 *pos++ = '\0';
7905 if (iface.confname[0] == '\0')
7906 iface.confname = NULL;
7907 if (pos == NULL)
7908 break;
7909
7910 iface.driver = pos;
7911 pos = os_strchr(pos, '\t');
7912 if (pos)
7913 *pos++ = '\0';
7914 if (iface.driver[0] == '\0')
7915 iface.driver = NULL;
7916 if (pos == NULL)
7917 break;
7918
7919 iface.ctrl_interface = pos;
7920 pos = os_strchr(pos, '\t');
7921 if (pos)
7922 *pos++ = '\0';
7923 if (iface.ctrl_interface[0] == '\0')
7924 iface.ctrl_interface = NULL;
7925 if (pos == NULL)
7926 break;
7927
7928 iface.driver_param = pos;
7929 pos = os_strchr(pos, '\t');
7930 if (pos)
7931 *pos++ = '\0';
7932 if (iface.driver_param[0] == '\0')
7933 iface.driver_param = NULL;
7934 if (pos == NULL)
7935 break;
7936
7937 iface.bridge_ifname = pos;
7938 pos = os_strchr(pos, '\t');
7939 if (pos)
7940 *pos++ = '\0';
7941 if (iface.bridge_ifname[0] == '\0')
7942 iface.bridge_ifname = NULL;
7943 if (pos == NULL)
7944 break;
7945 } while (0);
7946
7947 if (wpa_supplicant_get_iface(global, iface.ifname))
7948 return -1;
7949
7950 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
7951}
7952
7953
7954static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
7955 char *cmd)
7956{
7957 struct wpa_supplicant *wpa_s;
7958
7959 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
7960
7961 wpa_s = wpa_supplicant_get_iface(global, cmd);
7962 if (wpa_s == NULL)
7963 return -1;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07007964 return wpa_supplicant_remove_iface(global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007965}
7966
7967
7968static void wpa_free_iface_info(struct wpa_interface_info *iface)
7969{
7970 struct wpa_interface_info *prev;
7971
7972 while (iface) {
7973 prev = iface;
7974 iface = iface->next;
7975
7976 os_free(prev->ifname);
7977 os_free(prev->desc);
7978 os_free(prev);
7979 }
7980}
7981
7982
7983static int wpa_supplicant_global_iface_list(struct wpa_global *global,
7984 char *buf, int len)
7985{
7986 int i, res;
7987 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
7988 char *pos, *end;
7989
7990 for (i = 0; wpa_drivers[i]; i++) {
7991 struct wpa_driver_ops *drv = wpa_drivers[i];
7992 if (drv->get_interfaces == NULL)
7993 continue;
7994 tmp = drv->get_interfaces(global->drv_priv[i]);
7995 if (tmp == NULL)
7996 continue;
7997
7998 if (last == NULL)
7999 iface = last = tmp;
8000 else
8001 last->next = tmp;
8002 while (last->next)
8003 last = last->next;
8004 }
8005
8006 pos = buf;
8007 end = buf + len;
8008 for (tmp = iface; tmp; tmp = tmp->next) {
8009 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
8010 tmp->drv_name, tmp->ifname,
8011 tmp->desc ? tmp->desc : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008012 if (os_snprintf_error(end - pos, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008013 *pos = '\0';
8014 break;
8015 }
8016 pos += res;
8017 }
8018
8019 wpa_free_iface_info(iface);
8020
8021 return pos - buf;
8022}
8023
8024
8025static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
8026 char *buf, int len)
8027{
8028 int res;
8029 char *pos, *end;
8030 struct wpa_supplicant *wpa_s;
8031
8032 wpa_s = global->ifaces;
8033 pos = buf;
8034 end = buf + len;
8035
8036 while (wpa_s) {
8037 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008038 if (os_snprintf_error(end - pos, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008039 *pos = '\0';
8040 break;
8041 }
8042 pos += res;
8043 wpa_s = wpa_s->next;
8044 }
8045 return pos - buf;
8046}
8047
8048
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008049static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
8050 const char *ifname,
8051 char *cmd, size_t *resp_len)
8052{
8053 struct wpa_supplicant *wpa_s;
8054
8055 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8056 if (os_strcmp(ifname, wpa_s->ifname) == 0)
8057 break;
8058 }
8059
8060 if (wpa_s == NULL) {
8061 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
8062 if (resp)
8063 *resp_len = os_strlen(resp);
8064 else
8065 *resp_len = 1;
8066 return resp;
8067 }
8068
8069 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
8070}
8071
8072
8073static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
8074 char *buf, size_t *resp_len)
8075{
8076#ifdef CONFIG_P2P
8077 static const char * cmd[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07008078 "LIST_NETWORKS",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008079 "P2P_FIND",
8080 "P2P_STOP_FIND",
8081 "P2P_LISTEN",
8082 "P2P_GROUP_ADD",
8083 "P2P_GET_PASSPHRASE",
8084 "P2P_SERVICE_UPDATE",
8085 "P2P_SERVICE_FLUSH",
8086 "P2P_FLUSH",
8087 "P2P_CANCEL",
8088 "P2P_PRESENCE_REQ",
8089 "P2P_EXT_LISTEN",
8090 NULL
8091 };
8092 static const char * prefix[] = {
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08008093#ifdef ANDROID
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07008094 "DRIVER ",
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08008095#endif /* ANDROID */
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07008096 "GET_NETWORK ",
8097 "REMOVE_NETWORK ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008098 "P2P_FIND ",
8099 "P2P_CONNECT ",
8100 "P2P_LISTEN ",
8101 "P2P_GROUP_REMOVE ",
8102 "P2P_GROUP_ADD ",
8103 "P2P_PROV_DISC ",
8104 "P2P_SERV_DISC_REQ ",
8105 "P2P_SERV_DISC_CANCEL_REQ ",
8106 "P2P_SERV_DISC_RESP ",
8107 "P2P_SERV_DISC_EXTERNAL ",
8108 "P2P_SERVICE_ADD ",
8109 "P2P_SERVICE_DEL ",
8110 "P2P_REJECT ",
8111 "P2P_INVITE ",
8112 "P2P_PEER ",
8113 "P2P_SET ",
8114 "P2P_UNAUTHORIZE ",
8115 "P2P_PRESENCE_REQ ",
8116 "P2P_EXT_LISTEN ",
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008117 "P2P_REMOVE_CLIENT ",
Dmitry Shmidt413dde72014-04-11 10:23:22 -07008118 "NFC_GET_HANDOVER_SEL ",
8119 "NFC_GET_HANDOVER_REQ ",
8120 "NFC_REPORT_HANDOVER ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008121 NULL
8122 };
8123 int found = 0;
8124 int i;
8125
8126 if (global->p2p_init_wpa_s == NULL)
8127 return NULL;
8128
8129 for (i = 0; !found && cmd[i]; i++) {
8130 if (os_strcmp(buf, cmd[i]) == 0)
8131 found = 1;
8132 }
8133
8134 for (i = 0; !found && prefix[i]; i++) {
8135 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
8136 found = 1;
8137 }
8138
8139 if (found)
8140 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
8141 buf, resp_len);
8142#endif /* CONFIG_P2P */
8143 return NULL;
8144}
8145
8146
8147static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
8148 char *buf, size_t *resp_len)
8149{
8150#ifdef CONFIG_WIFI_DISPLAY
8151 if (global->p2p_init_wpa_s == NULL)
8152 return NULL;
8153 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
8154 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
8155 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
8156 buf, resp_len);
8157#endif /* CONFIG_WIFI_DISPLAY */
8158 return NULL;
8159}
8160
8161
8162static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
8163 char *buf, size_t *resp_len)
8164{
8165 char *ret;
8166
8167 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
8168 if (ret)
8169 return ret;
8170
8171 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
8172 if (ret)
8173 return ret;
8174
8175 return NULL;
8176}
8177
8178
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008179static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
8180{
8181 char *value;
8182
8183 value = os_strchr(cmd, ' ');
8184 if (value == NULL)
8185 return -1;
8186 *value++ = '\0';
8187
8188 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
8189
8190#ifdef CONFIG_WIFI_DISPLAY
8191 if (os_strcasecmp(cmd, "wifi_display") == 0) {
8192 wifi_display_enable(global, !!atoi(value));
8193 return 0;
8194 }
8195#endif /* CONFIG_WIFI_DISPLAY */
8196
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008197 /* Restore cmd to its original value to allow redirection */
8198 value[-1] = ' ';
8199
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008200 return -1;
8201}
8202
8203
8204#ifndef CONFIG_NO_CONFIG_WRITE
8205static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
8206{
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008207 int ret = 0, saved = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008208 struct wpa_supplicant *wpa_s;
8209
8210 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8211 if (!wpa_s->conf->update_config) {
8212 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
8213 continue;
8214 }
8215
8216 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
8217 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
8218 ret = 1;
8219 } else {
8220 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008221 saved++;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008222 }
8223 }
8224
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008225 if (!saved && !ret) {
8226 wpa_dbg(wpa_s, MSG_DEBUG,
8227 "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
8228 ret = 1;
8229 }
8230
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008231 return ret;
8232}
8233#endif /* CONFIG_NO_CONFIG_WRITE */
8234
8235
8236static int wpas_global_ctrl_iface_status(struct wpa_global *global,
8237 char *buf, size_t buflen)
8238{
8239 char *pos, *end;
8240 int ret;
8241 struct wpa_supplicant *wpa_s;
8242
8243 pos = buf;
8244 end = buf + buflen;
8245
8246#ifdef CONFIG_P2P
8247 if (global->p2p && !global->p2p_disabled) {
8248 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
8249 "\n"
8250 "p2p_state=%s\n",
8251 MAC2STR(global->p2p_dev_addr),
8252 p2p_get_state_txt(global->p2p));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008253 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008254 return pos - buf;
8255 pos += ret;
8256 } else if (global->p2p) {
8257 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008258 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008259 return pos - buf;
8260 pos += ret;
8261 }
8262#endif /* CONFIG_P2P */
8263
8264#ifdef CONFIG_WIFI_DISPLAY
8265 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
8266 !!global->wifi_display);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008267 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008268 return pos - buf;
8269 pos += ret;
8270#endif /* CONFIG_WIFI_DISPLAY */
8271
8272 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8273 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
8274 "address=" MACSTR "\n",
8275 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008276 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008277 return pos - buf;
8278 pos += ret;
8279 }
8280
8281 return pos - buf;
8282}
8283
8284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008285char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
8286 char *buf, size_t *resp_len)
8287{
8288 char *reply;
8289 const int reply_size = 2048;
8290 int reply_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008291 int level = MSG_DEBUG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008292
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008293 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
8294 char *pos = os_strchr(buf + 7, ' ');
8295 if (pos) {
8296 *pos++ = '\0';
8297 return wpas_global_ctrl_iface_ifname(global,
8298 buf + 7, pos,
8299 resp_len);
8300 }
8301 }
8302
8303 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
8304 if (reply)
8305 return reply;
8306
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008307 if (os_strcmp(buf, "PING") == 0)
8308 level = MSG_EXCESSIVE;
8309 wpa_hexdump_ascii(level, "RX global ctrl_iface",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008310 (const u8 *) buf, os_strlen(buf));
8311
8312 reply = os_malloc(reply_size);
8313 if (reply == NULL) {
8314 *resp_len = 1;
8315 return NULL;
8316 }
8317
8318 os_memcpy(reply, "OK\n", 3);
8319 reply_len = 3;
8320
8321 if (os_strcmp(buf, "PING") == 0) {
8322 os_memcpy(reply, "PONG\n", 5);
8323 reply_len = 5;
8324 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
8325 if (wpa_supplicant_global_iface_add(global, buf + 14))
8326 reply_len = -1;
8327 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
8328 if (wpa_supplicant_global_iface_remove(global, buf + 17))
8329 reply_len = -1;
8330 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
8331 reply_len = wpa_supplicant_global_iface_list(
8332 global, reply, reply_size);
8333 } else if (os_strcmp(buf, "INTERFACES") == 0) {
8334 reply_len = wpa_supplicant_global_iface_interfaces(
8335 global, reply, reply_size);
8336 } else if (os_strcmp(buf, "TERMINATE") == 0) {
8337 wpa_supplicant_terminate_proc(global);
8338 } else if (os_strcmp(buf, "SUSPEND") == 0) {
8339 wpas_notify_suspend(global);
8340 } else if (os_strcmp(buf, "RESUME") == 0) {
8341 wpas_notify_resume(global);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008342 } else if (os_strncmp(buf, "SET ", 4) == 0) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008343 if (wpas_global_ctrl_iface_set(global, buf + 4)) {
8344#ifdef CONFIG_P2P
8345 if (global->p2p_init_wpa_s) {
8346 os_free(reply);
8347 /* Check if P2P redirection would work for this
8348 * command. */
8349 return wpa_supplicant_ctrl_iface_process(
8350 global->p2p_init_wpa_s,
8351 buf, resp_len);
8352 }
8353#endif /* CONFIG_P2P */
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008354 reply_len = -1;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008355 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008356#ifndef CONFIG_NO_CONFIG_WRITE
8357 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
8358 if (wpas_global_ctrl_iface_save_config(global))
8359 reply_len = -1;
8360#endif /* CONFIG_NO_CONFIG_WRITE */
8361 } else if (os_strcmp(buf, "STATUS") == 0) {
8362 reply_len = wpas_global_ctrl_iface_status(global, reply,
8363 reply_size);
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08008364#ifdef CONFIG_MODULE_TESTS
8365 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
8366 int wpas_module_tests(void);
8367 if (wpas_module_tests() < 0)
8368 reply_len = -1;
8369#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008370 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
8371 if (wpa_debug_reopen_file() < 0)
8372 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008373 } else {
8374 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
8375 reply_len = 16;
8376 }
8377
8378 if (reply_len < 0) {
8379 os_memcpy(reply, "FAIL\n", 5);
8380 reply_len = 5;
8381 }
8382
8383 *resp_len = reply_len;
8384 return reply;
8385}