blob: 4ff70c73152fd7bad5c0d4c3ad1eaeea4909b154 [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 Shmidt4d9ecd82013-11-05 18:08:27 -080061 if ((wpa_s->wpa_state > WPA_SCANNING) &&
62 (wpa_s->wpa_state <= WPA_COMPLETED)) {
63 wpa_printf(MSG_ERROR, "PNO: In assoc process");
64 return -EAGAIN;
65 }
66
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080067 if (wpa_s->wpa_state == WPA_SCANNING) {
68 wpa_supplicant_cancel_sched_scan(wpa_s);
69 wpa_supplicant_cancel_scan(wpa_s);
70 }
71
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080072 os_memset(&params, 0, sizeof(params));
73
74 num_ssid = 0;
75 ssid = wpa_s->conf->ssid;
76 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070077 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080078 num_ssid++;
79 ssid = ssid->next;
80 }
81 if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
82 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
83 "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
84 num_ssid = WPAS_MAX_SCAN_SSIDS;
85 }
86
87 if (num_ssid == 0) {
88 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
89 return -1;
90 }
91
92 params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
93 num_ssid);
94 if (params.filter_ssids == NULL)
95 return -1;
96 i = 0;
97 ssid = wpa_s->conf->ssid;
98 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070099 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800100 params.ssids[i].ssid = ssid->ssid;
101 params.ssids[i].ssid_len = ssid->ssid_len;
102 params.num_ssids++;
103 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
104 ssid->ssid_len);
105 params.filter_ssids[i].ssid_len = ssid->ssid_len;
106 params.num_filter_ssids++;
107 i++;
108 if (i == num_ssid)
109 break;
110 }
111 ssid = ssid->next;
112 }
113
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700114 if (wpa_s->conf->filter_rssi)
115 params.filter_rssi = wpa_s->conf->filter_rssi;
116
Dmitry Shmidt56052862013-10-04 10:23:25 -0700117 interval = wpa_s->conf->sched_scan_interval ?
118 wpa_s->conf->sched_scan_interval : 10;
119
120 ret = wpa_drv_sched_scan(wpa_s, &params, interval * 1000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800121 os_free(params.filter_ssids);
122 if (ret == 0)
123 wpa_s->pno = 1;
124 return ret;
125}
126
127
128static int pno_stop(struct wpa_supplicant *wpa_s)
129{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800130 int ret = 0;
131
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800132 if (wpa_s->pno) {
133 wpa_s->pno = 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800134 ret = wpa_drv_stop_sched_scan(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800135 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800136
137 if (wpa_s->wpa_state == WPA_SCANNING)
138 wpa_supplicant_req_scan(wpa_s, 0, 0);
139
140 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800141}
142
143
Dmitry Shmidt04949592012-07-19 12:16:46 -0700144static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
145{
146 char *pos;
147 u8 addr[ETH_ALEN], *filter = NULL, *n;
148 size_t count = 0;
149
150 pos = val;
151 while (pos) {
152 if (*pos == '\0')
153 break;
154 if (hwaddr_aton(pos, addr)) {
155 os_free(filter);
156 return -1;
157 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700158 n = os_realloc_array(filter, count + 1, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700159 if (n == NULL) {
160 os_free(filter);
161 return -1;
162 }
163 filter = n;
164 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
165 count++;
166
167 pos = os_strchr(pos, ' ');
168 if (pos)
169 pos++;
170 }
171
172 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
173 os_free(wpa_s->bssid_filter);
174 wpa_s->bssid_filter = filter;
175 wpa_s->bssid_filter_count = count;
176
177 return 0;
178}
179
180
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800181static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
182{
183 char *pos;
184 u8 addr[ETH_ALEN], *bssid = NULL, *n;
185 struct wpa_ssid_value *ssid = NULL, *ns;
186 size_t count = 0, ssid_count = 0;
187 struct wpa_ssid *c;
188
189 /*
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800190 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800191 * SSID_SPEC ::= ssid <SSID_HEX>
192 * BSSID_SPEC ::= bssid <BSSID_HEX>
193 */
194
195 pos = val;
196 while (pos) {
197 if (*pos == '\0')
198 break;
199 if (os_strncmp(pos, "bssid ", 6) == 0) {
200 int res;
201 pos += 6;
202 res = hwaddr_aton2(pos, addr);
203 if (res < 0) {
204 os_free(ssid);
205 os_free(bssid);
206 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
207 "BSSID value '%s'", pos);
208 return -1;
209 }
210 pos += res;
211 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
212 if (n == NULL) {
213 os_free(ssid);
214 os_free(bssid);
215 return -1;
216 }
217 bssid = n;
218 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
219 count++;
220 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
221 char *end;
222 pos += 5;
223
224 end = pos;
225 while (*end) {
226 if (*end == '\0' || *end == ' ')
227 break;
228 end++;
229 }
230
231 ns = os_realloc_array(ssid, ssid_count + 1,
232 sizeof(struct wpa_ssid_value));
233 if (ns == NULL) {
234 os_free(ssid);
235 os_free(bssid);
236 return -1;
237 }
238 ssid = ns;
239
240 if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
241 hexstr2bin(pos, ssid[ssid_count].ssid,
242 (end - pos) / 2) < 0) {
243 os_free(ssid);
244 os_free(bssid);
245 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
246 "SSID value '%s'", pos);
247 return -1;
248 }
249 ssid[ssid_count].ssid_len = (end - pos) / 2;
250 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
251 ssid[ssid_count].ssid,
252 ssid[ssid_count].ssid_len);
253 ssid_count++;
254 pos = end;
255 } else {
256 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
257 "'%s'", pos);
258 os_free(ssid);
259 os_free(bssid);
260 return -1;
261 }
262
263 pos = os_strchr(pos, ' ');
264 if (pos)
265 pos++;
266 }
267
268 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
269 os_free(wpa_s->disallow_aps_bssid);
270 wpa_s->disallow_aps_bssid = bssid;
271 wpa_s->disallow_aps_bssid_count = count;
272
273 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
274 os_free(wpa_s->disallow_aps_ssid);
275 wpa_s->disallow_aps_ssid = ssid;
276 wpa_s->disallow_aps_ssid_count = ssid_count;
277
278 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
279 return 0;
280
281 c = wpa_s->current_ssid;
282 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
283 return 0;
284
285 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
286 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
287 return 0;
288
289 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
290 "because current AP was marked disallowed");
291
292#ifdef CONFIG_SME
293 wpa_s->sme.prev_bssid_set = 0;
294#endif /* CONFIG_SME */
295 wpa_s->reassociate = 1;
296 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
297 wpa_supplicant_req_scan(wpa_s, 0, 0);
298
299 return 0;
300}
301
302
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
304 char *cmd)
305{
306 char *value;
307 int ret = 0;
308
309 value = os_strchr(cmd, ' ');
310 if (value == NULL)
311 return -1;
312 *value++ = '\0';
313
314 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
315 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
316 eapol_sm_configure(wpa_s->eapol,
317 atoi(value), -1, -1, -1);
318 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
319 eapol_sm_configure(wpa_s->eapol,
320 -1, atoi(value), -1, -1);
321 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
322 eapol_sm_configure(wpa_s->eapol,
323 -1, -1, atoi(value), -1);
324 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
325 eapol_sm_configure(wpa_s->eapol,
326 -1, -1, -1, atoi(value));
327 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
328 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
329 atoi(value)))
330 ret = -1;
331 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
332 0) {
333 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
334 atoi(value)))
335 ret = -1;
336 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
337 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
338 ret = -1;
339 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
340 wpa_s->wps_fragment_size = atoi(value);
341#ifdef CONFIG_WPS_TESTING
342 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
343 long int val;
344 val = strtol(value, NULL, 0);
345 if (val < 0 || val > 0xff) {
346 ret = -1;
347 wpa_printf(MSG_DEBUG, "WPS: Invalid "
348 "wps_version_number %ld", val);
349 } else {
350 wps_version_number = val;
351 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
352 "version %u.%u",
353 (wps_version_number & 0xf0) >> 4,
354 wps_version_number & 0x0f);
355 }
356 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
357 wps_testing_dummy_cred = atoi(value);
358 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
359 wps_testing_dummy_cred);
360#endif /* CONFIG_WPS_TESTING */
361 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
362 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
363 ret = -1;
364#ifdef CONFIG_TDLS_TESTING
365 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
366 extern unsigned int tdls_testing;
367 tdls_testing = strtol(value, NULL, 0);
368 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
369#endif /* CONFIG_TDLS_TESTING */
370#ifdef CONFIG_TDLS
371 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
372 int disabled = atoi(value);
373 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
374 if (disabled) {
375 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
376 ret = -1;
377 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
378 ret = -1;
379 wpa_tdls_enable(wpa_s->wpa, !disabled);
380#endif /* CONFIG_TDLS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800381 } else if (os_strcasecmp(cmd, "pno") == 0) {
382 if (atoi(value))
383 ret = pno_start(wpa_s);
384 else
385 ret = pno_stop(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700386 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
387 int disabled = atoi(value);
388 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
389 ret = -1;
390 else if (disabled)
391 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
392 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
393 if (os_strcmp(value, "disable") == 0)
394 wpa_s->set_sta_uapsd = 0;
395 else {
396 int be, bk, vi, vo;
397 char *pos;
398 /* format: BE,BK,VI,VO;max SP Length */
399 be = atoi(value);
400 pos = os_strchr(value, ',');
401 if (pos == NULL)
402 return -1;
403 pos++;
404 bk = atoi(pos);
405 pos = os_strchr(pos, ',');
406 if (pos == NULL)
407 return -1;
408 pos++;
409 vi = atoi(pos);
410 pos = os_strchr(pos, ',');
411 if (pos == NULL)
412 return -1;
413 pos++;
414 vo = atoi(pos);
415 /* ignore max SP Length for now */
416
417 wpa_s->set_sta_uapsd = 1;
418 wpa_s->sta_uapsd = 0;
419 if (be)
420 wpa_s->sta_uapsd |= BIT(0);
421 if (bk)
422 wpa_s->sta_uapsd |= BIT(1);
423 if (vi)
424 wpa_s->sta_uapsd |= BIT(2);
425 if (vo)
426 wpa_s->sta_uapsd |= BIT(3);
427 }
Jouni Malinen21d6bc82012-04-10 16:17:59 -0700428 } else if (os_strcasecmp(cmd, "ps") == 0) {
429 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700430#ifdef CONFIG_WIFI_DISPLAY
431 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
432 wifi_display_enable(wpa_s->global, !!atoi(value));
433#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700434 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
435 ret = set_bssid_filter(wpa_s, value);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800436 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
437 ret = set_disallow_aps(wpa_s, value);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800438 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
439 wpa_s->no_keep_alive = !!atoi(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440 } else {
441 value[-1] = '=';
442 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
443 if (ret == 0)
444 wpa_supplicant_update_config(wpa_s);
445 }
446
447 return ret;
448}
449
450
451static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
452 char *cmd, char *buf, size_t buflen)
453{
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700454 int res = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455
456 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
457
458 if (os_strcmp(cmd, "version") == 0) {
459 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700460 } else if (os_strcasecmp(cmd, "country") == 0) {
461 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
462 res = os_snprintf(buf, buflen, "%c%c",
463 wpa_s->conf->country[0],
464 wpa_s->conf->country[1]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700465#ifdef CONFIG_WIFI_DISPLAY
466 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
467 res = os_snprintf(buf, buflen, "%d",
468 wpa_s->global->wifi_display);
469 if (res < 0 || (unsigned int) res >= buflen)
470 return -1;
471 return res;
472#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700473#ifdef CONFIG_TESTING_GET_GTK
474 } else if (os_strcmp(cmd, "gtk") == 0) {
475 if (wpa_s->last_gtk_len == 0)
476 return -1;
477 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
478 wpa_s->last_gtk_len);
479 return res;
480#endif /* CONFIG_TESTING_GET_GTK */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700481 }
482
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700483 if (res < 0 || (unsigned int) res >= buflen)
484 return -1;
485 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486}
487
488
489#ifdef IEEE8021X_EAPOL
490static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
491 char *addr)
492{
493 u8 bssid[ETH_ALEN];
494 struct wpa_ssid *ssid = wpa_s->current_ssid;
495
496 if (hwaddr_aton(addr, bssid)) {
497 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
498 "'%s'", addr);
499 return -1;
500 }
501
502 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
503 rsn_preauth_deinit(wpa_s->wpa);
504 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
505 return -1;
506
507 return 0;
508}
509#endif /* IEEE8021X_EAPOL */
510
511
512#ifdef CONFIG_PEERKEY
513/* MLME-STKSTART.request(peer) */
514static int wpa_supplicant_ctrl_iface_stkstart(
515 struct wpa_supplicant *wpa_s, char *addr)
516{
517 u8 peer[ETH_ALEN];
518
519 if (hwaddr_aton(addr, peer)) {
520 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
521 "address '%s'", addr);
522 return -1;
523 }
524
525 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
526 MAC2STR(peer));
527
528 return wpa_sm_stkstart(wpa_s->wpa, peer);
529}
530#endif /* CONFIG_PEERKEY */
531
532
533#ifdef CONFIG_TDLS
534
535static int wpa_supplicant_ctrl_iface_tdls_discover(
536 struct wpa_supplicant *wpa_s, char *addr)
537{
538 u8 peer[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800539 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540
541 if (hwaddr_aton(addr, peer)) {
542 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
543 "address '%s'", addr);
544 return -1;
545 }
546
547 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
548 MAC2STR(peer));
549
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800550 if (wpa_tdls_is_external_setup(wpa_s->wpa))
551 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
552 else
553 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
554
555 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556}
557
558
559static int wpa_supplicant_ctrl_iface_tdls_setup(
560 struct wpa_supplicant *wpa_s, char *addr)
561{
562 u8 peer[ETH_ALEN];
563 int ret;
564
565 if (hwaddr_aton(addr, peer)) {
566 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
567 "address '%s'", addr);
568 return -1;
569 }
570
571 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
572 MAC2STR(peer));
573
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800574 wpa_tdls_remove(wpa_s->wpa, peer);
575
576 if (wpa_tdls_is_external_setup(wpa_s->wpa))
577 ret = wpa_tdls_start(wpa_s->wpa, peer);
578 else
579 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700581 return ret;
582}
583
584
585static int wpa_supplicant_ctrl_iface_tdls_teardown(
586 struct wpa_supplicant *wpa_s, char *addr)
587{
588 u8 peer[ETH_ALEN];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700589 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590
591 if (hwaddr_aton(addr, peer)) {
592 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
593 "address '%s'", addr);
594 return -1;
595 }
596
597 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
598 MAC2STR(peer));
599
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700600 if (wpa_tdls_is_external_setup(wpa_s->wpa))
601 ret = wpa_tdls_teardown_link(
602 wpa_s->wpa, peer,
603 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
604 else
605 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
606
607 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608}
609
610#endif /* CONFIG_TDLS */
611
612
613#ifdef CONFIG_IEEE80211R
614static int wpa_supplicant_ctrl_iface_ft_ds(
615 struct wpa_supplicant *wpa_s, char *addr)
616{
617 u8 target_ap[ETH_ALEN];
618 struct wpa_bss *bss;
619 const u8 *mdie;
620
621 if (hwaddr_aton(addr, target_ap)) {
622 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
623 "address '%s'", addr);
624 return -1;
625 }
626
627 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
628
629 bss = wpa_bss_get_bssid(wpa_s, target_ap);
630 if (bss)
631 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
632 else
633 mdie = NULL;
634
635 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
636}
637#endif /* CONFIG_IEEE80211R */
638
639
640#ifdef CONFIG_WPS
641static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
642 char *cmd)
643{
644 u8 bssid[ETH_ALEN], *_bssid = bssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700645#ifdef CONFIG_P2P
646 u8 p2p_dev_addr[ETH_ALEN];
647#endif /* CONFIG_P2P */
648#ifdef CONFIG_AP
649 u8 *_p2p_dev_addr = NULL;
650#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800651
Dmitry Shmidtc0a8db02013-11-08 11:18:33 -0800652 if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700653 _bssid = NULL;
654#ifdef CONFIG_P2P
655 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
656 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
657 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
658 "P2P Device Address '%s'",
659 cmd + 13);
660 return -1;
661 }
662 _p2p_dev_addr = p2p_dev_addr;
663#endif /* CONFIG_P2P */
664 } else if (hwaddr_aton(cmd, bssid)) {
665 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
666 cmd);
667 return -1;
668 }
669
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800670#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671 if (wpa_s->ap_iface)
672 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
673#endif /* CONFIG_AP */
674
675 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
676}
677
678
679static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
680 char *cmd, char *buf,
681 size_t buflen)
682{
683 u8 bssid[ETH_ALEN], *_bssid = bssid;
684 char *pin;
685 int ret;
686
687 pin = os_strchr(cmd, ' ');
688 if (pin)
689 *pin++ = '\0';
690
691 if (os_strcmp(cmd, "any") == 0)
692 _bssid = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800693 else if (os_strcmp(cmd, "get") == 0) {
694 ret = wps_generate_pin();
695 goto done;
696 } else if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
698 cmd);
699 return -1;
700 }
701
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800702#ifdef CONFIG_AP
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800703 if (wpa_s->ap_iface) {
704 int timeout = 0;
705 char *pos;
706
707 if (pin) {
708 pos = os_strchr(pin, ' ');
709 if (pos) {
710 *pos++ = '\0';
711 timeout = atoi(pos);
712 }
713 }
714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800716 buf, buflen, timeout);
717 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718#endif /* CONFIG_AP */
719
720 if (pin) {
721 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
722 DEV_PW_DEFAULT);
723 if (ret < 0)
724 return -1;
725 ret = os_snprintf(buf, buflen, "%s", pin);
726 if (ret < 0 || (size_t) ret >= buflen)
727 return -1;
728 return ret;
729 }
730
731 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
732 if (ret < 0)
733 return -1;
734
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800735done:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736 /* Return the generated PIN */
737 ret = os_snprintf(buf, buflen, "%08d", ret);
738 if (ret < 0 || (size_t) ret >= buflen)
739 return -1;
740 return ret;
741}
742
743
744static int wpa_supplicant_ctrl_iface_wps_check_pin(
745 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
746{
747 char pin[9];
748 size_t len;
749 char *pos;
750 int ret;
751
752 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
753 (u8 *) cmd, os_strlen(cmd));
754 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
755 if (*pos < '0' || *pos > '9')
756 continue;
757 pin[len++] = *pos;
758 if (len == 9) {
759 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
760 return -1;
761 }
762 }
763 if (len != 4 && len != 8) {
764 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
765 return -1;
766 }
767 pin[len] = '\0';
768
769 if (len == 8) {
770 unsigned int pin_val;
771 pin_val = atoi(pin);
772 if (!wps_pin_valid(pin_val)) {
773 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
774 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
775 if (ret < 0 || (size_t) ret >= buflen)
776 return -1;
777 return ret;
778 }
779 }
780
781 ret = os_snprintf(buf, buflen, "%s", pin);
782 if (ret < 0 || (size_t) ret >= buflen)
783 return -1;
784
785 return ret;
786}
787
788
Dmitry Shmidt04949592012-07-19 12:16:46 -0700789#ifdef CONFIG_WPS_NFC
790
791static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
792 char *cmd)
793{
794 u8 bssid[ETH_ALEN], *_bssid = bssid;
795
796 if (cmd == NULL || cmd[0] == '\0')
797 _bssid = NULL;
798 else if (hwaddr_aton(cmd, bssid))
799 return -1;
800
801 return wpas_wps_start_nfc(wpa_s, _bssid);
802}
803
804
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800805static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
806 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
807{
808 int ndef;
809 struct wpabuf *buf;
810 int res;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700811 char *pos;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800812
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700813 pos = os_strchr(cmd, ' ');
814 if (pos)
815 *pos++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800816 if (os_strcmp(cmd, "WPS") == 0)
817 ndef = 0;
818 else if (os_strcmp(cmd, "NDEF") == 0)
819 ndef = 1;
820 else
821 return -1;
822
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700823 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800824 if (buf == NULL)
825 return -1;
826
827 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
828 wpabuf_len(buf));
829 reply[res++] = '\n';
830 reply[res] = '\0';
831
832 wpabuf_free(buf);
833
834 return res;
835}
836
837
Dmitry Shmidt04949592012-07-19 12:16:46 -0700838static int wpa_supplicant_ctrl_iface_wps_nfc_token(
839 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
840{
841 int ndef;
842 struct wpabuf *buf;
843 int res;
844
845 if (os_strcmp(cmd, "WPS") == 0)
846 ndef = 0;
847 else if (os_strcmp(cmd, "NDEF") == 0)
848 ndef = 1;
849 else
850 return -1;
851
852 buf = wpas_wps_nfc_token(wpa_s, ndef);
853 if (buf == NULL)
854 return -1;
855
856 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
857 wpabuf_len(buf));
858 reply[res++] = '\n';
859 reply[res] = '\0';
860
861 wpabuf_free(buf);
862
863 return res;
864}
865
866
867static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
868 struct wpa_supplicant *wpa_s, char *pos)
869{
870 size_t len;
871 struct wpabuf *buf;
872 int ret;
873
874 len = os_strlen(pos);
875 if (len & 0x01)
876 return -1;
877 len /= 2;
878
879 buf = wpabuf_alloc(len);
880 if (buf == NULL)
881 return -1;
882 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
883 wpabuf_free(buf);
884 return -1;
885 }
886
887 ret = wpas_wps_nfc_tag_read(wpa_s, buf);
888 wpabuf_free(buf);
889
890 return ret;
891}
892
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800893
894static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800895 char *reply, size_t max_len,
896 int cr)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800897{
898 struct wpabuf *buf;
899 int res;
900
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800901 buf = wpas_wps_nfc_handover_req(wpa_s, cr);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800902 if (buf == NULL)
903 return -1;
904
905 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
906 wpabuf_len(buf));
907 reply[res++] = '\n';
908 reply[res] = '\0';
909
910 wpabuf_free(buf);
911
912 return res;
913}
914
915
916static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
917 char *cmd, char *reply,
918 size_t max_len)
919{
920 char *pos;
921
922 pos = os_strchr(cmd, ' ');
923 if (pos == NULL)
924 return -1;
925 *pos++ = '\0';
926
927 if (os_strcmp(cmd, "NDEF") != 0)
928 return -1;
929
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800930 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
931 return wpas_ctrl_nfc_get_handover_req_wps(
932 wpa_s, reply, max_len, os_strcmp(pos, "WPS-CR") == 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800933 }
934
935 return -1;
936}
937
938
939static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800940 char *reply, size_t max_len,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800941 int ndef, int cr, char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800942{
943 struct wpabuf *buf;
944 int res;
945
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800946 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800947 if (buf == NULL)
948 return -1;
949
950 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
951 wpabuf_len(buf));
952 reply[res++] = '\n';
953 reply[res] = '\0';
954
955 wpabuf_free(buf);
956
957 return res;
958}
959
960
961static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
962 char *cmd, char *reply,
963 size_t max_len)
964{
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800965 char *pos, *pos2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800966 int ndef;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800967
968 pos = os_strchr(cmd, ' ');
969 if (pos == NULL)
970 return -1;
971 *pos++ = '\0';
972
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800973 if (os_strcmp(cmd, "WPS") == 0)
974 ndef = 0;
975 else if (os_strcmp(cmd, "NDEF") == 0)
976 ndef = 1;
977 else
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800978 return -1;
979
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800980 pos2 = os_strchr(pos, ' ');
981 if (pos2)
982 *pos2++ = '\0';
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800983 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
984 return wpas_ctrl_nfc_get_handover_sel_wps(
985 wpa_s, reply, max_len, ndef,
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -0800986 os_strcmp(pos, "WPS-CR") == 0, pos2);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800987 }
988
989 return -1;
990}
991
992
993static int wpas_ctrl_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
994 char *cmd, char *reply,
995 size_t max_len)
996{
997 size_t len;
998 struct wpabuf *buf;
999 int ret;
1000
1001 len = os_strlen(cmd);
1002 if (len & 0x01)
1003 return -1;
1004 len /= 2;
1005
1006 buf = wpabuf_alloc(len);
1007 if (buf == NULL)
1008 return -1;
1009 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
1010 wpabuf_free(buf);
1011 return -1;
1012 }
1013
1014 ret = wpas_wps_nfc_rx_handover_req(wpa_s, buf);
1015 wpabuf_free(buf);
1016
1017 return ret;
1018}
1019
1020
1021static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
1022 char *cmd)
1023{
1024 size_t len;
1025 struct wpabuf *buf;
1026 int ret;
1027
1028 len = os_strlen(cmd);
1029 if (len & 0x01)
1030 return -1;
1031 len /= 2;
1032
1033 buf = wpabuf_alloc(len);
1034 if (buf == NULL)
1035 return -1;
1036 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
1037 wpabuf_free(buf);
1038 return -1;
1039 }
1040
1041 ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
1042 wpabuf_free(buf);
1043
1044 return ret;
1045}
1046
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001047
1048static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1049 char *cmd)
1050{
1051 size_t len;
1052 struct wpabuf *req, *sel;
1053 int ret;
1054 char *pos, *role, *type, *pos2;
1055
1056 role = cmd;
1057 pos = os_strchr(role, ' ');
1058 if (pos == NULL)
1059 return -1;
1060 *pos++ = '\0';
1061
1062 type = pos;
1063 pos = os_strchr(type, ' ');
1064 if (pos == NULL)
1065 return -1;
1066 *pos++ = '\0';
1067
1068 pos2 = os_strchr(pos, ' ');
1069 if (pos2 == NULL)
1070 return -1;
1071 *pos2++ = '\0';
1072
1073 len = os_strlen(pos);
1074 if (len & 0x01)
1075 return -1;
1076 len /= 2;
1077
1078 req = wpabuf_alloc(len);
1079 if (req == NULL)
1080 return -1;
1081 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
1082 wpabuf_free(req);
1083 return -1;
1084 }
1085
1086 len = os_strlen(pos2);
1087 if (len & 0x01) {
1088 wpabuf_free(req);
1089 return -1;
1090 }
1091 len /= 2;
1092
1093 sel = wpabuf_alloc(len);
1094 if (sel == NULL) {
1095 wpabuf_free(req);
1096 return -1;
1097 }
1098 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
1099 wpabuf_free(req);
1100 wpabuf_free(sel);
1101 return -1;
1102 }
1103
1104 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1105 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
1106 } else {
1107 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1108 "reported: role=%s type=%s", role, type);
1109 ret = -1;
1110 }
1111 wpabuf_free(req);
1112 wpabuf_free(sel);
1113
1114 return ret;
1115}
1116
Dmitry Shmidt04949592012-07-19 12:16:46 -07001117#endif /* CONFIG_WPS_NFC */
1118
1119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001120static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1121 char *cmd)
1122{
1123 u8 bssid[ETH_ALEN];
1124 char *pin;
1125 char *new_ssid;
1126 char *new_auth;
1127 char *new_encr;
1128 char *new_key;
1129 struct wps_new_ap_settings ap;
1130
1131 pin = os_strchr(cmd, ' ');
1132 if (pin == NULL)
1133 return -1;
1134 *pin++ = '\0';
1135
1136 if (hwaddr_aton(cmd, bssid)) {
1137 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1138 cmd);
1139 return -1;
1140 }
1141
1142 new_ssid = os_strchr(pin, ' ');
1143 if (new_ssid == NULL)
1144 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1145 *new_ssid++ = '\0';
1146
1147 new_auth = os_strchr(new_ssid, ' ');
1148 if (new_auth == NULL)
1149 return -1;
1150 *new_auth++ = '\0';
1151
1152 new_encr = os_strchr(new_auth, ' ');
1153 if (new_encr == NULL)
1154 return -1;
1155 *new_encr++ = '\0';
1156
1157 new_key = os_strchr(new_encr, ' ');
1158 if (new_key == NULL)
1159 return -1;
1160 *new_key++ = '\0';
1161
1162 os_memset(&ap, 0, sizeof(ap));
1163 ap.ssid_hex = new_ssid;
1164 ap.auth = new_auth;
1165 ap.encr = new_encr;
1166 ap.key_hex = new_key;
1167 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1168}
1169
1170
1171#ifdef CONFIG_AP
1172static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1173 char *cmd, char *buf,
1174 size_t buflen)
1175{
1176 int timeout = 300;
1177 char *pos;
1178 const char *pin_txt;
1179
1180 if (!wpa_s->ap_iface)
1181 return -1;
1182
1183 pos = os_strchr(cmd, ' ');
1184 if (pos)
1185 *pos++ = '\0';
1186
1187 if (os_strcmp(cmd, "disable") == 0) {
1188 wpas_wps_ap_pin_disable(wpa_s);
1189 return os_snprintf(buf, buflen, "OK\n");
1190 }
1191
1192 if (os_strcmp(cmd, "random") == 0) {
1193 if (pos)
1194 timeout = atoi(pos);
1195 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1196 if (pin_txt == NULL)
1197 return -1;
1198 return os_snprintf(buf, buflen, "%s", pin_txt);
1199 }
1200
1201 if (os_strcmp(cmd, "get") == 0) {
1202 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1203 if (pin_txt == NULL)
1204 return -1;
1205 return os_snprintf(buf, buflen, "%s", pin_txt);
1206 }
1207
1208 if (os_strcmp(cmd, "set") == 0) {
1209 char *pin;
1210 if (pos == NULL)
1211 return -1;
1212 pin = pos;
1213 pos = os_strchr(pos, ' ');
1214 if (pos) {
1215 *pos++ = '\0';
1216 timeout = atoi(pos);
1217 }
1218 if (os_strlen(pin) > buflen)
1219 return -1;
1220 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1221 return -1;
1222 return os_snprintf(buf, buflen, "%s", pin);
1223 }
1224
1225 return -1;
1226}
1227#endif /* CONFIG_AP */
1228
1229
1230#ifdef CONFIG_WPS_ER
1231static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1232 char *cmd)
1233{
1234 char *uuid = cmd, *pin, *pos;
1235 u8 addr_buf[ETH_ALEN], *addr = NULL;
1236 pin = os_strchr(uuid, ' ');
1237 if (pin == NULL)
1238 return -1;
1239 *pin++ = '\0';
1240 pos = os_strchr(pin, ' ');
1241 if (pos) {
1242 *pos++ = '\0';
1243 if (hwaddr_aton(pos, addr_buf) == 0)
1244 addr = addr_buf;
1245 }
1246 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1247}
1248
1249
1250static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1251 char *cmd)
1252{
1253 char *uuid = cmd, *pin;
1254 pin = os_strchr(uuid, ' ');
1255 if (pin == NULL)
1256 return -1;
1257 *pin++ = '\0';
1258 return wpas_wps_er_learn(wpa_s, uuid, pin);
1259}
1260
1261
1262static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1263 struct wpa_supplicant *wpa_s, char *cmd)
1264{
1265 char *uuid = cmd, *id;
1266 id = os_strchr(uuid, ' ');
1267 if (id == NULL)
1268 return -1;
1269 *id++ = '\0';
1270 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1271}
1272
1273
1274static int wpa_supplicant_ctrl_iface_wps_er_config(
1275 struct wpa_supplicant *wpa_s, char *cmd)
1276{
1277 char *pin;
1278 char *new_ssid;
1279 char *new_auth;
1280 char *new_encr;
1281 char *new_key;
1282 struct wps_new_ap_settings ap;
1283
1284 pin = os_strchr(cmd, ' ');
1285 if (pin == NULL)
1286 return -1;
1287 *pin++ = '\0';
1288
1289 new_ssid = os_strchr(pin, ' ');
1290 if (new_ssid == NULL)
1291 return -1;
1292 *new_ssid++ = '\0';
1293
1294 new_auth = os_strchr(new_ssid, ' ');
1295 if (new_auth == NULL)
1296 return -1;
1297 *new_auth++ = '\0';
1298
1299 new_encr = os_strchr(new_auth, ' ');
1300 if (new_encr == NULL)
1301 return -1;
1302 *new_encr++ = '\0';
1303
1304 new_key = os_strchr(new_encr, ' ');
1305 if (new_key == NULL)
1306 return -1;
1307 *new_key++ = '\0';
1308
1309 os_memset(&ap, 0, sizeof(ap));
1310 ap.ssid_hex = new_ssid;
1311 ap.auth = new_auth;
1312 ap.encr = new_encr;
1313 ap.key_hex = new_key;
1314 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1315}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001316
1317
1318#ifdef CONFIG_WPS_NFC
1319static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1320 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1321{
1322 int ndef;
1323 struct wpabuf *buf;
1324 int res;
1325 char *uuid;
1326
1327 uuid = os_strchr(cmd, ' ');
1328 if (uuid == NULL)
1329 return -1;
1330 *uuid++ = '\0';
1331
1332 if (os_strcmp(cmd, "WPS") == 0)
1333 ndef = 0;
1334 else if (os_strcmp(cmd, "NDEF") == 0)
1335 ndef = 1;
1336 else
1337 return -1;
1338
1339 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1340 if (buf == NULL)
1341 return -1;
1342
1343 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1344 wpabuf_len(buf));
1345 reply[res++] = '\n';
1346 reply[res] = '\0';
1347
1348 wpabuf_free(buf);
1349
1350 return res;
1351}
1352#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353#endif /* CONFIG_WPS_ER */
1354
1355#endif /* CONFIG_WPS */
1356
1357
1358#ifdef CONFIG_IBSS_RSN
1359static int wpa_supplicant_ctrl_iface_ibss_rsn(
1360 struct wpa_supplicant *wpa_s, char *addr)
1361{
1362 u8 peer[ETH_ALEN];
1363
1364 if (hwaddr_aton(addr, peer)) {
1365 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1366 "address '%s'", addr);
1367 return -1;
1368 }
1369
1370 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1371 MAC2STR(peer));
1372
1373 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1374}
1375#endif /* CONFIG_IBSS_RSN */
1376
1377
1378static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1379 char *rsp)
1380{
1381#ifdef IEEE8021X_EAPOL
1382 char *pos, *id_pos;
1383 int id;
1384 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385
1386 pos = os_strchr(rsp, '-');
1387 if (pos == NULL)
1388 return -1;
1389 *pos++ = '\0';
1390 id_pos = pos;
1391 pos = os_strchr(pos, ':');
1392 if (pos == NULL)
1393 return -1;
1394 *pos++ = '\0';
1395 id = atoi(id_pos);
1396 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1397 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1398 (u8 *) pos, os_strlen(pos));
1399
1400 ssid = wpa_config_get_network(wpa_s->conf, id);
1401 if (ssid == NULL) {
1402 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1403 "to update", id);
1404 return -1;
1405 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001407 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1408 pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409#else /* IEEE8021X_EAPOL */
1410 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1411 return -1;
1412#endif /* IEEE8021X_EAPOL */
1413}
1414
1415
1416static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1417 const char *params,
1418 char *buf, size_t buflen)
1419{
1420 char *pos, *end, tmp[30];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001421 int res, verbose, wps, ret;
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001422
Dmitry Shmidt56052862013-10-04 10:23:25 -07001423 if (os_strcmp(params, "-DRIVER") == 0)
1424 return wpa_drv_status(wpa_s, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425 verbose = os_strcmp(params, "-VERBOSE") == 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001426 wps = os_strcmp(params, "-WPS") == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427 pos = buf;
1428 end = buf + buflen;
1429 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1430 struct wpa_ssid *ssid = wpa_s->current_ssid;
1431 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1432 MAC2STR(wpa_s->bssid));
1433 if (ret < 0 || ret >= end - pos)
1434 return pos - buf;
1435 pos += ret;
1436 if (ssid) {
1437 u8 *_ssid = ssid->ssid;
1438 size_t ssid_len = ssid->ssid_len;
1439 u8 ssid_buf[MAX_SSID_LEN];
1440 if (ssid_len == 0) {
1441 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1442 if (_res < 0)
1443 ssid_len = 0;
1444 else
1445 ssid_len = _res;
1446 _ssid = ssid_buf;
1447 }
1448 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1449 wpa_ssid_txt(_ssid, ssid_len),
1450 ssid->id);
1451 if (ret < 0 || ret >= end - pos)
1452 return pos - buf;
1453 pos += ret;
1454
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001455 if (wps && ssid->passphrase &&
1456 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1457 (ssid->mode == WPAS_MODE_AP ||
1458 ssid->mode == WPAS_MODE_P2P_GO)) {
1459 ret = os_snprintf(pos, end - pos,
1460 "passphrase=%s\n",
1461 ssid->passphrase);
1462 if (ret < 0 || ret >= end - pos)
1463 return pos - buf;
1464 pos += ret;
1465 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001466 if (ssid->id_str) {
1467 ret = os_snprintf(pos, end - pos,
1468 "id_str=%s\n",
1469 ssid->id_str);
1470 if (ret < 0 || ret >= end - pos)
1471 return pos - buf;
1472 pos += ret;
1473 }
1474
1475 switch (ssid->mode) {
1476 case WPAS_MODE_INFRA:
1477 ret = os_snprintf(pos, end - pos,
1478 "mode=station\n");
1479 break;
1480 case WPAS_MODE_IBSS:
1481 ret = os_snprintf(pos, end - pos,
1482 "mode=IBSS\n");
1483 break;
1484 case WPAS_MODE_AP:
1485 ret = os_snprintf(pos, end - pos,
1486 "mode=AP\n");
1487 break;
1488 case WPAS_MODE_P2P_GO:
1489 ret = os_snprintf(pos, end - pos,
1490 "mode=P2P GO\n");
1491 break;
1492 case WPAS_MODE_P2P_GROUP_FORMATION:
1493 ret = os_snprintf(pos, end - pos,
1494 "mode=P2P GO - group "
1495 "formation\n");
1496 break;
1497 default:
1498 ret = 0;
1499 break;
1500 }
1501 if (ret < 0 || ret >= end - pos)
1502 return pos - buf;
1503 pos += ret;
1504 }
1505
1506#ifdef CONFIG_AP
1507 if (wpa_s->ap_iface) {
1508 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1509 end - pos,
1510 verbose);
1511 } else
1512#endif /* CONFIG_AP */
1513 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1514 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001515#ifdef CONFIG_SAE
1516 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001517#ifdef CONFIG_AP
1518 !wpa_s->ap_iface &&
1519#endif /* CONFIG_AP */
1520 wpa_s->sme.sae.state == SAE_ACCEPTED) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001521 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1522 wpa_s->sme.sae.group);
1523 if (ret < 0 || ret >= end - pos)
1524 return pos - buf;
1525 pos += ret;
1526 }
1527#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001528 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1529 wpa_supplicant_state_txt(wpa_s->wpa_state));
1530 if (ret < 0 || ret >= end - pos)
1531 return pos - buf;
1532 pos += ret;
1533
1534 if (wpa_s->l2 &&
1535 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1536 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
1537 if (ret < 0 || ret >= end - pos)
1538 return pos - buf;
1539 pos += ret;
1540 }
1541
1542#ifdef CONFIG_P2P
1543 if (wpa_s->global->p2p) {
1544 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1545 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
1546 if (ret < 0 || ret >= end - pos)
1547 return pos - buf;
1548 pos += ret;
1549 }
1550#endif /* CONFIG_P2P */
1551
1552 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1553 MAC2STR(wpa_s->own_addr));
1554 if (ret < 0 || ret >= end - pos)
1555 return pos - buf;
1556 pos += ret;
1557
Dmitry Shmidt04949592012-07-19 12:16:46 -07001558#ifdef CONFIG_HS20
1559 if (wpa_s->current_bss &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001560 wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
1561 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1562 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001563 ret = os_snprintf(pos, end - pos, "hs20=1\n");
1564 if (ret < 0 || ret >= end - pos)
1565 return pos - buf;
1566 pos += ret;
1567 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001568
1569 if (wpa_s->current_ssid) {
1570 struct wpa_cred *cred;
1571 char *type;
1572
1573 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001574 size_t i;
1575
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001576 if (wpa_s->current_ssid->parent_cred != cred)
1577 continue;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001578
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001579 for (i = 0; cred->domain && i < cred->num_domain; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001580 ret = os_snprintf(pos, end - pos,
1581 "home_sp=%s\n",
1582 cred->domain[i]);
1583 if (ret < 0 || ret >= end - pos)
1584 return pos - buf;
1585 pos += ret;
1586 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001587
1588 if (wpa_s->current_bss == NULL ||
1589 wpa_s->current_bss->anqp == NULL)
1590 res = -1;
1591 else
1592 res = interworking_home_sp_cred(
1593 wpa_s, cred,
1594 wpa_s->current_bss->anqp->domain_name);
1595 if (res > 0)
1596 type = "home";
1597 else if (res == 0)
1598 type = "roaming";
1599 else
1600 type = "unknown";
1601
1602 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
1603 if (ret < 0 || ret >= end - pos)
1604 return pos - buf;
1605 pos += ret;
1606
1607 break;
1608 }
1609 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001610#endif /* CONFIG_HS20 */
1611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1613 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1614 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1615 verbose);
1616 if (res >= 0)
1617 pos += res;
1618 }
1619
1620 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1621 if (res >= 0)
1622 pos += res;
1623
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001624#ifdef ANDROID
1625 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
Irfan Sherifff20a4432012-04-16 16:48:34 -07001626 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001627 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
Irfan Sherifff20a4432012-04-16 16:48:34 -07001628 wpa_s->wpa_state,
Irfan Sheriffe78e7672012-08-01 11:10:15 -07001629 MAC2STR(wpa_s->bssid),
andy2_kuo5b5fb022012-05-22 11:53:07 -07001630 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1631 wpa_ssid_txt(wpa_s->current_ssid->ssid,
Irfan Sheriff10294772012-05-11 11:23:35 -07001632 wpa_s->current_ssid->ssid_len) : "");
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001633 if (wpa_s->wpa_state == WPA_COMPLETED) {
1634 struct wpa_ssid *ssid = wpa_s->current_ssid;
1635 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- connection to "
1636 MACSTR " completed %s [id=%d id_str=%s]",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001637 MAC2STR(wpa_s->bssid), "(auth)",
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001638 ssid ? ssid->id : -1,
1639 ssid && ssid->id_str ? ssid->id_str : "");
1640 }
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001641#endif /* ANDROID */
1642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643 return pos - buf;
1644}
1645
1646
1647static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1648 char *cmd)
1649{
1650 char *pos;
1651 int id;
1652 struct wpa_ssid *ssid;
1653 u8 bssid[ETH_ALEN];
1654
1655 /* cmd: "<network id> <BSSID>" */
1656 pos = os_strchr(cmd, ' ');
1657 if (pos == NULL)
1658 return -1;
1659 *pos++ = '\0';
1660 id = atoi(cmd);
1661 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1662 if (hwaddr_aton(pos, bssid)) {
1663 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1664 return -1;
1665 }
1666
1667 ssid = wpa_config_get_network(wpa_s->conf, id);
1668 if (ssid == NULL) {
1669 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1670 "to update", id);
1671 return -1;
1672 }
1673
1674 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1675 ssid->bssid_set = !is_zero_ether_addr(bssid);
1676
1677 return 0;
1678}
1679
1680
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001681static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001682 char *cmd, char *buf,
1683 size_t buflen)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001684{
1685 u8 bssid[ETH_ALEN];
1686 struct wpa_blacklist *e;
1687 char *pos, *end;
1688 int ret;
1689
1690 /* cmd: "BLACKLIST [<BSSID>]" */
1691 if (*cmd == '\0') {
1692 pos = buf;
1693 end = buf + buflen;
1694 e = wpa_s->blacklist;
1695 while (e) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001696 ret = os_snprintf(pos, end - pos, MACSTR "\n",
1697 MAC2STR(e->bssid));
1698 if (ret < 0 || ret >= end - pos)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001699 return pos - buf;
1700 pos += ret;
1701 e = e->next;
1702 }
1703 return pos - buf;
1704 }
1705
1706 cmd++;
1707 if (os_strncmp(cmd, "clear", 5) == 0) {
1708 wpa_blacklist_clear(wpa_s);
1709 os_memcpy(buf, "OK\n", 3);
1710 return 3;
1711 }
1712
1713 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1714 if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001715 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001716 return -1;
1717 }
1718
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001719 /*
1720 * Add the BSSID twice, so its count will be 2, causing it to be
1721 * skipped when processing scan results.
1722 */
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001723 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001724 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001725 return -1;
1726 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001727 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001728 return -1;
1729 os_memcpy(buf, "OK\n", 3);
1730 return 3;
1731}
1732
1733
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001734extern int wpa_debug_level;
1735extern int wpa_debug_timestamp;
1736
1737static const char * debug_level_str(int level)
1738{
1739 switch (level) {
1740 case MSG_EXCESSIVE:
1741 return "EXCESSIVE";
1742 case MSG_MSGDUMP:
1743 return "MSGDUMP";
1744 case MSG_DEBUG:
1745 return "DEBUG";
1746 case MSG_INFO:
1747 return "INFO";
1748 case MSG_WARNING:
1749 return "WARNING";
1750 case MSG_ERROR:
1751 return "ERROR";
1752 default:
1753 return "?";
1754 }
1755}
1756
1757
1758static int str_to_debug_level(const char *s)
1759{
1760 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1761 return MSG_EXCESSIVE;
1762 if (os_strcasecmp(s, "MSGDUMP") == 0)
1763 return MSG_MSGDUMP;
1764 if (os_strcasecmp(s, "DEBUG") == 0)
1765 return MSG_DEBUG;
1766 if (os_strcasecmp(s, "INFO") == 0)
1767 return MSG_INFO;
1768 if (os_strcasecmp(s, "WARNING") == 0)
1769 return MSG_WARNING;
1770 if (os_strcasecmp(s, "ERROR") == 0)
1771 return MSG_ERROR;
1772 return -1;
1773}
1774
1775
1776static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1777 char *cmd, char *buf,
1778 size_t buflen)
1779{
1780 char *pos, *end, *stamp;
1781 int ret;
1782
1783 if (cmd == NULL) {
1784 return -1;
1785 }
1786
1787 /* cmd: "LOG_LEVEL [<level>]" */
1788 if (*cmd == '\0') {
1789 pos = buf;
1790 end = buf + buflen;
1791 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1792 "Timestamp: %d\n",
1793 debug_level_str(wpa_debug_level),
1794 wpa_debug_timestamp);
1795 if (ret < 0 || ret >= end - pos)
1796 ret = 0;
1797
1798 return ret;
1799 }
1800
1801 while (*cmd == ' ')
1802 cmd++;
1803
1804 stamp = os_strchr(cmd, ' ');
1805 if (stamp) {
1806 *stamp++ = '\0';
1807 while (*stamp == ' ') {
1808 stamp++;
1809 }
1810 }
1811
1812 if (cmd && os_strlen(cmd)) {
1813 int level = str_to_debug_level(cmd);
1814 if (level < 0)
1815 return -1;
1816 wpa_debug_level = level;
1817 }
1818
1819 if (stamp && os_strlen(stamp))
1820 wpa_debug_timestamp = atoi(stamp);
1821
1822 os_memcpy(buf, "OK\n", 3);
1823 return 3;
1824}
1825
1826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827static int wpa_supplicant_ctrl_iface_list_networks(
1828 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1829{
1830 char *pos, *end;
1831 struct wpa_ssid *ssid;
1832 int ret;
1833
1834 pos = buf;
1835 end = buf + buflen;
1836 ret = os_snprintf(pos, end - pos,
1837 "network id / ssid / bssid / flags\n");
1838 if (ret < 0 || ret >= end - pos)
1839 return pos - buf;
1840 pos += ret;
1841
1842 ssid = wpa_s->conf->ssid;
1843 while (ssid) {
1844 ret = os_snprintf(pos, end - pos, "%d\t%s",
1845 ssid->id,
1846 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1847 if (ret < 0 || ret >= end - pos)
1848 return pos - buf;
1849 pos += ret;
1850 if (ssid->bssid_set) {
1851 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
1852 MAC2STR(ssid->bssid));
1853 } else {
1854 ret = os_snprintf(pos, end - pos, "\tany");
1855 }
1856 if (ret < 0 || ret >= end - pos)
1857 return pos - buf;
1858 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001859 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001860 ssid == wpa_s->current_ssid ?
1861 "[CURRENT]" : "",
1862 ssid->disabled ? "[DISABLED]" : "",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001863 ssid->disabled_until.sec ?
1864 "[TEMP-DISABLED]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
1866 "");
1867 if (ret < 0 || ret >= end - pos)
1868 return pos - buf;
1869 pos += ret;
1870 ret = os_snprintf(pos, end - pos, "\n");
1871 if (ret < 0 || ret >= end - pos)
1872 return pos - buf;
1873 pos += ret;
1874
1875 ssid = ssid->next;
1876 }
1877
1878 return pos - buf;
1879}
1880
1881
1882static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
1883{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001884 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 ret = os_snprintf(pos, end - pos, "-");
1886 if (ret < 0 || ret >= end - pos)
1887 return pos;
1888 pos += ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001889 ret = wpa_write_ciphers(pos, end, cipher, "+");
1890 if (ret < 0)
1891 return pos;
1892 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893 return pos;
1894}
1895
1896
1897static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
1898 const u8 *ie, size_t ie_len)
1899{
1900 struct wpa_ie_data data;
1901 int first, ret;
1902
1903 ret = os_snprintf(pos, end - pos, "[%s-", proto);
1904 if (ret < 0 || ret >= end - pos)
1905 return pos;
1906 pos += ret;
1907
1908 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
1909 ret = os_snprintf(pos, end - pos, "?]");
1910 if (ret < 0 || ret >= end - pos)
1911 return pos;
1912 pos += ret;
1913 return pos;
1914 }
1915
1916 first = 1;
1917 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1918 ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
1919 if (ret < 0 || ret >= end - pos)
1920 return pos;
1921 pos += ret;
1922 first = 0;
1923 }
1924 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
1925 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
1926 if (ret < 0 || ret >= end - pos)
1927 return pos;
1928 pos += ret;
1929 first = 0;
1930 }
1931 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
1932 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
1933 if (ret < 0 || ret >= end - pos)
1934 return pos;
1935 pos += ret;
1936 first = 0;
1937 }
1938#ifdef CONFIG_IEEE80211R
1939 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1940 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
1941 first ? "" : "+");
1942 if (ret < 0 || ret >= end - pos)
1943 return pos;
1944 pos += ret;
1945 first = 0;
1946 }
1947 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1948 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
1949 first ? "" : "+");
1950 if (ret < 0 || ret >= end - pos)
1951 return pos;
1952 pos += ret;
1953 first = 0;
1954 }
1955#endif /* CONFIG_IEEE80211R */
1956#ifdef CONFIG_IEEE80211W
1957 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1958 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
1959 first ? "" : "+");
1960 if (ret < 0 || ret >= end - pos)
1961 return pos;
1962 pos += ret;
1963 first = 0;
1964 }
1965 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1966 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
1967 first ? "" : "+");
1968 if (ret < 0 || ret >= end - pos)
1969 return pos;
1970 pos += ret;
1971 first = 0;
1972 }
1973#endif /* CONFIG_IEEE80211W */
1974
1975 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
1976
1977 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
1978 ret = os_snprintf(pos, end - pos, "-preauth");
1979 if (ret < 0 || ret >= end - pos)
1980 return pos;
1981 pos += ret;
1982 }
1983
1984 ret = os_snprintf(pos, end - pos, "]");
1985 if (ret < 0 || ret >= end - pos)
1986 return pos;
1987 pos += ret;
1988
1989 return pos;
1990}
1991
1992
1993#ifdef CONFIG_WPS
1994static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
1995 char *pos, char *end,
1996 struct wpabuf *wps_ie)
1997{
1998 int ret;
1999 const char *txt;
2000
2001 if (wps_ie == NULL)
2002 return pos;
2003 if (wps_is_selected_pbc_registrar(wps_ie))
2004 txt = "[WPS-PBC]";
2005#ifdef CONFIG_WPS2
2006 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2007 txt = "[WPS-AUTH]";
2008#endif /* CONFIG_WPS2 */
2009 else if (wps_is_selected_pin_registrar(wps_ie))
2010 txt = "[WPS-PIN]";
2011 else
2012 txt = "[WPS]";
2013
2014 ret = os_snprintf(pos, end - pos, "%s", txt);
2015 if (ret >= 0 && ret < end - pos)
2016 pos += ret;
2017 wpabuf_free(wps_ie);
2018 return pos;
2019}
2020#endif /* CONFIG_WPS */
2021
2022
2023static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2024 char *pos, char *end,
2025 const struct wpa_bss *bss)
2026{
2027#ifdef CONFIG_WPS
2028 struct wpabuf *wps_ie;
2029 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2030 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2031#else /* CONFIG_WPS */
2032 return pos;
2033#endif /* CONFIG_WPS */
2034}
2035
2036
2037/* Format one result on one text line into a buffer. */
2038static int wpa_supplicant_ctrl_iface_scan_result(
2039 struct wpa_supplicant *wpa_s,
2040 const struct wpa_bss *bss, char *buf, size_t buflen)
2041{
2042 char *pos, *end;
2043 int ret;
2044 const u8 *ie, *ie2, *p2p;
2045
2046 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07002047 if (!p2p)
2048 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2050 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2051 0)
2052 return 0; /* Do not show P2P listen discovery results here */
2053
2054 pos = buf;
2055 end = buf + buflen;
2056
2057 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2058 MAC2STR(bss->bssid), bss->freq, bss->level);
2059 if (ret < 0 || ret >= end - pos)
2060 return -1;
2061 pos += ret;
2062 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2063 if (ie)
2064 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2065 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2066 if (ie2)
2067 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
2068 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2069 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2070 ret = os_snprintf(pos, end - pos, "[WEP]");
2071 if (ret < 0 || ret >= end - pos)
2072 return -1;
2073 pos += ret;
2074 }
2075 if (bss->caps & IEEE80211_CAP_IBSS) {
2076 ret = os_snprintf(pos, end - pos, "[IBSS]");
2077 if (ret < 0 || ret >= end - pos)
2078 return -1;
2079 pos += ret;
2080 }
2081 if (bss->caps & IEEE80211_CAP_ESS) {
2082 ret = os_snprintf(pos, end - pos, "[ESS]");
2083 if (ret < 0 || ret >= end - pos)
2084 return -1;
2085 pos += ret;
2086 }
2087 if (p2p) {
2088 ret = os_snprintf(pos, end - pos, "[P2P]");
2089 if (ret < 0 || ret >= end - pos)
2090 return -1;
2091 pos += ret;
2092 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002093#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002094 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002095 ret = os_snprintf(pos, end - pos, "[HS20]");
2096 if (ret < 0 || ret >= end - pos)
2097 return -1;
2098 pos += ret;
2099 }
2100#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101
2102 ret = os_snprintf(pos, end - pos, "\t%s",
2103 wpa_ssid_txt(bss->ssid, bss->ssid_len));
2104 if (ret < 0 || ret >= end - pos)
2105 return -1;
2106 pos += ret;
2107
2108 ret = os_snprintf(pos, end - pos, "\n");
2109 if (ret < 0 || ret >= end - pos)
2110 return -1;
2111 pos += ret;
2112
2113 return pos - buf;
2114}
2115
2116
2117static int wpa_supplicant_ctrl_iface_scan_results(
2118 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2119{
2120 char *pos, *end;
2121 struct wpa_bss *bss;
2122 int ret;
2123
2124 pos = buf;
2125 end = buf + buflen;
2126 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2127 "flags / ssid\n");
2128 if (ret < 0 || ret >= end - pos)
2129 return pos - buf;
2130 pos += ret;
2131
2132 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2133 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2134 end - pos);
2135 if (ret < 0 || ret >= end - pos)
2136 return pos - buf;
2137 pos += ret;
2138 }
2139
2140 return pos - buf;
2141}
2142
2143
2144static int wpa_supplicant_ctrl_iface_select_network(
2145 struct wpa_supplicant *wpa_s, char *cmd)
2146{
2147 int id;
2148 struct wpa_ssid *ssid;
2149
2150 /* cmd: "<network id>" or "any" */
2151 if (os_strcmp(cmd, "any") == 0) {
2152 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2153 ssid = NULL;
2154 } else {
2155 id = atoi(cmd);
2156 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2157
2158 ssid = wpa_config_get_network(wpa_s->conf, id);
2159 if (ssid == NULL) {
2160 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2161 "network id=%d", id);
2162 return -1;
2163 }
2164 if (ssid->disabled == 2) {
2165 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2166 "SELECT_NETWORK with persistent P2P group");
2167 return -1;
2168 }
2169 }
2170
2171 wpa_supplicant_select_network(wpa_s, ssid);
2172
2173 return 0;
2174}
2175
2176
2177static int wpa_supplicant_ctrl_iface_enable_network(
2178 struct wpa_supplicant *wpa_s, char *cmd)
2179{
2180 int id;
2181 struct wpa_ssid *ssid;
2182
2183 /* cmd: "<network id>" or "all" */
2184 if (os_strcmp(cmd, "all") == 0) {
2185 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2186 ssid = NULL;
2187 } else {
2188 id = atoi(cmd);
2189 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2190
2191 ssid = wpa_config_get_network(wpa_s->conf, id);
2192 if (ssid == NULL) {
2193 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2194 "network id=%d", id);
2195 return -1;
2196 }
2197 if (ssid->disabled == 2) {
2198 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2199 "ENABLE_NETWORK with persistent P2P group");
2200 return -1;
2201 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002202
2203 if (os_strstr(cmd, " no-connect")) {
2204 ssid->disabled = 0;
2205 return 0;
2206 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002207 }
2208 wpa_supplicant_enable_network(wpa_s, ssid);
2209
2210 return 0;
2211}
2212
2213
2214static int wpa_supplicant_ctrl_iface_disable_network(
2215 struct wpa_supplicant *wpa_s, char *cmd)
2216{
2217 int id;
2218 struct wpa_ssid *ssid;
2219
2220 /* cmd: "<network id>" or "all" */
2221 if (os_strcmp(cmd, "all") == 0) {
2222 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2223 ssid = NULL;
2224 } else {
2225 id = atoi(cmd);
2226 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2227
2228 ssid = wpa_config_get_network(wpa_s->conf, id);
2229 if (ssid == NULL) {
2230 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2231 "network id=%d", id);
2232 return -1;
2233 }
2234 if (ssid->disabled == 2) {
2235 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2236 "DISABLE_NETWORK with persistent P2P "
2237 "group");
2238 return -1;
2239 }
2240 }
2241 wpa_supplicant_disable_network(wpa_s, ssid);
2242
2243 return 0;
2244}
2245
2246
2247static int wpa_supplicant_ctrl_iface_add_network(
2248 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2249{
2250 struct wpa_ssid *ssid;
2251 int ret;
2252
2253 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2254
2255 ssid = wpa_config_add_network(wpa_s->conf);
2256 if (ssid == NULL)
2257 return -1;
2258
2259 wpas_notify_network_added(wpa_s, ssid);
2260
2261 ssid->disabled = 1;
2262 wpa_config_set_network_defaults(ssid);
2263
2264 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
2265 if (ret < 0 || (size_t) ret >= buflen)
2266 return -1;
2267 return ret;
2268}
2269
2270
2271static int wpa_supplicant_ctrl_iface_remove_network(
2272 struct wpa_supplicant *wpa_s, char *cmd)
2273{
2274 int id;
2275 struct wpa_ssid *ssid;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002276 int was_disabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002277
2278 /* cmd: "<network id>" or "all" */
2279 if (os_strcmp(cmd, "all") == 0) {
2280 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002281 if (wpa_s->sched_scanning)
2282 wpa_supplicant_cancel_sched_scan(wpa_s);
2283
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002284 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285 if (wpa_s->current_ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002286#ifdef CONFIG_SME
2287 wpa_s->sme.prev_bssid_set = 0;
2288#endif /* CONFIG_SME */
Jouni Malinen75ecf522011-06-27 15:19:46 -07002289 wpa_sm_set_config(wpa_s->wpa, NULL);
2290 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002291 wpa_supplicant_deauthenticate(
2292 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002294 ssid = wpa_s->conf->ssid;
2295 while (ssid) {
2296 struct wpa_ssid *remove_ssid = ssid;
2297 id = ssid->id;
2298 ssid = ssid->next;
2299 wpas_notify_network_removed(wpa_s, remove_ssid);
2300 wpa_config_remove_network(wpa_s->conf, id);
2301 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002302 return 0;
2303 }
2304
2305 id = atoi(cmd);
2306 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2307
2308 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002309 if (ssid)
2310 wpas_notify_network_removed(wpa_s, ssid);
Deepthi Gowria831d782012-09-03 11:55:38 +03002311 if (ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002312 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2313 "id=%d", id);
2314 return -1;
2315 }
2316
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002317 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002318#ifdef CONFIG_SME
2319 wpa_s->sme.prev_bssid_set = 0;
2320#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002322 * Invalidate the EAP session cache if the current or
2323 * previously used network is removed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002324 */
2325 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002326 }
2327
2328 if (ssid == wpa_s->current_ssid) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002329 wpa_sm_set_config(wpa_s->wpa, NULL);
2330 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002332 wpa_supplicant_deauthenticate(wpa_s,
2333 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334 }
2335
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002336 was_disabled = ssid->disabled;
2337
Deepthi Gowria831d782012-09-03 11:55:38 +03002338 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2339 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2340 "network id=%d", id);
2341 return -1;
2342 }
2343
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002344 if (!was_disabled && wpa_s->sched_scanning) {
2345 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2346 "network from filters");
2347 wpa_supplicant_cancel_sched_scan(wpa_s);
2348 wpa_supplicant_req_scan(wpa_s, 0, 0);
2349 }
2350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002351 return 0;
2352}
2353
2354
2355static int wpa_supplicant_ctrl_iface_set_network(
2356 struct wpa_supplicant *wpa_s, char *cmd)
2357{
2358 int id;
2359 struct wpa_ssid *ssid;
2360 char *name, *value;
2361
2362 /* cmd: "<network id> <variable name> <value>" */
2363 name = os_strchr(cmd, ' ');
2364 if (name == NULL)
2365 return -1;
2366 *name++ = '\0';
2367
2368 value = os_strchr(name, ' ');
2369 if (value == NULL)
2370 return -1;
2371 *value++ = '\0';
2372
2373 id = atoi(cmd);
2374 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2375 id, name);
2376 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2377 (u8 *) value, os_strlen(value));
2378
2379 ssid = wpa_config_get_network(wpa_s->conf, id);
2380 if (ssid == NULL) {
2381 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2382 "id=%d", id);
2383 return -1;
2384 }
2385
2386 if (wpa_config_set(ssid, name, value, 0) < 0) {
2387 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2388 "variable '%s'", name);
2389 return -1;
2390 }
2391
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002392 if (os_strcmp(name, "bssid") != 0 &&
2393 os_strcmp(name, "priority") != 0)
2394 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002395
2396 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002397 /*
2398 * Invalidate the EAP session cache if anything in the current
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002399 * or previously used configuration changes.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002400 */
2401 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2402 }
2403
2404 if ((os_strcmp(name, "psk") == 0 &&
2405 value[0] == '"' && ssid->ssid_len) ||
2406 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2407 wpa_config_update_psk(ssid);
2408 else if (os_strcmp(name, "priority") == 0)
2409 wpa_config_update_prio_list(wpa_s->conf);
2410
2411 return 0;
2412}
2413
2414
2415static int wpa_supplicant_ctrl_iface_get_network(
2416 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2417{
2418 int id;
2419 size_t res;
2420 struct wpa_ssid *ssid;
2421 char *name, *value;
2422
2423 /* cmd: "<network id> <variable name>" */
2424 name = os_strchr(cmd, ' ');
2425 if (name == NULL || buflen == 0)
2426 return -1;
2427 *name++ = '\0';
2428
2429 id = atoi(cmd);
2430 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2431 id, name);
2432
2433 ssid = wpa_config_get_network(wpa_s->conf, id);
2434 if (ssid == NULL) {
2435 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2436 "id=%d", id);
2437 return -1;
2438 }
2439
2440 value = wpa_config_get_no_key(ssid, name);
2441 if (value == NULL) {
2442 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2443 "variable '%s'", name);
2444 return -1;
2445 }
2446
2447 res = os_strlcpy(buf, value, buflen);
2448 if (res >= buflen) {
2449 os_free(value);
2450 return -1;
2451 }
2452
2453 os_free(value);
2454
2455 return res;
2456}
2457
2458
Dmitry Shmidt04949592012-07-19 12:16:46 -07002459static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2460 char *buf, size_t buflen)
2461{
2462 char *pos, *end;
2463 struct wpa_cred *cred;
2464 int ret;
2465
2466 pos = buf;
2467 end = buf + buflen;
2468 ret = os_snprintf(pos, end - pos,
2469 "cred id / realm / username / domain / imsi\n");
2470 if (ret < 0 || ret >= end - pos)
2471 return pos - buf;
2472 pos += ret;
2473
2474 cred = wpa_s->conf->cred;
2475 while (cred) {
2476 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2477 cred->id, cred->realm ? cred->realm : "",
2478 cred->username ? cred->username : "",
Dmitry Shmidt051af732013-10-22 13:52:46 -07002479 cred->domain ? cred->domain[0] : "",
Dmitry Shmidt04949592012-07-19 12:16:46 -07002480 cred->imsi ? cred->imsi : "");
2481 if (ret < 0 || ret >= end - pos)
2482 return pos - buf;
2483 pos += ret;
2484
2485 cred = cred->next;
2486 }
2487
2488 return pos - buf;
2489}
2490
2491
2492static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2493 char *buf, size_t buflen)
2494{
2495 struct wpa_cred *cred;
2496 int ret;
2497
2498 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2499
2500 cred = wpa_config_add_cred(wpa_s->conf);
2501 if (cred == NULL)
2502 return -1;
2503
2504 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2505 if (ret < 0 || (size_t) ret >= buflen)
2506 return -1;
2507 return ret;
2508}
2509
2510
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002511static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2512 struct wpa_cred *cred)
2513{
2514 struct wpa_ssid *ssid;
2515 char str[20];
2516
2517 if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
2518 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2519 return -1;
2520 }
2521
2522 /* Remove any network entry created based on the removed credential */
2523 ssid = wpa_s->conf->ssid;
2524 while (ssid) {
2525 if (ssid->parent_cred == cred) {
2526 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
2527 "used the removed credential", ssid->id);
2528 os_snprintf(str, sizeof(str), "%d", ssid->id);
2529 ssid = ssid->next;
2530 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
2531 } else
2532 ssid = ssid->next;
2533 }
2534
2535 return 0;
2536}
2537
2538
Dmitry Shmidt04949592012-07-19 12:16:46 -07002539static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2540 char *cmd)
2541{
2542 int id;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002543 struct wpa_cred *cred, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002544
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002545 /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002546 if (os_strcmp(cmd, "all") == 0) {
2547 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2548 cred = wpa_s->conf->cred;
2549 while (cred) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002550 prev = cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002551 cred = cred->next;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002552 wpas_ctrl_remove_cred(wpa_s, prev);
2553 }
2554 return 0;
2555 }
2556
2557 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
2558 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
2559 cmd + 8);
2560 cred = wpa_s->conf->cred;
2561 while (cred) {
2562 prev = cred;
2563 cred = cred->next;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002564 if (prev->domain) {
2565 size_t i;
2566 for (i = 0; i < prev->num_domain; i++) {
2567 if (os_strcmp(prev->domain[i], cmd + 8)
2568 != 0)
2569 continue;
2570 wpas_ctrl_remove_cred(wpa_s, prev);
2571 break;
2572 }
2573 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002574 }
2575 return 0;
2576 }
2577
2578 id = atoi(cmd);
2579 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2580
2581 cred = wpa_config_get_cred(wpa_s->conf, id);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002582 return wpas_ctrl_remove_cred(wpa_s, cred);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002583}
2584
2585
2586static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2587 char *cmd)
2588{
2589 int id;
2590 struct wpa_cred *cred;
2591 char *name, *value;
2592
2593 /* cmd: "<cred id> <variable name> <value>" */
2594 name = os_strchr(cmd, ' ');
2595 if (name == NULL)
2596 return -1;
2597 *name++ = '\0';
2598
2599 value = os_strchr(name, ' ');
2600 if (value == NULL)
2601 return -1;
2602 *value++ = '\0';
2603
2604 id = atoi(cmd);
2605 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2606 id, name);
2607 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2608 (u8 *) value, os_strlen(value));
2609
2610 cred = wpa_config_get_cred(wpa_s->conf, id);
2611 if (cred == NULL) {
2612 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2613 id);
2614 return -1;
2615 }
2616
2617 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2618 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2619 "variable '%s'", name);
2620 return -1;
2621 }
2622
2623 return 0;
2624}
2625
2626
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627#ifndef CONFIG_NO_CONFIG_WRITE
2628static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2629{
2630 int ret;
2631
2632 if (!wpa_s->conf->update_config) {
2633 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2634 "to update configuration (update_config=0)");
2635 return -1;
2636 }
2637
2638 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2639 if (ret) {
2640 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2641 "update configuration");
2642 } else {
2643 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2644 " updated");
2645 }
2646
2647 return ret;
2648}
2649#endif /* CONFIG_NO_CONFIG_WRITE */
2650
2651
2652static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2653 struct wpa_driver_capa *capa,
2654 char *buf, size_t buflen)
2655{
2656 int ret, first = 1;
2657 char *pos, *end;
2658 size_t len;
2659
2660 pos = buf;
2661 end = pos + buflen;
2662
2663 if (res < 0) {
2664 if (strict)
2665 return 0;
2666 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2667 if (len >= buflen)
2668 return -1;
2669 return len;
2670 }
2671
2672 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2673 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2674 if (ret < 0 || ret >= end - pos)
2675 return pos - buf;
2676 pos += ret;
2677 first = 0;
2678 }
2679
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002680 if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2681 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
2682 if (ret < 0 || ret >= end - pos)
2683 return pos - buf;
2684 pos += ret;
2685 first = 0;
2686 }
2687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002688 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2689 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2690 if (ret < 0 || ret >= end - pos)
2691 return pos - buf;
2692 pos += ret;
2693 first = 0;
2694 }
2695
2696 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2697 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
2698 if (ret < 0 || ret >= end - pos)
2699 return pos - buf;
2700 pos += ret;
2701 first = 0;
2702 }
2703
2704 return pos - buf;
2705}
2706
2707
2708static int ctrl_iface_get_capability_group(int res, char *strict,
2709 struct wpa_driver_capa *capa,
2710 char *buf, size_t buflen)
2711{
2712 int ret, first = 1;
2713 char *pos, *end;
2714 size_t len;
2715
2716 pos = buf;
2717 end = pos + buflen;
2718
2719 if (res < 0) {
2720 if (strict)
2721 return 0;
2722 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
2723 if (len >= buflen)
2724 return -1;
2725 return len;
2726 }
2727
2728 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2729 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2730 if (ret < 0 || ret >= end - pos)
2731 return pos - buf;
2732 pos += ret;
2733 first = 0;
2734 }
2735
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002736 if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2737 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
2738 if (ret < 0 || ret >= end - pos)
2739 return pos - buf;
2740 pos += ret;
2741 first = 0;
2742 }
2743
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002744 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2745 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2746 if (ret < 0 || ret >= end - pos)
2747 return pos - buf;
2748 pos += ret;
2749 first = 0;
2750 }
2751
2752 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
2753 ret = os_snprintf(pos, end - pos, "%sWEP104",
2754 first ? "" : " ");
2755 if (ret < 0 || ret >= end - pos)
2756 return pos - buf;
2757 pos += ret;
2758 first = 0;
2759 }
2760
2761 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
2762 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
2763 if (ret < 0 || ret >= end - pos)
2764 return pos - buf;
2765 pos += ret;
2766 first = 0;
2767 }
2768
2769 return pos - buf;
2770}
2771
2772
2773static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
2774 struct wpa_driver_capa *capa,
2775 char *buf, size_t buflen)
2776{
2777 int ret;
2778 char *pos, *end;
2779 size_t len;
2780
2781 pos = buf;
2782 end = pos + buflen;
2783
2784 if (res < 0) {
2785 if (strict)
2786 return 0;
2787 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
2788 "NONE", buflen);
2789 if (len >= buflen)
2790 return -1;
2791 return len;
2792 }
2793
2794 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
2795 if (ret < 0 || ret >= end - pos)
2796 return pos - buf;
2797 pos += ret;
2798
2799 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2800 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
2801 ret = os_snprintf(pos, end - pos, " WPA-EAP");
2802 if (ret < 0 || ret >= end - pos)
2803 return pos - buf;
2804 pos += ret;
2805 }
2806
2807 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2808 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2809 ret = os_snprintf(pos, end - pos, " WPA-PSK");
2810 if (ret < 0 || ret >= end - pos)
2811 return pos - buf;
2812 pos += ret;
2813 }
2814
2815 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2816 ret = os_snprintf(pos, end - pos, " WPA-NONE");
2817 if (ret < 0 || ret >= end - pos)
2818 return pos - buf;
2819 pos += ret;
2820 }
2821
2822 return pos - buf;
2823}
2824
2825
2826static int ctrl_iface_get_capability_proto(int res, char *strict,
2827 struct wpa_driver_capa *capa,
2828 char *buf, size_t buflen)
2829{
2830 int ret, first = 1;
2831 char *pos, *end;
2832 size_t len;
2833
2834 pos = buf;
2835 end = pos + buflen;
2836
2837 if (res < 0) {
2838 if (strict)
2839 return 0;
2840 len = os_strlcpy(buf, "RSN WPA", buflen);
2841 if (len >= buflen)
2842 return -1;
2843 return len;
2844 }
2845
2846 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2847 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2848 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
2849 if (ret < 0 || ret >= end - pos)
2850 return pos - buf;
2851 pos += ret;
2852 first = 0;
2853 }
2854
2855 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2856 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
2857 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
2858 if (ret < 0 || ret >= end - pos)
2859 return pos - buf;
2860 pos += ret;
2861 first = 0;
2862 }
2863
2864 return pos - buf;
2865}
2866
2867
2868static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
2869 struct wpa_driver_capa *capa,
2870 char *buf, size_t buflen)
2871{
2872 int ret, first = 1;
2873 char *pos, *end;
2874 size_t len;
2875
2876 pos = buf;
2877 end = pos + buflen;
2878
2879 if (res < 0) {
2880 if (strict)
2881 return 0;
2882 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
2883 if (len >= buflen)
2884 return -1;
2885 return len;
2886 }
2887
2888 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
2889 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
2890 if (ret < 0 || ret >= end - pos)
2891 return pos - buf;
2892 pos += ret;
2893 first = 0;
2894 }
2895
2896 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
2897 ret = os_snprintf(pos, end - pos, "%sSHARED",
2898 first ? "" : " ");
2899 if (ret < 0 || ret >= end - pos)
2900 return pos - buf;
2901 pos += ret;
2902 first = 0;
2903 }
2904
2905 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
2906 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
2907 if (ret < 0 || ret >= end - pos)
2908 return pos - buf;
2909 pos += ret;
2910 first = 0;
2911 }
2912
2913 return pos - buf;
2914}
2915
2916
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002917static int ctrl_iface_get_capability_modes(int res, char *strict,
2918 struct wpa_driver_capa *capa,
2919 char *buf, size_t buflen)
2920{
2921 int ret, first = 1;
2922 char *pos, *end;
2923 size_t len;
2924
2925 pos = buf;
2926 end = pos + buflen;
2927
2928 if (res < 0) {
2929 if (strict)
2930 return 0;
2931 len = os_strlcpy(buf, "IBSS AP", buflen);
2932 if (len >= buflen)
2933 return -1;
2934 return len;
2935 }
2936
2937 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
2938 ret = os_snprintf(pos, end - pos, "%sIBSS", first ? "" : " ");
2939 if (ret < 0 || ret >= end - pos)
2940 return pos - buf;
2941 pos += ret;
2942 first = 0;
2943 }
2944
2945 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
2946 ret = os_snprintf(pos, end - pos, "%sAP", first ? "" : " ");
2947 if (ret < 0 || ret >= end - pos)
2948 return pos - buf;
2949 pos += ret;
2950 first = 0;
2951 }
2952
2953 return pos - buf;
2954}
2955
2956
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002957static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
2958 char *buf, size_t buflen)
2959{
2960 struct hostapd_channel_data *chnl;
2961 int ret, i, j;
2962 char *pos, *end, *hmode;
2963
2964 pos = buf;
2965 end = pos + buflen;
2966
2967 for (j = 0; j < wpa_s->hw.num_modes; j++) {
2968 switch (wpa_s->hw.modes[j].mode) {
2969 case HOSTAPD_MODE_IEEE80211B:
2970 hmode = "B";
2971 break;
2972 case HOSTAPD_MODE_IEEE80211G:
2973 hmode = "G";
2974 break;
2975 case HOSTAPD_MODE_IEEE80211A:
2976 hmode = "A";
2977 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002978 case HOSTAPD_MODE_IEEE80211AD:
2979 hmode = "AD";
2980 break;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002981 default:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002982 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002983 }
2984 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
2985 if (ret < 0 || ret >= end - pos)
2986 return pos - buf;
2987 pos += ret;
2988 chnl = wpa_s->hw.modes[j].channels;
2989 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002990 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
2991 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002992 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
2993 if (ret < 0 || ret >= end - pos)
2994 return pos - buf;
2995 pos += ret;
2996 }
2997 ret = os_snprintf(pos, end - pos, "\n");
2998 if (ret < 0 || ret >= end - pos)
2999 return pos - buf;
3000 pos += ret;
3001 }
3002
3003 return pos - buf;
3004}
3005
3006
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003007static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3008 char *buf, size_t buflen)
3009{
3010 struct hostapd_channel_data *chnl;
3011 int ret, i, j;
3012 char *pos, *end, *hmode;
3013
3014 pos = buf;
3015 end = pos + buflen;
3016
3017 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3018 switch (wpa_s->hw.modes[j].mode) {
3019 case HOSTAPD_MODE_IEEE80211B:
3020 hmode = "B";
3021 break;
3022 case HOSTAPD_MODE_IEEE80211G:
3023 hmode = "G";
3024 break;
3025 case HOSTAPD_MODE_IEEE80211A:
3026 hmode = "A";
3027 break;
3028 case HOSTAPD_MODE_IEEE80211AD:
3029 hmode = "AD";
3030 break;
3031 default:
3032 continue;
3033 }
3034 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3035 hmode);
3036 if (ret < 0 || ret >= end - pos)
3037 return pos - buf;
3038 pos += ret;
3039 chnl = wpa_s->hw.modes[j].channels;
3040 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3041 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3042 continue;
3043 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s\n",
3044 chnl[i].chan, chnl[i].freq,
3045 chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
3046 " (NO_IBSS)" : "");
3047 if (ret < 0 || ret >= end - pos)
3048 return pos - buf;
3049 pos += ret;
3050 }
3051 ret = os_snprintf(pos, end - pos, "\n");
3052 if (ret < 0 || ret >= end - pos)
3053 return pos - buf;
3054 pos += ret;
3055 }
3056
3057 return pos - buf;
3058}
3059
3060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061static int wpa_supplicant_ctrl_iface_get_capability(
3062 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3063 size_t buflen)
3064{
3065 struct wpa_driver_capa capa;
3066 int res;
3067 char *strict;
3068 char field[30];
3069 size_t len;
3070
3071 /* Determine whether or not strict checking was requested */
3072 len = os_strlcpy(field, _field, sizeof(field));
3073 if (len >= sizeof(field))
3074 return -1;
3075 strict = os_strchr(field, ' ');
3076 if (strict != NULL) {
3077 *strict++ = '\0';
3078 if (os_strcmp(strict, "strict") != 0)
3079 return -1;
3080 }
3081
3082 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3083 field, strict ? strict : "");
3084
3085 if (os_strcmp(field, "eap") == 0) {
3086 return eap_get_names(buf, buflen);
3087 }
3088
3089 res = wpa_drv_get_capa(wpa_s, &capa);
3090
3091 if (os_strcmp(field, "pairwise") == 0)
3092 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3093 buf, buflen);
3094
3095 if (os_strcmp(field, "group") == 0)
3096 return ctrl_iface_get_capability_group(res, strict, &capa,
3097 buf, buflen);
3098
3099 if (os_strcmp(field, "key_mgmt") == 0)
3100 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3101 buf, buflen);
3102
3103 if (os_strcmp(field, "proto") == 0)
3104 return ctrl_iface_get_capability_proto(res, strict, &capa,
3105 buf, buflen);
3106
3107 if (os_strcmp(field, "auth_alg") == 0)
3108 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
3109 buf, buflen);
3110
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003111 if (os_strcmp(field, "modes") == 0)
3112 return ctrl_iface_get_capability_modes(res, strict, &capa,
3113 buf, buflen);
3114
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07003115 if (os_strcmp(field, "channels") == 0)
3116 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3117
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003118 if (os_strcmp(field, "freq") == 0)
3119 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003121 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3122 field);
3123
3124 return -1;
3125}
3126
3127
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003128#ifdef CONFIG_INTERWORKING
3129static char * anqp_add_hex(char *pos, char *end, const char *title,
3130 struct wpabuf *data)
3131{
3132 char *start = pos;
3133 size_t i;
3134 int ret;
3135 const u8 *d;
3136
3137 if (data == NULL)
3138 return start;
3139
3140 ret = os_snprintf(pos, end - pos, "%s=", title);
3141 if (ret < 0 || ret >= end - pos)
3142 return start;
3143 pos += ret;
3144
3145 d = wpabuf_head_u8(data);
3146 for (i = 0; i < wpabuf_len(data); i++) {
3147 ret = os_snprintf(pos, end - pos, "%02x", *d++);
3148 if (ret < 0 || ret >= end - pos)
3149 return start;
3150 pos += ret;
3151 }
3152
3153 ret = os_snprintf(pos, end - pos, "\n");
3154 if (ret < 0 || ret >= end - pos)
3155 return start;
3156 pos += ret;
3157
3158 return pos;
3159}
3160#endif /* CONFIG_INTERWORKING */
3161
3162
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003163static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3164 unsigned long mask, char *buf, size_t buflen)
3165{
3166 size_t i;
3167 int ret;
3168 char *pos, *end;
3169 const u8 *ie, *ie2;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003170
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003171 pos = buf;
3172 end = buf + buflen;
3173
3174 if (mask & WPA_BSS_MASK_ID) {
3175 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
3176 if (ret < 0 || ret >= end - pos)
3177 return 0;
3178 pos += ret;
3179 }
3180
3181 if (mask & WPA_BSS_MASK_BSSID) {
3182 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3183 MAC2STR(bss->bssid));
3184 if (ret < 0 || ret >= end - pos)
3185 return 0;
3186 pos += ret;
3187 }
3188
3189 if (mask & WPA_BSS_MASK_FREQ) {
3190 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
3191 if (ret < 0 || ret >= end - pos)
3192 return 0;
3193 pos += ret;
3194 }
3195
3196 if (mask & WPA_BSS_MASK_BEACON_INT) {
3197 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3198 bss->beacon_int);
3199 if (ret < 0 || ret >= end - pos)
3200 return 0;
3201 pos += ret;
3202 }
3203
3204 if (mask & WPA_BSS_MASK_CAPABILITIES) {
3205 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3206 bss->caps);
3207 if (ret < 0 || ret >= end - pos)
3208 return 0;
3209 pos += ret;
3210 }
3211
3212 if (mask & WPA_BSS_MASK_QUAL) {
3213 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
3214 if (ret < 0 || ret >= end - pos)
3215 return 0;
3216 pos += ret;
3217 }
3218
3219 if (mask & WPA_BSS_MASK_NOISE) {
3220 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
3221 if (ret < 0 || ret >= end - pos)
3222 return 0;
3223 pos += ret;
3224 }
3225
3226 if (mask & WPA_BSS_MASK_LEVEL) {
3227 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
3228 if (ret < 0 || ret >= end - pos)
3229 return 0;
3230 pos += ret;
3231 }
3232
3233 if (mask & WPA_BSS_MASK_TSF) {
3234 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3235 (unsigned long long) bss->tsf);
3236 if (ret < 0 || ret >= end - pos)
3237 return 0;
3238 pos += ret;
3239 }
3240
3241 if (mask & WPA_BSS_MASK_AGE) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003242 struct os_time now;
3243
3244 os_get_time(&now);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003245 ret = os_snprintf(pos, end - pos, "age=%d\n",
3246 (int) (now.sec - bss->last_update.sec));
3247 if (ret < 0 || ret >= end - pos)
3248 return 0;
3249 pos += ret;
3250 }
3251
3252 if (mask & WPA_BSS_MASK_IE) {
3253 ret = os_snprintf(pos, end - pos, "ie=");
3254 if (ret < 0 || ret >= end - pos)
3255 return 0;
3256 pos += ret;
3257
3258 ie = (const u8 *) (bss + 1);
3259 for (i = 0; i < bss->ie_len; i++) {
3260 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
3261 if (ret < 0 || ret >= end - pos)
3262 return 0;
3263 pos += ret;
3264 }
3265
3266 ret = os_snprintf(pos, end - pos, "\n");
3267 if (ret < 0 || ret >= end - pos)
3268 return 0;
3269 pos += ret;
3270 }
3271
3272 if (mask & WPA_BSS_MASK_FLAGS) {
3273 ret = os_snprintf(pos, end - pos, "flags=");
3274 if (ret < 0 || ret >= end - pos)
3275 return 0;
3276 pos += ret;
3277
3278 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3279 if (ie)
3280 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3281 2 + ie[1]);
3282 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3283 if (ie2)
3284 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3285 2 + ie2[1]);
3286 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3287 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3288 ret = os_snprintf(pos, end - pos, "[WEP]");
3289 if (ret < 0 || ret >= end - pos)
3290 return 0;
3291 pos += ret;
3292 }
3293 if (bss->caps & IEEE80211_CAP_IBSS) {
3294 ret = os_snprintf(pos, end - pos, "[IBSS]");
3295 if (ret < 0 || ret >= end - pos)
3296 return 0;
3297 pos += ret;
3298 }
3299 if (bss->caps & IEEE80211_CAP_ESS) {
3300 ret = os_snprintf(pos, end - pos, "[ESS]");
3301 if (ret < 0 || ret >= end - pos)
3302 return 0;
3303 pos += ret;
3304 }
Dmitry Shmidt96571392013-10-14 12:54:46 -07003305 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
3306 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003307 ret = os_snprintf(pos, end - pos, "[P2P]");
3308 if (ret < 0 || ret >= end - pos)
3309 return 0;
3310 pos += ret;
3311 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003312#ifdef CONFIG_HS20
3313 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3314 ret = os_snprintf(pos, end - pos, "[HS20]");
3315 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003316 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003317 pos += ret;
3318 }
3319#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003320
3321 ret = os_snprintf(pos, end - pos, "\n");
3322 if (ret < 0 || ret >= end - pos)
3323 return 0;
3324 pos += ret;
3325 }
3326
3327 if (mask & WPA_BSS_MASK_SSID) {
3328 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3329 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3330 if (ret < 0 || ret >= end - pos)
3331 return 0;
3332 pos += ret;
3333 }
3334
3335#ifdef CONFIG_WPS
3336 if (mask & WPA_BSS_MASK_WPS_SCAN) {
3337 ie = (const u8 *) (bss + 1);
3338 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3339 if (ret < 0 || ret >= end - pos)
3340 return 0;
3341 pos += ret;
3342 }
3343#endif /* CONFIG_WPS */
3344
3345#ifdef CONFIG_P2P
3346 if (mask & WPA_BSS_MASK_P2P_SCAN) {
3347 ie = (const u8 *) (bss + 1);
3348 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3349 if (ret < 0 || ret >= end - pos)
3350 return 0;
3351 pos += ret;
3352 }
3353#endif /* CONFIG_P2P */
3354
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003355#ifdef CONFIG_WIFI_DISPLAY
3356 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3357 struct wpabuf *wfd;
3358 ie = (const u8 *) (bss + 1);
3359 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3360 WFD_IE_VENDOR_TYPE);
3361 if (wfd) {
3362 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
3363 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003364 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003365 pos += ret;
3366
3367 pos += wpa_snprintf_hex(pos, end - pos,
3368 wpabuf_head(wfd),
3369 wpabuf_len(wfd));
3370 wpabuf_free(wfd);
3371
3372 ret = os_snprintf(pos, end - pos, "\n");
3373 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003374 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003375 pos += ret;
3376 }
3377 }
3378#endif /* CONFIG_WIFI_DISPLAY */
3379
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003380#ifdef CONFIG_INTERWORKING
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003381 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3382 struct wpa_bss_anqp *anqp = bss->anqp;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003383 pos = anqp_add_hex(pos, end, "anqp_venue_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003384 anqp->venue_name);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003385 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003386 anqp->network_auth_type);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003387 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003388 anqp->roaming_consortium);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003389 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003390 anqp->ip_addr_type_availability);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003391 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003392 anqp->nai_realm);
3393 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003394 pos = anqp_add_hex(pos, end, "anqp_domain_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003395 anqp->domain_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003396#ifdef CONFIG_HS20
3397 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003398 anqp->hs20_operator_friendly_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003399 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003400 anqp->hs20_wan_metrics);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003401 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003402 anqp->hs20_connection_capability);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003403#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003404 }
3405#endif /* CONFIG_INTERWORKING */
3406
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003407 if (mask & WPA_BSS_MASK_DELIM) {
3408 ret = os_snprintf(pos, end - pos, "====\n");
3409 if (ret < 0 || ret >= end - pos)
3410 return 0;
3411 pos += ret;
3412 }
Irfan Sheriffe2ea0082012-08-13 10:56:16 -07003413
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003414 return pos - buf;
3415}
3416
Dmitry Shmidt04949592012-07-19 12:16:46 -07003417
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
3419 const char *cmd, char *buf,
3420 size_t buflen)
3421{
3422 u8 bssid[ETH_ALEN];
3423 size_t i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003424 struct wpa_bss *bss;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003425 struct wpa_bss *bsslast = NULL;
3426 struct dl_list *next;
3427 int ret = 0;
3428 int len;
3429 char *ctmp;
3430 unsigned long mask = WPA_BSS_MASK_ALL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003431
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003432 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
3433 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
3434 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003435 list_id);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003436 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
3437 list_id);
3438 } else { /* N1-N2 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003439 unsigned int id1, id2;
3440
3441 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
3442 wpa_printf(MSG_INFO, "Wrong BSS range "
3443 "format");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003444 return 0;
3445 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003446
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003447 if (*(cmd + 6) == '-')
3448 id1 = 0;
3449 else
3450 id1 = atoi(cmd + 6);
3451 ctmp++;
3452 if (*ctmp >= '0' && *ctmp <= '9')
3453 id2 = atoi(ctmp);
3454 else
3455 id2 = (unsigned int) -1;
3456 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
3457 if (id2 == (unsigned int) -1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003458 bsslast = dl_list_last(&wpa_s->bss_id,
3459 struct wpa_bss,
3460 list_id);
3461 else {
3462 bsslast = wpa_bss_get_id(wpa_s, id2);
3463 if (bsslast == NULL && bss && id2 > id1) {
3464 struct wpa_bss *tmp = bss;
3465 for (;;) {
3466 next = tmp->list_id.next;
3467 if (next == &wpa_s->bss_id)
3468 break;
3469 tmp = dl_list_entry(
3470 next, struct wpa_bss,
3471 list_id);
3472 if (tmp->id > id2)
3473 break;
3474 bsslast = tmp;
3475 }
3476 }
3477 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003478 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003479 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003480 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003481 else if (os_strncmp(cmd, "LAST", 4) == 0)
3482 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003483 else if (os_strncmp(cmd, "ID-", 3) == 0) {
3484 i = atoi(cmd + 3);
3485 bss = wpa_bss_get_id(wpa_s, i);
3486 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3487 i = atoi(cmd + 5);
3488 bss = wpa_bss_get_id(wpa_s, i);
3489 if (bss) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003490 next = bss->list_id.next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003491 if (next == &wpa_s->bss_id)
3492 bss = NULL;
3493 else
3494 bss = dl_list_entry(next, struct wpa_bss,
3495 list_id);
3496 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003497#ifdef CONFIG_P2P
3498 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
3499 if (hwaddr_aton(cmd + 13, bssid) == 0)
3500 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
3501 else
3502 bss = NULL;
3503#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003504 } else if (hwaddr_aton(cmd, bssid) == 0)
3505 bss = wpa_bss_get_bssid(wpa_s, bssid);
3506 else {
3507 struct wpa_bss *tmp;
3508 i = atoi(cmd);
3509 bss = NULL;
3510 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
3511 {
3512 if (i-- == 0) {
3513 bss = tmp;
3514 break;
3515 }
3516 }
3517 }
3518
Dmitry Shmidt04949592012-07-19 12:16:46 -07003519 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
3520 mask = strtoul(ctmp + 5, NULL, 0x10);
3521 if (mask == 0)
3522 mask = WPA_BSS_MASK_ALL;
3523 }
3524
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003525 if (bss == NULL)
3526 return 0;
3527
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003528 if (bsslast == NULL)
3529 bsslast = bss;
3530 do {
3531 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
3532 ret += len;
3533 buf += len;
3534 buflen -= len;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003535 if (bss == bsslast) {
3536 if ((mask & WPA_BSS_MASK_DELIM) && len &&
3537 (bss == dl_list_last(&wpa_s->bss_id,
3538 struct wpa_bss, list_id)))
3539 os_snprintf(buf - 5, 5, "####\n");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003540 break;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08003541 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003542 next = bss->list_id.next;
3543 if (next == &wpa_s->bss_id)
3544 break;
3545 bss = dl_list_entry(next, struct wpa_bss, list_id);
3546 } while (bss && len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003547
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07003548 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003549}
3550
3551
3552static int wpa_supplicant_ctrl_iface_ap_scan(
3553 struct wpa_supplicant *wpa_s, char *cmd)
3554{
3555 int ap_scan = atoi(cmd);
3556 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
3557}
3558
3559
3560static int wpa_supplicant_ctrl_iface_scan_interval(
3561 struct wpa_supplicant *wpa_s, char *cmd)
3562{
3563 int scan_int = atoi(cmd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003564 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003565}
3566
3567
3568static int wpa_supplicant_ctrl_iface_bss_expire_age(
3569 struct wpa_supplicant *wpa_s, char *cmd)
3570{
3571 int expire_age = atoi(cmd);
3572 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
3573}
3574
3575
3576static int wpa_supplicant_ctrl_iface_bss_expire_count(
3577 struct wpa_supplicant *wpa_s, char *cmd)
3578{
3579 int expire_count = atoi(cmd);
3580 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
3581}
3582
3583
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07003584static int wpa_supplicant_ctrl_iface_bss_flush(
3585 struct wpa_supplicant *wpa_s, char *cmd)
3586{
3587 int flush_age = atoi(cmd);
3588
3589 if (flush_age == 0)
3590 wpa_bss_flush(wpa_s);
3591 else
3592 wpa_bss_flush_by_age(wpa_s, flush_age);
3593 return 0;
3594}
3595
3596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003597static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
3598{
3599 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
3600 /* MLME-DELETEKEYS.request */
3601 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
3602 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
3603 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
3604 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
3605#ifdef CONFIG_IEEE80211W
3606 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
3607 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
3608#endif /* CONFIG_IEEE80211W */
3609
3610 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
3611 0);
3612 /* MLME-SETPROTECTION.request(None) */
3613 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
3614 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
3615 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3616 wpa_sm_drop_sa(wpa_s->wpa);
3617}
3618
3619
3620static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
3621 char *addr)
3622{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003623#ifdef CONFIG_NO_SCAN_PROCESSING
3624 return -1;
3625#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003626 u8 bssid[ETH_ALEN];
3627 struct wpa_bss *bss;
3628 struct wpa_ssid *ssid = wpa_s->current_ssid;
3629
3630 if (hwaddr_aton(addr, bssid)) {
3631 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
3632 "address '%s'", addr);
3633 return -1;
3634 }
3635
3636 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
3637
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003638 if (!ssid) {
3639 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
3640 "configuration known for the target AP");
3641 return -1;
3642 }
3643
3644 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003645 if (!bss) {
3646 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
3647 "from BSS table");
3648 return -1;
3649 }
3650
3651 /*
3652 * TODO: Find best network configuration block from configuration to
3653 * allow roaming to other networks
3654 */
3655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003656 wpa_s->reassociate = 1;
3657 wpa_supplicant_connect(wpa_s, bss, ssid);
3658
3659 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003660#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003661}
3662
3663
3664#ifdef CONFIG_P2P
3665static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
3666{
3667 unsigned int timeout = atoi(cmd);
3668 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003669 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
3670 char *pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003671 unsigned int search_delay;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003672
3673 if (os_strstr(cmd, "type=social"))
3674 type = P2P_FIND_ONLY_SOCIAL;
3675 else if (os_strstr(cmd, "type=progressive"))
3676 type = P2P_FIND_PROGRESSIVE;
3677
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003678 pos = os_strstr(cmd, "dev_id=");
3679 if (pos) {
3680 pos += 7;
3681 if (hwaddr_aton(pos, dev_id))
3682 return -1;
3683 _dev_id = dev_id;
3684 }
3685
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003686 pos = os_strstr(cmd, "delay=");
3687 if (pos) {
3688 pos += 6;
3689 search_delay = atoi(pos);
3690 } else
3691 search_delay = wpas_p2p_search_delay(wpa_s);
3692
3693 return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
3694 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003695}
3696
3697
3698static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
3699 char *buf, size_t buflen)
3700{
3701 u8 addr[ETH_ALEN];
3702 char *pos, *pos2;
3703 char *pin = NULL;
3704 enum p2p_wps_method wps_method;
3705 int new_pin;
3706 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003707 int persistent_group, persistent_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003708 int join;
3709 int auth;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003710 int automatic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003711 int go_intent = -1;
3712 int freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003713 int pd;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003714 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003715
Dmitry Shmidt04949592012-07-19 12:16:46 -07003716 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
3717 * [persistent|persistent=<network id>]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003718 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003719 * [ht40] [vht] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003720
3721 if (hwaddr_aton(cmd, addr))
3722 return -1;
3723
3724 pos = cmd + 17;
3725 if (*pos != ' ')
3726 return -1;
3727 pos++;
3728
3729 persistent_group = os_strstr(pos, " persistent") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003730 pos2 = os_strstr(pos, " persistent=");
3731 if (pos2) {
3732 struct wpa_ssid *ssid;
3733 persistent_id = atoi(pos2 + 12);
3734 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3735 if (ssid == NULL || ssid->disabled != 2 ||
3736 ssid->mode != WPAS_MODE_P2P_GO) {
3737 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3738 "SSID id=%d for persistent P2P group (GO)",
3739 persistent_id);
3740 return -1;
3741 }
3742 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003743 join = os_strstr(pos, " join") != NULL;
3744 auth = os_strstr(pos, " auth") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003745 automatic = os_strstr(pos, " auto") != NULL;
3746 pd = os_strstr(pos, " provdisc") != NULL;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003747 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
3748 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
3749 vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003750
3751 pos2 = os_strstr(pos, " go_intent=");
3752 if (pos2) {
3753 pos2 += 11;
3754 go_intent = atoi(pos2);
3755 if (go_intent < 0 || go_intent > 15)
3756 return -1;
3757 }
3758
3759 pos2 = os_strstr(pos, " freq=");
3760 if (pos2) {
3761 pos2 += 6;
3762 freq = atoi(pos2);
3763 if (freq <= 0)
3764 return -1;
3765 }
3766
3767 if (os_strncmp(pos, "pin", 3) == 0) {
3768 /* Request random PIN (to be displayed) and enable the PIN */
3769 wps_method = WPS_PIN_DISPLAY;
3770 } else if (os_strncmp(pos, "pbc", 3) == 0) {
3771 wps_method = WPS_PBC;
3772 } else {
3773 pin = pos;
3774 pos = os_strchr(pin, ' ');
3775 wps_method = WPS_PIN_KEYPAD;
3776 if (pos) {
3777 *pos++ = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003778 if (os_strncmp(pos, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779 wps_method = WPS_PIN_DISPLAY;
3780 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003781 if (!wps_pin_str_valid(pin)) {
3782 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
3783 return 17;
3784 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003785 }
3786
3787 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003788 persistent_group, automatic, join,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003789 auth, go_intent, freq, persistent_id, pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003790 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791 if (new_pin == -2) {
3792 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
3793 return 25;
3794 }
3795 if (new_pin == -3) {
3796 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
3797 return 25;
3798 }
3799 if (new_pin < 0)
3800 return -1;
3801 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
3802 ret = os_snprintf(buf, buflen, "%08d", new_pin);
3803 if (ret < 0 || (size_t) ret >= buflen)
3804 return -1;
3805 return ret;
3806 }
3807
3808 os_memcpy(buf, "OK\n", 3);
3809 return 3;
3810}
3811
3812
3813static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
3814{
3815 unsigned int timeout = atoi(cmd);
3816 return wpas_p2p_listen(wpa_s, timeout);
3817}
3818
3819
3820static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
3821{
3822 u8 addr[ETH_ALEN];
3823 char *pos;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003824 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003825
Dmitry Shmidt04949592012-07-19 12:16:46 -07003826 /* <addr> <config method> [join|auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003827
3828 if (hwaddr_aton(cmd, addr))
3829 return -1;
3830
3831 pos = cmd + 17;
3832 if (*pos != ' ')
3833 return -1;
3834 pos++;
3835
Dmitry Shmidt04949592012-07-19 12:16:46 -07003836 if (os_strstr(pos, " join") != NULL)
3837 use = WPAS_P2P_PD_FOR_JOIN;
3838 else if (os_strstr(pos, " auto") != NULL)
3839 use = WPAS_P2P_PD_AUTO;
3840
3841 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003842}
3843
3844
3845static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
3846 size_t buflen)
3847{
3848 struct wpa_ssid *ssid = wpa_s->current_ssid;
3849
3850 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3851 ssid->passphrase == NULL)
3852 return -1;
3853
3854 os_strlcpy(buf, ssid->passphrase, buflen);
3855 return os_strlen(buf);
3856}
3857
3858
3859static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
3860 char *buf, size_t buflen)
3861{
3862 u64 ref;
3863 int res;
3864 u8 dst_buf[ETH_ALEN], *dst;
3865 struct wpabuf *tlvs;
3866 char *pos;
3867 size_t len;
3868
3869 if (hwaddr_aton(cmd, dst_buf))
3870 return -1;
3871 dst = dst_buf;
3872 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
3873 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
3874 dst = NULL;
3875 pos = cmd + 17;
3876 if (*pos != ' ')
3877 return -1;
3878 pos++;
3879
3880 if (os_strncmp(pos, "upnp ", 5) == 0) {
3881 u8 version;
3882 pos += 5;
3883 if (hexstr2bin(pos, &version, 1) < 0)
3884 return -1;
3885 pos += 2;
3886 if (*pos != ' ')
3887 return -1;
3888 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003889 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003890#ifdef CONFIG_WIFI_DISPLAY
3891 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
3892 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
3893#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003894 } else {
3895 len = os_strlen(pos);
3896 if (len & 1)
3897 return -1;
3898 len /= 2;
3899 tlvs = wpabuf_alloc(len);
3900 if (tlvs == NULL)
3901 return -1;
3902 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
3903 wpabuf_free(tlvs);
3904 return -1;
3905 }
3906
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003907 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003908 wpabuf_free(tlvs);
3909 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003910 if (ref == 0)
3911 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
3913 if (res < 0 || (unsigned) res >= buflen)
3914 return -1;
3915 return res;
3916}
3917
3918
3919static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
3920 char *cmd)
3921{
3922 long long unsigned val;
3923 u64 req;
3924 if (sscanf(cmd, "%llx", &val) != 1)
3925 return -1;
3926 req = val;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003927 return wpas_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003928}
3929
3930
3931static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
3932{
3933 int freq;
3934 u8 dst[ETH_ALEN];
3935 u8 dialog_token;
3936 struct wpabuf *resp_tlvs;
3937 char *pos, *pos2;
3938 size_t len;
3939
3940 pos = os_strchr(cmd, ' ');
3941 if (pos == NULL)
3942 return -1;
3943 *pos++ = '\0';
3944 freq = atoi(cmd);
3945 if (freq == 0)
3946 return -1;
3947
3948 if (hwaddr_aton(pos, dst))
3949 return -1;
3950 pos += 17;
3951 if (*pos != ' ')
3952 return -1;
3953 pos++;
3954
3955 pos2 = os_strchr(pos, ' ');
3956 if (pos2 == NULL)
3957 return -1;
3958 *pos2++ = '\0';
3959 dialog_token = atoi(pos);
3960
3961 len = os_strlen(pos2);
3962 if (len & 1)
3963 return -1;
3964 len /= 2;
3965 resp_tlvs = wpabuf_alloc(len);
3966 if (resp_tlvs == NULL)
3967 return -1;
3968 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
3969 wpabuf_free(resp_tlvs);
3970 return -1;
3971 }
3972
3973 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
3974 wpabuf_free(resp_tlvs);
3975 return 0;
3976}
3977
3978
3979static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
3980 char *cmd)
3981{
Dmitry Shmidt04949592012-07-19 12:16:46 -07003982 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
3983 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003984 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
3985 return 0;
3986}
3987
3988
3989static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
3990 char *cmd)
3991{
3992 char *pos;
3993 size_t len;
3994 struct wpabuf *query, *resp;
3995
3996 pos = os_strchr(cmd, ' ');
3997 if (pos == NULL)
3998 return -1;
3999 *pos++ = '\0';
4000
4001 len = os_strlen(cmd);
4002 if (len & 1)
4003 return -1;
4004 len /= 2;
4005 query = wpabuf_alloc(len);
4006 if (query == NULL)
4007 return -1;
4008 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4009 wpabuf_free(query);
4010 return -1;
4011 }
4012
4013 len = os_strlen(pos);
4014 if (len & 1) {
4015 wpabuf_free(query);
4016 return -1;
4017 }
4018 len /= 2;
4019 resp = wpabuf_alloc(len);
4020 if (resp == NULL) {
4021 wpabuf_free(query);
4022 return -1;
4023 }
4024 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4025 wpabuf_free(query);
4026 wpabuf_free(resp);
4027 return -1;
4028 }
4029
4030 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4031 wpabuf_free(query);
4032 wpabuf_free(resp);
4033 return -1;
4034 }
4035 return 0;
4036}
4037
4038
4039static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4040{
4041 char *pos;
4042 u8 version;
4043
4044 pos = os_strchr(cmd, ' ');
4045 if (pos == NULL)
4046 return -1;
4047 *pos++ = '\0';
4048
4049 if (hexstr2bin(cmd, &version, 1) < 0)
4050 return -1;
4051
4052 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4053}
4054
4055
4056static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4057{
4058 char *pos;
4059
4060 pos = os_strchr(cmd, ' ');
4061 if (pos == NULL)
4062 return -1;
4063 *pos++ = '\0';
4064
4065 if (os_strcmp(cmd, "bonjour") == 0)
4066 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4067 if (os_strcmp(cmd, "upnp") == 0)
4068 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4069 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4070 return -1;
4071}
4072
4073
4074static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4075 char *cmd)
4076{
4077 size_t len;
4078 struct wpabuf *query;
4079 int ret;
4080
4081 len = os_strlen(cmd);
4082 if (len & 1)
4083 return -1;
4084 len /= 2;
4085 query = wpabuf_alloc(len);
4086 if (query == NULL)
4087 return -1;
4088 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4089 wpabuf_free(query);
4090 return -1;
4091 }
4092
4093 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4094 wpabuf_free(query);
4095 return ret;
4096}
4097
4098
4099static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4100{
4101 char *pos;
4102 u8 version;
4103
4104 pos = os_strchr(cmd, ' ');
4105 if (pos == NULL)
4106 return -1;
4107 *pos++ = '\0';
4108
4109 if (hexstr2bin(cmd, &version, 1) < 0)
4110 return -1;
4111
4112 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4113}
4114
4115
4116static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4117{
4118 char *pos;
4119
4120 pos = os_strchr(cmd, ' ');
4121 if (pos == NULL)
4122 return -1;
4123 *pos++ = '\0';
4124
4125 if (os_strcmp(cmd, "bonjour") == 0)
4126 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4127 if (os_strcmp(cmd, "upnp") == 0)
4128 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4129 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4130 return -1;
4131}
4132
4133
4134static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4135{
4136 u8 addr[ETH_ALEN];
4137
4138 /* <addr> */
4139
4140 if (hwaddr_aton(cmd, addr))
4141 return -1;
4142
4143 return wpas_p2p_reject(wpa_s, addr);
4144}
4145
4146
4147static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4148{
4149 char *pos;
4150 int id;
4151 struct wpa_ssid *ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004152 u8 *_peer = NULL, peer[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004153 int freq = 0, pref_freq = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004154 int ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004155
4156 id = atoi(cmd);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004157 pos = os_strstr(cmd, " peer=");
4158 if (pos) {
4159 pos += 6;
4160 if (hwaddr_aton(pos, peer))
4161 return -1;
4162 _peer = peer;
4163 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004164 ssid = wpa_config_get_network(wpa_s->conf, id);
4165 if (ssid == NULL || ssid->disabled != 2) {
4166 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4167 "for persistent P2P group",
4168 id);
4169 return -1;
4170 }
4171
Jouni Malinen31be0a42012-08-31 21:20:51 +03004172 pos = os_strstr(cmd, " freq=");
4173 if (pos) {
4174 pos += 6;
4175 freq = atoi(pos);
4176 if (freq <= 0)
4177 return -1;
4178 }
4179
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004180 pos = os_strstr(cmd, " pref=");
4181 if (pos) {
4182 pos += 6;
4183 pref_freq = atoi(pos);
4184 if (pref_freq <= 0)
4185 return -1;
4186 }
4187
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004188 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4189 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4190 vht;
Jouni Malinen31be0a42012-08-31 21:20:51 +03004191
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004192 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
4193 pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004194}
4195
4196
4197static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
4198{
4199 char *pos;
4200 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
4201
4202 pos = os_strstr(cmd, " peer=");
4203 if (!pos)
4204 return -1;
4205
4206 *pos = '\0';
4207 pos += 6;
4208 if (hwaddr_aton(pos, peer)) {
4209 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
4210 return -1;
4211 }
4212
4213 pos = os_strstr(pos, " go_dev_addr=");
4214 if (pos) {
4215 pos += 13;
4216 if (hwaddr_aton(pos, go_dev_addr)) {
4217 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
4218 pos);
4219 return -1;
4220 }
4221 go_dev = go_dev_addr;
4222 }
4223
4224 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
4225}
4226
4227
4228static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
4229{
4230 if (os_strncmp(cmd, "persistent=", 11) == 0)
4231 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
4232 if (os_strncmp(cmd, "group=", 6) == 0)
4233 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
4234
4235 return -1;
4236}
4237
4238
4239static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004240 char *cmd, int freq, int ht40,
4241 int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004242{
4243 int id;
4244 struct wpa_ssid *ssid;
4245
4246 id = atoi(cmd);
4247 ssid = wpa_config_get_network(wpa_s->conf, id);
4248 if (ssid == NULL || ssid->disabled != 2) {
4249 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4250 "for persistent P2P group",
4251 id);
4252 return -1;
4253 }
4254
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004255 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40, vht,
4256 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004257}
4258
4259
4260static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
4261{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004262 int freq = 0, ht40, vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263 char *pos;
4264
4265 pos = os_strstr(cmd, "freq=");
4266 if (pos)
4267 freq = atoi(pos + 5);
4268
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004269 vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
4270 ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4271 vht;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004273 if (os_strncmp(cmd, "persistent=", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004274 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004275 ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004276 if (os_strcmp(cmd, "persistent") == 0 ||
4277 os_strncmp(cmd, "persistent ", 11) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004278 return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004279 if (os_strncmp(cmd, "freq=", 5) == 0)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004280 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004281 if (ht40)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004282 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283
4284 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
4285 cmd);
4286 return -1;
4287}
4288
4289
4290static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
4291 char *buf, size_t buflen)
4292{
4293 u8 addr[ETH_ALEN], *addr_ptr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004294 int next, res;
4295 const struct p2p_peer_info *info;
4296 char *pos, *end;
4297 char devtype[WPS_DEV_TYPE_BUFSIZE];
4298 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004299 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004300
4301 if (!wpa_s->global->p2p)
4302 return -1;
4303
4304 if (os_strcmp(cmd, "FIRST") == 0) {
4305 addr_ptr = NULL;
4306 next = 0;
4307 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4308 if (hwaddr_aton(cmd + 5, addr) < 0)
4309 return -1;
4310 addr_ptr = addr;
4311 next = 1;
4312 } else {
4313 if (hwaddr_aton(cmd, addr) < 0)
4314 return -1;
4315 addr_ptr = addr;
4316 next = 0;
4317 }
4318
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004319 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4320 if (info == NULL)
4321 return -1;
4322
4323 pos = buf;
4324 end = buf + buflen;
4325
4326 res = os_snprintf(pos, end - pos, MACSTR "\n"
4327 "pri_dev_type=%s\n"
4328 "device_name=%s\n"
4329 "manufacturer=%s\n"
4330 "model_name=%s\n"
4331 "model_number=%s\n"
4332 "serial_number=%s\n"
4333 "config_methods=0x%x\n"
4334 "dev_capab=0x%x\n"
4335 "group_capab=0x%x\n"
4336 "level=%d\n",
4337 MAC2STR(info->p2p_device_addr),
4338 wps_dev_type_bin2str(info->pri_dev_type,
4339 devtype, sizeof(devtype)),
4340 info->device_name,
4341 info->manufacturer,
4342 info->model_name,
4343 info->model_number,
4344 info->serial_number,
4345 info->config_methods,
4346 info->dev_capab,
4347 info->group_capab,
4348 info->level);
4349 if (res < 0 || res >= end - pos)
4350 return pos - buf;
4351 pos += res;
4352
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004353 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4354 {
4355 const u8 *t;
4356 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4357 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4358 wps_dev_type_bin2str(t, devtype,
4359 sizeof(devtype)));
4360 if (res < 0 || res >= end - pos)
4361 return pos - buf;
4362 pos += res;
4363 }
4364
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004365 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004366 if (ssid) {
4367 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
4368 if (res < 0 || res >= end - pos)
4369 return pos - buf;
4370 pos += res;
4371 }
4372
4373 res = p2p_get_peer_info_txt(info, pos, end - pos);
4374 if (res < 0)
4375 return pos - buf;
4376 pos += res;
4377
4378 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004379}
4380
4381
Dmitry Shmidt04949592012-07-19 12:16:46 -07004382static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
4383 const char *param)
4384{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004385 unsigned int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004386
4387 if (wpa_s->global->p2p == NULL)
4388 return -1;
4389
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004390 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
4391 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004392
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004393 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
4394 struct wpa_freq_range *freq;
4395 freq = &wpa_s->global->p2p_disallow_freq.range[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004396 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004397 freq->min, freq->max);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004398 }
4399
Dmitry Shmidt04949592012-07-19 12:16:46 -07004400 wpas_p2p_update_channel_list(wpa_s);
4401 return 0;
4402}
4403
4404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004405static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
4406{
4407 char *param;
4408
4409 if (wpa_s->global->p2p == NULL)
4410 return -1;
4411
4412 param = os_strchr(cmd, ' ');
4413 if (param == NULL)
4414 return -1;
4415 *param++ = '\0';
4416
4417 if (os_strcmp(cmd, "discoverability") == 0) {
4418 p2p_set_client_discoverability(wpa_s->global->p2p,
4419 atoi(param));
4420 return 0;
4421 }
4422
4423 if (os_strcmp(cmd, "managed") == 0) {
4424 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
4425 return 0;
4426 }
4427
4428 if (os_strcmp(cmd, "listen_channel") == 0) {
4429 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
4430 atoi(param));
4431 }
4432
4433 if (os_strcmp(cmd, "ssid_postfix") == 0) {
4434 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
4435 os_strlen(param));
4436 }
4437
4438 if (os_strcmp(cmd, "noa") == 0) {
4439 char *pos;
4440 int count, start, duration;
4441 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
4442 count = atoi(param);
4443 pos = os_strchr(param, ',');
4444 if (pos == NULL)
4445 return -1;
4446 pos++;
4447 start = atoi(pos);
4448 pos = os_strchr(pos, ',');
4449 if (pos == NULL)
4450 return -1;
4451 pos++;
4452 duration = atoi(pos);
4453 if (count < 0 || count > 255 || start < 0 || duration < 0)
4454 return -1;
4455 if (count == 0 && duration > 0)
4456 return -1;
4457 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
4458 "start=%d duration=%d", count, start, duration);
4459 return wpas_p2p_set_noa(wpa_s, count, start, duration);
4460 }
4461
4462 if (os_strcmp(cmd, "ps") == 0)
4463 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
4464
4465 if (os_strcmp(cmd, "oppps") == 0)
4466 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
4467
4468 if (os_strcmp(cmd, "ctwindow") == 0)
4469 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
4470
4471 if (os_strcmp(cmd, "disabled") == 0) {
4472 wpa_s->global->p2p_disabled = atoi(param);
4473 wpa_printf(MSG_DEBUG, "P2P functionality %s",
4474 wpa_s->global->p2p_disabled ?
4475 "disabled" : "enabled");
4476 if (wpa_s->global->p2p_disabled) {
4477 wpas_p2p_stop_find(wpa_s);
4478 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4479 p2p_flush(wpa_s->global->p2p);
4480 }
4481 return 0;
4482 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004483
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004484 if (os_strcmp(cmd, "conc_pref") == 0) {
4485 if (os_strcmp(param, "sta") == 0)
4486 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
4487 else if (os_strcmp(param, "p2p") == 0)
4488 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004489 else {
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004490 wpa_printf(MSG_INFO, "Invalid conc_pref value");
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004491 return -1;
4492 }
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004493 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004494 "%s", param);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004495 return 0;
4496 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004498 if (os_strcmp(cmd, "force_long_sd") == 0) {
4499 wpa_s->force_long_sd = atoi(param);
4500 return 0;
4501 }
4502
4503 if (os_strcmp(cmd, "peer_filter") == 0) {
4504 u8 addr[ETH_ALEN];
4505 if (hwaddr_aton(param, addr))
4506 return -1;
4507 p2p_set_peer_filter(wpa_s->global->p2p, addr);
4508 return 0;
4509 }
4510
4511 if (os_strcmp(cmd, "cross_connect") == 0)
4512 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
4513
4514 if (os_strcmp(cmd, "go_apsd") == 0) {
4515 if (os_strcmp(param, "disable") == 0)
4516 wpa_s->set_ap_uapsd = 0;
4517 else {
4518 wpa_s->set_ap_uapsd = 1;
4519 wpa_s->ap_uapsd = atoi(param);
4520 }
4521 return 0;
4522 }
4523
4524 if (os_strcmp(cmd, "client_apsd") == 0) {
4525 if (os_strcmp(param, "disable") == 0)
4526 wpa_s->set_sta_uapsd = 0;
4527 else {
4528 int be, bk, vi, vo;
4529 char *pos;
4530 /* format: BE,BK,VI,VO;max SP Length */
4531 be = atoi(param);
4532 pos = os_strchr(param, ',');
4533 if (pos == NULL)
4534 return -1;
4535 pos++;
4536 bk = atoi(pos);
4537 pos = os_strchr(pos, ',');
4538 if (pos == NULL)
4539 return -1;
4540 pos++;
4541 vi = atoi(pos);
4542 pos = os_strchr(pos, ',');
4543 if (pos == NULL)
4544 return -1;
4545 pos++;
4546 vo = atoi(pos);
4547 /* ignore max SP Length for now */
4548
4549 wpa_s->set_sta_uapsd = 1;
4550 wpa_s->sta_uapsd = 0;
4551 if (be)
4552 wpa_s->sta_uapsd |= BIT(0);
4553 if (bk)
4554 wpa_s->sta_uapsd |= BIT(1);
4555 if (vi)
4556 wpa_s->sta_uapsd |= BIT(2);
4557 if (vo)
4558 wpa_s->sta_uapsd |= BIT(3);
4559 }
4560 return 0;
4561 }
4562
Dmitry Shmidt04949592012-07-19 12:16:46 -07004563 if (os_strcmp(cmd, "disallow_freq") == 0)
4564 return p2p_ctrl_disallow_freq(wpa_s, param);
4565
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004566 if (os_strcmp(cmd, "disc_int") == 0) {
4567 int min_disc_int, max_disc_int, max_disc_tu;
4568 char *pos;
4569
4570 pos = param;
4571
4572 min_disc_int = atoi(pos);
4573 pos = os_strchr(pos, ' ');
4574 if (pos == NULL)
4575 return -1;
4576 *pos++ = '\0';
4577
4578 max_disc_int = atoi(pos);
4579 pos = os_strchr(pos, ' ');
4580 if (pos == NULL)
4581 return -1;
4582 *pos++ = '\0';
4583
4584 max_disc_tu = atoi(pos);
4585
4586 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
4587 max_disc_int, max_disc_tu);
4588 }
4589
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004590 if (os_strcmp(cmd, "per_sta_psk") == 0) {
4591 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
4592 return 0;
4593 }
4594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004595 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
4596 cmd);
4597
4598 return -1;
4599}
4600
4601
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004602static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
4603{
4604 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4605 wpa_s->force_long_sd = 0;
4606 if (wpa_s->global->p2p)
4607 p2p_flush(wpa_s->global->p2p);
4608}
4609
4610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
4612{
4613 char *pos, *pos2;
4614 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
4615
4616 if (cmd[0]) {
4617 pos = os_strchr(cmd, ' ');
4618 if (pos == NULL)
4619 return -1;
4620 *pos++ = '\0';
4621 dur1 = atoi(cmd);
4622
4623 pos2 = os_strchr(pos, ' ');
4624 if (pos2)
4625 *pos2++ = '\0';
4626 int1 = atoi(pos);
4627 } else
4628 pos2 = NULL;
4629
4630 if (pos2) {
4631 pos = os_strchr(pos2, ' ');
4632 if (pos == NULL)
4633 return -1;
4634 *pos++ = '\0';
4635 dur2 = atoi(pos2);
4636 int2 = atoi(pos);
4637 }
4638
4639 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
4640}
4641
4642
4643static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
4644{
4645 char *pos;
4646 unsigned int period = 0, interval = 0;
4647
4648 if (cmd[0]) {
4649 pos = os_strchr(cmd, ' ');
4650 if (pos == NULL)
4651 return -1;
4652 *pos++ = '\0';
4653 period = atoi(cmd);
4654 interval = atoi(pos);
4655 }
4656
4657 return wpas_p2p_ext_listen(wpa_s, period, interval);
4658}
4659
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004660
4661static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
4662{
4663 const char *pos;
4664 u8 peer[ETH_ALEN];
4665 int iface_addr = 0;
4666
4667 pos = cmd;
4668 if (os_strncmp(pos, "iface=", 6) == 0) {
4669 iface_addr = 1;
4670 pos += 6;
4671 }
4672 if (hwaddr_aton(pos, peer))
4673 return -1;
4674
4675 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
4676 return 0;
4677}
4678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004679#endif /* CONFIG_P2P */
4680
4681
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004682#ifdef CONFIG_INTERWORKING
4683static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
4684{
4685 u8 bssid[ETH_ALEN];
4686 struct wpa_bss *bss;
4687
4688 if (hwaddr_aton(dst, bssid)) {
4689 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
4690 return -1;
4691 }
4692
4693 bss = wpa_bss_get_bssid(wpa_s, bssid);
4694 if (bss == NULL) {
4695 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
4696 MAC2STR(bssid));
4697 return -1;
4698 }
4699
4700 return interworking_connect(wpa_s, bss);
4701}
4702
4703
4704static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
4705{
4706 u8 dst_addr[ETH_ALEN];
4707 int used;
4708 char *pos;
4709#define MAX_ANQP_INFO_ID 100
4710 u16 id[MAX_ANQP_INFO_ID];
4711 size_t num_id = 0;
4712
4713 used = hwaddr_aton2(dst, dst_addr);
4714 if (used < 0)
4715 return -1;
4716 pos = dst + used;
4717 while (num_id < MAX_ANQP_INFO_ID) {
4718 id[num_id] = atoi(pos);
4719 if (id[num_id])
4720 num_id++;
4721 pos = os_strchr(pos + 1, ',');
4722 if (pos == NULL)
4723 break;
4724 pos++;
4725 }
4726
4727 if (num_id == 0)
4728 return -1;
4729
4730 return anqp_send_req(wpa_s, dst_addr, id, num_id);
4731}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004732
4733
4734static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
4735{
4736 u8 dst_addr[ETH_ALEN];
4737 struct wpabuf *advproto, *query = NULL;
4738 int used, ret = -1;
4739 char *pos, *end;
4740 size_t len;
4741
4742 used = hwaddr_aton2(cmd, dst_addr);
4743 if (used < 0)
4744 return -1;
4745
4746 pos = cmd + used;
4747 while (*pos == ' ')
4748 pos++;
4749
4750 /* Advertisement Protocol ID */
4751 end = os_strchr(pos, ' ');
4752 if (end)
4753 len = end - pos;
4754 else
4755 len = os_strlen(pos);
4756 if (len & 0x01)
4757 return -1;
4758 len /= 2;
4759 if (len == 0)
4760 return -1;
4761 advproto = wpabuf_alloc(len);
4762 if (advproto == NULL)
4763 return -1;
4764 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
4765 goto fail;
4766
4767 if (end) {
4768 /* Optional Query Request */
4769 pos = end + 1;
4770 while (*pos == ' ')
4771 pos++;
4772
4773 len = os_strlen(pos);
4774 if (len) {
4775 if (len & 0x01)
4776 goto fail;
4777 len /= 2;
4778 if (len == 0)
4779 goto fail;
4780 query = wpabuf_alloc(len);
4781 if (query == NULL)
4782 goto fail;
4783 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
4784 goto fail;
4785 }
4786 }
4787
4788 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
4789
4790fail:
4791 wpabuf_free(advproto);
4792 wpabuf_free(query);
4793
4794 return ret;
4795}
4796
4797
4798static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
4799 size_t buflen)
4800{
4801 u8 addr[ETH_ALEN];
4802 int dialog_token;
4803 int used;
4804 char *pos;
4805 size_t resp_len, start, requested_len;
4806
4807 if (!wpa_s->last_gas_resp)
4808 return -1;
4809
4810 used = hwaddr_aton2(cmd, addr);
4811 if (used < 0)
4812 return -1;
4813
4814 pos = cmd + used;
4815 while (*pos == ' ')
4816 pos++;
4817 dialog_token = atoi(pos);
4818
4819 if (os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) != 0 ||
4820 dialog_token != wpa_s->last_gas_dialog_token)
4821 return -1;
4822
4823 resp_len = wpabuf_len(wpa_s->last_gas_resp);
4824 start = 0;
4825 requested_len = resp_len;
4826
4827 pos = os_strchr(pos, ' ');
4828 if (pos) {
4829 start = atoi(pos);
4830 if (start > resp_len)
4831 return os_snprintf(buf, buflen, "FAIL-Invalid range");
4832 pos = os_strchr(pos, ',');
4833 if (pos == NULL)
4834 return -1;
4835 pos++;
4836 requested_len = atoi(pos);
4837 if (start + requested_len > resp_len)
4838 return os_snprintf(buf, buflen, "FAIL-Invalid range");
4839 }
4840
4841 if (requested_len * 2 + 1 > buflen)
4842 return os_snprintf(buf, buflen, "FAIL-Too long response");
4843
4844 return wpa_snprintf_hex(buf, buflen,
4845 wpabuf_head_u8(wpa_s->last_gas_resp) + start,
4846 requested_len);
4847}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004848#endif /* CONFIG_INTERWORKING */
4849
4850
Dmitry Shmidt04949592012-07-19 12:16:46 -07004851#ifdef CONFIG_HS20
4852
4853static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
4854{
4855 u8 dst_addr[ETH_ALEN];
4856 int used;
4857 char *pos;
4858 u32 subtypes = 0;
4859
4860 used = hwaddr_aton2(dst, dst_addr);
4861 if (used < 0)
4862 return -1;
4863 pos = dst + used;
4864 for (;;) {
4865 int num = atoi(pos);
4866 if (num <= 0 || num > 31)
4867 return -1;
4868 subtypes |= BIT(num);
4869 pos = os_strchr(pos + 1, ',');
4870 if (pos == NULL)
4871 break;
4872 pos++;
4873 }
4874
4875 if (subtypes == 0)
4876 return -1;
4877
4878 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
4879}
4880
4881
4882static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4883 const u8 *addr, const char *realm)
4884{
4885 u8 *buf;
4886 size_t rlen, len;
4887 int ret;
4888
4889 rlen = os_strlen(realm);
4890 len = 3 + rlen;
4891 buf = os_malloc(len);
4892 if (buf == NULL)
4893 return -1;
4894 buf[0] = 1; /* NAI Home Realm Count */
4895 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
4896 buf[2] = rlen;
4897 os_memcpy(buf + 3, realm, rlen);
4898
4899 ret = hs20_anqp_send_req(wpa_s, addr,
4900 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4901 buf, len);
4902
4903 os_free(buf);
4904
4905 return ret;
4906}
4907
4908
4909static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4910 char *dst)
4911{
4912 struct wpa_cred *cred = wpa_s->conf->cred;
4913 u8 dst_addr[ETH_ALEN];
4914 int used;
4915 u8 *buf;
4916 size_t len;
4917 int ret;
4918
4919 used = hwaddr_aton2(dst, dst_addr);
4920 if (used < 0)
4921 return -1;
4922
4923 while (dst[used] == ' ')
4924 used++;
4925 if (os_strncmp(dst + used, "realm=", 6) == 0)
4926 return hs20_nai_home_realm_list(wpa_s, dst_addr,
4927 dst + used + 6);
4928
4929 len = os_strlen(dst + used);
4930
4931 if (len == 0 && cred && cred->realm)
4932 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
4933
4934 if (len % 1)
4935 return -1;
4936 len /= 2;
4937 buf = os_malloc(len);
4938 if (buf == NULL)
4939 return -1;
4940 if (hexstr2bin(dst + used, buf, len) < 0) {
4941 os_free(buf);
4942 return -1;
4943 }
4944
4945 ret = hs20_anqp_send_req(wpa_s, dst_addr,
4946 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4947 buf, len);
4948 os_free(buf);
4949
4950 return ret;
4951}
4952
4953#endif /* CONFIG_HS20 */
4954
4955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004956static int wpa_supplicant_ctrl_iface_sta_autoconnect(
4957 struct wpa_supplicant *wpa_s, char *cmd)
4958{
4959 wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
4960 return 0;
4961}
4962
4963
Dmitry Shmidt04949592012-07-19 12:16:46 -07004964#ifdef CONFIG_AUTOSCAN
4965
4966static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
4967 char *cmd)
4968{
4969 enum wpa_states state = wpa_s->wpa_state;
4970 char *new_params = NULL;
4971
4972 if (os_strlen(cmd) > 0) {
4973 new_params = os_strdup(cmd);
4974 if (new_params == NULL)
4975 return -1;
4976 }
4977
4978 os_free(wpa_s->conf->autoscan);
4979 wpa_s->conf->autoscan = new_params;
4980
4981 if (wpa_s->conf->autoscan == NULL)
4982 autoscan_deinit(wpa_s);
4983 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
4984 autoscan_init(wpa_s, 1);
4985 else if (state == WPA_SCANNING)
4986 wpa_supplicant_reinit_autoscan(wpa_s);
4987
4988 return 0;
4989}
4990
4991#endif /* CONFIG_AUTOSCAN */
4992
4993
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004994#ifdef CONFIG_WNM
4995
4996static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
4997{
4998 int enter;
4999 int intval = 0;
5000 char *pos;
5001 int ret;
5002 struct wpabuf *tfs_req = NULL;
5003
5004 if (os_strncmp(cmd, "enter", 5) == 0)
5005 enter = 1;
5006 else if (os_strncmp(cmd, "exit", 4) == 0)
5007 enter = 0;
5008 else
5009 return -1;
5010
5011 pos = os_strstr(cmd, " interval=");
5012 if (pos)
5013 intval = atoi(pos + 10);
5014
5015 pos = os_strstr(cmd, " tfs_req=");
5016 if (pos) {
5017 char *end;
5018 size_t len;
5019 pos += 9;
5020 end = os_strchr(pos, ' ');
5021 if (end)
5022 len = end - pos;
5023 else
5024 len = os_strlen(pos);
5025 if (len & 1)
5026 return -1;
5027 len /= 2;
5028 tfs_req = wpabuf_alloc(len);
5029 if (tfs_req == NULL)
5030 return -1;
5031 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
5032 wpabuf_free(tfs_req);
5033 return -1;
5034 }
5035 }
5036
5037 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
5038 WNM_SLEEP_MODE_EXIT, intval,
5039 tfs_req);
5040 wpabuf_free(tfs_req);
5041
5042 return ret;
5043}
5044
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005045
5046static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
5047{
5048 int query_reason;
5049
5050 query_reason = atoi(cmd);
5051
5052 wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
5053 query_reason);
5054
5055 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
5056}
5057
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005058#endif /* CONFIG_WNM */
5059
5060
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005061/* Get string representation of channel width */
5062static const char * channel_width_name(enum chan_width width)
5063{
5064 switch (width) {
5065 case CHAN_WIDTH_20_NOHT:
5066 return "20 MHz (no HT)";
5067 case CHAN_WIDTH_20:
5068 return "20 MHz";
5069 case CHAN_WIDTH_40:
5070 return "40 MHz";
5071 case CHAN_WIDTH_80:
5072 return "80 MHz";
5073 case CHAN_WIDTH_80P80:
5074 return "80+80 MHz";
5075 case CHAN_WIDTH_160:
5076 return "160 MHz";
5077 default:
5078 return "unknown";
5079 }
5080}
5081
5082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005083static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
5084 size_t buflen)
5085{
5086 struct wpa_signal_info si;
5087 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005088 char *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005089
5090 ret = wpa_drv_signal_poll(wpa_s, &si);
5091 if (ret)
5092 return -1;
5093
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005094 pos = buf;
5095 end = buf + buflen;
5096
5097 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098 "NOISE=%d\nFREQUENCY=%u\n",
5099 si.current_signal, si.current_txrate / 1000,
5100 si.current_noise, si.frequency);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005101 if (ret < 0 || ret > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005103 pos += ret;
5104
5105 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
5106 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
5107 channel_width_name(si.chanwidth));
5108 if (ret < 0 || ret > end - pos)
5109 return -1;
5110 pos += ret;
5111 }
5112
5113 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
5114 ret = os_snprintf(pos, end - pos,
5115 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
5116 si.center_frq1, si.center_frq2);
5117 if (ret < 0 || ret > end - pos)
5118 return -1;
5119 pos += ret;
5120 }
5121
5122 if (si.avg_signal) {
5123 ret = os_snprintf(pos, end - pos,
5124 "AVG_RSSI=%d\n", si.avg_signal);
5125 if (ret < 0 || ret >= end - pos)
5126 return -1;
5127 pos += ret;
5128 }
5129
5130 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005131}
5132
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005133
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005134static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
5135 size_t buflen)
5136{
5137 struct hostap_sta_driver_data sta;
5138 int ret;
5139
5140 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
5141 if (ret)
5142 return -1;
5143
5144 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005145 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
5146 if (ret < 0 || (size_t) ret > buflen)
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005147 return -1;
5148 return ret;
5149}
5150
5151
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005152#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005153static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5154 char *buf, size_t buflen)
5155{
5156 int ret;
5157
5158 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005159 if (ret == 0) {
5160 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
5161 struct p2p_data *p2p = wpa_s->global->p2p;
5162 if (p2p) {
5163 char country[3];
5164 country[0] = cmd[8];
5165 country[1] = cmd[9];
5166 country[2] = 0x04;
5167 p2p_set_country(p2p, country);
5168 }
5169 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005170 ret = sprintf(buf, "%s\n", "OK");
Dmitry Shmidt9432e122013-09-12 12:39:30 -07005171 }
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005172 return ret;
5173}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005174#endif
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005175
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07005176
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005177static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
5178{
5179 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
5180
5181#ifdef CONFIG_P2P
5182 wpas_p2p_stop_find(wpa_s);
5183 p2p_ctrl_flush(wpa_s);
5184 wpas_p2p_group_remove(wpa_s, "*");
5185#endif /* CONFIG_P2P */
5186
5187#ifdef CONFIG_WPS_TESTING
5188 wps_version_number = 0x20;
5189 wps_testing_dummy_cred = 0;
5190#endif /* CONFIG_WPS_TESTING */
5191#ifdef CONFIG_WPS
5192 wpas_wps_cancel(wpa_s);
5193#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07005194 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005195 wpa_s->known_wps_freq = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005196
5197#ifdef CONFIG_TDLS_TESTING
5198 extern unsigned int tdls_testing;
5199 tdls_testing = 0;
5200#endif /* CONFIG_TDLS_TESTING */
5201#ifdef CONFIG_TDLS
5202 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
5203 wpa_tdls_enable(wpa_s->wpa, 1);
5204#endif /* CONFIG_TDLS */
5205
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005206 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
5207 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
5208
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005209 wpa_s->no_keep_alive = 0;
5210
5211 os_free(wpa_s->disallow_aps_bssid);
5212 wpa_s->disallow_aps_bssid = NULL;
5213 wpa_s->disallow_aps_bssid_count = 0;
5214 os_free(wpa_s->disallow_aps_ssid);
5215 wpa_s->disallow_aps_ssid = NULL;
5216 wpa_s->disallow_aps_ssid_count = 0;
5217
5218 wpa_s->set_sta_uapsd = 0;
5219 wpa_s->sta_uapsd = 0;
5220
5221 wpa_drv_radio_disable(wpa_s, 0);
5222
5223 wpa_bss_flush(wpa_s);
5224 wpa_blacklist_clear(wpa_s);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07005225 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005226 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
5227 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
5228}
5229
5230
Dmitry Shmidt051af732013-10-22 13:52:46 -07005231static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
5232{
5233 struct wpa_supplicant *wpa_s = eloop_ctx;
5234 eapol_sm_notify_ctrl_response(wpa_s->eapol);
5235}
5236
5237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005238char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
5239 char *buf, size_t *resp_len)
5240{
5241 char *reply;
5242 const int reply_size = 4096;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005243 int reply_len;
5244
5245 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005246 os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
5247 os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005248 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005249 os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005250 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
5251 (const u8 *) buf, os_strlen(buf));
5252 } else {
5253 int level = MSG_DEBUG;
5254 if (os_strcmp(buf, "PING") == 0)
5255 level = MSG_EXCESSIVE;
5256 wpa_hexdump_ascii(level, "RX ctrl_iface",
5257 (const u8 *) buf, os_strlen(buf));
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005258 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005259 }
5260
5261 reply = os_malloc(reply_size);
5262 if (reply == NULL) {
5263 *resp_len = 1;
5264 return NULL;
5265 }
5266
5267 os_memcpy(reply, "OK\n", 3);
5268 reply_len = 3;
5269
5270 if (os_strcmp(buf, "PING") == 0) {
5271 os_memcpy(reply, "PONG\n", 5);
5272 reply_len = 5;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005273 } else if (os_strcmp(buf, "IFNAME") == 0) {
5274 reply_len = os_strlen(wpa_s->ifname);
5275 os_memcpy(reply, wpa_s->ifname, reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005276 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
5277 if (wpa_debug_reopen_file() < 0)
5278 reply_len = -1;
5279 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
5280 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
5281 } else if (os_strcmp(buf, "MIB") == 0) {
5282 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
5283 if (reply_len >= 0) {
5284 int res;
5285 res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
5286 reply_size - reply_len);
5287 if (res < 0)
5288 reply_len = -1;
5289 else
5290 reply_len += res;
5291 }
5292 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
5293 reply_len = wpa_supplicant_ctrl_iface_status(
5294 wpa_s, buf + 6, reply, reply_size);
5295 } else if (os_strcmp(buf, "PMKSA") == 0) {
5296 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
5297 reply_size);
5298 } else if (os_strncmp(buf, "SET ", 4) == 0) {
5299 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
5300 reply_len = -1;
5301 } else if (os_strncmp(buf, "GET ", 4) == 0) {
5302 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
5303 reply, reply_size);
5304 } else if (os_strcmp(buf, "LOGON") == 0) {
5305 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5306 } else if (os_strcmp(buf, "LOGOFF") == 0) {
5307 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
5308 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
5309 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5310 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005311 else
5312 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005313 } else if (os_strcmp(buf, "RECONNECT") == 0) {
5314 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5315 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005316 else if (wpa_s->disconnected)
5317 wpas_request_connection(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005318#ifdef IEEE8021X_EAPOL
5319 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
5320 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
5321 reply_len = -1;
5322#endif /* IEEE8021X_EAPOL */
5323#ifdef CONFIG_PEERKEY
5324 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
5325 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
5326 reply_len = -1;
5327#endif /* CONFIG_PEERKEY */
5328#ifdef CONFIG_IEEE80211R
5329 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
5330 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
5331 reply_len = -1;
5332#endif /* CONFIG_IEEE80211R */
5333#ifdef CONFIG_WPS
5334 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
5335 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
5336 if (res == -2) {
5337 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5338 reply_len = 17;
5339 } else if (res)
5340 reply_len = -1;
5341 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
5342 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
5343 if (res == -2) {
5344 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5345 reply_len = 17;
5346 } else if (res)
5347 reply_len = -1;
5348 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
5349 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
5350 reply,
5351 reply_size);
5352 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
5353 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
5354 wpa_s, buf + 14, reply, reply_size);
5355 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
5356 if (wpas_wps_cancel(wpa_s))
5357 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005358#ifdef CONFIG_WPS_NFC
5359 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
5360 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
5361 reply_len = -1;
5362 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
5363 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
5364 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005365 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
5366 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
5367 wpa_s, buf + 21, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005368 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
5369 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
5370 wpa_s, buf + 14, reply, reply_size);
5371 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
5372 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
5373 buf + 17))
5374 reply_len = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005375 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
5376 reply_len = wpas_ctrl_nfc_get_handover_req(
5377 wpa_s, buf + 21, reply, reply_size);
5378 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
5379 reply_len = wpas_ctrl_nfc_get_handover_sel(
5380 wpa_s, buf + 21, reply, reply_size);
5381 } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
5382 reply_len = wpas_ctrl_nfc_rx_handover_req(
5383 wpa_s, buf + 20, reply, reply_size);
5384 } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
5385 if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
5386 reply_len = -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005387 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
5388 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
5389 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005390#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005391 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
5392 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
5393 reply_len = -1;
5394#ifdef CONFIG_AP
5395 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
5396 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
5397 wpa_s, buf + 11, reply, reply_size);
5398#endif /* CONFIG_AP */
5399#ifdef CONFIG_WPS_ER
5400 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
5401 if (wpas_wps_er_start(wpa_s, NULL))
5402 reply_len = -1;
5403 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
5404 if (wpas_wps_er_start(wpa_s, buf + 13))
5405 reply_len = -1;
5406 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
5407 if (wpas_wps_er_stop(wpa_s))
5408 reply_len = -1;
5409 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
5410 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
5411 reply_len = -1;
5412 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
5413 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
5414 if (ret == -2) {
5415 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
5416 reply_len = 17;
5417 } else if (ret == -3) {
5418 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
5419 reply_len = 18;
5420 } else if (ret == -4) {
5421 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
5422 reply_len = 20;
5423 } else if (ret)
5424 reply_len = -1;
5425 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
5426 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
5427 reply_len = -1;
5428 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
5429 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
5430 buf + 18))
5431 reply_len = -1;
5432 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
5433 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
5434 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005435#ifdef CONFIG_WPS_NFC
5436 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
5437 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
5438 wpa_s, buf + 24, reply, reply_size);
5439#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005440#endif /* CONFIG_WPS_ER */
5441#endif /* CONFIG_WPS */
5442#ifdef CONFIG_IBSS_RSN
5443 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
5444 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
5445 reply_len = -1;
5446#endif /* CONFIG_IBSS_RSN */
5447#ifdef CONFIG_P2P
5448 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
5449 if (p2p_ctrl_find(wpa_s, buf + 9))
5450 reply_len = -1;
5451 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
5452 if (p2p_ctrl_find(wpa_s, ""))
5453 reply_len = -1;
5454 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
5455 wpas_p2p_stop_find(wpa_s);
5456 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
5457 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
5458 reply_size);
5459 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
5460 if (p2p_ctrl_listen(wpa_s, buf + 11))
5461 reply_len = -1;
5462 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
5463 if (p2p_ctrl_listen(wpa_s, ""))
5464 reply_len = -1;
5465 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
5466 if (wpas_p2p_group_remove(wpa_s, buf + 17))
5467 reply_len = -1;
5468 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005469 if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005470 reply_len = -1;
5471 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
5472 if (p2p_ctrl_group_add(wpa_s, buf + 14))
5473 reply_len = -1;
5474 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
5475 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
5476 reply_len = -1;
5477 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
5478 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
5479 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
5480 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
5481 reply_size);
5482 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
5483 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
5484 reply_len = -1;
5485 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
5486 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
5487 reply_len = -1;
5488 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
5489 wpas_p2p_sd_service_update(wpa_s);
5490 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
5491 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
5492 reply_len = -1;
5493 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
5494 wpas_p2p_service_flush(wpa_s);
5495 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
5496 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
5497 reply_len = -1;
5498 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
5499 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
5500 reply_len = -1;
5501 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
5502 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
5503 reply_len = -1;
5504 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
5505 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
5506 reply_len = -1;
5507 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
5508 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
5509 reply_size);
5510 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
5511 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
5512 reply_len = -1;
5513 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005514 p2p_ctrl_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005515 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
5516 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
5517 reply_len = -1;
5518 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
5519 if (wpas_p2p_cancel(wpa_s))
5520 reply_len = -1;
5521 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
5522 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
5523 reply_len = -1;
5524 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
5525 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
5526 reply_len = -1;
5527 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
5528 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
5529 reply_len = -1;
5530 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
5531 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
5532 reply_len = -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005533 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
5534 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
5535 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005536#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005537#ifdef CONFIG_WIFI_DISPLAY
5538 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
5539 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
5540 reply_len = -1;
5541 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
5542 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
5543 reply, reply_size);
5544#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005545#ifdef CONFIG_INTERWORKING
5546 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
5547 if (interworking_fetch_anqp(wpa_s) < 0)
5548 reply_len = -1;
5549 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
5550 interworking_stop_fetch_anqp(wpa_s);
5551 } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
5552 if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
5553 NULL) < 0)
5554 reply_len = -1;
5555 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
5556 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
5557 reply_len = -1;
5558 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
5559 if (get_anqp(wpa_s, buf + 9) < 0)
5560 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005561 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
5562 if (gas_request(wpa_s, buf + 12) < 0)
5563 reply_len = -1;
5564 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
5565 reply_len = gas_response_get(wpa_s, buf + 17, reply,
5566 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005567#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005568#ifdef CONFIG_HS20
5569 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
5570 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
5571 reply_len = -1;
5572 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
5573 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
5574 reply_len = -1;
5575#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005576 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
5577 {
5578 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
5579 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
5580 reply_len = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005581 else {
5582 /*
5583 * Notify response from timeout to allow the control
5584 * interface response to be sent first.
5585 */
5586 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
5587 wpa_s, NULL);
5588 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005589 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
5590 if (wpa_supplicant_reload_configuration(wpa_s))
5591 reply_len = -1;
5592 } else if (os_strcmp(buf, "TERMINATE") == 0) {
5593 wpa_supplicant_terminate_proc(wpa_s->global);
5594 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
5595 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
5596 reply_len = -1;
Dmitry Shmidte19501d2011-03-16 14:32:18 -07005597 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005598 reply_len = wpa_supplicant_ctrl_iface_blacklist(
5599 wpa_s, buf + 9, reply, reply_size);
5600 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
5601 reply_len = wpa_supplicant_ctrl_iface_log_level(
5602 wpa_s, buf + 9, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
5604 reply_len = wpa_supplicant_ctrl_iface_list_networks(
5605 wpa_s, reply, reply_size);
5606 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005607#ifdef CONFIG_SME
5608 wpa_s->sme.prev_bssid_set = 0;
5609#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005610 wpa_s->reassociate = 0;
5611 wpa_s->disconnected = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005612 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005613 wpa_supplicant_cancel_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005614 wpa_supplicant_deauthenticate(wpa_s,
5615 WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005616 } else if (os_strcmp(buf, "SCAN") == 0 ||
5617 os_strncmp(buf, "SCAN ", 5) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005618 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
5619 reply_len = -1;
5620 else {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005621 if (os_strlen(buf) > 4 &&
5622 os_strncasecmp(buf + 5, "TYPE=ONLY", 9) == 0)
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08005623 wpa_s->scan_res_handler = scan_only_handler;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005624 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005625 ((wpa_s->wpa_state <= WPA_SCANNING) ||
5626 (wpa_s->wpa_state == WPA_COMPLETED))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005627 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005628 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005629 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005630 wpa_s->known_wps_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005631 wpa_supplicant_req_scan(wpa_s, 0, 0);
5632 } else if (wpa_s->sched_scanning) {
5633 wpa_printf(MSG_DEBUG, "Stop ongoing "
5634 "sched_scan to allow requested "
5635 "full scan to proceed");
5636 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005637 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005638 wpa_supplicant_req_scan(wpa_s, 0, 0);
5639 } else {
5640 wpa_printf(MSG_DEBUG, "Ongoing scan action - "
5641 "reject new request");
5642 reply_len = os_snprintf(reply, reply_size,
5643 "FAIL-BUSY\n");
5644 }
5645 }
5646 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
5647 reply_len = wpa_supplicant_ctrl_iface_scan_results(
5648 wpa_s, reply, reply_size);
5649 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
5650 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
5651 reply_len = -1;
5652 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
5653 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
5654 reply_len = -1;
5655 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
5656 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
5657 reply_len = -1;
5658 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
5659 reply_len = wpa_supplicant_ctrl_iface_add_network(
5660 wpa_s, reply, reply_size);
5661 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
5662 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
5663 reply_len = -1;
5664 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
5665 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
5666 reply_len = -1;
5667 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
5668 reply_len = wpa_supplicant_ctrl_iface_get_network(
5669 wpa_s, buf + 12, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005670 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
5671 reply_len = wpa_supplicant_ctrl_iface_list_creds(
5672 wpa_s, reply, reply_size);
5673 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
5674 reply_len = wpa_supplicant_ctrl_iface_add_cred(
5675 wpa_s, reply, reply_size);
5676 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
5677 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
5678 reply_len = -1;
5679 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
5680 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
5681 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005682#ifndef CONFIG_NO_CONFIG_WRITE
5683 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
5684 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
5685 reply_len = -1;
5686#endif /* CONFIG_NO_CONFIG_WRITE */
5687 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
5688 reply_len = wpa_supplicant_ctrl_iface_get_capability(
5689 wpa_s, buf + 15, reply, reply_size);
5690 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
5691 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
5692 reply_len = -1;
5693 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
5694 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
5695 reply_len = -1;
5696 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
5697 reply_len = wpa_supplicant_global_iface_list(
5698 wpa_s->global, reply, reply_size);
5699 } else if (os_strcmp(buf, "INTERFACES") == 0) {
5700 reply_len = wpa_supplicant_global_iface_interfaces(
5701 wpa_s->global, reply, reply_size);
5702 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
5703 reply_len = wpa_supplicant_ctrl_iface_bss(
5704 wpa_s, buf + 4, reply, reply_size);
5705#ifdef CONFIG_AP
5706 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
5707 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
5708 } else if (os_strncmp(buf, "STA ", 4) == 0) {
5709 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
5710 reply_size);
5711 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
5712 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
5713 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005714 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
5715 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
5716 reply_len = -1;
5717 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
5718 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
5719 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005720#endif /* CONFIG_AP */
5721 } else if (os_strcmp(buf, "SUSPEND") == 0) {
5722 wpas_notify_suspend(wpa_s->global);
5723 } else if (os_strcmp(buf, "RESUME") == 0) {
5724 wpas_notify_resume(wpa_s->global);
5725 } else if (os_strcmp(buf, "DROP_SA") == 0) {
5726 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
5727 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
5728 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
5729 reply_len = -1;
5730 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
5731 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
5732 reply_len = -1;
5733 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
5734 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
5735 reply_len = -1;
5736 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
5737 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
5738 buf + 17))
5739 reply_len = -1;
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07005740 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
5741 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
5742 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005743#ifdef CONFIG_TDLS
5744 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
5745 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
5746 reply_len = -1;
5747 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
5748 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
5749 reply_len = -1;
5750 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
5751 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
5752 reply_len = -1;
5753#endif /* CONFIG_TDLS */
5754 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
5755 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
5756 reply_size);
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07005757 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
5758 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
5759 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005760#ifdef CONFIG_AUTOSCAN
5761 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
5762 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
5763 reply_len = -1;
5764#endif /* CONFIG_AUTOSCAN */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005765#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07005766 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
5767 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
5768 reply_size);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005769#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005770 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005771 pmksa_cache_clear_current(wpa_s->wpa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005772 eapol_sm_request_reauth(wpa_s->eapol);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005773#ifdef CONFIG_WNM
5774 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
5775 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
5776 reply_len = -1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005777 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
5778 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
5779 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005780#endif /* CONFIG_WNM */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005781 } else if (os_strcmp(buf, "FLUSH") == 0) {
5782 wpa_supplicant_ctrl_iface_flush(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005783 } else {
5784 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
5785 reply_len = 16;
5786 }
5787
5788 if (reply_len < 0) {
5789 os_memcpy(reply, "FAIL\n", 5);
5790 reply_len = 5;
5791 }
5792
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005793 *resp_len = reply_len;
5794 return reply;
5795}
5796
5797
5798static int wpa_supplicant_global_iface_add(struct wpa_global *global,
5799 char *cmd)
5800{
5801 struct wpa_interface iface;
5802 char *pos;
5803
5804 /*
5805 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
5806 * TAB<bridge_ifname>
5807 */
5808 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
5809
5810 os_memset(&iface, 0, sizeof(iface));
5811
5812 do {
5813 iface.ifname = pos = cmd;
5814 pos = os_strchr(pos, '\t');
5815 if (pos)
5816 *pos++ = '\0';
5817 if (iface.ifname[0] == '\0')
5818 return -1;
5819 if (pos == NULL)
5820 break;
5821
5822 iface.confname = pos;
5823 pos = os_strchr(pos, '\t');
5824 if (pos)
5825 *pos++ = '\0';
5826 if (iface.confname[0] == '\0')
5827 iface.confname = NULL;
5828 if (pos == NULL)
5829 break;
5830
5831 iface.driver = pos;
5832 pos = os_strchr(pos, '\t');
5833 if (pos)
5834 *pos++ = '\0';
5835 if (iface.driver[0] == '\0')
5836 iface.driver = NULL;
5837 if (pos == NULL)
5838 break;
5839
5840 iface.ctrl_interface = pos;
5841 pos = os_strchr(pos, '\t');
5842 if (pos)
5843 *pos++ = '\0';
5844 if (iface.ctrl_interface[0] == '\0')
5845 iface.ctrl_interface = NULL;
5846 if (pos == NULL)
5847 break;
5848
5849 iface.driver_param = pos;
5850 pos = os_strchr(pos, '\t');
5851 if (pos)
5852 *pos++ = '\0';
5853 if (iface.driver_param[0] == '\0')
5854 iface.driver_param = NULL;
5855 if (pos == NULL)
5856 break;
5857
5858 iface.bridge_ifname = pos;
5859 pos = os_strchr(pos, '\t');
5860 if (pos)
5861 *pos++ = '\0';
5862 if (iface.bridge_ifname[0] == '\0')
5863 iface.bridge_ifname = NULL;
5864 if (pos == NULL)
5865 break;
5866 } while (0);
5867
5868 if (wpa_supplicant_get_iface(global, iface.ifname))
5869 return -1;
5870
5871 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
5872}
5873
5874
5875static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
5876 char *cmd)
5877{
5878 struct wpa_supplicant *wpa_s;
5879
5880 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
5881
5882 wpa_s = wpa_supplicant_get_iface(global, cmd);
5883 if (wpa_s == NULL)
5884 return -1;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07005885 return wpa_supplicant_remove_iface(global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005886}
5887
5888
5889static void wpa_free_iface_info(struct wpa_interface_info *iface)
5890{
5891 struct wpa_interface_info *prev;
5892
5893 while (iface) {
5894 prev = iface;
5895 iface = iface->next;
5896
5897 os_free(prev->ifname);
5898 os_free(prev->desc);
5899 os_free(prev);
5900 }
5901}
5902
5903
5904static int wpa_supplicant_global_iface_list(struct wpa_global *global,
5905 char *buf, int len)
5906{
5907 int i, res;
5908 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
5909 char *pos, *end;
5910
5911 for (i = 0; wpa_drivers[i]; i++) {
5912 struct wpa_driver_ops *drv = wpa_drivers[i];
5913 if (drv->get_interfaces == NULL)
5914 continue;
5915 tmp = drv->get_interfaces(global->drv_priv[i]);
5916 if (tmp == NULL)
5917 continue;
5918
5919 if (last == NULL)
5920 iface = last = tmp;
5921 else
5922 last->next = tmp;
5923 while (last->next)
5924 last = last->next;
5925 }
5926
5927 pos = buf;
5928 end = buf + len;
5929 for (tmp = iface; tmp; tmp = tmp->next) {
5930 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
5931 tmp->drv_name, tmp->ifname,
5932 tmp->desc ? tmp->desc : "");
5933 if (res < 0 || res >= end - pos) {
5934 *pos = '\0';
5935 break;
5936 }
5937 pos += res;
5938 }
5939
5940 wpa_free_iface_info(iface);
5941
5942 return pos - buf;
5943}
5944
5945
5946static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
5947 char *buf, int len)
5948{
5949 int res;
5950 char *pos, *end;
5951 struct wpa_supplicant *wpa_s;
5952
5953 wpa_s = global->ifaces;
5954 pos = buf;
5955 end = buf + len;
5956
5957 while (wpa_s) {
5958 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
5959 if (res < 0 || res >= end - pos) {
5960 *pos = '\0';
5961 break;
5962 }
5963 pos += res;
5964 wpa_s = wpa_s->next;
5965 }
5966 return pos - buf;
5967}
5968
5969
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005970static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
5971 const char *ifname,
5972 char *cmd, size_t *resp_len)
5973{
5974 struct wpa_supplicant *wpa_s;
5975
5976 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5977 if (os_strcmp(ifname, wpa_s->ifname) == 0)
5978 break;
5979 }
5980
5981 if (wpa_s == NULL) {
5982 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
5983 if (resp)
5984 *resp_len = os_strlen(resp);
5985 else
5986 *resp_len = 1;
5987 return resp;
5988 }
5989
5990 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
5991}
5992
5993
5994static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
5995 char *buf, size_t *resp_len)
5996{
5997#ifdef CONFIG_P2P
5998 static const char * cmd[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07005999#ifdef ANDROID_P2P
6000 "LIST_NETWORKS",
6001 "SAVE_CONFIG",
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07006002#endif
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006003 "P2P_FIND",
6004 "P2P_STOP_FIND",
6005 "P2P_LISTEN",
6006 "P2P_GROUP_ADD",
6007 "P2P_GET_PASSPHRASE",
6008 "P2P_SERVICE_UPDATE",
6009 "P2P_SERVICE_FLUSH",
6010 "P2P_FLUSH",
6011 "P2P_CANCEL",
6012 "P2P_PRESENCE_REQ",
6013 "P2P_EXT_LISTEN",
6014 NULL
6015 };
6016 static const char * prefix[] = {
Dmitry Shmidt0c18dcd2013-08-16 15:29:47 -07006017#ifdef ANDROID_P2P
6018 "DRIVER ",
6019 "GET_NETWORK ",
6020 "REMOVE_NETWORK ",
6021 "SET ",
6022#endif
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006023 "P2P_FIND ",
6024 "P2P_CONNECT ",
6025 "P2P_LISTEN ",
6026 "P2P_GROUP_REMOVE ",
6027 "P2P_GROUP_ADD ",
6028 "P2P_PROV_DISC ",
6029 "P2P_SERV_DISC_REQ ",
6030 "P2P_SERV_DISC_CANCEL_REQ ",
6031 "P2P_SERV_DISC_RESP ",
6032 "P2P_SERV_DISC_EXTERNAL ",
6033 "P2P_SERVICE_ADD ",
6034 "P2P_SERVICE_DEL ",
6035 "P2P_REJECT ",
6036 "P2P_INVITE ",
6037 "P2P_PEER ",
6038 "P2P_SET ",
6039 "P2P_UNAUTHORIZE ",
6040 "P2P_PRESENCE_REQ ",
6041 "P2P_EXT_LISTEN ",
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006042 "P2P_REMOVE_CLIENT ",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006043 NULL
6044 };
6045 int found = 0;
6046 int i;
6047
6048 if (global->p2p_init_wpa_s == NULL)
6049 return NULL;
6050
6051 for (i = 0; !found && cmd[i]; i++) {
6052 if (os_strcmp(buf, cmd[i]) == 0)
6053 found = 1;
6054 }
6055
6056 for (i = 0; !found && prefix[i]; i++) {
6057 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
6058 found = 1;
6059 }
6060
6061 if (found)
6062 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6063 buf, resp_len);
6064#endif /* CONFIG_P2P */
6065 return NULL;
6066}
6067
6068
6069static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
6070 char *buf, size_t *resp_len)
6071{
6072#ifdef CONFIG_WIFI_DISPLAY
6073 if (global->p2p_init_wpa_s == NULL)
6074 return NULL;
6075 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
6076 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
6077 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
6078 buf, resp_len);
6079#endif /* CONFIG_WIFI_DISPLAY */
6080 return NULL;
6081}
6082
6083
6084static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
6085 char *buf, size_t *resp_len)
6086{
6087 char *ret;
6088
6089 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
6090 if (ret)
6091 return ret;
6092
6093 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
6094 if (ret)
6095 return ret;
6096
6097 return NULL;
6098}
6099
6100
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006101static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
6102{
6103 char *value;
6104
6105 value = os_strchr(cmd, ' ');
6106 if (value == NULL)
6107 return -1;
6108 *value++ = '\0';
6109
6110 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
6111
6112#ifdef CONFIG_WIFI_DISPLAY
6113 if (os_strcasecmp(cmd, "wifi_display") == 0) {
6114 wifi_display_enable(global, !!atoi(value));
6115 return 0;
6116 }
6117#endif /* CONFIG_WIFI_DISPLAY */
6118
6119 return -1;
6120}
6121
6122
6123#ifndef CONFIG_NO_CONFIG_WRITE
6124static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
6125{
6126 int ret = 0;
6127 struct wpa_supplicant *wpa_s;
6128
6129 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6130 if (!wpa_s->conf->update_config) {
6131 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
6132 continue;
6133 }
6134
6135 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
6136 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
6137 ret = 1;
6138 } else {
6139 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
6140 }
6141 }
6142
6143 return ret;
6144}
6145#endif /* CONFIG_NO_CONFIG_WRITE */
6146
6147
6148static int wpas_global_ctrl_iface_status(struct wpa_global *global,
6149 char *buf, size_t buflen)
6150{
6151 char *pos, *end;
6152 int ret;
6153 struct wpa_supplicant *wpa_s;
6154
6155 pos = buf;
6156 end = buf + buflen;
6157
6158#ifdef CONFIG_P2P
6159 if (global->p2p && !global->p2p_disabled) {
6160 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
6161 "\n"
6162 "p2p_state=%s\n",
6163 MAC2STR(global->p2p_dev_addr),
6164 p2p_get_state_txt(global->p2p));
6165 if (ret < 0 || ret >= end - pos)
6166 return pos - buf;
6167 pos += ret;
6168 } else if (global->p2p) {
6169 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
6170 if (ret < 0 || ret >= end - pos)
6171 return pos - buf;
6172 pos += ret;
6173 }
6174#endif /* CONFIG_P2P */
6175
6176#ifdef CONFIG_WIFI_DISPLAY
6177 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
6178 !!global->wifi_display);
6179 if (ret < 0 || ret >= end - pos)
6180 return pos - buf;
6181 pos += ret;
6182#endif /* CONFIG_WIFI_DISPLAY */
6183
6184 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6185 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
6186 "address=" MACSTR "\n",
6187 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
6188 if (ret < 0 || ret >= end - pos)
6189 return pos - buf;
6190 pos += ret;
6191 }
6192
6193 return pos - buf;
6194}
6195
6196
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006197char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
6198 char *buf, size_t *resp_len)
6199{
6200 char *reply;
6201 const int reply_size = 2048;
6202 int reply_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006203 int level = MSG_DEBUG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006204
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006205 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
6206 char *pos = os_strchr(buf + 7, ' ');
6207 if (pos) {
6208 *pos++ = '\0';
6209 return wpas_global_ctrl_iface_ifname(global,
6210 buf + 7, pos,
6211 resp_len);
6212 }
6213 }
6214
6215 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
6216 if (reply)
6217 return reply;
6218
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006219 if (os_strcmp(buf, "PING") == 0)
6220 level = MSG_EXCESSIVE;
6221 wpa_hexdump_ascii(level, "RX global ctrl_iface",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006222 (const u8 *) buf, os_strlen(buf));
6223
6224 reply = os_malloc(reply_size);
6225 if (reply == NULL) {
6226 *resp_len = 1;
6227 return NULL;
6228 }
6229
6230 os_memcpy(reply, "OK\n", 3);
6231 reply_len = 3;
6232
6233 if (os_strcmp(buf, "PING") == 0) {
6234 os_memcpy(reply, "PONG\n", 5);
6235 reply_len = 5;
6236 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
6237 if (wpa_supplicant_global_iface_add(global, buf + 14))
6238 reply_len = -1;
6239 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
6240 if (wpa_supplicant_global_iface_remove(global, buf + 17))
6241 reply_len = -1;
6242 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
6243 reply_len = wpa_supplicant_global_iface_list(
6244 global, reply, reply_size);
6245 } else if (os_strcmp(buf, "INTERFACES") == 0) {
6246 reply_len = wpa_supplicant_global_iface_interfaces(
6247 global, reply, reply_size);
6248 } else if (os_strcmp(buf, "TERMINATE") == 0) {
6249 wpa_supplicant_terminate_proc(global);
6250 } else if (os_strcmp(buf, "SUSPEND") == 0) {
6251 wpas_notify_suspend(global);
6252 } else if (os_strcmp(buf, "RESUME") == 0) {
6253 wpas_notify_resume(global);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006254 } else if (os_strncmp(buf, "SET ", 4) == 0) {
6255 if (wpas_global_ctrl_iface_set(global, buf + 4))
6256 reply_len = -1;
6257#ifndef CONFIG_NO_CONFIG_WRITE
6258 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
6259 if (wpas_global_ctrl_iface_save_config(global))
6260 reply_len = -1;
6261#endif /* CONFIG_NO_CONFIG_WRITE */
6262 } else if (os_strcmp(buf, "STATUS") == 0) {
6263 reply_len = wpas_global_ctrl_iface_status(global, reply,
6264 reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006265 } else {
6266 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
6267 reply_len = 16;
6268 }
6269
6270 if (reply_len < 0) {
6271 os_memcpy(reply, "FAIL\n", 5);
6272 reply_len = 5;
6273 }
6274
6275 *resp_len = reply_len;
6276 return reply;
6277}