blob: f612b494db2058d29fabea7a1f92ebb13ac8fd16 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Control interface (shared code for all backends)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080013#include "utils/uuid.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "common/version.h"
15#include "common/ieee802_11_defs.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070016#include "common/ieee802_11_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "common/wpa_ctrl.h"
18#include "eap_peer/eap.h"
19#include "eapol_supp/eapol_supp_sm.h"
20#include "rsn_supp/wpa.h"
21#include "rsn_supp/preauth.h"
22#include "rsn_supp/pmksa_cache.h"
23#include "l2_packet/l2_packet.h"
24#include "wps/wps.h"
25#include "config.h"
26#include "wpa_supplicant_i.h"
27#include "driver_i.h"
28#include "wps_supplicant.h"
29#include "ibss_rsn.h"
30#include "ap.h"
31#include "p2p_supplicant.h"
32#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070033#include "hs20_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070034#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "notify.h"
36#include "bss.h"
37#include "scan.h"
38#include "ctrl_iface.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080039#include "interworking.h"
Dmitry Shmidte19501d2011-03-16 14:32:18 -070040#include "blacklist.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070041#include "autoscan.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080042#include "wnm_sta.h"
Dmitry Shmidt818ea482014-03-10 13:15:21 -070043#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045static int wpa_supplicant_global_iface_list(struct wpa_global *global,
46 char *buf, int len);
47static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
48 char *buf, int len);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -070049static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
50 char *val);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051
Dmitry Shmidt04949592012-07-19 12:16:46 -070052static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
53{
54 char *pos;
55 u8 addr[ETH_ALEN], *filter = NULL, *n;
56 size_t count = 0;
57
58 pos = val;
59 while (pos) {
60 if (*pos == '\0')
61 break;
62 if (hwaddr_aton(pos, addr)) {
63 os_free(filter);
64 return -1;
65 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070066 n = os_realloc_array(filter, count + 1, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -070067 if (n == NULL) {
68 os_free(filter);
69 return -1;
70 }
71 filter = n;
72 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
73 count++;
74
75 pos = os_strchr(pos, ' ');
76 if (pos)
77 pos++;
78 }
79
80 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
81 os_free(wpa_s->bssid_filter);
82 wpa_s->bssid_filter = filter;
83 wpa_s->bssid_filter_count = count;
84
85 return 0;
86}
87
88
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080089static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
90{
91 char *pos;
92 u8 addr[ETH_ALEN], *bssid = NULL, *n;
93 struct wpa_ssid_value *ssid = NULL, *ns;
94 size_t count = 0, ssid_count = 0;
95 struct wpa_ssid *c;
96
97 /*
Dmitry Shmidtcce06662013-11-04 18:44:24 -080098 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080099 * SSID_SPEC ::= ssid <SSID_HEX>
100 * BSSID_SPEC ::= bssid <BSSID_HEX>
101 */
102
103 pos = val;
104 while (pos) {
105 if (*pos == '\0')
106 break;
107 if (os_strncmp(pos, "bssid ", 6) == 0) {
108 int res;
109 pos += 6;
110 res = hwaddr_aton2(pos, addr);
111 if (res < 0) {
112 os_free(ssid);
113 os_free(bssid);
114 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
115 "BSSID value '%s'", pos);
116 return -1;
117 }
118 pos += res;
119 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
120 if (n == NULL) {
121 os_free(ssid);
122 os_free(bssid);
123 return -1;
124 }
125 bssid = n;
126 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
127 count++;
128 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
129 char *end;
130 pos += 5;
131
132 end = pos;
133 while (*end) {
134 if (*end == '\0' || *end == ' ')
135 break;
136 end++;
137 }
138
139 ns = os_realloc_array(ssid, ssid_count + 1,
140 sizeof(struct wpa_ssid_value));
141 if (ns == NULL) {
142 os_free(ssid);
143 os_free(bssid);
144 return -1;
145 }
146 ssid = ns;
147
148 if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
149 hexstr2bin(pos, ssid[ssid_count].ssid,
150 (end - pos) / 2) < 0) {
151 os_free(ssid);
152 os_free(bssid);
153 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
154 "SSID value '%s'", pos);
155 return -1;
156 }
157 ssid[ssid_count].ssid_len = (end - pos) / 2;
158 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
159 ssid[ssid_count].ssid,
160 ssid[ssid_count].ssid_len);
161 ssid_count++;
162 pos = end;
163 } else {
164 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
165 "'%s'", pos);
166 os_free(ssid);
167 os_free(bssid);
168 return -1;
169 }
170
171 pos = os_strchr(pos, ' ');
172 if (pos)
173 pos++;
174 }
175
176 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
177 os_free(wpa_s->disallow_aps_bssid);
178 wpa_s->disallow_aps_bssid = bssid;
179 wpa_s->disallow_aps_bssid_count = count;
180
181 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
182 os_free(wpa_s->disallow_aps_ssid);
183 wpa_s->disallow_aps_ssid = ssid;
184 wpa_s->disallow_aps_ssid_count = ssid_count;
185
186 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
187 return 0;
188
189 c = wpa_s->current_ssid;
190 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
191 return 0;
192
193 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
194 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
195 return 0;
196
197 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
198 "because current AP was marked disallowed");
199
200#ifdef CONFIG_SME
201 wpa_s->sme.prev_bssid_set = 0;
202#endif /* CONFIG_SME */
203 wpa_s->reassociate = 1;
204 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
205 wpa_supplicant_req_scan(wpa_s, 0, 0);
206
207 return 0;
208}
209
210
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700211#ifndef CONFIG_NO_CONFIG_BLOBS
212static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
213{
214 char *name = pos;
215 struct wpa_config_blob *blob;
216 size_t len;
217
218 pos = os_strchr(pos, ' ');
219 if (pos == NULL)
220 return -1;
221 *pos++ = '\0';
222 len = os_strlen(pos);
223 if (len & 1)
224 return -1;
225
226 wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
227 blob = os_zalloc(sizeof(*blob));
228 if (blob == NULL)
229 return -1;
230 blob->name = os_strdup(name);
231 blob->data = os_malloc(len / 2);
232 if (blob->name == NULL || blob->data == NULL) {
233 wpa_config_free_blob(blob);
234 return -1;
235 }
236
237 if (hexstr2bin(pos, blob->data, len / 2) < 0) {
238 wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
239 wpa_config_free_blob(blob);
240 return -1;
241 }
242 blob->len = len / 2;
243
244 wpa_config_set_blob(wpa_s->conf, blob);
245
246 return 0;
247}
248#endif /* CONFIG_NO_CONFIG_BLOBS */
249
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700250
251static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
252{
253 char *params;
254 char *pos;
255 int *freqs = NULL;
256 int ret;
257
258 if (atoi(cmd)) {
259 params = os_strchr(cmd, ' ');
260 os_free(wpa_s->manual_sched_scan_freqs);
261 if (params) {
262 params++;
263 pos = os_strstr(params, "freq=");
264 if (pos)
265 freqs = freq_range_to_channel_list(wpa_s,
266 pos + 5);
267 }
268 wpa_s->manual_sched_scan_freqs = freqs;
269 ret = wpas_start_pno(wpa_s);
270 } else {
271 ret = wpas_stop_pno(wpa_s);
272 }
273 return ret;
274}
275
276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
278 char *cmd)
279{
280 char *value;
281 int ret = 0;
282
283 value = os_strchr(cmd, ' ');
284 if (value == NULL)
285 return -1;
286 *value++ = '\0';
287
288 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
289 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
290 eapol_sm_configure(wpa_s->eapol,
291 atoi(value), -1, -1, -1);
292 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
293 eapol_sm_configure(wpa_s->eapol,
294 -1, atoi(value), -1, -1);
295 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
296 eapol_sm_configure(wpa_s->eapol,
297 -1, -1, atoi(value), -1);
298 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
299 eapol_sm_configure(wpa_s->eapol,
300 -1, -1, -1, atoi(value));
301 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
302 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
303 atoi(value)))
304 ret = -1;
305 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
306 0) {
307 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
308 atoi(value)))
309 ret = -1;
310 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
311 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
312 ret = -1;
313 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
314 wpa_s->wps_fragment_size = atoi(value);
315#ifdef CONFIG_WPS_TESTING
316 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
317 long int val;
318 val = strtol(value, NULL, 0);
319 if (val < 0 || val > 0xff) {
320 ret = -1;
321 wpa_printf(MSG_DEBUG, "WPS: Invalid "
322 "wps_version_number %ld", val);
323 } else {
324 wps_version_number = val;
325 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
326 "version %u.%u",
327 (wps_version_number & 0xf0) >> 4,
328 wps_version_number & 0x0f);
329 }
330 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
331 wps_testing_dummy_cred = atoi(value);
332 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
333 wps_testing_dummy_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800334 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
335 wps_corrupt_pkhash = atoi(value);
336 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
337 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338#endif /* CONFIG_WPS_TESTING */
339 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
340 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
341 ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800342#ifdef CONFIG_TDLS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343#ifdef CONFIG_TDLS_TESTING
344 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
345 extern unsigned int tdls_testing;
346 tdls_testing = strtol(value, NULL, 0);
347 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
348#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
350 int disabled = atoi(value);
351 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
352 if (disabled) {
353 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
354 ret = -1;
355 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
356 ret = -1;
357 wpa_tdls_enable(wpa_s->wpa, !disabled);
358#endif /* CONFIG_TDLS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800359 } else if (os_strcasecmp(cmd, "pno") == 0) {
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700360 ret = wpas_ctrl_pno(wpa_s, value);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700361 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
362 int disabled = atoi(value);
363 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
364 ret = -1;
365 else if (disabled)
366 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
367 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
368 if (os_strcmp(value, "disable") == 0)
369 wpa_s->set_sta_uapsd = 0;
370 else {
371 int be, bk, vi, vo;
372 char *pos;
373 /* format: BE,BK,VI,VO;max SP Length */
374 be = atoi(value);
375 pos = os_strchr(value, ',');
376 if (pos == NULL)
377 return -1;
378 pos++;
379 bk = atoi(pos);
380 pos = os_strchr(pos, ',');
381 if (pos == NULL)
382 return -1;
383 pos++;
384 vi = atoi(pos);
385 pos = os_strchr(pos, ',');
386 if (pos == NULL)
387 return -1;
388 pos++;
389 vo = atoi(pos);
390 /* ignore max SP Length for now */
391
392 wpa_s->set_sta_uapsd = 1;
393 wpa_s->sta_uapsd = 0;
394 if (be)
395 wpa_s->sta_uapsd |= BIT(0);
396 if (bk)
397 wpa_s->sta_uapsd |= BIT(1);
398 if (vi)
399 wpa_s->sta_uapsd |= BIT(2);
400 if (vo)
401 wpa_s->sta_uapsd |= BIT(3);
402 }
Jouni Malinen21d6bc82012-04-10 16:17:59 -0700403 } else if (os_strcasecmp(cmd, "ps") == 0) {
404 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700405#ifdef CONFIG_WIFI_DISPLAY
406 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
Dmitry Shmidted003d22014-02-06 10:09:12 -0800407 int enabled = !!atoi(value);
408 if (enabled && !wpa_s->global->p2p)
409 ret = -1;
410 else
411 wifi_display_enable(wpa_s->global, enabled);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700412#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700413 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
414 ret = set_bssid_filter(wpa_s, value);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800415 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
416 ret = set_disallow_aps(wpa_s, value);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800417 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
418 wpa_s->no_keep_alive = !!atoi(value);
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700419#ifdef CONFIG_TESTING_OPTIONS
420 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
421 wpa_s->ext_mgmt_frame_handling = !!atoi(value);
422#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700423#ifndef CONFIG_NO_CONFIG_BLOBS
424 } else if (os_strcmp(cmd, "blob") == 0) {
425 ret = wpas_ctrl_set_blob(wpa_s, value);
426#endif /* CONFIG_NO_CONFIG_BLOBS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 } else {
428 value[-1] = '=';
429 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
430 if (ret == 0)
431 wpa_supplicant_update_config(wpa_s);
432 }
433
434 return ret;
435}
436
437
438static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
439 char *cmd, char *buf, size_t buflen)
440{
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700441 int res = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442
443 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
444
445 if (os_strcmp(cmd, "version") == 0) {
446 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700447 } else if (os_strcasecmp(cmd, "country") == 0) {
448 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
449 res = os_snprintf(buf, buflen, "%c%c",
450 wpa_s->conf->country[0],
451 wpa_s->conf->country[1]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700452#ifdef CONFIG_WIFI_DISPLAY
453 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
Dmitry Shmidted003d22014-02-06 10:09:12 -0800454 int enabled;
455 if (wpa_s->global->p2p == NULL ||
456 wpa_s->global->p2p_disabled)
457 enabled = 0;
458 else
459 enabled = wpa_s->global->wifi_display;
460 res = os_snprintf(buf, buflen, "%d", enabled);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700461 if (res < 0 || (unsigned int) res >= buflen)
462 return -1;
463 return res;
464#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700465#ifdef CONFIG_TESTING_GET_GTK
466 } else if (os_strcmp(cmd, "gtk") == 0) {
467 if (wpa_s->last_gtk_len == 0)
468 return -1;
469 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
470 wpa_s->last_gtk_len);
471 return res;
472#endif /* CONFIG_TESTING_GET_GTK */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473 }
474
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700475 if (res < 0 || (unsigned int) res >= buflen)
476 return -1;
477 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478}
479
480
481#ifdef IEEE8021X_EAPOL
482static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
483 char *addr)
484{
485 u8 bssid[ETH_ALEN];
486 struct wpa_ssid *ssid = wpa_s->current_ssid;
487
488 if (hwaddr_aton(addr, bssid)) {
489 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
490 "'%s'", addr);
491 return -1;
492 }
493
494 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
495 rsn_preauth_deinit(wpa_s->wpa);
496 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
497 return -1;
498
499 return 0;
500}
501#endif /* IEEE8021X_EAPOL */
502
503
504#ifdef CONFIG_PEERKEY
505/* MLME-STKSTART.request(peer) */
506static int wpa_supplicant_ctrl_iface_stkstart(
507 struct wpa_supplicant *wpa_s, char *addr)
508{
509 u8 peer[ETH_ALEN];
510
511 if (hwaddr_aton(addr, peer)) {
512 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
513 "address '%s'", addr);
514 return -1;
515 }
516
517 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
518 MAC2STR(peer));
519
520 return wpa_sm_stkstart(wpa_s->wpa, peer);
521}
522#endif /* CONFIG_PEERKEY */
523
524
525#ifdef CONFIG_TDLS
526
527static int wpa_supplicant_ctrl_iface_tdls_discover(
528 struct wpa_supplicant *wpa_s, char *addr)
529{
530 u8 peer[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800531 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532
533 if (hwaddr_aton(addr, peer)) {
534 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
535 "address '%s'", addr);
536 return -1;
537 }
538
539 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
540 MAC2STR(peer));
541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800542 if (wpa_tdls_is_external_setup(wpa_s->wpa))
543 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
544 else
545 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
546
547 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700548}
549
550
551static int wpa_supplicant_ctrl_iface_tdls_setup(
552 struct wpa_supplicant *wpa_s, char *addr)
553{
554 u8 peer[ETH_ALEN];
555 int ret;
556
557 if (hwaddr_aton(addr, peer)) {
558 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
559 "address '%s'", addr);
560 return -1;
561 }
562
563 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
564 MAC2STR(peer));
565
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800566 if ((wpa_s->conf->tdls_external_control) &&
567 wpa_tdls_is_external_setup(wpa_s->wpa))
568 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
569
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800570 wpa_tdls_remove(wpa_s->wpa, peer);
571
572 if (wpa_tdls_is_external_setup(wpa_s->wpa))
573 ret = wpa_tdls_start(wpa_s->wpa, peer);
574 else
575 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700577 return ret;
578}
579
580
581static int wpa_supplicant_ctrl_iface_tdls_teardown(
582 struct wpa_supplicant *wpa_s, char *addr)
583{
584 u8 peer[ETH_ALEN];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700585 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700587 if (os_strcmp(addr, "*") == 0) {
588 /* remove everyone */
589 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
590 wpa_tdls_teardown_peers(wpa_s->wpa);
591 return 0;
592 }
593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594 if (hwaddr_aton(addr, peer)) {
595 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
596 "address '%s'", addr);
597 return -1;
598 }
599
600 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
601 MAC2STR(peer));
602
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800603 if ((wpa_s->conf->tdls_external_control) &&
604 wpa_tdls_is_external_setup(wpa_s->wpa))
605 return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
606
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700607 if (wpa_tdls_is_external_setup(wpa_s->wpa))
608 ret = wpa_tdls_teardown_link(
609 wpa_s->wpa, peer,
610 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
611 else
612 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
613
614 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615}
616
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700617
618static int ctrl_iface_get_capability_tdls(
619 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
620{
621 int ret;
622
623 ret = os_snprintf(buf, buflen, "%s\n",
624 wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
625 (wpa_s->drv_flags &
626 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
627 "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
628 if (ret < 0 || (size_t) ret > buflen)
629 return -1;
630 return ret;
631}
632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633#endif /* CONFIG_TDLS */
634
635
636#ifdef CONFIG_IEEE80211R
637static int wpa_supplicant_ctrl_iface_ft_ds(
638 struct wpa_supplicant *wpa_s, char *addr)
639{
640 u8 target_ap[ETH_ALEN];
641 struct wpa_bss *bss;
642 const u8 *mdie;
643
644 if (hwaddr_aton(addr, target_ap)) {
645 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
646 "address '%s'", addr);
647 return -1;
648 }
649
650 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
651
652 bss = wpa_bss_get_bssid(wpa_s, target_ap);
653 if (bss)
654 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
655 else
656 mdie = NULL;
657
658 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
659}
660#endif /* CONFIG_IEEE80211R */
661
662
663#ifdef CONFIG_WPS
664static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
665 char *cmd)
666{
667 u8 bssid[ETH_ALEN], *_bssid = bssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700668#ifdef CONFIG_P2P
669 u8 p2p_dev_addr[ETH_ALEN];
670#endif /* CONFIG_P2P */
671#ifdef CONFIG_AP
672 u8 *_p2p_dev_addr = NULL;
673#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800674
Dmitry Shmidtc0a8db02013-11-08 11:18:33 -0800675 if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676 _bssid = NULL;
677#ifdef CONFIG_P2P
678 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
679 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
680 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
681 "P2P Device Address '%s'",
682 cmd + 13);
683 return -1;
684 }
685 _p2p_dev_addr = p2p_dev_addr;
686#endif /* CONFIG_P2P */
687 } else if (hwaddr_aton(cmd, bssid)) {
688 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
689 cmd);
690 return -1;
691 }
692
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800693#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700694 if (wpa_s->ap_iface)
695 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
696#endif /* CONFIG_AP */
697
698 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
699}
700
701
702static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
703 char *cmd, char *buf,
704 size_t buflen)
705{
706 u8 bssid[ETH_ALEN], *_bssid = bssid;
707 char *pin;
708 int ret;
709
710 pin = os_strchr(cmd, ' ');
711 if (pin)
712 *pin++ = '\0';
713
714 if (os_strcmp(cmd, "any") == 0)
715 _bssid = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800716 else if (os_strcmp(cmd, "get") == 0) {
717 ret = wps_generate_pin();
718 goto done;
719 } else if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700720 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
721 cmd);
722 return -1;
723 }
724
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800725#ifdef CONFIG_AP
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800726 if (wpa_s->ap_iface) {
727 int timeout = 0;
728 char *pos;
729
730 if (pin) {
731 pos = os_strchr(pin, ' ');
732 if (pos) {
733 *pos++ = '\0';
734 timeout = atoi(pos);
735 }
736 }
737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800739 buf, buflen, timeout);
740 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741#endif /* CONFIG_AP */
742
743 if (pin) {
744 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
745 DEV_PW_DEFAULT);
746 if (ret < 0)
747 return -1;
748 ret = os_snprintf(buf, buflen, "%s", pin);
749 if (ret < 0 || (size_t) ret >= buflen)
750 return -1;
751 return ret;
752 }
753
754 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
755 if (ret < 0)
756 return -1;
757
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800758done:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759 /* Return the generated PIN */
760 ret = os_snprintf(buf, buflen, "%08d", ret);
761 if (ret < 0 || (size_t) ret >= buflen)
762 return -1;
763 return ret;
764}
765
766
767static int wpa_supplicant_ctrl_iface_wps_check_pin(
768 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
769{
770 char pin[9];
771 size_t len;
772 char *pos;
773 int ret;
774
775 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
776 (u8 *) cmd, os_strlen(cmd));
777 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
778 if (*pos < '0' || *pos > '9')
779 continue;
780 pin[len++] = *pos;
781 if (len == 9) {
782 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
783 return -1;
784 }
785 }
786 if (len != 4 && len != 8) {
787 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
788 return -1;
789 }
790 pin[len] = '\0';
791
792 if (len == 8) {
793 unsigned int pin_val;
794 pin_val = atoi(pin);
795 if (!wps_pin_valid(pin_val)) {
796 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
797 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
798 if (ret < 0 || (size_t) ret >= buflen)
799 return -1;
800 return ret;
801 }
802 }
803
804 ret = os_snprintf(buf, buflen, "%s", pin);
805 if (ret < 0 || (size_t) ret >= buflen)
806 return -1;
807
808 return ret;
809}
810
811
Dmitry Shmidt04949592012-07-19 12:16:46 -0700812#ifdef CONFIG_WPS_NFC
813
814static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
815 char *cmd)
816{
817 u8 bssid[ETH_ALEN], *_bssid = bssid;
818
819 if (cmd == NULL || cmd[0] == '\0')
820 _bssid = NULL;
821 else if (hwaddr_aton(cmd, bssid))
822 return -1;
823
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800824 return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
825 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700826}
827
828
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800829static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
830 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
831{
832 int ndef;
833 struct wpabuf *buf;
834 int res;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700835 char *pos;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800836
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700837 pos = os_strchr(cmd, ' ');
838 if (pos)
839 *pos++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800840 if (os_strcmp(cmd, "WPS") == 0)
841 ndef = 0;
842 else if (os_strcmp(cmd, "NDEF") == 0)
843 ndef = 1;
844 else
845 return -1;
846
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700847 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800848 if (buf == NULL)
849 return -1;
850
851 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
852 wpabuf_len(buf));
853 reply[res++] = '\n';
854 reply[res] = '\0';
855
856 wpabuf_free(buf);
857
858 return res;
859}
860
861
Dmitry Shmidt04949592012-07-19 12:16:46 -0700862static int wpa_supplicant_ctrl_iface_wps_nfc_token(
863 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
864{
865 int ndef;
866 struct wpabuf *buf;
867 int res;
868
869 if (os_strcmp(cmd, "WPS") == 0)
870 ndef = 0;
871 else if (os_strcmp(cmd, "NDEF") == 0)
872 ndef = 1;
873 else
874 return -1;
875
876 buf = wpas_wps_nfc_token(wpa_s, ndef);
877 if (buf == NULL)
878 return -1;
879
880 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
881 wpabuf_len(buf));
882 reply[res++] = '\n';
883 reply[res] = '\0';
884
885 wpabuf_free(buf);
886
887 return res;
888}
889
890
891static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
892 struct wpa_supplicant *wpa_s, char *pos)
893{
894 size_t len;
895 struct wpabuf *buf;
896 int ret;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800897 char *freq;
898 int forced_freq = 0;
899
900 freq = strstr(pos, " freq=");
901 if (freq) {
902 *freq = '\0';
903 freq += 6;
904 forced_freq = atoi(freq);
905 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700906
907 len = os_strlen(pos);
908 if (len & 0x01)
909 return -1;
910 len /= 2;
911
912 buf = wpabuf_alloc(len);
913 if (buf == NULL)
914 return -1;
915 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
916 wpabuf_free(buf);
917 return -1;
918 }
919
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800920 ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700921 wpabuf_free(buf);
922
923 return ret;
924}
925
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800926
927static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800928 char *reply, size_t max_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800929 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800930{
931 struct wpabuf *buf;
932 int res;
933
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800934 buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800935 if (buf == NULL)
936 return -1;
937
938 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
939 wpabuf_len(buf));
940 reply[res++] = '\n';
941 reply[res] = '\0';
942
943 wpabuf_free(buf);
944
945 return res;
946}
947
948
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800949#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800950static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
951 char *reply, size_t max_len,
952 int ndef)
953{
954 struct wpabuf *buf;
955 int res;
956
957 buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
958 if (buf == NULL) {
959 wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
960 return -1;
961 }
962
963 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
964 wpabuf_len(buf));
965 reply[res++] = '\n';
966 reply[res] = '\0';
967
968 wpabuf_free(buf);
969
970 return res;
971}
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800972#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800973
974
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800975static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
976 char *cmd, char *reply,
977 size_t max_len)
978{
979 char *pos;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800980 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800981
982 pos = os_strchr(cmd, ' ');
983 if (pos == NULL)
984 return -1;
985 *pos++ = '\0';
986
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800987 if (os_strcmp(cmd, "WPS") == 0)
988 ndef = 0;
989 else if (os_strcmp(cmd, "NDEF") == 0)
990 ndef = 1;
991 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800992 return -1;
993
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800994 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800995 if (!ndef)
996 return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800997 return wpas_ctrl_nfc_get_handover_req_wps(
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800998 wpa_s, reply, max_len, ndef);
999 }
1000
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001001#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001002 if (os_strcmp(pos, "P2P-CR") == 0) {
1003 return wpas_ctrl_nfc_get_handover_req_p2p(
1004 wpa_s, reply, max_len, ndef);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001005 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001006#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001007
1008 return -1;
1009}
1010
1011
1012static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001013 char *reply, size_t max_len,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001014 int ndef, int cr, char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001015{
1016 struct wpabuf *buf;
1017 int res;
1018
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001019 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001020 if (buf == NULL)
1021 return -1;
1022
1023 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1024 wpabuf_len(buf));
1025 reply[res++] = '\n';
1026 reply[res] = '\0';
1027
1028 wpabuf_free(buf);
1029
1030 return res;
1031}
1032
1033
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001034#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001035static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1036 char *reply, size_t max_len,
1037 int ndef, int tag)
1038{
1039 struct wpabuf *buf;
1040 int res;
1041
1042 buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1043 if (buf == NULL)
1044 return -1;
1045
1046 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1047 wpabuf_len(buf));
1048 reply[res++] = '\n';
1049 reply[res] = '\0';
1050
1051 wpabuf_free(buf);
1052
1053 return res;
1054}
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001055#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001056
1057
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001058static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1059 char *cmd, char *reply,
1060 size_t max_len)
1061{
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001062 char *pos, *pos2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001063 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001064
1065 pos = os_strchr(cmd, ' ');
1066 if (pos == NULL)
1067 return -1;
1068 *pos++ = '\0';
1069
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001070 if (os_strcmp(cmd, "WPS") == 0)
1071 ndef = 0;
1072 else if (os_strcmp(cmd, "NDEF") == 0)
1073 ndef = 1;
1074 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001075 return -1;
1076
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001077 pos2 = os_strchr(pos, ' ');
1078 if (pos2)
1079 *pos2++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001080 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001081 if (!ndef)
1082 return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001083 return wpas_ctrl_nfc_get_handover_sel_wps(
1084 wpa_s, reply, max_len, ndef,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001085 os_strcmp(pos, "WPS-CR") == 0, pos2);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001086 }
1087
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001088#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001089 if (os_strcmp(pos, "P2P-CR") == 0) {
1090 return wpas_ctrl_nfc_get_handover_sel_p2p(
1091 wpa_s, reply, max_len, ndef, 0);
1092 }
1093
1094 if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1095 return wpas_ctrl_nfc_get_handover_sel_p2p(
1096 wpa_s, reply, max_len, ndef, 1);
1097 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001098#endif /* CONFIG_P2P */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001099
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001100 return -1;
1101}
1102
1103
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001104static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1105 char *cmd)
1106{
1107 size_t len;
1108 struct wpabuf *req, *sel;
1109 int ret;
1110 char *pos, *role, *type, *pos2;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001111#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001112 char *freq;
1113 int forced_freq = 0;
1114
1115 freq = strstr(cmd, " freq=");
1116 if (freq) {
1117 *freq = '\0';
1118 freq += 6;
1119 forced_freq = atoi(freq);
1120 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001121#endif /* CONFIG_P2P */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001122
1123 role = cmd;
1124 pos = os_strchr(role, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001125 if (pos == NULL) {
1126 wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001127 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001128 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001129 *pos++ = '\0';
1130
1131 type = pos;
1132 pos = os_strchr(type, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001133 if (pos == NULL) {
1134 wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001135 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001136 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001137 *pos++ = '\0';
1138
1139 pos2 = os_strchr(pos, ' ');
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001140 if (pos2 == NULL) {
1141 wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001142 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001143 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001144 *pos2++ = '\0';
1145
1146 len = os_strlen(pos);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001147 if (len & 0x01) {
1148 wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001149 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001150 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001151 len /= 2;
1152
1153 req = wpabuf_alloc(len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001154 if (req == NULL) {
1155 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001156 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001157 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001158 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001159 wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001160 wpabuf_free(req);
1161 return -1;
1162 }
1163
1164 len = os_strlen(pos2);
1165 if (len & 0x01) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001166 wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001167 wpabuf_free(req);
1168 return -1;
1169 }
1170 len /= 2;
1171
1172 sel = wpabuf_alloc(len);
1173 if (sel == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001174 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001175 wpabuf_free(req);
1176 return -1;
1177 }
1178 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001179 wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001180 wpabuf_free(req);
1181 wpabuf_free(sel);
1182 return -1;
1183 }
1184
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001185 wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1186 role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1187
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001188 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1189 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001190#ifdef CONFIG_AP
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001191 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1192 {
1193 ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1194 if (ret < 0)
1195 ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001196#endif /* CONFIG_AP */
1197#ifdef CONFIG_P2P
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001198 } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1199 {
1200 ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
1201 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1202 {
1203 ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1204 forced_freq);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001205#endif /* CONFIG_P2P */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001206 } else {
1207 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1208 "reported: role=%s type=%s", role, type);
1209 ret = -1;
1210 }
1211 wpabuf_free(req);
1212 wpabuf_free(sel);
1213
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001214 if (ret)
1215 wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1216
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001217 return ret;
1218}
1219
Dmitry Shmidt04949592012-07-19 12:16:46 -07001220#endif /* CONFIG_WPS_NFC */
1221
1222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1224 char *cmd)
1225{
1226 u8 bssid[ETH_ALEN];
1227 char *pin;
1228 char *new_ssid;
1229 char *new_auth;
1230 char *new_encr;
1231 char *new_key;
1232 struct wps_new_ap_settings ap;
1233
1234 pin = os_strchr(cmd, ' ');
1235 if (pin == NULL)
1236 return -1;
1237 *pin++ = '\0';
1238
1239 if (hwaddr_aton(cmd, bssid)) {
1240 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1241 cmd);
1242 return -1;
1243 }
1244
1245 new_ssid = os_strchr(pin, ' ');
1246 if (new_ssid == NULL)
1247 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1248 *new_ssid++ = '\0';
1249
1250 new_auth = os_strchr(new_ssid, ' ');
1251 if (new_auth == NULL)
1252 return -1;
1253 *new_auth++ = '\0';
1254
1255 new_encr = os_strchr(new_auth, ' ');
1256 if (new_encr == NULL)
1257 return -1;
1258 *new_encr++ = '\0';
1259
1260 new_key = os_strchr(new_encr, ' ');
1261 if (new_key == NULL)
1262 return -1;
1263 *new_key++ = '\0';
1264
1265 os_memset(&ap, 0, sizeof(ap));
1266 ap.ssid_hex = new_ssid;
1267 ap.auth = new_auth;
1268 ap.encr = new_encr;
1269 ap.key_hex = new_key;
1270 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1271}
1272
1273
1274#ifdef CONFIG_AP
1275static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1276 char *cmd, char *buf,
1277 size_t buflen)
1278{
1279 int timeout = 300;
1280 char *pos;
1281 const char *pin_txt;
1282
1283 if (!wpa_s->ap_iface)
1284 return -1;
1285
1286 pos = os_strchr(cmd, ' ');
1287 if (pos)
1288 *pos++ = '\0';
1289
1290 if (os_strcmp(cmd, "disable") == 0) {
1291 wpas_wps_ap_pin_disable(wpa_s);
1292 return os_snprintf(buf, buflen, "OK\n");
1293 }
1294
1295 if (os_strcmp(cmd, "random") == 0) {
1296 if (pos)
1297 timeout = atoi(pos);
1298 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1299 if (pin_txt == NULL)
1300 return -1;
1301 return os_snprintf(buf, buflen, "%s", pin_txt);
1302 }
1303
1304 if (os_strcmp(cmd, "get") == 0) {
1305 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1306 if (pin_txt == NULL)
1307 return -1;
1308 return os_snprintf(buf, buflen, "%s", pin_txt);
1309 }
1310
1311 if (os_strcmp(cmd, "set") == 0) {
1312 char *pin;
1313 if (pos == NULL)
1314 return -1;
1315 pin = pos;
1316 pos = os_strchr(pos, ' ');
1317 if (pos) {
1318 *pos++ = '\0';
1319 timeout = atoi(pos);
1320 }
1321 if (os_strlen(pin) > buflen)
1322 return -1;
1323 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1324 return -1;
1325 return os_snprintf(buf, buflen, "%s", pin);
1326 }
1327
1328 return -1;
1329}
1330#endif /* CONFIG_AP */
1331
1332
1333#ifdef CONFIG_WPS_ER
1334static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1335 char *cmd)
1336{
1337 char *uuid = cmd, *pin, *pos;
1338 u8 addr_buf[ETH_ALEN], *addr = NULL;
1339 pin = os_strchr(uuid, ' ');
1340 if (pin == NULL)
1341 return -1;
1342 *pin++ = '\0';
1343 pos = os_strchr(pin, ' ');
1344 if (pos) {
1345 *pos++ = '\0';
1346 if (hwaddr_aton(pos, addr_buf) == 0)
1347 addr = addr_buf;
1348 }
1349 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1350}
1351
1352
1353static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1354 char *cmd)
1355{
1356 char *uuid = cmd, *pin;
1357 pin = os_strchr(uuid, ' ');
1358 if (pin == NULL)
1359 return -1;
1360 *pin++ = '\0';
1361 return wpas_wps_er_learn(wpa_s, uuid, pin);
1362}
1363
1364
1365static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1366 struct wpa_supplicant *wpa_s, char *cmd)
1367{
1368 char *uuid = cmd, *id;
1369 id = os_strchr(uuid, ' ');
1370 if (id == NULL)
1371 return -1;
1372 *id++ = '\0';
1373 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1374}
1375
1376
1377static int wpa_supplicant_ctrl_iface_wps_er_config(
1378 struct wpa_supplicant *wpa_s, char *cmd)
1379{
1380 char *pin;
1381 char *new_ssid;
1382 char *new_auth;
1383 char *new_encr;
1384 char *new_key;
1385 struct wps_new_ap_settings ap;
1386
1387 pin = os_strchr(cmd, ' ');
1388 if (pin == NULL)
1389 return -1;
1390 *pin++ = '\0';
1391
1392 new_ssid = os_strchr(pin, ' ');
1393 if (new_ssid == NULL)
1394 return -1;
1395 *new_ssid++ = '\0';
1396
1397 new_auth = os_strchr(new_ssid, ' ');
1398 if (new_auth == NULL)
1399 return -1;
1400 *new_auth++ = '\0';
1401
1402 new_encr = os_strchr(new_auth, ' ');
1403 if (new_encr == NULL)
1404 return -1;
1405 *new_encr++ = '\0';
1406
1407 new_key = os_strchr(new_encr, ' ');
1408 if (new_key == NULL)
1409 return -1;
1410 *new_key++ = '\0';
1411
1412 os_memset(&ap, 0, sizeof(ap));
1413 ap.ssid_hex = new_ssid;
1414 ap.auth = new_auth;
1415 ap.encr = new_encr;
1416 ap.key_hex = new_key;
1417 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1418}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001419
1420
1421#ifdef CONFIG_WPS_NFC
1422static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1423 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1424{
1425 int ndef;
1426 struct wpabuf *buf;
1427 int res;
1428 char *uuid;
1429
1430 uuid = os_strchr(cmd, ' ');
1431 if (uuid == NULL)
1432 return -1;
1433 *uuid++ = '\0';
1434
1435 if (os_strcmp(cmd, "WPS") == 0)
1436 ndef = 0;
1437 else if (os_strcmp(cmd, "NDEF") == 0)
1438 ndef = 1;
1439 else
1440 return -1;
1441
1442 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1443 if (buf == NULL)
1444 return -1;
1445
1446 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1447 wpabuf_len(buf));
1448 reply[res++] = '\n';
1449 reply[res] = '\0';
1450
1451 wpabuf_free(buf);
1452
1453 return res;
1454}
1455#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456#endif /* CONFIG_WPS_ER */
1457
1458#endif /* CONFIG_WPS */
1459
1460
1461#ifdef CONFIG_IBSS_RSN
1462static int wpa_supplicant_ctrl_iface_ibss_rsn(
1463 struct wpa_supplicant *wpa_s, char *addr)
1464{
1465 u8 peer[ETH_ALEN];
1466
1467 if (hwaddr_aton(addr, peer)) {
1468 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1469 "address '%s'", addr);
1470 return -1;
1471 }
1472
1473 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1474 MAC2STR(peer));
1475
1476 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1477}
1478#endif /* CONFIG_IBSS_RSN */
1479
1480
1481static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1482 char *rsp)
1483{
1484#ifdef IEEE8021X_EAPOL
1485 char *pos, *id_pos;
1486 int id;
1487 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488
1489 pos = os_strchr(rsp, '-');
1490 if (pos == NULL)
1491 return -1;
1492 *pos++ = '\0';
1493 id_pos = pos;
1494 pos = os_strchr(pos, ':');
1495 if (pos == NULL)
1496 return -1;
1497 *pos++ = '\0';
1498 id = atoi(id_pos);
1499 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1500 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1501 (u8 *) pos, os_strlen(pos));
1502
1503 ssid = wpa_config_get_network(wpa_s->conf, id);
1504 if (ssid == NULL) {
1505 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1506 "to update", id);
1507 return -1;
1508 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001510 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1511 pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001512#else /* IEEE8021X_EAPOL */
1513 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1514 return -1;
1515#endif /* IEEE8021X_EAPOL */
1516}
1517
1518
1519static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1520 const char *params,
1521 char *buf, size_t buflen)
1522{
1523 char *pos, *end, tmp[30];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001524 int res, verbose, wps, ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001525#ifdef CONFIG_HS20
1526 const u8 *hs20;
1527#endif /* CONFIG_HS20 */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001528
Dmitry Shmidt56052862013-10-04 10:23:25 -07001529 if (os_strcmp(params, "-DRIVER") == 0)
1530 return wpa_drv_status(wpa_s, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001531 verbose = os_strcmp(params, "-VERBOSE") == 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001532 wps = os_strcmp(params, "-WPS") == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533 pos = buf;
1534 end = buf + buflen;
1535 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1536 struct wpa_ssid *ssid = wpa_s->current_ssid;
1537 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1538 MAC2STR(wpa_s->bssid));
1539 if (ret < 0 || ret >= end - pos)
1540 return pos - buf;
1541 pos += ret;
1542 if (ssid) {
1543 u8 *_ssid = ssid->ssid;
1544 size_t ssid_len = ssid->ssid_len;
1545 u8 ssid_buf[MAX_SSID_LEN];
1546 if (ssid_len == 0) {
1547 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1548 if (_res < 0)
1549 ssid_len = 0;
1550 else
1551 ssid_len = _res;
1552 _ssid = ssid_buf;
1553 }
1554 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1555 wpa_ssid_txt(_ssid, ssid_len),
1556 ssid->id);
1557 if (ret < 0 || ret >= end - pos)
1558 return pos - buf;
1559 pos += ret;
1560
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001561 if (wps && ssid->passphrase &&
1562 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1563 (ssid->mode == WPAS_MODE_AP ||
1564 ssid->mode == WPAS_MODE_P2P_GO)) {
1565 ret = os_snprintf(pos, end - pos,
1566 "passphrase=%s\n",
1567 ssid->passphrase);
1568 if (ret < 0 || ret >= end - pos)
1569 return pos - buf;
1570 pos += ret;
1571 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572 if (ssid->id_str) {
1573 ret = os_snprintf(pos, end - pos,
1574 "id_str=%s\n",
1575 ssid->id_str);
1576 if (ret < 0 || ret >= end - pos)
1577 return pos - buf;
1578 pos += ret;
1579 }
1580
1581 switch (ssid->mode) {
1582 case WPAS_MODE_INFRA:
1583 ret = os_snprintf(pos, end - pos,
1584 "mode=station\n");
1585 break;
1586 case WPAS_MODE_IBSS:
1587 ret = os_snprintf(pos, end - pos,
1588 "mode=IBSS\n");
1589 break;
1590 case WPAS_MODE_AP:
1591 ret = os_snprintf(pos, end - pos,
1592 "mode=AP\n");
1593 break;
1594 case WPAS_MODE_P2P_GO:
1595 ret = os_snprintf(pos, end - pos,
1596 "mode=P2P GO\n");
1597 break;
1598 case WPAS_MODE_P2P_GROUP_FORMATION:
1599 ret = os_snprintf(pos, end - pos,
1600 "mode=P2P GO - group "
1601 "formation\n");
1602 break;
1603 default:
1604 ret = 0;
1605 break;
1606 }
1607 if (ret < 0 || ret >= end - pos)
1608 return pos - buf;
1609 pos += ret;
1610 }
1611
1612#ifdef CONFIG_AP
1613 if (wpa_s->ap_iface) {
1614 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1615 end - pos,
1616 verbose);
1617 } else
1618#endif /* CONFIG_AP */
1619 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1620 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001621#ifdef CONFIG_SAE
1622 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001623#ifdef CONFIG_AP
1624 !wpa_s->ap_iface &&
1625#endif /* CONFIG_AP */
1626 wpa_s->sme.sae.state == SAE_ACCEPTED) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001627 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1628 wpa_s->sme.sae.group);
1629 if (ret < 0 || ret >= end - pos)
1630 return pos - buf;
1631 pos += ret;
1632 }
1633#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1635 wpa_supplicant_state_txt(wpa_s->wpa_state));
1636 if (ret < 0 || ret >= end - pos)
1637 return pos - buf;
1638 pos += ret;
1639
1640 if (wpa_s->l2 &&
1641 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1642 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
1643 if (ret < 0 || ret >= end - pos)
1644 return pos - buf;
1645 pos += ret;
1646 }
1647
1648#ifdef CONFIG_P2P
1649 if (wpa_s->global->p2p) {
1650 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1651 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
1652 if (ret < 0 || ret >= end - pos)
1653 return pos - buf;
1654 pos += ret;
1655 }
1656#endif /* CONFIG_P2P */
1657
1658 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1659 MAC2STR(wpa_s->own_addr));
1660 if (ret < 0 || ret >= end - pos)
1661 return pos - buf;
1662 pos += ret;
1663
Dmitry Shmidt04949592012-07-19 12:16:46 -07001664#ifdef CONFIG_HS20
1665 if (wpa_s->current_bss &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001666 (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1667 HS20_IE_VENDOR_TYPE)) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001668 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1669 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001670 int release = 1;
1671 if (hs20[1] >= 5) {
1672 u8 rel_num = (hs20[6] & 0xf0) >> 4;
1673 release = rel_num + 1;
1674 }
1675 ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001676 if (ret < 0 || ret >= end - pos)
1677 return pos - buf;
1678 pos += ret;
1679 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001680
1681 if (wpa_s->current_ssid) {
1682 struct wpa_cred *cred;
1683 char *type;
1684
1685 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001686 size_t i;
1687
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001688 if (wpa_s->current_ssid->parent_cred != cred)
1689 continue;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001690
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001691 if (cred->provisioning_sp) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001692 ret = os_snprintf(pos, end - pos,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001693 "provisioning_sp=%s\n",
1694 cred->provisioning_sp);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001695 if (ret < 0 || ret >= end - pos)
1696 return pos - buf;
1697 pos += ret;
1698 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001699
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001700 if (!cred->domain)
1701 goto no_domain;
1702
1703 i = 0;
1704 if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
1705 struct wpabuf *names =
1706 wpa_s->current_bss->anqp->domain_name;
1707 for (i = 0; names && i < cred->num_domain; i++)
1708 {
1709 if (domain_name_list_contains(
1710 names, cred->domain[i], 1))
1711 break;
1712 }
1713 if (i == cred->num_domain)
1714 i = 0; /* show first entry by default */
1715 }
1716 ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
1717 cred->domain[i]);
1718 if (ret < 0 || ret >= end - pos)
1719 return pos - buf;
1720 pos += ret;
1721
1722 no_domain:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001723 if (wpa_s->current_bss == NULL ||
1724 wpa_s->current_bss->anqp == NULL)
1725 res = -1;
1726 else
1727 res = interworking_home_sp_cred(
1728 wpa_s, cred,
1729 wpa_s->current_bss->anqp->domain_name);
1730 if (res > 0)
1731 type = "home";
1732 else if (res == 0)
1733 type = "roaming";
1734 else
1735 type = "unknown";
1736
1737 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
1738 if (ret < 0 || ret >= end - pos)
1739 return pos - buf;
1740 pos += ret;
1741
1742 break;
1743 }
1744 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001745#endif /* CONFIG_HS20 */
1746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1748 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1749 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1750 verbose);
1751 if (res >= 0)
1752 pos += res;
1753 }
1754
1755 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1756 if (res >= 0)
1757 pos += res;
1758
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001759#ifdef CONFIG_WPS
1760 {
1761 char uuid_str[100];
1762 uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
1763 ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
1764 if (ret < 0 || ret >= end - pos)
1765 return pos - buf;
1766 pos += ret;
1767 }
1768#endif /* CONFIG_WPS */
1769
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001770#ifdef ANDROID
1771 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
Irfan Sherifff20a4432012-04-16 16:48:34 -07001772 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001773 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
Irfan Sherifff20a4432012-04-16 16:48:34 -07001774 wpa_s->wpa_state,
Irfan Sheriffe78e7672012-08-01 11:10:15 -07001775 MAC2STR(wpa_s->bssid),
andy2_kuo5b5fb022012-05-22 11:53:07 -07001776 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1777 wpa_ssid_txt(wpa_s->current_ssid->ssid,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001778 wpa_s->current_ssid->ssid_len) : "");
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001779 if (wpa_s->wpa_state == WPA_COMPLETED) {
1780 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001781 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
1782 "- connection to " MACSTR
1783 " completed %s [id=%d id_str=%s]",
1784 MAC2STR(wpa_s->bssid), "(auth)",
1785 ssid ? ssid->id : -1,
1786 ssid && ssid->id_str ? ssid->id_str : "");
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001787 }
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001788#endif /* ANDROID */
1789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001790 return pos - buf;
1791}
1792
1793
1794static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1795 char *cmd)
1796{
1797 char *pos;
1798 int id;
1799 struct wpa_ssid *ssid;
1800 u8 bssid[ETH_ALEN];
1801
1802 /* cmd: "<network id> <BSSID>" */
1803 pos = os_strchr(cmd, ' ');
1804 if (pos == NULL)
1805 return -1;
1806 *pos++ = '\0';
1807 id = atoi(cmd);
1808 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1809 if (hwaddr_aton(pos, bssid)) {
1810 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1811 return -1;
1812 }
1813
1814 ssid = wpa_config_get_network(wpa_s->conf, id);
1815 if (ssid == NULL) {
1816 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1817 "to update", id);
1818 return -1;
1819 }
1820
1821 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1822 ssid->bssid_set = !is_zero_ether_addr(bssid);
1823
1824 return 0;
1825}
1826
1827
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001828static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001829 char *cmd, char *buf,
1830 size_t buflen)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001831{
1832 u8 bssid[ETH_ALEN];
1833 struct wpa_blacklist *e;
1834 char *pos, *end;
1835 int ret;
1836
1837 /* cmd: "BLACKLIST [<BSSID>]" */
1838 if (*cmd == '\0') {
1839 pos = buf;
1840 end = buf + buflen;
1841 e = wpa_s->blacklist;
1842 while (e) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001843 ret = os_snprintf(pos, end - pos, MACSTR "\n",
1844 MAC2STR(e->bssid));
1845 if (ret < 0 || ret >= end - pos)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001846 return pos - buf;
1847 pos += ret;
1848 e = e->next;
1849 }
1850 return pos - buf;
1851 }
1852
1853 cmd++;
1854 if (os_strncmp(cmd, "clear", 5) == 0) {
1855 wpa_blacklist_clear(wpa_s);
1856 os_memcpy(buf, "OK\n", 3);
1857 return 3;
1858 }
1859
1860 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1861 if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001862 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001863 return -1;
1864 }
1865
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001866 /*
1867 * Add the BSSID twice, so its count will be 2, causing it to be
1868 * skipped when processing scan results.
1869 */
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001870 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001871 if (ret < 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001872 return -1;
1873 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001874 if (ret < 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001875 return -1;
1876 os_memcpy(buf, "OK\n", 3);
1877 return 3;
1878}
1879
1880
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001881static const char * debug_level_str(int level)
1882{
1883 switch (level) {
1884 case MSG_EXCESSIVE:
1885 return "EXCESSIVE";
1886 case MSG_MSGDUMP:
1887 return "MSGDUMP";
1888 case MSG_DEBUG:
1889 return "DEBUG";
1890 case MSG_INFO:
1891 return "INFO";
1892 case MSG_WARNING:
1893 return "WARNING";
1894 case MSG_ERROR:
1895 return "ERROR";
1896 default:
1897 return "?";
1898 }
1899}
1900
1901
1902static int str_to_debug_level(const char *s)
1903{
1904 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1905 return MSG_EXCESSIVE;
1906 if (os_strcasecmp(s, "MSGDUMP") == 0)
1907 return MSG_MSGDUMP;
1908 if (os_strcasecmp(s, "DEBUG") == 0)
1909 return MSG_DEBUG;
1910 if (os_strcasecmp(s, "INFO") == 0)
1911 return MSG_INFO;
1912 if (os_strcasecmp(s, "WARNING") == 0)
1913 return MSG_WARNING;
1914 if (os_strcasecmp(s, "ERROR") == 0)
1915 return MSG_ERROR;
1916 return -1;
1917}
1918
1919
1920static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1921 char *cmd, char *buf,
1922 size_t buflen)
1923{
1924 char *pos, *end, *stamp;
1925 int ret;
1926
1927 if (cmd == NULL) {
1928 return -1;
1929 }
1930
1931 /* cmd: "LOG_LEVEL [<level>]" */
1932 if (*cmd == '\0') {
1933 pos = buf;
1934 end = buf + buflen;
1935 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1936 "Timestamp: %d\n",
1937 debug_level_str(wpa_debug_level),
1938 wpa_debug_timestamp);
1939 if (ret < 0 || ret >= end - pos)
1940 ret = 0;
1941
1942 return ret;
1943 }
1944
1945 while (*cmd == ' ')
1946 cmd++;
1947
1948 stamp = os_strchr(cmd, ' ');
1949 if (stamp) {
1950 *stamp++ = '\0';
1951 while (*stamp == ' ') {
1952 stamp++;
1953 }
1954 }
1955
1956 if (cmd && os_strlen(cmd)) {
1957 int level = str_to_debug_level(cmd);
1958 if (level < 0)
1959 return -1;
1960 wpa_debug_level = level;
1961 }
1962
1963 if (stamp && os_strlen(stamp))
1964 wpa_debug_timestamp = atoi(stamp);
1965
1966 os_memcpy(buf, "OK\n", 3);
1967 return 3;
1968}
1969
1970
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971static int wpa_supplicant_ctrl_iface_list_networks(
1972 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1973{
1974 char *pos, *end;
1975 struct wpa_ssid *ssid;
1976 int ret;
1977
1978 pos = buf;
1979 end = buf + buflen;
1980 ret = os_snprintf(pos, end - pos,
1981 "network id / ssid / bssid / flags\n");
1982 if (ret < 0 || ret >= end - pos)
1983 return pos - buf;
1984 pos += ret;
1985
1986 ssid = wpa_s->conf->ssid;
1987 while (ssid) {
1988 ret = os_snprintf(pos, end - pos, "%d\t%s",
1989 ssid->id,
1990 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1991 if (ret < 0 || ret >= end - pos)
1992 return pos - buf;
1993 pos += ret;
1994 if (ssid->bssid_set) {
1995 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
1996 MAC2STR(ssid->bssid));
1997 } else {
1998 ret = os_snprintf(pos, end - pos, "\tany");
1999 }
2000 if (ret < 0 || ret >= end - pos)
2001 return pos - buf;
2002 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002003 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002004 ssid == wpa_s->current_ssid ?
2005 "[CURRENT]" : "",
2006 ssid->disabled ? "[DISABLED]" : "",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002007 ssid->disabled_until.sec ?
2008 "[TEMP-DISABLED]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002009 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2010 "");
2011 if (ret < 0 || ret >= end - pos)
2012 return pos - buf;
2013 pos += ret;
2014 ret = os_snprintf(pos, end - pos, "\n");
2015 if (ret < 0 || ret >= end - pos)
2016 return pos - buf;
2017 pos += ret;
2018
2019 ssid = ssid->next;
2020 }
2021
2022 return pos - buf;
2023}
2024
2025
2026static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2027{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002028 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029 ret = os_snprintf(pos, end - pos, "-");
2030 if (ret < 0 || ret >= end - pos)
2031 return pos;
2032 pos += ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002033 ret = wpa_write_ciphers(pos, end, cipher, "+");
2034 if (ret < 0)
2035 return pos;
2036 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002037 return pos;
2038}
2039
2040
2041static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2042 const u8 *ie, size_t ie_len)
2043{
2044 struct wpa_ie_data data;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002045 char *start;
2046 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047
2048 ret = os_snprintf(pos, end - pos, "[%s-", proto);
2049 if (ret < 0 || ret >= end - pos)
2050 return pos;
2051 pos += ret;
2052
2053 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2054 ret = os_snprintf(pos, end - pos, "?]");
2055 if (ret < 0 || ret >= end - pos)
2056 return pos;
2057 pos += ret;
2058 return pos;
2059 }
2060
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002061 start = pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002063 ret = os_snprintf(pos, end - pos, "%sEAP",
2064 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065 if (ret < 0 || ret >= end - pos)
2066 return pos;
2067 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068 }
2069 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002070 ret = os_snprintf(pos, end - pos, "%sPSK",
2071 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072 if (ret < 0 || ret >= end - pos)
2073 return pos;
2074 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002075 }
2076 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002077 ret = os_snprintf(pos, end - pos, "%sNone",
2078 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079 if (ret < 0 || ret >= end - pos)
2080 return pos;
2081 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082 }
2083#ifdef CONFIG_IEEE80211R
2084 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2085 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002086 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002087 if (ret < 0 || ret >= end - pos)
2088 return pos;
2089 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090 }
2091 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2092 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002093 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 if (ret < 0 || ret >= end - pos)
2095 return pos;
2096 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097 }
2098#endif /* CONFIG_IEEE80211R */
2099#ifdef CONFIG_IEEE80211W
2100 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2101 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002102 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103 if (ret < 0 || ret >= end - pos)
2104 return pos;
2105 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 }
2107 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2108 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002109 pos == start ? "" : "+");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 if (ret < 0 || ret >= end - pos)
2111 return pos;
2112 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113 }
2114#endif /* CONFIG_IEEE80211W */
2115
2116 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2117
2118 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2119 ret = os_snprintf(pos, end - pos, "-preauth");
2120 if (ret < 0 || ret >= end - pos)
2121 return pos;
2122 pos += ret;
2123 }
2124
2125 ret = os_snprintf(pos, end - pos, "]");
2126 if (ret < 0 || ret >= end - pos)
2127 return pos;
2128 pos += ret;
2129
2130 return pos;
2131}
2132
2133
2134#ifdef CONFIG_WPS
2135static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2136 char *pos, char *end,
2137 struct wpabuf *wps_ie)
2138{
2139 int ret;
2140 const char *txt;
2141
2142 if (wps_ie == NULL)
2143 return pos;
2144 if (wps_is_selected_pbc_registrar(wps_ie))
2145 txt = "[WPS-PBC]";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2147 txt = "[WPS-AUTH]";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002148 else if (wps_is_selected_pin_registrar(wps_ie))
2149 txt = "[WPS-PIN]";
2150 else
2151 txt = "[WPS]";
2152
2153 ret = os_snprintf(pos, end - pos, "%s", txt);
2154 if (ret >= 0 && ret < end - pos)
2155 pos += ret;
2156 wpabuf_free(wps_ie);
2157 return pos;
2158}
2159#endif /* CONFIG_WPS */
2160
2161
2162static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2163 char *pos, char *end,
2164 const struct wpa_bss *bss)
2165{
2166#ifdef CONFIG_WPS
2167 struct wpabuf *wps_ie;
2168 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2169 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2170#else /* CONFIG_WPS */
2171 return pos;
2172#endif /* CONFIG_WPS */
2173}
2174
2175
2176/* Format one result on one text line into a buffer. */
2177static int wpa_supplicant_ctrl_iface_scan_result(
2178 struct wpa_supplicant *wpa_s,
2179 const struct wpa_bss *bss, char *buf, size_t buflen)
2180{
2181 char *pos, *end;
2182 int ret;
2183 const u8 *ie, *ie2, *p2p;
2184
2185 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07002186 if (!p2p)
2187 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2189 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2190 0)
2191 return 0; /* Do not show P2P listen discovery results here */
2192
2193 pos = buf;
2194 end = buf + buflen;
2195
2196 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2197 MAC2STR(bss->bssid), bss->freq, bss->level);
2198 if (ret < 0 || ret >= end - pos)
2199 return -1;
2200 pos += ret;
2201 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2202 if (ie)
2203 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2204 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2205 if (ie2)
2206 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
2207 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2208 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2209 ret = os_snprintf(pos, end - pos, "[WEP]");
2210 if (ret < 0 || ret >= end - pos)
2211 return -1;
2212 pos += ret;
2213 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002214 if (bss_is_dmg(bss)) {
2215 const char *s;
2216 ret = os_snprintf(pos, end - pos, "[DMG]");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002217 if (ret < 0 || ret >= end - pos)
2218 return -1;
2219 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002220 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
2221 case IEEE80211_CAP_DMG_IBSS:
2222 s = "[IBSS]";
2223 break;
2224 case IEEE80211_CAP_DMG_AP:
2225 s = "[ESS]";
2226 break;
2227 case IEEE80211_CAP_DMG_PBSS:
2228 s = "[PBSS]";
2229 break;
2230 default:
2231 s = "";
2232 break;
2233 }
2234 ret = os_snprintf(pos, end - pos, "%s", s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235 if (ret < 0 || ret >= end - pos)
2236 return -1;
2237 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002238 } else {
2239 if (bss->caps & IEEE80211_CAP_IBSS) {
2240 ret = os_snprintf(pos, end - pos, "[IBSS]");
2241 if (ret < 0 || ret >= end - pos)
2242 return -1;
2243 pos += ret;
2244 }
2245 if (bss->caps & IEEE80211_CAP_ESS) {
2246 ret = os_snprintf(pos, end - pos, "[ESS]");
2247 if (ret < 0 || ret >= end - pos)
2248 return -1;
2249 pos += ret;
2250 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251 }
2252 if (p2p) {
2253 ret = os_snprintf(pos, end - pos, "[P2P]");
2254 if (ret < 0 || ret >= end - pos)
2255 return -1;
2256 pos += ret;
2257 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002258#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002259 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002260 ret = os_snprintf(pos, end - pos, "[HS20]");
2261 if (ret < 0 || ret >= end - pos)
2262 return -1;
2263 pos += ret;
2264 }
2265#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266
2267 ret = os_snprintf(pos, end - pos, "\t%s",
2268 wpa_ssid_txt(bss->ssid, bss->ssid_len));
2269 if (ret < 0 || ret >= end - pos)
2270 return -1;
2271 pos += ret;
2272
2273 ret = os_snprintf(pos, end - pos, "\n");
2274 if (ret < 0 || ret >= end - pos)
2275 return -1;
2276 pos += ret;
2277
2278 return pos - buf;
2279}
2280
2281
2282static int wpa_supplicant_ctrl_iface_scan_results(
2283 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2284{
2285 char *pos, *end;
2286 struct wpa_bss *bss;
2287 int ret;
2288
2289 pos = buf;
2290 end = buf + buflen;
2291 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2292 "flags / ssid\n");
2293 if (ret < 0 || ret >= end - pos)
2294 return pos - buf;
2295 pos += ret;
2296
2297 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2298 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2299 end - pos);
2300 if (ret < 0 || ret >= end - pos)
2301 return pos - buf;
2302 pos += ret;
2303 }
2304
2305 return pos - buf;
2306}
2307
2308
2309static int wpa_supplicant_ctrl_iface_select_network(
2310 struct wpa_supplicant *wpa_s, char *cmd)
2311{
2312 int id;
2313 struct wpa_ssid *ssid;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002314 char *pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315
2316 /* cmd: "<network id>" or "any" */
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002317 if (os_strncmp(cmd, "any", 3) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2319 ssid = NULL;
2320 } else {
2321 id = atoi(cmd);
2322 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2323
2324 ssid = wpa_config_get_network(wpa_s->conf, id);
2325 if (ssid == NULL) {
2326 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2327 "network id=%d", id);
2328 return -1;
2329 }
2330 if (ssid->disabled == 2) {
2331 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2332 "SELECT_NETWORK with persistent P2P group");
2333 return -1;
2334 }
2335 }
2336
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002337 pos = os_strstr(cmd, " freq=");
2338 if (pos) {
2339 int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
2340 if (freqs) {
2341 wpa_s->scan_req = MANUAL_SCAN_REQ;
2342 os_free(wpa_s->manual_scan_freqs);
2343 wpa_s->manual_scan_freqs = freqs;
2344 }
2345 }
2346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347 wpa_supplicant_select_network(wpa_s, ssid);
2348
2349 return 0;
2350}
2351
2352
2353static int wpa_supplicant_ctrl_iface_enable_network(
2354 struct wpa_supplicant *wpa_s, char *cmd)
2355{
2356 int id;
2357 struct wpa_ssid *ssid;
2358
2359 /* cmd: "<network id>" or "all" */
2360 if (os_strcmp(cmd, "all") == 0) {
2361 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2362 ssid = NULL;
2363 } else {
2364 id = atoi(cmd);
2365 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2366
2367 ssid = wpa_config_get_network(wpa_s->conf, id);
2368 if (ssid == NULL) {
2369 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2370 "network id=%d", id);
2371 return -1;
2372 }
2373 if (ssid->disabled == 2) {
2374 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2375 "ENABLE_NETWORK with persistent P2P group");
2376 return -1;
2377 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002378
2379 if (os_strstr(cmd, " no-connect")) {
2380 ssid->disabled = 0;
2381 return 0;
2382 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383 }
2384 wpa_supplicant_enable_network(wpa_s, ssid);
2385
2386 return 0;
2387}
2388
2389
2390static int wpa_supplicant_ctrl_iface_disable_network(
2391 struct wpa_supplicant *wpa_s, char *cmd)
2392{
2393 int id;
2394 struct wpa_ssid *ssid;
2395
2396 /* cmd: "<network id>" or "all" */
2397 if (os_strcmp(cmd, "all") == 0) {
2398 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2399 ssid = NULL;
2400 } else {
2401 id = atoi(cmd);
2402 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2403
2404 ssid = wpa_config_get_network(wpa_s->conf, id);
2405 if (ssid == NULL) {
2406 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2407 "network id=%d", id);
2408 return -1;
2409 }
2410 if (ssid->disabled == 2) {
2411 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2412 "DISABLE_NETWORK with persistent P2P "
2413 "group");
2414 return -1;
2415 }
2416 }
2417 wpa_supplicant_disable_network(wpa_s, ssid);
2418
2419 return 0;
2420}
2421
2422
2423static int wpa_supplicant_ctrl_iface_add_network(
2424 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2425{
2426 struct wpa_ssid *ssid;
2427 int ret;
2428
2429 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2430
2431 ssid = wpa_config_add_network(wpa_s->conf);
2432 if (ssid == NULL)
2433 return -1;
2434
2435 wpas_notify_network_added(wpa_s, ssid);
2436
2437 ssid->disabled = 1;
2438 wpa_config_set_network_defaults(ssid);
2439
2440 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
2441 if (ret < 0 || (size_t) ret >= buflen)
2442 return -1;
2443 return ret;
2444}
2445
2446
2447static int wpa_supplicant_ctrl_iface_remove_network(
2448 struct wpa_supplicant *wpa_s, char *cmd)
2449{
2450 int id;
2451 struct wpa_ssid *ssid;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002452 int was_disabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453
2454 /* cmd: "<network id>" or "all" */
2455 if (os_strcmp(cmd, "all") == 0) {
2456 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002457 if (wpa_s->sched_scanning)
2458 wpa_supplicant_cancel_sched_scan(wpa_s);
2459
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002460 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002461 if (wpa_s->current_ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002462#ifdef CONFIG_SME
2463 wpa_s->sme.prev_bssid_set = 0;
2464#endif /* CONFIG_SME */
Jouni Malinen75ecf522011-06-27 15:19:46 -07002465 wpa_sm_set_config(wpa_s->wpa, NULL);
2466 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002467 wpa_supplicant_deauthenticate(
2468 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002470 ssid = wpa_s->conf->ssid;
2471 while (ssid) {
2472 struct wpa_ssid *remove_ssid = ssid;
2473 id = ssid->id;
2474 ssid = ssid->next;
2475 wpas_notify_network_removed(wpa_s, remove_ssid);
2476 wpa_config_remove_network(wpa_s->conf, id);
2477 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002478 return 0;
2479 }
2480
2481 id = atoi(cmd);
2482 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2483
2484 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002485 if (ssid)
2486 wpas_notify_network_removed(wpa_s, ssid);
Deepthi Gowria831d782012-09-03 11:55:38 +03002487 if (ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2489 "id=%d", id);
2490 return -1;
2491 }
2492
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002493 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002494#ifdef CONFIG_SME
2495 wpa_s->sme.prev_bssid_set = 0;
2496#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002498 * Invalidate the EAP session cache if the current or
2499 * previously used network is removed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500 */
2501 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002502 }
2503
2504 if (ssid == wpa_s->current_ssid) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002505 wpa_sm_set_config(wpa_s->wpa, NULL);
2506 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002507
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002508 wpa_supplicant_deauthenticate(wpa_s,
2509 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510 }
2511
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002512 was_disabled = ssid->disabled;
2513
Deepthi Gowria831d782012-09-03 11:55:38 +03002514 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2515 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2516 "network id=%d", id);
2517 return -1;
2518 }
2519
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002520 if (!was_disabled && wpa_s->sched_scanning) {
2521 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2522 "network from filters");
2523 wpa_supplicant_cancel_sched_scan(wpa_s);
2524 wpa_supplicant_req_scan(wpa_s, 0, 0);
2525 }
2526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527 return 0;
2528}
2529
2530
Dmitry Shmidt684785c2014-05-12 13:34:29 -07002531static int wpa_supplicant_ctrl_iface_update_network(
2532 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2533 char *name, char *value)
2534{
2535 if (wpa_config_set(ssid, name, value, 0) < 0) {
2536 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2537 "variable '%s'", name);
2538 return -1;
2539 }
2540
2541 if (os_strcmp(name, "bssid") != 0 &&
2542 os_strcmp(name, "priority") != 0)
2543 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
2544
2545 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
2546 /*
2547 * Invalidate the EAP session cache if anything in the current
2548 * or previously used configuration changes.
2549 */
2550 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2551 }
2552
2553 if ((os_strcmp(name, "psk") == 0 &&
2554 value[0] == '"' && ssid->ssid_len) ||
2555 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2556 wpa_config_update_psk(ssid);
2557 else if (os_strcmp(name, "priority") == 0)
2558 wpa_config_update_prio_list(wpa_s->conf);
2559
2560 return 0;
2561}
2562
2563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002564static int wpa_supplicant_ctrl_iface_set_network(
2565 struct wpa_supplicant *wpa_s, char *cmd)
2566{
2567 int id;
2568 struct wpa_ssid *ssid;
2569 char *name, *value;
2570
2571 /* cmd: "<network id> <variable name> <value>" */
2572 name = os_strchr(cmd, ' ');
2573 if (name == NULL)
2574 return -1;
2575 *name++ = '\0';
2576
2577 value = os_strchr(name, ' ');
2578 if (value == NULL)
2579 return -1;
2580 *value++ = '\0';
2581
2582 id = atoi(cmd);
2583 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2584 id, name);
2585 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2586 (u8 *) value, os_strlen(value));
2587
2588 ssid = wpa_config_get_network(wpa_s->conf, id);
2589 if (ssid == NULL) {
2590 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2591 "id=%d", id);
2592 return -1;
2593 }
2594
Dmitry Shmidt684785c2014-05-12 13:34:29 -07002595 return wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
2596 value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597}
2598
2599
2600static int wpa_supplicant_ctrl_iface_get_network(
2601 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2602{
2603 int id;
2604 size_t res;
2605 struct wpa_ssid *ssid;
2606 char *name, *value;
2607
2608 /* cmd: "<network id> <variable name>" */
2609 name = os_strchr(cmd, ' ');
2610 if (name == NULL || buflen == 0)
2611 return -1;
2612 *name++ = '\0';
2613
2614 id = atoi(cmd);
2615 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2616 id, name);
2617
2618 ssid = wpa_config_get_network(wpa_s->conf, id);
2619 if (ssid == NULL) {
2620 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2621 "id=%d", id);
2622 return -1;
2623 }
2624
2625 value = wpa_config_get_no_key(ssid, name);
2626 if (value == NULL) {
2627 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2628 "variable '%s'", name);
2629 return -1;
2630 }
2631
2632 res = os_strlcpy(buf, value, buflen);
2633 if (res >= buflen) {
2634 os_free(value);
2635 return -1;
2636 }
2637
2638 os_free(value);
2639
2640 return res;
2641}
2642
2643
Dmitry Shmidt684785c2014-05-12 13:34:29 -07002644static int wpa_supplicant_ctrl_iface_dup_network(
2645 struct wpa_supplicant *wpa_s, char *cmd)
2646{
2647 struct wpa_ssid *ssid_s, *ssid_d;
2648 char *name, *id, *value;
2649 int id_s, id_d, ret;
2650
2651 /* cmd: "<src network id> <dst network id> <variable name>" */
2652 id = os_strchr(cmd, ' ');
2653 if (id == NULL)
2654 return -1;
2655 *id++ = '\0';
2656
2657 name = os_strchr(id, ' ');
2658 if (name == NULL)
2659 return -1;
2660 *name++ = '\0';
2661
2662 id_s = atoi(cmd);
2663 id_d = atoi(id);
2664 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DUP_NETWORK id=%d -> %d name='%s'",
2665 id_s, id_d, name);
2666
2667 ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
2668 if (ssid_s == NULL) {
2669 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2670 "network id=%d", id_s);
2671 return -1;
2672 }
2673
2674 ssid_d = wpa_config_get_network(wpa_s->conf, id_d);
2675 if (ssid_d == NULL) {
2676 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2677 "network id=%d", id_s);
2678 return -1;
2679 }
2680
2681 value = wpa_config_get(ssid_s, name);
2682 if (value == NULL) {
2683 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2684 "variable '%s'", name);
2685 return -1;
2686 }
2687
2688 ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid_d, name,
2689 value);
2690
2691 os_free(value);
2692
2693 return ret;
2694}
2695
2696
Dmitry Shmidt04949592012-07-19 12:16:46 -07002697static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2698 char *buf, size_t buflen)
2699{
2700 char *pos, *end;
2701 struct wpa_cred *cred;
2702 int ret;
2703
2704 pos = buf;
2705 end = buf + buflen;
2706 ret = os_snprintf(pos, end - pos,
2707 "cred id / realm / username / domain / imsi\n");
2708 if (ret < 0 || ret >= end - pos)
2709 return pos - buf;
2710 pos += ret;
2711
2712 cred = wpa_s->conf->cred;
2713 while (cred) {
2714 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2715 cred->id, cred->realm ? cred->realm : "",
2716 cred->username ? cred->username : "",
Dmitry Shmidt051af732013-10-22 13:52:46 -07002717 cred->domain ? cred->domain[0] : "",
Dmitry Shmidt04949592012-07-19 12:16:46 -07002718 cred->imsi ? cred->imsi : "");
2719 if (ret < 0 || ret >= end - pos)
2720 return pos - buf;
2721 pos += ret;
2722
2723 cred = cred->next;
2724 }
2725
2726 return pos - buf;
2727}
2728
2729
2730static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2731 char *buf, size_t buflen)
2732{
2733 struct wpa_cred *cred;
2734 int ret;
2735
2736 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2737
2738 cred = wpa_config_add_cred(wpa_s->conf);
2739 if (cred == NULL)
2740 return -1;
2741
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002742 wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
2743
Dmitry Shmidt04949592012-07-19 12:16:46 -07002744 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2745 if (ret < 0 || (size_t) ret >= buflen)
2746 return -1;
2747 return ret;
2748}
2749
2750
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002751static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2752 struct wpa_cred *cred)
2753{
2754 struct wpa_ssid *ssid;
2755 char str[20];
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002756 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002757
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002758 if (cred == NULL) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002759 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2760 return -1;
2761 }
2762
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002763 id = cred->id;
2764 if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
2765 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2766 return -1;
2767 }
2768
2769 wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
2770
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002771 /* Remove any network entry created based on the removed credential */
2772 ssid = wpa_s->conf->ssid;
2773 while (ssid) {
2774 if (ssid->parent_cred == cred) {
2775 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
2776 "used the removed credential", ssid->id);
2777 os_snprintf(str, sizeof(str), "%d", ssid->id);
2778 ssid = ssid->next;
2779 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
2780 } else
2781 ssid = ssid->next;
2782 }
2783
2784 return 0;
2785}
2786
2787
Dmitry Shmidt04949592012-07-19 12:16:46 -07002788static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2789 char *cmd)
2790{
2791 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002792 struct wpa_cred *cred, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002793
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002794 /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
2795 * "provisioning_sp=<FQDN> */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002796 if (os_strcmp(cmd, "all") == 0) {
2797 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2798 cred = wpa_s->conf->cred;
2799 while (cred) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002800 prev = cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002801 cred = cred->next;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002802 wpas_ctrl_remove_cred(wpa_s, prev);
2803 }
2804 return 0;
2805 }
2806
2807 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
2808 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
2809 cmd + 8);
2810 cred = wpa_s->conf->cred;
2811 while (cred) {
2812 prev = cred;
2813 cred = cred->next;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002814 if (prev->domain) {
2815 size_t i;
2816 for (i = 0; i < prev->num_domain; i++) {
2817 if (os_strcmp(prev->domain[i], cmd + 8)
2818 != 0)
2819 continue;
2820 wpas_ctrl_remove_cred(wpa_s, prev);
2821 break;
2822 }
2823 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002824 }
2825 return 0;
2826 }
2827
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002828 if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
2829 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
2830 cmd + 16);
2831 cred = wpa_s->conf->cred;
2832 while (cred) {
2833 prev = cred;
2834 cred = cred->next;
2835 if (prev->provisioning_sp &&
2836 os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
2837 wpas_ctrl_remove_cred(wpa_s, prev);
2838 }
2839 return 0;
2840 }
2841
Dmitry Shmidt04949592012-07-19 12:16:46 -07002842 id = atoi(cmd);
2843 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2844
2845 cred = wpa_config_get_cred(wpa_s->conf, id);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002846 return wpas_ctrl_remove_cred(wpa_s, cred);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002847}
2848
2849
2850static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2851 char *cmd)
2852{
2853 int id;
2854 struct wpa_cred *cred;
2855 char *name, *value;
2856
2857 /* cmd: "<cred id> <variable name> <value>" */
2858 name = os_strchr(cmd, ' ');
2859 if (name == NULL)
2860 return -1;
2861 *name++ = '\0';
2862
2863 value = os_strchr(name, ' ');
2864 if (value == NULL)
2865 return -1;
2866 *value++ = '\0';
2867
2868 id = atoi(cmd);
2869 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2870 id, name);
2871 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2872 (u8 *) value, os_strlen(value));
2873
2874 cred = wpa_config_get_cred(wpa_s->conf, id);
2875 if (cred == NULL) {
2876 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2877 id);
2878 return -1;
2879 }
2880
2881 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2882 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2883 "variable '%s'", name);
2884 return -1;
2885 }
2886
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002887 wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
2888
Dmitry Shmidt04949592012-07-19 12:16:46 -07002889 return 0;
2890}
2891
2892
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002893static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
2894 char *cmd, char *buf,
2895 size_t buflen)
2896{
2897 int id;
2898 size_t res;
2899 struct wpa_cred *cred;
2900 char *name, *value;
2901
2902 /* cmd: "<cred id> <variable name>" */
2903 name = os_strchr(cmd, ' ');
2904 if (name == NULL)
2905 return -1;
2906 *name++ = '\0';
2907
2908 id = atoi(cmd);
2909 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
2910 id, name);
2911
2912 cred = wpa_config_get_cred(wpa_s->conf, id);
2913 if (cred == NULL) {
2914 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2915 id);
2916 return -1;
2917 }
2918
2919 value = wpa_config_get_cred_no_key(cred, name);
2920 if (value == NULL) {
2921 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
2922 name);
2923 return -1;
2924 }
2925
2926 res = os_strlcpy(buf, value, buflen);
2927 if (res >= buflen) {
2928 os_free(value);
2929 return -1;
2930 }
2931
2932 os_free(value);
2933
2934 return res;
2935}
2936
2937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938#ifndef CONFIG_NO_CONFIG_WRITE
2939static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2940{
2941 int ret;
2942
2943 if (!wpa_s->conf->update_config) {
2944 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2945 "to update configuration (update_config=0)");
2946 return -1;
2947 }
2948
2949 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2950 if (ret) {
2951 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2952 "update configuration");
2953 } else {
2954 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2955 " updated");
2956 }
2957
2958 return ret;
2959}
2960#endif /* CONFIG_NO_CONFIG_WRITE */
2961
2962
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002963struct cipher_info {
2964 unsigned int capa;
2965 const char *name;
2966 int group_only;
2967};
2968
2969static const struct cipher_info ciphers[] = {
2970 { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
2971 { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
2972 { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
2973 { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
2974 { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
2975 { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
2976 { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
2977 { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
2978};
2979
2980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002981static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2982 struct wpa_driver_capa *capa,
2983 char *buf, size_t buflen)
2984{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002985 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002986 char *pos, *end;
2987 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002988 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002989
2990 pos = buf;
2991 end = pos + buflen;
2992
2993 if (res < 0) {
2994 if (strict)
2995 return 0;
2996 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2997 if (len >= buflen)
2998 return -1;
2999 return len;
3000 }
3001
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003002 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3003 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
3004 ret = os_snprintf(pos, end - pos, "%s%s",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003005 pos == buf ? "" : " ",
3006 ciphers[i].name);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003007 if (ret < 0 || ret >= end - pos)
3008 return pos - buf;
3009 pos += ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003010 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003011 }
3012
3013 return pos - buf;
3014}
3015
3016
3017static int ctrl_iface_get_capability_group(int res, char *strict,
3018 struct wpa_driver_capa *capa,
3019 char *buf, size_t buflen)
3020{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003021 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003022 char *pos, *end;
3023 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003024 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003025
3026 pos = buf;
3027 end = pos + buflen;
3028
3029 if (res < 0) {
3030 if (strict)
3031 return 0;
3032 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
3033 if (len >= buflen)
3034 return -1;
3035 return len;
3036 }
3037
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003038 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3039 if (capa->enc & ciphers[i].capa) {
3040 ret = os_snprintf(pos, end - pos, "%s%s",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003041 pos == buf ? "" : " ",
3042 ciphers[i].name);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003043 if (ret < 0 || ret >= end - pos)
3044 return pos - buf;
3045 pos += ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003046 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047 }
3048
3049 return pos - buf;
3050}
3051
3052
3053static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
3054 struct wpa_driver_capa *capa,
3055 char *buf, size_t buflen)
3056{
3057 int ret;
3058 char *pos, *end;
3059 size_t len;
3060
3061 pos = buf;
3062 end = pos + buflen;
3063
3064 if (res < 0) {
3065 if (strict)
3066 return 0;
3067 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
3068 "NONE", buflen);
3069 if (len >= buflen)
3070 return -1;
3071 return len;
3072 }
3073
3074 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
3075 if (ret < 0 || ret >= end - pos)
3076 return pos - buf;
3077 pos += ret;
3078
3079 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3080 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
3081 ret = os_snprintf(pos, end - pos, " WPA-EAP");
3082 if (ret < 0 || ret >= end - pos)
3083 return pos - buf;
3084 pos += ret;
3085 }
3086
3087 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3088 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
3089 ret = os_snprintf(pos, end - pos, " WPA-PSK");
3090 if (ret < 0 || ret >= end - pos)
3091 return pos - buf;
3092 pos += ret;
3093 }
3094
3095 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
3096 ret = os_snprintf(pos, end - pos, " WPA-NONE");
3097 if (ret < 0 || ret >= end - pos)
3098 return pos - buf;
3099 pos += ret;
3100 }
3101
3102 return pos - buf;
3103}
3104
3105
3106static int ctrl_iface_get_capability_proto(int res, char *strict,
3107 struct wpa_driver_capa *capa,
3108 char *buf, size_t buflen)
3109{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003110 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111 char *pos, *end;
3112 size_t len;
3113
3114 pos = buf;
3115 end = pos + buflen;
3116
3117 if (res < 0) {
3118 if (strict)
3119 return 0;
3120 len = os_strlcpy(buf, "RSN WPA", buflen);
3121 if (len >= buflen)
3122 return -1;
3123 return len;
3124 }
3125
3126 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3127 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003128 ret = os_snprintf(pos, end - pos, "%sRSN",
3129 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003130 if (ret < 0 || ret >= end - pos)
3131 return pos - buf;
3132 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003133 }
3134
3135 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3136 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003137 ret = os_snprintf(pos, end - pos, "%sWPA",
3138 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003139 if (ret < 0 || ret >= end - pos)
3140 return pos - buf;
3141 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003142 }
3143
3144 return pos - buf;
3145}
3146
3147
3148static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
3149 struct wpa_driver_capa *capa,
3150 char *buf, size_t buflen)
3151{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003152 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003153 char *pos, *end;
3154 size_t len;
3155
3156 pos = buf;
3157 end = pos + buflen;
3158
3159 if (res < 0) {
3160 if (strict)
3161 return 0;
3162 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
3163 if (len >= buflen)
3164 return -1;
3165 return len;
3166 }
3167
3168 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003169 ret = os_snprintf(pos, end - pos, "%sOPEN",
3170 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003171 if (ret < 0 || ret >= end - pos)
3172 return pos - buf;
3173 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003174 }
3175
3176 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
3177 ret = os_snprintf(pos, end - pos, "%sSHARED",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003178 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003179 if (ret < 0 || ret >= end - pos)
3180 return pos - buf;
3181 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003182 }
3183
3184 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003185 ret = os_snprintf(pos, end - pos, "%sLEAP",
3186 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003187 if (ret < 0 || ret >= end - pos)
3188 return pos - buf;
3189 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003190 }
3191
3192 return pos - buf;
3193}
3194
3195
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003196static int ctrl_iface_get_capability_modes(int res, char *strict,
3197 struct wpa_driver_capa *capa,
3198 char *buf, size_t buflen)
3199{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003200 int ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003201 char *pos, *end;
3202 size_t len;
3203
3204 pos = buf;
3205 end = pos + buflen;
3206
3207 if (res < 0) {
3208 if (strict)
3209 return 0;
3210 len = os_strlcpy(buf, "IBSS AP", buflen);
3211 if (len >= buflen)
3212 return -1;
3213 return len;
3214 }
3215
3216 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003217 ret = os_snprintf(pos, end - pos, "%sIBSS",
3218 pos == buf ? "" : " ");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003219 if (ret < 0 || ret >= end - pos)
3220 return pos - buf;
3221 pos += ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003222 }
3223
3224 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003225 ret = os_snprintf(pos, end - pos, "%sAP",
3226 pos == buf ? "" : " ");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003227 if (ret < 0 || ret >= end - pos)
3228 return pos - buf;
3229 pos += ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003230 }
3231
3232 return pos - buf;
3233}
3234
3235
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003236static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
3237 char *buf, size_t buflen)
3238{
3239 struct hostapd_channel_data *chnl;
3240 int ret, i, j;
3241 char *pos, *end, *hmode;
3242
3243 pos = buf;
3244 end = pos + buflen;
3245
3246 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3247 switch (wpa_s->hw.modes[j].mode) {
3248 case HOSTAPD_MODE_IEEE80211B:
3249 hmode = "B";
3250 break;
3251 case HOSTAPD_MODE_IEEE80211G:
3252 hmode = "G";
3253 break;
3254 case HOSTAPD_MODE_IEEE80211A:
3255 hmode = "A";
3256 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003257 case HOSTAPD_MODE_IEEE80211AD:
3258 hmode = "AD";
3259 break;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003260 default:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003261 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003262 }
3263 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
3264 if (ret < 0 || ret >= end - pos)
3265 return pos - buf;
3266 pos += ret;
3267 chnl = wpa_s->hw.modes[j].channels;
3268 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003269 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3270 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003271 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
3272 if (ret < 0 || ret >= end - pos)
3273 return pos - buf;
3274 pos += ret;
3275 }
3276 ret = os_snprintf(pos, end - pos, "\n");
3277 if (ret < 0 || ret >= end - pos)
3278 return pos - buf;
3279 pos += ret;
3280 }
3281
3282 return pos - buf;
3283}
3284
3285
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003286static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3287 char *buf, size_t buflen)
3288{
3289 struct hostapd_channel_data *chnl;
3290 int ret, i, j;
3291 char *pos, *end, *hmode;
3292
3293 pos = buf;
3294 end = pos + buflen;
3295
3296 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3297 switch (wpa_s->hw.modes[j].mode) {
3298 case HOSTAPD_MODE_IEEE80211B:
3299 hmode = "B";
3300 break;
3301 case HOSTAPD_MODE_IEEE80211G:
3302 hmode = "G";
3303 break;
3304 case HOSTAPD_MODE_IEEE80211A:
3305 hmode = "A";
3306 break;
3307 case HOSTAPD_MODE_IEEE80211AD:
3308 hmode = "AD";
3309 break;
3310 default:
3311 continue;
3312 }
3313 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3314 hmode);
3315 if (ret < 0 || ret >= end - pos)
3316 return pos - buf;
3317 pos += ret;
3318 chnl = wpa_s->hw.modes[j].channels;
3319 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3320 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3321 continue;
Dmitry Shmidt5da5e352014-02-03 13:30:46 -08003322 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003323 chnl[i].chan, chnl[i].freq,
3324 chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
Dmitry Shmidt5da5e352014-02-03 13:30:46 -08003325 " (NO_IBSS)" : "",
3326 chnl[i].flag & HOSTAPD_CHAN_RADAR ?
3327 " (DFS)" : "");
3328
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003329 if (ret < 0 || ret >= end - pos)
3330 return pos - buf;
3331 pos += ret;
3332 }
3333 ret = os_snprintf(pos, end - pos, "\n");
3334 if (ret < 0 || ret >= end - pos)
3335 return pos - buf;
3336 pos += ret;
3337 }
3338
3339 return pos - buf;
3340}
3341
3342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003343static int wpa_supplicant_ctrl_iface_get_capability(
3344 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3345 size_t buflen)
3346{
3347 struct wpa_driver_capa capa;
3348 int res;
3349 char *strict;
3350 char field[30];
3351 size_t len;
3352
3353 /* Determine whether or not strict checking was requested */
3354 len = os_strlcpy(field, _field, sizeof(field));
3355 if (len >= sizeof(field))
3356 return -1;
3357 strict = os_strchr(field, ' ');
3358 if (strict != NULL) {
3359 *strict++ = '\0';
3360 if (os_strcmp(strict, "strict") != 0)
3361 return -1;
3362 }
3363
3364 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3365 field, strict ? strict : "");
3366
3367 if (os_strcmp(field, "eap") == 0) {
3368 return eap_get_names(buf, buflen);
3369 }
3370
3371 res = wpa_drv_get_capa(wpa_s, &capa);
3372
3373 if (os_strcmp(field, "pairwise") == 0)
3374 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3375 buf, buflen);
3376
3377 if (os_strcmp(field, "group") == 0)
3378 return ctrl_iface_get_capability_group(res, strict, &capa,
3379 buf, buflen);
3380
3381 if (os_strcmp(field, "key_mgmt") == 0)
3382 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3383 buf, buflen);
3384
3385 if (os_strcmp(field, "proto") == 0)
3386 return ctrl_iface_get_capability_proto(res, strict, &capa,
3387 buf, buflen);
3388
3389 if (os_strcmp(field, "auth_alg") == 0)
3390 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
3391 buf, buflen);
3392
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003393 if (os_strcmp(field, "modes") == 0)
3394 return ctrl_iface_get_capability_modes(res, strict, &capa,
3395 buf, buflen);
3396
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003397 if (os_strcmp(field, "channels") == 0)
3398 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3399
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003400 if (os_strcmp(field, "freq") == 0)
3401 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3402
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003403#ifdef CONFIG_TDLS
3404 if (os_strcmp(field, "tdls") == 0)
3405 return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
3406#endif /* CONFIG_TDLS */
3407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003408 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3409 field);
3410
3411 return -1;
3412}
3413
3414
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003415#ifdef CONFIG_INTERWORKING
3416static char * anqp_add_hex(char *pos, char *end, const char *title,
3417 struct wpabuf *data)
3418{
3419 char *start = pos;
3420 size_t i;
3421 int ret;
3422 const u8 *d;
3423
3424 if (data == NULL)
3425 return start;
3426
3427 ret = os_snprintf(pos, end - pos, "%s=", title);
3428 if (ret < 0 || ret >= end - pos)
3429 return start;
3430 pos += ret;
3431
3432 d = wpabuf_head_u8(data);
3433 for (i = 0; i < wpabuf_len(data); i++) {
3434 ret = os_snprintf(pos, end - pos, "%02x", *d++);
3435 if (ret < 0 || ret >= end - pos)
3436 return start;
3437 pos += ret;
3438 }
3439
3440 ret = os_snprintf(pos, end - pos, "\n");
3441 if (ret < 0 || ret >= end - pos)
3442 return start;
3443 pos += ret;
3444
3445 return pos;
3446}
3447#endif /* CONFIG_INTERWORKING */
3448
3449
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003450static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3451 unsigned long mask, char *buf, size_t buflen)
3452{
3453 size_t i;
3454 int ret;
3455 char *pos, *end;
3456 const u8 *ie, *ie2;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003457
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003458 pos = buf;
3459 end = buf + buflen;
3460
3461 if (mask & WPA_BSS_MASK_ID) {
3462 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
3463 if (ret < 0 || ret >= end - pos)
3464 return 0;
3465 pos += ret;
3466 }
3467
3468 if (mask & WPA_BSS_MASK_BSSID) {
3469 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3470 MAC2STR(bss->bssid));
3471 if (ret < 0 || ret >= end - pos)
3472 return 0;
3473 pos += ret;
3474 }
3475
3476 if (mask & WPA_BSS_MASK_FREQ) {
3477 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
3478 if (ret < 0 || ret >= end - pos)
3479 return 0;
3480 pos += ret;
3481 }
3482
3483 if (mask & WPA_BSS_MASK_BEACON_INT) {
3484 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3485 bss->beacon_int);
3486 if (ret < 0 || ret >= end - pos)
3487 return 0;
3488 pos += ret;
3489 }
3490
3491 if (mask & WPA_BSS_MASK_CAPABILITIES) {
3492 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3493 bss->caps);
3494 if (ret < 0 || ret >= end - pos)
3495 return 0;
3496 pos += ret;
3497 }
3498
3499 if (mask & WPA_BSS_MASK_QUAL) {
3500 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
3501 if (ret < 0 || ret >= end - pos)
3502 return 0;
3503 pos += ret;
3504 }
3505
3506 if (mask & WPA_BSS_MASK_NOISE) {
3507 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
3508 if (ret < 0 || ret >= end - pos)
3509 return 0;
3510 pos += ret;
3511 }
3512
3513 if (mask & WPA_BSS_MASK_LEVEL) {
3514 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
3515 if (ret < 0 || ret >= end - pos)
3516 return 0;
3517 pos += ret;
3518 }
3519
3520 if (mask & WPA_BSS_MASK_TSF) {
3521 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3522 (unsigned long long) bss->tsf);
3523 if (ret < 0 || ret >= end - pos)
3524 return 0;
3525 pos += ret;
3526 }
3527
3528 if (mask & WPA_BSS_MASK_AGE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003529 struct os_reltime now;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003530
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003531 os_get_reltime(&now);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003532 ret = os_snprintf(pos, end - pos, "age=%d\n",
3533 (int) (now.sec - bss->last_update.sec));
3534 if (ret < 0 || ret >= end - pos)
3535 return 0;
3536 pos += ret;
3537 }
3538
3539 if (mask & WPA_BSS_MASK_IE) {
3540 ret = os_snprintf(pos, end - pos, "ie=");
3541 if (ret < 0 || ret >= end - pos)
3542 return 0;
3543 pos += ret;
3544
3545 ie = (const u8 *) (bss + 1);
3546 for (i = 0; i < bss->ie_len; i++) {
3547 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
3548 if (ret < 0 || ret >= end - pos)
3549 return 0;
3550 pos += ret;
3551 }
3552
3553 ret = os_snprintf(pos, end - pos, "\n");
3554 if (ret < 0 || ret >= end - pos)
3555 return 0;
3556 pos += ret;
3557 }
3558
3559 if (mask & WPA_BSS_MASK_FLAGS) {
3560 ret = os_snprintf(pos, end - pos, "flags=");
3561 if (ret < 0 || ret >= end - pos)
3562 return 0;
3563 pos += ret;
3564
3565 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3566 if (ie)
3567 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3568 2 + ie[1]);
3569 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3570 if (ie2)
3571 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3572 2 + ie2[1]);
3573 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3574 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3575 ret = os_snprintf(pos, end - pos, "[WEP]");
3576 if (ret < 0 || ret >= end - pos)
3577 return 0;
3578 pos += ret;
3579 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003580 if (bss_is_dmg(bss)) {
3581 const char *s;
3582 ret = os_snprintf(pos, end - pos, "[DMG]");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003583 if (ret < 0 || ret >= end - pos)
3584 return 0;
3585 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003586 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
3587 case IEEE80211_CAP_DMG_IBSS:
3588 s = "[IBSS]";
3589 break;
3590 case IEEE80211_CAP_DMG_AP:
3591 s = "[ESS]";
3592 break;
3593 case IEEE80211_CAP_DMG_PBSS:
3594 s = "[PBSS]";
3595 break;
3596 default:
3597 s = "";
3598 break;
3599 }
3600 ret = os_snprintf(pos, end - pos, "%s", s);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003601 if (ret < 0 || ret >= end - pos)
3602 return 0;
3603 pos += ret;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003604 } else {
3605 if (bss->caps & IEEE80211_CAP_IBSS) {
3606 ret = os_snprintf(pos, end - pos, "[IBSS]");
3607 if (ret < 0 || ret >= end - pos)
3608 return 0;
3609 pos += ret;
3610 }
3611 if (bss->caps & IEEE80211_CAP_ESS) {
3612 ret = os_snprintf(pos, end - pos, "[ESS]");
3613 if (ret < 0 || ret >= end - pos)
3614 return 0;
3615 pos += ret;
3616 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003617 }
Dmitry Shmidt96571392013-10-14 12:54:46 -07003618 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
3619 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003620 ret = os_snprintf(pos, end - pos, "[P2P]");
3621 if (ret < 0 || ret >= end - pos)
3622 return 0;
3623 pos += ret;
3624 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003625#ifdef CONFIG_HS20
3626 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3627 ret = os_snprintf(pos, end - pos, "[HS20]");
3628 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003629 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003630 pos += ret;
3631 }
3632#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003633
3634 ret = os_snprintf(pos, end - pos, "\n");
3635 if (ret < 0 || ret >= end - pos)
3636 return 0;
3637 pos += ret;
3638 }
3639
3640 if (mask & WPA_BSS_MASK_SSID) {
3641 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3642 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3643 if (ret < 0 || ret >= end - pos)
3644 return 0;
3645 pos += ret;
3646 }
3647
3648#ifdef CONFIG_WPS
3649 if (mask & WPA_BSS_MASK_WPS_SCAN) {
3650 ie = (const u8 *) (bss + 1);
3651 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3652 if (ret < 0 || ret >= end - pos)
3653 return 0;
3654 pos += ret;
3655 }
3656#endif /* CONFIG_WPS */
3657
3658#ifdef CONFIG_P2P
3659 if (mask & WPA_BSS_MASK_P2P_SCAN) {
3660 ie = (const u8 *) (bss + 1);
3661 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3662 if (ret < 0 || ret >= end - pos)
3663 return 0;
3664 pos += ret;
3665 }
3666#endif /* CONFIG_P2P */
3667
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003668#ifdef CONFIG_WIFI_DISPLAY
3669 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3670 struct wpabuf *wfd;
3671 ie = (const u8 *) (bss + 1);
3672 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3673 WFD_IE_VENDOR_TYPE);
3674 if (wfd) {
3675 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003676 if (ret < 0 || ret >= end - pos) {
3677 wpabuf_free(wfd);
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003678 return 0;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003679 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003680 pos += ret;
3681
3682 pos += wpa_snprintf_hex(pos, end - pos,
3683 wpabuf_head(wfd),
3684 wpabuf_len(wfd));
3685 wpabuf_free(wfd);
3686
3687 ret = os_snprintf(pos, end - pos, "\n");
3688 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003689 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003690 pos += ret;
3691 }
3692 }
3693#endif /* CONFIG_WIFI_DISPLAY */
3694
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003695#ifdef CONFIG_INTERWORKING
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003696 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3697 struct wpa_bss_anqp *anqp = bss->anqp;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003698 pos = anqp_add_hex(pos, end, "anqp_venue_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003699 anqp->venue_name);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003700 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003701 anqp->network_auth_type);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003702 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003703 anqp->roaming_consortium);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003704 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003705 anqp->ip_addr_type_availability);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003706 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003707 anqp->nai_realm);
3708 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003709 pos = anqp_add_hex(pos, end, "anqp_domain_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003710 anqp->domain_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003711#ifdef CONFIG_HS20
3712 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003713 anqp->hs20_operator_friendly_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003714 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003715 anqp->hs20_wan_metrics);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003716 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003717 anqp->hs20_connection_capability);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003718 pos = anqp_add_hex(pos, end, "hs20_operating_class",
3719 anqp->hs20_operating_class);
3720 pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
3721 anqp->hs20_osu_providers_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003722#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003723 }
3724#endif /* CONFIG_INTERWORKING */
3725
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003726 if (mask & WPA_BSS_MASK_DELIM) {
3727 ret = os_snprintf(pos, end - pos, "====\n");
3728 if (ret < 0 || ret >= end - pos)
3729 return 0;
3730 pos += ret;
3731 }
Irfan Sheriffe2ea0082012-08-13 10:56:16 -07003732
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003733 return pos - buf;
3734}
3735
Dmitry Shmidt04949592012-07-19 12:16:46 -07003736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003737static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
3738 const char *cmd, char *buf,
3739 size_t buflen)
3740{
3741 u8 bssid[ETH_ALEN];
3742 size_t i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003743 struct wpa_bss *bss;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003744 struct wpa_bss *bsslast = NULL;
3745 struct dl_list *next;
3746 int ret = 0;
3747 int len;
3748 char *ctmp;
3749 unsigned long mask = WPA_BSS_MASK_ALL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003750
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003751 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
3752 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
3753 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003754 list_id);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003755 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
3756 list_id);
3757 } else { /* N1-N2 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003758 unsigned int id1, id2;
3759
3760 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
3761 wpa_printf(MSG_INFO, "Wrong BSS range "
3762 "format");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003763 return 0;
3764 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003765
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003766 if (*(cmd + 6) == '-')
3767 id1 = 0;
3768 else
3769 id1 = atoi(cmd + 6);
3770 ctmp++;
3771 if (*ctmp >= '0' && *ctmp <= '9')
3772 id2 = atoi(ctmp);
3773 else
3774 id2 = (unsigned int) -1;
3775 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
3776 if (id2 == (unsigned int) -1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003777 bsslast = dl_list_last(&wpa_s->bss_id,
3778 struct wpa_bss,
3779 list_id);
3780 else {
3781 bsslast = wpa_bss_get_id(wpa_s, id2);
3782 if (bsslast == NULL && bss && id2 > id1) {
3783 struct wpa_bss *tmp = bss;
3784 for (;;) {
3785 next = tmp->list_id.next;
3786 if (next == &wpa_s->bss_id)
3787 break;
3788 tmp = dl_list_entry(
3789 next, struct wpa_bss,
3790 list_id);
3791 if (tmp->id > id2)
3792 break;
3793 bsslast = tmp;
3794 }
3795 }
3796 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003797 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003798 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003799 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003800 else if (os_strncmp(cmd, "LAST", 4) == 0)
3801 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003802 else if (os_strncmp(cmd, "ID-", 3) == 0) {
3803 i = atoi(cmd + 3);
3804 bss = wpa_bss_get_id(wpa_s, i);
3805 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3806 i = atoi(cmd + 5);
3807 bss = wpa_bss_get_id(wpa_s, i);
3808 if (bss) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003809 next = bss->list_id.next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003810 if (next == &wpa_s->bss_id)
3811 bss = NULL;
3812 else
3813 bss = dl_list_entry(next, struct wpa_bss,
3814 list_id);
3815 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003816#ifdef CONFIG_P2P
3817 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
3818 if (hwaddr_aton(cmd + 13, bssid) == 0)
3819 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
3820 else
3821 bss = NULL;
3822#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003823 } else if (hwaddr_aton(cmd, bssid) == 0)
3824 bss = wpa_bss_get_bssid(wpa_s, bssid);
3825 else {
3826 struct wpa_bss *tmp;
3827 i = atoi(cmd);
3828 bss = NULL;
3829 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
3830 {
3831 if (i-- == 0) {
3832 bss = tmp;
3833 break;
3834 }
3835 }
3836 }
3837
Dmitry Shmidt04949592012-07-19 12:16:46 -07003838 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
3839 mask = strtoul(ctmp + 5, NULL, 0x10);
3840 if (mask == 0)
3841 mask = WPA_BSS_MASK_ALL;
3842 }
3843
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003844 if (bss == NULL)
3845 return 0;
3846
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003847 if (bsslast == NULL)
3848 bsslast = bss;
3849 do {
3850 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
3851 ret += len;
3852 buf += len;
3853 buflen -= len;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003854 if (bss == bsslast) {
3855 if ((mask & WPA_BSS_MASK_DELIM) && len &&
3856 (bss == dl_list_last(&wpa_s->bss_id,
3857 struct wpa_bss, list_id)))
3858 os_snprintf(buf - 5, 5, "####\n");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003859 break;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003860 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003861 next = bss->list_id.next;
3862 if (next == &wpa_s->bss_id)
3863 break;
3864 bss = dl_list_entry(next, struct wpa_bss, list_id);
3865 } while (bss && len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003867 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868}
3869
3870
3871static int wpa_supplicant_ctrl_iface_ap_scan(
3872 struct wpa_supplicant *wpa_s, char *cmd)
3873{
3874 int ap_scan = atoi(cmd);
3875 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
3876}
3877
3878
3879static int wpa_supplicant_ctrl_iface_scan_interval(
3880 struct wpa_supplicant *wpa_s, char *cmd)
3881{
3882 int scan_int = atoi(cmd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003883 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003884}
3885
3886
3887static int wpa_supplicant_ctrl_iface_bss_expire_age(
3888 struct wpa_supplicant *wpa_s, char *cmd)
3889{
3890 int expire_age = atoi(cmd);
3891 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
3892}
3893
3894
3895static int wpa_supplicant_ctrl_iface_bss_expire_count(
3896 struct wpa_supplicant *wpa_s, char *cmd)
3897{
3898 int expire_count = atoi(cmd);
3899 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
3900}
3901
3902
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07003903static int wpa_supplicant_ctrl_iface_bss_flush(
3904 struct wpa_supplicant *wpa_s, char *cmd)
3905{
3906 int flush_age = atoi(cmd);
3907
3908 if (flush_age == 0)
3909 wpa_bss_flush(wpa_s);
3910 else
3911 wpa_bss_flush_by_age(wpa_s, flush_age);
3912 return 0;
3913}
3914
3915
Dmitry Shmidt21de2142014-04-08 10:50:52 -07003916#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003917static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
3918{
3919 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
3920 /* MLME-DELETEKEYS.request */
3921 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
3922 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
3923 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
3924 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
3925#ifdef CONFIG_IEEE80211W
3926 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
3927 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
3928#endif /* CONFIG_IEEE80211W */
3929
3930 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
3931 0);
3932 /* MLME-SETPROTECTION.request(None) */
3933 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
3934 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
3935 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3936 wpa_sm_drop_sa(wpa_s->wpa);
3937}
Dmitry Shmidt21de2142014-04-08 10:50:52 -07003938#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939
3940
3941static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
3942 char *addr)
3943{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003944#ifdef CONFIG_NO_SCAN_PROCESSING
3945 return -1;
3946#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947 u8 bssid[ETH_ALEN];
3948 struct wpa_bss *bss;
3949 struct wpa_ssid *ssid = wpa_s->current_ssid;
3950
3951 if (hwaddr_aton(addr, bssid)) {
3952 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
3953 "address '%s'", addr);
3954 return -1;
3955 }
3956
3957 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
3958
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003959 if (!ssid) {
3960 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
3961 "configuration known for the target AP");
3962 return -1;
3963 }
3964
3965 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003966 if (!bss) {
3967 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
3968 "from BSS table");
3969 return -1;
3970 }
3971
3972 /*
3973 * TODO: Find best network configuration block from configuration to
3974 * allow roaming to other networks
3975 */
3976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003977 wpa_s->reassociate = 1;
3978 wpa_supplicant_connect(wpa_s, bss, ssid);
3979
3980 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003981#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003982}
3983
3984
3985#ifdef CONFIG_P2P
3986static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
3987{
3988 unsigned int timeout = atoi(cmd);
3989 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003990 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003991 u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003992 char *pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003993 unsigned int search_delay;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003994
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003995 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
3996 wpa_dbg(wpa_s, MSG_INFO,
3997 "Reject P2P_FIND since interface is disabled");
3998 return -1;
3999 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004000 if (os_strstr(cmd, "type=social"))
4001 type = P2P_FIND_ONLY_SOCIAL;
4002 else if (os_strstr(cmd, "type=progressive"))
4003 type = P2P_FIND_PROGRESSIVE;
4004
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004005 pos = os_strstr(cmd, "dev_id=");
4006 if (pos) {
4007 pos += 7;
4008 if (hwaddr_aton(pos, dev_id))
4009 return -1;
4010 _dev_id = dev_id;
4011 }
4012
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004013 pos = os_strstr(cmd, "dev_type=");
4014 if (pos) {
4015 pos += 9;
4016 if (wps_dev_type_str2bin(pos, dev_type) < 0)
4017 return -1;
4018 _dev_type = dev_type;
4019 }
4020
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004021 pos = os_strstr(cmd, "delay=");
4022 if (pos) {
4023 pos += 6;
4024 search_delay = atoi(pos);
4025 } else
4026 search_delay = wpas_p2p_search_delay(wpa_s);
4027
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004028 return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
4029 _dev_id, search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004030}
4031
4032
4033static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
4034 char *buf, size_t buflen)
4035{
4036 u8 addr[ETH_ALEN];
4037 char *pos, *pos2;
4038 char *pin = NULL;
4039 enum p2p_wps_method wps_method;
4040 int new_pin;
4041 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004042 int persistent_group, persistent_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004043 int join;
4044 int auth;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004045 int automatic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004046 int go_intent = -1;
4047 int freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004048 int pd;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004049 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004050
Dmitry Shmidt04949592012-07-19 12:16:46 -07004051 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
4052 * [persistent|persistent=<network id>]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004053 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004054 * [ht40] [vht] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004055
4056 if (hwaddr_aton(cmd, addr))
4057 return -1;
4058
4059 pos = cmd + 17;
4060 if (*pos != ' ')
4061 return -1;
4062 pos++;
4063
4064 persistent_group = os_strstr(pos, " persistent") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004065 pos2 = os_strstr(pos, " persistent=");
4066 if (pos2) {
4067 struct wpa_ssid *ssid;
4068 persistent_id = atoi(pos2 + 12);
4069 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4070 if (ssid == NULL || ssid->disabled != 2 ||
4071 ssid->mode != WPAS_MODE_P2P_GO) {
4072 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
4073 "SSID id=%d for persistent P2P group (GO)",
4074 persistent_id);
4075 return -1;
4076 }
4077 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004078 join = os_strstr(pos, " join") != NULL;
4079 auth = os_strstr(pos, " auth") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004080 automatic = os_strstr(pos, " auto") != NULL;
4081 pd = os_strstr(pos, " provdisc") != NULL;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004082 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4083 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4084 vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004085
4086 pos2 = os_strstr(pos, " go_intent=");
4087 if (pos2) {
4088 pos2 += 11;
4089 go_intent = atoi(pos2);
4090 if (go_intent < 0 || go_intent > 15)
4091 return -1;
4092 }
4093
4094 pos2 = os_strstr(pos, " freq=");
4095 if (pos2) {
4096 pos2 += 6;
4097 freq = atoi(pos2);
4098 if (freq <= 0)
4099 return -1;
4100 }
4101
4102 if (os_strncmp(pos, "pin", 3) == 0) {
4103 /* Request random PIN (to be displayed) and enable the PIN */
4104 wps_method = WPS_PIN_DISPLAY;
4105 } else if (os_strncmp(pos, "pbc", 3) == 0) {
4106 wps_method = WPS_PBC;
4107 } else {
4108 pin = pos;
4109 pos = os_strchr(pin, ' ');
4110 wps_method = WPS_PIN_KEYPAD;
4111 if (pos) {
4112 *pos++ = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004113 if (os_strncmp(pos, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004114 wps_method = WPS_PIN_DISPLAY;
4115 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004116 if (!wps_pin_str_valid(pin)) {
4117 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
4118 return 17;
4119 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004120 }
4121
4122 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004123 persistent_group, automatic, join,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004124 auth, go_intent, freq, persistent_id, pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004125 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004126 if (new_pin == -2) {
4127 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
4128 return 25;
4129 }
4130 if (new_pin == -3) {
4131 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
4132 return 25;
4133 }
4134 if (new_pin < 0)
4135 return -1;
4136 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
4137 ret = os_snprintf(buf, buflen, "%08d", new_pin);
4138 if (ret < 0 || (size_t) ret >= buflen)
4139 return -1;
4140 return ret;
4141 }
4142
4143 os_memcpy(buf, "OK\n", 3);
4144 return 3;
4145}
4146
4147
4148static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
4149{
4150 unsigned int timeout = atoi(cmd);
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004151 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4152 wpa_dbg(wpa_s, MSG_INFO,
4153 "Reject P2P_LISTEN since interface is disabled");
4154 return -1;
4155 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004156 return wpas_p2p_listen(wpa_s, timeout);
4157}
4158
4159
4160static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
4161{
4162 u8 addr[ETH_ALEN];
4163 char *pos;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004164 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004165
Dmitry Shmidt04949592012-07-19 12:16:46 -07004166 /* <addr> <config method> [join|auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004167
4168 if (hwaddr_aton(cmd, addr))
4169 return -1;
4170
4171 pos = cmd + 17;
4172 if (*pos != ' ')
4173 return -1;
4174 pos++;
4175
Dmitry Shmidt04949592012-07-19 12:16:46 -07004176 if (os_strstr(pos, " join") != NULL)
4177 use = WPAS_P2P_PD_FOR_JOIN;
4178 else if (os_strstr(pos, " auto") != NULL)
4179 use = WPAS_P2P_PD_AUTO;
4180
4181 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182}
4183
4184
4185static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
4186 size_t buflen)
4187{
4188 struct wpa_ssid *ssid = wpa_s->current_ssid;
4189
4190 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
4191 ssid->passphrase == NULL)
4192 return -1;
4193
4194 os_strlcpy(buf, ssid->passphrase, buflen);
4195 return os_strlen(buf);
4196}
4197
4198
4199static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
4200 char *buf, size_t buflen)
4201{
4202 u64 ref;
4203 int res;
4204 u8 dst_buf[ETH_ALEN], *dst;
4205 struct wpabuf *tlvs;
4206 char *pos;
4207 size_t len;
4208
4209 if (hwaddr_aton(cmd, dst_buf))
4210 return -1;
4211 dst = dst_buf;
4212 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
4213 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
4214 dst = NULL;
4215 pos = cmd + 17;
4216 if (*pos != ' ')
4217 return -1;
4218 pos++;
4219
4220 if (os_strncmp(pos, "upnp ", 5) == 0) {
4221 u8 version;
4222 pos += 5;
4223 if (hexstr2bin(pos, &version, 1) < 0)
4224 return -1;
4225 pos += 2;
4226 if (*pos != ' ')
4227 return -1;
4228 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004229 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004230#ifdef CONFIG_WIFI_DISPLAY
4231 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
4232 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
4233#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234 } else {
4235 len = os_strlen(pos);
4236 if (len & 1)
4237 return -1;
4238 len /= 2;
4239 tlvs = wpabuf_alloc(len);
4240 if (tlvs == NULL)
4241 return -1;
4242 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
4243 wpabuf_free(tlvs);
4244 return -1;
4245 }
4246
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004247 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004248 wpabuf_free(tlvs);
4249 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004250 if (ref == 0)
4251 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004252 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
4253 if (res < 0 || (unsigned) res >= buflen)
4254 return -1;
4255 return res;
4256}
4257
4258
4259static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
4260 char *cmd)
4261{
4262 long long unsigned val;
4263 u64 req;
4264 if (sscanf(cmd, "%llx", &val) != 1)
4265 return -1;
4266 req = val;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004267 return wpas_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004268}
4269
4270
4271static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
4272{
4273 int freq;
4274 u8 dst[ETH_ALEN];
4275 u8 dialog_token;
4276 struct wpabuf *resp_tlvs;
4277 char *pos, *pos2;
4278 size_t len;
4279
4280 pos = os_strchr(cmd, ' ');
4281 if (pos == NULL)
4282 return -1;
4283 *pos++ = '\0';
4284 freq = atoi(cmd);
4285 if (freq == 0)
4286 return -1;
4287
4288 if (hwaddr_aton(pos, dst))
4289 return -1;
4290 pos += 17;
4291 if (*pos != ' ')
4292 return -1;
4293 pos++;
4294
4295 pos2 = os_strchr(pos, ' ');
4296 if (pos2 == NULL)
4297 return -1;
4298 *pos2++ = '\0';
4299 dialog_token = atoi(pos);
4300
4301 len = os_strlen(pos2);
4302 if (len & 1)
4303 return -1;
4304 len /= 2;
4305 resp_tlvs = wpabuf_alloc(len);
4306 if (resp_tlvs == NULL)
4307 return -1;
4308 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
4309 wpabuf_free(resp_tlvs);
4310 return -1;
4311 }
4312
4313 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
4314 wpabuf_free(resp_tlvs);
4315 return 0;
4316}
4317
4318
4319static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
4320 char *cmd)
4321{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004322 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
4323 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
4325 return 0;
4326}
4327
4328
4329static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
4330 char *cmd)
4331{
4332 char *pos;
4333 size_t len;
4334 struct wpabuf *query, *resp;
4335
4336 pos = os_strchr(cmd, ' ');
4337 if (pos == NULL)
4338 return -1;
4339 *pos++ = '\0';
4340
4341 len = os_strlen(cmd);
4342 if (len & 1)
4343 return -1;
4344 len /= 2;
4345 query = wpabuf_alloc(len);
4346 if (query == NULL)
4347 return -1;
4348 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4349 wpabuf_free(query);
4350 return -1;
4351 }
4352
4353 len = os_strlen(pos);
4354 if (len & 1) {
4355 wpabuf_free(query);
4356 return -1;
4357 }
4358 len /= 2;
4359 resp = wpabuf_alloc(len);
4360 if (resp == NULL) {
4361 wpabuf_free(query);
4362 return -1;
4363 }
4364 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4365 wpabuf_free(query);
4366 wpabuf_free(resp);
4367 return -1;
4368 }
4369
4370 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4371 wpabuf_free(query);
4372 wpabuf_free(resp);
4373 return -1;
4374 }
4375 return 0;
4376}
4377
4378
4379static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4380{
4381 char *pos;
4382 u8 version;
4383
4384 pos = os_strchr(cmd, ' ');
4385 if (pos == NULL)
4386 return -1;
4387 *pos++ = '\0';
4388
4389 if (hexstr2bin(cmd, &version, 1) < 0)
4390 return -1;
4391
4392 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4393}
4394
4395
4396static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4397{
4398 char *pos;
4399
4400 pos = os_strchr(cmd, ' ');
4401 if (pos == NULL)
4402 return -1;
4403 *pos++ = '\0';
4404
4405 if (os_strcmp(cmd, "bonjour") == 0)
4406 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4407 if (os_strcmp(cmd, "upnp") == 0)
4408 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4409 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4410 return -1;
4411}
4412
4413
4414static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4415 char *cmd)
4416{
4417 size_t len;
4418 struct wpabuf *query;
4419 int ret;
4420
4421 len = os_strlen(cmd);
4422 if (len & 1)
4423 return -1;
4424 len /= 2;
4425 query = wpabuf_alloc(len);
4426 if (query == NULL)
4427 return -1;
4428 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4429 wpabuf_free(query);
4430 return -1;
4431 }
4432
4433 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4434 wpabuf_free(query);
4435 return ret;
4436}
4437
4438
4439static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4440{
4441 char *pos;
4442 u8 version;
4443
4444 pos = os_strchr(cmd, ' ');
4445 if (pos == NULL)
4446 return -1;
4447 *pos++ = '\0';
4448
4449 if (hexstr2bin(cmd, &version, 1) < 0)
4450 return -1;
4451
4452 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4453}
4454
4455
4456static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4457{
4458 char *pos;
4459
4460 pos = os_strchr(cmd, ' ');
4461 if (pos == NULL)
4462 return -1;
4463 *pos++ = '\0';
4464
4465 if (os_strcmp(cmd, "bonjour") == 0)
4466 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4467 if (os_strcmp(cmd, "upnp") == 0)
4468 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4469 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4470 return -1;
4471}
4472
4473
4474static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4475{
4476 u8 addr[ETH_ALEN];
4477
4478 /* <addr> */
4479
4480 if (hwaddr_aton(cmd, addr))
4481 return -1;
4482
4483 return wpas_p2p_reject(wpa_s, addr);
4484}
4485
4486
4487static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4488{
4489 char *pos;
4490 int id;
4491 struct wpa_ssid *ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004492 u8 *_peer = NULL, peer[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004493 int freq = 0, pref_freq = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004494 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004495
4496 id = atoi(cmd);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004497 pos = os_strstr(cmd, " peer=");
4498 if (pos) {
4499 pos += 6;
4500 if (hwaddr_aton(pos, peer))
4501 return -1;
4502 _peer = peer;
4503 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504 ssid = wpa_config_get_network(wpa_s->conf, id);
4505 if (ssid == NULL || ssid->disabled != 2) {
4506 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4507 "for persistent P2P group",
4508 id);
4509 return -1;
4510 }
4511
Jouni Malinen31be0a42012-08-31 21:20:51 +03004512 pos = os_strstr(cmd, " freq=");
4513 if (pos) {
4514 pos += 6;
4515 freq = atoi(pos);
4516 if (freq <= 0)
4517 return -1;
4518 }
4519
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004520 pos = os_strstr(cmd, " pref=");
4521 if (pos) {
4522 pos += 6;
4523 pref_freq = atoi(pos);
4524 if (pref_freq <= 0)
4525 return -1;
4526 }
4527
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004528 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4529 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4530 vht;
Jouni Malinen31be0a42012-08-31 21:20:51 +03004531
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004532 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
4533 pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004534}
4535
4536
4537static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
4538{
4539 char *pos;
4540 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
4541
4542 pos = os_strstr(cmd, " peer=");
4543 if (!pos)
4544 return -1;
4545
4546 *pos = '\0';
4547 pos += 6;
4548 if (hwaddr_aton(pos, peer)) {
4549 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
4550 return -1;
4551 }
4552
4553 pos = os_strstr(pos, " go_dev_addr=");
4554 if (pos) {
4555 pos += 13;
4556 if (hwaddr_aton(pos, go_dev_addr)) {
4557 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
4558 pos);
4559 return -1;
4560 }
4561 go_dev = go_dev_addr;
4562 }
4563
4564 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
4565}
4566
4567
4568static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
4569{
4570 if (os_strncmp(cmd, "persistent=", 11) == 0)
4571 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
4572 if (os_strncmp(cmd, "group=", 6) == 0)
4573 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
4574
4575 return -1;
4576}
4577
4578
4579static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004580 char *cmd, int freq, int ht40,
4581 int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004582{
4583 int id;
4584 struct wpa_ssid *ssid;
4585
4586 id = atoi(cmd);
4587 ssid = wpa_config_get_network(wpa_s->conf, id);
4588 if (ssid == NULL || ssid->disabled != 2) {
4589 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4590 "for persistent P2P group",
4591 id);
4592 return -1;
4593 }
4594
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004595 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004596 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004597}
4598
4599
4600static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
4601{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004602 int freq = 0, ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004603 char *pos;
4604
4605 pos = os_strstr(cmd, "freq=");
4606 if (pos)
4607 freq = atoi(pos + 5);
4608
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004609 vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
4610 ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4611 vht;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004613 if (os_strncmp(cmd, "persistent=", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004614 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004615 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004616 if (os_strcmp(cmd, "persistent") == 0 ||
4617 os_strncmp(cmd, "persistent ", 11) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004618 return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619 if (os_strncmp(cmd, "freq=", 5) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004620 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004621 if (ht40)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004622 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623
4624 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
4625 cmd);
4626 return -1;
4627}
4628
4629
4630static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
4631 char *buf, size_t buflen)
4632{
4633 u8 addr[ETH_ALEN], *addr_ptr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004634 int next, res;
4635 const struct p2p_peer_info *info;
4636 char *pos, *end;
4637 char devtype[WPS_DEV_TYPE_BUFSIZE];
4638 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004639 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004640
4641 if (!wpa_s->global->p2p)
4642 return -1;
4643
4644 if (os_strcmp(cmd, "FIRST") == 0) {
4645 addr_ptr = NULL;
4646 next = 0;
4647 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4648 if (hwaddr_aton(cmd + 5, addr) < 0)
4649 return -1;
4650 addr_ptr = addr;
4651 next = 1;
4652 } else {
4653 if (hwaddr_aton(cmd, addr) < 0)
4654 return -1;
4655 addr_ptr = addr;
4656 next = 0;
4657 }
4658
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004659 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4660 if (info == NULL)
4661 return -1;
4662
4663 pos = buf;
4664 end = buf + buflen;
4665
4666 res = os_snprintf(pos, end - pos, MACSTR "\n"
4667 "pri_dev_type=%s\n"
4668 "device_name=%s\n"
4669 "manufacturer=%s\n"
4670 "model_name=%s\n"
4671 "model_number=%s\n"
4672 "serial_number=%s\n"
4673 "config_methods=0x%x\n"
4674 "dev_capab=0x%x\n"
4675 "group_capab=0x%x\n"
4676 "level=%d\n",
4677 MAC2STR(info->p2p_device_addr),
4678 wps_dev_type_bin2str(info->pri_dev_type,
4679 devtype, sizeof(devtype)),
4680 info->device_name,
4681 info->manufacturer,
4682 info->model_name,
4683 info->model_number,
4684 info->serial_number,
4685 info->config_methods,
4686 info->dev_capab,
4687 info->group_capab,
4688 info->level);
4689 if (res < 0 || res >= end - pos)
4690 return pos - buf;
4691 pos += res;
4692
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004693 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4694 {
4695 const u8 *t;
4696 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4697 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4698 wps_dev_type_bin2str(t, devtype,
4699 sizeof(devtype)));
4700 if (res < 0 || res >= end - pos)
4701 return pos - buf;
4702 pos += res;
4703 }
4704
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004705 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004706 if (ssid) {
4707 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
4708 if (res < 0 || res >= end - pos)
4709 return pos - buf;
4710 pos += res;
4711 }
4712
4713 res = p2p_get_peer_info_txt(info, pos, end - pos);
4714 if (res < 0)
4715 return pos - buf;
4716 pos += res;
4717
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07004718 if (info->vendor_elems) {
4719 res = os_snprintf(pos, end - pos, "vendor_elems=");
4720 if (res < 0 || res >= end - pos)
4721 return pos - buf;
4722 pos += res;
4723
4724 pos += wpa_snprintf_hex(pos, end - pos,
4725 wpabuf_head(info->vendor_elems),
4726 wpabuf_len(info->vendor_elems));
4727
4728 res = os_snprintf(pos, end - pos, "\n");
4729 if (res < 0 || res >= end - pos)
4730 return pos - buf;
4731 pos += res;
4732 }
4733
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004734 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735}
4736
4737
Dmitry Shmidt04949592012-07-19 12:16:46 -07004738static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
4739 const char *param)
4740{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004741 unsigned int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004742
4743 if (wpa_s->global->p2p == NULL)
4744 return -1;
4745
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004746 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
4747 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004748
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004749 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
4750 struct wpa_freq_range *freq;
4751 freq = &wpa_s->global->p2p_disallow_freq.range[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004752 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004753 freq->min, freq->max);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004754 }
4755
Dmitry Shmidt04949592012-07-19 12:16:46 -07004756 wpas_p2p_update_channel_list(wpa_s);
4757 return 0;
4758}
4759
4760
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004761static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
4762{
4763 char *param;
4764
4765 if (wpa_s->global->p2p == NULL)
4766 return -1;
4767
4768 param = os_strchr(cmd, ' ');
4769 if (param == NULL)
4770 return -1;
4771 *param++ = '\0';
4772
4773 if (os_strcmp(cmd, "discoverability") == 0) {
4774 p2p_set_client_discoverability(wpa_s->global->p2p,
4775 atoi(param));
4776 return 0;
4777 }
4778
4779 if (os_strcmp(cmd, "managed") == 0) {
4780 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
4781 return 0;
4782 }
4783
4784 if (os_strcmp(cmd, "listen_channel") == 0) {
4785 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004786 atoi(param), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004787 }
4788
4789 if (os_strcmp(cmd, "ssid_postfix") == 0) {
4790 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
4791 os_strlen(param));
4792 }
4793
4794 if (os_strcmp(cmd, "noa") == 0) {
4795 char *pos;
4796 int count, start, duration;
4797 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
4798 count = atoi(param);
4799 pos = os_strchr(param, ',');
4800 if (pos == NULL)
4801 return -1;
4802 pos++;
4803 start = atoi(pos);
4804 pos = os_strchr(pos, ',');
4805 if (pos == NULL)
4806 return -1;
4807 pos++;
4808 duration = atoi(pos);
4809 if (count < 0 || count > 255 || start < 0 || duration < 0)
4810 return -1;
4811 if (count == 0 && duration > 0)
4812 return -1;
4813 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
4814 "start=%d duration=%d", count, start, duration);
4815 return wpas_p2p_set_noa(wpa_s, count, start, duration);
4816 }
4817
4818 if (os_strcmp(cmd, "ps") == 0)
4819 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
4820
4821 if (os_strcmp(cmd, "oppps") == 0)
4822 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
4823
4824 if (os_strcmp(cmd, "ctwindow") == 0)
4825 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
4826
4827 if (os_strcmp(cmd, "disabled") == 0) {
4828 wpa_s->global->p2p_disabled = atoi(param);
4829 wpa_printf(MSG_DEBUG, "P2P functionality %s",
4830 wpa_s->global->p2p_disabled ?
4831 "disabled" : "enabled");
4832 if (wpa_s->global->p2p_disabled) {
4833 wpas_p2p_stop_find(wpa_s);
4834 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4835 p2p_flush(wpa_s->global->p2p);
4836 }
4837 return 0;
4838 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004839
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004840 if (os_strcmp(cmd, "conc_pref") == 0) {
4841 if (os_strcmp(param, "sta") == 0)
4842 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
4843 else if (os_strcmp(param, "p2p") == 0)
4844 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004845 else {
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004846 wpa_printf(MSG_INFO, "Invalid conc_pref value");
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004847 return -1;
4848 }
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004849 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004850 "%s", param);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004851 return 0;
4852 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004853
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004854 if (os_strcmp(cmd, "force_long_sd") == 0) {
4855 wpa_s->force_long_sd = atoi(param);
4856 return 0;
4857 }
4858
4859 if (os_strcmp(cmd, "peer_filter") == 0) {
4860 u8 addr[ETH_ALEN];
4861 if (hwaddr_aton(param, addr))
4862 return -1;
4863 p2p_set_peer_filter(wpa_s->global->p2p, addr);
4864 return 0;
4865 }
4866
4867 if (os_strcmp(cmd, "cross_connect") == 0)
4868 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
4869
4870 if (os_strcmp(cmd, "go_apsd") == 0) {
4871 if (os_strcmp(param, "disable") == 0)
4872 wpa_s->set_ap_uapsd = 0;
4873 else {
4874 wpa_s->set_ap_uapsd = 1;
4875 wpa_s->ap_uapsd = atoi(param);
4876 }
4877 return 0;
4878 }
4879
4880 if (os_strcmp(cmd, "client_apsd") == 0) {
4881 if (os_strcmp(param, "disable") == 0)
4882 wpa_s->set_sta_uapsd = 0;
4883 else {
4884 int be, bk, vi, vo;
4885 char *pos;
4886 /* format: BE,BK,VI,VO;max SP Length */
4887 be = atoi(param);
4888 pos = os_strchr(param, ',');
4889 if (pos == NULL)
4890 return -1;
4891 pos++;
4892 bk = atoi(pos);
4893 pos = os_strchr(pos, ',');
4894 if (pos == NULL)
4895 return -1;
4896 pos++;
4897 vi = atoi(pos);
4898 pos = os_strchr(pos, ',');
4899 if (pos == NULL)
4900 return -1;
4901 pos++;
4902 vo = atoi(pos);
4903 /* ignore max SP Length for now */
4904
4905 wpa_s->set_sta_uapsd = 1;
4906 wpa_s->sta_uapsd = 0;
4907 if (be)
4908 wpa_s->sta_uapsd |= BIT(0);
4909 if (bk)
4910 wpa_s->sta_uapsd |= BIT(1);
4911 if (vi)
4912 wpa_s->sta_uapsd |= BIT(2);
4913 if (vo)
4914 wpa_s->sta_uapsd |= BIT(3);
4915 }
4916 return 0;
4917 }
4918
Dmitry Shmidt04949592012-07-19 12:16:46 -07004919 if (os_strcmp(cmd, "disallow_freq") == 0)
4920 return p2p_ctrl_disallow_freq(wpa_s, param);
4921
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004922 if (os_strcmp(cmd, "disc_int") == 0) {
4923 int min_disc_int, max_disc_int, max_disc_tu;
4924 char *pos;
4925
4926 pos = param;
4927
4928 min_disc_int = atoi(pos);
4929 pos = os_strchr(pos, ' ');
4930 if (pos == NULL)
4931 return -1;
4932 *pos++ = '\0';
4933
4934 max_disc_int = atoi(pos);
4935 pos = os_strchr(pos, ' ');
4936 if (pos == NULL)
4937 return -1;
4938 *pos++ = '\0';
4939
4940 max_disc_tu = atoi(pos);
4941
4942 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
4943 max_disc_int, max_disc_tu);
4944 }
4945
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004946 if (os_strcmp(cmd, "per_sta_psk") == 0) {
4947 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
4948 return 0;
4949 }
4950
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004951#ifdef CONFIG_WPS_NFC
4952 if (os_strcmp(cmd, "nfc_tag") == 0)
4953 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
4954#endif /* CONFIG_WPS_NFC */
4955
4956 if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
4957 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
4958 return 0;
4959 }
4960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004961 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
4962 cmd);
4963
4964 return -1;
4965}
4966
4967
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004968static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
4969{
4970 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4971 wpa_s->force_long_sd = 0;
4972 if (wpa_s->global->p2p)
4973 p2p_flush(wpa_s->global->p2p);
4974}
4975
4976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004977static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
4978{
4979 char *pos, *pos2;
4980 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
4981
4982 if (cmd[0]) {
4983 pos = os_strchr(cmd, ' ');
4984 if (pos == NULL)
4985 return -1;
4986 *pos++ = '\0';
4987 dur1 = atoi(cmd);
4988
4989 pos2 = os_strchr(pos, ' ');
4990 if (pos2)
4991 *pos2++ = '\0';
4992 int1 = atoi(pos);
4993 } else
4994 pos2 = NULL;
4995
4996 if (pos2) {
4997 pos = os_strchr(pos2, ' ');
4998 if (pos == NULL)
4999 return -1;
5000 *pos++ = '\0';
5001 dur2 = atoi(pos2);
5002 int2 = atoi(pos);
5003 }
5004
5005 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
5006}
5007
5008
5009static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
5010{
5011 char *pos;
5012 unsigned int period = 0, interval = 0;
5013
5014 if (cmd[0]) {
5015 pos = os_strchr(cmd, ' ');
5016 if (pos == NULL)
5017 return -1;
5018 *pos++ = '\0';
5019 period = atoi(cmd);
5020 interval = atoi(pos);
5021 }
5022
5023 return wpas_p2p_ext_listen(wpa_s, period, interval);
5024}
5025
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005026
5027static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
5028{
5029 const char *pos;
5030 u8 peer[ETH_ALEN];
5031 int iface_addr = 0;
5032
5033 pos = cmd;
5034 if (os_strncmp(pos, "iface=", 6) == 0) {
5035 iface_addr = 1;
5036 pos += 6;
5037 }
5038 if (hwaddr_aton(pos, peer))
5039 return -1;
5040
5041 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
5042 return 0;
5043}
5044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005045#endif /* CONFIG_P2P */
5046
5047
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005048static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
5049{
5050 struct wpa_freq_range_list ranges;
5051 int *freqs = NULL;
5052 struct hostapd_hw_modes *mode;
5053 u16 i;
5054
5055 if (wpa_s->hw.modes == NULL)
5056 return NULL;
5057
5058 os_memset(&ranges, 0, sizeof(ranges));
5059 if (freq_range_list_parse(&ranges, val) < 0)
5060 return NULL;
5061
5062 for (i = 0; i < wpa_s->hw.num_modes; i++) {
5063 int j;
5064
5065 mode = &wpa_s->hw.modes[i];
5066 for (j = 0; j < mode->num_channels; j++) {
5067 unsigned int freq;
5068
5069 if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
5070 continue;
5071
5072 freq = mode->channels[j].freq;
5073 if (!freq_range_list_includes(&ranges, freq))
5074 continue;
5075
5076 int_array_add_unique(&freqs, freq);
5077 }
5078 }
5079
5080 os_free(ranges.range);
5081 return freqs;
5082}
5083
5084
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005085#ifdef CONFIG_INTERWORKING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005086
5087static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
5088{
5089 int auto_sel = 0;
5090 int *freqs = NULL;
5091
5092 if (param) {
5093 char *pos;
5094
5095 auto_sel = os_strstr(param, "auto") != NULL;
5096
5097 pos = os_strstr(param, "freq=");
5098 if (pos) {
5099 freqs = freq_range_to_channel_list(wpa_s, pos + 5);
5100 if (freqs == NULL)
5101 return -1;
5102 }
5103
5104 }
5105
5106 return interworking_select(wpa_s, auto_sel, freqs);
5107}
5108
5109
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005110static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
5111{
5112 u8 bssid[ETH_ALEN];
5113 struct wpa_bss *bss;
5114
5115 if (hwaddr_aton(dst, bssid)) {
5116 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
5117 return -1;
5118 }
5119
5120 bss = wpa_bss_get_bssid(wpa_s, bssid);
5121 if (bss == NULL) {
5122 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
5123 MAC2STR(bssid));
5124 return -1;
5125 }
5126
5127 return interworking_connect(wpa_s, bss);
5128}
5129
5130
5131static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
5132{
5133 u8 dst_addr[ETH_ALEN];
5134 int used;
5135 char *pos;
5136#define MAX_ANQP_INFO_ID 100
5137 u16 id[MAX_ANQP_INFO_ID];
5138 size_t num_id = 0;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005139 u32 subtypes = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005140
5141 used = hwaddr_aton2(dst, dst_addr);
5142 if (used < 0)
5143 return -1;
5144 pos = dst + used;
5145 while (num_id < MAX_ANQP_INFO_ID) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07005146 if (os_strncmp(pos, "hs20:", 5) == 0) {
5147#ifdef CONFIG_HS20
5148 int num = atoi(pos + 5);
5149 if (num <= 0 || num > 31)
5150 return -1;
5151 subtypes |= BIT(num);
5152#else /* CONFIG_HS20 */
5153 return -1;
5154#endif /* CONFIG_HS20 */
5155 } else {
5156 id[num_id] = atoi(pos);
5157 if (id[num_id])
5158 num_id++;
5159 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005160 pos = os_strchr(pos + 1, ',');
5161 if (pos == NULL)
5162 break;
5163 pos++;
5164 }
5165
5166 if (num_id == 0)
5167 return -1;
5168
Dmitry Shmidt15907092014-03-25 10:42:57 -07005169 return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005170}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005171
5172
5173static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
5174{
5175 u8 dst_addr[ETH_ALEN];
5176 struct wpabuf *advproto, *query = NULL;
5177 int used, ret = -1;
5178 char *pos, *end;
5179 size_t len;
5180
5181 used = hwaddr_aton2(cmd, dst_addr);
5182 if (used < 0)
5183 return -1;
5184
5185 pos = cmd + used;
5186 while (*pos == ' ')
5187 pos++;
5188
5189 /* Advertisement Protocol ID */
5190 end = os_strchr(pos, ' ');
5191 if (end)
5192 len = end - pos;
5193 else
5194 len = os_strlen(pos);
5195 if (len & 0x01)
5196 return -1;
5197 len /= 2;
5198 if (len == 0)
5199 return -1;
5200 advproto = wpabuf_alloc(len);
5201 if (advproto == NULL)
5202 return -1;
5203 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
5204 goto fail;
5205
5206 if (end) {
5207 /* Optional Query Request */
5208 pos = end + 1;
5209 while (*pos == ' ')
5210 pos++;
5211
5212 len = os_strlen(pos);
5213 if (len) {
5214 if (len & 0x01)
5215 goto fail;
5216 len /= 2;
5217 if (len == 0)
5218 goto fail;
5219 query = wpabuf_alloc(len);
5220 if (query == NULL)
5221 goto fail;
5222 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
5223 goto fail;
5224 }
5225 }
5226
5227 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
5228
5229fail:
5230 wpabuf_free(advproto);
5231 wpabuf_free(query);
5232
5233 return ret;
5234}
5235
5236
5237static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
5238 size_t buflen)
5239{
5240 u8 addr[ETH_ALEN];
5241 int dialog_token;
5242 int used;
5243 char *pos;
5244 size_t resp_len, start, requested_len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005245 struct wpabuf *resp;
5246 int ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005247
5248 used = hwaddr_aton2(cmd, addr);
5249 if (used < 0)
5250 return -1;
5251
5252 pos = cmd + used;
5253 while (*pos == ' ')
5254 pos++;
5255 dialog_token = atoi(pos);
5256
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005257 if (wpa_s->last_gas_resp &&
5258 os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
5259 dialog_token == wpa_s->last_gas_dialog_token)
5260 resp = wpa_s->last_gas_resp;
5261 else if (wpa_s->prev_gas_resp &&
5262 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
5263 dialog_token == wpa_s->prev_gas_dialog_token)
5264 resp = wpa_s->prev_gas_resp;
5265 else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005266 return -1;
5267
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005268 resp_len = wpabuf_len(resp);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005269 start = 0;
5270 requested_len = resp_len;
5271
5272 pos = os_strchr(pos, ' ');
5273 if (pos) {
5274 start = atoi(pos);
5275 if (start > resp_len)
5276 return os_snprintf(buf, buflen, "FAIL-Invalid range");
5277 pos = os_strchr(pos, ',');
5278 if (pos == NULL)
5279 return -1;
5280 pos++;
5281 requested_len = atoi(pos);
5282 if (start + requested_len > resp_len)
5283 return os_snprintf(buf, buflen, "FAIL-Invalid range");
5284 }
5285
5286 if (requested_len * 2 + 1 > buflen)
5287 return os_snprintf(buf, buflen, "FAIL-Too long response");
5288
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005289 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
5290 requested_len);
5291
5292 if (start + requested_len == resp_len) {
5293 /*
5294 * Free memory by dropping the response after it has been
5295 * fetched.
5296 */
5297 if (resp == wpa_s->prev_gas_resp) {
5298 wpabuf_free(wpa_s->prev_gas_resp);
5299 wpa_s->prev_gas_resp = NULL;
5300 } else {
5301 wpabuf_free(wpa_s->last_gas_resp);
5302 wpa_s->last_gas_resp = NULL;
5303 }
5304 }
5305
5306 return ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005307}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005308#endif /* CONFIG_INTERWORKING */
5309
5310
Dmitry Shmidt04949592012-07-19 12:16:46 -07005311#ifdef CONFIG_HS20
5312
5313static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
5314{
5315 u8 dst_addr[ETH_ALEN];
5316 int used;
5317 char *pos;
5318 u32 subtypes = 0;
5319
5320 used = hwaddr_aton2(dst, dst_addr);
5321 if (used < 0)
5322 return -1;
5323 pos = dst + used;
5324 for (;;) {
5325 int num = atoi(pos);
5326 if (num <= 0 || num > 31)
5327 return -1;
5328 subtypes |= BIT(num);
5329 pos = os_strchr(pos + 1, ',');
5330 if (pos == NULL)
5331 break;
5332 pos++;
5333 }
5334
5335 if (subtypes == 0)
5336 return -1;
5337
5338 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
5339}
5340
5341
5342static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5343 const u8 *addr, const char *realm)
5344{
5345 u8 *buf;
5346 size_t rlen, len;
5347 int ret;
5348
5349 rlen = os_strlen(realm);
5350 len = 3 + rlen;
5351 buf = os_malloc(len);
5352 if (buf == NULL)
5353 return -1;
5354 buf[0] = 1; /* NAI Home Realm Count */
5355 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
5356 buf[2] = rlen;
5357 os_memcpy(buf + 3, realm, rlen);
5358
5359 ret = hs20_anqp_send_req(wpa_s, addr,
5360 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5361 buf, len);
5362
5363 os_free(buf);
5364
5365 return ret;
5366}
5367
5368
5369static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5370 char *dst)
5371{
5372 struct wpa_cred *cred = wpa_s->conf->cred;
5373 u8 dst_addr[ETH_ALEN];
5374 int used;
5375 u8 *buf;
5376 size_t len;
5377 int ret;
5378
5379 used = hwaddr_aton2(dst, dst_addr);
5380 if (used < 0)
5381 return -1;
5382
5383 while (dst[used] == ' ')
5384 used++;
5385 if (os_strncmp(dst + used, "realm=", 6) == 0)
5386 return hs20_nai_home_realm_list(wpa_s, dst_addr,
5387 dst + used + 6);
5388
5389 len = os_strlen(dst + used);
5390
5391 if (len == 0 && cred && cred->realm)
5392 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
5393
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07005394 if (len & 1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07005395 return -1;
5396 len /= 2;
5397 buf = os_malloc(len);
5398 if (buf == NULL)
5399 return -1;
5400 if (hexstr2bin(dst + used, buf, len) < 0) {
5401 os_free(buf);
5402 return -1;
5403 }
5404
5405 ret = hs20_anqp_send_req(wpa_s, dst_addr,
5406 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5407 buf, len);
5408 os_free(buf);
5409
5410 return ret;
5411}
5412
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005413
5414static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
5415{
5416 u8 dst_addr[ETH_ALEN];
5417 int used;
5418 char *icon;
5419
5420 used = hwaddr_aton2(cmd, dst_addr);
5421 if (used < 0)
5422 return -1;
5423
5424 while (cmd[used] == ' ')
5425 used++;
5426 icon = &cmd[used];
5427
5428 wpa_s->fetch_osu_icon_in_progress = 0;
5429 return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
5430 (u8 *) icon, os_strlen(icon));
5431}
5432
Dmitry Shmidt04949592012-07-19 12:16:46 -07005433#endif /* CONFIG_HS20 */
5434
5435
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005436static int wpa_supplicant_ctrl_iface_sta_autoconnect(
5437 struct wpa_supplicant *wpa_s, char *cmd)
5438{
5439 wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
5440 return 0;
5441}
5442
5443
Dmitry Shmidt04949592012-07-19 12:16:46 -07005444#ifdef CONFIG_AUTOSCAN
5445
5446static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
5447 char *cmd)
5448{
5449 enum wpa_states state = wpa_s->wpa_state;
5450 char *new_params = NULL;
5451
5452 if (os_strlen(cmd) > 0) {
5453 new_params = os_strdup(cmd);
5454 if (new_params == NULL)
5455 return -1;
5456 }
5457
5458 os_free(wpa_s->conf->autoscan);
5459 wpa_s->conf->autoscan = new_params;
5460
5461 if (wpa_s->conf->autoscan == NULL)
5462 autoscan_deinit(wpa_s);
5463 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
5464 autoscan_init(wpa_s, 1);
5465 else if (state == WPA_SCANNING)
5466 wpa_supplicant_reinit_autoscan(wpa_s);
5467
5468 return 0;
5469}
5470
5471#endif /* CONFIG_AUTOSCAN */
5472
5473
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005474#ifdef CONFIG_WNM
5475
5476static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
5477{
5478 int enter;
5479 int intval = 0;
5480 char *pos;
5481 int ret;
5482 struct wpabuf *tfs_req = NULL;
5483
5484 if (os_strncmp(cmd, "enter", 5) == 0)
5485 enter = 1;
5486 else if (os_strncmp(cmd, "exit", 4) == 0)
5487 enter = 0;
5488 else
5489 return -1;
5490
5491 pos = os_strstr(cmd, " interval=");
5492 if (pos)
5493 intval = atoi(pos + 10);
5494
5495 pos = os_strstr(cmd, " tfs_req=");
5496 if (pos) {
5497 char *end;
5498 size_t len;
5499 pos += 9;
5500 end = os_strchr(pos, ' ');
5501 if (end)
5502 len = end - pos;
5503 else
5504 len = os_strlen(pos);
5505 if (len & 1)
5506 return -1;
5507 len /= 2;
5508 tfs_req = wpabuf_alloc(len);
5509 if (tfs_req == NULL)
5510 return -1;
5511 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
5512 wpabuf_free(tfs_req);
5513 return -1;
5514 }
5515 }
5516
5517 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
5518 WNM_SLEEP_MODE_EXIT, intval,
5519 tfs_req);
5520 wpabuf_free(tfs_req);
5521
5522 return ret;
5523}
5524
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005525
5526static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
5527{
5528 int query_reason;
5529
5530 query_reason = atoi(cmd);
5531
5532 wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
5533 query_reason);
5534
5535 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
5536}
5537
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005538#endif /* CONFIG_WNM */
5539
5540
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005541/* Get string representation of channel width */
5542static const char * channel_width_name(enum chan_width width)
5543{
5544 switch (width) {
5545 case CHAN_WIDTH_20_NOHT:
5546 return "20 MHz (no HT)";
5547 case CHAN_WIDTH_20:
5548 return "20 MHz";
5549 case CHAN_WIDTH_40:
5550 return "40 MHz";
5551 case CHAN_WIDTH_80:
5552 return "80 MHz";
5553 case CHAN_WIDTH_80P80:
5554 return "80+80 MHz";
5555 case CHAN_WIDTH_160:
5556 return "160 MHz";
5557 default:
5558 return "unknown";
5559 }
5560}
5561
5562
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005563static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
5564 size_t buflen)
5565{
5566 struct wpa_signal_info si;
5567 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005568 char *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005569
5570 ret = wpa_drv_signal_poll(wpa_s, &si);
5571 if (ret)
5572 return -1;
5573
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005574 pos = buf;
5575 end = buf + buflen;
5576
5577 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005578 "NOISE=%d\nFREQUENCY=%u\n",
5579 si.current_signal, si.current_txrate / 1000,
5580 si.current_noise, si.frequency);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005581 if (ret < 0 || ret > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005582 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005583 pos += ret;
5584
5585 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
5586 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
5587 channel_width_name(si.chanwidth));
5588 if (ret < 0 || ret > end - pos)
5589 return -1;
5590 pos += ret;
5591 }
5592
5593 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
5594 ret = os_snprintf(pos, end - pos,
5595 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
5596 si.center_frq1, si.center_frq2);
5597 if (ret < 0 || ret > end - pos)
5598 return -1;
5599 pos += ret;
5600 }
5601
5602 if (si.avg_signal) {
5603 ret = os_snprintf(pos, end - pos,
5604 "AVG_RSSI=%d\n", si.avg_signal);
5605 if (ret < 0 || ret >= end - pos)
5606 return -1;
5607 pos += ret;
5608 }
5609
5610 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611}
5612
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005613
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005614static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
5615 size_t buflen)
5616{
5617 struct hostap_sta_driver_data sta;
5618 int ret;
5619
5620 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
5621 if (ret)
5622 return -1;
5623
5624 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005625 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
5626 if (ret < 0 || (size_t) ret > buflen)
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005627 return -1;
5628 return ret;
5629}
5630
5631
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005632#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005633static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5634 char *buf, size_t buflen)
5635{
5636 int ret;
5637
5638 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005639 if (ret == 0) {
5640 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
5641 struct p2p_data *p2p = wpa_s->global->p2p;
5642 if (p2p) {
5643 char country[3];
5644 country[0] = cmd[8];
5645 country[1] = cmd[9];
5646 country[2] = 0x04;
5647 p2p_set_country(p2p, country);
5648 }
5649 }
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08005650 ret = os_snprintf(buf, buflen, "%s\n", "OK");
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005651 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005652 return ret;
5653}
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08005654#endif /* ANDROID */
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005655
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07005656
Dmitry Shmidta38abf92014-03-06 13:38:44 -08005657static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5658 char *buf, size_t buflen)
5659{
5660 int ret;
5661 char *pos;
5662 u8 *data = NULL;
5663 unsigned int vendor_id, subcmd;
5664 struct wpabuf *reply;
5665 size_t data_len = 0;
5666
5667 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
5668 vendor_id = strtoul(cmd, &pos, 16);
5669 if (!isblank(*pos))
5670 return -EINVAL;
5671
5672 subcmd = strtoul(pos, &pos, 10);
5673
5674 if (*pos != '\0') {
5675 if (!isblank(*pos++))
5676 return -EINVAL;
5677 data_len = os_strlen(pos);
5678 }
5679
5680 if (data_len) {
5681 data_len /= 2;
5682 data = os_malloc(data_len);
5683 if (!data)
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07005684 return -1;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08005685
5686 if (hexstr2bin(pos, data, data_len)) {
5687 wpa_printf(MSG_DEBUG,
5688 "Vendor command: wrong parameter format");
5689 os_free(data);
5690 return -EINVAL;
5691 }
5692 }
5693
5694 reply = wpabuf_alloc((buflen - 1) / 2);
5695 if (!reply) {
5696 os_free(data);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07005697 return -1;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08005698 }
5699
5700 ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
5701 reply);
5702
5703 if (ret == 0)
5704 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
5705 wpabuf_len(reply));
5706
5707 wpabuf_free(reply);
5708 os_free(data);
5709
5710 return ret;
5711}
5712
5713
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005714static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
5715{
5716 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
5717
5718#ifdef CONFIG_P2P
Dmitry Shmidt21de2142014-04-08 10:50:52 -07005719 wpas_p2p_cancel(wpa_s);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005720 wpas_p2p_stop_find(wpa_s);
5721 p2p_ctrl_flush(wpa_s);
5722 wpas_p2p_group_remove(wpa_s, "*");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005723 wpas_p2p_service_flush(wpa_s);
5724 wpa_s->global->p2p_disabled = 0;
5725 wpa_s->global->p2p_per_sta_psk = 0;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005726 wpa_s->conf->num_sec_device_types = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005727 wpa_s->p2p_disable_ip_addr_req = 0;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07005728 os_free(wpa_s->global->p2p_go_avoid_freq.range);
5729 wpa_s->global->p2p_go_avoid_freq.range = NULL;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005730#endif /* CONFIG_P2P */
5731
5732#ifdef CONFIG_WPS_TESTING
5733 wps_version_number = 0x20;
5734 wps_testing_dummy_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005735 wps_corrupt_pkhash = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005736#endif /* CONFIG_WPS_TESTING */
5737#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005738 wpa_s->wps_fragment_size = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005739 wpas_wps_cancel(wpa_s);
5740#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07005741 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005742 wpa_s->known_wps_freq = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005743
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005744#ifdef CONFIG_TDLS
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005745#ifdef CONFIG_TDLS_TESTING
5746 extern unsigned int tdls_testing;
5747 tdls_testing = 0;
5748#endif /* CONFIG_TDLS_TESTING */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005749 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
5750 wpa_tdls_enable(wpa_s->wpa, 1);
5751#endif /* CONFIG_TDLS */
5752
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005753 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
5754 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
5755
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005756 wpa_s->no_keep_alive = 0;
5757
5758 os_free(wpa_s->disallow_aps_bssid);
5759 wpa_s->disallow_aps_bssid = NULL;
5760 wpa_s->disallow_aps_bssid_count = 0;
5761 os_free(wpa_s->disallow_aps_ssid);
5762 wpa_s->disallow_aps_ssid = NULL;
5763 wpa_s->disallow_aps_ssid_count = 0;
5764
5765 wpa_s->set_sta_uapsd = 0;
5766 wpa_s->sta_uapsd = 0;
5767
5768 wpa_drv_radio_disable(wpa_s, 0);
5769
5770 wpa_bss_flush(wpa_s);
5771 wpa_blacklist_clear(wpa_s);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07005772 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005773 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
5774 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005775 wpa_config_flush_blobs(wpa_s->conf);
Dmitry Shmidt18463232014-01-24 12:29:41 -08005776 wpa_s->conf->auto_interworking = 0;
5777 wpa_s->conf->okc = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005778
5779 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
5780 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
5781 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
5782 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5783
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08005784 radio_remove_works(wpa_s, NULL, 1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005785
5786 wpa_s->next_ssid = NULL;
5787
5788#ifdef CONFIG_INTERWORKING
5789 hs20_cancel_fetch_osu(wpa_s);
5790#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt818ea482014-03-10 13:15:21 -07005791
5792 wpa_s->ext_mgmt_frame_handling = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005793}
5794
5795
5796static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
5797 char *buf, size_t buflen)
5798{
5799 struct wpa_radio_work *work;
5800 char *pos, *end;
5801 struct os_reltime now, diff;
5802
5803 pos = buf;
5804 end = buf + buflen;
5805
5806 os_get_reltime(&now);
5807
5808 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
5809 {
5810 int ret;
5811
5812 os_reltime_sub(&now, &work->time, &diff);
5813 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
5814 work->type, work->wpa_s->ifname, work->freq,
5815 work->started, diff.sec, diff.usec);
5816 if (ret < 0 || ret >= end - pos)
5817 break;
5818 pos += ret;
5819 }
5820
5821 return pos - buf;
5822}
5823
5824
5825static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
5826{
5827 struct wpa_radio_work *work = eloop_ctx;
5828 struct wpa_external_work *ework = work->ctx;
5829
5830 wpa_dbg(work->wpa_s, MSG_DEBUG,
5831 "Timing out external radio work %u (%s)",
5832 ework->id, work->type);
5833 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005834 radio_work_done(work);
Dmitry Shmidt71757432014-06-02 13:50:35 -07005835 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005836}
5837
5838
5839static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
5840{
5841 struct wpa_external_work *ework = work->ctx;
5842
5843 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08005844 if (work->started)
5845 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
5846 work, NULL);
5847
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005848 os_free(ework);
5849 return;
5850 }
5851
5852 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
5853 ework->id, ework->type);
5854 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
5855 if (!ework->timeout)
5856 ework->timeout = 10;
5857 eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
5858 work, NULL);
5859}
5860
5861
5862static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
5863 char *buf, size_t buflen)
5864{
5865 struct wpa_external_work *ework;
5866 char *pos, *pos2;
5867 size_t type_len;
5868 int ret;
5869 unsigned int freq = 0;
5870
5871 /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
5872
5873 ework = os_zalloc(sizeof(*ework));
5874 if (ework == NULL)
5875 return -1;
5876
5877 pos = os_strchr(cmd, ' ');
5878 if (pos) {
5879 type_len = pos - cmd;
5880 pos++;
5881
5882 pos2 = os_strstr(pos, "freq=");
5883 if (pos2)
5884 freq = atoi(pos2 + 5);
5885
5886 pos2 = os_strstr(pos, "timeout=");
5887 if (pos2)
5888 ework->timeout = atoi(pos2 + 8);
5889 } else {
5890 type_len = os_strlen(cmd);
5891 }
5892 if (4 + type_len >= sizeof(ework->type))
5893 type_len = sizeof(ework->type) - 4 - 1;
5894 os_strlcpy(ework->type, "ext:", sizeof(ework->type));
5895 os_memcpy(ework->type + 4, cmd, type_len);
5896 ework->type[4 + type_len] = '\0';
5897
5898 wpa_s->ext_work_id++;
5899 if (wpa_s->ext_work_id == 0)
5900 wpa_s->ext_work_id++;
5901 ework->id = wpa_s->ext_work_id;
5902
5903 if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
5904 ework) < 0) {
5905 os_free(ework);
5906 return -1;
5907 }
5908
5909 ret = os_snprintf(buf, buflen, "%u", ework->id);
5910 if (ret < 0 || (size_t) ret >= buflen)
5911 return -1;
5912 return ret;
5913}
5914
5915
5916static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
5917{
5918 struct wpa_radio_work *work;
5919 unsigned int id = atoi(cmd);
5920
5921 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
5922 {
5923 struct wpa_external_work *ework;
5924
5925 if (os_strncmp(work->type, "ext:", 4) != 0)
5926 continue;
5927 ework = work->ctx;
5928 if (id && ework->id != id)
5929 continue;
5930 wpa_dbg(wpa_s, MSG_DEBUG,
5931 "Completed external radio work %u (%s)",
5932 ework->id, ework->type);
5933 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005934 radio_work_done(work);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07005935 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005936 return 3; /* "OK\n" */
5937 }
5938
5939 return -1;
5940}
5941
5942
5943static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
5944 char *buf, size_t buflen)
5945{
5946 if (os_strcmp(cmd, "show") == 0)
5947 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
5948 if (os_strncmp(cmd, "add ", 4) == 0)
5949 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
5950 if (os_strncmp(cmd, "done ", 5) == 0)
5951 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
5952 return -1;
5953}
5954
5955
5956void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
5957{
5958 struct wpa_radio_work *work, *tmp;
5959
Dmitry Shmidt18463232014-01-24 12:29:41 -08005960 if (!wpa_s || !wpa_s->radio)
5961 return;
5962
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005963 dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
5964 struct wpa_radio_work, list) {
5965 struct wpa_external_work *ework;
5966
5967 if (os_strncmp(work->type, "ext:", 4) != 0)
5968 continue;
5969 ework = work->ctx;
5970 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07005971 "Flushing%s external radio work %u (%s)",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005972 work->started ? " started" : "", ework->id,
5973 ework->type);
5974 if (work->started)
5975 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
5976 work, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005977 radio_work_done(work);
Dmitry Shmidt71757432014-06-02 13:50:35 -07005978 os_free(ework);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005979 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005980}
5981
5982
Dmitry Shmidt051af732013-10-22 13:52:46 -07005983static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
5984{
5985 struct wpa_supplicant *wpa_s = eloop_ctx;
5986 eapol_sm_notify_ctrl_response(wpa_s->eapol);
5987}
5988
5989
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005990static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
5991{
5992 int *freqs = NULL;
5993
5994 freqs = freq_range_to_channel_list(wpa_s, val);
5995 if (freqs == NULL)
5996 return -1;
5997
5998 os_free(wpa_s->manual_scan_freqs);
5999 wpa_s->manual_scan_freqs = freqs;
6000
6001 return 0;
6002}
6003
6004
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006005static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value)
6006{
6007 const char *pos = value;
6008
6009 while (pos) {
6010 if (*pos == ' ' || *pos == '\0')
6011 break;
6012 if (wpa_s->scan_id_count == MAX_SCAN_ID)
6013 return -1;
6014 wpa_s->scan_id[wpa_s->scan_id_count++] = atoi(pos);
6015 pos = os_strchr(pos, ',');
6016 if (pos)
6017 pos++;
6018 }
6019
6020 return 0;
6021}
6022
6023
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006024static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
6025 char *reply, int reply_size, int *reply_len)
6026{
6027 char *pos;
6028
6029 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6030 *reply_len = -1;
6031 return;
6032 }
6033
6034 wpa_s->manual_scan_passive = 0;
6035 wpa_s->manual_scan_use_id = 0;
6036 wpa_s->manual_scan_only_new = 0;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006037 wpa_s->scan_id_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006038
6039 if (params) {
6040 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
6041 wpa_s->scan_res_handler = scan_only_handler;
6042
6043 pos = os_strstr(params, "freq=");
6044 if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
6045 *reply_len = -1;
6046 return;
6047 }
6048
6049 pos = os_strstr(params, "passive=");
6050 if (pos)
6051 wpa_s->manual_scan_passive = !!atoi(pos + 8);
6052
6053 pos = os_strstr(params, "use_id=");
6054 if (pos)
6055 wpa_s->manual_scan_use_id = atoi(pos + 7);
6056
6057 pos = os_strstr(params, "only_new=1");
6058 if (pos)
6059 wpa_s->manual_scan_only_new = 1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006060
6061 pos = os_strstr(params, "scan_id=");
6062 if (pos && scan_id_list_parse(wpa_s, pos + 8) < 0) {
6063 *reply_len = -1;
6064 return;
6065 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006066 } else {
6067 os_free(wpa_s->manual_scan_freqs);
6068 wpa_s->manual_scan_freqs = NULL;
6069 if (wpa_s->scan_res_handler == scan_only_handler)
6070 wpa_s->scan_res_handler = NULL;
6071 }
6072
6073 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
6074 ((wpa_s->wpa_state <= WPA_SCANNING) ||
6075 (wpa_s->wpa_state == WPA_COMPLETED))) {
6076 wpa_s->normal_scans = 0;
6077 wpa_s->scan_req = MANUAL_SCAN_REQ;
6078 wpa_s->after_wps = 0;
6079 wpa_s->known_wps_freq = 0;
6080 wpa_supplicant_req_scan(wpa_s, 0, 0);
6081 if (wpa_s->manual_scan_use_id) {
6082 wpa_s->manual_scan_id++;
6083 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
6084 wpa_s->manual_scan_id);
6085 *reply_len = os_snprintf(reply, reply_size, "%u\n",
6086 wpa_s->manual_scan_id);
6087 }
6088 } else if (wpa_s->sched_scanning) {
6089 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
6090 wpa_supplicant_cancel_sched_scan(wpa_s);
6091 wpa_s->scan_req = MANUAL_SCAN_REQ;
6092 wpa_supplicant_req_scan(wpa_s, 0, 0);
6093 if (wpa_s->manual_scan_use_id) {
6094 wpa_s->manual_scan_id++;
6095 *reply_len = os_snprintf(reply, reply_size, "%u\n",
6096 wpa_s->manual_scan_id);
6097 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
6098 wpa_s->manual_scan_id);
6099 }
6100 } else {
6101 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
6102 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
6103 }
6104}
6105
6106
Dmitry Shmidt818ea482014-03-10 13:15:21 -07006107#ifdef CONFIG_TESTING_OPTIONS
6108
6109static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
6110 unsigned int freq, const u8 *dst,
6111 const u8 *src, const u8 *bssid,
6112 const u8 *data, size_t data_len,
6113 enum offchannel_send_action_result
6114 result)
6115{
6116 wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
6117 " src=" MACSTR " bssid=" MACSTR " result=%s",
6118 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
6119 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
6120 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
6121 "NO_ACK" : "FAILED"));
6122}
6123
6124
6125static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
6126{
6127 char *pos, *param;
6128 size_t len;
6129 u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
6130 int res, used;
6131 int freq = 0, no_cck = 0, wait_time = 0;
6132
6133 /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
6134 * <action=Action frame payload> */
6135
6136 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
6137
6138 pos = cmd;
6139 used = hwaddr_aton2(pos, da);
6140 if (used < 0)
6141 return -1;
6142 pos += used;
6143 while (*pos == ' ')
6144 pos++;
6145 used = hwaddr_aton2(pos, bssid);
6146 if (used < 0)
6147 return -1;
6148 pos += used;
6149
6150 param = os_strstr(pos, " freq=");
6151 if (param) {
6152 param += 6;
6153 freq = atoi(param);
6154 }
6155
6156 param = os_strstr(pos, " no_cck=");
6157 if (param) {
6158 param += 8;
6159 no_cck = atoi(param);
6160 }
6161
6162 param = os_strstr(pos, " wait_time=");
6163 if (param) {
6164 param += 11;
6165 wait_time = atoi(param);
6166 }
6167
6168 param = os_strstr(pos, " action=");
6169 if (param == NULL)
6170 return -1;
6171 param += 8;
6172
6173 len = os_strlen(param);
6174 if (len & 1)
6175 return -1;
6176 len /= 2;
6177
6178 buf = os_malloc(len);
6179 if (buf == NULL)
6180 return -1;
6181
6182 if (hexstr2bin(param, buf, len) < 0) {
6183 os_free(buf);
6184 return -1;
6185 }
6186
6187 res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
6188 buf, len, wait_time,
6189 wpas_ctrl_iface_mgmt_tx_cb, no_cck);
6190 os_free(buf);
6191 return res;
6192}
6193
6194
6195static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
6196{
6197 wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
6198 offchannel_send_action_done(wpa_s);
6199}
6200
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07006201
6202static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
6203{
6204 char *pos, *param;
6205 union wpa_event_data event;
6206 enum wpa_event_type ev;
6207
6208 /* <event name> [parameters..] */
6209
6210 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
6211
6212 pos = cmd;
6213 param = os_strchr(pos, ' ');
6214 if (param)
6215 *param++ = '\0';
6216
6217 os_memset(&event, 0, sizeof(event));
6218
6219 if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
6220 ev = EVENT_INTERFACE_ENABLED;
6221 } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
6222 ev = EVENT_INTERFACE_DISABLED;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006223 } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
6224 ev = EVENT_AVOID_FREQUENCIES;
6225 if (param == NULL)
6226 param = "";
6227 if (freq_range_list_parse(&event.freq_range, param) < 0)
6228 return -1;
6229 wpa_supplicant_event(wpa_s, ev, &event);
6230 os_free(event.freq_range.range);
6231 return 0;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07006232 } else {
6233 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
6234 cmd);
6235 return -1;
6236 }
6237
6238 wpa_supplicant_event(wpa_s, ev, &event);
6239
6240 return 0;
6241}
6242
Dmitry Shmidt818ea482014-03-10 13:15:21 -07006243#endif /* CONFIG_TESTING_OPTIONS */
6244
6245
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07006246static void wpas_ctrl_vendor_elem_update(struct wpa_supplicant *wpa_s)
6247{
6248 unsigned int i;
6249 char buf[30];
6250
6251 wpa_printf(MSG_DEBUG, "Update vendor elements");
6252
6253 for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
6254 if (wpa_s->vendor_elem[i]) {
6255 os_snprintf(buf, sizeof(buf), "frame[%u]", i);
6256 wpa_hexdump_buf(MSG_DEBUG, buf, wpa_s->vendor_elem[i]);
6257 }
6258 }
6259
6260#ifdef CONFIG_P2P
6261 if (wpa_s->parent == wpa_s &&
6262 wpa_s->global->p2p &&
6263 !wpa_s->global->p2p_disabled)
6264 p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
6265#endif /* CONFIG_P2P */
6266}
6267
6268
6269static struct wpa_supplicant *
6270wpas_ctrl_vendor_elem_iface(struct wpa_supplicant *wpa_s,
6271 enum wpa_vendor_elem_frame frame)
6272{
6273 switch (frame) {
6274#ifdef CONFIG_P2P
6275 case VENDOR_ELEM_PROBE_REQ_P2P:
6276 case VENDOR_ELEM_PROBE_RESP_P2P:
6277 case VENDOR_ELEM_PROBE_RESP_P2P_GO:
6278 case VENDOR_ELEM_BEACON_P2P_GO:
6279 case VENDOR_ELEM_P2P_PD_REQ:
6280 case VENDOR_ELEM_P2P_PD_RESP:
6281 case VENDOR_ELEM_P2P_GO_NEG_REQ:
6282 case VENDOR_ELEM_P2P_GO_NEG_RESP:
6283 case VENDOR_ELEM_P2P_GO_NEG_CONF:
6284 case VENDOR_ELEM_P2P_INV_REQ:
6285 case VENDOR_ELEM_P2P_INV_RESP:
6286 case VENDOR_ELEM_P2P_ASSOC_REQ:
6287 return wpa_s->parent;
6288#endif /* CONFIG_P2P */
6289 default:
6290 return wpa_s;
6291 }
6292}
6293
6294
6295static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
6296{
6297 char *pos = cmd;
6298 int frame;
6299 size_t len;
6300 struct wpabuf *buf;
6301 struct ieee802_11_elems elems;
6302
6303 frame = atoi(pos);
6304 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
6305 return -1;
6306 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
6307
6308 pos = os_strchr(pos, ' ');
6309 if (pos == NULL)
6310 return -1;
6311 pos++;
6312
6313 len = os_strlen(pos);
6314 if (len == 0)
6315 return 0;
6316 if (len & 1)
6317 return -1;
6318 len /= 2;
6319
6320 buf = wpabuf_alloc(len);
6321 if (buf == NULL)
6322 return -1;
6323
6324 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
6325 wpabuf_free(buf);
6326 return -1;
6327 }
6328
6329 if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
6330 ParseFailed) {
6331 wpabuf_free(buf);
6332 return -1;
6333 }
6334
6335 if (wpa_s->vendor_elem[frame] == NULL) {
6336 wpa_s->vendor_elem[frame] = buf;
6337 wpas_ctrl_vendor_elem_update(wpa_s);
6338 return 0;
6339 }
6340
6341 if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
6342 wpabuf_free(buf);
6343 return -1;
6344 }
6345
6346 wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
6347 wpabuf_free(buf);
6348 wpas_ctrl_vendor_elem_update(wpa_s);
6349
6350 return 0;
6351}
6352
6353
6354static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
6355 char *buf, size_t buflen)
6356{
6357 int frame = atoi(cmd);
6358
6359 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
6360 return -1;
6361 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
6362
6363 if (wpa_s->vendor_elem[frame] == NULL)
6364 return 0;
6365
6366 return wpa_snprintf_hex(buf, buflen,
6367 wpabuf_head_u8(wpa_s->vendor_elem[frame]),
6368 wpabuf_len(wpa_s->vendor_elem[frame]));
6369}
6370
6371
6372static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
6373{
6374 char *pos = cmd;
6375 int frame;
6376 size_t len;
6377 u8 *buf;
6378 struct ieee802_11_elems elems;
6379 u8 *ie, *end;
6380
6381 frame = atoi(pos);
6382 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
6383 return -1;
6384 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
6385
6386 pos = os_strchr(pos, ' ');
6387 if (pos == NULL)
6388 return -1;
6389 pos++;
6390
6391 if (*pos == '*') {
6392 wpabuf_free(wpa_s->vendor_elem[frame]);
6393 wpa_s->vendor_elem[frame] = NULL;
6394 wpas_ctrl_vendor_elem_update(wpa_s);
6395 return 0;
6396 }
6397
6398 if (wpa_s->vendor_elem[frame] == NULL)
6399 return -1;
6400
6401 len = os_strlen(pos);
6402 if (len == 0)
6403 return 0;
6404 if (len & 1)
6405 return -1;
6406 len /= 2;
6407
6408 buf = os_malloc(len);
6409 if (buf == NULL)
6410 return -1;
6411
6412 if (hexstr2bin(pos, buf, len) < 0) {
6413 os_free(buf);
6414 return -1;
6415 }
6416
6417 if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
6418 os_free(buf);
6419 return -1;
6420 }
6421
6422 ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
6423 end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
6424
6425 for (; ie + 1 < end; ie += 2 + ie[1]) {
6426 if (ie + len > end)
6427 break;
6428 if (os_memcmp(ie, buf, len) != 0)
6429 continue;
6430
6431 if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
6432 wpabuf_free(wpa_s->vendor_elem[frame]);
6433 wpa_s->vendor_elem[frame] = NULL;
6434 } else {
6435 os_memmove(ie, ie + len,
6436 wpabuf_len(wpa_s->vendor_elem[frame]) - len);
6437 wpa_s->vendor_elem[frame]->used -= len;
6438 }
6439 os_free(buf);
6440 wpas_ctrl_vendor_elem_update(wpa_s);
6441 return 0;
6442 }
6443
6444 os_free(buf);
6445
6446 return -1;
6447}
6448
6449
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006450char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
6451 char *buf, size_t *resp_len)
6452{
6453 char *reply;
6454 const int reply_size = 4096;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006455 int reply_len;
6456
6457 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006458 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
6459 if (wpa_debug_show_keys)
6460 wpa_dbg(wpa_s, MSG_DEBUG,
6461 "Control interface command '%s'", buf);
6462 else
6463 wpa_dbg(wpa_s, MSG_DEBUG,
6464 "Control interface command '%s [REMOVED]'",
6465 os_strncmp(buf, WPA_CTRL_RSP,
6466 os_strlen(WPA_CTRL_RSP)) == 0 ?
6467 WPA_CTRL_RSP : "SET_NETWORK");
6468 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006469 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006470 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
6471 (const u8 *) buf, os_strlen(buf));
6472 } else {
6473 int level = MSG_DEBUG;
6474 if (os_strcmp(buf, "PING") == 0)
6475 level = MSG_EXCESSIVE;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006476 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006477 }
6478
6479 reply = os_malloc(reply_size);
6480 if (reply == NULL) {
6481 *resp_len = 1;
6482 return NULL;
6483 }
6484
6485 os_memcpy(reply, "OK\n", 3);
6486 reply_len = 3;
6487
6488 if (os_strcmp(buf, "PING") == 0) {
6489 os_memcpy(reply, "PONG\n", 5);
6490 reply_len = 5;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006491 } else if (os_strcmp(buf, "IFNAME") == 0) {
6492 reply_len = os_strlen(wpa_s->ifname);
6493 os_memcpy(reply, wpa_s->ifname, reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006494 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
6495 if (wpa_debug_reopen_file() < 0)
6496 reply_len = -1;
6497 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
6498 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
6499 } else if (os_strcmp(buf, "MIB") == 0) {
6500 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
6501 if (reply_len >= 0) {
6502 int res;
6503 res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
6504 reply_size - reply_len);
6505 if (res < 0)
6506 reply_len = -1;
6507 else
6508 reply_len += res;
6509 }
6510 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
6511 reply_len = wpa_supplicant_ctrl_iface_status(
6512 wpa_s, buf + 6, reply, reply_size);
6513 } else if (os_strcmp(buf, "PMKSA") == 0) {
6514 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
6515 reply_size);
6516 } else if (os_strncmp(buf, "SET ", 4) == 0) {
6517 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
6518 reply_len = -1;
6519 } else if (os_strncmp(buf, "GET ", 4) == 0) {
6520 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
6521 reply, reply_size);
6522 } else if (os_strcmp(buf, "LOGON") == 0) {
6523 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
6524 } else if (os_strcmp(buf, "LOGOFF") == 0) {
6525 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
6526 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
6527 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
6528 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006529 else
6530 wpas_request_connection(wpa_s);
Dmitry Shmidt98660862014-03-11 17:26:21 -07006531 } else if (os_strcmp(buf, "REATTACH") == 0) {
6532 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
6533 !wpa_s->current_ssid)
6534 reply_len = -1;
6535 else {
6536 wpa_s->reattach = 1;
6537 wpas_request_connection(wpa_s);
6538 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006539 } else if (os_strcmp(buf, "RECONNECT") == 0) {
6540 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
6541 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006542 else if (wpa_s->disconnected)
6543 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006544#ifdef IEEE8021X_EAPOL
6545 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
6546 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
6547 reply_len = -1;
6548#endif /* IEEE8021X_EAPOL */
6549#ifdef CONFIG_PEERKEY
6550 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
6551 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
6552 reply_len = -1;
6553#endif /* CONFIG_PEERKEY */
6554#ifdef CONFIG_IEEE80211R
6555 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
6556 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
6557 reply_len = -1;
6558#endif /* CONFIG_IEEE80211R */
6559#ifdef CONFIG_WPS
6560 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
6561 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
6562 if (res == -2) {
6563 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
6564 reply_len = 17;
6565 } else if (res)
6566 reply_len = -1;
6567 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
6568 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
6569 if (res == -2) {
6570 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
6571 reply_len = 17;
6572 } else if (res)
6573 reply_len = -1;
6574 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
6575 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
6576 reply,
6577 reply_size);
6578 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
6579 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
6580 wpa_s, buf + 14, reply, reply_size);
6581 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
6582 if (wpas_wps_cancel(wpa_s))
6583 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006584#ifdef CONFIG_WPS_NFC
6585 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
6586 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
6587 reply_len = -1;
6588 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
6589 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
6590 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006591 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
6592 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
6593 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006594 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
6595 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
6596 wpa_s, buf + 14, reply, reply_size);
6597 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
6598 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
6599 buf + 17))
6600 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006601 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
6602 reply_len = wpas_ctrl_nfc_get_handover_req(
6603 wpa_s, buf + 21, reply, reply_size);
6604 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
6605 reply_len = wpas_ctrl_nfc_get_handover_sel(
6606 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006607 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
6608 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
6609 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006610#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006611 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
6612 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
6613 reply_len = -1;
6614#ifdef CONFIG_AP
6615 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
6616 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
6617 wpa_s, buf + 11, reply, reply_size);
6618#endif /* CONFIG_AP */
6619#ifdef CONFIG_WPS_ER
6620 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
6621 if (wpas_wps_er_start(wpa_s, NULL))
6622 reply_len = -1;
6623 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
6624 if (wpas_wps_er_start(wpa_s, buf + 13))
6625 reply_len = -1;
6626 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
6627 if (wpas_wps_er_stop(wpa_s))
6628 reply_len = -1;
6629 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
6630 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
6631 reply_len = -1;
6632 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
6633 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
6634 if (ret == -2) {
6635 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
6636 reply_len = 17;
6637 } else if (ret == -3) {
6638 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
6639 reply_len = 18;
6640 } else if (ret == -4) {
6641 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
6642 reply_len = 20;
6643 } else if (ret)
6644 reply_len = -1;
6645 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
6646 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
6647 reply_len = -1;
6648 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
6649 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
6650 buf + 18))
6651 reply_len = -1;
6652 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
6653 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
6654 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006655#ifdef CONFIG_WPS_NFC
6656 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
6657 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
6658 wpa_s, buf + 24, reply, reply_size);
6659#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006660#endif /* CONFIG_WPS_ER */
6661#endif /* CONFIG_WPS */
6662#ifdef CONFIG_IBSS_RSN
6663 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
6664 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
6665 reply_len = -1;
6666#endif /* CONFIG_IBSS_RSN */
6667#ifdef CONFIG_P2P
6668 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
6669 if (p2p_ctrl_find(wpa_s, buf + 9))
6670 reply_len = -1;
6671 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
6672 if (p2p_ctrl_find(wpa_s, ""))
6673 reply_len = -1;
6674 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
6675 wpas_p2p_stop_find(wpa_s);
6676 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
6677 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
6678 reply_size);
6679 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
6680 if (p2p_ctrl_listen(wpa_s, buf + 11))
6681 reply_len = -1;
6682 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
6683 if (p2p_ctrl_listen(wpa_s, ""))
6684 reply_len = -1;
6685 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
6686 if (wpas_p2p_group_remove(wpa_s, buf + 17))
6687 reply_len = -1;
6688 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006689 if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006690 reply_len = -1;
6691 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
6692 if (p2p_ctrl_group_add(wpa_s, buf + 14))
6693 reply_len = -1;
6694 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
6695 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
6696 reply_len = -1;
6697 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
6698 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
6699 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
6700 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
6701 reply_size);
6702 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
6703 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
6704 reply_len = -1;
6705 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
6706 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
6707 reply_len = -1;
6708 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
6709 wpas_p2p_sd_service_update(wpa_s);
6710 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
6711 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
6712 reply_len = -1;
6713 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
6714 wpas_p2p_service_flush(wpa_s);
6715 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
6716 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
6717 reply_len = -1;
6718 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
6719 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
6720 reply_len = -1;
6721 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
6722 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
6723 reply_len = -1;
6724 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
6725 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
6726 reply_len = -1;
6727 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
6728 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
6729 reply_size);
6730 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
6731 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
6732 reply_len = -1;
6733 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006734 p2p_ctrl_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006735 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
6736 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
6737 reply_len = -1;
6738 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
6739 if (wpas_p2p_cancel(wpa_s))
6740 reply_len = -1;
6741 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
6742 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
6743 reply_len = -1;
6744 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
6745 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
6746 reply_len = -1;
6747 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
6748 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
6749 reply_len = -1;
6750 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
6751 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
6752 reply_len = -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006753 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
6754 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
6755 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006756#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006757#ifdef CONFIG_WIFI_DISPLAY
6758 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
6759 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
6760 reply_len = -1;
6761 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
6762 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
6763 reply, reply_size);
6764#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006765#ifdef CONFIG_INTERWORKING
6766 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
6767 if (interworking_fetch_anqp(wpa_s) < 0)
6768 reply_len = -1;
6769 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
6770 interworking_stop_fetch_anqp(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006771 } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
6772 if (ctrl_interworking_select(wpa_s, NULL) < 0)
6773 reply_len = -1;
6774 } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
6775 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006776 reply_len = -1;
6777 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
6778 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
6779 reply_len = -1;
6780 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
6781 if (get_anqp(wpa_s, buf + 9) < 0)
6782 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006783 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
6784 if (gas_request(wpa_s, buf + 12) < 0)
6785 reply_len = -1;
6786 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
6787 reply_len = gas_response_get(wpa_s, buf + 17, reply,
6788 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006789#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07006790#ifdef CONFIG_HS20
6791 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
6792 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
6793 reply_len = -1;
6794 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
6795 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
6796 reply_len = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006797 } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
6798 if (hs20_icon_request(wpa_s, buf + 18) < 0)
6799 reply_len = -1;
6800 } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
6801 if (hs20_fetch_osu(wpa_s) < 0)
6802 reply_len = -1;
6803 } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
6804 hs20_cancel_fetch_osu(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006805#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006806 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
6807 {
6808 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
6809 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
6810 reply_len = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006811 else {
6812 /*
6813 * Notify response from timeout to allow the control
6814 * interface response to be sent first.
6815 */
6816 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
6817 wpa_s, NULL);
6818 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006819 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
6820 if (wpa_supplicant_reload_configuration(wpa_s))
6821 reply_len = -1;
6822 } else if (os_strcmp(buf, "TERMINATE") == 0) {
6823 wpa_supplicant_terminate_proc(wpa_s->global);
6824 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
6825 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
6826 reply_len = -1;
Dmitry Shmidte19501d2011-03-16 14:32:18 -07006827 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006828 reply_len = wpa_supplicant_ctrl_iface_blacklist(
6829 wpa_s, buf + 9, reply, reply_size);
6830 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
6831 reply_len = wpa_supplicant_ctrl_iface_log_level(
6832 wpa_s, buf + 9, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006833 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
6834 reply_len = wpa_supplicant_ctrl_iface_list_networks(
6835 wpa_s, reply, reply_size);
6836 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006837#ifdef CONFIG_SME
6838 wpa_s->sme.prev_bssid_set = 0;
6839#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006840 wpa_s->reassociate = 0;
6841 wpa_s->disconnected = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006842 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006843 wpa_supplicant_cancel_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006844 wpa_supplicant_deauthenticate(wpa_s,
6845 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006846 } else if (os_strcmp(buf, "SCAN") == 0) {
6847 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
6848 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
6849 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006850 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
6851 reply_len = wpa_supplicant_ctrl_iface_scan_results(
6852 wpa_s, reply, reply_size);
6853 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
6854 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
6855 reply_len = -1;
6856 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
6857 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
6858 reply_len = -1;
6859 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
6860 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
6861 reply_len = -1;
6862 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
6863 reply_len = wpa_supplicant_ctrl_iface_add_network(
6864 wpa_s, reply, reply_size);
6865 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
6866 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
6867 reply_len = -1;
6868 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
6869 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
6870 reply_len = -1;
6871 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
6872 reply_len = wpa_supplicant_ctrl_iface_get_network(
6873 wpa_s, buf + 12, reply, reply_size);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006874 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
6875 if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12))
6876 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006877 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
6878 reply_len = wpa_supplicant_ctrl_iface_list_creds(
6879 wpa_s, reply, reply_size);
6880 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
6881 reply_len = wpa_supplicant_ctrl_iface_add_cred(
6882 wpa_s, reply, reply_size);
6883 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
6884 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
6885 reply_len = -1;
6886 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
6887 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
6888 reply_len = -1;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07006889 } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
6890 reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
6891 reply,
6892 reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006893#ifndef CONFIG_NO_CONFIG_WRITE
6894 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
6895 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
6896 reply_len = -1;
6897#endif /* CONFIG_NO_CONFIG_WRITE */
6898 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
6899 reply_len = wpa_supplicant_ctrl_iface_get_capability(
6900 wpa_s, buf + 15, reply, reply_size);
6901 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
6902 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
6903 reply_len = -1;
6904 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
6905 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
6906 reply_len = -1;
6907 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
6908 reply_len = wpa_supplicant_global_iface_list(
6909 wpa_s->global, reply, reply_size);
6910 } else if (os_strcmp(buf, "INTERFACES") == 0) {
6911 reply_len = wpa_supplicant_global_iface_interfaces(
6912 wpa_s->global, reply, reply_size);
6913 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
6914 reply_len = wpa_supplicant_ctrl_iface_bss(
6915 wpa_s, buf + 4, reply, reply_size);
6916#ifdef CONFIG_AP
6917 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
6918 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
6919 } else if (os_strncmp(buf, "STA ", 4) == 0) {
6920 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
6921 reply_size);
6922 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
6923 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
6924 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006925 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
6926 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
6927 reply_len = -1;
6928 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
6929 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
6930 reply_len = -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006931 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
6932 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
6933 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006934#endif /* CONFIG_AP */
6935 } else if (os_strcmp(buf, "SUSPEND") == 0) {
6936 wpas_notify_suspend(wpa_s->global);
6937 } else if (os_strcmp(buf, "RESUME") == 0) {
6938 wpas_notify_resume(wpa_s->global);
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006939#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006940 } else if (os_strcmp(buf, "DROP_SA") == 0) {
6941 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006942#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006943 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
6944 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
6945 reply_len = -1;
6946 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
6947 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
6948 reply_len = -1;
6949 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
6950 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
6951 reply_len = -1;
6952 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
6953 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
6954 buf + 17))
6955 reply_len = -1;
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07006956 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
6957 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
6958 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006959#ifdef CONFIG_TDLS
6960 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
6961 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
6962 reply_len = -1;
6963 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
6964 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
6965 reply_len = -1;
6966 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
6967 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
6968 reply_len = -1;
6969#endif /* CONFIG_TDLS */
6970 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
6971 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
6972 reply_size);
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07006973 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
6974 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
6975 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006976#ifdef CONFIG_AUTOSCAN
6977 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
6978 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
6979 reply_len = -1;
6980#endif /* CONFIG_AUTOSCAN */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006981#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07006982 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
6983 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
6984 reply_size);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006985#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006986 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
6987 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
6988 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006989 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006990 pmksa_cache_clear_current(wpa_s->wpa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006991 eapol_sm_request_reauth(wpa_s->eapol);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006992#ifdef CONFIG_WNM
6993 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
6994 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
6995 reply_len = -1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07006996 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
6997 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
6998 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006999#endif /* CONFIG_WNM */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007000 } else if (os_strcmp(buf, "FLUSH") == 0) {
7001 wpa_supplicant_ctrl_iface_flush(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007002 } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
7003 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
7004 reply_size);
Dmitry Shmidt818ea482014-03-10 13:15:21 -07007005#ifdef CONFIG_TESTING_OPTIONS
7006 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
7007 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
7008 reply_len = -1;
7009 } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
7010 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007011 } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
7012 if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
7013 reply_len = -1;
Dmitry Shmidt818ea482014-03-10 13:15:21 -07007014#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07007015 } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
7016 if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
7017 reply_len = -1;
7018 } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
7019 reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
7020 reply_size);
7021 } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
7022 if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
7023 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007024 } else {
7025 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
7026 reply_len = 16;
7027 }
7028
7029 if (reply_len < 0) {
7030 os_memcpy(reply, "FAIL\n", 5);
7031 reply_len = 5;
7032 }
7033
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007034 *resp_len = reply_len;
7035 return reply;
7036}
7037
7038
7039static int wpa_supplicant_global_iface_add(struct wpa_global *global,
7040 char *cmd)
7041{
7042 struct wpa_interface iface;
7043 char *pos;
7044
7045 /*
7046 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
7047 * TAB<bridge_ifname>
7048 */
7049 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
7050
7051 os_memset(&iface, 0, sizeof(iface));
7052
7053 do {
7054 iface.ifname = pos = cmd;
7055 pos = os_strchr(pos, '\t');
7056 if (pos)
7057 *pos++ = '\0';
7058 if (iface.ifname[0] == '\0')
7059 return -1;
7060 if (pos == NULL)
7061 break;
7062
7063 iface.confname = pos;
7064 pos = os_strchr(pos, '\t');
7065 if (pos)
7066 *pos++ = '\0';
7067 if (iface.confname[0] == '\0')
7068 iface.confname = NULL;
7069 if (pos == NULL)
7070 break;
7071
7072 iface.driver = pos;
7073 pos = os_strchr(pos, '\t');
7074 if (pos)
7075 *pos++ = '\0';
7076 if (iface.driver[0] == '\0')
7077 iface.driver = NULL;
7078 if (pos == NULL)
7079 break;
7080
7081 iface.ctrl_interface = pos;
7082 pos = os_strchr(pos, '\t');
7083 if (pos)
7084 *pos++ = '\0';
7085 if (iface.ctrl_interface[0] == '\0')
7086 iface.ctrl_interface = NULL;
7087 if (pos == NULL)
7088 break;
7089
7090 iface.driver_param = pos;
7091 pos = os_strchr(pos, '\t');
7092 if (pos)
7093 *pos++ = '\0';
7094 if (iface.driver_param[0] == '\0')
7095 iface.driver_param = NULL;
7096 if (pos == NULL)
7097 break;
7098
7099 iface.bridge_ifname = pos;
7100 pos = os_strchr(pos, '\t');
7101 if (pos)
7102 *pos++ = '\0';
7103 if (iface.bridge_ifname[0] == '\0')
7104 iface.bridge_ifname = NULL;
7105 if (pos == NULL)
7106 break;
7107 } while (0);
7108
7109 if (wpa_supplicant_get_iface(global, iface.ifname))
7110 return -1;
7111
7112 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
7113}
7114
7115
7116static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
7117 char *cmd)
7118{
7119 struct wpa_supplicant *wpa_s;
7120
7121 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
7122
7123 wpa_s = wpa_supplicant_get_iface(global, cmd);
7124 if (wpa_s == NULL)
7125 return -1;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07007126 return wpa_supplicant_remove_iface(global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007127}
7128
7129
7130static void wpa_free_iface_info(struct wpa_interface_info *iface)
7131{
7132 struct wpa_interface_info *prev;
7133
7134 while (iface) {
7135 prev = iface;
7136 iface = iface->next;
7137
7138 os_free(prev->ifname);
7139 os_free(prev->desc);
7140 os_free(prev);
7141 }
7142}
7143
7144
7145static int wpa_supplicant_global_iface_list(struct wpa_global *global,
7146 char *buf, int len)
7147{
7148 int i, res;
7149 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
7150 char *pos, *end;
7151
7152 for (i = 0; wpa_drivers[i]; i++) {
7153 struct wpa_driver_ops *drv = wpa_drivers[i];
7154 if (drv->get_interfaces == NULL)
7155 continue;
7156 tmp = drv->get_interfaces(global->drv_priv[i]);
7157 if (tmp == NULL)
7158 continue;
7159
7160 if (last == NULL)
7161 iface = last = tmp;
7162 else
7163 last->next = tmp;
7164 while (last->next)
7165 last = last->next;
7166 }
7167
7168 pos = buf;
7169 end = buf + len;
7170 for (tmp = iface; tmp; tmp = tmp->next) {
7171 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
7172 tmp->drv_name, tmp->ifname,
7173 tmp->desc ? tmp->desc : "");
7174 if (res < 0 || res >= end - pos) {
7175 *pos = '\0';
7176 break;
7177 }
7178 pos += res;
7179 }
7180
7181 wpa_free_iface_info(iface);
7182
7183 return pos - buf;
7184}
7185
7186
7187static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
7188 char *buf, int len)
7189{
7190 int res;
7191 char *pos, *end;
7192 struct wpa_supplicant *wpa_s;
7193
7194 wpa_s = global->ifaces;
7195 pos = buf;
7196 end = buf + len;
7197
7198 while (wpa_s) {
7199 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
7200 if (res < 0 || res >= end - pos) {
7201 *pos = '\0';
7202 break;
7203 }
7204 pos += res;
7205 wpa_s = wpa_s->next;
7206 }
7207 return pos - buf;
7208}
7209
7210
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007211static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
7212 const char *ifname,
7213 char *cmd, size_t *resp_len)
7214{
7215 struct wpa_supplicant *wpa_s;
7216
7217 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7218 if (os_strcmp(ifname, wpa_s->ifname) == 0)
7219 break;
7220 }
7221
7222 if (wpa_s == NULL) {
7223 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
7224 if (resp)
7225 *resp_len = os_strlen(resp);
7226 else
7227 *resp_len = 1;
7228 return resp;
7229 }
7230
7231 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
7232}
7233
7234
7235static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
7236 char *buf, size_t *resp_len)
7237{
7238#ifdef CONFIG_P2P
7239 static const char * cmd[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07007240 "LIST_NETWORKS",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007241 "P2P_FIND",
7242 "P2P_STOP_FIND",
7243 "P2P_LISTEN",
7244 "P2P_GROUP_ADD",
7245 "P2P_GET_PASSPHRASE",
7246 "P2P_SERVICE_UPDATE",
7247 "P2P_SERVICE_FLUSH",
7248 "P2P_FLUSH",
7249 "P2P_CANCEL",
7250 "P2P_PRESENCE_REQ",
7251 "P2P_EXT_LISTEN",
7252 NULL
7253 };
7254 static const char * prefix[] = {
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08007255#ifdef ANDROID
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07007256 "DRIVER ",
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08007257#endif /* ANDROID */
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07007258 "GET_NETWORK ",
7259 "REMOVE_NETWORK ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007260 "P2P_FIND ",
7261 "P2P_CONNECT ",
7262 "P2P_LISTEN ",
7263 "P2P_GROUP_REMOVE ",
7264 "P2P_GROUP_ADD ",
7265 "P2P_PROV_DISC ",
7266 "P2P_SERV_DISC_REQ ",
7267 "P2P_SERV_DISC_CANCEL_REQ ",
7268 "P2P_SERV_DISC_RESP ",
7269 "P2P_SERV_DISC_EXTERNAL ",
7270 "P2P_SERVICE_ADD ",
7271 "P2P_SERVICE_DEL ",
7272 "P2P_REJECT ",
7273 "P2P_INVITE ",
7274 "P2P_PEER ",
7275 "P2P_SET ",
7276 "P2P_UNAUTHORIZE ",
7277 "P2P_PRESENCE_REQ ",
7278 "P2P_EXT_LISTEN ",
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007279 "P2P_REMOVE_CLIENT ",
Dmitry Shmidt413dde72014-04-11 10:23:22 -07007280 "NFC_GET_HANDOVER_SEL ",
7281 "NFC_GET_HANDOVER_REQ ",
7282 "NFC_REPORT_HANDOVER ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007283 NULL
7284 };
7285 int found = 0;
7286 int i;
7287
7288 if (global->p2p_init_wpa_s == NULL)
7289 return NULL;
7290
7291 for (i = 0; !found && cmd[i]; i++) {
7292 if (os_strcmp(buf, cmd[i]) == 0)
7293 found = 1;
7294 }
7295
7296 for (i = 0; !found && prefix[i]; i++) {
7297 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
7298 found = 1;
7299 }
7300
7301 if (found)
7302 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
7303 buf, resp_len);
7304#endif /* CONFIG_P2P */
7305 return NULL;
7306}
7307
7308
7309static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
7310 char *buf, size_t *resp_len)
7311{
7312#ifdef CONFIG_WIFI_DISPLAY
7313 if (global->p2p_init_wpa_s == NULL)
7314 return NULL;
7315 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
7316 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
7317 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
7318 buf, resp_len);
7319#endif /* CONFIG_WIFI_DISPLAY */
7320 return NULL;
7321}
7322
7323
7324static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
7325 char *buf, size_t *resp_len)
7326{
7327 char *ret;
7328
7329 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
7330 if (ret)
7331 return ret;
7332
7333 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
7334 if (ret)
7335 return ret;
7336
7337 return NULL;
7338}
7339
7340
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007341static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
7342{
7343 char *value;
7344
7345 value = os_strchr(cmd, ' ');
7346 if (value == NULL)
7347 return -1;
7348 *value++ = '\0';
7349
7350 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
7351
7352#ifdef CONFIG_WIFI_DISPLAY
7353 if (os_strcasecmp(cmd, "wifi_display") == 0) {
7354 wifi_display_enable(global, !!atoi(value));
7355 return 0;
7356 }
7357#endif /* CONFIG_WIFI_DISPLAY */
7358
Dmitry Shmidt61593f02014-04-21 16:27:35 -07007359 /* Restore cmd to its original value to allow redirection */
7360 value[-1] = ' ';
7361
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007362 return -1;
7363}
7364
7365
7366#ifndef CONFIG_NO_CONFIG_WRITE
7367static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
7368{
Dmitry Shmidt61593f02014-04-21 16:27:35 -07007369 int ret = 0, saved = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007370 struct wpa_supplicant *wpa_s;
7371
7372 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7373 if (!wpa_s->conf->update_config) {
7374 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
7375 continue;
7376 }
7377
7378 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
7379 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
7380 ret = 1;
7381 } else {
7382 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
Dmitry Shmidt61593f02014-04-21 16:27:35 -07007383 saved++;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007384 }
7385 }
7386
Dmitry Shmidt61593f02014-04-21 16:27:35 -07007387 if (!saved && !ret) {
7388 wpa_dbg(wpa_s, MSG_DEBUG,
7389 "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
7390 ret = 1;
7391 }
7392
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007393 return ret;
7394}
7395#endif /* CONFIG_NO_CONFIG_WRITE */
7396
7397
7398static int wpas_global_ctrl_iface_status(struct wpa_global *global,
7399 char *buf, size_t buflen)
7400{
7401 char *pos, *end;
7402 int ret;
7403 struct wpa_supplicant *wpa_s;
7404
7405 pos = buf;
7406 end = buf + buflen;
7407
7408#ifdef CONFIG_P2P
7409 if (global->p2p && !global->p2p_disabled) {
7410 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
7411 "\n"
7412 "p2p_state=%s\n",
7413 MAC2STR(global->p2p_dev_addr),
7414 p2p_get_state_txt(global->p2p));
7415 if (ret < 0 || ret >= end - pos)
7416 return pos - buf;
7417 pos += ret;
7418 } else if (global->p2p) {
7419 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
7420 if (ret < 0 || ret >= end - pos)
7421 return pos - buf;
7422 pos += ret;
7423 }
7424#endif /* CONFIG_P2P */
7425
7426#ifdef CONFIG_WIFI_DISPLAY
7427 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
7428 !!global->wifi_display);
7429 if (ret < 0 || ret >= end - pos)
7430 return pos - buf;
7431 pos += ret;
7432#endif /* CONFIG_WIFI_DISPLAY */
7433
7434 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7435 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
7436 "address=" MACSTR "\n",
7437 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
7438 if (ret < 0 || ret >= end - pos)
7439 return pos - buf;
7440 pos += ret;
7441 }
7442
7443 return pos - buf;
7444}
7445
7446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007447char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
7448 char *buf, size_t *resp_len)
7449{
7450 char *reply;
7451 const int reply_size = 2048;
7452 int reply_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007453 int level = MSG_DEBUG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007454
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007455 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
7456 char *pos = os_strchr(buf + 7, ' ');
7457 if (pos) {
7458 *pos++ = '\0';
7459 return wpas_global_ctrl_iface_ifname(global,
7460 buf + 7, pos,
7461 resp_len);
7462 }
7463 }
7464
7465 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
7466 if (reply)
7467 return reply;
7468
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007469 if (os_strcmp(buf, "PING") == 0)
7470 level = MSG_EXCESSIVE;
7471 wpa_hexdump_ascii(level, "RX global ctrl_iface",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007472 (const u8 *) buf, os_strlen(buf));
7473
7474 reply = os_malloc(reply_size);
7475 if (reply == NULL) {
7476 *resp_len = 1;
7477 return NULL;
7478 }
7479
7480 os_memcpy(reply, "OK\n", 3);
7481 reply_len = 3;
7482
7483 if (os_strcmp(buf, "PING") == 0) {
7484 os_memcpy(reply, "PONG\n", 5);
7485 reply_len = 5;
7486 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
7487 if (wpa_supplicant_global_iface_add(global, buf + 14))
7488 reply_len = -1;
7489 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
7490 if (wpa_supplicant_global_iface_remove(global, buf + 17))
7491 reply_len = -1;
7492 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
7493 reply_len = wpa_supplicant_global_iface_list(
7494 global, reply, reply_size);
7495 } else if (os_strcmp(buf, "INTERFACES") == 0) {
7496 reply_len = wpa_supplicant_global_iface_interfaces(
7497 global, reply, reply_size);
7498 } else if (os_strcmp(buf, "TERMINATE") == 0) {
7499 wpa_supplicant_terminate_proc(global);
7500 } else if (os_strcmp(buf, "SUSPEND") == 0) {
7501 wpas_notify_suspend(global);
7502 } else if (os_strcmp(buf, "RESUME") == 0) {
7503 wpas_notify_resume(global);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007504 } else if (os_strncmp(buf, "SET ", 4) == 0) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07007505 if (wpas_global_ctrl_iface_set(global, buf + 4)) {
7506#ifdef CONFIG_P2P
7507 if (global->p2p_init_wpa_s) {
7508 os_free(reply);
7509 /* Check if P2P redirection would work for this
7510 * command. */
7511 return wpa_supplicant_ctrl_iface_process(
7512 global->p2p_init_wpa_s,
7513 buf, resp_len);
7514 }
7515#endif /* CONFIG_P2P */
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007516 reply_len = -1;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07007517 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007518#ifndef CONFIG_NO_CONFIG_WRITE
7519 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
7520 if (wpas_global_ctrl_iface_save_config(global))
7521 reply_len = -1;
7522#endif /* CONFIG_NO_CONFIG_WRITE */
7523 } else if (os_strcmp(buf, "STATUS") == 0) {
7524 reply_len = wpas_global_ctrl_iface_status(global, reply,
7525 reply_size);
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08007526#ifdef CONFIG_MODULE_TESTS
7527 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
7528 int wpas_module_tests(void);
7529 if (wpas_module_tests() < 0)
7530 reply_len = -1;
7531#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007532 } else {
7533 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
7534 reply_len = 16;
7535 }
7536
7537 if (reply_len < 0) {
7538 os_memcpy(reply, "FAIL\n", 5);
7539 reply_len = 5;
7540 }
7541
7542 *resp_len = reply_len;
7543 return reply;
7544}