blob: 1b81797d085eb821b30e1569670591f9a6d6e2e5 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Control interface (shared code for all backends)
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010#ifdef CONFIG_TESTING_OPTIONS
11#include <net/ethernet.h>
12#include <netinet/ip.h>
13#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014
15#include "utils/common.h"
16#include "utils/eloop.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080017#include "utils/uuid.h"
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -070018#include "utils/module_tests.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019#include "common/version.h"
20#include "common/ieee802_11_defs.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070021#include "common/ieee802_11_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "common/wpa_ctrl.h"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080023#include "crypto/tls.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080024#include "ap/hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "eap_peer/eap.h"
26#include "eapol_supp/eapol_supp_sm.h"
27#include "rsn_supp/wpa.h"
28#include "rsn_supp/preauth.h"
29#include "rsn_supp/pmksa_cache.h"
30#include "l2_packet/l2_packet.h"
31#include "wps/wps.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080032#include "fst/fst.h"
33#include "fst/fst_ctrl_iface.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "config.h"
35#include "wpa_supplicant_i.h"
36#include "driver_i.h"
37#include "wps_supplicant.h"
38#include "ibss_rsn.h"
39#include "ap.h"
40#include "p2p_supplicant.h"
41#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070042#include "hs20_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070043#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044#include "notify.h"
45#include "bss.h"
46#include "scan.h"
47#include "ctrl_iface.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080048#include "interworking.h"
Dmitry Shmidte19501d2011-03-16 14:32:18 -070049#include "blacklist.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070050#include "autoscan.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080051#include "wnm_sta.h"
Dmitry Shmidt818ea482014-03-10 13:15:21 -070052#include "offchannel.h"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070053#include "drivers/driver.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080054#include "mesh.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056static int wpa_supplicant_global_iface_list(struct wpa_global *global,
57 char *buf, int len);
58static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080059 const char *input,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060 char *buf, int len);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -070061static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
62 char *val);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063
Dmitry Shmidt04949592012-07-19 12:16:46 -070064static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
65{
66 char *pos;
67 u8 addr[ETH_ALEN], *filter = NULL, *n;
68 size_t count = 0;
69
70 pos = val;
71 while (pos) {
72 if (*pos == '\0')
73 break;
74 if (hwaddr_aton(pos, addr)) {
75 os_free(filter);
76 return -1;
77 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070078 n = os_realloc_array(filter, count + 1, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -070079 if (n == NULL) {
80 os_free(filter);
81 return -1;
82 }
83 filter = n;
84 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
85 count++;
86
87 pos = os_strchr(pos, ' ');
88 if (pos)
89 pos++;
90 }
91
92 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
93 os_free(wpa_s->bssid_filter);
94 wpa_s->bssid_filter = filter;
95 wpa_s->bssid_filter_count = count;
96
97 return 0;
98}
99
100
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800101static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
102{
103 char *pos;
104 u8 addr[ETH_ALEN], *bssid = NULL, *n;
105 struct wpa_ssid_value *ssid = NULL, *ns;
106 size_t count = 0, ssid_count = 0;
107 struct wpa_ssid *c;
108
109 /*
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800110 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800111 * SSID_SPEC ::= ssid <SSID_HEX>
112 * BSSID_SPEC ::= bssid <BSSID_HEX>
113 */
114
115 pos = val;
116 while (pos) {
117 if (*pos == '\0')
118 break;
119 if (os_strncmp(pos, "bssid ", 6) == 0) {
120 int res;
121 pos += 6;
122 res = hwaddr_aton2(pos, addr);
123 if (res < 0) {
124 os_free(ssid);
125 os_free(bssid);
126 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
127 "BSSID value '%s'", pos);
128 return -1;
129 }
130 pos += res;
131 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
132 if (n == NULL) {
133 os_free(ssid);
134 os_free(bssid);
135 return -1;
136 }
137 bssid = n;
138 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
139 count++;
140 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
141 char *end;
142 pos += 5;
143
144 end = pos;
145 while (*end) {
146 if (*end == '\0' || *end == ' ')
147 break;
148 end++;
149 }
150
151 ns = os_realloc_array(ssid, ssid_count + 1,
152 sizeof(struct wpa_ssid_value));
153 if (ns == NULL) {
154 os_free(ssid);
155 os_free(bssid);
156 return -1;
157 }
158 ssid = ns;
159
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700160 if ((end - pos) & 0x01 ||
161 end - pos > 2 * SSID_MAX_LEN ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800162 hexstr2bin(pos, ssid[ssid_count].ssid,
163 (end - pos) / 2) < 0) {
164 os_free(ssid);
165 os_free(bssid);
166 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
167 "SSID value '%s'", pos);
168 return -1;
169 }
170 ssid[ssid_count].ssid_len = (end - pos) / 2;
171 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
172 ssid[ssid_count].ssid,
173 ssid[ssid_count].ssid_len);
174 ssid_count++;
175 pos = end;
176 } else {
177 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
178 "'%s'", pos);
179 os_free(ssid);
180 os_free(bssid);
181 return -1;
182 }
183
184 pos = os_strchr(pos, ' ');
185 if (pos)
186 pos++;
187 }
188
189 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
190 os_free(wpa_s->disallow_aps_bssid);
191 wpa_s->disallow_aps_bssid = bssid;
192 wpa_s->disallow_aps_bssid_count = count;
193
194 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
195 os_free(wpa_s->disallow_aps_ssid);
196 wpa_s->disallow_aps_ssid = ssid;
197 wpa_s->disallow_aps_ssid_count = ssid_count;
198
199 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
200 return 0;
201
202 c = wpa_s->current_ssid;
203 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
204 return 0;
205
206 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
207 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
208 return 0;
209
210 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
211 "because current AP was marked disallowed");
212
213#ifdef CONFIG_SME
214 wpa_s->sme.prev_bssid_set = 0;
215#endif /* CONFIG_SME */
216 wpa_s->reassociate = 1;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800217 wpa_s->own_disconnect_req = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800218 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
219 wpa_supplicant_req_scan(wpa_s, 0, 0);
220
221 return 0;
222}
223
224
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700225#ifndef CONFIG_NO_CONFIG_BLOBS
226static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
227{
228 char *name = pos;
229 struct wpa_config_blob *blob;
230 size_t len;
231
232 pos = os_strchr(pos, ' ');
233 if (pos == NULL)
234 return -1;
235 *pos++ = '\0';
236 len = os_strlen(pos);
237 if (len & 1)
238 return -1;
239
240 wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
241 blob = os_zalloc(sizeof(*blob));
242 if (blob == NULL)
243 return -1;
244 blob->name = os_strdup(name);
245 blob->data = os_malloc(len / 2);
246 if (blob->name == NULL || blob->data == NULL) {
247 wpa_config_free_blob(blob);
248 return -1;
249 }
250
251 if (hexstr2bin(pos, blob->data, len / 2) < 0) {
252 wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
253 wpa_config_free_blob(blob);
254 return -1;
255 }
256 blob->len = len / 2;
257
258 wpa_config_set_blob(wpa_s->conf, blob);
259
260 return 0;
261}
262#endif /* CONFIG_NO_CONFIG_BLOBS */
263
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700264
265static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
266{
267 char *params;
268 char *pos;
269 int *freqs = NULL;
270 int ret;
271
272 if (atoi(cmd)) {
273 params = os_strchr(cmd, ' ');
274 os_free(wpa_s->manual_sched_scan_freqs);
275 if (params) {
276 params++;
277 pos = os_strstr(params, "freq=");
278 if (pos)
279 freqs = freq_range_to_channel_list(wpa_s,
280 pos + 5);
281 }
282 wpa_s->manual_sched_scan_freqs = freqs;
283 ret = wpas_start_pno(wpa_s);
284 } else {
285 ret = wpas_stop_pno(wpa_s);
286 }
287 return ret;
288}
289
290
Ravi Joshie6ccb162015-07-16 17:45:41 -0700291static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *band)
292{
293 union wpa_event_data event;
294
295 if (os_strcmp(band, "AUTO") == 0)
296 wpa_s->setband = WPA_SETBAND_AUTO;
297 else if (os_strcmp(band, "5G") == 0)
298 wpa_s->setband = WPA_SETBAND_5G;
299 else if (os_strcmp(band, "2G") == 0)
300 wpa_s->setband = WPA_SETBAND_2G;
301 else
302 return -1;
303
304 if (wpa_drv_setband(wpa_s, wpa_s->setband) == 0) {
305 os_memset(&event, 0, sizeof(event));
306 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
307 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
308 wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
309 }
310
311 return 0;
312}
313
314
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700315static int wpas_ctrl_iface_set_lci(struct wpa_supplicant *wpa_s,
316 const char *cmd)
317{
318 struct wpabuf *lci;
319
320 if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
321 wpabuf_free(wpa_s->lci);
322 wpa_s->lci = NULL;
323 return 0;
324 }
325
326 lci = wpabuf_parse_bin(cmd);
327 if (!lci)
328 return -1;
329
330 if (os_get_reltime(&wpa_s->lci_time)) {
331 wpabuf_free(lci);
332 return -1;
333 }
334
335 wpabuf_free(wpa_s->lci);
336 wpa_s->lci = lci;
337
338 return 0;
339}
340
341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
343 char *cmd)
344{
345 char *value;
346 int ret = 0;
347
348 value = os_strchr(cmd, ' ');
349 if (value == NULL)
350 return -1;
351 *value++ = '\0';
352
353 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
354 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
355 eapol_sm_configure(wpa_s->eapol,
356 atoi(value), -1, -1, -1);
357 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
358 eapol_sm_configure(wpa_s->eapol,
359 -1, atoi(value), -1, -1);
360 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
361 eapol_sm_configure(wpa_s->eapol,
362 -1, -1, atoi(value), -1);
363 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
364 eapol_sm_configure(wpa_s->eapol,
365 -1, -1, -1, atoi(value));
366 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
367 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
368 atoi(value)))
369 ret = -1;
370 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
371 0) {
372 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
373 atoi(value)))
374 ret = -1;
375 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
376 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
377 ret = -1;
378 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
379 wpa_s->wps_fragment_size = atoi(value);
380#ifdef CONFIG_WPS_TESTING
381 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
382 long int val;
383 val = strtol(value, NULL, 0);
384 if (val < 0 || val > 0xff) {
385 ret = -1;
386 wpa_printf(MSG_DEBUG, "WPS: Invalid "
387 "wps_version_number %ld", val);
388 } else {
389 wps_version_number = val;
390 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
391 "version %u.%u",
392 (wps_version_number & 0xf0) >> 4,
393 wps_version_number & 0x0f);
394 }
395 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
396 wps_testing_dummy_cred = atoi(value);
397 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
398 wps_testing_dummy_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800399 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
400 wps_corrupt_pkhash = atoi(value);
401 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
402 wps_corrupt_pkhash);
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800403 } else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
404 if (value[0] == '\0') {
405 wps_force_auth_types_in_use = 0;
406 } else {
407 wps_force_auth_types = strtol(value, NULL, 0);
408 wps_force_auth_types_in_use = 1;
409 }
410 } else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
411 if (value[0] == '\0') {
412 wps_force_encr_types_in_use = 0;
413 } else {
414 wps_force_encr_types = strtol(value, NULL, 0);
415 wps_force_encr_types_in_use = 1;
416 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417#endif /* CONFIG_WPS_TESTING */
418 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
419 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
420 ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800421#ifdef CONFIG_TDLS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422#ifdef CONFIG_TDLS_TESTING
423 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424 tdls_testing = strtol(value, NULL, 0);
425 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
426#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
428 int disabled = atoi(value);
429 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
430 if (disabled) {
431 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
432 ret = -1;
433 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
434 ret = -1;
435 wpa_tdls_enable(wpa_s->wpa, !disabled);
436#endif /* CONFIG_TDLS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800437 } else if (os_strcasecmp(cmd, "pno") == 0) {
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700438 ret = wpas_ctrl_pno(wpa_s, value);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700439 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
440 int disabled = atoi(value);
441 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
442 ret = -1;
443 else if (disabled)
444 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
445 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
446 if (os_strcmp(value, "disable") == 0)
447 wpa_s->set_sta_uapsd = 0;
448 else {
449 int be, bk, vi, vo;
450 char *pos;
451 /* format: BE,BK,VI,VO;max SP Length */
452 be = atoi(value);
453 pos = os_strchr(value, ',');
454 if (pos == NULL)
455 return -1;
456 pos++;
457 bk = atoi(pos);
458 pos = os_strchr(pos, ',');
459 if (pos == NULL)
460 return -1;
461 pos++;
462 vi = atoi(pos);
463 pos = os_strchr(pos, ',');
464 if (pos == NULL)
465 return -1;
466 pos++;
467 vo = atoi(pos);
468 /* ignore max SP Length for now */
469
470 wpa_s->set_sta_uapsd = 1;
471 wpa_s->sta_uapsd = 0;
472 if (be)
473 wpa_s->sta_uapsd |= BIT(0);
474 if (bk)
475 wpa_s->sta_uapsd |= BIT(1);
476 if (vi)
477 wpa_s->sta_uapsd |= BIT(2);
478 if (vo)
479 wpa_s->sta_uapsd |= BIT(3);
480 }
Jouni Malinen21d6bc82012-04-10 16:17:59 -0700481 } else if (os_strcasecmp(cmd, "ps") == 0) {
482 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700483#ifdef CONFIG_WIFI_DISPLAY
484 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
Dmitry Shmidted003d22014-02-06 10:09:12 -0800485 int enabled = !!atoi(value);
486 if (enabled && !wpa_s->global->p2p)
487 ret = -1;
488 else
489 wifi_display_enable(wpa_s->global, enabled);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700490#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700491 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
492 ret = set_bssid_filter(wpa_s, value);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800493 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
494 ret = set_disallow_aps(wpa_s, value);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800495 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
496 wpa_s->no_keep_alive = !!atoi(value);
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700497#ifdef CONFIG_TESTING_OPTIONS
498 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
499 wpa_s->ext_mgmt_frame_handling = !!atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800500 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
501 wpa_s->ext_eapol_frame_io = !!atoi(value);
502#ifdef CONFIG_AP
503 if (wpa_s->ap_iface) {
504 wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
505 wpa_s->ext_eapol_frame_io;
506 }
507#endif /* CONFIG_AP */
508 } else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
509 wpa_s->extra_roc_dur = atoi(value);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800510 } else if (os_strcasecmp(cmd, "test_failure") == 0) {
511 wpa_s->test_failure = atoi(value);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800512 } else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
513 wpa_s->p2p_go_csa_on_inv = !!atoi(value);
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700514 } else if (os_strcasecmp(cmd, "ignore_auth_resp") == 0) {
515 wpa_s->ignore_auth_resp = !!atoi(value);
516 } else if (os_strcasecmp(cmd, "ignore_assoc_disallow") == 0) {
517 wpa_s->ignore_assoc_disallow = !!atoi(value);
518 } else if (os_strcasecmp(cmd, "reject_btm_req_reason") == 0) {
519 wpa_s->reject_btm_req_reason = atoi(value);
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700520#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700521#ifndef CONFIG_NO_CONFIG_BLOBS
522 } else if (os_strcmp(cmd, "blob") == 0) {
523 ret = wpas_ctrl_set_blob(wpa_s, value);
524#endif /* CONFIG_NO_CONFIG_BLOBS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800525 } else if (os_strcasecmp(cmd, "setband") == 0) {
Ravi Joshie6ccb162015-07-16 17:45:41 -0700526 ret = wpas_ctrl_set_band(wpa_s, value);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800527#ifdef CONFIG_MBO
528 } else if (os_strcasecmp(cmd, "non_pref_chan") == 0) {
529 ret = wpas_mbo_update_non_pref_chan(wpa_s, value);
530 } else if (os_strcasecmp(cmd, "mbo_cell_capa") == 0) {
531 wpas_mbo_update_cell_capa(wpa_s, atoi(value));
532#endif /* CONFIG_MBO */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700533 } else if (os_strcasecmp(cmd, "lci") == 0) {
534 ret = wpas_ctrl_iface_set_lci(wpa_s, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535 } else {
536 value[-1] = '=';
537 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
538 if (ret == 0)
539 wpa_supplicant_update_config(wpa_s);
540 }
541
542 return ret;
543}
544
545
546static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
547 char *cmd, char *buf, size_t buflen)
548{
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700549 int res = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550
551 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
552
553 if (os_strcmp(cmd, "version") == 0) {
554 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700555 } else if (os_strcasecmp(cmd, "country") == 0) {
556 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
557 res = os_snprintf(buf, buflen, "%c%c",
558 wpa_s->conf->country[0],
559 wpa_s->conf->country[1]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700560#ifdef CONFIG_WIFI_DISPLAY
561 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
Dmitry Shmidted003d22014-02-06 10:09:12 -0800562 int enabled;
563 if (wpa_s->global->p2p == NULL ||
564 wpa_s->global->p2p_disabled)
565 enabled = 0;
566 else
567 enabled = wpa_s->global->wifi_display;
568 res = os_snprintf(buf, buflen, "%d", enabled);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700569#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700570#ifdef CONFIG_TESTING_GET_GTK
571 } else if (os_strcmp(cmd, "gtk") == 0) {
572 if (wpa_s->last_gtk_len == 0)
573 return -1;
574 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
575 wpa_s->last_gtk_len);
576 return res;
577#endif /* CONFIG_TESTING_GET_GTK */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800578 } else if (os_strcmp(cmd, "tls_library") == 0) {
579 res = tls_get_library_version(buf, buflen);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800580 } else {
581 res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582 }
583
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800584 if (os_snprintf_error(buflen, res))
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700585 return -1;
586 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587}
588
589
590#ifdef IEEE8021X_EAPOL
591static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
592 char *addr)
593{
594 u8 bssid[ETH_ALEN];
595 struct wpa_ssid *ssid = wpa_s->current_ssid;
596
597 if (hwaddr_aton(addr, bssid)) {
598 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
599 "'%s'", addr);
600 return -1;
601 }
602
603 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
604 rsn_preauth_deinit(wpa_s->wpa);
605 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
606 return -1;
607
608 return 0;
609}
610#endif /* IEEE8021X_EAPOL */
611
612
613#ifdef CONFIG_PEERKEY
614/* MLME-STKSTART.request(peer) */
615static int wpa_supplicant_ctrl_iface_stkstart(
616 struct wpa_supplicant *wpa_s, char *addr)
617{
618 u8 peer[ETH_ALEN];
619
620 if (hwaddr_aton(addr, peer)) {
621 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
622 "address '%s'", addr);
623 return -1;
624 }
625
626 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
627 MAC2STR(peer));
628
629 return wpa_sm_stkstart(wpa_s->wpa, peer);
630}
631#endif /* CONFIG_PEERKEY */
632
633
634#ifdef CONFIG_TDLS
635
636static int wpa_supplicant_ctrl_iface_tdls_discover(
637 struct wpa_supplicant *wpa_s, char *addr)
638{
639 u8 peer[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800640 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641
642 if (hwaddr_aton(addr, peer)) {
643 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
644 "address '%s'", addr);
645 return -1;
646 }
647
648 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
649 MAC2STR(peer));
650
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800651 if (wpa_tdls_is_external_setup(wpa_s->wpa))
652 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
653 else
654 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
655
656 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657}
658
659
660static int wpa_supplicant_ctrl_iface_tdls_setup(
661 struct wpa_supplicant *wpa_s, char *addr)
662{
663 u8 peer[ETH_ALEN];
664 int ret;
665
666 if (hwaddr_aton(addr, peer)) {
667 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
668 "address '%s'", addr);
669 return -1;
670 }
671
672 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
673 MAC2STR(peer));
674
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800675 if ((wpa_s->conf->tdls_external_control) &&
676 wpa_tdls_is_external_setup(wpa_s->wpa))
677 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
678
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800679 wpa_tdls_remove(wpa_s->wpa, peer);
680
681 if (wpa_tdls_is_external_setup(wpa_s->wpa))
682 ret = wpa_tdls_start(wpa_s->wpa, peer);
683 else
684 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800685
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686 return ret;
687}
688
689
690static int wpa_supplicant_ctrl_iface_tdls_teardown(
691 struct wpa_supplicant *wpa_s, char *addr)
692{
693 u8 peer[ETH_ALEN];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700694 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700696 if (os_strcmp(addr, "*") == 0) {
697 /* remove everyone */
698 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
699 wpa_tdls_teardown_peers(wpa_s->wpa);
700 return 0;
701 }
702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703 if (hwaddr_aton(addr, peer)) {
704 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
705 "address '%s'", addr);
706 return -1;
707 }
708
709 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
710 MAC2STR(peer));
711
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800712 if ((wpa_s->conf->tdls_external_control) &&
713 wpa_tdls_is_external_setup(wpa_s->wpa))
714 return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
715
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700716 if (wpa_tdls_is_external_setup(wpa_s->wpa))
717 ret = wpa_tdls_teardown_link(
718 wpa_s->wpa, peer,
719 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
720 else
721 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
722
723 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724}
725
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700726
727static int ctrl_iface_get_capability_tdls(
728 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
729{
730 int ret;
731
732 ret = os_snprintf(buf, buflen, "%s\n",
733 wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
734 (wpa_s->drv_flags &
735 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
736 "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800737 if (os_snprintf_error(buflen, ret))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700738 return -1;
739 return ret;
740}
741
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800742
743static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
744 struct wpa_supplicant *wpa_s, char *cmd)
745{
746 u8 peer[ETH_ALEN];
747 struct hostapd_freq_params freq_params;
748 u8 oper_class;
749 char *pos, *end;
750
751 if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
752 wpa_printf(MSG_INFO,
753 "tdls_chanswitch: Only supported with external setup");
754 return -1;
755 }
756
757 os_memset(&freq_params, 0, sizeof(freq_params));
758
759 pos = os_strchr(cmd, ' ');
760 if (pos == NULL)
761 return -1;
762 *pos++ = '\0';
763
764 oper_class = strtol(pos, &end, 10);
765 if (pos == end) {
766 wpa_printf(MSG_INFO,
767 "tdls_chanswitch: Invalid op class provided");
768 return -1;
769 }
770
771 pos = end;
772 freq_params.freq = atoi(pos);
773 if (freq_params.freq == 0) {
774 wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
775 return -1;
776 }
777
778#define SET_FREQ_SETTING(str) \
779 do { \
780 const char *pos2 = os_strstr(pos, " " #str "="); \
781 if (pos2) { \
782 pos2 += sizeof(" " #str "=") - 1; \
783 freq_params.str = atoi(pos2); \
784 } \
785 } while (0)
786
787 SET_FREQ_SETTING(center_freq1);
788 SET_FREQ_SETTING(center_freq2);
789 SET_FREQ_SETTING(bandwidth);
790 SET_FREQ_SETTING(sec_channel_offset);
791#undef SET_FREQ_SETTING
792
793 freq_params.ht_enabled = !!os_strstr(pos, " ht");
794 freq_params.vht_enabled = !!os_strstr(pos, " vht");
795
796 if (hwaddr_aton(cmd, peer)) {
797 wpa_printf(MSG_DEBUG,
798 "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
799 cmd);
800 return -1;
801 }
802
803 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
804 " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
805 MAC2STR(peer), oper_class, freq_params.freq,
806 freq_params.center_freq1, freq_params.center_freq2,
807 freq_params.bandwidth, freq_params.sec_channel_offset,
808 freq_params.ht_enabled ? " HT" : "",
809 freq_params.vht_enabled ? " VHT" : "");
810
811 return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
812 &freq_params);
813}
814
815
816static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
817 struct wpa_supplicant *wpa_s, char *cmd)
818{
819 u8 peer[ETH_ALEN];
820
821 if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
822 wpa_printf(MSG_INFO,
823 "tdls_chanswitch: Only supported with external setup");
824 return -1;
825 }
826
827 if (hwaddr_aton(cmd, peer)) {
828 wpa_printf(MSG_DEBUG,
829 "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
830 cmd);
831 return -1;
832 }
833
834 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
835 MAC2STR(peer));
836
837 return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
838}
839
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -0700840
841static int wpa_supplicant_ctrl_iface_tdls_link_status(
842 struct wpa_supplicant *wpa_s, const char *addr,
843 char *buf, size_t buflen)
844{
845 u8 peer[ETH_ALEN];
846 const char *tdls_status;
847 int ret;
848
849 if (hwaddr_aton(addr, peer)) {
850 wpa_printf(MSG_DEBUG,
851 "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
852 addr);
853 return -1;
854 }
855 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
856 MAC2STR(peer));
857
858 tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
859 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
860 ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
861 if (os_snprintf_error(buflen, ret))
862 return -1;
863
864 return ret;
865}
866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867#endif /* CONFIG_TDLS */
868
869
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800870static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
871{
872 char *token, *context = NULL;
873 struct wmm_ac_ts_setup_params params = {
874 .tsid = 0xff,
875 .direction = 0xff,
876 };
877
878 while ((token = str_token(cmd, " ", &context))) {
879 if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
880 sscanf(token, "up=%i", &params.user_priority) == 1 ||
881 sscanf(token, "nominal_msdu_size=%i",
882 &params.nominal_msdu_size) == 1 ||
883 sscanf(token, "mean_data_rate=%i",
884 &params.mean_data_rate) == 1 ||
885 sscanf(token, "min_phy_rate=%i",
886 &params.minimum_phy_rate) == 1 ||
887 sscanf(token, "sba=%i",
888 &params.surplus_bandwidth_allowance) == 1)
889 continue;
890
891 if (os_strcasecmp(token, "downlink") == 0) {
892 params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
893 } else if (os_strcasecmp(token, "uplink") == 0) {
894 params.direction = WMM_TSPEC_DIRECTION_UPLINK;
895 } else if (os_strcasecmp(token, "bidi") == 0) {
896 params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
897 } else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
898 params.fixed_nominal_msdu = 1;
899 } else {
900 wpa_printf(MSG_DEBUG,
901 "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
902 token);
903 return -1;
904 }
905
906 }
907
908 return wpas_wmm_ac_addts(wpa_s, &params);
909}
910
911
912static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
913{
914 u8 tsid = atoi(cmd);
915
916 return wpas_wmm_ac_delts(wpa_s, tsid);
917}
918
919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920#ifdef CONFIG_IEEE80211R
921static int wpa_supplicant_ctrl_iface_ft_ds(
922 struct wpa_supplicant *wpa_s, char *addr)
923{
924 u8 target_ap[ETH_ALEN];
925 struct wpa_bss *bss;
926 const u8 *mdie;
927
928 if (hwaddr_aton(addr, target_ap)) {
929 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
930 "address '%s'", addr);
931 return -1;
932 }
933
934 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
935
936 bss = wpa_bss_get_bssid(wpa_s, target_ap);
937 if (bss)
938 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
939 else
940 mdie = NULL;
941
942 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
943}
944#endif /* CONFIG_IEEE80211R */
945
946
947#ifdef CONFIG_WPS
948static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
949 char *cmd)
950{
951 u8 bssid[ETH_ALEN], *_bssid = bssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700952#ifdef CONFIG_P2P
953 u8 p2p_dev_addr[ETH_ALEN];
954#endif /* CONFIG_P2P */
955#ifdef CONFIG_AP
956 u8 *_p2p_dev_addr = NULL;
957#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800958
Dmitry Shmidtc0a8db02013-11-08 11:18:33 -0800959 if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 _bssid = NULL;
961#ifdef CONFIG_P2P
962 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
963 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
964 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
965 "P2P Device Address '%s'",
966 cmd + 13);
967 return -1;
968 }
969 _p2p_dev_addr = p2p_dev_addr;
970#endif /* CONFIG_P2P */
971 } else if (hwaddr_aton(cmd, bssid)) {
972 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
973 cmd);
974 return -1;
975 }
976
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800977#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978 if (wpa_s->ap_iface)
979 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
980#endif /* CONFIG_AP */
981
982 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
983}
984
985
986static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
987 char *cmd, char *buf,
988 size_t buflen)
989{
990 u8 bssid[ETH_ALEN], *_bssid = bssid;
991 char *pin;
992 int ret;
993
994 pin = os_strchr(cmd, ' ');
995 if (pin)
996 *pin++ = '\0';
997
998 if (os_strcmp(cmd, "any") == 0)
999 _bssid = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001000 else if (os_strcmp(cmd, "get") == 0) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001001 if (wps_generate_pin((unsigned int *) &ret) < 0)
1002 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001003 goto done;
1004 } else if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
1006 cmd);
1007 return -1;
1008 }
1009
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001010#ifdef CONFIG_AP
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001011 if (wpa_s->ap_iface) {
1012 int timeout = 0;
1013 char *pos;
1014
1015 if (pin) {
1016 pos = os_strchr(pin, ' ');
1017 if (pos) {
1018 *pos++ = '\0';
1019 timeout = atoi(pos);
1020 }
1021 }
1022
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001024 buf, buflen, timeout);
1025 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001026#endif /* CONFIG_AP */
1027
1028 if (pin) {
1029 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
1030 DEV_PW_DEFAULT);
1031 if (ret < 0)
1032 return -1;
1033 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001034 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035 return -1;
1036 return ret;
1037 }
1038
1039 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
1040 if (ret < 0)
1041 return -1;
1042
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001043done:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 /* Return the generated PIN */
1045 ret = os_snprintf(buf, buflen, "%08d", ret);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001046 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 return -1;
1048 return ret;
1049}
1050
1051
1052static int wpa_supplicant_ctrl_iface_wps_check_pin(
1053 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1054{
1055 char pin[9];
1056 size_t len;
1057 char *pos;
1058 int ret;
1059
1060 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
1061 (u8 *) cmd, os_strlen(cmd));
1062 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
1063 if (*pos < '0' || *pos > '9')
1064 continue;
1065 pin[len++] = *pos;
1066 if (len == 9) {
1067 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
1068 return -1;
1069 }
1070 }
1071 if (len != 4 && len != 8) {
1072 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
1073 return -1;
1074 }
1075 pin[len] = '\0';
1076
1077 if (len == 8) {
1078 unsigned int pin_val;
1079 pin_val = atoi(pin);
1080 if (!wps_pin_valid(pin_val)) {
1081 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
1082 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001083 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084 return -1;
1085 return ret;
1086 }
1087 }
1088
1089 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001090 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 return -1;
1092
1093 return ret;
1094}
1095
1096
Dmitry Shmidt04949592012-07-19 12:16:46 -07001097#ifdef CONFIG_WPS_NFC
1098
1099static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
1100 char *cmd)
1101{
1102 u8 bssid[ETH_ALEN], *_bssid = bssid;
1103
1104 if (cmd == NULL || cmd[0] == '\0')
1105 _bssid = NULL;
1106 else if (hwaddr_aton(cmd, bssid))
1107 return -1;
1108
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001109 return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
1110 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001111}
1112
1113
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001114static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
1115 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1116{
1117 int ndef;
1118 struct wpabuf *buf;
1119 int res;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001120 char *pos;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001121
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001122 pos = os_strchr(cmd, ' ');
1123 if (pos)
1124 *pos++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001125 if (os_strcmp(cmd, "WPS") == 0)
1126 ndef = 0;
1127 else if (os_strcmp(cmd, "NDEF") == 0)
1128 ndef = 1;
1129 else
1130 return -1;
1131
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001132 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001133 if (buf == NULL)
1134 return -1;
1135
1136 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1137 wpabuf_len(buf));
1138 reply[res++] = '\n';
1139 reply[res] = '\0';
1140
1141 wpabuf_free(buf);
1142
1143 return res;
1144}
1145
1146
Dmitry Shmidt04949592012-07-19 12:16:46 -07001147static int wpa_supplicant_ctrl_iface_wps_nfc_token(
1148 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1149{
1150 int ndef;
1151 struct wpabuf *buf;
1152 int res;
1153
1154 if (os_strcmp(cmd, "WPS") == 0)
1155 ndef = 0;
1156 else if (os_strcmp(cmd, "NDEF") == 0)
1157 ndef = 1;
1158 else
1159 return -1;
1160
1161 buf = wpas_wps_nfc_token(wpa_s, ndef);
1162 if (buf == NULL)
1163 return -1;
1164
1165 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1166 wpabuf_len(buf));
1167 reply[res++] = '\n';
1168 reply[res] = '\0';
1169
1170 wpabuf_free(buf);
1171
1172 return res;
1173}
1174
1175
1176static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
1177 struct wpa_supplicant *wpa_s, char *pos)
1178{
1179 size_t len;
1180 struct wpabuf *buf;
1181 int ret;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001182 char *freq;
1183 int forced_freq = 0;
1184
1185 freq = strstr(pos, " freq=");
1186 if (freq) {
1187 *freq = '\0';
1188 freq += 6;
1189 forced_freq = atoi(freq);
1190 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001191
1192 len = os_strlen(pos);
1193 if (len & 0x01)
1194 return -1;
1195 len /= 2;
1196
1197 buf = wpabuf_alloc(len);
1198 if (buf == NULL)
1199 return -1;
1200 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
1201 wpabuf_free(buf);
1202 return -1;
1203 }
1204
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001205 ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001206 wpabuf_free(buf);
1207
1208 return ret;
1209}
1210
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001211
1212static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001213 char *reply, size_t max_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001214 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001215{
1216 struct wpabuf *buf;
1217 int res;
1218
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001219 buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001220 if (buf == NULL)
1221 return -1;
1222
1223 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1224 wpabuf_len(buf));
1225 reply[res++] = '\n';
1226 reply[res] = '\0';
1227
1228 wpabuf_free(buf);
1229
1230 return res;
1231}
1232
1233
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001234#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001235static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
1236 char *reply, size_t max_len,
1237 int ndef)
1238{
1239 struct wpabuf *buf;
1240 int res;
1241
1242 buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
1243 if (buf == NULL) {
1244 wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
1245 return -1;
1246 }
1247
1248 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1249 wpabuf_len(buf));
1250 reply[res++] = '\n';
1251 reply[res] = '\0';
1252
1253 wpabuf_free(buf);
1254
1255 return res;
1256}
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001257#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001258
1259
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001260static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
1261 char *cmd, char *reply,
1262 size_t max_len)
1263{
1264 char *pos;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001265 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001266
1267 pos = os_strchr(cmd, ' ');
1268 if (pos == NULL)
1269 return -1;
1270 *pos++ = '\0';
1271
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001272 if (os_strcmp(cmd, "WPS") == 0)
1273 ndef = 0;
1274 else if (os_strcmp(cmd, "NDEF") == 0)
1275 ndef = 1;
1276 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001277 return -1;
1278
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001279 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001280 if (!ndef)
1281 return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001282 return wpas_ctrl_nfc_get_handover_req_wps(
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001283 wpa_s, reply, max_len, ndef);
1284 }
1285
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001286#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001287 if (os_strcmp(pos, "P2P-CR") == 0) {
1288 return wpas_ctrl_nfc_get_handover_req_p2p(
1289 wpa_s, reply, max_len, ndef);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001290 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001291#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001292
1293 return -1;
1294}
1295
1296
1297static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001298 char *reply, size_t max_len,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001299 int ndef, int cr, char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001300{
1301 struct wpabuf *buf;
1302 int res;
1303
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001304 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001305 if (buf == NULL)
1306 return -1;
1307
1308 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1309 wpabuf_len(buf));
1310 reply[res++] = '\n';
1311 reply[res] = '\0';
1312
1313 wpabuf_free(buf);
1314
1315 return res;
1316}
1317
1318
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001319#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001320static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1321 char *reply, size_t max_len,
1322 int ndef, int tag)
1323{
1324 struct wpabuf *buf;
1325 int res;
1326
1327 buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1328 if (buf == NULL)
1329 return -1;
1330
1331 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1332 wpabuf_len(buf));
1333 reply[res++] = '\n';
1334 reply[res] = '\0';
1335
1336 wpabuf_free(buf);
1337
1338 return res;
1339}
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001340#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001341
1342
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001343static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1344 char *cmd, char *reply,
1345 size_t max_len)
1346{
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001347 char *pos, *pos2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001348 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001349
1350 pos = os_strchr(cmd, ' ');
1351 if (pos == NULL)
1352 return -1;
1353 *pos++ = '\0';
1354
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001355 if (os_strcmp(cmd, "WPS") == 0)
1356 ndef = 0;
1357 else if (os_strcmp(cmd, "NDEF") == 0)
1358 ndef = 1;
1359 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001360 return -1;
1361
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001362 pos2 = os_strchr(pos, ' ');
1363 if (pos2)
1364 *pos2++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001365 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001366 if (!ndef)
1367 return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001368 return wpas_ctrl_nfc_get_handover_sel_wps(
1369 wpa_s, reply, max_len, ndef,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001370 os_strcmp(pos, "WPS-CR") == 0, pos2);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001371 }
1372
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001373#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001374 if (os_strcmp(pos, "P2P-CR") == 0) {
1375 return wpas_ctrl_nfc_get_handover_sel_p2p(
1376 wpa_s, reply, max_len, ndef, 0);
1377 }
1378
1379 if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1380 return wpas_ctrl_nfc_get_handover_sel_p2p(
1381 wpa_s, reply, max_len, ndef, 1);
1382 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001383#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001384
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001385 return -1;
1386}
1387
1388
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001389static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1390 char *cmd)
1391{
1392 size_t len;
1393 struct wpabuf *req, *sel;
1394 int ret;
1395 char *pos, *role, *type, *pos2;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001396#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001397 char *freq;
1398 int forced_freq = 0;
1399
1400 freq = strstr(cmd, " freq=");
1401 if (freq) {
1402 *freq = '\0';
1403 freq += 6;
1404 forced_freq = atoi(freq);
1405 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001406#endif /* CONFIG_P2P */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001407
1408 role = cmd;
1409 pos = os_strchr(role, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001410 if (pos == NULL) {
1411 wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001412 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001413 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001414 *pos++ = '\0';
1415
1416 type = pos;
1417 pos = os_strchr(type, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001418 if (pos == NULL) {
1419 wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001420 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001421 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001422 *pos++ = '\0';
1423
1424 pos2 = os_strchr(pos, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001425 if (pos2 == NULL) {
1426 wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001427 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001428 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001429 *pos2++ = '\0';
1430
1431 len = os_strlen(pos);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001432 if (len & 0x01) {
1433 wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001434 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001435 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001436 len /= 2;
1437
1438 req = wpabuf_alloc(len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001439 if (req == NULL) {
1440 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001441 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001442 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001443 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001444 wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001445 wpabuf_free(req);
1446 return -1;
1447 }
1448
1449 len = os_strlen(pos2);
1450 if (len & 0x01) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001451 wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001452 wpabuf_free(req);
1453 return -1;
1454 }
1455 len /= 2;
1456
1457 sel = wpabuf_alloc(len);
1458 if (sel == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001459 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001460 wpabuf_free(req);
1461 return -1;
1462 }
1463 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001464 wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001465 wpabuf_free(req);
1466 wpabuf_free(sel);
1467 return -1;
1468 }
1469
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001470 wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1471 role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1472
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001473 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1474 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001475#ifdef CONFIG_AP
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001476 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1477 {
1478 ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1479 if (ret < 0)
1480 ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001481#endif /* CONFIG_AP */
1482#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001483 } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1484 {
1485 ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
1486 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1487 {
1488 ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1489 forced_freq);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001490#endif /* CONFIG_P2P */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001491 } else {
1492 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1493 "reported: role=%s type=%s", role, type);
1494 ret = -1;
1495 }
1496 wpabuf_free(req);
1497 wpabuf_free(sel);
1498
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001499 if (ret)
1500 wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1501
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001502 return ret;
1503}
1504
Dmitry Shmidt04949592012-07-19 12:16:46 -07001505#endif /* CONFIG_WPS_NFC */
1506
1507
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001508static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1509 char *cmd)
1510{
1511 u8 bssid[ETH_ALEN];
1512 char *pin;
1513 char *new_ssid;
1514 char *new_auth;
1515 char *new_encr;
1516 char *new_key;
1517 struct wps_new_ap_settings ap;
1518
1519 pin = os_strchr(cmd, ' ');
1520 if (pin == NULL)
1521 return -1;
1522 *pin++ = '\0';
1523
1524 if (hwaddr_aton(cmd, bssid)) {
1525 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1526 cmd);
1527 return -1;
1528 }
1529
1530 new_ssid = os_strchr(pin, ' ');
1531 if (new_ssid == NULL)
1532 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1533 *new_ssid++ = '\0';
1534
1535 new_auth = os_strchr(new_ssid, ' ');
1536 if (new_auth == NULL)
1537 return -1;
1538 *new_auth++ = '\0';
1539
1540 new_encr = os_strchr(new_auth, ' ');
1541 if (new_encr == NULL)
1542 return -1;
1543 *new_encr++ = '\0';
1544
1545 new_key = os_strchr(new_encr, ' ');
1546 if (new_key == NULL)
1547 return -1;
1548 *new_key++ = '\0';
1549
1550 os_memset(&ap, 0, sizeof(ap));
1551 ap.ssid_hex = new_ssid;
1552 ap.auth = new_auth;
1553 ap.encr = new_encr;
1554 ap.key_hex = new_key;
1555 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1556}
1557
1558
1559#ifdef CONFIG_AP
1560static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1561 char *cmd, char *buf,
1562 size_t buflen)
1563{
1564 int timeout = 300;
1565 char *pos;
1566 const char *pin_txt;
1567
1568 if (!wpa_s->ap_iface)
1569 return -1;
1570
1571 pos = os_strchr(cmd, ' ');
1572 if (pos)
1573 *pos++ = '\0';
1574
1575 if (os_strcmp(cmd, "disable") == 0) {
1576 wpas_wps_ap_pin_disable(wpa_s);
1577 return os_snprintf(buf, buflen, "OK\n");
1578 }
1579
1580 if (os_strcmp(cmd, "random") == 0) {
1581 if (pos)
1582 timeout = atoi(pos);
1583 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1584 if (pin_txt == NULL)
1585 return -1;
1586 return os_snprintf(buf, buflen, "%s", pin_txt);
1587 }
1588
1589 if (os_strcmp(cmd, "get") == 0) {
1590 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1591 if (pin_txt == NULL)
1592 return -1;
1593 return os_snprintf(buf, buflen, "%s", pin_txt);
1594 }
1595
1596 if (os_strcmp(cmd, "set") == 0) {
1597 char *pin;
1598 if (pos == NULL)
1599 return -1;
1600 pin = pos;
1601 pos = os_strchr(pos, ' ');
1602 if (pos) {
1603 *pos++ = '\0';
1604 timeout = atoi(pos);
1605 }
1606 if (os_strlen(pin) > buflen)
1607 return -1;
1608 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1609 return -1;
1610 return os_snprintf(buf, buflen, "%s", pin);
1611 }
1612
1613 return -1;
1614}
1615#endif /* CONFIG_AP */
1616
1617
1618#ifdef CONFIG_WPS_ER
1619static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1620 char *cmd)
1621{
1622 char *uuid = cmd, *pin, *pos;
1623 u8 addr_buf[ETH_ALEN], *addr = NULL;
1624 pin = os_strchr(uuid, ' ');
1625 if (pin == NULL)
1626 return -1;
1627 *pin++ = '\0';
1628 pos = os_strchr(pin, ' ');
1629 if (pos) {
1630 *pos++ = '\0';
1631 if (hwaddr_aton(pos, addr_buf) == 0)
1632 addr = addr_buf;
1633 }
1634 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1635}
1636
1637
1638static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1639 char *cmd)
1640{
1641 char *uuid = cmd, *pin;
1642 pin = os_strchr(uuid, ' ');
1643 if (pin == NULL)
1644 return -1;
1645 *pin++ = '\0';
1646 return wpas_wps_er_learn(wpa_s, uuid, pin);
1647}
1648
1649
1650static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1651 struct wpa_supplicant *wpa_s, char *cmd)
1652{
1653 char *uuid = cmd, *id;
1654 id = os_strchr(uuid, ' ');
1655 if (id == NULL)
1656 return -1;
1657 *id++ = '\0';
1658 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1659}
1660
1661
1662static int wpa_supplicant_ctrl_iface_wps_er_config(
1663 struct wpa_supplicant *wpa_s, char *cmd)
1664{
1665 char *pin;
1666 char *new_ssid;
1667 char *new_auth;
1668 char *new_encr;
1669 char *new_key;
1670 struct wps_new_ap_settings ap;
1671
1672 pin = os_strchr(cmd, ' ');
1673 if (pin == NULL)
1674 return -1;
1675 *pin++ = '\0';
1676
1677 new_ssid = os_strchr(pin, ' ');
1678 if (new_ssid == NULL)
1679 return -1;
1680 *new_ssid++ = '\0';
1681
1682 new_auth = os_strchr(new_ssid, ' ');
1683 if (new_auth == NULL)
1684 return -1;
1685 *new_auth++ = '\0';
1686
1687 new_encr = os_strchr(new_auth, ' ');
1688 if (new_encr == NULL)
1689 return -1;
1690 *new_encr++ = '\0';
1691
1692 new_key = os_strchr(new_encr, ' ');
1693 if (new_key == NULL)
1694 return -1;
1695 *new_key++ = '\0';
1696
1697 os_memset(&ap, 0, sizeof(ap));
1698 ap.ssid_hex = new_ssid;
1699 ap.auth = new_auth;
1700 ap.encr = new_encr;
1701 ap.key_hex = new_key;
1702 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1703}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001704
1705
1706#ifdef CONFIG_WPS_NFC
1707static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1708 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1709{
1710 int ndef;
1711 struct wpabuf *buf;
1712 int res;
1713 char *uuid;
1714
1715 uuid = os_strchr(cmd, ' ');
1716 if (uuid == NULL)
1717 return -1;
1718 *uuid++ = '\0';
1719
1720 if (os_strcmp(cmd, "WPS") == 0)
1721 ndef = 0;
1722 else if (os_strcmp(cmd, "NDEF") == 0)
1723 ndef = 1;
1724 else
1725 return -1;
1726
1727 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1728 if (buf == NULL)
1729 return -1;
1730
1731 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1732 wpabuf_len(buf));
1733 reply[res++] = '\n';
1734 reply[res] = '\0';
1735
1736 wpabuf_free(buf);
1737
1738 return res;
1739}
1740#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741#endif /* CONFIG_WPS_ER */
1742
1743#endif /* CONFIG_WPS */
1744
1745
1746#ifdef CONFIG_IBSS_RSN
1747static int wpa_supplicant_ctrl_iface_ibss_rsn(
1748 struct wpa_supplicant *wpa_s, char *addr)
1749{
1750 u8 peer[ETH_ALEN];
1751
1752 if (hwaddr_aton(addr, peer)) {
1753 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1754 "address '%s'", addr);
1755 return -1;
1756 }
1757
1758 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1759 MAC2STR(peer));
1760
1761 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1762}
1763#endif /* CONFIG_IBSS_RSN */
1764
1765
1766static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1767 char *rsp)
1768{
1769#ifdef IEEE8021X_EAPOL
1770 char *pos, *id_pos;
1771 int id;
1772 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773
1774 pos = os_strchr(rsp, '-');
1775 if (pos == NULL)
1776 return -1;
1777 *pos++ = '\0';
1778 id_pos = pos;
1779 pos = os_strchr(pos, ':');
1780 if (pos == NULL)
1781 return -1;
1782 *pos++ = '\0';
1783 id = atoi(id_pos);
1784 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1785 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1786 (u8 *) pos, os_strlen(pos));
1787
1788 ssid = wpa_config_get_network(wpa_s->conf, id);
1789 if (ssid == NULL) {
1790 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1791 "to update", id);
1792 return -1;
1793 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001795 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1796 pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001797#else /* IEEE8021X_EAPOL */
1798 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1799 return -1;
1800#endif /* IEEE8021X_EAPOL */
1801}
1802
1803
1804static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1805 const char *params,
1806 char *buf, size_t buflen)
1807{
1808 char *pos, *end, tmp[30];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001809 int res, verbose, wps, ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001810#ifdef CONFIG_HS20
1811 const u8 *hs20;
1812#endif /* CONFIG_HS20 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001813 const u8 *sess_id;
1814 size_t sess_id_len;
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001815
Dmitry Shmidt56052862013-10-04 10:23:25 -07001816 if (os_strcmp(params, "-DRIVER") == 0)
1817 return wpa_drv_status(wpa_s, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818 verbose = os_strcmp(params, "-VERBOSE") == 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001819 wps = os_strcmp(params, "-WPS") == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820 pos = buf;
1821 end = buf + buflen;
1822 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1823 struct wpa_ssid *ssid = wpa_s->current_ssid;
1824 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1825 MAC2STR(wpa_s->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001826 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827 return pos - buf;
1828 pos += ret;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001829 ret = os_snprintf(pos, end - pos, "freq=%u\n",
1830 wpa_s->assoc_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001831 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001832 return pos - buf;
1833 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001834 if (ssid) {
1835 u8 *_ssid = ssid->ssid;
1836 size_t ssid_len = ssid->ssid_len;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001837 u8 ssid_buf[SSID_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838 if (ssid_len == 0) {
1839 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1840 if (_res < 0)
1841 ssid_len = 0;
1842 else
1843 ssid_len = _res;
1844 _ssid = ssid_buf;
1845 }
1846 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1847 wpa_ssid_txt(_ssid, ssid_len),
1848 ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001849 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850 return pos - buf;
1851 pos += ret;
1852
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001853 if (wps && ssid->passphrase &&
1854 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1855 (ssid->mode == WPAS_MODE_AP ||
1856 ssid->mode == WPAS_MODE_P2P_GO)) {
1857 ret = os_snprintf(pos, end - pos,
1858 "passphrase=%s\n",
1859 ssid->passphrase);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001860 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001861 return pos - buf;
1862 pos += ret;
1863 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864 if (ssid->id_str) {
1865 ret = os_snprintf(pos, end - pos,
1866 "id_str=%s\n",
1867 ssid->id_str);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001868 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001869 return pos - buf;
1870 pos += ret;
1871 }
1872
1873 switch (ssid->mode) {
1874 case WPAS_MODE_INFRA:
1875 ret = os_snprintf(pos, end - pos,
1876 "mode=station\n");
1877 break;
1878 case WPAS_MODE_IBSS:
1879 ret = os_snprintf(pos, end - pos,
1880 "mode=IBSS\n");
1881 break;
1882 case WPAS_MODE_AP:
1883 ret = os_snprintf(pos, end - pos,
1884 "mode=AP\n");
1885 break;
1886 case WPAS_MODE_P2P_GO:
1887 ret = os_snprintf(pos, end - pos,
1888 "mode=P2P GO\n");
1889 break;
1890 case WPAS_MODE_P2P_GROUP_FORMATION:
1891 ret = os_snprintf(pos, end - pos,
1892 "mode=P2P GO - group "
1893 "formation\n");
1894 break;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001895 case WPAS_MODE_MESH:
1896 ret = os_snprintf(pos, end - pos,
1897 "mode=mesh\n");
1898 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899 default:
1900 ret = 0;
1901 break;
1902 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001903 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904 return pos - buf;
1905 pos += ret;
1906 }
1907
1908#ifdef CONFIG_AP
1909 if (wpa_s->ap_iface) {
1910 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1911 end - pos,
1912 verbose);
1913 } else
1914#endif /* CONFIG_AP */
1915 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1916 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001917#ifdef CONFIG_SAE
1918 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001919#ifdef CONFIG_AP
1920 !wpa_s->ap_iface &&
1921#endif /* CONFIG_AP */
1922 wpa_s->sme.sae.state == SAE_ACCEPTED) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001923 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1924 wpa_s->sme.sae.group);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001925 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001926 return pos - buf;
1927 pos += ret;
1928 }
1929#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1931 wpa_supplicant_state_txt(wpa_s->wpa_state));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001932 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001933 return pos - buf;
1934 pos += ret;
1935
1936 if (wpa_s->l2 &&
1937 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1938 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001939 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001940 return pos - buf;
1941 pos += ret;
1942 }
1943
1944#ifdef CONFIG_P2P
1945 if (wpa_s->global->p2p) {
1946 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1947 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001948 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949 return pos - buf;
1950 pos += ret;
1951 }
1952#endif /* CONFIG_P2P */
1953
1954 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1955 MAC2STR(wpa_s->own_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001956 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 return pos - buf;
1958 pos += ret;
1959
Dmitry Shmidt04949592012-07-19 12:16:46 -07001960#ifdef CONFIG_HS20
1961 if (wpa_s->current_bss &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001962 (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1963 HS20_IE_VENDOR_TYPE)) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001964 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1965 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001966 int release = 1;
1967 if (hs20[1] >= 5) {
1968 u8 rel_num = (hs20[6] & 0xf0) >> 4;
1969 release = rel_num + 1;
1970 }
1971 ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001972 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001973 return pos - buf;
1974 pos += ret;
1975 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001976
1977 if (wpa_s->current_ssid) {
1978 struct wpa_cred *cred;
1979 char *type;
1980
1981 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001982 size_t i;
1983
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001984 if (wpa_s->current_ssid->parent_cred != cred)
1985 continue;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001986
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001987 if (cred->provisioning_sp) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001988 ret = os_snprintf(pos, end - pos,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001989 "provisioning_sp=%s\n",
1990 cred->provisioning_sp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001991 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001992 return pos - buf;
1993 pos += ret;
1994 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001995
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001996 if (!cred->domain)
1997 goto no_domain;
1998
1999 i = 0;
2000 if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
2001 struct wpabuf *names =
2002 wpa_s->current_bss->anqp->domain_name;
2003 for (i = 0; names && i < cred->num_domain; i++)
2004 {
2005 if (domain_name_list_contains(
2006 names, cred->domain[i], 1))
2007 break;
2008 }
2009 if (i == cred->num_domain)
2010 i = 0; /* show first entry by default */
2011 }
2012 ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
2013 cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002014 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002015 return pos - buf;
2016 pos += ret;
2017
2018 no_domain:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002019 if (wpa_s->current_bss == NULL ||
2020 wpa_s->current_bss->anqp == NULL)
2021 res = -1;
2022 else
2023 res = interworking_home_sp_cred(
2024 wpa_s, cred,
2025 wpa_s->current_bss->anqp->domain_name);
2026 if (res > 0)
2027 type = "home";
2028 else if (res == 0)
2029 type = "roaming";
2030 else
2031 type = "unknown";
2032
2033 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002034 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002035 return pos - buf;
2036 pos += ret;
2037
2038 break;
2039 }
2040 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002041#endif /* CONFIG_HS20 */
2042
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2044 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2045 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
2046 verbose);
2047 if (res >= 0)
2048 pos += res;
2049 }
2050
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002051 sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
2052 if (sess_id) {
2053 char *start = pos;
2054
2055 ret = os_snprintf(pos, end - pos, "eap_session_id=");
2056 if (os_snprintf_error(end - pos, ret))
2057 return start - buf;
2058 pos += ret;
2059 ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
2060 if (ret <= 0)
2061 return start - buf;
2062 pos += ret;
2063 ret = os_snprintf(pos, end - pos, "\n");
2064 if (os_snprintf_error(end - pos, ret))
2065 return start - buf;
2066 pos += ret;
2067 }
2068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002069 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
2070 if (res >= 0)
2071 pos += res;
2072
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002073#ifdef CONFIG_WPS
2074 {
2075 char uuid_str[100];
2076 uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
2077 ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002078 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002079 return pos - buf;
2080 pos += ret;
2081 }
2082#endif /* CONFIG_WPS */
2083
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08002084#ifdef ANDROID
vandwalleffc70182014-09-11 11:40:14 -07002085 /*
2086 * Allow using the STATUS command with default behavior, say for debug,
2087 * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
2088 * events with STATUS-NO_EVENTS.
2089 */
2090 if (os_strcmp(params, "-NO_EVENTS")) {
2091 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
2092 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
2093 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
2094 wpa_s->wpa_state,
2095 MAC2STR(wpa_s->bssid),
2096 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
2097 wpa_ssid_txt(wpa_s->current_ssid->ssid,
2098 wpa_s->current_ssid->ssid_len) : "");
2099 if (wpa_s->wpa_state == WPA_COMPLETED) {
2100 struct wpa_ssid *ssid = wpa_s->current_ssid;
2101 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
2102 "- connection to " MACSTR
2103 " completed %s [id=%d id_str=%s]",
2104 MAC2STR(wpa_s->bssid), "(auth)",
2105 ssid ? ssid->id : -1,
2106 ssid && ssid->id_str ? ssid->id_str : "");
2107 }
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08002108 }
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08002109#endif /* ANDROID */
2110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111 return pos - buf;
2112}
2113
2114
2115static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
2116 char *cmd)
2117{
2118 char *pos;
2119 int id;
2120 struct wpa_ssid *ssid;
2121 u8 bssid[ETH_ALEN];
2122
2123 /* cmd: "<network id> <BSSID>" */
2124 pos = os_strchr(cmd, ' ');
2125 if (pos == NULL)
2126 return -1;
2127 *pos++ = '\0';
2128 id = atoi(cmd);
2129 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
2130 if (hwaddr_aton(pos, bssid)) {
2131 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
2132 return -1;
2133 }
2134
2135 ssid = wpa_config_get_network(wpa_s->conf, id);
2136 if (ssid == NULL) {
2137 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2138 "to update", id);
2139 return -1;
2140 }
2141
2142 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
2143 ssid->bssid_set = !is_zero_ether_addr(bssid);
2144
2145 return 0;
2146}
2147
2148
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002149static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002150 char *cmd, char *buf,
2151 size_t buflen)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002152{
2153 u8 bssid[ETH_ALEN];
2154 struct wpa_blacklist *e;
2155 char *pos, *end;
2156 int ret;
2157
2158 /* cmd: "BLACKLIST [<BSSID>]" */
2159 if (*cmd == '\0') {
2160 pos = buf;
2161 end = buf + buflen;
2162 e = wpa_s->blacklist;
2163 while (e) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002164 ret = os_snprintf(pos, end - pos, MACSTR "\n",
2165 MAC2STR(e->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002166 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002167 return pos - buf;
2168 pos += ret;
2169 e = e->next;
2170 }
2171 return pos - buf;
2172 }
2173
2174 cmd++;
2175 if (os_strncmp(cmd, "clear", 5) == 0) {
2176 wpa_blacklist_clear(wpa_s);
2177 os_memcpy(buf, "OK\n", 3);
2178 return 3;
2179 }
2180
2181 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
2182 if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002183 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002184 return -1;
2185 }
2186
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002187 /*
2188 * Add the BSSID twice, so its count will be 2, causing it to be
2189 * skipped when processing scan results.
2190 */
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002191 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002192 if (ret < 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002193 return -1;
2194 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002195 if (ret < 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07002196 return -1;
2197 os_memcpy(buf, "OK\n", 3);
2198 return 3;
2199}
2200
2201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002202static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
2203 char *cmd, char *buf,
2204 size_t buflen)
2205{
2206 char *pos, *end, *stamp;
2207 int ret;
2208
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002209 /* cmd: "LOG_LEVEL [<level>]" */
2210 if (*cmd == '\0') {
2211 pos = buf;
2212 end = buf + buflen;
2213 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2214 "Timestamp: %d\n",
2215 debug_level_str(wpa_debug_level),
2216 wpa_debug_timestamp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002217 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002218 ret = 0;
2219
2220 return ret;
2221 }
2222
2223 while (*cmd == ' ')
2224 cmd++;
2225
2226 stamp = os_strchr(cmd, ' ');
2227 if (stamp) {
2228 *stamp++ = '\0';
2229 while (*stamp == ' ') {
2230 stamp++;
2231 }
2232 }
2233
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002234 if (os_strlen(cmd)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002235 int level = str_to_debug_level(cmd);
2236 if (level < 0)
2237 return -1;
2238 wpa_debug_level = level;
2239 }
2240
2241 if (stamp && os_strlen(stamp))
2242 wpa_debug_timestamp = atoi(stamp);
2243
2244 os_memcpy(buf, "OK\n", 3);
2245 return 3;
2246}
2247
2248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249static int wpa_supplicant_ctrl_iface_list_networks(
Vinit Deshpandeda134e92014-12-02 10:59:29 -08002250 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251{
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002252 char *pos, *end, *prev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253 struct wpa_ssid *ssid;
2254 int ret;
2255
2256 pos = buf;
2257 end = buf + buflen;
2258 ret = os_snprintf(pos, end - pos,
2259 "network id / ssid / bssid / flags\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002260 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261 return pos - buf;
2262 pos += ret;
2263
2264 ssid = wpa_s->conf->ssid;
Vinit Deshpandeda134e92014-12-02 10:59:29 -08002265
2266 /* skip over ssids until we find next one */
2267 if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
2268 int last_id = atoi(cmd + 8);
2269 if (last_id != -1) {
2270 while (ssid != NULL && ssid->id <= last_id) {
2271 ssid = ssid->next;
2272 }
2273 }
2274 }
2275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002276 while (ssid) {
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002277 prev = pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002278 ret = os_snprintf(pos, end - pos, "%d\t%s",
2279 ssid->id,
2280 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002281 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002282 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283 pos += ret;
2284 if (ssid->bssid_set) {
2285 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2286 MAC2STR(ssid->bssid));
2287 } else {
2288 ret = os_snprintf(pos, end - pos, "\tany");
2289 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002290 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002291 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002293 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294 ssid == wpa_s->current_ssid ?
2295 "[CURRENT]" : "",
2296 ssid->disabled ? "[DISABLED]" : "",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002297 ssid->disabled_until.sec ?
2298 "[TEMP-DISABLED]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002299 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2300 "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002301 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002302 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303 pos += ret;
2304 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002305 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt9e37fc22014-12-03 11:48:46 -08002306 return prev - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307 pos += ret;
2308
2309 ssid = ssid->next;
2310 }
2311
2312 return pos - buf;
2313}
2314
2315
2316static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2317{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002318 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002319 ret = os_snprintf(pos, end - pos, "-");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002320 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321 return pos;
2322 pos += ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002323 ret = wpa_write_ciphers(pos, end, cipher, "+");
2324 if (ret < 0)
2325 return pos;
2326 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002327 return pos;
2328}
2329
2330
2331static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2332 const u8 *ie, size_t ie_len)
2333{
2334 struct wpa_ie_data data;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002335 char *start;
2336 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337
2338 ret = os_snprintf(pos, end - pos, "[%s-", proto);
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 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2344 ret = os_snprintf(pos, end - pos, "?]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002345 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 return pos;
2347 pos += ret;
2348 return pos;
2349 }
2350
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002351 start = pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002353 ret = os_snprintf(pos, end - pos, "%sEAP",
2354 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002355 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002356 return pos;
2357 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358 }
2359 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002360 ret = os_snprintf(pos, end - pos, "%sPSK",
2361 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002362 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 return pos;
2364 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002365 }
2366 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002367 ret = os_snprintf(pos, end - pos, "%sNone",
2368 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002369 if (os_snprintf_error(end - pos, ret))
2370 return pos;
2371 pos += ret;
2372 }
2373 if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
2374 ret = os_snprintf(pos, end - pos, "%sSAE",
2375 pos == start ? "" : "+");
2376 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377 return pos;
2378 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379 }
2380#ifdef CONFIG_IEEE80211R
2381 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2382 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002383 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002384 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385 return pos;
2386 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387 }
2388 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2389 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002390 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002391 if (os_snprintf_error(end - pos, ret))
2392 return pos;
2393 pos += ret;
2394 }
2395 if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2396 ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2397 pos == start ? "" : "+");
2398 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002399 return pos;
2400 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002401 }
2402#endif /* CONFIG_IEEE80211R */
2403#ifdef CONFIG_IEEE80211W
2404 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2405 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002406 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002407 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002408 return pos;
2409 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002410 }
2411 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2412 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002413 pos == start ? "" : "+");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002414 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002415 return pos;
2416 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417 }
2418#endif /* CONFIG_IEEE80211W */
2419
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002420#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002421 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2422 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
2423 pos == start ? "" : "+");
2424 if (os_snprintf_error(end - pos, ret))
2425 return pos;
2426 pos += ret;
2427 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002428#endif /* CONFIG_SUITEB */
2429
2430#ifdef CONFIG_SUITEB192
2431 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
2432 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
2433 pos == start ? "" : "+");
2434 if (os_snprintf_error(end - pos, ret))
2435 return pos;
2436 pos += ret;
2437 }
2438#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002439
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002440 if (data.key_mgmt & WPA_KEY_MGMT_OSEN) {
2441 ret = os_snprintf(pos, end - pos, "%sOSEN",
2442 pos == start ? "" : "+");
2443 if (os_snprintf_error(end - pos, ret))
2444 return pos;
2445 pos += ret;
2446 }
2447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002448 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2449
2450 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2451 ret = os_snprintf(pos, end - pos, "-preauth");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002452 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453 return pos;
2454 pos += ret;
2455 }
2456
2457 ret = os_snprintf(pos, end - pos, "]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002458 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002459 return pos;
2460 pos += ret;
2461
2462 return pos;
2463}
2464
2465
2466#ifdef CONFIG_WPS
2467static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2468 char *pos, char *end,
2469 struct wpabuf *wps_ie)
2470{
2471 int ret;
2472 const char *txt;
2473
2474 if (wps_ie == NULL)
2475 return pos;
2476 if (wps_is_selected_pbc_registrar(wps_ie))
2477 txt = "[WPS-PBC]";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002478 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2479 txt = "[WPS-AUTH]";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480 else if (wps_is_selected_pin_registrar(wps_ie))
2481 txt = "[WPS-PIN]";
2482 else
2483 txt = "[WPS]";
2484
2485 ret = os_snprintf(pos, end - pos, "%s", txt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002486 if (!os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487 pos += ret;
2488 wpabuf_free(wps_ie);
2489 return pos;
2490}
2491#endif /* CONFIG_WPS */
2492
2493
2494static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2495 char *pos, char *end,
2496 const struct wpa_bss *bss)
2497{
2498#ifdef CONFIG_WPS
2499 struct wpabuf *wps_ie;
2500 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2501 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2502#else /* CONFIG_WPS */
2503 return pos;
2504#endif /* CONFIG_WPS */
2505}
2506
2507
2508/* Format one result on one text line into a buffer. */
2509static int wpa_supplicant_ctrl_iface_scan_result(
2510 struct wpa_supplicant *wpa_s,
2511 const struct wpa_bss *bss, char *buf, size_t buflen)
2512{
2513 char *pos, *end;
2514 int ret;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002515 const u8 *ie, *ie2, *osen_ie, *p2p, *mesh;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002517 mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07002519 if (!p2p)
2520 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2522 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2523 0)
2524 return 0; /* Do not show P2P listen discovery results here */
2525
2526 pos = buf;
2527 end = buf + buflen;
2528
2529 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2530 MAC2STR(bss->bssid), bss->freq, bss->level);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002531 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 return -1;
2533 pos += ret;
2534 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2535 if (ie)
2536 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2537 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002538 if (ie2) {
2539 pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
2540 ie2, 2 + ie2[1]);
2541 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002542 osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
2543 if (osen_ie)
2544 pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
2545 osen_ie, 2 + osen_ie[1]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002547 if (!ie && !ie2 && !osen_ie && (bss->caps & IEEE80211_CAP_PRIVACY)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548 ret = os_snprintf(pos, end - pos, "[WEP]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002549 if (os_snprintf_error(end - pos, ret))
2550 return -1;
2551 pos += ret;
2552 }
2553 if (mesh) {
2554 ret = os_snprintf(pos, end - pos, "[MESH]");
2555 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556 return -1;
2557 pos += ret;
2558 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002559 if (bss_is_dmg(bss)) {
2560 const char *s;
2561 ret = os_snprintf(pos, end - pos, "[DMG]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002562 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563 return -1;
2564 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002565 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
2566 case IEEE80211_CAP_DMG_IBSS:
2567 s = "[IBSS]";
2568 break;
2569 case IEEE80211_CAP_DMG_AP:
2570 s = "[ESS]";
2571 break;
2572 case IEEE80211_CAP_DMG_PBSS:
2573 s = "[PBSS]";
2574 break;
2575 default:
2576 s = "";
2577 break;
2578 }
2579 ret = os_snprintf(pos, end - pos, "%s", s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002580 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 return -1;
2582 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002583 } else {
2584 if (bss->caps & IEEE80211_CAP_IBSS) {
2585 ret = os_snprintf(pos, end - pos, "[IBSS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002586 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002587 return -1;
2588 pos += ret;
2589 }
2590 if (bss->caps & IEEE80211_CAP_ESS) {
2591 ret = os_snprintf(pos, end - pos, "[ESS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002592 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002593 return -1;
2594 pos += ret;
2595 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596 }
2597 if (p2p) {
2598 ret = os_snprintf(pos, end - pos, "[P2P]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002599 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600 return -1;
2601 pos += ret;
2602 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002603#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002604 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002605 ret = os_snprintf(pos, end - pos, "[HS20]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002606 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07002607 return -1;
2608 pos += ret;
2609 }
2610#endif /* CONFIG_HS20 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002611#ifdef CONFIG_FST
2612 if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
2613 ret = os_snprintf(pos, end - pos, "[FST]");
2614 if (os_snprintf_error(end - pos, ret))
2615 return -1;
2616 pos += ret;
2617 }
2618#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619
2620 ret = os_snprintf(pos, end - pos, "\t%s",
2621 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002622 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002623 return -1;
2624 pos += ret;
2625
2626 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002627 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002628 return -1;
2629 pos += ret;
2630
2631 return pos - buf;
2632}
2633
2634
2635static int wpa_supplicant_ctrl_iface_scan_results(
2636 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2637{
2638 char *pos, *end;
2639 struct wpa_bss *bss;
2640 int ret;
2641
2642 pos = buf;
2643 end = buf + buflen;
2644 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2645 "flags / ssid\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002646 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002647 return pos - buf;
2648 pos += ret;
2649
2650 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2651 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2652 end - pos);
2653 if (ret < 0 || ret >= end - pos)
2654 return pos - buf;
2655 pos += ret;
2656 }
2657
2658 return pos - buf;
2659}
2660
2661
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002662#ifdef CONFIG_MESH
2663
2664static int wpa_supplicant_ctrl_iface_mesh_interface_add(
2665 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
2666{
2667 char *pos, ifname[IFNAMSIZ + 1];
2668
2669 ifname[0] = '\0';
2670
2671 pos = os_strstr(cmd, "ifname=");
2672 if (pos) {
2673 pos += 7;
2674 os_strlcpy(ifname, pos, sizeof(ifname));
2675 }
2676
2677 if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
2678 return -1;
2679
2680 os_strlcpy(reply, ifname, max_len);
2681 return os_strlen(ifname);
2682}
2683
2684
2685static int wpa_supplicant_ctrl_iface_mesh_group_add(
2686 struct wpa_supplicant *wpa_s, char *cmd)
2687{
2688 int id;
2689 struct wpa_ssid *ssid;
2690
2691 id = atoi(cmd);
2692 wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
2693
2694 ssid = wpa_config_get_network(wpa_s->conf, id);
2695 if (ssid == NULL) {
2696 wpa_printf(MSG_DEBUG,
2697 "CTRL_IFACE: Could not find network id=%d", id);
2698 return -1;
2699 }
2700 if (ssid->mode != WPAS_MODE_MESH) {
2701 wpa_printf(MSG_DEBUG,
2702 "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
2703 return -1;
2704 }
2705 if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
2706 ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
2707 wpa_printf(MSG_ERROR,
2708 "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
2709 return -1;
2710 }
2711
2712 /*
2713 * TODO: If necessary write our own group_add function,
2714 * for now we can reuse select_network
2715 */
2716 wpa_supplicant_select_network(wpa_s, ssid);
2717
2718 return 0;
2719}
2720
2721
2722static int wpa_supplicant_ctrl_iface_mesh_group_remove(
2723 struct wpa_supplicant *wpa_s, char *cmd)
2724{
2725 struct wpa_supplicant *orig;
2726 struct wpa_global *global;
2727 int found = 0;
2728
2729 wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
2730
2731 global = wpa_s->global;
2732 orig = wpa_s;
2733
2734 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2735 if (os_strcmp(wpa_s->ifname, cmd) == 0) {
2736 found = 1;
2737 break;
2738 }
2739 }
2740 if (!found) {
2741 wpa_printf(MSG_ERROR,
2742 "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
2743 cmd);
2744 return -1;
2745 }
2746 if (wpa_s->mesh_if_created && wpa_s == orig) {
2747 wpa_printf(MSG_ERROR,
2748 "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
2749 return -1;
2750 }
2751
2752 wpa_s->reassociate = 0;
2753 wpa_s->disconnected = 1;
2754 wpa_supplicant_cancel_sched_scan(wpa_s);
2755 wpa_supplicant_cancel_scan(wpa_s);
2756
2757 /*
2758 * TODO: If necessary write our own group_remove function,
2759 * for now we can reuse deauthenticate
2760 */
2761 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2762
2763 if (wpa_s->mesh_if_created)
2764 wpa_supplicant_remove_iface(global, wpa_s, 0);
2765
2766 return 0;
2767}
2768
Dmitry Shmidte4663042016-04-04 10:07:49 -07002769
2770static int wpa_supplicant_ctrl_iface_mesh_peer_remove(
2771 struct wpa_supplicant *wpa_s, char *cmd)
2772{
2773 u8 addr[ETH_ALEN];
2774
2775 if (hwaddr_aton(cmd, addr) < 0)
2776 return -1;
2777
2778 return wpas_mesh_peer_remove(wpa_s, addr);
2779}
2780
2781
2782static int wpa_supplicant_ctrl_iface_mesh_peer_add(
2783 struct wpa_supplicant *wpa_s, char *cmd)
2784{
2785 u8 addr[ETH_ALEN];
2786 int duration;
2787 char *pos;
2788
2789 pos = os_strstr(cmd, " duration=");
2790 if (pos) {
2791 *pos = '\0';
2792 duration = atoi(pos + 10);
2793 } else {
2794 duration = -1;
2795 }
2796
2797 if (hwaddr_aton(cmd, addr))
2798 return -1;
2799
2800 return wpas_mesh_peer_add(wpa_s, addr, duration);
2801}
2802
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002803#endif /* CONFIG_MESH */
2804
2805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806static int wpa_supplicant_ctrl_iface_select_network(
2807 struct wpa_supplicant *wpa_s, char *cmd)
2808{
2809 int id;
2810 struct wpa_ssid *ssid;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002811 char *pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812
2813 /* cmd: "<network id>" or "any" */
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002814 if (os_strncmp(cmd, "any", 3) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2816 ssid = NULL;
2817 } else {
2818 id = atoi(cmd);
2819 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2820
2821 ssid = wpa_config_get_network(wpa_s->conf, id);
2822 if (ssid == NULL) {
2823 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2824 "network id=%d", id);
2825 return -1;
2826 }
2827 if (ssid->disabled == 2) {
2828 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2829 "SELECT_NETWORK with persistent P2P group");
2830 return -1;
2831 }
2832 }
2833
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002834 pos = os_strstr(cmd, " freq=");
2835 if (pos) {
2836 int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
2837 if (freqs) {
2838 wpa_s->scan_req = MANUAL_SCAN_REQ;
2839 os_free(wpa_s->manual_scan_freqs);
2840 wpa_s->manual_scan_freqs = freqs;
2841 }
2842 }
2843
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002844 wpa_s->scan_min_time.sec = 0;
2845 wpa_s->scan_min_time.usec = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002846 wpa_supplicant_select_network(wpa_s, ssid);
2847
2848 return 0;
2849}
2850
2851
2852static int wpa_supplicant_ctrl_iface_enable_network(
2853 struct wpa_supplicant *wpa_s, char *cmd)
2854{
2855 int id;
2856 struct wpa_ssid *ssid;
2857
2858 /* cmd: "<network id>" or "all" */
2859 if (os_strcmp(cmd, "all") == 0) {
2860 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2861 ssid = NULL;
2862 } else {
2863 id = atoi(cmd);
2864 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2865
2866 ssid = wpa_config_get_network(wpa_s->conf, id);
2867 if (ssid == NULL) {
2868 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2869 "network id=%d", id);
2870 return -1;
2871 }
2872 if (ssid->disabled == 2) {
2873 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2874 "ENABLE_NETWORK with persistent P2P group");
2875 return -1;
2876 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002877
2878 if (os_strstr(cmd, " no-connect")) {
2879 ssid->disabled = 0;
2880 return 0;
2881 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002882 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002883 wpa_s->scan_min_time.sec = 0;
2884 wpa_s->scan_min_time.usec = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885 wpa_supplicant_enable_network(wpa_s, ssid);
2886
2887 return 0;
2888}
2889
2890
2891static int wpa_supplicant_ctrl_iface_disable_network(
2892 struct wpa_supplicant *wpa_s, char *cmd)
2893{
2894 int id;
2895 struct wpa_ssid *ssid;
2896
2897 /* cmd: "<network id>" or "all" */
2898 if (os_strcmp(cmd, "all") == 0) {
2899 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2900 ssid = NULL;
2901 } else {
2902 id = atoi(cmd);
2903 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2904
2905 ssid = wpa_config_get_network(wpa_s->conf, id);
2906 if (ssid == NULL) {
2907 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2908 "network id=%d", id);
2909 return -1;
2910 }
2911 if (ssid->disabled == 2) {
2912 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2913 "DISABLE_NETWORK with persistent P2P "
2914 "group");
2915 return -1;
2916 }
2917 }
2918 wpa_supplicant_disable_network(wpa_s, ssid);
2919
2920 return 0;
2921}
2922
2923
2924static int wpa_supplicant_ctrl_iface_add_network(
2925 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2926{
2927 struct wpa_ssid *ssid;
2928 int ret;
2929
2930 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2931
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002932 ssid = wpa_supplicant_add_network(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002933 if (ssid == NULL)
2934 return -1;
2935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002937 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938 return -1;
2939 return ret;
2940}
2941
2942
2943static int wpa_supplicant_ctrl_iface_remove_network(
2944 struct wpa_supplicant *wpa_s, char *cmd)
2945{
2946 int id;
2947 struct wpa_ssid *ssid;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002948 int result;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002949
2950 /* cmd: "<network id>" or "all" */
2951 if (os_strcmp(cmd, "all") == 0) {
2952 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002953 if (wpa_s->sched_scanning)
2954 wpa_supplicant_cancel_sched_scan(wpa_s);
2955
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002956 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002957 if (wpa_s->current_ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002958#ifdef CONFIG_SME
2959 wpa_s->sme.prev_bssid_set = 0;
2960#endif /* CONFIG_SME */
Jouni Malinen75ecf522011-06-27 15:19:46 -07002961 wpa_sm_set_config(wpa_s->wpa, NULL);
2962 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002963 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2964 wpa_s->own_disconnect_req = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002965 wpa_supplicant_deauthenticate(
2966 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002967 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002968 ssid = wpa_s->conf->ssid;
2969 while (ssid) {
2970 struct wpa_ssid *remove_ssid = ssid;
2971 id = ssid->id;
2972 ssid = ssid->next;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002973 if (wpa_s->last_ssid == remove_ssid)
2974 wpa_s->last_ssid = NULL;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002975 wpas_notify_network_removed(wpa_s, remove_ssid);
2976 wpa_config_remove_network(wpa_s->conf, id);
2977 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002978 return 0;
2979 }
2980
2981 id = atoi(cmd);
2982 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2983
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002984 result = wpa_supplicant_remove_network(wpa_s, id);
2985 if (result == -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002986 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2987 "id=%d", id);
2988 return -1;
2989 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002990 if (result == -2) {
Deepthi Gowria831d782012-09-03 11:55:38 +03002991 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2992 "network id=%d", id);
2993 return -1;
2994 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995 return 0;
2996}
2997
2998
Dmitry Shmidt684785c2014-05-12 13:34:29 -07002999static int wpa_supplicant_ctrl_iface_update_network(
3000 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3001 char *name, char *value)
3002{
Dmitry Shmidte4663042016-04-04 10:07:49 -07003003 int ret;
3004
3005 ret = wpa_config_set(ssid, name, value, 0);
3006 if (ret < 0) {
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003007 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
3008 "variable '%s'", name);
3009 return -1;
3010 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07003011 if (ret == 1)
3012 return 0; /* No change to the previously configured value */
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003013
3014 if (os_strcmp(name, "bssid") != 0 &&
Dmitry Shmidte4663042016-04-04 10:07:49 -07003015 os_strcmp(name, "priority") != 0) {
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003016 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
3017
Dmitry Shmidte4663042016-04-04 10:07:49 -07003018 if (wpa_s->current_ssid == ssid ||
3019 wpa_s->current_ssid == NULL) {
3020 /*
3021 * Invalidate the EAP session cache if anything in the
3022 * current or previously used configuration changes.
3023 */
3024 eapol_sm_invalidate_cached_session(wpa_s->eapol);
3025 }
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003026 }
3027
3028 if ((os_strcmp(name, "psk") == 0 &&
3029 value[0] == '"' && ssid->ssid_len) ||
3030 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
3031 wpa_config_update_psk(ssid);
3032 else if (os_strcmp(name, "priority") == 0)
3033 wpa_config_update_prio_list(wpa_s->conf);
3034
3035 return 0;
3036}
3037
3038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003039static int wpa_supplicant_ctrl_iface_set_network(
3040 struct wpa_supplicant *wpa_s, char *cmd)
3041{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003042 int id, ret, prev_bssid_set, prev_disabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003043 struct wpa_ssid *ssid;
3044 char *name, *value;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003045 u8 prev_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046
3047 /* cmd: "<network id> <variable name> <value>" */
3048 name = os_strchr(cmd, ' ');
3049 if (name == NULL)
3050 return -1;
3051 *name++ = '\0';
3052
3053 value = os_strchr(name, ' ');
3054 if (value == NULL)
3055 return -1;
3056 *value++ = '\0';
3057
3058 id = atoi(cmd);
3059 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
3060 id, name);
3061 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3062 (u8 *) value, os_strlen(value));
3063
3064 ssid = wpa_config_get_network(wpa_s->conf, id);
3065 if (ssid == NULL) {
3066 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3067 "id=%d", id);
3068 return -1;
3069 }
3070
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003071 prev_bssid_set = ssid->bssid_set;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003072 prev_disabled = ssid->disabled;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003073 os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
3074 ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
3075 value);
3076 if (ret == 0 &&
3077 (ssid->bssid_set != prev_bssid_set ||
3078 os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
3079 wpas_notify_network_bssid_set_changed(wpa_s, ssid);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003080
3081 if (prev_disabled != ssid->disabled &&
3082 (prev_disabled == 2 || ssid->disabled == 2))
3083 wpas_notify_network_type_changed(wpa_s, ssid);
3084
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003085 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086}
3087
3088
3089static int wpa_supplicant_ctrl_iface_get_network(
3090 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
3091{
3092 int id;
3093 size_t res;
3094 struct wpa_ssid *ssid;
3095 char *name, *value;
3096
3097 /* cmd: "<network id> <variable name>" */
3098 name = os_strchr(cmd, ' ');
3099 if (name == NULL || buflen == 0)
3100 return -1;
3101 *name++ = '\0';
3102
3103 id = atoi(cmd);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003104 wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105 id, name);
3106
3107 ssid = wpa_config_get_network(wpa_s->conf, id);
3108 if (ssid == NULL) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003109 wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003110 "id=%d", id);
3111 return -1;
3112 }
3113
3114 value = wpa_config_get_no_key(ssid, name);
3115 if (value == NULL) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003116 wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003117 "variable '%s'", name);
3118 return -1;
3119 }
3120
3121 res = os_strlcpy(buf, value, buflen);
3122 if (res >= buflen) {
3123 os_free(value);
3124 return -1;
3125 }
3126
3127 os_free(value);
3128
3129 return res;
3130}
3131
3132
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003133static int wpa_supplicant_ctrl_iface_dup_network(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003134 struct wpa_supplicant *wpa_s, char *cmd,
3135 struct wpa_supplicant *dst_wpa_s)
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003136{
3137 struct wpa_ssid *ssid_s, *ssid_d;
3138 char *name, *id, *value;
3139 int id_s, id_d, ret;
3140
3141 /* cmd: "<src network id> <dst network id> <variable name>" */
3142 id = os_strchr(cmd, ' ');
3143 if (id == NULL)
3144 return -1;
3145 *id++ = '\0';
3146
3147 name = os_strchr(id, ' ');
3148 if (name == NULL)
3149 return -1;
3150 *name++ = '\0';
3151
3152 id_s = atoi(cmd);
3153 id_d = atoi(id);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003154
3155 wpa_printf(MSG_DEBUG,
3156 "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
3157 wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003158
3159 ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
3160 if (ssid_s == NULL) {
3161 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3162 "network id=%d", id_s);
3163 return -1;
3164 }
3165
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003166 ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003167 if (ssid_d == NULL) {
3168 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003169 "network id=%d", id_d);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003170 return -1;
3171 }
3172
3173 value = wpa_config_get(ssid_s, name);
3174 if (value == NULL) {
3175 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
3176 "variable '%s'", name);
3177 return -1;
3178 }
3179
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003180 ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003181 value);
3182
3183 os_free(value);
3184
3185 return ret;
3186}
3187
3188
Dmitry Shmidt04949592012-07-19 12:16:46 -07003189static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
3190 char *buf, size_t buflen)
3191{
3192 char *pos, *end;
3193 struct wpa_cred *cred;
3194 int ret;
3195
3196 pos = buf;
3197 end = buf + buflen;
3198 ret = os_snprintf(pos, end - pos,
3199 "cred id / realm / username / domain / imsi\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003200 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07003201 return pos - buf;
3202 pos += ret;
3203
3204 cred = wpa_s->conf->cred;
3205 while (cred) {
3206 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
3207 cred->id, cred->realm ? cred->realm : "",
3208 cred->username ? cred->username : "",
Dmitry Shmidt051af732013-10-22 13:52:46 -07003209 cred->domain ? cred->domain[0] : "",
Dmitry Shmidt04949592012-07-19 12:16:46 -07003210 cred->imsi ? cred->imsi : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003211 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07003212 return pos - buf;
3213 pos += ret;
3214
3215 cred = cred->next;
3216 }
3217
3218 return pos - buf;
3219}
3220
3221
3222static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
3223 char *buf, size_t buflen)
3224{
3225 struct wpa_cred *cred;
3226 int ret;
3227
3228 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
3229
3230 cred = wpa_config_add_cred(wpa_s->conf);
3231 if (cred == NULL)
3232 return -1;
3233
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003234 wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
3235
Dmitry Shmidt04949592012-07-19 12:16:46 -07003236 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003237 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt04949592012-07-19 12:16:46 -07003238 return -1;
3239 return ret;
3240}
3241
3242
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003243static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
3244 struct wpa_cred *cred)
3245{
3246 struct wpa_ssid *ssid;
3247 char str[20];
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003248 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003249
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003250 if (cred == NULL) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003251 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3252 return -1;
3253 }
3254
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003255 id = cred->id;
3256 if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
3257 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3258 return -1;
3259 }
3260
3261 wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
3262
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003263 /* Remove any network entry created based on the removed credential */
3264 ssid = wpa_s->conf->ssid;
3265 while (ssid) {
3266 if (ssid->parent_cred == cred) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003267 int res;
3268
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003269 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
3270 "used the removed credential", ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003271 res = os_snprintf(str, sizeof(str), "%d", ssid->id);
3272 if (os_snprintf_error(sizeof(str), res))
3273 str[sizeof(str) - 1] = '\0';
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003274 ssid = ssid->next;
3275 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
3276 } else
3277 ssid = ssid->next;
3278 }
3279
3280 return 0;
3281}
3282
3283
Dmitry Shmidt04949592012-07-19 12:16:46 -07003284static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
3285 char *cmd)
3286{
3287 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003288 struct wpa_cred *cred, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003289
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003290 /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
3291 * "provisioning_sp=<FQDN> */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003292 if (os_strcmp(cmd, "all") == 0) {
3293 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
3294 cred = wpa_s->conf->cred;
3295 while (cred) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003296 prev = cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003297 cred = cred->next;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003298 wpas_ctrl_remove_cred(wpa_s, prev);
3299 }
3300 return 0;
3301 }
3302
3303 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
3304 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
3305 cmd + 8);
3306 cred = wpa_s->conf->cred;
3307 while (cred) {
3308 prev = cred;
3309 cred = cred->next;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003310 if (prev->domain) {
3311 size_t i;
3312 for (i = 0; i < prev->num_domain; i++) {
3313 if (os_strcmp(prev->domain[i], cmd + 8)
3314 != 0)
3315 continue;
3316 wpas_ctrl_remove_cred(wpa_s, prev);
3317 break;
3318 }
3319 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003320 }
3321 return 0;
3322 }
3323
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003324 if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
3325 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
3326 cmd + 16);
3327 cred = wpa_s->conf->cred;
3328 while (cred) {
3329 prev = cred;
3330 cred = cred->next;
3331 if (prev->provisioning_sp &&
3332 os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
3333 wpas_ctrl_remove_cred(wpa_s, prev);
3334 }
3335 return 0;
3336 }
3337
Dmitry Shmidt04949592012-07-19 12:16:46 -07003338 id = atoi(cmd);
3339 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
3340
3341 cred = wpa_config_get_cred(wpa_s->conf, id);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003342 return wpas_ctrl_remove_cred(wpa_s, cred);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003343}
3344
3345
3346static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
3347 char *cmd)
3348{
3349 int id;
3350 struct wpa_cred *cred;
3351 char *name, *value;
3352
3353 /* cmd: "<cred id> <variable name> <value>" */
3354 name = os_strchr(cmd, ' ');
3355 if (name == NULL)
3356 return -1;
3357 *name++ = '\0';
3358
3359 value = os_strchr(name, ' ');
3360 if (value == NULL)
3361 return -1;
3362 *value++ = '\0';
3363
3364 id = atoi(cmd);
3365 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
3366 id, name);
3367 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3368 (u8 *) value, os_strlen(value));
3369
3370 cred = wpa_config_get_cred(wpa_s->conf, id);
3371 if (cred == NULL) {
3372 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3373 id);
3374 return -1;
3375 }
3376
3377 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
3378 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
3379 "variable '%s'", name);
3380 return -1;
3381 }
3382
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003383 wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
3384
Dmitry Shmidt04949592012-07-19 12:16:46 -07003385 return 0;
3386}
3387
3388
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003389static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
3390 char *cmd, char *buf,
3391 size_t buflen)
3392{
3393 int id;
3394 size_t res;
3395 struct wpa_cred *cred;
3396 char *name, *value;
3397
3398 /* cmd: "<cred id> <variable name>" */
3399 name = os_strchr(cmd, ' ');
3400 if (name == NULL)
3401 return -1;
3402 *name++ = '\0';
3403
3404 id = atoi(cmd);
3405 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
3406 id, name);
3407
3408 cred = wpa_config_get_cred(wpa_s->conf, id);
3409 if (cred == NULL) {
3410 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3411 id);
3412 return -1;
3413 }
3414
3415 value = wpa_config_get_cred_no_key(cred, name);
3416 if (value == NULL) {
3417 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
3418 name);
3419 return -1;
3420 }
3421
3422 res = os_strlcpy(buf, value, buflen);
3423 if (res >= buflen) {
3424 os_free(value);
3425 return -1;
3426 }
3427
3428 os_free(value);
3429
3430 return res;
3431}
3432
3433
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434#ifndef CONFIG_NO_CONFIG_WRITE
3435static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
3436{
3437 int ret;
3438
3439 if (!wpa_s->conf->update_config) {
3440 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
3441 "to update configuration (update_config=0)");
3442 return -1;
3443 }
3444
3445 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
3446 if (ret) {
3447 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
3448 "update configuration");
3449 } else {
3450 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
3451 " updated");
3452 }
3453
3454 return ret;
3455}
3456#endif /* CONFIG_NO_CONFIG_WRITE */
3457
3458
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003459struct cipher_info {
3460 unsigned int capa;
3461 const char *name;
3462 int group_only;
3463};
3464
3465static const struct cipher_info ciphers[] = {
3466 { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
3467 { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
3468 { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
3469 { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
3470 { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
3471 { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
3472 { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
3473 { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
3474};
3475
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003476static const struct cipher_info ciphers_group_mgmt[] = {
3477 { WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
3478 { WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
3479 { WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
3480 { WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
3481};
3482
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003484static int ctrl_iface_get_capability_pairwise(int res, char *strict,
3485 struct wpa_driver_capa *capa,
3486 char *buf, size_t buflen)
3487{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003488 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003489 char *pos, *end;
3490 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003491 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003492
3493 pos = buf;
3494 end = pos + buflen;
3495
3496 if (res < 0) {
3497 if (strict)
3498 return 0;
3499 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
3500 if (len >= buflen)
3501 return -1;
3502 return len;
3503 }
3504
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003505 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3506 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
3507 ret = os_snprintf(pos, end - pos, "%s%s",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003508 pos == buf ? "" : " ",
3509 ciphers[i].name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003510 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003511 return pos - buf;
3512 pos += ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003513 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003514 }
3515
3516 return pos - buf;
3517}
3518
3519
3520static int ctrl_iface_get_capability_group(int res, char *strict,
3521 struct wpa_driver_capa *capa,
3522 char *buf, size_t buflen)
3523{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003524 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003525 char *pos, *end;
3526 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003527 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003528
3529 pos = buf;
3530 end = pos + buflen;
3531
3532 if (res < 0) {
3533 if (strict)
3534 return 0;
3535 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
3536 if (len >= buflen)
3537 return -1;
3538 return len;
3539 }
3540
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003541 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3542 if (capa->enc & ciphers[i].capa) {
3543 ret = os_snprintf(pos, end - pos, "%s%s",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003544 pos == buf ? "" : " ",
3545 ciphers[i].name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003546 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003547 return pos - buf;
3548 pos += ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003549 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003550 }
3551
3552 return pos - buf;
3553}
3554
3555
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003556static int ctrl_iface_get_capability_group_mgmt(int res, char *strict,
3557 struct wpa_driver_capa *capa,
3558 char *buf, size_t buflen)
3559{
3560 int ret;
3561 char *pos, *end;
3562 unsigned int i;
3563
3564 pos = buf;
3565 end = pos + buflen;
3566
3567 if (res < 0)
3568 return 0;
3569
3570 for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
3571 if (capa->enc & ciphers_group_mgmt[i].capa) {
3572 ret = os_snprintf(pos, end - pos, "%s%s",
3573 pos == buf ? "" : " ",
3574 ciphers_group_mgmt[i].name);
3575 if (os_snprintf_error(end - pos, ret))
3576 return pos - buf;
3577 pos += ret;
3578 }
3579 }
3580
3581 return pos - buf;
3582}
3583
3584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003585static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
3586 struct wpa_driver_capa *capa,
3587 char *buf, size_t buflen)
3588{
3589 int ret;
3590 char *pos, *end;
3591 size_t len;
3592
3593 pos = buf;
3594 end = pos + buflen;
3595
3596 if (res < 0) {
3597 if (strict)
3598 return 0;
3599 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
3600 "NONE", buflen);
3601 if (len >= buflen)
3602 return -1;
3603 return len;
3604 }
3605
3606 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003607 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003608 return pos - buf;
3609 pos += ret;
3610
3611 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3612 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
3613 ret = os_snprintf(pos, end - pos, " WPA-EAP");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003614 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003615 return pos - buf;
3616 pos += ret;
3617 }
3618
3619 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3620 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
3621 ret = os_snprintf(pos, end - pos, " WPA-PSK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003622 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623 return pos - buf;
3624 pos += ret;
3625 }
3626
3627 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
3628 ret = os_snprintf(pos, end - pos, " WPA-NONE");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003629 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003630 return pos - buf;
3631 pos += ret;
3632 }
3633
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003634#ifdef CONFIG_SUITEB
3635 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
3636 ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
3637 if (os_snprintf_error(end - pos, ret))
3638 return pos - buf;
3639 pos += ret;
3640 }
3641#endif /* CONFIG_SUITEB */
3642#ifdef CONFIG_SUITEB192
3643 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
3644 ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
3645 if (os_snprintf_error(end - pos, ret))
3646 return pos - buf;
3647 pos += ret;
3648 }
3649#endif /* CONFIG_SUITEB192 */
3650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003651 return pos - buf;
3652}
3653
3654
3655static int ctrl_iface_get_capability_proto(int res, char *strict,
3656 struct wpa_driver_capa *capa,
3657 char *buf, size_t buflen)
3658{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003659 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003660 char *pos, *end;
3661 size_t len;
3662
3663 pos = buf;
3664 end = pos + buflen;
3665
3666 if (res < 0) {
3667 if (strict)
3668 return 0;
3669 len = os_strlcpy(buf, "RSN WPA", buflen);
3670 if (len >= buflen)
3671 return -1;
3672 return len;
3673 }
3674
3675 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3676 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003677 ret = os_snprintf(pos, end - pos, "%sRSN",
3678 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003679 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680 return pos - buf;
3681 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003682 }
3683
3684 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3685 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003686 ret = os_snprintf(pos, end - pos, "%sWPA",
3687 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003688 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003689 return pos - buf;
3690 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003691 }
3692
3693 return pos - buf;
3694}
3695
3696
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003697static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
3698 int res, char *strict,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003699 struct wpa_driver_capa *capa,
3700 char *buf, size_t buflen)
3701{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003702 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003703 char *pos, *end;
3704 size_t len;
3705
3706 pos = buf;
3707 end = pos + buflen;
3708
3709 if (res < 0) {
3710 if (strict)
3711 return 0;
3712 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
3713 if (len >= buflen)
3714 return -1;
3715 return len;
3716 }
3717
3718 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003719 ret = os_snprintf(pos, end - pos, "%sOPEN",
3720 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003721 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003722 return pos - buf;
3723 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003724 }
3725
3726 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
3727 ret = os_snprintf(pos, end - pos, "%sSHARED",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003728 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003729 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003730 return pos - buf;
3731 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003732 }
3733
3734 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003735 ret = os_snprintf(pos, end - pos, "%sLEAP",
3736 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003737 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003738 return pos - buf;
3739 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003740 }
3741
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003742#ifdef CONFIG_SAE
3743 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
3744 ret = os_snprintf(pos, end - pos, "%sSAE",
3745 pos == buf ? "" : " ");
3746 if (os_snprintf_error(end - pos, ret))
3747 return pos - buf;
3748 pos += ret;
3749 }
3750#endif /* CONFIG_SAE */
3751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003752 return pos - buf;
3753}
3754
3755
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003756static int ctrl_iface_get_capability_modes(int res, char *strict,
3757 struct wpa_driver_capa *capa,
3758 char *buf, size_t buflen)
3759{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003760 int ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003761 char *pos, *end;
3762 size_t len;
3763
3764 pos = buf;
3765 end = pos + buflen;
3766
3767 if (res < 0) {
3768 if (strict)
3769 return 0;
3770 len = os_strlcpy(buf, "IBSS AP", buflen);
3771 if (len >= buflen)
3772 return -1;
3773 return len;
3774 }
3775
3776 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003777 ret = os_snprintf(pos, end - pos, "%sIBSS",
3778 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003779 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003780 return pos - buf;
3781 pos += ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003782 }
3783
3784 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003785 ret = os_snprintf(pos, end - pos, "%sAP",
3786 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003787 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003788 return pos - buf;
3789 pos += ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003790 }
3791
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003792#ifdef CONFIG_MESH
3793 if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
3794 ret = os_snprintf(pos, end - pos, "%sMESH",
3795 pos == buf ? "" : " ");
3796 if (os_snprintf_error(end - pos, ret))
3797 return pos - buf;
3798 pos += ret;
3799 }
3800#endif /* CONFIG_MESH */
3801
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003802 return pos - buf;
3803}
3804
3805
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003806static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
3807 char *buf, size_t buflen)
3808{
3809 struct hostapd_channel_data *chnl;
3810 int ret, i, j;
3811 char *pos, *end, *hmode;
3812
3813 pos = buf;
3814 end = pos + buflen;
3815
3816 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3817 switch (wpa_s->hw.modes[j].mode) {
3818 case HOSTAPD_MODE_IEEE80211B:
3819 hmode = "B";
3820 break;
3821 case HOSTAPD_MODE_IEEE80211G:
3822 hmode = "G";
3823 break;
3824 case HOSTAPD_MODE_IEEE80211A:
3825 hmode = "A";
3826 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003827 case HOSTAPD_MODE_IEEE80211AD:
3828 hmode = "AD";
3829 break;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003830 default:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003831 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003832 }
3833 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003834 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003835 return pos - buf;
3836 pos += ret;
3837 chnl = wpa_s->hw.modes[j].channels;
3838 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003839 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3840 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003841 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003842 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003843 return pos - buf;
3844 pos += ret;
3845 }
3846 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003847 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003848 return pos - buf;
3849 pos += ret;
3850 }
3851
3852 return pos - buf;
3853}
3854
3855
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003856static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3857 char *buf, size_t buflen)
3858{
3859 struct hostapd_channel_data *chnl;
3860 int ret, i, j;
3861 char *pos, *end, *hmode;
3862
3863 pos = buf;
3864 end = pos + buflen;
3865
3866 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3867 switch (wpa_s->hw.modes[j].mode) {
3868 case HOSTAPD_MODE_IEEE80211B:
3869 hmode = "B";
3870 break;
3871 case HOSTAPD_MODE_IEEE80211G:
3872 hmode = "G";
3873 break;
3874 case HOSTAPD_MODE_IEEE80211A:
3875 hmode = "A";
3876 break;
3877 case HOSTAPD_MODE_IEEE80211AD:
3878 hmode = "AD";
3879 break;
3880 default:
3881 continue;
3882 }
3883 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3884 hmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003885 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003886 return pos - buf;
3887 pos += ret;
3888 chnl = wpa_s->hw.modes[j].channels;
3889 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3890 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3891 continue;
Dmitry Shmidt5da5e352014-02-03 13:30:46 -08003892 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003893 chnl[i].chan, chnl[i].freq,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003894 chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
3895 " (NO_IR)" : "",
Dmitry Shmidt5da5e352014-02-03 13:30:46 -08003896 chnl[i].flag & HOSTAPD_CHAN_RADAR ?
3897 " (DFS)" : "");
3898
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003899 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003900 return pos - buf;
3901 pos += ret;
3902 }
3903 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003904 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003905 return pos - buf;
3906 pos += ret;
3907 }
3908
3909 return pos - buf;
3910}
3911
3912
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003913static int wpa_supplicant_ctrl_iface_get_capability(
3914 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3915 size_t buflen)
3916{
3917 struct wpa_driver_capa capa;
3918 int res;
3919 char *strict;
3920 char field[30];
3921 size_t len;
3922
3923 /* Determine whether or not strict checking was requested */
3924 len = os_strlcpy(field, _field, sizeof(field));
3925 if (len >= sizeof(field))
3926 return -1;
3927 strict = os_strchr(field, ' ');
3928 if (strict != NULL) {
3929 *strict++ = '\0';
3930 if (os_strcmp(strict, "strict") != 0)
3931 return -1;
3932 }
3933
3934 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3935 field, strict ? strict : "");
3936
3937 if (os_strcmp(field, "eap") == 0) {
3938 return eap_get_names(buf, buflen);
3939 }
3940
3941 res = wpa_drv_get_capa(wpa_s, &capa);
3942
3943 if (os_strcmp(field, "pairwise") == 0)
3944 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3945 buf, buflen);
3946
3947 if (os_strcmp(field, "group") == 0)
3948 return ctrl_iface_get_capability_group(res, strict, &capa,
3949 buf, buflen);
3950
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003951 if (os_strcmp(field, "group_mgmt") == 0)
3952 return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
3953 buf, buflen);
3954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003955 if (os_strcmp(field, "key_mgmt") == 0)
3956 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3957 buf, buflen);
3958
3959 if (os_strcmp(field, "proto") == 0)
3960 return ctrl_iface_get_capability_proto(res, strict, &capa,
3961 buf, buflen);
3962
3963 if (os_strcmp(field, "auth_alg") == 0)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003964 return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
3965 &capa, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003966
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003967 if (os_strcmp(field, "modes") == 0)
3968 return ctrl_iface_get_capability_modes(res, strict, &capa,
3969 buf, buflen);
3970
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003971 if (os_strcmp(field, "channels") == 0)
3972 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3973
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003974 if (os_strcmp(field, "freq") == 0)
3975 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3976
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003977#ifdef CONFIG_TDLS
3978 if (os_strcmp(field, "tdls") == 0)
3979 return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
3980#endif /* CONFIG_TDLS */
3981
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003982#ifdef CONFIG_ERP
3983 if (os_strcmp(field, "erp") == 0) {
3984 res = os_snprintf(buf, buflen, "ERP");
3985 if (os_snprintf_error(buflen, res))
3986 return -1;
3987 return res;
3988 }
3989#endif /* CONFIG_EPR */
3990
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003991#ifdef CONFIG_FIPS
3992 if (os_strcmp(field, "fips") == 0) {
3993 res = os_snprintf(buf, buflen, "FIPS");
3994 if (os_snprintf_error(buflen, res))
3995 return -1;
3996 return res;
3997 }
3998#endif /* CONFIG_FIPS */
3999
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08004000#ifdef CONFIG_ACS
4001 if (os_strcmp(field, "acs") == 0) {
4002 res = os_snprintf(buf, buflen, "ACS");
4003 if (os_snprintf_error(buflen, res))
4004 return -1;
4005 return res;
4006 }
4007#endif /* CONFIG_ACS */
4008
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004009 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
4010 field);
4011
4012 return -1;
4013}
4014
4015
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004016#ifdef CONFIG_INTERWORKING
4017static char * anqp_add_hex(char *pos, char *end, const char *title,
4018 struct wpabuf *data)
4019{
4020 char *start = pos;
4021 size_t i;
4022 int ret;
4023 const u8 *d;
4024
4025 if (data == NULL)
4026 return start;
4027
4028 ret = os_snprintf(pos, end - pos, "%s=", title);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004029 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004030 return start;
4031 pos += ret;
4032
4033 d = wpabuf_head_u8(data);
4034 for (i = 0; i < wpabuf_len(data); i++) {
4035 ret = os_snprintf(pos, end - pos, "%02x", *d++);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004036 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004037 return start;
4038 pos += ret;
4039 }
4040
4041 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004042 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004043 return start;
4044 pos += ret;
4045
4046 return pos;
4047}
4048#endif /* CONFIG_INTERWORKING */
4049
4050
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004051static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4052 unsigned long mask, char *buf, size_t buflen)
4053{
4054 size_t i;
4055 int ret;
4056 char *pos, *end;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07004057 const u8 *ie, *ie2, *osen_ie;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004058
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004059 pos = buf;
4060 end = buf + buflen;
4061
4062 if (mask & WPA_BSS_MASK_ID) {
4063 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004064 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004065 return 0;
4066 pos += ret;
4067 }
4068
4069 if (mask & WPA_BSS_MASK_BSSID) {
4070 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
4071 MAC2STR(bss->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004072 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004073 return 0;
4074 pos += ret;
4075 }
4076
4077 if (mask & WPA_BSS_MASK_FREQ) {
4078 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004079 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004080 return 0;
4081 pos += ret;
4082 }
4083
4084 if (mask & WPA_BSS_MASK_BEACON_INT) {
4085 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
4086 bss->beacon_int);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004087 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004088 return 0;
4089 pos += ret;
4090 }
4091
4092 if (mask & WPA_BSS_MASK_CAPABILITIES) {
4093 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
4094 bss->caps);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004095 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004096 return 0;
4097 pos += ret;
4098 }
4099
4100 if (mask & WPA_BSS_MASK_QUAL) {
4101 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004102 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004103 return 0;
4104 pos += ret;
4105 }
4106
4107 if (mask & WPA_BSS_MASK_NOISE) {
4108 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004109 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004110 return 0;
4111 pos += ret;
4112 }
4113
4114 if (mask & WPA_BSS_MASK_LEVEL) {
4115 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004116 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004117 return 0;
4118 pos += ret;
4119 }
4120
4121 if (mask & WPA_BSS_MASK_TSF) {
4122 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
4123 (unsigned long long) bss->tsf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004124 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004125 return 0;
4126 pos += ret;
4127 }
4128
4129 if (mask & WPA_BSS_MASK_AGE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004130 struct os_reltime now;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004131
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004132 os_get_reltime(&now);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004133 ret = os_snprintf(pos, end - pos, "age=%d\n",
4134 (int) (now.sec - bss->last_update.sec));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004135 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004136 return 0;
4137 pos += ret;
4138 }
4139
4140 if (mask & WPA_BSS_MASK_IE) {
4141 ret = os_snprintf(pos, end - pos, "ie=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004142 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004143 return 0;
4144 pos += ret;
4145
4146 ie = (const u8 *) (bss + 1);
4147 for (i = 0; i < bss->ie_len; i++) {
4148 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004149 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004150 return 0;
4151 pos += ret;
4152 }
4153
4154 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004155 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004156 return 0;
4157 pos += ret;
4158 }
4159
4160 if (mask & WPA_BSS_MASK_FLAGS) {
4161 ret = os_snprintf(pos, end - pos, "flags=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004162 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004163 return 0;
4164 pos += ret;
4165
4166 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
4167 if (ie)
4168 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
4169 2 + ie[1]);
4170 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
4171 if (ie2)
4172 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
4173 2 + ie2[1]);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07004174 osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
4175 if (osen_ie)
4176 pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
4177 osen_ie, 2 + osen_ie[1]);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004178 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07004179 if (!ie && !ie2 && !osen_ie &&
4180 (bss->caps & IEEE80211_CAP_PRIVACY)) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004181 ret = os_snprintf(pos, end - pos, "[WEP]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004182 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004183 return 0;
4184 pos += ret;
4185 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004186 if (bss_is_dmg(bss)) {
4187 const char *s;
4188 ret = os_snprintf(pos, end - pos, "[DMG]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004189 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004190 return 0;
4191 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004192 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
4193 case IEEE80211_CAP_DMG_IBSS:
4194 s = "[IBSS]";
4195 break;
4196 case IEEE80211_CAP_DMG_AP:
4197 s = "[ESS]";
4198 break;
4199 case IEEE80211_CAP_DMG_PBSS:
4200 s = "[PBSS]";
4201 break;
4202 default:
4203 s = "";
4204 break;
4205 }
4206 ret = os_snprintf(pos, end - pos, "%s", s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004207 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004208 return 0;
4209 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004210 } else {
4211 if (bss->caps & IEEE80211_CAP_IBSS) {
4212 ret = os_snprintf(pos, end - pos, "[IBSS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004213 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004214 return 0;
4215 pos += ret;
4216 }
4217 if (bss->caps & IEEE80211_CAP_ESS) {
4218 ret = os_snprintf(pos, end - pos, "[ESS]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004219 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004220 return 0;
4221 pos += ret;
4222 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004223 }
Dmitry Shmidt96571392013-10-14 12:54:46 -07004224 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
4225 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004226 ret = os_snprintf(pos, end - pos, "[P2P]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004227 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004228 return 0;
4229 pos += ret;
4230 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004231#ifdef CONFIG_HS20
4232 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
4233 ret = os_snprintf(pos, end - pos, "[HS20]");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004234 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004235 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004236 pos += ret;
4237 }
4238#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004239
4240 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004241 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004242 return 0;
4243 pos += ret;
4244 }
4245
4246 if (mask & WPA_BSS_MASK_SSID) {
4247 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
4248 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004249 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004250 return 0;
4251 pos += ret;
4252 }
4253
4254#ifdef CONFIG_WPS
4255 if (mask & WPA_BSS_MASK_WPS_SCAN) {
4256 ie = (const u8 *) (bss + 1);
4257 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004258 if (ret >= end - pos)
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004259 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004260 if (ret > 0)
4261 pos += ret;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004262 }
4263#endif /* CONFIG_WPS */
4264
4265#ifdef CONFIG_P2P
4266 if (mask & WPA_BSS_MASK_P2P_SCAN) {
4267 ie = (const u8 *) (bss + 1);
4268 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004269 if (ret >= end - pos)
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004270 return 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004271 if (ret > 0)
4272 pos += ret;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004273 }
4274#endif /* CONFIG_P2P */
4275
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004276#ifdef CONFIG_WIFI_DISPLAY
4277 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
4278 struct wpabuf *wfd;
4279 ie = (const u8 *) (bss + 1);
4280 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
4281 WFD_IE_VENDOR_TYPE);
4282 if (wfd) {
4283 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004284 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004285 wpabuf_free(wfd);
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004286 return 0;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004287 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004288 pos += ret;
4289
4290 pos += wpa_snprintf_hex(pos, end - pos,
4291 wpabuf_head(wfd),
4292 wpabuf_len(wfd));
4293 wpabuf_free(wfd);
4294
4295 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004296 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004297 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004298 pos += ret;
4299 }
4300 }
4301#endif /* CONFIG_WIFI_DISPLAY */
4302
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004303#ifdef CONFIG_INTERWORKING
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004304 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
4305 struct wpa_bss_anqp *anqp = bss->anqp;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004306 struct wpa_bss_anqp_elem *elem;
4307
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004308 pos = anqp_add_hex(pos, end, "anqp_capability_list",
4309 anqp->capability_list);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004310 pos = anqp_add_hex(pos, end, "anqp_venue_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004311 anqp->venue_name);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004312 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004313 anqp->network_auth_type);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004314 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004315 anqp->roaming_consortium);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004316 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004317 anqp->ip_addr_type_availability);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004318 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004319 anqp->nai_realm);
4320 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004321 pos = anqp_add_hex(pos, end, "anqp_domain_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004322 anqp->domain_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004323#ifdef CONFIG_HS20
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004324 pos = anqp_add_hex(pos, end, "hs20_capability_list",
4325 anqp->hs20_capability_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004326 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004327 anqp->hs20_operator_friendly_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004328 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004329 anqp->hs20_wan_metrics);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004330 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004331 anqp->hs20_connection_capability);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004332 pos = anqp_add_hex(pos, end, "hs20_operating_class",
4333 anqp->hs20_operating_class);
4334 pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
4335 anqp->hs20_osu_providers_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004336#endif /* CONFIG_HS20 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004337
4338 dl_list_for_each(elem, &anqp->anqp_elems,
4339 struct wpa_bss_anqp_elem, list) {
4340 char title[20];
4341
4342 os_snprintf(title, sizeof(title), "anqp[%u]",
4343 elem->infoid);
4344 pos = anqp_add_hex(pos, end, title, elem->payload);
4345 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004346 }
4347#endif /* CONFIG_INTERWORKING */
4348
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004349#ifdef CONFIG_MESH
4350 if (mask & WPA_BSS_MASK_MESH_SCAN) {
4351 ie = (const u8 *) (bss + 1);
4352 ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004353 if (ret >= end - pos)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004354 return 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004355 if (ret > 0)
4356 pos += ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004357 }
4358#endif /* CONFIG_MESH */
4359
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004360 if (mask & WPA_BSS_MASK_SNR) {
4361 ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
4362 if (os_snprintf_error(end - pos, ret))
4363 return 0;
4364 pos += ret;
4365 }
4366
4367 if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
4368 ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
4369 bss->est_throughput);
4370 if (os_snprintf_error(end - pos, ret))
4371 return 0;
4372 pos += ret;
4373 }
4374
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004375#ifdef CONFIG_FST
4376 if (mask & WPA_BSS_MASK_FST) {
4377 ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
4378 if (ret < 0 || ret >= end - pos)
4379 return 0;
4380 pos += ret;
4381 }
4382#endif /* CONFIG_FST */
4383
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004384 if (mask & WPA_BSS_MASK_DELIM) {
4385 ret = os_snprintf(pos, end - pos, "====\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004386 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004387 return 0;
4388 pos += ret;
4389 }
Irfan Sheriffe2ea0082012-08-13 10:56:16 -07004390
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004391 return pos - buf;
4392}
4393
Dmitry Shmidt04949592012-07-19 12:16:46 -07004394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004395static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
4396 const char *cmd, char *buf,
4397 size_t buflen)
4398{
4399 u8 bssid[ETH_ALEN];
4400 size_t i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004401 struct wpa_bss *bss;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004402 struct wpa_bss *bsslast = NULL;
4403 struct dl_list *next;
4404 int ret = 0;
4405 int len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004406 char *ctmp, *end = buf + buflen;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004407 unsigned long mask = WPA_BSS_MASK_ALL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004408
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004409 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
4410 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
4411 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004412 list_id);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004413 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
4414 list_id);
4415 } else { /* N1-N2 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004416 unsigned int id1, id2;
4417
4418 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
4419 wpa_printf(MSG_INFO, "Wrong BSS range "
4420 "format");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004421 return 0;
4422 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004423
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004424 if (*(cmd + 6) == '-')
4425 id1 = 0;
4426 else
4427 id1 = atoi(cmd + 6);
4428 ctmp++;
4429 if (*ctmp >= '0' && *ctmp <= '9')
4430 id2 = atoi(ctmp);
4431 else
4432 id2 = (unsigned int) -1;
4433 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
4434 if (id2 == (unsigned int) -1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004435 bsslast = dl_list_last(&wpa_s->bss_id,
4436 struct wpa_bss,
4437 list_id);
4438 else {
4439 bsslast = wpa_bss_get_id(wpa_s, id2);
4440 if (bsslast == NULL && bss && id2 > id1) {
4441 struct wpa_bss *tmp = bss;
4442 for (;;) {
4443 next = tmp->list_id.next;
4444 if (next == &wpa_s->bss_id)
4445 break;
4446 tmp = dl_list_entry(
4447 next, struct wpa_bss,
4448 list_id);
4449 if (tmp->id > id2)
4450 break;
4451 bsslast = tmp;
4452 }
4453 }
4454 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004455 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004456 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004457 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004458 else if (os_strncmp(cmd, "LAST", 4) == 0)
4459 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004460 else if (os_strncmp(cmd, "ID-", 3) == 0) {
4461 i = atoi(cmd + 3);
4462 bss = wpa_bss_get_id(wpa_s, i);
4463 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4464 i = atoi(cmd + 5);
4465 bss = wpa_bss_get_id(wpa_s, i);
4466 if (bss) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004467 next = bss->list_id.next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004468 if (next == &wpa_s->bss_id)
4469 bss = NULL;
4470 else
4471 bss = dl_list_entry(next, struct wpa_bss,
4472 list_id);
4473 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004474#ifdef CONFIG_P2P
4475 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
4476 if (hwaddr_aton(cmd + 13, bssid) == 0)
4477 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
4478 else
4479 bss = NULL;
4480#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004481 } else if (hwaddr_aton(cmd, bssid) == 0)
4482 bss = wpa_bss_get_bssid(wpa_s, bssid);
4483 else {
4484 struct wpa_bss *tmp;
4485 i = atoi(cmd);
4486 bss = NULL;
4487 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
4488 {
4489 if (i-- == 0) {
4490 bss = tmp;
4491 break;
4492 }
4493 }
4494 }
4495
Dmitry Shmidt04949592012-07-19 12:16:46 -07004496 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
4497 mask = strtoul(ctmp + 5, NULL, 0x10);
4498 if (mask == 0)
4499 mask = WPA_BSS_MASK_ALL;
4500 }
4501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004502 if (bss == NULL)
4503 return 0;
4504
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004505 if (bsslast == NULL)
4506 bsslast = bss;
4507 do {
4508 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
4509 ret += len;
4510 buf += len;
4511 buflen -= len;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004512 if (bss == bsslast) {
4513 if ((mask & WPA_BSS_MASK_DELIM) && len &&
4514 (bss == dl_list_last(&wpa_s->bss_id,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004515 struct wpa_bss, list_id))) {
4516 int res;
4517
4518 res = os_snprintf(buf - 5, end - buf + 5,
4519 "####\n");
4520 if (os_snprintf_error(end - buf + 5, res)) {
4521 wpa_printf(MSG_DEBUG,
4522 "Could not add end delim");
4523 }
4524 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004525 break;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004526 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004527 next = bss->list_id.next;
4528 if (next == &wpa_s->bss_id)
4529 break;
4530 bss = dl_list_entry(next, struct wpa_bss, list_id);
4531 } while (bss && len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004532
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07004533 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004534}
4535
4536
4537static int wpa_supplicant_ctrl_iface_ap_scan(
4538 struct wpa_supplicant *wpa_s, char *cmd)
4539{
4540 int ap_scan = atoi(cmd);
4541 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
4542}
4543
4544
4545static int wpa_supplicant_ctrl_iface_scan_interval(
4546 struct wpa_supplicant *wpa_s, char *cmd)
4547{
4548 int scan_int = atoi(cmd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004549 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004550}
4551
4552
4553static int wpa_supplicant_ctrl_iface_bss_expire_age(
4554 struct wpa_supplicant *wpa_s, char *cmd)
4555{
4556 int expire_age = atoi(cmd);
4557 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
4558}
4559
4560
4561static int wpa_supplicant_ctrl_iface_bss_expire_count(
4562 struct wpa_supplicant *wpa_s, char *cmd)
4563{
4564 int expire_count = atoi(cmd);
4565 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
4566}
4567
4568
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004569static void wpa_supplicant_ctrl_iface_bss_flush(
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07004570 struct wpa_supplicant *wpa_s, char *cmd)
4571{
4572 int flush_age = atoi(cmd);
4573
4574 if (flush_age == 0)
4575 wpa_bss_flush(wpa_s);
4576 else
4577 wpa_bss_flush_by_age(wpa_s, flush_age);
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07004578}
4579
4580
Dmitry Shmidt21de2142014-04-08 10:50:52 -07004581#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004582static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
4583{
4584 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
4585 /* MLME-DELETEKEYS.request */
4586 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
4587 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
4588 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
4589 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
4590#ifdef CONFIG_IEEE80211W
4591 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
4592 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
4593#endif /* CONFIG_IEEE80211W */
4594
4595 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
4596 0);
4597 /* MLME-SETPROTECTION.request(None) */
4598 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
4599 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
4600 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
4601 wpa_sm_drop_sa(wpa_s->wpa);
4602}
Dmitry Shmidt21de2142014-04-08 10:50:52 -07004603#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004604
4605
4606static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
4607 char *addr)
4608{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004609#ifdef CONFIG_NO_SCAN_PROCESSING
4610 return -1;
4611#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612 u8 bssid[ETH_ALEN];
4613 struct wpa_bss *bss;
4614 struct wpa_ssid *ssid = wpa_s->current_ssid;
4615
4616 if (hwaddr_aton(addr, bssid)) {
4617 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
4618 "address '%s'", addr);
4619 return -1;
4620 }
4621
4622 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
4623
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004624 if (!ssid) {
4625 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
4626 "configuration known for the target AP");
4627 return -1;
4628 }
4629
4630 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004631 if (!bss) {
4632 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
4633 "from BSS table");
4634 return -1;
4635 }
4636
4637 /*
4638 * TODO: Find best network configuration block from configuration to
4639 * allow roaming to other networks
4640 */
4641
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004642 wpa_s->reassociate = 1;
4643 wpa_supplicant_connect(wpa_s, bss, ssid);
4644
4645 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004646#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647}
4648
4649
4650#ifdef CONFIG_P2P
4651static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
4652{
4653 unsigned int timeout = atoi(cmd);
4654 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004655 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004656 u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004657 char *pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004658 unsigned int search_delay;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004659 const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004660 u8 seek_count = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004661 int freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004663 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4664 wpa_dbg(wpa_s, MSG_INFO,
4665 "Reject P2P_FIND since interface is disabled");
4666 return -1;
4667 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004668 if (os_strstr(cmd, "type=social"))
4669 type = P2P_FIND_ONLY_SOCIAL;
4670 else if (os_strstr(cmd, "type=progressive"))
4671 type = P2P_FIND_PROGRESSIVE;
4672
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004673 pos = os_strstr(cmd, "dev_id=");
4674 if (pos) {
4675 pos += 7;
4676 if (hwaddr_aton(pos, dev_id))
4677 return -1;
4678 _dev_id = dev_id;
4679 }
4680
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004681 pos = os_strstr(cmd, "dev_type=");
4682 if (pos) {
4683 pos += 9;
4684 if (wps_dev_type_str2bin(pos, dev_type) < 0)
4685 return -1;
4686 _dev_type = dev_type;
4687 }
4688
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004689 pos = os_strstr(cmd, "delay=");
4690 if (pos) {
4691 pos += 6;
4692 search_delay = atoi(pos);
4693 } else
4694 search_delay = wpas_p2p_search_delay(wpa_s);
4695
Dmitry Shmidt41712582015-06-29 11:02:15 -07004696 pos = os_strstr(cmd, "freq=");
4697 if (pos) {
4698 pos += 5;
4699 freq = atoi(pos);
4700 if (freq <= 0)
4701 return -1;
4702 }
4703
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004704 /* Must be searched for last, because it adds nul termination */
4705 pos = os_strstr(cmd, " seek=");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004706 if (pos)
4707 pos += 6;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004708 while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
4709 char *term;
4710
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004711 _seek[seek_count++] = pos;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004712 seek = _seek;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004713 term = os_strchr(pos, ' ');
4714 if (!term)
4715 break;
4716 *term = '\0';
4717 pos = os_strstr(term + 1, "seek=");
4718 if (pos)
4719 pos += 5;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004720 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004721 if (seek_count > P2P_MAX_QUERY_HASH) {
4722 seek[0] = NULL;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004723 seek_count = 1;
4724 }
4725
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004726 return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004727 _dev_id, search_delay, seek_count, seek, freq);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004728}
4729
4730
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004731static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
4732{
4733 const char *last = NULL;
4734 const char *token;
4735 long int token_len;
4736 unsigned int i;
4737
4738 /* Expected predefined CPT names delimited by ':' */
4739 for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
4740 if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
4741 wpa_printf(MSG_ERROR,
4742 "P2PS: CPT name list is too long, expected up to %d names",
4743 P2PS_FEATURE_CAPAB_CPT_MAX);
4744 cpt[0] = 0;
4745 return -1;
4746 }
4747
4748 token_len = last - token;
4749
4750 if (token_len == 3 &&
4751 os_memcmp(token, "UDP", token_len) == 0) {
4752 cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
4753 } else if (token_len == 3 &&
4754 os_memcmp(token, "MAC", token_len) == 0) {
4755 cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
4756 } else {
4757 wpa_printf(MSG_ERROR,
4758 "P2PS: Unsupported CPT name '%s'", token);
4759 cpt[0] = 0;
4760 return -1;
4761 }
4762
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004763 if (isblank((unsigned char) *last)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004764 i++;
4765 break;
4766 }
4767 }
4768 cpt[i] = 0;
4769 return 0;
4770}
4771
4772
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004773static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
4774{
4775 struct p2ps_provision *p2ps_prov;
4776 char *pos;
4777 size_t info_len = 0;
4778 char *info = NULL;
4779 u8 role = P2PS_SETUP_NONE;
4780 long long unsigned val;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004781 int i;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004782
4783 pos = os_strstr(cmd, "info=");
4784 if (pos) {
4785 pos += 5;
4786 info_len = os_strlen(pos);
4787
4788 if (info_len) {
4789 info = os_malloc(info_len + 1);
4790 if (info) {
4791 info_len = utf8_unescape(pos, info_len,
4792 info, info_len + 1);
4793 } else
4794 info_len = 0;
4795 }
4796 }
4797
4798 p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
4799 if (p2ps_prov == NULL) {
4800 os_free(info);
4801 return NULL;
4802 }
4803
4804 if (info) {
4805 os_memcpy(p2ps_prov->info, info, info_len);
4806 p2ps_prov->info[info_len] = '\0';
4807 os_free(info);
4808 }
4809
4810 pos = os_strstr(cmd, "status=");
4811 if (pos)
4812 p2ps_prov->status = atoi(pos + 7);
4813 else
4814 p2ps_prov->status = -1;
4815
4816 pos = os_strstr(cmd, "adv_id=");
4817 if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
4818 goto invalid_args;
4819 p2ps_prov->adv_id = val;
4820
4821 pos = os_strstr(cmd, "method=");
4822 if (pos)
4823 p2ps_prov->method = strtol(pos + 7, NULL, 16);
4824 else
4825 p2ps_prov->method = 0;
4826
4827 pos = os_strstr(cmd, "session=");
4828 if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
4829 goto invalid_args;
4830 p2ps_prov->session_id = val;
4831
4832 pos = os_strstr(cmd, "adv_mac=");
4833 if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
4834 goto invalid_args;
4835
4836 pos = os_strstr(cmd, "session_mac=");
4837 if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
4838 goto invalid_args;
4839
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004840 pos = os_strstr(cmd, "cpt=");
4841 if (pos) {
4842 if (p2ps_ctrl_parse_cpt_priority(pos + 4,
4843 p2ps_prov->cpt_priority))
4844 goto invalid_args;
4845 } else {
4846 p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
4847 }
4848
4849 for (i = 0; p2ps_prov->cpt_priority[i]; i++)
4850 p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
4851
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004852 /* force conncap with tstCap (no sanity checks) */
4853 pos = os_strstr(cmd, "tstCap=");
4854 if (pos) {
4855 role = strtol(pos + 7, NULL, 16);
4856 } else {
4857 pos = os_strstr(cmd, "role=");
4858 if (pos) {
4859 role = strtol(pos + 5, NULL, 16);
4860 if (role != P2PS_SETUP_CLIENT &&
4861 role != P2PS_SETUP_GROUP_OWNER)
4862 role = P2PS_SETUP_NONE;
4863 }
4864 }
4865 p2ps_prov->role = role;
4866
4867 return p2ps_prov;
4868
4869invalid_args:
4870 os_free(p2ps_prov);
4871 return NULL;
4872}
4873
4874
4875static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
4876{
4877 u8 addr[ETH_ALEN];
4878 struct p2ps_provision *p2ps_prov;
4879 char *pos;
4880
4881 /* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
4882
4883 wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
4884
4885 if (hwaddr_aton(cmd, addr))
4886 return -1;
4887
4888 pos = cmd + 17;
4889 if (*pos != ' ')
4890 return -1;
4891
4892 p2ps_prov = p2p_parse_asp_provision_cmd(pos);
4893 if (!p2ps_prov)
4894 return -1;
4895
4896 if (p2ps_prov->status < 0) {
4897 os_free(p2ps_prov);
4898 return -1;
4899 }
4900
4901 return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
4902 p2ps_prov);
4903}
4904
4905
4906static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
4907{
4908 u8 addr[ETH_ALEN];
4909 struct p2ps_provision *p2ps_prov;
4910 char *pos;
4911
4912 /* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
4913 * session=<ses_id> mac=<ses_mac> [info=<infodata>]
4914 */
4915
4916 wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
4917 if (hwaddr_aton(cmd, addr))
4918 return -1;
4919
4920 pos = cmd + 17;
4921 if (*pos != ' ')
4922 return -1;
4923
4924 p2ps_prov = p2p_parse_asp_provision_cmd(pos);
4925 if (!p2ps_prov)
4926 return -1;
4927
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004928 p2ps_prov->pd_seeker = 1;
4929
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004930 return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
4931 p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004932}
4933
4934
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004935static int parse_freq(int chwidth, int freq2)
4936{
4937 if (freq2 < 0)
4938 return -1;
4939 if (freq2)
4940 return VHT_CHANWIDTH_80P80MHZ;
4941
4942 switch (chwidth) {
4943 case 0:
4944 case 20:
4945 case 40:
4946 return VHT_CHANWIDTH_USE_HT;
4947 case 80:
4948 return VHT_CHANWIDTH_80MHZ;
4949 case 160:
4950 return VHT_CHANWIDTH_160MHZ;
4951 default:
4952 wpa_printf(MSG_DEBUG, "Unknown max oper bandwidth: %d",
4953 chwidth);
4954 return -1;
4955 }
4956}
4957
4958
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004959static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
4960 char *buf, size_t buflen)
4961{
4962 u8 addr[ETH_ALEN];
4963 char *pos, *pos2;
4964 char *pin = NULL;
4965 enum p2p_wps_method wps_method;
4966 int new_pin;
4967 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004968 int persistent_group, persistent_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004969 int join;
4970 int auth;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004971 int automatic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972 int go_intent = -1;
4973 int freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004974 int pd;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004975 int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004976 u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
4977 size_t group_ssid_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004978
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004979 if (!wpa_s->global->p2p_init_wpa_s)
4980 return -1;
4981 if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
4982 wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
4983 wpa_s->global->p2p_init_wpa_s->ifname);
4984 wpa_s = wpa_s->global->p2p_init_wpa_s;
4985 }
4986
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004987 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
Dmitry Shmidt04949592012-07-19 12:16:46 -07004988 * [persistent|persistent=<network id>]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004989 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004990 * [ht40] [vht] [auto] [ssid=<hexdump>] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004991
4992 if (hwaddr_aton(cmd, addr))
4993 return -1;
4994
4995 pos = cmd + 17;
4996 if (*pos != ' ')
4997 return -1;
4998 pos++;
4999
5000 persistent_group = os_strstr(pos, " persistent") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005001 pos2 = os_strstr(pos, " persistent=");
5002 if (pos2) {
5003 struct wpa_ssid *ssid;
5004 persistent_id = atoi(pos2 + 12);
5005 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5006 if (ssid == NULL || ssid->disabled != 2 ||
5007 ssid->mode != WPAS_MODE_P2P_GO) {
5008 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
5009 "SSID id=%d for persistent P2P group (GO)",
5010 persistent_id);
5011 return -1;
5012 }
5013 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 join = os_strstr(pos, " join") != NULL;
5015 auth = os_strstr(pos, " auth") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005016 automatic = os_strstr(pos, " auto") != NULL;
5017 pd = os_strstr(pos, " provdisc") != NULL;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005018 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
5019 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
5020 vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005021
5022 pos2 = os_strstr(pos, " go_intent=");
5023 if (pos2) {
5024 pos2 += 11;
5025 go_intent = atoi(pos2);
5026 if (go_intent < 0 || go_intent > 15)
5027 return -1;
5028 }
5029
5030 pos2 = os_strstr(pos, " freq=");
5031 if (pos2) {
5032 pos2 += 6;
5033 freq = atoi(pos2);
5034 if (freq <= 0)
5035 return -1;
5036 }
5037
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005038 pos2 = os_strstr(pos, " freq2=");
5039 if (pos2)
5040 freq2 = atoi(pos2 + 7);
5041
5042 pos2 = os_strstr(pos, " max_oper_chwidth=");
5043 if (pos2)
5044 chwidth = atoi(pos2 + 18);
5045
5046 max_oper_chwidth = parse_freq(chwidth, freq2);
5047 if (max_oper_chwidth < 0)
5048 return -1;
5049
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005050 pos2 = os_strstr(pos, " ssid=");
5051 if (pos2) {
5052 char *end;
5053
5054 pos2 += 6;
5055 end = os_strchr(pos2, ' ');
5056 if (!end)
5057 group_ssid_len = os_strlen(pos2) / 2;
5058 else
5059 group_ssid_len = (end - pos2) / 2;
5060 if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
5061 hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
5062 return -1;
5063 group_ssid = _group_ssid;
5064 }
5065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005066 if (os_strncmp(pos, "pin", 3) == 0) {
5067 /* Request random PIN (to be displayed) and enable the PIN */
5068 wps_method = WPS_PIN_DISPLAY;
5069 } else if (os_strncmp(pos, "pbc", 3) == 0) {
5070 wps_method = WPS_PBC;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005071 } else if (os_strstr(pos, "p2ps") != NULL) {
5072 wps_method = WPS_P2PS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005073 } else {
5074 pin = pos;
5075 pos = os_strchr(pin, ' ');
5076 wps_method = WPS_PIN_KEYPAD;
5077 if (pos) {
5078 *pos++ = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005079 if (os_strncmp(pos, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080 wps_method = WPS_PIN_DISPLAY;
5081 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005082 if (!wps_pin_str_valid(pin)) {
5083 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
5084 return 17;
5085 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005086 }
5087
5088 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005089 persistent_group, automatic, join,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005090 auth, go_intent, freq, freq2, persistent_id,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005091 pd, ht40, vht, max_oper_chwidth,
5092 group_ssid, group_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005093 if (new_pin == -2) {
5094 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
5095 return 25;
5096 }
5097 if (new_pin == -3) {
5098 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
5099 return 25;
5100 }
5101 if (new_pin < 0)
5102 return -1;
5103 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
5104 ret = os_snprintf(buf, buflen, "%08d", new_pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005105 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005106 return -1;
5107 return ret;
5108 }
5109
5110 os_memcpy(buf, "OK\n", 3);
5111 return 3;
5112}
5113
5114
5115static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
5116{
5117 unsigned int timeout = atoi(cmd);
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07005118 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5119 wpa_dbg(wpa_s, MSG_INFO,
5120 "Reject P2P_LISTEN since interface is disabled");
5121 return -1;
5122 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005123 return wpas_p2p_listen(wpa_s, timeout);
5124}
5125
5126
5127static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
5128{
5129 u8 addr[ETH_ALEN];
5130 char *pos;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005131 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005132
Dmitry Shmidt04949592012-07-19 12:16:46 -07005133 /* <addr> <config method> [join|auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005134
5135 if (hwaddr_aton(cmd, addr))
5136 return -1;
5137
5138 pos = cmd + 17;
5139 if (*pos != ' ')
5140 return -1;
5141 pos++;
5142
Dmitry Shmidt04949592012-07-19 12:16:46 -07005143 if (os_strstr(pos, " join") != NULL)
5144 use = WPAS_P2P_PD_FOR_JOIN;
5145 else if (os_strstr(pos, " auto") != NULL)
5146 use = WPAS_P2P_PD_AUTO;
5147
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005148 return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005149}
5150
5151
5152static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
5153 size_t buflen)
5154{
5155 struct wpa_ssid *ssid = wpa_s->current_ssid;
5156
5157 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
5158 ssid->passphrase == NULL)
5159 return -1;
5160
5161 os_strlcpy(buf, ssid->passphrase, buflen);
5162 return os_strlen(buf);
5163}
5164
5165
5166static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
5167 char *buf, size_t buflen)
5168{
5169 u64 ref;
5170 int res;
5171 u8 dst_buf[ETH_ALEN], *dst;
5172 struct wpabuf *tlvs;
5173 char *pos;
5174 size_t len;
5175
5176 if (hwaddr_aton(cmd, dst_buf))
5177 return -1;
5178 dst = dst_buf;
5179 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
5180 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
5181 dst = NULL;
5182 pos = cmd + 17;
5183 if (*pos != ' ')
5184 return -1;
5185 pos++;
5186
5187 if (os_strncmp(pos, "upnp ", 5) == 0) {
5188 u8 version;
5189 pos += 5;
5190 if (hexstr2bin(pos, &version, 1) < 0)
5191 return -1;
5192 pos += 2;
5193 if (*pos != ' ')
5194 return -1;
5195 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005196 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005197#ifdef CONFIG_WIFI_DISPLAY
5198 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
5199 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
5200#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005201 } else if (os_strncmp(pos, "asp ", 4) == 0) {
5202 char *svc_str;
5203 char *svc_info = NULL;
5204 u32 id;
5205
5206 pos += 4;
5207 if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
5208 return -1;
5209
5210 pos = os_strchr(pos, ' ');
5211 if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
5212 return -1;
5213
5214 svc_str = pos + 1;
5215
5216 pos = os_strchr(svc_str, ' ');
5217
5218 if (pos)
5219 *pos++ = '\0';
5220
5221 /* All remaining data is the svc_info string */
5222 if (pos && pos[0] && pos[0] != ' ') {
5223 len = os_strlen(pos);
5224
5225 /* Unescape in place */
5226 len = utf8_unescape(pos, len, pos, len);
5227 if (len > 0xff)
5228 return -1;
5229
5230 svc_info = pos;
5231 }
5232
5233 ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
5234 svc_str, svc_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005235 } else {
5236 len = os_strlen(pos);
5237 if (len & 1)
5238 return -1;
5239 len /= 2;
5240 tlvs = wpabuf_alloc(len);
5241 if (tlvs == NULL)
5242 return -1;
5243 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
5244 wpabuf_free(tlvs);
5245 return -1;
5246 }
5247
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005248 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005249 wpabuf_free(tlvs);
5250 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005251 if (ref == 0)
5252 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005253 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005254 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005255 return -1;
5256 return res;
5257}
5258
5259
5260static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
5261 char *cmd)
5262{
5263 long long unsigned val;
5264 u64 req;
5265 if (sscanf(cmd, "%llx", &val) != 1)
5266 return -1;
5267 req = val;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005268 return wpas_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005269}
5270
5271
5272static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
5273{
5274 int freq;
5275 u8 dst[ETH_ALEN];
5276 u8 dialog_token;
5277 struct wpabuf *resp_tlvs;
5278 char *pos, *pos2;
5279 size_t len;
5280
5281 pos = os_strchr(cmd, ' ');
5282 if (pos == NULL)
5283 return -1;
5284 *pos++ = '\0';
5285 freq = atoi(cmd);
5286 if (freq == 0)
5287 return -1;
5288
5289 if (hwaddr_aton(pos, dst))
5290 return -1;
5291 pos += 17;
5292 if (*pos != ' ')
5293 return -1;
5294 pos++;
5295
5296 pos2 = os_strchr(pos, ' ');
5297 if (pos2 == NULL)
5298 return -1;
5299 *pos2++ = '\0';
5300 dialog_token = atoi(pos);
5301
5302 len = os_strlen(pos2);
5303 if (len & 1)
5304 return -1;
5305 len /= 2;
5306 resp_tlvs = wpabuf_alloc(len);
5307 if (resp_tlvs == NULL)
5308 return -1;
5309 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
5310 wpabuf_free(resp_tlvs);
5311 return -1;
5312 }
5313
5314 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
5315 wpabuf_free(resp_tlvs);
5316 return 0;
5317}
5318
5319
5320static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
5321 char *cmd)
5322{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005323 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
5324 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005325 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
5326 return 0;
5327}
5328
5329
5330static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
5331 char *cmd)
5332{
5333 char *pos;
5334 size_t len;
5335 struct wpabuf *query, *resp;
5336
5337 pos = os_strchr(cmd, ' ');
5338 if (pos == NULL)
5339 return -1;
5340 *pos++ = '\0';
5341
5342 len = os_strlen(cmd);
5343 if (len & 1)
5344 return -1;
5345 len /= 2;
5346 query = wpabuf_alloc(len);
5347 if (query == NULL)
5348 return -1;
5349 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
5350 wpabuf_free(query);
5351 return -1;
5352 }
5353
5354 len = os_strlen(pos);
5355 if (len & 1) {
5356 wpabuf_free(query);
5357 return -1;
5358 }
5359 len /= 2;
5360 resp = wpabuf_alloc(len);
5361 if (resp == NULL) {
5362 wpabuf_free(query);
5363 return -1;
5364 }
5365 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
5366 wpabuf_free(query);
5367 wpabuf_free(resp);
5368 return -1;
5369 }
5370
5371 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
5372 wpabuf_free(query);
5373 wpabuf_free(resp);
5374 return -1;
5375 }
5376 return 0;
5377}
5378
5379
5380static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
5381{
5382 char *pos;
5383 u8 version;
5384
5385 pos = os_strchr(cmd, ' ');
5386 if (pos == NULL)
5387 return -1;
5388 *pos++ = '\0';
5389
5390 if (hexstr2bin(cmd, &version, 1) < 0)
5391 return -1;
5392
5393 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
5394}
5395
5396
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005397static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
5398 u8 replace, char *cmd)
5399{
5400 char *pos;
5401 char *adv_str;
5402 u32 auto_accept, adv_id, svc_state, config_methods;
5403 char *svc_info = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005404 char *cpt_prio_str;
5405 u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005406
5407 pos = os_strchr(cmd, ' ');
5408 if (pos == NULL)
5409 return -1;
5410 *pos++ = '\0';
5411
5412 /* Auto-Accept value is mandatory, and must be one of the
5413 * single values (0, 1, 2, 4) */
5414 auto_accept = atoi(cmd);
5415 switch (auto_accept) {
5416 case P2PS_SETUP_NONE: /* No auto-accept */
5417 case P2PS_SETUP_NEW:
5418 case P2PS_SETUP_CLIENT:
5419 case P2PS_SETUP_GROUP_OWNER:
5420 break;
5421 default:
5422 return -1;
5423 }
5424
5425 /* Advertisement ID is mandatory */
5426 cmd = pos;
5427 pos = os_strchr(cmd, ' ');
5428 if (pos == NULL)
5429 return -1;
5430 *pos++ = '\0';
5431
5432 /* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
5433 if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
5434 return -1;
5435
5436 /* Only allow replacements if exist, and adds if not */
5437 if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
5438 if (!replace)
5439 return -1;
5440 } else {
5441 if (replace)
5442 return -1;
5443 }
5444
5445 /* svc_state between 0 - 0xff is mandatory */
5446 if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
5447 return -1;
5448
5449 pos = os_strchr(pos, ' ');
5450 if (pos == NULL)
5451 return -1;
5452
5453 /* config_methods is mandatory */
5454 pos++;
5455 if (sscanf(pos, "%x", &config_methods) != 1)
5456 return -1;
5457
5458 if (!(config_methods &
5459 (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
5460 return -1;
5461
5462 pos = os_strchr(pos, ' ');
5463 if (pos == NULL)
5464 return -1;
5465
5466 pos++;
5467 adv_str = pos;
5468
5469 /* Advertisement string is mandatory */
5470 if (!pos[0] || pos[0] == ' ')
5471 return -1;
5472
5473 /* Terminate svc string */
5474 pos = os_strchr(pos, ' ');
5475 if (pos != NULL)
5476 *pos++ = '\0';
5477
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005478 cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
5479 if (cpt_prio_str) {
5480 pos = os_strchr(pos, ' ');
5481 if (pos != NULL)
5482 *pos++ = '\0';
5483
5484 if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
5485 return -1;
5486 } else {
5487 cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
5488 cpt_prio[1] = 0;
5489 }
5490
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005491 /* Service and Response Information are optional */
5492 if (pos && pos[0]) {
5493 size_t len;
5494
5495 /* Note the bare ' included, which cannot exist legally
5496 * in unescaped string. */
5497 svc_info = os_strstr(pos, "svc_info='");
5498
5499 if (svc_info) {
5500 svc_info += 9;
5501 len = os_strlen(svc_info);
5502 utf8_unescape(svc_info, len, svc_info, len);
5503 }
5504 }
5505
5506 return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
5507 (u8) svc_state, (u16) config_methods,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005508 svc_info, cpt_prio);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005509}
5510
5511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005512static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
5513{
5514 char *pos;
5515
5516 pos = os_strchr(cmd, ' ');
5517 if (pos == NULL)
5518 return -1;
5519 *pos++ = '\0';
5520
5521 if (os_strcmp(cmd, "bonjour") == 0)
5522 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
5523 if (os_strcmp(cmd, "upnp") == 0)
5524 return p2p_ctrl_service_add_upnp(wpa_s, pos);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005525 if (os_strcmp(cmd, "asp") == 0)
5526 return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005527 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
5528 return -1;
5529}
5530
5531
5532static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
5533 char *cmd)
5534{
5535 size_t len;
5536 struct wpabuf *query;
5537 int ret;
5538
5539 len = os_strlen(cmd);
5540 if (len & 1)
5541 return -1;
5542 len /= 2;
5543 query = wpabuf_alloc(len);
5544 if (query == NULL)
5545 return -1;
5546 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
5547 wpabuf_free(query);
5548 return -1;
5549 }
5550
5551 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
5552 wpabuf_free(query);
5553 return ret;
5554}
5555
5556
5557static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
5558{
5559 char *pos;
5560 u8 version;
5561
5562 pos = os_strchr(cmd, ' ');
5563 if (pos == NULL)
5564 return -1;
5565 *pos++ = '\0';
5566
5567 if (hexstr2bin(cmd, &version, 1) < 0)
5568 return -1;
5569
5570 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
5571}
5572
5573
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005574static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
5575{
5576 u32 adv_id;
5577
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07005578 if (os_strcmp(cmd, "all") == 0) {
5579 wpas_p2p_service_flush_asp(wpa_s);
5580 return 0;
5581 }
5582
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005583 if (sscanf(cmd, "%x", &adv_id) != 1)
5584 return -1;
5585
5586 return wpas_p2p_service_del_asp(wpa_s, adv_id);
5587}
5588
5589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005590static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
5591{
5592 char *pos;
5593
5594 pos = os_strchr(cmd, ' ');
5595 if (pos == NULL)
5596 return -1;
5597 *pos++ = '\0';
5598
5599 if (os_strcmp(cmd, "bonjour") == 0)
5600 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
5601 if (os_strcmp(cmd, "upnp") == 0)
5602 return p2p_ctrl_service_del_upnp(wpa_s, pos);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005603 if (os_strcmp(cmd, "asp") == 0)
5604 return p2p_ctrl_service_del_asp(wpa_s, pos);
5605 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
5606 return -1;
5607}
5608
5609
5610static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
5611{
5612 char *pos;
5613
5614 pos = os_strchr(cmd, ' ');
5615 if (pos == NULL)
5616 return -1;
5617 *pos++ = '\0';
5618
5619 if (os_strcmp(cmd, "asp") == 0)
5620 return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
5621
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005622 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
5623 return -1;
5624}
5625
5626
5627static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
5628{
5629 u8 addr[ETH_ALEN];
5630
5631 /* <addr> */
5632
5633 if (hwaddr_aton(cmd, addr))
5634 return -1;
5635
5636 return wpas_p2p_reject(wpa_s, addr);
5637}
5638
5639
5640static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
5641{
5642 char *pos;
5643 int id;
5644 struct wpa_ssid *ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005645 u8 *_peer = NULL, peer[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005646 int freq = 0, pref_freq = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005647 int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005648
5649 id = atoi(cmd);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005650 pos = os_strstr(cmd, " peer=");
5651 if (pos) {
5652 pos += 6;
5653 if (hwaddr_aton(pos, peer))
5654 return -1;
5655 _peer = peer;
5656 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005657 ssid = wpa_config_get_network(wpa_s->conf, id);
5658 if (ssid == NULL || ssid->disabled != 2) {
5659 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
5660 "for persistent P2P group",
5661 id);
5662 return -1;
5663 }
5664
Jouni Malinen31be0a42012-08-31 21:20:51 +03005665 pos = os_strstr(cmd, " freq=");
5666 if (pos) {
5667 pos += 6;
5668 freq = atoi(pos);
5669 if (freq <= 0)
5670 return -1;
5671 }
5672
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005673 pos = os_strstr(cmd, " pref=");
5674 if (pos) {
5675 pos += 6;
5676 pref_freq = atoi(pos);
5677 if (pref_freq <= 0)
5678 return -1;
5679 }
5680
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005681 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
5682 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
5683 vht;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005684
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005685 pos = os_strstr(cmd, "freq2=");
5686 if (pos)
5687 freq2 = atoi(pos + 6);
5688
5689 pos = os_strstr(cmd, " max_oper_chwidth=");
5690 if (pos)
5691 chwidth = atoi(pos + 18);
5692
5693 max_oper_chwidth = parse_freq(chwidth, freq2);
5694 if (max_oper_chwidth < 0)
5695 return -1;
5696
5697 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
5698 max_oper_chwidth, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005699}
5700
5701
5702static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
5703{
5704 char *pos;
5705 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
5706
5707 pos = os_strstr(cmd, " peer=");
5708 if (!pos)
5709 return -1;
5710
5711 *pos = '\0';
5712 pos += 6;
5713 if (hwaddr_aton(pos, peer)) {
5714 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
5715 return -1;
5716 }
5717
5718 pos = os_strstr(pos, " go_dev_addr=");
5719 if (pos) {
5720 pos += 13;
5721 if (hwaddr_aton(pos, go_dev_addr)) {
5722 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
5723 pos);
5724 return -1;
5725 }
5726 go_dev = go_dev_addr;
5727 }
5728
5729 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
5730}
5731
5732
5733static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
5734{
5735 if (os_strncmp(cmd, "persistent=", 11) == 0)
5736 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
5737 if (os_strncmp(cmd, "group=", 6) == 0)
5738 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
5739
5740 return -1;
5741}
5742
5743
5744static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005745 int id, int freq, int vht_center_freq2,
5746 int ht40, int vht, int vht_chwidth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005747{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005748 struct wpa_ssid *ssid;
5749
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005750 ssid = wpa_config_get_network(wpa_s->conf, id);
5751 if (ssid == NULL || ssid->disabled != 2) {
5752 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
5753 "for persistent P2P group",
5754 id);
5755 return -1;
5756 }
5757
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005758 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq,
5759 vht_center_freq2, 0, ht40, vht,
5760 vht_chwidth, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005761}
5762
5763
5764static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
5765{
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005766 int freq = 0, persistent = 0, group_id = -1;
5767 int vht = wpa_s->conf->p2p_go_vht;
5768 int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005769 int max_oper_chwidth, chwidth = 0, freq2 = 0;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005770 char *token, *context = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005771
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005772 while ((token = str_token(cmd, " ", &context))) {
5773 if (sscanf(token, "freq=%d", &freq) == 1 ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005774 sscanf(token, "freq2=%d", &freq2) == 1 ||
5775 sscanf(token, "persistent=%d", &group_id) == 1 ||
5776 sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005777 continue;
5778 } else if (os_strcmp(token, "ht40") == 0) {
5779 ht40 = 1;
5780 } else if (os_strcmp(token, "vht") == 0) {
5781 vht = 1;
5782 ht40 = 1;
5783 } else if (os_strcmp(token, "persistent") == 0) {
5784 persistent = 1;
5785 } else {
5786 wpa_printf(MSG_DEBUG,
5787 "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
5788 token);
5789 return -1;
5790 }
5791 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005792
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005793 max_oper_chwidth = parse_freq(chwidth, freq2);
5794 if (max_oper_chwidth < 0)
5795 return -1;
5796
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005797 if (group_id >= 0)
5798 return p2p_ctrl_group_add_persistent(wpa_s, group_id,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005799 freq, freq2, ht40, vht,
5800 max_oper_chwidth);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005801
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005802 return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
5803 max_oper_chwidth);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005804}
5805
5806
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005807static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
5808 char *buf, size_t buflen)
5809{
5810 u8 dev_addr[ETH_ALEN];
5811 struct wpa_ssid *ssid;
5812 int res;
5813 const u8 *iaddr;
5814
5815 ssid = wpa_s->current_ssid;
5816 if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
5817 hwaddr_aton(cmd, dev_addr))
5818 return -1;
5819
5820 iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
5821 if (!iaddr)
5822 return -1;
5823 res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
5824 if (os_snprintf_error(buflen, res))
5825 return -1;
5826 return res;
5827}
5828
5829
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005830static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
5831 char *buf, size_t buflen)
5832{
5833 u8 addr[ETH_ALEN], *addr_ptr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005834 int next, res;
5835 const struct p2p_peer_info *info;
5836 char *pos, *end;
5837 char devtype[WPS_DEV_TYPE_BUFSIZE];
5838 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005839 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005840
5841 if (!wpa_s->global->p2p)
5842 return -1;
5843
5844 if (os_strcmp(cmd, "FIRST") == 0) {
5845 addr_ptr = NULL;
5846 next = 0;
5847 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
5848 if (hwaddr_aton(cmd + 5, addr) < 0)
5849 return -1;
5850 addr_ptr = addr;
5851 next = 1;
5852 } else {
5853 if (hwaddr_aton(cmd, addr) < 0)
5854 return -1;
5855 addr_ptr = addr;
5856 next = 0;
5857 }
5858
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005859 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
5860 if (info == NULL)
5861 return -1;
5862
5863 pos = buf;
5864 end = buf + buflen;
5865
5866 res = os_snprintf(pos, end - pos, MACSTR "\n"
5867 "pri_dev_type=%s\n"
5868 "device_name=%s\n"
5869 "manufacturer=%s\n"
5870 "model_name=%s\n"
5871 "model_number=%s\n"
5872 "serial_number=%s\n"
5873 "config_methods=0x%x\n"
5874 "dev_capab=0x%x\n"
5875 "group_capab=0x%x\n"
5876 "level=%d\n",
5877 MAC2STR(info->p2p_device_addr),
5878 wps_dev_type_bin2str(info->pri_dev_type,
5879 devtype, sizeof(devtype)),
5880 info->device_name,
5881 info->manufacturer,
5882 info->model_name,
5883 info->model_number,
5884 info->serial_number,
5885 info->config_methods,
5886 info->dev_capab,
5887 info->group_capab,
5888 info->level);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005889 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005890 return pos - buf;
5891 pos += res;
5892
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005893 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
5894 {
5895 const u8 *t;
5896 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
5897 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
5898 wps_dev_type_bin2str(t, devtype,
5899 sizeof(devtype)));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005900 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005901 return pos - buf;
5902 pos += res;
5903 }
5904
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005905 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005906 if (ssid) {
5907 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005908 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005909 return pos - buf;
5910 pos += res;
5911 }
5912
5913 res = p2p_get_peer_info_txt(info, pos, end - pos);
5914 if (res < 0)
5915 return pos - buf;
5916 pos += res;
5917
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005918 if (info->vendor_elems) {
5919 res = os_snprintf(pos, end - pos, "vendor_elems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005920 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005921 return pos - buf;
5922 pos += res;
5923
5924 pos += wpa_snprintf_hex(pos, end - pos,
5925 wpabuf_head(info->vendor_elems),
5926 wpabuf_len(info->vendor_elems));
5927
5928 res = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005929 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005930 return pos - buf;
5931 pos += res;
5932 }
5933
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005934 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005935}
5936
5937
Dmitry Shmidt04949592012-07-19 12:16:46 -07005938static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
5939 const char *param)
5940{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005941 unsigned int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005942
5943 if (wpa_s->global->p2p == NULL)
5944 return -1;
5945
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005946 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
5947 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005948
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005949 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
5950 struct wpa_freq_range *freq;
5951 freq = &wpa_s->global->p2p_disallow_freq.range[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005952 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07005953 freq->min, freq->max);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005954 }
5955
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005956 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005957 return 0;
5958}
5959
5960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005961static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
5962{
5963 char *param;
5964
5965 if (wpa_s->global->p2p == NULL)
5966 return -1;
5967
5968 param = os_strchr(cmd, ' ');
5969 if (param == NULL)
5970 return -1;
5971 *param++ = '\0';
5972
5973 if (os_strcmp(cmd, "discoverability") == 0) {
5974 p2p_set_client_discoverability(wpa_s->global->p2p,
5975 atoi(param));
5976 return 0;
5977 }
5978
5979 if (os_strcmp(cmd, "managed") == 0) {
5980 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
5981 return 0;
5982 }
5983
5984 if (os_strcmp(cmd, "listen_channel") == 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005985 char *pos;
5986 u8 channel, op_class;
5987
5988 channel = atoi(param);
5989 pos = os_strchr(param, ' ');
5990 op_class = pos ? atoi(pos) : 81;
5991
5992 return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
5993 channel, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005994 }
5995
5996 if (os_strcmp(cmd, "ssid_postfix") == 0) {
5997 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
5998 os_strlen(param));
5999 }
6000
6001 if (os_strcmp(cmd, "noa") == 0) {
6002 char *pos;
6003 int count, start, duration;
6004 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
6005 count = atoi(param);
6006 pos = os_strchr(param, ',');
6007 if (pos == NULL)
6008 return -1;
6009 pos++;
6010 start = atoi(pos);
6011 pos = os_strchr(pos, ',');
6012 if (pos == NULL)
6013 return -1;
6014 pos++;
6015 duration = atoi(pos);
6016 if (count < 0 || count > 255 || start < 0 || duration < 0)
6017 return -1;
6018 if (count == 0 && duration > 0)
6019 return -1;
6020 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
6021 "start=%d duration=%d", count, start, duration);
6022 return wpas_p2p_set_noa(wpa_s, count, start, duration);
6023 }
6024
6025 if (os_strcmp(cmd, "ps") == 0)
6026 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
6027
6028 if (os_strcmp(cmd, "oppps") == 0)
6029 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
6030
6031 if (os_strcmp(cmd, "ctwindow") == 0)
6032 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
6033
6034 if (os_strcmp(cmd, "disabled") == 0) {
6035 wpa_s->global->p2p_disabled = atoi(param);
6036 wpa_printf(MSG_DEBUG, "P2P functionality %s",
6037 wpa_s->global->p2p_disabled ?
6038 "disabled" : "enabled");
6039 if (wpa_s->global->p2p_disabled) {
6040 wpas_p2p_stop_find(wpa_s);
6041 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6042 p2p_flush(wpa_s->global->p2p);
6043 }
6044 return 0;
6045 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006046
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07006047 if (os_strcmp(cmd, "conc_pref") == 0) {
6048 if (os_strcmp(param, "sta") == 0)
6049 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
6050 else if (os_strcmp(param, "p2p") == 0)
6051 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07006052 else {
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07006053 wpa_printf(MSG_INFO, "Invalid conc_pref value");
Dmitry Shmidt687922c2012-03-26 14:02:32 -07006054 return -1;
6055 }
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07006056 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
Dmitry Shmidt04949592012-07-19 12:16:46 -07006057 "%s", param);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07006058 return 0;
6059 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006061 if (os_strcmp(cmd, "force_long_sd") == 0) {
6062 wpa_s->force_long_sd = atoi(param);
6063 return 0;
6064 }
6065
6066 if (os_strcmp(cmd, "peer_filter") == 0) {
6067 u8 addr[ETH_ALEN];
6068 if (hwaddr_aton(param, addr))
6069 return -1;
6070 p2p_set_peer_filter(wpa_s->global->p2p, addr);
6071 return 0;
6072 }
6073
6074 if (os_strcmp(cmd, "cross_connect") == 0)
6075 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
6076
6077 if (os_strcmp(cmd, "go_apsd") == 0) {
6078 if (os_strcmp(param, "disable") == 0)
6079 wpa_s->set_ap_uapsd = 0;
6080 else {
6081 wpa_s->set_ap_uapsd = 1;
6082 wpa_s->ap_uapsd = atoi(param);
6083 }
6084 return 0;
6085 }
6086
6087 if (os_strcmp(cmd, "client_apsd") == 0) {
6088 if (os_strcmp(param, "disable") == 0)
6089 wpa_s->set_sta_uapsd = 0;
6090 else {
6091 int be, bk, vi, vo;
6092 char *pos;
6093 /* format: BE,BK,VI,VO;max SP Length */
6094 be = atoi(param);
6095 pos = os_strchr(param, ',');
6096 if (pos == NULL)
6097 return -1;
6098 pos++;
6099 bk = atoi(pos);
6100 pos = os_strchr(pos, ',');
6101 if (pos == NULL)
6102 return -1;
6103 pos++;
6104 vi = atoi(pos);
6105 pos = os_strchr(pos, ',');
6106 if (pos == NULL)
6107 return -1;
6108 pos++;
6109 vo = atoi(pos);
6110 /* ignore max SP Length for now */
6111
6112 wpa_s->set_sta_uapsd = 1;
6113 wpa_s->sta_uapsd = 0;
6114 if (be)
6115 wpa_s->sta_uapsd |= BIT(0);
6116 if (bk)
6117 wpa_s->sta_uapsd |= BIT(1);
6118 if (vi)
6119 wpa_s->sta_uapsd |= BIT(2);
6120 if (vo)
6121 wpa_s->sta_uapsd |= BIT(3);
6122 }
6123 return 0;
6124 }
6125
Dmitry Shmidt04949592012-07-19 12:16:46 -07006126 if (os_strcmp(cmd, "disallow_freq") == 0)
6127 return p2p_ctrl_disallow_freq(wpa_s, param);
6128
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006129 if (os_strcmp(cmd, "disc_int") == 0) {
6130 int min_disc_int, max_disc_int, max_disc_tu;
6131 char *pos;
6132
6133 pos = param;
6134
6135 min_disc_int = atoi(pos);
6136 pos = os_strchr(pos, ' ');
6137 if (pos == NULL)
6138 return -1;
6139 *pos++ = '\0';
6140
6141 max_disc_int = atoi(pos);
6142 pos = os_strchr(pos, ' ');
6143 if (pos == NULL)
6144 return -1;
6145 *pos++ = '\0';
6146
6147 max_disc_tu = atoi(pos);
6148
6149 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
6150 max_disc_int, max_disc_tu);
6151 }
6152
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006153 if (os_strcmp(cmd, "per_sta_psk") == 0) {
6154 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
6155 return 0;
6156 }
6157
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006158#ifdef CONFIG_WPS_NFC
6159 if (os_strcmp(cmd, "nfc_tag") == 0)
6160 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
6161#endif /* CONFIG_WPS_NFC */
6162
6163 if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
6164 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
6165 return 0;
6166 }
6167
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006168 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
6169 cmd);
6170
6171 return -1;
6172}
6173
6174
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006175static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
6176{
6177 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6178 wpa_s->force_long_sd = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006179 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006180 wpa_s->parent->p2ps_method_config_any = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006181 if (wpa_s->global->p2p)
6182 p2p_flush(wpa_s->global->p2p);
6183}
6184
6185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006186static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
6187{
6188 char *pos, *pos2;
6189 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
6190
6191 if (cmd[0]) {
6192 pos = os_strchr(cmd, ' ');
6193 if (pos == NULL)
6194 return -1;
6195 *pos++ = '\0';
6196 dur1 = atoi(cmd);
6197
6198 pos2 = os_strchr(pos, ' ');
6199 if (pos2)
6200 *pos2++ = '\0';
6201 int1 = atoi(pos);
6202 } else
6203 pos2 = NULL;
6204
6205 if (pos2) {
6206 pos = os_strchr(pos2, ' ');
6207 if (pos == NULL)
6208 return -1;
6209 *pos++ = '\0';
6210 dur2 = atoi(pos2);
6211 int2 = atoi(pos);
6212 }
6213
6214 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
6215}
6216
6217
6218static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
6219{
6220 char *pos;
6221 unsigned int period = 0, interval = 0;
6222
6223 if (cmd[0]) {
6224 pos = os_strchr(cmd, ' ');
6225 if (pos == NULL)
6226 return -1;
6227 *pos++ = '\0';
6228 period = atoi(cmd);
6229 interval = atoi(pos);
6230 }
6231
6232 return wpas_p2p_ext_listen(wpa_s, period, interval);
6233}
6234
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006235
6236static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
6237{
6238 const char *pos;
6239 u8 peer[ETH_ALEN];
6240 int iface_addr = 0;
6241
6242 pos = cmd;
6243 if (os_strncmp(pos, "iface=", 6) == 0) {
6244 iface_addr = 1;
6245 pos += 6;
6246 }
6247 if (hwaddr_aton(pos, peer))
6248 return -1;
6249
6250 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
6251 return 0;
6252}
6253
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006254
6255static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
6256{
6257 int freq = 0, period = 0, interval = 0, count = 0;
6258
6259 if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
6260 {
6261 wpa_printf(MSG_DEBUG,
6262 "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
6263 return -1;
6264 }
6265
6266 return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
6267}
6268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006269#endif /* CONFIG_P2P */
6270
6271
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006272static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
6273{
6274 struct wpa_freq_range_list ranges;
6275 int *freqs = NULL;
6276 struct hostapd_hw_modes *mode;
6277 u16 i;
6278
6279 if (wpa_s->hw.modes == NULL)
6280 return NULL;
6281
6282 os_memset(&ranges, 0, sizeof(ranges));
6283 if (freq_range_list_parse(&ranges, val) < 0)
6284 return NULL;
6285
6286 for (i = 0; i < wpa_s->hw.num_modes; i++) {
6287 int j;
6288
6289 mode = &wpa_s->hw.modes[i];
6290 for (j = 0; j < mode->num_channels; j++) {
6291 unsigned int freq;
6292
6293 if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
6294 continue;
6295
6296 freq = mode->channels[j].freq;
6297 if (!freq_range_list_includes(&ranges, freq))
6298 continue;
6299
6300 int_array_add_unique(&freqs, freq);
6301 }
6302 }
6303
6304 os_free(ranges.range);
6305 return freqs;
6306}
6307
6308
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006309#ifdef CONFIG_INTERWORKING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006310
6311static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
6312{
6313 int auto_sel = 0;
6314 int *freqs = NULL;
6315
6316 if (param) {
6317 char *pos;
6318
6319 auto_sel = os_strstr(param, "auto") != NULL;
6320
6321 pos = os_strstr(param, "freq=");
6322 if (pos) {
6323 freqs = freq_range_to_channel_list(wpa_s, pos + 5);
6324 if (freqs == NULL)
6325 return -1;
6326 }
6327
6328 }
6329
6330 return interworking_select(wpa_s, auto_sel, freqs);
6331}
6332
6333
Dmitry Shmidt7f656022015-02-25 14:36:37 -08006334static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
6335 int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006336{
6337 u8 bssid[ETH_ALEN];
6338 struct wpa_bss *bss;
6339
6340 if (hwaddr_aton(dst, bssid)) {
6341 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
6342 return -1;
6343 }
6344
6345 bss = wpa_bss_get_bssid(wpa_s, bssid);
6346 if (bss == NULL) {
6347 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
6348 MAC2STR(bssid));
6349 return -1;
6350 }
6351
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006352 if (bss->ssid_len == 0) {
6353 int found = 0;
6354
6355 wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
6356 " does not have SSID information", MAC2STR(bssid));
6357
6358 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
6359 list) {
6360 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
6361 bss->ssid_len > 0) {
6362 found = 1;
6363 break;
6364 }
6365 }
6366
6367 if (!found)
6368 return -1;
6369 wpa_printf(MSG_DEBUG,
6370 "Found another matching BSS entry with SSID");
6371 }
6372
Dmitry Shmidt7f656022015-02-25 14:36:37 -08006373 return interworking_connect(wpa_s, bss, only_add);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006374}
6375
6376
6377static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
6378{
6379 u8 dst_addr[ETH_ALEN];
6380 int used;
6381 char *pos;
6382#define MAX_ANQP_INFO_ID 100
6383 u16 id[MAX_ANQP_INFO_ID];
6384 size_t num_id = 0;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006385 u32 subtypes = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006386
6387 used = hwaddr_aton2(dst, dst_addr);
6388 if (used < 0)
6389 return -1;
6390 pos = dst + used;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006391 if (*pos == ' ')
6392 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006393 while (num_id < MAX_ANQP_INFO_ID) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006394 if (os_strncmp(pos, "hs20:", 5) == 0) {
6395#ifdef CONFIG_HS20
6396 int num = atoi(pos + 5);
6397 if (num <= 0 || num > 31)
6398 return -1;
6399 subtypes |= BIT(num);
6400#else /* CONFIG_HS20 */
6401 return -1;
6402#endif /* CONFIG_HS20 */
6403 } else {
6404 id[num_id] = atoi(pos);
6405 if (id[num_id])
6406 num_id++;
6407 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006408 pos = os_strchr(pos + 1, ',');
6409 if (pos == NULL)
6410 break;
6411 pos++;
6412 }
6413
6414 if (num_id == 0)
6415 return -1;
6416
Dmitry Shmidt15907092014-03-25 10:42:57 -07006417 return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006418}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006419
6420
6421static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
6422{
6423 u8 dst_addr[ETH_ALEN];
6424 struct wpabuf *advproto, *query = NULL;
6425 int used, ret = -1;
6426 char *pos, *end;
6427 size_t len;
6428
6429 used = hwaddr_aton2(cmd, dst_addr);
6430 if (used < 0)
6431 return -1;
6432
6433 pos = cmd + used;
6434 while (*pos == ' ')
6435 pos++;
6436
6437 /* Advertisement Protocol ID */
6438 end = os_strchr(pos, ' ');
6439 if (end)
6440 len = end - pos;
6441 else
6442 len = os_strlen(pos);
6443 if (len & 0x01)
6444 return -1;
6445 len /= 2;
6446 if (len == 0)
6447 return -1;
6448 advproto = wpabuf_alloc(len);
6449 if (advproto == NULL)
6450 return -1;
6451 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
6452 goto fail;
6453
6454 if (end) {
6455 /* Optional Query Request */
6456 pos = end + 1;
6457 while (*pos == ' ')
6458 pos++;
6459
6460 len = os_strlen(pos);
6461 if (len) {
6462 if (len & 0x01)
6463 goto fail;
6464 len /= 2;
6465 if (len == 0)
6466 goto fail;
6467 query = wpabuf_alloc(len);
6468 if (query == NULL)
6469 goto fail;
6470 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
6471 goto fail;
6472 }
6473 }
6474
6475 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
6476
6477fail:
6478 wpabuf_free(advproto);
6479 wpabuf_free(query);
6480
6481 return ret;
6482}
6483
6484
6485static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
6486 size_t buflen)
6487{
6488 u8 addr[ETH_ALEN];
6489 int dialog_token;
6490 int used;
6491 char *pos;
6492 size_t resp_len, start, requested_len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006493 struct wpabuf *resp;
6494 int ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006495
6496 used = hwaddr_aton2(cmd, addr);
6497 if (used < 0)
6498 return -1;
6499
6500 pos = cmd + used;
6501 while (*pos == ' ')
6502 pos++;
6503 dialog_token = atoi(pos);
6504
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006505 if (wpa_s->last_gas_resp &&
6506 os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
6507 dialog_token == wpa_s->last_gas_dialog_token)
6508 resp = wpa_s->last_gas_resp;
6509 else if (wpa_s->prev_gas_resp &&
6510 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
6511 dialog_token == wpa_s->prev_gas_dialog_token)
6512 resp = wpa_s->prev_gas_resp;
6513 else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006514 return -1;
6515
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006516 resp_len = wpabuf_len(resp);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006517 start = 0;
6518 requested_len = resp_len;
6519
6520 pos = os_strchr(pos, ' ');
6521 if (pos) {
6522 start = atoi(pos);
6523 if (start > resp_len)
6524 return os_snprintf(buf, buflen, "FAIL-Invalid range");
6525 pos = os_strchr(pos, ',');
6526 if (pos == NULL)
6527 return -1;
6528 pos++;
6529 requested_len = atoi(pos);
6530 if (start + requested_len > resp_len)
6531 return os_snprintf(buf, buflen, "FAIL-Invalid range");
6532 }
6533
6534 if (requested_len * 2 + 1 > buflen)
6535 return os_snprintf(buf, buflen, "FAIL-Too long response");
6536
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006537 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
6538 requested_len);
6539
6540 if (start + requested_len == resp_len) {
6541 /*
6542 * Free memory by dropping the response after it has been
6543 * fetched.
6544 */
6545 if (resp == wpa_s->prev_gas_resp) {
6546 wpabuf_free(wpa_s->prev_gas_resp);
6547 wpa_s->prev_gas_resp = NULL;
6548 } else {
6549 wpabuf_free(wpa_s->last_gas_resp);
6550 wpa_s->last_gas_resp = NULL;
6551 }
6552 }
6553
6554 return ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006555}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006556#endif /* CONFIG_INTERWORKING */
6557
6558
Dmitry Shmidt04949592012-07-19 12:16:46 -07006559#ifdef CONFIG_HS20
6560
6561static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
6562{
6563 u8 dst_addr[ETH_ALEN];
6564 int used;
6565 char *pos;
6566 u32 subtypes = 0;
6567
6568 used = hwaddr_aton2(dst, dst_addr);
6569 if (used < 0)
6570 return -1;
6571 pos = dst + used;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006572 if (*pos == ' ')
6573 pos++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006574 for (;;) {
6575 int num = atoi(pos);
6576 if (num <= 0 || num > 31)
6577 return -1;
6578 subtypes |= BIT(num);
6579 pos = os_strchr(pos + 1, ',');
6580 if (pos == NULL)
6581 break;
6582 pos++;
6583 }
6584
6585 if (subtypes == 0)
6586 return -1;
6587
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08006588 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006589}
6590
6591
6592static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
6593 const u8 *addr, const char *realm)
6594{
6595 u8 *buf;
6596 size_t rlen, len;
6597 int ret;
6598
6599 rlen = os_strlen(realm);
6600 len = 3 + rlen;
6601 buf = os_malloc(len);
6602 if (buf == NULL)
6603 return -1;
6604 buf[0] = 1; /* NAI Home Realm Count */
6605 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
6606 buf[2] = rlen;
6607 os_memcpy(buf + 3, realm, rlen);
6608
6609 ret = hs20_anqp_send_req(wpa_s, addr,
6610 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08006611 buf, len, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006612
6613 os_free(buf);
6614
6615 return ret;
6616}
6617
6618
6619static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
6620 char *dst)
6621{
6622 struct wpa_cred *cred = wpa_s->conf->cred;
6623 u8 dst_addr[ETH_ALEN];
6624 int used;
6625 u8 *buf;
6626 size_t len;
6627 int ret;
6628
6629 used = hwaddr_aton2(dst, dst_addr);
6630 if (used < 0)
6631 return -1;
6632
6633 while (dst[used] == ' ')
6634 used++;
6635 if (os_strncmp(dst + used, "realm=", 6) == 0)
6636 return hs20_nai_home_realm_list(wpa_s, dst_addr,
6637 dst + used + 6);
6638
6639 len = os_strlen(dst + used);
6640
6641 if (len == 0 && cred && cred->realm)
6642 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
6643
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07006644 if (len & 1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07006645 return -1;
6646 len /= 2;
6647 buf = os_malloc(len);
6648 if (buf == NULL)
6649 return -1;
6650 if (hexstr2bin(dst + used, buf, len) < 0) {
6651 os_free(buf);
6652 return -1;
6653 }
6654
6655 ret = hs20_anqp_send_req(wpa_s, dst_addr,
6656 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08006657 buf, len, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006658 os_free(buf);
6659
6660 return ret;
6661}
6662
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006663
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08006664static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
6665 int buflen)
6666{
6667 u8 dst_addr[ETH_ALEN];
6668 int used;
6669 char *ctx = NULL, *icon, *poffset, *psize;
6670
6671 used = hwaddr_aton2(cmd, dst_addr);
6672 if (used < 0)
6673 return -1;
6674 cmd += used;
6675
6676 icon = str_token(cmd, " ", &ctx);
6677 poffset = str_token(cmd, " ", &ctx);
6678 psize = str_token(cmd, " ", &ctx);
6679 if (!icon || !poffset || !psize)
6680 return -1;
6681
6682 wpa_s->fetch_osu_icon_in_progress = 0;
6683 return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
6684 reply, buflen);
6685}
6686
6687
6688static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
6689{
6690 u8 dst_addr[ETH_ALEN];
6691 int used;
6692 char *icon;
6693
6694 if (!cmd[0])
6695 return hs20_del_icon(wpa_s, NULL, NULL);
6696
6697 used = hwaddr_aton2(cmd, dst_addr);
6698 if (used < 0)
6699 return -1;
6700
6701 while (cmd[used] == ' ')
6702 used++;
6703 icon = cmd[used] ? &cmd[used] : NULL;
6704
6705 return hs20_del_icon(wpa_s, dst_addr, icon);
6706}
6707
6708
6709static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006710{
6711 u8 dst_addr[ETH_ALEN];
6712 int used;
6713 char *icon;
6714
6715 used = hwaddr_aton2(cmd, dst_addr);
6716 if (used < 0)
6717 return -1;
6718
6719 while (cmd[used] == ' ')
6720 used++;
6721 icon = &cmd[used];
6722
6723 wpa_s->fetch_osu_icon_in_progress = 0;
6724 return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08006725 (u8 *) icon, os_strlen(icon), inmem);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006726}
6727
Dmitry Shmidt04949592012-07-19 12:16:46 -07006728#endif /* CONFIG_HS20 */
6729
6730
Dmitry Shmidt04949592012-07-19 12:16:46 -07006731#ifdef CONFIG_AUTOSCAN
6732
6733static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
6734 char *cmd)
6735{
6736 enum wpa_states state = wpa_s->wpa_state;
6737 char *new_params = NULL;
6738
6739 if (os_strlen(cmd) > 0) {
6740 new_params = os_strdup(cmd);
6741 if (new_params == NULL)
6742 return -1;
6743 }
6744
6745 os_free(wpa_s->conf->autoscan);
6746 wpa_s->conf->autoscan = new_params;
6747
6748 if (wpa_s->conf->autoscan == NULL)
6749 autoscan_deinit(wpa_s);
6750 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
6751 autoscan_init(wpa_s, 1);
6752 else if (state == WPA_SCANNING)
6753 wpa_supplicant_reinit_autoscan(wpa_s);
6754
6755 return 0;
6756}
6757
6758#endif /* CONFIG_AUTOSCAN */
6759
6760
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006761#ifdef CONFIG_WNM
6762
6763static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
6764{
6765 int enter;
6766 int intval = 0;
6767 char *pos;
6768 int ret;
6769 struct wpabuf *tfs_req = NULL;
6770
6771 if (os_strncmp(cmd, "enter", 5) == 0)
6772 enter = 1;
6773 else if (os_strncmp(cmd, "exit", 4) == 0)
6774 enter = 0;
6775 else
6776 return -1;
6777
6778 pos = os_strstr(cmd, " interval=");
6779 if (pos)
6780 intval = atoi(pos + 10);
6781
6782 pos = os_strstr(cmd, " tfs_req=");
6783 if (pos) {
6784 char *end;
6785 size_t len;
6786 pos += 9;
6787 end = os_strchr(pos, ' ');
6788 if (end)
6789 len = end - pos;
6790 else
6791 len = os_strlen(pos);
6792 if (len & 1)
6793 return -1;
6794 len /= 2;
6795 tfs_req = wpabuf_alloc(len);
6796 if (tfs_req == NULL)
6797 return -1;
6798 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
6799 wpabuf_free(tfs_req);
6800 return -1;
6801 }
6802 }
6803
6804 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
6805 WNM_SLEEP_MODE_EXIT, intval,
6806 tfs_req);
6807 wpabuf_free(tfs_req);
6808
6809 return ret;
6810}
6811
Dmitry Shmidt44c95782013-05-17 09:51:35 -07006812
6813static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
6814{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006815 int query_reason, list = 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07006816
6817 query_reason = atoi(cmd);
6818
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006819 cmd = os_strchr(cmd, ' ');
6820 if (cmd) {
6821 cmd++;
6822 if (os_strncmp(cmd, "list", 4) == 0) {
6823 list = 1;
6824 } else {
6825 wpa_printf(MSG_DEBUG, "WNM Query: Invalid option %s",
6826 cmd);
6827 return -1;
6828 }
6829 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07006830
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006831 wpa_printf(MSG_DEBUG,
6832 "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
6833 query_reason, list ? " candidate list" : "");
6834
6835 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason, list);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07006836}
6837
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006838#endif /* CONFIG_WNM */
6839
6840
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006841static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
6842 size_t buflen)
6843{
6844 struct wpa_signal_info si;
6845 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006846 char *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006847
6848 ret = wpa_drv_signal_poll(wpa_s, &si);
6849 if (ret)
6850 return -1;
6851
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006852 pos = buf;
6853 end = buf + buflen;
6854
6855 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006856 "NOISE=%d\nFREQUENCY=%u\n",
6857 si.current_signal, si.current_txrate / 1000,
6858 si.current_noise, si.frequency);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006859 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006860 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006861 pos += ret;
6862
6863 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
6864 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006865 channel_width_to_string(si.chanwidth));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006866 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006867 return -1;
6868 pos += ret;
6869 }
6870
6871 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
6872 ret = os_snprintf(pos, end - pos,
6873 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
6874 si.center_frq1, si.center_frq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006875 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006876 return -1;
6877 pos += ret;
6878 }
6879
6880 if (si.avg_signal) {
6881 ret = os_snprintf(pos, end - pos,
6882 "AVG_RSSI=%d\n", si.avg_signal);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006883 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006884 return -1;
6885 pos += ret;
6886 }
6887
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07006888 if (si.avg_beacon_signal) {
6889 ret = os_snprintf(pos, end - pos,
6890 "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
6891 if (os_snprintf_error(end - pos, ret))
6892 return -1;
6893 pos += ret;
6894 }
6895
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006896 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006897}
6898
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006899
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08006900static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
6901 const char *cmd)
6902{
6903 const char *pos;
6904 int threshold = 0;
6905 int hysteresis = 0;
6906
6907 if (wpa_s->bgscan && wpa_s->bgscan_priv) {
6908 wpa_printf(MSG_DEBUG,
6909 "Reject SIGNAL_MONITOR command - bgscan is active");
6910 return -1;
6911 }
6912 pos = os_strstr(cmd, "THRESHOLD=");
6913 if (pos)
6914 threshold = atoi(pos + 10);
6915 pos = os_strstr(cmd, "HYSTERESIS=");
6916 if (pos)
6917 hysteresis = atoi(pos + 11);
6918 return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
6919}
6920
6921
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006922static int wpas_ctrl_iface_get_pref_freq_list(
6923 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
6924{
6925 unsigned int freq_list[100], num = 100, i;
6926 int ret;
6927 enum wpa_driver_if_type iface_type;
6928 char *pos, *end;
6929
6930 pos = buf;
6931 end = buf + buflen;
6932
6933 /* buf: "<interface_type>" */
6934 if (os_strcmp(cmd, "STATION") == 0)
6935 iface_type = WPA_IF_STATION;
6936 else if (os_strcmp(cmd, "AP") == 0)
6937 iface_type = WPA_IF_AP_BSS;
6938 else if (os_strcmp(cmd, "P2P_GO") == 0)
6939 iface_type = WPA_IF_P2P_GO;
6940 else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
6941 iface_type = WPA_IF_P2P_CLIENT;
6942 else if (os_strcmp(cmd, "IBSS") == 0)
6943 iface_type = WPA_IF_IBSS;
6944 else if (os_strcmp(cmd, "TDLS") == 0)
6945 iface_type = WPA_IF_TDLS;
6946 else
6947 return -1;
6948
6949 wpa_printf(MSG_DEBUG,
6950 "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
6951 iface_type, buf);
6952
6953 ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
6954 if (ret)
6955 return -1;
6956
6957 for (i = 0; i < num; i++) {
6958 ret = os_snprintf(pos, end - pos, "%s%u",
6959 i > 0 ? "," : "", freq_list[i]);
6960 if (os_snprintf_error(end - pos, ret))
6961 return -1;
6962 pos += ret;
6963 }
6964
6965 return pos - buf;
6966}
6967
6968
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006969static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
6970 char *buf, size_t buflen)
6971{
6972 int ret, i;
6973 char *pos, *end;
6974
6975 ret = os_snprintf(buf, buflen, "%016llX:\n",
6976 (long long unsigned) wpa_s->drv_flags);
6977 if (os_snprintf_error(buflen, ret))
6978 return -1;
6979
6980 pos = buf + ret;
6981 end = buf + buflen;
6982
6983 for (i = 0; i < 64; i++) {
6984 if (wpa_s->drv_flags & (1LLU << i)) {
6985 ret = os_snprintf(pos, end - pos, "%s\n",
6986 driver_flag_to_string(1LLU << i));
6987 if (os_snprintf_error(end - pos, ret))
6988 return -1;
6989 pos += ret;
6990 }
6991 }
6992
6993 return pos - buf;
6994}
6995
6996
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07006997static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
6998 size_t buflen)
6999{
7000 struct hostap_sta_driver_data sta;
7001 int ret;
7002
7003 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
7004 if (ret)
7005 return -1;
7006
7007 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007008 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007009 if (os_snprintf_error(buflen, ret))
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07007010 return -1;
7011 return ret;
7012}
7013
7014
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007015#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07007016static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
7017 char *buf, size_t buflen)
7018{
7019 int ret;
7020
7021 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
Dmitry Shmidt9432e122013-09-12 12:39:30 -07007022 if (ret == 0) {
7023 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
7024 struct p2p_data *p2p = wpa_s->global->p2p;
7025 if (p2p) {
7026 char country[3];
7027 country[0] = cmd[8];
7028 country[1] = cmd[9];
7029 country[2] = 0x04;
7030 p2p_set_country(p2p, country);
7031 }
7032 }
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007033 ret = os_snprintf(buf, buflen, "%s\n", "OK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007034 if (os_snprintf_error(buflen, ret))
7035 ret = -1;
Dmitry Shmidt9432e122013-09-12 12:39:30 -07007036 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07007037 return ret;
7038}
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007039#endif /* ANDROID */
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07007040
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07007041
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007042static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
7043 char *buf, size_t buflen)
7044{
7045 int ret;
7046 char *pos;
7047 u8 *data = NULL;
7048 unsigned int vendor_id, subcmd;
7049 struct wpabuf *reply;
7050 size_t data_len = 0;
7051
7052 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
7053 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007054 if (!isblank((unsigned char) *pos))
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007055 return -EINVAL;
7056
7057 subcmd = strtoul(pos, &pos, 10);
7058
7059 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007060 if (!isblank((unsigned char) *pos++))
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007061 return -EINVAL;
7062 data_len = os_strlen(pos);
7063 }
7064
7065 if (data_len) {
7066 data_len /= 2;
7067 data = os_malloc(data_len);
7068 if (!data)
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007069 return -1;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007070
7071 if (hexstr2bin(pos, data, data_len)) {
7072 wpa_printf(MSG_DEBUG,
7073 "Vendor command: wrong parameter format");
7074 os_free(data);
7075 return -EINVAL;
7076 }
7077 }
7078
7079 reply = wpabuf_alloc((buflen - 1) / 2);
7080 if (!reply) {
7081 os_free(data);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007082 return -1;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007083 }
7084
7085 ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
7086 reply);
7087
7088 if (ret == 0)
7089 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
7090 wpabuf_len(reply));
7091
7092 wpabuf_free(reply);
7093 os_free(data);
7094
7095 return ret;
7096}
7097
7098
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007099static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
7100{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007101#ifdef CONFIG_P2P
7102 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
7103 wpa_s->global->p2p_init_wpa_s : wpa_s;
7104#endif /* CONFIG_P2P */
7105
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007106 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
7107
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007108 wpas_abort_ongoing_scan(wpa_s);
7109
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007110 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
7111 /*
7112 * Avoid possible auto connect re-connection on getting
7113 * disconnected due to state flush.
7114 */
7115 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
7116 }
7117
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007118#ifdef CONFIG_P2P
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007119 wpas_p2p_group_remove(p2p_wpa_s, "*");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007120 wpas_p2p_cancel(p2p_wpa_s);
7121 p2p_ctrl_flush(p2p_wpa_s);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007122 wpas_p2p_service_flush(p2p_wpa_s);
7123 p2p_wpa_s->global->p2p_disabled = 0;
7124 p2p_wpa_s->global->p2p_per_sta_psk = 0;
7125 p2p_wpa_s->conf->num_sec_device_types = 0;
7126 p2p_wpa_s->p2p_disable_ip_addr_req = 0;
7127 os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
7128 p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007129 p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007130 p2p_wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007131 p2p_wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007132#endif /* CONFIG_P2P */
7133
7134#ifdef CONFIG_WPS_TESTING
7135 wps_version_number = 0x20;
7136 wps_testing_dummy_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007137 wps_corrupt_pkhash = 0;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007138 wps_force_auth_types_in_use = 0;
7139 wps_force_encr_types_in_use = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007140#endif /* CONFIG_WPS_TESTING */
7141#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007142 wpa_s->wps_fragment_size = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007143 wpas_wps_cancel(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007144 wps_registrar_flush(wpa_s->wps->registrar);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007145#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07007146 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007147 wpa_s->known_wps_freq = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007148
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007149#ifdef CONFIG_TDLS
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007150#ifdef CONFIG_TDLS_TESTING
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007151 tdls_testing = 0;
7152#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007153 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
7154 wpa_tdls_enable(wpa_s->wpa, 1);
7155#endif /* CONFIG_TDLS */
7156
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007157 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
7158 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
7159
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007160 wpa_s->no_keep_alive = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007161 wpa_s->own_disconnect_req = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007162
7163 os_free(wpa_s->disallow_aps_bssid);
7164 wpa_s->disallow_aps_bssid = NULL;
7165 wpa_s->disallow_aps_bssid_count = 0;
7166 os_free(wpa_s->disallow_aps_ssid);
7167 wpa_s->disallow_aps_ssid = NULL;
7168 wpa_s->disallow_aps_ssid_count = 0;
7169
7170 wpa_s->set_sta_uapsd = 0;
7171 wpa_s->sta_uapsd = 0;
7172
7173 wpa_drv_radio_disable(wpa_s, 0);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007174 wpa_blacklist_clear(wpa_s);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07007175 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007176 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
7177 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08007178 wpa_config_flush_blobs(wpa_s->conf);
Dmitry Shmidt18463232014-01-24 12:29:41 -08007179 wpa_s->conf->auto_interworking = 0;
7180 wpa_s->conf->okc = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007181
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007182 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
7183 rsn_preauth_deinit(wpa_s->wpa);
7184
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007185 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
7186 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
7187 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
7188 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
7189
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007190 radio_remove_works(wpa_s, NULL, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007191 wpa_s->ext_work_in_progress = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007192
7193 wpa_s->next_ssid = NULL;
7194
7195#ifdef CONFIG_INTERWORKING
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007196#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007197 hs20_cancel_fetch_osu(wpa_s);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007198 hs20_del_icon(wpa_s, NULL, NULL);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007199#endif /* CONFIG_HS20 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007200#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt818ea482014-03-10 13:15:21 -07007201
7202 wpa_s->ext_mgmt_frame_handling = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007203 wpa_s->ext_eapol_frame_io = 0;
7204#ifdef CONFIG_TESTING_OPTIONS
7205 wpa_s->extra_roc_dur = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007206 wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007207 wpa_s->p2p_go_csa_on_inv = 0;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007208 wpa_s->ignore_auth_resp = 0;
7209 wpa_s->ignore_assoc_disallow = 0;
7210 wpa_s->reject_btm_req_reason = 0;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08007211 wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007212#endif /* CONFIG_TESTING_OPTIONS */
7213
7214 wpa_s->disconnected = 0;
7215 os_free(wpa_s->next_scan_freqs);
7216 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007217
7218 wpa_bss_flush(wpa_s);
7219 if (!dl_list_empty(&wpa_s->bss)) {
7220 wpa_printf(MSG_DEBUG,
7221 "BSS table not empty after flush: %u entries, current_bss=%p bssid="
7222 MACSTR " pending_bssid=" MACSTR,
7223 dl_list_len(&wpa_s->bss), wpa_s->current_bss,
7224 MAC2STR(wpa_s->bssid),
7225 MAC2STR(wpa_s->pending_bssid));
7226 }
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07007227
7228 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -08007229 wpa_s->wnmsleep_used = 0;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007230
7231#ifdef CONFIG_SME
7232 wpa_s->sme.last_unprot_disconnect.sec = 0;
7233#endif /* CONFIG_SME */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007234}
7235
7236
7237static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
7238 char *buf, size_t buflen)
7239{
7240 struct wpa_radio_work *work;
7241 char *pos, *end;
7242 struct os_reltime now, diff;
7243
7244 pos = buf;
7245 end = buf + buflen;
7246
7247 os_get_reltime(&now);
7248
7249 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
7250 {
7251 int ret;
7252
7253 os_reltime_sub(&now, &work->time, &diff);
7254 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
7255 work->type, work->wpa_s->ifname, work->freq,
7256 work->started, diff.sec, diff.usec);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007257 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007258 break;
7259 pos += ret;
7260 }
7261
7262 return pos - buf;
7263}
7264
7265
7266static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
7267{
7268 struct wpa_radio_work *work = eloop_ctx;
7269 struct wpa_external_work *ework = work->ctx;
7270
7271 wpa_dbg(work->wpa_s, MSG_DEBUG,
7272 "Timing out external radio work %u (%s)",
7273 ework->id, work->type);
7274 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007275 work->wpa_s->ext_work_in_progress = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007276 radio_work_done(work);
Dmitry Shmidt71757432014-06-02 13:50:35 -07007277 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007278}
7279
7280
7281static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
7282{
7283 struct wpa_external_work *ework = work->ctx;
7284
7285 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007286 if (work->started)
7287 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
7288 work, NULL);
7289
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007290 /*
7291 * work->type points to a buffer in ework, so need to replace
7292 * that here with a fixed string to avoid use of freed memory
7293 * in debug prints.
7294 */
7295 work->type = "freed-ext-work";
7296 work->ctx = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007297 os_free(ework);
7298 return;
7299 }
7300
7301 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
7302 ework->id, ework->type);
7303 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007304 work->wpa_s->ext_work_in_progress = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007305 if (!ework->timeout)
7306 ework->timeout = 10;
7307 eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
7308 work, NULL);
7309}
7310
7311
7312static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
7313 char *buf, size_t buflen)
7314{
7315 struct wpa_external_work *ework;
7316 char *pos, *pos2;
7317 size_t type_len;
7318 int ret;
7319 unsigned int freq = 0;
7320
7321 /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
7322
7323 ework = os_zalloc(sizeof(*ework));
7324 if (ework == NULL)
7325 return -1;
7326
7327 pos = os_strchr(cmd, ' ');
7328 if (pos) {
7329 type_len = pos - cmd;
7330 pos++;
7331
7332 pos2 = os_strstr(pos, "freq=");
7333 if (pos2)
7334 freq = atoi(pos2 + 5);
7335
7336 pos2 = os_strstr(pos, "timeout=");
7337 if (pos2)
7338 ework->timeout = atoi(pos2 + 8);
7339 } else {
7340 type_len = os_strlen(cmd);
7341 }
7342 if (4 + type_len >= sizeof(ework->type))
7343 type_len = sizeof(ework->type) - 4 - 1;
7344 os_strlcpy(ework->type, "ext:", sizeof(ework->type));
7345 os_memcpy(ework->type + 4, cmd, type_len);
7346 ework->type[4 + type_len] = '\0';
7347
7348 wpa_s->ext_work_id++;
7349 if (wpa_s->ext_work_id == 0)
7350 wpa_s->ext_work_id++;
7351 ework->id = wpa_s->ext_work_id;
7352
7353 if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
7354 ework) < 0) {
7355 os_free(ework);
7356 return -1;
7357 }
7358
7359 ret = os_snprintf(buf, buflen, "%u", ework->id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007360 if (os_snprintf_error(buflen, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007361 return -1;
7362 return ret;
7363}
7364
7365
7366static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
7367{
7368 struct wpa_radio_work *work;
7369 unsigned int id = atoi(cmd);
7370
7371 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
7372 {
7373 struct wpa_external_work *ework;
7374
7375 if (os_strncmp(work->type, "ext:", 4) != 0)
7376 continue;
7377 ework = work->ctx;
7378 if (id && ework->id != id)
7379 continue;
7380 wpa_dbg(wpa_s, MSG_DEBUG,
7381 "Completed external radio work %u (%s)",
7382 ework->id, ework->type);
7383 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007384 wpa_s->ext_work_in_progress = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007385 radio_work_done(work);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07007386 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007387 return 3; /* "OK\n" */
7388 }
7389
7390 return -1;
7391}
7392
7393
7394static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
7395 char *buf, size_t buflen)
7396{
7397 if (os_strcmp(cmd, "show") == 0)
7398 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
7399 if (os_strncmp(cmd, "add ", 4) == 0)
7400 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
7401 if (os_strncmp(cmd, "done ", 5) == 0)
7402 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
7403 return -1;
7404}
7405
7406
7407void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
7408{
7409 struct wpa_radio_work *work, *tmp;
7410
Dmitry Shmidt18463232014-01-24 12:29:41 -08007411 if (!wpa_s || !wpa_s->radio)
7412 return;
7413
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007414 dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
7415 struct wpa_radio_work, list) {
7416 struct wpa_external_work *ework;
7417
7418 if (os_strncmp(work->type, "ext:", 4) != 0)
7419 continue;
7420 ework = work->ctx;
7421 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007422 "Flushing%s external radio work %u (%s)",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007423 work->started ? " started" : "", ework->id,
7424 ework->type);
7425 if (work->started)
7426 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
7427 work, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007428 radio_work_done(work);
Dmitry Shmidt71757432014-06-02 13:50:35 -07007429 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007430 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007431}
7432
7433
Dmitry Shmidt051af732013-10-22 13:52:46 -07007434static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
7435{
7436 struct wpa_supplicant *wpa_s = eloop_ctx;
7437 eapol_sm_notify_ctrl_response(wpa_s->eapol);
7438}
7439
7440
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007441static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
7442 unsigned int *scan_id_count, int scan_id[])
Dmitry Shmidtc2817022014-07-02 10:32:10 -07007443{
7444 const char *pos = value;
7445
7446 while (pos) {
7447 if (*pos == ' ' || *pos == '\0')
7448 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007449 if (*scan_id_count == MAX_SCAN_ID)
Dmitry Shmidtc2817022014-07-02 10:32:10 -07007450 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007451 scan_id[(*scan_id_count)++] = atoi(pos);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07007452 pos = os_strchr(pos, ',');
7453 if (pos)
7454 pos++;
7455 }
7456
7457 return 0;
7458}
7459
7460
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007461static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
7462 char *reply, int reply_size, int *reply_len)
7463{
7464 char *pos;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007465 unsigned int manual_scan_passive = 0;
7466 unsigned int manual_scan_use_id = 0;
7467 unsigned int manual_scan_only_new = 0;
7468 unsigned int scan_only = 0;
7469 unsigned int scan_id_count = 0;
7470 int scan_id[MAX_SCAN_ID];
7471 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
7472 struct wpa_scan_results *scan_res);
7473 int *manual_scan_freqs = NULL;
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07007474 struct wpa_ssid_value *ssid = NULL, *ns;
7475 unsigned int ssid_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007476
7477 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
7478 *reply_len = -1;
7479 return;
7480 }
7481
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007482 if (radio_work_pending(wpa_s, "scan")) {
7483 wpa_printf(MSG_DEBUG,
7484 "Pending scan scheduled - reject new request");
7485 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
7486 return;
7487 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007488
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07007489#ifdef CONFIG_INTERWORKING
7490 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
7491 wpa_printf(MSG_DEBUG,
7492 "Interworking select in progress - reject new scan");
7493 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
7494 return;
7495 }
7496#endif /* CONFIG_INTERWORKING */
7497
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007498 if (params) {
7499 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007500 scan_only = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007501
7502 pos = os_strstr(params, "freq=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007503 if (pos) {
7504 manual_scan_freqs = freq_range_to_channel_list(wpa_s,
7505 pos + 5);
7506 if (manual_scan_freqs == NULL) {
7507 *reply_len = -1;
7508 goto done;
7509 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007510 }
7511
7512 pos = os_strstr(params, "passive=");
7513 if (pos)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007514 manual_scan_passive = !!atoi(pos + 8);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007515
7516 pos = os_strstr(params, "use_id=");
7517 if (pos)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007518 manual_scan_use_id = atoi(pos + 7);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007519
7520 pos = os_strstr(params, "only_new=1");
7521 if (pos)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007522 manual_scan_only_new = 1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07007523
7524 pos = os_strstr(params, "scan_id=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007525 if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
7526 scan_id) < 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07007527 *reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007528 goto done;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07007529 }
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07007530
7531 pos = params;
7532 while (pos && *pos != '\0') {
7533 if (os_strncmp(pos, "ssid ", 5) == 0) {
7534 char *end;
7535
7536 pos += 5;
7537 end = pos;
7538 while (*end) {
7539 if (*end == '\0' || *end == ' ')
7540 break;
7541 end++;
7542 }
7543
7544 ns = os_realloc_array(
7545 ssid, ssid_count + 1,
7546 sizeof(struct wpa_ssid_value));
7547 if (ns == NULL) {
7548 *reply_len = -1;
7549 goto done;
7550 }
7551 ssid = ns;
7552
7553 if ((end - pos) & 0x01 ||
7554 end - pos > 2 * SSID_MAX_LEN ||
7555 hexstr2bin(pos, ssid[ssid_count].ssid,
7556 (end - pos) / 2) < 0) {
7557 wpa_printf(MSG_DEBUG,
7558 "Invalid SSID value '%s'",
7559 pos);
7560 *reply_len = -1;
7561 goto done;
7562 }
7563 ssid[ssid_count].ssid_len = (end - pos) / 2;
7564 wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
7565 ssid[ssid_count].ssid,
7566 ssid[ssid_count].ssid_len);
7567 ssid_count++;
7568 pos = end;
7569 }
7570
7571 pos = os_strchr(pos, ' ');
7572 if (pos)
7573 pos++;
7574 }
7575 }
7576
7577 wpa_s->num_ssids_from_scan_req = ssid_count;
7578 os_free(wpa_s->ssids_from_scan_req);
7579 if (ssid_count) {
7580 wpa_s->ssids_from_scan_req = ssid;
7581 ssid = NULL;
7582 } else {
7583 wpa_s->ssids_from_scan_req = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007584 }
7585
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007586 if (scan_only)
7587 scan_res_handler = scan_only_handler;
7588 else if (wpa_s->scan_res_handler == scan_only_handler)
7589 scan_res_handler = NULL;
7590 else
7591 scan_res_handler = wpa_s->scan_res_handler;
7592
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007593 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
7594 ((wpa_s->wpa_state <= WPA_SCANNING) ||
7595 (wpa_s->wpa_state == WPA_COMPLETED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007596 wpa_s->manual_scan_passive = manual_scan_passive;
7597 wpa_s->manual_scan_use_id = manual_scan_use_id;
7598 wpa_s->manual_scan_only_new = manual_scan_only_new;
7599 wpa_s->scan_id_count = scan_id_count;
7600 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
7601 wpa_s->scan_res_handler = scan_res_handler;
7602 os_free(wpa_s->manual_scan_freqs);
7603 wpa_s->manual_scan_freqs = manual_scan_freqs;
7604 manual_scan_freqs = NULL;
7605
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007606 wpa_s->normal_scans = 0;
7607 wpa_s->scan_req = MANUAL_SCAN_REQ;
7608 wpa_s->after_wps = 0;
7609 wpa_s->known_wps_freq = 0;
7610 wpa_supplicant_req_scan(wpa_s, 0, 0);
7611 if (wpa_s->manual_scan_use_id) {
7612 wpa_s->manual_scan_id++;
7613 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
7614 wpa_s->manual_scan_id);
7615 *reply_len = os_snprintf(reply, reply_size, "%u\n",
7616 wpa_s->manual_scan_id);
7617 }
7618 } else if (wpa_s->sched_scanning) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007619 wpa_s->manual_scan_passive = manual_scan_passive;
7620 wpa_s->manual_scan_use_id = manual_scan_use_id;
7621 wpa_s->manual_scan_only_new = manual_scan_only_new;
7622 wpa_s->scan_id_count = scan_id_count;
7623 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
7624 wpa_s->scan_res_handler = scan_res_handler;
7625 os_free(wpa_s->manual_scan_freqs);
7626 wpa_s->manual_scan_freqs = manual_scan_freqs;
7627 manual_scan_freqs = NULL;
7628
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007629 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
7630 wpa_supplicant_cancel_sched_scan(wpa_s);
7631 wpa_s->scan_req = MANUAL_SCAN_REQ;
7632 wpa_supplicant_req_scan(wpa_s, 0, 0);
7633 if (wpa_s->manual_scan_use_id) {
7634 wpa_s->manual_scan_id++;
7635 *reply_len = os_snprintf(reply, reply_size, "%u\n",
7636 wpa_s->manual_scan_id);
7637 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
7638 wpa_s->manual_scan_id);
7639 }
7640 } else {
7641 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
7642 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
7643 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007644
7645done:
7646 os_free(manual_scan_freqs);
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07007647 os_free(ssid);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007648}
7649
7650
Dmitry Shmidt818ea482014-03-10 13:15:21 -07007651#ifdef CONFIG_TESTING_OPTIONS
7652
7653static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
7654 unsigned int freq, const u8 *dst,
7655 const u8 *src, const u8 *bssid,
7656 const u8 *data, size_t data_len,
7657 enum offchannel_send_action_result
7658 result)
7659{
7660 wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
7661 " src=" MACSTR " bssid=" MACSTR " result=%s",
7662 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
7663 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
7664 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
7665 "NO_ACK" : "FAILED"));
7666}
7667
7668
7669static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
7670{
7671 char *pos, *param;
7672 size_t len;
7673 u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
7674 int res, used;
7675 int freq = 0, no_cck = 0, wait_time = 0;
7676
7677 /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
7678 * <action=Action frame payload> */
7679
7680 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
7681
7682 pos = cmd;
7683 used = hwaddr_aton2(pos, da);
7684 if (used < 0)
7685 return -1;
7686 pos += used;
7687 while (*pos == ' ')
7688 pos++;
7689 used = hwaddr_aton2(pos, bssid);
7690 if (used < 0)
7691 return -1;
7692 pos += used;
7693
7694 param = os_strstr(pos, " freq=");
7695 if (param) {
7696 param += 6;
7697 freq = atoi(param);
7698 }
7699
7700 param = os_strstr(pos, " no_cck=");
7701 if (param) {
7702 param += 8;
7703 no_cck = atoi(param);
7704 }
7705
7706 param = os_strstr(pos, " wait_time=");
7707 if (param) {
7708 param += 11;
7709 wait_time = atoi(param);
7710 }
7711
7712 param = os_strstr(pos, " action=");
7713 if (param == NULL)
7714 return -1;
7715 param += 8;
7716
7717 len = os_strlen(param);
7718 if (len & 1)
7719 return -1;
7720 len /= 2;
7721
7722 buf = os_malloc(len);
7723 if (buf == NULL)
7724 return -1;
7725
7726 if (hexstr2bin(param, buf, len) < 0) {
7727 os_free(buf);
7728 return -1;
7729 }
7730
7731 res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
7732 buf, len, wait_time,
7733 wpas_ctrl_iface_mgmt_tx_cb, no_cck);
7734 os_free(buf);
7735 return res;
7736}
7737
7738
7739static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
7740{
7741 wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
7742 offchannel_send_action_done(wpa_s);
7743}
7744
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007745
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007746static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
7747 char *cmd)
7748{
7749 char *pos, *param;
7750 size_t len;
7751 u8 *buf;
7752 int freq = 0, datarate = 0, ssi_signal = 0;
7753 union wpa_event_data event;
7754
7755 if (!wpa_s->ext_mgmt_frame_handling)
7756 return -1;
7757
7758 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
7759
7760 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
7761
7762 pos = cmd;
7763 param = os_strstr(pos, "freq=");
7764 if (param) {
7765 param += 5;
7766 freq = atoi(param);
7767 }
7768
7769 param = os_strstr(pos, " datarate=");
7770 if (param) {
7771 param += 10;
7772 datarate = atoi(param);
7773 }
7774
7775 param = os_strstr(pos, " ssi_signal=");
7776 if (param) {
7777 param += 12;
7778 ssi_signal = atoi(param);
7779 }
7780
7781 param = os_strstr(pos, " frame=");
7782 if (param == NULL)
7783 return -1;
7784 param += 7;
7785
7786 len = os_strlen(param);
7787 if (len & 1)
7788 return -1;
7789 len /= 2;
7790
7791 buf = os_malloc(len);
7792 if (buf == NULL)
7793 return -1;
7794
7795 if (hexstr2bin(param, buf, len) < 0) {
7796 os_free(buf);
7797 return -1;
7798 }
7799
7800 os_memset(&event, 0, sizeof(event));
7801 event.rx_mgmt.freq = freq;
7802 event.rx_mgmt.frame = buf;
7803 event.rx_mgmt.frame_len = len;
7804 event.rx_mgmt.ssi_signal = ssi_signal;
7805 event.rx_mgmt.datarate = datarate;
7806 wpa_s->ext_mgmt_frame_handling = 0;
7807 wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
7808 wpa_s->ext_mgmt_frame_handling = 1;
7809
7810 os_free(buf);
7811
7812 return 0;
7813}
7814
7815
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007816static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
7817{
7818 char *pos, *param;
7819 union wpa_event_data event;
7820 enum wpa_event_type ev;
7821
7822 /* <event name> [parameters..] */
7823
7824 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
7825
7826 pos = cmd;
7827 param = os_strchr(pos, ' ');
7828 if (param)
7829 *param++ = '\0';
7830
7831 os_memset(&event, 0, sizeof(event));
7832
7833 if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
7834 ev = EVENT_INTERFACE_ENABLED;
7835 } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
7836 ev = EVENT_INTERFACE_DISABLED;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007837 } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
7838 ev = EVENT_AVOID_FREQUENCIES;
7839 if (param == NULL)
7840 param = "";
7841 if (freq_range_list_parse(&event.freq_range, param) < 0)
7842 return -1;
7843 wpa_supplicant_event(wpa_s, ev, &event);
7844 os_free(event.freq_range.range);
7845 return 0;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007846 } else {
7847 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
7848 cmd);
7849 return -1;
7850 }
7851
7852 wpa_supplicant_event(wpa_s, ev, &event);
7853
7854 return 0;
7855}
7856
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007857
7858static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
7859{
7860 char *pos;
7861 u8 src[ETH_ALEN], *buf;
7862 int used;
7863 size_t len;
7864
7865 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
7866
7867 pos = cmd;
7868 used = hwaddr_aton2(pos, src);
7869 if (used < 0)
7870 return -1;
7871 pos += used;
7872 while (*pos == ' ')
7873 pos++;
7874
7875 len = os_strlen(pos);
7876 if (len & 1)
7877 return -1;
7878 len /= 2;
7879
7880 buf = os_malloc(len);
7881 if (buf == NULL)
7882 return -1;
7883
7884 if (hexstr2bin(pos, buf, len) < 0) {
7885 os_free(buf);
7886 return -1;
7887 }
7888
7889 wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
7890 os_free(buf);
7891
7892 return 0;
7893}
7894
7895
7896static u16 ipv4_hdr_checksum(const void *buf, size_t len)
7897{
7898 size_t i;
7899 u32 sum = 0;
7900 const u16 *pos = buf;
7901
7902 for (i = 0; i < len / 2; i++)
7903 sum += *pos++;
7904
7905 while (sum >> 16)
7906 sum = (sum & 0xffff) + (sum >> 16);
7907
7908 return sum ^ 0xffff;
7909}
7910
7911
7912#define HWSIM_PACKETLEN 1500
7913#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
7914
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07007915static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
7916 size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007917{
7918 struct wpa_supplicant *wpa_s = ctx;
7919 const struct ether_header *eth;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007920 struct iphdr ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007921 const u8 *pos;
7922 unsigned int i;
7923
7924 if (len != HWSIM_PACKETLEN)
7925 return;
7926
7927 eth = (const struct ether_header *) buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007928 os_memcpy(&ip, eth + 1, sizeof(ip));
7929 pos = &buf[sizeof(*eth) + sizeof(ip)];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007930
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007931 if (ip.ihl != 5 || ip.version != 4 ||
7932 ntohs(ip.tot_len) != HWSIM_IP_LEN)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007933 return;
7934
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007935 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007936 if (*pos != (u8) i)
7937 return;
7938 pos++;
7939 }
7940
7941 wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
7942 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
7943}
7944
7945
7946static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
7947 char *cmd)
7948{
7949 int enabled = atoi(cmd);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007950 char *pos;
7951 const char *ifname;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007952
7953 if (!enabled) {
7954 if (wpa_s->l2_test) {
7955 l2_packet_deinit(wpa_s->l2_test);
7956 wpa_s->l2_test = NULL;
7957 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
7958 }
7959 return 0;
7960 }
7961
7962 if (wpa_s->l2_test)
7963 return 0;
7964
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007965 pos = os_strstr(cmd, " ifname=");
7966 if (pos)
7967 ifname = pos + 8;
7968 else
7969 ifname = wpa_s->ifname;
7970
7971 wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007972 ETHERTYPE_IP, wpas_data_test_rx,
7973 wpa_s, 1);
7974 if (wpa_s->l2_test == NULL)
7975 return -1;
7976
7977 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
7978
7979 return 0;
7980}
7981
7982
7983static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
7984{
7985 u8 dst[ETH_ALEN], src[ETH_ALEN];
7986 char *pos;
7987 int used;
7988 long int val;
7989 u8 tos;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007990 u8 buf[2 + HWSIM_PACKETLEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007991 struct ether_header *eth;
7992 struct iphdr *ip;
7993 u8 *dpos;
7994 unsigned int i;
7995
7996 if (wpa_s->l2_test == NULL)
7997 return -1;
7998
7999 /* format: <dst> <src> <tos> */
8000
8001 pos = cmd;
8002 used = hwaddr_aton2(pos, dst);
8003 if (used < 0)
8004 return -1;
8005 pos += used;
8006 while (*pos == ' ')
8007 pos++;
8008 used = hwaddr_aton2(pos, src);
8009 if (used < 0)
8010 return -1;
8011 pos += used;
8012
8013 val = strtol(pos, NULL, 0);
8014 if (val < 0 || val > 0xff)
8015 return -1;
8016 tos = val;
8017
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008018 eth = (struct ether_header *) &buf[2];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008019 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
8020 os_memcpy(eth->ether_shost, src, ETH_ALEN);
8021 eth->ether_type = htons(ETHERTYPE_IP);
8022 ip = (struct iphdr *) (eth + 1);
8023 os_memset(ip, 0, sizeof(*ip));
8024 ip->ihl = 5;
8025 ip->version = 4;
8026 ip->ttl = 64;
8027 ip->tos = tos;
8028 ip->tot_len = htons(HWSIM_IP_LEN);
8029 ip->protocol = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008030 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
8031 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008032 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
8033 dpos = (u8 *) (ip + 1);
8034 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
8035 *dpos++ = i;
8036
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008037 if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008038 HWSIM_PACKETLEN) < 0)
8039 return -1;
8040
8041 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
8042 " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
8043
8044 return 0;
8045}
8046
8047
8048static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
8049 char *cmd)
8050{
8051 u8 *buf;
8052 struct ether_header *eth;
8053 struct l2_packet_data *l2 = NULL;
8054 size_t len;
8055 u16 ethertype;
8056 int res = -1;
8057
8058 len = os_strlen(cmd);
8059 if (len & 1 || len < ETH_HLEN * 2)
8060 return -1;
8061 len /= 2;
8062
8063 buf = os_malloc(len);
8064 if (buf == NULL)
8065 return -1;
8066
8067 if (hexstr2bin(cmd, buf, len) < 0)
8068 goto done;
8069
8070 eth = (struct ether_header *) buf;
8071 ethertype = ntohs(eth->ether_type);
8072
8073 l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
8074 wpas_data_test_rx, wpa_s, 1);
8075 if (l2 == NULL)
8076 goto done;
8077
8078 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
8079 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
8080done:
8081 if (l2)
8082 l2_packet_deinit(l2);
8083 os_free(buf);
8084
8085 return res < 0 ? -1 : 0;
8086}
8087
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008088
8089static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
8090{
8091#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008092 char *pos;
8093
8094 wpa_trace_fail_after = atoi(cmd);
8095 pos = os_strchr(cmd, ':');
8096 if (pos) {
8097 pos++;
8098 os_strlcpy(wpa_trace_fail_func, pos,
8099 sizeof(wpa_trace_fail_func));
8100 } else {
8101 wpa_trace_fail_after = 0;
8102 }
8103 return 0;
8104#else /* WPA_TRACE_BFD */
8105 return -1;
8106#endif /* WPA_TRACE_BFD */
8107}
8108
8109
8110static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
8111 char *buf, size_t buflen)
8112{
8113#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008114 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
8115 wpa_trace_fail_func);
8116#else /* WPA_TRACE_BFD */
8117 return -1;
8118#endif /* WPA_TRACE_BFD */
8119}
8120
Jouni Malinenc4818362015-10-04 11:45:13 +03008121
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008122static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
8123{
8124#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008125 char *pos;
8126
8127 wpa_trace_test_fail_after = atoi(cmd);
8128 pos = os_strchr(cmd, ':');
8129 if (pos) {
8130 pos++;
8131 os_strlcpy(wpa_trace_test_fail_func, pos,
8132 sizeof(wpa_trace_test_fail_func));
8133 } else {
8134 wpa_trace_test_fail_after = 0;
8135 }
8136 return 0;
8137#else /* WPA_TRACE_BFD */
8138 return -1;
8139#endif /* WPA_TRACE_BFD */
8140}
8141
8142
8143static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
8144 char *buf, size_t buflen)
8145{
8146#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008147 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
8148 wpa_trace_test_fail_func);
8149#else /* WPA_TRACE_BFD */
8150 return -1;
8151#endif /* WPA_TRACE_BFD */
8152}
8153
8154
Jouni Malinenc4818362015-10-04 11:45:13 +03008155static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
8156{
8157 struct wpa_supplicant *wpa_s = eloop_ctx;
8158 int i, count = (intptr_t) timeout_ctx;
8159
8160 wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
8161 count);
8162 for (i = 0; i < count; i++) {
8163 wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
8164 i + 1, count);
8165 }
8166}
8167
8168
8169static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
8170{
8171 int count;
8172
8173 count = atoi(cmd);
8174 if (count <= 0)
8175 return -1;
8176
8177 return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
8178 (void *) (intptr_t) count);
8179}
8180
Dmitry Shmidt818ea482014-03-10 13:15:21 -07008181
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008182static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
8183 const char *cmd)
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008184{
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008185 struct wpabuf *buf;
8186 size_t len;
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008187
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008188 len = os_strlen(cmd);
8189 if (len & 1)
8190 return -1;
8191 len /= 2;
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008192
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008193 if (len == 0) {
8194 buf = NULL;
8195 } else {
8196 buf = wpabuf_alloc(len);
8197 if (buf == NULL)
8198 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008199
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008200 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
8201 wpabuf_free(buf);
8202 return -1;
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008203 }
8204 }
8205
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008206 wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
8207 return 0;
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008208}
8209
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008210#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008211
8212
8213static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
8214{
8215 char *pos = cmd;
8216 int frame;
8217 size_t len;
8218 struct wpabuf *buf;
8219 struct ieee802_11_elems elems;
8220
8221 frame = atoi(pos);
8222 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
8223 return -1;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008224 wpa_s = wpas_vendor_elem(wpa_s, frame);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008225
8226 pos = os_strchr(pos, ' ');
8227 if (pos == NULL)
8228 return -1;
8229 pos++;
8230
8231 len = os_strlen(pos);
8232 if (len == 0)
8233 return 0;
8234 if (len & 1)
8235 return -1;
8236 len /= 2;
8237
8238 buf = wpabuf_alloc(len);
8239 if (buf == NULL)
8240 return -1;
8241
8242 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
8243 wpabuf_free(buf);
8244 return -1;
8245 }
8246
8247 if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
8248 ParseFailed) {
8249 wpabuf_free(buf);
8250 return -1;
8251 }
8252
8253 if (wpa_s->vendor_elem[frame] == NULL) {
8254 wpa_s->vendor_elem[frame] = buf;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008255 wpas_vendor_elem_update(wpa_s);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008256 return 0;
8257 }
8258
8259 if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
8260 wpabuf_free(buf);
8261 return -1;
8262 }
8263
8264 wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
8265 wpabuf_free(buf);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008266 wpas_vendor_elem_update(wpa_s);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008267
8268 return 0;
8269}
8270
8271
8272static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
8273 char *buf, size_t buflen)
8274{
8275 int frame = atoi(cmd);
8276
8277 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
8278 return -1;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008279 wpa_s = wpas_vendor_elem(wpa_s, frame);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008280
8281 if (wpa_s->vendor_elem[frame] == NULL)
8282 return 0;
8283
8284 return wpa_snprintf_hex(buf, buflen,
8285 wpabuf_head_u8(wpa_s->vendor_elem[frame]),
8286 wpabuf_len(wpa_s->vendor_elem[frame]));
8287}
8288
8289
8290static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
8291{
8292 char *pos = cmd;
8293 int frame;
8294 size_t len;
8295 u8 *buf;
8296 struct ieee802_11_elems elems;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008297 int res;
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008298
8299 frame = atoi(pos);
8300 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
8301 return -1;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008302 wpa_s = wpas_vendor_elem(wpa_s, frame);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008303
8304 pos = os_strchr(pos, ' ');
8305 if (pos == NULL)
8306 return -1;
8307 pos++;
8308
8309 if (*pos == '*') {
8310 wpabuf_free(wpa_s->vendor_elem[frame]);
8311 wpa_s->vendor_elem[frame] = NULL;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008312 wpas_vendor_elem_update(wpa_s);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008313 return 0;
8314 }
8315
8316 if (wpa_s->vendor_elem[frame] == NULL)
8317 return -1;
8318
8319 len = os_strlen(pos);
8320 if (len == 0)
8321 return 0;
8322 if (len & 1)
8323 return -1;
8324 len /= 2;
8325
8326 buf = os_malloc(len);
8327 if (buf == NULL)
8328 return -1;
8329
8330 if (hexstr2bin(pos, buf, len) < 0) {
8331 os_free(buf);
8332 return -1;
8333 }
8334
8335 if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
8336 os_free(buf);
8337 return -1;
8338 }
8339
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008340 res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008341 os_free(buf);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008342 return res;
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07008343}
8344
8345
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008346static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
8347{
8348 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008349 size_t len;
8350 const u8 *data;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008351
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008352 /*
8353 * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
8354 * BSSID[6]
8355 * BSSID Information[4]
8356 * Operating Class[1]
8357 * Channel Number[1]
8358 * PHY Type[1]
8359 * Optional Subelements[variable]
8360 */
8361#define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
8362
8363 if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008364 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008365 goto out;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008366 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008367
8368 data = wpabuf_head_u8(neighbor_rep);
8369 len = wpabuf_len(neighbor_rep);
8370
8371 while (len >= 2 + NR_IE_MIN_LEN) {
8372 const u8 *nr;
8373 char lci[256 * 2 + 1];
8374 char civic[256 * 2 + 1];
8375 u8 nr_len = data[1];
8376 const u8 *pos = data, *end;
8377
8378 if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
8379 nr_len < NR_IE_MIN_LEN) {
8380 wpa_printf(MSG_DEBUG,
8381 "CTRL: Invalid Neighbor Report element: id=%u len=%u",
8382 data[0], nr_len);
8383 goto out;
8384 }
8385
8386 if (2U + nr_len > len) {
8387 wpa_printf(MSG_DEBUG,
8388 "CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
8389 data[0], len, nr_len);
8390 goto out;
8391 }
8392 pos += 2;
8393 end = pos + nr_len;
8394
8395 nr = pos;
8396 pos += NR_IE_MIN_LEN;
8397
8398 lci[0] = '\0';
8399 civic[0] = '\0';
8400 while (end - pos > 2) {
8401 u8 s_id, s_len;
8402
8403 s_id = *pos++;
8404 s_len = *pos++;
8405 if (s_len > end - pos)
8406 goto out;
8407 if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
8408 /* Measurement Token[1] */
8409 /* Measurement Report Mode[1] */
8410 /* Measurement Type[1] */
8411 /* Measurement Report[variable] */
8412 switch (pos[2]) {
8413 case MEASURE_TYPE_LCI:
8414 if (lci[0])
8415 break;
8416 wpa_snprintf_hex(lci, sizeof(lci),
8417 pos, s_len);
8418 break;
8419 case MEASURE_TYPE_LOCATION_CIVIC:
8420 if (civic[0])
8421 break;
8422 wpa_snprintf_hex(civic, sizeof(civic),
8423 pos, s_len);
8424 break;
8425 }
8426 }
8427
8428 pos += s_len;
8429 }
8430
8431 wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
8432 "bssid=" MACSTR
8433 " info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
8434 MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
8435 nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
8436 nr[ETH_ALEN + 6],
8437 lci[0] ? " lci=" : "", lci,
8438 civic[0] ? " civic=" : "", civic);
8439
8440 data = end;
8441 len -= 2 + nr_len;
8442 }
8443
8444out:
8445 wpabuf_free(neighbor_rep);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008446}
8447
8448
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008449static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
8450 char *cmd)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008451{
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008452 struct wpa_ssid_value ssid, *ssid_p = NULL;
8453 int ret, lci = 0, civic = 0;
8454 char *ssid_s;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008455
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008456 ssid_s = os_strstr(cmd, "ssid=");
8457 if (ssid_s) {
8458 if (ssid_parse(ssid_s + 5, &ssid)) {
8459 wpa_printf(MSG_ERROR,
8460 "CTRL: Send Neighbor Report: bad SSID");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008461 return -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008462 }
8463
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008464 ssid_p = &ssid;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008465
8466 /*
8467 * Move cmd after the SSID text that may include "lci" or
8468 * "civic".
8469 */
8470 cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
8471 if (cmd)
8472 cmd++;
8473
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008474 }
8475
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008476 if (cmd && os_strstr(cmd, "lci"))
8477 lci = 1;
8478
8479 if (cmd && os_strstr(cmd, "civic"))
8480 civic = 1;
8481
8482 ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008483 wpas_ctrl_neighbor_rep_cb,
8484 wpa_s);
8485
8486 return ret;
8487}
8488
8489
8490static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
8491{
8492 eapol_sm_erp_flush(wpa_s->eapol);
8493 return 0;
8494}
8495
8496
8497static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
8498 char *cmd)
8499{
8500 char *token, *context = NULL;
8501 unsigned int enable = ~0, type = 0;
8502 u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
8503 u8 *addr = NULL, *mask = NULL;
8504
8505 while ((token = str_token(cmd, " ", &context))) {
8506 if (os_strcasecmp(token, "scan") == 0) {
8507 type |= MAC_ADDR_RAND_SCAN;
8508 } else if (os_strcasecmp(token, "sched") == 0) {
8509 type |= MAC_ADDR_RAND_SCHED_SCAN;
8510 } else if (os_strcasecmp(token, "pno") == 0) {
8511 type |= MAC_ADDR_RAND_PNO;
8512 } else if (os_strcasecmp(token, "all") == 0) {
8513 type = wpa_s->mac_addr_rand_supported;
8514 } else if (os_strncasecmp(token, "enable=", 7) == 0) {
8515 enable = atoi(token + 7);
8516 } else if (os_strncasecmp(token, "addr=", 5) == 0) {
8517 addr = _addr;
8518 if (hwaddr_aton(token + 5, addr)) {
8519 wpa_printf(MSG_INFO,
8520 "CTRL: Invalid MAC address: %s",
8521 token);
8522 return -1;
8523 }
8524 } else if (os_strncasecmp(token, "mask=", 5) == 0) {
8525 mask = _mask;
8526 if (hwaddr_aton(token + 5, mask)) {
8527 wpa_printf(MSG_INFO,
8528 "CTRL: Invalid MAC address mask: %s",
8529 token);
8530 return -1;
8531 }
8532 } else {
8533 wpa_printf(MSG_INFO,
8534 "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
8535 token);
8536 return -1;
8537 }
8538 }
8539
8540 if (!type) {
8541 wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
8542 return -1;
8543 }
8544
8545 if ((wpa_s->mac_addr_rand_supported & type) != type) {
8546 wpa_printf(MSG_INFO,
8547 "CTRL: MAC_RAND_SCAN types=%u != supported=%u",
8548 type, wpa_s->mac_addr_rand_supported);
8549 return -1;
8550 }
8551
8552 if (enable > 1) {
8553 wpa_printf(MSG_INFO,
8554 "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
8555 return -1;
8556 }
8557
8558 if (!enable) {
8559 wpas_mac_addr_rand_scan_clear(wpa_s, type);
8560 if (wpa_s->pno) {
8561 if (type & MAC_ADDR_RAND_PNO) {
8562 wpas_stop_pno(wpa_s);
8563 wpas_start_pno(wpa_s);
8564 }
8565 } else if (wpa_s->sched_scanning &&
8566 (type & MAC_ADDR_RAND_SCHED_SCAN)) {
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07008567 wpas_scan_restart_sched_scan(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008568 }
8569 return 0;
8570 }
8571
8572 if ((addr && !mask) || (!addr && mask)) {
8573 wpa_printf(MSG_INFO,
8574 "CTRL: MAC_RAND_SCAN invalid addr/mask combination");
8575 return -1;
8576 }
8577
8578 if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
8579 wpa_printf(MSG_INFO,
8580 "CTRL: MAC_RAND_SCAN cannot allow multicast address");
8581 return -1;
8582 }
8583
8584 if (type & MAC_ADDR_RAND_SCAN) {
8585 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
8586 addr, mask);
8587 }
8588
8589 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
8590 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
8591 addr, mask);
8592
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07008593 if (wpa_s->sched_scanning && !wpa_s->pno)
8594 wpas_scan_restart_sched_scan(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008595 }
8596
8597 if (type & MAC_ADDR_RAND_PNO) {
8598 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
8599 addr, mask);
8600 if (wpa_s->pno) {
8601 wpas_stop_pno(wpa_s);
8602 wpas_start_pno(wpa_s);
8603 }
8604 }
8605
8606 return 0;
8607}
8608
8609
Dmitry Shmidte4663042016-04-04 10:07:49 -07008610static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
8611 char *buf, size_t buflen)
8612{
8613 size_t reply_len;
8614
8615 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
8616#ifdef CONFIG_AP
8617 reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
8618 buflen - reply_len);
8619#endif /* CONFIG_AP */
8620 return reply_len;
8621}
8622
8623
8624static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
8625{
8626 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
8627#ifdef CONFIG_AP
8628 wpas_ap_pmksa_cache_flush(wpa_s);
8629#endif /* CONFIG_AP */
8630}
8631
8632
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008633static int wpas_ctrl_cmd_debug_level(const char *cmd)
8634{
8635 if (os_strcmp(cmd, "PING") == 0 ||
8636 os_strncmp(cmd, "BSS ", 4) == 0 ||
8637 os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
8638 os_strncmp(cmd, "STATUS", 6) == 0 ||
8639 os_strncmp(cmd, "STA ", 4) == 0 ||
8640 os_strncmp(cmd, "STA-", 4) == 0)
8641 return MSG_EXCESSIVE;
8642 return MSG_DEBUG;
8643}
8644
8645
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008646char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
8647 char *buf, size_t *resp_len)
8648{
8649 char *reply;
8650 const int reply_size = 4096;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008651 int reply_len;
8652
8653 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008654 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
8655 if (wpa_debug_show_keys)
8656 wpa_dbg(wpa_s, MSG_DEBUG,
8657 "Control interface command '%s'", buf);
8658 else
8659 wpa_dbg(wpa_s, MSG_DEBUG,
8660 "Control interface command '%s [REMOVED]'",
8661 os_strncmp(buf, WPA_CTRL_RSP,
8662 os_strlen(WPA_CTRL_RSP)) == 0 ?
8663 WPA_CTRL_RSP : "SET_NETWORK");
8664 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008665 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008666 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
8667 (const u8 *) buf, os_strlen(buf));
8668 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008669 int level = wpas_ctrl_cmd_debug_level(buf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008670 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008671 }
8672
8673 reply = os_malloc(reply_size);
8674 if (reply == NULL) {
8675 *resp_len = 1;
8676 return NULL;
8677 }
8678
8679 os_memcpy(reply, "OK\n", 3);
8680 reply_len = 3;
8681
8682 if (os_strcmp(buf, "PING") == 0) {
8683 os_memcpy(reply, "PONG\n", 5);
8684 reply_len = 5;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008685 } else if (os_strcmp(buf, "IFNAME") == 0) {
8686 reply_len = os_strlen(wpa_s->ifname);
8687 os_memcpy(reply, wpa_s->ifname, reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008688 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
8689 if (wpa_debug_reopen_file() < 0)
8690 reply_len = -1;
8691 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
8692 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
8693 } else if (os_strcmp(buf, "MIB") == 0) {
8694 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
8695 if (reply_len >= 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008696 reply_len += eapol_sm_get_mib(wpa_s->eapol,
8697 reply + reply_len,
8698 reply_size - reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008699 }
8700 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
8701 reply_len = wpa_supplicant_ctrl_iface_status(
8702 wpa_s, buf + 6, reply, reply_size);
8703 } else if (os_strcmp(buf, "PMKSA") == 0) {
Dmitry Shmidte4663042016-04-04 10:07:49 -07008704 reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008705 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
Dmitry Shmidte4663042016-04-04 10:07:49 -07008706 wpas_ctrl_iface_pmksa_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008707 } else if (os_strncmp(buf, "SET ", 4) == 0) {
8708 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
8709 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008710 } else if (os_strncmp(buf, "DUMP", 4) == 0) {
8711 reply_len = wpa_config_dump_values(wpa_s->conf,
8712 reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008713 } else if (os_strncmp(buf, "GET ", 4) == 0) {
8714 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
8715 reply, reply_size);
8716 } else if (os_strcmp(buf, "LOGON") == 0) {
8717 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
8718 } else if (os_strcmp(buf, "LOGOFF") == 0) {
8719 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
8720 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
8721 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
8722 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008723 else
8724 wpas_request_connection(wpa_s);
Dmitry Shmidt98660862014-03-11 17:26:21 -07008725 } else if (os_strcmp(buf, "REATTACH") == 0) {
8726 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
8727 !wpa_s->current_ssid)
8728 reply_len = -1;
8729 else {
8730 wpa_s->reattach = 1;
8731 wpas_request_connection(wpa_s);
8732 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008733 } else if (os_strcmp(buf, "RECONNECT") == 0) {
8734 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
8735 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008736 else if (wpa_s->disconnected)
8737 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008738#ifdef IEEE8021X_EAPOL
8739 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
8740 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
8741 reply_len = -1;
8742#endif /* IEEE8021X_EAPOL */
8743#ifdef CONFIG_PEERKEY
8744 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
8745 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
8746 reply_len = -1;
8747#endif /* CONFIG_PEERKEY */
8748#ifdef CONFIG_IEEE80211R
8749 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
8750 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
8751 reply_len = -1;
8752#endif /* CONFIG_IEEE80211R */
8753#ifdef CONFIG_WPS
8754 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
8755 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
8756 if (res == -2) {
8757 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
8758 reply_len = 17;
8759 } else if (res)
8760 reply_len = -1;
8761 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
8762 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
8763 if (res == -2) {
8764 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
8765 reply_len = 17;
8766 } else if (res)
8767 reply_len = -1;
8768 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
8769 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
8770 reply,
8771 reply_size);
8772 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
8773 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
8774 wpa_s, buf + 14, reply, reply_size);
8775 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
8776 if (wpas_wps_cancel(wpa_s))
8777 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008778#ifdef CONFIG_WPS_NFC
8779 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
8780 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
8781 reply_len = -1;
8782 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
8783 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
8784 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008785 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
8786 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
8787 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008788 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
8789 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
8790 wpa_s, buf + 14, reply, reply_size);
8791 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
8792 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
8793 buf + 17))
8794 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008795 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
8796 reply_len = wpas_ctrl_nfc_get_handover_req(
8797 wpa_s, buf + 21, reply, reply_size);
8798 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
8799 reply_len = wpas_ctrl_nfc_get_handover_sel(
8800 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008801 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
8802 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
8803 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008804#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008805 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
8806 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
8807 reply_len = -1;
8808#ifdef CONFIG_AP
8809 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
8810 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
8811 wpa_s, buf + 11, reply, reply_size);
8812#endif /* CONFIG_AP */
8813#ifdef CONFIG_WPS_ER
8814 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
8815 if (wpas_wps_er_start(wpa_s, NULL))
8816 reply_len = -1;
8817 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
8818 if (wpas_wps_er_start(wpa_s, buf + 13))
8819 reply_len = -1;
8820 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008821 wpas_wps_er_stop(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008822 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
8823 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
8824 reply_len = -1;
8825 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
8826 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
8827 if (ret == -2) {
8828 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
8829 reply_len = 17;
8830 } else if (ret == -3) {
8831 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
8832 reply_len = 18;
8833 } else if (ret == -4) {
8834 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
8835 reply_len = 20;
8836 } else if (ret)
8837 reply_len = -1;
8838 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
8839 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
8840 reply_len = -1;
8841 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
8842 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
8843 buf + 18))
8844 reply_len = -1;
8845 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
8846 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
8847 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008848#ifdef CONFIG_WPS_NFC
8849 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
8850 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
8851 wpa_s, buf + 24, reply, reply_size);
8852#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008853#endif /* CONFIG_WPS_ER */
8854#endif /* CONFIG_WPS */
8855#ifdef CONFIG_IBSS_RSN
8856 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
8857 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
8858 reply_len = -1;
8859#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008860#ifdef CONFIG_MESH
8861 } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
8862 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
8863 wpa_s, buf + 19, reply, reply_size);
8864 } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
8865 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
8866 wpa_s, "", reply, reply_size);
8867 } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
8868 if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
8869 reply_len = -1;
8870 } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
8871 if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
8872 buf + 18))
8873 reply_len = -1;
Dmitry Shmidte4663042016-04-04 10:07:49 -07008874 } else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
8875 if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
8876 reply_len = -1;
8877 } else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
8878 if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
8879 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008880#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008881#ifdef CONFIG_P2P
8882 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008883 if (p2p_ctrl_find(wpa_s, buf + 8))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008884 reply_len = -1;
8885 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
8886 if (p2p_ctrl_find(wpa_s, ""))
8887 reply_len = -1;
8888 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
8889 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008890 } else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
8891 if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
8892 reply_len = -1;
8893 } else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
8894 if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
8895 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008896 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
8897 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
8898 reply_size);
8899 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
8900 if (p2p_ctrl_listen(wpa_s, buf + 11))
8901 reply_len = -1;
8902 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
8903 if (p2p_ctrl_listen(wpa_s, ""))
8904 reply_len = -1;
8905 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
8906 if (wpas_p2p_group_remove(wpa_s, buf + 17))
8907 reply_len = -1;
8908 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008909 if (p2p_ctrl_group_add(wpa_s, ""))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008910 reply_len = -1;
8911 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
8912 if (p2p_ctrl_group_add(wpa_s, buf + 14))
8913 reply_len = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008914 } else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
8915 reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
8916 reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008917 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
8918 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
8919 reply_len = -1;
8920 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
8921 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
8922 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
8923 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
8924 reply_size);
8925 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
8926 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
8927 reply_len = -1;
8928 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
8929 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
8930 reply_len = -1;
8931 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
8932 wpas_p2p_sd_service_update(wpa_s);
8933 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
8934 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
8935 reply_len = -1;
8936 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
8937 wpas_p2p_service_flush(wpa_s);
8938 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
8939 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
8940 reply_len = -1;
8941 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
8942 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
8943 reply_len = -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008944 } else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
8945 if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
8946 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008947 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
8948 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
8949 reply_len = -1;
8950 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
8951 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
8952 reply_len = -1;
8953 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
8954 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
8955 reply_size);
8956 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
8957 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
8958 reply_len = -1;
8959 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07008960 p2p_ctrl_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008961 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
8962 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
8963 reply_len = -1;
8964 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
8965 if (wpas_p2p_cancel(wpa_s))
8966 reply_len = -1;
8967 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
8968 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
8969 reply_len = -1;
8970 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
8971 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
8972 reply_len = -1;
8973 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
8974 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
8975 reply_len = -1;
8976 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
8977 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
8978 reply_len = -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008979 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
8980 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
8981 reply_len = -1;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07008982 } else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
8983 if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
8984 reply_len = -1;
8985 } else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
8986 if (wpas_p2p_lo_stop(wpa_s))
8987 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008988#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008989#ifdef CONFIG_WIFI_DISPLAY
8990 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
8991 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
8992 reply_len = -1;
8993 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
8994 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
8995 reply, reply_size);
8996#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008997#ifdef CONFIG_INTERWORKING
8998 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
8999 if (interworking_fetch_anqp(wpa_s) < 0)
9000 reply_len = -1;
9001 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
9002 interworking_stop_fetch_anqp(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009003 } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
9004 if (ctrl_interworking_select(wpa_s, NULL) < 0)
9005 reply_len = -1;
9006 } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
9007 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009008 reply_len = -1;
9009 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009010 if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009011 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009012 } else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
9013 int id;
9014
9015 id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
9016 if (id < 0)
9017 reply_len = -1;
9018 else {
9019 reply_len = os_snprintf(reply, reply_size, "%d\n", id);
9020 if (os_snprintf_error(reply_size, reply_len))
9021 reply_len = -1;
9022 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009023 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
9024 if (get_anqp(wpa_s, buf + 9) < 0)
9025 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009026 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
9027 if (gas_request(wpa_s, buf + 12) < 0)
9028 reply_len = -1;
9029 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
9030 reply_len = gas_response_get(wpa_s, buf + 17, reply,
9031 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009032#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07009033#ifdef CONFIG_HS20
9034 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
9035 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
9036 reply_len = -1;
9037 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
9038 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
9039 reply_len = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08009040 } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08009041 if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
9042 reply_len = -1;
9043 } else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
9044 if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
9045 reply_len = -1;
9046 } else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
9047 reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
9048 } else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
9049 if (del_hs20_icon(wpa_s, buf + 14) < 0)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08009050 reply_len = -1;
9051 } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009052 if (hs20_fetch_osu(wpa_s, 0) < 0)
9053 reply_len = -1;
9054 } else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
9055 if (hs20_fetch_osu(wpa_s, 1) < 0)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08009056 reply_len = -1;
9057 } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
9058 hs20_cancel_fetch_osu(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07009059#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009060 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
9061 {
9062 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
9063 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
9064 reply_len = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07009065 else {
9066 /*
9067 * Notify response from timeout to allow the control
9068 * interface response to be sent first.
9069 */
9070 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
9071 wpa_s, NULL);
9072 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009073 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
9074 if (wpa_supplicant_reload_configuration(wpa_s))
9075 reply_len = -1;
9076 } else if (os_strcmp(buf, "TERMINATE") == 0) {
9077 wpa_supplicant_terminate_proc(wpa_s->global);
9078 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
9079 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
9080 reply_len = -1;
Dmitry Shmidte19501d2011-03-16 14:32:18 -07009081 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009082 reply_len = wpa_supplicant_ctrl_iface_blacklist(
9083 wpa_s, buf + 9, reply, reply_size);
9084 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
9085 reply_len = wpa_supplicant_ctrl_iface_log_level(
9086 wpa_s, buf + 9, reply, reply_size);
Vinit Deshpandeda134e92014-12-02 10:59:29 -08009087 } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
9088 reply_len = wpa_supplicant_ctrl_iface_list_networks(
9089 wpa_s, buf + 14, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009090 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
9091 reply_len = wpa_supplicant_ctrl_iface_list_networks(
Vinit Deshpandeda134e92014-12-02 10:59:29 -08009092 wpa_s, NULL, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009093 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07009094 wpas_request_disconnection(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009095 } else if (os_strcmp(buf, "SCAN") == 0) {
9096 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
9097 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
9098 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009099 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
9100 reply_len = wpa_supplicant_ctrl_iface_scan_results(
9101 wpa_s, reply, reply_size);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009102 } else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
9103 if (wpas_abort_ongoing_scan(wpa_s) < 0)
9104 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009105 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
9106 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
9107 reply_len = -1;
9108 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
9109 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
9110 reply_len = -1;
9111 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
9112 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
9113 reply_len = -1;
9114 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
9115 reply_len = wpa_supplicant_ctrl_iface_add_network(
9116 wpa_s, reply, reply_size);
9117 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
9118 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
9119 reply_len = -1;
9120 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
9121 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
9122 reply_len = -1;
9123 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
9124 reply_len = wpa_supplicant_ctrl_iface_get_network(
9125 wpa_s, buf + 12, reply, reply_size);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07009126 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009127 if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
9128 wpa_s))
Dmitry Shmidt684785c2014-05-12 13:34:29 -07009129 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009130 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
9131 reply_len = wpa_supplicant_ctrl_iface_list_creds(
9132 wpa_s, reply, reply_size);
9133 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
9134 reply_len = wpa_supplicant_ctrl_iface_add_cred(
9135 wpa_s, reply, reply_size);
9136 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
9137 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
9138 reply_len = -1;
9139 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
9140 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
9141 reply_len = -1;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07009142 } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
9143 reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
9144 reply,
9145 reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009146#ifndef CONFIG_NO_CONFIG_WRITE
9147 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
9148 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
9149 reply_len = -1;
9150#endif /* CONFIG_NO_CONFIG_WRITE */
9151 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
9152 reply_len = wpa_supplicant_ctrl_iface_get_capability(
9153 wpa_s, buf + 15, reply, reply_size);
9154 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
9155 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
9156 reply_len = -1;
9157 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
9158 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
9159 reply_len = -1;
9160 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
9161 reply_len = wpa_supplicant_global_iface_list(
9162 wpa_s->global, reply, reply_size);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08009163 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009164 reply_len = wpa_supplicant_global_iface_interfaces(
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08009165 wpa_s->global, buf + 10, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009166 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
9167 reply_len = wpa_supplicant_ctrl_iface_bss(
9168 wpa_s, buf + 4, reply, reply_size);
9169#ifdef CONFIG_AP
9170 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
9171 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
9172 } else if (os_strncmp(buf, "STA ", 4) == 0) {
9173 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
9174 reply_size);
9175 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
9176 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
9177 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07009178 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
9179 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
9180 reply_len = -1;
9181 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
9182 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
9183 reply_len = -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009184 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
9185 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
9186 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009187 } else if (os_strcmp(buf, "STOP_AP") == 0) {
9188 if (wpas_ap_stop_ap(wpa_s))
9189 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009190#endif /* CONFIG_AP */
9191 } else if (os_strcmp(buf, "SUSPEND") == 0) {
9192 wpas_notify_suspend(wpa_s->global);
9193 } else if (os_strcmp(buf, "RESUME") == 0) {
9194 wpas_notify_resume(wpa_s->global);
Dmitry Shmidt21de2142014-04-08 10:50:52 -07009195#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009196 } else if (os_strcmp(buf, "DROP_SA") == 0) {
9197 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
Dmitry Shmidt21de2142014-04-08 10:50:52 -07009198#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009199 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
9200 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
9201 reply_len = -1;
9202 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009203 wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009204 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
9205 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
9206 reply_len = -1;
9207 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
9208 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
9209 buf + 17))
9210 reply_len = -1;
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07009211 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009212 wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009213#ifdef CONFIG_TDLS
9214 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
9215 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
9216 reply_len = -1;
9217 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
9218 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
9219 reply_len = -1;
9220 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
9221 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
9222 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009223 } else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
9224 if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
9225 buf + 17))
9226 reply_len = -1;
9227 } else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
9228 if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
9229 buf + 24))
9230 reply_len = -1;
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07009231 } else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
9232 reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
9233 wpa_s, buf + 17, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009234#endif /* CONFIG_TDLS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009235 } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
9236 reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
9237 } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
9238 if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
9239 reply_len = -1;
9240 } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
9241 if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
9242 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009243 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
9244 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
9245 reply_size);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08009246 } else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
9247 if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
9248 reply_len = -1;
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07009249 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
9250 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
9251 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07009252#ifdef CONFIG_AUTOSCAN
9253 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
9254 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
9255 reply_len = -1;
9256#endif /* CONFIG_AUTOSCAN */
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009257 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
9258 reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
9259 reply_size);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009260#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07009261 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
9262 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
9263 reply_size);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08009264#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009265 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
9266 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
9267 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009268 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08009269 pmksa_cache_clear_current(wpa_s->wpa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009270 eapol_sm_request_reauth(wpa_s->eapol);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08009271#ifdef CONFIG_WNM
9272 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
9273 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
9274 reply_len = -1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08009275 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
9276 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
Dmitry Shmidt44c95782013-05-17 09:51:35 -07009277 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08009278#endif /* CONFIG_WNM */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07009279 } else if (os_strcmp(buf, "FLUSH") == 0) {
9280 wpa_supplicant_ctrl_iface_flush(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009281 } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
9282 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
9283 reply_size);
Dmitry Shmidt818ea482014-03-10 13:15:21 -07009284#ifdef CONFIG_TESTING_OPTIONS
9285 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
9286 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
9287 reply_len = -1;
9288 } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
9289 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009290 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
9291 if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
9292 reply_len = -1;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07009293 } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
9294 if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
9295 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009296 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
9297 if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
9298 reply_len = -1;
9299 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
9300 if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
9301 reply_len = -1;
9302 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
9303 if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
9304 reply_len = -1;
9305 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
9306 if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
9307 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08009308 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
9309 if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
9310 reply_len = -1;
9311 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
9312 reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009313 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
9314 if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
9315 reply_len = -1;
9316 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
9317 reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
Jouni Malinenc4818362015-10-04 11:45:13 +03009318 } else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
9319 if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
9320 reply_len = -1;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08009321 } else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
9322 if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
9323 reply_len = -1;
Dmitry Shmidt818ea482014-03-10 13:15:21 -07009324#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07009325 } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
9326 if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
9327 reply_len = -1;
9328 } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
9329 reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
9330 reply_size);
9331 } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
9332 if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
9333 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009334 } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009335 if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009336 reply_len = -1;
9337 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
9338 wpas_ctrl_iface_erp_flush(wpa_s);
9339 } else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
9340 if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
9341 reply_len = -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009342 } else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
9343 reply_len = wpas_ctrl_iface_get_pref_freq_list(
9344 wpa_s, buf + 19, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009345 } else {
9346 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
9347 reply_len = 16;
9348 }
9349
9350 if (reply_len < 0) {
9351 os_memcpy(reply, "FAIL\n", 5);
9352 reply_len = 5;
9353 }
9354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009355 *resp_len = reply_len;
9356 return reply;
9357}
9358
9359
9360static int wpa_supplicant_global_iface_add(struct wpa_global *global,
9361 char *cmd)
9362{
9363 struct wpa_interface iface;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07009364 char *pos, *extra;
9365 struct wpa_supplicant *wpa_s;
9366 unsigned int create_iface = 0;
9367 u8 mac_addr[ETH_ALEN];
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009368 enum wpa_driver_if_type type = WPA_IF_STATION;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009369
9370 /*
9371 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009372 * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009373 */
9374 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
9375
9376 os_memset(&iface, 0, sizeof(iface));
9377
9378 do {
9379 iface.ifname = pos = cmd;
9380 pos = os_strchr(pos, '\t');
9381 if (pos)
9382 *pos++ = '\0';
9383 if (iface.ifname[0] == '\0')
9384 return -1;
9385 if (pos == NULL)
9386 break;
9387
9388 iface.confname = pos;
9389 pos = os_strchr(pos, '\t');
9390 if (pos)
9391 *pos++ = '\0';
9392 if (iface.confname[0] == '\0')
9393 iface.confname = NULL;
9394 if (pos == NULL)
9395 break;
9396
9397 iface.driver = pos;
9398 pos = os_strchr(pos, '\t');
9399 if (pos)
9400 *pos++ = '\0';
9401 if (iface.driver[0] == '\0')
9402 iface.driver = NULL;
9403 if (pos == NULL)
9404 break;
9405
9406 iface.ctrl_interface = pos;
9407 pos = os_strchr(pos, '\t');
9408 if (pos)
9409 *pos++ = '\0';
9410 if (iface.ctrl_interface[0] == '\0')
9411 iface.ctrl_interface = NULL;
9412 if (pos == NULL)
9413 break;
9414
9415 iface.driver_param = pos;
9416 pos = os_strchr(pos, '\t');
9417 if (pos)
9418 *pos++ = '\0';
9419 if (iface.driver_param[0] == '\0')
9420 iface.driver_param = NULL;
9421 if (pos == NULL)
9422 break;
9423
9424 iface.bridge_ifname = pos;
9425 pos = os_strchr(pos, '\t');
9426 if (pos)
9427 *pos++ = '\0';
9428 if (iface.bridge_ifname[0] == '\0')
9429 iface.bridge_ifname = NULL;
9430 if (pos == NULL)
9431 break;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07009432
9433 extra = pos;
9434 pos = os_strchr(pos, '\t');
9435 if (pos)
9436 *pos++ = '\0';
Dmitry Shmidt83474442015-04-15 13:47:09 -07009437 if (!extra[0])
9438 break;
9439
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009440 if (os_strcmp(extra, "create") == 0) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07009441 create_iface = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009442 if (!pos)
9443 break;
9444
9445 if (os_strcmp(pos, "sta") == 0) {
9446 type = WPA_IF_STATION;
9447 } else if (os_strcmp(pos, "ap") == 0) {
9448 type = WPA_IF_AP_BSS;
9449 } else {
9450 wpa_printf(MSG_DEBUG,
9451 "INTERFACE_ADD unsupported interface type: '%s'",
9452 pos);
9453 return -1;
9454 }
9455 } else {
Dmitry Shmidt83474442015-04-15 13:47:09 -07009456 wpa_printf(MSG_DEBUG,
9457 "INTERFACE_ADD unsupported extra parameter: '%s'",
9458 extra);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07009459 return -1;
Dmitry Shmidt83474442015-04-15 13:47:09 -07009460 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009461 } while (0);
9462
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07009463 if (create_iface) {
9464 wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
9465 iface.ifname);
9466 if (!global->ifaces)
9467 return -1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009468 if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07009469 NULL, NULL, NULL, mac_addr, NULL) < 0) {
9470 wpa_printf(MSG_ERROR,
9471 "CTRL_IFACE interface creation failed");
9472 return -1;
9473 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009474
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07009475 wpa_printf(MSG_DEBUG,
9476 "CTRL_IFACE interface '%s' created with MAC addr: "
9477 MACSTR, iface.ifname, MAC2STR(mac_addr));
9478 }
9479
9480 if (wpa_supplicant_get_iface(global, iface.ifname))
9481 goto fail;
9482
9483 wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
9484 if (!wpa_s)
9485 goto fail;
9486 wpa_s->added_vif = create_iface;
9487 return 0;
9488
9489fail:
9490 if (create_iface)
9491 wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
9492 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009493}
9494
9495
9496static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
9497 char *cmd)
9498{
9499 struct wpa_supplicant *wpa_s;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07009500 int ret;
9501 unsigned int delete_iface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009502
9503 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
9504
9505 wpa_s = wpa_supplicant_get_iface(global, cmd);
9506 if (wpa_s == NULL)
9507 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07009508 delete_iface = wpa_s->added_vif;
9509 ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
9510 if (!ret && delete_iface) {
9511 wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
9512 cmd);
9513 ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
9514 }
9515 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009516}
9517
9518
9519static void wpa_free_iface_info(struct wpa_interface_info *iface)
9520{
9521 struct wpa_interface_info *prev;
9522
9523 while (iface) {
9524 prev = iface;
9525 iface = iface->next;
9526
9527 os_free(prev->ifname);
9528 os_free(prev->desc);
9529 os_free(prev);
9530 }
9531}
9532
9533
9534static int wpa_supplicant_global_iface_list(struct wpa_global *global,
9535 char *buf, int len)
9536{
9537 int i, res;
9538 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
9539 char *pos, *end;
9540
9541 for (i = 0; wpa_drivers[i]; i++) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07009542 const struct wpa_driver_ops *drv = wpa_drivers[i];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009543 if (drv->get_interfaces == NULL)
9544 continue;
9545 tmp = drv->get_interfaces(global->drv_priv[i]);
9546 if (tmp == NULL)
9547 continue;
9548
9549 if (last == NULL)
9550 iface = last = tmp;
9551 else
9552 last->next = tmp;
9553 while (last->next)
9554 last = last->next;
9555 }
9556
9557 pos = buf;
9558 end = buf + len;
9559 for (tmp = iface; tmp; tmp = tmp->next) {
9560 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
9561 tmp->drv_name, tmp->ifname,
9562 tmp->desc ? tmp->desc : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009563 if (os_snprintf_error(end - pos, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009564 *pos = '\0';
9565 break;
9566 }
9567 pos += res;
9568 }
9569
9570 wpa_free_iface_info(iface);
9571
9572 return pos - buf;
9573}
9574
9575
9576static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08009577 const char *input,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009578 char *buf, int len)
9579{
9580 int res;
9581 char *pos, *end;
9582 struct wpa_supplicant *wpa_s;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08009583 int show_ctrl = 0;
9584
9585 if (input)
9586 show_ctrl = !!os_strstr(input, "ctrl");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009587
9588 wpa_s = global->ifaces;
9589 pos = buf;
9590 end = buf + len;
9591
9592 while (wpa_s) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08009593 if (show_ctrl)
9594 res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
9595 wpa_s->ifname,
9596 wpa_s->conf->ctrl_interface ?
9597 wpa_s->conf->ctrl_interface : "N/A");
9598 else
9599 res = os_snprintf(pos, end - pos, "%s\n",
9600 wpa_s->ifname);
9601
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009602 if (os_snprintf_error(end - pos, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009603 *pos = '\0';
9604 break;
9605 }
9606 pos += res;
9607 wpa_s = wpa_s->next;
9608 }
9609 return pos - buf;
9610}
9611
9612
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07009613static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
9614 const char *ifname,
9615 char *cmd, size_t *resp_len)
9616{
9617 struct wpa_supplicant *wpa_s;
9618
9619 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9620 if (os_strcmp(ifname, wpa_s->ifname) == 0)
9621 break;
9622 }
9623
9624 if (wpa_s == NULL) {
9625 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
9626 if (resp)
9627 *resp_len = os_strlen(resp);
9628 else
9629 *resp_len = 1;
9630 return resp;
9631 }
9632
9633 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
9634}
9635
9636
9637static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
9638 char *buf, size_t *resp_len)
9639{
9640#ifdef CONFIG_P2P
9641 static const char * cmd[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07009642 "LIST_NETWORKS",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07009643 "P2P_FIND",
9644 "P2P_STOP_FIND",
9645 "P2P_LISTEN",
9646 "P2P_GROUP_ADD",
9647 "P2P_GET_PASSPHRASE",
9648 "P2P_SERVICE_UPDATE",
9649 "P2P_SERVICE_FLUSH",
9650 "P2P_FLUSH",
9651 "P2P_CANCEL",
9652 "P2P_PRESENCE_REQ",
9653 "P2P_EXT_LISTEN",
9654 NULL
9655 };
9656 static const char * prefix[] = {
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08009657#ifdef ANDROID
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07009658 "DRIVER ",
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08009659#endif /* ANDROID */
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07009660 "GET_NETWORK ",
9661 "REMOVE_NETWORK ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07009662 "P2P_FIND ",
9663 "P2P_CONNECT ",
9664 "P2P_LISTEN ",
9665 "P2P_GROUP_REMOVE ",
9666 "P2P_GROUP_ADD ",
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009667 "P2P_GROUP_MEMBER ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07009668 "P2P_PROV_DISC ",
9669 "P2P_SERV_DISC_REQ ",
9670 "P2P_SERV_DISC_CANCEL_REQ ",
9671 "P2P_SERV_DISC_RESP ",
9672 "P2P_SERV_DISC_EXTERNAL ",
9673 "P2P_SERVICE_ADD ",
9674 "P2P_SERVICE_DEL ",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009675 "P2P_SERVICE_REP ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07009676 "P2P_REJECT ",
9677 "P2P_INVITE ",
9678 "P2P_PEER ",
9679 "P2P_SET ",
9680 "P2P_UNAUTHORIZE ",
9681 "P2P_PRESENCE_REQ ",
9682 "P2P_EXT_LISTEN ",
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009683 "P2P_REMOVE_CLIENT ",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08009684 "WPS_NFC_TOKEN ",
9685 "WPS_NFC_TAG_READ ",
Dmitry Shmidt413dde72014-04-11 10:23:22 -07009686 "NFC_GET_HANDOVER_SEL ",
9687 "NFC_GET_HANDOVER_REQ ",
9688 "NFC_REPORT_HANDOVER ",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009689 "P2P_ASP_PROVISION ",
9690 "P2P_ASP_PROVISION_RESP ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07009691 NULL
9692 };
9693 int found = 0;
9694 int i;
9695
9696 if (global->p2p_init_wpa_s == NULL)
9697 return NULL;
9698
9699 for (i = 0; !found && cmd[i]; i++) {
9700 if (os_strcmp(buf, cmd[i]) == 0)
9701 found = 1;
9702 }
9703
9704 for (i = 0; !found && prefix[i]; i++) {
9705 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
9706 found = 1;
9707 }
9708
9709 if (found)
9710 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
9711 buf, resp_len);
9712#endif /* CONFIG_P2P */
9713 return NULL;
9714}
9715
9716
9717static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
9718 char *buf, size_t *resp_len)
9719{
9720#ifdef CONFIG_WIFI_DISPLAY
9721 if (global->p2p_init_wpa_s == NULL)
9722 return NULL;
9723 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
9724 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
9725 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
9726 buf, resp_len);
9727#endif /* CONFIG_WIFI_DISPLAY */
9728 return NULL;
9729}
9730
9731
9732static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
9733 char *buf, size_t *resp_len)
9734{
9735 char *ret;
9736
9737 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
9738 if (ret)
9739 return ret;
9740
9741 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
9742 if (ret)
9743 return ret;
9744
9745 return NULL;
9746}
9747
9748
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009749static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
9750{
9751 char *value;
9752
9753 value = os_strchr(cmd, ' ');
9754 if (value == NULL)
9755 return -1;
9756 *value++ = '\0';
9757
9758 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
9759
9760#ifdef CONFIG_WIFI_DISPLAY
9761 if (os_strcasecmp(cmd, "wifi_display") == 0) {
9762 wifi_display_enable(global, !!atoi(value));
9763 return 0;
9764 }
9765#endif /* CONFIG_WIFI_DISPLAY */
9766
Dmitry Shmidt61593f02014-04-21 16:27:35 -07009767 /* Restore cmd to its original value to allow redirection */
9768 value[-1] = ' ';
9769
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009770 return -1;
9771}
9772
9773
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009774static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
9775 char *cmd)
9776{
9777 struct wpa_supplicant *wpa_s[2]; /* src, dst */
9778 char *p;
9779 unsigned int i;
9780
9781 /* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
9782 * <variable name> */
9783
9784 for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
9785 p = os_strchr(cmd, ' ');
9786 if (p == NULL)
9787 return -1;
9788 *p = '\0';
9789
9790 wpa_s[i] = global->ifaces;
9791 for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
9792 if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
9793 break;
9794 }
9795
9796 if (!wpa_s[i]) {
9797 wpa_printf(MSG_DEBUG,
9798 "CTRL_IFACE: Could not find iface=%s", cmd);
9799 return -1;
9800 }
9801
9802 cmd = p + 1;
9803 }
9804
9805 return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
9806}
9807
9808
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009809#ifndef CONFIG_NO_CONFIG_WRITE
9810static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
9811{
Dmitry Shmidt61593f02014-04-21 16:27:35 -07009812 int ret = 0, saved = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009813 struct wpa_supplicant *wpa_s;
9814
9815 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9816 if (!wpa_s->conf->update_config) {
9817 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
9818 continue;
9819 }
9820
9821 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
9822 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
9823 ret = 1;
9824 } else {
9825 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
Dmitry Shmidt61593f02014-04-21 16:27:35 -07009826 saved++;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009827 }
9828 }
9829
Dmitry Shmidt61593f02014-04-21 16:27:35 -07009830 if (!saved && !ret) {
9831 wpa_dbg(wpa_s, MSG_DEBUG,
9832 "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
9833 ret = 1;
9834 }
9835
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009836 return ret;
9837}
9838#endif /* CONFIG_NO_CONFIG_WRITE */
9839
9840
9841static int wpas_global_ctrl_iface_status(struct wpa_global *global,
9842 char *buf, size_t buflen)
9843{
9844 char *pos, *end;
9845 int ret;
9846 struct wpa_supplicant *wpa_s;
9847
9848 pos = buf;
9849 end = buf + buflen;
9850
9851#ifdef CONFIG_P2P
9852 if (global->p2p && !global->p2p_disabled) {
9853 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
9854 "\n"
9855 "p2p_state=%s\n",
9856 MAC2STR(global->p2p_dev_addr),
9857 p2p_get_state_txt(global->p2p));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009858 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009859 return pos - buf;
9860 pos += ret;
9861 } else if (global->p2p) {
9862 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009863 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009864 return pos - buf;
9865 pos += ret;
9866 }
9867#endif /* CONFIG_P2P */
9868
9869#ifdef CONFIG_WIFI_DISPLAY
9870 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
9871 !!global->wifi_display);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009872 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009873 return pos - buf;
9874 pos += ret;
9875#endif /* CONFIG_WIFI_DISPLAY */
9876
9877 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9878 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
9879 "address=" MACSTR "\n",
9880 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009881 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009882 return pos - buf;
9883 pos += ret;
9884 }
9885
9886 return pos - buf;
9887}
9888
9889
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009890#ifdef CONFIG_FST
9891
9892static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
9893 char *cmd, char *buf,
9894 size_t reply_size)
9895{
9896 char ifname[IFNAMSIZ + 1];
9897 struct fst_iface_cfg cfg;
9898 struct wpa_supplicant *wpa_s;
9899 struct fst_wpa_obj iface_obj;
9900
9901 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
9902 wpa_s = wpa_supplicant_get_iface(global, ifname);
9903 if (wpa_s) {
9904 if (wpa_s->fst) {
9905 wpa_printf(MSG_INFO, "FST: Already attached");
9906 return -1;
9907 }
9908 fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
9909 wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
9910 &iface_obj, &cfg);
9911 if (wpa_s->fst)
9912 return os_snprintf(buf, reply_size, "OK\n");
9913 }
9914 }
9915
9916 return -1;
9917}
9918
9919
9920static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
9921 char *cmd, char *buf,
9922 size_t reply_size)
9923{
9924 char ifname[IFNAMSIZ + 1];
9925 struct wpa_supplicant *wpa_s;
9926
9927 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
9928 wpa_s = wpa_supplicant_get_iface(global, ifname);
9929 if (wpa_s) {
9930 if (!fst_iface_detach(ifname)) {
9931 wpa_s->fst = NULL;
9932 return os_snprintf(buf, reply_size, "OK\n");
9933 }
9934 }
9935 }
9936
9937 return -1;
9938}
9939
9940#endif /* CONFIG_FST */
9941
9942
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009943char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
9944 char *buf, size_t *resp_len)
9945{
9946 char *reply;
9947 const int reply_size = 2048;
9948 int reply_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009949 int level = MSG_DEBUG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009950
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07009951 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
9952 char *pos = os_strchr(buf + 7, ' ');
9953 if (pos) {
9954 *pos++ = '\0';
9955 return wpas_global_ctrl_iface_ifname(global,
9956 buf + 7, pos,
9957 resp_len);
9958 }
9959 }
9960
9961 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
9962 if (reply)
9963 return reply;
9964
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009965 if (os_strcmp(buf, "PING") == 0)
9966 level = MSG_EXCESSIVE;
9967 wpa_hexdump_ascii(level, "RX global ctrl_iface",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009968 (const u8 *) buf, os_strlen(buf));
9969
9970 reply = os_malloc(reply_size);
9971 if (reply == NULL) {
9972 *resp_len = 1;
9973 return NULL;
9974 }
9975
9976 os_memcpy(reply, "OK\n", 3);
9977 reply_len = 3;
9978
9979 if (os_strcmp(buf, "PING") == 0) {
9980 os_memcpy(reply, "PONG\n", 5);
9981 reply_len = 5;
9982 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
9983 if (wpa_supplicant_global_iface_add(global, buf + 14))
9984 reply_len = -1;
9985 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
9986 if (wpa_supplicant_global_iface_remove(global, buf + 17))
9987 reply_len = -1;
9988 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
9989 reply_len = wpa_supplicant_global_iface_list(
9990 global, reply, reply_size);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08009991 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009992 reply_len = wpa_supplicant_global_iface_interfaces(
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08009993 global, buf + 10, reply, reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009994#ifdef CONFIG_FST
9995 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
9996 reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
9997 reply,
9998 reply_size);
9999 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
10000 reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
10001 reply,
10002 reply_size);
10003 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
10004 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
10005#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010006 } else if (os_strcmp(buf, "TERMINATE") == 0) {
10007 wpa_supplicant_terminate_proc(global);
10008 } else if (os_strcmp(buf, "SUSPEND") == 0) {
10009 wpas_notify_suspend(global);
10010 } else if (os_strcmp(buf, "RESUME") == 0) {
10011 wpas_notify_resume(global);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010012 } else if (os_strncmp(buf, "SET ", 4) == 0) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -070010013 if (wpas_global_ctrl_iface_set(global, buf + 4)) {
10014#ifdef CONFIG_P2P
10015 if (global->p2p_init_wpa_s) {
10016 os_free(reply);
10017 /* Check if P2P redirection would work for this
10018 * command. */
10019 return wpa_supplicant_ctrl_iface_process(
10020 global->p2p_init_wpa_s,
10021 buf, resp_len);
10022 }
10023#endif /* CONFIG_P2P */
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010024 reply_len = -1;
Dmitry Shmidt61593f02014-04-21 16:27:35 -070010025 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010026 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
10027 if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
10028 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010029#ifndef CONFIG_NO_CONFIG_WRITE
10030 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
10031 if (wpas_global_ctrl_iface_save_config(global))
10032 reply_len = -1;
10033#endif /* CONFIG_NO_CONFIG_WRITE */
10034 } else if (os_strcmp(buf, "STATUS") == 0) {
10035 reply_len = wpas_global_ctrl_iface_status(global, reply,
10036 reply_size);
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -080010037#ifdef CONFIG_MODULE_TESTS
10038 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -080010039 if (wpas_module_tests() < 0)
10040 reply_len = -1;
10041#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010042 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
10043 if (wpa_debug_reopen_file() < 0)
10044 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010045 } else {
10046 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
10047 reply_len = 16;
10048 }
10049
10050 if (reply_len < 0) {
10051 os_memcpy(reply, "FAIL\n", 5);
10052 reply_len = 5;
10053 }
10054
10055 *resp_len = reply_len;
10056 return reply;
10057}