blob: 4aa67f77060e5fd9fafd781c10477e77117b2c63 [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) {
1568 if (wpa_s->current_ssid->parent_cred != cred)
1569 continue;
1570 if (!cred->domain)
1571 continue;
1572
1573 ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
1574 cred->domain);
1575 if (ret < 0 || ret >= end - pos)
1576 return pos - buf;
1577 pos += ret;
1578
1579 if (wpa_s->current_bss == NULL ||
1580 wpa_s->current_bss->anqp == NULL)
1581 res = -1;
1582 else
1583 res = interworking_home_sp_cred(
1584 wpa_s, cred,
1585 wpa_s->current_bss->anqp->domain_name);
1586 if (res > 0)
1587 type = "home";
1588 else if (res == 0)
1589 type = "roaming";
1590 else
1591 type = "unknown";
1592
1593 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
1594 if (ret < 0 || ret >= end - pos)
1595 return pos - buf;
1596 pos += ret;
1597
1598 break;
1599 }
1600 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001601#endif /* CONFIG_HS20 */
1602
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001603 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1604 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1605 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1606 verbose);
1607 if (res >= 0)
1608 pos += res;
1609 }
1610
1611 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1612 if (res >= 0)
1613 pos += res;
1614
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001615#ifdef ANDROID
1616 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
Irfan Sherifff20a4432012-04-16 16:48:34 -07001617 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001618 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
Irfan Sherifff20a4432012-04-16 16:48:34 -07001619 wpa_s->wpa_state,
Irfan Sheriffe78e7672012-08-01 11:10:15 -07001620 MAC2STR(wpa_s->bssid),
andy2_kuo5b5fb022012-05-22 11:53:07 -07001621 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1622 wpa_ssid_txt(wpa_s->current_ssid->ssid,
Irfan Sheriff10294772012-05-11 11:23:35 -07001623 wpa_s->current_ssid->ssid_len) : "");
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001624 if (wpa_s->wpa_state == WPA_COMPLETED) {
1625 struct wpa_ssid *ssid = wpa_s->current_ssid;
1626 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- connection to "
1627 MACSTR " completed %s [id=%d id_str=%s]",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001628 MAC2STR(wpa_s->bssid), "(auth)",
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001629 ssid ? ssid->id : -1,
1630 ssid && ssid->id_str ? ssid->id_str : "");
1631 }
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001632#endif /* ANDROID */
1633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 return pos - buf;
1635}
1636
1637
1638static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1639 char *cmd)
1640{
1641 char *pos;
1642 int id;
1643 struct wpa_ssid *ssid;
1644 u8 bssid[ETH_ALEN];
1645
1646 /* cmd: "<network id> <BSSID>" */
1647 pos = os_strchr(cmd, ' ');
1648 if (pos == NULL)
1649 return -1;
1650 *pos++ = '\0';
1651 id = atoi(cmd);
1652 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1653 if (hwaddr_aton(pos, bssid)) {
1654 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1655 return -1;
1656 }
1657
1658 ssid = wpa_config_get_network(wpa_s->conf, id);
1659 if (ssid == NULL) {
1660 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1661 "to update", id);
1662 return -1;
1663 }
1664
1665 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1666 ssid->bssid_set = !is_zero_ether_addr(bssid);
1667
1668 return 0;
1669}
1670
1671
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001672static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001673 char *cmd, char *buf,
1674 size_t buflen)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001675{
1676 u8 bssid[ETH_ALEN];
1677 struct wpa_blacklist *e;
1678 char *pos, *end;
1679 int ret;
1680
1681 /* cmd: "BLACKLIST [<BSSID>]" */
1682 if (*cmd == '\0') {
1683 pos = buf;
1684 end = buf + buflen;
1685 e = wpa_s->blacklist;
1686 while (e) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001687 ret = os_snprintf(pos, end - pos, MACSTR "\n",
1688 MAC2STR(e->bssid));
1689 if (ret < 0 || ret >= end - pos)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001690 return pos - buf;
1691 pos += ret;
1692 e = e->next;
1693 }
1694 return pos - buf;
1695 }
1696
1697 cmd++;
1698 if (os_strncmp(cmd, "clear", 5) == 0) {
1699 wpa_blacklist_clear(wpa_s);
1700 os_memcpy(buf, "OK\n", 3);
1701 return 3;
1702 }
1703
1704 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1705 if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001706 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001707 return -1;
1708 }
1709
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001710 /*
1711 * Add the BSSID twice, so its count will be 2, causing it to be
1712 * skipped when processing scan results.
1713 */
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001714 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001715 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001716 return -1;
1717 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 os_memcpy(buf, "OK\n", 3);
1721 return 3;
1722}
1723
1724
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001725extern int wpa_debug_level;
1726extern int wpa_debug_timestamp;
1727
1728static const char * debug_level_str(int level)
1729{
1730 switch (level) {
1731 case MSG_EXCESSIVE:
1732 return "EXCESSIVE";
1733 case MSG_MSGDUMP:
1734 return "MSGDUMP";
1735 case MSG_DEBUG:
1736 return "DEBUG";
1737 case MSG_INFO:
1738 return "INFO";
1739 case MSG_WARNING:
1740 return "WARNING";
1741 case MSG_ERROR:
1742 return "ERROR";
1743 default:
1744 return "?";
1745 }
1746}
1747
1748
1749static int str_to_debug_level(const char *s)
1750{
1751 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1752 return MSG_EXCESSIVE;
1753 if (os_strcasecmp(s, "MSGDUMP") == 0)
1754 return MSG_MSGDUMP;
1755 if (os_strcasecmp(s, "DEBUG") == 0)
1756 return MSG_DEBUG;
1757 if (os_strcasecmp(s, "INFO") == 0)
1758 return MSG_INFO;
1759 if (os_strcasecmp(s, "WARNING") == 0)
1760 return MSG_WARNING;
1761 if (os_strcasecmp(s, "ERROR") == 0)
1762 return MSG_ERROR;
1763 return -1;
1764}
1765
1766
1767static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1768 char *cmd, char *buf,
1769 size_t buflen)
1770{
1771 char *pos, *end, *stamp;
1772 int ret;
1773
1774 if (cmd == NULL) {
1775 return -1;
1776 }
1777
1778 /* cmd: "LOG_LEVEL [<level>]" */
1779 if (*cmd == '\0') {
1780 pos = buf;
1781 end = buf + buflen;
1782 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1783 "Timestamp: %d\n",
1784 debug_level_str(wpa_debug_level),
1785 wpa_debug_timestamp);
1786 if (ret < 0 || ret >= end - pos)
1787 ret = 0;
1788
1789 return ret;
1790 }
1791
1792 while (*cmd == ' ')
1793 cmd++;
1794
1795 stamp = os_strchr(cmd, ' ');
1796 if (stamp) {
1797 *stamp++ = '\0';
1798 while (*stamp == ' ') {
1799 stamp++;
1800 }
1801 }
1802
1803 if (cmd && os_strlen(cmd)) {
1804 int level = str_to_debug_level(cmd);
1805 if (level < 0)
1806 return -1;
1807 wpa_debug_level = level;
1808 }
1809
1810 if (stamp && os_strlen(stamp))
1811 wpa_debug_timestamp = atoi(stamp);
1812
1813 os_memcpy(buf, "OK\n", 3);
1814 return 3;
1815}
1816
1817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818static int wpa_supplicant_ctrl_iface_list_networks(
1819 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1820{
1821 char *pos, *end;
1822 struct wpa_ssid *ssid;
1823 int ret;
1824
1825 pos = buf;
1826 end = buf + buflen;
1827 ret = os_snprintf(pos, end - pos,
1828 "network id / ssid / bssid / flags\n");
1829 if (ret < 0 || ret >= end - pos)
1830 return pos - buf;
1831 pos += ret;
1832
1833 ssid = wpa_s->conf->ssid;
1834 while (ssid) {
1835 ret = os_snprintf(pos, end - pos, "%d\t%s",
1836 ssid->id,
1837 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1838 if (ret < 0 || ret >= end - pos)
1839 return pos - buf;
1840 pos += ret;
1841 if (ssid->bssid_set) {
1842 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
1843 MAC2STR(ssid->bssid));
1844 } else {
1845 ret = os_snprintf(pos, end - pos, "\tany");
1846 }
1847 if (ret < 0 || ret >= end - pos)
1848 return pos - buf;
1849 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001850 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001851 ssid == wpa_s->current_ssid ?
1852 "[CURRENT]" : "",
1853 ssid->disabled ? "[DISABLED]" : "",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001854 ssid->disabled_until.sec ?
1855 "[TEMP-DISABLED]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
1857 "");
1858 if (ret < 0 || ret >= end - pos)
1859 return pos - buf;
1860 pos += ret;
1861 ret = os_snprintf(pos, end - pos, "\n");
1862 if (ret < 0 || ret >= end - pos)
1863 return pos - buf;
1864 pos += ret;
1865
1866 ssid = ssid->next;
1867 }
1868
1869 return pos - buf;
1870}
1871
1872
1873static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
1874{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001875 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 ret = os_snprintf(pos, end - pos, "-");
1877 if (ret < 0 || ret >= end - pos)
1878 return pos;
1879 pos += ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001880 ret = wpa_write_ciphers(pos, end, cipher, "+");
1881 if (ret < 0)
1882 return pos;
1883 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884 return pos;
1885}
1886
1887
1888static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
1889 const u8 *ie, size_t ie_len)
1890{
1891 struct wpa_ie_data data;
1892 int first, ret;
1893
1894 ret = os_snprintf(pos, end - pos, "[%s-", proto);
1895 if (ret < 0 || ret >= end - pos)
1896 return pos;
1897 pos += ret;
1898
1899 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
1900 ret = os_snprintf(pos, end - pos, "?]");
1901 if (ret < 0 || ret >= end - pos)
1902 return pos;
1903 pos += ret;
1904 return pos;
1905 }
1906
1907 first = 1;
1908 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1909 ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
1910 if (ret < 0 || ret >= end - pos)
1911 return pos;
1912 pos += ret;
1913 first = 0;
1914 }
1915 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
1916 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
1917 if (ret < 0 || ret >= end - pos)
1918 return pos;
1919 pos += ret;
1920 first = 0;
1921 }
1922 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
1923 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
1924 if (ret < 0 || ret >= end - pos)
1925 return pos;
1926 pos += ret;
1927 first = 0;
1928 }
1929#ifdef CONFIG_IEEE80211R
1930 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1931 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
1932 first ? "" : "+");
1933 if (ret < 0 || ret >= end - pos)
1934 return pos;
1935 pos += ret;
1936 first = 0;
1937 }
1938 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1939 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
1940 first ? "" : "+");
1941 if (ret < 0 || ret >= end - pos)
1942 return pos;
1943 pos += ret;
1944 first = 0;
1945 }
1946#endif /* CONFIG_IEEE80211R */
1947#ifdef CONFIG_IEEE80211W
1948 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1949 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
1950 first ? "" : "+");
1951 if (ret < 0 || ret >= end - pos)
1952 return pos;
1953 pos += ret;
1954 first = 0;
1955 }
1956 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1957 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
1958 first ? "" : "+");
1959 if (ret < 0 || ret >= end - pos)
1960 return pos;
1961 pos += ret;
1962 first = 0;
1963 }
1964#endif /* CONFIG_IEEE80211W */
1965
1966 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
1967
1968 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
1969 ret = os_snprintf(pos, end - pos, "-preauth");
1970 if (ret < 0 || ret >= end - pos)
1971 return pos;
1972 pos += ret;
1973 }
1974
1975 ret = os_snprintf(pos, end - pos, "]");
1976 if (ret < 0 || ret >= end - pos)
1977 return pos;
1978 pos += ret;
1979
1980 return pos;
1981}
1982
1983
1984#ifdef CONFIG_WPS
1985static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
1986 char *pos, char *end,
1987 struct wpabuf *wps_ie)
1988{
1989 int ret;
1990 const char *txt;
1991
1992 if (wps_ie == NULL)
1993 return pos;
1994 if (wps_is_selected_pbc_registrar(wps_ie))
1995 txt = "[WPS-PBC]";
1996#ifdef CONFIG_WPS2
1997 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
1998 txt = "[WPS-AUTH]";
1999#endif /* CONFIG_WPS2 */
2000 else if (wps_is_selected_pin_registrar(wps_ie))
2001 txt = "[WPS-PIN]";
2002 else
2003 txt = "[WPS]";
2004
2005 ret = os_snprintf(pos, end - pos, "%s", txt);
2006 if (ret >= 0 && ret < end - pos)
2007 pos += ret;
2008 wpabuf_free(wps_ie);
2009 return pos;
2010}
2011#endif /* CONFIG_WPS */
2012
2013
2014static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2015 char *pos, char *end,
2016 const struct wpa_bss *bss)
2017{
2018#ifdef CONFIG_WPS
2019 struct wpabuf *wps_ie;
2020 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2021 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2022#else /* CONFIG_WPS */
2023 return pos;
2024#endif /* CONFIG_WPS */
2025}
2026
2027
2028/* Format one result on one text line into a buffer. */
2029static int wpa_supplicant_ctrl_iface_scan_result(
2030 struct wpa_supplicant *wpa_s,
2031 const struct wpa_bss *bss, char *buf, size_t buflen)
2032{
2033 char *pos, *end;
2034 int ret;
2035 const u8 *ie, *ie2, *p2p;
2036
2037 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07002038 if (!p2p)
2039 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2041 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2042 0)
2043 return 0; /* Do not show P2P listen discovery results here */
2044
2045 pos = buf;
2046 end = buf + buflen;
2047
2048 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2049 MAC2STR(bss->bssid), bss->freq, bss->level);
2050 if (ret < 0 || ret >= end - pos)
2051 return -1;
2052 pos += ret;
2053 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2054 if (ie)
2055 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2056 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2057 if (ie2)
2058 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
2059 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2060 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2061 ret = os_snprintf(pos, end - pos, "[WEP]");
2062 if (ret < 0 || ret >= end - pos)
2063 return -1;
2064 pos += ret;
2065 }
2066 if (bss->caps & IEEE80211_CAP_IBSS) {
2067 ret = os_snprintf(pos, end - pos, "[IBSS]");
2068 if (ret < 0 || ret >= end - pos)
2069 return -1;
2070 pos += ret;
2071 }
2072 if (bss->caps & IEEE80211_CAP_ESS) {
2073 ret = os_snprintf(pos, end - pos, "[ESS]");
2074 if (ret < 0 || ret >= end - pos)
2075 return -1;
2076 pos += ret;
2077 }
2078 if (p2p) {
2079 ret = os_snprintf(pos, end - pos, "[P2P]");
2080 if (ret < 0 || ret >= end - pos)
2081 return -1;
2082 pos += ret;
2083 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002084#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002085 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002086 ret = os_snprintf(pos, end - pos, "[HS20]");
2087 if (ret < 0 || ret >= end - pos)
2088 return -1;
2089 pos += ret;
2090 }
2091#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002092
2093 ret = os_snprintf(pos, end - pos, "\t%s",
2094 wpa_ssid_txt(bss->ssid, bss->ssid_len));
2095 if (ret < 0 || ret >= end - pos)
2096 return -1;
2097 pos += ret;
2098
2099 ret = os_snprintf(pos, end - pos, "\n");
2100 if (ret < 0 || ret >= end - pos)
2101 return -1;
2102 pos += ret;
2103
2104 return pos - buf;
2105}
2106
2107
2108static int wpa_supplicant_ctrl_iface_scan_results(
2109 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2110{
2111 char *pos, *end;
2112 struct wpa_bss *bss;
2113 int ret;
2114
2115 pos = buf;
2116 end = buf + buflen;
2117 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2118 "flags / ssid\n");
2119 if (ret < 0 || ret >= end - pos)
2120 return pos - buf;
2121 pos += ret;
2122
2123 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2124 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2125 end - pos);
2126 if (ret < 0 || ret >= end - pos)
2127 return pos - buf;
2128 pos += ret;
2129 }
2130
2131 return pos - buf;
2132}
2133
2134
2135static int wpa_supplicant_ctrl_iface_select_network(
2136 struct wpa_supplicant *wpa_s, char *cmd)
2137{
2138 int id;
2139 struct wpa_ssid *ssid;
2140
2141 /* cmd: "<network id>" or "any" */
2142 if (os_strcmp(cmd, "any") == 0) {
2143 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2144 ssid = NULL;
2145 } else {
2146 id = atoi(cmd);
2147 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2148
2149 ssid = wpa_config_get_network(wpa_s->conf, id);
2150 if (ssid == NULL) {
2151 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2152 "network id=%d", id);
2153 return -1;
2154 }
2155 if (ssid->disabled == 2) {
2156 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2157 "SELECT_NETWORK with persistent P2P group");
2158 return -1;
2159 }
2160 }
2161
2162 wpa_supplicant_select_network(wpa_s, ssid);
2163
2164 return 0;
2165}
2166
2167
2168static int wpa_supplicant_ctrl_iface_enable_network(
2169 struct wpa_supplicant *wpa_s, char *cmd)
2170{
2171 int id;
2172 struct wpa_ssid *ssid;
2173
2174 /* cmd: "<network id>" or "all" */
2175 if (os_strcmp(cmd, "all") == 0) {
2176 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2177 ssid = NULL;
2178 } else {
2179 id = atoi(cmd);
2180 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2181
2182 ssid = wpa_config_get_network(wpa_s->conf, id);
2183 if (ssid == NULL) {
2184 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2185 "network id=%d", id);
2186 return -1;
2187 }
2188 if (ssid->disabled == 2) {
2189 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2190 "ENABLE_NETWORK with persistent P2P group");
2191 return -1;
2192 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002193
2194 if (os_strstr(cmd, " no-connect")) {
2195 ssid->disabled = 0;
2196 return 0;
2197 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002198 }
2199 wpa_supplicant_enable_network(wpa_s, ssid);
2200
2201 return 0;
2202}
2203
2204
2205static int wpa_supplicant_ctrl_iface_disable_network(
2206 struct wpa_supplicant *wpa_s, char *cmd)
2207{
2208 int id;
2209 struct wpa_ssid *ssid;
2210
2211 /* cmd: "<network id>" or "all" */
2212 if (os_strcmp(cmd, "all") == 0) {
2213 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2214 ssid = NULL;
2215 } else {
2216 id = atoi(cmd);
2217 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2218
2219 ssid = wpa_config_get_network(wpa_s->conf, id);
2220 if (ssid == NULL) {
2221 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2222 "network id=%d", id);
2223 return -1;
2224 }
2225 if (ssid->disabled == 2) {
2226 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2227 "DISABLE_NETWORK with persistent P2P "
2228 "group");
2229 return -1;
2230 }
2231 }
2232 wpa_supplicant_disable_network(wpa_s, ssid);
2233
2234 return 0;
2235}
2236
2237
2238static int wpa_supplicant_ctrl_iface_add_network(
2239 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2240{
2241 struct wpa_ssid *ssid;
2242 int ret;
2243
2244 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2245
2246 ssid = wpa_config_add_network(wpa_s->conf);
2247 if (ssid == NULL)
2248 return -1;
2249
2250 wpas_notify_network_added(wpa_s, ssid);
2251
2252 ssid->disabled = 1;
2253 wpa_config_set_network_defaults(ssid);
2254
2255 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
2256 if (ret < 0 || (size_t) ret >= buflen)
2257 return -1;
2258 return ret;
2259}
2260
2261
2262static int wpa_supplicant_ctrl_iface_remove_network(
2263 struct wpa_supplicant *wpa_s, char *cmd)
2264{
2265 int id;
2266 struct wpa_ssid *ssid;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002267 int was_disabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002268
2269 /* cmd: "<network id>" or "all" */
2270 if (os_strcmp(cmd, "all") == 0) {
2271 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002272 if (wpa_s->sched_scanning)
2273 wpa_supplicant_cancel_sched_scan(wpa_s);
2274
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002275 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002276 if (wpa_s->current_ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002277#ifdef CONFIG_SME
2278 wpa_s->sme.prev_bssid_set = 0;
2279#endif /* CONFIG_SME */
Jouni Malinen75ecf522011-06-27 15:19:46 -07002280 wpa_sm_set_config(wpa_s->wpa, NULL);
2281 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002282 wpa_supplicant_deauthenticate(
2283 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002284 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002285 ssid = wpa_s->conf->ssid;
2286 while (ssid) {
2287 struct wpa_ssid *remove_ssid = ssid;
2288 id = ssid->id;
2289 ssid = ssid->next;
2290 wpas_notify_network_removed(wpa_s, remove_ssid);
2291 wpa_config_remove_network(wpa_s->conf, id);
2292 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293 return 0;
2294 }
2295
2296 id = atoi(cmd);
2297 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2298
2299 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002300 if (ssid)
2301 wpas_notify_network_removed(wpa_s, ssid);
Deepthi Gowria831d782012-09-03 11:55:38 +03002302 if (ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2304 "id=%d", id);
2305 return -1;
2306 }
2307
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002308 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002309#ifdef CONFIG_SME
2310 wpa_s->sme.prev_bssid_set = 0;
2311#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002312 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002313 * Invalidate the EAP session cache if the current or
2314 * previously used network is removed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315 */
2316 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002317 }
2318
2319 if (ssid == wpa_s->current_ssid) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002320 wpa_sm_set_config(wpa_s->wpa, NULL);
2321 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002322
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002323 wpa_supplicant_deauthenticate(wpa_s,
2324 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325 }
2326
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002327 was_disabled = ssid->disabled;
2328
Deepthi Gowria831d782012-09-03 11:55:38 +03002329 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2330 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2331 "network id=%d", id);
2332 return -1;
2333 }
2334
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002335 if (!was_disabled && wpa_s->sched_scanning) {
2336 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2337 "network from filters");
2338 wpa_supplicant_cancel_sched_scan(wpa_s);
2339 wpa_supplicant_req_scan(wpa_s, 0, 0);
2340 }
2341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 return 0;
2343}
2344
2345
2346static int wpa_supplicant_ctrl_iface_set_network(
2347 struct wpa_supplicant *wpa_s, char *cmd)
2348{
2349 int id;
2350 struct wpa_ssid *ssid;
2351 char *name, *value;
2352
2353 /* cmd: "<network id> <variable name> <value>" */
2354 name = os_strchr(cmd, ' ');
2355 if (name == NULL)
2356 return -1;
2357 *name++ = '\0';
2358
2359 value = os_strchr(name, ' ');
2360 if (value == NULL)
2361 return -1;
2362 *value++ = '\0';
2363
2364 id = atoi(cmd);
2365 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2366 id, name);
2367 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2368 (u8 *) value, os_strlen(value));
2369
2370 ssid = wpa_config_get_network(wpa_s->conf, id);
2371 if (ssid == NULL) {
2372 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2373 "id=%d", id);
2374 return -1;
2375 }
2376
2377 if (wpa_config_set(ssid, name, value, 0) < 0) {
2378 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2379 "variable '%s'", name);
2380 return -1;
2381 }
2382
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002383 if (os_strcmp(name, "bssid") != 0 &&
2384 os_strcmp(name, "priority") != 0)
2385 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002386
2387 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002388 /*
2389 * Invalidate the EAP session cache if anything in the current
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002390 * or previously used configuration changes.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002391 */
2392 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2393 }
2394
2395 if ((os_strcmp(name, "psk") == 0 &&
2396 value[0] == '"' && ssid->ssid_len) ||
2397 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2398 wpa_config_update_psk(ssid);
2399 else if (os_strcmp(name, "priority") == 0)
2400 wpa_config_update_prio_list(wpa_s->conf);
2401
2402 return 0;
2403}
2404
2405
2406static int wpa_supplicant_ctrl_iface_get_network(
2407 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2408{
2409 int id;
2410 size_t res;
2411 struct wpa_ssid *ssid;
2412 char *name, *value;
2413
2414 /* cmd: "<network id> <variable name>" */
2415 name = os_strchr(cmd, ' ');
2416 if (name == NULL || buflen == 0)
2417 return -1;
2418 *name++ = '\0';
2419
2420 id = atoi(cmd);
2421 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2422 id, name);
2423
2424 ssid = wpa_config_get_network(wpa_s->conf, id);
2425 if (ssid == NULL) {
2426 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2427 "id=%d", id);
2428 return -1;
2429 }
2430
2431 value = wpa_config_get_no_key(ssid, name);
2432 if (value == NULL) {
2433 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2434 "variable '%s'", name);
2435 return -1;
2436 }
2437
2438 res = os_strlcpy(buf, value, buflen);
2439 if (res >= buflen) {
2440 os_free(value);
2441 return -1;
2442 }
2443
2444 os_free(value);
2445
2446 return res;
2447}
2448
2449
Dmitry Shmidt04949592012-07-19 12:16:46 -07002450static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2451 char *buf, size_t buflen)
2452{
2453 char *pos, *end;
2454 struct wpa_cred *cred;
2455 int ret;
2456
2457 pos = buf;
2458 end = buf + buflen;
2459 ret = os_snprintf(pos, end - pos,
2460 "cred id / realm / username / domain / imsi\n");
2461 if (ret < 0 || ret >= end - pos)
2462 return pos - buf;
2463 pos += ret;
2464
2465 cred = wpa_s->conf->cred;
2466 while (cred) {
2467 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2468 cred->id, cred->realm ? cred->realm : "",
2469 cred->username ? cred->username : "",
2470 cred->domain ? cred->domain : "",
2471 cred->imsi ? cred->imsi : "");
2472 if (ret < 0 || ret >= end - pos)
2473 return pos - buf;
2474 pos += ret;
2475
2476 cred = cred->next;
2477 }
2478
2479 return pos - buf;
2480}
2481
2482
2483static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2484 char *buf, size_t buflen)
2485{
2486 struct wpa_cred *cred;
2487 int ret;
2488
2489 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2490
2491 cred = wpa_config_add_cred(wpa_s->conf);
2492 if (cred == NULL)
2493 return -1;
2494
2495 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2496 if (ret < 0 || (size_t) ret >= buflen)
2497 return -1;
2498 return ret;
2499}
2500
2501
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002502static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2503 struct wpa_cred *cred)
2504{
2505 struct wpa_ssid *ssid;
2506 char str[20];
2507
2508 if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
2509 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2510 return -1;
2511 }
2512
2513 /* Remove any network entry created based on the removed credential */
2514 ssid = wpa_s->conf->ssid;
2515 while (ssid) {
2516 if (ssid->parent_cred == cred) {
2517 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
2518 "used the removed credential", ssid->id);
2519 os_snprintf(str, sizeof(str), "%d", ssid->id);
2520 ssid = ssid->next;
2521 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
2522 } else
2523 ssid = ssid->next;
2524 }
2525
2526 return 0;
2527}
2528
2529
Dmitry Shmidt04949592012-07-19 12:16:46 -07002530static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2531 char *cmd)
2532{
2533 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002534 struct wpa_cred *cred, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002535
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002536 /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002537 if (os_strcmp(cmd, "all") == 0) {
2538 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2539 cred = wpa_s->conf->cred;
2540 while (cred) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002541 prev = cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002542 cred = cred->next;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002543 wpas_ctrl_remove_cred(wpa_s, prev);
2544 }
2545 return 0;
2546 }
2547
2548 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
2549 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
2550 cmd + 8);
2551 cred = wpa_s->conf->cred;
2552 while (cred) {
2553 prev = cred;
2554 cred = cred->next;
2555 if (prev->domain &&
2556 os_strcmp(prev->domain, cmd + 8) == 0)
2557 wpas_ctrl_remove_cred(wpa_s, prev);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002558 }
2559 return 0;
2560 }
2561
2562 id = atoi(cmd);
2563 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2564
2565 cred = wpa_config_get_cred(wpa_s->conf, id);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002566 return wpas_ctrl_remove_cred(wpa_s, cred);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002567}
2568
2569
2570static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2571 char *cmd)
2572{
2573 int id;
2574 struct wpa_cred *cred;
2575 char *name, *value;
2576
2577 /* cmd: "<cred id> <variable name> <value>" */
2578 name = os_strchr(cmd, ' ');
2579 if (name == NULL)
2580 return -1;
2581 *name++ = '\0';
2582
2583 value = os_strchr(name, ' ');
2584 if (value == NULL)
2585 return -1;
2586 *value++ = '\0';
2587
2588 id = atoi(cmd);
2589 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2590 id, name);
2591 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2592 (u8 *) value, os_strlen(value));
2593
2594 cred = wpa_config_get_cred(wpa_s->conf, id);
2595 if (cred == NULL) {
2596 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2597 id);
2598 return -1;
2599 }
2600
2601 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2602 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2603 "variable '%s'", name);
2604 return -1;
2605 }
2606
2607 return 0;
2608}
2609
2610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002611#ifndef CONFIG_NO_CONFIG_WRITE
2612static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2613{
2614 int ret;
2615
2616 if (!wpa_s->conf->update_config) {
2617 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2618 "to update configuration (update_config=0)");
2619 return -1;
2620 }
2621
2622 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2623 if (ret) {
2624 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2625 "update configuration");
2626 } else {
2627 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2628 " updated");
2629 }
2630
2631 return ret;
2632}
2633#endif /* CONFIG_NO_CONFIG_WRITE */
2634
2635
2636static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2637 struct wpa_driver_capa *capa,
2638 char *buf, size_t buflen)
2639{
2640 int ret, first = 1;
2641 char *pos, *end;
2642 size_t len;
2643
2644 pos = buf;
2645 end = pos + buflen;
2646
2647 if (res < 0) {
2648 if (strict)
2649 return 0;
2650 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2651 if (len >= buflen)
2652 return -1;
2653 return len;
2654 }
2655
2656 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2657 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2658 if (ret < 0 || ret >= end - pos)
2659 return pos - buf;
2660 pos += ret;
2661 first = 0;
2662 }
2663
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002664 if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2665 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
2666 if (ret < 0 || ret >= end - pos)
2667 return pos - buf;
2668 pos += ret;
2669 first = 0;
2670 }
2671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002672 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2673 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2674 if (ret < 0 || ret >= end - pos)
2675 return pos - buf;
2676 pos += ret;
2677 first = 0;
2678 }
2679
2680 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2681 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
2682 if (ret < 0 || ret >= end - pos)
2683 return pos - buf;
2684 pos += ret;
2685 first = 0;
2686 }
2687
2688 return pos - buf;
2689}
2690
2691
2692static int ctrl_iface_get_capability_group(int res, char *strict,
2693 struct wpa_driver_capa *capa,
2694 char *buf, size_t buflen)
2695{
2696 int ret, first = 1;
2697 char *pos, *end;
2698 size_t len;
2699
2700 pos = buf;
2701 end = pos + buflen;
2702
2703 if (res < 0) {
2704 if (strict)
2705 return 0;
2706 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
2707 if (len >= buflen)
2708 return -1;
2709 return len;
2710 }
2711
2712 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2713 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2714 if (ret < 0 || ret >= end - pos)
2715 return pos - buf;
2716 pos += ret;
2717 first = 0;
2718 }
2719
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002720 if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2721 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
2722 if (ret < 0 || ret >= end - pos)
2723 return pos - buf;
2724 pos += ret;
2725 first = 0;
2726 }
2727
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002728 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2729 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2730 if (ret < 0 || ret >= end - pos)
2731 return pos - buf;
2732 pos += ret;
2733 first = 0;
2734 }
2735
2736 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
2737 ret = os_snprintf(pos, end - pos, "%sWEP104",
2738 first ? "" : " ");
2739 if (ret < 0 || ret >= end - pos)
2740 return pos - buf;
2741 pos += ret;
2742 first = 0;
2743 }
2744
2745 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
2746 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
2747 if (ret < 0 || ret >= end - pos)
2748 return pos - buf;
2749 pos += ret;
2750 first = 0;
2751 }
2752
2753 return pos - buf;
2754}
2755
2756
2757static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
2758 struct wpa_driver_capa *capa,
2759 char *buf, size_t buflen)
2760{
2761 int ret;
2762 char *pos, *end;
2763 size_t len;
2764
2765 pos = buf;
2766 end = pos + buflen;
2767
2768 if (res < 0) {
2769 if (strict)
2770 return 0;
2771 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
2772 "NONE", buflen);
2773 if (len >= buflen)
2774 return -1;
2775 return len;
2776 }
2777
2778 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
2779 if (ret < 0 || ret >= end - pos)
2780 return pos - buf;
2781 pos += ret;
2782
2783 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2784 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
2785 ret = os_snprintf(pos, end - pos, " WPA-EAP");
2786 if (ret < 0 || ret >= end - pos)
2787 return pos - buf;
2788 pos += ret;
2789 }
2790
2791 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2792 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2793 ret = os_snprintf(pos, end - pos, " WPA-PSK");
2794 if (ret < 0 || ret >= end - pos)
2795 return pos - buf;
2796 pos += ret;
2797 }
2798
2799 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2800 ret = os_snprintf(pos, end - pos, " WPA-NONE");
2801 if (ret < 0 || ret >= end - pos)
2802 return pos - buf;
2803 pos += ret;
2804 }
2805
2806 return pos - buf;
2807}
2808
2809
2810static int ctrl_iface_get_capability_proto(int res, char *strict,
2811 struct wpa_driver_capa *capa,
2812 char *buf, size_t buflen)
2813{
2814 int ret, first = 1;
2815 char *pos, *end;
2816 size_t len;
2817
2818 pos = buf;
2819 end = pos + buflen;
2820
2821 if (res < 0) {
2822 if (strict)
2823 return 0;
2824 len = os_strlcpy(buf, "RSN WPA", buflen);
2825 if (len >= buflen)
2826 return -1;
2827 return len;
2828 }
2829
2830 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2831 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2832 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
2833 if (ret < 0 || ret >= end - pos)
2834 return pos - buf;
2835 pos += ret;
2836 first = 0;
2837 }
2838
2839 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2840 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
2841 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
2842 if (ret < 0 || ret >= end - pos)
2843 return pos - buf;
2844 pos += ret;
2845 first = 0;
2846 }
2847
2848 return pos - buf;
2849}
2850
2851
2852static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
2853 struct wpa_driver_capa *capa,
2854 char *buf, size_t buflen)
2855{
2856 int ret, first = 1;
2857 char *pos, *end;
2858 size_t len;
2859
2860 pos = buf;
2861 end = pos + buflen;
2862
2863 if (res < 0) {
2864 if (strict)
2865 return 0;
2866 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
2867 if (len >= buflen)
2868 return -1;
2869 return len;
2870 }
2871
2872 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
2873 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
2874 if (ret < 0 || ret >= end - pos)
2875 return pos - buf;
2876 pos += ret;
2877 first = 0;
2878 }
2879
2880 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
2881 ret = os_snprintf(pos, end - pos, "%sSHARED",
2882 first ? "" : " ");
2883 if (ret < 0 || ret >= end - pos)
2884 return pos - buf;
2885 pos += ret;
2886 first = 0;
2887 }
2888
2889 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
2890 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
2891 if (ret < 0 || ret >= end - pos)
2892 return pos - buf;
2893 pos += ret;
2894 first = 0;
2895 }
2896
2897 return pos - buf;
2898}
2899
2900
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002901static int ctrl_iface_get_capability_modes(int res, char *strict,
2902 struct wpa_driver_capa *capa,
2903 char *buf, size_t buflen)
2904{
2905 int ret, first = 1;
2906 char *pos, *end;
2907 size_t len;
2908
2909 pos = buf;
2910 end = pos + buflen;
2911
2912 if (res < 0) {
2913 if (strict)
2914 return 0;
2915 len = os_strlcpy(buf, "IBSS AP", buflen);
2916 if (len >= buflen)
2917 return -1;
2918 return len;
2919 }
2920
2921 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
2922 ret = os_snprintf(pos, end - pos, "%sIBSS", first ? "" : " ");
2923 if (ret < 0 || ret >= end - pos)
2924 return pos - buf;
2925 pos += ret;
2926 first = 0;
2927 }
2928
2929 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
2930 ret = os_snprintf(pos, end - pos, "%sAP", first ? "" : " ");
2931 if (ret < 0 || ret >= end - pos)
2932 return pos - buf;
2933 pos += ret;
2934 first = 0;
2935 }
2936
2937 return pos - buf;
2938}
2939
2940
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002941static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
2942 char *buf, size_t buflen)
2943{
2944 struct hostapd_channel_data *chnl;
2945 int ret, i, j;
2946 char *pos, *end, *hmode;
2947
2948 pos = buf;
2949 end = pos + buflen;
2950
2951 for (j = 0; j < wpa_s->hw.num_modes; j++) {
2952 switch (wpa_s->hw.modes[j].mode) {
2953 case HOSTAPD_MODE_IEEE80211B:
2954 hmode = "B";
2955 break;
2956 case HOSTAPD_MODE_IEEE80211G:
2957 hmode = "G";
2958 break;
2959 case HOSTAPD_MODE_IEEE80211A:
2960 hmode = "A";
2961 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002962 case HOSTAPD_MODE_IEEE80211AD:
2963 hmode = "AD";
2964 break;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002965 default:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002966 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002967 }
2968 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
2969 if (ret < 0 || ret >= end - pos)
2970 return pos - buf;
2971 pos += ret;
2972 chnl = wpa_s->hw.modes[j].channels;
2973 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002974 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
2975 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002976 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
2977 if (ret < 0 || ret >= end - pos)
2978 return pos - buf;
2979 pos += ret;
2980 }
2981 ret = os_snprintf(pos, end - pos, "\n");
2982 if (ret < 0 || ret >= end - pos)
2983 return pos - buf;
2984 pos += ret;
2985 }
2986
2987 return pos - buf;
2988}
2989
2990
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002991static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
2992 char *buf, size_t buflen)
2993{
2994 struct hostapd_channel_data *chnl;
2995 int ret, i, j;
2996 char *pos, *end, *hmode;
2997
2998 pos = buf;
2999 end = pos + buflen;
3000
3001 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3002 switch (wpa_s->hw.modes[j].mode) {
3003 case HOSTAPD_MODE_IEEE80211B:
3004 hmode = "B";
3005 break;
3006 case HOSTAPD_MODE_IEEE80211G:
3007 hmode = "G";
3008 break;
3009 case HOSTAPD_MODE_IEEE80211A:
3010 hmode = "A";
3011 break;
3012 case HOSTAPD_MODE_IEEE80211AD:
3013 hmode = "AD";
3014 break;
3015 default:
3016 continue;
3017 }
3018 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3019 hmode);
3020 if (ret < 0 || ret >= end - pos)
3021 return pos - buf;
3022 pos += ret;
3023 chnl = wpa_s->hw.modes[j].channels;
3024 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3025 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3026 continue;
3027 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s\n",
3028 chnl[i].chan, chnl[i].freq,
3029 chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
3030 " (NO_IBSS)" : "");
3031 if (ret < 0 || ret >= end - pos)
3032 return pos - buf;
3033 pos += ret;
3034 }
3035 ret = os_snprintf(pos, end - pos, "\n");
3036 if (ret < 0 || ret >= end - pos)
3037 return pos - buf;
3038 pos += ret;
3039 }
3040
3041 return pos - buf;
3042}
3043
3044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003045static int wpa_supplicant_ctrl_iface_get_capability(
3046 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3047 size_t buflen)
3048{
3049 struct wpa_driver_capa capa;
3050 int res;
3051 char *strict;
3052 char field[30];
3053 size_t len;
3054
3055 /* Determine whether or not strict checking was requested */
3056 len = os_strlcpy(field, _field, sizeof(field));
3057 if (len >= sizeof(field))
3058 return -1;
3059 strict = os_strchr(field, ' ');
3060 if (strict != NULL) {
3061 *strict++ = '\0';
3062 if (os_strcmp(strict, "strict") != 0)
3063 return -1;
3064 }
3065
3066 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3067 field, strict ? strict : "");
3068
3069 if (os_strcmp(field, "eap") == 0) {
3070 return eap_get_names(buf, buflen);
3071 }
3072
3073 res = wpa_drv_get_capa(wpa_s, &capa);
3074
3075 if (os_strcmp(field, "pairwise") == 0)
3076 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3077 buf, buflen);
3078
3079 if (os_strcmp(field, "group") == 0)
3080 return ctrl_iface_get_capability_group(res, strict, &capa,
3081 buf, buflen);
3082
3083 if (os_strcmp(field, "key_mgmt") == 0)
3084 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3085 buf, buflen);
3086
3087 if (os_strcmp(field, "proto") == 0)
3088 return ctrl_iface_get_capability_proto(res, strict, &capa,
3089 buf, buflen);
3090
3091 if (os_strcmp(field, "auth_alg") == 0)
3092 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
3093 buf, buflen);
3094
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003095 if (os_strcmp(field, "modes") == 0)
3096 return ctrl_iface_get_capability_modes(res, strict, &capa,
3097 buf, buflen);
3098
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003099 if (os_strcmp(field, "channels") == 0)
3100 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3101
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003102 if (os_strcmp(field, "freq") == 0)
3103 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3106 field);
3107
3108 return -1;
3109}
3110
3111
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003112#ifdef CONFIG_INTERWORKING
3113static char * anqp_add_hex(char *pos, char *end, const char *title,
3114 struct wpabuf *data)
3115{
3116 char *start = pos;
3117 size_t i;
3118 int ret;
3119 const u8 *d;
3120
3121 if (data == NULL)
3122 return start;
3123
3124 ret = os_snprintf(pos, end - pos, "%s=", title);
3125 if (ret < 0 || ret >= end - pos)
3126 return start;
3127 pos += ret;
3128
3129 d = wpabuf_head_u8(data);
3130 for (i = 0; i < wpabuf_len(data); i++) {
3131 ret = os_snprintf(pos, end - pos, "%02x", *d++);
3132 if (ret < 0 || ret >= end - pos)
3133 return start;
3134 pos += ret;
3135 }
3136
3137 ret = os_snprintf(pos, end - pos, "\n");
3138 if (ret < 0 || ret >= end - pos)
3139 return start;
3140 pos += ret;
3141
3142 return pos;
3143}
3144#endif /* CONFIG_INTERWORKING */
3145
3146
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003147static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3148 unsigned long mask, char *buf, size_t buflen)
3149{
3150 size_t i;
3151 int ret;
3152 char *pos, *end;
3153 const u8 *ie, *ie2;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003154
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003155 pos = buf;
3156 end = buf + buflen;
3157
3158 if (mask & WPA_BSS_MASK_ID) {
3159 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
3160 if (ret < 0 || ret >= end - pos)
3161 return 0;
3162 pos += ret;
3163 }
3164
3165 if (mask & WPA_BSS_MASK_BSSID) {
3166 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3167 MAC2STR(bss->bssid));
3168 if (ret < 0 || ret >= end - pos)
3169 return 0;
3170 pos += ret;
3171 }
3172
3173 if (mask & WPA_BSS_MASK_FREQ) {
3174 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
3175 if (ret < 0 || ret >= end - pos)
3176 return 0;
3177 pos += ret;
3178 }
3179
3180 if (mask & WPA_BSS_MASK_BEACON_INT) {
3181 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3182 bss->beacon_int);
3183 if (ret < 0 || ret >= end - pos)
3184 return 0;
3185 pos += ret;
3186 }
3187
3188 if (mask & WPA_BSS_MASK_CAPABILITIES) {
3189 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3190 bss->caps);
3191 if (ret < 0 || ret >= end - pos)
3192 return 0;
3193 pos += ret;
3194 }
3195
3196 if (mask & WPA_BSS_MASK_QUAL) {
3197 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
3198 if (ret < 0 || ret >= end - pos)
3199 return 0;
3200 pos += ret;
3201 }
3202
3203 if (mask & WPA_BSS_MASK_NOISE) {
3204 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
3205 if (ret < 0 || ret >= end - pos)
3206 return 0;
3207 pos += ret;
3208 }
3209
3210 if (mask & WPA_BSS_MASK_LEVEL) {
3211 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
3212 if (ret < 0 || ret >= end - pos)
3213 return 0;
3214 pos += ret;
3215 }
3216
3217 if (mask & WPA_BSS_MASK_TSF) {
3218 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3219 (unsigned long long) bss->tsf);
3220 if (ret < 0 || ret >= end - pos)
3221 return 0;
3222 pos += ret;
3223 }
3224
3225 if (mask & WPA_BSS_MASK_AGE) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003226 struct os_time now;
3227
3228 os_get_time(&now);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003229 ret = os_snprintf(pos, end - pos, "age=%d\n",
3230 (int) (now.sec - bss->last_update.sec));
3231 if (ret < 0 || ret >= end - pos)
3232 return 0;
3233 pos += ret;
3234 }
3235
3236 if (mask & WPA_BSS_MASK_IE) {
3237 ret = os_snprintf(pos, end - pos, "ie=");
3238 if (ret < 0 || ret >= end - pos)
3239 return 0;
3240 pos += ret;
3241
3242 ie = (const u8 *) (bss + 1);
3243 for (i = 0; i < bss->ie_len; i++) {
3244 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
3245 if (ret < 0 || ret >= end - pos)
3246 return 0;
3247 pos += ret;
3248 }
3249
3250 ret = os_snprintf(pos, end - pos, "\n");
3251 if (ret < 0 || ret >= end - pos)
3252 return 0;
3253 pos += ret;
3254 }
3255
3256 if (mask & WPA_BSS_MASK_FLAGS) {
3257 ret = os_snprintf(pos, end - pos, "flags=");
3258 if (ret < 0 || ret >= end - pos)
3259 return 0;
3260 pos += ret;
3261
3262 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3263 if (ie)
3264 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3265 2 + ie[1]);
3266 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3267 if (ie2)
3268 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3269 2 + ie2[1]);
3270 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3271 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3272 ret = os_snprintf(pos, end - pos, "[WEP]");
3273 if (ret < 0 || ret >= end - pos)
3274 return 0;
3275 pos += ret;
3276 }
3277 if (bss->caps & IEEE80211_CAP_IBSS) {
3278 ret = os_snprintf(pos, end - pos, "[IBSS]");
3279 if (ret < 0 || ret >= end - pos)
3280 return 0;
3281 pos += ret;
3282 }
3283 if (bss->caps & IEEE80211_CAP_ESS) {
3284 ret = os_snprintf(pos, end - pos, "[ESS]");
3285 if (ret < 0 || ret >= end - pos)
3286 return 0;
3287 pos += ret;
3288 }
Dmitry Shmidt96571392013-10-14 12:54:46 -07003289 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
3290 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003291 ret = os_snprintf(pos, end - pos, "[P2P]");
3292 if (ret < 0 || ret >= end - pos)
3293 return 0;
3294 pos += ret;
3295 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003296#ifdef CONFIG_HS20
3297 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3298 ret = os_snprintf(pos, end - pos, "[HS20]");
3299 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003300 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003301 pos += ret;
3302 }
3303#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003304
3305 ret = os_snprintf(pos, end - pos, "\n");
3306 if (ret < 0 || ret >= end - pos)
3307 return 0;
3308 pos += ret;
3309 }
3310
3311 if (mask & WPA_BSS_MASK_SSID) {
3312 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3313 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3314 if (ret < 0 || ret >= end - pos)
3315 return 0;
3316 pos += ret;
3317 }
3318
3319#ifdef CONFIG_WPS
3320 if (mask & WPA_BSS_MASK_WPS_SCAN) {
3321 ie = (const u8 *) (bss + 1);
3322 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3323 if (ret < 0 || ret >= end - pos)
3324 return 0;
3325 pos += ret;
3326 }
3327#endif /* CONFIG_WPS */
3328
3329#ifdef CONFIG_P2P
3330 if (mask & WPA_BSS_MASK_P2P_SCAN) {
3331 ie = (const u8 *) (bss + 1);
3332 ret = wpas_p2p_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_P2P */
3338
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003339#ifdef CONFIG_WIFI_DISPLAY
3340 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3341 struct wpabuf *wfd;
3342 ie = (const u8 *) (bss + 1);
3343 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3344 WFD_IE_VENDOR_TYPE);
3345 if (wfd) {
3346 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
3347 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003348 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003349 pos += ret;
3350
3351 pos += wpa_snprintf_hex(pos, end - pos,
3352 wpabuf_head(wfd),
3353 wpabuf_len(wfd));
3354 wpabuf_free(wfd);
3355
3356 ret = os_snprintf(pos, end - pos, "\n");
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 }
3362#endif /* CONFIG_WIFI_DISPLAY */
3363
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003364#ifdef CONFIG_INTERWORKING
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003365 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3366 struct wpa_bss_anqp *anqp = bss->anqp;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003367 pos = anqp_add_hex(pos, end, "anqp_venue_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003368 anqp->venue_name);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003369 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003370 anqp->network_auth_type);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003371 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003372 anqp->roaming_consortium);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003373 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003374 anqp->ip_addr_type_availability);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003375 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003376 anqp->nai_realm);
3377 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003378 pos = anqp_add_hex(pos, end, "anqp_domain_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003379 anqp->domain_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003380#ifdef CONFIG_HS20
3381 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003382 anqp->hs20_operator_friendly_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003383 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003384 anqp->hs20_wan_metrics);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003385 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003386 anqp->hs20_connection_capability);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003387#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003388 }
3389#endif /* CONFIG_INTERWORKING */
3390
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003391 if (mask & WPA_BSS_MASK_DELIM) {
3392 ret = os_snprintf(pos, end - pos, "====\n");
3393 if (ret < 0 || ret >= end - pos)
3394 return 0;
3395 pos += ret;
3396 }
Irfan Sheriffe2ea0082012-08-13 10:56:16 -07003397
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003398 return pos - buf;
3399}
3400
Dmitry Shmidt04949592012-07-19 12:16:46 -07003401
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003402static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
3403 const char *cmd, char *buf,
3404 size_t buflen)
3405{
3406 u8 bssid[ETH_ALEN];
3407 size_t i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003408 struct wpa_bss *bss;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003409 struct wpa_bss *bsslast = NULL;
3410 struct dl_list *next;
3411 int ret = 0;
3412 int len;
3413 char *ctmp;
3414 unsigned long mask = WPA_BSS_MASK_ALL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003416 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
3417 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
3418 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003419 list_id);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003420 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
3421 list_id);
3422 } else { /* N1-N2 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003423 unsigned int id1, id2;
3424
3425 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
3426 wpa_printf(MSG_INFO, "Wrong BSS range "
3427 "format");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003428 return 0;
3429 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003430
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003431 if (*(cmd + 6) == '-')
3432 id1 = 0;
3433 else
3434 id1 = atoi(cmd + 6);
3435 ctmp++;
3436 if (*ctmp >= '0' && *ctmp <= '9')
3437 id2 = atoi(ctmp);
3438 else
3439 id2 = (unsigned int) -1;
3440 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
3441 if (id2 == (unsigned int) -1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003442 bsslast = dl_list_last(&wpa_s->bss_id,
3443 struct wpa_bss,
3444 list_id);
3445 else {
3446 bsslast = wpa_bss_get_id(wpa_s, id2);
3447 if (bsslast == NULL && bss && id2 > id1) {
3448 struct wpa_bss *tmp = bss;
3449 for (;;) {
3450 next = tmp->list_id.next;
3451 if (next == &wpa_s->bss_id)
3452 break;
3453 tmp = dl_list_entry(
3454 next, struct wpa_bss,
3455 list_id);
3456 if (tmp->id > id2)
3457 break;
3458 bsslast = tmp;
3459 }
3460 }
3461 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003462 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003463 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003464 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003465 else if (os_strncmp(cmd, "LAST", 4) == 0)
3466 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003467 else if (os_strncmp(cmd, "ID-", 3) == 0) {
3468 i = atoi(cmd + 3);
3469 bss = wpa_bss_get_id(wpa_s, i);
3470 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3471 i = atoi(cmd + 5);
3472 bss = wpa_bss_get_id(wpa_s, i);
3473 if (bss) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003474 next = bss->list_id.next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003475 if (next == &wpa_s->bss_id)
3476 bss = NULL;
3477 else
3478 bss = dl_list_entry(next, struct wpa_bss,
3479 list_id);
3480 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003481#ifdef CONFIG_P2P
3482 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
3483 if (hwaddr_aton(cmd + 13, bssid) == 0)
3484 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
3485 else
3486 bss = NULL;
3487#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003488 } else if (hwaddr_aton(cmd, bssid) == 0)
3489 bss = wpa_bss_get_bssid(wpa_s, bssid);
3490 else {
3491 struct wpa_bss *tmp;
3492 i = atoi(cmd);
3493 bss = NULL;
3494 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
3495 {
3496 if (i-- == 0) {
3497 bss = tmp;
3498 break;
3499 }
3500 }
3501 }
3502
Dmitry Shmidt04949592012-07-19 12:16:46 -07003503 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
3504 mask = strtoul(ctmp + 5, NULL, 0x10);
3505 if (mask == 0)
3506 mask = WPA_BSS_MASK_ALL;
3507 }
3508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003509 if (bss == NULL)
3510 return 0;
3511
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003512 if (bsslast == NULL)
3513 bsslast = bss;
3514 do {
3515 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
3516 ret += len;
3517 buf += len;
3518 buflen -= len;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003519 if (bss == bsslast) {
3520 if ((mask & WPA_BSS_MASK_DELIM) && len &&
3521 (bss == dl_list_last(&wpa_s->bss_id,
3522 struct wpa_bss, list_id)))
3523 os_snprintf(buf - 5, 5, "####\n");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003524 break;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003525 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003526 next = bss->list_id.next;
3527 if (next == &wpa_s->bss_id)
3528 break;
3529 bss = dl_list_entry(next, struct wpa_bss, list_id);
3530 } while (bss && len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003531
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003532 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003533}
3534
3535
3536static int wpa_supplicant_ctrl_iface_ap_scan(
3537 struct wpa_supplicant *wpa_s, char *cmd)
3538{
3539 int ap_scan = atoi(cmd);
3540 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
3541}
3542
3543
3544static int wpa_supplicant_ctrl_iface_scan_interval(
3545 struct wpa_supplicant *wpa_s, char *cmd)
3546{
3547 int scan_int = atoi(cmd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003548 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003549}
3550
3551
3552static int wpa_supplicant_ctrl_iface_bss_expire_age(
3553 struct wpa_supplicant *wpa_s, char *cmd)
3554{
3555 int expire_age = atoi(cmd);
3556 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
3557}
3558
3559
3560static int wpa_supplicant_ctrl_iface_bss_expire_count(
3561 struct wpa_supplicant *wpa_s, char *cmd)
3562{
3563 int expire_count = atoi(cmd);
3564 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
3565}
3566
3567
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07003568static int wpa_supplicant_ctrl_iface_bss_flush(
3569 struct wpa_supplicant *wpa_s, char *cmd)
3570{
3571 int flush_age = atoi(cmd);
3572
3573 if (flush_age == 0)
3574 wpa_bss_flush(wpa_s);
3575 else
3576 wpa_bss_flush_by_age(wpa_s, flush_age);
3577 return 0;
3578}
3579
3580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003581static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
3582{
3583 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
3584 /* MLME-DELETEKEYS.request */
3585 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
3586 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
3587 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
3588 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
3589#ifdef CONFIG_IEEE80211W
3590 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
3591 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
3592#endif /* CONFIG_IEEE80211W */
3593
3594 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
3595 0);
3596 /* MLME-SETPROTECTION.request(None) */
3597 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
3598 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
3599 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3600 wpa_sm_drop_sa(wpa_s->wpa);
3601}
3602
3603
3604static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
3605 char *addr)
3606{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003607#ifdef CONFIG_NO_SCAN_PROCESSING
3608 return -1;
3609#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003610 u8 bssid[ETH_ALEN];
3611 struct wpa_bss *bss;
3612 struct wpa_ssid *ssid = wpa_s->current_ssid;
3613
3614 if (hwaddr_aton(addr, bssid)) {
3615 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
3616 "address '%s'", addr);
3617 return -1;
3618 }
3619
3620 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
3621
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003622 if (!ssid) {
3623 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
3624 "configuration known for the target AP");
3625 return -1;
3626 }
3627
3628 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629 if (!bss) {
3630 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
3631 "from BSS table");
3632 return -1;
3633 }
3634
3635 /*
3636 * TODO: Find best network configuration block from configuration to
3637 * allow roaming to other networks
3638 */
3639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003640 wpa_s->reassociate = 1;
3641 wpa_supplicant_connect(wpa_s, bss, ssid);
3642
3643 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003644#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003645}
3646
3647
3648#ifdef CONFIG_P2P
3649static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
3650{
3651 unsigned int timeout = atoi(cmd);
3652 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003653 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
3654 char *pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003655 unsigned int search_delay;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003656
3657 if (os_strstr(cmd, "type=social"))
3658 type = P2P_FIND_ONLY_SOCIAL;
3659 else if (os_strstr(cmd, "type=progressive"))
3660 type = P2P_FIND_PROGRESSIVE;
3661
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003662 pos = os_strstr(cmd, "dev_id=");
3663 if (pos) {
3664 pos += 7;
3665 if (hwaddr_aton(pos, dev_id))
3666 return -1;
3667 _dev_id = dev_id;
3668 }
3669
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003670 pos = os_strstr(cmd, "delay=");
3671 if (pos) {
3672 pos += 6;
3673 search_delay = atoi(pos);
3674 } else
3675 search_delay = wpas_p2p_search_delay(wpa_s);
3676
3677 return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
3678 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679}
3680
3681
3682static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
3683 char *buf, size_t buflen)
3684{
3685 u8 addr[ETH_ALEN];
3686 char *pos, *pos2;
3687 char *pin = NULL;
3688 enum p2p_wps_method wps_method;
3689 int new_pin;
3690 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003691 int persistent_group, persistent_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003692 int join;
3693 int auth;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003694 int automatic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003695 int go_intent = -1;
3696 int freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003697 int pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003698 int ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003699
Dmitry Shmidt04949592012-07-19 12:16:46 -07003700 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
3701 * [persistent|persistent=<network id>]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003702 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
3703 * [ht40] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003704
3705 if (hwaddr_aton(cmd, addr))
3706 return -1;
3707
3708 pos = cmd + 17;
3709 if (*pos != ' ')
3710 return -1;
3711 pos++;
3712
3713 persistent_group = os_strstr(pos, " persistent") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003714 pos2 = os_strstr(pos, " persistent=");
3715 if (pos2) {
3716 struct wpa_ssid *ssid;
3717 persistent_id = atoi(pos2 + 12);
3718 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3719 if (ssid == NULL || ssid->disabled != 2 ||
3720 ssid->mode != WPAS_MODE_P2P_GO) {
3721 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3722 "SSID id=%d for persistent P2P group (GO)",
3723 persistent_id);
3724 return -1;
3725 }
3726 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003727 join = os_strstr(pos, " join") != NULL;
3728 auth = os_strstr(pos, " auth") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003729 automatic = os_strstr(pos, " auto") != NULL;
3730 pd = os_strstr(pos, " provdisc") != NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003731 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003732
3733 pos2 = os_strstr(pos, " go_intent=");
3734 if (pos2) {
3735 pos2 += 11;
3736 go_intent = atoi(pos2);
3737 if (go_intent < 0 || go_intent > 15)
3738 return -1;
3739 }
3740
3741 pos2 = os_strstr(pos, " freq=");
3742 if (pos2) {
3743 pos2 += 6;
3744 freq = atoi(pos2);
3745 if (freq <= 0)
3746 return -1;
3747 }
3748
3749 if (os_strncmp(pos, "pin", 3) == 0) {
3750 /* Request random PIN (to be displayed) and enable the PIN */
3751 wps_method = WPS_PIN_DISPLAY;
3752 } else if (os_strncmp(pos, "pbc", 3) == 0) {
3753 wps_method = WPS_PBC;
3754 } else {
3755 pin = pos;
3756 pos = os_strchr(pin, ' ');
3757 wps_method = WPS_PIN_KEYPAD;
3758 if (pos) {
3759 *pos++ = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003760 if (os_strncmp(pos, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003761 wps_method = WPS_PIN_DISPLAY;
3762 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003763 if (!wps_pin_str_valid(pin)) {
3764 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
3765 return 17;
3766 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003767 }
3768
3769 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003770 persistent_group, automatic, join,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003771 auth, go_intent, freq, persistent_id, pd,
3772 ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003773 if (new_pin == -2) {
3774 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
3775 return 25;
3776 }
3777 if (new_pin == -3) {
3778 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
3779 return 25;
3780 }
3781 if (new_pin < 0)
3782 return -1;
3783 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
3784 ret = os_snprintf(buf, buflen, "%08d", new_pin);
3785 if (ret < 0 || (size_t) ret >= buflen)
3786 return -1;
3787 return ret;
3788 }
3789
3790 os_memcpy(buf, "OK\n", 3);
3791 return 3;
3792}
3793
3794
3795static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
3796{
3797 unsigned int timeout = atoi(cmd);
3798 return wpas_p2p_listen(wpa_s, timeout);
3799}
3800
3801
3802static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
3803{
3804 u8 addr[ETH_ALEN];
3805 char *pos;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003806 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003807
Dmitry Shmidt04949592012-07-19 12:16:46 -07003808 /* <addr> <config method> [join|auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003809
3810 if (hwaddr_aton(cmd, addr))
3811 return -1;
3812
3813 pos = cmd + 17;
3814 if (*pos != ' ')
3815 return -1;
3816 pos++;
3817
Dmitry Shmidt04949592012-07-19 12:16:46 -07003818 if (os_strstr(pos, " join") != NULL)
3819 use = WPAS_P2P_PD_FOR_JOIN;
3820 else if (os_strstr(pos, " auto") != NULL)
3821 use = WPAS_P2P_PD_AUTO;
3822
3823 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003824}
3825
3826
3827static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
3828 size_t buflen)
3829{
3830 struct wpa_ssid *ssid = wpa_s->current_ssid;
3831
3832 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3833 ssid->passphrase == NULL)
3834 return -1;
3835
3836 os_strlcpy(buf, ssid->passphrase, buflen);
3837 return os_strlen(buf);
3838}
3839
3840
3841static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
3842 char *buf, size_t buflen)
3843{
3844 u64 ref;
3845 int res;
3846 u8 dst_buf[ETH_ALEN], *dst;
3847 struct wpabuf *tlvs;
3848 char *pos;
3849 size_t len;
3850
3851 if (hwaddr_aton(cmd, dst_buf))
3852 return -1;
3853 dst = dst_buf;
3854 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
3855 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
3856 dst = NULL;
3857 pos = cmd + 17;
3858 if (*pos != ' ')
3859 return -1;
3860 pos++;
3861
3862 if (os_strncmp(pos, "upnp ", 5) == 0) {
3863 u8 version;
3864 pos += 5;
3865 if (hexstr2bin(pos, &version, 1) < 0)
3866 return -1;
3867 pos += 2;
3868 if (*pos != ' ')
3869 return -1;
3870 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003871 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003872#ifdef CONFIG_WIFI_DISPLAY
3873 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
3874 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
3875#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003876 } else {
3877 len = os_strlen(pos);
3878 if (len & 1)
3879 return -1;
3880 len /= 2;
3881 tlvs = wpabuf_alloc(len);
3882 if (tlvs == NULL)
3883 return -1;
3884 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
3885 wpabuf_free(tlvs);
3886 return -1;
3887 }
3888
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003889 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003890 wpabuf_free(tlvs);
3891 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003892 if (ref == 0)
3893 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003894 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
3895 if (res < 0 || (unsigned) res >= buflen)
3896 return -1;
3897 return res;
3898}
3899
3900
3901static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
3902 char *cmd)
3903{
3904 long long unsigned val;
3905 u64 req;
3906 if (sscanf(cmd, "%llx", &val) != 1)
3907 return -1;
3908 req = val;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003909 return wpas_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003910}
3911
3912
3913static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
3914{
3915 int freq;
3916 u8 dst[ETH_ALEN];
3917 u8 dialog_token;
3918 struct wpabuf *resp_tlvs;
3919 char *pos, *pos2;
3920 size_t len;
3921
3922 pos = os_strchr(cmd, ' ');
3923 if (pos == NULL)
3924 return -1;
3925 *pos++ = '\0';
3926 freq = atoi(cmd);
3927 if (freq == 0)
3928 return -1;
3929
3930 if (hwaddr_aton(pos, dst))
3931 return -1;
3932 pos += 17;
3933 if (*pos != ' ')
3934 return -1;
3935 pos++;
3936
3937 pos2 = os_strchr(pos, ' ');
3938 if (pos2 == NULL)
3939 return -1;
3940 *pos2++ = '\0';
3941 dialog_token = atoi(pos);
3942
3943 len = os_strlen(pos2);
3944 if (len & 1)
3945 return -1;
3946 len /= 2;
3947 resp_tlvs = wpabuf_alloc(len);
3948 if (resp_tlvs == NULL)
3949 return -1;
3950 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
3951 wpabuf_free(resp_tlvs);
3952 return -1;
3953 }
3954
3955 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
3956 wpabuf_free(resp_tlvs);
3957 return 0;
3958}
3959
3960
3961static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
3962 char *cmd)
3963{
Dmitry Shmidt04949592012-07-19 12:16:46 -07003964 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
3965 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003966 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
3967 return 0;
3968}
3969
3970
3971static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
3972 char *cmd)
3973{
3974 char *pos;
3975 size_t len;
3976 struct wpabuf *query, *resp;
3977
3978 pos = os_strchr(cmd, ' ');
3979 if (pos == NULL)
3980 return -1;
3981 *pos++ = '\0';
3982
3983 len = os_strlen(cmd);
3984 if (len & 1)
3985 return -1;
3986 len /= 2;
3987 query = wpabuf_alloc(len);
3988 if (query == NULL)
3989 return -1;
3990 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
3991 wpabuf_free(query);
3992 return -1;
3993 }
3994
3995 len = os_strlen(pos);
3996 if (len & 1) {
3997 wpabuf_free(query);
3998 return -1;
3999 }
4000 len /= 2;
4001 resp = wpabuf_alloc(len);
4002 if (resp == NULL) {
4003 wpabuf_free(query);
4004 return -1;
4005 }
4006 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4007 wpabuf_free(query);
4008 wpabuf_free(resp);
4009 return -1;
4010 }
4011
4012 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4013 wpabuf_free(query);
4014 wpabuf_free(resp);
4015 return -1;
4016 }
4017 return 0;
4018}
4019
4020
4021static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4022{
4023 char *pos;
4024 u8 version;
4025
4026 pos = os_strchr(cmd, ' ');
4027 if (pos == NULL)
4028 return -1;
4029 *pos++ = '\0';
4030
4031 if (hexstr2bin(cmd, &version, 1) < 0)
4032 return -1;
4033
4034 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4035}
4036
4037
4038static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4039{
4040 char *pos;
4041
4042 pos = os_strchr(cmd, ' ');
4043 if (pos == NULL)
4044 return -1;
4045 *pos++ = '\0';
4046
4047 if (os_strcmp(cmd, "bonjour") == 0)
4048 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4049 if (os_strcmp(cmd, "upnp") == 0)
4050 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4051 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4052 return -1;
4053}
4054
4055
4056static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4057 char *cmd)
4058{
4059 size_t len;
4060 struct wpabuf *query;
4061 int ret;
4062
4063 len = os_strlen(cmd);
4064 if (len & 1)
4065 return -1;
4066 len /= 2;
4067 query = wpabuf_alloc(len);
4068 if (query == NULL)
4069 return -1;
4070 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4071 wpabuf_free(query);
4072 return -1;
4073 }
4074
4075 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4076 wpabuf_free(query);
4077 return ret;
4078}
4079
4080
4081static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4082{
4083 char *pos;
4084 u8 version;
4085
4086 pos = os_strchr(cmd, ' ');
4087 if (pos == NULL)
4088 return -1;
4089 *pos++ = '\0';
4090
4091 if (hexstr2bin(cmd, &version, 1) < 0)
4092 return -1;
4093
4094 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4095}
4096
4097
4098static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4099{
4100 char *pos;
4101
4102 pos = os_strchr(cmd, ' ');
4103 if (pos == NULL)
4104 return -1;
4105 *pos++ = '\0';
4106
4107 if (os_strcmp(cmd, "bonjour") == 0)
4108 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4109 if (os_strcmp(cmd, "upnp") == 0)
4110 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4111 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4112 return -1;
4113}
4114
4115
4116static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4117{
4118 u8 addr[ETH_ALEN];
4119
4120 /* <addr> */
4121
4122 if (hwaddr_aton(cmd, addr))
4123 return -1;
4124
4125 return wpas_p2p_reject(wpa_s, addr);
4126}
4127
4128
4129static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4130{
4131 char *pos;
4132 int id;
4133 struct wpa_ssid *ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004134 u8 *_peer = NULL, peer[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004135 int freq = 0, pref_freq = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03004136 int ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004137
4138 id = atoi(cmd);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004139 pos = os_strstr(cmd, " peer=");
4140 if (pos) {
4141 pos += 6;
4142 if (hwaddr_aton(pos, peer))
4143 return -1;
4144 _peer = peer;
4145 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004146 ssid = wpa_config_get_network(wpa_s->conf, id);
4147 if (ssid == NULL || ssid->disabled != 2) {
4148 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4149 "for persistent P2P group",
4150 id);
4151 return -1;
4152 }
4153
Jouni Malinen31be0a42012-08-31 21:20:51 +03004154 pos = os_strstr(cmd, " freq=");
4155 if (pos) {
4156 pos += 6;
4157 freq = atoi(pos);
4158 if (freq <= 0)
4159 return -1;
4160 }
4161
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004162 pos = os_strstr(cmd, " pref=");
4163 if (pos) {
4164 pos += 6;
4165 pref_freq = atoi(pos);
4166 if (pref_freq <= 0)
4167 return -1;
4168 }
4169
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004170 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
Jouni Malinen31be0a42012-08-31 21:20:51 +03004171
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004172 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173}
4174
4175
4176static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
4177{
4178 char *pos;
4179 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
4180
4181 pos = os_strstr(cmd, " peer=");
4182 if (!pos)
4183 return -1;
4184
4185 *pos = '\0';
4186 pos += 6;
4187 if (hwaddr_aton(pos, peer)) {
4188 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
4189 return -1;
4190 }
4191
4192 pos = os_strstr(pos, " go_dev_addr=");
4193 if (pos) {
4194 pos += 13;
4195 if (hwaddr_aton(pos, go_dev_addr)) {
4196 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
4197 pos);
4198 return -1;
4199 }
4200 go_dev = go_dev_addr;
4201 }
4202
4203 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
4204}
4205
4206
4207static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
4208{
4209 if (os_strncmp(cmd, "persistent=", 11) == 0)
4210 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
4211 if (os_strncmp(cmd, "group=", 6) == 0)
4212 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
4213
4214 return -1;
4215}
4216
4217
4218static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004219 char *cmd, int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004220{
4221 int id;
4222 struct wpa_ssid *ssid;
4223
4224 id = atoi(cmd);
4225 ssid = wpa_config_get_network(wpa_s->conf, id);
4226 if (ssid == NULL || ssid->disabled != 2) {
4227 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4228 "for persistent P2P group",
4229 id);
4230 return -1;
4231 }
4232
Dmitry Shmidt56052862013-10-04 10:23:25 -07004233 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40, NULL,
4234 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004235}
4236
4237
4238static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
4239{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004240 int freq = 0, ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004241 char *pos;
4242
4243 pos = os_strstr(cmd, "freq=");
4244 if (pos)
4245 freq = atoi(pos + 5);
4246
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004247 ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004249 if (os_strncmp(cmd, "persistent=", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004250 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
4251 ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004252 if (os_strcmp(cmd, "persistent") == 0 ||
4253 os_strncmp(cmd, "persistent ", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004254 return wpas_p2p_group_add(wpa_s, 1, freq, ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004255 if (os_strncmp(cmd, "freq=", 5) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004256 return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
4257 if (ht40)
4258 return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259
4260 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
4261 cmd);
4262 return -1;
4263}
4264
4265
4266static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
4267 char *buf, size_t buflen)
4268{
4269 u8 addr[ETH_ALEN], *addr_ptr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004270 int next, res;
4271 const struct p2p_peer_info *info;
4272 char *pos, *end;
4273 char devtype[WPS_DEV_TYPE_BUFSIZE];
4274 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004275 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004276
4277 if (!wpa_s->global->p2p)
4278 return -1;
4279
4280 if (os_strcmp(cmd, "FIRST") == 0) {
4281 addr_ptr = NULL;
4282 next = 0;
4283 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4284 if (hwaddr_aton(cmd + 5, addr) < 0)
4285 return -1;
4286 addr_ptr = addr;
4287 next = 1;
4288 } else {
4289 if (hwaddr_aton(cmd, addr) < 0)
4290 return -1;
4291 addr_ptr = addr;
4292 next = 0;
4293 }
4294
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004295 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4296 if (info == NULL)
4297 return -1;
4298
4299 pos = buf;
4300 end = buf + buflen;
4301
4302 res = os_snprintf(pos, end - pos, MACSTR "\n"
4303 "pri_dev_type=%s\n"
4304 "device_name=%s\n"
4305 "manufacturer=%s\n"
4306 "model_name=%s\n"
4307 "model_number=%s\n"
4308 "serial_number=%s\n"
4309 "config_methods=0x%x\n"
4310 "dev_capab=0x%x\n"
4311 "group_capab=0x%x\n"
4312 "level=%d\n",
4313 MAC2STR(info->p2p_device_addr),
4314 wps_dev_type_bin2str(info->pri_dev_type,
4315 devtype, sizeof(devtype)),
4316 info->device_name,
4317 info->manufacturer,
4318 info->model_name,
4319 info->model_number,
4320 info->serial_number,
4321 info->config_methods,
4322 info->dev_capab,
4323 info->group_capab,
4324 info->level);
4325 if (res < 0 || res >= end - pos)
4326 return pos - buf;
4327 pos += res;
4328
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004329 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4330 {
4331 const u8 *t;
4332 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4333 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4334 wps_dev_type_bin2str(t, devtype,
4335 sizeof(devtype)));
4336 if (res < 0 || res >= end - pos)
4337 return pos - buf;
4338 pos += res;
4339 }
4340
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004341 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004342 if (ssid) {
4343 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
4344 if (res < 0 || res >= end - pos)
4345 return pos - buf;
4346 pos += res;
4347 }
4348
4349 res = p2p_get_peer_info_txt(info, pos, end - pos);
4350 if (res < 0)
4351 return pos - buf;
4352 pos += res;
4353
4354 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004355}
4356
4357
Dmitry Shmidt04949592012-07-19 12:16:46 -07004358static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
4359 const char *param)
4360{
4361 struct wpa_freq_range *freq = NULL, *n;
4362 unsigned int count = 0, i;
4363 const char *pos, *pos2, *pos3;
4364
4365 if (wpa_s->global->p2p == NULL)
4366 return -1;
4367
4368 /*
4369 * param includes comma separated frequency range.
4370 * For example: 2412-2432,2462,5000-6000
4371 */
4372 pos = param;
4373 while (pos && pos[0]) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004374 n = os_realloc_array(freq, count + 1,
4375 sizeof(struct wpa_freq_range));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004376 if (n == NULL) {
4377 os_free(freq);
4378 return -1;
4379 }
4380 freq = n;
4381 freq[count].min = atoi(pos);
4382 pos2 = os_strchr(pos, '-');
4383 pos3 = os_strchr(pos, ',');
4384 if (pos2 && (!pos3 || pos2 < pos3)) {
4385 pos2++;
4386 freq[count].max = atoi(pos2);
4387 } else
4388 freq[count].max = freq[count].min;
4389 pos = pos3;
4390 if (pos)
4391 pos++;
4392 count++;
4393 }
4394
4395 for (i = 0; i < count; i++) {
4396 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
4397 freq[i].min, freq[i].max);
4398 }
4399
4400 os_free(wpa_s->global->p2p_disallow_freq);
4401 wpa_s->global->p2p_disallow_freq = freq;
4402 wpa_s->global->num_p2p_disallow_freq = count;
4403 wpas_p2p_update_channel_list(wpa_s);
4404 return 0;
4405}
4406
4407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004408static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
4409{
4410 char *param;
4411
4412 if (wpa_s->global->p2p == NULL)
4413 return -1;
4414
4415 param = os_strchr(cmd, ' ');
4416 if (param == NULL)
4417 return -1;
4418 *param++ = '\0';
4419
4420 if (os_strcmp(cmd, "discoverability") == 0) {
4421 p2p_set_client_discoverability(wpa_s->global->p2p,
4422 atoi(param));
4423 return 0;
4424 }
4425
4426 if (os_strcmp(cmd, "managed") == 0) {
4427 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
4428 return 0;
4429 }
4430
4431 if (os_strcmp(cmd, "listen_channel") == 0) {
4432 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
4433 atoi(param));
4434 }
4435
4436 if (os_strcmp(cmd, "ssid_postfix") == 0) {
4437 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
4438 os_strlen(param));
4439 }
4440
4441 if (os_strcmp(cmd, "noa") == 0) {
4442 char *pos;
4443 int count, start, duration;
4444 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
4445 count = atoi(param);
4446 pos = os_strchr(param, ',');
4447 if (pos == NULL)
4448 return -1;
4449 pos++;
4450 start = atoi(pos);
4451 pos = os_strchr(pos, ',');
4452 if (pos == NULL)
4453 return -1;
4454 pos++;
4455 duration = atoi(pos);
4456 if (count < 0 || count > 255 || start < 0 || duration < 0)
4457 return -1;
4458 if (count == 0 && duration > 0)
4459 return -1;
4460 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
4461 "start=%d duration=%d", count, start, duration);
4462 return wpas_p2p_set_noa(wpa_s, count, start, duration);
4463 }
4464
4465 if (os_strcmp(cmd, "ps") == 0)
4466 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
4467
4468 if (os_strcmp(cmd, "oppps") == 0)
4469 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
4470
4471 if (os_strcmp(cmd, "ctwindow") == 0)
4472 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
4473
4474 if (os_strcmp(cmd, "disabled") == 0) {
4475 wpa_s->global->p2p_disabled = atoi(param);
4476 wpa_printf(MSG_DEBUG, "P2P functionality %s",
4477 wpa_s->global->p2p_disabled ?
4478 "disabled" : "enabled");
4479 if (wpa_s->global->p2p_disabled) {
4480 wpas_p2p_stop_find(wpa_s);
4481 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4482 p2p_flush(wpa_s->global->p2p);
4483 }
4484 return 0;
4485 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004486
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004487 if (os_strcmp(cmd, "conc_pref") == 0) {
4488 if (os_strcmp(param, "sta") == 0)
4489 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
4490 else if (os_strcmp(param, "p2p") == 0)
4491 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004492 else {
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004493 wpa_printf(MSG_INFO, "Invalid conc_pref value");
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004494 return -1;
4495 }
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004496 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004497 "%s", param);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004498 return 0;
4499 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004501 if (os_strcmp(cmd, "force_long_sd") == 0) {
4502 wpa_s->force_long_sd = atoi(param);
4503 return 0;
4504 }
4505
4506 if (os_strcmp(cmd, "peer_filter") == 0) {
4507 u8 addr[ETH_ALEN];
4508 if (hwaddr_aton(param, addr))
4509 return -1;
4510 p2p_set_peer_filter(wpa_s->global->p2p, addr);
4511 return 0;
4512 }
4513
4514 if (os_strcmp(cmd, "cross_connect") == 0)
4515 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
4516
4517 if (os_strcmp(cmd, "go_apsd") == 0) {
4518 if (os_strcmp(param, "disable") == 0)
4519 wpa_s->set_ap_uapsd = 0;
4520 else {
4521 wpa_s->set_ap_uapsd = 1;
4522 wpa_s->ap_uapsd = atoi(param);
4523 }
4524 return 0;
4525 }
4526
4527 if (os_strcmp(cmd, "client_apsd") == 0) {
4528 if (os_strcmp(param, "disable") == 0)
4529 wpa_s->set_sta_uapsd = 0;
4530 else {
4531 int be, bk, vi, vo;
4532 char *pos;
4533 /* format: BE,BK,VI,VO;max SP Length */
4534 be = atoi(param);
4535 pos = os_strchr(param, ',');
4536 if (pos == NULL)
4537 return -1;
4538 pos++;
4539 bk = atoi(pos);
4540 pos = os_strchr(pos, ',');
4541 if (pos == NULL)
4542 return -1;
4543 pos++;
4544 vi = atoi(pos);
4545 pos = os_strchr(pos, ',');
4546 if (pos == NULL)
4547 return -1;
4548 pos++;
4549 vo = atoi(pos);
4550 /* ignore max SP Length for now */
4551
4552 wpa_s->set_sta_uapsd = 1;
4553 wpa_s->sta_uapsd = 0;
4554 if (be)
4555 wpa_s->sta_uapsd |= BIT(0);
4556 if (bk)
4557 wpa_s->sta_uapsd |= BIT(1);
4558 if (vi)
4559 wpa_s->sta_uapsd |= BIT(2);
4560 if (vo)
4561 wpa_s->sta_uapsd |= BIT(3);
4562 }
4563 return 0;
4564 }
4565
Dmitry Shmidt04949592012-07-19 12:16:46 -07004566 if (os_strcmp(cmd, "disallow_freq") == 0)
4567 return p2p_ctrl_disallow_freq(wpa_s, param);
4568
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004569 if (os_strcmp(cmd, "disc_int") == 0) {
4570 int min_disc_int, max_disc_int, max_disc_tu;
4571 char *pos;
4572
4573 pos = param;
4574
4575 min_disc_int = atoi(pos);
4576 pos = os_strchr(pos, ' ');
4577 if (pos == NULL)
4578 return -1;
4579 *pos++ = '\0';
4580
4581 max_disc_int = atoi(pos);
4582 pos = os_strchr(pos, ' ');
4583 if (pos == NULL)
4584 return -1;
4585 *pos++ = '\0';
4586
4587 max_disc_tu = atoi(pos);
4588
4589 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
4590 max_disc_int, max_disc_tu);
4591 }
4592
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004593 if (os_strcmp(cmd, "per_sta_psk") == 0) {
4594 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
4595 return 0;
4596 }
4597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004598 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
4599 cmd);
4600
4601 return -1;
4602}
4603
4604
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004605static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
4606{
4607 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4608 wpa_s->force_long_sd = 0;
4609 if (wpa_s->global->p2p)
4610 p2p_flush(wpa_s->global->p2p);
4611}
4612
4613
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004614static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
4615{
4616 char *pos, *pos2;
4617 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
4618
4619 if (cmd[0]) {
4620 pos = os_strchr(cmd, ' ');
4621 if (pos == NULL)
4622 return -1;
4623 *pos++ = '\0';
4624 dur1 = atoi(cmd);
4625
4626 pos2 = os_strchr(pos, ' ');
4627 if (pos2)
4628 *pos2++ = '\0';
4629 int1 = atoi(pos);
4630 } else
4631 pos2 = NULL;
4632
4633 if (pos2) {
4634 pos = os_strchr(pos2, ' ');
4635 if (pos == NULL)
4636 return -1;
4637 *pos++ = '\0';
4638 dur2 = atoi(pos2);
4639 int2 = atoi(pos);
4640 }
4641
4642 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
4643}
4644
4645
4646static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
4647{
4648 char *pos;
4649 unsigned int period = 0, interval = 0;
4650
4651 if (cmd[0]) {
4652 pos = os_strchr(cmd, ' ');
4653 if (pos == NULL)
4654 return -1;
4655 *pos++ = '\0';
4656 period = atoi(cmd);
4657 interval = atoi(pos);
4658 }
4659
4660 return wpas_p2p_ext_listen(wpa_s, period, interval);
4661}
4662
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004663
4664static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
4665{
4666 const char *pos;
4667 u8 peer[ETH_ALEN];
4668 int iface_addr = 0;
4669
4670 pos = cmd;
4671 if (os_strncmp(pos, "iface=", 6) == 0) {
4672 iface_addr = 1;
4673 pos += 6;
4674 }
4675 if (hwaddr_aton(pos, peer))
4676 return -1;
4677
4678 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
4679 return 0;
4680}
4681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004682#endif /* CONFIG_P2P */
4683
4684
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004685#ifdef CONFIG_INTERWORKING
4686static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
4687{
4688 u8 bssid[ETH_ALEN];
4689 struct wpa_bss *bss;
4690
4691 if (hwaddr_aton(dst, bssid)) {
4692 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
4693 return -1;
4694 }
4695
4696 bss = wpa_bss_get_bssid(wpa_s, bssid);
4697 if (bss == NULL) {
4698 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
4699 MAC2STR(bssid));
4700 return -1;
4701 }
4702
4703 return interworking_connect(wpa_s, bss);
4704}
4705
4706
4707static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
4708{
4709 u8 dst_addr[ETH_ALEN];
4710 int used;
4711 char *pos;
4712#define MAX_ANQP_INFO_ID 100
4713 u16 id[MAX_ANQP_INFO_ID];
4714 size_t num_id = 0;
4715
4716 used = hwaddr_aton2(dst, dst_addr);
4717 if (used < 0)
4718 return -1;
4719 pos = dst + used;
4720 while (num_id < MAX_ANQP_INFO_ID) {
4721 id[num_id] = atoi(pos);
4722 if (id[num_id])
4723 num_id++;
4724 pos = os_strchr(pos + 1, ',');
4725 if (pos == NULL)
4726 break;
4727 pos++;
4728 }
4729
4730 if (num_id == 0)
4731 return -1;
4732
4733 return anqp_send_req(wpa_s, dst_addr, id, num_id);
4734}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004735
4736
4737static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
4738{
4739 u8 dst_addr[ETH_ALEN];
4740 struct wpabuf *advproto, *query = NULL;
4741 int used, ret = -1;
4742 char *pos, *end;
4743 size_t len;
4744
4745 used = hwaddr_aton2(cmd, dst_addr);
4746 if (used < 0)
4747 return -1;
4748
4749 pos = cmd + used;
4750 while (*pos == ' ')
4751 pos++;
4752
4753 /* Advertisement Protocol ID */
4754 end = os_strchr(pos, ' ');
4755 if (end)
4756 len = end - pos;
4757 else
4758 len = os_strlen(pos);
4759 if (len & 0x01)
4760 return -1;
4761 len /= 2;
4762 if (len == 0)
4763 return -1;
4764 advproto = wpabuf_alloc(len);
4765 if (advproto == NULL)
4766 return -1;
4767 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
4768 goto fail;
4769
4770 if (end) {
4771 /* Optional Query Request */
4772 pos = end + 1;
4773 while (*pos == ' ')
4774 pos++;
4775
4776 len = os_strlen(pos);
4777 if (len) {
4778 if (len & 0x01)
4779 goto fail;
4780 len /= 2;
4781 if (len == 0)
4782 goto fail;
4783 query = wpabuf_alloc(len);
4784 if (query == NULL)
4785 goto fail;
4786 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
4787 goto fail;
4788 }
4789 }
4790
4791 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
4792
4793fail:
4794 wpabuf_free(advproto);
4795 wpabuf_free(query);
4796
4797 return ret;
4798}
4799
4800
4801static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
4802 size_t buflen)
4803{
4804 u8 addr[ETH_ALEN];
4805 int dialog_token;
4806 int used;
4807 char *pos;
4808 size_t resp_len, start, requested_len;
4809
4810 if (!wpa_s->last_gas_resp)
4811 return -1;
4812
4813 used = hwaddr_aton2(cmd, addr);
4814 if (used < 0)
4815 return -1;
4816
4817 pos = cmd + used;
4818 while (*pos == ' ')
4819 pos++;
4820 dialog_token = atoi(pos);
4821
4822 if (os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) != 0 ||
4823 dialog_token != wpa_s->last_gas_dialog_token)
4824 return -1;
4825
4826 resp_len = wpabuf_len(wpa_s->last_gas_resp);
4827 start = 0;
4828 requested_len = resp_len;
4829
4830 pos = os_strchr(pos, ' ');
4831 if (pos) {
4832 start = atoi(pos);
4833 if (start > resp_len)
4834 return os_snprintf(buf, buflen, "FAIL-Invalid range");
4835 pos = os_strchr(pos, ',');
4836 if (pos == NULL)
4837 return -1;
4838 pos++;
4839 requested_len = atoi(pos);
4840 if (start + requested_len > resp_len)
4841 return os_snprintf(buf, buflen, "FAIL-Invalid range");
4842 }
4843
4844 if (requested_len * 2 + 1 > buflen)
4845 return os_snprintf(buf, buflen, "FAIL-Too long response");
4846
4847 return wpa_snprintf_hex(buf, buflen,
4848 wpabuf_head_u8(wpa_s->last_gas_resp) + start,
4849 requested_len);
4850}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004851#endif /* CONFIG_INTERWORKING */
4852
4853
Dmitry Shmidt04949592012-07-19 12:16:46 -07004854#ifdef CONFIG_HS20
4855
4856static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
4857{
4858 u8 dst_addr[ETH_ALEN];
4859 int used;
4860 char *pos;
4861 u32 subtypes = 0;
4862
4863 used = hwaddr_aton2(dst, dst_addr);
4864 if (used < 0)
4865 return -1;
4866 pos = dst + used;
4867 for (;;) {
4868 int num = atoi(pos);
4869 if (num <= 0 || num > 31)
4870 return -1;
4871 subtypes |= BIT(num);
4872 pos = os_strchr(pos + 1, ',');
4873 if (pos == NULL)
4874 break;
4875 pos++;
4876 }
4877
4878 if (subtypes == 0)
4879 return -1;
4880
4881 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
4882}
4883
4884
4885static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4886 const u8 *addr, const char *realm)
4887{
4888 u8 *buf;
4889 size_t rlen, len;
4890 int ret;
4891
4892 rlen = os_strlen(realm);
4893 len = 3 + rlen;
4894 buf = os_malloc(len);
4895 if (buf == NULL)
4896 return -1;
4897 buf[0] = 1; /* NAI Home Realm Count */
4898 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
4899 buf[2] = rlen;
4900 os_memcpy(buf + 3, realm, rlen);
4901
4902 ret = hs20_anqp_send_req(wpa_s, addr,
4903 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4904 buf, len);
4905
4906 os_free(buf);
4907
4908 return ret;
4909}
4910
4911
4912static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4913 char *dst)
4914{
4915 struct wpa_cred *cred = wpa_s->conf->cred;
4916 u8 dst_addr[ETH_ALEN];
4917 int used;
4918 u8 *buf;
4919 size_t len;
4920 int ret;
4921
4922 used = hwaddr_aton2(dst, dst_addr);
4923 if (used < 0)
4924 return -1;
4925
4926 while (dst[used] == ' ')
4927 used++;
4928 if (os_strncmp(dst + used, "realm=", 6) == 0)
4929 return hs20_nai_home_realm_list(wpa_s, dst_addr,
4930 dst + used + 6);
4931
4932 len = os_strlen(dst + used);
4933
4934 if (len == 0 && cred && cred->realm)
4935 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
4936
4937 if (len % 1)
4938 return -1;
4939 len /= 2;
4940 buf = os_malloc(len);
4941 if (buf == NULL)
4942 return -1;
4943 if (hexstr2bin(dst + used, buf, len) < 0) {
4944 os_free(buf);
4945 return -1;
4946 }
4947
4948 ret = hs20_anqp_send_req(wpa_s, dst_addr,
4949 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4950 buf, len);
4951 os_free(buf);
4952
4953 return ret;
4954}
4955
4956#endif /* CONFIG_HS20 */
4957
4958
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004959static int wpa_supplicant_ctrl_iface_sta_autoconnect(
4960 struct wpa_supplicant *wpa_s, char *cmd)
4961{
4962 wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
4963 return 0;
4964}
4965
4966
Dmitry Shmidt04949592012-07-19 12:16:46 -07004967#ifdef CONFIG_AUTOSCAN
4968
4969static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
4970 char *cmd)
4971{
4972 enum wpa_states state = wpa_s->wpa_state;
4973 char *new_params = NULL;
4974
4975 if (os_strlen(cmd) > 0) {
4976 new_params = os_strdup(cmd);
4977 if (new_params == NULL)
4978 return -1;
4979 }
4980
4981 os_free(wpa_s->conf->autoscan);
4982 wpa_s->conf->autoscan = new_params;
4983
4984 if (wpa_s->conf->autoscan == NULL)
4985 autoscan_deinit(wpa_s);
4986 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
4987 autoscan_init(wpa_s, 1);
4988 else if (state == WPA_SCANNING)
4989 wpa_supplicant_reinit_autoscan(wpa_s);
4990
4991 return 0;
4992}
4993
4994#endif /* CONFIG_AUTOSCAN */
4995
4996
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004997#ifdef CONFIG_WNM
4998
4999static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
5000{
5001 int enter;
5002 int intval = 0;
5003 char *pos;
5004 int ret;
5005 struct wpabuf *tfs_req = NULL;
5006
5007 if (os_strncmp(cmd, "enter", 5) == 0)
5008 enter = 1;
5009 else if (os_strncmp(cmd, "exit", 4) == 0)
5010 enter = 0;
5011 else
5012 return -1;
5013
5014 pos = os_strstr(cmd, " interval=");
5015 if (pos)
5016 intval = atoi(pos + 10);
5017
5018 pos = os_strstr(cmd, " tfs_req=");
5019 if (pos) {
5020 char *end;
5021 size_t len;
5022 pos += 9;
5023 end = os_strchr(pos, ' ');
5024 if (end)
5025 len = end - pos;
5026 else
5027 len = os_strlen(pos);
5028 if (len & 1)
5029 return -1;
5030 len /= 2;
5031 tfs_req = wpabuf_alloc(len);
5032 if (tfs_req == NULL)
5033 return -1;
5034 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
5035 wpabuf_free(tfs_req);
5036 return -1;
5037 }
5038 }
5039
5040 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
5041 WNM_SLEEP_MODE_EXIT, intval,
5042 tfs_req);
5043 wpabuf_free(tfs_req);
5044
5045 return ret;
5046}
5047
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005048
5049static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
5050{
5051 int query_reason;
5052
5053 query_reason = atoi(cmd);
5054
5055 wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
5056 query_reason);
5057
5058 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
5059}
5060
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005061#endif /* CONFIG_WNM */
5062
5063
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005064/* Get string representation of channel width */
5065static const char * channel_width_name(enum chan_width width)
5066{
5067 switch (width) {
5068 case CHAN_WIDTH_20_NOHT:
5069 return "20 MHz (no HT)";
5070 case CHAN_WIDTH_20:
5071 return "20 MHz";
5072 case CHAN_WIDTH_40:
5073 return "40 MHz";
5074 case CHAN_WIDTH_80:
5075 return "80 MHz";
5076 case CHAN_WIDTH_80P80:
5077 return "80+80 MHz";
5078 case CHAN_WIDTH_160:
5079 return "160 MHz";
5080 default:
5081 return "unknown";
5082 }
5083}
5084
5085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005086static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
5087 size_t buflen)
5088{
5089 struct wpa_signal_info si;
5090 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005091 char *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005092
5093 ret = wpa_drv_signal_poll(wpa_s, &si);
5094 if (ret)
5095 return -1;
5096
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005097 pos = buf;
5098 end = buf + buflen;
5099
5100 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005101 "NOISE=%d\nFREQUENCY=%u\n",
5102 si.current_signal, si.current_txrate / 1000,
5103 si.current_noise, si.frequency);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005104 if (ret < 0 || ret > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005105 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005106 pos += ret;
5107
5108 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
5109 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
5110 channel_width_name(si.chanwidth));
5111 if (ret < 0 || ret > end - pos)
5112 return -1;
5113 pos += ret;
5114 }
5115
5116 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
5117 ret = os_snprintf(pos, end - pos,
5118 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
5119 si.center_frq1, si.center_frq2);
5120 if (ret < 0 || ret > end - pos)
5121 return -1;
5122 pos += ret;
5123 }
5124
5125 if (si.avg_signal) {
5126 ret = os_snprintf(pos, end - pos,
5127 "AVG_RSSI=%d\n", si.avg_signal);
5128 if (ret < 0 || ret >= end - pos)
5129 return -1;
5130 pos += ret;
5131 }
5132
5133 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005134}
5135
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005136
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005137static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
5138 size_t buflen)
5139{
5140 struct hostap_sta_driver_data sta;
5141 int ret;
5142
5143 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
5144 if (ret)
5145 return -1;
5146
5147 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005148 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
5149 if (ret < 0 || (size_t) ret > buflen)
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005150 return -1;
5151 return ret;
5152}
5153
5154
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005155#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005156static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5157 char *buf, size_t buflen)
5158{
5159 int ret;
5160
5161 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005162 if (ret == 0) {
5163 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
5164 struct p2p_data *p2p = wpa_s->global->p2p;
5165 if (p2p) {
5166 char country[3];
5167 country[0] = cmd[8];
5168 country[1] = cmd[9];
5169 country[2] = 0x04;
5170 p2p_set_country(p2p, country);
5171 }
5172 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005173 ret = sprintf(buf, "%s\n", "OK");
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005174 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005175 return ret;
5176}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005177#endif
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005178
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07005179
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005180static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
5181{
5182 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
5183
5184#ifdef CONFIG_P2P
5185 wpas_p2p_stop_find(wpa_s);
5186 p2p_ctrl_flush(wpa_s);
5187 wpas_p2p_group_remove(wpa_s, "*");
5188#endif /* CONFIG_P2P */
5189
5190#ifdef CONFIG_WPS_TESTING
5191 wps_version_number = 0x20;
5192 wps_testing_dummy_cred = 0;
5193#endif /* CONFIG_WPS_TESTING */
5194#ifdef CONFIG_WPS
5195 wpas_wps_cancel(wpa_s);
5196#endif /* CONFIG_WPS */
5197
5198#ifdef CONFIG_TDLS_TESTING
5199 extern unsigned int tdls_testing;
5200 tdls_testing = 0;
5201#endif /* CONFIG_TDLS_TESTING */
5202#ifdef CONFIG_TDLS
5203 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
5204 wpa_tdls_enable(wpa_s->wpa, 1);
5205#endif /* CONFIG_TDLS */
5206
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005207 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
5208 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
5209
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005210 wpa_s->no_keep_alive = 0;
5211
5212 os_free(wpa_s->disallow_aps_bssid);
5213 wpa_s->disallow_aps_bssid = NULL;
5214 wpa_s->disallow_aps_bssid_count = 0;
5215 os_free(wpa_s->disallow_aps_ssid);
5216 wpa_s->disallow_aps_ssid = NULL;
5217 wpa_s->disallow_aps_ssid_count = 0;
5218
5219 wpa_s->set_sta_uapsd = 0;
5220 wpa_s->sta_uapsd = 0;
5221
5222 wpa_drv_radio_disable(wpa_s, 0);
5223
5224 wpa_bss_flush(wpa_s);
5225 wpa_blacklist_clear(wpa_s);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07005226 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005227 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
5228 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
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;
5237 int ctrl_rsp = 0;
5238 int reply_len;
5239
5240 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005241 os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
5242 os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005243 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005244 os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005245 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
5246 (const u8 *) buf, os_strlen(buf));
5247 } else {
5248 int level = MSG_DEBUG;
5249 if (os_strcmp(buf, "PING") == 0)
5250 level = MSG_EXCESSIVE;
5251 wpa_hexdump_ascii(level, "RX ctrl_iface",
5252 (const u8 *) buf, os_strlen(buf));
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005253 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005254 }
5255
5256 reply = os_malloc(reply_size);
5257 if (reply == NULL) {
5258 *resp_len = 1;
5259 return NULL;
5260 }
5261
5262 os_memcpy(reply, "OK\n", 3);
5263 reply_len = 3;
5264
5265 if (os_strcmp(buf, "PING") == 0) {
5266 os_memcpy(reply, "PONG\n", 5);
5267 reply_len = 5;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005268 } else if (os_strcmp(buf, "IFNAME") == 0) {
5269 reply_len = os_strlen(wpa_s->ifname);
5270 os_memcpy(reply, wpa_s->ifname, reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005271 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
5272 if (wpa_debug_reopen_file() < 0)
5273 reply_len = -1;
5274 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
5275 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
5276 } else if (os_strcmp(buf, "MIB") == 0) {
5277 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
5278 if (reply_len >= 0) {
5279 int res;
5280 res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
5281 reply_size - reply_len);
5282 if (res < 0)
5283 reply_len = -1;
5284 else
5285 reply_len += res;
5286 }
5287 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
5288 reply_len = wpa_supplicant_ctrl_iface_status(
5289 wpa_s, buf + 6, reply, reply_size);
5290 } else if (os_strcmp(buf, "PMKSA") == 0) {
5291 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
5292 reply_size);
5293 } else if (os_strncmp(buf, "SET ", 4) == 0) {
5294 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
5295 reply_len = -1;
5296 } else if (os_strncmp(buf, "GET ", 4) == 0) {
5297 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
5298 reply, reply_size);
5299 } else if (os_strcmp(buf, "LOGON") == 0) {
5300 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5301 } else if (os_strcmp(buf, "LOGOFF") == 0) {
5302 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
5303 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
5304 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5305 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005306 else
5307 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005308 } else if (os_strcmp(buf, "RECONNECT") == 0) {
5309 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5310 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005311 else if (wpa_s->disconnected)
5312 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005313#ifdef IEEE8021X_EAPOL
5314 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
5315 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
5316 reply_len = -1;
5317#endif /* IEEE8021X_EAPOL */
5318#ifdef CONFIG_PEERKEY
5319 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
5320 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
5321 reply_len = -1;
5322#endif /* CONFIG_PEERKEY */
5323#ifdef CONFIG_IEEE80211R
5324 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
5325 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
5326 reply_len = -1;
5327#endif /* CONFIG_IEEE80211R */
5328#ifdef CONFIG_WPS
5329 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
5330 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
5331 if (res == -2) {
5332 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5333 reply_len = 17;
5334 } else if (res)
5335 reply_len = -1;
5336 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
5337 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
5338 if (res == -2) {
5339 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5340 reply_len = 17;
5341 } else if (res)
5342 reply_len = -1;
5343 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
5344 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
5345 reply,
5346 reply_size);
5347 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
5348 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
5349 wpa_s, buf + 14, reply, reply_size);
5350 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
5351 if (wpas_wps_cancel(wpa_s))
5352 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005353#ifdef CONFIG_WPS_NFC
5354 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
5355 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
5356 reply_len = -1;
5357 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
5358 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
5359 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005360 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
5361 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
5362 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005363 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
5364 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
5365 wpa_s, buf + 14, reply, reply_size);
5366 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
5367 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
5368 buf + 17))
5369 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005370 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
5371 reply_len = wpas_ctrl_nfc_get_handover_req(
5372 wpa_s, buf + 21, reply, reply_size);
5373 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
5374 reply_len = wpas_ctrl_nfc_get_handover_sel(
5375 wpa_s, buf + 21, reply, reply_size);
5376 } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
5377 reply_len = wpas_ctrl_nfc_rx_handover_req(
5378 wpa_s, buf + 20, reply, reply_size);
5379 } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
5380 if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
5381 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005382 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
5383 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
5384 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005385#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005386 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
5387 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
5388 reply_len = -1;
5389#ifdef CONFIG_AP
5390 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
5391 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
5392 wpa_s, buf + 11, reply, reply_size);
5393#endif /* CONFIG_AP */
5394#ifdef CONFIG_WPS_ER
5395 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
5396 if (wpas_wps_er_start(wpa_s, NULL))
5397 reply_len = -1;
5398 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
5399 if (wpas_wps_er_start(wpa_s, buf + 13))
5400 reply_len = -1;
5401 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
5402 if (wpas_wps_er_stop(wpa_s))
5403 reply_len = -1;
5404 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
5405 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
5406 reply_len = -1;
5407 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
5408 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
5409 if (ret == -2) {
5410 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5411 reply_len = 17;
5412 } else if (ret == -3) {
5413 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
5414 reply_len = 18;
5415 } else if (ret == -4) {
5416 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
5417 reply_len = 20;
5418 } else if (ret)
5419 reply_len = -1;
5420 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
5421 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
5422 reply_len = -1;
5423 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
5424 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
5425 buf + 18))
5426 reply_len = -1;
5427 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
5428 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
5429 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005430#ifdef CONFIG_WPS_NFC
5431 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
5432 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
5433 wpa_s, buf + 24, reply, reply_size);
5434#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005435#endif /* CONFIG_WPS_ER */
5436#endif /* CONFIG_WPS */
5437#ifdef CONFIG_IBSS_RSN
5438 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
5439 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
5440 reply_len = -1;
5441#endif /* CONFIG_IBSS_RSN */
5442#ifdef CONFIG_P2P
5443 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
5444 if (p2p_ctrl_find(wpa_s, buf + 9))
5445 reply_len = -1;
5446 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
5447 if (p2p_ctrl_find(wpa_s, ""))
5448 reply_len = -1;
5449 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
5450 wpas_p2p_stop_find(wpa_s);
5451 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
5452 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
5453 reply_size);
5454 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
5455 if (p2p_ctrl_listen(wpa_s, buf + 11))
5456 reply_len = -1;
5457 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
5458 if (p2p_ctrl_listen(wpa_s, ""))
5459 reply_len = -1;
5460 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
5461 if (wpas_p2p_group_remove(wpa_s, buf + 17))
5462 reply_len = -1;
5463 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005464 if (wpas_p2p_group_add(wpa_s, 0, 0, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005465 reply_len = -1;
5466 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
5467 if (p2p_ctrl_group_add(wpa_s, buf + 14))
5468 reply_len = -1;
5469 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
5470 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
5471 reply_len = -1;
5472 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
5473 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
5474 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
5475 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
5476 reply_size);
5477 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
5478 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
5479 reply_len = -1;
5480 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
5481 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
5482 reply_len = -1;
5483 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
5484 wpas_p2p_sd_service_update(wpa_s);
5485 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
5486 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
5487 reply_len = -1;
5488 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
5489 wpas_p2p_service_flush(wpa_s);
5490 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
5491 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
5492 reply_len = -1;
5493 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
5494 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
5495 reply_len = -1;
5496 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
5497 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
5498 reply_len = -1;
5499 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
5500 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
5501 reply_len = -1;
5502 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
5503 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
5504 reply_size);
5505 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
5506 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
5507 reply_len = -1;
5508 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005509 p2p_ctrl_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005510 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
5511 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
5512 reply_len = -1;
5513 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
5514 if (wpas_p2p_cancel(wpa_s))
5515 reply_len = -1;
5516 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
5517 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
5518 reply_len = -1;
5519 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
5520 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
5521 reply_len = -1;
5522 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
5523 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
5524 reply_len = -1;
5525 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
5526 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
5527 reply_len = -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005528 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
5529 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
5530 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005531#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005532#ifdef CONFIG_WIFI_DISPLAY
5533 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
5534 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
5535 reply_len = -1;
5536 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
5537 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
5538 reply, reply_size);
5539#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005540#ifdef CONFIG_INTERWORKING
5541 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
5542 if (interworking_fetch_anqp(wpa_s) < 0)
5543 reply_len = -1;
5544 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
5545 interworking_stop_fetch_anqp(wpa_s);
5546 } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
5547 if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
5548 NULL) < 0)
5549 reply_len = -1;
5550 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
5551 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
5552 reply_len = -1;
5553 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
5554 if (get_anqp(wpa_s, buf + 9) < 0)
5555 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005556 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
5557 if (gas_request(wpa_s, buf + 12) < 0)
5558 reply_len = -1;
5559 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
5560 reply_len = gas_response_get(wpa_s, buf + 17, reply,
5561 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005562#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005563#ifdef CONFIG_HS20
5564 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
5565 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
5566 reply_len = -1;
5567 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
5568 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
5569 reply_len = -1;
5570#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005571 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
5572 {
5573 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
5574 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
5575 reply_len = -1;
5576 else
5577 ctrl_rsp = 1;
5578 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
5579 if (wpa_supplicant_reload_configuration(wpa_s))
5580 reply_len = -1;
5581 } else if (os_strcmp(buf, "TERMINATE") == 0) {
5582 wpa_supplicant_terminate_proc(wpa_s->global);
5583 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
5584 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
5585 reply_len = -1;
Dmitry Shmidte19501d2011-03-16 14:32:18 -07005586 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005587 reply_len = wpa_supplicant_ctrl_iface_blacklist(
5588 wpa_s, buf + 9, reply, reply_size);
5589 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
5590 reply_len = wpa_supplicant_ctrl_iface_log_level(
5591 wpa_s, buf + 9, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005592 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
5593 reply_len = wpa_supplicant_ctrl_iface_list_networks(
5594 wpa_s, reply, reply_size);
5595 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005596#ifdef CONFIG_SME
5597 wpa_s->sme.prev_bssid_set = 0;
5598#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005599 wpa_s->reassociate = 0;
5600 wpa_s->disconnected = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005601 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005602 wpa_supplicant_cancel_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603 wpa_supplicant_deauthenticate(wpa_s,
5604 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005605 } else if (os_strcmp(buf, "SCAN") == 0 ||
5606 os_strncmp(buf, "SCAN ", 5) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005607 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5608 reply_len = -1;
5609 else {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005610 if (os_strlen(buf) > 4 &&
5611 os_strncasecmp(buf + 5, "TYPE=ONLY", 9) == 0)
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08005612 wpa_s->scan_res_handler = scan_only_handler;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005613 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005614 ((wpa_s->wpa_state <= WPA_SCANNING) ||
5615 (wpa_s->wpa_state == WPA_COMPLETED))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005616 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005617 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005618 wpa_supplicant_req_scan(wpa_s, 0, 0);
5619 } else if (wpa_s->sched_scanning) {
5620 wpa_printf(MSG_DEBUG, "Stop ongoing "
5621 "sched_scan to allow requested "
5622 "full scan to proceed");
5623 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005624 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005625 wpa_supplicant_req_scan(wpa_s, 0, 0);
5626 } else {
5627 wpa_printf(MSG_DEBUG, "Ongoing scan action - "
5628 "reject new request");
5629 reply_len = os_snprintf(reply, reply_size,
5630 "FAIL-BUSY\n");
5631 }
5632 }
5633 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
5634 reply_len = wpa_supplicant_ctrl_iface_scan_results(
5635 wpa_s, reply, reply_size);
5636 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
5637 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
5638 reply_len = -1;
5639 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
5640 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
5641 reply_len = -1;
5642 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
5643 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
5644 reply_len = -1;
5645 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
5646 reply_len = wpa_supplicant_ctrl_iface_add_network(
5647 wpa_s, reply, reply_size);
5648 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
5649 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
5650 reply_len = -1;
5651 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
5652 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
5653 reply_len = -1;
5654 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
5655 reply_len = wpa_supplicant_ctrl_iface_get_network(
5656 wpa_s, buf + 12, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005657 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
5658 reply_len = wpa_supplicant_ctrl_iface_list_creds(
5659 wpa_s, reply, reply_size);
5660 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
5661 reply_len = wpa_supplicant_ctrl_iface_add_cred(
5662 wpa_s, reply, reply_size);
5663 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
5664 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
5665 reply_len = -1;
5666 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
5667 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
5668 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005669#ifndef CONFIG_NO_CONFIG_WRITE
5670 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
5671 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
5672 reply_len = -1;
5673#endif /* CONFIG_NO_CONFIG_WRITE */
5674 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
5675 reply_len = wpa_supplicant_ctrl_iface_get_capability(
5676 wpa_s, buf + 15, reply, reply_size);
5677 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
5678 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
5679 reply_len = -1;
5680 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
5681 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
5682 reply_len = -1;
5683 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
5684 reply_len = wpa_supplicant_global_iface_list(
5685 wpa_s->global, reply, reply_size);
5686 } else if (os_strcmp(buf, "INTERFACES") == 0) {
5687 reply_len = wpa_supplicant_global_iface_interfaces(
5688 wpa_s->global, reply, reply_size);
5689 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
5690 reply_len = wpa_supplicant_ctrl_iface_bss(
5691 wpa_s, buf + 4, reply, reply_size);
5692#ifdef CONFIG_AP
5693 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
5694 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
5695 } else if (os_strncmp(buf, "STA ", 4) == 0) {
5696 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
5697 reply_size);
5698 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
5699 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
5700 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005701 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
5702 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
5703 reply_len = -1;
5704 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
5705 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
5706 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005707#endif /* CONFIG_AP */
5708 } else if (os_strcmp(buf, "SUSPEND") == 0) {
5709 wpas_notify_suspend(wpa_s->global);
5710 } else if (os_strcmp(buf, "RESUME") == 0) {
5711 wpas_notify_resume(wpa_s->global);
5712 } else if (os_strcmp(buf, "DROP_SA") == 0) {
5713 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
5714 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
5715 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
5716 reply_len = -1;
5717 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
5718 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
5719 reply_len = -1;
5720 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
5721 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
5722 reply_len = -1;
5723 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
5724 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
5725 buf + 17))
5726 reply_len = -1;
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07005727 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
5728 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
5729 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005730#ifdef CONFIG_TDLS
5731 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
5732 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
5733 reply_len = -1;
5734 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
5735 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
5736 reply_len = -1;
5737 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
5738 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
5739 reply_len = -1;
5740#endif /* CONFIG_TDLS */
5741 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
5742 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
5743 reply_size);
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005744 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
5745 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
5746 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005747#ifdef CONFIG_AUTOSCAN
5748 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
5749 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
5750 reply_len = -1;
5751#endif /* CONFIG_AUTOSCAN */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005752#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005753 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
5754 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
5755 reply_size);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005756#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005757 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005758 pmksa_cache_clear_current(wpa_s->wpa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005759 eapol_sm_request_reauth(wpa_s->eapol);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005760#ifdef CONFIG_WNM
5761 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
5762 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
5763 reply_len = -1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005764 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
5765 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
5766 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005767#endif /* CONFIG_WNM */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005768 } else if (os_strcmp(buf, "FLUSH") == 0) {
5769 wpa_supplicant_ctrl_iface_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005770 } else {
5771 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
5772 reply_len = 16;
5773 }
5774
5775 if (reply_len < 0) {
5776 os_memcpy(reply, "FAIL\n", 5);
5777 reply_len = 5;
5778 }
5779
5780 if (ctrl_rsp)
5781 eapol_sm_notify_ctrl_response(wpa_s->eapol);
5782
5783 *resp_len = reply_len;
5784 return reply;
5785}
5786
5787
5788static int wpa_supplicant_global_iface_add(struct wpa_global *global,
5789 char *cmd)
5790{
5791 struct wpa_interface iface;
5792 char *pos;
5793
5794 /*
5795 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
5796 * TAB<bridge_ifname>
5797 */
5798 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
5799
5800 os_memset(&iface, 0, sizeof(iface));
5801
5802 do {
5803 iface.ifname = pos = cmd;
5804 pos = os_strchr(pos, '\t');
5805 if (pos)
5806 *pos++ = '\0';
5807 if (iface.ifname[0] == '\0')
5808 return -1;
5809 if (pos == NULL)
5810 break;
5811
5812 iface.confname = pos;
5813 pos = os_strchr(pos, '\t');
5814 if (pos)
5815 *pos++ = '\0';
5816 if (iface.confname[0] == '\0')
5817 iface.confname = NULL;
5818 if (pos == NULL)
5819 break;
5820
5821 iface.driver = pos;
5822 pos = os_strchr(pos, '\t');
5823 if (pos)
5824 *pos++ = '\0';
5825 if (iface.driver[0] == '\0')
5826 iface.driver = NULL;
5827 if (pos == NULL)
5828 break;
5829
5830 iface.ctrl_interface = pos;
5831 pos = os_strchr(pos, '\t');
5832 if (pos)
5833 *pos++ = '\0';
5834 if (iface.ctrl_interface[0] == '\0')
5835 iface.ctrl_interface = NULL;
5836 if (pos == NULL)
5837 break;
5838
5839 iface.driver_param = pos;
5840 pos = os_strchr(pos, '\t');
5841 if (pos)
5842 *pos++ = '\0';
5843 if (iface.driver_param[0] == '\0')
5844 iface.driver_param = NULL;
5845 if (pos == NULL)
5846 break;
5847
5848 iface.bridge_ifname = pos;
5849 pos = os_strchr(pos, '\t');
5850 if (pos)
5851 *pos++ = '\0';
5852 if (iface.bridge_ifname[0] == '\0')
5853 iface.bridge_ifname = NULL;
5854 if (pos == NULL)
5855 break;
5856 } while (0);
5857
5858 if (wpa_supplicant_get_iface(global, iface.ifname))
5859 return -1;
5860
5861 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
5862}
5863
5864
5865static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
5866 char *cmd)
5867{
5868 struct wpa_supplicant *wpa_s;
5869
5870 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
5871
5872 wpa_s = wpa_supplicant_get_iface(global, cmd);
5873 if (wpa_s == NULL)
5874 return -1;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07005875 return wpa_supplicant_remove_iface(global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005876}
5877
5878
5879static void wpa_free_iface_info(struct wpa_interface_info *iface)
5880{
5881 struct wpa_interface_info *prev;
5882
5883 while (iface) {
5884 prev = iface;
5885 iface = iface->next;
5886
5887 os_free(prev->ifname);
5888 os_free(prev->desc);
5889 os_free(prev);
5890 }
5891}
5892
5893
5894static int wpa_supplicant_global_iface_list(struct wpa_global *global,
5895 char *buf, int len)
5896{
5897 int i, res;
5898 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
5899 char *pos, *end;
5900
5901 for (i = 0; wpa_drivers[i]; i++) {
5902 struct wpa_driver_ops *drv = wpa_drivers[i];
5903 if (drv->get_interfaces == NULL)
5904 continue;
5905 tmp = drv->get_interfaces(global->drv_priv[i]);
5906 if (tmp == NULL)
5907 continue;
5908
5909 if (last == NULL)
5910 iface = last = tmp;
5911 else
5912 last->next = tmp;
5913 while (last->next)
5914 last = last->next;
5915 }
5916
5917 pos = buf;
5918 end = buf + len;
5919 for (tmp = iface; tmp; tmp = tmp->next) {
5920 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
5921 tmp->drv_name, tmp->ifname,
5922 tmp->desc ? tmp->desc : "");
5923 if (res < 0 || res >= end - pos) {
5924 *pos = '\0';
5925 break;
5926 }
5927 pos += res;
5928 }
5929
5930 wpa_free_iface_info(iface);
5931
5932 return pos - buf;
5933}
5934
5935
5936static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
5937 char *buf, int len)
5938{
5939 int res;
5940 char *pos, *end;
5941 struct wpa_supplicant *wpa_s;
5942
5943 wpa_s = global->ifaces;
5944 pos = buf;
5945 end = buf + len;
5946
5947 while (wpa_s) {
5948 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
5949 if (res < 0 || res >= end - pos) {
5950 *pos = '\0';
5951 break;
5952 }
5953 pos += res;
5954 wpa_s = wpa_s->next;
5955 }
5956 return pos - buf;
5957}
5958
5959
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005960static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
5961 const char *ifname,
5962 char *cmd, size_t *resp_len)
5963{
5964 struct wpa_supplicant *wpa_s;
5965
5966 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5967 if (os_strcmp(ifname, wpa_s->ifname) == 0)
5968 break;
5969 }
5970
5971 if (wpa_s == NULL) {
5972 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
5973 if (resp)
5974 *resp_len = os_strlen(resp);
5975 else
5976 *resp_len = 1;
5977 return resp;
5978 }
5979
5980 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
5981}
5982
5983
5984static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
5985 char *buf, size_t *resp_len)
5986{
5987#ifdef CONFIG_P2P
5988 static const char * cmd[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07005989#ifdef ANDROID_P2P
5990 "LIST_NETWORKS",
5991 "SAVE_CONFIG",
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07005992#endif
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005993 "P2P_FIND",
5994 "P2P_STOP_FIND",
5995 "P2P_LISTEN",
5996 "P2P_GROUP_ADD",
5997 "P2P_GET_PASSPHRASE",
5998 "P2P_SERVICE_UPDATE",
5999 "P2P_SERVICE_FLUSH",
6000 "P2P_FLUSH",
6001 "P2P_CANCEL",
6002 "P2P_PRESENCE_REQ",
6003 "P2P_EXT_LISTEN",
6004 NULL
6005 };
6006 static const char * prefix[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07006007#ifdef ANDROID_P2P
6008 "DRIVER ",
6009 "GET_NETWORK ",
6010 "REMOVE_NETWORK ",
6011 "SET ",
6012#endif
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006013 "P2P_FIND ",
6014 "P2P_CONNECT ",
6015 "P2P_LISTEN ",
6016 "P2P_GROUP_REMOVE ",
6017 "P2P_GROUP_ADD ",
6018 "P2P_PROV_DISC ",
6019 "P2P_SERV_DISC_REQ ",
6020 "P2P_SERV_DISC_CANCEL_REQ ",
6021 "P2P_SERV_DISC_RESP ",
6022 "P2P_SERV_DISC_EXTERNAL ",
6023 "P2P_SERVICE_ADD ",
6024 "P2P_SERVICE_DEL ",
6025 "P2P_REJECT ",
6026 "P2P_INVITE ",
6027 "P2P_PEER ",
6028 "P2P_SET ",
6029 "P2P_UNAUTHORIZE ",
6030 "P2P_PRESENCE_REQ ",
6031 "P2P_EXT_LISTEN ",
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006032 "P2P_REMOVE_CLIENT ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006033 NULL
6034 };
6035 int found = 0;
6036 int i;
6037
6038 if (global->p2p_init_wpa_s == NULL)
6039 return NULL;
6040
6041 for (i = 0; !found && cmd[i]; i++) {
6042 if (os_strcmp(buf, cmd[i]) == 0)
6043 found = 1;
6044 }
6045
6046 for (i = 0; !found && prefix[i]; i++) {
6047 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
6048 found = 1;
6049 }
6050
6051 if (found)
6052 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6053 buf, resp_len);
6054#endif /* CONFIG_P2P */
6055 return NULL;
6056}
6057
6058
6059static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
6060 char *buf, size_t *resp_len)
6061{
6062#ifdef CONFIG_WIFI_DISPLAY
6063 if (global->p2p_init_wpa_s == NULL)
6064 return NULL;
6065 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
6066 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
6067 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6068 buf, resp_len);
6069#endif /* CONFIG_WIFI_DISPLAY */
6070 return NULL;
6071}
6072
6073
6074static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
6075 char *buf, size_t *resp_len)
6076{
6077 char *ret;
6078
6079 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
6080 if (ret)
6081 return ret;
6082
6083 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
6084 if (ret)
6085 return ret;
6086
6087 return NULL;
6088}
6089
6090
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006091static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
6092{
6093 char *value;
6094
6095 value = os_strchr(cmd, ' ');
6096 if (value == NULL)
6097 return -1;
6098 *value++ = '\0';
6099
6100 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
6101
6102#ifdef CONFIG_WIFI_DISPLAY
6103 if (os_strcasecmp(cmd, "wifi_display") == 0) {
6104 wifi_display_enable(global, !!atoi(value));
6105 return 0;
6106 }
6107#endif /* CONFIG_WIFI_DISPLAY */
6108
6109 return -1;
6110}
6111
6112
6113#ifndef CONFIG_NO_CONFIG_WRITE
6114static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
6115{
6116 int ret = 0;
6117 struct wpa_supplicant *wpa_s;
6118
6119 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6120 if (!wpa_s->conf->update_config) {
6121 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
6122 continue;
6123 }
6124
6125 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
6126 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
6127 ret = 1;
6128 } else {
6129 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
6130 }
6131 }
6132
6133 return ret;
6134}
6135#endif /* CONFIG_NO_CONFIG_WRITE */
6136
6137
6138static int wpas_global_ctrl_iface_status(struct wpa_global *global,
6139 char *buf, size_t buflen)
6140{
6141 char *pos, *end;
6142 int ret;
6143 struct wpa_supplicant *wpa_s;
6144
6145 pos = buf;
6146 end = buf + buflen;
6147
6148#ifdef CONFIG_P2P
6149 if (global->p2p && !global->p2p_disabled) {
6150 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
6151 "\n"
6152 "p2p_state=%s\n",
6153 MAC2STR(global->p2p_dev_addr),
6154 p2p_get_state_txt(global->p2p));
6155 if (ret < 0 || ret >= end - pos)
6156 return pos - buf;
6157 pos += ret;
6158 } else if (global->p2p) {
6159 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
6160 if (ret < 0 || ret >= end - pos)
6161 return pos - buf;
6162 pos += ret;
6163 }
6164#endif /* CONFIG_P2P */
6165
6166#ifdef CONFIG_WIFI_DISPLAY
6167 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
6168 !!global->wifi_display);
6169 if (ret < 0 || ret >= end - pos)
6170 return pos - buf;
6171 pos += ret;
6172#endif /* CONFIG_WIFI_DISPLAY */
6173
6174 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6175 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
6176 "address=" MACSTR "\n",
6177 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
6178 if (ret < 0 || ret >= end - pos)
6179 return pos - buf;
6180 pos += ret;
6181 }
6182
6183 return pos - buf;
6184}
6185
6186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006187char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
6188 char *buf, size_t *resp_len)
6189{
6190 char *reply;
6191 const int reply_size = 2048;
6192 int reply_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006193 int level = MSG_DEBUG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006194
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006195 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
6196 char *pos = os_strchr(buf + 7, ' ');
6197 if (pos) {
6198 *pos++ = '\0';
6199 return wpas_global_ctrl_iface_ifname(global,
6200 buf + 7, pos,
6201 resp_len);
6202 }
6203 }
6204
6205 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
6206 if (reply)
6207 return reply;
6208
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006209 if (os_strcmp(buf, "PING") == 0)
6210 level = MSG_EXCESSIVE;
6211 wpa_hexdump_ascii(level, "RX global ctrl_iface",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006212 (const u8 *) buf, os_strlen(buf));
6213
6214 reply = os_malloc(reply_size);
6215 if (reply == NULL) {
6216 *resp_len = 1;
6217 return NULL;
6218 }
6219
6220 os_memcpy(reply, "OK\n", 3);
6221 reply_len = 3;
6222
6223 if (os_strcmp(buf, "PING") == 0) {
6224 os_memcpy(reply, "PONG\n", 5);
6225 reply_len = 5;
6226 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
6227 if (wpa_supplicant_global_iface_add(global, buf + 14))
6228 reply_len = -1;
6229 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
6230 if (wpa_supplicant_global_iface_remove(global, buf + 17))
6231 reply_len = -1;
6232 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
6233 reply_len = wpa_supplicant_global_iface_list(
6234 global, reply, reply_size);
6235 } else if (os_strcmp(buf, "INTERFACES") == 0) {
6236 reply_len = wpa_supplicant_global_iface_interfaces(
6237 global, reply, reply_size);
6238 } else if (os_strcmp(buf, "TERMINATE") == 0) {
6239 wpa_supplicant_terminate_proc(global);
6240 } else if (os_strcmp(buf, "SUSPEND") == 0) {
6241 wpas_notify_suspend(global);
6242 } else if (os_strcmp(buf, "RESUME") == 0) {
6243 wpas_notify_resume(global);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006244 } else if (os_strncmp(buf, "SET ", 4) == 0) {
6245 if (wpas_global_ctrl_iface_set(global, buf + 4))
6246 reply_len = -1;
6247#ifndef CONFIG_NO_CONFIG_WRITE
6248 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
6249 if (wpas_global_ctrl_iface_save_config(global))
6250 reply_len = -1;
6251#endif /* CONFIG_NO_CONFIG_WRITE */
6252 } else if (os_strcmp(buf, "STATUS") == 0) {
6253 reply_len = wpas_global_ctrl_iface_status(global, reply,
6254 reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006255 } else {
6256 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
6257 reply_len = 16;
6258 }
6259
6260 if (reply_len < 0) {
6261 os_memcpy(reply, "FAIL\n", 5);
6262 reply_len = 5;
6263 }
6264
6265 *resp_len = reply_len;
6266 return reply;
6267}