blob: 1b2bbbb6b30a4f639da90a0ca65a012c1ac737cb [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Control interface (shared code for all backends)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003 * Copyright (c) 2004-2013, 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"
13#include "common/version.h"
14#include "common/ieee802_11_defs.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070015#include "common/ieee802_11_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016#include "common/wpa_ctrl.h"
17#include "eap_peer/eap.h"
18#include "eapol_supp/eapol_supp_sm.h"
19#include "rsn_supp/wpa.h"
20#include "rsn_supp/preauth.h"
21#include "rsn_supp/pmksa_cache.h"
22#include "l2_packet/l2_packet.h"
23#include "wps/wps.h"
24#include "config.h"
25#include "wpa_supplicant_i.h"
26#include "driver_i.h"
27#include "wps_supplicant.h"
28#include "ibss_rsn.h"
29#include "ap.h"
30#include "p2p_supplicant.h"
31#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070032#include "hs20_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070033#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "notify.h"
35#include "bss.h"
36#include "scan.h"
37#include "ctrl_iface.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080038#include "interworking.h"
Dmitry Shmidte19501d2011-03-16 14:32:18 -070039#include "blacklist.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070040#include "autoscan.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080041#include "wnm_sta.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042
43extern struct wpa_driver_ops *wpa_drivers[];
44
45static 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);
49
50
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080051static int pno_start(struct wpa_supplicant *wpa_s)
52{
Dmitry Shmidt56052862013-10-04 10:23:25 -070053 int ret, interval;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080054 size_t i, num_ssid;
55 struct wpa_ssid *ssid;
56 struct wpa_driver_scan_params params;
57
58 if (wpa_s->pno)
59 return 0;
60
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080061 if (wpa_s->wpa_state == WPA_SCANNING) {
62 wpa_supplicant_cancel_sched_scan(wpa_s);
63 wpa_supplicant_cancel_scan(wpa_s);
64 }
65
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080066 os_memset(&params, 0, sizeof(params));
67
68 num_ssid = 0;
69 ssid = wpa_s->conf->ssid;
70 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070071 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080072 num_ssid++;
73 ssid = ssid->next;
74 }
75 if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
76 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
77 "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
78 num_ssid = WPAS_MAX_SCAN_SSIDS;
79 }
80
81 if (num_ssid == 0) {
82 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
83 return -1;
84 }
85
86 params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
87 num_ssid);
88 if (params.filter_ssids == NULL)
89 return -1;
90 i = 0;
91 ssid = wpa_s->conf->ssid;
92 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070093 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080094 params.ssids[i].ssid = ssid->ssid;
95 params.ssids[i].ssid_len = ssid->ssid_len;
96 params.num_ssids++;
97 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
98 ssid->ssid_len);
99 params.filter_ssids[i].ssid_len = ssid->ssid_len;
100 params.num_filter_ssids++;
101 i++;
102 if (i == num_ssid)
103 break;
104 }
105 ssid = ssid->next;
106 }
107
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700108 if (wpa_s->conf->filter_rssi)
109 params.filter_rssi = wpa_s->conf->filter_rssi;
110
Dmitry Shmidt56052862013-10-04 10:23:25 -0700111 interval = wpa_s->conf->sched_scan_interval ?
112 wpa_s->conf->sched_scan_interval : 10;
113
114 ret = wpa_drv_sched_scan(wpa_s, &params, interval * 1000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800115 os_free(params.filter_ssids);
116 if (ret == 0)
117 wpa_s->pno = 1;
118 return ret;
119}
120
121
122static int pno_stop(struct wpa_supplicant *wpa_s)
123{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800124 int ret = 0;
125
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800126 if (wpa_s->pno) {
127 wpa_s->pno = 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800128 ret = wpa_drv_stop_sched_scan(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800129 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800130
131 if (wpa_s->wpa_state == WPA_SCANNING)
132 wpa_supplicant_req_scan(wpa_s, 0, 0);
133
134 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800135}
136
137
Dmitry Shmidt04949592012-07-19 12:16:46 -0700138static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
139{
140 char *pos;
141 u8 addr[ETH_ALEN], *filter = NULL, *n;
142 size_t count = 0;
143
144 pos = val;
145 while (pos) {
146 if (*pos == '\0')
147 break;
148 if (hwaddr_aton(pos, addr)) {
149 os_free(filter);
150 return -1;
151 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700152 n = os_realloc_array(filter, count + 1, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700153 if (n == NULL) {
154 os_free(filter);
155 return -1;
156 }
157 filter = n;
158 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
159 count++;
160
161 pos = os_strchr(pos, ' ');
162 if (pos)
163 pos++;
164 }
165
166 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
167 os_free(wpa_s->bssid_filter);
168 wpa_s->bssid_filter = filter;
169 wpa_s->bssid_filter_count = count;
170
171 return 0;
172}
173
174
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800175static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
176{
177 char *pos;
178 u8 addr[ETH_ALEN], *bssid = NULL, *n;
179 struct wpa_ssid_value *ssid = NULL, *ns;
180 size_t count = 0, ssid_count = 0;
181 struct wpa_ssid *c;
182
183 /*
184 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | “”
185 * SSID_SPEC ::= ssid <SSID_HEX>
186 * BSSID_SPEC ::= bssid <BSSID_HEX>
187 */
188
189 pos = val;
190 while (pos) {
191 if (*pos == '\0')
192 break;
193 if (os_strncmp(pos, "bssid ", 6) == 0) {
194 int res;
195 pos += 6;
196 res = hwaddr_aton2(pos, addr);
197 if (res < 0) {
198 os_free(ssid);
199 os_free(bssid);
200 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
201 "BSSID value '%s'", pos);
202 return -1;
203 }
204 pos += res;
205 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
206 if (n == NULL) {
207 os_free(ssid);
208 os_free(bssid);
209 return -1;
210 }
211 bssid = n;
212 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
213 count++;
214 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
215 char *end;
216 pos += 5;
217
218 end = pos;
219 while (*end) {
220 if (*end == '\0' || *end == ' ')
221 break;
222 end++;
223 }
224
225 ns = os_realloc_array(ssid, ssid_count + 1,
226 sizeof(struct wpa_ssid_value));
227 if (ns == NULL) {
228 os_free(ssid);
229 os_free(bssid);
230 return -1;
231 }
232 ssid = ns;
233
234 if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
235 hexstr2bin(pos, ssid[ssid_count].ssid,
236 (end - pos) / 2) < 0) {
237 os_free(ssid);
238 os_free(bssid);
239 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
240 "SSID value '%s'", pos);
241 return -1;
242 }
243 ssid[ssid_count].ssid_len = (end - pos) / 2;
244 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
245 ssid[ssid_count].ssid,
246 ssid[ssid_count].ssid_len);
247 ssid_count++;
248 pos = end;
249 } else {
250 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
251 "'%s'", pos);
252 os_free(ssid);
253 os_free(bssid);
254 return -1;
255 }
256
257 pos = os_strchr(pos, ' ');
258 if (pos)
259 pos++;
260 }
261
262 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
263 os_free(wpa_s->disallow_aps_bssid);
264 wpa_s->disallow_aps_bssid = bssid;
265 wpa_s->disallow_aps_bssid_count = count;
266
267 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
268 os_free(wpa_s->disallow_aps_ssid);
269 wpa_s->disallow_aps_ssid = ssid;
270 wpa_s->disallow_aps_ssid_count = ssid_count;
271
272 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
273 return 0;
274
275 c = wpa_s->current_ssid;
276 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
277 return 0;
278
279 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
280 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
281 return 0;
282
283 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
284 "because current AP was marked disallowed");
285
286#ifdef CONFIG_SME
287 wpa_s->sme.prev_bssid_set = 0;
288#endif /* CONFIG_SME */
289 wpa_s->reassociate = 1;
290 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
291 wpa_supplicant_req_scan(wpa_s, 0, 0);
292
293 return 0;
294}
295
296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
298 char *cmd)
299{
300 char *value;
301 int ret = 0;
302
303 value = os_strchr(cmd, ' ');
304 if (value == NULL)
305 return -1;
306 *value++ = '\0';
307
308 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
309 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
310 eapol_sm_configure(wpa_s->eapol,
311 atoi(value), -1, -1, -1);
312 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
313 eapol_sm_configure(wpa_s->eapol,
314 -1, atoi(value), -1, -1);
315 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
316 eapol_sm_configure(wpa_s->eapol,
317 -1, -1, atoi(value), -1);
318 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
319 eapol_sm_configure(wpa_s->eapol,
320 -1, -1, -1, atoi(value));
321 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
322 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
323 atoi(value)))
324 ret = -1;
325 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
326 0) {
327 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
328 atoi(value)))
329 ret = -1;
330 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
331 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
332 ret = -1;
333 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
334 wpa_s->wps_fragment_size = atoi(value);
335#ifdef CONFIG_WPS_TESTING
336 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
337 long int val;
338 val = strtol(value, NULL, 0);
339 if (val < 0 || val > 0xff) {
340 ret = -1;
341 wpa_printf(MSG_DEBUG, "WPS: Invalid "
342 "wps_version_number %ld", val);
343 } else {
344 wps_version_number = val;
345 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
346 "version %u.%u",
347 (wps_version_number & 0xf0) >> 4,
348 wps_version_number & 0x0f);
349 }
350 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
351 wps_testing_dummy_cred = atoi(value);
352 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
353 wps_testing_dummy_cred);
354#endif /* CONFIG_WPS_TESTING */
355 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
356 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
357 ret = -1;
358#ifdef CONFIG_TDLS_TESTING
359 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
360 extern unsigned int tdls_testing;
361 tdls_testing = strtol(value, NULL, 0);
362 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
363#endif /* CONFIG_TDLS_TESTING */
364#ifdef CONFIG_TDLS
365 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
366 int disabled = atoi(value);
367 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
368 if (disabled) {
369 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
370 ret = -1;
371 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
372 ret = -1;
373 wpa_tdls_enable(wpa_s->wpa, !disabled);
374#endif /* CONFIG_TDLS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800375 } else if (os_strcasecmp(cmd, "pno") == 0) {
376 if (atoi(value))
377 ret = pno_start(wpa_s);
378 else
379 ret = pno_stop(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700380 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
381 int disabled = atoi(value);
382 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
383 ret = -1;
384 else if (disabled)
385 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
386 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
387 if (os_strcmp(value, "disable") == 0)
388 wpa_s->set_sta_uapsd = 0;
389 else {
390 int be, bk, vi, vo;
391 char *pos;
392 /* format: BE,BK,VI,VO;max SP Length */
393 be = atoi(value);
394 pos = os_strchr(value, ',');
395 if (pos == NULL)
396 return -1;
397 pos++;
398 bk = atoi(pos);
399 pos = os_strchr(pos, ',');
400 if (pos == NULL)
401 return -1;
402 pos++;
403 vi = atoi(pos);
404 pos = os_strchr(pos, ',');
405 if (pos == NULL)
406 return -1;
407 pos++;
408 vo = atoi(pos);
409 /* ignore max SP Length for now */
410
411 wpa_s->set_sta_uapsd = 1;
412 wpa_s->sta_uapsd = 0;
413 if (be)
414 wpa_s->sta_uapsd |= BIT(0);
415 if (bk)
416 wpa_s->sta_uapsd |= BIT(1);
417 if (vi)
418 wpa_s->sta_uapsd |= BIT(2);
419 if (vo)
420 wpa_s->sta_uapsd |= BIT(3);
421 }
Jouni Malinen21d6bc82012-04-10 16:17:59 -0700422 } else if (os_strcasecmp(cmd, "ps") == 0) {
423 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700424#ifdef CONFIG_WIFI_DISPLAY
425 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
426 wifi_display_enable(wpa_s->global, !!atoi(value));
427#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700428 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
429 ret = set_bssid_filter(wpa_s, value);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800430 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
431 ret = set_disallow_aps(wpa_s, value);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800432 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
433 wpa_s->no_keep_alive = !!atoi(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700434 } else {
435 value[-1] = '=';
436 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
437 if (ret == 0)
438 wpa_supplicant_update_config(wpa_s);
439 }
440
441 return ret;
442}
443
444
445static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
446 char *cmd, char *buf, size_t buflen)
447{
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700448 int res = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449
450 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
451
452 if (os_strcmp(cmd, "version") == 0) {
453 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700454 } else if (os_strcasecmp(cmd, "country") == 0) {
455 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
456 res = os_snprintf(buf, buflen, "%c%c",
457 wpa_s->conf->country[0],
458 wpa_s->conf->country[1]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700459#ifdef CONFIG_WIFI_DISPLAY
460 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
461 res = os_snprintf(buf, buflen, "%d",
462 wpa_s->global->wifi_display);
463 if (res < 0 || (unsigned int) res >= buflen)
464 return -1;
465 return res;
466#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700467#ifdef CONFIG_TESTING_GET_GTK
468 } else if (os_strcmp(cmd, "gtk") == 0) {
469 if (wpa_s->last_gtk_len == 0)
470 return -1;
471 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
472 wpa_s->last_gtk_len);
473 return res;
474#endif /* CONFIG_TESTING_GET_GTK */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 }
476
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700477 if (res < 0 || (unsigned int) res >= buflen)
478 return -1;
479 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480}
481
482
483#ifdef IEEE8021X_EAPOL
484static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
485 char *addr)
486{
487 u8 bssid[ETH_ALEN];
488 struct wpa_ssid *ssid = wpa_s->current_ssid;
489
490 if (hwaddr_aton(addr, bssid)) {
491 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
492 "'%s'", addr);
493 return -1;
494 }
495
496 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
497 rsn_preauth_deinit(wpa_s->wpa);
498 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
499 return -1;
500
501 return 0;
502}
503#endif /* IEEE8021X_EAPOL */
504
505
506#ifdef CONFIG_PEERKEY
507/* MLME-STKSTART.request(peer) */
508static int wpa_supplicant_ctrl_iface_stkstart(
509 struct wpa_supplicant *wpa_s, char *addr)
510{
511 u8 peer[ETH_ALEN];
512
513 if (hwaddr_aton(addr, peer)) {
514 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
515 "address '%s'", addr);
516 return -1;
517 }
518
519 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
520 MAC2STR(peer));
521
522 return wpa_sm_stkstart(wpa_s->wpa, peer);
523}
524#endif /* CONFIG_PEERKEY */
525
526
527#ifdef CONFIG_TDLS
528
529static int wpa_supplicant_ctrl_iface_tdls_discover(
530 struct wpa_supplicant *wpa_s, char *addr)
531{
532 u8 peer[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800533 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700534
535 if (hwaddr_aton(addr, peer)) {
536 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
537 "address '%s'", addr);
538 return -1;
539 }
540
541 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
542 MAC2STR(peer));
543
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800544 if (wpa_tdls_is_external_setup(wpa_s->wpa))
545 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
546 else
547 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
548
549 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550}
551
552
553static int wpa_supplicant_ctrl_iface_tdls_setup(
554 struct wpa_supplicant *wpa_s, char *addr)
555{
556 u8 peer[ETH_ALEN];
557 int ret;
558
559 if (hwaddr_aton(addr, peer)) {
560 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
561 "address '%s'", addr);
562 return -1;
563 }
564
565 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
566 MAC2STR(peer));
567
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800568 wpa_tdls_remove(wpa_s->wpa, peer);
569
570 if (wpa_tdls_is_external_setup(wpa_s->wpa))
571 ret = wpa_tdls_start(wpa_s->wpa, peer);
572 else
573 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800574
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700575 return ret;
576}
577
578
579static int wpa_supplicant_ctrl_iface_tdls_teardown(
580 struct wpa_supplicant *wpa_s, char *addr)
581{
582 u8 peer[ETH_ALEN];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700583 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584
585 if (hwaddr_aton(addr, peer)) {
586 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
587 "address '%s'", addr);
588 return -1;
589 }
590
591 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
592 MAC2STR(peer));
593
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700594 if (wpa_tdls_is_external_setup(wpa_s->wpa))
595 ret = wpa_tdls_teardown_link(
596 wpa_s->wpa, peer,
597 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
598 else
599 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
600
601 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700602}
603
604#endif /* CONFIG_TDLS */
605
606
607#ifdef CONFIG_IEEE80211R
608static int wpa_supplicant_ctrl_iface_ft_ds(
609 struct wpa_supplicant *wpa_s, char *addr)
610{
611 u8 target_ap[ETH_ALEN];
612 struct wpa_bss *bss;
613 const u8 *mdie;
614
615 if (hwaddr_aton(addr, target_ap)) {
616 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
617 "address '%s'", addr);
618 return -1;
619 }
620
621 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
622
623 bss = wpa_bss_get_bssid(wpa_s, target_ap);
624 if (bss)
625 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
626 else
627 mdie = NULL;
628
629 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
630}
631#endif /* CONFIG_IEEE80211R */
632
633
634#ifdef CONFIG_WPS
635static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
636 char *cmd)
637{
638 u8 bssid[ETH_ALEN], *_bssid = bssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700639#ifdef CONFIG_P2P
640 u8 p2p_dev_addr[ETH_ALEN];
641#endif /* CONFIG_P2P */
642#ifdef CONFIG_AP
643 u8 *_p2p_dev_addr = NULL;
644#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800645
Irfan Sheriffa7534b92012-09-06 18:30:39 -0700646 if (cmd == NULL || os_strcmp(cmd, "any") == 0 || cmd[0] == '\0') {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700647 _bssid = NULL;
648#ifdef CONFIG_P2P
649 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
650 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
651 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
652 "P2P Device Address '%s'",
653 cmd + 13);
654 return -1;
655 }
656 _p2p_dev_addr = p2p_dev_addr;
657#endif /* CONFIG_P2P */
658 } else if (hwaddr_aton(cmd, bssid)) {
659 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
660 cmd);
661 return -1;
662 }
663
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800664#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700665 if (wpa_s->ap_iface)
666 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
667#endif /* CONFIG_AP */
668
669 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
670}
671
672
673static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
674 char *cmd, char *buf,
675 size_t buflen)
676{
677 u8 bssid[ETH_ALEN], *_bssid = bssid;
678 char *pin;
679 int ret;
680
681 pin = os_strchr(cmd, ' ');
682 if (pin)
683 *pin++ = '\0';
684
685 if (os_strcmp(cmd, "any") == 0)
686 _bssid = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800687 else if (os_strcmp(cmd, "get") == 0) {
688 ret = wps_generate_pin();
689 goto done;
690 } else if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
692 cmd);
693 return -1;
694 }
695
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800696#ifdef CONFIG_AP
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800697 if (wpa_s->ap_iface) {
698 int timeout = 0;
699 char *pos;
700
701 if (pin) {
702 pos = os_strchr(pin, ' ');
703 if (pos) {
704 *pos++ = '\0';
705 timeout = atoi(pos);
706 }
707 }
708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800710 buf, buflen, timeout);
711 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700712#endif /* CONFIG_AP */
713
714 if (pin) {
715 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
716 DEV_PW_DEFAULT);
717 if (ret < 0)
718 return -1;
719 ret = os_snprintf(buf, buflen, "%s", pin);
720 if (ret < 0 || (size_t) ret >= buflen)
721 return -1;
722 return ret;
723 }
724
725 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
726 if (ret < 0)
727 return -1;
728
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800729done:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730 /* Return the generated PIN */
731 ret = os_snprintf(buf, buflen, "%08d", ret);
732 if (ret < 0 || (size_t) ret >= buflen)
733 return -1;
734 return ret;
735}
736
737
738static int wpa_supplicant_ctrl_iface_wps_check_pin(
739 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
740{
741 char pin[9];
742 size_t len;
743 char *pos;
744 int ret;
745
746 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
747 (u8 *) cmd, os_strlen(cmd));
748 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
749 if (*pos < '0' || *pos > '9')
750 continue;
751 pin[len++] = *pos;
752 if (len == 9) {
753 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
754 return -1;
755 }
756 }
757 if (len != 4 && len != 8) {
758 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
759 return -1;
760 }
761 pin[len] = '\0';
762
763 if (len == 8) {
764 unsigned int pin_val;
765 pin_val = atoi(pin);
766 if (!wps_pin_valid(pin_val)) {
767 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
768 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
769 if (ret < 0 || (size_t) ret >= buflen)
770 return -1;
771 return ret;
772 }
773 }
774
775 ret = os_snprintf(buf, buflen, "%s", pin);
776 if (ret < 0 || (size_t) ret >= buflen)
777 return -1;
778
779 return ret;
780}
781
782
Dmitry Shmidt04949592012-07-19 12:16:46 -0700783#ifdef CONFIG_WPS_NFC
784
785static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
786 char *cmd)
787{
788 u8 bssid[ETH_ALEN], *_bssid = bssid;
789
790 if (cmd == NULL || cmd[0] == '\0')
791 _bssid = NULL;
792 else if (hwaddr_aton(cmd, bssid))
793 return -1;
794
795 return wpas_wps_start_nfc(wpa_s, _bssid);
796}
797
798
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800799static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
800 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
801{
802 int ndef;
803 struct wpabuf *buf;
804 int res;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700805 char *pos;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800806
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700807 pos = os_strchr(cmd, ' ');
808 if (pos)
809 *pos++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800810 if (os_strcmp(cmd, "WPS") == 0)
811 ndef = 0;
812 else if (os_strcmp(cmd, "NDEF") == 0)
813 ndef = 1;
814 else
815 return -1;
816
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700817 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800818 if (buf == NULL)
819 return -1;
820
821 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
822 wpabuf_len(buf));
823 reply[res++] = '\n';
824 reply[res] = '\0';
825
826 wpabuf_free(buf);
827
828 return res;
829}
830
831
Dmitry Shmidt04949592012-07-19 12:16:46 -0700832static int wpa_supplicant_ctrl_iface_wps_nfc_token(
833 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
834{
835 int ndef;
836 struct wpabuf *buf;
837 int res;
838
839 if (os_strcmp(cmd, "WPS") == 0)
840 ndef = 0;
841 else if (os_strcmp(cmd, "NDEF") == 0)
842 ndef = 1;
843 else
844 return -1;
845
846 buf = wpas_wps_nfc_token(wpa_s, ndef);
847 if (buf == NULL)
848 return -1;
849
850 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
851 wpabuf_len(buf));
852 reply[res++] = '\n';
853 reply[res] = '\0';
854
855 wpabuf_free(buf);
856
857 return res;
858}
859
860
861static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
862 struct wpa_supplicant *wpa_s, char *pos)
863{
864 size_t len;
865 struct wpabuf *buf;
866 int ret;
867
868 len = os_strlen(pos);
869 if (len & 0x01)
870 return -1;
871 len /= 2;
872
873 buf = wpabuf_alloc(len);
874 if (buf == NULL)
875 return -1;
876 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
877 wpabuf_free(buf);
878 return -1;
879 }
880
881 ret = wpas_wps_nfc_tag_read(wpa_s, buf);
882 wpabuf_free(buf);
883
884 return ret;
885}
886
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800887
888static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800889 char *reply, size_t max_len,
890 int cr)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800891{
892 struct wpabuf *buf;
893 int res;
894
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800895 buf = wpas_wps_nfc_handover_req(wpa_s, cr);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800896 if (buf == NULL)
897 return -1;
898
899 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
900 wpabuf_len(buf));
901 reply[res++] = '\n';
902 reply[res] = '\0';
903
904 wpabuf_free(buf);
905
906 return res;
907}
908
909
910static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
911 char *cmd, char *reply,
912 size_t max_len)
913{
914 char *pos;
915
916 pos = os_strchr(cmd, ' ');
917 if (pos == NULL)
918 return -1;
919 *pos++ = '\0';
920
921 if (os_strcmp(cmd, "NDEF") != 0)
922 return -1;
923
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800924 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
925 return wpas_ctrl_nfc_get_handover_req_wps(
926 wpa_s, reply, max_len, os_strcmp(pos, "WPS-CR") == 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800927 }
928
929 return -1;
930}
931
932
933static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800934 char *reply, size_t max_len,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800935 int ndef, int cr, char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800936{
937 struct wpabuf *buf;
938 int res;
939
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800940 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800941 if (buf == NULL)
942 return -1;
943
944 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
945 wpabuf_len(buf));
946 reply[res++] = '\n';
947 reply[res] = '\0';
948
949 wpabuf_free(buf);
950
951 return res;
952}
953
954
955static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
956 char *cmd, char *reply,
957 size_t max_len)
958{
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800959 char *pos, *pos2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800960 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800961
962 pos = os_strchr(cmd, ' ');
963 if (pos == NULL)
964 return -1;
965 *pos++ = '\0';
966
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800967 if (os_strcmp(cmd, "WPS") == 0)
968 ndef = 0;
969 else if (os_strcmp(cmd, "NDEF") == 0)
970 ndef = 1;
971 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800972 return -1;
973
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800974 pos2 = os_strchr(pos, ' ');
975 if (pos2)
976 *pos2++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800977 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
978 return wpas_ctrl_nfc_get_handover_sel_wps(
979 wpa_s, reply, max_len, ndef,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800980 os_strcmp(pos, "WPS-CR") == 0, pos2);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800981 }
982
983 return -1;
984}
985
986
987static int wpas_ctrl_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
988 char *cmd, char *reply,
989 size_t max_len)
990{
991 size_t len;
992 struct wpabuf *buf;
993 int ret;
994
995 len = os_strlen(cmd);
996 if (len & 0x01)
997 return -1;
998 len /= 2;
999
1000 buf = wpabuf_alloc(len);
1001 if (buf == NULL)
1002 return -1;
1003 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
1004 wpabuf_free(buf);
1005 return -1;
1006 }
1007
1008 ret = wpas_wps_nfc_rx_handover_req(wpa_s, buf);
1009 wpabuf_free(buf);
1010
1011 return ret;
1012}
1013
1014
1015static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
1016 char *cmd)
1017{
1018 size_t len;
1019 struct wpabuf *buf;
1020 int ret;
1021
1022 len = os_strlen(cmd);
1023 if (len & 0x01)
1024 return -1;
1025 len /= 2;
1026
1027 buf = wpabuf_alloc(len);
1028 if (buf == NULL)
1029 return -1;
1030 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
1031 wpabuf_free(buf);
1032 return -1;
1033 }
1034
1035 ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
1036 wpabuf_free(buf);
1037
1038 return ret;
1039}
1040
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001041
1042static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1043 char *cmd)
1044{
1045 size_t len;
1046 struct wpabuf *req, *sel;
1047 int ret;
1048 char *pos, *role, *type, *pos2;
1049
1050 role = cmd;
1051 pos = os_strchr(role, ' ');
1052 if (pos == NULL)
1053 return -1;
1054 *pos++ = '\0';
1055
1056 type = pos;
1057 pos = os_strchr(type, ' ');
1058 if (pos == NULL)
1059 return -1;
1060 *pos++ = '\0';
1061
1062 pos2 = os_strchr(pos, ' ');
1063 if (pos2 == NULL)
1064 return -1;
1065 *pos2++ = '\0';
1066
1067 len = os_strlen(pos);
1068 if (len & 0x01)
1069 return -1;
1070 len /= 2;
1071
1072 req = wpabuf_alloc(len);
1073 if (req == NULL)
1074 return -1;
1075 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
1076 wpabuf_free(req);
1077 return -1;
1078 }
1079
1080 len = os_strlen(pos2);
1081 if (len & 0x01) {
1082 wpabuf_free(req);
1083 return -1;
1084 }
1085 len /= 2;
1086
1087 sel = wpabuf_alloc(len);
1088 if (sel == NULL) {
1089 wpabuf_free(req);
1090 return -1;
1091 }
1092 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
1093 wpabuf_free(req);
1094 wpabuf_free(sel);
1095 return -1;
1096 }
1097
1098 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1099 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
1100 } else {
1101 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1102 "reported: role=%s type=%s", role, type);
1103 ret = -1;
1104 }
1105 wpabuf_free(req);
1106 wpabuf_free(sel);
1107
1108 return ret;
1109}
1110
Dmitry Shmidt04949592012-07-19 12:16:46 -07001111#endif /* CONFIG_WPS_NFC */
1112
1113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1115 char *cmd)
1116{
1117 u8 bssid[ETH_ALEN];
1118 char *pin;
1119 char *new_ssid;
1120 char *new_auth;
1121 char *new_encr;
1122 char *new_key;
1123 struct wps_new_ap_settings ap;
1124
1125 pin = os_strchr(cmd, ' ');
1126 if (pin == NULL)
1127 return -1;
1128 *pin++ = '\0';
1129
1130 if (hwaddr_aton(cmd, bssid)) {
1131 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1132 cmd);
1133 return -1;
1134 }
1135
1136 new_ssid = os_strchr(pin, ' ');
1137 if (new_ssid == NULL)
1138 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1139 *new_ssid++ = '\0';
1140
1141 new_auth = os_strchr(new_ssid, ' ');
1142 if (new_auth == NULL)
1143 return -1;
1144 *new_auth++ = '\0';
1145
1146 new_encr = os_strchr(new_auth, ' ');
1147 if (new_encr == NULL)
1148 return -1;
1149 *new_encr++ = '\0';
1150
1151 new_key = os_strchr(new_encr, ' ');
1152 if (new_key == NULL)
1153 return -1;
1154 *new_key++ = '\0';
1155
1156 os_memset(&ap, 0, sizeof(ap));
1157 ap.ssid_hex = new_ssid;
1158 ap.auth = new_auth;
1159 ap.encr = new_encr;
1160 ap.key_hex = new_key;
1161 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1162}
1163
1164
1165#ifdef CONFIG_AP
1166static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1167 char *cmd, char *buf,
1168 size_t buflen)
1169{
1170 int timeout = 300;
1171 char *pos;
1172 const char *pin_txt;
1173
1174 if (!wpa_s->ap_iface)
1175 return -1;
1176
1177 pos = os_strchr(cmd, ' ');
1178 if (pos)
1179 *pos++ = '\0';
1180
1181 if (os_strcmp(cmd, "disable") == 0) {
1182 wpas_wps_ap_pin_disable(wpa_s);
1183 return os_snprintf(buf, buflen, "OK\n");
1184 }
1185
1186 if (os_strcmp(cmd, "random") == 0) {
1187 if (pos)
1188 timeout = atoi(pos);
1189 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1190 if (pin_txt == NULL)
1191 return -1;
1192 return os_snprintf(buf, buflen, "%s", pin_txt);
1193 }
1194
1195 if (os_strcmp(cmd, "get") == 0) {
1196 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1197 if (pin_txt == NULL)
1198 return -1;
1199 return os_snprintf(buf, buflen, "%s", pin_txt);
1200 }
1201
1202 if (os_strcmp(cmd, "set") == 0) {
1203 char *pin;
1204 if (pos == NULL)
1205 return -1;
1206 pin = pos;
1207 pos = os_strchr(pos, ' ');
1208 if (pos) {
1209 *pos++ = '\0';
1210 timeout = atoi(pos);
1211 }
1212 if (os_strlen(pin) > buflen)
1213 return -1;
1214 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1215 return -1;
1216 return os_snprintf(buf, buflen, "%s", pin);
1217 }
1218
1219 return -1;
1220}
1221#endif /* CONFIG_AP */
1222
1223
1224#ifdef CONFIG_WPS_ER
1225static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1226 char *cmd)
1227{
1228 char *uuid = cmd, *pin, *pos;
1229 u8 addr_buf[ETH_ALEN], *addr = NULL;
1230 pin = os_strchr(uuid, ' ');
1231 if (pin == NULL)
1232 return -1;
1233 *pin++ = '\0';
1234 pos = os_strchr(pin, ' ');
1235 if (pos) {
1236 *pos++ = '\0';
1237 if (hwaddr_aton(pos, addr_buf) == 0)
1238 addr = addr_buf;
1239 }
1240 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1241}
1242
1243
1244static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1245 char *cmd)
1246{
1247 char *uuid = cmd, *pin;
1248 pin = os_strchr(uuid, ' ');
1249 if (pin == NULL)
1250 return -1;
1251 *pin++ = '\0';
1252 return wpas_wps_er_learn(wpa_s, uuid, pin);
1253}
1254
1255
1256static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1257 struct wpa_supplicant *wpa_s, char *cmd)
1258{
1259 char *uuid = cmd, *id;
1260 id = os_strchr(uuid, ' ');
1261 if (id == NULL)
1262 return -1;
1263 *id++ = '\0';
1264 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1265}
1266
1267
1268static int wpa_supplicant_ctrl_iface_wps_er_config(
1269 struct wpa_supplicant *wpa_s, char *cmd)
1270{
1271 char *pin;
1272 char *new_ssid;
1273 char *new_auth;
1274 char *new_encr;
1275 char *new_key;
1276 struct wps_new_ap_settings ap;
1277
1278 pin = os_strchr(cmd, ' ');
1279 if (pin == NULL)
1280 return -1;
1281 *pin++ = '\0';
1282
1283 new_ssid = os_strchr(pin, ' ');
1284 if (new_ssid == NULL)
1285 return -1;
1286 *new_ssid++ = '\0';
1287
1288 new_auth = os_strchr(new_ssid, ' ');
1289 if (new_auth == NULL)
1290 return -1;
1291 *new_auth++ = '\0';
1292
1293 new_encr = os_strchr(new_auth, ' ');
1294 if (new_encr == NULL)
1295 return -1;
1296 *new_encr++ = '\0';
1297
1298 new_key = os_strchr(new_encr, ' ');
1299 if (new_key == NULL)
1300 return -1;
1301 *new_key++ = '\0';
1302
1303 os_memset(&ap, 0, sizeof(ap));
1304 ap.ssid_hex = new_ssid;
1305 ap.auth = new_auth;
1306 ap.encr = new_encr;
1307 ap.key_hex = new_key;
1308 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1309}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001310
1311
1312#ifdef CONFIG_WPS_NFC
1313static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1314 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1315{
1316 int ndef;
1317 struct wpabuf *buf;
1318 int res;
1319 char *uuid;
1320
1321 uuid = os_strchr(cmd, ' ');
1322 if (uuid == NULL)
1323 return -1;
1324 *uuid++ = '\0';
1325
1326 if (os_strcmp(cmd, "WPS") == 0)
1327 ndef = 0;
1328 else if (os_strcmp(cmd, "NDEF") == 0)
1329 ndef = 1;
1330 else
1331 return -1;
1332
1333 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1334 if (buf == NULL)
1335 return -1;
1336
1337 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1338 wpabuf_len(buf));
1339 reply[res++] = '\n';
1340 reply[res] = '\0';
1341
1342 wpabuf_free(buf);
1343
1344 return res;
1345}
1346#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347#endif /* CONFIG_WPS_ER */
1348
1349#endif /* CONFIG_WPS */
1350
1351
1352#ifdef CONFIG_IBSS_RSN
1353static int wpa_supplicant_ctrl_iface_ibss_rsn(
1354 struct wpa_supplicant *wpa_s, char *addr)
1355{
1356 u8 peer[ETH_ALEN];
1357
1358 if (hwaddr_aton(addr, peer)) {
1359 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1360 "address '%s'", addr);
1361 return -1;
1362 }
1363
1364 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1365 MAC2STR(peer));
1366
1367 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1368}
1369#endif /* CONFIG_IBSS_RSN */
1370
1371
1372static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1373 char *rsp)
1374{
1375#ifdef IEEE8021X_EAPOL
1376 char *pos, *id_pos;
1377 int id;
1378 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379
1380 pos = os_strchr(rsp, '-');
1381 if (pos == NULL)
1382 return -1;
1383 *pos++ = '\0';
1384 id_pos = pos;
1385 pos = os_strchr(pos, ':');
1386 if (pos == NULL)
1387 return -1;
1388 *pos++ = '\0';
1389 id = atoi(id_pos);
1390 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1391 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1392 (u8 *) pos, os_strlen(pos));
1393
1394 ssid = wpa_config_get_network(wpa_s->conf, id);
1395 if (ssid == NULL) {
1396 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1397 "to update", id);
1398 return -1;
1399 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001401 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1402 pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403#else /* IEEE8021X_EAPOL */
1404 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1405 return -1;
1406#endif /* IEEE8021X_EAPOL */
1407}
1408
1409
1410static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1411 const char *params,
1412 char *buf, size_t buflen)
1413{
1414 char *pos, *end, tmp[30];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001415 int res, verbose, wps, ret;
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001416
Dmitry Shmidt56052862013-10-04 10:23:25 -07001417 if (os_strcmp(params, "-DRIVER") == 0)
1418 return wpa_drv_status(wpa_s, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001419 verbose = os_strcmp(params, "-VERBOSE") == 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001420 wps = os_strcmp(params, "-WPS") == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421 pos = buf;
1422 end = buf + buflen;
1423 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1424 struct wpa_ssid *ssid = wpa_s->current_ssid;
1425 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1426 MAC2STR(wpa_s->bssid));
1427 if (ret < 0 || ret >= end - pos)
1428 return pos - buf;
1429 pos += ret;
1430 if (ssid) {
1431 u8 *_ssid = ssid->ssid;
1432 size_t ssid_len = ssid->ssid_len;
1433 u8 ssid_buf[MAX_SSID_LEN];
1434 if (ssid_len == 0) {
1435 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1436 if (_res < 0)
1437 ssid_len = 0;
1438 else
1439 ssid_len = _res;
1440 _ssid = ssid_buf;
1441 }
1442 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1443 wpa_ssid_txt(_ssid, ssid_len),
1444 ssid->id);
1445 if (ret < 0 || ret >= end - pos)
1446 return pos - buf;
1447 pos += ret;
1448
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001449 if (wps && ssid->passphrase &&
1450 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1451 (ssid->mode == WPAS_MODE_AP ||
1452 ssid->mode == WPAS_MODE_P2P_GO)) {
1453 ret = os_snprintf(pos, end - pos,
1454 "passphrase=%s\n",
1455 ssid->passphrase);
1456 if (ret < 0 || ret >= end - pos)
1457 return pos - buf;
1458 pos += ret;
1459 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 if (ssid->id_str) {
1461 ret = os_snprintf(pos, end - pos,
1462 "id_str=%s\n",
1463 ssid->id_str);
1464 if (ret < 0 || ret >= end - pos)
1465 return pos - buf;
1466 pos += ret;
1467 }
1468
1469 switch (ssid->mode) {
1470 case WPAS_MODE_INFRA:
1471 ret = os_snprintf(pos, end - pos,
1472 "mode=station\n");
1473 break;
1474 case WPAS_MODE_IBSS:
1475 ret = os_snprintf(pos, end - pos,
1476 "mode=IBSS\n");
1477 break;
1478 case WPAS_MODE_AP:
1479 ret = os_snprintf(pos, end - pos,
1480 "mode=AP\n");
1481 break;
1482 case WPAS_MODE_P2P_GO:
1483 ret = os_snprintf(pos, end - pos,
1484 "mode=P2P GO\n");
1485 break;
1486 case WPAS_MODE_P2P_GROUP_FORMATION:
1487 ret = os_snprintf(pos, end - pos,
1488 "mode=P2P GO - group "
1489 "formation\n");
1490 break;
1491 default:
1492 ret = 0;
1493 break;
1494 }
1495 if (ret < 0 || ret >= end - pos)
1496 return pos - buf;
1497 pos += ret;
1498 }
1499
1500#ifdef CONFIG_AP
1501 if (wpa_s->ap_iface) {
1502 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1503 end - pos,
1504 verbose);
1505 } else
1506#endif /* CONFIG_AP */
1507 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1508 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001509#ifdef CONFIG_SAE
1510 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001511#ifdef CONFIG_AP
1512 !wpa_s->ap_iface &&
1513#endif /* CONFIG_AP */
1514 wpa_s->sme.sae.state == SAE_ACCEPTED) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001515 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1516 wpa_s->sme.sae.group);
1517 if (ret < 0 || ret >= end - pos)
1518 return pos - buf;
1519 pos += ret;
1520 }
1521#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001522 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1523 wpa_supplicant_state_txt(wpa_s->wpa_state));
1524 if (ret < 0 || ret >= end - pos)
1525 return pos - buf;
1526 pos += ret;
1527
1528 if (wpa_s->l2 &&
1529 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1530 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
1531 if (ret < 0 || ret >= end - pos)
1532 return pos - buf;
1533 pos += ret;
1534 }
1535
1536#ifdef CONFIG_P2P
1537 if (wpa_s->global->p2p) {
1538 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1539 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
1540 if (ret < 0 || ret >= end - pos)
1541 return pos - buf;
1542 pos += ret;
1543 }
1544#endif /* CONFIG_P2P */
1545
1546 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1547 MAC2STR(wpa_s->own_addr));
1548 if (ret < 0 || ret >= end - pos)
1549 return pos - buf;
1550 pos += ret;
1551
Dmitry Shmidt04949592012-07-19 12:16:46 -07001552#ifdef CONFIG_HS20
1553 if (wpa_s->current_bss &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001554 wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
1555 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1556 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001557 ret = os_snprintf(pos, end - pos, "hs20=1\n");
1558 if (ret < 0 || ret >= end - pos)
1559 return pos - buf;
1560 pos += ret;
1561 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001562
1563 if (wpa_s->current_ssid) {
1564 struct wpa_cred *cred;
1565 char *type;
1566
1567 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001568 size_t i;
1569
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001570 if (wpa_s->current_ssid->parent_cred != cred)
1571 continue;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001572
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001573 for (i = 0; cred->domain && i < cred->num_domain; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001574 ret = os_snprintf(pos, end - pos,
1575 "home_sp=%s\n",
1576 cred->domain[i]);
1577 if (ret < 0 || ret >= end - pos)
1578 return pos - buf;
1579 pos += ret;
1580 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001581
1582 if (wpa_s->current_bss == NULL ||
1583 wpa_s->current_bss->anqp == NULL)
1584 res = -1;
1585 else
1586 res = interworking_home_sp_cred(
1587 wpa_s, cred,
1588 wpa_s->current_bss->anqp->domain_name);
1589 if (res > 0)
1590 type = "home";
1591 else if (res == 0)
1592 type = "roaming";
1593 else
1594 type = "unknown";
1595
1596 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
1597 if (ret < 0 || ret >= end - pos)
1598 return pos - buf;
1599 pos += ret;
1600
1601 break;
1602 }
1603 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001604#endif /* CONFIG_HS20 */
1605
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1607 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1608 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1609 verbose);
1610 if (res >= 0)
1611 pos += res;
1612 }
1613
1614 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1615 if (res >= 0)
1616 pos += res;
1617
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001618#ifdef ANDROID
1619 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
Irfan Sherifff20a4432012-04-16 16:48:34 -07001620 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001621 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
Irfan Sherifff20a4432012-04-16 16:48:34 -07001622 wpa_s->wpa_state,
Irfan Sheriffe78e7672012-08-01 11:10:15 -07001623 MAC2STR(wpa_s->bssid),
andy2_kuo5b5fb022012-05-22 11:53:07 -07001624 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1625 wpa_ssid_txt(wpa_s->current_ssid->ssid,
Irfan Sheriff10294772012-05-11 11:23:35 -07001626 wpa_s->current_ssid->ssid_len) : "");
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001627 if (wpa_s->wpa_state == WPA_COMPLETED) {
1628 struct wpa_ssid *ssid = wpa_s->current_ssid;
1629 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- connection to "
1630 MACSTR " completed %s [id=%d id_str=%s]",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001631 MAC2STR(wpa_s->bssid), "(auth)",
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001632 ssid ? ssid->id : -1,
1633 ssid && ssid->id_str ? ssid->id_str : "");
1634 }
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001635#endif /* ANDROID */
1636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001637 return pos - buf;
1638}
1639
1640
1641static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1642 char *cmd)
1643{
1644 char *pos;
1645 int id;
1646 struct wpa_ssid *ssid;
1647 u8 bssid[ETH_ALEN];
1648
1649 /* cmd: "<network id> <BSSID>" */
1650 pos = os_strchr(cmd, ' ');
1651 if (pos == NULL)
1652 return -1;
1653 *pos++ = '\0';
1654 id = atoi(cmd);
1655 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1656 if (hwaddr_aton(pos, bssid)) {
1657 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1658 return -1;
1659 }
1660
1661 ssid = wpa_config_get_network(wpa_s->conf, id);
1662 if (ssid == NULL) {
1663 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1664 "to update", id);
1665 return -1;
1666 }
1667
1668 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1669 ssid->bssid_set = !is_zero_ether_addr(bssid);
1670
1671 return 0;
1672}
1673
1674
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001675static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001676 char *cmd, char *buf,
1677 size_t buflen)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001678{
1679 u8 bssid[ETH_ALEN];
1680 struct wpa_blacklist *e;
1681 char *pos, *end;
1682 int ret;
1683
1684 /* cmd: "BLACKLIST [<BSSID>]" */
1685 if (*cmd == '\0') {
1686 pos = buf;
1687 end = buf + buflen;
1688 e = wpa_s->blacklist;
1689 while (e) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001690 ret = os_snprintf(pos, end - pos, MACSTR "\n",
1691 MAC2STR(e->bssid));
1692 if (ret < 0 || ret >= end - pos)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001693 return pos - buf;
1694 pos += ret;
1695 e = e->next;
1696 }
1697 return pos - buf;
1698 }
1699
1700 cmd++;
1701 if (os_strncmp(cmd, "clear", 5) == 0) {
1702 wpa_blacklist_clear(wpa_s);
1703 os_memcpy(buf, "OK\n", 3);
1704 return 3;
1705 }
1706
1707 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1708 if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001709 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001710 return -1;
1711 }
1712
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001713 /*
1714 * Add the BSSID twice, so its count will be 2, causing it to be
1715 * skipped when processing scan results.
1716 */
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001717 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001718 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001719 return -1;
1720 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001721 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001722 return -1;
1723 os_memcpy(buf, "OK\n", 3);
1724 return 3;
1725}
1726
1727
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001728extern int wpa_debug_level;
1729extern int wpa_debug_timestamp;
1730
1731static const char * debug_level_str(int level)
1732{
1733 switch (level) {
1734 case MSG_EXCESSIVE:
1735 return "EXCESSIVE";
1736 case MSG_MSGDUMP:
1737 return "MSGDUMP";
1738 case MSG_DEBUG:
1739 return "DEBUG";
1740 case MSG_INFO:
1741 return "INFO";
1742 case MSG_WARNING:
1743 return "WARNING";
1744 case MSG_ERROR:
1745 return "ERROR";
1746 default:
1747 return "?";
1748 }
1749}
1750
1751
1752static int str_to_debug_level(const char *s)
1753{
1754 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1755 return MSG_EXCESSIVE;
1756 if (os_strcasecmp(s, "MSGDUMP") == 0)
1757 return MSG_MSGDUMP;
1758 if (os_strcasecmp(s, "DEBUG") == 0)
1759 return MSG_DEBUG;
1760 if (os_strcasecmp(s, "INFO") == 0)
1761 return MSG_INFO;
1762 if (os_strcasecmp(s, "WARNING") == 0)
1763 return MSG_WARNING;
1764 if (os_strcasecmp(s, "ERROR") == 0)
1765 return MSG_ERROR;
1766 return -1;
1767}
1768
1769
1770static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1771 char *cmd, char *buf,
1772 size_t buflen)
1773{
1774 char *pos, *end, *stamp;
1775 int ret;
1776
1777 if (cmd == NULL) {
1778 return -1;
1779 }
1780
1781 /* cmd: "LOG_LEVEL [<level>]" */
1782 if (*cmd == '\0') {
1783 pos = buf;
1784 end = buf + buflen;
1785 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1786 "Timestamp: %d\n",
1787 debug_level_str(wpa_debug_level),
1788 wpa_debug_timestamp);
1789 if (ret < 0 || ret >= end - pos)
1790 ret = 0;
1791
1792 return ret;
1793 }
1794
1795 while (*cmd == ' ')
1796 cmd++;
1797
1798 stamp = os_strchr(cmd, ' ');
1799 if (stamp) {
1800 *stamp++ = '\0';
1801 while (*stamp == ' ') {
1802 stamp++;
1803 }
1804 }
1805
1806 if (cmd && os_strlen(cmd)) {
1807 int level = str_to_debug_level(cmd);
1808 if (level < 0)
1809 return -1;
1810 wpa_debug_level = level;
1811 }
1812
1813 if (stamp && os_strlen(stamp))
1814 wpa_debug_timestamp = atoi(stamp);
1815
1816 os_memcpy(buf, "OK\n", 3);
1817 return 3;
1818}
1819
1820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821static int wpa_supplicant_ctrl_iface_list_networks(
1822 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1823{
1824 char *pos, *end;
1825 struct wpa_ssid *ssid;
1826 int ret;
1827
1828 pos = buf;
1829 end = buf + buflen;
1830 ret = os_snprintf(pos, end - pos,
1831 "network id / ssid / bssid / flags\n");
1832 if (ret < 0 || ret >= end - pos)
1833 return pos - buf;
1834 pos += ret;
1835
1836 ssid = wpa_s->conf->ssid;
1837 while (ssid) {
1838 ret = os_snprintf(pos, end - pos, "%d\t%s",
1839 ssid->id,
1840 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1841 if (ret < 0 || ret >= end - pos)
1842 return pos - buf;
1843 pos += ret;
1844 if (ssid->bssid_set) {
1845 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
1846 MAC2STR(ssid->bssid));
1847 } else {
1848 ret = os_snprintf(pos, end - pos, "\tany");
1849 }
1850 if (ret < 0 || ret >= end - pos)
1851 return pos - buf;
1852 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001853 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854 ssid == wpa_s->current_ssid ?
1855 "[CURRENT]" : "",
1856 ssid->disabled ? "[DISABLED]" : "",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001857 ssid->disabled_until.sec ?
1858 "[TEMP-DISABLED]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001859 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
1860 "");
1861 if (ret < 0 || ret >= end - pos)
1862 return pos - buf;
1863 pos += ret;
1864 ret = os_snprintf(pos, end - pos, "\n");
1865 if (ret < 0 || ret >= end - pos)
1866 return pos - buf;
1867 pos += ret;
1868
1869 ssid = ssid->next;
1870 }
1871
1872 return pos - buf;
1873}
1874
1875
1876static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
1877{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001878 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001879 ret = os_snprintf(pos, end - pos, "-");
1880 if (ret < 0 || ret >= end - pos)
1881 return pos;
1882 pos += ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001883 ret = wpa_write_ciphers(pos, end, cipher, "+");
1884 if (ret < 0)
1885 return pos;
1886 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001887 return pos;
1888}
1889
1890
1891static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
1892 const u8 *ie, size_t ie_len)
1893{
1894 struct wpa_ie_data data;
1895 int first, ret;
1896
1897 ret = os_snprintf(pos, end - pos, "[%s-", proto);
1898 if (ret < 0 || ret >= end - pos)
1899 return pos;
1900 pos += ret;
1901
1902 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
1903 ret = os_snprintf(pos, end - pos, "?]");
1904 if (ret < 0 || ret >= end - pos)
1905 return pos;
1906 pos += ret;
1907 return pos;
1908 }
1909
1910 first = 1;
1911 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1912 ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
1913 if (ret < 0 || ret >= end - pos)
1914 return pos;
1915 pos += ret;
1916 first = 0;
1917 }
1918 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
1919 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
1920 if (ret < 0 || ret >= end - pos)
1921 return pos;
1922 pos += ret;
1923 first = 0;
1924 }
1925 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
1926 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
1927 if (ret < 0 || ret >= end - pos)
1928 return pos;
1929 pos += ret;
1930 first = 0;
1931 }
1932#ifdef CONFIG_IEEE80211R
1933 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1934 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
1935 first ? "" : "+");
1936 if (ret < 0 || ret >= end - pos)
1937 return pos;
1938 pos += ret;
1939 first = 0;
1940 }
1941 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1942 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
1943 first ? "" : "+");
1944 if (ret < 0 || ret >= end - pos)
1945 return pos;
1946 pos += ret;
1947 first = 0;
1948 }
1949#endif /* CONFIG_IEEE80211R */
1950#ifdef CONFIG_IEEE80211W
1951 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1952 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
1953 first ? "" : "+");
1954 if (ret < 0 || ret >= end - pos)
1955 return pos;
1956 pos += ret;
1957 first = 0;
1958 }
1959 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1960 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
1961 first ? "" : "+");
1962 if (ret < 0 || ret >= end - pos)
1963 return pos;
1964 pos += ret;
1965 first = 0;
1966 }
1967#endif /* CONFIG_IEEE80211W */
1968
1969 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
1970
1971 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
1972 ret = os_snprintf(pos, end - pos, "-preauth");
1973 if (ret < 0 || ret >= end - pos)
1974 return pos;
1975 pos += ret;
1976 }
1977
1978 ret = os_snprintf(pos, end - pos, "]");
1979 if (ret < 0 || ret >= end - pos)
1980 return pos;
1981 pos += ret;
1982
1983 return pos;
1984}
1985
1986
1987#ifdef CONFIG_WPS
1988static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
1989 char *pos, char *end,
1990 struct wpabuf *wps_ie)
1991{
1992 int ret;
1993 const char *txt;
1994
1995 if (wps_ie == NULL)
1996 return pos;
1997 if (wps_is_selected_pbc_registrar(wps_ie))
1998 txt = "[WPS-PBC]";
1999#ifdef CONFIG_WPS2
2000 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2001 txt = "[WPS-AUTH]";
2002#endif /* CONFIG_WPS2 */
2003 else if (wps_is_selected_pin_registrar(wps_ie))
2004 txt = "[WPS-PIN]";
2005 else
2006 txt = "[WPS]";
2007
2008 ret = os_snprintf(pos, end - pos, "%s", txt);
2009 if (ret >= 0 && ret < end - pos)
2010 pos += ret;
2011 wpabuf_free(wps_ie);
2012 return pos;
2013}
2014#endif /* CONFIG_WPS */
2015
2016
2017static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2018 char *pos, char *end,
2019 const struct wpa_bss *bss)
2020{
2021#ifdef CONFIG_WPS
2022 struct wpabuf *wps_ie;
2023 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2024 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2025#else /* CONFIG_WPS */
2026 return pos;
2027#endif /* CONFIG_WPS */
2028}
2029
2030
2031/* Format one result on one text line into a buffer. */
2032static int wpa_supplicant_ctrl_iface_scan_result(
2033 struct wpa_supplicant *wpa_s,
2034 const struct wpa_bss *bss, char *buf, size_t buflen)
2035{
2036 char *pos, *end;
2037 int ret;
2038 const u8 *ie, *ie2, *p2p;
2039
2040 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07002041 if (!p2p)
2042 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2044 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2045 0)
2046 return 0; /* Do not show P2P listen discovery results here */
2047
2048 pos = buf;
2049 end = buf + buflen;
2050
2051 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2052 MAC2STR(bss->bssid), bss->freq, bss->level);
2053 if (ret < 0 || ret >= end - pos)
2054 return -1;
2055 pos += ret;
2056 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2057 if (ie)
2058 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2059 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2060 if (ie2)
2061 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
2062 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2063 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2064 ret = os_snprintf(pos, end - pos, "[WEP]");
2065 if (ret < 0 || ret >= end - pos)
2066 return -1;
2067 pos += ret;
2068 }
2069 if (bss->caps & IEEE80211_CAP_IBSS) {
2070 ret = os_snprintf(pos, end - pos, "[IBSS]");
2071 if (ret < 0 || ret >= end - pos)
2072 return -1;
2073 pos += ret;
2074 }
2075 if (bss->caps & IEEE80211_CAP_ESS) {
2076 ret = os_snprintf(pos, end - pos, "[ESS]");
2077 if (ret < 0 || ret >= end - pos)
2078 return -1;
2079 pos += ret;
2080 }
2081 if (p2p) {
2082 ret = os_snprintf(pos, end - pos, "[P2P]");
2083 if (ret < 0 || ret >= end - pos)
2084 return -1;
2085 pos += ret;
2086 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002087#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002088 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002089 ret = os_snprintf(pos, end - pos, "[HS20]");
2090 if (ret < 0 || ret >= end - pos)
2091 return -1;
2092 pos += ret;
2093 }
2094#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095
2096 ret = os_snprintf(pos, end - pos, "\t%s",
2097 wpa_ssid_txt(bss->ssid, bss->ssid_len));
2098 if (ret < 0 || ret >= end - pos)
2099 return -1;
2100 pos += ret;
2101
2102 ret = os_snprintf(pos, end - pos, "\n");
2103 if (ret < 0 || ret >= end - pos)
2104 return -1;
2105 pos += ret;
2106
2107 return pos - buf;
2108}
2109
2110
2111static int wpa_supplicant_ctrl_iface_scan_results(
2112 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2113{
2114 char *pos, *end;
2115 struct wpa_bss *bss;
2116 int ret;
2117
2118 pos = buf;
2119 end = buf + buflen;
2120 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2121 "flags / ssid\n");
2122 if (ret < 0 || ret >= end - pos)
2123 return pos - buf;
2124 pos += ret;
2125
2126 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2127 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2128 end - pos);
2129 if (ret < 0 || ret >= end - pos)
2130 return pos - buf;
2131 pos += ret;
2132 }
2133
2134 return pos - buf;
2135}
2136
2137
2138static int wpa_supplicant_ctrl_iface_select_network(
2139 struct wpa_supplicant *wpa_s, char *cmd)
2140{
2141 int id;
2142 struct wpa_ssid *ssid;
2143
2144 /* cmd: "<network id>" or "any" */
2145 if (os_strcmp(cmd, "any") == 0) {
2146 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2147 ssid = NULL;
2148 } else {
2149 id = atoi(cmd);
2150 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2151
2152 ssid = wpa_config_get_network(wpa_s->conf, id);
2153 if (ssid == NULL) {
2154 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2155 "network id=%d", id);
2156 return -1;
2157 }
2158 if (ssid->disabled == 2) {
2159 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2160 "SELECT_NETWORK with persistent P2P group");
2161 return -1;
2162 }
2163 }
2164
2165 wpa_supplicant_select_network(wpa_s, ssid);
2166
2167 return 0;
2168}
2169
2170
2171static int wpa_supplicant_ctrl_iface_enable_network(
2172 struct wpa_supplicant *wpa_s, char *cmd)
2173{
2174 int id;
2175 struct wpa_ssid *ssid;
2176
2177 /* cmd: "<network id>" or "all" */
2178 if (os_strcmp(cmd, "all") == 0) {
2179 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2180 ssid = NULL;
2181 } else {
2182 id = atoi(cmd);
2183 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2184
2185 ssid = wpa_config_get_network(wpa_s->conf, id);
2186 if (ssid == NULL) {
2187 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2188 "network id=%d", id);
2189 return -1;
2190 }
2191 if (ssid->disabled == 2) {
2192 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2193 "ENABLE_NETWORK with persistent P2P group");
2194 return -1;
2195 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002196
2197 if (os_strstr(cmd, " no-connect")) {
2198 ssid->disabled = 0;
2199 return 0;
2200 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201 }
2202 wpa_supplicant_enable_network(wpa_s, ssid);
2203
2204 return 0;
2205}
2206
2207
2208static int wpa_supplicant_ctrl_iface_disable_network(
2209 struct wpa_supplicant *wpa_s, char *cmd)
2210{
2211 int id;
2212 struct wpa_ssid *ssid;
2213
2214 /* cmd: "<network id>" or "all" */
2215 if (os_strcmp(cmd, "all") == 0) {
2216 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2217 ssid = NULL;
2218 } else {
2219 id = atoi(cmd);
2220 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2221
2222 ssid = wpa_config_get_network(wpa_s->conf, id);
2223 if (ssid == NULL) {
2224 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2225 "network id=%d", id);
2226 return -1;
2227 }
2228 if (ssid->disabled == 2) {
2229 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2230 "DISABLE_NETWORK with persistent P2P "
2231 "group");
2232 return -1;
2233 }
2234 }
2235 wpa_supplicant_disable_network(wpa_s, ssid);
2236
2237 return 0;
2238}
2239
2240
2241static int wpa_supplicant_ctrl_iface_add_network(
2242 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2243{
2244 struct wpa_ssid *ssid;
2245 int ret;
2246
2247 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2248
2249 ssid = wpa_config_add_network(wpa_s->conf);
2250 if (ssid == NULL)
2251 return -1;
2252
2253 wpas_notify_network_added(wpa_s, ssid);
2254
2255 ssid->disabled = 1;
2256 wpa_config_set_network_defaults(ssid);
2257
2258 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
2259 if (ret < 0 || (size_t) ret >= buflen)
2260 return -1;
2261 return ret;
2262}
2263
2264
2265static int wpa_supplicant_ctrl_iface_remove_network(
2266 struct wpa_supplicant *wpa_s, char *cmd)
2267{
2268 int id;
2269 struct wpa_ssid *ssid;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002270 int was_disabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002271
2272 /* cmd: "<network id>" or "all" */
2273 if (os_strcmp(cmd, "all") == 0) {
2274 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002275 if (wpa_s->sched_scanning)
2276 wpa_supplicant_cancel_sched_scan(wpa_s);
2277
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002278 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002279 if (wpa_s->current_ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002280#ifdef CONFIG_SME
2281 wpa_s->sme.prev_bssid_set = 0;
2282#endif /* CONFIG_SME */
Jouni Malinen75ecf522011-06-27 15:19:46 -07002283 wpa_sm_set_config(wpa_s->wpa, NULL);
2284 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002285 wpa_supplicant_deauthenticate(
2286 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002287 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002288 ssid = wpa_s->conf->ssid;
2289 while (ssid) {
2290 struct wpa_ssid *remove_ssid = ssid;
2291 id = ssid->id;
2292 ssid = ssid->next;
2293 wpas_notify_network_removed(wpa_s, remove_ssid);
2294 wpa_config_remove_network(wpa_s->conf, id);
2295 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002296 return 0;
2297 }
2298
2299 id = atoi(cmd);
2300 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2301
2302 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002303 if (ssid)
2304 wpas_notify_network_removed(wpa_s, ssid);
Deepthi Gowria831d782012-09-03 11:55:38 +03002305 if (ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002306 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2307 "id=%d", id);
2308 return -1;
2309 }
2310
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002311 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002312#ifdef CONFIG_SME
2313 wpa_s->sme.prev_bssid_set = 0;
2314#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002316 * Invalidate the EAP session cache if the current or
2317 * previously used network is removed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318 */
2319 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002320 }
2321
2322 if (ssid == wpa_s->current_ssid) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002323 wpa_sm_set_config(wpa_s->wpa, NULL);
2324 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002326 wpa_supplicant_deauthenticate(wpa_s,
2327 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002328 }
2329
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002330 was_disabled = ssid->disabled;
2331
Deepthi Gowria831d782012-09-03 11:55:38 +03002332 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2333 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2334 "network id=%d", id);
2335 return -1;
2336 }
2337
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002338 if (!was_disabled && wpa_s->sched_scanning) {
2339 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2340 "network from filters");
2341 wpa_supplicant_cancel_sched_scan(wpa_s);
2342 wpa_supplicant_req_scan(wpa_s, 0, 0);
2343 }
2344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345 return 0;
2346}
2347
2348
2349static int wpa_supplicant_ctrl_iface_set_network(
2350 struct wpa_supplicant *wpa_s, char *cmd)
2351{
2352 int id;
2353 struct wpa_ssid *ssid;
2354 char *name, *value;
2355
2356 /* cmd: "<network id> <variable name> <value>" */
2357 name = os_strchr(cmd, ' ');
2358 if (name == NULL)
2359 return -1;
2360 *name++ = '\0';
2361
2362 value = os_strchr(name, ' ');
2363 if (value == NULL)
2364 return -1;
2365 *value++ = '\0';
2366
2367 id = atoi(cmd);
2368 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2369 id, name);
2370 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2371 (u8 *) value, os_strlen(value));
2372
2373 ssid = wpa_config_get_network(wpa_s->conf, id);
2374 if (ssid == NULL) {
2375 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2376 "id=%d", id);
2377 return -1;
2378 }
2379
2380 if (wpa_config_set(ssid, name, value, 0) < 0) {
2381 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2382 "variable '%s'", name);
2383 return -1;
2384 }
2385
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002386 if (os_strcmp(name, "bssid") != 0 &&
2387 os_strcmp(name, "priority") != 0)
2388 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002389
2390 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002391 /*
2392 * Invalidate the EAP session cache if anything in the current
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002393 * or previously used configuration changes.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002394 */
2395 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2396 }
2397
2398 if ((os_strcmp(name, "psk") == 0 &&
2399 value[0] == '"' && ssid->ssid_len) ||
2400 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2401 wpa_config_update_psk(ssid);
2402 else if (os_strcmp(name, "priority") == 0)
2403 wpa_config_update_prio_list(wpa_s->conf);
2404
2405 return 0;
2406}
2407
2408
2409static int wpa_supplicant_ctrl_iface_get_network(
2410 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2411{
2412 int id;
2413 size_t res;
2414 struct wpa_ssid *ssid;
2415 char *name, *value;
2416
2417 /* cmd: "<network id> <variable name>" */
2418 name = os_strchr(cmd, ' ');
2419 if (name == NULL || buflen == 0)
2420 return -1;
2421 *name++ = '\0';
2422
2423 id = atoi(cmd);
2424 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2425 id, name);
2426
2427 ssid = wpa_config_get_network(wpa_s->conf, id);
2428 if (ssid == NULL) {
2429 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2430 "id=%d", id);
2431 return -1;
2432 }
2433
2434 value = wpa_config_get_no_key(ssid, name);
2435 if (value == NULL) {
2436 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2437 "variable '%s'", name);
2438 return -1;
2439 }
2440
2441 res = os_strlcpy(buf, value, buflen);
2442 if (res >= buflen) {
2443 os_free(value);
2444 return -1;
2445 }
2446
2447 os_free(value);
2448
2449 return res;
2450}
2451
2452
Dmitry Shmidt04949592012-07-19 12:16:46 -07002453static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2454 char *buf, size_t buflen)
2455{
2456 char *pos, *end;
2457 struct wpa_cred *cred;
2458 int ret;
2459
2460 pos = buf;
2461 end = buf + buflen;
2462 ret = os_snprintf(pos, end - pos,
2463 "cred id / realm / username / domain / imsi\n");
2464 if (ret < 0 || ret >= end - pos)
2465 return pos - buf;
2466 pos += ret;
2467
2468 cred = wpa_s->conf->cred;
2469 while (cred) {
2470 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2471 cred->id, cred->realm ? cred->realm : "",
2472 cred->username ? cred->username : "",
Dmitry Shmidt051af732013-10-22 13:52:46 -07002473 cred->domain ? cred->domain[0] : "",
Dmitry Shmidt04949592012-07-19 12:16:46 -07002474 cred->imsi ? cred->imsi : "");
2475 if (ret < 0 || ret >= end - pos)
2476 return pos - buf;
2477 pos += ret;
2478
2479 cred = cred->next;
2480 }
2481
2482 return pos - buf;
2483}
2484
2485
2486static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2487 char *buf, size_t buflen)
2488{
2489 struct wpa_cred *cred;
2490 int ret;
2491
2492 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2493
2494 cred = wpa_config_add_cred(wpa_s->conf);
2495 if (cred == NULL)
2496 return -1;
2497
2498 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2499 if (ret < 0 || (size_t) ret >= buflen)
2500 return -1;
2501 return ret;
2502}
2503
2504
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002505static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2506 struct wpa_cred *cred)
2507{
2508 struct wpa_ssid *ssid;
2509 char str[20];
2510
2511 if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
2512 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2513 return -1;
2514 }
2515
2516 /* Remove any network entry created based on the removed credential */
2517 ssid = wpa_s->conf->ssid;
2518 while (ssid) {
2519 if (ssid->parent_cred == cred) {
2520 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
2521 "used the removed credential", ssid->id);
2522 os_snprintf(str, sizeof(str), "%d", ssid->id);
2523 ssid = ssid->next;
2524 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
2525 } else
2526 ssid = ssid->next;
2527 }
2528
2529 return 0;
2530}
2531
2532
Dmitry Shmidt04949592012-07-19 12:16:46 -07002533static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2534 char *cmd)
2535{
2536 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002537 struct wpa_cred *cred, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002538
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002539 /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002540 if (os_strcmp(cmd, "all") == 0) {
2541 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2542 cred = wpa_s->conf->cred;
2543 while (cred) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002544 prev = cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002545 cred = cred->next;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002546 wpas_ctrl_remove_cred(wpa_s, prev);
2547 }
2548 return 0;
2549 }
2550
2551 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
2552 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
2553 cmd + 8);
2554 cred = wpa_s->conf->cred;
2555 while (cred) {
2556 prev = cred;
2557 cred = cred->next;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002558 if (prev->domain) {
2559 size_t i;
2560 for (i = 0; i < prev->num_domain; i++) {
2561 if (os_strcmp(prev->domain[i], cmd + 8)
2562 != 0)
2563 continue;
2564 wpas_ctrl_remove_cred(wpa_s, prev);
2565 break;
2566 }
2567 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002568 }
2569 return 0;
2570 }
2571
2572 id = atoi(cmd);
2573 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2574
2575 cred = wpa_config_get_cred(wpa_s->conf, id);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002576 return wpas_ctrl_remove_cred(wpa_s, cred);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002577}
2578
2579
2580static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2581 char *cmd)
2582{
2583 int id;
2584 struct wpa_cred *cred;
2585 char *name, *value;
2586
2587 /* cmd: "<cred id> <variable name> <value>" */
2588 name = os_strchr(cmd, ' ');
2589 if (name == NULL)
2590 return -1;
2591 *name++ = '\0';
2592
2593 value = os_strchr(name, ' ');
2594 if (value == NULL)
2595 return -1;
2596 *value++ = '\0';
2597
2598 id = atoi(cmd);
2599 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2600 id, name);
2601 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2602 (u8 *) value, os_strlen(value));
2603
2604 cred = wpa_config_get_cred(wpa_s->conf, id);
2605 if (cred == NULL) {
2606 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2607 id);
2608 return -1;
2609 }
2610
2611 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2612 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2613 "variable '%s'", name);
2614 return -1;
2615 }
2616
2617 return 0;
2618}
2619
2620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002621#ifndef CONFIG_NO_CONFIG_WRITE
2622static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2623{
2624 int ret;
2625
2626 if (!wpa_s->conf->update_config) {
2627 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2628 "to update configuration (update_config=0)");
2629 return -1;
2630 }
2631
2632 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2633 if (ret) {
2634 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2635 "update configuration");
2636 } else {
2637 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2638 " updated");
2639 }
2640
2641 return ret;
2642}
2643#endif /* CONFIG_NO_CONFIG_WRITE */
2644
2645
2646static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2647 struct wpa_driver_capa *capa,
2648 char *buf, size_t buflen)
2649{
2650 int ret, first = 1;
2651 char *pos, *end;
2652 size_t len;
2653
2654 pos = buf;
2655 end = pos + buflen;
2656
2657 if (res < 0) {
2658 if (strict)
2659 return 0;
2660 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2661 if (len >= buflen)
2662 return -1;
2663 return len;
2664 }
2665
2666 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2667 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2668 if (ret < 0 || ret >= end - pos)
2669 return pos - buf;
2670 pos += ret;
2671 first = 0;
2672 }
2673
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002674 if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2675 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
2676 if (ret < 0 || ret >= end - pos)
2677 return pos - buf;
2678 pos += ret;
2679 first = 0;
2680 }
2681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002682 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2683 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2684 if (ret < 0 || ret >= end - pos)
2685 return pos - buf;
2686 pos += ret;
2687 first = 0;
2688 }
2689
2690 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2691 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
2692 if (ret < 0 || ret >= end - pos)
2693 return pos - buf;
2694 pos += ret;
2695 first = 0;
2696 }
2697
2698 return pos - buf;
2699}
2700
2701
2702static int ctrl_iface_get_capability_group(int res, char *strict,
2703 struct wpa_driver_capa *capa,
2704 char *buf, size_t buflen)
2705{
2706 int ret, first = 1;
2707 char *pos, *end;
2708 size_t len;
2709
2710 pos = buf;
2711 end = pos + buflen;
2712
2713 if (res < 0) {
2714 if (strict)
2715 return 0;
2716 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
2717 if (len >= buflen)
2718 return -1;
2719 return len;
2720 }
2721
2722 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2723 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2724 if (ret < 0 || ret >= end - pos)
2725 return pos - buf;
2726 pos += ret;
2727 first = 0;
2728 }
2729
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002730 if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2731 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
2732 if (ret < 0 || ret >= end - pos)
2733 return pos - buf;
2734 pos += ret;
2735 first = 0;
2736 }
2737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002738 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2739 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2740 if (ret < 0 || ret >= end - pos)
2741 return pos - buf;
2742 pos += ret;
2743 first = 0;
2744 }
2745
2746 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
2747 ret = os_snprintf(pos, end - pos, "%sWEP104",
2748 first ? "" : " ");
2749 if (ret < 0 || ret >= end - pos)
2750 return pos - buf;
2751 pos += ret;
2752 first = 0;
2753 }
2754
2755 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
2756 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
2757 if (ret < 0 || ret >= end - pos)
2758 return pos - buf;
2759 pos += ret;
2760 first = 0;
2761 }
2762
2763 return pos - buf;
2764}
2765
2766
2767static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
2768 struct wpa_driver_capa *capa,
2769 char *buf, size_t buflen)
2770{
2771 int ret;
2772 char *pos, *end;
2773 size_t len;
2774
2775 pos = buf;
2776 end = pos + buflen;
2777
2778 if (res < 0) {
2779 if (strict)
2780 return 0;
2781 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
2782 "NONE", buflen);
2783 if (len >= buflen)
2784 return -1;
2785 return len;
2786 }
2787
2788 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
2789 if (ret < 0 || ret >= end - pos)
2790 return pos - buf;
2791 pos += ret;
2792
2793 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2794 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
2795 ret = os_snprintf(pos, end - pos, " WPA-EAP");
2796 if (ret < 0 || ret >= end - pos)
2797 return pos - buf;
2798 pos += ret;
2799 }
2800
2801 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2802 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2803 ret = os_snprintf(pos, end - pos, " WPA-PSK");
2804 if (ret < 0 || ret >= end - pos)
2805 return pos - buf;
2806 pos += ret;
2807 }
2808
2809 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2810 ret = os_snprintf(pos, end - pos, " WPA-NONE");
2811 if (ret < 0 || ret >= end - pos)
2812 return pos - buf;
2813 pos += ret;
2814 }
2815
2816 return pos - buf;
2817}
2818
2819
2820static int ctrl_iface_get_capability_proto(int res, char *strict,
2821 struct wpa_driver_capa *capa,
2822 char *buf, size_t buflen)
2823{
2824 int ret, first = 1;
2825 char *pos, *end;
2826 size_t len;
2827
2828 pos = buf;
2829 end = pos + buflen;
2830
2831 if (res < 0) {
2832 if (strict)
2833 return 0;
2834 len = os_strlcpy(buf, "RSN WPA", buflen);
2835 if (len >= buflen)
2836 return -1;
2837 return len;
2838 }
2839
2840 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2841 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2842 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
2843 if (ret < 0 || ret >= end - pos)
2844 return pos - buf;
2845 pos += ret;
2846 first = 0;
2847 }
2848
2849 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2850 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
2851 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
2852 if (ret < 0 || ret >= end - pos)
2853 return pos - buf;
2854 pos += ret;
2855 first = 0;
2856 }
2857
2858 return pos - buf;
2859}
2860
2861
2862static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
2863 struct wpa_driver_capa *capa,
2864 char *buf, size_t buflen)
2865{
2866 int ret, first = 1;
2867 char *pos, *end;
2868 size_t len;
2869
2870 pos = buf;
2871 end = pos + buflen;
2872
2873 if (res < 0) {
2874 if (strict)
2875 return 0;
2876 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
2877 if (len >= buflen)
2878 return -1;
2879 return len;
2880 }
2881
2882 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
2883 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
2884 if (ret < 0 || ret >= end - pos)
2885 return pos - buf;
2886 pos += ret;
2887 first = 0;
2888 }
2889
2890 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
2891 ret = os_snprintf(pos, end - pos, "%sSHARED",
2892 first ? "" : " ");
2893 if (ret < 0 || ret >= end - pos)
2894 return pos - buf;
2895 pos += ret;
2896 first = 0;
2897 }
2898
2899 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
2900 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
2901 if (ret < 0 || ret >= end - pos)
2902 return pos - buf;
2903 pos += ret;
2904 first = 0;
2905 }
2906
2907 return pos - buf;
2908}
2909
2910
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002911static int ctrl_iface_get_capability_modes(int res, char *strict,
2912 struct wpa_driver_capa *capa,
2913 char *buf, size_t buflen)
2914{
2915 int ret, first = 1;
2916 char *pos, *end;
2917 size_t len;
2918
2919 pos = buf;
2920 end = pos + buflen;
2921
2922 if (res < 0) {
2923 if (strict)
2924 return 0;
2925 len = os_strlcpy(buf, "IBSS AP", buflen);
2926 if (len >= buflen)
2927 return -1;
2928 return len;
2929 }
2930
2931 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
2932 ret = os_snprintf(pos, end - pos, "%sIBSS", first ? "" : " ");
2933 if (ret < 0 || ret >= end - pos)
2934 return pos - buf;
2935 pos += ret;
2936 first = 0;
2937 }
2938
2939 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
2940 ret = os_snprintf(pos, end - pos, "%sAP", first ? "" : " ");
2941 if (ret < 0 || ret >= end - pos)
2942 return pos - buf;
2943 pos += ret;
2944 first = 0;
2945 }
2946
2947 return pos - buf;
2948}
2949
2950
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002951static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
2952 char *buf, size_t buflen)
2953{
2954 struct hostapd_channel_data *chnl;
2955 int ret, i, j;
2956 char *pos, *end, *hmode;
2957
2958 pos = buf;
2959 end = pos + buflen;
2960
2961 for (j = 0; j < wpa_s->hw.num_modes; j++) {
2962 switch (wpa_s->hw.modes[j].mode) {
2963 case HOSTAPD_MODE_IEEE80211B:
2964 hmode = "B";
2965 break;
2966 case HOSTAPD_MODE_IEEE80211G:
2967 hmode = "G";
2968 break;
2969 case HOSTAPD_MODE_IEEE80211A:
2970 hmode = "A";
2971 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002972 case HOSTAPD_MODE_IEEE80211AD:
2973 hmode = "AD";
2974 break;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002975 default:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002976 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002977 }
2978 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
2979 if (ret < 0 || ret >= end - pos)
2980 return pos - buf;
2981 pos += ret;
2982 chnl = wpa_s->hw.modes[j].channels;
2983 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002984 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
2985 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002986 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
2987 if (ret < 0 || ret >= end - pos)
2988 return pos - buf;
2989 pos += ret;
2990 }
2991 ret = os_snprintf(pos, end - pos, "\n");
2992 if (ret < 0 || ret >= end - pos)
2993 return pos - buf;
2994 pos += ret;
2995 }
2996
2997 return pos - buf;
2998}
2999
3000
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003001static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3002 char *buf, size_t buflen)
3003{
3004 struct hostapd_channel_data *chnl;
3005 int ret, i, j;
3006 char *pos, *end, *hmode;
3007
3008 pos = buf;
3009 end = pos + buflen;
3010
3011 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3012 switch (wpa_s->hw.modes[j].mode) {
3013 case HOSTAPD_MODE_IEEE80211B:
3014 hmode = "B";
3015 break;
3016 case HOSTAPD_MODE_IEEE80211G:
3017 hmode = "G";
3018 break;
3019 case HOSTAPD_MODE_IEEE80211A:
3020 hmode = "A";
3021 break;
3022 case HOSTAPD_MODE_IEEE80211AD:
3023 hmode = "AD";
3024 break;
3025 default:
3026 continue;
3027 }
3028 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3029 hmode);
3030 if (ret < 0 || ret >= end - pos)
3031 return pos - buf;
3032 pos += ret;
3033 chnl = wpa_s->hw.modes[j].channels;
3034 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3035 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3036 continue;
3037 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s\n",
3038 chnl[i].chan, chnl[i].freq,
3039 chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
3040 " (NO_IBSS)" : "");
3041 if (ret < 0 || ret >= end - pos)
3042 return pos - buf;
3043 pos += ret;
3044 }
3045 ret = os_snprintf(pos, end - pos, "\n");
3046 if (ret < 0 || ret >= end - pos)
3047 return pos - buf;
3048 pos += ret;
3049 }
3050
3051 return pos - buf;
3052}
3053
3054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055static int wpa_supplicant_ctrl_iface_get_capability(
3056 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3057 size_t buflen)
3058{
3059 struct wpa_driver_capa capa;
3060 int res;
3061 char *strict;
3062 char field[30];
3063 size_t len;
3064
3065 /* Determine whether or not strict checking was requested */
3066 len = os_strlcpy(field, _field, sizeof(field));
3067 if (len >= sizeof(field))
3068 return -1;
3069 strict = os_strchr(field, ' ');
3070 if (strict != NULL) {
3071 *strict++ = '\0';
3072 if (os_strcmp(strict, "strict") != 0)
3073 return -1;
3074 }
3075
3076 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3077 field, strict ? strict : "");
3078
3079 if (os_strcmp(field, "eap") == 0) {
3080 return eap_get_names(buf, buflen);
3081 }
3082
3083 res = wpa_drv_get_capa(wpa_s, &capa);
3084
3085 if (os_strcmp(field, "pairwise") == 0)
3086 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3087 buf, buflen);
3088
3089 if (os_strcmp(field, "group") == 0)
3090 return ctrl_iface_get_capability_group(res, strict, &capa,
3091 buf, buflen);
3092
3093 if (os_strcmp(field, "key_mgmt") == 0)
3094 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3095 buf, buflen);
3096
3097 if (os_strcmp(field, "proto") == 0)
3098 return ctrl_iface_get_capability_proto(res, strict, &capa,
3099 buf, buflen);
3100
3101 if (os_strcmp(field, "auth_alg") == 0)
3102 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
3103 buf, buflen);
3104
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003105 if (os_strcmp(field, "modes") == 0)
3106 return ctrl_iface_get_capability_modes(res, strict, &capa,
3107 buf, buflen);
3108
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003109 if (os_strcmp(field, "channels") == 0)
3110 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3111
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003112 if (os_strcmp(field, "freq") == 0)
3113 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003115 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3116 field);
3117
3118 return -1;
3119}
3120
3121
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003122#ifdef CONFIG_INTERWORKING
3123static char * anqp_add_hex(char *pos, char *end, const char *title,
3124 struct wpabuf *data)
3125{
3126 char *start = pos;
3127 size_t i;
3128 int ret;
3129 const u8 *d;
3130
3131 if (data == NULL)
3132 return start;
3133
3134 ret = os_snprintf(pos, end - pos, "%s=", title);
3135 if (ret < 0 || ret >= end - pos)
3136 return start;
3137 pos += ret;
3138
3139 d = wpabuf_head_u8(data);
3140 for (i = 0; i < wpabuf_len(data); i++) {
3141 ret = os_snprintf(pos, end - pos, "%02x", *d++);
3142 if (ret < 0 || ret >= end - pos)
3143 return start;
3144 pos += ret;
3145 }
3146
3147 ret = os_snprintf(pos, end - pos, "\n");
3148 if (ret < 0 || ret >= end - pos)
3149 return start;
3150 pos += ret;
3151
3152 return pos;
3153}
3154#endif /* CONFIG_INTERWORKING */
3155
3156
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003157static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3158 unsigned long mask, char *buf, size_t buflen)
3159{
3160 size_t i;
3161 int ret;
3162 char *pos, *end;
3163 const u8 *ie, *ie2;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003164
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003165 pos = buf;
3166 end = buf + buflen;
3167
3168 if (mask & WPA_BSS_MASK_ID) {
3169 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
3170 if (ret < 0 || ret >= end - pos)
3171 return 0;
3172 pos += ret;
3173 }
3174
3175 if (mask & WPA_BSS_MASK_BSSID) {
3176 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3177 MAC2STR(bss->bssid));
3178 if (ret < 0 || ret >= end - pos)
3179 return 0;
3180 pos += ret;
3181 }
3182
3183 if (mask & WPA_BSS_MASK_FREQ) {
3184 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
3185 if (ret < 0 || ret >= end - pos)
3186 return 0;
3187 pos += ret;
3188 }
3189
3190 if (mask & WPA_BSS_MASK_BEACON_INT) {
3191 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3192 bss->beacon_int);
3193 if (ret < 0 || ret >= end - pos)
3194 return 0;
3195 pos += ret;
3196 }
3197
3198 if (mask & WPA_BSS_MASK_CAPABILITIES) {
3199 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3200 bss->caps);
3201 if (ret < 0 || ret >= end - pos)
3202 return 0;
3203 pos += ret;
3204 }
3205
3206 if (mask & WPA_BSS_MASK_QUAL) {
3207 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
3208 if (ret < 0 || ret >= end - pos)
3209 return 0;
3210 pos += ret;
3211 }
3212
3213 if (mask & WPA_BSS_MASK_NOISE) {
3214 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
3215 if (ret < 0 || ret >= end - pos)
3216 return 0;
3217 pos += ret;
3218 }
3219
3220 if (mask & WPA_BSS_MASK_LEVEL) {
3221 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
3222 if (ret < 0 || ret >= end - pos)
3223 return 0;
3224 pos += ret;
3225 }
3226
3227 if (mask & WPA_BSS_MASK_TSF) {
3228 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3229 (unsigned long long) bss->tsf);
3230 if (ret < 0 || ret >= end - pos)
3231 return 0;
3232 pos += ret;
3233 }
3234
3235 if (mask & WPA_BSS_MASK_AGE) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003236 struct os_time now;
3237
3238 os_get_time(&now);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003239 ret = os_snprintf(pos, end - pos, "age=%d\n",
3240 (int) (now.sec - bss->last_update.sec));
3241 if (ret < 0 || ret >= end - pos)
3242 return 0;
3243 pos += ret;
3244 }
3245
3246 if (mask & WPA_BSS_MASK_IE) {
3247 ret = os_snprintf(pos, end - pos, "ie=");
3248 if (ret < 0 || ret >= end - pos)
3249 return 0;
3250 pos += ret;
3251
3252 ie = (const u8 *) (bss + 1);
3253 for (i = 0; i < bss->ie_len; i++) {
3254 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
3255 if (ret < 0 || ret >= end - pos)
3256 return 0;
3257 pos += ret;
3258 }
3259
3260 ret = os_snprintf(pos, end - pos, "\n");
3261 if (ret < 0 || ret >= end - pos)
3262 return 0;
3263 pos += ret;
3264 }
3265
3266 if (mask & WPA_BSS_MASK_FLAGS) {
3267 ret = os_snprintf(pos, end - pos, "flags=");
3268 if (ret < 0 || ret >= end - pos)
3269 return 0;
3270 pos += ret;
3271
3272 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3273 if (ie)
3274 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3275 2 + ie[1]);
3276 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3277 if (ie2)
3278 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3279 2 + ie2[1]);
3280 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3281 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3282 ret = os_snprintf(pos, end - pos, "[WEP]");
3283 if (ret < 0 || ret >= end - pos)
3284 return 0;
3285 pos += ret;
3286 }
3287 if (bss->caps & IEEE80211_CAP_IBSS) {
3288 ret = os_snprintf(pos, end - pos, "[IBSS]");
3289 if (ret < 0 || ret >= end - pos)
3290 return 0;
3291 pos += ret;
3292 }
3293 if (bss->caps & IEEE80211_CAP_ESS) {
3294 ret = os_snprintf(pos, end - pos, "[ESS]");
3295 if (ret < 0 || ret >= end - pos)
3296 return 0;
3297 pos += ret;
3298 }
Dmitry Shmidt96571392013-10-14 12:54:46 -07003299 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
3300 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003301 ret = os_snprintf(pos, end - pos, "[P2P]");
3302 if (ret < 0 || ret >= end - pos)
3303 return 0;
3304 pos += ret;
3305 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003306#ifdef CONFIG_HS20
3307 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3308 ret = os_snprintf(pos, end - pos, "[HS20]");
3309 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003310 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003311 pos += ret;
3312 }
3313#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003314
3315 ret = os_snprintf(pos, end - pos, "\n");
3316 if (ret < 0 || ret >= end - pos)
3317 return 0;
3318 pos += ret;
3319 }
3320
3321 if (mask & WPA_BSS_MASK_SSID) {
3322 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3323 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3324 if (ret < 0 || ret >= end - pos)
3325 return 0;
3326 pos += ret;
3327 }
3328
3329#ifdef CONFIG_WPS
3330 if (mask & WPA_BSS_MASK_WPS_SCAN) {
3331 ie = (const u8 *) (bss + 1);
3332 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3333 if (ret < 0 || ret >= end - pos)
3334 return 0;
3335 pos += ret;
3336 }
3337#endif /* CONFIG_WPS */
3338
3339#ifdef CONFIG_P2P
3340 if (mask & WPA_BSS_MASK_P2P_SCAN) {
3341 ie = (const u8 *) (bss + 1);
3342 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3343 if (ret < 0 || ret >= end - pos)
3344 return 0;
3345 pos += ret;
3346 }
3347#endif /* CONFIG_P2P */
3348
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003349#ifdef CONFIG_WIFI_DISPLAY
3350 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3351 struct wpabuf *wfd;
3352 ie = (const u8 *) (bss + 1);
3353 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3354 WFD_IE_VENDOR_TYPE);
3355 if (wfd) {
3356 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
3357 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003358 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003359 pos += ret;
3360
3361 pos += wpa_snprintf_hex(pos, end - pos,
3362 wpabuf_head(wfd),
3363 wpabuf_len(wfd));
3364 wpabuf_free(wfd);
3365
3366 ret = os_snprintf(pos, end - pos, "\n");
3367 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003368 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003369 pos += ret;
3370 }
3371 }
3372#endif /* CONFIG_WIFI_DISPLAY */
3373
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003374#ifdef CONFIG_INTERWORKING
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003375 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3376 struct wpa_bss_anqp *anqp = bss->anqp;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003377 pos = anqp_add_hex(pos, end, "anqp_venue_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003378 anqp->venue_name);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003379 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003380 anqp->network_auth_type);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003381 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003382 anqp->roaming_consortium);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003383 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003384 anqp->ip_addr_type_availability);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003385 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003386 anqp->nai_realm);
3387 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003388 pos = anqp_add_hex(pos, end, "anqp_domain_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003389 anqp->domain_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003390#ifdef CONFIG_HS20
3391 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003392 anqp->hs20_operator_friendly_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003393 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003394 anqp->hs20_wan_metrics);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003395 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003396 anqp->hs20_connection_capability);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003397#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003398 }
3399#endif /* CONFIG_INTERWORKING */
3400
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003401 if (mask & WPA_BSS_MASK_DELIM) {
3402 ret = os_snprintf(pos, end - pos, "====\n");
3403 if (ret < 0 || ret >= end - pos)
3404 return 0;
3405 pos += ret;
3406 }
Irfan Sheriffe2ea0082012-08-13 10:56:16 -07003407
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003408 return pos - buf;
3409}
3410
Dmitry Shmidt04949592012-07-19 12:16:46 -07003411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003412static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
3413 const char *cmd, char *buf,
3414 size_t buflen)
3415{
3416 u8 bssid[ETH_ALEN];
3417 size_t i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003418 struct wpa_bss *bss;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003419 struct wpa_bss *bsslast = NULL;
3420 struct dl_list *next;
3421 int ret = 0;
3422 int len;
3423 char *ctmp;
3424 unsigned long mask = WPA_BSS_MASK_ALL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003425
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003426 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
3427 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
3428 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003429 list_id);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003430 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
3431 list_id);
3432 } else { /* N1-N2 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003433 unsigned int id1, id2;
3434
3435 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
3436 wpa_printf(MSG_INFO, "Wrong BSS range "
3437 "format");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003438 return 0;
3439 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003440
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003441 if (*(cmd + 6) == '-')
3442 id1 = 0;
3443 else
3444 id1 = atoi(cmd + 6);
3445 ctmp++;
3446 if (*ctmp >= '0' && *ctmp <= '9')
3447 id2 = atoi(ctmp);
3448 else
3449 id2 = (unsigned int) -1;
3450 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
3451 if (id2 == (unsigned int) -1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003452 bsslast = dl_list_last(&wpa_s->bss_id,
3453 struct wpa_bss,
3454 list_id);
3455 else {
3456 bsslast = wpa_bss_get_id(wpa_s, id2);
3457 if (bsslast == NULL && bss && id2 > id1) {
3458 struct wpa_bss *tmp = bss;
3459 for (;;) {
3460 next = tmp->list_id.next;
3461 if (next == &wpa_s->bss_id)
3462 break;
3463 tmp = dl_list_entry(
3464 next, struct wpa_bss,
3465 list_id);
3466 if (tmp->id > id2)
3467 break;
3468 bsslast = tmp;
3469 }
3470 }
3471 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003472 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003473 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003474 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003475 else if (os_strncmp(cmd, "LAST", 4) == 0)
3476 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003477 else if (os_strncmp(cmd, "ID-", 3) == 0) {
3478 i = atoi(cmd + 3);
3479 bss = wpa_bss_get_id(wpa_s, i);
3480 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3481 i = atoi(cmd + 5);
3482 bss = wpa_bss_get_id(wpa_s, i);
3483 if (bss) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003484 next = bss->list_id.next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003485 if (next == &wpa_s->bss_id)
3486 bss = NULL;
3487 else
3488 bss = dl_list_entry(next, struct wpa_bss,
3489 list_id);
3490 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003491#ifdef CONFIG_P2P
3492 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
3493 if (hwaddr_aton(cmd + 13, bssid) == 0)
3494 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
3495 else
3496 bss = NULL;
3497#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498 } else if (hwaddr_aton(cmd, bssid) == 0)
3499 bss = wpa_bss_get_bssid(wpa_s, bssid);
3500 else {
3501 struct wpa_bss *tmp;
3502 i = atoi(cmd);
3503 bss = NULL;
3504 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
3505 {
3506 if (i-- == 0) {
3507 bss = tmp;
3508 break;
3509 }
3510 }
3511 }
3512
Dmitry Shmidt04949592012-07-19 12:16:46 -07003513 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
3514 mask = strtoul(ctmp + 5, NULL, 0x10);
3515 if (mask == 0)
3516 mask = WPA_BSS_MASK_ALL;
3517 }
3518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003519 if (bss == NULL)
3520 return 0;
3521
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003522 if (bsslast == NULL)
3523 bsslast = bss;
3524 do {
3525 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
3526 ret += len;
3527 buf += len;
3528 buflen -= len;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003529 if (bss == bsslast) {
3530 if ((mask & WPA_BSS_MASK_DELIM) && len &&
3531 (bss == dl_list_last(&wpa_s->bss_id,
3532 struct wpa_bss, list_id)))
3533 os_snprintf(buf - 5, 5, "####\n");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003534 break;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003535 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003536 next = bss->list_id.next;
3537 if (next == &wpa_s->bss_id)
3538 break;
3539 bss = dl_list_entry(next, struct wpa_bss, list_id);
3540 } while (bss && len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003541
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003542 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003543}
3544
3545
3546static int wpa_supplicant_ctrl_iface_ap_scan(
3547 struct wpa_supplicant *wpa_s, char *cmd)
3548{
3549 int ap_scan = atoi(cmd);
3550 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
3551}
3552
3553
3554static int wpa_supplicant_ctrl_iface_scan_interval(
3555 struct wpa_supplicant *wpa_s, char *cmd)
3556{
3557 int scan_int = atoi(cmd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003558 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003559}
3560
3561
3562static int wpa_supplicant_ctrl_iface_bss_expire_age(
3563 struct wpa_supplicant *wpa_s, char *cmd)
3564{
3565 int expire_age = atoi(cmd);
3566 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
3567}
3568
3569
3570static int wpa_supplicant_ctrl_iface_bss_expire_count(
3571 struct wpa_supplicant *wpa_s, char *cmd)
3572{
3573 int expire_count = atoi(cmd);
3574 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
3575}
3576
3577
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07003578static int wpa_supplicant_ctrl_iface_bss_flush(
3579 struct wpa_supplicant *wpa_s, char *cmd)
3580{
3581 int flush_age = atoi(cmd);
3582
3583 if (flush_age == 0)
3584 wpa_bss_flush(wpa_s);
3585 else
3586 wpa_bss_flush_by_age(wpa_s, flush_age);
3587 return 0;
3588}
3589
3590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003591static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
3592{
3593 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
3594 /* MLME-DELETEKEYS.request */
3595 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
3596 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
3597 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
3598 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
3599#ifdef CONFIG_IEEE80211W
3600 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
3601 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
3602#endif /* CONFIG_IEEE80211W */
3603
3604 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
3605 0);
3606 /* MLME-SETPROTECTION.request(None) */
3607 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
3608 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
3609 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3610 wpa_sm_drop_sa(wpa_s->wpa);
3611}
3612
3613
3614static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
3615 char *addr)
3616{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003617#ifdef CONFIG_NO_SCAN_PROCESSING
3618 return -1;
3619#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620 u8 bssid[ETH_ALEN];
3621 struct wpa_bss *bss;
3622 struct wpa_ssid *ssid = wpa_s->current_ssid;
3623
3624 if (hwaddr_aton(addr, bssid)) {
3625 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
3626 "address '%s'", addr);
3627 return -1;
3628 }
3629
3630 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
3631
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003632 if (!ssid) {
3633 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
3634 "configuration known for the target AP");
3635 return -1;
3636 }
3637
3638 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003639 if (!bss) {
3640 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
3641 "from BSS table");
3642 return -1;
3643 }
3644
3645 /*
3646 * TODO: Find best network configuration block from configuration to
3647 * allow roaming to other networks
3648 */
3649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003650 wpa_s->reassociate = 1;
3651 wpa_supplicant_connect(wpa_s, bss, ssid);
3652
3653 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003654#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003655}
3656
3657
3658#ifdef CONFIG_P2P
3659static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
3660{
3661 unsigned int timeout = atoi(cmd);
3662 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003663 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
3664 char *pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003665 unsigned int search_delay;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666
3667 if (os_strstr(cmd, "type=social"))
3668 type = P2P_FIND_ONLY_SOCIAL;
3669 else if (os_strstr(cmd, "type=progressive"))
3670 type = P2P_FIND_PROGRESSIVE;
3671
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003672 pos = os_strstr(cmd, "dev_id=");
3673 if (pos) {
3674 pos += 7;
3675 if (hwaddr_aton(pos, dev_id))
3676 return -1;
3677 _dev_id = dev_id;
3678 }
3679
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003680 pos = os_strstr(cmd, "delay=");
3681 if (pos) {
3682 pos += 6;
3683 search_delay = atoi(pos);
3684 } else
3685 search_delay = wpas_p2p_search_delay(wpa_s);
3686
3687 return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
3688 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003689}
3690
3691
3692static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
3693 char *buf, size_t buflen)
3694{
3695 u8 addr[ETH_ALEN];
3696 char *pos, *pos2;
3697 char *pin = NULL;
3698 enum p2p_wps_method wps_method;
3699 int new_pin;
3700 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003701 int persistent_group, persistent_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003702 int join;
3703 int auth;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003704 int automatic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003705 int go_intent = -1;
3706 int freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003707 int pd;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003708 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003709
Dmitry Shmidt04949592012-07-19 12:16:46 -07003710 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
3711 * [persistent|persistent=<network id>]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003712 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003713 * [ht40] [vht] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003714
3715 if (hwaddr_aton(cmd, addr))
3716 return -1;
3717
3718 pos = cmd + 17;
3719 if (*pos != ' ')
3720 return -1;
3721 pos++;
3722
3723 persistent_group = os_strstr(pos, " persistent") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003724 pos2 = os_strstr(pos, " persistent=");
3725 if (pos2) {
3726 struct wpa_ssid *ssid;
3727 persistent_id = atoi(pos2 + 12);
3728 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3729 if (ssid == NULL || ssid->disabled != 2 ||
3730 ssid->mode != WPAS_MODE_P2P_GO) {
3731 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3732 "SSID id=%d for persistent P2P group (GO)",
3733 persistent_id);
3734 return -1;
3735 }
3736 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003737 join = os_strstr(pos, " join") != NULL;
3738 auth = os_strstr(pos, " auth") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003739 automatic = os_strstr(pos, " auto") != NULL;
3740 pd = os_strstr(pos, " provdisc") != NULL;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003741 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
3742 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
3743 vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744
3745 pos2 = os_strstr(pos, " go_intent=");
3746 if (pos2) {
3747 pos2 += 11;
3748 go_intent = atoi(pos2);
3749 if (go_intent < 0 || go_intent > 15)
3750 return -1;
3751 }
3752
3753 pos2 = os_strstr(pos, " freq=");
3754 if (pos2) {
3755 pos2 += 6;
3756 freq = atoi(pos2);
3757 if (freq <= 0)
3758 return -1;
3759 }
3760
3761 if (os_strncmp(pos, "pin", 3) == 0) {
3762 /* Request random PIN (to be displayed) and enable the PIN */
3763 wps_method = WPS_PIN_DISPLAY;
3764 } else if (os_strncmp(pos, "pbc", 3) == 0) {
3765 wps_method = WPS_PBC;
3766 } else {
3767 pin = pos;
3768 pos = os_strchr(pin, ' ');
3769 wps_method = WPS_PIN_KEYPAD;
3770 if (pos) {
3771 *pos++ = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003772 if (os_strncmp(pos, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003773 wps_method = WPS_PIN_DISPLAY;
3774 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003775 if (!wps_pin_str_valid(pin)) {
3776 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
3777 return 17;
3778 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779 }
3780
3781 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003782 persistent_group, automatic, join,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003783 auth, go_intent, freq, persistent_id, pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003784 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003785 if (new_pin == -2) {
3786 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
3787 return 25;
3788 }
3789 if (new_pin == -3) {
3790 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
3791 return 25;
3792 }
3793 if (new_pin < 0)
3794 return -1;
3795 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
3796 ret = os_snprintf(buf, buflen, "%08d", new_pin);
3797 if (ret < 0 || (size_t) ret >= buflen)
3798 return -1;
3799 return ret;
3800 }
3801
3802 os_memcpy(buf, "OK\n", 3);
3803 return 3;
3804}
3805
3806
3807static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
3808{
3809 unsigned int timeout = atoi(cmd);
3810 return wpas_p2p_listen(wpa_s, timeout);
3811}
3812
3813
3814static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
3815{
3816 u8 addr[ETH_ALEN];
3817 char *pos;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003818 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003819
Dmitry Shmidt04949592012-07-19 12:16:46 -07003820 /* <addr> <config method> [join|auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003821
3822 if (hwaddr_aton(cmd, addr))
3823 return -1;
3824
3825 pos = cmd + 17;
3826 if (*pos != ' ')
3827 return -1;
3828 pos++;
3829
Dmitry Shmidt04949592012-07-19 12:16:46 -07003830 if (os_strstr(pos, " join") != NULL)
3831 use = WPAS_P2P_PD_FOR_JOIN;
3832 else if (os_strstr(pos, " auto") != NULL)
3833 use = WPAS_P2P_PD_AUTO;
3834
3835 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003836}
3837
3838
3839static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
3840 size_t buflen)
3841{
3842 struct wpa_ssid *ssid = wpa_s->current_ssid;
3843
3844 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3845 ssid->passphrase == NULL)
3846 return -1;
3847
3848 os_strlcpy(buf, ssid->passphrase, buflen);
3849 return os_strlen(buf);
3850}
3851
3852
3853static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
3854 char *buf, size_t buflen)
3855{
3856 u64 ref;
3857 int res;
3858 u8 dst_buf[ETH_ALEN], *dst;
3859 struct wpabuf *tlvs;
3860 char *pos;
3861 size_t len;
3862
3863 if (hwaddr_aton(cmd, dst_buf))
3864 return -1;
3865 dst = dst_buf;
3866 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
3867 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
3868 dst = NULL;
3869 pos = cmd + 17;
3870 if (*pos != ' ')
3871 return -1;
3872 pos++;
3873
3874 if (os_strncmp(pos, "upnp ", 5) == 0) {
3875 u8 version;
3876 pos += 5;
3877 if (hexstr2bin(pos, &version, 1) < 0)
3878 return -1;
3879 pos += 2;
3880 if (*pos != ' ')
3881 return -1;
3882 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003883 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003884#ifdef CONFIG_WIFI_DISPLAY
3885 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
3886 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
3887#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 } else {
3889 len = os_strlen(pos);
3890 if (len & 1)
3891 return -1;
3892 len /= 2;
3893 tlvs = wpabuf_alloc(len);
3894 if (tlvs == NULL)
3895 return -1;
3896 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
3897 wpabuf_free(tlvs);
3898 return -1;
3899 }
3900
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003901 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902 wpabuf_free(tlvs);
3903 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003904 if (ref == 0)
3905 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003906 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
3907 if (res < 0 || (unsigned) res >= buflen)
3908 return -1;
3909 return res;
3910}
3911
3912
3913static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
3914 char *cmd)
3915{
3916 long long unsigned val;
3917 u64 req;
3918 if (sscanf(cmd, "%llx", &val) != 1)
3919 return -1;
3920 req = val;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003921 return wpas_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003922}
3923
3924
3925static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
3926{
3927 int freq;
3928 u8 dst[ETH_ALEN];
3929 u8 dialog_token;
3930 struct wpabuf *resp_tlvs;
3931 char *pos, *pos2;
3932 size_t len;
3933
3934 pos = os_strchr(cmd, ' ');
3935 if (pos == NULL)
3936 return -1;
3937 *pos++ = '\0';
3938 freq = atoi(cmd);
3939 if (freq == 0)
3940 return -1;
3941
3942 if (hwaddr_aton(pos, dst))
3943 return -1;
3944 pos += 17;
3945 if (*pos != ' ')
3946 return -1;
3947 pos++;
3948
3949 pos2 = os_strchr(pos, ' ');
3950 if (pos2 == NULL)
3951 return -1;
3952 *pos2++ = '\0';
3953 dialog_token = atoi(pos);
3954
3955 len = os_strlen(pos2);
3956 if (len & 1)
3957 return -1;
3958 len /= 2;
3959 resp_tlvs = wpabuf_alloc(len);
3960 if (resp_tlvs == NULL)
3961 return -1;
3962 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
3963 wpabuf_free(resp_tlvs);
3964 return -1;
3965 }
3966
3967 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
3968 wpabuf_free(resp_tlvs);
3969 return 0;
3970}
3971
3972
3973static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
3974 char *cmd)
3975{
Dmitry Shmidt04949592012-07-19 12:16:46 -07003976 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
3977 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
3979 return 0;
3980}
3981
3982
3983static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
3984 char *cmd)
3985{
3986 char *pos;
3987 size_t len;
3988 struct wpabuf *query, *resp;
3989
3990 pos = os_strchr(cmd, ' ');
3991 if (pos == NULL)
3992 return -1;
3993 *pos++ = '\0';
3994
3995 len = os_strlen(cmd);
3996 if (len & 1)
3997 return -1;
3998 len /= 2;
3999 query = wpabuf_alloc(len);
4000 if (query == NULL)
4001 return -1;
4002 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4003 wpabuf_free(query);
4004 return -1;
4005 }
4006
4007 len = os_strlen(pos);
4008 if (len & 1) {
4009 wpabuf_free(query);
4010 return -1;
4011 }
4012 len /= 2;
4013 resp = wpabuf_alloc(len);
4014 if (resp == NULL) {
4015 wpabuf_free(query);
4016 return -1;
4017 }
4018 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4019 wpabuf_free(query);
4020 wpabuf_free(resp);
4021 return -1;
4022 }
4023
4024 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4025 wpabuf_free(query);
4026 wpabuf_free(resp);
4027 return -1;
4028 }
4029 return 0;
4030}
4031
4032
4033static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4034{
4035 char *pos;
4036 u8 version;
4037
4038 pos = os_strchr(cmd, ' ');
4039 if (pos == NULL)
4040 return -1;
4041 *pos++ = '\0';
4042
4043 if (hexstr2bin(cmd, &version, 1) < 0)
4044 return -1;
4045
4046 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4047}
4048
4049
4050static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4051{
4052 char *pos;
4053
4054 pos = os_strchr(cmd, ' ');
4055 if (pos == NULL)
4056 return -1;
4057 *pos++ = '\0';
4058
4059 if (os_strcmp(cmd, "bonjour") == 0)
4060 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4061 if (os_strcmp(cmd, "upnp") == 0)
4062 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4063 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4064 return -1;
4065}
4066
4067
4068static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4069 char *cmd)
4070{
4071 size_t len;
4072 struct wpabuf *query;
4073 int ret;
4074
4075 len = os_strlen(cmd);
4076 if (len & 1)
4077 return -1;
4078 len /= 2;
4079 query = wpabuf_alloc(len);
4080 if (query == NULL)
4081 return -1;
4082 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4083 wpabuf_free(query);
4084 return -1;
4085 }
4086
4087 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4088 wpabuf_free(query);
4089 return ret;
4090}
4091
4092
4093static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4094{
4095 char *pos;
4096 u8 version;
4097
4098 pos = os_strchr(cmd, ' ');
4099 if (pos == NULL)
4100 return -1;
4101 *pos++ = '\0';
4102
4103 if (hexstr2bin(cmd, &version, 1) < 0)
4104 return -1;
4105
4106 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4107}
4108
4109
4110static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4111{
4112 char *pos;
4113
4114 pos = os_strchr(cmd, ' ');
4115 if (pos == NULL)
4116 return -1;
4117 *pos++ = '\0';
4118
4119 if (os_strcmp(cmd, "bonjour") == 0)
4120 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4121 if (os_strcmp(cmd, "upnp") == 0)
4122 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4123 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4124 return -1;
4125}
4126
4127
4128static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4129{
4130 u8 addr[ETH_ALEN];
4131
4132 /* <addr> */
4133
4134 if (hwaddr_aton(cmd, addr))
4135 return -1;
4136
4137 return wpas_p2p_reject(wpa_s, addr);
4138}
4139
4140
4141static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4142{
4143 char *pos;
4144 int id;
4145 struct wpa_ssid *ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004146 u8 *_peer = NULL, peer[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004147 int freq = 0, pref_freq = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004148 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004149
4150 id = atoi(cmd);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004151 pos = os_strstr(cmd, " peer=");
4152 if (pos) {
4153 pos += 6;
4154 if (hwaddr_aton(pos, peer))
4155 return -1;
4156 _peer = peer;
4157 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004158 ssid = wpa_config_get_network(wpa_s->conf, id);
4159 if (ssid == NULL || ssid->disabled != 2) {
4160 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4161 "for persistent P2P group",
4162 id);
4163 return -1;
4164 }
4165
Jouni Malinen31be0a42012-08-31 21:20:51 +03004166 pos = os_strstr(cmd, " freq=");
4167 if (pos) {
4168 pos += 6;
4169 freq = atoi(pos);
4170 if (freq <= 0)
4171 return -1;
4172 }
4173
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004174 pos = os_strstr(cmd, " pref=");
4175 if (pos) {
4176 pos += 6;
4177 pref_freq = atoi(pos);
4178 if (pref_freq <= 0)
4179 return -1;
4180 }
4181
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004182 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4183 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4184 vht;
Jouni Malinen31be0a42012-08-31 21:20:51 +03004185
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004186 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
4187 pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004188}
4189
4190
4191static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
4192{
4193 char *pos;
4194 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
4195
4196 pos = os_strstr(cmd, " peer=");
4197 if (!pos)
4198 return -1;
4199
4200 *pos = '\0';
4201 pos += 6;
4202 if (hwaddr_aton(pos, peer)) {
4203 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
4204 return -1;
4205 }
4206
4207 pos = os_strstr(pos, " go_dev_addr=");
4208 if (pos) {
4209 pos += 13;
4210 if (hwaddr_aton(pos, go_dev_addr)) {
4211 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
4212 pos);
4213 return -1;
4214 }
4215 go_dev = go_dev_addr;
4216 }
4217
4218 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
4219}
4220
4221
4222static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
4223{
4224 if (os_strncmp(cmd, "persistent=", 11) == 0)
4225 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
4226 if (os_strncmp(cmd, "group=", 6) == 0)
4227 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
4228
4229 return -1;
4230}
4231
4232
4233static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004234 char *cmd, int freq, int ht40,
4235 int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236{
4237 int id;
4238 struct wpa_ssid *ssid;
4239
4240 id = atoi(cmd);
4241 ssid = wpa_config_get_network(wpa_s->conf, id);
4242 if (ssid == NULL || ssid->disabled != 2) {
4243 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4244 "for persistent P2P group",
4245 id);
4246 return -1;
4247 }
4248
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004249 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40, vht,
4250 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004251}
4252
4253
4254static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
4255{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004256 int freq = 0, ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004257 char *pos;
4258
4259 pos = os_strstr(cmd, "freq=");
4260 if (pos)
4261 freq = atoi(pos + 5);
4262
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004263 vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
4264 ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4265 vht;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004267 if (os_strncmp(cmd, "persistent=", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004268 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004269 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004270 if (os_strcmp(cmd, "persistent") == 0 ||
4271 os_strncmp(cmd, "persistent ", 11) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004272 return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004273 if (os_strncmp(cmd, "freq=", 5) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004274 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004275 if (ht40)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004276 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004277
4278 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
4279 cmd);
4280 return -1;
4281}
4282
4283
4284static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
4285 char *buf, size_t buflen)
4286{
4287 u8 addr[ETH_ALEN], *addr_ptr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004288 int next, res;
4289 const struct p2p_peer_info *info;
4290 char *pos, *end;
4291 char devtype[WPS_DEV_TYPE_BUFSIZE];
4292 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004293 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004294
4295 if (!wpa_s->global->p2p)
4296 return -1;
4297
4298 if (os_strcmp(cmd, "FIRST") == 0) {
4299 addr_ptr = NULL;
4300 next = 0;
4301 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4302 if (hwaddr_aton(cmd + 5, addr) < 0)
4303 return -1;
4304 addr_ptr = addr;
4305 next = 1;
4306 } else {
4307 if (hwaddr_aton(cmd, addr) < 0)
4308 return -1;
4309 addr_ptr = addr;
4310 next = 0;
4311 }
4312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004313 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4314 if (info == NULL)
4315 return -1;
4316
4317 pos = buf;
4318 end = buf + buflen;
4319
4320 res = os_snprintf(pos, end - pos, MACSTR "\n"
4321 "pri_dev_type=%s\n"
4322 "device_name=%s\n"
4323 "manufacturer=%s\n"
4324 "model_name=%s\n"
4325 "model_number=%s\n"
4326 "serial_number=%s\n"
4327 "config_methods=0x%x\n"
4328 "dev_capab=0x%x\n"
4329 "group_capab=0x%x\n"
4330 "level=%d\n",
4331 MAC2STR(info->p2p_device_addr),
4332 wps_dev_type_bin2str(info->pri_dev_type,
4333 devtype, sizeof(devtype)),
4334 info->device_name,
4335 info->manufacturer,
4336 info->model_name,
4337 info->model_number,
4338 info->serial_number,
4339 info->config_methods,
4340 info->dev_capab,
4341 info->group_capab,
4342 info->level);
4343 if (res < 0 || res >= end - pos)
4344 return pos - buf;
4345 pos += res;
4346
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004347 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4348 {
4349 const u8 *t;
4350 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4351 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4352 wps_dev_type_bin2str(t, devtype,
4353 sizeof(devtype)));
4354 if (res < 0 || res >= end - pos)
4355 return pos - buf;
4356 pos += res;
4357 }
4358
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004359 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004360 if (ssid) {
4361 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
4362 if (res < 0 || res >= end - pos)
4363 return pos - buf;
4364 pos += res;
4365 }
4366
4367 res = p2p_get_peer_info_txt(info, pos, end - pos);
4368 if (res < 0)
4369 return pos - buf;
4370 pos += res;
4371
4372 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004373}
4374
4375
Dmitry Shmidt04949592012-07-19 12:16:46 -07004376static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
4377 const char *param)
4378{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004379 unsigned int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004380
4381 if (wpa_s->global->p2p == NULL)
4382 return -1;
4383
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004384 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
4385 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004386
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004387 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
4388 struct wpa_freq_range *freq;
4389 freq = &wpa_s->global->p2p_disallow_freq.range[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004390 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004391 freq->min, freq->max);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004392 }
4393
Dmitry Shmidt04949592012-07-19 12:16:46 -07004394 wpas_p2p_update_channel_list(wpa_s);
4395 return 0;
4396}
4397
4398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004399static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
4400{
4401 char *param;
4402
4403 if (wpa_s->global->p2p == NULL)
4404 return -1;
4405
4406 param = os_strchr(cmd, ' ');
4407 if (param == NULL)
4408 return -1;
4409 *param++ = '\0';
4410
4411 if (os_strcmp(cmd, "discoverability") == 0) {
4412 p2p_set_client_discoverability(wpa_s->global->p2p,
4413 atoi(param));
4414 return 0;
4415 }
4416
4417 if (os_strcmp(cmd, "managed") == 0) {
4418 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
4419 return 0;
4420 }
4421
4422 if (os_strcmp(cmd, "listen_channel") == 0) {
4423 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
4424 atoi(param));
4425 }
4426
4427 if (os_strcmp(cmd, "ssid_postfix") == 0) {
4428 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
4429 os_strlen(param));
4430 }
4431
4432 if (os_strcmp(cmd, "noa") == 0) {
4433 char *pos;
4434 int count, start, duration;
4435 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
4436 count = atoi(param);
4437 pos = os_strchr(param, ',');
4438 if (pos == NULL)
4439 return -1;
4440 pos++;
4441 start = atoi(pos);
4442 pos = os_strchr(pos, ',');
4443 if (pos == NULL)
4444 return -1;
4445 pos++;
4446 duration = atoi(pos);
4447 if (count < 0 || count > 255 || start < 0 || duration < 0)
4448 return -1;
4449 if (count == 0 && duration > 0)
4450 return -1;
4451 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
4452 "start=%d duration=%d", count, start, duration);
4453 return wpas_p2p_set_noa(wpa_s, count, start, duration);
4454 }
4455
4456 if (os_strcmp(cmd, "ps") == 0)
4457 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
4458
4459 if (os_strcmp(cmd, "oppps") == 0)
4460 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
4461
4462 if (os_strcmp(cmd, "ctwindow") == 0)
4463 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
4464
4465 if (os_strcmp(cmd, "disabled") == 0) {
4466 wpa_s->global->p2p_disabled = atoi(param);
4467 wpa_printf(MSG_DEBUG, "P2P functionality %s",
4468 wpa_s->global->p2p_disabled ?
4469 "disabled" : "enabled");
4470 if (wpa_s->global->p2p_disabled) {
4471 wpas_p2p_stop_find(wpa_s);
4472 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4473 p2p_flush(wpa_s->global->p2p);
4474 }
4475 return 0;
4476 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004477
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004478 if (os_strcmp(cmd, "conc_pref") == 0) {
4479 if (os_strcmp(param, "sta") == 0)
4480 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
4481 else if (os_strcmp(param, "p2p") == 0)
4482 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004483 else {
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004484 wpa_printf(MSG_INFO, "Invalid conc_pref value");
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004485 return -1;
4486 }
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004487 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004488 "%s", param);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004489 return 0;
4490 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004492 if (os_strcmp(cmd, "force_long_sd") == 0) {
4493 wpa_s->force_long_sd = atoi(param);
4494 return 0;
4495 }
4496
4497 if (os_strcmp(cmd, "peer_filter") == 0) {
4498 u8 addr[ETH_ALEN];
4499 if (hwaddr_aton(param, addr))
4500 return -1;
4501 p2p_set_peer_filter(wpa_s->global->p2p, addr);
4502 return 0;
4503 }
4504
4505 if (os_strcmp(cmd, "cross_connect") == 0)
4506 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
4507
4508 if (os_strcmp(cmd, "go_apsd") == 0) {
4509 if (os_strcmp(param, "disable") == 0)
4510 wpa_s->set_ap_uapsd = 0;
4511 else {
4512 wpa_s->set_ap_uapsd = 1;
4513 wpa_s->ap_uapsd = atoi(param);
4514 }
4515 return 0;
4516 }
4517
4518 if (os_strcmp(cmd, "client_apsd") == 0) {
4519 if (os_strcmp(param, "disable") == 0)
4520 wpa_s->set_sta_uapsd = 0;
4521 else {
4522 int be, bk, vi, vo;
4523 char *pos;
4524 /* format: BE,BK,VI,VO;max SP Length */
4525 be = atoi(param);
4526 pos = os_strchr(param, ',');
4527 if (pos == NULL)
4528 return -1;
4529 pos++;
4530 bk = atoi(pos);
4531 pos = os_strchr(pos, ',');
4532 if (pos == NULL)
4533 return -1;
4534 pos++;
4535 vi = atoi(pos);
4536 pos = os_strchr(pos, ',');
4537 if (pos == NULL)
4538 return -1;
4539 pos++;
4540 vo = atoi(pos);
4541 /* ignore max SP Length for now */
4542
4543 wpa_s->set_sta_uapsd = 1;
4544 wpa_s->sta_uapsd = 0;
4545 if (be)
4546 wpa_s->sta_uapsd |= BIT(0);
4547 if (bk)
4548 wpa_s->sta_uapsd |= BIT(1);
4549 if (vi)
4550 wpa_s->sta_uapsd |= BIT(2);
4551 if (vo)
4552 wpa_s->sta_uapsd |= BIT(3);
4553 }
4554 return 0;
4555 }
4556
Dmitry Shmidt04949592012-07-19 12:16:46 -07004557 if (os_strcmp(cmd, "disallow_freq") == 0)
4558 return p2p_ctrl_disallow_freq(wpa_s, param);
4559
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004560 if (os_strcmp(cmd, "disc_int") == 0) {
4561 int min_disc_int, max_disc_int, max_disc_tu;
4562 char *pos;
4563
4564 pos = param;
4565
4566 min_disc_int = atoi(pos);
4567 pos = os_strchr(pos, ' ');
4568 if (pos == NULL)
4569 return -1;
4570 *pos++ = '\0';
4571
4572 max_disc_int = atoi(pos);
4573 pos = os_strchr(pos, ' ');
4574 if (pos == NULL)
4575 return -1;
4576 *pos++ = '\0';
4577
4578 max_disc_tu = atoi(pos);
4579
4580 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
4581 max_disc_int, max_disc_tu);
4582 }
4583
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004584 if (os_strcmp(cmd, "per_sta_psk") == 0) {
4585 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
4586 return 0;
4587 }
4588
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004589 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
4590 cmd);
4591
4592 return -1;
4593}
4594
4595
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004596static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
4597{
4598 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4599 wpa_s->force_long_sd = 0;
4600 if (wpa_s->global->p2p)
4601 p2p_flush(wpa_s->global->p2p);
4602}
4603
4604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
4606{
4607 char *pos, *pos2;
4608 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
4609
4610 if (cmd[0]) {
4611 pos = os_strchr(cmd, ' ');
4612 if (pos == NULL)
4613 return -1;
4614 *pos++ = '\0';
4615 dur1 = atoi(cmd);
4616
4617 pos2 = os_strchr(pos, ' ');
4618 if (pos2)
4619 *pos2++ = '\0';
4620 int1 = atoi(pos);
4621 } else
4622 pos2 = NULL;
4623
4624 if (pos2) {
4625 pos = os_strchr(pos2, ' ');
4626 if (pos == NULL)
4627 return -1;
4628 *pos++ = '\0';
4629 dur2 = atoi(pos2);
4630 int2 = atoi(pos);
4631 }
4632
4633 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
4634}
4635
4636
4637static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
4638{
4639 char *pos;
4640 unsigned int period = 0, interval = 0;
4641
4642 if (cmd[0]) {
4643 pos = os_strchr(cmd, ' ');
4644 if (pos == NULL)
4645 return -1;
4646 *pos++ = '\0';
4647 period = atoi(cmd);
4648 interval = atoi(pos);
4649 }
4650
4651 return wpas_p2p_ext_listen(wpa_s, period, interval);
4652}
4653
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004654
4655static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
4656{
4657 const char *pos;
4658 u8 peer[ETH_ALEN];
4659 int iface_addr = 0;
4660
4661 pos = cmd;
4662 if (os_strncmp(pos, "iface=", 6) == 0) {
4663 iface_addr = 1;
4664 pos += 6;
4665 }
4666 if (hwaddr_aton(pos, peer))
4667 return -1;
4668
4669 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
4670 return 0;
4671}
4672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673#endif /* CONFIG_P2P */
4674
4675
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004676#ifdef CONFIG_INTERWORKING
4677static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
4678{
4679 u8 bssid[ETH_ALEN];
4680 struct wpa_bss *bss;
4681
4682 if (hwaddr_aton(dst, bssid)) {
4683 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
4684 return -1;
4685 }
4686
4687 bss = wpa_bss_get_bssid(wpa_s, bssid);
4688 if (bss == NULL) {
4689 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
4690 MAC2STR(bssid));
4691 return -1;
4692 }
4693
4694 return interworking_connect(wpa_s, bss);
4695}
4696
4697
4698static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
4699{
4700 u8 dst_addr[ETH_ALEN];
4701 int used;
4702 char *pos;
4703#define MAX_ANQP_INFO_ID 100
4704 u16 id[MAX_ANQP_INFO_ID];
4705 size_t num_id = 0;
4706
4707 used = hwaddr_aton2(dst, dst_addr);
4708 if (used < 0)
4709 return -1;
4710 pos = dst + used;
4711 while (num_id < MAX_ANQP_INFO_ID) {
4712 id[num_id] = atoi(pos);
4713 if (id[num_id])
4714 num_id++;
4715 pos = os_strchr(pos + 1, ',');
4716 if (pos == NULL)
4717 break;
4718 pos++;
4719 }
4720
4721 if (num_id == 0)
4722 return -1;
4723
4724 return anqp_send_req(wpa_s, dst_addr, id, num_id);
4725}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004726
4727
4728static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
4729{
4730 u8 dst_addr[ETH_ALEN];
4731 struct wpabuf *advproto, *query = NULL;
4732 int used, ret = -1;
4733 char *pos, *end;
4734 size_t len;
4735
4736 used = hwaddr_aton2(cmd, dst_addr);
4737 if (used < 0)
4738 return -1;
4739
4740 pos = cmd + used;
4741 while (*pos == ' ')
4742 pos++;
4743
4744 /* Advertisement Protocol ID */
4745 end = os_strchr(pos, ' ');
4746 if (end)
4747 len = end - pos;
4748 else
4749 len = os_strlen(pos);
4750 if (len & 0x01)
4751 return -1;
4752 len /= 2;
4753 if (len == 0)
4754 return -1;
4755 advproto = wpabuf_alloc(len);
4756 if (advproto == NULL)
4757 return -1;
4758 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
4759 goto fail;
4760
4761 if (end) {
4762 /* Optional Query Request */
4763 pos = end + 1;
4764 while (*pos == ' ')
4765 pos++;
4766
4767 len = os_strlen(pos);
4768 if (len) {
4769 if (len & 0x01)
4770 goto fail;
4771 len /= 2;
4772 if (len == 0)
4773 goto fail;
4774 query = wpabuf_alloc(len);
4775 if (query == NULL)
4776 goto fail;
4777 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
4778 goto fail;
4779 }
4780 }
4781
4782 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
4783
4784fail:
4785 wpabuf_free(advproto);
4786 wpabuf_free(query);
4787
4788 return ret;
4789}
4790
4791
4792static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
4793 size_t buflen)
4794{
4795 u8 addr[ETH_ALEN];
4796 int dialog_token;
4797 int used;
4798 char *pos;
4799 size_t resp_len, start, requested_len;
4800
4801 if (!wpa_s->last_gas_resp)
4802 return -1;
4803
4804 used = hwaddr_aton2(cmd, addr);
4805 if (used < 0)
4806 return -1;
4807
4808 pos = cmd + used;
4809 while (*pos == ' ')
4810 pos++;
4811 dialog_token = atoi(pos);
4812
4813 if (os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) != 0 ||
4814 dialog_token != wpa_s->last_gas_dialog_token)
4815 return -1;
4816
4817 resp_len = wpabuf_len(wpa_s->last_gas_resp);
4818 start = 0;
4819 requested_len = resp_len;
4820
4821 pos = os_strchr(pos, ' ');
4822 if (pos) {
4823 start = atoi(pos);
4824 if (start > resp_len)
4825 return os_snprintf(buf, buflen, "FAIL-Invalid range");
4826 pos = os_strchr(pos, ',');
4827 if (pos == NULL)
4828 return -1;
4829 pos++;
4830 requested_len = atoi(pos);
4831 if (start + requested_len > resp_len)
4832 return os_snprintf(buf, buflen, "FAIL-Invalid range");
4833 }
4834
4835 if (requested_len * 2 + 1 > buflen)
4836 return os_snprintf(buf, buflen, "FAIL-Too long response");
4837
4838 return wpa_snprintf_hex(buf, buflen,
4839 wpabuf_head_u8(wpa_s->last_gas_resp) + start,
4840 requested_len);
4841}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004842#endif /* CONFIG_INTERWORKING */
4843
4844
Dmitry Shmidt04949592012-07-19 12:16:46 -07004845#ifdef CONFIG_HS20
4846
4847static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
4848{
4849 u8 dst_addr[ETH_ALEN];
4850 int used;
4851 char *pos;
4852 u32 subtypes = 0;
4853
4854 used = hwaddr_aton2(dst, dst_addr);
4855 if (used < 0)
4856 return -1;
4857 pos = dst + used;
4858 for (;;) {
4859 int num = atoi(pos);
4860 if (num <= 0 || num > 31)
4861 return -1;
4862 subtypes |= BIT(num);
4863 pos = os_strchr(pos + 1, ',');
4864 if (pos == NULL)
4865 break;
4866 pos++;
4867 }
4868
4869 if (subtypes == 0)
4870 return -1;
4871
4872 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
4873}
4874
4875
4876static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4877 const u8 *addr, const char *realm)
4878{
4879 u8 *buf;
4880 size_t rlen, len;
4881 int ret;
4882
4883 rlen = os_strlen(realm);
4884 len = 3 + rlen;
4885 buf = os_malloc(len);
4886 if (buf == NULL)
4887 return -1;
4888 buf[0] = 1; /* NAI Home Realm Count */
4889 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
4890 buf[2] = rlen;
4891 os_memcpy(buf + 3, realm, rlen);
4892
4893 ret = hs20_anqp_send_req(wpa_s, addr,
4894 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4895 buf, len);
4896
4897 os_free(buf);
4898
4899 return ret;
4900}
4901
4902
4903static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4904 char *dst)
4905{
4906 struct wpa_cred *cred = wpa_s->conf->cred;
4907 u8 dst_addr[ETH_ALEN];
4908 int used;
4909 u8 *buf;
4910 size_t len;
4911 int ret;
4912
4913 used = hwaddr_aton2(dst, dst_addr);
4914 if (used < 0)
4915 return -1;
4916
4917 while (dst[used] == ' ')
4918 used++;
4919 if (os_strncmp(dst + used, "realm=", 6) == 0)
4920 return hs20_nai_home_realm_list(wpa_s, dst_addr,
4921 dst + used + 6);
4922
4923 len = os_strlen(dst + used);
4924
4925 if (len == 0 && cred && cred->realm)
4926 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
4927
4928 if (len % 1)
4929 return -1;
4930 len /= 2;
4931 buf = os_malloc(len);
4932 if (buf == NULL)
4933 return -1;
4934 if (hexstr2bin(dst + used, buf, len) < 0) {
4935 os_free(buf);
4936 return -1;
4937 }
4938
4939 ret = hs20_anqp_send_req(wpa_s, dst_addr,
4940 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4941 buf, len);
4942 os_free(buf);
4943
4944 return ret;
4945}
4946
4947#endif /* CONFIG_HS20 */
4948
4949
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004950static int wpa_supplicant_ctrl_iface_sta_autoconnect(
4951 struct wpa_supplicant *wpa_s, char *cmd)
4952{
4953 wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
4954 return 0;
4955}
4956
4957
Dmitry Shmidt04949592012-07-19 12:16:46 -07004958#ifdef CONFIG_AUTOSCAN
4959
4960static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
4961 char *cmd)
4962{
4963 enum wpa_states state = wpa_s->wpa_state;
4964 char *new_params = NULL;
4965
4966 if (os_strlen(cmd) > 0) {
4967 new_params = os_strdup(cmd);
4968 if (new_params == NULL)
4969 return -1;
4970 }
4971
4972 os_free(wpa_s->conf->autoscan);
4973 wpa_s->conf->autoscan = new_params;
4974
4975 if (wpa_s->conf->autoscan == NULL)
4976 autoscan_deinit(wpa_s);
4977 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
4978 autoscan_init(wpa_s, 1);
4979 else if (state == WPA_SCANNING)
4980 wpa_supplicant_reinit_autoscan(wpa_s);
4981
4982 return 0;
4983}
4984
4985#endif /* CONFIG_AUTOSCAN */
4986
4987
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004988#ifdef CONFIG_WNM
4989
4990static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
4991{
4992 int enter;
4993 int intval = 0;
4994 char *pos;
4995 int ret;
4996 struct wpabuf *tfs_req = NULL;
4997
4998 if (os_strncmp(cmd, "enter", 5) == 0)
4999 enter = 1;
5000 else if (os_strncmp(cmd, "exit", 4) == 0)
5001 enter = 0;
5002 else
5003 return -1;
5004
5005 pos = os_strstr(cmd, " interval=");
5006 if (pos)
5007 intval = atoi(pos + 10);
5008
5009 pos = os_strstr(cmd, " tfs_req=");
5010 if (pos) {
5011 char *end;
5012 size_t len;
5013 pos += 9;
5014 end = os_strchr(pos, ' ');
5015 if (end)
5016 len = end - pos;
5017 else
5018 len = os_strlen(pos);
5019 if (len & 1)
5020 return -1;
5021 len /= 2;
5022 tfs_req = wpabuf_alloc(len);
5023 if (tfs_req == NULL)
5024 return -1;
5025 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
5026 wpabuf_free(tfs_req);
5027 return -1;
5028 }
5029 }
5030
5031 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
5032 WNM_SLEEP_MODE_EXIT, intval,
5033 tfs_req);
5034 wpabuf_free(tfs_req);
5035
5036 return ret;
5037}
5038
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005039
5040static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
5041{
5042 int query_reason;
5043
5044 query_reason = atoi(cmd);
5045
5046 wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
5047 query_reason);
5048
5049 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
5050}
5051
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005052#endif /* CONFIG_WNM */
5053
5054
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005055/* Get string representation of channel width */
5056static const char * channel_width_name(enum chan_width width)
5057{
5058 switch (width) {
5059 case CHAN_WIDTH_20_NOHT:
5060 return "20 MHz (no HT)";
5061 case CHAN_WIDTH_20:
5062 return "20 MHz";
5063 case CHAN_WIDTH_40:
5064 return "40 MHz";
5065 case CHAN_WIDTH_80:
5066 return "80 MHz";
5067 case CHAN_WIDTH_80P80:
5068 return "80+80 MHz";
5069 case CHAN_WIDTH_160:
5070 return "160 MHz";
5071 default:
5072 return "unknown";
5073 }
5074}
5075
5076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005077static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
5078 size_t buflen)
5079{
5080 struct wpa_signal_info si;
5081 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005082 char *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005083
5084 ret = wpa_drv_signal_poll(wpa_s, &si);
5085 if (ret)
5086 return -1;
5087
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005088 pos = buf;
5089 end = buf + buflen;
5090
5091 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005092 "NOISE=%d\nFREQUENCY=%u\n",
5093 si.current_signal, si.current_txrate / 1000,
5094 si.current_noise, si.frequency);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005095 if (ret < 0 || ret > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005096 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005097 pos += ret;
5098
5099 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
5100 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
5101 channel_width_name(si.chanwidth));
5102 if (ret < 0 || ret > end - pos)
5103 return -1;
5104 pos += ret;
5105 }
5106
5107 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
5108 ret = os_snprintf(pos, end - pos,
5109 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
5110 si.center_frq1, si.center_frq2);
5111 if (ret < 0 || ret > end - pos)
5112 return -1;
5113 pos += ret;
5114 }
5115
5116 if (si.avg_signal) {
5117 ret = os_snprintf(pos, end - pos,
5118 "AVG_RSSI=%d\n", si.avg_signal);
5119 if (ret < 0 || ret >= end - pos)
5120 return -1;
5121 pos += ret;
5122 }
5123
5124 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005125}
5126
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005127
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005128static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
5129 size_t buflen)
5130{
5131 struct hostap_sta_driver_data sta;
5132 int ret;
5133
5134 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
5135 if (ret)
5136 return -1;
5137
5138 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005139 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
5140 if (ret < 0 || (size_t) ret > buflen)
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005141 return -1;
5142 return ret;
5143}
5144
5145
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005146#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005147static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5148 char *buf, size_t buflen)
5149{
5150 int ret;
5151
5152 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005153 if (ret == 0) {
5154 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
5155 struct p2p_data *p2p = wpa_s->global->p2p;
5156 if (p2p) {
5157 char country[3];
5158 country[0] = cmd[8];
5159 country[1] = cmd[9];
5160 country[2] = 0x04;
5161 p2p_set_country(p2p, country);
5162 }
5163 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005164 ret = sprintf(buf, "%s\n", "OK");
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005165 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005166 return ret;
5167}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005168#endif
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005169
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07005170
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005171static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
5172{
5173 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
5174
5175#ifdef CONFIG_P2P
5176 wpas_p2p_stop_find(wpa_s);
5177 p2p_ctrl_flush(wpa_s);
5178 wpas_p2p_group_remove(wpa_s, "*");
5179#endif /* CONFIG_P2P */
5180
5181#ifdef CONFIG_WPS_TESTING
5182 wps_version_number = 0x20;
5183 wps_testing_dummy_cred = 0;
5184#endif /* CONFIG_WPS_TESTING */
5185#ifdef CONFIG_WPS
5186 wpas_wps_cancel(wpa_s);
5187#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07005188 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005189 wpa_s->known_wps_freq = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005190
5191#ifdef CONFIG_TDLS_TESTING
5192 extern unsigned int tdls_testing;
5193 tdls_testing = 0;
5194#endif /* CONFIG_TDLS_TESTING */
5195#ifdef CONFIG_TDLS
5196 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
5197 wpa_tdls_enable(wpa_s->wpa, 1);
5198#endif /* CONFIG_TDLS */
5199
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005200 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
5201 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
5202
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005203 wpa_s->no_keep_alive = 0;
5204
5205 os_free(wpa_s->disallow_aps_bssid);
5206 wpa_s->disallow_aps_bssid = NULL;
5207 wpa_s->disallow_aps_bssid_count = 0;
5208 os_free(wpa_s->disallow_aps_ssid);
5209 wpa_s->disallow_aps_ssid = NULL;
5210 wpa_s->disallow_aps_ssid_count = 0;
5211
5212 wpa_s->set_sta_uapsd = 0;
5213 wpa_s->sta_uapsd = 0;
5214
5215 wpa_drv_radio_disable(wpa_s, 0);
5216
5217 wpa_bss_flush(wpa_s);
5218 wpa_blacklist_clear(wpa_s);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07005219 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005220 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
5221 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
5222}
5223
5224
Dmitry Shmidt051af732013-10-22 13:52:46 -07005225static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
5226{
5227 struct wpa_supplicant *wpa_s = eloop_ctx;
5228 eapol_sm_notify_ctrl_response(wpa_s->eapol);
5229}
5230
5231
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005232char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
5233 char *buf, size_t *resp_len)
5234{
5235 char *reply;
5236 const int reply_size = 4096;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005237 int reply_len;
5238
5239 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005240 os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
5241 os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005242 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005243 os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005244 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
5245 (const u8 *) buf, os_strlen(buf));
5246 } else {
5247 int level = MSG_DEBUG;
5248 if (os_strcmp(buf, "PING") == 0)
5249 level = MSG_EXCESSIVE;
5250 wpa_hexdump_ascii(level, "RX ctrl_iface",
5251 (const u8 *) buf, os_strlen(buf));
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005252 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005253 }
5254
5255 reply = os_malloc(reply_size);
5256 if (reply == NULL) {
5257 *resp_len = 1;
5258 return NULL;
5259 }
5260
5261 os_memcpy(reply, "OK\n", 3);
5262 reply_len = 3;
5263
5264 if (os_strcmp(buf, "PING") == 0) {
5265 os_memcpy(reply, "PONG\n", 5);
5266 reply_len = 5;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005267 } else if (os_strcmp(buf, "IFNAME") == 0) {
5268 reply_len = os_strlen(wpa_s->ifname);
5269 os_memcpy(reply, wpa_s->ifname, reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005270 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
5271 if (wpa_debug_reopen_file() < 0)
5272 reply_len = -1;
5273 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
5274 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
5275 } else if (os_strcmp(buf, "MIB") == 0) {
5276 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
5277 if (reply_len >= 0) {
5278 int res;
5279 res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
5280 reply_size - reply_len);
5281 if (res < 0)
5282 reply_len = -1;
5283 else
5284 reply_len += res;
5285 }
5286 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
5287 reply_len = wpa_supplicant_ctrl_iface_status(
5288 wpa_s, buf + 6, reply, reply_size);
5289 } else if (os_strcmp(buf, "PMKSA") == 0) {
5290 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
5291 reply_size);
5292 } else if (os_strncmp(buf, "SET ", 4) == 0) {
5293 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
5294 reply_len = -1;
5295 } else if (os_strncmp(buf, "GET ", 4) == 0) {
5296 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
5297 reply, reply_size);
5298 } else if (os_strcmp(buf, "LOGON") == 0) {
5299 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5300 } else if (os_strcmp(buf, "LOGOFF") == 0) {
5301 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
5302 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
5303 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5304 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005305 else
5306 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005307 } else if (os_strcmp(buf, "RECONNECT") == 0) {
5308 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5309 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005310 else if (wpa_s->disconnected)
5311 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005312#ifdef IEEE8021X_EAPOL
5313 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
5314 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
5315 reply_len = -1;
5316#endif /* IEEE8021X_EAPOL */
5317#ifdef CONFIG_PEERKEY
5318 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
5319 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
5320 reply_len = -1;
5321#endif /* CONFIG_PEERKEY */
5322#ifdef CONFIG_IEEE80211R
5323 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
5324 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
5325 reply_len = -1;
5326#endif /* CONFIG_IEEE80211R */
5327#ifdef CONFIG_WPS
5328 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
5329 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
5330 if (res == -2) {
5331 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5332 reply_len = 17;
5333 } else if (res)
5334 reply_len = -1;
5335 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
5336 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
5337 if (res == -2) {
5338 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5339 reply_len = 17;
5340 } else if (res)
5341 reply_len = -1;
5342 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
5343 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
5344 reply,
5345 reply_size);
5346 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
5347 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
5348 wpa_s, buf + 14, reply, reply_size);
5349 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
5350 if (wpas_wps_cancel(wpa_s))
5351 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005352#ifdef CONFIG_WPS_NFC
5353 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
5354 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
5355 reply_len = -1;
5356 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
5357 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
5358 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005359 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
5360 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
5361 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005362 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
5363 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
5364 wpa_s, buf + 14, reply, reply_size);
5365 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
5366 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
5367 buf + 17))
5368 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005369 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
5370 reply_len = wpas_ctrl_nfc_get_handover_req(
5371 wpa_s, buf + 21, reply, reply_size);
5372 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
5373 reply_len = wpas_ctrl_nfc_get_handover_sel(
5374 wpa_s, buf + 21, reply, reply_size);
5375 } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
5376 reply_len = wpas_ctrl_nfc_rx_handover_req(
5377 wpa_s, buf + 20, reply, reply_size);
5378 } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
5379 if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
5380 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005381 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
5382 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
5383 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005384#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005385 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
5386 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
5387 reply_len = -1;
5388#ifdef CONFIG_AP
5389 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
5390 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
5391 wpa_s, buf + 11, reply, reply_size);
5392#endif /* CONFIG_AP */
5393#ifdef CONFIG_WPS_ER
5394 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
5395 if (wpas_wps_er_start(wpa_s, NULL))
5396 reply_len = -1;
5397 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
5398 if (wpas_wps_er_start(wpa_s, buf + 13))
5399 reply_len = -1;
5400 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
5401 if (wpas_wps_er_stop(wpa_s))
5402 reply_len = -1;
5403 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
5404 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
5405 reply_len = -1;
5406 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
5407 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
5408 if (ret == -2) {
5409 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5410 reply_len = 17;
5411 } else if (ret == -3) {
5412 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
5413 reply_len = 18;
5414 } else if (ret == -4) {
5415 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
5416 reply_len = 20;
5417 } else if (ret)
5418 reply_len = -1;
5419 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
5420 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
5421 reply_len = -1;
5422 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
5423 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
5424 buf + 18))
5425 reply_len = -1;
5426 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
5427 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
5428 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005429#ifdef CONFIG_WPS_NFC
5430 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
5431 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
5432 wpa_s, buf + 24, reply, reply_size);
5433#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005434#endif /* CONFIG_WPS_ER */
5435#endif /* CONFIG_WPS */
5436#ifdef CONFIG_IBSS_RSN
5437 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
5438 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
5439 reply_len = -1;
5440#endif /* CONFIG_IBSS_RSN */
5441#ifdef CONFIG_P2P
5442 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
5443 if (p2p_ctrl_find(wpa_s, buf + 9))
5444 reply_len = -1;
5445 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
5446 if (p2p_ctrl_find(wpa_s, ""))
5447 reply_len = -1;
5448 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
5449 wpas_p2p_stop_find(wpa_s);
5450 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
5451 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
5452 reply_size);
5453 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
5454 if (p2p_ctrl_listen(wpa_s, buf + 11))
5455 reply_len = -1;
5456 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
5457 if (p2p_ctrl_listen(wpa_s, ""))
5458 reply_len = -1;
5459 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
5460 if (wpas_p2p_group_remove(wpa_s, buf + 17))
5461 reply_len = -1;
5462 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005463 if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005464 reply_len = -1;
5465 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
5466 if (p2p_ctrl_group_add(wpa_s, buf + 14))
5467 reply_len = -1;
5468 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
5469 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
5470 reply_len = -1;
5471 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
5472 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
5473 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
5474 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
5475 reply_size);
5476 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
5477 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
5478 reply_len = -1;
5479 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
5480 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
5481 reply_len = -1;
5482 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
5483 wpas_p2p_sd_service_update(wpa_s);
5484 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
5485 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
5486 reply_len = -1;
5487 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
5488 wpas_p2p_service_flush(wpa_s);
5489 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
5490 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
5491 reply_len = -1;
5492 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
5493 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
5494 reply_len = -1;
5495 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
5496 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
5497 reply_len = -1;
5498 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
5499 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
5500 reply_len = -1;
5501 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
5502 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
5503 reply_size);
5504 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
5505 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
5506 reply_len = -1;
5507 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005508 p2p_ctrl_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005509 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
5510 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
5511 reply_len = -1;
5512 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
5513 if (wpas_p2p_cancel(wpa_s))
5514 reply_len = -1;
5515 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
5516 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
5517 reply_len = -1;
5518 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
5519 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
5520 reply_len = -1;
5521 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
5522 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
5523 reply_len = -1;
5524 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
5525 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
5526 reply_len = -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005527 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
5528 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
5529 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005530#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005531#ifdef CONFIG_WIFI_DISPLAY
5532 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
5533 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
5534 reply_len = -1;
5535 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
5536 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
5537 reply, reply_size);
5538#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005539#ifdef CONFIG_INTERWORKING
5540 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
5541 if (interworking_fetch_anqp(wpa_s) < 0)
5542 reply_len = -1;
5543 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
5544 interworking_stop_fetch_anqp(wpa_s);
5545 } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
5546 if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
5547 NULL) < 0)
5548 reply_len = -1;
5549 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
5550 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
5551 reply_len = -1;
5552 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
5553 if (get_anqp(wpa_s, buf + 9) < 0)
5554 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005555 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
5556 if (gas_request(wpa_s, buf + 12) < 0)
5557 reply_len = -1;
5558 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
5559 reply_len = gas_response_get(wpa_s, buf + 17, reply,
5560 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005561#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005562#ifdef CONFIG_HS20
5563 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
5564 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
5565 reply_len = -1;
5566 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
5567 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
5568 reply_len = -1;
5569#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005570 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
5571 {
5572 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
5573 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
5574 reply_len = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005575 else {
5576 /*
5577 * Notify response from timeout to allow the control
5578 * interface response to be sent first.
5579 */
5580 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
5581 wpa_s, NULL);
5582 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005583 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
5584 if (wpa_supplicant_reload_configuration(wpa_s))
5585 reply_len = -1;
5586 } else if (os_strcmp(buf, "TERMINATE") == 0) {
5587 wpa_supplicant_terminate_proc(wpa_s->global);
5588 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
5589 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
5590 reply_len = -1;
Dmitry Shmidte19501d2011-03-16 14:32:18 -07005591 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005592 reply_len = wpa_supplicant_ctrl_iface_blacklist(
5593 wpa_s, buf + 9, reply, reply_size);
5594 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
5595 reply_len = wpa_supplicant_ctrl_iface_log_level(
5596 wpa_s, buf + 9, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005597 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
5598 reply_len = wpa_supplicant_ctrl_iface_list_networks(
5599 wpa_s, reply, reply_size);
5600 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005601#ifdef CONFIG_SME
5602 wpa_s->sme.prev_bssid_set = 0;
5603#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005604 wpa_s->reassociate = 0;
5605 wpa_s->disconnected = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005606 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005607 wpa_supplicant_cancel_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005608 wpa_supplicant_deauthenticate(wpa_s,
5609 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005610 } else if (os_strcmp(buf, "SCAN") == 0 ||
5611 os_strncmp(buf, "SCAN ", 5) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005612 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5613 reply_len = -1;
5614 else {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005615 if (os_strlen(buf) > 4 &&
5616 os_strncasecmp(buf + 5, "TYPE=ONLY", 9) == 0)
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08005617 wpa_s->scan_res_handler = scan_only_handler;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005618 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005619 ((wpa_s->wpa_state <= WPA_SCANNING) ||
5620 (wpa_s->wpa_state == WPA_COMPLETED))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005621 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005622 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005623 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005624 wpa_s->known_wps_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005625 wpa_supplicant_req_scan(wpa_s, 0, 0);
5626 } else if (wpa_s->sched_scanning) {
5627 wpa_printf(MSG_DEBUG, "Stop ongoing "
5628 "sched_scan to allow requested "
5629 "full scan to proceed");
5630 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005631 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005632 wpa_supplicant_req_scan(wpa_s, 0, 0);
5633 } else {
5634 wpa_printf(MSG_DEBUG, "Ongoing scan action - "
5635 "reject new request");
5636 reply_len = os_snprintf(reply, reply_size,
5637 "FAIL-BUSY\n");
5638 }
5639 }
5640 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
5641 reply_len = wpa_supplicant_ctrl_iface_scan_results(
5642 wpa_s, reply, reply_size);
5643 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
5644 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
5645 reply_len = -1;
5646 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
5647 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
5648 reply_len = -1;
5649 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
5650 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
5651 reply_len = -1;
5652 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
5653 reply_len = wpa_supplicant_ctrl_iface_add_network(
5654 wpa_s, reply, reply_size);
5655 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
5656 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
5657 reply_len = -1;
5658 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
5659 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
5660 reply_len = -1;
5661 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
5662 reply_len = wpa_supplicant_ctrl_iface_get_network(
5663 wpa_s, buf + 12, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005664 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
5665 reply_len = wpa_supplicant_ctrl_iface_list_creds(
5666 wpa_s, reply, reply_size);
5667 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
5668 reply_len = wpa_supplicant_ctrl_iface_add_cred(
5669 wpa_s, reply, reply_size);
5670 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
5671 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
5672 reply_len = -1;
5673 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
5674 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
5675 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005676#ifndef CONFIG_NO_CONFIG_WRITE
5677 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
5678 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
5679 reply_len = -1;
5680#endif /* CONFIG_NO_CONFIG_WRITE */
5681 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
5682 reply_len = wpa_supplicant_ctrl_iface_get_capability(
5683 wpa_s, buf + 15, reply, reply_size);
5684 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
5685 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
5686 reply_len = -1;
5687 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
5688 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
5689 reply_len = -1;
5690 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
5691 reply_len = wpa_supplicant_global_iface_list(
5692 wpa_s->global, reply, reply_size);
5693 } else if (os_strcmp(buf, "INTERFACES") == 0) {
5694 reply_len = wpa_supplicant_global_iface_interfaces(
5695 wpa_s->global, reply, reply_size);
5696 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
5697 reply_len = wpa_supplicant_ctrl_iface_bss(
5698 wpa_s, buf + 4, reply, reply_size);
5699#ifdef CONFIG_AP
5700 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
5701 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
5702 } else if (os_strncmp(buf, "STA ", 4) == 0) {
5703 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
5704 reply_size);
5705 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
5706 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
5707 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005708 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
5709 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
5710 reply_len = -1;
5711 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
5712 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
5713 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005714#endif /* CONFIG_AP */
5715 } else if (os_strcmp(buf, "SUSPEND") == 0) {
5716 wpas_notify_suspend(wpa_s->global);
5717 } else if (os_strcmp(buf, "RESUME") == 0) {
5718 wpas_notify_resume(wpa_s->global);
5719 } else if (os_strcmp(buf, "DROP_SA") == 0) {
5720 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
5721 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
5722 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
5723 reply_len = -1;
5724 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
5725 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
5726 reply_len = -1;
5727 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
5728 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
5729 reply_len = -1;
5730 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
5731 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
5732 buf + 17))
5733 reply_len = -1;
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07005734 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
5735 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
5736 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005737#ifdef CONFIG_TDLS
5738 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
5739 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
5740 reply_len = -1;
5741 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
5742 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
5743 reply_len = -1;
5744 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
5745 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
5746 reply_len = -1;
5747#endif /* CONFIG_TDLS */
5748 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
5749 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
5750 reply_size);
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005751 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
5752 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
5753 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005754#ifdef CONFIG_AUTOSCAN
5755 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
5756 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
5757 reply_len = -1;
5758#endif /* CONFIG_AUTOSCAN */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005759#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005760 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
5761 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
5762 reply_size);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005763#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005764 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005765 pmksa_cache_clear_current(wpa_s->wpa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005766 eapol_sm_request_reauth(wpa_s->eapol);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005767#ifdef CONFIG_WNM
5768 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
5769 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
5770 reply_len = -1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005771 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
5772 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
5773 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005774#endif /* CONFIG_WNM */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005775 } else if (os_strcmp(buf, "FLUSH") == 0) {
5776 wpa_supplicant_ctrl_iface_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005777 } else {
5778 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
5779 reply_len = 16;
5780 }
5781
5782 if (reply_len < 0) {
5783 os_memcpy(reply, "FAIL\n", 5);
5784 reply_len = 5;
5785 }
5786
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005787 *resp_len = reply_len;
5788 return reply;
5789}
5790
5791
5792static int wpa_supplicant_global_iface_add(struct wpa_global *global,
5793 char *cmd)
5794{
5795 struct wpa_interface iface;
5796 char *pos;
5797
5798 /*
5799 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
5800 * TAB<bridge_ifname>
5801 */
5802 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
5803
5804 os_memset(&iface, 0, sizeof(iface));
5805
5806 do {
5807 iface.ifname = pos = cmd;
5808 pos = os_strchr(pos, '\t');
5809 if (pos)
5810 *pos++ = '\0';
5811 if (iface.ifname[0] == '\0')
5812 return -1;
5813 if (pos == NULL)
5814 break;
5815
5816 iface.confname = pos;
5817 pos = os_strchr(pos, '\t');
5818 if (pos)
5819 *pos++ = '\0';
5820 if (iface.confname[0] == '\0')
5821 iface.confname = NULL;
5822 if (pos == NULL)
5823 break;
5824
5825 iface.driver = pos;
5826 pos = os_strchr(pos, '\t');
5827 if (pos)
5828 *pos++ = '\0';
5829 if (iface.driver[0] == '\0')
5830 iface.driver = NULL;
5831 if (pos == NULL)
5832 break;
5833
5834 iface.ctrl_interface = pos;
5835 pos = os_strchr(pos, '\t');
5836 if (pos)
5837 *pos++ = '\0';
5838 if (iface.ctrl_interface[0] == '\0')
5839 iface.ctrl_interface = NULL;
5840 if (pos == NULL)
5841 break;
5842
5843 iface.driver_param = pos;
5844 pos = os_strchr(pos, '\t');
5845 if (pos)
5846 *pos++ = '\0';
5847 if (iface.driver_param[0] == '\0')
5848 iface.driver_param = NULL;
5849 if (pos == NULL)
5850 break;
5851
5852 iface.bridge_ifname = pos;
5853 pos = os_strchr(pos, '\t');
5854 if (pos)
5855 *pos++ = '\0';
5856 if (iface.bridge_ifname[0] == '\0')
5857 iface.bridge_ifname = NULL;
5858 if (pos == NULL)
5859 break;
5860 } while (0);
5861
5862 if (wpa_supplicant_get_iface(global, iface.ifname))
5863 return -1;
5864
5865 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
5866}
5867
5868
5869static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
5870 char *cmd)
5871{
5872 struct wpa_supplicant *wpa_s;
5873
5874 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
5875
5876 wpa_s = wpa_supplicant_get_iface(global, cmd);
5877 if (wpa_s == NULL)
5878 return -1;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07005879 return wpa_supplicant_remove_iface(global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005880}
5881
5882
5883static void wpa_free_iface_info(struct wpa_interface_info *iface)
5884{
5885 struct wpa_interface_info *prev;
5886
5887 while (iface) {
5888 prev = iface;
5889 iface = iface->next;
5890
5891 os_free(prev->ifname);
5892 os_free(prev->desc);
5893 os_free(prev);
5894 }
5895}
5896
5897
5898static int wpa_supplicant_global_iface_list(struct wpa_global *global,
5899 char *buf, int len)
5900{
5901 int i, res;
5902 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
5903 char *pos, *end;
5904
5905 for (i = 0; wpa_drivers[i]; i++) {
5906 struct wpa_driver_ops *drv = wpa_drivers[i];
5907 if (drv->get_interfaces == NULL)
5908 continue;
5909 tmp = drv->get_interfaces(global->drv_priv[i]);
5910 if (tmp == NULL)
5911 continue;
5912
5913 if (last == NULL)
5914 iface = last = tmp;
5915 else
5916 last->next = tmp;
5917 while (last->next)
5918 last = last->next;
5919 }
5920
5921 pos = buf;
5922 end = buf + len;
5923 for (tmp = iface; tmp; tmp = tmp->next) {
5924 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
5925 tmp->drv_name, tmp->ifname,
5926 tmp->desc ? tmp->desc : "");
5927 if (res < 0 || res >= end - pos) {
5928 *pos = '\0';
5929 break;
5930 }
5931 pos += res;
5932 }
5933
5934 wpa_free_iface_info(iface);
5935
5936 return pos - buf;
5937}
5938
5939
5940static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
5941 char *buf, int len)
5942{
5943 int res;
5944 char *pos, *end;
5945 struct wpa_supplicant *wpa_s;
5946
5947 wpa_s = global->ifaces;
5948 pos = buf;
5949 end = buf + len;
5950
5951 while (wpa_s) {
5952 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
5953 if (res < 0 || res >= end - pos) {
5954 *pos = '\0';
5955 break;
5956 }
5957 pos += res;
5958 wpa_s = wpa_s->next;
5959 }
5960 return pos - buf;
5961}
5962
5963
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005964static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
5965 const char *ifname,
5966 char *cmd, size_t *resp_len)
5967{
5968 struct wpa_supplicant *wpa_s;
5969
5970 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5971 if (os_strcmp(ifname, wpa_s->ifname) == 0)
5972 break;
5973 }
5974
5975 if (wpa_s == NULL) {
5976 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
5977 if (resp)
5978 *resp_len = os_strlen(resp);
5979 else
5980 *resp_len = 1;
5981 return resp;
5982 }
5983
5984 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
5985}
5986
5987
5988static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
5989 char *buf, size_t *resp_len)
5990{
5991#ifdef CONFIG_P2P
5992 static const char * cmd[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07005993#ifdef ANDROID_P2P
5994 "LIST_NETWORKS",
5995 "SAVE_CONFIG",
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07005996#endif
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005997 "P2P_FIND",
5998 "P2P_STOP_FIND",
5999 "P2P_LISTEN",
6000 "P2P_GROUP_ADD",
6001 "P2P_GET_PASSPHRASE",
6002 "P2P_SERVICE_UPDATE",
6003 "P2P_SERVICE_FLUSH",
6004 "P2P_FLUSH",
6005 "P2P_CANCEL",
6006 "P2P_PRESENCE_REQ",
6007 "P2P_EXT_LISTEN",
6008 NULL
6009 };
6010 static const char * prefix[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07006011#ifdef ANDROID_P2P
6012 "DRIVER ",
6013 "GET_NETWORK ",
6014 "REMOVE_NETWORK ",
6015 "SET ",
6016#endif
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006017 "P2P_FIND ",
6018 "P2P_CONNECT ",
6019 "P2P_LISTEN ",
6020 "P2P_GROUP_REMOVE ",
6021 "P2P_GROUP_ADD ",
6022 "P2P_PROV_DISC ",
6023 "P2P_SERV_DISC_REQ ",
6024 "P2P_SERV_DISC_CANCEL_REQ ",
6025 "P2P_SERV_DISC_RESP ",
6026 "P2P_SERV_DISC_EXTERNAL ",
6027 "P2P_SERVICE_ADD ",
6028 "P2P_SERVICE_DEL ",
6029 "P2P_REJECT ",
6030 "P2P_INVITE ",
6031 "P2P_PEER ",
6032 "P2P_SET ",
6033 "P2P_UNAUTHORIZE ",
6034 "P2P_PRESENCE_REQ ",
6035 "P2P_EXT_LISTEN ",
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006036 "P2P_REMOVE_CLIENT ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006037 NULL
6038 };
6039 int found = 0;
6040 int i;
6041
6042 if (global->p2p_init_wpa_s == NULL)
6043 return NULL;
6044
6045 for (i = 0; !found && cmd[i]; i++) {
6046 if (os_strcmp(buf, cmd[i]) == 0)
6047 found = 1;
6048 }
6049
6050 for (i = 0; !found && prefix[i]; i++) {
6051 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
6052 found = 1;
6053 }
6054
6055 if (found)
6056 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6057 buf, resp_len);
6058#endif /* CONFIG_P2P */
6059 return NULL;
6060}
6061
6062
6063static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
6064 char *buf, size_t *resp_len)
6065{
6066#ifdef CONFIG_WIFI_DISPLAY
6067 if (global->p2p_init_wpa_s == NULL)
6068 return NULL;
6069 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
6070 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
6071 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6072 buf, resp_len);
6073#endif /* CONFIG_WIFI_DISPLAY */
6074 return NULL;
6075}
6076
6077
6078static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
6079 char *buf, size_t *resp_len)
6080{
6081 char *ret;
6082
6083 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
6084 if (ret)
6085 return ret;
6086
6087 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
6088 if (ret)
6089 return ret;
6090
6091 return NULL;
6092}
6093
6094
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006095static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
6096{
6097 char *value;
6098
6099 value = os_strchr(cmd, ' ');
6100 if (value == NULL)
6101 return -1;
6102 *value++ = '\0';
6103
6104 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
6105
6106#ifdef CONFIG_WIFI_DISPLAY
6107 if (os_strcasecmp(cmd, "wifi_display") == 0) {
6108 wifi_display_enable(global, !!atoi(value));
6109 return 0;
6110 }
6111#endif /* CONFIG_WIFI_DISPLAY */
6112
6113 return -1;
6114}
6115
6116
6117#ifndef CONFIG_NO_CONFIG_WRITE
6118static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
6119{
6120 int ret = 0;
6121 struct wpa_supplicant *wpa_s;
6122
6123 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6124 if (!wpa_s->conf->update_config) {
6125 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
6126 continue;
6127 }
6128
6129 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
6130 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
6131 ret = 1;
6132 } else {
6133 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
6134 }
6135 }
6136
6137 return ret;
6138}
6139#endif /* CONFIG_NO_CONFIG_WRITE */
6140
6141
6142static int wpas_global_ctrl_iface_status(struct wpa_global *global,
6143 char *buf, size_t buflen)
6144{
6145 char *pos, *end;
6146 int ret;
6147 struct wpa_supplicant *wpa_s;
6148
6149 pos = buf;
6150 end = buf + buflen;
6151
6152#ifdef CONFIG_P2P
6153 if (global->p2p && !global->p2p_disabled) {
6154 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
6155 "\n"
6156 "p2p_state=%s\n",
6157 MAC2STR(global->p2p_dev_addr),
6158 p2p_get_state_txt(global->p2p));
6159 if (ret < 0 || ret >= end - pos)
6160 return pos - buf;
6161 pos += ret;
6162 } else if (global->p2p) {
6163 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
6164 if (ret < 0 || ret >= end - pos)
6165 return pos - buf;
6166 pos += ret;
6167 }
6168#endif /* CONFIG_P2P */
6169
6170#ifdef CONFIG_WIFI_DISPLAY
6171 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
6172 !!global->wifi_display);
6173 if (ret < 0 || ret >= end - pos)
6174 return pos - buf;
6175 pos += ret;
6176#endif /* CONFIG_WIFI_DISPLAY */
6177
6178 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6179 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
6180 "address=" MACSTR "\n",
6181 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
6182 if (ret < 0 || ret >= end - pos)
6183 return pos - buf;
6184 pos += ret;
6185 }
6186
6187 return pos - buf;
6188}
6189
6190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006191char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
6192 char *buf, size_t *resp_len)
6193{
6194 char *reply;
6195 const int reply_size = 2048;
6196 int reply_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006197 int level = MSG_DEBUG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006198
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006199 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
6200 char *pos = os_strchr(buf + 7, ' ');
6201 if (pos) {
6202 *pos++ = '\0';
6203 return wpas_global_ctrl_iface_ifname(global,
6204 buf + 7, pos,
6205 resp_len);
6206 }
6207 }
6208
6209 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
6210 if (reply)
6211 return reply;
6212
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006213 if (os_strcmp(buf, "PING") == 0)
6214 level = MSG_EXCESSIVE;
6215 wpa_hexdump_ascii(level, "RX global ctrl_iface",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006216 (const u8 *) buf, os_strlen(buf));
6217
6218 reply = os_malloc(reply_size);
6219 if (reply == NULL) {
6220 *resp_len = 1;
6221 return NULL;
6222 }
6223
6224 os_memcpy(reply, "OK\n", 3);
6225 reply_len = 3;
6226
6227 if (os_strcmp(buf, "PING") == 0) {
6228 os_memcpy(reply, "PONG\n", 5);
6229 reply_len = 5;
6230 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
6231 if (wpa_supplicant_global_iface_add(global, buf + 14))
6232 reply_len = -1;
6233 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
6234 if (wpa_supplicant_global_iface_remove(global, buf + 17))
6235 reply_len = -1;
6236 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
6237 reply_len = wpa_supplicant_global_iface_list(
6238 global, reply, reply_size);
6239 } else if (os_strcmp(buf, "INTERFACES") == 0) {
6240 reply_len = wpa_supplicant_global_iface_interfaces(
6241 global, reply, reply_size);
6242 } else if (os_strcmp(buf, "TERMINATE") == 0) {
6243 wpa_supplicant_terminate_proc(global);
6244 } else if (os_strcmp(buf, "SUSPEND") == 0) {
6245 wpas_notify_suspend(global);
6246 } else if (os_strcmp(buf, "RESUME") == 0) {
6247 wpas_notify_resume(global);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006248 } else if (os_strncmp(buf, "SET ", 4) == 0) {
6249 if (wpas_global_ctrl_iface_set(global, buf + 4))
6250 reply_len = -1;
6251#ifndef CONFIG_NO_CONFIG_WRITE
6252 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
6253 if (wpas_global_ctrl_iface_save_config(global))
6254 reply_len = -1;
6255#endif /* CONFIG_NO_CONFIG_WRITE */
6256 } else if (os_strcmp(buf, "STATUS") == 0) {
6257 reply_len = wpas_global_ctrl_iface_status(global, reply,
6258 reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006259 } else {
6260 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
6261 reply_len = 16;
6262 }
6263
6264 if (reply_len < 0) {
6265 os_memcpy(reply, "FAIL\n", 5);
6266 reply_len = 5;
6267 }
6268
6269 *resp_len = reply_len;
6270 return reply;
6271}